final Player player = game.getPlayer(connId);
int[] entityIds = (int[]) packet.getObject(0);
Vector<Player> declared = null;
Player other = null;
Enumeration<EntityAction> pending = null;
UnloadStrandedAction action = null;
Entity entity = null;
// Is this the right phase?
if (game.getPhase() != IGame.Phase.PHASE_MOVEMENT) {
System.err.println("error: server got unload stranded packet in wrong phase");
return;
}
// Are we in an "unload stranded entities" turn?
if (game.getTurn() instanceof GameTurn.UnloadStrandedTurn) {
turn = (GameTurn.UnloadStrandedTurn) game.getTurn();
} else {
System.err.println("error: server got unload stranded packet out of sequence");
StringBuffer message = new StringBuffer();
message.append(player.getName()).append(" should not be sending 'unload stranded entity' packets at this time.");
sendServerChat(message.toString());
return;
}
// Can this player act right now?
if (!turn.isValid(connId, game)) {
System.err.println("error: server got unload stranded packet from invalid player");
StringBuffer message = new StringBuffer();
message.append(player.getName()).append(" should not be sending 'unload stranded entity' packets.");
sendServerChat(message.toString());
return;
}
// Did the player already send an 'unload' request?
// N.B. we're also building the list of players who
// have declared their "unload stranded" actions.
declared = new Vector<Player>();
pending = game.getActions();
while (pending.hasMoreElements()) {
action = (UnloadStrandedAction) pending.nextElement();
if (action.getPlayerId() == connId) {
System.err.println("error: server got multiple unload stranded packets from player");
StringBuffer message = new StringBuffer();
message.append(player.getName()).append(" should not send multiple 'unload stranded entity' packets.");
sendServerChat(message.toString());
return;
}
// This player is not from the current connection.
// Record this player to determine if this turn is done.
other = game.getPlayer(action.getPlayerId());
if (!declared.contains(other)) {
declared.addElement(other);
}
} // Handle the next "unload stranded" action.
// Make sure the player selected at least *one* valid entity ID.
boolean foundValid = false;
for (int index = 0; (null != entityIds) && (index < entityIds.length); index++) {
entity = game.getEntity(entityIds[index]);
if (!game.getTurn().isValid(connId, entity, game)) {
System.err.println("error: server got unload stranded packet for invalid entity");
StringBuffer message = new StringBuffer();
message.append(player.getName()).append(" can not unload stranded entity ");
if (null == entity) {
message.append('#').append(entityIds[index]);
} else {
message.append(entity.getDisplayName());
}
message.append(" at this time.");
sendServerChat(message.toString());
} else {
foundValid = true;
game.addAction(new UnloadStrandedAction(connId, entityIds[index]));
}
}
// Did the player choose not to unload any valid stranded entity?
if (!foundValid) {
game.addAction(new UnloadStrandedAction(connId, Entity.NONE));
}
// Either way, the connection's player has now declared.
declared.addElement(player);
// Are all players who are unloading entities done? Walk
// through the turn's stranded entities, and look to see
// if their player has finished their turn.
entityIds = turn.getEntityIds();
for (int entityId : entityIds) {
entity = game.getEntity(entityId);
other = entity.getOwner();
if (!declared.contains(other)) {
// At least one player still needs to declare.
return;
}
}
// All players have declared whether they're unloading stranded units.
// Walk the list of pending actions and unload the entities.
pending = game.getActions();
while (pending.hasMoreElements()) {
action = (UnloadStrandedAction) pending.nextElement();
// Some players don't want to unload any stranded units.
if (Entity.NONE != action.getEntityId()) {
entity = game.getEntity(action.getEntityId());
if (null == entity) {
// After all this, we couldn't find the entity!!!
System.err.print("error: server could not find stranded entity #");
System.err.print(action.getEntityId());
System.err.println(" to unload!!!");
} else {
// Unload the entity. Get the unit's transporter.
Entity transporter = game.getEntity(entity.getTransportId());
unloadUnit(transporter, entity, transporter.getPosition(), transporter.getFacing(), transporter.getElevation());