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());
  }
示例#2
0
  public void testUniversity() {
    Game game = ServerTestHelper.startServerGame(getTestMap(true));

    // otherwise this test will crash and burn
    boolean selection = FreeColTestUtils.setStudentSelection(false);

    Colony colony = getSchoolColony(4, SchoolLevel.UNIVERSITY);
    assertEquals(4, colony.getUnitCount());
    Building university = colony.getBuilding(universityType);
    assertNotNull(university);
    Iterator<Unit> units = colony.getUnitIterator();

    Unit colonist = units.next();
    colonist.setType(freeColonistType);
    colonist.setLocation(colony.getBuilding(townHallType));

    Unit elder = units.next();
    assertEquals(elder.getType(), elderStatesmanType);

    elder.setLocation(university);
    assertEquals(elder.getStudent(), colonist);

    trainForTurns(colony, elder.getNeededTurnsOfTraining());
    assertEquals(elderStatesmanType, colonist.getType());

    FreeColTestUtils.setStudentSelection(selection);
  }
  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 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 testLibertyAndImmigration() {
    Game game = ServerTestHelper.startServerGame(getTestMap(true));

    final int population = 3;
    Colony colony = getStandardColony(population);

    ServerBuilding townHall = (ServerBuilding) colony.getBuilding(townHallType);
    Unit statesman = colony.getUnitList().get(0);
    townHall.setWorkFor(statesman);
    assertEquals(bellsType, statesman.getWorkType());

    ServerBuilding church = (ServerBuilding) colony.getBuilding(chapelType);
    church.upgrade();
    Unit preacher = colony.getUnitList().get(1);
    church.setWorkFor(preacher);
    assertEquals(crossesType, preacher.getWorkType());

    assertEquals(0, colony.getGoodsCount(bellsType));
    ServerTestHelper.newTurn();

    int bells = 3;
    assertEquals(population, colony.getUnitCount());
    assertEquals(bells, colony.getNetProductionOf(bellsType));
    assertEquals(bells, colony.getGoodsCount(bellsType));

    colony.addGoods(bellsType, 7);
    bells += 7;
    assertEquals(bells, colony.getGoodsCount(bellsType));
    assertEquals(bells, colony.getLiberty());

    colony.removeGoods(bellsType, 5);
    bells -= 5;
    assertEquals(bells, colony.getGoodsCount(bellsType));
    assertEquals(bells, colony.getLiberty());

    int crosses = colony.getTotalProductionOf(crossesType) - colony.getConsumptionOf(crossesType);
    assertEquals(crosses, colony.getNetProductionOf(crossesType));
    assertEquals(crosses, colony.getGoodsCount(crossesType));
    assertEquals(crosses, colony.getImmigration());

    colony.addGoods(crossesType, 7);
    crosses += 7;
    assertEquals(crosses, colony.getGoodsCount(crossesType));
    assertEquals(crosses, colony.getImmigration());

    colony.removeGoods(crossesType, 5);
    crosses -= 5;
    assertEquals(crosses, colony.getGoodsCount(crossesType));
    assertEquals(crosses, colony.getImmigration());
  }
  public void testEqualFoodProductionConsumptionCase() {
    Game game = ServerTestHelper.startServerGame(getTestMap(desert));

    // Setting test colony
    Tile colonyTile = game.getMap().getTile(5, 8);
    Colony colony =
        FreeColTestUtils.getColonyBuilder().colonyTile(colonyTile).initialColonists(1).build();

    // Set the food production of the center tile of the colony to 2
    // This will be the only food production of the colony
    List<AbstractGoods> colonyTileProduction = colonyTile.getType().getPossibleProduction(true);
    for (int i = 0; i < colonyTileProduction.size(); i++) {
      AbstractGoods production = colonyTileProduction.get(i);
      if (production.getType() == foodGoodsType) {
        production.setAmount(2);
        break;
      }
    }
    Unit unit = colony.getUnitList().get(0);
    unit.setLocation(colony.getWorkLocationFor(unit, bellsType));

    // Verify that there is enough food stored
    colony.addGoods(foodGoodsType, colony.getFoodConsumption() * 2);

    assertEquals(
        "Production not equal to consumption",
        colony.getFoodConsumption(),
        colony.getFoodProduction());

    int colonists = colony.getUnitCount();
    assertEquals("Unexpected change of colonists in colony", colonists, colony.getUnitCount());

    assertEquals(
        "Unexpected change of production/consumption ratio",
        colony.getFoodProduction(),
        colony.getFoodConsumption());
  }
