001/* 002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games. 003 * Copyright (C) 2000-2010 The Gridarta Developers. 004 * 005 * This program is free software; you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation; either version 2 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License along 016 * with this program; if not, write to the Free Software Foundation, Inc., 017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 018 */ 019 020package net.sf.gridarta.model.mapmodel; 021 022import java.awt.Point; 023import java.awt.image.BufferedImage; 024import javax.swing.ImageIcon; 025import net.sf.gridarta.model.anim.AnimationObjects; 026import net.sf.gridarta.model.anim.TestAnimationObjects; 027import net.sf.gridarta.model.archetype.ArchetypeFactory; 028import net.sf.gridarta.model.archetype.DuplicateArchetypeException; 029import net.sf.gridarta.model.archetype.TestArchetype; 030import net.sf.gridarta.model.archetype.TestArchetypeFactory; 031import net.sf.gridarta.model.archetype.TestDefaultArchetype; 032import net.sf.gridarta.model.archetype.UndefinedArchetypeException; 033import net.sf.gridarta.model.archetypechooser.ArchetypeChooserModel; 034import net.sf.gridarta.model.archetypeset.ArchetypeSet; 035import net.sf.gridarta.model.archetypeset.DefaultArchetypeSet; 036import net.sf.gridarta.model.autojoin.AutojoinLists; 037import net.sf.gridarta.model.baseobject.BaseObject; 038import net.sf.gridarta.model.face.EmptyFaceProvider; 039import net.sf.gridarta.model.face.FaceObjectProviders; 040import net.sf.gridarta.model.face.FaceObjects; 041import net.sf.gridarta.model.face.FaceProvider; 042import net.sf.gridarta.model.face.TestFaceObjects; 043import net.sf.gridarta.model.gameobject.GameObject; 044import net.sf.gridarta.model.gameobject.GameObjectFactory; 045import net.sf.gridarta.model.gameobject.TestGameObject; 046import net.sf.gridarta.model.gameobject.TestGameObjectFactory; 047import net.sf.gridarta.model.io.GameObjectParser; 048import net.sf.gridarta.model.io.TestGameObjectParser; 049import net.sf.gridarta.model.maparchobject.TestMapArchObject; 050import net.sf.gridarta.model.mapviewsettings.MapViewSettings; 051import net.sf.gridarta.model.mapviewsettings.TestMapViewSettings; 052import net.sf.gridarta.model.match.GameObjectMatchers; 053import net.sf.gridarta.model.match.TypeNrsGameObjectMatcher; 054import net.sf.gridarta.utils.ResourceIcons; 055import net.sf.gridarta.utils.Size2D; 056import org.jetbrains.annotations.NotNull; 057import org.junit.Assert; 058 059/** 060 * Helper class for regression tests to create {@link MapModel} instances. 061 * @author Andreas Kirschbaum 062 */ 063public class TestMapModelCreator { 064 065 /** 066 * The {@link ArchetypeSet} instance. 067 */ 068 @NotNull 069 private final ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> archetypeSet; 070 071 /** 072 * Predefined icon names. 073 */ 074 @NotNull 075 private static final String[] ICON_NAMES = { ResourceIcons.SQUARE_SELECTED_SQUARE, ResourceIcons.SQUARE_SELECTED_SQUARE_NORTH, ResourceIcons.SQUARE_SELECTED_SQUARE_EAST, ResourceIcons.SQUARE_SELECTED_SQUARE_SOUTH, ResourceIcons.SQUARE_SELECTED_SQUARE_WEST, ResourceIcons.SQUARE_PRE_SELECTED_SQUARE, ResourceIcons.SQUARE_CURSOR, ResourceIcons.SQUARE_EMPTY, ResourceIcons.SQUARE_UNKNOWN, ResourceIcons.SQUARE_NO_FACE, ResourceIcons.SQUARE_NO_ARCH, ResourceIcons.DEFAULT_ICON, ResourceIcons.DEFAULT_PREVIEW, ResourceIcons.SQUARE_WARNING, }; 076 077 /** 078 * The {@link MapViewSettings} instance. 079 */ 080 @NotNull 081 private final MapViewSettings mapViewSettings = new TestMapViewSettings(); 082 083 /** 084 * The {@link ResourceIcons} instance. 085 */ 086 @NotNull 087 private final ResourceIcons resourceIcons; 088 089 /** 090 * The {@link FaceObjectProviders} instance. 091 */ 092 @NotNull 093 private final FaceObjectProviders faceObjectProviders; 094 095 /** 096 * The {@link GameObjectMatchers} instance. 097 */ 098 @NotNull 099 private final GameObjectMatchers gameObjectMatchers = new GameObjectMatchers(); 100 101 /** 102 * The {@link ArchetypeChooserModel} instance. 103 */ 104 @NotNull 105 private final ArchetypeChooserModel<TestGameObject, TestMapArchObject, TestArchetype> archetypeChooserModel = new ArchetypeChooserModel<TestGameObject, TestMapArchObject, TestArchetype>(); 106 107 /** 108 * The {@link AutojoinLists} instance. 109 */ 110 @NotNull 111 private final AutojoinLists<TestGameObject, TestMapArchObject, TestArchetype> autojoinLists; 112 113 /** 114 * The {@link GameObjectFactory} instance. 115 */ 116 @NotNull 117 private final GameObjectFactory<TestGameObject, TestMapArchObject, TestArchetype> gameObjectFactory; 118 119 /** 120 * The {@link AnimationObjects} instance. 121 */ 122 @NotNull 123 private final AnimationObjects animationObjects = new TestAnimationObjects(); 124 125 /** 126 * The "topmost" {@link InsertionMode} instance. 127 */ 128 @NotNull 129 private final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> topmostInsertionMode; 130 131 /** 132 * The {@link InsertionModeSet} instance. 133 */ 134 @NotNull 135 private final InsertionModeSet<TestGameObject, TestMapArchObject, TestArchetype> insertionModeSet; 136 137 /** 138 * Creates a new instance. 139 * @param createIcons whether to create icon instances 140 */ 141 public TestMapModelCreator(final boolean createIcons) { 142 autojoinLists = new AutojoinLists<TestGameObject, TestMapArchObject, TestArchetype>(); 143 resourceIcons = new ResourceIcons(); 144 if (createIcons) { 145 final ImageIcon imageIcon = new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB)); 146 for (final String iconName : ICON_NAMES) { 147 resourceIcons.addToCache(iconName, imageIcon); 148 } 149 } 150 final FaceObjects faceObjects = new TestFaceObjects(); 151 faceObjectProviders = new FaceObjectProviders(0, faceObjects, resourceIcons); 152 final FaceProvider faceProvider = new EmptyFaceProvider(); 153 faceObjectProviders.setNormal(faceProvider); 154 final ArchetypeFactory<TestGameObject, TestMapArchObject, TestArchetype> archetypeFactory = new TestArchetypeFactory(faceObjectProviders, animationObjects); 155 archetypeSet = new DefaultArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype>(archetypeFactory, null); 156 gameObjectFactory = new TestGameObjectFactory(faceObjectProviders, animationObjects); 157 topmostInsertionMode = new TopmostInsertionMode<TestGameObject, TestMapArchObject, TestArchetype>(); 158 insertionModeSet = new InsertionModeSet<TestGameObject, TestMapArchObject, TestArchetype>(topmostInsertionMode, new TypeNrsGameObjectMatcher(), new TypeNrsGameObjectMatcher(), new TypeNrsGameObjectMatcher(), new TypeNrsGameObjectMatcher()); 159 } 160 161 /** 162 * Creates a new {@link MapModel} instance. 163 * @param w the width in squares 164 * @param h the height in squares 165 * @return the new map model instance 166 */ 167 @NotNull 168 public MapModel<TestGameObject, TestMapArchObject, TestArchetype> newMapModel(final int w, final int h) { 169 final TestMapArchObject mapArchObject = new TestMapArchObject(); 170 mapArchObject.setMapSize(new Size2D(w, h)); 171 return new DefaultMapModel<TestGameObject, TestMapArchObject, TestArchetype>(autojoinLists, mapArchObject, archetypeChooserModel, gameObjectFactory, gameObjectMatchers, topmostInsertionMode); 172 } 173 174 /** 175 * Creates a new game object. 176 * @param archetypeName the name of the game object´s archetype 177 * @param objectName the object name to set 178 * @return the game object 179 */ 180 @NotNull 181 public TestGameObject newGameObject(@NotNull final String archetypeName, @NotNull final String objectName) { 182 final TestArchetype archetype = getArchetype(archetypeName); 183 final TestGameObject gameObject = new TestGameObject(archetype, faceObjectProviders, animationObjects); 184 gameObject.setAttributeString(BaseObject.NAME, objectName); 185 return gameObject; 186 } 187 188 /** 189 * Inserts a game object into a map. 190 * @param mapModel the map to add to 191 * @param archetypeName the name of the game object´s archetype 192 * @param name the name of the game object to add 193 * @param x the x coordinate to add to 194 * @param y the y coordinate to add to 195 * @param insertionMode the insertion mode to use 196 */ 197 public void addGameObjectToMap(@NotNull final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel, @NotNull final String archetypeName, @NotNull final String name, final int x, final int y, @NotNull final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> insertionMode) { 198 final TestGameObject gameObject = newGameObject(archetypeName, name); 199 mapModel.addGameObjectToMap(gameObject, new Point(x, y), insertionMode); 200 } 201 202 /** 203 * Inserts a game object into the inventory of another game object. 204 * @param gameObject the game object to add to 205 * @param archetypeName the name of the game object´s archetype 206 * @param name the name of the game object to add 207 */ 208 public void insertGameObject(@NotNull final GameObject<TestGameObject, TestMapArchObject, TestArchetype> gameObject, @NotNull final String archetypeName, @NotNull final String name) { 209 final TestGameObject inv = newGameObject(archetypeName, name); 210 gameObject.addLast(inv); 211 } 212 213 /** 214 * Returns the {@link InsertionModeSet} instance. 215 * @return the insertion mode set instance 216 */ 217 @NotNull 218 public InsertionModeSet<TestGameObject, TestMapArchObject, TestArchetype> getInsertionModeSet() { 219 return insertionModeSet; 220 } 221 222 /** 223 * Returns the "topmost" insertion mode. 224 * @return the "topmost" insertion mode 225 */ 226 @NotNull 227 public InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> getTopmostInsertionMode() { 228 return topmostInsertionMode; 229 } 230 231 /** 232 * Returns an archetype. 233 * @param archetypeName the archetype name 234 * @return the archetype 235 */ 236 @NotNull 237 public TestArchetype getArchetype(@NotNull final String archetypeName) { 238 try { 239 return archetypeSet.getArchetype(archetypeName); 240 } catch (final UndefinedArchetypeException ignored) { 241 // ignore 242 } 243 244 final TestArchetype archetype = newArchetype(archetypeName); 245 try { 246 archetypeSet.addArchetype(archetype); 247 } catch (final DuplicateArchetypeException ex) { 248 Assert.fail(ex.getMessage()); 249 throw new AssertionError(ex); 250 } 251 return archetype; 252 } 253 254 /** 255 * Creates a new {@link TestArchetype} instance but doesn't add it to the 256 * archetype set. 257 * @param archetypeName the archetype´s name 258 * @return the new instance 259 */ 260 @NotNull 261 public TestArchetype newArchetype(@NotNull final String archetypeName) { 262 return new TestDefaultArchetype(archetypeName, faceObjectProviders, animationObjects); 263 } 264 265 /** 266 * Returns the {@link GameObjectFactory} instance. 267 * @return the game object factory instance 268 */ 269 @NotNull 270 public GameObjectFactory<TestGameObject, TestMapArchObject, TestArchetype> getGameObjectFactory() { 271 return gameObjectFactory; 272 } 273 274 /** 275 * Returns the {@link MapViewSettings} instance. 276 * @return the map view settings instance 277 */ 278 @NotNull 279 public MapViewSettings getMapViewSettings() { 280 return mapViewSettings; 281 } 282 283 /** 284 * Creates a new {@link TestMapModelHelper} instance. 285 * @return the new test map model creator instance 286 * @throws DuplicateArchetypeException if an internal error occurs 287 */ 288 @NotNull 289 public TestMapModelHelper newTestMapModelHelper() throws DuplicateArchetypeException { 290 return new TestMapModelHelper(topmostInsertionMode, gameObjectFactory, archetypeSet, faceObjectProviders, animationObjects); 291 } 292 293 /** 294 * Returns the {@link AutojoinLists} instance. 295 * @return the autojoin lists instance 296 */ 297 @NotNull 298 public AutojoinLists<TestGameObject, TestMapArchObject, TestArchetype> getAutojoinLists() { 299 return autojoinLists; 300 } 301 302 /** 303 * Returns the {@link ArchetypeChooserModel} instance. 304 * @return the archetype chooser model instance 305 */ 306 @NotNull 307 public ArchetypeChooserModel<TestGameObject, TestMapArchObject, TestArchetype> getArchetypeChooserModel() { 308 return archetypeChooserModel; 309 } 310 311 /** 312 * Returns the {@link GameObjectMatchers} instance. 313 * @return the game object matchers instance 314 */ 315 @NotNull 316 public GameObjectMatchers getGameObjectMatchers() { 317 return gameObjectMatchers; 318 } 319 320 /** 321 * Returns the {@link ArchetypeSet}. 322 * @return the archetype set 323 */ 324 @NotNull 325 public ArchetypeSet<TestGameObject, TestMapArchObject, TestArchetype> getArchetypeSet() { 326 return archetypeSet; 327 } 328 329 /** 330 * Creates a new {@link GameObjectParser} instance. 331 * @return the new game object parser instance 332 */ 333 public GameObjectParser<TestGameObject, TestMapArchObject, TestArchetype> newGameObjectParser() { 334 return new TestGameObjectParser(gameObjectFactory, archetypeSet); 335 } 336 337 /** 338 * Returns the {@link FaceObjectProviders} instance. 339 * @return the face object providers instance 340 */ 341 @NotNull 342 public FaceObjectProviders getFaceObjectProviders() { 343 return faceObjectProviders; 344 } 345 346 /** 347 * Returns the {@link ResourceIcons} instance. 348 * @return the system icons instance 349 */ 350 @NotNull 351 public ResourceIcons getResourceIcons() { 352 return resourceIcons; 353 } 354 355}