Search in sources :

Example 6 with SOCTradeOffer

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

the class SOCRobotBrain method tradeStopWaitingClearOffer.

/**
 * Stop waiting for responses to a trade offer.
 * Remember other players' responses,
 * Call {@link SOCRobotClient#clearOffer(SOCGame) client.clearOffer},
 * clear {@link #waitingForTradeResponse} and {@link #counter}.
 * @since 1.1.09
 */
private void tradeStopWaitingClearOffer() {
    // /
    // / record which players said no by not saying anything
    // /
    SOCTradeOffer ourCurrentOffer = ourPlayerData.getCurrentOffer();
    if (ourCurrentOffer != null) {
        boolean[] offeredTo = ourCurrentOffer.getTo();
        SOCResourceSet getSet = ourCurrentOffer.getGetSet();
        for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
            if (getSet.contains(rsrcType)) {
                for (int pn = 0; pn < game.maxPlayers; pn++) {
                    if (offeredTo[pn]) {
                        negotiator.markAsNotSelling(pn, rsrcType);
                        negotiator.markAsNotWantingAnotherOffer(pn, rsrcType);
                    }
                }
            }
        }
        pause(1500);
        client.clearOffer(game);
        pause(500);
    }
    counter = 0;
    waitingForTradeResponse = false;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Example 7 with SOCTradeOffer

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

the class SOCRobotBrain method makeCounterOffer.

/**
 * Plan a counter offer to another player, and make it from our client.
 * Calls {@link SOCRobotNegotiator#makeCounterOffer(SOCTradeOffer)},
 * then {@link #ourPlayerData}{@link SOCPlayer#setCurrentOffer(SOCTradeOffer) .setCurrentOffer(..)}
 * with the result or {@code null}. Updates {@link #waitingForTradeResponse} flag.
 * If no counteroffer is made here, sets {@link #doneTrading}.
 *
 * @param offer  the other player's offer
 * @return true if we made and sent an offer
 */
protected boolean makeCounterOffer(SOCTradeOffer offer) {
    boolean result = false;
    SOCTradeOffer counterOffer = negotiator.makeCounterOffer(offer);
    ourPlayerData.setCurrentOffer(counterOffer);
    if (counterOffer != null) {
        // /
        // /  reset the offerRejections flag
        // /
        offerRejections[offer.getFrom()] = false;
        waitingForTradeResponse = true;
        counter = 0;
        client.offerTrade(game, counterOffer);
        result = true;
    } else {
        doneTrading = true;
        waitingForTradeResponse = false;
    }
    return result;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer)

Example 8 with SOCTradeOffer

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

the class SOCRobotBrain method handleMAKEOFFER.

/**
 * Handle a MAKEOFFER for this game.
 * if another player makes an offer, that's the
 * same as a rejection, but still wants to deal.
 * Call {@link #considerOffer(SOCTradeOffer)}, and if
 * we accept, clear our {@link #buildingPlan} so we'll replan it.
 * Ignore our own MAKEOFFERs echoed from server.
 * @since 1.1.08
 */
private void handleMAKEOFFER(SOCMakeOffer mes) {
    SOCTradeOffer offer = mes.getOffer();
    game.getPlayer(offer.getFrom()).setCurrentOffer(offer);
    if ((offer.getFrom() == ourPlayerNumber)) {
        // <---- Ignore our own offers ----
        return;
    }
    // /
    // / record that this player wants to sell me the stuff
    // /
    SOCResourceSet giveSet = offer.getGiveSet();
    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
        if (giveSet.contains(rsrcType)) {
            D.ebugPrintln("%%% player " + offer.getFrom() + " wants to sell " + rsrcType);
            negotiator.markAsWantsAnotherOffer(offer.getFrom(), rsrcType);
        }
    }
    // /
    // / record that this player is not selling the resources
    // / he is asking for
    // /
    SOCResourceSet getSet = offer.getGetSet();
    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
        if (getSet.contains(rsrcType)) {
            D.ebugPrintln("%%% player " + offer.getFrom() + " wants to buy " + rsrcType + " and therefore does not want to sell it");
            negotiator.markAsNotSelling(offer.getFrom(), rsrcType);
        }
    }
    if (waitingForTradeResponse) {
        offerRejections[offer.getFrom()] = true;
        boolean everyoneRejected = true;
        D.ebugPrintln("ourPlayerData.getCurrentOffer() = " + ourPlayerData.getCurrentOffer());
        if (ourPlayerData.getCurrentOffer() != null) {
            boolean[] offeredTo = ourPlayerData.getCurrentOffer().getTo();
            for (int i = 0; i < game.maxPlayers; i++) {
                D.ebugPrintln("offerRejections[" + i + "]=" + offerRejections[i]);
                if (offeredTo[i] && !offerRejections[i])
                    everyoneRejected = false;
            }
        }
        D.ebugPrintln("everyoneRejected=" + everyoneRejected);
        if (everyoneRejected) {
            negotiator.addToOffersMade(ourPlayerData.getCurrentOffer());
            client.clearOffer(game);
            waitingForTradeResponse = false;
        }
    }
    // /
    // / consider the offer
    // /
    int ourResponseToOffer = considerOffer(offer);
    D.ebugPrintln("%%% ourResponseToOffer = " + ourResponseToOffer);
    if (ourResponseToOffer < 0)
        // <--- Early return: SOCRobotNegotiator.IGNORE_OFFER ---
        return;
    int delayLength = Math.abs(rand.nextInt() % 500) + 3500;
    if (gameIs6Player && !waitingForTradeResponse) {
        // usually, pause is half-length in 6-player
        delayLength *= 2;
    }
    pause(delayLength);
    switch(ourResponseToOffer) {
        case SOCRobotNegotiator.ACCEPT_OFFER:
            client.acceptOffer(game, offer.getFrom());
            // /
            // / clear our building plan, so that we replan
            // /
            buildingPlan.clear();
            negotiator.setTargetPiece(ourPlayerNumber, null);
            break;
        case SOCRobotNegotiator.REJECT_OFFER:
            if (!waitingForTradeResponse)
                client.rejectOffer(game);
            break;
        case SOCRobotNegotiator.COUNTER_OFFER:
            if (!makeCounterOffer(offer))
                client.rejectOffer(game);
            break;
    }
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Example 9 with SOCTradeOffer

use of soc.game.SOCTradeOffer 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 10 with SOCTradeOffer

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

Aggregations

SOCTradeOffer (soc.game.SOCTradeOffer)16 SOCResourceSet (soc.game.SOCResourceSet)10 SOCGame (soc.game.SOCGame)2 SOCPlayer (soc.game.SOCPlayer)2 Stack (java.util.Stack)1 StringTokenizer (java.util.StringTokenizer)1