Search in sources :

Example 6 with SOCResourceSet

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

the class SOCRobotBrain method handleREJECTOFFER.

/**
 * Handle a REJECTOFFER for this game.
 * watch rejections of other players' offers, and of our offers.
 * @since 1.1.08
 */
private void handleREJECTOFFER(SOCRejectOffer mes) {
    // /
    // / see if everyone has rejected our offer
    // /
    int rejector = mes.getPlayerNumber();
    if ((ourPlayerData.getCurrentOffer() != null) && (waitingForTradeResponse)) {
        D.ebugPrintln("%%%%%%%%% REJECT OFFER %%%%%%%%%%%%%");
        // /
        // / record which player said no
        // /
        SOCResourceSet getSet = ourPlayerData.getCurrentOffer().getGetSet();
        for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
            if (getSet.contains(rsrcType) && !negotiator.wantsAnotherOffer(rejector, rsrcType))
                negotiator.markAsNotSelling(rejector, rsrcType);
        }
        offerRejections[mes.getPlayerNumber()] = true;
        boolean everyoneRejected = true;
        D.ebugPrintln("ourPlayerData.getCurrentOffer() = " + ourPlayerData.getCurrentOffer());
        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;
        }
    } else {
        // /
        // / we also want to watch rejections of other players' offers
        // /
        D.ebugPrintln("%%%% ALT REJECT OFFER %%%%");
        for (int pn = 0; pn < game.maxPlayers; pn++) {
            SOCTradeOffer offer = game.getPlayer(pn).getCurrentOffer();
            if (offer != null) {
                boolean[] offeredTo = offer.getTo();
                if (offeredTo[rejector]) {
                    // 
                    // I think they were rejecting this offer
                    // mark them as not selling what was asked for
                    // 
                    SOCResourceSet getSet = offer.getGetSet();
                    for (int rsrcType = SOCResourceConstants.CLAY; rsrcType <= SOCResourceConstants.WOOD; rsrcType++) {
                        if (getSet.contains(rsrcType) && !negotiator.wantsAnotherOffer(rejector, rsrcType))
                            negotiator.markAsNotSelling(rejector, rsrcType);
                    }
                }
            }
        }
    }
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet)

Example 7 with SOCResourceSet

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

the class SOCRobotBrain method expandTradeTreeNode.

/**
 * expand a trade tree node
 *
 * @param currentTreeNode   the tree node that we're expanding
 * @param table  the table of all of the nodes in the tree except this one
 */
