@Override
 public void saveTileToSnapshot(Tile tile, Document doc, Element tileNode) {
   if (tile.getBridge() != null) {
     Location realLoc = tile.getBridge().getRawLocation();
     tileNode.setAttribute("bridge", realLoc.toString());
   }
 }
Example #2
0
 /** merge this to another tile - method argument is tile placed before */
 protected void merge(Tile tile, Location loc) {
   if (logger.isDebugEnabled()) logger.debug("Merging " + id + " with " + tile.getId());
   Location oppositeLoc = loc.rev();
   MultiTileFeature oppositePiece = (MultiTileFeature) tile.getFeaturePartOf(oppositeLoc);
   if (oppositePiece != null) {
     if (isAbbeyTile()) {
       oppositePiece.setAbbeyEdge(oppositeLoc);
     } else {
       MultiTileFeature thisPiece = (MultiTileFeature) getFeaturePartOf(loc);
       oppositePiece.setEdge(oppositeLoc, thisPiece);
       thisPiece.setEdge(loc, oppositePiece);
     }
   }
   for (int i = 0; i < 2; i++) {
     Location halfSide = i == 0 ? loc.getLeftFarm() : loc.getRightFarm();
     Location oppositeHalfSide = halfSide.rev();
     oppositePiece = (MultiTileFeature) tile.getFeaturePartOf(oppositeHalfSide);
     if (oppositePiece != null) {
       if (isAbbeyTile()) {
         oppositePiece.setAbbeyEdge(oppositeHalfSide);
       } else {
         MultiTileFeature thisPiece = (MultiTileFeature) getFeaturePartOf(halfSide);
         oppositePiece.setEdge(oppositeHalfSide, thisPiece);
         thisPiece.setEdge(halfSide, oppositePiece);
       }
     }
   }
 }
Example #3
0
  public void tilePlaced(Tile tile, TileLayer tileLayer) {
    Position p = tile.getPosition();

    removeLayer(AbstractTilePlacementLayer.class);
    removeLayer(PlacementHistory.class);

    if (p.x == left) --left;
    if (p.x == right) ++right;
    if (p.y == top) --top;
    if (p.y == bottom) ++bottom;

    tileLayer.tilePlaced(tile);

    if (client.getSettings().isShowHistory()) {
      showRecentHistory();
    }
    boolean initialPlacement =
        client.getActivePlayer() == null; // if active player is null we are placing initial tiles
    if ((!initialPlacement && !client.isClientActive())
        || (initialPlacement && tile.equals(client.getGame().getCurrentTile()))) {
      getAnimationService()
          .registerAnimation(tile.getPosition(), new RecentPlacement(tile.getPosition()));
    }
    repaint();
  }
Example #4
0
	private void createTileElements(Game game) {
		tileElemens = new HashMap<Position, Element>();
		Element parent = doc.createElement("tiles");
		if (game.getCurrentTile() != null) {
			parent.setAttribute("next", game.getCurrentTile().getId());
		}
		root.appendChild(parent);
		//for()
		for(String group : game.getTilePack().getGroups()) {
			Element el = doc.createElement("group");
			el.setAttribute("name", group);
			el.setAttribute("active", "" + game.getTilePack().isGroupActive(group));
			parent.appendChild(el);
		}
		for(Tile tile : game.getBoard().getAllTiles()) {
			Element el = doc.createElement("tile");
			el.setAttribute("name", tile.getId());
			el.setAttribute("rotation", tile.getRotation().name());
			XmlUtils.injectPosition(el, tile.getPosition());
			parent.appendChild(el);
			tileElemens.put(tile.getPosition(), el);
		}
		for(String tileId : game.getBoard().getDiscardedTiles()) {
			Element el = doc.createElement("discard");
			el.setAttribute("name", tileId);
			parent.appendChild(el);
		}
	}
