Implemented combat system. Implemented nation/color selection in pregame.

This commit is contained in:
smelenchuk 2004-04-27 01:45:14 +00:00
parent 5a4423df04
commit 8866aa3917
14 changed files with 557 additions and 64 deletions

View File

@ -126,7 +126,7 @@ public final class InGameController {
switch (move) {
case Unit.MOVE: reallyMove(unit, direction); break;
//case Unit.ATTACK: attack(unit, direction); break;
case Unit.ATTACK: attack(unit, direction); break;
case Unit.DISEMBARK: disembark(unit, direction); break;
case Unit.EMBARK: embark(unit, direction); break;
case Unit.MOVE_HIGH_SEAS: moveHighSeas(unit, direction); break;
@ -171,7 +171,26 @@ public final class InGameController {
Element reply = client.ask(moveElement);
freeColClient.getInGameInputHandler().handle(client.getConnection(), reply);
}
/**
* Performs an attack in a specified direction. Note that the server
* handles the attack calculations here.
*
* @param unit The unit to perform the attack.
* @param direction The direction in which to attack.
*/
private void attack(Unit unit, int direction) {
Client client = freeColClient.getClient();
// Inform the server:
Element attackElement = Message.createNewRootElement("attack");
attackElement.setAttribute("unit", unit.getID());
attackElement.setAttribute("direction", Integer.toString(direction));
//client.send(moveElement);
Element reply = client.ask(attackElement);
freeColClient.getInGameInputHandler().handle(client.getConnection(), reply);
}
/**
* Disembarks the specified unit in a specified direction.

View File

@ -72,6 +72,10 @@ public final class InGameInputHandler implements MessageHandler {
reply = remove(element);
} else if (type.equals("opponentMove")) {
reply = opponentMove(element);
} else if (type.equals("opponentAttack")) {
reply = opponentAttack(element);
} else if (type.equals("attackResult")) {
reply = attackResult(element);
} else if (type.equals("setCurrentPlayer")) {
reply = setCurrentPlayer(element);
} else if (type.equals("newTurn")) {
@ -173,7 +177,61 @@ public final class InGameInputHandler implements MessageHandler {
return null;
}
/**
* Handles an "opponentAttack"-message.
*
* @param opponentAttackElement The element (root element in a DOM-parsed XML tree) that
* holds all the information.
*/
private Element opponentAttack(Element opponentAttackElement) {
Game game = freeColClient.getGame();
Map map = game.getMap();
int direction = Integer.parseInt(opponentAttackElement.getAttribute("direction"));
int result = Integer.parseInt(opponentAttackElement.getAttribute("result"));
Unit unit = (Unit) game.getFreeColGameObject(opponentAttackElement.getAttribute("unit"));
Unit defender = map.getNeighbourOrNull(direction, unit.getTile()).getDefendingUnit(unit);
Player player = unit.getOwner();
if (result == Unit.ATTACKER_LOSS) {
unit.loseAttack();
} else {
unit.winAttack(defender);
}
return null;
}
/**
* Handles an "attackResult"-message.
*
* @param attackResultElement The element (root element in a DOM-parsed XML tree) that
* holds all the information.
*/
private Element attackResult(Element attackResultElement) {
Game game = freeColClient.getGame();
Map map = game.getMap();
int direction = Integer.parseInt(attackResultElement.getAttribute("direction"));
int result = Integer.parseInt(attackResultElement.getAttribute("result"));
Unit unit = (Unit) game.getFreeColGameObject(attackResultElement.getAttribute("unit"));
Unit defender = map.getNeighbourOrNull(direction, unit.getTile()).getDefendingUnit(unit);
if (result == Unit.ATTACKER_LOSS) {
unit.loseAttack();
} else {
unit.winAttack(defender);
}
if (attackResultElement.hasAttribute("update")) {
this.update((Element) attackResultElement.getElementsByTagName("update").item(0));
}
return null;
}
/**
* Handles a "setCurrentPlayer"-message.
*

View File

@ -65,6 +65,35 @@ public final class PreGameController {
freeColClient.getClient().send(readyElement);
}
/**
* Sets this client's player's nation.
* @param nation Which nation this player wishes to set.
*/
public void setNation(String nation) {
// Make the change:
freeColClient.getMyPlayer().setNation(nation);
// Inform the server:
Element nationElement = Message.createNewRootElement("setNation");
nationElement.setAttribute("value", nation);
freeColClient.getClient().send(nationElement);
}
/**
* Sets this client's player's color.
* @param color Which color this player wishes to set.
*/
public void setColor(String color) {
// Make the change:
freeColClient.getMyPlayer().setColor(color);
// Inform the server:
Element colorElement = Message.createNewRootElement("setColor");
colorElement.setAttribute("value", color);
freeColClient.getClient().send(colorElement);
}
/**
* Requests the game to be started. This will only be successful

View File

@ -167,20 +167,13 @@ public final class PreGameInputHandler implements MessageHandler {
* @param element The element (root element in a DOM-parsed XML tree) that
* holds all the information.
*/
private Element updateNation(Element element) {
/*if (!element.hasAttribute("username") || !element.hasAttribute("nation")) {
throw new FreeColException("Invalid message format.");
}
private Element updateNation(Element element) {
Game game = freeColClient.getGame();
String username = element.getAttribute("username");
String nation = Player.nationToString(Integer.parseInt(element.getAttribute("nation")));
Player player = (Player) game.getFreeColGameObject(element.getAttribute("player"));
String nation = element.getAttribute("nation");
try {
canvas.getMultiplayerPanel().setNation(username, nation);
} catch (FreeColException e) {
e.printStackTrace();
}
*/
player.setNation(nation);
return null;
}
@ -192,19 +185,12 @@ public final class PreGameInputHandler implements MessageHandler {
* holds all the information.
*/
private Element updateColor(Element element) {
/*if (!element.hasAttribute("username") || !element.hasAttribute("color")) {
throw new FreeColException("Invalid message format.");
}
String username = element.getAttribute("username");
Color color = new Color(Integer.parseInt(element.getAttribute("color")));
Game game = freeColClient.getGame();
try {
canvas.getMultiplayerPanel().setColor(username, color);
} catch (FreeColException e) {
e.printStackTrace();
}
*/
Player player = (Player) game.getFreeColGameObject(element.getAttribute("player"));
String color = element.getAttribute("color");
player.setColor(color);
return null;
}

View File

@ -241,34 +241,10 @@ public final class StartGamePanel extends JPanel implements ActionListener {
case MAPSIZE:
break;
case NATION:
/*if ((player != null) && (client != null)) {
try {
int n = Player.stringToNation(((String)nation.getSelectedItem()).toLowerCase());
player.setNation(n);
client.setNation(n);
}
catch (FreeColException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}*/
freeColClient.getPreGameController().setNation(((String)nation.getSelectedItem()).toLowerCase());
break;
case COLOR:
/*if ((player != null) && (client != null)) {
try {
Color c = Player.stringToColor(((String)color.getSelectedItem()).toLowerCase());
player.setColor(c);
client.setColor((String)color.getSelectedItem());
}
catch (FreeColException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}*/
freeColClient.getPreGameController().setColor(((String)color.getSelectedItem()).toLowerCase());
break;
case READY:
freeColClient.getPreGameController().setReady(readyBox.isSelected());

View File

@ -95,7 +95,36 @@ public final class Colony extends Settlement implements Location {
}
/**
* Sets the owner of this <code>Colony</code>, including all units within.
*
* @param owner The <code>Player</code> that shall own this <code>Settlement</code>.
* @see #getOwner
*/
public void setOwner(Player owner) {
this.owner = owner;
Iterator i = getWorkLocationIterator();
ArrayList units = new ArrayList();
while (i.hasNext()) {
WorkLocation w = (WorkLocation) i.next();
if (w instanceof Building) {
Iterator unitIterator = w.getUnitIterator();
while (unitIterator.hasNext()) {
Unit unit = (Unit)unitIterator.next();
units.add(unit);
}
} else if (w instanceof ColonyTile) {
Unit unit = ((ColonyTile)w).getUnit();
if (unit != null) units.add(unit);
}
}
Iterator unitIterator = units.iterator();
while (unitIterator.hasNext()) {
((Unit)unitIterator.next()).setOwner(owner);
}
}
/**
@ -337,7 +366,48 @@ public final class Colony extends Settlement implements Location {
public boolean canAdd(Locatable locatable) {
throw new UnsupportedOperationException();
}
/**
* Gets the <code>Unit</code> that is currently defending this <code>Colony</code>.
* @param attacker The target that would be attacking this colony.
* @return The <code>Unit</code> that has been choosen to defend this colony.
*/
public Unit getDefendingUnit(Unit attacker) {
Iterator i = getWorkLocationIterator();
ArrayList units = new ArrayList();
while (i.hasNext()) {
WorkLocation w = (WorkLocation) i.next();
if (w instanceof Building) {
Iterator unitIterator = w.getUnitIterator();
while (unitIterator.hasNext()) {
Unit unit = (Unit)unitIterator.next();
units.add(unit);
}
} else if (w instanceof ColonyTile) {
Unit unit = ((ColonyTile)w).getUnit();
if (unit != null) units.add(unit);
}
}
Iterator unitIterator = units.iterator();
Unit defender = null;
if (unitIterator.hasNext()) {
defender = (Unit) unitIterator.next();
} else {
return null;
}
while (unitIterator.hasNext()) {
Unit nextUnit = (Unit) unitIterator.next();
if (nextUnit.getDefensePower(attacker) > defender.getDefensePower(attacker)) {
defender = nextUnit;
}
}
return defender;
}
/**
* Gets a string representation of the Colony. Currently this method

View File

@ -1,6 +1,7 @@
package net.sf.freecol.common.model;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Iterator;
import java.util.logging.Logger;
@ -185,7 +186,9 @@ public class ColonyTile extends FreeColGameObject implements WorkLocation {
public Iterator getUnitIterator() {
throw new UnsupportedOperationException();
ArrayList units = new ArrayList();
units.add(getUnit());
return units.iterator();
}

View File

@ -305,6 +305,21 @@ public class Player extends FreeColGameObject {
nation = n;
}
/**
* Sets the nation for this player.
* @param n The new nation for this player.
*/
public void setNation(String n) {
if (n.toLowerCase().equals("dutch")) {
setNation(DUTCH);
} else if (n.toLowerCase().equals("english")) {
setNation(ENGLISH);
} else if (n.toLowerCase().equals("french")) {
setNation(FRENCH);
} else if (n.toLowerCase().equals("spanish")) {
setNation(SPANISH);
}
}
/**
* Sets the color for this player.
@ -314,6 +329,35 @@ public class Player extends FreeColGameObject {
color = c;
}
/**
* Sets the color for this player.
* @param c The new color for this player.
*/
public void setColor(String c) {
if (c.toLowerCase().equals("black")) {
setColor(Color.BLACK);
} else if (c.toLowerCase().equals("blue")) {
setColor(Color.BLUE);
} else if (c.toLowerCase().equals("cyan")) {
setColor(Color.CYAN);
} else if (c.toLowerCase().equals("gray")) {
setColor(Color.GRAY);
} else if (c.toLowerCase().equals("green")) {
setColor(Color.GREEN);
} else if (c.toLowerCase().equals("magenta")) {
setColor(Color.MAGENTA);
} else if (c.toLowerCase().equals("orange")) {
setColor(Color.ORANGE);
} else if (c.toLowerCase().equals("pink")) {
setColor(Color.PINK);
} else if (c.toLowerCase().equals("red")) {
setColor(Color.RED);
} else if (c.toLowerCase().equals("white")) {
setColor(Color.WHITE);
} else if (c.toLowerCase().equals("yellow")) {
setColor(Color.YELLOW);
}
}
/**
* Checks if this <code>Player</code> is ready to start the game.

View File

@ -30,7 +30,7 @@ abstract public class Settlement extends FreeColGameObject implements Location {
this.tile = tile;
setOwner(owner);
this.owner = owner;
}

View File

@ -4,6 +4,7 @@
package net.sf.freecol.common.model;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Iterator;
import java.util.logging.Logger;
@ -143,10 +144,10 @@ public final class Tile extends FreeColGameObject implements Location {
public Unit getDefendingUnit(Unit attacker) {
Iterator unitIterator = getUnitIterator();
Unit defender;
Unit defender = null;
if (unitIterator.hasNext()) {
defender = (Unit) unitIterator.next();
} else {
} else if (settlement == null) {
return null;
}
@ -157,7 +158,17 @@ public final class Tile extends FreeColGameObject implements Location {
defender = nextUnit;
}
}
if (settlement != null) {
Unit settlementDefender = null;
if (settlement instanceof Colony) {
settlementDefender = ((Colony)settlement).getDefendingUnit(attacker);
}
if ((settlementDefender != null) && ((defender == null) || (settlementDefender.getDefensePower(attacker) > defender.getDefensePower(attacker)))) {
defender = settlementDefender;
}
}
return defender;
}
@ -553,6 +564,9 @@ public final class Tile extends FreeColGameObject implements Location {
* @return The amount of units at this <code>Location</code>.
*/
public int getUnitCount() {
if (settlement != null) {
return settlement.getUnitCount() + unitContainer.getUnitCount();
}
return unitContainer.getUnitCount();
}
@ -568,6 +582,14 @@ public final class Tile extends FreeColGameObject implements Location {
return unitContainer.getUnitIterator();
}
/**
* Gets a clone of the UnitContainer's <code>units</code> array.
*
* @return The clone.
*/
public ArrayList getUnitsClone() {
return unitContainer.getUnitsClone();
}
/**
* Checks wether or not the specified locatable may be added to this
@ -604,7 +626,7 @@ public final class Tile extends FreeColGameObject implements Location {
{{3,2}, {0,0}, {0,0}, {0,0}, {0,3}, {0,4}, {2,1}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}, // Tundra
{{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}, // Arctic
{{4,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}}, // Ocean
{{4,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}} // High seas
{{4,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}} // High seas
};
int basepotential = potentialtable[type][goods][(forested ? 1 : 0)];

View File

@ -92,7 +92,8 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
DISEMBARK = 4,
ILLEGAL_MOVE = 5;
public static final int ATTACKER_LOSS = 0,
ATTACKER_WIN = 1;
private int type;
private boolean armed,
@ -747,9 +748,14 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
/**
* Sets the armed attribute of this unit.
* @param <i>true</i> if this unit should be armed and <i>false</i> otherwise.
* @param b <i>true</i> if this unit should be armed and <i>false</i> otherwise.
* @param isCombat Whether this is a result of combat.
*/
public void setArmed(boolean b) {
public void setArmed(boolean b, boolean isCombat) {
if (isCombat) {
armed = b; // No questions asked.
return;
}
if ((b) && (!armed)) {
if (getGoodsDumpLocation() != null) {
if (getGoodsDumpLocation().getGoodsCount(Goods.MUSKETS) < 50) {
@ -779,6 +785,13 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
}
}
/**
* Sets the armed attribute of this unit.
* @param b <i>true</i> if this unit should be armed and <i>false</i> otherwise.
*/
public void setArmed(boolean b) {
setArmed(b, false);
}
/**
* Checks if this <code>Unit</code> is currently armed.
@ -792,8 +805,13 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
/**
* Sets the mounted attribute of this unit.
* @param <i>true</i> if this unit should be mounted and <i>false</i> otherwise.
* @param isCombat Whether this is a result of combat.
*/
public void setMounted(boolean b) {
public void setMounted(boolean b, boolean isCombat) {
if (isCombat) {
mounted = b; // No questions asked.
return;
}
if ((b) && (!mounted)) {
if (getGoodsDumpLocation() != null) {
if (getGoodsDumpLocation().getGoodsCount(Goods.HORSES) < 50) return;
@ -816,6 +834,14 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
}
}
}
/**
* Sets the mounted attribute of this unit.
* @param b <i>true</i> if this unit should be mounted and <i>false</i> otherwise.
*/
public void setMounted(boolean b) {
setMounted(b, false);
}
/**
@ -948,6 +974,13 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
return owner;
}
/**
* Sets the owner of this Unit.
* @parm type The new owner of this Unit.
*/
public void setOwner(Player owner) {
this.owner = owner;
}
/**
* Gets the nation the unit is serving. One of {DUTCH , ENGLISH, FRENCH,
@ -968,6 +1001,13 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
return type;
}
/**
* Sets the type of the unit.
* @parm type The new type of the unit.
*/
public void setType(int type) {
this.type = type;
}
/**
* Checks if this unit is of a given type.
@ -1862,6 +1902,100 @@ public final class Unit extends FreeColGameObject implements Location, Locatable
}
return modified_power;
}
/**
* Carries out the winning of an attack.
* @param defender The target of the attack.
*/
public void winAttack(Unit defender) {
Tile newTile = defender.getTile();
movesLeft = 0;
if (defender.getType() == BRAVE) {
defender.dispose();
if (newTile.getSettlement() != null) {
if (newTile.getSettlement().getUnitCount() <= 0) {
//TODO: Burn the camp. Get treasure.
newTile.getSettlement().dispose();
setLocation(newTile);
}
} else {
Iterator unitIterator = newTile.getUnitIterator();
while (unitIterator.hasNext()) {
((Unit)unitIterator.next()).dispose();
}
}
} else if (isNaval()) {
//TODO: seizure of cargo and the like. For now just sink.
defender.dispose();
} else if (!(defender.isArmed()) && !(defender.getType() == ARTILLERY) && !(defender.getType() == DAMAGED_ARTILLERY)) {
if (defender.isMounted()) {
defender.dispose(); // Scouts die if they lose.
}
Colony targetcolony = null;
boolean captureColony = ((newTile.getSettlement() != null) && (newTile.getSettlement() instanceof Colony));
if (captureColony) {
targetcolony = (Colony)(newTile.getSettlement());
targetcolony.setOwner(getOwner()); // This also changes over all of the units...
setLocation(newTile);
}
// Colonists get captured if they lose.
Iterator unitIterator = newTile.getUnitsClone().iterator();
while (unitIterator.hasNext()) {
Unit target = (Unit)unitIterator.next();
target.setOwner(getOwner());
if (!captureColony) target.setLocation(getTile());
}
} else {
if (defender.isMounted()) {
defender.setMounted(false, true);
if (getType() == BRAVE) {
//TODO: don't always do this. Have some random chance.
setMounted(true, true);
}
} else if ((defender.getType() == ARTILLERY)) {
defender.setType(DAMAGED_ARTILLERY);
} else if ((defender.getType() == KINGS_REGULAR) || (defender.getType() == DAMAGED_ARTILLERY)) {
defender.dispose();
} else {
defender.setArmed(false, true);
if (getType() == BRAVE) {
//TODO: don't always do this. Have some random chance.
setArmed(true, true);
}
}
}
}
/**
* Carries out the loss of an attack.
*/
public void loseAttack() {
movesLeft = 0;
if (getType() == BRAVE) {
dispose();
return; // Do NOT try to go any further!
} else if (isNaval()) {
//TODO: evasion and the like. For now just sink.
dispose();
return; // Do NOT try to go any further!
} else if (!(isArmed()) && !(getType() == ARTILLERY) && !(getType() == DAMAGED_ARTILLERY)) {
dispose(); // Only scouts should ever reach this point. Nobody else should be able to attack.
return; // Do NOT try to go any further!
} else {
if (isMounted()) {
setMounted(false, true);
} else if (getType() == ARTILLERY) {
setType(DAMAGED_ARTILLERY);
} else if ((getType() == KINGS_REGULAR) || (getType() == DAMAGED_ARTILLERY)) {
dispose();
return; // Do NOT try to go any further!
} else {
setArmed(false, true);
}
}
return;
}
/**

View File

@ -141,6 +141,14 @@ public class UnitContainer extends FreeColGameObject {
return amount + getUnitCount();
}
/**
* Gets a clone of the <code>units</code> array.
*
* @return The clone.
*/
public ArrayList getUnitsClone() {
return (ArrayList)units.clone();
}
/**
* Gets an <code>Iterator</code> of every <code>Unit</code> in this

View File

@ -6,6 +6,7 @@ import java.net.Socket;
import java.util.Vector;
import java.util.logging.Logger;
import java.util.Iterator;
import java.util.Random;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@ -31,8 +32,8 @@ public final class InGameInputHandler implements MessageHandler {
private static Logger logger = Logger.getLogger(InGameInputHandler.class.getName());
private FreeColServer freeColServer;
public static Random attackCalculator;
/**
* The constructor to use.
@ -40,6 +41,7 @@ public final class InGameInputHandler implements MessageHandler {
*/
public InGameInputHandler(FreeColServer freeColServer) {
this.freeColServer = freeColServer;
attackCalculator = new Random();
}
@ -61,6 +63,8 @@ public final class InGameInputHandler implements MessageHandler {
if (freeColServer.getGame().getCurrentPlayer().equals(freeColServer.getPlayer(connection))) {
if (type.equals("move")) {
reply = move(connection, element);
} else if (type.equals("attack")) {
reply = attack(connection, element);
} else if (type.equals("embark")) {
reply = embark(connection, element);
} else if (type.equals("boardShip")) {
@ -173,6 +177,91 @@ public final class InGameInputHandler implements MessageHandler {
return reply;
}
/**
* Handles an "attack"-message from a client.
*
* @param connection The connection the message came from.
* @param moveElement The element containing the request.
* @exception IllegalArgumentException If the data format of the message is invalid.
* @exception IllegalStateException If the request is not accepted by the model.
*
*/
private Element attack(Connection connection, Element attackElement) {
Game game = freeColServer.getGame();
ServerPlayer player = freeColServer.getPlayer(connection);
Unit unit = (Unit) game.getFreeColGameObject(attackElement.getAttribute("unit"));
int direction = Integer.parseInt(attackElement.getAttribute("direction"));
Unit defender = null;
if (unit == null) {
throw new IllegalArgumentException("Could not find 'Unit' with specified ID: " + attackElement.getAttribute("unit"));
}
Tile oldTile = unit.getTile();
Tile newTile = game.getMap().getNeighbourOrNull(direction, unit.getTile());
if (newTile == null) {
throw new IllegalArgumentException("Could not find tile in direction " + direction + " from unit with ID " + attackElement.getAttribute("unit"));
}
defender = newTile.getDefendingUnit(unit);
if (defender == null) {
throw new IllegalStateException("Nothing to attack in direction " + direction + " from unit with ID " + attackElement.getAttribute("unit"));
}
int attack_power = unit.getOffensePower(defender);
int total_probability = attack_power + defender.getDefensePower(unit);
int result = Unit.ATTACKER_LOSS; // Assume this until otherwise calculated.
if ((attackCalculator.nextInt(total_probability)) <= attack_power) {
// Win.
result = Unit.ATTACKER_WIN;
unit.winAttack(defender);
} // Loss code is later, when unit is no longer needed.
Iterator enemyPlayerIterator = game.getPlayerIterator();
while (enemyPlayerIterator.hasNext()) {
ServerPlayer enemyPlayer = (ServerPlayer) enemyPlayerIterator.next();
if (player.equals(enemyPlayer)) {
continue;
}
Element opponentAttackElement = Message.createNewRootElement("opponentAttack");
opponentAttackElement.setAttribute("direction", Integer.toString(direction));
opponentAttackElement.setAttribute("result", Integer.toString(result));
opponentAttackElement.setAttribute("unit", unit.getID());
try {
enemyPlayer.getConnection().send(opponentAttackElement);
} catch (IOException e) {
logger.warning("Could not send message to: " + enemyPlayer.getName() + " with connection " + enemyPlayer.getConnection());
}
}
Element reply = Message.createNewRootElement("attackResult");
reply.setAttribute("direction", Integer.toString(direction));
reply.setAttribute("result", Integer.toString(result));
reply.setAttribute("unit", unit.getID());
if (unit.getTile().equals(newTile)) { // In other words, we moved...
Element update = reply.getOwnerDocument().createElement("update");
Vector surroundingTiles = game.getMap().getSurroundingTiles(unit.getTile(), unit.getLineOfSight());
for (int i=0; i<surroundingTiles.size(); i++) {
Tile t = (Tile) surroundingTiles.get(i);
player.setExplored(t);
update.appendChild(t.toXMLElement(player, update.getOwnerDocument()));
}
reply.appendChild(update);
}
// This is down here do it doesn't interfere with other unit code.
if (result == Unit.ATTACKER_LOSS) {
unit.loseAttack();
}
return reply;
}
/**
* Handles an "embark"-message from a client.

View File

@ -60,6 +60,10 @@ public final class PreGameInputHandler implements MessageHandler {
if (element != null) {
if (type.equals("ready")) {
reply = ready(connection, element);
} else if (type.equals("setNation")) {
reply = nation(connection, element);
} else if (type.equals("setColor")) {
reply = color(connection, element);
} else if (type.equals("requestLaunch")) {
reply = requestLaunch(connection, element);
} else if (type.equals("chat")) {
@ -97,6 +101,57 @@ public final class PreGameInputHandler implements MessageHandler {
return null;
}
/**
* Handles a "nation"-message from a client.
*
* @param connection The connection the message came from.
* @param element The element containing the request.
*/
private Element nation(Connection connection, Element element) {
ServerPlayer player = freeColServer.getPlayer(connection);
if (player != null) {
String nation = element.getAttribute("value");
player.setNation(nation);
Element updateNation = Message.createNewRootElement("updateNation");
updateNation.setAttribute("player", player.getID());
updateNation.setAttribute("value", nation);
freeColServer.getServer().sendToAll(updateNation, player.getConnection());
} else {
logger.warning("Nation from unknown connection.");
}
return null;
}
/**
* Handles a "color"-message from a client.
*
* @param connection The connection the message came from.
* @param element The element containing the request.
*/
private Element color(Connection connection, Element element) {
ServerPlayer player = freeColServer.getPlayer(connection);
if (player != null) {
String color = element.getAttribute("value");
player.setColor(color);
Element updateColor = Message.createNewRootElement("updateColor");
updateColor.setAttribute("player", player.getID());
updateColor.setAttribute("value", color);
freeColServer.getServer().sendToAll(updateColor, player.getConnection());
} else {
logger.warning("Color from unknown connection.");
}
return null;
}
/**
* Handles a "requestLaunch"-message from a client.