@Override
  public void loadFromSnapshot(Document doc, Element node) {
    NodeList nl = node.getElementsByTagName("player");
    for (int i = 0; i < nl.getLength(); i++) {
      Element playerEl = (Element) nl.item(i);
      Player player = game.getPlayer(Integer.parseInt(playerEl.getAttribute("index")));
      castles.put(player, Integer.parseInt(playerEl.getAttribute("castles")));
    }

    nl = node.getElementsByTagName("castle");
    for (int i = 0; i < nl.getLength(); i++) {
      Element castleEl = (Element) nl.item(i);
      Position pos = XMLUtils.extractPosition(castleEl);
      Location loc = Location.valueOf(castleEl.getAttribute("location"));
      Castle castle = convertCityToCastle(pos, loc, true);
      boolean isNew = XMLUtils.attributeBoolValue(castleEl, "new");
      boolean isCompleted = XMLUtils.attributeBoolValue(castleEl, "completed");
      if (isNew) {
        newCastles.add(castle);
      } else if (isCompleted) {
        emptyCastles.add(castle);
      } else {
        scoreableCastleVicinity.put(castle, castle.getVicinity());
      }
    }
  }
 @Override
 public void turnPartCleanUp() {
   for (Castle castle : newCastles) {
     scoreableCastleVicinity.put(castle, castle.getVicinity());
   }
   newCastles.clear();
   castleScore.clear();
 }
Example #3
0
 private void scoreCastle(Castle castle, int points) {
   List<Meeple> meeples = castle.getMeeples();
   if (meeples.isEmpty()) meeples = castle.getSecondFeature().getMeeples();
   Meeple m = meeples.get(0); // all meeples must share same owner
   m.getPlayer().addPoints(points, PointCategory.CASTLE);
   game.post(new ScoreEvent(m.getFeature(), points, PointCategory.CASTLE, m));
   m.undeploy(false);
 }
  private Castle replaceCityWithCastle(Tile tile, Location loc) {
    ListIterator<Feature> iter = tile.getFeatures().listIterator();
    City city = null;
    while (iter.hasNext()) {
      Feature feature = iter.next();
      if (feature.getLocation() == loc) {
        city = (City) feature;
        break;
      }
    }
    List<Meeple> meeples =
        new ArrayList<>(city.getMeeples()); // collection copy required!!! undeploy modify it
    for (Meeple m : meeples) {
      m.undeploy();
    }
    Castle castle = new Castle();
    castle.setTile(tile);
    castle.setId(game.idSequnceNextVal());
    castle.setLocation(loc.rotateCCW(tile.getRotation()));
    iter.set(castle);

    for (Feature f : tile.getFeatures()) { // replace also city references
      if (f instanceof Farm) {
        Farm farm = (Farm) f;
        Feature[] adjoining = farm.getAdjoiningCities();
        if (adjoining != null) {
          for (int i = 0; i < adjoining.length; i++) {
            if (adjoining[i] == city) {
              adjoining[i] = castle;
              break;
            }
          }
        }
      }
    }

    FeaturePointer fp = new FeaturePointer(tile.getPosition(), loc);
    for (Meeple m : meeples) {
      if (m.getPlayer() == game.getActivePlayer() && m.isDeploymentAllowed(castle).result) {
        m.deploy(fp);
      }
    }
    return castle;
  }
 private void checkCastleVicinity(Iterable<Position> triggerPositions, int score) {
   for (Position p : triggerPositions) {
     for (Entry<Castle, Position[]> entry : scoreableCastleVicinity.entrySet()) {
       Position[] vicinity = entry.getValue();
       for (int i = 0; i < vicinity.length; i++) {
         if (vicinity[i].equals(p)) {
           Castle master = entry.getKey();
           Integer currentCastleScore = castleScore.get(master);
           if (currentCastleScore == null || currentCastleScore < score) {
             castleScore.put(master, score);
             // chain reaction, one completed castle triggers another
             checkCastleVicinity(Arrays.asList(master.getCastleBase()), score);
           }
           break;
         }
       }
     }
   }
 }
 private Castle convertCityToCastle(Position pos, Location loc, boolean loadFromSnaphot) {
   Castle castle1 = replaceCityWithCastle(getBoard().get(pos), loc);
   Castle castle2 = replaceCityWithCastle(getBoard().get(pos.add(loc)), loc.rev());
   castle1.getEdges()[0] = castle2;
   castle2.getEdges()[0] = castle1;
   if (!loadFromSnaphot) {
     newCastles.add(castle1.getMaster());
   }
   game.post(new CastleDeployedEvent(game.getActivePlayer(), castle1, castle2));
   return castle1.getMaster();
 }
 private Element createCastleXmlElement(Document doc, Castle castle) {
   Element el = doc.createElement("castle");
   el.setAttribute("location", castle.getLocation().toString());
   XMLUtils.injectPosition(el, castle.getTile().getPosition());
   return el;
 }