Search in sources :

Example 36 with SOCPlayer

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

the class SOCDisplaylessPlayerClient method handlePOTENTIALSETTLEMENTS.

/**
 * handle the "list of potential settlements" message
 * @param mes  the message
 * @param games  The hashtable of client's {@link SOCGame}s; key = game name
 * @throws IllegalStateException if the board has
 *     {@link SOCBoardLarge#getAddedLayoutPart(String) SOCBoardLarge.getAddedLayoutPart("AL")} != {@code null} but
 *     badly formed (node list number 0, or a node list number not followed by a land area number).
 *     This Added Layout Part is rarely used, and this would be discovered quickly while testing
 *     the board layout that contained it.
 */
public static void handlePOTENTIALSETTLEMENTS(SOCPotentialSettlements mes, Hashtable<String, SOCGame> games) throws IllegalStateException {
    SOCGame ga = games.get(mes.getGame());
    if (ga == null)
        return;
    final List<Integer> vset = mes.getPotentialSettlements();
    final HashSet<Integer>[] las = mes.landAreasLegalNodes;
    // must set for players after pl.setPotentialAndLegalSettlements, if not null
    final int[] loneSettles;
    // usually null, except in _SC_PIRI
    final int[][] legalSeaEdges = mes.legalSeaEdges;
    int pn = mes.getPlayerNumber();
    if (ga.hasSeaBoard) {
        SOCBoardLarge bl = ((SOCBoardLarge) ga.getBoard());
        if ((pn == -1) || ((pn == 0) && bl.getLegalSettlements().isEmpty()))
            bl.setLegalSettlements(vset, mes.startingLandArea, // throws IllegalStateException if board layout
            las);
        // has malformed Added Layout Part "AL"
        // usually null, except in _SC_PIRI
        loneSettles = bl.getAddedLayoutPart("LS");
    } else {
        loneSettles = null;
    }
    if (pn != -1) {
        SOCPlayer player = ga.getPlayer(pn);
        player.setPotentialAndLegalSettlements(vset, true, las);
        if (loneSettles != null)
            player.addLegalSettlement(loneSettles[pn], false);
        if (legalSeaEdges != null)
            player.setRestrictedLegalShips(legalSeaEdges[0]);
    } else {
        for (pn = ga.maxPlayers - 1; pn >= 0; --pn) {
            SOCPlayer pl = ga.getPlayer(pn);
            pl.setPotentialAndLegalSettlements(vset, true, las);
            if (loneSettles != null)
                pl.addLegalSettlement(loneSettles[pn], false);
            if (legalSeaEdges != null)
                pl.setRestrictedLegalShips(legalSeaEdges[pn]);
        }
    }
}
Also used : SOCBoardLarge(soc.game.SOCBoardLarge) SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame) HashSet(java.util.HashSet)

Example 37 with SOCPlayer

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

the class SOCRobotBrain method considerOffer.

/**
 * Consider a trade offer made by another player.
 * If offered to our player, calls {@link SOCRobotNegotiator#considerOffer2(SOCTradeOffer, int)}.
 *
 * @param offer  the offer to consider
 * @return a code from {@link SOCRobotNegotiator} that represents how we want to respond:
 *     {@link SOCRobotNegotiator#IGNORE_OFFER IGNORE_OFFER},
 *     {@link SOCRobotNegotiator#ACCEPT_OFFER ACCEPT_OFFER},
 *     {@link SOCRobotNegotiator#REJECT_OFFER REJECT_OFFER},
 *     or {@link SOCRobotNegotiator#COUNTER_OFFER COUNTER_OFFER}.
 *     Note: any negative value returned means we do nothing
 *     ({@link SOCRobotNegotiator#IGNORE_OFFER IGNORE_OFFER}).
 * @see #makeCounterOffer(SOCTradeOffer)
 */
protected int considerOffer(SOCTradeOffer offer) {
    int response = SOCRobotNegotiator.IGNORE_OFFER;
    SOCPlayer offeringPlayer = game.getPlayer(offer.getFrom());
    if ((offeringPlayer.getCurrentOffer() != null) && (offer == offeringPlayer.getCurrentOffer())) {
        boolean[] offeredTo = offer.getTo();
        if (offeredTo[ourPlayerNumber]) {
            response = negotiator.considerOffer2(offer, ourPlayerNumber);
        }
    }
    return response;
}
Also used : SOCPlayer(soc.game.SOCPlayer)

Example 38 with SOCPlayer

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

the class SOCRobotDM method dumbFastGameStrategy.