Example #5
0
 private boolean makeDebugDraw() {
   if (debugTiles != null && debugTiles.size() > 0) { // for debug purposes only
     String tileId = debugTiles.remove(0);
     if (tileId.equals(DEBUG_END_OF_PACK)) {
       next(GameOverPhase.class);
       return true;
     }
     TilePack tilePack = getTilePack();
     Tile tile = tilePack.drawTile(tileId);
     if (tile == null) {
       logger.warn("Invalid debug draw id: " + tileId);
     } else {
       boolean riverActive =
           tilePack.getGroupState("river-start") == TileGroupState.ACTIVE
               || tilePack.getGroupState("river") == TileGroupState.ACTIVE;
       if (game.hasCapability(RiverCapability.class) && tile.getRiver() == null && riverActive) {
         game.getCapability(RiverCapability.class).activateNonRiverTiles();
         tilePack.setGroupState("river-start", TileGroupState.RETIRED);
         game.setCurrentTile(tile); // recovery from lake placement
       }
       nextTile(tile);
       return true;
     }
   }
   return false;
 }
 private BridgeAction prepareTileBridgeAction(Tile tile, BridgeAction action, Location bridgeLoc) {
   if (isBridgePlacementAllowed(tile, tile.getPosition(), bridgeLoc)) {
     if (action == null) action = new BridgeAction();
     action.add(new FeaturePointer(tile.getPosition(), bridgeLoc));
   }
   return action;
 }
 public void deployBridge(Position pos, Location loc) {
   Tile tile = getBoard().get(pos);
   if (!tile.isBridgeAllowed(loc)) {
     throw new IllegalArgumentException("Cannot deploy " + loc + " bridge on " + pos);
   }
   bridgeUsed = true;
   tile.placeBridge(loc);
   game.post(new BridgeDeployedEvent(game.getActivePlayer(), pos, loc));
 }
Example #8
0
 private void scoreCompletedNearAbbey(Position pos) {
   for (Entry<Location, Tile> e : getBoard().getAdjacentTilesMap(pos).entrySet()) {
     Tile tile = e.getValue();
     Feature feature = tile.getFeaturePartOf(e.getKey().rev());
     if (feature instanceof Completable) {
       scoreCompleted((Completable) feature, false);
     }
   }
 }
 public void update() {
   history.clear();
   int i = placedTilesView.size();
   int limit = getGame().getAllPlayers().length;
   for (Tile t : placedTilesView) {
     if (i <= limit) {
       history.put(t.getPosition(), "" + i);
     }
     i--;
   }
 }
 private boolean isBridgePlacementAllowed(Tile tile, Position p, Location bridgeLoc) {
   if (!tile.isBridgeAllowed(bridgeLoc)) return false;
   for (Entry<Location, Tile> e : getBoard().getAdjacentTilesMap(p).entrySet()) {
     Location rel = e.getKey();
     if (rel.intersect(bridgeLoc) != null) {
       Tile adjacent = e.getValue();
       char adjacentSide = adjacent.getEdge(rel.rev());
       if (adjacentSide != 'R') return false;
     }
   }
   return true;
 }
 private BridgeAction prepareBridgeAction() {
   BridgeAction action = null;
   Tile tile = game.getCurrentTile();
   action = prepareTileBridgeAction(tile, action, Location.NS);
   action = prepareTileBridgeAction(tile, action, Location.WE);
   for (Entry<Location, Tile> entry :
       getBoard().getAdjacentTilesMap(tile.getPosition()).entrySet()) {
     Tile adjacent = entry.getValue();
     Location rel = entry.getKey();
     action = prepareTileBridgeAction(adjacent, action, getBridgeLocationForAdjacent(rel));
   }
   return action;
 }
 @Override
 public boolean visit(Feature feature) {
   cloister = (Cloister) feature;
   Position pos = cloister.getTile().getPosition();
   List<Tile> neigbouringTiles = game.getBoard().getAdjacentAndDiagonalTiles(pos);
   neigbouringTilesCount = neigbouringTiles.size();
   if (lbCap != null) {
     collectLittleBuildings(cloister.getTile().getPosition());
     for (Tile tile : neigbouringTiles) {
       collectLittleBuildings(tile.getPosition());
     }
   }
   return true;
 }
Example #13
0
 @Override
 public boolean visit(Feature feature) {
   City city = (City) feature;
   if (city.isBesieged()) { // cloister must border Cathar tile
     isBesieged = true;
     Position p = city.getTile().getPosition();
     for (Tile tile : getBoard().getAdjacentAndDiagonalTiles(p)) {
       if (tile.hasCloister()) {
         cloisterNearby = true;
         return false; // do not continue, besieged cloister exists
       }
     }
   }
   return true;
 }
 @Override
 public void loadTileFromSnapshot(Tile tile, Element tileNode) {
   if (tileNode.hasAttribute("bridge")) {
     Location loc = Location.valueOf(tileNode.getAttribute("bridge"));
     tile.placeBridge(loc);
   }
 }
