use of soc.game.SOCTradeOffer 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);
}
}
}
}
}
}
use of soc.game.SOCTradeOffer in project JSettlers2 by jdmonin.
the class SOCHandPanel method updateCurrentOffer.
/**
* Display or update this player's trade offer, or hide if none.
* If a game reset request is in progress, don't show the offer, because
* they use the same display component ({@link #offer}). In that case
* the trade offer will be refreshed after the reset is cancelled.
*<P>
* Does not display if playerIsClient.
*
* @param isNewOffer If true this is for a newly made trade offer,
* not a refresh based on other game or player info.
* @param resourcesOnly If true, instead of updating the entire offer,
* only show or hide "Accept" button based on the client player's resources.
* Calls {@link TradeOfferPanel#updateOfferButtons()}.
* If no offer is currently visible, does nothing.
*/
public void updateCurrentOffer(final boolean isNewOffer, final boolean resourcesOnly) {
if (inPlay) {
if (resourcesOnly) {
offer.updateOfferButtons();
return;
}
SOCTradeOffer currentOffer = player.getCurrentOffer();
if (currentOffer != null) {
if (!(offerIsResetMessage || offerIsDiscardOrPickMessage)) {
if (!playerIsClient) {
offer.setOffer(currentOffer);
offer.setVisible(true);
if (offerHidesControls)
hideTradeMsgShowOthers(false);
offer.repaint();
if (isNewOffer && offer.isOfferToClientPlayer())
playerInterface.playSound(SOCPlayerInterface.SOUND_OFFERED_TRADE);
}
} else
// Will show after voting
offerIsMessageWasTrade = true;
} else {
clearOffer(false);
}
}
}
use of soc.game.SOCTradeOffer in project JSettlers2 by jdmonin.
the class SOCMakeOffer method parseDataStr.
/**
* Parse the command String into a MakeOffer message
*
* @param s the String to parse
* @return a MakeOffer message, or null if the data is garbled
*/
public static SOCMakeOffer parseDataStr(String s) {
// the game name
String ga;
// the number of the offering player
int from;
// the players to which this trade is offered
boolean[] to;
// the set of resources being asked for
SOCResourceSet give;
// the set of resources that the offerer wants in exchange
SOCResourceSet get;
give = new SOCResourceSet();
get = new SOCResourceSet();
StringTokenizer st = new StringTokenizer(s, sep2);
try {
ga = st.nextToken();
from = Integer.parseInt(st.nextToken());
// Should be == game.maxPlayers
final int numPlayerTokens = st.countTokens() - (2 * 5);
to = new boolean[numPlayerTokens];
for (int i = 0; i < numPlayerTokens; i++) {
to[i] = (Boolean.valueOf(st.nextToken())).booleanValue();
}
/**
* Note: this only works if SOCResourceConstants.CLAY == 1
*/
for (int i = 1; i <= SOCResourceConstants.WOOD; i++) {
give.setAmount(Integer.parseInt(st.nextToken()), i);
}
for (int i = 1; i <= SOCResourceConstants.WOOD; i++) {
get.setAmount(Integer.parseInt(st.nextToken()), i);
}
} catch (Exception e) {
return null;
}
return new SOCMakeOffer(ga, new SOCTradeOffer(ga, from, to, give, get));
}
use of soc.game.SOCTradeOffer in project JSettlers2 by jdmonin.
the class SOCRobotBrain method tradeToTarget2.
/**
* Make bank trades or port trades to get the target resources, if possible.
*
* @param targetResources the resources that we want, can be {@code null} for an empty set (method returns false)
* @return true if we sent a request to trade, false if
* we already have the resources or if we don't have
* enough to trade in for <tt>targetResources</tt>.
*/
protected boolean tradeToTarget2(SOCResourceSet targetResources) {
if ((targetResources == null) || ourPlayerData.getResources().contains(targetResources)) {
return false;
}
SOCTradeOffer bankTrade = negotiator.getOfferToBank(targetResources, ourPlayerData.getResources());
if ((bankTrade != null) && (ourPlayerData.getResources().contains(bankTrade.getGiveSet()))) {
client.bankTrade(game, bankTrade.getGiveSet(), bankTrade.getGetSet());
pause(2000);
return true;
}
return false;
}
use of soc.game.SOCTradeOffer in project JSettlers2 by jdmonin.
the class SOCRobotBrain method makeOffer.
/**
* Make a trade offer to another player, or decide to make no offer.
* Calls {@link SOCRobotNegotiator#makeOffer(SOCPossiblePiece)}.
* Will set either {@link #waitingForTradeResponse} or {@link #doneTrading},
* and update {@link #ourPlayerData}.{@link SOCPlayer#setCurrentOffer(SOCTradeOffer) setCurrentOffer()},
*
* @param target the resources that we want
* @return true if we made an offer
*/
protected boolean makeOffer(SOCPossiblePiece target) {
boolean result = false;
SOCTradeOffer offer = negotiator.makeOffer(target);
ourPlayerData.setCurrentOffer(offer);
negotiator.resetWantsAnotherOffer();
if (offer != null) {
// /
for (int i = 0; i < game.maxPlayers; i++) {
offerRejections[i] = false;
}
waitingForTradeResponse = true;
counter = 0;
client.offerTrade(game, offer);
result = true;
} else {
doneTrading = true;
waitingForTradeResponse = false;
}
return result;
}
Aggregations