示例#7
0
  @SuppressWarnings("unchecked") // FIXME in Java7
  public BuildQueuePanel(FreeColClient freeColClient, GUI gui, Colony colony) {

    super(
        freeColClient,
        gui,
        new MigLayout("wrap 3", "[260:][390:, fill][260:]", "[][][300:400:][]"));
    this.colony = colony;
    this.unitCount = colony.getUnitCount();
    featureContainer = new FeatureContainer();

    for (UnitType unitType : getSpecification().getUnitTypeList()) {
      if (unitType.needsGoodsToBuild() && !unitType.hasAbility(Ability.BORN_IN_COLONY)) {
        buildableUnits.add(unitType); // can be built
      }
    }

    DefaultListModel current = new DefaultListModel();
    for (BuildableType type : colony.getBuildQueue()) {
      current.addElement(type);
      FeatureContainer.addFeatures(featureContainer, type);
    }

    cellRenderer = getCellRenderer();

    // remove previous listeners
    for (ItemListener listener : compact.getItemListeners()) {
      compact.removeItemListener(listener);
    }
    compact.setText(Messages.message("colonyPanel.compactView"));
    compact.addItemListener(this);

    // remove previous listeners
    for (ItemListener listener : showAll.getItemListeners()) {
      showAll.removeItemListener(listener);
    }
    showAll.setText(Messages.message("colonyPanel.showAll"));
    showAll.addItemListener(this);

    buildQueueList = new JList(current);
    buildQueueList.setTransferHandler(buildQueueHandler);
    buildQueueList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    buildQueueList.setDragEnabled(true);
    buildQueueList.setCellRenderer(cellRenderer);
    buildQueueList.addMouseListener(new BuildQueueMouseAdapter(false));

    Action deleteAction =
        new AbstractAction() {
          @SuppressWarnings("deprecation") // FIXME in Java7
          public void actionPerformed(ActionEvent e) {
            for (Object type : buildQueueList.getSelectedValues()) {
              removeBuildable(type);
            }
            updateAllLists();
          }
        };

    buildQueueList.getInputMap().put(KeyStroke.getKeyStroke("DELETE"), "delete");
    buildQueueList.getActionMap().put("delete", deleteAction);

    Action addAction =
        new AbstractAction() {
          @SuppressWarnings("deprecation") // FIXME in Java7
          public void actionPerformed(ActionEvent e) {
            DefaultListModel model = (DefaultListModel) buildQueueList.getModel();
            for (Object type : ((JList) e.getSource()).getSelectedValues()) {
              model.addElement(type);
            }
            updateAllLists();
          }
        };

    BuildQueueMouseAdapter adapter = new BuildQueueMouseAdapter(true);
    DefaultListModel units = new DefaultListModel();
    unitList = new JList(units);
    unitList.setTransferHandler(buildQueueHandler);
    unitList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    unitList.setDragEnabled(true);
    unitList.setCellRenderer(cellRenderer);
    unitList.addMouseListener(adapter);

    unitList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "add");
    unitList.getActionMap().put("add", addAction);

    DefaultListModel buildings = new DefaultListModel();
    buildingList = new JList(buildings);
    buildingList.setTransferHandler(buildQueueHandler);
    buildingList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    buildingList.setDragEnabled(true);
    buildingList.setCellRenderer(cellRenderer);
    buildingList.addMouseListener(adapter);

    buildingList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "add");
    buildingList.getActionMap().put("add", addAction);

    JLabel headLine = new JLabel(Messages.message("colonyPanel.buildQueue"));
    headLine.setFont(bigHeaderFont);

    buyBuilding = new JButton(Messages.message("colonyPanel.buyBuilding"));
    buyBuilding.setActionCommand(BUY);
    buyBuilding.addActionListener(this);

    constructionPanel = new ConstructionPanel(gui, colony, false);
    constructionPanel.setOpaque(false);
    StringTemplate buildingNothing =
        StringTemplate.template("colonyPanel.currentlyBuilding").add("%buildable%", "nothing");
    constructionPanel.setDefaultLabel(buildingNothing);

    updateAllLists();

    add(headLine, "span 3, align center, wrap 40");
    add(new JLabel(Messages.message("colonyPanel.units")), "align center");
    add(new JLabel(Messages.message("colonyPanel.buildQueue")), "align center");
    add(new JLabel(Messages.message("colonyPanel.buildings")), "align center");
    add(new JScrollPane(unitList), "grow");
    add(constructionPanel, "split 2, flowy");
    add(new JScrollPane(buildQueueList), "grow");
    add(new JScrollPane(buildingList), "grow, wrap 20");
    add(buyBuilding, "span, split 4");
    add(compact);
    add(showAll);
    add(okButton, "tag ok");
  }