Crossfire JXClient, Trunk
CfMapSquare.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.map;
24 
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
40 public class CfMapSquare {
41 
45  public static final int DEFAULT_DARKNESS = 255;
46 
50  public static final int DEFAULT_SMOOTH = 0;
51 
55  public static final int DEFAULT_COLOR = -1;
56 
60  public static final int DARKNESS_FULL_BRIGHT = 255;
61 
65  @Nullable
66  public static final Face DEFAULT_FACE = null;
67 
71  @NotNull
72  private final CfMap map;
73 
77  private final int x;
78 
82  private final int y;
83 
88  private boolean fogOfWar;
89 
94  private int darkness = DEFAULT_DARKNESS;
95 
100  private int color = DEFAULT_COLOR;
101 
105  @Nullable
106  private final Face @NotNull [] faces = new Face[Map2.NUM_LAYERS];
107 
112  @NotNull
113  private final CfMapSquare @NotNull [] heads = new CfMapSquare[Map2.NUM_LAYERS];
114 
118  private final int @NotNull [] smooths = new int[Map2.NUM_LAYERS];
119 
128  public CfMapSquare(@NotNull final CfMap map, final int x, final int y) {
129  this.map = map;
130  this.x = x;
131  this.y = y;
132  }
133 
138  public int getX() {
139  return x;
140  }
141 
146  public int getY() {
147  return y;
148  }
149 
153  public void dirty() {
154  map.squareModified(this);
155  }
156 
161  public void clear() {
162  if (fogOfWar) {
163  return;
164  }
165 
166  // need to check individual values because the server sometimes sends a
167  // "clear" command for already cleared squares; without this check the
168  // black square would be displayed as fog-of-war
169  if (darkness == DEFAULT_DARKNESS) {
170  int layer;
171  for (layer = 0; layer < faces.length; layer++) {
172  //noinspection ConstantConditions
173  if (faces[layer] != DEFAULT_FACE || heads[layer] != null || smooths[layer] != 0) {
174  break;
175  }
176  }
177  if (layer >= faces.length) {
178  return;
179  }
180  }
181 
182  fogOfWar = true;
183  dirty();
184  }
185 
192  public boolean setDarkness(final int darkness) {
193  final boolean result = fogOfWar;
194  final boolean markDirty = fogOfWar || this.darkness != darkness;
195  fogOfWar = false;
196  this.darkness = darkness;
197  if (markDirty) {
198  dirty();
199  }
200  return result;
201  }
202 
207  public int getDarkness() {
208  return darkness;
209  }
210 
218  public int setSmooth(final int layer, final int smooth) {
219  final boolean fogOfWarCleared = fogOfWar;
220  final boolean smoothChanged = smooths[layer] != smooth;
221  smooths[layer] = smooth;
222  final boolean markDirty = fogOfWar || smoothChanged;
223  fogOfWar = false;
224  if (markDirty) {
225  dirty();
226  }
227  return (fogOfWarCleared ? 1 : 0)|(smoothChanged ? 2 : 0);
228  }
229 
235  public int getSmooth(final int layer) {
236  return smooths[layer];
237  }
238 
243  public void setColor(final int color) {
244  final boolean markDirty = this.color != color;
245  this.color = color;
246  if (markDirty) {
247  dirty();
248  }
249  }
250 
255  public int getColor() {
256  return color;
257  }
258 
265  public void setFace(final int layer, @Nullable final Face face) {
266  if (faces[layer] != face) {
267  faces[layer] = face;
268  dirty();
269  }
270  }
271 
277  @Nullable
278  public Face getFace(final int layer) {
279  return faces[layer];
280  }
281 
291  public void setHeadMapSquare(final int layer, @Nullable final CfMapSquare mapSquare, final boolean setAlways) {
292  //noinspection ConstantConditions
293  if (heads[layer] != mapSquare && (setAlways || heads[layer] == null || heads[layer].fogOfWar)) {
294  //noinspection ConstantConditions
295  heads[layer] = mapSquare;
296  dirty();
297  }
298  }
299 
306  @Nullable
307  public CfMapSquare getHeadMapSquare(final int layer) {
308  // suppress parts of fog-of-war objects if this square is not
309  // fog-of-war
310  //noinspection ConstantConditions
311  if (heads[layer] != null && !fogOfWar && heads[layer].fogOfWar) {
312  return null;
313  }
314 
315  return heads[layer];
316  }
317 
322  public boolean isFogOfWar() {
323  return fogOfWar;
324  }
325 
330  public boolean resetFogOfWar() {
331  if (!fogOfWar) {
332  return false;
333  }
334 
335  fogOfWar = false;
336  dirty();
337  return true;
338  }
339 
340  @NotNull
341  @Override
342  public String toString() {
343  return x+"/"+y;
344  }
345 
346 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.map.CfMapSquare.darkness
int darkness
Definition: CfMapSquare.java:94
com.realtime.crossfire.jxclient.map.CfMapSquare.DEFAULT_DARKNESS
static final int DEFAULT_DARKNESS
Definition: CfMapSquare.java:45
com.realtime.crossfire.jxclient.map.CfMapSquare.getSmooth
int getSmooth(final int layer)
Definition: CfMapSquare.java:235
com.realtime.crossfire.jxclient.map.CfMapSquare.fogOfWar
boolean fogOfWar
Definition: CfMapSquare.java:88
com.realtime.crossfire.jxclient.map.CfMapSquare.map
final CfMap map
Definition: CfMapSquare.java:72
com.realtime.crossfire.jxclient.map.CfMapSquare.color
int color
Definition: CfMapSquare.java:100
com.realtime.crossfire.jxclient.map.CfMapSquare.smooths
final int[] smooths
Definition: CfMapSquare.java:118
com.realtime.crossfire.jxclient.map.CfMapSquare.dirty
void dirty()
Definition: CfMapSquare.java:153
com.realtime.crossfire.jxclient.map.CfMapSquare.setDarkness
boolean setDarkness(final int darkness)
Definition: CfMapSquare.java:192
com.realtime.crossfire.jxclient.map.CfMap
Definition: CfMap.java:46
com.realtime.crossfire.jxclient.faces
Definition: AbstractFaceQueue.java:23
com.realtime.crossfire.jxclient.map.CfMapSquare.getDarkness
int getDarkness()
Definition: CfMapSquare.java:207
com.realtime.crossfire.jxclient.map.CfMapSquare.DEFAULT_FACE
static final Face DEFAULT_FACE
Definition: CfMapSquare.java:66
com.realtime.crossfire.jxclient.map.CfMapSquare.getY
int getY()
Definition: CfMapSquare.java:146
com.realtime.crossfire.jxclient.map.CfMapSquare.DARKNESS_FULL_BRIGHT
static final int DARKNESS_FULL_BRIGHT
Definition: CfMapSquare.java:60
com.realtime.crossfire.jxclient.protocol
Definition: MagicMap.java:23
com.realtime.crossfire.jxclient.map.CfMapSquare.getX
int getX()
Definition: CfMapSquare.java:138
com.realtime.crossfire.jxclient.protocol.Map2.NUM_LAYERS
int NUM_LAYERS
Definition: Map2.java:34
com.realtime.crossfire.jxclient.map.CfMapSquare.setSmooth
int setSmooth(final int layer, final int smooth)
Definition: CfMapSquare.java:218
com.realtime.crossfire.jxclient.map.CfMapSquare.clear
void clear()
Definition: CfMapSquare.java:161
com.realtime.crossfire.jxclient.map.CfMapSquare.resetFogOfWar
boolean resetFogOfWar()
Definition: CfMapSquare.java:330
com.realtime.crossfire.jxclient.map.CfMapSquare.setHeadMapSquare
void setHeadMapSquare(final int layer, @Nullable final CfMapSquare mapSquare, final boolean setAlways)
Definition: CfMapSquare.java:291
com.realtime.crossfire.jxclient.map.CfMap.squareModified
void squareModified(@NotNull final CfMapSquare mapSquare)
Definition: CfMap.java:728
com.realtime.crossfire.jxclient.protocol.Map2
Definition: Map2.java:29
com.realtime.crossfire.jxclient.map.CfMapSquare.getFace
Face getFace(final int layer)
Definition: CfMapSquare.java:278
com.realtime.crossfire.jxclient.map.CfMapSquare.CfMapSquare
CfMapSquare(@NotNull final CfMap map, final int x, final int y)
Definition: CfMapSquare.java:128
com.realtime.crossfire.jxclient.map.CfMapSquare.y
final int y
Definition: CfMapSquare.java:82
com.realtime.crossfire.jxclient.map.CfMapSquare.getHeadMapSquare
CfMapSquare getHeadMapSquare(final int layer)
Definition: CfMapSquare.java:307
com.realtime.crossfire
com.realtime.crossfire.jxclient.map.CfMapSquare.heads
final CfMapSquare[] heads
Definition: CfMapSquare.java:113
com.realtime.crossfire.jxclient.map.CfMapSquare.faces
final Face[] faces
Definition: CfMapSquare.java:106
com.realtime
com
com.realtime.crossfire.jxclient.map.CfMapSquare.DEFAULT_SMOOTH
static final int DEFAULT_SMOOTH
Definition: CfMapSquare.java:50
com.realtime.crossfire.jxclient.map.CfMapSquare.isFogOfWar
boolean isFogOfWar()
Definition: CfMapSquare.java:322
com.realtime.crossfire.jxclient.map.CfMapSquare.toString
String toString()
Definition: CfMapSquare.java:342
com.realtime.crossfire.jxclient.map.CfMapSquare.getColor
int getColor()
Definition: CfMapSquare.java:255
com.realtime.crossfire.jxclient.map.CfMapSquare.x
final int x
Definition: CfMapSquare.java:77
com.realtime.crossfire.jxclient.map.CfMapSquare.setFace
void setFace(final int layer, @Nullable final Face face)
Definition: CfMapSquare.java:265
com.realtime.crossfire.jxclient.map.CfMapSquare.DEFAULT_COLOR
static final int DEFAULT_COLOR
Definition: CfMapSquare.java:55
com.realtime.crossfire.jxclient.faces.Face
Definition: Face.java:37
com.realtime.crossfire.jxclient.map.CfMapSquare
Definition: CfMapSquare.java:40
com.realtime.crossfire.jxclient.map.CfMapSquare.setColor
void setColor(final int color)
Definition: CfMapSquare.java:243