Gridarta Editor
MapSquare.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapmodel;
21 
22 import java.awt.Point;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
33 
45 public class MapSquare<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends GameObjectContainer<G, A, R> {
46 
50  private static final long serialVersionUID = 1L;
51 
55  @NotNull
56  private final MapModel<G, A, R> mapModel;
57 
61  private final int mapX;
62 
66  private final int mapY;
67 
72  private int lightRadius;
73 
79  @NotNull
80  private List<MapSquare<G, A, R>> lightSources = Collections.emptyList();
81 
88  public MapSquare(@NotNull final MapModel<G, A, R> mapModel, final int mapX, final int mapY) {
89  this.mapModel = mapModel;
90  this.mapX = mapX;
91  this.mapY = mapY;
92  }
93 
98  @NotNull
100  return mapModel;
101  }
102 
107  public int getMapX() {
108  return mapX;
109  }
110 
115  public int getMapY() {
116  return mapY;
117  }
118 
123  @NotNull
124  public Point getMapLocation() {
125  return new Point(mapX, mapY);
126  }
127 
132  public void getMapLocation(@NotNull final Point pos) {
133  pos.x = mapX;
134  pos.y = mapY;
135  }
136 
143  public void getMapLocation(@NotNull final Point pos, final int dx, final int dy) {
144  pos.x = mapX + dx;
145  pos.y = mapY + dy;
146  }
147 
148  @Override
149  @NotNull
151  return this;
152  }
153 
154  @Override
155  @NotNull
157  return this;
158  }
159 
160  @Override
161  protected void notifyBeginChange() {
163  }
164 
165  @Override
166  protected void notifyEndChange() {
168  }
169 
170  @NotNull
171  @Override
172  @SuppressWarnings("unchecked")
173  protected MapSquare<G, A, R> clone() {
174  //noinspection UnnecessaryLocalVariable
176  return clone;
177  }
178 
179  @Override
180  protected void setThisContainer(@NotNull final G gameObject) {
181  gameObject.setContainer(this, mapX, mapY);
182  }
183 
184  @Nullable
185  @Override
186  public G asGameObject() {
187  return null;
188  }
189 
195  @Nullable
196  public G getLast(@NotNull final GameObjectMatcher gameObjectMatcher) {
197  for (final G gameObject : reverse()) {
198  if (gameObjectMatcher.isMatching(gameObject)) {
199  return gameObject;
200  }
201  }
202  return null;
203  }
204 
212  @Nullable
213  public G getAfterLast(@NotNull final GameObjectMatcher gameObjectMatcher) {
214  G prev = null;
215  for (final G gameObject : reverse()) {
216  if (gameObjectMatcher.isMatching(gameObject)) {
217  return prev;
218  }
219  prev = gameObject;
220  }
221  return null;
222  }
223 
229  @Nullable
230  public G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher) {
231  for (final G gameObject : this) {
232  if (gameObjectMatcher.isMatching(gameObject)) {
233  return gameObject;
234  }
235  }
236  return null;
237  }
238 
246  @Nullable
247  public G getBeforeFirst(@NotNull final GameObjectMatcher gameObjectMatcher) {
248  G prev = null;
249  for (final G gameObject : this) {
250  if (gameObjectMatcher.isMatching(gameObject)) {
251  return prev;
252  }
253  prev = gameObject;
254  }
255  return null;
256  }
257 
265  @Nullable
266  public G getLastOfLeadingSpan(@NotNull final GameObjectMatcher gameObjectMatcher) {
267  @Nullable G result = null;
268  for (final G tmp : this) {
269  if (!gameObjectMatcher.isMatching(tmp)) {
270  break;
271  }
272  result = tmp;
273  }
274  return result;
275  }
276 
280  public void beginSquareChange() {
282  }
283 
287  public void endSquareChange() {
289  }
290 
294  @Override
295  public boolean equals(@Nullable final Object obj) {
296  if (obj == null || obj.getClass() != getClass()) {
297  return false;
298  }
299 
300  final MapSquare<?, ?, ?> mapSquare = (MapSquare<?, ?, ?>) obj;
301  return mapModel == mapSquare.mapModel && mapX == mapSquare.mapX && mapY == mapSquare.mapY;
302  }
303 
307  @Override
308  public int hashCode() {
309  return System.identityHashCode(mapModel) + 13 * mapX + 65537 * mapY;
310  }
311 
315  @NotNull
316  @Override
317  public String toString() {
318  return mapModel.getMapArchObject().getMapName() + ":" + mapX + "/" + mapY;
319  }
320 
327  public int getLightRadius() {
328  return lightRadius;
329  }
330 
337  public void setLightRadius(final int lightRadius) {
338  if (this.lightRadius == lightRadius) {
339  return;
340  }
341 
343  try {
344  this.lightRadius = lightRadius;
345  } finally {
346  notifyEndChange();
347  }
348  }
349 
354  public void addLightSource(@NotNull final MapSquare<G, A, R> mapSquare) {
355  if (lightSources.isEmpty()) {
356  lightSources = new ArrayList<>();
357  }
358 
360  try {
361  lightSources.add(mapSquare);
362  } finally {
363  notifyEndChange();
364  }
365  }
366 
371  public void removeLightSource(@NotNull final MapSquare<G, A, R> mapSquare) {
373  try {
374  if (!lightSources.remove(mapSquare)) {
375  assert false;
376  }
377  if (lightSources.isEmpty()) {
378  lightSources = Collections.emptyList();
379  }
380  } finally {
381  notifyEndChange();
382  }
383  }
384 
391  public boolean isLight() {
392  return !lightSources.isEmpty();
393  }
394 
395 }
net.sf.gridarta.model.mapmodel.MapSquare.getMapY
int getMapY()
Returns the y coordinate on the map.
Definition: MapSquare.java:115
net.sf.gridarta.model.mapmodel.MapModel< G, A, R >
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.model.mapmodel.MapSquare.getMapSquareOptional
MapSquare< G, A, R > getMapSquareOptional()
Returns the MapSquare this game object is part of.
Definition: MapSquare.java:156
net.sf.gridarta.model.mapmodel.MapSquare.getLast
G getLast(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the last occurrence of a matching game object.
Definition: MapSquare.java:196
net.sf.gridarta.model.mapmodel.MapSquare.getMapLocation
Point getMapLocation()
Returns the coordinate on the map.
Definition: MapSquare.java:124
net.sf.gridarta.model.mapmodel.MapSquare.mapY
final int mapY
The Y Coordinate of this map square within the model's grid.
Definition: MapSquare.java:66
net.sf.gridarta.model.mapmodel.MapSquare.getMapX
int getMapX()
Returns the x coordinate on the map.
Definition: MapSquare.java:107
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquare.getFirst
G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the first occurrence of a matching game object.
Definition: MapSquare.java:230
net.sf.gridarta.model.mapmodel.MapSquare.lightRadius
int lightRadius
The maximum light radius of all objects within this map square.
Definition: MapSquare.java:72
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf
net.sf.gridarta.model.mapmodel.MapSquare.removeLightSource
void removeLightSource(@NotNull final MapSquare< G, A, R > mapSquare)
Removes a light emitting game object that affects this map square.
Definition: MapSquare.java:371
net.sf.gridarta.model.mapmodel.MapModel.beginSquareChange
void beginSquareChange(@NotNull MapSquare< G, A, R > mapSquare)
Method to notify the model that a map square is about to change.
net.sf.gridarta.model.mapmodel.MapSquare.notifyEndChange
void notifyEndChange()
Notifies the map model that this container has changed.
Definition: MapSquare.java:166
net.sf.gridarta.model.mapmodel.MapSquare.notifyBeginChange
void notifyBeginChange()
Notifies the map model that this container is about to change.
Definition: MapSquare.java:161
net.sf.gridarta.model.match.GameObjectMatcher
Interface for classes that match GameObjects.
Definition: GameObjectMatcher.java:30
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapmodel.MapSquare.endSquareChange
void endSquareChange()
Method to notify the model that a map square was changed.
Definition: MapSquare.java:287
net.sf.gridarta.model.mapmodel.MapModel.endSquareChange
void endSquareChange(@NotNull MapSquare< G, A, R > mapSquare)
Method to notify the model that a map square was changed.
net.sf.gridarta.model.mapmodel.MapSquare.lightSources
List< MapSquare< G, A, R > > lightSources
The MapSquares on the map that contain light emitting game objects that affect this map square.
Definition: MapSquare.java:80
net.sf.gridarta.model.mapmodel.MapSquare.getMapLocation
void getMapLocation(@NotNull final Point pos, final int dx, final int dy)
Returns the coordinate with an offset on the map.
Definition: MapSquare.java:143
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.model.mapmodel.MapSquare.toString
String toString()
}
Definition: MapSquare.java:317
net.sf.gridarta.model.mapmodel.MapSquare.mapX
final int mapX
The X Coordinate of this map square within the model's grid.
Definition: MapSquare.java:61
net.sf.gridarta.model.mapmodel.MapSquare.getLightRadius
int getLightRadius()
Returns the maximum light radius of all light emitting objects within this map square.
Definition: MapSquare.java:327
net.sf.gridarta.model.match
Classes related to matching {GameObjects}, so called { net.sf.gridarta.model.match....
Definition: AndGameObjectMatcher.java:20
net.sf.gridarta.model.baseobject.GameObjectContainer.reverse
transient Iterable< G > reverse
Iterable implementation for reverse traversal.
Definition: GameObjectContainer.java:75
net.sf.gridarta.model.mapmodel.MapSquare.getBeforeFirst
G getBeforeFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the game object before the first occurrence of a matching game object.
Definition: MapSquare.java:247
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapmodel.MapSquare.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: MapSquare.java:50
net.sf.gridarta.model.mapmodel.MapSquare.equals
boolean equals(@Nullable final Object obj)
}
Definition: MapSquare.java:295
net.sf.gridarta.model.mapmodel.MapSquare.isLight
boolean isLight()
Returns whether this map square is affected by any light emitting game objects.
Definition: MapSquare.java:391
net.sf.gridarta.model.mapmodel.MapSquare.setLightRadius
void setLightRadius(final int lightRadius)
Sets the maximum light radius of all light emitting objects within this map square.
Definition: MapSquare.java:337
net.sf.gridarta.model.mapmodel.MapSquare.hashCode
int hashCode()
}
Definition: MapSquare.java:308
net.sf.gridarta.model.mapmodel.MapSquare.getMapModel
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
Definition: MapSquare.java:99
net.sf.gridarta.model.mapmodel.MapSquare.asGameObject
G asGameObject()
Returns this instance as a GameObject or.
Definition: MapSquare.java:186
net.sf.gridarta.model.mapmodel.MapSquare.mapModel
final MapModel< G, A, R > mapModel
The MaoModel this square is associated with.
Definition: MapSquare.java:56
net.sf.gridarta.model.mapmodel.MapSquare.MapSquare
MapSquare(@NotNull final MapModel< G, A, R > mapModel, final int mapX, final int mapY)
Creates a new instance.
Definition: MapSquare.java:88
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.mapmodel.MapSquare.getMapLocation
void getMapLocation(@NotNull final Point pos)
Returns the coordinate on the map.
Definition: MapSquare.java:132
net.sf.gridarta.model.mapmodel.MapSquare.setThisContainer
void setThisContainer(@NotNull final G gameObject)
Sets a GameObject's container to this container.
Definition: MapSquare.java:180
net.sf.gridarta.model.mapmodel.MapSquare.getAfterLast
G getAfterLast(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the game object after the last occurrence of a matching game object.
Definition: MapSquare.java:213
net.sf.gridarta.model.mapmodel.MapSquare.addLightSource
void addLightSource(@NotNull final MapSquare< G, A, R > mapSquare)
Adds a light emitting game object that affects this map square.
Definition: MapSquare.java:354
net.sf.gridarta.model.mapmodel.MapSquare.getLastOfLeadingSpan
G getLastOfLeadingSpan(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the last game object of the initial segment of matching game objects.
Definition: MapSquare.java:266
net.sf.gridarta.model.mapmodel.MapSquare.clone
MapSquare< G, A, R > clone()
Definition: MapSquare.java:173
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.baseobject.GameObjectContainer
Base class for classes that contain GameObjects as children in the sense of containment.
Definition: GameObjectContainer.java:50
net.sf.gridarta.model.mapmodel.MapSquare.beginSquareChange
void beginSquareChange()
Method to notify the model that a map square is about to change.
Definition: MapSquare.java:280
net.sf.gridarta.model.mapmodel.MapSquare.getMapSquare
MapSquare< G, A, R > getMapSquare()
Returns the MapSquare this game object is part of.
Definition: MapSquare.java:150