Crossfire JXClient, Trunk  R20561
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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.map;
23 
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28 
39 public class CfMapSquare {
40 
44  public static final int DEFAULT_DARKNESS = 255;
45 
49  public static final int DEFAULT_SMOOTH = 0;
50 
54  public static final int DEFAULT_COLOR = -1;
55 
59  public static final int DARKNESS_FULL_BRIGHT = 255;
60 
64  @Nullable
65  public static final Face DEFAULT_FACE = null;
66 
70  @NotNull
71  private final CfMap map;
72 
76  private final int x;
77 
81  private final int y;
82 
87  private boolean fogOfWar;
88 
93  private int darkness = DEFAULT_DARKNESS;
94 
99  private int color = DEFAULT_COLOR;
100 
104  @NotNull
105  private final Face[] faces = new Face[Map2.NUM_LAYERS];
106 
111  @NotNull
112  private final CfMapSquare[] heads = new CfMapSquare[Map2.NUM_LAYERS];
113 
117  @NotNull
118  private final int[] 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  if (faces[layer] != DEFAULT_FACE || heads[layer] != null || smooths[layer] != 0) {
173  break;
174  }
175  }
176  if (layer >= faces.length) {
177  return;
178  }
179  }
180 
181  fogOfWar = true;
182  dirty();
183  }
184 
191  public boolean setDarkness(final int darkness) {
192  final boolean result = fogOfWar;
193  final boolean markDirty = fogOfWar || this.darkness != darkness;
194  fogOfWar = false;
195  this.darkness = darkness;
196  if (markDirty) {
197  dirty();
198  }
199  return result;
200  }
201 
206  public int getDarkness() {
207  return darkness;
208  }
209 
217  public int setSmooth(final int layer, final int smooth) {
218  final boolean fogOfWarCleared = fogOfWar;
219  final boolean smoothChanged = smooths[layer] != smooth;
220  smooths[layer] = smooth;
221  final boolean markDirty = fogOfWar || smoothChanged;
222  fogOfWar = false;
223  if (markDirty) {
224  dirty();
225  }
226  return (fogOfWarCleared ? 1 : 0)|(smoothChanged ? 2 : 0);
227  }
228 
234  public int getSmooth(final int layer) {
235  return smooths[layer];
236  }
237 
243  public boolean setColor(final int color) {
244  final boolean result = fogOfWar;
245  final boolean markDirty = fogOfWar || this.color != color;
246  fogOfWar = false;
247  this.color = color;
248  if (markDirty) {
249  dirty();
250  }
251  return result;
252  }
253 
258  public int getColor() {
259  return color;
260  }
261 
268  public void setFace(final int layer, @Nullable final Face face) {
269  if (faces[layer] != face) {
270  faces[layer] = face;
271  dirty();
272  }
273  }
274 
280  @Nullable
281  public Face getFace(final int layer) {
282  return faces[layer];
283  }
284 
294  public void setHeadMapSquare(final int layer, @Nullable final CfMapSquare mapSquare, final boolean setAlways) {
295  if (heads[layer] != mapSquare && (setAlways || heads[layer] == null || heads[layer].fogOfWar)) {
296  heads[layer] = mapSquare;
297  dirty();
298  }
299  }
300 
307  @Nullable
308  public CfMapSquare getHeadMapSquare(final int layer) {
309  // suppress parts of fog-of-war objects if this square is not
310  // fog-of-war
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 
343  @NotNull
344  @Override
345  public String toString() {
346  return x+"/"+y;
347  }
348 
349 }
CfMapSquare getHeadMapSquare(final int layer)
Returns the map square of the head of a multi-square object.
Represents a square in a CfMap.
int getSmooth(final int layer)
Returns the smooth value of this square.
static final int DEFAULT_DARKNESS
The default darkness value for newly created squares.
Represents a map (as seen by the client).
Definition: CfMap.java:45
void setFace(final int layer, @Nullable final Face face)
Sets the face of a layer.
int getX()
Returns the absolute map x-coordinate of this square.
void dirty()
Marks this square as dirty, i.e., needing redraw.
int getDarkness()
Returns the darkness value of this square.
final int y
The absolute y-coordinate of this square in its CfMap.
int darkness
The darkness value of the square in the range [0..255].
Manages image information ("faces") needed to display the map view, items, and spell icons...
boolean fogOfWar
Flag used to defer clearing the values: when a.
int getY()
Returns the absolute map y-coordinate of this square.
int NUM_LAYERS
The total number of map layers to display.
Definition: Map2.java:33
static final int DEFAULT_COLOR
The default magic map color for newly created squares.
Interface defining constants for the "map2" Crossfire protocol message.
Definition: Map2.java:28
static final int DEFAULT_SMOOTH
The default smooth value for newly created squares.
final int [] smooths
The smooth values of all layers as sent by the server.
boolean setColor(final int color)
Sets the magic map color of this square.
void squareModified(@NotNull final CfMapSquare mapSquare)
Marks a CfMapSquare as dirty.
Definition: CfMap.java:732
boolean isFogOfWar()
Determines if the square is not up-to-date.
static final Face DEFAULT_FACE
The default face value for newly creates squares.
final CfMapSquare [] heads
If this square contains a non-head part of a multi-square object this points to the head square...
void clear()
Marks this square as &#39;fog-og-war&#39;.
CfMapSquare(@NotNull final CfMap map, final int x, final int y)
Creates a new (empty) square.
int setSmooth(final int layer, final int smooth)
Sets the smooth value of this square.
boolean setDarkness(final int darkness)
Sets the darkness value of this square.
final int x
The absolute x-coordinate of this square in its CfMap.
Face getFace(final int layer)
Returns the face of a layer.
final Face [] faces
The faces (of head-parts) of all layers as sent by the server.
int color
The magic map color of the square.
int getColor()
Returns the magic map color of this square.
boolean resetFogOfWar()
Returns and resets the "fog-of-war" flag.
void setHeadMapSquare(final int layer, @Nullable final CfMapSquare mapSquare, final boolean setAlways)
Sets the map square containing the head face for a layer.
final CfMap map
The CfMap this map square is part of.
static final int DARKNESS_FULL_BRIGHT
The darkness value for a full bright square.