Gridarta Editor
MapSquareGrid.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.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
29 import net.sf.gridarta.utils.Size2D;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class MapSquareGrid<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements Serializable {
37 
41  private static final long serialVersionUID = 1L;
42 
47  @NotNull
48  private final MapModel<G, A, R> mapModel;
49 
54  @NotNull
55  private Size2D mapSize;
56 
61  @NotNull
62  private List<List<MapSquare<G, A, R>>> mapGrid;
63 
69  public MapSquareGrid(@NotNull final MapModel<G, A, R> mapModel, @NotNull final Size2D mapSize) {
70  this.mapModel = mapModel;
71  this.mapSize = mapSize;
73  for (int y = 0; y < mapSize.getHeight(); y++) {
74  for (int x = 0; x < mapSize.getWidth(); x++) {
75  mapGrid.get(x).set(y, new MapSquare<>(mapModel, x, y));
76  }
77  }
78  }
79 
85  @NotNull
86  private List<List<MapSquare<G, A, R>>> newMapGrid(@NotNull final Size2D mapSize) {
87  final ArrayList<List<MapSquare<G, A, R>>> result = new ArrayList<>(mapSize.getWidth());
88  for (int x = 0; x < mapSize.getWidth(); x++) {
89  final ArrayList<MapSquare<G, A, R>> column = new ArrayList<>(mapSize.getHeight());
90  for (int y = 0; y < mapSize.getHeight(); y++) {
91  column.add(null);
92  }
93  column.trimToSize();
94  result.add(column);
95  }
96  result.trimToSize();
97  return result;
98  }
99 
107  @NotNull
108  public MapSquare<G, A, R> getMapSquare(final int x, final int y) {
109  return mapGrid.get(x).get(y);
110  }
111 
119  public void clearMap() {
120  for (int x = 0; x < mapSize.getWidth(); x++) {
121  for (int y = 0; y < mapSize.getHeight(); y++) {
122  final List<MapSquare<G, A, R>> column = mapGrid.get(x);
123  if (column.get(y) == null) {
124  column.set(y, new MapSquare<>(mapModel, x, y));
125  } else if (!column.get(y).isEmpty()) {
126  column.get(y).beginSquareChange();
127  try {
128  column.set(y, new MapSquare<>(mapModel, x, y));
129  } finally {
130  column.get(y).endSquareChange();
131  }
132  }
133  }
134  }
135  }
136 
142  public boolean isEmpty() {
143  for (int x = 0; x < mapSize.getWidth(); x++) {
144  for (int y = 0; y < mapSize.getHeight(); y++) {
145  if (!mapGrid.get(x).get(y).isEmpty()) {
146  return false;
147  }
148  }
149  }
150  return true;
151  }
152 
157  public void resize(@NotNull final Size2D newSize) {
158  final List<List<MapSquare<G, A, R>>> newGrid = newMapGrid(newSize);
159 
160  // relink all arches to the new grid
161  for (int x = 0; x < newSize.getWidth(); x++) {
162  for (int y = 0; y < newSize.getHeight(); y++) {
163  newGrid.get(x).set(y, x < mapSize.getWidth() && y < mapSize.getHeight() ? mapGrid.get(x).get(y) : new MapSquare<>(mapModel, x, y));
164  }
165  }
166 
167  // replace old grid by new one
168  mapSize = newSize;
169  mapGrid = newGrid;
170  }
171 
180  public void collectHeads(final int minX, final int minY, final int maxX, final int maxY, @NotNull final Collection<GameObject<G, A, R>> objectsToDelete) {
181  for (int x = minX; x < maxX; x++) {
182  for (int y = minY; y < maxY; y++) {
183  for (final GameObject<G, A, R> node : mapGrid.get(x).get(y)) {
184  objectsToDelete.add(node.getHead());
185  }
186  }
187  }
188  }
189 
194  @NotNull
195  public Size2D getMapSize() {
196  return mapSize;
197  }
198 
199 }
net.sf.gridarta.model.mapmodel.MapSquareGrid.newMapGrid
List< List< MapSquare< G, A, R > > > newMapGrid(@NotNull final Size2D mapSize)
Allocates a new 2-dimensional MapSquare array of the given size.
Definition: MapSquareGrid.java:86
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.mapmodel.MapSquareGrid.getMapSize
Size2D getMapSize()
Returns the size of this map grid in map squares.
Definition: MapSquareGrid.java:195
net.sf.gridarta.model.mapmodel.MapModel< G, A, R >
net.sf.gridarta.model.mapmodel.MapSquareGrid.resize
void resize(@NotNull final Size2D newSize)
Resizes the map grid to a new size.
Definition: MapSquareGrid.java:157
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf
net.sf.gridarta.model.mapmodel.MapSquareGrid.clearMap
void clearMap()
This implementation is very safe by recreating every single MapSquare as new empty square with the tr...
Definition: MapSquareGrid.java:119
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.MapSquareGrid.getMapSquare
MapSquare< G, A, R > getMapSquare(final int x, final int y)
Returns the MapSquare at a given location.
Definition: MapSquareGrid.java:108
net.sf.gridarta.model.mapmodel.MapSquareGrid.MapSquareGrid
MapSquareGrid(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Size2D mapSize)
Creates a new instance.
Definition: MapSquareGrid.java:69
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.model.mapmodel.MapSquareGrid.isEmpty
boolean isEmpty()
Returns whether the map is empty.
Definition: MapSquareGrid.java:142
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.model.mapmodel.MapSquareGrid.collectHeads
void collectHeads(final int minX, final int minY, final int maxX, final int maxY, @NotNull final Collection< GameObject< G, A, R >> objectsToDelete)
Adds all head parts for game object within an area to a collection.
Definition: MapSquareGrid.java:180
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapmodel.MapSquareGrid.mapSize
Size2D mapSize
The size of mapGrid.
Definition: MapSquareGrid.java:55
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.mapmodel.MapSquareGrid.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: MapSquareGrid.java:41
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapmodel.MapSquareGrid.mapGrid
List< List< MapSquare< G, A, R > > > mapGrid
The MapSquare of this grid.
Definition: MapSquareGrid.java:62
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.mapmodel.MapSquareGrid.mapModel
final MapModel< G, A, R > mapModel
The associated MapModel.
Definition: MapSquareGrid.java:48
net.sf.gridarta.model.mapmodel.MapSquareGrid
A rectangular grid of MapSquare instances.
Definition: MapSquareGrid.java:36