Gridarta Editor
SavedSquares.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.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
33 import net.sf.gridarta.utils.Size2D;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class SavedSquares<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Serializable {
42 
46  private static final long serialVersionUID = 1L;
47 
52  @NotNull
53  private final List<ArrayList<ArrayList<G>>> savedSquares = new ArrayList<>();
54 
58  @NotNull
60 
64  @NotNull
66 
73  this.gameObjectFactory = gameObjectFactory;
74  this.gameObjectMatchers = gameObjectMatchers;
75  }
76 
82  public void recordMapSquare(@NotNull final MapSquare<G, A, R> mapSquare) {
83  final int x = mapSquare.getMapX();
84  final int y = mapSquare.getMapY();
85  final ArrayList<G> list = allocateMapSquare(x, y);
86  if (list == null) {
87  return;
88  }
89 
90  for (final G content : mapSquare) {
91  if (content.isHead()) {
93  }
94  }
95  list.trimToSize();
96  }
97 
105  @Nullable
106  private ArrayList<G> allocateMapSquare(final int x, final int y) {
107  final ArrayList<ArrayList<G>> col;
108  if (x >= savedSquares.size()) {
109  while (savedSquares.size() < x) {
110  savedSquares.add(null);
111  }
112  col = new ArrayList<>();
113  savedSquares.add(col);
114  } else {
115  final ArrayList<ArrayList<G>> tmp = savedSquares.get(x);
116  if (tmp == null) {
117  col = new ArrayList<>();
118  savedSquares.set(x, col);
119  } else {
120  col = tmp;
121  }
122  }
123 
124  final ArrayList<G> result;
125  if (y >= col.size()) {
126  while (col.size() < y) {
127  col.add(null);
128  }
129  result = new ArrayList<>();
130  col.add(result);
131  } else {
132  final Collection<G> tmp = col.get(y);
133  if (tmp != null) {
134  return null;
135  }
136 
137  result = new ArrayList<>();
138  col.set(y, result);
139  }
140 
141  return result;
142  }
143 
148  public boolean isEmpty() {
149  return savedSquares.isEmpty();
150  }
151 
155  public void clear() {
156  savedSquares.clear();
157  }
158 
164  @NotNull
167  clone.savedSquares.addAll(savedSquares);
168  savedSquares.clear();
169  return clone;
170  }
171 
176  public void applyChanges(@NotNull final MapModel<G, A, R> mapModel) {
177  final Point point = new Point();
178  point.x = 0;
179  final Collection<G> objectsToDelete = new ArrayList<>();
180  for (final Iterable<ArrayList<G>> col : savedSquares) {
181  if (col != null) {
182  point.y = 0;
183  for (final Iterable<G> square : col) {
184  if (square != null) {
185  final GameObjectContainer<G, A, R> mapSquare = mapModel.getMapSquare(point);
186  for (final G gameObject : mapSquare) {
187  if (gameObject.isHead()) {
188  objectsToDelete.add(gameObject);
189  }
190  }
191  for (final G gameObject : objectsToDelete) {
192  mapModel.removeGameObject(gameObject, false);
193  }
194  objectsToDelete.clear();
195  for (final G gameObject : square) {
196  mapSquare.addLast(gameObject);
197  }
198  }
199  point.y++;
200  }
201  }
202  point.x++;
203  }
204 
205  final Point point2 = new Point();
206  point.x = 0;
207  for (final Iterable<ArrayList<G>> col : savedSquares) {
208  if (col != null) {
209  point.y = 0;
210  for (final Iterable<G> square : col) {
211  if (square != null) {
212  for (final GameObject<G, A, R> gameObject : square) {
213  final Point map = gameObject.getMapLocation();
214  for (G tailGameObject = gameObject.getMultiNext(); tailGameObject != null; tailGameObject = tailGameObject.getMultiNext()) {
215  point2.x = map.x + tailGameObject.getArchetype().getMultiX();
216  point2.y = map.y + tailGameObject.getArchetype().getMultiY();
217  final GameObjectContainer<G, A, R> mapSquare = mapModel.getMapSquare(point2);
218  mapSquare.addLast(tailGameObject);
219  }
220  }
221  }
222  point.y++;
223  }
224  }
225  point.x++;
226  }
227  }
228 
233  public void removeEmptySquares(@NotNull final Size2D size) {
234  final int width = size.getWidth();
235  final int height = size.getHeight();
236 
237  if (!isOutsideEmpty(width, height)) {
238  throw new IllegalArgumentException("area outside " + width + "x" + height + " is not empty");
239  }
240 
241  for (int x = 0; x < width && x < savedSquares.size(); x++) {
242  final List<ArrayList<G>> col = savedSquares.get(x);
243  if (col != null) {
244  for (int y = col.size() - 1; y >= height; y--) {
245  col.remove(y);
246  }
247  }
248  }
249  for (int x = savedSquares.size() - 1; x >= width; x--) {
250  savedSquares.remove(x);
251  }
252  }
253 
260  private boolean isOutsideEmpty(final int width, final int height) {
261  for (int x = 0; x < width && x < savedSquares.size(); x++) {
262  final List<ArrayList<G>> col = savedSquares.get(x);
263  if (col != null) {
264  for (int y = height; y < col.size(); y++) {
265  final Collection<G> square = col.get(y);
266  if (!square.isEmpty()) {
267  return false;
268  }
269  }
270  }
271  }
272 
273  for (int x = width; x < savedSquares.size(); x++) {
274  final Iterable<ArrayList<G>> col = savedSquares.get(x);
275  if (col != null) {
276  for (final Collection<G> square : col) {
277  if (square != null && !square.isEmpty()) {
278  return false;
279  }
280  }
281  }
282  }
283 
284  return true;
285  }
286 
287 }
net.sf.gridarta.model.mapmodel.MapModel< G, A, R >
net.sf.gridarta.model.gameobject.GameObjectFactory
Abstract factory for creating GameObject instances.
Definition: GameObjectFactory.java:31
net.sf.gridarta.model.mapmodel.SavedSquares.savedSquares
final List< ArrayList< ArrayList< G > > > savedSquares
The saved squares.
Definition: SavedSquares.java:53
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquare< G, A, R >
net.sf
net.sf.gridarta.model.mapmodel.SavedSquares.allocateMapSquare
ArrayList< G > allocateMapSquare(final int x, final int y)
Allocates a saved map square.
Definition: SavedSquares.java:106
net.sf.gridarta.model.mapmodel.SavedSquares.gameObjectFactory
final GameObjectFactory< G, A, R > gameObjectFactory
The GameObjectFactory to use.
Definition: SavedSquares.java:59
net.sf.gridarta.model.mapmodel.SavedSquares.SavedSquares
SavedSquares(@NotNull final GameObjectFactory< G, A, R > gameObjectFactory, @NotNull final GameObjectMatchers gameObjectMatchers)
Creates a new instance.
Definition: SavedSquares.java:72
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.SavedSquares
Records a set of changed map squares.
Definition: SavedSquares.java:41
net.sf.gridarta.model.mapmodel.SavedSquares.applyChanges
void applyChanges(@NotNull final MapModel< G, A, R > mapModel)
Applies the saved squares to the given map model.
Definition: SavedSquares.java:176
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.SavedSquares.clear
void clear()
Forgets all saved squares.
Definition: SavedSquares.java:155
net.sf.gridarta.model.baseobject.GameObjectContainer.addLast
void addLast(@NotNull final G gameObject)
Adds the given game object at the end of this container.
Definition: GameObjectContainer.java:413
net.sf.gridarta.model.mapmodel.SavedSquares.recordMapSquare
void recordMapSquare(@NotNull final MapSquare< G, A, R > mapSquare)
Records a map square as changed.
Definition: SavedSquares.java:82
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.mapmodel.SavedSquares.isOutsideEmpty
boolean isOutsideEmpty(final int width, final int height)
Returns whether all squares outside a given area are empty or unchanged.
Definition: SavedSquares.java:260
list
This document describes some hints and requirements for general development on the CrossfireEditor If you plan to make changes to the editor code or setup please read the following and keep it in derived from a basic editor application called Gridder by Pasi Ker�nen so please communicate with best through the cf devel mailing list
Definition: Developer_README.txt:13
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.match.GameObjectMatchers
Maintains GameObjectMatcher instances.
Definition: GameObjectMatchers.java:40
net.sf.gridarta.model.mapmodel.SavedSquares.removeEmptySquares
void removeEmptySquares(@NotNull final Size2D size)
Removes empty squares outside a given area.
Definition: SavedSquares.java:233
net.sf.gridarta.model.mapmodel.SavedSquares.cloneAndClear
SavedSquares< G, A, R > cloneAndClear()
Creates a new instance having the same contents as this instance, then forgets all saves squares in t...
Definition: SavedSquares.java:165
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.mapmodel.SavedSquares.isEmpty
boolean isEmpty()
Returns whether no saved squares exist.
Definition: SavedSquares.java:148
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.gameobject.GameObjectFactory.cloneMultiGameObject
G cloneMultiGameObject(@NotNull G gameObject)
Creates a copy of a game object, including tail parts.
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.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.model.baseobject.GameObjectContainer.getMapSquare
abstract MapSquare< G, A, R > getMapSquare()
Returns the MapSquare this game object is part of.
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.mapmodel.SavedSquares.gameObjectMatchers
final GameObjectMatchers gameObjectMatchers
The GameObjectMatchers to use.
Definition: SavedSquares.java:65
net.sf.gridarta.model.mapmodel.SavedSquares.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: SavedSquares.java:46