refactor: move totalFunds property to separate class

this new class (CityBudget) will contain all finance-related balances

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@600 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-03-23 15:45:37 +00:00
parent 92f79886ae
commit 04534d6273
5 changed files with 44 additions and 23 deletions

View file

@ -0,0 +1,20 @@
// This file is part of MicropolisJ.
// Copyright (C) 2013 Jason Long
// Portions Copyright (C) 1989-2007 Electronic Arts Inc.
//
// MicropolisJ is free software; you can redistribute it and/or modify
// it under the terms of the GNU GPLv3, with additional terms.
// See the README file, included in this distribution, for details.
package micropolisj.engine;
public class CityBudget
{
private final Micropolis city;
public int totalFunds;
CityBudget(Micropolis city)
{
this.city = city;
}
}

View file

@ -60,7 +60,8 @@ public class MapGenerator
public void generateSomeCity(long r) public void generateSomeCity(long r)
{ {
engine.totalFunds = GameLevel.getStartingFunds(engine.gameLevel); //FIXME- initial funds should be set by caller
engine.budget.totalFunds = GameLevel.getStartingFunds(engine.gameLevel);
generateMap(r); generateMap(r);
engine.fireWholeMapChanged(); engine.fireWholeMapChanged();
} }

View file

@ -94,7 +94,7 @@ public class Micropolis
static final int DEFAULT_WIDTH = 120; static final int DEFAULT_WIDTH = 120;
static final int DEFAULT_HEIGHT = 100; static final int DEFAULT_HEIGHT = 100;
public int totalFunds; public final CityBudget budget = new CityBudget(this);
public boolean autoBulldoze = true; public boolean autoBulldoze = true;
public boolean autoBudget = false; public boolean autoBudget = false;
public Speed simSpeed = Speed.NORMAL; public Speed simSpeed = Speed.NORMAL;
@ -205,7 +205,7 @@ public class Micropolis
public void spend(int amount) public void spend(int amount)
{ {
totalFunds -= amount; budget.totalFunds -= amount;
fireFundsChanged(); fireFundsChanged();
} }
@ -1725,7 +1725,7 @@ public class Micropolis
cashFlow = revenue - expenses; cashFlow = revenue - expenses;
spend(-cashFlow); spend(-cashFlow);
hist.totalFunds = totalFunds; hist.totalFunds = budget.totalFunds;
financialHistory.add(0,hist); financialHistory.add(0,hist);
taxFund = 0; taxFund = 0;
@ -1751,7 +1751,7 @@ public class Micropolis
b.firePercent = Math.max(0.0, firePercent); b.firePercent = Math.max(0.0, firePercent);
b.policePercent = Math.max(0.0, policePercent); b.policePercent = Math.max(0.0, policePercent);
b.previousBalance = totalFunds; b.previousBalance = budget.totalFunds;
b.taxIncome = (int)Math.round(lastTotalPop * landValueAverage / 120 * b.taxRate * FLevels[gameLevel]); b.taxIncome = (int)Math.round(lastTotalPop * landValueAverage / 120 * b.taxRate * FLevels[gameLevel]);
assert b.taxIncome >= 0; assert b.taxIncome >= 0;
@ -1763,7 +1763,7 @@ public class Micropolis
b.fireFunded = (int)Math.round(b.fireRequest * b.firePercent); b.fireFunded = (int)Math.round(b.fireRequest * b.firePercent);
b.policeFunded = (int)Math.round(b.policeRequest * b.policePercent); b.policeFunded = (int)Math.round(b.policeRequest * b.policePercent);
int yumDuckets = totalFunds + b.taxIncome; int yumDuckets = budget.totalFunds + b.taxIncome;
assert yumDuckets >= 0; assert yumDuckets >= 0;
if (yumDuckets >= b.roadFunded) if (yumDuckets >= b.roadFunded)
@ -1919,7 +1919,7 @@ public class Micropolis
dis.readShort(); dis.readShort();
} }
totalFunds = dis.readInt(); //[50-51] total funds budget.totalFunds = dis.readInt(); //[50-51] total funds
autoBulldoze = dis.readShort() != 0; //52 autoBulldoze = dis.readShort() != 0; //52
autoBudget = dis.readShort() != 0; autoBudget = dis.readShort() != 0;
autoGo = dis.readShort() != 0; //54 autoGo = dis.readShort() != 0; //54
@ -1986,7 +1986,7 @@ public class Micropolis
out.writeShort(0); out.writeShort(0);
} }
//50 //50
out.writeInt(totalFunds); out.writeInt(budget.totalFunds);
out.writeShort(autoBulldoze ? 1 : 0); out.writeShort(autoBulldoze ? 1 : 0);
out.writeShort(autoBudget ? 1 : 0); out.writeShort(autoBudget ? 1 : 0);
//54 //54
@ -2613,7 +2613,7 @@ public class Micropolis
gameLevel = newLevel; gameLevel = newLevel;
fireOptionsChanged(); fireOptionsChanged();
if (totalFunds > delta) { if (budget.totalFunds > delta) {
spend(delta); spend(delta);
} }
} }

