/** Tests invalid completion of buildable, having enough resources */ public void testInvalidCompletion() { Game game = ServerTestHelper.startServerGame(getTestMap(true)); Colony colony = getStandardColony(2); Building carpenterHouse = colony.getBuilding(carpenterHouseType); assertEquals( "Colony should not have lumber mill", carpenterHouse, colony.getBuilding(lumberMillType)); assertFalse("Colony should not be able to build lumber mill", colony.canBuild(lumberMillType)); colony.setCurrentlyBuilding(lumberMillType); assertEquals( "Colony should be building lumber mill", lumberMillType, colony.getCurrentlyBuilding()); // Add sufficient units and goods to build lumber mill. Unit unit = new ServerUnit(game, colony.getTile(), colony.getOwner(), colonistType); unit.setLocation(colony); for (AbstractGoods ag : lumberMillType.getRequiredGoods()) { GoodsType type = ag.getType(); int amount = ag.getAmount() + 1; colony.addGoods(type, amount); assertEquals("Wrong quantity of " + type, amount, colony.getGoodsCount(type)); } // Allow the building to finish ServerTestHelper.newTurn(); assertEquals( "Colony should have lumber mill", lumberMillType, colony.getBuilding(lumberMillType).getType()); assertFalse( "Colony should no longer be building lumber mill", colony.getCurrentlyBuilding() == lumberMillType); }
/** * Progress in teaching is bound to the teacher and not the learner. * * <p>Moving students around does not slow education. This behavior is there to simplify gameplay. */ public void testTeacherStoresProgress() { boolean selection = FreeColTestUtils.setStudentSelection(false); Game game = ServerTestHelper.startServerGame(getTestMap(true)); Colony colony = getSchoolColony(4, SchoolLevel.UNIVERSITY); Building university = colony.getBuilding(universityType); Iterator<Unit> units = colony.getUnitIterator(); Unit outsider = new ServerUnit(game, colony.getTile(), colony.getOwner(), freeColonistType); Building townHall = colony.getBuilding(townHallType); Unit colonist = units.next(); colonist.setType(freeColonistType); colonist.setLocation(townHall); Unit teacher = units.next(); teacher.setType(expertOreMinerType); teacher.setLocation(university); assertEquals(teacher.getStudent(), colonist); assertEquals(colonist.getTeacher(), teacher); // Train to become free colonist then swap the colonist with // another one. trainForTurns(colony, teacher.getNeededTurnsOfTraining() - 1); colonist.setLocation(colony.getTile()); outsider.setLocation(townHall); assertEquals(1, getUnitList(colony, freeColonistType).size()); assertEquals(teacher.getStudent(), outsider); ServerTestHelper.newTurn(); assertEquals(0, getUnitList(colony, freeColonistType).size()); assertEquals(expertOreMinerType, outsider.getType()); FreeColTestUtils.setStudentSelection(selection); }
public void testAvoidStarvation() { Game game = ServerTestHelper.startServerGame(getTestMap(marsh)); int unitsBeforeNewTurn = 3; Colony colony = getStandardColony(unitsBeforeNewTurn); ServerPlayer player = (ServerPlayer) colony.getOwner(); assertEquals("Wrong number of units in colony", unitsBeforeNewTurn, colony.getUnitCount()); final Building townHall = colony.getBuilding(townHallType); for (Unit u : colony.getUnitList()) { u.setLocation(townHall); } colony.removeGoods(foodGoodsType); colony.invalidateCache(); int consumption = colony.getFoodConsumption(); int production = colony.getTile().getType().getPotentialProduction(grainType, null); assertEquals(6, consumption); assertEquals(3, production); assertEquals(-3, colony.getNetProductionOf(foodType)); assertEquals(0, colony.getGoodsCount(foodType)); assertEquals(0, colony.getTile().getUnitCount()); colony.addGoods(foodType, 202); ServerTestHelper.newTurn(); assertEquals(199, colony.getGoodsCount(foodType)); assertEquals(0, colony.getTile().getUnitCount()); assertEquals(3, colony.getUnitCount()); colony.addGoods(foodType, 15); ServerTestHelper.newTurn(); assertEquals(11, colony.getGoodsCount(foodType)); assertEquals(1, colony.getTile().getUnitCount()); }
/** * This function analyses an event and calls the right methods to take care of the user's * requests. * * @param event The incoming ActionEvent. */ @Override public void actionPerformed(ActionEvent event) { final String FAIL = "FAIL"; if (colony.getOwner() == getMyPlayer()) { String command = event.getActionCommand(); List<BuildableType> buildables = getBuildableTypes(buildQueueList); while (!buildables.isEmpty() && lockReasons.get(buildables.get(0)) != null) { getGUI() .showInformationMessage( buildables.get(0), StringTemplate.template("colonyPanel.unbuildable") .addName("%colony%", colony.getName()) .add("%object%", buildables.get(0).getNameKey())); command = FAIL; removeBuildable(buildables.remove(0)); } getController().setBuildQueue(colony, buildables); if (FAIL.equals(command)) { // Let the user reconsider. updateAllLists(); return; } else if (OK.equals(command)) { // do nothing? } else if (BUY.equals(command)) { getController().payForBuilding(colony); } else { logger.warning("Unsupported command " + command); } } getGUI().removeFromCanvas(this); }
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)); }
/** * Why would an IndianDemandMission be invalid with the given unit and colony. * * @param aiUnit The <code>AIUnit</code> to test. * @param colony The <code>Colony</code> to test. * @return A reason why the mission would be invalid with the unit and colony or null if none * found. */ private static String invalidColonyReason(AIUnit aiUnit, Colony colony) { String reason = invalidTargetReason(colony); if (reason != null) return reason; final Unit unit = aiUnit.getUnit(); final Player owner = unit.getOwner(); Player targetPlayer = colony.getOwner(); switch (owner.getStance(targetPlayer)) { case UNCONTACTED: case PEACE: case ALLIANCE: return "bad-stance"; case WAR: case CEASE_FIRE: Tension tension = unit.getHomeIndianSettlement().getAlarm(targetPlayer); if (tension != null && tension.getLevel().compareTo(Tension.Level.CONTENT) <= 0) return "happy"; break; } return null; }
public void testDeathByStarvation() { Game game = ServerTestHelper.startServerGame(getTestMap(marsh)); int consumption, production, unitsBeforeNewTurn = 3; Colony colony = getStandardColony(unitsBeforeNewTurn); ServerPlayer player = (ServerPlayer) colony.getOwner(); final Building townHall = colony.getBuilding(townHallType); for (Unit u : colony.getUnitList()) { u.setLocation(townHall); } colony.removeGoods(foodGoodsType); colony.invalidateCache(); consumption = colony.getFoodConsumption(); production = colony.getFoodProduction(); assertTrue( "Food consumption (" + consumption + ") should be higher than production (" + production + ")", consumption > production); assertEquals("No food stored in colony", 0, colony.getGoodsCount(foodType)); assertEquals("Wrong number of units in colony", unitsBeforeNewTurn, colony.getUnitCount()); ServerTestHelper.newTurn(); consumption = colony.getFoodConsumption(); production = colony.getFoodProduction(); assertTrue( "Food consumption (" + consumption + ") should be higher than production (" + production + ")", consumption > production); assertEquals("No food stored in colony", 0, colony.getGoodsCount(foodType)); assertEquals("Wrong number of units in colony", unitsBeforeNewTurn - 1, colony.getUnitCount()); }
public void testColonialRegular() { boolean selection = FreeColTestUtils.setStudentSelection(false); Game game = ServerTestHelper.startServerGame(getTestMap(true)); Colony colony = getSchoolColony(4, SchoolLevel.UNIVERSITY); Building university = colony.getBuilding(universityType); colony.getOwner().addAbility(new Ability(Ability.INDEPENDENCE_DECLARED)); Iterator<Unit> units = colony.getUnitIterator(); Unit regular = units.next(); regular.setType(colonialRegularType); Unit colonist = units.next(); colonist.setType(freeColonistType); colonist.setLocation(colony.getBuilding(townHallType)); regular.setLocation(university); assertEquals(colonist, regular.getStudent()); trainForTurns(colony, freeColonistType.getEducationTurns(veteranSoldierType)); assertEquals(veteranSoldierType, colonist.getType()); FreeColTestUtils.setStudentSelection(selection); }
/** {@inheritDoc} */ @Override public Mission doMission(LogBuilder lb) { lb.add(tag); String reason = invalidReason(); if (reason != null) return lbFail(lb, false, reason); final AIUnit aiUnit = getAIUnit(); final Unit unit = getUnit(); final IndianSettlement is = unit.getHomeIndianSettlement(); Direction d; while (!this.demanded) { Unit.MoveType mt = travelToTarget(getTarget(), null, lb); switch (mt) { case MOVE_HIGH_SEAS: case MOVE_NO_MOVES: case MOVE_ILLEGAL: return lbWait(lb); case MOVE_NO_REPAIR: return lbFail(lb, false, AIUNITDIED); case MOVE_NO_TILE: return this; case ATTACK_SETTLEMENT: // Arrived? d = unit.getTile().getDirection(getTarget().getTile()); if (d != null) break; // Yes, arrived at target // Fall through case ATTACK_UNIT: // Something is blocking our path Location blocker = resolveBlockage(aiUnit, getTarget()); if (blocker == null) { moveRandomly(tag, null); continue; } d = unit.getTile().getDirection(blocker.getTile()); if (AIMessage.askAttack(aiUnit, d)) { return lbAttack(lb, blocker); } continue; default: return lbMove(lb, mt); } // Load the goods. lbAt(lb); Colony colony = (Colony) getTarget(); Player enemy = colony.getOwner(); Goods goods = selectGoods(colony); GoodsType type = (goods == null) ? null : goods.getType(); int amount = (goods == null) ? 0 : goods.getAmount(); if (goods == null) { if (!enemy.checkGold(1)) { return lbDone(lb, false, "empty handed"); } amount = enemy.getGold() / 20; if (amount == 0) amount = enemy.getGold(); } this.demanded = AIMessage.askIndianDemand(aiUnit, colony, type, amount); if (this.demanded && (goods == null || hasTribute())) { if (goods == null) { return lbDone(lb, false, "accepted tribute ", amount, " gold"); } lb.add(", accepted tribute ", goods); return lbRetarget(lb); } // Consider attacking if not content. int unitTension = (is == null) ? 0 : is.getAlarm(enemy).getValue(); int tension = Math.max(unitTension, unit.getOwner().getTension(enemy).getValue()); d = unit.getTile().getDirection(colony.getTile()); if (tension >= Tension.Level.CONTENT.getLimit() && d != null) { if (AIMessage.askAttack(aiUnit, d)) lbAttack(lb, colony); } return lbDone(lb, false, "refused at ", colony); } // Take the goods home for (; ; ) { Unit.MoveType mt = travelToTarget(getTarget(), CostDeciders.avoidSettlementsAndBlockingUnits(), lb); switch (mt) { case MOVE: // Arrived break; case MOVE_HIGH_SEAS: case MOVE_NO_MOVES: case MOVE_ILLEGAL: return lbWait(lb); case MOVE_NO_REPAIR: return lbFail(lb, false, AIUNITDIED); case MOVE_NO_TILE: return this; default: return lbMove(lb, mt); } // Unload the goods lbAt(lb); GoodsContainer container = unit.getGoodsContainer(); for (Goods goods : container.getCompactGoods()) { Goods tribute = container.removeGoods(goods.getType()); is.addGoods(tribute); } return lbDone(lb, false, "unloaded tribute"); } }
/** * Selects the most desirable goods from the colony. * * @param target The colony. * @return The goods to demand. */ public Goods selectGoods(Colony target) { final Specification spec = getSpecification(); Tension.Level tension = getUnit().getOwner().getTension(target.getOwner()).getLevel(); int dx = spec.getInteger(GameOptions.NATIVE_DEMANDS) + 1; final GoodsType food = spec.getPrimaryFoodType(); Goods goods = null; int amount = capAmount(target.getGoodsCount(food), dx); if (tension.compareTo(Tension.Level.CONTENT) <= 0 && target.getGoodsCount(food) >= amount) { return new Goods(getGame(), target, food, amount); } else if (tension.compareTo(Tension.Level.DISPLEASED) <= 0) { Market market = target.getOwner().getMarket(); int value = 0; for (Goods currentGoods : target.getCompactGoods()) { int goodsValue = market.getSalePrice(currentGoods); if (currentGoods.getType().isFoodType() || currentGoods.getType().isMilitaryGoods()) { continue; } else if (goodsValue > value) { value = goodsValue; goods = currentGoods; } } if (goods != null) { goods.setAmount(capAmount(goods.getAmount(), dx)); return goods; } } else { // Military goods for (GoodsType preferred : spec.getGoodsTypeList()) { if (preferred.isMilitaryGoods()) { amount = target.getGoodsCount(preferred); if (amount > 0) { return new Goods(getGame(), target, preferred, capAmount(amount, dx)); } } } // Storable building materials (what do the natives need tools for?) for (GoodsType preferred : spec.getStorableGoodsTypeList()) { if (preferred.isBuildingMaterial()) { amount = target.getGoodsCount(preferred); if (amount > 0) { return new Goods(getGame(), target, preferred, capAmount(amount, dx)); } } } // Trade goods for (GoodsType preferred : spec.getStorableGoodsTypeList()) { if (preferred.isTradeGoods()) { amount = target.getGoodsCount(preferred); if (amount > 0) { return new Goods(getGame(), target, preferred, capAmount(amount, dx)); } } } // Refined goods for (GoodsType preferred : spec.getStorableGoodsTypeList()) { if (preferred.isRefined()) { amount = target.getGoodsCount(preferred); if (amount > 0) { return new Goods(getGame(), target, preferred, capAmount(amount, dx)); } } } } // Have not found what we want Market market = target.getOwner().getMarket(); int value = 0; for (Goods currentGoods : target.getCompactGoods()) { int goodsValue = market.getSalePrice(currentGoods); if (goodsValue > value) { value = goodsValue; goods = currentGoods; } } if (goods != null) { goods.setAmount(capAmount(goods.getAmount(), dx)); } return goods; }