/************************************************************************************ * Copyright (c) 2015 /// Project SWG /// www.projectswg.com * * * * ProjectSWG is the first NGE emulator for Star Wars Galaxies founded on * * July 7th, 2011 after SOE announced the official shutdown of Star Wars Galaxies. * * Our goal is to create an emulator which will provide a server for players to * * continue playing a game similar to the one they used to play. We are basing * * it on the final publish of the game prior to end-game events. * * * * This file is part of Holocore. * * * * -------------------------------------------------------------------------------- * * * * Holocore is free software: you can redistribute it and/or modify * * it under the terms of the GNU Affero General Public License as * * published by the Free Software Foundation, either version 3 of the * * License, or (at your option) any later version. * * * * Holocore is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Affero General Public License for more details. * * * * You should have received a copy of the GNU Affero General Public License * * along with Holocore. If not, see . * * * ***********************************************************************************/ package resources.objects.awareness; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import com.projectswg.common.data.location.Terrain; import com.projectswg.common.debug.Log; import resources.objects.SWGObject; import resources.objects.cell.CellObject; import test_resources.GenericCreatureObject; @RunWith(JUnit4.class) public class TestTerrainMap { @Test public void testWithoutParent() { TerrainMap map = new TerrainMap(Terrain.TATOOINE); MapCallback callback = new MapCallback(); map.setCallback(callback); try { map.start(); GenericCreatureObject objA = new GenericCreatureObject(1); GenericCreatureObject objB = new GenericCreatureObject(2); objA.setPosition(Terrain.TATOOINE, 0, 0, 0); objB.setPosition(Terrain.TATOOINE, 5, 0, 5); map.moveWithinMap(objA); // 0 - Within Range, 1 - Successful Move map.moveWithinMap(objB); // 1 - Within Range, 1 - Successful Move awaitCallbacks(map, 1000); callback.testAssert(1, 0, 2, 0); } finally { map.stop(); } } @Test public void testWithParent() { TerrainMap map = new TerrainMap(Terrain.TATOOINE); MapCallback callback = new MapCallback(); map.setCallback(callback); try { map.start(); GenericCreatureObject objA = new GenericCreatureObject(1); GenericCreatureObject objB = new GenericCreatureObject(2); CellObject cell = new CellObject(3); cell.setPosition(Terrain.TATOOINE, 5, 0, 5); objA.setPosition(Terrain.TATOOINE, 0, 0, 0); map.moveWithinMap(cell); // 0 - Within Range, 1 - Successful Move map.moveWithinMap(objA); // 1 - Within Range, 1 - Successful Move objB.setPosition(Terrain.TATOOINE, 5, 0, 5); objB.moveToContainer(cell); awaitCallbacks(map, 1000); callback.testAssert(1, 0, 2, 0); } finally { map.stop(); } } @Test public void testMultiChunk() { TerrainMap map = new TerrainMap(Terrain.TATOOINE); MapCallback callback = new MapCallback(); map.setCallback(callback); try { map.start(); GenericCreatureObject objA = new GenericCreatureObject(1); GenericCreatureObject objB = new GenericCreatureObject(2); objA.setPrefLoadRange(200); objB.setPrefLoadRange(200); objA.setPosition(Terrain.TATOOINE, 5, 0, 5); objB.setPosition(Terrain.TATOOINE, -5, 0, -5); map.moveWithinMap(objA); // 0 - Within Range, 1 - Successful Move map.moveWithinMap(objB); // 1 - Within Range, 1 - Successful Move awaitCallbacks(map, 1000); callback.testAssert(1, 0, 2, 0); } finally { map.stop(); } } @Test public void testTatooine() { TerrainMap map = new TerrainMap(Terrain.TATOOINE); AtomicBoolean onlyWithinRange = new AtomicBoolean(true); GenericCreatureObject creature = new GenericCreatureObject(1); creature.setPosition(Terrain.TATOOINE, 3500, 5, -4800); List withinRange = TestBuildoutObjectList.getInstance().getWithinRangeObjects(creature); map.setCallback(new MapCallback() { @Override public void onWithinRange(SWGObject obj, SWGObject inRange) { if (obj.equals(creature) && !withinRange.remove(inRange)) { Log.e("Not in list: %s", inRange); onlyWithinRange.set(false); } } }); try { map.start(); for (SWGObject obj : getMosEisleyObjects()) { map.moveWithinMap(obj); } map.moveWithinMap(creature); awaitCallbacks(map, 1000); Assert.assertEquals(0, withinRange.size()); Assert.assertTrue("TEST-ONLY-WITHIN-RANGE", onlyWithinRange.get()); } finally { map.stop(); } } private List getMosEisleyObjects() { return TestBuildoutObjectList.getInstance().getMosEisleyObjects(); } private void awaitCallbacks(TerrainMap map, long timeout) { try { while (!map.isCallbacksDone() && timeout > 0) { Thread.sleep(1); timeout--; } } catch (InterruptedException e) { Log.e(e); } } }