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()); }
/** * 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); }
/** 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); }
/** * Why is this mission invalid with a given colony target, given that intermediate colonies are * excluded. * * @param aiUnit The <code>AIUnit</code> to check. * @param colony The potential target <code>Colony</code>. * @return A reason for mission invalidity, or null if none found. */ private static String invalidFullColonyReason(AIUnit aiUnit, Colony colony) { String reason = invalidTargetReason(colony, aiUnit.getUnit().getOwner()); return (reason != null) ? reason : (!aiUnit.getUnit().canCashInTreasureTrain(colony.getTile())) ? "cashin-impossible-at-location" : null; }
/** {@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"); } }
/** * Progress in teaching is bound to the teacher and not the learner. * * <p>Moving a teacher inside the colony should not reset its training. */ public void testMoveTeacherInside() { boolean selection = FreeColTestUtils.setStudentSelection(false); Game game = ServerTestHelper.startServerGame(getTestMap(true)); Colony colony = getSchoolColony(4, SchoolLevel.UNIVERSITY); // prevent starvation colony.addGoods(foodType, 100); Building university = colony.getBuilding(universityType); Iterator<Unit> units = colony.getUnitIterator(); Unit colonist = units.next(); colonist.setType(freeColonistType); colonist.setLocation(colony.getBuilding(townHallType)); Unit criminal = units.next(); criminal.setType(pettyCriminalType); criminal.setLocation(colony.getBuilding(townHallType)); Unit teacher1 = units.next(); teacher1.setType(expertOreMinerType); Unit teacher2 = units.next(); teacher2.setType(masterCarpenterType); // The carpenter is set in the school before the miner. // In this case, the colonist will become a miner (and the criminal // will become a servant). teacher2.setLocation(university); teacher1.setLocation(university); assertEquals(4, teacher1.getNeededTurnsOfTraining()); assertEquals(4, teacher2.getNeededTurnsOfTraining()); // wait a little ServerTestHelper.newTurn(); ServerTestHelper.newTurn(); assertEquals(2, teacher1.getTurnsOfTraining()); assertEquals(2, teacher2.getTurnsOfTraining()); assertEquals(1, getUnitList(colony, freeColonistType).size()); assertEquals(1, getUnitList(colony, pettyCriminalType).size()); assertEquals(1, getUnitList(colony, expertOreMinerType).size()); assertEquals(1, getUnitList(colony, masterCarpenterType).size()); // Now we want the colonist to be a carpenter. We just want to // shuffle the teachers. Move them out of the school. teacher2.setLocation(colony.getWorkLocationFor(teacher2, grainType)); teacher1.setLocation(colony.getTile()); assertNull(teacher1.getStudent()); assertNull(teacher2.getStudent()); // Moving outside school resets training ServerTestHelper.newTurn(); assertEquals(0, teacher1.getTurnsOfTraining()); assertEquals(0, teacher2.getTurnsOfTraining()); assertEquals(1, getUnitList(colony, freeColonistType).size()); assertEquals(1, getUnitList(colony, pettyCriminalType).size()); // Move teachers back to school, miner first to pick up the criminal teacher1.setLocation(university); teacher2.setLocation(university); ServerTestHelper.newTurn(); assertEquals(1, teacher1.getTurnsOfTraining()); assertEquals(1, teacher2.getTurnsOfTraining()); assertEquals(1, getUnitList(colony, freeColonistType).size()); assertEquals(1, getUnitList(colony, pettyCriminalType).size()); ServerTestHelper.newTurn(); ServerTestHelper.newTurn(); ServerTestHelper.newTurn(); assertEquals(0, teacher1.getTurnsOfTraining()); assertEquals(0, teacher2.getTurnsOfTraining()); // Teacher1's student (criminal) should be a servant now // Teacher2's student (colonist) should be a carpenter now assertEquals(0, getUnitList(colony, freeColonistType).size()); assertEquals(0, getUnitList(colony, pettyCriminalType).size()); assertEquals(1, getUnitList(colony, indenturedServantType).size()); assertEquals(1, getUnitList(colony, expertOreMinerType).size()); assertEquals(2, getUnitList(colony, masterCarpenterType).size()); FreeColTestUtils.setStudentSelection(selection); }