Search in sources :

Example 26 with SOCShip

use of soc.game.SOCShip in project JSettlers2 by jdmonin.

the class SOCPlayerTracker method updateLRValues.

/**
 * update the longest road values for all possible roads/ships.
 *<P>
 * longest road value is how much this
 * road/ship would increase our longest road
 * if it were built.
 *<P>
 * the longest road potential is how much
 * this road/ship would increase our LR value
 * if other roads supported by this one were
 * built.
 */
public void updateLRValues() {
    SOCPlayer dummy = new SOCPlayer(player);
    int lrLength = player.getLongestRoadLength();
    // 
    // for each possible road with no necessary roads
    // 
    Iterator<SOCPossibleRoad> posRoadsIter = possibleRoads.values().iterator();
    while (posRoadsIter.hasNext()) {
        SOCPossibleRoad posRoad = posRoadsIter.next();
        if (posRoad.getNecessaryRoads().isEmpty()) {
            // 
            // calc longest road value
            // 
            SOCRoad dummyRoad;
            if (posRoad.isRoadNotShip() || ((posRoad instanceof SOCPossibleShip) && ((SOCPossibleShip) posRoad).isCoastalRoadAndShip))
                // TODO better coastal handling
                dummyRoad = new SOCRoad(dummy, posRoad.getCoordinates(), null);
            else
                dummyRoad = new SOCShip(dummy, posRoad.getCoordinates(), null);
            dummy.putPiece(dummyRoad, true);
            int newLRLength = dummy.calcLongestRoad2();
            if (newLRLength <= lrLength) {
                posRoad.setLRValue(0);
            } else {
                posRoad.setLRValue(newLRLength - lrLength);
            }
            // D.ebugPrintln("$$ updateLRValue for "+Integer.toHexString(posRoad.getCoordinates())+" is "+posRoad.getLRValue());
            // 
            // update potential LR value
            // 
            posRoad.setLRPotential(0);
            updateLRPotential(posRoad, dummy, dummyRoad, lrLength, LR_CALC_LEVEL);
            dummy.removePiece(dummyRoad, null);
        } else {
            posRoad.setLRValue(0);
            posRoad.setLRPotential(0);
        }
    }
    dummy.destroyPlayer();
}
Also used : SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCRoad(soc.game.SOCRoad)

Example 27 with SOCShip

use of soc.game.SOCShip in project JSettlers2 by jdmonin.

the class SOCPlayerTracker method addOurNewRoadOrShip.

/**
 * Add one of our roads or ships that has just been built.
 * Look for new adjacent possible settlements.
 * Calls {@link #expandRoadOrShip(SOCPossibleRoad, SOCPlayer, SOCPlayer, HashMap, int)}
 * on newly possible adjacent roads or ships.
 *
 * @param road         the road or ship
 * @param trackers     player trackers for the players
 * @param expandLevel  how far out we should expand roads/ships;
 *                     passed to {@link #expandRoadOrShip(SOCPossibleRoad, SOCPlayer, SOCPlayer, HashMap, int)}
 */
