Search in sources :

Example 11 with SOCTradeOffer

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

the class SOCRobotNegotiator method makeOfferAux.

/**
 * aux function for make offer
 */
protected SOCTradeOffer makeOfferAux(SOCResourceSet giveResourceSet, SOCResourceSet getResourceSet, int neededResource) {
    D.ebugPrintln("**** makeOfferAux ****");
    D.ebugPrintln("giveResourceSet = " + giveResourceSet);
    D.ebugPrintln("getResourceSet = " + getResourceSet);
    SOCTradeOffer offer = null;
    // /
    // / see if we've made this offer before
    // /
    boolean match = false;
    Iterator<SOCTradeOffer> offersMadeIter = offersMade.iterator();
    while ((offersMadeIter.hasNext() && !match)) {
        SOCTradeOffer pastOffer = offersMadeIter.next();
        if ((pastOffer != null) && (pastOffer.getGiveSet().equals(giveResourceSet)) && (pastOffer.getGetSet().equals(getResourceSet))) {
            match = true;
        }
    }
    // /
    if (!match) {
        for (int i = 0; i < game.maxPlayers; i++) {
            if (i != ourPlayerNumber) {
                SOCTradeOffer outsideOffer = game.getPlayer(i).getCurrentOffer();
                if ((outsideOffer != null) && (outsideOffer.getGetSet().equals(giveResourceSet)) && (outsideOffer.getGiveSet().equals(getResourceSet))) {
                    match = true;
                    break;
                }
            }
        }
    }
    D.ebugPrintln("*** match = " + match);
    if (!match) {
        // /
        // / this is a new offer
        // /
        D.ebugPrintln("* this is a new offer");
        int numOfferedTo = 0;
        boolean[] offeredTo = new boolean[game.maxPlayers];
        // /
        if (game.getCurrentPlayerNumber() == ourPlayerNumber) {
            // /
            for (int i = 0; i < game.maxPlayers; i++) {
                D.ebugPrintln("** isSellingResource[" + i + "][" + neededResource + "] = " + isSellingResource[i][neededResource]);
                if ((i != ourPlayerNumber) && isSellingResource[i][neededResource] && (!game.isSeatVacant(i)) && (game.getPlayer(i).getResources().getTotal() >= getResourceSet.getTotal())) {
                    final SOCPlayerTracker tracker = playerTrackers.get(Integer.valueOf(i));
                    if ((tracker != null) && (tracker.getWinGameETA() >= WIN_GAME_CUTOFF)) {
                        numOfferedTo++;
                        offeredTo[i] = true;
                    } else {
                        offeredTo[i] = false;
                    }
                }
            }
        } else {
            // /
            // / it's not our turn, just offer to the player who's turn it is
            // /
            final int curpn = game.getCurrentPlayerNumber();
            if (isSellingResource[curpn][neededResource] && (game.getPlayer(curpn).getResources().getTotal() >= getResourceSet.getTotal())) {
                D.ebugPrintln("** isSellingResource[" + curpn + "][" + neededResource + "] = " + isSellingResource[curpn][neededResource]);
                final SOCPlayerTracker tracker = playerTrackers.get(Integer.valueOf(curpn));
                if ((tracker != null) && (tracker.getWinGameETA() >= WIN_GAME_CUTOFF)) {
                    numOfferedTo++;
                    offeredTo[curpn] = true;
                }
            }
        }
        D.ebugPrintln("** numOfferedTo = " + numOfferedTo);
        if (numOfferedTo > 0) {
            // /
            // /  the offer
            // /
            offer = new SOCTradeOffer(game.getName(), ourPlayerNumber, offeredTo, giveResourceSet, getResourceSet);
            // /
            // / only make the offer if we think someone will take it
            // /
            boolean acceptable = false;
            for (int pn = 0; pn < game.maxPlayers; pn++) {
                if (offeredTo[pn]) {
                    int offerResponse = considerOffer2(offer, pn);
                    D.ebugPrintln("* considerOffer2(offer, " + pn + ") = " + offerResponse);
                    if (offerResponse == ACCEPT_OFFER) {
                        acceptable = true;
                        break;
                    }
                }
            }
            if (!acceptable) {
                offer = null;
            }
        }
    }
    return offer;
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer)