/**
 * Plan building for the dumbFastGameStrategy ({@link #FAST_STRATEGY}).
 * uses rules to determine what to build next
 * and update {@link #buildingPlan}.
 *<P>
 * For example, if {@link #favoriteSettlement} is chosen,
 * it's chosen from {@link #ourPlayerTracker}{@link SOCPlayerTracker#getPossibleSettlements() .getPossibleSettlements()}.
 *
 *<H4>Outline:</H4>
 * Possible cities and settlements are looked at first.
 * Find the city with best {@link SOCPossibleCity#getSpeedupTotal()}, then check each possible
 * settlement's {@link SOCPossiblePiece#getETA()} against the city's ETA to possibly choose one to build.
 * (If one is chosen, its {@link SOCPossibleSettlement#getNecessaryRoads()}
 * will also be chosen here.)  Then, Knights or Dev Cards.
 * Only then would roads or ships be looked at, for Longest Route
 * (and only if we're at 5 VP or more).
 *<P>
 * This method never directly checks
 * {@code ourPlayerTracker}{@link SOCPlayerTracker#getPossibleRoads() .getPossibleRoads()}, instead
 * it adds the roads or ships from {@link SOCPossibleSettlement#getNecessaryRoads()} to {@link #buildingPlan}
 * when a possible settlement is picked to build.
 *<P>
 * Some scenarios require special moves or certain actions to win the game.  If we're playing in
 * such a scenario, after calculating {@link #favoriteSettlement}, {@link #favoriteCity}, etc, calls
 * {@link #scenarioGameStrategyPlan(float, float, boolean, boolean, SOCBuildingSpeedEstimate, int, boolean)}.
 * See that method for the list of scenarios which need such planning.
 *
 * @param buildingETAs  the ETAs for building each piece type
 * @see #smartGameStrategy(int[])
 */
