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.util.Iterator;
024import net.sf.gridarta.model.archetype.DuplicateArchetypeException;
025import net.sf.gridarta.model.archetype.TestArchetype;
026import net.sf.gridarta.model.baseobject.BaseObject;
027import net.sf.gridarta.model.gameobject.TestGameObject;
028import net.sf.gridarta.model.maparchobject.TestMapArchObject;
029import net.sf.gridarta.model.match.GameObjectMatcher;
030import net.sf.gridarta.model.match.TypeNrsGameObjectMatcher;
031import org.jetbrains.annotations.NotNull;
032import org.junit.Assert;
033import org.junit.Before;
034import org.junit.Test;
035
036/**
037 * Regression tests for {@link AutoInsertionMode}.
038 * @author Andreas Kirschbaum
039 */
040public class AutoInsertionModeTest {
041
042    /**
043     * The archetype type for floor objects.
044     */
045    private static final int TYPE_FLOOR = 1;
046
047    /**
048     * The archetype type for wall objects.
049     */
050    private static final int TYPE_WALL = 2;
051
052    /**
053     * The archetype type for objects to be put below the floor.
054     */
055    private static final int TYPE_BELOW_FLOOR = 3;
056
057    /**
058     * The archetype type for system objects.
059     */
060    private static final int TYPE_SYSTEM = 4;
061
062    /**
063     * A {@link GameObjectMatcher} that matches floor objects.
064     */
065    @NotNull
066    private final GameObjectMatcher floorGameObjectMatcher = new TypeNrsGameObjectMatcher(TYPE_FLOOR);
067
068    /**
069     * A {@link GameObjectMatcher} that matches wall objects.
070     */
071    @NotNull
072    private final GameObjectMatcher wallGameObjectMatcher = new TypeNrsGameObjectMatcher(TYPE_WALL);
073
074    /**
075     * A {@link GameObjectMatcher} that matches objects to be put below the
076     * floor.
077     */
078    @NotNull
079    private final GameObjectMatcher belowFloorGameObjectMatcher = new TypeNrsGameObjectMatcher(TYPE_BELOW_FLOOR);
080
081    /**
082     * A {@link GameObjectMatcher} that matches system objects.
083     */
084    @NotNull
085    private final GameObjectMatcher systemGameObjectMatcher = new TypeNrsGameObjectMatcher(TYPE_SYSTEM);
086
087    /**
088     * The auto-insertion mode.
089     */
090    @NotNull
091    private final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> autoInsertionMode = new AutoInsertionMode<TestGameObject, TestMapArchObject, TestArchetype>(floorGameObjectMatcher, wallGameObjectMatcher, belowFloorGameObjectMatcher, systemGameObjectMatcher);
092
093    /**
094     * The topmost-insertion mode.
095     */
096    @NotNull
097    private final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> topmostInsertionMode = new TopmostInsertionMode<TestGameObject, TestMapArchObject, TestArchetype>();
098
099    /**
100     * The {@link TestMapModelCreator} instance.
101     */
102    private TestMapModelCreator mapModelCreator;
103
104    /**
105     * Checks that the auto-insertion mode works as expected.
106     * @throws DuplicateArchetypeException if the test fails
107     */
108    @Test
109    public void testInsertSystemObject1() throws DuplicateArchetypeException {
110        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(1, 1);
111        mapModel.beginTransaction("test");
112        try {
113            mapModelCreator.addGameObjectToMap(mapModel, "floor", "1", 0, 0, topmostInsertionMode);
114            mapModelCreator.addGameObjectToMap(mapModel, "wall", "2", 0, 0, topmostInsertionMode);
115            mapModelCreator.addGameObjectToMap(mapModel, "sys", "3", 0, 0, topmostInsertionMode);
116            mapModelCreator.addGameObjectToMap(mapModel, "wall", "4", 0, 0, autoInsertionMode);
117        } finally {
118            mapModel.endTransaction();
119        }
120        final MapSquare<TestGameObject, TestMapArchObject, TestArchetype> mapSquare = mapModel.getMapSquare(new Point(0, 0));
121        final Iterator<TestGameObject> it = mapSquare.iterator();
122        Assert.assertTrue(it.hasNext());
123        Assert.assertEquals("1", it.next().getBestName());
124        Assert.assertTrue(it.hasNext());
125        Assert.assertEquals("4", it.next().getBestName());
126        Assert.assertTrue(it.hasNext());
127        Assert.assertEquals("3", it.next().getBestName());
128        Assert.assertFalse(it.hasNext());
129    }
130
131    /**
132     * Checks that the auto-insertion mode works as expected.
133     */
134    @Test
135    public void testInsertSystemObject2() {
136        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(1, 1);
137        mapModel.beginTransaction("test");
138        try {
139            mapModelCreator.addGameObjectToMap(mapModel, "floor", "1", 0, 0, topmostInsertionMode);
140            mapModelCreator.addGameObjectToMap(mapModel, "sys", "3", 0, 0, topmostInsertionMode);
141            mapModelCreator.addGameObjectToMap(mapModel, "wall", "4", 0, 0, autoInsertionMode);
142        } finally {
143            mapModel.endTransaction();
144        }
145        final MapSquare<TestGameObject, TestMapArchObject, TestArchetype> mapSquare = mapModel.getMapSquare(new Point(0, 0));
146        final Iterator<TestGameObject> it = mapSquare.iterator();
147        Assert.assertTrue(it.hasNext());
148        Assert.assertEquals("1", it.next().getBestName());
149        Assert.assertTrue(it.hasNext());
150        Assert.assertEquals("4", it.next().getBestName());
151        Assert.assertTrue(it.hasNext());
152        Assert.assertEquals("3", it.next().getBestName());
153        Assert.assertFalse(it.hasNext());
154    }
155
156    /**
157     * Initializes the tests.
158     * @throws DuplicateArchetypeException if the test fails
159     */
160    @Before
161    public void setUp() throws DuplicateArchetypeException {
162        mapModelCreator = new TestMapModelCreator(false);
163
164        final TestArchetype floorArchetype = mapModelCreator.newArchetype("floor");
165        floorArchetype.setAttributeInt(BaseObject.TYPE, TYPE_FLOOR);
166        mapModelCreator.getArchetypeSet().addArchetype(floorArchetype);
167
168        final TestArchetype wallArchetype = mapModelCreator.newArchetype("wall");
169        wallArchetype.setAttributeInt(BaseObject.TYPE, TYPE_WALL);
170        mapModelCreator.getArchetypeSet().addArchetype(wallArchetype);
171
172        final TestArchetype sysArchetype = mapModelCreator.newArchetype("sys");
173        sysArchetype.setAttributeInt(BaseObject.TYPE, TYPE_SYSTEM);
174        mapModelCreator.getArchetypeSet().addArchetype(sysArchetype);
175    }
176
177}