Gridarta Editor
TestMapModelHelper.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.awt.Point;
23 import java.util.regex.Pattern;
36 import net.sf.gridarta.utils.Size2D;
38 import org.jetbrains.annotations.NotNull;
39 import org.junit.Assert;
40 
45 public class TestMapModelHelper {
46 
50  public static final int EXIT_TYPE = 1;
51 
55  private static final int FLOOR_TYPE = 2;
56 
60  private static final int MOB_TYPE = 3;
61 
65  @NotNull
66  private static final String[] EMPTY_STRING_ARRAY = new String[0];
67 
71  @NotNull
73 
77  @NotNull
79 
83  @NotNull
85 
89  @NotNull
91 
95  @NotNull
97 
107  public TestMapModelHelper(@NotNull final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> topmostInsertionMode, @NotNull final GameObjectFactory<TestGameObject, TestMapArchObject, TestArchetype> gameObjectFactory, @NotNull final ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> archetypeSet, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final AnimationObjects animationObjects) throws DuplicateArchetypeException {
108  this.topmostInsertionMode = topmostInsertionMode;
109  this.gameObjectFactory = gameObjectFactory;
110 
111  floorArchetype = new TestDefaultArchetype("floor", faceObjectProviders, animationObjects);
112  floorArchetype.setAttributeInt(BaseObject.TYPE, FLOOR_TYPE);
113  archetypeSet.addArchetype(floorArchetype);
114 
115  exitArchetype = new TestDefaultArchetype("exit", faceObjectProviders, animationObjects);
116  exitArchetype.setAttributeInt(BaseObject.TYPE, EXIT_TYPE);
117  archetypeSet.addArchetype(exitArchetype);
118 
119  mob21Archetype = new TestDefaultArchetype("mob21", faceObjectProviders, animationObjects);
120  mob21Archetype.setAttributeInt(BaseObject.TYPE, MOB_TYPE);
121  archetypeSet.addArchetype(mob21Archetype);
122  final TestArchetype mob21bArchetype = new TestDefaultArchetype("mob21b", faceObjectProviders, animationObjects);
123  mob21bArchetype.setMultiX(1);
124  mob21Archetype.addTailPart(mob21bArchetype);
125  archetypeSet.addArchetype(mob21bArchetype);
126  }
127 
134  @NotNull
135  public TestGameObject insertFloor(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final Point point) {
136  return insertArchetype(mapModel, point, floorArchetype, false);
137  }
138 
145  @NotNull
146  public TestGameObject insertExit(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final Point point) {
147  return insertArchetype(mapModel, point, exitArchetype, false);
148  }
149 
156  @NotNull
157  public TestGameObject insertMob21(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final Point point) {
158  return insertArchetype(mapModel, point, mob21Archetype, false);
159  }
160 
170  @NotNull
171  public TestGameObject insertArchetype(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, final int x, final int y, @NotNull final BaseObject<TestGameObject, TestMapArchObject, TestArchetype, ?> archetype, final boolean join) {
172  return insertArchetype(mapModel, new Point(x, y), archetype, join);
173  }
174 
183  @NotNull
184  public TestGameObject insertArchetype(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final Point point, @NotNull final BaseObject<TestGameObject, TestMapArchObject, TestArchetype, ?> archetype, final boolean join) {
185  final TestGameObject gameObject = mapModel.insertBaseObject(archetype, point, true, join, topmostInsertionMode);
186  if (gameObject == null) {
187  throw new IllegalArgumentException();
188  }
189  return gameObject;
190  }
191 
197  @NotNull
199  return insertArchetype(gameObject, exitArchetype);
200  }
201 
207  @NotNull
209  return insertArchetype(gameObject, mob21Archetype);
210  }
211 
218  @NotNull
220  final TestGameObject newGameObject = archetype.newInstance(gameObjectFactory);
221  gameObject.addLast(newGameObject);
222  return newGameObject;
223  }
224 
230  public static void checkMapContents(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final String... lines) {
231  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
232  Assert.assertEquals(lines.length, mapSize.getHeight());
233  final Pattern pattern1 = Pattern.compile("\\|");
234  final Pattern pattern2 = StringUtils.PATTERN_COMMA;
235  final Point pos = new Point();
236  for (int y = 0; y < lines.length; y++) {
237  final CharSequence line = lines[y];
238  final String[] square = pattern1.split(line, -1);
239  Assert.assertEquals(square.length, mapSize.getWidth());
240 
241  for (int x = 0; x < square.length; x++) {
242  final String square2 = square[x];
243  final String[] gameObjects = square2.isEmpty() ? EMPTY_STRING_ARRAY : pattern2.split(square2, -1);
244  pos.x = x;
245  pos.y = y;
246  checkContentsString(mapModel.getMapSquare(pos), gameObjects);
247  }
248  }
249  }
250 
256  private static void checkContentsString(@NotNull final MapSquare<TestGameObject, TestMapArchObject, TestArchetype> mapSquare, @NotNull final String... gameObjects) {
257  int i = 0;
258  for (final BaseObject<?, ?, ?, ?> gameObject : mapSquare) {
259  final String gameObjectName = gameObject.getBestName();
260  if (i >= gameObjects.length) {
261  Assert.fail("map square " + mapSquare.getMapX() + "/" + mapSquare.getMapY() + " contains excess game object '" + gameObjectName + "'");
262  } else if (!gameObjectName.equals(gameObjects[i])) {
263  Assert.fail("map square " + mapSquare.getMapX() + "/" + mapSquare.getMapY() + " contains wrong game object '" + gameObjectName + "' at index " + i + ", expected '" + gameObjects[i] + "'");
264  }
265  i++;
266  }
267  if (i < gameObjects.length) {
268  Assert.fail("map square " + mapSquare.getMapX() + "/" + mapSquare.getMapY() + " is missing game object '" + gameObjects[i] + "'");
269  }
270  }
271 
277  public static void checkContents(@NotNull final Iterable<TestGameObject> mapSquare, @NotNull final BaseObject<?, ?, ?, ?>... gameObjects) {
278  int i = 0;
279  for (final BaseObject<?, ?, ?, ?> gameObject : mapSquare) {
280  final String gameObjectName = gameObject.getBestName();
281  if (i >= gameObjects.length) {
282  Assert.fail("map square contains excess game object '" + gameObjectName + "'");
283  } else if (gameObject != gameObjects[i]) {
284  Assert.fail("map square contains wrong game object '" + gameObjectName + "' at index " + i + ", expected '" + gameObjects[i].getBestName() + "'");
285  }
286  i++;
287  }
288  if (i < gameObjects.length) {
289  Assert.fail("map square is missing game object '" + gameObjects[i].getBestName() + "'");
290  }
291 
292  final boolean inContainer = mapSquare instanceof GameObject;
293  for (final GameObject<?, ?, ?> gameObject : mapSquare) {
294  Assert.assertEquals(inContainer, gameObject.isInContainer());
295  if (inContainer) {
296  // game objects within inventories must not contain tail parts
297  Assert.assertFalse(gameObject.isMulti());
298  } else {
299  // game objects on the map must have expanded tail parts
300  Assert.assertEquals(gameObject.getArchetype().isMulti(), gameObject.isMulti());
301  }
302  }
303  }
304 
305 }
Utility class for string manipulation.
static final int EXIT_TYPE
The archetype type used for "exit" game objects.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Helper class for creating MapModel instances for regression tests.
Gridarta can handle frame information of animations and allow the selection of an animation using a t...
TestGameObject insertArchetype(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point, @NotNull final BaseObject< TestGameObject, TestMapArchObject, TestArchetype, ?> archetype, final boolean join)
Inserts an archetype game object into a map model.
static void checkContentsString(@NotNull final MapSquare< TestGameObject, TestMapArchObject, TestArchetype > mapSquare, @NotNull final String... gameObjects)
Checks that a MapSquare contains the given game objects.
TestMapModelHelper(@NotNull final InsertionMode< TestGameObject, TestMapArchObject, TestArchetype > topmostInsertionMode, @NotNull final GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > gameObjectFactory, @NotNull final ArchetypeSet< TestGameObject, TestMapArchObject, TestArchetype > archetypeSet, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final AnimationObjects animationObjects)
Creates a new instance.
final TestArchetype exitArchetype
The archetype to create exit game objects.
TestGameObject insertArchetype(@NotNull final GameObjectContainer< TestGameObject, TestMapArchObject, TestArchetype > gameObject, @NotNull final BaseObject< TestGameObject, TestMapArchObject, TestArchetype, ?> archetype)
Inserts an archetype into a game object.
Base class for classes that contain GameObjects as children in the sense of containment.
A MapArchObject implementation for testing purposes.
static void checkContents(@NotNull final Iterable< TestGameObject > mapSquare, @NotNull final BaseObject<?, ?, ?, ?>... gameObjects)
Checks that a MapSquare contains the given game objects.
String TYPE
The attribute name of the object&#39;s type.
Definition: BaseObject.java:66
int getMapX()
Returns the X coordinate of this GameObject on its map.
TestGameObject insertFloor(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts a floorArchetype game object into a map model.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
An Archetype implementation for testing purposes.
Abstract factory for creating GameObject instances.
static final int MOB_TYPE
The archetype type used for "mob" game objects.
TestGameObject insertArchetype(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, final int x, final int y, @NotNull final BaseObject< TestGameObject, TestMapArchObject, TestArchetype, ?> archetype, final boolean join)
Inserts an archetype game object into a map model.
int getMapY()
Returns the Y coordinate of this GameObject on its map.
An Exception indicating that an Archetype name is not unique.
void setMultiX(int multiX)
Sets the x-position of this part of a multi-part object.
AnimationObjects is a container for AnimationObjects.
static final int FLOOR_TYPE
The archetype type used for "floor" game objects.
final TestArchetype floorArchetype
The archetype to create floor game objects.
GameObjects are the objects based on Archetypes found on maps.
static void checkMapContents(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final String... lines)
Checks for expected MapModel&#39;s contents.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
TestGameObject insertMob21(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts a mob21Archetype game object into a map model.
static final Pattern PATTERN_COMMA
The pattern that matches a single comma (",").
TestGameObject insertExit(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts an exitArchetype game object into a map model.
final TestArchetype mob21Archetype
The archetype to create 2x1 mob game objects.
TestGameObject insertExit(@NotNull final GameObjectContainer< TestGameObject, TestMapArchObject, TestArchetype > gameObject)
Inserts an exitArchetype game object into a game object.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
Interface that captures similarities between different ArchetypeSet implementations.
TestGameObject insertMob21(@NotNull final GameObjectContainer< TestGameObject, TestMapArchObject, TestArchetype > gameObject)
Inserts a mob21Archetype game object into a game object.
static final String [] EMPTY_STRING_ARRAY
An empty array of strings.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
final GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > gameObjectFactory
The GameObjectFactory instance.
final InsertionMode< TestGameObject, TestMapArchObject, TestArchetype > topmostInsertionMode
The "topmost" InsertionMode instance.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30