9 package micropolisj.engine;
11 import static micropolisj.engine.TileConstants.*;
13 class RoadLikeTool
extends ToolStroke
15 RoadLikeTool(Micropolis city, MicropolisTool tool,
int xpos,
int ypos)
17 super(city, tool, xpos, ypos);
21 protected void applyArea(ToolEffectIfc eff)
24 if (!applyForward(eff)) {
27 if (!applyBackward(eff)) {
33 boolean applyBackward(ToolEffectIfc eff)
35 boolean anyChange =
false;
37 CityRect b = getBounds();
38 for (
int i = b.height - 1; i >= 0; i--) {
39 for (
int j = b.width - 1; j >= 0; j--) {
40 TranslatedToolEffect tte =
new TranslatedToolEffect(eff, b.x+j, b.y+i);
41 anyChange = anyChange || applySingle(tte);
47 boolean applyForward(ToolEffectIfc eff)
49 boolean anyChange =
false;
51 CityRect b = getBounds();
52 for (
int i = 0; i < b.height; i++) {
53 for (
int j = 0; j < b.width; j++) {
54 TranslatedToolEffect tte =
new TranslatedToolEffect(eff, b.x+j, b.y+i);
55 anyChange = anyChange || applySingle(tte);
62 public CityRect getBounds()
70 if (Math.abs(xdest-xpos) >= Math.abs(ydest-ypos)) {
72 CityRect r =
new CityRect();
73 r.x = Math.min(xpos, xdest);
74 r.width = Math.abs(xdest-xpos) + 1;
81 CityRect r =
new CityRect();
84 r.y = Math.min(ypos, ydest);
85 r.height = Math.abs(ydest-ypos) + 1;
90 boolean applySingle(ToolEffectIfc eff)
95 return applyRailTool(eff);
98 return applyRoadTool(eff);
101 return applyWireTool(eff);
104 throw new Error(
"Unexpected tool: " + tool);
108 boolean applyRailTool(ToolEffectIfc eff)
119 boolean applyRoadTool(ToolEffectIfc eff)
130 boolean applyWireTool(ToolEffectIfc eff)
141 private boolean layRail(ToolEffectIfc eff)
143 final int RAIL_COST = 20;
144 final int TUNNEL_COST = 100;
146 int cost = RAIL_COST;
148 char tile = (char) eff.getTile(0, 0);
149 tile = neutralizeRoad(tile);
161 char eTile = neutralizeRoad(eff.getTile(1, 0));
162 if (eTile == RAILHPOWERV ||
164 (eTile >= LHRAIL && eTile <= HRAILROAD))
166 eff.setTile(0, 0, HRAIL);
173 char wTile = neutralizeRoad(eff.getTile(-1, 0));
174 if (wTile == RAILHPOWERV ||
176 (wTile > VRAIL && wTile < VRAILROAD))
178 eff.setTile(0, 0, HRAIL);
185 char sTile = neutralizeRoad(eff.getTile(0, 1));
186 if (sTile == RAILVPOWERH ||
187 sTile == VRAILROAD ||
188 (sTile > HRAIL && sTile < HRAILROAD))
190 eff.setTile(0, 0, VRAIL);
197 char nTile = neutralizeRoad(eff.getTile(0, -1));
198 if (nTile == RAILVPOWERH ||
199 nTile == VRAILROAD ||
200 (nTile > HRAIL && nTile < HRAILROAD))
202 eff.setTile(0, 0, VRAIL);
211 eff.setTile(0, 0, RAILVPOWERH);
215 eff.setTile(0, 0, RAILHPOWERV);
218 case TileConstants.ROADS:
219 eff.setTile(0, 0, VRAILROAD);
223 eff.setTile(0, 0, HRAILROAD);
238 eff.setTile(0, 0, LHRAIL);
246 private boolean layRoad(ToolEffectIfc eff)
248 final int ROAD_COST = 10;
249 final int BRIDGE_COST = 50;
251 int cost = ROAD_COST;
253 char tile = (char) eff.getTile(0, 0);
264 char eTile = neutralizeRoad(eff.getTile(1, 0));
265 if (eTile == VRAILROAD ||
267 (eTile >= TileConstants.ROADS && eTile <= HROADPOWER))
269 eff.setTile(0, 0, HBRIDGE);
276 char wTile = neutralizeRoad(eff.getTile(-1, 0));
277 if (wTile == VRAILROAD ||
279 (wTile >= TileConstants.ROADS && wTile <= INTERSECTION))
281 eff.setTile(0, 0, HBRIDGE);
288 char sTile = neutralizeRoad(eff.getTile(0, 1));
289 if (sTile == HRAILROAD ||
290 sTile == VROADPOWER ||
291 (sTile >= VBRIDGE && sTile <= INTERSECTION))
293 eff.setTile(0, 0, VBRIDGE);
300 char nTile = neutralizeRoad(eff.getTile(0, -1));
301 if (nTile == HRAILROAD ||
302 nTile == VROADPOWER ||
303 (nTile >= VBRIDGE && nTile <= INTERSECTION))
305 eff.setTile(0, 0, VBRIDGE);
314 eff.setTile(0, 0, VROADPOWER);
318 eff.setTile(0, 0, HROADPOWER);
322 eff.setTile(0, 0, HRAILROAD);
326 eff.setTile(0, 0, VRAILROAD);
342 eff.setTile(0, 0, TileConstants.ROADS);
350 private boolean layWire(ToolEffectIfc eff)
352 final int WIRE_COST = 5;
353 final int UNDERWATER_WIRE_COST = 25;
355 int cost = WIRE_COST;
357 char tile = (char) eff.getTile(0, 0);
358 tile = neutralizeRoad(tile);
366 cost = UNDERWATER_WIRE_COST;
370 int tmp = eff.getTile(1, 0);
371 char tmpn = neutralizeRoad(tmp);
373 if (isConductive(tmp) &&
374 tmpn != HROADPOWER &&
375 tmpn != RAILHPOWERV &&
378 eff.setTile(0, 0, VPOWER);
385 int tmp = eff.getTile(-1, 0);
386 char tmpn = neutralizeRoad(tmp);
388 if (isConductive(tmp) &&
389 tmpn != HROADPOWER &&
390 tmpn != RAILHPOWERV &&
393 eff.setTile(0, 0, VPOWER);
400 int tmp = eff.getTile(0, 1);
401 char tmpn = neutralizeRoad(tmp);
403 if (isConductive(tmp) &&
404 tmpn != VROADPOWER &&
405 tmpn != RAILVPOWERH &&
408 eff.setTile(0, 0, HPOWER);
415 int tmp = eff.getTile(0, -1);
416 char tmpn = neutralizeRoad(tmp);
418 if (isConductive(tmp) &&
419 tmpn != VROADPOWER &&
420 tmpn != RAILVPOWERH &&
423 eff.setTile(0, 0, HPOWER);
431 case TileConstants.ROADS:
432 eff.setTile(0, 0, HROADPOWER);
436 eff.setTile(0, 0, VROADPOWER);
440 eff.setTile(0, 0, RAILHPOWERV);
444 eff.setTile(0, 0, RAILVPOWERH);
459 eff.setTile(0, 0, LHPOWER);