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-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.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 @NotNull [] EMPTY_STRING_ARRAY = new String[0];
67 
71  @NotNull
73 
77  @NotNull
79 
83  @NotNull
85 
89  @NotNull
91 
95  @NotNull
97 
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("failed to insert archetype");
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 }
net.sf.gridarta.model.archetype.TestArchetype
An Archetype implementation for testing purposes.
Definition: TestArchetype.java:30
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.archetype.TestDefaultArchetype
An Archetype implementation for testing purposes.
Definition: TestDefaultArchetype.java:32
net.sf.gridarta.model.gameobject.GameObjectFactory
Abstract factory for creating GameObject instances.
Definition: GameObjectFactory.java:31
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertExit
TestGameObject insertExit(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts an exitArchetype game object into a map model.
Definition: TestMapModelHelper.java:146
net.sf.gridarta.model.mapmodel.TestMapModelHelper.mob21Archetype
final TestArchetype mob21Archetype
The archetype to create 2x1 mob game objects.
Definition: TestMapModelHelper.java:96
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.baseobject.BaseObject.getMapX
int getMapX()
Returns the X coordinate of this GameObject on its map.
net.sf.gridarta.model.mapmodel.TestMapModelHelper
Helper class for creating MapModel instances for regression tests.
Definition: TestMapModelHelper.java:45
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.model.anim.AnimationObjects
AnimationObjects is a container for AnimationObjects.
Definition: AnimationObjects.java:30
net.sf.gridarta.model.gameobject.TestGameObject
A GameObject implementation for testing purposes.
Definition: TestGameObject.java:34
net.sf
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.face.FaceObjectProviders
Provider for faces of GameObjects and Archetypes.
Definition: FaceObjectProviders.java:46
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertMob21
TestGameObject insertMob21(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts a mob21Archetype game object into a map model.
Definition: TestMapModelHelper.java:157
net.sf.gridarta.model.mapmodel.TestMapModelHelper.checkContentsString
static void checkContentsString(@NotNull final MapSquare< TestGameObject, TestMapArchObject, TestArchetype > mapSquare, @NotNull final String... gameObjects)
Checks that a MapSquare contains the given game objects.
Definition: TestMapModelHelper.java:256
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertMob21
TestGameObject insertMob21(@NotNull final GameObjectContainer< TestGameObject, TestMapArchObject, TestArchetype > gameObject)
Inserts a mob21Archetype game object into a game object.
Definition: TestMapModelHelper.java:208
net.sf.gridarta.model.mapmodel.TestMapModelHelper.gameObjectFactory
final GameObjectFactory< TestGameObject, TestMapArchObject, TestArchetype > gameObjectFactory
The GameObjectFactory instance.
Definition: TestMapModelHelper.java:78
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertExit
TestGameObject insertExit(@NotNull final GameObjectContainer< TestGameObject, TestMapArchObject, TestArchetype > gameObject)
Inserts an exitArchetype game object into a game object.
Definition: TestMapModelHelper.java:198
net.sf.gridarta.model.mapmodel.TestMapModelHelper.exitArchetype
final TestArchetype exitArchetype
The archetype to create exit game objects.
Definition: TestMapModelHelper.java:90
net.sf.gridarta.model.mapmodel.TestMapModelHelper.EMPTY_STRING_ARRAY
static final String[] EMPTY_STRING_ARRAY
An empty array of strings.
Definition: TestMapModelHelper.java:66
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertFloor
TestGameObject insertFloor(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final Point point)
Inserts a floorArchetype game object into a map model.
Definition: TestMapModelHelper.java:135
net.sf.gridarta.model.archetypeset.ArchetypeSet
Interface that captures similarities between different ArchetypeSet implementations.
Definition: ArchetypeSet.java:37
net.sf.gridarta.model.mapmodel.InsertionMode
Insertion modes.
Definition: InsertionMode.java:33
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.utils.StringUtils
Utility class for string manipulation.
Definition: StringUtils.java:31
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertArchetype
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.
Definition: TestMapModelHelper.java:171
net.sf.gridarta.model.baseobject.BaseObject.getMapY
int getMapY()
Returns the Y coordinate of this GameObject on its map.
net.sf.gridarta.model.mapmodel.TestMapModelHelper.FLOOR_TYPE
static final int FLOOR_TYPE
The archetype type used for "floor" game objects.
Definition: TestMapModelHelper.java:55
net.sf.gridarta.utils.StringUtils.PATTERN_COMMA
static final Pattern PATTERN_COMMA
The pattern that matches a single comma (",").
Definition: StringUtils.java:97
net.sf.gridarta.model.mapmodel.TestMapModelHelper.MOB_TYPE
static final int MOB_TYPE
The archetype type used for "mob" game objects.
Definition: TestMapModelHelper.java:60
net.sf.gridarta.model
net.sf.gridarta.model.mapmodel.TestMapModelHelper.checkMapContents
static void checkMapContents(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final String... lines)
Checks for expected MapModel's contents.
Definition: TestMapModelHelper.java:230
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.mapmodel.TestMapModelHelper.EXIT_TYPE
static final int EXIT_TYPE
The archetype type used for "exit" game objects.
Definition: TestMapModelHelper.java:50
net.sf.gridarta.model.face
The face is the appearance of an object.
Definition: AbstractFaceObjects.java:20
net.sf.gridarta.model.anim
Gridarta can handle frame information of animations and allow the selection of an animation using a t...
Definition: AbstractAnimationObjects.java:20
net.sf.gridarta.var.crossfire.model.gameobject.GameObject<?, ?, ?>
net.sf.gridarta.model.archetype.DuplicateArchetypeException
An Exception indicating that an Archetype name is not unique.
Definition: DuplicateArchetypeException.java:29
net.sf.gridarta.model.mapmodel.TestMapModelHelper.floorArchetype
final TestArchetype floorArchetype
The archetype to create floor game objects.
Definition: TestMapModelHelper.java:84
net.sf.gridarta.model.maparchobject.TestMapArchObject
A MapArchObject implementation for testing purposes.
Definition: TestMapArchObject.java:28
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.model.mapmodel.TestMapModelHelper.TestMapModelHelper
TestMapModelHelper(@NotNull final InsertionMode 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.
Definition: TestMapModelHelper.java:107
net.sf.gridarta.model.baseobject.BaseObject.TYPE
String TYPE
The attribute name of the object's type.
Definition: BaseObject.java:66
net.sf.gridarta.model.archetypeset
Definition: ArchetypeSet.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.TestMapModelHelper.topmostInsertionMode
final InsertionMode topmostInsertionMode
The "topmost" InsertionMode instance.
Definition: TestMapModelHelper.java:72
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertArchetype
TestGameObject insertArchetype(@NotNull final GameObjectContainer< TestGameObject, TestMapArchObject, TestArchetype > gameObject, @NotNull final BaseObject< TestGameObject, TestMapArchObject, TestArchetype, ?> archetype)
Inserts an archetype into a game object.
Definition: TestMapModelHelper.java:219
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.mapmodel.TestMapModelHelper.checkContents
static void checkContents(@NotNull final Iterable< TestGameObject > mapSquare, @NotNull final BaseObject<?, ?, ?, ?>... gameObjects)
Checks that a MapSquare contains the given game objects.
Definition: TestMapModelHelper.java:277
net.sf.gridarta.model.mapmodel.TestMapModelHelper.insertArchetype
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.
Definition: TestMapModelHelper.java:184
net.sf.gridarta.model.archetype.Archetype.setMultiX
void setMultiX(int multiX)
Sets the x-position of this part of a multi-part object.