use of soc.game.SOCPlayer in project JSettlers2 by jdmonin.
the class SOCGameMessageHandler method handleACCEPTOFFER.
/**
* handle "accept offer" message.
*
* @param c the connection that sent the message
* @param mes the message
* @since 1.0.0
*/
private void handleACCEPTOFFER(SOCGame ga, Connection c, final SOCAcceptOffer mes) {
ga.takeMonitor();
try {
SOCPlayer player = ga.getPlayer(c.getData());
if (player != null) {
final int acceptingNumber = player.getPlayerNumber();
final int offeringNumber = mes.getOfferingNumber();
final String gaName = ga.getName();
if (ga.canMakeTrade(offeringNumber, acceptingNumber)) {
ga.makeTrade(offeringNumber, acceptingNumber);
handler.reportTrade(ga, offeringNumber, acceptingNumber);
srv.recordGameEvent(gaName, mes);
/**
* clear all offers
*/
for (int i = 0; i < ga.maxPlayers; i++) ga.getPlayer(i).setCurrentOffer(null);
srv.gameList.takeMonitorForGame(gaName);
if (ga.clientVersionLowest >= SOCClearOffer.VERSION_FOR_CLEAR_ALL)
srv.messageToGameWithMon(gaName, new SOCClearOffer(gaName, -1));
else
for (int i = 0; i < ga.maxPlayers; i++) srv.messageToGameWithMon(gaName, new SOCClearOffer(gaName, i));
srv.gameList.releaseMonitorForGame(gaName);
/**
* send a message (for the bots) that the offer was accepted
*/
srv.messageToGame(gaName, mes);
} else {
srv.messageToPlayer(c, gaName, "You can't make that trade.");
}
}
} catch (Exception e) {
D.ebugPrintStackTrace(e, "Exception caught");
}
ga.releaseMonitor();
}
use of soc.game.SOCPlayer in project JSettlers2 by jdmonin.
the class SOCGameMessageHandler method handleDEBUGFREEPLACE.
/**
* Handle the client's debug Free Placement putpiece request.
* @since 1.1.12
*/
private void handleDEBUGFREEPLACE(final SOCGame ga, final Connection c, final SOCDebugFreePlace mes) {
if (!ga.isDebugFreePlacement())
return;
final String gaName = ga.getName();
final int coord = mes.getCoordinates();
final SOCPlayer player = ga.getPlayer(mes.getPlayerNumber());
if (player == null)
return;
boolean didPut = false;
final int pieceType = mes.getPieceType();
final boolean initialDeny = ga.isInitialPlacement() && !player.canBuildInitialPieceType(pieceType);
switch(pieceType) {
case SOCPlayingPiece.ROAD:
if (player.isPotentialRoad(coord) && !initialDeny) {
ga.putPiece(new SOCRoad(player, coord, null));
didPut = true;
}
break;
case SOCPlayingPiece.SETTLEMENT:
if (player.canPlaceSettlement(coord) && !initialDeny) {
ga.putPiece(new SOCSettlement(player, coord, null));
didPut = true;
}
break;
case SOCPlayingPiece.CITY:
if (player.isPotentialCity(coord) && !initialDeny) {
ga.putPiece(new SOCCity(player, coord, null));
didPut = true;
}
break;
case SOCPlayingPiece.SHIP:
if (ga.canPlaceShip(player, coord) && !initialDeny) {
ga.putPiece(new SOCShip(player, coord, null));
didPut = true;
}
break;
default:
srv.messageToPlayer(c, gaName, "* Unknown piece type: " + pieceType);
}
if (didPut) {
srv.messageToGame(gaName, new SOCPutPiece(gaName, mes.getPlayerNumber(), pieceType, coord));
// Check for initial settlement next to gold hex
if (pieceType == SOCPlayingPiece.SETTLEMENT) {
final int numGoldRes = player.getNeedToPickGoldHexResources();
if (numGoldRes > 0)
srv.messageToPlayer(c, new SOCSimpleRequest(gaName, player.getPlayerNumber(), SOCSimpleRequest.PROMPT_PICK_RESOURCES, numGoldRes));
}
if (ga.getGameState() >= SOCGame.OVER) {
// exit debug mode, announce end of game
handler.processDebugCommand_freePlace(c, gaName, "0");
handler.sendGameState(ga, false, false);
}
} else {
if (initialDeny) {
final String pieceTypeFirst = ((player.getPieces().size() % 2) == 0) ? "settlement" : "road";
srv.messageToPlayer(c, gaName, "Place a " + pieceTypeFirst + " before placing that.");
} else {
srv.messageToPlayer(c, gaName, "Not a valid location to place that.");
}
}
}
use of soc.game.SOCPlayer in project JSettlers2 by jdmonin.
the class SOCRobotBrain method handlePLAYERELEMENTS.
/**
* Handle a PLAYERELEMENTS for this game.
* See {@link #handlePLAYERELEMENT(SOCPlayer, int, int, int, int)} for actions taken.
* @since 2.0.00
*/
private void handlePLAYERELEMENTS(SOCPlayerElements mes) {
final int pn = mes.getPlayerNumber();
final SOCPlayer pl = (pn != -1) ? game.getPlayer(pn) : null;
final int action = mes.getAction();
final int[] etypes = mes.getElementTypes(), amounts = mes.getAmounts();
for (int i = 0; i < etypes.length; ++i) handlePLAYERELEMENT(pl, pn, action, etypes[i], amounts[i]);
}
use of soc.game.SOCPlayer in project JSettlers2 by jdmonin.
the class SOCRobotBrain method setOurPlayerData.
/**
* When we join a game and sit down to begin play,
* find our player data using our nickname.
* Called from {@link SOCRobotClient} when the
* server sends a {@link SOCSitDown} message.
* Initializes our game and player data,
* {@link SOCRobotDM}, {@link SOCRobotNegotiator},
* strategy fields, {@link SOCPlayerTracker}s, etc.
*/
public void setOurPlayerData() {
ourPlayerData = game.getPlayer(client.getNickname());
ourPlayerTracker = new SOCPlayerTracker(ourPlayerData, this);
ourPlayerNumber = ourPlayerData.getPlayerNumber();
playerTrackers = new HashMap<Integer, SOCPlayerTracker>();
playerTrackers.put(new Integer(ourPlayerNumber), ourPlayerTracker);
for (int pn = 0; pn < game.maxPlayers; pn++) {
if ((pn != ourPlayerNumber) && !game.isSeatVacant(pn)) {
SOCPlayerTracker tracker = new SOCPlayerTracker(game.getPlayer(pn), this);
playerTrackers.put(new Integer(pn), tracker);
}
}
decisionMaker = new SOCRobotDM(this);
negotiator = new SOCRobotNegotiator(this);
openingBuildStrategy = new OpeningBuildStrategy(game, ourPlayerData);
monopolyStrategy = new MonopolyStrategy(game, ourPlayerData);
dummyCancelPlayerData = new SOCPlayer(-2, game);
// Verify expected face (fast or smart robot)
int faceId;
switch(getRobotParameters().getStrategyType()) {
case SOCRobotDM.SMART_STRATEGY:
// smarter robot face
faceId = -1;
break;
default:
// default robot face
faceId = 0;
}
if (ourPlayerData.getFaceId() != faceId) {
ourPlayerData.setFaceId(faceId);
// robotclient will handle sending it to server
}
}
use of soc.game.SOCPlayer in project JSettlers2 by jdmonin.
the class SOCRobotBrain method playKnightCardIfShould.
/**
* If we haven't played a dev card yet this turn, and we have a knight, and we can get
* largest army, play the knight. Must be our turn and gameState {@code PLAY1}.
* {@link SOCPlayer#hasPlayedDevCard() ourPlayerData.hasPlayedDevCard()} must be false.
*<P>
* In scenario {@code _SC_PIRI} (which has no robber and no largest army), play one
* whenever we have it, someone else has resources, and we can convert a ship to a warship.
*<P>
* If we call {@link #playKnightCard()}, it sets the flags
* {@code expectPLACING_ROBBER} and {@code waitingForGameState}.
*
* @see #rollOrPlayKnightOrExpectDice()
* @since 2.0.00
*/
private void playKnightCardIfShould() {
final boolean canGrowArmy;
if (game.isGameOptionSet(SOCGameOption.K_SC_PIRI)) {
// Convert ship to warship:
// Play whenever we have one and someone else has resources
boolean anyOpponentHasRsrcs = false;
for (int pn = 0; pn < game.maxPlayers; ++pn) {
if ((pn == ourPlayerNumber) || game.isSeatVacant(pn))
continue;
if (game.getPlayer(pn).getResources().getTotal() > 0) {
anyOpponentHasRsrcs = true;
break;
}
}
canGrowArmy = anyOpponentHasRsrcs;
} else {
final SOCPlayer laPlayer = game.getPlayerWithLargestArmy();
if ((laPlayer == null) || (laPlayer.getPlayerNumber() != ourPlayerNumber)) {
final int larmySize;
if (laPlayer == null)
larmySize = 3;
else
larmySize = laPlayer.getNumKnights() + 1;
canGrowArmy = ((ourPlayerData.getNumKnights() + ourPlayerData.getInventory().getAmount(SOCDevCardConstants.KNIGHT)) >= larmySize);
} else {
// we already have largest army
canGrowArmy = false;
// TODO Should we defend it if another player is close to taking it from us?
}
}
if (canGrowArmy && // has an old KNIGHT devcard, etc;
game.canPlayKnight(ourPlayerNumber) && // for _SC_PIRI, also checks if # of warships ships less than # of ships
(rejectedPlayDevCardType != SOCDevCardConstants.KNIGHT)) {
/**
* play a knight card
* (or, in scenario _SC_PIRI, a Convert to Warship card)
*/
// sets expectPLACING_ROBBER, waitingForGameState
playKnightCard();
}
}
Aggregations