Example #15
0
 private void scoreCompletedOnTile(Tile tile) {
   for (Feature feature : tile.getFeatures()) {
     if (feature instanceof Completable) {
       scoreCompleted((Completable) feature, true);
     }
   }
 }
Example #16
0
 static TileSymmetry forTile(Tile tile) {
   if (tile.getFlier() != null) {
     return TileSymmetry.NONE;
   }
   if (tile.getWindRose() != null) {
     return TileSymmetry.NONE;
   }
   TileSymmetry symmetry = countBaseSymmetry(tile);
   Location river = tile.getRiver();
   if (river != null) {
     if (river == Location.WE || river == Location.NS) {
       if (symmetry == TileSymmetry.S4) symmetry = TileSymmetry.S2;
     } else {
       symmetry = TileSymmetry.NONE;
     }
   }
   return symmetry;
 }
  public BridgeAction prepareMandatoryBridgeAction() {
    Tile tile = game.getCurrentTile();
    for (Entry<Location, Tile> entry :
        getBoard().getAdjacentTilesMap(tile.getPosition()).entrySet()) {
      Tile adjacent = entry.getValue();
      Location rel = entry.getKey();

      char adjacentSide = adjacent.getEdge(rel.rev());
      char tileSide = tile.getEdge(rel);
      if (tileSide != adjacentSide) {
        Location bridgeLoc = getBridgeLocationForAdjacent(rel);
        BridgeAction action = prepareTileBridgeAction(tile, null, bridgeLoc);
        if (action != null) return action;
        return prepareTileBridgeAction(adjacent, null, bridgeLoc);
      }
    }
    throw new IllegalStateException();
  }
  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;
  }
Example #19
0
 private static TileSymmetry countBaseSymmetry(Tile tile) {
   for (Feature piece : tile.getFeatures()) {
     if (piece instanceof Road || piece instanceof City) {
       Feature opposite = tile.getFeature(piece.getLocation().rev());
       if (opposite == null || !opposite.getClass().equals(piece.getClass())) {
         return TileSymmetry.NONE;
       }
     }
   }
   for (Feature piece : tile.getFeatures()) {
     if (piece instanceof Road || piece instanceof City) {
       Feature opposite = tile.getFeature(piece.getLocation().rotateCW(Rotation.R90));
       if (opposite == null || !opposite.getClass().equals(piece.getClass())) {
         return TileSymmetry.S2;
       }
     }
   }
   return TileSymmetry.S4;
 }
Example #20
0
  @Override
  public void enter() {
    Position pos = getTile().getPosition();

    // TODO separate event here ??? and move this code to abbey and mayor game
    if (barnCap != null) {
      Map<City, CityScoreContext> cityCache = new HashMap<>();
      for (Feature feature : getTile().getFeatures()) {
        if (feature instanceof Farm) {
          scoreFollowersOnBarnFarm((Farm) feature, cityCache);
        }
      }
    }

    scoreCompletedOnTile(getTile());
    if (getTile().isAbbeyTile()) {
      scoreCompletedNearAbbey(pos);
    }

    if (tunnelCap != null) {
      Road r = tunnelCap.getPlacedTunnel();
      if (r != null) {
        scoreCompleted(r, true);
      }
    }

    for (Tile neighbour : getBoard().getAdjacentAndDiagonalTiles(pos)) {
      Cloister cloister = neighbour.getCloister();
      if (cloister != null) {
        scoreCompleted(cloister, false);
      }
    }

    if (castleCap != null) {
      for (Entry<Castle, Integer> entry : castleCap.getCastleScore().entrySet()) {
        scoreCastle(entry.getKey(), entry.getValue());
      }
    }

    alredyScored.clear();
    next();
  }
Example #21
0
 protected void deploy(Tile tile, Location loc, Feature feature) {
   DeploymentCheckResult check = isDeploymentAllowed(feature);
   if (!check.result) {
     throw new IllegalArgumentException(check.error);
   }
   feature.addMeeple(this);
   setPosition(tile.getPosition());
   setLocation(loc);
   setFeature(feature);
   game.post(new MeepleEvent(MeepleEvent.DEPLOY, this));
 }
