Search in sources :

Example 31 with SOCResourceSet

use of soc.game.SOCResourceSet 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)

Example 32 with SOCResourceSet

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

the class SOCRobotNegotiator method getETAToTargetResources.

/**
 * another aux function
 * this one returns the number of rolls until we reach
 * the target given a possible offer
 *
 * @param player             our player data
 * @param targetResources    the resources we want
 * @param giveSet            the set of resources we're giving
 * @param getSet             the set of resources we're receiving
 * @param estimate           a SOCBuildingSpeedEstimate for our player
 */
protected int getETAToTargetResources(SOCPlayer player, SOCResourceSet targetResources, SOCResourceSet giveSet, SOCResourceSet getSet, SOCBuildingSpeedEstimate estimate) {
    SOCResourceSet ourResourcesCopy = player.getResources().copy();
    D.ebugPrintln("*** giveSet = " + giveSet);
    D.ebugPrintln("*** getSet = " + getSet);
    ourResourcesCopy.subtract(giveSet);
    ourResourcesCopy.add(getSet);
    final int offerBuildingTime = estimate.calculateRollsFast(ourResourcesCopy, targetResources, 1000, player.getPortFlags());
    D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
    D.ebugPrintln("*** ourResourcesCopy = " + ourResourcesCopy);
    return (offerBuildingTime);
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 33 with SOCResourceSet

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

the class SOCRobotNegotiator method makeOffer.

/**
 * make an offer to another player
 *
 * @param targetPiece  the piece that we want to build
 * @return the offer we want to make, or null for no offer
 */
public SOCTradeOffer makeOffer(SOCPossiblePiece targetPiece) {
    D.ebugPrintln("***** MAKE OFFER *****");
    if (targetPiece == null) {
        return null;
    }
    SOCTradeOffer offer = null;
    SOCResourceSet targetResources = targetPiece.getResourcesToBuild();
    if (targetResources == null)
        return null;
    SOCResourceSet ourResources = ourPlayerData.getResources();
    D.ebugPrintln("*** targetResources = " + targetResources);
    D.ebugPrintln("*** ourResources = " + ourResources);
    if (ourResources.contains(targetResources)) {
        return offer;
    }
    if (ourResources.contains(SOCResourceConstants.UNKNOWN)) {
        D.ebugPrintln("AGG WE HAVE UNKNOWN RESOURCES !!!! %%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        return offer;
    }
    SOCTradeOffer batna = getOfferToBank(targetResources);
    D.ebugPrintln("*** BATNA = " + batna);
    SOCBuildingSpeedEstimate estimate = new SOCBuildingSpeedEstimate(ourPlayerData.getNumbers());
    SOCResourceSet giveResourceSet = new SOCResourceSet();
    SOCResourceSet getResourceSet = new SOCResourceSet();
    int batnaBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
    D.ebugPrintln("*** batnaBuildingTime = " + batnaBuildingTime);
    if (batna != null) {
        batnaBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, batna.getGiveSet(), batna.getGetSet(), estimate);
    }
    D.ebugPrintln("*** batnaBuildingTime = " + batnaBuildingTime);
    // /
    // / Seperate resource types into needed and not-needed.  Sort
    // / groups by frequency, most to least.  Start with most frequent
    // / not-needed resources.  Trade for least frequent needed resources.
    // /
    int[] rollsPerResource = estimate.getRollsPerResource();
    int[] neededRsrc = new int[5];
    int[] notNeededRsrc = new int[5];
    int neededRsrcCount = 0;
    int notNeededRsrcCount = 0;
    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
        if (targetResources.contains(rsrcType)) {
            neededRsrc[neededRsrcCount] = rsrcType;
            neededRsrcCount++;
        } else {
            notNeededRsrc[notNeededRsrcCount] = rsrcType;
            notNeededRsrcCount++;
        }
    }
    for (int j = neededRsrcCount - 1; j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            // D.ebugPrintln("neededRsrc[i]="+neededRsrc[i]+" "+rollsPerResource[neededRsrc[i]]);
            if (rollsPerResource[neededRsrc[i]] > rollsPerResource[neededRsrc[i + 1]]) {
                // D.ebugPrintln("swap with "+neededRsrc[i+1]+" "+rollsPerResource[neededRsrc[i+1]]);
                int tmp = neededRsrc[i];
                neededRsrc[i] = neededRsrc[i + 1];
                neededRsrc[i + 1] = tmp;
            }
        }
    }
    if (D.ebugOn) {
        for (int i = 0; i < neededRsrcCount; i++) {
            D.ebugPrintln("NEEDED RSRC: " + neededRsrc[i] + " : " + rollsPerResource[neededRsrc[i]]);
        }
    }
    for (int j = notNeededRsrcCount - 1; j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            // D.ebugPrintln("notNeededRsrc[i]="+notNeededRsrc[i]+" "+rollsPerResource[notNeededRsrc[i]]);
            if (rollsPerResource[notNeededRsrc[i]] > rollsPerResource[notNeededRsrc[i + 1]]) {
                // D.ebugPrintln("swap with "+notNeededRsrc[i+1]+" "+rollsPerResource[notNeededRsrc[i+1]]);
                int tmp = notNeededRsrc[i];
                notNeededRsrc[i] = notNeededRsrc[i + 1];
                notNeededRsrc[i + 1] = tmp;
            }
        }
    }
    if (D.ebugOn) {
        for (int i = 0; i < notNeededRsrcCount; i++) {
            D.ebugPrintln("NOT-NEEDED RSRC: " + notNeededRsrc[i] + " : " + rollsPerResource[notNeededRsrc[i]]);
        }
    }
    // /
    // / make a list of what other players are selling
    // /
    boolean[] someoneIsSellingResource = new boolean[SOCResourceConstants.MAXPLUSONE];
    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
        someoneIsSellingResource[rsrcType] = false;
        for (int pn = 0; pn < game.maxPlayers; pn++) {
            if ((pn != ourPlayerNumber) && (isSellingResource[pn][rsrcType])) {
                someoneIsSellingResource[rsrcType] = true;
                D.ebugPrintln("*** player " + pn + " is selling " + rsrcType);
                break;
            }
        }
    }
    // /
    // / figure out which resources we don't have enough of
    // / that someone is selling
    // /
    int getRsrcIdx = neededRsrcCount - 1;
    while ((getRsrcIdx >= 0) && ((ourResources.getAmount(neededRsrc[getRsrcIdx]) >= targetResources.getAmount(neededRsrc[getRsrcIdx])) || (!someoneIsSellingResource[neededRsrc[getRsrcIdx]]))) {
        getRsrcIdx--;
    }
    // /
    if (getRsrcIdx >= 0) {
        D.ebugPrintln("*** getRsrc = " + neededRsrc[getRsrcIdx]);
        getResourceSet.add(1, neededRsrc[getRsrcIdx]);
        D.ebugPrintln("*** offer should be null : offer = " + offer);
        // /
        // / consider offers where we give one unneeded for one needed
        // /
        int giveRsrcIdx = 0;
        while ((giveRsrcIdx < notNeededRsrcCount) && (offer == null)) {
            D.ebugPrintln("*** ourResources.getAmount(" + notNeededRsrc[giveRsrcIdx] + ") = " + ourResources.getAmount(notNeededRsrc[giveRsrcIdx]));
            if (ourResources.contains(notNeededRsrc[giveRsrcIdx])) {
                giveResourceSet.clear();
                giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx]);
                offer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                D.ebugPrintln("*** offer = " + offer);
                int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
            }
            giveRsrcIdx++;
        }
        D.ebugPrintln("*** ourResources = " + ourResources);
        // /
        if (offer == null) {
            int giveRsrcIdx1 = 0;
            while ((giveRsrcIdx1 < neededRsrcCount) && (offer == null)) {
                D.ebugPrintln("*** ourResources.getAmount(" + neededRsrc[giveRsrcIdx1] + ") = " + ourResources.getAmount(neededRsrc[giveRsrcIdx1]));
                D.ebugPrintln("*** targetResources.getAmount(" + neededRsrc[giveRsrcIdx1] + ") = " + targetResources.getAmount(neededRsrc[giveRsrcIdx1]));
                if ((ourResources.getAmount(neededRsrc[giveRsrcIdx1]) > targetResources.getAmount(neededRsrc[giveRsrcIdx1])) && (neededRsrc[giveRsrcIdx1] != neededRsrc[getRsrcIdx])) {
                    giveResourceSet.clear();
                    giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                    // /
                    // / make sure the offer is better than our BATNA
                    // /
                    int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                    if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                        offer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                        D.ebugPrintln("*** offer = " + offer);
                        D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                    }
                }
                giveRsrcIdx1++;
            }
        }
        D.ebugPrintln("*** ourResources = " + ourResources);
        SOCResourceSet leftovers = ourResources.copy();
        leftovers.subtract(targetResources);
        D.ebugPrintln("*** leftovers = " + leftovers);
        // /
        if (offer == null) {
            int giveRsrcIdx1 = 0;
            int giveRsrcIdx2 = 0;
            while ((giveRsrcIdx1 < notNeededRsrcCount) && (offer == null)) {
                if (ourResources.contains(notNeededRsrc[giveRsrcIdx1])) {
                    while ((giveRsrcIdx2 < notNeededRsrcCount) && (offer == null)) {
                        giveResourceSet.clear();
                        giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                        giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx2]);
                        if (ourResources.contains(giveResourceSet)) {
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                offer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                D.ebugPrintln("*** offer = " + offer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        giveRsrcIdx2++;
                    }
                    giveRsrcIdx2 = 0;
                    while ((giveRsrcIdx2 < neededRsrcCount) && (offer == null)) {
                        if (neededRsrc[giveRsrcIdx2] != neededRsrc[getRsrcIdx]) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx2]);
                            if (leftovers.contains(giveResourceSet)) {
                                // /
                                // / make sure the offer is better than our BATNA
                                // /
                                int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                                if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                    offer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                    D.ebugPrintln("*** offer = " + offer);
                                    D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                                }
                            }
                        }
                        giveRsrcIdx2++;
                    }
                }
                giveRsrcIdx1++;
            }
            giveRsrcIdx1 = 0;
            giveRsrcIdx2 = 0;
            while ((giveRsrcIdx1 < neededRsrcCount) && (offer == null)) {
                if (leftovers.contains(neededRsrc[giveRsrcIdx1]) && (neededRsrc[giveRsrcIdx1] != neededRsrc[getRsrcIdx])) {
                    while ((giveRsrcIdx2 < notNeededRsrcCount) && (offer == null)) {
                        giveResourceSet.clear();
                        giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                        giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx2]);
                        if (leftovers.contains(giveResourceSet)) {
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                offer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                D.ebugPrintln("*** offer = " + offer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        giveRsrcIdx2++;
                    }
                    giveRsrcIdx2 = 0;
                    while ((giveRsrcIdx2 < neededRsrcCount) && (offer == null)) {
                        if (neededRsrc[giveRsrcIdx2] != neededRsrc[getRsrcIdx]) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx2]);
                            if (leftovers.contains(giveResourceSet)) {
                                // /
                                // / make sure the offer is better than our BATNA
                                // /
                                int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                                if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                    offer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                    D.ebugPrintln("*** offer = " + offer);
                                    D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                                }
                            }
                        }
                        giveRsrcIdx2++;
                    }
                }
                giveRsrcIdx1++;
            }
        }
    }
    // /
    if (offer == null) {
        SOCResourceSet leftovers = ourResources.copy();
        leftovers.subtract(targetResources);
        D.ebugPrintln("*** leftovers = " + leftovers);
        int getRsrcIdx2 = notNeededRsrcCount - 1;
        while ((getRsrcIdx2 >= 0) && (!someoneIsSellingResource[neededRsrc[getRsrcIdx2]])) {
            getRsrcIdx2--;
        }
        while ((getRsrcIdx2 >= 0) && (offer == null)) {
            getResourceSet.clear();
            getResourceSet.add(1, notNeededRsrc[getRsrcIdx2]);
            leftovers.add(1, notNeededRsrc[getRsrcIdx2]);
            // /
            if (offer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < notNeededRsrcCount) && (offer == null)) {
                    if (leftovers.contains(notNeededRsrc[giveRsrcIdx1]) && (notNeededRsrc[giveRsrcIdx1] != notNeededRsrc[getRsrcIdx2])) {
                        leftovers.subtract(1, notNeededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                offer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** offer = " + offer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        leftovers.add(1, notNeededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            // /
            if (offer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < neededRsrcCount) && (offer == null)) {
                    if (leftovers.contains(neededRsrc[giveRsrcIdx1])) {
                        leftovers.subtract(1, neededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                offer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** offer = " + offer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        leftovers.add(1, neededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            leftovers.subtract(1, notNeededRsrc[getRsrcIdx2]);
            getRsrcIdx2--;
        }
    }
    return offer;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Example 34 with SOCResourceSet

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

the class SOCRobotNegotiator method makeCounterOffer.

/**
 * consider an offer made by another player
 *
 * @param offer  the offer to consider
 * @return true if we want to accept the offer
 */
/*
       public int considerOffer(SOCTradeOffer offer) {
       D.ebugPrintln("***** CONSIDER OFFER *****");
       int response = REJECT_OFFER;

       SOCPlayer offeringPlayer = game.getPlayer(offer.getFrom());
       SOCResourceSet rsrcsOut = offer.getGetSet();
       SOCResourceSet rsrcsIn = offer.getGiveSet();
       if (ourPlayerData.getResources().contains(rsrcsOut)) {
       int ourOriginalWGETA = 0;
       SOCPossiblePiece ourOriginalPiece = null;
       int ourOriginalPieceType = 0;
       int ourOriginalPieceCoord = 0;
       int ourOriginalPieceETA = 0;
       int ourOriginalPieceScore = 0;
       SOCPossibleSettlement ourOriginalFavoriteSettlement = null;
       int ourOriginalFavoriteSettlementCoord = 0;
       int ourOriginalFavoriteSettlementETA = 50;
       int ourOriginalFavoriteSettlementScore = 0;
       SOCPossibleCity ourOriginalFavoriteCity = null;
       int ourOriginalFavoriteCityCoord = 0;
       int ourOriginalFavoriteCityETA = 50;
       int ourOriginalFavoriteCityScore = 0;
       SOCPossibleRoad ourOriginalFavoriteRoad = null;
       int ourOriginalFavoriteRoadCoord = 0;
       int ourOriginalFavoriteRoadETA = 50;
       int ourOriginalFavoriteRoadScore = 0;
       SOCPossibleCard ourOriginalPossibleCard = null;
       int ourOriginalPossibleCardETA = 50;
       int ourOriginalPossibleCardScore = 0;
       int ourNewWGETA = 0;
       int ourNewPieceType = 0;
       int ourNewPieceCoord = 0;
       int ourNewPieceETA = 0;
       int ourNewPieceScore = 0;
       SOCPossibleSettlement ourNewFavoriteSettlement = null;
       int ourNewFavoriteSettlementCoord = 0;
       int ourNewFavoriteSettlementETA = 50;
       int ourNewFavoriteSettlementScore = 0;
       SOCPossibleCity ourNewFavoriteCity = null;
       int ourNewFavoriteCityCoord = 0;
       int ourNewFavoriteCityETA = 50;
       int ourNewFavoriteCityScore = 0;
       SOCPossibleRoad ourNewFavoriteRoad = null;
       int ourNewFavoriteRoadCoord = 0;
       int ourNewFavoriteRoadETA = 50;
       int ourNewFavoriteRoadScore = 0;
       SOCPossibleCard ourNewPossibleCard = null;
       int ourNewPossibleCardETA = 50;
       int ourNewPossibleCardScore = 0;
       int theirOriginalWGETA = 0;
       SOCPossiblePiece theirOriginalPiece = null;
       int theirOriginalPieceType = 0;
       int theirOriginalPieceCoord = 0;
       int theirOriginalPieceETA = 0;
       int theirOriginalPieceScore = 0;
       int theirNewWGETA = 0;
       int theirNewPieceType = 0;
       int theirNewPieceCoord = 0;
       int theirNewPieceETA = 0;
       int theirNewPieceScore = 0;
       SOCPlayerTracker.updateWinGameETAs(playerTrackers);
       ourOriginalWGETA = ourPlayerTracker.getWinGameETA();

       SOCResourceSet theirResources = offeringPlayer.getResources();
       SOCResourceSet ourResources = ourPlayerData.getResources();

       SOCRobotDM simulator;

       Stack ourBuildingPlan = buildingPlan;
       if (ourBuildingPlan.empty()) {
       D.ebugPrintln("**** our building plan is empty ****");
       simulator = new SOCRobotDM(brain.getRobotParameters(),
       playerTrackers,
       ourPlayerTracker,
       ourPlayerData,
       ourBuildingPlan);
       simulator.planStuff();
       }

       if (ourBuildingPlan.empty()) {
       return response;
       }
       SOCPossiblePiece targetPiece = (SOCPossiblePiece)ourBuildingPlan.peek();
       ourOriginalFavoriteSettlement = decisionMaker.getFavoriteSettlement();
       ourOriginalFavoriteCity = decisionMaker.getFavoriteCity();
       ourOriginalFavoriteRoad = decisionMaker.getFavoriteRoad();
       ourOriginalPossibleCard = decisionMaker.getPossibleCard();
       SOCPlayerTracker theirPlayerTracker = (SOCPlayerTracker)playerTrackers.get(new Integer(offer.getFrom()));

       if (theirPlayerTracker != null) {
       theirOriginalWGETA = theirPlayerTracker.getWinGameETA();
       D.ebugPrintln("CHECKING OFFER FROM PLAYER "+offer.getFrom());
       D.ebugPrintln("they give : "+rsrcsIn);
       D.ebugPrintln("they get : "+rsrcsOut);

       D.ebugPrintln("---------< before >-----------");
       ourOriginalPiece = targetPiece;
       ourOriginalPieceType = targetPiece.getType();
       ourOriginalPieceCoord = targetPiece.getCoordinates();
       ourOriginalPieceETA = targetPiece.getETA();
       ourOriginalPieceScore = targetPiece.getScore();
       D.ebugPrintln("ourResources : "+ourResources);
       D.ebugPrintln("ourOriginalWGETA = "+ourOriginalWGETA);
       D.ebugPrintln("our target piece type : "+targetPiece.getType());
       D.ebugPrintln("our target piece coord : "+Integer.toHexString(targetPiece.getCoordinates()));
       D.ebugPrintln("our target piece eta : "+targetPiece.getETA());
       D.ebugPrintln("our target piece score : "+targetPiece.getScore());

       if (ourOriginalFavoriteSettlement != null) {
       ourOriginalFavoriteSettlementCoord = ourOriginalFavoriteSettlement.getCoordinates();
       ourOriginalFavoriteSettlementETA = ourOriginalFavoriteSettlement.getETA();
       ourOriginalFavoriteSettlementScore = ourOriginalFavoriteSettlement.getScore();
       }
       if (ourOriginalFavoriteCity != null) {
       ourOriginalFavoriteCityCoord = ourOriginalFavoriteCity.getCoordinates();
       ourOriginalFavoriteCityETA = ourOriginalFavoriteCity.getETA();
       ourOriginalFavoriteCityScore = ourOriginalFavoriteCity.getScore();
       }
       if (ourOriginalFavoriteRoad != null) {
       ourOriginalFavoriteRoadCoord = ourOriginalFavoriteRoad.getCoordinates();
       ourOriginalFavoriteRoadETA = ourOriginalFavoriteRoad.getETA();
       ourOriginalFavoriteRoadScore = ourOriginalFavoriteRoad.getScore();
       }
       if (ourOriginalPossibleCard != null) {
       ourOriginalPossibleCardETA = ourOriginalPossibleCard.getETA();
       ourOriginalPossibleCardScore = ourOriginalPossibleCard.getScore();
       }
       Stack theirBuildingPlan = new Stack();
       simulator = new SOCRobotDM(brain.getRobotParameters(),
       playerTrackers,
       theirPlayerTracker,
       theirPlayerTracker.getPlayer(),
       theirBuildingPlan);
       simulator.planStuff();
       theirOriginalPiece = (SOCPossiblePiece)theirBuildingPlan.pop();
       theirOriginalPieceType = theirOriginalPiece.getType();
       theirOriginalPieceCoord = theirOriginalPiece.getCoordinates();
       theirOriginalPieceETA = theirOriginalPiece.getETA();
       theirOriginalPieceScore = theirOriginalPiece.getScore();
       D.ebugPrintln("theirResources : "+theirResources);
       D.ebugPrintln("theirOriginalWGETA = "+theirOriginalWGETA);
       if (theirOriginalPiece != null) {
       D.ebugPrintln("their target piece type : "+theirOriginalPiece.getType());
       D.ebugPrintln("their target piece coord : "+Integer.toHexString(theirOriginalPiece.getCoordinates()));
       D.ebugPrintln("their target piece eta : "+theirOriginalPiece.getETA());
       D.ebugPrintln("their target piece score : "+theirOriginalPiece.getScore());
       } else {
       D.ebugPrintln("their target piece == null");
       }

       theirResources.add(rsrcsOut);
       theirResources.subtract(rsrcsIn);
       ourResources.add(rsrcsIn);
       ourResources.subtract(rsrcsOut);

       D.ebugPrintln("---------< after >-----------");

       SOCPlayerTracker.updateWinGameETAs(playerTrackers);
       ourNewWGETA = ourPlayerTracker.getWinGameETA();
       theirNewWGETA = theirPlayerTracker.getWinGameETA();
       Stack ourBuildingPlanAfter = new Stack();
       simulator = new SOCRobotDM(brain.getRobotParameters(),
       playerTrackers,
       ourPlayerTracker,
       ourPlayerData,
       ourBuildingPlanAfter);
       simulator.planStuff();
       SOCPossiblePiece ourNewTargetPiece = (SOCPossiblePiece)ourBuildingPlanAfter.pop();
       ourNewFavoriteSettlement = simulator.getFavoriteSettlement();
       ourNewFavoriteCity = simulator.getFavoriteCity();
       ourNewFavoriteRoad = simulator.getFavoriteRoad();
       ourNewPossibleCard = simulator.getPossibleCard();
       ourNewPieceType = ourNewTargetPiece.getType();
       ourNewPieceCoord = ourNewTargetPiece.getCoordinates();
       ourNewPieceETA = ourNewTargetPiece.getETA();
       ourNewPieceScore = ourNewTargetPiece.getScore();

       D.ebugPrintln("ourResources : "+ourResources);
       D.ebugPrintln("ourNewWGETA = "+ourNewWGETA);
       if (ourNewTargetPiece != null) {
       D.ebugPrintln("our target piece type : "+ourNewTargetPiece.getType());
       D.ebugPrintln("our target piece coord : "+Integer.toHexString(ourNewTargetPiece.getCoordinates()));
       D.ebugPrintln("our target piece eta : "+ourNewTargetPiece.getETA());
       D.ebugPrintln("our target piece score : "+ourNewTargetPiece.getScore());
       } else {
       D.ebugPrintln("our target piece == null");
       }

       if (ourNewFavoriteSettlement != null) {
       ourNewFavoriteSettlementCoord = ourNewFavoriteSettlement.getCoordinates();
       ourNewFavoriteSettlementETA = ourNewFavoriteSettlement.getETA();
       ourNewFavoriteSettlementScore = ourNewFavoriteSettlement.getScore();
       }
       if (ourNewFavoriteCity != null) {
       ourNewFavoriteCityCoord = ourNewFavoriteCity.getCoordinates();
       ourNewFavoriteCityETA = ourNewFavoriteCity.getETA();
       ourNewFavoriteCityScore = ourNewFavoriteCity.getScore();
       }
       if (ourNewFavoriteRoad != null) {
       ourNewFavoriteRoadCoord = ourNewFavoriteRoad.getCoordinates();
       ourNewFavoriteRoadETA = ourNewFavoriteRoad.getETA();
       ourNewFavoriteRoadScore = ourNewFavoriteRoad.getScore();
       }
       if (ourNewPossibleCard != null) {
       ourNewPossibleCardETA = ourNewPossibleCard.getETA();
       ourNewPossibleCardScore = ourNewPossibleCard.getScore();
       }
       theirBuildingPlan.clear();
       simulator = new SOCRobotDM(brain.getRobotParameters(),
       playerTrackers,
       theirPlayerTracker,
       theirPlayerTracker.getPlayer(),
       theirBuildingPlan);
       simulator.planStuff();
       SOCPossiblePiece theirNewTargetPiece = (SOCPossiblePiece)theirBuildingPlan.pop();

       theirNewPieceType = theirNewTargetPiece.getType();
       theirNewPieceCoord = theirNewTargetPiece.getCoordinates();
       theirNewPieceETA = theirNewTargetPiece.getETA();
       theirNewPieceScore = theirNewTargetPiece.getScore();
       D.ebugPrintln("theirResources : "+theirResources);
       D.ebugPrintln("theirNewWGETA = "+theirNewWGETA);
       if (theirNewTargetPiece != null) {
       D.ebugPrintln("their target piece type : "+theirNewTargetPiece.getType());
       D.ebugPrintln("their target piece coord : "+Integer.toHexString(theirNewTargetPiece.getCoordinates()));
       D.ebugPrintln("their target piece eta : "+theirNewTargetPiece.getETA());
       D.ebugPrintln("their target piece score : "+theirNewTargetPiece.getScore());
       } else {
       D.ebugPrintln("their target piece == null");
       }

       D.ebugPrintln("---------< cleanup >-----------");

       theirResources.subtract(rsrcsOut);
       theirResources.add(rsrcsIn);
       ourResources.subtract(rsrcsIn);
       ourResources.add(rsrcsOut);

       SOCPlayerTracker.updateWinGameETAs(playerTrackers);

       D.ebugPrintln("ourResources : "+ourResources);
       D.ebugPrintln("theirResources : "+theirResources);

       D.ebugPrintln("---------< done >-----------");
       }
       //
       //  now that we have the info, decide if taking
       //  the offer is worth it
       //
       if (theirOriginalWGETA < WIN_GAME_CUTOFF) {
       //brain.getClient().sendText(game, "You're too close to winning.");
       } else {
       //
       //  see if we are in a race with them
       //
       boolean inARace = false;
       if ((ourOriginalPieceType == SOCPossiblePiece.SETTLEMENT) ||
       (ourOriginalPieceType == SOCPossiblePiece.ROAD)) {
       Enumeration threatsEnum = ourOriginalPiece.getThreats().elements();
       while (threatsEnum.hasMoreElements()) {
       SOCPossiblePiece threat = (SOCPossiblePiece)threatsEnum.nextElement();
       if ((threat.getCoordinates() == theirOriginalPieceCoord) ||
       (threat.getCoordinates() == theirNewPieceCoord)) {
       inARace = true;
       break;
       }
       }
       if (inARace) {
       D.ebugPrintln("inARace == true (threat == their new piece)");
       //brain.getClient().sendText(game, "No way!  We're racing for the same spot.");
       } else if (ourOriginalPieceType == SOCPossiblePiece.SETTLEMENT) {
       Enumeration conflictsEnum = ((SOCPossibleSettlement)ourOriginalPiece).getConflicts().elements();
       while (conflictsEnum.hasMoreElements()) {
       SOCPossibleSettlement conflict = (SOCPossibleSettlement)conflictsEnum.nextElement();
       if ((conflict.getCoordinates() == theirOriginalPieceCoord) ||
       (conflict.getCoordinates() == theirNewPieceCoord)) {
       inARace = true;
       break;
       }
       }
       if (inARace) {
       D.ebugPrintln("inARace == true (conflict == their new piece)");
       //brain.getClient().sendText(game, "If you build your settlement, it'll prevent me from building mine.");
       }
       }
       }
       if (!inARace) {
       D.ebugPrintln("-- ourOriginalWGETA: "+ourOriginalWGETA);
       D.ebugPrintln("--      ourNewWGETA: "+ourNewWGETA);
       D.ebugPrintln("-- theirOriginalWGETA: "+theirOriginalWGETA);
       D.ebugPrintln("--      theirNewWGETA: "+theirNewWGETA);
       D.ebugPrintln("--  ourOriginalPieceType: "+ourOriginalPieceType);
       D.ebugPrintln("--       ourNewPieceType: "+ourNewPieceType);
       D.ebugPrintln("--   ourOriginalPieceETA: "+ourOriginalPieceETA);
       D.ebugPrintln("--        ourNewPieceETA: "+ourNewPieceETA);
       D.ebugPrintln("-- ourOriginalPieceScore: "+ourOriginalPieceScore);
       D.ebugPrintln("--      ourNewPieceScore: "+ourNewPieceScore);
       D.ebugPrintln("-- ourOriginalFavoriteSettlementETA: "+ourOriginalFavoriteSettlementETA);
       D.ebugPrintln("--       ourOriginalFavoriteCityETA: "+ourOriginalFavoriteCityETA);
       D.ebugPrintln("--       ourOriginalFavoriteRoadETA: "+ourOriginalFavoriteRoadETA);
       D.ebugPrintln("--       ourOriginalPossibleCardETA: "+ourOriginalPossibleCardETA);
       D.ebugPrintln("--                            total: "+(ourOriginalFavoriteSettlementETA
       + ourOriginalFavoriteCityETA + ourOriginalFavoriteRoadETA + ourOriginalPossibleCardETA));
       D.ebugPrintln("-- ourNewFavoriteSettlementETA: "+ourNewFavoriteSettlementETA);
       D.ebugPrintln("--       ourNewFavoriteCityETA: "+ourNewFavoriteCityETA);
       D.ebugPrintln("--       ourNewFavoriteRoadETA: "+ourNewFavoriteRoadETA);
       D.ebugPrintln("--       ourNewPossibleCardETA: "+ourNewPossibleCardETA);
       D.ebugPrintln("--                            total: "+(ourNewFavoriteSettlementETA
       + ourNewFavoriteCityETA + ourNewFavoriteRoadETA + ourNewPossibleCardETA));
       D.ebugPrintln("-- ourOriginalFavoriteSettlementScore: "+ourOriginalFavoriteSettlementScore);
       D.ebugPrintln("--       ourOriginalFavoriteCityScore: "+ourOriginalFavoriteCityScore);
       D.ebugPrintln("--       ourOriginalFavoriteRoadScore: "+ourOriginalFavoriteRoadScore);
       D.ebugPrintln("--       ourOriginalPossibleCardScore: "+ourOriginalPossibleCardScore);
       D.ebugPrintln("--                            total: "+(ourOriginalFavoriteSettlementScore
       + ourOriginalFavoriteCityScore + ourOriginalFavoriteRoadScore + ourOriginalPossibleCardScore));
       D.ebugPrintln("-- ourNewFavoriteSettlementScore: "+ourNewFavoriteSettlementScore);
       D.ebugPrintln("--       ourNewFavoriteCityScore: "+ourNewFavoriteCityScore);
       D.ebugPrintln("--       ourNewFavoriteRoadScore: "+ourNewFavoriteRoadScore);
       D.ebugPrintln("--       ourNewPossibleCardScore: "+ourNewPossibleCardScore);
       D.ebugPrintln("--                            total: "+(ourNewFavoriteSettlementScore
       + ourNewFavoriteCityScore + ourNewFavoriteRoadScore + ourNewPossibleCardScore));

       //
       // see if we have something to gain from the offer
       //
       if (ourOriginalPieceType == ourNewPieceType) {
       //
       //  things to check if we want to build the
       //  same piece before and after the deal
       //
       if ((ourOriginalPieceETA > ourNewPieceETA) ||
       ((ourOriginalPieceETA == ourNewPieceETA) &&
       (ourOriginalPieceScore < ourNewPieceScore))) {
       response = ACCEPT_OFFER;
       } else {
       response = COUNTER_OFFER;
       //brain.getClient().sendText(game, "That deal's not good for me.");
       }
       } else {
       //
       //  things to check if we changed our
       //  plans based on the deal
       //
       if ((ourOriginalPieceScore < ourNewPieceScore) ||
       ((ourOriginalPieceScore == ourNewPieceScore) &&
       (ourOriginalPieceETA > ourNewPieceETA))) {
       response = ACCEPT_OFFER;
       } else {
       response = COUNTER_OFFER;
       //brain.getClient().sendText(game, "That deal's not good for me.");
       }
       }
     */
/*
       if (response == ACCEPT_OFFER) {
       //
       //  if the deal allows them to build a piece now,
       //  see if their piece has a higher score than ours
       //
       if ((theirNewPieceETA == 0) &&
       (theirNewPieceScore > ourNewPieceScore)) {
       brain.getClient().sendText(game, "I think you're comming out ahead on that deal.");
       response = REJECT_OFFER;
       }
       }
     */
/*
       }
       }
       }
       return response;
       }
     */
/**
 * @return a counter offer or null
 *
 * @param originalOffer  the offer given to us
 */
public SOCTradeOffer makeCounterOffer(SOCTradeOffer originalOffer) {
    D.ebugPrintln("***** MAKE COUNTER OFFER *****");
    SOCTradeOffer counterOffer = null;
    SOCPossiblePiece targetPiece = targetPieces[ourPlayerNumber];
    if (targetPiece == null) {
        Stack<SOCPossiblePiece> ourBuildingPlan = buildingPlan;
        if (ourBuildingPlan.empty()) {
            SOCRobotDM simulator;
            D.ebugPrintln("**** our building plan is empty ****");
            simulator = new SOCRobotDM(brain.getRobotParameters(), playerTrackers, ourPlayerTracker, ourPlayerData, ourBuildingPlan);
            simulator.planStuff(strategyType);
        }
        if (ourBuildingPlan.empty()) {
            return counterOffer;
        }
        targetPiece = ourBuildingPlan.peek();
        targetPieces[ourPlayerNumber] = targetPiece;
    }
    SOCResourceSet targetResources = targetPiece.getResourcesToBuild();
    if (targetResources == null)
        return null;
    SOCResourceSet ourResources = ourPlayerData.getResources();
    D.ebugPrintln("*** targetResources = " + targetResources);
    D.ebugPrintln("*** ourResources = " + ourResources);
    if (ourResources.contains(targetResources)) {
        return counterOffer;
    }
    if (ourResources.contains(SOCResourceConstants.UNKNOWN)) {
        D.ebugPrintln("AGG WE HAVE UNKNOWN RESOURCES !!!! %%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        return counterOffer;
    }
    SOCTradeOffer batna = getOfferToBank(targetResources);
    D.ebugPrintln("*** BATNA = " + batna);
    SOCBuildingSpeedEstimate estimate = new SOCBuildingSpeedEstimate(ourPlayerData.getNumbers());
    SOCResourceSet giveResourceSet = new SOCResourceSet();
    SOCResourceSet getResourceSet = new SOCResourceSet();
    int batnaBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
    if (batna != null) {
        batnaBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, batna.getGiveSet(), batna.getGetSet(), estimate);
    }
    D.ebugPrintln("*** batnaBuildingTime = " + batnaBuildingTime);
    // /
    // / Seperate resource types into needed and not-needed.  Sort
    // / groups by frequency, most to least.  Start with most frequent
    // / not-needed resources.  Trade for least frequent needed resources.
    // /
    int[] rollsPerResource = estimate.getRollsPerResource();
    int[] neededRsrc = new int[5];
    int[] notNeededRsrc = new int[5];
    int neededRsrcCount = 0;
    int notNeededRsrcCount = 0;
    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
        if (targetResources.contains(rsrcType)) {
            neededRsrc[neededRsrcCount] = rsrcType;
            neededRsrcCount++;
        } else {
            notNeededRsrc[notNeededRsrcCount] = rsrcType;
            notNeededRsrcCount++;
        }
    }
    for (int j = neededRsrcCount - 1; j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            // D.ebugPrintln("neededRsrc[i]="+neededRsrc[i]+" "+rollsPerResource[neededRsrc[i]]);
            if (rollsPerResource[neededRsrc[i]] > rollsPerResource[neededRsrc[i + 1]]) {
                // D.ebugPrintln("swap with "+neededRsrc[i+1]+" "+rollsPerResource[neededRsrc[i+1]]);
                int tmp = neededRsrc[i];
                neededRsrc[i] = neededRsrc[i + 1];
                neededRsrc[i + 1] = tmp;
            }
        }
    }
    if (D.ebugOn) {
        for (int i = 0; i < neededRsrcCount; i++) {
            D.ebugPrintln("NEEDED RSRC: " + neededRsrc[i] + " : " + rollsPerResource[neededRsrc[i]]);
        }
    }
    for (int j = notNeededRsrcCount - 1; j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            // D.ebugPrintln("notNeededRsrc[i]="+notNeededRsrc[i]+" "+rollsPerResource[notNeededRsrc[i]]);
            if (rollsPerResource[notNeededRsrc[i]] > rollsPerResource[notNeededRsrc[i + 1]]) {
                // D.ebugPrintln("swap with "+notNeededRsrc[i+1]+" "+rollsPerResource[notNeededRsrc[i+1]]);
                int tmp = notNeededRsrc[i];
                notNeededRsrc[i] = notNeededRsrc[i + 1];
                notNeededRsrc[i + 1] = tmp;
            }
        }
    }
    if (D.ebugOn) {
        for (int i = 0; i < notNeededRsrcCount; i++) {
            D.ebugPrintln("NOT-NEEDED RSRC: " + notNeededRsrc[i] + " : " + rollsPerResource[notNeededRsrc[i]]);
        }
    }
    // /
    // / figure out which resources we don't have enough of
    // / that the offering player is selling
    // /
    int getRsrcIdx = neededRsrcCount - 1;
    while ((getRsrcIdx >= 0) && ((ourResources.getAmount(neededRsrc[getRsrcIdx]) >= targetResources.getAmount(neededRsrc[getRsrcIdx])) || !originalOffer.getGiveSet().contains(neededRsrc[getRsrcIdx]))) {
        getRsrcIdx--;
    }
    // /
    if (getRsrcIdx >= 0) {
        D.ebugPrintln("*** getRsrc = " + neededRsrc[getRsrcIdx]);
        getResourceSet.add(1, neededRsrc[getRsrcIdx]);
        D.ebugPrintln("*** counterOffer should be null : counterOffer = " + counterOffer);
        // /
        // / consider offers where we give one unneeded for one needed
        // /
        int giveRsrcIdx = 0;
        while ((giveRsrcIdx < notNeededRsrcCount) && (counterOffer == null)) {
            D.ebugPrintln("*** ourResources.getAmount(" + notNeededRsrc[giveRsrcIdx] + ") = " + ourResources.getAmount(notNeededRsrc[giveRsrcIdx]));
            if (ourResources.contains(notNeededRsrc[giveRsrcIdx])) {
                giveResourceSet.clear();
                giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx]);
                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                D.ebugPrintln("*** counterOffer = " + counterOffer);
            }
            giveRsrcIdx++;
        }
        D.ebugPrintln("*** ourResources = " + ourResources);
        // /
        if (counterOffer == null) {
            int giveRsrcIdx1 = 0;
            while ((giveRsrcIdx1 < neededRsrcCount) && (counterOffer == null)) {
                D.ebugPrintln("*** ourResources.getAmount(" + neededRsrc[giveRsrcIdx1] + ") = " + ourResources.getAmount(neededRsrc[giveRsrcIdx1]));
                D.ebugPrintln("*** targetResources.getAmount(" + neededRsrc[giveRsrcIdx1] + ") = " + targetResources.getAmount(neededRsrc[giveRsrcIdx1]));
                if ((ourResources.getAmount(neededRsrc[giveRsrcIdx1]) > targetResources.getAmount(neededRsrc[giveRsrcIdx1])) && (neededRsrc[giveRsrcIdx1] != neededRsrc[getRsrcIdx])) {
                    giveResourceSet.clear();
                    giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                    // /
                    // / make sure the offer is better than our BATNA
                    // /
                    int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                    if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                        counterOffer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                        D.ebugPrintln("*** counterOffer = " + counterOffer);
                        D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                    }
                }
                giveRsrcIdx1++;
            }
        }
        D.ebugPrintln("*** ourResources = " + ourResources);
        SOCResourceSet leftovers = ourResources.copy();
        leftovers.subtract(targetResources);
        D.ebugPrintln("*** leftovers = " + leftovers);
        // /
        if (counterOffer == null) {
            int giveRsrcIdx1 = 0;
            int giveRsrcIdx2 = 0;
            while ((giveRsrcIdx1 < notNeededRsrcCount) && (counterOffer == null)) {
                if (ourResources.contains(notNeededRsrc[giveRsrcIdx1])) {
                    while ((giveRsrcIdx2 < notNeededRsrcCount) && (counterOffer == null)) {
                        giveResourceSet.clear();
                        giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                        giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx2]);
                        if (ourResources.contains(giveResourceSet)) {
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        giveRsrcIdx2++;
                    }
                    giveRsrcIdx2 = 0;
                    while ((giveRsrcIdx2 < neededRsrcCount) && (counterOffer == null)) {
                        if (neededRsrc[giveRsrcIdx2] != neededRsrc[getRsrcIdx]) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx2]);
                            if (leftovers.contains(giveResourceSet)) {
                                // /
                                // / make sure the offer is better than our BATNA
                                // /
                                int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                                if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                    counterOffer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                    D.ebugPrintln("*** counterOffer = " + counterOffer);
                                    D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                                }
                            }
                        }
                        giveRsrcIdx2++;
                    }
                }
                giveRsrcIdx1++;
            }
            giveRsrcIdx1 = 0;
            giveRsrcIdx2 = 0;
            while ((giveRsrcIdx1 < neededRsrcCount) && (counterOffer == null)) {
                if (leftovers.contains(neededRsrc[giveRsrcIdx1]) && (neededRsrc[giveRsrcIdx1] != neededRsrc[getRsrcIdx])) {
                    while ((giveRsrcIdx2 < notNeededRsrcCount) && (counterOffer == null)) {
                        giveResourceSet.clear();
                        giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                        giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx2]);
                        if (leftovers.contains(giveResourceSet)) {
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        giveRsrcIdx2++;
                    }
                    giveRsrcIdx2 = 0;
                    while ((giveRsrcIdx2 < neededRsrcCount) && (counterOffer == null)) {
                        if (neededRsrc[giveRsrcIdx2] != neededRsrc[getRsrcIdx]) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx2]);
                            if (leftovers.contains(giveResourceSet)) {
                                // /
                                // / make sure the offer is better than our BATNA
                                // /
                                int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                                if ((offerBuildingTime < batnaBuildingTime) || ((batna != null) && (offerBuildingTime == batnaBuildingTime) && (giveResourceSet.getTotal() < batna.getGiveSet().getTotal()))) {
                                    counterOffer = makeOfferAux(giveResourceSet, getResourceSet, neededRsrc[getRsrcIdx]);
                                    D.ebugPrintln("*** counterOffer = " + counterOffer);
                                    D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                                }
                            }
                        }
                        giveRsrcIdx2++;
                    }
                }
                giveRsrcIdx1++;
            }
        }
    }
    // /
    if (counterOffer == null) {
        SOCResourceSet leftovers = ourResources.copy();
        leftovers.subtract(targetResources);
        D.ebugPrintln("*** leftovers = " + leftovers);
        int getRsrcIdx2 = notNeededRsrcCount - 1;
        while ((getRsrcIdx2 >= 0) && !originalOffer.getGiveSet().contains(notNeededRsrc[getRsrcIdx2])) {
            getRsrcIdx2--;
        }
        while ((getRsrcIdx2 >= 0) && (counterOffer == null)) {
            getResourceSet.clear();
            getResourceSet.add(1, notNeededRsrc[getRsrcIdx2]);
            leftovers.add(1, notNeededRsrc[getRsrcIdx2]);
            // /
            if (counterOffer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < notNeededRsrcCount) && (counterOffer == null)) {
                    if (leftovers.contains(notNeededRsrc[giveRsrcIdx1]) && (notNeededRsrc[giveRsrcIdx1] != notNeededRsrc[getRsrcIdx2])) {
                        leftovers.subtract(1, notNeededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        leftovers.add(1, notNeededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            // /
            if (counterOffer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < neededRsrcCount) && (counterOffer == null)) {
                    if (leftovers.contains(neededRsrc[giveRsrcIdx1])) {
                        leftovers.subtract(1, neededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                            }
                        }
                        leftovers.add(1, neededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            leftovers.subtract(1, notNeededRsrc[getRsrcIdx2]);
            getRsrcIdx2--;
        }
    }
    // /
    if (counterOffer == null) {
        SOCResourceSet leftovers = ourResources.copy();
        leftovers.subtract(targetResources);
        D.ebugPrintln("*** leftovers = " + leftovers);
        int getRsrcIdx2 = notNeededRsrcCount - 1;
        while ((getRsrcIdx2 >= 0) && !originalOffer.getGiveSet().contains(notNeededRsrc[getRsrcIdx2])) {
            getRsrcIdx2--;
        }
        while ((getRsrcIdx2 >= 0) && (counterOffer == null)) {
            getResourceSet.clear();
            getResourceSet.add(2, notNeededRsrc[getRsrcIdx2]);
            leftovers.add(2, notNeededRsrc[getRsrcIdx2]);
            // /
            if (counterOffer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < notNeededRsrcCount) && (counterOffer == null)) {
                    if (leftovers.contains(notNeededRsrc[giveRsrcIdx1]) && (notNeededRsrc[giveRsrcIdx1] != notNeededRsrc[getRsrcIdx2])) {
                        leftovers.subtract(1, notNeededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        leftovers.add(1, notNeededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            // /
            if (counterOffer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < neededRsrcCount) && (counterOffer == null)) {
                    if (leftovers.contains(neededRsrc[giveRsrcIdx1])) {
                        leftovers.subtract(1, neededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                            }
                        }
                        leftovers.add(1, neededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            leftovers.subtract(2, notNeededRsrc[getRsrcIdx2]);
            getRsrcIdx2--;
        }
    }
    // /
    if (counterOffer == null) {
        SOCResourceSet leftovers = ourResources.copy();
        leftovers.subtract(targetResources);
        D.ebugPrintln("*** leftovers = " + leftovers);
        int getRsrcIdx2 = notNeededRsrcCount - 1;
        while ((getRsrcIdx2 >= 0) && !originalOffer.getGiveSet().contains(notNeededRsrc[getRsrcIdx2])) {
            getRsrcIdx2--;
        }
        while ((getRsrcIdx2 >= 0) && (counterOffer == null)) {
            getResourceSet.clear();
            getResourceSet.add(3, notNeededRsrc[getRsrcIdx2]);
            leftovers.add(3, notNeededRsrc[getRsrcIdx2]);
            // /
            if (counterOffer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < notNeededRsrcCount) && (counterOffer == null)) {
                    if (leftovers.contains(notNeededRsrc[giveRsrcIdx1]) && (notNeededRsrc[giveRsrcIdx1] != notNeededRsrc[getRsrcIdx2])) {
                        leftovers.subtract(1, notNeededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, notNeededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                                D.ebugPrintln("*** offerBuildingTime = " + offerBuildingTime);
                            }
                        }
                        leftovers.add(1, notNeededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            // /
            if (counterOffer == null) {
                int giveRsrcIdx1 = 0;
                while ((giveRsrcIdx1 < neededRsrcCount) && (counterOffer == null)) {
                    if (leftovers.contains(neededRsrc[giveRsrcIdx1])) {
                        leftovers.subtract(1, neededRsrc[giveRsrcIdx1]);
                        if (getOfferToBank(targetResources, leftovers) != null) {
                            giveResourceSet.clear();
                            giveResourceSet.add(1, neededRsrc[giveRsrcIdx1]);
                            // /
                            // / make sure the offer is better than our BATNA
                            // /
                            int offerBuildingTime = getETAToTargetResources(ourPlayerData, targetResources, giveResourceSet, getResourceSet, estimate);
                            if (offerBuildingTime < batnaBuildingTime) {
                                counterOffer = makeOfferAux(giveResourceSet, getResourceSet, notNeededRsrc[getRsrcIdx2]);
                                D.ebugPrintln("*** counterOffer = " + counterOffer);
                            }
                        }
                        leftovers.add(1, neededRsrc[giveRsrcIdx1]);
                    }
                    giveRsrcIdx1++;
                }
            }
            leftovers.subtract(3, notNeededRsrc[getRsrcIdx2]);
            getRsrcIdx2--;
        }
    }
    return counterOffer;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Example 35 with SOCResourceSet

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

the class SOCRobotNegotiator method getOfferToBank.

/**
 * @return the offer that we'll make to the bank/ports,
 *     or {@code null} if {@code ourResources} already contains all needed {@code targetResources}
 *
 * @param targetResources  what resources we want
 * @param ourResources     the resources we have
 */
public SOCTradeOffer getOfferToBank(SOCResourceSet targetResources, SOCResourceSet ourResources) {
    SOCTradeOffer bankTrade = null;
    if (ourResources.contains(targetResources)) {
        return bankTrade;
    }
    SOCBuildingSpeedEstimate estimate = new SOCBuildingSpeedEstimate(ourPlayerData.getNumbers());
    int[] rollsPerResource = estimate.getRollsPerResource();
    boolean[] ports = ourPlayerData.getPortFlags();
    /**
     * do any possible trading with the bank/ports
     */
    // /
    // / Seperate resource types into needed and not-needed.  Sort
    // / groups by frequency, most to least.  Start with most frequent
    // / not-needed resources.  Trade for least frequent needed resources.
    // / Loop until freq. of give resource + thresh >= get resource freq.
    // / and there is not enough of that resource to trade after
    // / subtracting needed ammount.
    // /
    int[] neededRsrc = new int[5];
    int[] notNeededRsrc = new int[5];
    int neededRsrcCount = 0;
    int notNeededRsrcCount = 0;
    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
        if (targetResources.contains(rsrcType)) {
            neededRsrc[neededRsrcCount] = rsrcType;
            neededRsrcCount++;
        } else {
            notNeededRsrc[notNeededRsrcCount] = rsrcType;
            notNeededRsrcCount++;
        }
    }
    if (neededRsrcCount == 0) {
        // <--- Early return bankTrade (null): nothing needed ---
        return bankTrade;
    }
    for (int j = neededRsrcCount - 1; j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            // D.ebugPrintln("neededRsrc[i]="+neededRsrc[i]+" "+rollsPerResource[neededRsrc[i]]);
            if (rollsPerResource[neededRsrc[i]] > rollsPerResource[neededRsrc[i + 1]]) {
                // D.ebugPrintln("swap with "+neededRsrc[i+1]+" "+rollsPerResource[neededRsrc[i+1]]);
                int tmp = neededRsrc[i];
                neededRsrc[i] = neededRsrc[i + 1];
                neededRsrc[i + 1] = tmp;
            }
        }
    }
    /*
           for (int i = 0; i < neededRsrcCount; i++) {
           //D.ebugPrintln("NEEDED RSRC: "+neededRsrc[i]+" : "+rollsPerResource[neededRsrc[i]]);
           }
         */
    for (int j = notNeededRsrcCount - 1; j >= 0; j--) {
        for (int i = 0; i < j; i++) {
            // D.ebugPrintln("notNeededRsrc[i]="+notNeededRsrc[i]+" "+rollsPerResource[notNeededRsrc[i]]);
            if (rollsPerResource[notNeededRsrc[i]] > rollsPerResource[notNeededRsrc[i + 1]]) {
                // D.ebugPrintln("swap with "+notNeededRsrc[i+1]+" "+rollsPerResource[notNeededRsrc[i+1]]);
                int tmp = notNeededRsrc[i];
                notNeededRsrc[i] = notNeededRsrc[i + 1];
                notNeededRsrc[i + 1] = tmp;
            }
        }
    }
    /*
           for (int i = 0; i < notNeededRsrcCount; i++) {
           //D.ebugPrintln("NOT-NEEDED RSRC: "+notNeededRsrc[i]+" : "+rollsPerResource[notNeededRsrc[i]]);
           }
         */
    // /
    // / figure out which resources we don't have enough of
    // /
    int getRsrcIdx = neededRsrcCount - 1;
    while (ourResources.getAmount(neededRsrc[getRsrcIdx]) >= targetResources.getAmount(neededRsrc[getRsrcIdx])) {
        getRsrcIdx--;
    }
    int giveRsrcIdx = 0;
    while (giveRsrcIdx < notNeededRsrcCount) {
        // /
        // / find the ratio at which we can trade
        // /
        int tradeRatio;
        if (ports[notNeededRsrc[giveRsrcIdx]]) {
            tradeRatio = 2;
        } else if (ports[SOCBoard.MISC_PORT]) {
            tradeRatio = 3;
        } else {
            tradeRatio = 4;
        }
        if (ourResources.getAmount(notNeededRsrc[giveRsrcIdx]) >= tradeRatio) {
            // /
            // / make the trade
            // /
            SOCResourceSet give = new SOCResourceSet();
            SOCResourceSet get = new SOCResourceSet();
            give.add(tradeRatio, notNeededRsrc[giveRsrcIdx]);
            get.add(1, neededRsrc[getRsrcIdx]);
            // D.ebugPrintln("our resources: "+ourPlayerData.getResources());
            // D.ebugPrintln("Making bank trade:");
            // D.ebugPrintln("give: "+give);
            // D.ebugPrintln("get: "+get);
            boolean[] to = new boolean[game.maxPlayers];
            for (int i = 0; i < game.maxPlayers; i++) {
                to[i] = false;
            }
            bankTrade = new SOCTradeOffer(game.getName(), ourPlayerNumber, to, give, get);
            return bankTrade;
        }
        giveRsrcIdx++;
    }
    // /
    // / Can't trade not-needed resources.
    // / Try trading needed resources.
    // /
    giveRsrcIdx = 0;
    while (giveRsrcIdx < neededRsrcCount) {
        // /
        // / find the ratio at which we can trade
        // /
        int tradeRatio;
        if (ports[neededRsrc[giveRsrcIdx]]) {
            tradeRatio = 2;
        } else if (ports[SOCBoard.MISC_PORT]) {
            tradeRatio = 3;
        } else {
            tradeRatio = 4;
        }
        if (rollsPerResource[neededRsrc[giveRsrcIdx]] >= rollsPerResource[neededRsrc[getRsrcIdx]]) {
            // /
            if ((ourResources.getAmount(neededRsrc[giveRsrcIdx]) - targetResources.getAmount(neededRsrc[giveRsrcIdx])) >= tradeRatio) {
                // /
                // / make the trade
                // /
                SOCResourceSet give = new SOCResourceSet();
                SOCResourceSet get = new SOCResourceSet();
                give.add(tradeRatio, neededRsrc[giveRsrcIdx]);
                get.add(1, neededRsrc[getRsrcIdx]);
                // D.ebugPrintln("our resources: "+ourPlayerData.getResources());
                // D.ebugPrintln("Making bank trade:");
                // D.ebugPrintln("give: "+give);
                // D.ebugPrintln("get: "+get);
                boolean[] to = new boolean[game.maxPlayers];
                for (int i = 0; i < game.maxPlayers; i++) {
                    to[i] = false;
                }
                bankTrade = new SOCTradeOffer(game.getName(), ourPlayerNumber, to, give, get);
                return bankTrade;
            }
        } else {
            // /
            if (ourResources.getAmount(neededRsrc[giveRsrcIdx]) >= tradeRatio) {
                // /
                // / make the trade
                // /
                SOCResourceSet give = new SOCResourceSet();
                SOCResourceSet get = new SOCResourceSet();
                give.add(tradeRatio, neededRsrc[giveRsrcIdx]);
                get.add(1, neededRsrc[getRsrcIdx]);
                // D.ebugPrintln("our resources: "+ourPlayerData.getResources());
                // D.ebugPrintln("Making bank trade:");
                // D.ebugPrintln("give: "+give);
                // D.ebugPrintln("get: "+get);
                boolean[] to = new boolean[game.maxPlayers];
                for (int i = 0; i < game.maxPlayers; i++) {
                    to[i] = false;
                }
                bankTrade = new SOCTradeOffer(game.getName(), ourPlayerNumber, to, give, get);
                return bankTrade;
            }
        }
        giveRsrcIdx++;
    }
    return bankTrade;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Aggregations

SOCResourceSet (soc.game.SOCResourceSet)54 Test (org.junit.Test)14 SOCTradeOffer (soc.game.SOCTradeOffer)10 SOCPlayer (soc.game.SOCPlayer)8 Stack (java.util.Stack)4 SOCShip (soc.game.SOCShip)3 ArrayList (java.util.ArrayList)2 StringTokenizer (java.util.StringTokenizer)2 SOCBoardLarge (soc.game.SOCBoardLarge)2 SOCGame (soc.game.SOCGame)2 SOCRoad (soc.game.SOCRoad)2 Queue (soc.util.Queue)2 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MissingResourceException (java.util.MissingResourceException)1 Vector (java.util.Vector)1 SOCInventoryItem (soc.game.SOCInventoryItem)1 SOCLRPathData (soc.game.SOCLRPathData)1 SOCSpecialItem (soc.game.SOCSpecialItem)1