protected void dumbFastGameStrategy(final int[] buildingETAs) {
    D.ebugPrintln("***** dumbFastGameStrategy *****");
    // If this game is on the 6-player board, check whether we're planning for
    // the Special Building Phase.  Can't buy cards or trade in that phase.
    final boolean forSpecialBuildingPhase = game.isSpecialBuilding() || (game.getCurrentPlayerNumber() != ourPlayerNumber);
    int bestETA = 500;
    SOCBuildingSpeedEstimate ourBSE = new SOCBuildingSpeedEstimate(ourPlayerData.getNumbers());
    if (ourPlayerData.getTotalVP() < 5) {
        // 
        if (ourPlayerData.getNumPieces(SOCPlayingPiece.CITY) > 0) {
            Iterator<SOCPossibleCity> posCitiesIter = ourPlayerTracker.getPossibleCities().values().iterator();
            while (posCitiesIter.hasNext()) {
                SOCPossibleCity posCity = posCitiesIter.next();
                D.ebugPrintln("Estimate speedup of city at " + game.getBoard().nodeCoordToString(posCity.getCoordinates()));
                D.ebugPrintln("Speedup = " + posCity.getSpeedupTotal());
                D.ebugPrintln("ETA = " + buildingETAs[SOCBuildingSpeedEstimate.CITY]);
                if ((brain != null) && brain.getDRecorder().isOn()) {
                    brain.getDRecorder().startRecording("CITY" + posCity.getCoordinates());
                    brain.getDRecorder().record("Estimate speedup of city at " + game.getBoard().nodeCoordToString(posCity.getCoordinates()));
                    brain.getDRecorder().record("Speedup = " + posCity.getSpeedupTotal());
                    brain.getDRecorder().record("ETA = " + buildingETAs[SOCBuildingSpeedEstimate.CITY]);
                    brain.getDRecorder().stopRecording();
                }
                if ((favoriteCity == null) || (posCity.getSpeedupTotal() > favoriteCity.getSpeedupTotal())) {
                    favoriteCity = posCity;
                    bestETA = buildingETAs[SOCBuildingSpeedEstimate.CITY];
                }
            }
        }
        // 
        // score the possible settlements
        // 
        scoreSettlementsForDumb(buildingETAs[SOCBuildingSpeedEstimate.SETTLEMENT], ourBSE);
        // 
        // pick something to build
        // 
        Iterator<SOCPossibleSettlement> posSetsIter = ourPlayerTracker.getPossibleSettlements().values().iterator();
        while (posSetsIter.hasNext()) {
            SOCPossibleSettlement posSet = posSetsIter.next();
            if ((brain != null) && brain.getDRecorder().isOn()) {
                brain.getDRecorder().startRecording("SETTLEMENT" + posSet.getCoordinates());
                brain.getDRecorder().record("Estimate speedup of stlmt at " + game.getBoard().nodeCoordToString(posSet.getCoordinates()));
                brain.getDRecorder().record("Speedup = " + posSet.getSpeedupTotal());
                brain.getDRecorder().record("ETA = " + posSet.getETA());
                Stack<SOCPossibleRoad> roadPath = posSet.getRoadPath();
                if (roadPath != null) {
                    brain.getDRecorder().record("Path:");
                    Iterator<SOCPossibleRoad> rpIter = roadPath.iterator();
                    while (rpIter.hasNext()) {
                        SOCPossibleRoad posRoad = rpIter.next();
                        brain.getDRecorder().record("Road at " + game.getBoard().edgeCoordToString(posRoad.getCoordinates()));
                    }
                }
                brain.getDRecorder().stopRecording();
            }
            if (posSet.getETA() < bestETA) {
                bestETA = posSet.getETA();
                favoriteSettlement = posSet;
            } else if (posSet.getETA() == bestETA) {
                if (favoriteSettlement == null) {
                    if ((favoriteCity == null) || (posSet.getSpeedupTotal() > favoriteCity.getSpeedupTotal())) {
                        favoriteSettlement = posSet;
                    }
                } else {
                    if (posSet.getSpeedupTotal() > favoriteSettlement.getSpeedupTotal()) {
                        favoriteSettlement = posSet;
                    }
                }
            }
        }
        if (favoriteSettlement != null) {
            // 
            // we want to build a settlement
            // 
            D.ebugPrintln("Picked favorite settlement at " + game.getBoard().nodeCoordToString(favoriteSettlement.getCoordinates()));
            buildingPlan.push(favoriteSettlement);
            if (!favoriteSettlement.getNecessaryRoads().isEmpty()) {
                // 
                // we need to build roads first
                // 
                Stack<SOCPossibleRoad> roadPath = favoriteSettlement.getRoadPath();
                while (!roadPath.empty()) {
                    buildingPlan.push(roadPath.pop());
                }
            }
        } else if (favoriteCity != null) {
            // 
            // we want to build a city
            // 
            D.ebugPrintln("Picked favorite city at " + game.getBoard().nodeCoordToString(favoriteCity.getCoordinates()));
            buildingPlan.push(favoriteCity);
        } else {
            // 
            if ((game.getNumDevCards() > 0) && !forSpecialBuildingPhase) {
                // 
                // buy a card if there are any left
                // 
                D.ebugPrintln("Buy a card");
                SOCPossibleCard posCard = new SOCPossibleCard(ourPlayerData, buildingETAs[SOCBuildingSpeedEstimate.CARD]);
                buildingPlan.push(posCard);
            }
        }
    } else {
        // 
        // we have more than 4 points
        // 
        int choice = -1;
        // 
        // consider Largest Army
        // 
        D.ebugPrintln("Calculating Largest Army ETA");
        int laETA = 500;
        int laSize = 0;
        SOCPlayer laPlayer = game.getPlayerWithLargestArmy();
        if (laPlayer == null) {
            // /
            // / no one has largest army
            // /
            laSize = 3;
        } else if (laPlayer.getPlayerNumber() == ourPlayerNumber) {
            // /
            // / we have largest army
            // /
            D.ebugPrintln("We have largest army");
        } else {
            laSize = laPlayer.getNumKnights() + 1;
        }
        // /
        // / figure out how many knights we need to buy
        // /
        int knightsToBuy = 0;
        if ((ourPlayerData.getNumKnights() + // OLD + NEW knights
        ourPlayerData.getInventory().getAmount(SOCDevCardConstants.KNIGHT)) < laSize) {
            knightsToBuy = laSize - (ourPlayerData.getNumKnights() + ourPlayerData.getInventory().getAmount(SOCInventory.OLD, SOCDevCardConstants.KNIGHT));
        }
        D.ebugPrintln("knightsToBuy = " + knightsToBuy);
        if (ourPlayerData.getGame().getNumDevCards() >= knightsToBuy) {
            // /
            // / figure out how long it takes to buy this many knights
            // /
            SOCResourceSet targetResources = new SOCResourceSet();
            for (int i = 0; i < knightsToBuy; i++) {
                targetResources.add(SOCDevCard.COST);
            }
            laETA = ourBSE.calculateRollsFast(ourPlayerData.getResources(), targetResources, 100, ourPlayerData.getPortFlags());
        } else {
        // /
        // / not enough dev cards left
        // /
        }
        if ((laETA < bestETA) && !forSpecialBuildingPhase) {
            bestETA = laETA;
            choice = LA_CHOICE;
        }
        D.ebugPrintln("laETA = " + laETA);
        // 
        // consider Longest Road
        // 
        D.ebugPrintln("Calculating Longest Road ETA");
        int lrETA = 500;
        Stack<?> bestLRPath = null;
        int lrLength;
        SOCPlayer lrPlayer = game.getPlayerWithLongestRoad();
        if ((lrPlayer != null) && (lrPlayer.getPlayerNumber() == ourPlayerNumber)) {
            // /
            // / we have longest road
            // /
            D.ebugPrintln("We have longest road");
        } else if (!game.isGameOptionSet(SOCGameOption.K_SC_0RVP)) {
            if (lrPlayer == null) {
                // /
                // / no one has longest road
                // /
                lrLength = Math.max(4, ourPlayerData.getLongestRoadLength());
            } else {
                lrLength = lrPlayer.getLongestRoadLength();
            }
            Iterator<SOCLRPathData> lrPathsIter = ourPlayerData.getLRPaths().iterator();
            int depth;
            while (lrPathsIter.hasNext()) {
                Stack<?> path;
                SOCLRPathData pathData = lrPathsIter.next();
                depth = Math.min(((lrLength + 1) - pathData.getLength()), ourPlayerData.getNumPieces(SOCPlayingPiece.ROAD));
                path = (Stack<?>) recalcLongestRoadETAAux(ourPlayerData, true, pathData.getBeginning(), pathData.getLength(), lrLength, depth);
                if ((path != null) && ((bestLRPath == null) || (path.size() < bestLRPath.size()))) {
                    bestLRPath = path;
                }
                path = (Stack<?>) recalcLongestRoadETAAux(ourPlayerData, true, pathData.getEnd(), pathData.getLength(), lrLength, depth);
                if ((path != null) && ((bestLRPath == null) || (path.size() < bestLRPath.size()))) {
                    bestLRPath = path;
                }
            }
            if (bestLRPath != null) {
                // 
                // calculate LR eta
                // 
                D.ebugPrintln("Number of roads: " + bestLRPath.size());
                SOCResourceSet targetResources = new SOCResourceSet();
                for (int i = 0; i < bestLRPath.size(); i++) {
                    targetResources.add(SOCRoad.COST);
                }
                lrETA = ourBSE.calculateRollsFast(ourPlayerData.getResources(), targetResources, 100, ourPlayerData.getPortFlags());
            }
        }
        if (lrETA < bestETA) {
            bestETA = lrETA;
            choice = LR_CHOICE;
        }
        D.ebugPrintln("lrETA = " + lrETA);
        // 
        if ((ourPlayerData.getNumPieces(SOCPlayingPiece.CITY) > 0) && (buildingETAs[SOCBuildingSpeedEstimate.CITY] <= bestETA)) {
            Iterator<SOCPossibleCity> posCitiesIter = ourPlayerTracker.getPossibleCities().values().iterator();
            while (posCitiesIter.hasNext()) {
                SOCPossibleCity posCity = posCitiesIter.next();
                if ((brain != null) && brain.getDRecorder().isOn()) {
                    brain.getDRecorder().startRecording("CITY" + posCity.getCoordinates());
                    brain.getDRecorder().record("Estimate speedup of city at " + game.getBoard().nodeCoordToString(posCity.getCoordinates()));
                    brain.getDRecorder().record("Speedup = " + posCity.getSpeedupTotal());
                    brain.getDRecorder().record("ETA = " + buildingETAs[SOCBuildingSpeedEstimate.CITY]);
                    brain.getDRecorder().stopRecording();
                }
                if ((favoriteCity == null) || (posCity.getSpeedupTotal() > favoriteCity.getSpeedupTotal())) {
                    favoriteCity = posCity;
                    bestETA = buildingETAs[SOCBuildingSpeedEstimate.CITY];
                    choice = CITY_CHOICE;
                }
            }
        }
        // 
        if (ourPlayerData.getNumPieces(SOCPlayingPiece.SETTLEMENT) > 0) {
            scoreSettlementsForDumb(buildingETAs[SOCBuildingSpeedEstimate.SETTLEMENT], ourBSE);
            Iterator<SOCPossibleSettlement> posSetsIter = ourPlayerTracker.getPossibleSettlements().values().iterator();
            while (posSetsIter.hasNext()) {
                SOCPossibleSettlement posSet = posSetsIter.next();
                if ((brain != null) && brain.getDRecorder().isOn()) {
                    brain.getDRecorder().startRecording("SETTLEMENT" + posSet.getCoordinates());
                    brain.getDRecorder().record("Estimate speedup of stlmt at " + game.getBoard().nodeCoordToString(posSet.getCoordinates()));
                    brain.getDRecorder().record("Speedup = " + posSet.getSpeedupTotal());
                    brain.getDRecorder().record("ETA = " + posSet.getETA());
                    Stack<SOCPossibleRoad> roadPath = posSet.getRoadPath();
                    if (roadPath != null) {
                        brain.getDRecorder().record("Path:");
                        Iterator<SOCPossibleRoad> rpIter = roadPath.iterator();
                        while (rpIter.hasNext()) {
                            SOCPossibleRoad posRoad = rpIter.next();
                            brain.getDRecorder().record("Road at " + game.getBoard().edgeCoordToString(posRoad.getCoordinates()));
                        }
                    }
                    brain.getDRecorder().stopRecording();
                }
                if ((posSet.getRoadPath() == null) || (ourPlayerData.getNumPieces(SOCPlayingPiece.ROAD) >= posSet.getRoadPath().size())) {
                    if (posSet.getETA() < bestETA) {
                        bestETA = posSet.getETA();
                        favoriteSettlement = posSet;
                        choice = SETTLEMENT_CHOICE;
                    } else if (posSet.getETA() == bestETA) {
                        if (favoriteSettlement == null) {
                            if ((favoriteCity == null) || (posSet.getSpeedupTotal() > favoriteCity.getSpeedupTotal())) {
                                favoriteSettlement = posSet;
                                choice = SETTLEMENT_CHOICE;
                            }
                        } else {
                            if (posSet.getSpeedupTotal() > favoriteSettlement.getSpeedupTotal()) {
                                favoriteSettlement = posSet;
                            }
                        }
                    }
                }
            }
        }
        if (game.isGameOptionSet(SOCGameOption.K_SC_PIRI) || game.isGameOptionSet(SOCGameOption.K_SC_WOND)) {
            if (scenarioGameStrategyPlan(bestETA, -1f, false, (choice == LA_CHOICE), ourBSE, 0, forSpecialBuildingPhase))
                // <--- Early return: Scenario-specific buildingPlan was pushed ---
                return;
        }
        // 
        switch(choice) {
            case LA_CHOICE:
                D.ebugPrintln("Picked LA");
                if (!forSpecialBuildingPhase) {
                    for (int i = 0; i < knightsToBuy; i++) {
                        SOCPossibleCard posCard = new SOCPossibleCard(ourPlayerData, 1);
                        buildingPlan.push(posCard);
                    }
                }
                break;
            case LR_CHOICE:
                D.ebugPrintln("Picked LR");
                while (!bestLRPath.empty()) {
                    SOCPossibleRoad pr = (SOCPossibleRoad) bestLRPath.pop();
                    D.ebugPrintln("LR road at " + game.getBoard().edgeCoordToString(pr.getCoordinates()));
                    buildingPlan.push(pr);
                }
                break;
            case CITY_CHOICE:
                D.ebugPrintln("Picked favorite city at " + game.getBoard().nodeCoordToString(favoriteCity.getCoordinates()));
                buildingPlan.push(favoriteCity);
                break;
            case SETTLEMENT_CHOICE:
                D.ebugPrintln("Picked favorite settlement at " + game.getBoard().nodeCoordToString(favoriteSettlement.getCoordinates()));
                buildingPlan.push(favoriteSettlement);
                if (!favoriteSettlement.getNecessaryRoads().isEmpty()) {
                    // 
                    // we need to build roads first
                    // 
                    Stack<SOCPossibleRoad> roadPath = favoriteSettlement.getRoadPath();
                    while (!roadPath.empty()) {
                        SOCPossibleRoad pr = roadPath.pop();
                        D.ebugPrintln("Nec road at " + game.getBoard().edgeCoordToString(pr.getCoordinates()));
                        buildingPlan.push(pr);
                    }
                }
        }
    }
}
Also used : Stack(java.util.Stack) Iterator(java.util.Iterator) SOCPlayer(soc.game.SOCPlayer) SOCResourceSet(soc.game.SOCResourceSet) SOCLRPathData(soc.game.SOCLRPathData)

