/** 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); }
private int getMinimumIndex(BuildableType buildableType) { ListModel buildQueue = buildQueueList.getModel(); if (buildableType instanceof UnitType) { if (colony.canBuild(buildableType)) { return 0; } else { for (int index = 0; index < buildQueue.getSize(); index++) { if (((BuildableType) buildQueue.getElementAt(index)) .hasAbility(Ability.BUILD, buildableType)) { return index + 1; } } } } else if (buildableType instanceof BuildingType) { BuildingType upgradesFrom = ((BuildingType) buildableType).getUpgradesFrom(); if (upgradesFrom == null) { return 0; } else { Building building = colony.getBuilding((BuildingType) buildableType); BuildingType buildingType = (building == null) ? null : building.getType(); if (buildingType == upgradesFrom) { return 0; } else { for (int index = 0; index < buildQueue.getSize(); index++) { if (upgradesFrom.equals(buildQueue.getElementAt(index))) { return index + 1; } } } } } return UNABLE_TO_BUILD; }
/** Tests completion of buildable */ public void testBuildingCompletion() { Game game = ServerTestHelper.startServerGame(getTestMap(true)); Colony colony = getStandardColony(); ServerBuilding initialWarehouse = new ServerBuilding(getGame(), colony, depotType); colony.addBuilding(initialWarehouse); assertTrue("Colony should be able to build warehouse", colony.canBuild(warehouseType)); // Simulate that the build is done colony.setCurrentlyBuilding(warehouseType); colony.addGoods(hammerGoodsType, 90); assertFalse( "Colony should not have warehouse", colony.getWarehouse().getType() == warehouseType); ServerTestHelper.newTurn(); assertTrue("Colony should have warehouse", colony.getWarehouse().getType() == warehouseType); }
private int getMaximumIndex(BuildableType buildableType) { ListModel buildQueue = buildQueueList.getModel(); final int buildQueueLastPos = buildQueue.getSize(); boolean canBuild = false; if (colony.canBuild(buildableType)) { canBuild = true; } if (buildableType instanceof UnitType) { // does not depend on anything, nothing depends on it // can be built at any time if (canBuild) { return buildQueueLastPos; } // check for building in queue that allows builting this unit for (int index = 0; index < buildQueue.getSize(); index++) { BuildableType toBuild = (BuildableType) buildQueue.getElementAt(index); if (toBuild == buildableType) { continue; } if (toBuild.hasAbility(Ability.BUILD, buildableType)) { return buildQueueLastPos; } } return UNABLE_TO_BUILD; } if (buildableType instanceof BuildingType) { BuildingType upgradesFrom = ((BuildingType) buildableType).getUpgradesFrom(); BuildingType upgradesTo = ((BuildingType) buildableType).getUpgradesTo(); // does not depend on nothing, but still cannot be built if (!canBuild && upgradesFrom == null) { return UNABLE_TO_BUILD; } // if can be built and does not have any upgrade, // then it can be built at any time if (canBuild && upgradesTo == null) { return buildQueueLastPos; } // if can be built, does not depend on anything, mark upgradesfrom as found boolean foundUpgradesFrom = canBuild ? true : false; for (int index = 0; index < buildQueue.getSize(); index++) { BuildableType toBuild = (BuildableType) buildQueue.getElementAt(index); if (toBuild == buildableType) { continue; } if (!canBuild && !foundUpgradesFrom && upgradesFrom.equals(toBuild)) { foundUpgradesFrom = true; // nothing else to upgrade this building to if (upgradesTo == null) { return buildQueueLastPos; } } // found a building it upgrades to, cannot go to or beyond this position if (foundUpgradesFrom && upgradesTo != null && upgradesTo.equals(toBuild)) { return index; } // Don't go past a unit this building can build. if (buildableType.hasAbility(Ability.BUILD, toBuild)) { return index; } } return buildQueueLastPos; } return UNABLE_TO_BUILD; }