private void addOurNewRoadOrShip(SOCRoad road, HashMap<Integer, SOCPlayerTracker> trackers, int expandLevel) {
    // D.ebugPrintln("$$$ addOurNewRoad : "+road);
    // 
    // see if the new road was a possible road
    // 
    Iterator<SOCPossibleRoad> prIter = possibleRoads.values().iterator();
    while (prIter.hasNext()) {
        SOCPossibleRoad pr = prIter.next();
        // 
        // reset all expanded flags for possible roads
        // 
        pr.resetExpandedFlag();
        if (pr.getCoordinates() == road.getCoordinates()) {
            // 
            // if so, remove it
            // 
            // D.ebugPrintln("$$$ removing "+Integer.toHexString(road.getCoordinates()));
            possibleRoads.remove(Integer.valueOf(pr.getCoordinates()));
            removeFromNecessaryRoads(pr);
            break;
        }
    }
    // D.ebugPrintln("$$$ checking for possible settlements");
    // 
    // see if this road/ship adds any new possible settlements
    // 
    // check adjacent nodes to road for potential settlements
    // 
    final SOCBoard board = game.getBoard();
    Collection<Integer> adjNodeEnum = board.getAdjacentNodesToEdge(road.getCoordinates());
    for (Integer adjNode : adjNodeEnum) {
        if (player.canPlaceSettlement(adjNode.intValue())) {
            // 
            // see if possible settlement is already in the list
            // 
            // D.ebugPrintln("$$$ seeing if "+Integer.toHexString(adjNode.intValue())+" is already in the list");
            SOCPossibleSettlement posSet = possibleSettlements.get(adjNode);
            if (posSet != null) {
                // 
                // if so, clear necessary road list and remove from np lists
                // 
                // D.ebugPrintln("$$$ found it");
                removeFromNecessaryRoads(posSet);
                posSet.getNecessaryRoads().clear();
                posSet.setNumberOfNecessaryRoads(0);
            } else {
                // 
                // else, add new possible settlement
                // 
                // D.ebugPrintln("$$$ adding new possible settlement at "+Integer.toHexString(adjNode.intValue()));
                SOCPossibleSettlement newPosSet = new SOCPossibleSettlement(player, adjNode.intValue(), null);
                newPosSet.setNumberOfNecessaryRoads(0);
                possibleSettlements.put(adjNode, newPosSet);
                updateSettlementConflicts(newPosSet, trackers);
            }
        }
    }
    // D.ebugPrintln("$$$ checking roads adjacent to "+Integer.toHexString(road.getCoordinates()));
    // 
    // see if this road adds any new possible roads
    // 
    ArrayList<SOCPossibleRoad> newPossibleRoads = new ArrayList<SOCPossibleRoad>();
    ArrayList<SOCPossibleRoad> roadsToExpand = new ArrayList<SOCPossibleRoad>();
    // 
    for (Integer adjEdge : board.getAdjacentEdgesToEdge(road.getCoordinates())) {
        final int edge = adjEdge.intValue();
        // D.ebugPrintln("$$$ edge "+Integer.toHexString(adjEdge.intValue())+" is legal:"+player.isPotentialRoad(adjEdge.intValue()));
        // 
        // see if edge is a potential road
        // or ship to continue this route
        // 
        boolean edgeIsPotentialRoute = (road.isRoadNotShip()) ? player.isPotentialRoad(edge) : player.isPotentialShip(edge);
        // If true, this edge transitions
        // between ships <-> roads, at a
        // coastal settlement
        boolean edgeRequiresCoastalSettlement = false;
        if ((!edgeIsPotentialRoute) && game.hasSeaBoard) {
            // Determine if can transition ship <-> road
            // at a coastal settlement
            final int nodeBetween = ((SOCBoardLarge) board).getNodeBetweenAdjacentEdges(road.getCoordinates(), edge);
            if (player.canPlaceSettlement(nodeBetween)) {
                // check opposite type at transition
                edgeIsPotentialRoute = (road.isRoadNotShip()) ? player.isPotentialShip(edge) : player.isPotentialRoad(edge);
                if (edgeIsPotentialRoute)
                    edgeRequiresCoastalSettlement = true;
            }
        }
        if (edgeIsPotentialRoute) {
            // 
            // see if possible road is already in the list
            // 
            SOCPossibleRoad pr = possibleRoads.get(adjEdge);
            if (pr != null) {
                // For now, can't differ along a coastal route.
                if (edgeRequiresCoastalSettlement && (pr.isRoadNotShip() != road.isRoadNotShip())) {
                    // <--- road vs ship mismatch ---
                    continue;
                }
                // D.ebugPrintln("$$$ pr "+Integer.toHexString(pr.getCoordinates())+" already in list");
                if (!pr.getNecessaryRoads().isEmpty()) {
                    // D.ebugPrintln("$$$    clearing nr list");
                    removeFromNecessaryRoads(pr);
                    pr.getNecessaryRoads().clear();
                    pr.setNumberOfNecessaryRoads(0);
                }
                roadsToExpand.add(pr);
                pr.setExpandedFlag();
            } else {
                // 
                // else, add new possible road
                // 
                // D.ebugPrintln("$$$ adding new pr at "+Integer.toHexString(adjEdge.intValue()));
                SOCPossibleRoad newPR;
                // for effort if requires settlement
                final int roadsBetween;
                boolean isRoad = road.isRoadNotShip();
                if (edgeRequiresCoastalSettlement) {
                    isRoad = !isRoad;
                    // roughly account for effort & cost of new settlement
                    roadsBetween = 2;
                } else {
                    roadsBetween = 0;
                }
                // use coastal road/ship type (isCoastalRoadAndShip) only if we can
                // require a coastal settlement to switch from road-only or ship-only
                final boolean isCoastal = edgeRequiresCoastalSettlement && player.isPotentialRoad(edge) && player.isPotentialShip(edge);
                if (isRoad && !isCoastal) {
                    newPR = new SOCPossibleRoad(player, edge, null);
                } else {
                    newPR = new SOCPossibleShip(player, edge, isCoastal, null);
                    System.err.println("L793: " + toString() + ": new PossibleShip(" + isCoastal + ") at 0x" + Integer.toHexString(edge));
                }
                // 0 unless requires settlement
                newPR.setNumberOfNecessaryRoads(roadsBetween);
                newPossibleRoads.add(newPR);
                roadsToExpand.add(newPR);
                newPR.setExpandedFlag();
            }
        }
    }
    // 
    for (SOCPossibleRoad newPR : newPossibleRoads) {
        possibleRoads.put(Integer.valueOf(newPR.getCoordinates()), newPR);
    }
    // 
    // expand possible roads that we've touched or added
    // 
    SOCPlayer dummy = new SOCPlayer(player);
    for (SOCPossibleRoad expandPR : roadsToExpand) {
        expandRoadOrShip(expandPR, player, dummy, trackers, expandLevel);
    }
    dummy.destroyPlayer();
    // 
    if ((road instanceof SOCShip) && game.isGameOptionSet(SOCGameOption.K_SC_PIRI))
        updateScenario_SC_PIRI_closestShipToFortress((SOCShip) road, true);
}
Also used : SOCBoardLarge(soc.game.SOCBoardLarge) SOCBoard(soc.game.SOCBoard) ArrayList(java.util.ArrayList) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer)

Aggregations

SOCShip (soc.game.SOCShip)27 SOCRoad (soc.game.SOCRoad)17 SOCPlayer (soc.game.SOCPlayer)12 SOCSettlement (soc.game.SOCSettlement)11 SOCCity (soc.game.SOCCity)10 SOCFortress (soc.game.SOCFortress)8 SOCBoardLarge (soc.game.SOCBoardLarge)6 SOCBoard (soc.game.SOCBoard)4 SOCPlayingPiece (soc.game.SOCPlayingPiece)3 SOCResourceSet (soc.game.SOCResourceSet)3 ArrayList (java.util.ArrayList)2 SOCGame (soc.game.SOCGame)2 SOCVillage (soc.game.SOCVillage)2 Graphics2D (java.awt.Graphics2D)1 Image (java.awt.Image)1 BufferedImage (java.awt.image.BufferedImage)1 Vector (java.util.Vector)1 SOCInventoryItem (soc.game.SOCInventoryItem)1 SOCAcceptOffer (soc.message.SOCAcceptOffer)1 SOCChoosePlayerRequest (soc.message.SOCChoosePlayerRequest)1