Example 39 with SOCPlayer

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

the class SOCRobotDM method calcWGETABonusAux.

/**
 * Helps calculate WGETA bonus for making a move or other change in the game.
 * The bonus is based on lowering your bot's WGETA and increasing the leaders' WGETA.
 *
 * @param originalWGETAs   the original WGETAs; each player's {@link SOCPlayerTracker#getWinGameETA()} before the change
 * @param trackersAfter    the playerTrackers after the change;
 *          call {@link SOCPlayerTracker#updateWinGameETAs(HashMap) SOCPlayerTracker.updateWinGameETAs(trackersAfter)}
 *          before calling this method
 * @param leaders          a list of leaders (players winning soonest);
 *          the player(s) with lowest {@link SOCPlayerTracker#getWinGameETA()}.
 *          Contains only one element, unless there is an ETA tie.
 */
private float calcWGETABonusAux(final int[] originalWGETAs, HashMap<Integer, SOCPlayerTracker> trackersAfter, Vector<SOCPlayerTracker> leaders) {
    int[] WGETAdiffs = new int[game.maxPlayers];
    int bestWGETA = 1000;
    float bonus = 0;
    for (int i = 0; i < game.maxPlayers; i++) {
        WGETAdiffs[i] = originalWGETAs[i];
        if (originalWGETAs[i] < bestWGETA) {
            bestWGETA = originalWGETAs[i];
        }
    }
    Iterator<SOCPlayerTracker> trackersAfterIter = trackersAfter.values().iterator();
    while (trackersAfterIter.hasNext()) {
        SOCPlayerTracker trackerAfter = trackersAfterIter.next();
        final int pn = trackerAfter.getPlayer().getPlayerNumber();
        WGETAdiffs[pn] -= trackerAfter.getWinGameETA();
        D.ebugPrintln("$$$ win game ETA diff for player " + pn + " = " + WGETAdiffs[pn]);
        if (pn == ourPlayerNumber) {
            if (trackerAfter.getWinGameETA() == 0) {
                D.ebugPrintln("$$$$ adding win game bonus : +" + (100 / game.maxPlayers));
                bonus += (100.0f / game.maxPlayers);
                if ((brain != null) && (brain.getDRecorder().isOn())) {
                    brain.getDRecorder().record("Adding Win Game bonus :" + df1.format(bonus));
                }
            }
        }
    }
    if ((brain != null) && (brain.getDRecorder().isOn())) {
        brain.getDRecorder().record("WGETA Diffs: " + WGETAdiffs[0] + " " + WGETAdiffs[1] + " " + WGETAdiffs[2] + " " + WGETAdiffs[3]);
    }
    // 
    if ((originalWGETAs[ourPlayerNumber] > 0) && (bonus == 0)) {
        bonus += ((100.0f / game.maxPlayers) * ((float) WGETAdiffs[ourPlayerNumber] / (float) originalWGETAs[ourPlayerNumber]));
    }
    D.ebugPrintln("^^^^ our current bonus = " + bonus);
    if ((brain != null) && (brain.getDRecorder().isOn())) {
        brain.getDRecorder().record("WGETA bonus for only myself = " + df1.format(bonus));
    }
    // 
    for (int pn = 0; pn < game.maxPlayers; pn++) {
        Enumeration<SOCPlayerTracker> leadersEnum = leaders.elements();
        while (leadersEnum.hasMoreElements()) {
            final int leaderPN = leadersEnum.nextElement().getPlayer().getPlayerNumber();
            if ((pn == ourPlayerNumber) || (pn == leaderPN))
                continue;
            if (originalWGETAs[pn] > 0) {
                final float takedownBonus = -1.0f * (100.0f / game.maxPlayers) * adversarialFactor * ((float) WGETAdiffs[pn] / (float) originalWGETAs[pn]) * ((float) bestWGETA / (float) originalWGETAs[pn]);
                bonus += takedownBonus;
                D.ebugPrintln("^^^^ added takedown bonus for player " + pn + " : " + takedownBonus);
                if (((brain != null) && (brain.getDRecorder().isOn())) && (takedownBonus != 0)) {
                    brain.getDRecorder().record("Bonus for AI with " + pn + " : " + df1.format(takedownBonus));
                }
            } else if (WGETAdiffs[pn] < 0) {
                float takedownBonus = (100.0f / game.maxPlayers) * adversarialFactor;
                bonus += takedownBonus;
                D.ebugPrintln("^^^^ added takedown bonus for player " + pn + " : " + takedownBonus);
                if (((brain != null) && (brain.getDRecorder().isOn())) && (takedownBonus != 0)) {
                    brain.getDRecorder().record("Bonus for AI with " + pn + " : " + df1.format(takedownBonus));
                }
            }
        }
    }
    // 
    // take down bonus for leaders
    // 
    Enumeration<SOCPlayerTracker> leadersEnum = leaders.elements();
    while (leadersEnum.hasMoreElements()) {
        final SOCPlayer leader = leadersEnum.nextElement().getPlayer();
        final int leaderPN = leader.getPlayerNumber();
        if (leaderPN == ourPlayerNumber)
            continue;
        if (originalWGETAs[leaderPN] > 0) {
            final float takedownBonus = -1.0f * (100.0f / game.maxPlayers) * leaderAdversarialFactor * ((float) WGETAdiffs[leaderPN] / (float) originalWGETAs[leaderPN]);
            bonus += takedownBonus;
            D.ebugPrintln("^^^^ added takedown bonus for leader " + leaderPN + " : +" + takedownBonus);
            if (((brain != null) && (brain.getDRecorder().isOn())) && (takedownBonus != 0)) {
                brain.getDRecorder().record("Bonus for LI with " + leader.getName() + " : +" + df1.format(takedownBonus));
            }
        } else if (WGETAdiffs[leaderPN] < 0) {
            final float takedownBonus = (100.0f / game.maxPlayers) * leaderAdversarialFactor;
            bonus += takedownBonus;
            D.ebugPrintln("^^^^ added takedown bonus for leader " + leaderPN + " : +" + takedownBonus);
            if (((brain != null) && (brain.getDRecorder().isOn())) && (takedownBonus != 0)) {
                brain.getDRecorder().record("Bonus for LI with " + leader.getName() + " : +" + df1.format(takedownBonus));
            }
        }
    }
    if ((brain != null) && (brain.getDRecorder().isOn())) {
        brain.getDRecorder().record("WGETA bonus = " + df1.format(bonus));
    }
    return bonus;
}
Also used : SOCPlayer(soc.game.SOCPlayer)