Example #22
0
 public void tileEvent(TileEvent ev) {
   Tile tile = ev.getTile();
   if (ev.getType() == TileEvent.PLACEMENT) {
     ResourceManager resourceManager = getClient().getResourceManager();
     Set<Location> farmLocations = new HashSet<>();
     for (Feature f : tile.getFeatures()) {
       if (f instanceof Farm) {
         farmLocations.add(f.getLocation());
       }
     }
     if (farmLocations.isEmpty()) return;
     Map<Location, Area> tAreas = resourceManager.getFeatureAreas(tile, FULL_SIZE, farmLocations);
     areas.put(tile, tAreas);
     refreshHints();
   }
   if (ev.getType() == TileEvent.REMOVE) {
     areas.remove(tile);
     refreshHints();
   }
 }
  private boolean isTilePlacementWithOneAdjacentBridgeAllowed(Tile tile, Position p) {
    boolean bridgeUsed = false;
    for (Entry<Location, Tile> e : getBoard().getAdjacentTilesMap(p).entrySet()) {
      Tile adjacent = e.getValue();
      Location rel = e.getKey();

      char tileSide = tile.getEdge(rel);
      char adjacentSide = adjacent.getEdge(rel.rev());

      if (tileSide != adjacentSide) {
        if (bridgeUsed) return false;
        if (tileSide != 'R') return false;

        Location bridgeLoc = getBridgeLocationForAdjacent(rel);
        if (!isBridgePlacementAllowed(adjacent, adjacent.getPosition(), bridgeLoc)) return false;
        bridgeUsed = true;
      }
    }
    return bridgeUsed; // ok if exactly one bridge is used
  }
Example #24
0
  @Override
  public void paint(Graphics2D g2) {
    // TODO nice shadow
    if (!getClient().getGridPanel().containsDecoration(AbstractTilePlacementLayer.class)) {
      g2.setColor(Color.WHITE);
      int squareSize = getSquareSize(), thickness = squareSize / 11;
      for (Tile tile : placedTiles) {
        Position p = tile.getPosition();
        int x = getOffsetX(p), y = getOffsetY(p);
        g2.fillRect(
            x - thickness, y - thickness, squareSize + 2 * thickness, squareSize + 2 * thickness);
      }
    }

    for (Tile tile : placedTiles) {
      Image img = getClient().getResourceManager().getTileImage(tile);
      g2.drawImage(
          img,
          getAffineTransform(img.getWidth(null), tile.getPosition(), tile.getRotation()),
          null);
    }
  }
Example #25
0
 protected void unmerge(Tile tile, Location loc) {
   Location oppositeLoc = loc.rev();
   MultiTileFeature oppositePiece = (MultiTileFeature) tile.getFeaturePartOf(oppositeLoc);
   if (oppositePiece != null) {
     oppositePiece.setEdge(oppositeLoc, null);
     if (!isAbbeyTile()) {
       MultiTileFeature thisPiece = (MultiTileFeature) getFeaturePartOf(loc);
       thisPiece.setEdge(loc, null);
     }
   }
   for (int i = 0; i < 2; i++) {
     Location halfSide = i == 0 ? loc.getLeftFarm() : loc.getRightFarm();
     Location oppositeHalfSide = halfSide.rev();
     oppositePiece = (MultiTileFeature) tile.getFeaturePartOf(oppositeHalfSide);
     if (oppositePiece != null) {
       oppositePiece.setEdge(oppositeHalfSide, null);
       if (!isAbbeyTile()) {
         MultiTileFeature thisPiece = (MultiTileFeature) getFeaturePartOf(halfSide);
         thisPiece.setEdge(halfSide, null);
       }
     }
   }
 }
Example #26
0
 protected Game getGame() {
   return tile.getGame();
 }
Example #27
0
 public Feature getDeploymentFeature(Tile tile, Location loc) {
   return tile.getFeature(loc);
 }
Example #28
0
 protected boolean check(Tile tile, Location rel, Board board) {
   return getEdge(rel) == tile.getEdge(rel.rev());
 }
Example #29
0
 public Location getLocation() {
   return location.rotateCW(tile.getRotation());
 }