Added new features:

- Breeding of horses in colonies.
- If the amount of goods exceeds the capacity of the warehouse, it is now thrown away.
This commit is contained in:
Stian Grenborgen 2004-06-11 22:57:03 +00:00
parent a6e0ec05a4
commit b5148ca54c
13 changed files with 223 additions and 55 deletions

View File

@ -217,6 +217,7 @@ public final class Canvas extends JLayeredPane {
startGamePanel.initialize(game, player);
startGamePanel.setLocation(getWidth() / 2 - startGamePanel.getWidth() / 2, getHeight() / 2 - startGamePanel.getHeight() / 2);
add(startGamePanel);
startGamePanel.requestFocus();
} else {
logger.warning("Tried to open 'StartGamePanel' without having 'game' and/or 'player' set.");
}
@ -242,6 +243,7 @@ public final class Canvas extends JLayeredPane {
closeMenus();
newPanel.setLocation(getWidth() / 2 - newPanel.getWidth() / 2, getHeight() / 2 - newPanel.getHeight() / 2);
add(newPanel);
newPanel.requestFocus();
}
@ -341,6 +343,7 @@ public final class Canvas extends JLayeredPane {
mapControls.removeFromComponent(this);
setEnabled(false);
add(europePanel);
europePanel.requestFocus();
}
}
@ -361,6 +364,8 @@ public final class Canvas extends JLayeredPane {
mapControls.removeFromComponent(this);
setEnabled(false);
add(colonyPanel);
colonyPanel.requestFocus();
}
@ -531,6 +536,7 @@ public final class Canvas extends JLayeredPane {
errorPanel.setLocation(getWidth() / 2 - errorPanel.getWidth() / 2, getHeight() / 2 - errorPanel.getHeight() / 2);
setEnabled(false);
add(errorPanel, JLayeredPane.MODAL_LAYER);
errorPanel.requestFocus();
}
@ -617,6 +623,7 @@ public final class Canvas extends JLayeredPane {
public void showMainPanel() {
mainPanel.setLocation(getWidth() / 2 - mainPanel.getWidth() / 2, getHeight() / 2 - mainPanel.getHeight() / 2);
add(mainPanel, new Integer(-100));
mainPanel.requestFocus();
}
@ -679,6 +686,7 @@ public final class Canvas extends JLayeredPane {
public boolean confirmQuitDialog() {
quitDialog.setLocation(getWidth() / 2 - quitDialog.getWidth() / 2, getHeight() / 2 - quitDialog.getHeight() / 2);
add(quitDialog, JLayeredPane.POPUP_LAYER);
quitDialog.requestFocus();
return quitDialog.getResponseBoolean();
}

View File

@ -79,7 +79,7 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
private Game game;
private UnitLabel selectedUnit;
private JButton exitButton = new JButton("Close");
@ -134,7 +134,6 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
buildingBox = new BuildingBox(this);
JButton exitButton = new JButton("Close");
JScrollPane outsideColonyScroll = new JScrollPane(outsideColonyPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
inPortScroll = new JScrollPane(inPortPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
cargoScroll = new JScrollPane(cargoPanel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
@ -222,6 +221,10 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
public void requestFocus() {
exitButton.requestFocus();
}
/**
* Refreshes this panel.
@ -732,7 +735,7 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
public void initialize() {
warehousePanel.removeAll();
Iterator goodsIterator = colony.getGoodsIterator();
Iterator goodsIterator = colony.getCompactGoodsIterator();
while (goodsIterator.hasNext()) {
Goods goods = (Goods) goodsIterator.next();
@ -742,8 +745,8 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
warehousePanel.add(goodsLabel, false);
}
warehousePanel.revalidate();
warehousePanel.revalidate();
}
}
@ -786,10 +789,18 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
return comp;
}
} else if (comp instanceof GoodsLabel) {
comp.getParent().remove(comp);
Goods g = ((GoodsLabel)comp).getGoods();
// Transfer a maximum of 100 goods at a time:
if (g.getAmount() > 100) {
g.setAmount(g.getAmount() - 100);
g = new Goods(game, g.getLocation(), g.getType(), 100);
g.setAmount(100);
} else {
comp.getParent().remove(comp);
}
((GoodsLabel) comp).setSmall(false);
logger.info("Attempting to load cargo.");
inGameController.loadCargo(g, selectedUnit.getUnit());
colonyPanel.getWarehousePanel().revalidate();
//colonyPanel.getCargoPanel().revalidate();
@ -799,7 +810,6 @@ public final class ColonyPanel extends JLayeredPane implements ActionListener {
selectedUnit = null;
setSelectedUnit(t);
//refresh();
return comp;
} else {
logger.warning("An invalid component got dropped on this CargoPanel.");

View File

@ -56,6 +56,12 @@ public final class ErrorPanel extends JPanel implements ActionListener {
add(errorButton);
}
public void requestFocus() {
errorButton.requestFocus();
}
/**
* Adapts the appearance of this ErrorPanel to the given error message.
* @param message The error message to display in this error panel.

View File

@ -102,6 +102,7 @@ public final class EuropePanel extends JLayeredPane implements ActionListener {
private Game game;
private UnitLabel selectedUnit;
private JButton exitButton = new JButton("Close");
@ -157,8 +158,7 @@ public final class EuropePanel extends JLayeredPane implements ActionListener {
cargoLabel = new JLabel("<html><strike>Cargo</strike></html>");
goldLabel = new JLabel("Gold: 0");
JButton exitButton = new JButton("Close"),
recruitButton = new JButton("Recruit"),
JButton recruitButton = new JButton("Recruit"),
purchaseButton = new JButton("Purchase"),
trainButton = new JButton("Train");
JScrollPane toAmericaScroll = new JScrollPane(toAmericaPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
@ -246,7 +246,9 @@ public final class EuropePanel extends JLayeredPane implements ActionListener {
}
public void requestFocus() {
exitButton.requestFocus();
}
@ -969,6 +971,7 @@ public final class EuropePanel extends JLayeredPane implements ActionListener {
*/
public MarketPanel(EuropePanel europePanel) {
this.europePanel = europePanel;
setLayout(new GridLayout(2,8));
}

View File

@ -48,7 +48,9 @@ public final class MainPanel extends JPanel implements ActionListener {
QUIT = 2;
private final Canvas parent;
private JButton newButton;
/**
* The constructor that will add the items to this panel.
* @param parent The parent of this panel.
@ -58,18 +60,11 @@ public final class MainPanel extends JPanel implements ActionListener {
this.parent = parent;
JButton newButton = new JButton("New"),
openButton = new JButton("Open"),
JButton openButton = new JButton("Open"),
quitButton = new JButton("Quit");
/*
newButton.setSize(80, 20);
openButton.setSize(80, 20);
quitButton.setSize(80, 20);
newButton.setLocation(10, 10);
openButton.setLocation(10, 35);
quitButton.setLocation(10, 60);
*/
newButton = new JButton("New");
newButton.setActionCommand(String.valueOf(NEW));
openButton.setActionCommand(String.valueOf(OPEN));
quitButton.setActionCommand(String.valueOf(QUIT));
@ -106,6 +101,11 @@ public final class MainPanel extends JPanel implements ActionListener {
setSize(getPreferredSize());
}
public void requestFocus() {
newButton.requestFocus();
}
/**
* Sets whether or not this component is enabled. It also does this for
* its children.

View File

@ -173,16 +173,18 @@ public final class MapControls {
//
// Remove the GUI Objects from the container
//
container.remove(infoPanel);
container.remove(miniMap);
container.remove(miniMapZoomOutButton);
container.remove(miniMapZoomInButton);
for(int i=0; i<NUMBER_OF_BUTTONS; i++) {
container.remove(unitButton[i]);
if (container != null) {
container.remove(infoPanel);
container.remove(miniMap);
container.remove(miniMapZoomOutButton);
container.remove(miniMapZoomInButton);
for(int i=0; i<NUMBER_OF_BUTTONS; i++) {
container.remove(unitButton[i]);
}
container = null;
}
container = null;
}
/**

View File

@ -57,7 +57,9 @@ public final class NewPanel extends JPanel implements ActionListener {
private final Canvas parent;
private final ConnectController connectController;
private JButton ok = new JButton("OK");
/**
* The constructor that will add the items to this panel.
* @param parent The parent of this panel.
@ -66,8 +68,7 @@ public final class NewPanel extends JPanel implements ActionListener {
this.parent = parent;
this.connectController = connectController;
JButton ok = new JButton("OK"),
cancel = new JButton("Cancel");
JButton cancel = new JButton("Cancel");
ButtonGroup group = new ButtonGroup();
JLabel nameLabel = new JLabel("Name");
@ -157,6 +158,12 @@ public final class NewPanel extends JPanel implements ActionListener {
setSize(260, 235);
}
public void requestFocus() {
ok.requestFocus();
}
/**
* Sets whether or not this component is enabled. It also does this for
* its children.

View File

@ -38,8 +38,12 @@ public final class QuitDialog extends FreeColDialog implements ActionListener {
private static final Logger logger = Logger.getLogger(QuitDialog.class.getName());
private static final int OK = 0,
CANCEL = 1;
private final Canvas parent;
private JButton ok = new JButton("Yes");
/**
* The constructor that will add the items to this panel.
@ -47,19 +51,18 @@ public final class QuitDialog extends FreeColDialog implements ActionListener {
*/
public QuitDialog(Canvas parent) {
this.parent = parent;
JButton ok = new JButton("Yes"),
cancel = new JButton("No");
JButton cancel = new JButton("No");
JLabel qLabel = new JLabel("Are you sure you want to Quit?");
qLabel.setSize(200, 20);
ok.setSize(60, 20);
cancel.setSize(60, 20);
qLabel.setLocation(10, 10);
ok.setLocation(30, 40);
cancel.setLocation(130, 40);
setLayout(null);
ok.setActionCommand(String.valueOf(OK));
@ -82,6 +85,11 @@ public final class QuitDialog extends FreeColDialog implements ActionListener {
setSize(220, 70);
}
public void requestFocus() {
ok.requestFocus();
}
/**
* This function analyses an event and calls the right methods to take
* care of the user's requests.

View File

@ -186,8 +186,11 @@ public final class StartGamePanel extends JPanel implements ActionListener {
}
public void requestFocus() {
start.requestFocus();
}
public void initialize(Game game, Player thisPlayer) {
this.game = game;

View File

@ -385,6 +385,19 @@ public final class Colony extends Settlement implements Location {
public Iterator getGoodsIterator() {
return goodsContainer.getGoodsIterator();
}
/**
* Gets an <code>Iterator</code> of every <code>Goods</code> in this
* <code>Colony</code>. There is only one <code>Goods</code>
* for each type of goods.
*
* @return The <code>Iterator</code>.
*/
public Iterator getCompactGoodsIterator() {
return goodsContainer.getCompactGoodsIterator();
}
public boolean contains(Locatable locatable) {
throw new UnsupportedOperationException();
@ -549,13 +562,44 @@ public final class Colony extends Settlement implements Location {
}
/**
* Gets the production of food.
*/
public int getFoodProduction() {
int amount = 0;
Iterator colonyTileIterator = getColonyTileIterator();
while (colonyTileIterator.hasNext()) {
amount += ((ColonyTile) colonyTileIterator.next()).getFoodProduction();
}
return amount;
}
/**
* Returns the horse production (given that enough food
* is beeing produced).
*/
public int getPotentialHorseProduction() {
if (getGoodsCount(Goods.HORSES) < 2) {
return 0;
}
int amount = getGoodsCount(Goods.HORSES) / 10;
return (amount>=1) ? amount : 1;
}
/**
* Prepares this <code>Colony</code> for a new turn.
*/
public void newTurn() {
// Eat food:
int eat = getUnitCount() * 2;
int food = getGoodsCount(Goods.FOOD);
if (eat > food) {
// Kill a colonist:
((Unit) getUnitIterator().next()).dispose();
@ -564,6 +608,27 @@ public final class Colony extends Settlement implements Location {
} else {
removeGoods(Goods.FOOD, eat);
}
// Breed horses:
if (getGoodsCount(Goods.HORSES) >= 2 && surplus > 1) {
int surplus = getFoodProduction() - eat;
if (!getBuilding(Building.STABLES).isBuilt()) {
int horseProduction = Math.min(surplus / 2, getPotentialHorseProduction());
removeGoods(Goods.FOOD, horseProduction);
addGoods(Goods.HORSES, horseProduction);
} else {
int horseProduction = Math.min(surplus, getPotentialHorseProduction());
removeGoods(Goods.FOOD, horseProduction/2);
addGoods(Goods.HORSES, horseProduction);
}
}
// Throw away goods there is no room for.
int capacity = 100 + getBuilding(Building.WAREHOUSE).getLevel() * 100;
goodsContainer.removeAbove(capacity);
}

View File

@ -224,6 +224,24 @@ public class ColonyTile extends FreeColGameObject implements WorkLocation {
return getUnit();
}
/**
* Returns the production of food on this tile.
*/
public int getFoodProduction() {
if (isColonyCenterTile()) {
return workTile.potential(Goods.FOOD);
} else if (getUnit() != null) {
if (getUnit().getWorkType() == Goods.FOOD) {
return getUnit().getFarmedPotential(getUnit().getWorkType(), workTile);
} else {
return 0;
}
} else {
return 0;
}
}
/**
* Prepares this <code>ColonyTile</code> for a new turn.
@ -232,7 +250,7 @@ public class ColonyTile extends FreeColGameObject implements WorkLocation {
if ((getUnit() == null) && !(isColonyCenterTile())) {
return; // Produce nothing if there's nobody to work the terrain.
}
if (!(isColonyCenterTile())) {
int amount = getUnit().getFarmedPotential(getUnit().getWorkType(), workTile);

View File

@ -103,6 +103,12 @@ public class GoodsContainer extends FreeColGameObject {
}
/**
* Removes the given amount of the given type of goods.
*
* @param type The type of goods to remove.
* @param amount The type of amount to remove.
*/
public void removeGoods(int type, int amount) {
if (storedGoods[type] - amount < 0) {
throw new IllegalStateException("Operation would leave " + (storedGoods[type] - amount) + " goods of type " + type + " here.");
@ -110,6 +116,19 @@ public class GoodsContainer extends FreeColGameObject {
storedGoods[type] -= amount;
}
/**
* Removes all goods above given amount, except for
* <code>Goods.FOOD</code> which is left unchanged.
*/
public void removeAbove(int amount) {
for (int i=1; i<storedGoods.length; i++) {
if (storedGoods[i] > amount) {
storedGoods[i] = amount;
}
}
}
/**
@ -152,9 +171,11 @@ public class GoodsContainer extends FreeColGameObject {
/**
* Gets an <code>Iterator</code> of every <code>Goods</code> in this
* <code>GoodsContainer</code>.
* <code>GoodsContainer</code>. Each <code>Goods</code> have a maximum
* amount of 100.
*
* @return The <code>Iterator</code>.
* @see #getCompactGoodsIterator
*/
public Iterator getGoodsIterator() {
ArrayList totalGoods = new ArrayList();
@ -171,6 +192,27 @@ public class GoodsContainer extends FreeColGameObject {
return totalGoods.iterator();
}
/**
* Gets an <code>Iterator</code> of every <code>Goods</code> in this
* <code>GoodsContainer</code>. There is only one <code>Goods</code>
* for each type of goods.
*
* @return The <code>Iterator</code>.
* @see #getGoodsIterator
*/
public Iterator getCompactGoodsIterator() {
ArrayList totalGoods = new ArrayList();
for (int i=0; i<storedGoods.length; i++) {
if (storedGoods[i] > 0) {
totalGoods.add(new Goods(getGame(), parent, i, storedGoods[i]));
}
}
return totalGoods.iterator();
}
/**
* Gets the first <code>Goods</code> in this <code>GoodsContainer</code>.

View File

@ -1328,10 +1328,6 @@ public class Unit extends FreeColGameObject implements Location, Locatable {
* {ACTIVE, FORTIFIED, ...}.
*/
public void setState(int s) {
// For now everything takes forever(=-1) turn to complete (build road, ...).
// This should change according to the terrain etc. !!
// TODO
if (!checkSetState(s)) {
throw new IllegalStateException();
}
@ -2169,7 +2165,7 @@ public class Unit extends FreeColGameObject implements Location, Locatable {
return (Colony) location;
}
}
/**
* Gets the Colony the goods of this unit would go to if it were to de-equip.
* @return The Colony the goods would go to, or null if there is no appropriate Colony
@ -2193,7 +2189,7 @@ public class Unit extends FreeColGameObject implements Location, Locatable {
return null;
}
/**
* Given a type of goods to produce in a building, returns the unit's potential to do so.
* @param goods The type of goods to be produced.
@ -2233,7 +2229,7 @@ public class Unit extends FreeColGameObject implements Location, Locatable {
return 1;
default: // Beats me who or what is working here, but he doesn't get a bonus.
if (isColonist())
return 3;
return 3;
else
return 0; // Can't work if you're not a colonist.
}
@ -2250,7 +2246,7 @@ public class Unit extends FreeColGameObject implements Location, Locatable {
int base = tile.potential(goods);
switch (type) {
case EXPERT_FARMER:
if ((goods == Goods.FOOD) && !tile.isLand()) {
if ((goods == Goods.FOOD) && tile.isLand()) {
//TODO: Special tile stuff. He gets +6/+4 for wheat/deer tiles respectively...
base += 2;
}
@ -2298,14 +2294,14 @@ public class Unit extends FreeColGameObject implements Location, Locatable {
}
break;
case INDIAN_CONVERT:
if ((goods == Goods.FOOD) || (goods == Goods.SUGAR) || (goods == Goods.COTTON) || (goods == Goods.TOBACCO) || (goods == Goods.FURS)) {
if ((goods == Goods.FOOD) || (goods == Goods.SUGAR) || (goods == Goods.COTTON) || (goods == Goods.TOBACCO) || (goods == Goods.FURS) || (goods == Goods.ORE || (goods == Goods.SILVER))) {
base += 1;
}
break;
default: // Beats me who or what is working here, but he doesn't get a bonus.
break;
}
return base;
}