public void testAssignDefendSettlementMission() { Game game = ServerTestHelper.startServerGame(getTestMap()); Map map = game.getMap(); AIMain aiMain = ServerTestHelper.getServer().getAIMain(); // Create player and unit ServerPlayer dutch = (ServerPlayer) game.getPlayerByNationId("model.nation.dutch"); Tile tile1 = map.getTile(2, 2); Unit soldier = new ServerUnit(game, tile1, dutch, veteranType); AIUnit aiUnit = aiMain.getAIUnit(soldier); assertNotNull(aiUnit); // Add nearby colony in need of defense Tile colonyTile = map.getTile(2, 3); assertTrue(colonyTile != null); colonyTile.setExplored(dutch, true); Colony colony = FreeColTestUtils.getColonyBuilder().player(dutch).colonyTile(colonyTile).build(); assertTrue(colonyTile.getSettlement() == colony); assertTrue(colony.getOwner() == dutch); assertTrue(colony.getUnitCount() == 1); aiUnit.setMission(null); assertEquals( "DefendSettlementMission should be possible", null, DefendSettlementMission.invalidReason(aiUnit)); assertEquals( "DefendSettlementMission should work with colony", null, DefendSettlementMission.invalidReason(aiUnit, colony)); }
public void testSecureIndianSettlementMission() { Game game = ServerTestHelper.startServerGame(getTestMap()); Map map = game.getMap(); AIMain aiMain = ServerTestHelper.getServer().getAIMain(); // Create player and unit ServerPlayer inca = (ServerPlayer) game.getPlayerByNationId("model.nation.inca"); NativeAIPlayer aiInca = (NativeAIPlayer) aiMain.getAIPlayer(inca); ServerPlayer dutch = (ServerPlayer) game.getPlayerByNationId("model.nation.dutch"); Tile settlementTile = map.getTile(9, 9); Tile adjacentTile = settlementTile.getNeighbourOrNull(Direction.N); assertTrue("Settlement tile should be land", settlementTile.isLand()); assertTrue("Adjacent tile should be land", adjacentTile != null && adjacentTile.isLand()); FreeColTestCase.IndianSettlementBuilder builder = new FreeColTestCase.IndianSettlementBuilder(game); IndianSettlement camp = builder.player(inca).settlementTile(settlementTile).initialBravesInCamp(8).build(); assertEquals("One camp", 1, inca.getNumberOfSettlements()); // Put one brave outside the camp, but in the settlement tile, // so that he may defend the settlement Unit braveOutside = new ServerUnit(game, settlementTile, inca, braveType); braveOutside.setHomeIndianSettlement(camp); // Setup enemy units int enemyUnits = camp.getUnitCount() + 1; for (int i = 0; i < enemyUnits; i++) { new ServerUnit(game, adjacentTile, dutch, veteranType); } Iterator<Unit> campUnitIter = camp.getOwnedUnitsIterator(); while (campUnitIter.hasNext()) { Unit brave = campUnitIter.next(); assertNotNull("Got null while getting the camps units", brave); AIUnit aiUnit = aiMain.getAIUnit(brave); assertNotNull("Couldnt get the ai object for the brave", aiUnit); new UnitWanderHostileMission(aiMain, aiUnit); assertTrue( "Should be UnitWanderHostileMission", aiUnit.hasMission(UnitWanderHostileMission.class)); assertEquals( "Unit should be candidate for seek+destroy", null, UnitSeekAndDestroyMission.invalidReason(aiUnit)); assertEquals( "Unit should be candidate for defend", null, DefendSettlementMission.invalidReason(aiUnit)); } inca.setStance(dutch, Stance.WAR); inca.setTension(dutch, new Tension(Tension.Level.HATEFUL.getLimit())); camp.setAlarm(dutch, inca.getTension(dutch)); assertTrue("Indian player should be at war with dutch", inca.getStance(dutch) == Stance.WAR); assertEquals( "Wrong Indian player tension towards dutch", Tension.Level.HATEFUL.getLimit(), inca.getTension(dutch).getValue()); aiInca.secureIndianSettlement(camp, lb); // Verify if a unit was assigned a UnitSeekAndDestroyMission boolean isSeekAndDestroyMission = false; for (Unit brave : inca.getUnits()) { AIUnit aiUnit = aiMain.getAIUnit(brave); assertNotNull("Couldnt get aiUnit for players brave", aiUnit); assertNotNull("Unit missing mission", aiUnit.getMission()); isSeekAndDestroyMission = aiUnit.hasMission(UnitSeekAndDestroyMission.class); if (isSeekAndDestroyMission) break; } assertTrue("A brave should have a UnitSeekAndDestroyMission", isSeekAndDestroyMission); }