use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.
the class TestResourceSet method removeTwoResources_doesNotThrowException.
@Test
public void removeTwoResources_doesNotThrowException() {
SOCResourceSet rs = onePerType();
rs.subtract(2, SOCResourceConstants.CLAY);
assertEquals(3, rs.getTotal());
assertEquals(0, rs.getAmount(SOCResourceConstants.CLAY));
assertEquals(-1, rs.getAmount(SOCResourceConstants.UNKNOWN));
}
use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.
the class TestResourceSet method onePerType_hasOnePerType.
@Test
public void onePerType_hasOnePerType() {
SOCResourceSet rs = onePerType();
assertTrue(rs.contains(SOCResourceConstants.CLAY));
assertTrue(rs.contains(SOCResourceConstants.WHEAT));
assertTrue(rs.contains(SOCResourceConstants.WOOD));
assertTrue(rs.contains(SOCResourceConstants.ORE));
assertTrue(rs.contains(SOCResourceConstants.SHEEP));
}
use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.
the class TestResourceSet method copyConstructor_ConstructsACopy.
@Test
public void copyConstructor_ConstructsACopy() {
SOCResourceSet all = onePerType();
SOCResourceSet copy = new SOCResourceSet(all);
assertEquals(all, copy);
}
use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.
the class SOCDisplaylessPlayerClient method handlePLAYERELEMENT_numRsrc.
/**
* Update a player's amount of a resource, for {@link #handlePLAYERELEMENT(SOCGame, SOCPlayer, int, int, int, int)}.
*<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.
*</ul>
*<P>
* To avoid code duplication, also called from
* {@link SOCPlayerClient.MessageTreater#handlePLAYERELEMENT(SOCPlayerElement)}
* and {@link soc.robot.SOCRobotBrain#run()}.
*<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 amount The new value to set, or the delta to gain/lose
*/
public static void handlePLAYERELEMENT_numRsrc(final SOCPlayer pl, final int action, final int rtype, final int amount) {
switch(action) {
case SOCPlayerElement.SET:
pl.getResources().setAmount(amount, rtype);
break;
case SOCPlayerElement.GAIN:
pl.getResources().add(amount, rtype);
break;
case SOCPlayerElement.LOSE:
if (rtype != SOCResourceConstants.UNKNOWN) {
int playerAmt = pl.getResources().getAmount(rtype);
if (playerAmt >= amount) {
pl.getResources().subtract(amount, rtype);
} else {
pl.getResources().subtract(amount - playerAmt, SOCResourceConstants.UNKNOWN);
pl.getResources().setAmount(0, rtype);
}
} else {
SOCResourceSet rs = pl.getResources();
/**
* first convert player's known resources to unknown resources,
* then remove mes's unknown resources from player
*/
rs.convertToUnknown();
pl.getResources().subtract(amount, SOCResourceConstants.UNKNOWN);
}
break;
}
}
use of soc.game.SOCResourceSet in project JSettlers2 by jdmonin.
the class SOCDisplaylessPlayerClient method handleDICERESULTRESOURCES.
/**
* Handle all players' dice roll result resources: static version to share with SOCPlayerClient.
* Game players gain resources.
* @param mes Message data
* @param ga Game to update
* @param nickname Our client player's nickname, needed for element data update
* @param skipResourceCount If true, ignore the resource part of the message
* because caller will handle that separately.
* @since 2.0.00
*/
public static final void handleDICERESULTRESOURCES(final SOCDiceResultResources mes, final SOCGame ga, final String nickname, final boolean skipResourceCount) {
final int n = mes.playerNum.size();
for (// current index reading from playerNum and playerRsrc
int p = 0; // current index reading from playerNum and playerRsrc
p < n; // current index reading from playerNum and playerRsrc
++p) {
final SOCResourceSet rs = mes.playerRsrc.get(p);
final int pn = mes.playerNum.get(p);
final SOCPlayer pl = ga.getPlayer(pn);
pl.getResources().add(rs);
if (!skipResourceCount)
handlePLAYERELEMENT_simple(ga, pl, pn, SOCPlayerElement.SET, SOCPlayerElement.RESOURCE_COUNT, mes.playerResTotal.get(p), nickname);
}
}
Aggregations