From 0fe2c2fe1d0c7e6507c0881725d768948ac8e477 Mon Sep 17 00:00:00 2001 From: "jason@long.name" Date: Thu, 16 May 2013 23:41:40 +0000 Subject: [PATCH] toolstroke: move bulldozer tool code to a separate file git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@627 d9718cc8-9f43-0410-858b-315f434eb58c --- src/micropolisj/engine/Bulldozer.java | 130 +++++++++++++++++++ src/micropolisj/engine/MicropolisTool.java | 7 +- src/micropolisj/engine/ToolStroke.java | 137 --------------------- 3 files changed, 136 insertions(+), 138 deletions(-) create mode 100644 src/micropolisj/engine/Bulldozer.java diff --git a/src/micropolisj/engine/Bulldozer.java b/src/micropolisj/engine/Bulldozer.java new file mode 100644 index 0000000..8f2fafc --- /dev/null +++ b/src/micropolisj/engine/Bulldozer.java @@ -0,0 +1,130 @@ +package micropolisj.engine; + +import java.awt.Rectangle; +import static micropolisj.engine.TileConstants.*; + +class Bulldozer extends ToolStroke +{ + Bulldozer(Micropolis city, int xpos, int ypos) + { + super(city, MicropolisTool.BULLDOZER, xpos, ypos); + } + + @Override + public ToolResult apply() + { + ToolResult checkResult = check(); + if (checkResult != ToolResult.SUCCESS) { + return checkResult; + } + + Rectangle b = getBounds(); + if (b.width == 1 && b.height == 1 && + isZoneCenter(city.getTile(b.x, b.y))) + { + return dozeZone(b.x, b.y); + } + + int countDozed = 0; + for (int y = b.y; y < b.y+b.height; y++) { + for (int x = b.x; x < b.x+b.width; x++) { + + char tile = city.getTile(x, y); + if (isDozeable(tile)) { + countDozed++; + + dozeField(x, y); + } + + } + } + return ToolResult.SUCCESS; + } + + @Override + public ToolResult check() + { + Rectangle b = getBounds(); + if (b.width == 1 && b.height == 1 && + isZoneCenter(city.getTile(b.x, b.y))) + { + int cost = 1; + return city.budget.totalFunds >= cost ? + ToolResult.SUCCESS : + ToolResult.INSUFFICIENT_FUNDS; + } + + int countDozed = 0; + for (int y = b.y; y < b.y+b.height; y++) { + for (int x = b.x; x < b.x+b.width; x++) { + + char tile = city.getTile(x, y); + if (isDozeable(tile)) { + countDozed++; + } + + } + } + + int cost = 1 * countDozed; + return city.budget.totalFunds < cost ? ToolResult.INSUFFICIENT_FUNDS : + countDozed != 0 ? ToolResult.SUCCESS : + ToolResult.NONE; + } + + ToolResult dozeZone(int xpos, int ypos) + { + assert city.testBounds(xpos, ypos); + + char currTile = city.getTile(xpos, ypos); + char tmp = (char)(currTile & LOMASK); + + // zone center bit is set + assert isZoneCenter(currTile); + + city.spend(1); + switch (checkSize(tmp)) + { + case 3: + city.makeSound(xpos, ypos, Sound.EXPLOSION_HIGH); + putRubble(xpos, ypos, 3, 3); + break; + case 4: + city.makeSound(xpos, ypos, Sound.EXPLOSION_LOW); + putRubble(xpos, ypos, 4, 4); + break; + case 6: + city.makeSound(xpos, ypos, Sound.EXPLOSION_BOTH); + putRubble(xpos, ypos, 6, 6); + break; + default: + assert false; + break; + } + return ToolResult.SUCCESS; + } + + ToolResult dozeField(int xpos, int ypos) + { + char tile = city.getTile(xpos, ypos); + + // check dozeable bit + assert isDozeable(tile); + + tile = neutralizeRoad(tile); + if (isOverWater(tile)) + { + // dozing over water, replace with water. + city.setTile(xpos, ypos, RIVER); + } + else + { + // dozing on land, replace with land. Simple, eh? + city.setTile(xpos, ypos, DIRT); + } + + city.spend(1); + return ToolResult.SUCCESS; + } + +} diff --git a/src/micropolisj/engine/MicropolisTool.java b/src/micropolisj/engine/MicropolisTool.java index 7fdb613..4b9ba6f 100644 --- a/src/micropolisj/engine/MicropolisTool.java +++ b/src/micropolisj/engine/MicropolisTool.java @@ -65,7 +65,12 @@ public enum MicropolisTool public ToolStroke beginStroke(Micropolis engine, int xpos, int ypos) { - return new ToolStroke(engine, this, xpos, ypos); + if (this == BULLDOZER) { + return new Bulldozer(engine, xpos, ypos); + } + else { + return new ToolStroke(engine, this, xpos, ypos); + } } public ToolResult apply(Micropolis engine, int xpos, int ypos) diff --git a/src/micropolisj/engine/ToolStroke.java b/src/micropolisj/engine/ToolStroke.java index cec74b4..724a7e5 100644 --- a/src/micropolisj/engine/ToolStroke.java +++ b/src/micropolisj/engine/ToolStroke.java @@ -84,8 +84,6 @@ public class ToolStroke { switch (tool) { - case BULLDOZER: - return checkBulldozer(); // case RAIL: // case ROADS: // case WIRE: @@ -102,9 +100,6 @@ public class ToolStroke { switch (tool) { - case BULLDOZER: - return applyBulldozer(xpos, ypos); - case RAIL: return applyRailTool(xpos, ypos); @@ -455,111 +450,6 @@ public class ToolStroke } } - ToolResult checkBulldozer() - { - Rectangle b = getBounds(); - if (b.width == 1 && b.height == 1 && - isZoneCenter(city.getTile(b.x, b.y))) - { - int cost = 1; - return city.budget.totalFunds >= cost ? - ToolResult.SUCCESS : - ToolResult.INSUFFICIENT_FUNDS; - } - - int countDozed = 0; - for (int y = b.y; y < b.y+b.height; y++) { - for (int x = b.x; x < b.x+b.width; x++) { - - char tile = city.getTile(x, y); - if (isDozeable(tile)) { - countDozed++; - } - - } - } - - int cost = 1 * countDozed; - return city.budget.totalFunds < cost ? ToolResult.INSUFFICIENT_FUNDS : - countDozed != 0 ? ToolResult.SUCCESS : - ToolResult.NONE; - } - - ToolResult applyBulldozer(int xpos, int ypos) - { - if (!city.testBounds(xpos, ypos)) - return ToolResult.UH_OH; - - char currTile = city.getTile(xpos, ypos); - char tmp = (char)(currTile & LOMASK); - - if ((currTile & ZONEBIT) != 0) - { - // zone center bit is set - if (city.budget.totalFunds >= 1) - { - city.spend(1); - switch (checkSize(tmp)) - { - case 3: - city.makeSound(xpos, ypos, Sound.EXPLOSION_HIGH); - putRubble(xpos, ypos, 3, 3); - break; - case 4: - city.makeSound(xpos, ypos, Sound.EXPLOSION_LOW); - putRubble(xpos, ypos, 4, 4); - break; - case 6: - city.makeSound(xpos, ypos, Sound.EXPLOSION_BOTH); - putRubble(xpos, ypos, 6, 6); - break; - default: - assert false; - break; - } - return ToolResult.SUCCESS; - } - else - { - return ToolResult.INSUFFICIENT_FUNDS; - } - } - else if (false && isBigZone(tmp)) - { - // The GPL Micropolis will uses a bunch of code to find - // the center of this zone, and then converts it to rubble - // the same as clicking the center of the zone. - // I prefer to make the user click the critical spot of - // the zone to destroy it. - return ToolResult.UH_OH; - } - else if (tmp == RIVER || - tmp == REDGE || - tmp == CHANNEL) - { - if (city.budget.totalFunds >= 6) - { - ToolResult result = layDoze(xpos, ypos); - if (tmp != (city.getTile(xpos, ypos) & LOMASK)) - { - // tile changed - city.spend(5); - fixZone(xpos, ypos); - } - return result; - } - else { - return ToolResult.INSUFFICIENT_FUNDS; - } - } - else - { - ToolResult result = layDoze(xpos, ypos); - fixZone(xpos, ypos); - return result; - } - } - ToolResult applyRailTool(int xpos, int ypos) { if (!city.testBounds(xpos, ypos)) @@ -638,33 +528,6 @@ public class ToolStroke return tile; } - private ToolResult layDoze(int xpos, int ypos) - { - if (city.budget.totalFunds <= 0) - return ToolResult.INSUFFICIENT_FUNDS; - - char tile = city.getTile(xpos, ypos); - - // check dozeable bit - if ((tile & BULLBIT) == 0) - return ToolResult.NONE; - - tile = neutralizeRoad(tile); - if (isOverWater(tile)) - { - // dozing over water, replace with water. - city.setTile(xpos, ypos, RIVER); - } - else - { - // dozing on land, replace with land. Simple, eh? - city.setTile(xpos, ypos, DIRT); - } - - city.spend(1); - return ToolResult.SUCCESS; - } - private ToolResult layRail(int xpos, int ypos) { final int RAIL_COST = 20;