00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.map;
00023
00024 import com.realtime.crossfire.jxclient.faces.Face;
00025 import com.realtime.crossfire.jxclient.server.crossfire.messages.Map2;
00026 import org.jetbrains.annotations.NotNull;
00027 import org.jetbrains.annotations.Nullable;
00028
00039 public class CfMapSquare {
00040
00044 public static final int DEFAULT_DARKNESS = 255;
00045
00049 public static final int DEFAULT_SMOOTH = 0;
00050
00054 public static final int DEFAULT_COLOR = -1;
00055
00059 public static final int DARKNESS_FULL_BRIGHT = 255;
00060
00064 @Nullable
00065 public static final Face DEFAULT_FACE = null;
00066
00070 @NotNull
00071 private final CfMap map;
00072
00076 private final int x;
00077
00081 private final int y;
00082
00088 private boolean fogOfWar = false;
00089
00094 private int darkness = DEFAULT_DARKNESS;
00095
00100 private int color = DEFAULT_COLOR;
00101
00105 @NotNull
00106 private final Face[] faces = new Face[Map2.NUM_LAYERS];
00107
00112 @NotNull
00113 private final CfMapSquare[] heads = new CfMapSquare[Map2.NUM_LAYERS];
00114
00118 @NotNull
00119 private final int[] smooths = new int[Map2.NUM_LAYERS];
00120
00129 public CfMapSquare(@NotNull final CfMap map, final int x, final int y) {
00130 this.map = map;
00131 this.x = x;
00132 this.y = y;
00133 }
00134
00139 public int getX() {
00140 return x;
00141 }
00142
00147 public int getY() {
00148 return y;
00149 }
00150
00154 public void dirty() {
00155 map.squareModified(this);
00156 }
00157
00162 public void clear() {
00163 if (fogOfWar) {
00164 return;
00165 }
00166
00167
00168
00169
00170 if (darkness == DEFAULT_DARKNESS) {
00171 int layer;
00172 for (layer = 0; layer < faces.length; layer++) {
00173 if (faces[layer] != DEFAULT_FACE || heads[layer] != null || smooths[layer] != 0) {
00174 break;
00175 }
00176 }
00177 if (layer >= faces.length) {
00178 return;
00179 }
00180 }
00181
00182 fogOfWar = true;
00183 dirty();
00184 }
00185
00192 public boolean setDarkness(final int darkness) {
00193 final boolean result = fogOfWar;
00194 final boolean markDirty = fogOfWar || this.darkness != darkness;
00195 fogOfWar = false;
00196 this.darkness = darkness;
00197 if (markDirty) {
00198 dirty();
00199 }
00200 return result;
00201 }
00202
00207 public int getDarkness() {
00208 return darkness;
00209 }
00210
00218 public int setSmooth(final int layer, final int smooth) {
00219 final boolean fogOfWarCleared = fogOfWar;
00220 final boolean smoothChanged = smooths[layer] != smooth;
00221 smooths[layer] = smooth;
00222 final boolean markDirty = fogOfWar || smoothChanged;
00223 fogOfWar = false;
00224 if (markDirty) {
00225 dirty();
00226 }
00227 return (fogOfWarCleared ? 1 : 0)|(smoothChanged ? 2 : 0);
00228 }
00229
00235 public int getSmooth(final int layer) {
00236 return smooths[layer];
00237 }
00238
00244 public boolean setColor(final int color) {
00245 final boolean result = fogOfWar;
00246 final boolean markDirty = fogOfWar || this.color != color;
00247 fogOfWar = false;
00248 this.color = color;
00249 if (markDirty) {
00250 dirty();
00251 }
00252 return result;
00253 }
00254
00259 public int getColor() {
00260 return color;
00261 }
00262
00269 public void setFace(final int layer, @Nullable final Face face) {
00270 if (faces[layer] != face) {
00271 faces[layer] = face;
00272 dirty();
00273 }
00274 }
00275
00281 @Nullable
00282 public Face getFace(final int layer) {
00283 return faces[layer];
00284 }
00285
00295 public void setHeadMapSquare(final int layer, @Nullable final CfMapSquare mapSquare, final boolean setAlways) {
00296 if (heads[layer] != mapSquare && (setAlways || heads[layer] == null || heads[layer].isFogOfWar())) {
00297 heads[layer] = mapSquare;
00298 dirty();
00299 }
00300 }
00301
00308 @Nullable
00309 public CfMapSquare getHeadMapSquare(final int layer) {
00310
00311
00312 if (heads[layer] != null && !fogOfWar && heads[layer].fogOfWar) {
00313 return null;
00314 }
00315
00316 return heads[layer];
00317 }
00318
00323 public boolean isFogOfWar() {
00324 return fogOfWar;
00325 }
00326
00331 public boolean resetFogOfWar() {
00332 if (!fogOfWar) {
00333 return false;
00334 }
00335
00336 fogOfWar = false;
00337 dirty();
00338 return true;
00339 }
00340
00344 @NotNull
00345 @Override
00346 public String toString() {
00347 return x+"/"+y;
00348 }
00349
00350 }