Example 40 with SOCPlayer

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

the class SOCRobotNegotiator method considerOffer2.

/**
 * consider an offer made by another player
 *
 * @param offer  the offer to consider
 * @param receiverNum  the player number of the receiver
 *
 * @return if we want to accept, reject, or make a counter offer
 *     ( {@link #ACCEPT_OFFER}, {@link #REJECT_OFFER}, or {@link #COUNTER_OFFER} )
 */
public int considerOffer2(SOCTradeOffer offer, final int receiverNum) {
    // /
    // / This version should be faster
    // /
    D.ebugPrintln("***** CONSIDER OFFER 2 *****");
    int response = REJECT_OFFER;
    SOCPlayer receiverPlayerData = game.getPlayer(receiverNum);
    SOCResourceSet receiverResources = receiverPlayerData.getResources();
    SOCResourceSet rsrcsOut = offer.getGetSet();
    SOCResourceSet rsrcsIn = offer.getGiveSet();
    // 
    if (!(receiverResources.contains(SOCResourceConstants.UNKNOWN) || receiverResources.contains(rsrcsOut))) {
        return response;
    }
    final int senderNum = offer.getFrom();
    D.ebugPrintln("senderNum = " + senderNum);
    D.ebugPrintln("receiverNum = " + receiverNum);
    D.ebugPrintln("rsrcs from receiver = " + rsrcsOut);
    D.ebugPrintln("rsrcs to receiver = " + rsrcsIn);
    SOCPossiblePiece receiverTargetPiece = targetPieces[receiverNum];
    D.ebugPrintln("targetPieces[" + receiverNum + "] = " + receiverTargetPiece);
    SOCPlayerTracker receiverPlayerTracker = playerTrackers.get(Integer.valueOf(receiverNum));
    if (receiverPlayerTracker == null) {
        return response;
    }
    SOCPlayerTracker senderPlayerTracker = playerTrackers.get(Integer.valueOf(senderNum));
    if (senderPlayerTracker == null) {
        return response;
    }
    SOCRobotDM simulator;
    if (receiverTargetPiece == null) {
        Stack<SOCPossiblePiece> receiverBuildingPlan = new Stack<SOCPossiblePiece>();
        simulator = new SOCRobotDM(brain.getRobotParameters(), playerTrackers, receiverPlayerTracker, receiverPlayerData, receiverBuildingPlan);
        if (receiverNum == ourPlayerNumber) {
            simulator.planStuff(strategyType);
        } else {
            simulator.planStuff(strategyType);
        }
        if (receiverBuildingPlan.empty()) {
            return response;
        }
        receiverTargetPiece = receiverBuildingPlan.peek();
        targetPieces[receiverNum] = receiverTargetPiece;
    }
    D.ebugPrintln("receiverTargetPiece = " + receiverTargetPiece);
    SOCPossiblePiece senderTargetPiece = targetPieces[senderNum];
    D.ebugPrintln("targetPieces[" + senderNum + "] = " + senderTargetPiece);
    SOCPlayer senderPlayerData = game.getPlayer(senderNum);
    if (senderTargetPiece == null) {
        Stack<SOCPossiblePiece> senderBuildingPlan = new Stack<SOCPossiblePiece>();
        simulator = new SOCRobotDM(brain.getRobotParameters(), playerTrackers, senderPlayerTracker, senderPlayerData, senderBuildingPlan);
        if (senderNum == ourPlayerNumber) {
            simulator.planStuff(strategyType);
        } else {
            simulator.planStuff(strategyType);
        }
        if (senderBuildingPlan.empty()) {
            return response;
        }
        senderTargetPiece = senderBuildingPlan.peek();
        targetPieces[senderNum] = senderTargetPiece;
    }
    D.ebugPrintln("senderTargetPiece = " + senderTargetPiece);
    int senderWGETA = senderPlayerTracker.getWinGameETA();
    if (senderWGETA > WIN_GAME_CUTOFF) {
        // 
        // see if the sender is in a race with the receiver
        // 
        boolean inARace = false;
        if ((receiverTargetPiece.getType() == SOCPossiblePiece.SETTLEMENT) || (receiverTargetPiece.getType() == SOCPossiblePiece.ROAD)) {
            for (SOCPossiblePiece threat : receiverTargetPiece.getThreats()) {
                if ((threat.getType() == senderTargetPiece.getType()) && (threat.getCoordinates() == senderTargetPiece.getCoordinates())) {
                    inARace = true;
                    break;
                }
            }
            if (inARace) {
                D.ebugPrintln("inARace == true (threat from sender)");
            } else if (receiverTargetPiece.getType() == SOCPossiblePiece.SETTLEMENT) {
                for (SOCPossibleSettlement conflict : ((SOCPossibleSettlement) receiverTargetPiece).getConflicts()) {
                    if ((senderTargetPiece.getType() == SOCPossiblePiece.SETTLEMENT) && (conflict.getCoordinates() == senderTargetPiece.getCoordinates())) {
                        inARace = true;
                        break;
                    }
                }
                if (inARace) {
                    D.ebugPrintln("inARace == true (conflict with sender)");
                }
            }
        }
        if (!inARace) {
            // /
            // / see if this is good for the receiver
            // /
            SOCResourceSet targetResources = receiverTargetPiece.getResourcesToBuild();
            if (targetResources == null)
                return REJECT_OFFER;
            SOCBuildingSpeedEstimate estimate = new SOCBuildingSpeedEstimate(receiverPlayerData.getNumbers());
            SOCTradeOffer receiverBatna = getOfferToBank(targetResources);
            D.ebugPrintln("*** receiverBatna = " + receiverBatna);
            int batnaBuildingTime = getETAToTargetResources(receiverPlayerData, targetResources, SOCResourceSet.EMPTY_SET, SOCResourceSet.EMPTY_SET, estimate);
            D.ebugPrintln("*** batnaBuildingTime = " + batnaBuildingTime);
            int offerBuildingTime = getETAToTargetResources(receiverPlayerData, targetResources, rsrcsOut, rsrcsIn, estimate);
            D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
            // 
            if (offerBuildingTime < batnaBuildingTime) {
                response = ACCEPT_OFFER;
            } else {
                response = COUNTER_OFFER;
            }
        }
    }
    return response;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCPlayer(soc.game.SOCPlayer) SOCResourceSet(soc.game.SOCResourceSet) Stack(java.util.Stack)

Aggregations

SOCPlayer (soc.game.SOCPlayer)61 SOCGame (soc.game.SOCGame)17 SOCShip (soc.game.SOCShip)12 SOCSettlement (soc.game.SOCSettlement)11 SOCRoad (soc.game.SOCRoad)9 SOCResourceSet (soc.game.SOCResourceSet)8 SOCCity (soc.game.SOCCity)7 SOCBoardLarge (soc.game.SOCBoardLarge)6 SOCFortress (soc.game.SOCFortress)5 ArrayList (java.util.ArrayList)4 SOCBoard (soc.game.SOCBoard)4 Connection (soc.server.genericServer.Connection)4 SOCInventoryItem (soc.game.SOCInventoryItem)3 SOCVillage (soc.game.SOCVillage)3 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 Stack (java.util.Stack)2 SOCLRPathData (soc.game.SOCLRPathData)2 SOCPlayingPiece (soc.game.SOCPlayingPiece)2 SOCSpecialItem (soc.game.SOCSpecialItem)2