Example 12 with SOCTradeOffer

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

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

Example 14 with SOCTradeOffer

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

the class SOCDisplaylessPlayerClient method handleMAKEOFFER.

/**
 * handle the "make offer" message
 * @param mes  the message
 */
protected void handleMAKEOFFER(SOCMakeOffer mes) {
    SOCGame ga = games.get(mes.getGame());
    if (ga != null) {
        SOCTradeOffer offer = mes.getOffer();
        ga.getPlayer(offer.getFrom()).setCurrentOffer(offer);
    }
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCGame(soc.game.SOCGame)

Example 15 with SOCTradeOffer

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

the class SOCGameMessageHandler method handleMAKEOFFER.

// / Player trades and bank trades ///
/**
 * handle "make offer" message.
 *
 * @param c  the connection that sent the message
 * @param mes  the message
 * @since 1.0.0
 */
private void handleMAKEOFFER(SOCGame ga, Connection c, final SOCMakeOffer mes) {
    final String gaName = ga.getName();
    if (ga.isGameOptionSet("NT")) {
        srv.messageToPlayer(c, gaName, "Trading is not allowed in this game.");
        // <---- Early return: No Trading ----
        return;
    }
    ga.takeMonitor();
    try {
        SOCTradeOffer offer = mes.getOffer();
        /**
         * remake the offer with data that we know is accurate,
         * namely the 'from' datum
         */
        SOCPlayer player = ga.getPlayer(c.getData());
        /**
         * announce the offer, including text message similar to bank/port trade.
         */
        if (player != null) {
            SOCTradeOffer remadeOffer;
            {
                SOCResourceSet offGive = offer.getGiveSet(), offGet = offer.getGetSet();
                remadeOffer = new SOCTradeOffer(gaName, player.getPlayerNumber(), offer.getTo(), offGive, offGet);
                player.setCurrentOffer(remadeOffer);
                // older clients need it sent from the server
                if (ga.clientVersionLowest < SOCStringManager.VERSION_FOR_I18N) {
                    // I18N OK: Pre-2.0.00 clients always use english
                    final String txt = SOCStringManager.getFallbackServerManagerForClient().formatSpecial(ga, "{0} offered to give {1,rsrcs} for {2,rsrcs}.", player.getName(), offGive, offGet);
                    srv.messageToGameForVersions(ga, 0, SOCStringManager.VERSION_FOR_I18N - 1, new SOCGameTextMsg(gaName, SOCServer.SERVERNAME, txt), true);
                }
            }
            SOCMakeOffer makeOfferMessage = new SOCMakeOffer(gaName, remadeOffer);
            srv.messageToGame(gaName, makeOfferMessage);
            srv.recordGameEvent(gaName, makeOfferMessage);
            /**
             * clear all the trade messages because a new offer has been made
             */
            srv.gameList.takeMonitorForGame(gaName);
            if (ga.clientVersionLowest >= SOCClearTradeMsg.VERSION_FOR_CLEAR_ALL) {
                srv.messageToGameWithMon(gaName, new SOCClearTradeMsg(gaName, -1));
            } else {
                for (int i = 0; i < ga.maxPlayers; i++) srv.messageToGameWithMon(gaName, new SOCClearTradeMsg(gaName, i));
            }
            srv.gameList.releaseMonitorForGame(gaName);
        }
    } catch (Exception e) {
        D.ebugPrintStackTrace(e, "Exception caught");
    }
    ga.releaseMonitor();
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCPlayer(soc.game.SOCPlayer) 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