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-2015 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;
72  mapGrid = newMapGrid(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  if (x < mapSize.getWidth() && y < mapSize.getHeight()) {
164  newGrid.get(x).set(y, mapGrid.get(x).get(y));
165  } else {
166  newGrid.get(x).set(y, new MapSquare<>(mapModel, x, y));
167  }
168  }
169  }
170 
171  // replace old grid by new one
172  mapSize = newSize;
173  mapGrid = newGrid;
174  }
175 
184  public void collectHeads(final int minX, final int minY, final int maxX, final int maxY, @NotNull final Collection<GameObject<G, A, R>> objectsToDelete) {
185  for (int x = minX; x < maxX; x++) {
186  for (int y = minY; y < maxY; y++) {
187  for (final GameObject<G, A, R> node : mapGrid.get(x).get(y)) {
188  objectsToDelete.add(node.getHead());
189  }
190  }
191  }
192  }
193 
198  @NotNull
199  public Size2D getMapSize() {
200  return mapSize;
201  }
202 
203 }
static final long serialVersionUID
The serial version UID.
final MapModel< G, A, R > mapModel
The associated MapModel.
List< List< MapSquare< G, A, R > > > mapGrid
The MapSquare of this grid.
void resize(@NotNull final Size2D newSize)
Resizes the map grid to a new size.
boolean isEmpty()
Returns whether the map is empty.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
GameObjects are the objects based on Archetypes found on maps.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
void clearMap()
This implementation is very safe by recreating every single MapSquare as new empty square with the tr...
MapSquareGrid(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Size2D mapSize)
Creates a new instance.
MapSquare< G, A, R > getMapSquare(final int x, final int y)
Returns the MapSquare at a given location.
A rectangular grid of MapSquare instances.
List< List< MapSquare< G, A, R > > > newMapGrid(@NotNull final Size2D mapSize)
Allocates a new 2-dimensional MapSquare array of the given size.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
Size2D getMapSize()
Returns the size of this map grid in map squares.
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.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30