protected void expandTradeTreeNode(SOCTradeTree currentTreeNode, Hashtable<SOCResourceSet, SOCTradeTree> table) {
    /**
     * the resources that we have to work with
     */
    SOCResourceSet rSet = currentTreeNode.getResourceSet();
    /**
     * go through the resources one by one, and generate all possible
     * resource sets that result from trading that type of resource
     */
    for (int giveResource = SOCResourceConstants.CLAY; giveResource <= SOCResourceConstants.WOOD; giveResource++) {
        /**
         * find the ratio at which we can trade
         */
        int tradeRatio;
        if (ourPlayerData.getPortFlag(giveResource)) {
            tradeRatio = 2;
        } else if (ourPlayerData.getPortFlag(SOCBoard.MISC_PORT)) {
            tradeRatio = 3;
        } else {
            tradeRatio = 4;
        }
        /**
         * make sure we have enough resources to trade
         */
        if (rSet.getAmount(giveResource) >= tradeRatio) {
            /**
             * trade the resource that we're looking at for one
             * of every other resource
             */
            for (int getResource = SOCResourceConstants.CLAY; getResource <= SOCResourceConstants.WOOD; getResource++) {
                if (getResource != giveResource) {
                    SOCResourceSet newTradeResult = rSet.copy();
                    newTradeResult.subtract(tradeRatio, giveResource);
                    newTradeResult.add(1, getResource);
                    SOCTradeTree newTree = new SOCTradeTree(newTradeResult, currentTreeNode);
                    /**
                     * if the trade results in a set of resources that is
                     * equal to or worse than a trade we've already seen,
                     * then we don't want to expand this tree node
                     */
                    Enumeration<SOCResourceSet> tableEnum = table.keys();
                    while (tableEnum.hasMoreElements()) {
                        SOCResourceSet oldTradeResult = tableEnum.nextElement();
                        /*
                               //D.ebugPrintln("%%%     "+newTradeResult);
                               //D.ebugPrintln("%%%  <= "+oldTradeResult+" : "+
                               SOCResourceSet.lte(newTradeResult, oldTradeResult));
                             */
                        if (SOCResourceSet.lte(newTradeResult, oldTradeResult)) {
                            newTree.setNeedsToBeExpanded(false);
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 8 with SOCResourceSet

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

the class SOCRobotBrain method pickFreeResources.

/**
 * Respond to server's request to pick resources to gain from the Gold Hex.
 * Use {@link #buildingPlan} or, if that's empty (initial placement),
 * pick what's rare from {@link OpeningBuildStrategy#estimateResourceRarity()}.
 * @param numChoose  Number of resources to pick
 * @since 2.0.00
 */
protected void pickFreeResources(int numChoose) {
    SOCResourceSet targetResources;
    // try to make a plan if we don't have one
    if (buildingPlan.isEmpty()) {
        planBuilding();
    }
    if (!buildingPlan.isEmpty()) {
        final SOCPossiblePiece targetPiece = buildingPlan.peek();
        // may be null
        targetResources = targetPiece.getResourcesToBuild();
        chooseFreeResourcesIfNeeded(targetResources, numChoose, true);
    } else {
        // Pick based on board dice-roll rarities.
        // TODO: After initial placement, consider based on our
        // number probabilities based on settlements/cities placed.
        // (BSE.getRollsForResourcesSorted)
        resourceChoices.clear();
        final int[] resourceEstimates = openingBuildStrategy.estimateResourceRarity();
        // in case we pick 5, keep going for 6-10
        int numEach = 0;
        while (numChoose > 0) {
            int res = -1, pct = Integer.MAX_VALUE;
            for (int i = SOCBoard.CLAY_HEX; i <= SOCBoard.WOOD_HEX; ++i) {
                if ((resourceEstimates[i] < pct) && (resourceChoices.getAmount(i) < numEach)) {
                    res = i;
                    pct = resourceEstimates[i];
                }
            }
            if (res != -1) {
                resourceChoices.add(1, res);
                --numChoose;
            } else {
                // has chosen all 5 by now
                ++numEach;
            }
        }
    }
    client.pickResources(game, resourceChoices);
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 9 with SOCResourceSet

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

the class SOCRobotBrain method printResources.

/**
 * For each player in game:
 * client.sendText, and debug-print to console, game.getPlayer(i).getResources()
 */
private void printResources() {
    if (D.ebugOn) {
        for (int i = 0; i < game.maxPlayers; i++) {
            SOCResourceSet rsrcs = game.getPlayer(i).getResources();
            String resourceMessage = "PLAYER " + i + " RESOURCES: ";
            resourceMessage += (rsrcs.getAmount(SOCResourceConstants.CLAY) + " ");
            resourceMessage += (rsrcs.getAmount(SOCResourceConstants.ORE) + " ");
            resourceMessage += (rsrcs.getAmount(SOCResourceConstants.SHEEP) + " ");
            resourceMessage += (rsrcs.getAmount(SOCResourceConstants.WHEAT) + " ");
            resourceMessage += (rsrcs.getAmount(SOCResourceConstants.WOOD) + " ");
            resourceMessage += (rsrcs.getAmount(SOCResourceConstants.UNKNOWN) + " ");
            client.sendText(game, resourceMessage);
            D.ebugPrintln(resourceMessage);
        }
    }
}
Also used : SOCResourceSet(soc.game.SOCResourceSet)

Example 10 with SOCResourceSet

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

the class SOCRobotBrain method handlePLAYERELEMENT_numRsrc.

/**
 * Update a player's amount of a resource.
 *<ul>
 *<LI> If this is a {@link SOCPlayerElement#LOSE} action, and the player does not have enough of that type,
 *     the rest are taken from the player's UNKNOWN amount.
 *<LI> If we are losing from type UNKNOWN,
 *     first convert player's known resources to unknown resources
 *     (individual amount information will be lost),
 *     then remove mes's unknown resources from player.
 *<LI> If this is a SET action, and it's for our own robot player,
 *     check the amount against {@link #ourPlayerData}, and debug print
 *     if they don't match already.
 *</ul>
 *<P>
 * If our player is losing a resource needed for the {@link #buildingPlan},
 * clear the plan if this is for the Special Building Phase (on the 6-player board).
 * In normal game play, we clear the building plan at the start of each turn.
 *<P>
 * Before v2.0.00 this method directly took a {@link SOCPlayerElement} instead of that message's
 * {@code action} and {@code amount} fields.
 *
 * @param pl       Player to update
 * @param action   {@link SOCPlayerElement#SET}, {@link SOCPlayerElement#GAIN GAIN},
 *     or {@link SOCPlayerElement#LOSE LOSE}
 * @param rtype    Type of resource, like {@link SOCResourceConstants#CLAY}
 * @param rtypeStr Resource type name, for debugging
 * @param amount   The new value to set, or the delta to gain/lose
 */
// unnecessary dead-code warning "if (D.ebugOn)"
@SuppressWarnings("unused")
protected void handlePLAYERELEMENT_numRsrc(SOCPlayer pl, final int action, int rtype, String rtypeStr, final int amount) {
    /**
     * for SET, check the amount of unknown resources against
     * what we think we know about our player.
     */
    if (D.ebugOn && (pl == ourPlayerData) && (action == SOCPlayerElement.SET)) {
        if (amount != ourPlayerData.getResources().getAmount(rtype)) {
            client.sendText(game, ">>> RSRC ERROR FOR " + rtypeStr + ": " + amount + " != " + ourPlayerData.getResources().getAmount(rtype));
        }
    }
    /**
     * Update game data.
     */
    SOCDisplaylessPlayerClient.handlePLAYERELEMENT_numRsrc(pl, action, rtype, amount);
    /**
     * Clear building plan, if we just lost a resource we need.
     * Only necessary for Special Building Phase (6-player board),
     * because in normal game play, we clear the building plan
     * at the start of each turn.
     */
    if (waitingForSpecialBuild && (pl == ourPlayerData) && (action != SOCPlayerElement.GAIN) && !buildingPlan.isEmpty()) {
        final SOCPossiblePiece targetPiece = buildingPlan.peek();
        // may be null
        final SOCResourceSet targetResources = targetPiece.getResourcesToBuild();
        if (!ourPlayerData.getResources().contains(targetResources)) {
            buildingPlan.clear();
        // The buildingPlan is clear, so we'll calculate
        // a new plan when our Special Building turn begins.
        // Don't clear decidedIfSpecialBuild flag, to prevent
        // needless plan calculation before our turn begins,
        // especially from multiple PLAYERELEMENT(LOSE),
        // as may happen for a discard.
        }
    }
}
Also used : 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