View file

@ -158,7 +158,7 @@ public enum MicropolisTool
cost += getToolCost(); cost += getToolCost();
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
// take care of the money situation here // take care of the money situation here
@ -220,7 +220,7 @@ public enum MicropolisTool
cost += getToolCost(); cost += getToolCost();
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
// take care of the money situation here // take care of the money situation here
@ -283,7 +283,7 @@ public enum MicropolisTool
cost += getToolCost(); cost += getToolCost();
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
// take care of the money situation here // take care of the money situation here
@ -394,7 +394,7 @@ public enum MicropolisTool
if ((currTile & ZONEBIT) != 0) if ((currTile & ZONEBIT) != 0)
{ {
// zone center bit is set // zone center bit is set
if (engine.totalFunds >= 1) if (engine.budget.totalFunds >= 1)
{ {
engine.spend(1); engine.spend(1);
switch (checkSize(tmp)) switch (checkSize(tmp))
@ -435,7 +435,7 @@ public enum MicropolisTool
tmp == REDGE || tmp == REDGE ||
tmp == CHANNEL) tmp == CHANNEL)
{ {
if (engine.totalFunds >= 6) if (engine.budget.totalFunds >= 6)
{ {
ToolResult result = layDoze(engine, xpos, ypos); ToolResult result = layDoze(engine, xpos, ypos);
if (tmp != (engine.getTile(xpos, ypos) & LOMASK)) if (tmp != (engine.getTile(xpos, ypos) & LOMASK))
@ -501,7 +501,7 @@ public enum MicropolisTool
} }
} }
if (engine.totalFunds < cost) { if (engine.budget.totalFunds < cost) {
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
} }
@ -538,7 +538,7 @@ public enum MicropolisTool
private ToolResult layDoze(Micropolis engine, int xpos, int ypos) private ToolResult layDoze(Micropolis engine, int xpos, int ypos)
{ {
if (engine.totalFunds <= 0) if (engine.budget.totalFunds <= 0)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
char tile = engine.getTile(xpos, ypos); char tile = engine.getTile(xpos, ypos);
@ -569,7 +569,7 @@ public enum MicropolisTool
final int TUNNEL_COST = 100; final int TUNNEL_COST = 100;
int cost = RAIL_COST; int cost = RAIL_COST;
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
char tile = (char) (engine.getTile(xpos, ypos) & LOMASK); char tile = (char) (engine.getTile(xpos, ypos) & LOMASK);
@ -580,7 +580,7 @@ public enum MicropolisTool
case CHANNEL: case CHANNEL:
cost = TUNNEL_COST; cost = TUNNEL_COST;
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
if (xpos + 1 < engine.getWidth()) if (xpos + 1 < engine.getWidth())
@ -676,7 +676,7 @@ public enum MicropolisTool
final int BRIDGE_COST = 50; final int BRIDGE_COST = 50;
int cost = ROAD_COST; int cost = ROAD_COST;
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
char tile = (char) (engine.getTile(xpos, ypos) & LOMASK); char tile = (char) (engine.getTile(xpos, ypos) & LOMASK);
@ -687,7 +687,7 @@ public enum MicropolisTool
case CHANNEL: // check how to build bridges, if possible. case CHANNEL: // check how to build bridges, if possible.
cost = BRIDGE_COST; cost = BRIDGE_COST;
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
if (xpos + 1 < engine.getWidth()) if (xpos + 1 < engine.getWidth())
@ -783,7 +783,7 @@ public enum MicropolisTool
final int UNDERWATER_WIRE_COST = 25; final int UNDERWATER_WIRE_COST = 25;
int cost = WIRE_COST; int cost = WIRE_COST;
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
char tile = (char) (engine.getTile(xpos, ypos) & LOMASK); char tile = (char) (engine.getTile(xpos, ypos) & LOMASK);
@ -796,7 +796,7 @@ public enum MicropolisTool
case CHANNEL: case CHANNEL:
cost = UNDERWATER_WIRE_COST; cost = UNDERWATER_WIRE_COST;
if (engine.totalFunds < cost) if (engine.budget.totalFunds < cost)
return ToolResult.INSUFFICIENT_FUNDS; return ToolResult.INSUFFICIENT_FUNDS;
if (xpos + 1 < engine.getWidth()) if (xpos + 1 < engine.getWidth())

View file

@ -1211,7 +1211,7 @@ public class MainWindow extends JFrame
private void reloadFunds() private void reloadFunds()
{ {
fundsLbl.setText(formatFunds(getEngine().totalFunds)); fundsLbl.setText(formatFunds(getEngine().budget.totalFunds));
} }
//implements Micropolis.Listener //implements Micropolis.Listener