9 package micropolisj.engine;
12 import static micropolisj.engine.TileConstants.*;
19 final Micropolis city;
25 Stack<CityLocation> positions =
new Stack<CityLocation>();
27 static final int MAX_TRAFFIC_DISTANCE = 30;
29 public TrafficGen(Micropolis city)
36 if (findPerimeterRoad())
56 while (!positions.isEmpty())
58 CityLocation pos = positions.pop();
61 assert city.testBounds(mapX, mapY);
64 int tile = city.getTile(mapX, mapY);
65 if (tile >= ROADBASE && tile < POWERBASE)
67 city.addTraffic(mapX, mapY, 50);
72 static final int [] PerimX = { -1, 0, 1, 2, 2, 2, 1, 0,-1, -2,-2,-2 };
73 static final int [] PerimY = { -2,-2,-2, -1, 0, 1, 2, 2, 2, 1, 0,-1 };
74 boolean findPerimeterRoad()
76 for (
int z = 0; z < 12; z++)
78 int tx = mapX + PerimX[z];
79 int ty = mapY + PerimY[z];
91 boolean roadTest(
int tx,
int ty)
93 if (!city.testBounds(tx, ty)) {
97 char c = city.getTile(tx, ty);
101 else if (c > LASTRAIL)
103 else if (c >= POWERBASE && c < LASTPOWER)
114 for (
int z = 0; z < MAX_TRAFFIC_DISTANCE; z++)
128 if (!positions.isEmpty())
144 static final int [] DX = { 0, 1, 0, -1 };
145 static final int [] DY = { -1, 0, 1, 0 };
149 int rdir = city.PRNG.nextInt(4);
151 for (
int d = rdir; d < rdir + 4; d++)
154 if (realdir == lastdir)
157 if (roadTest(mapX + DX[realdir], mapY + DY[realdir]))
161 lastdir = (realdir + 2) % 4;
166 positions.push(
new CityLocation(mapX, mapY));
194 throw new Error(
"unreachable");
199 int tile = city.getTile(mapX, mapY-1);
200 if (tile >= low && tile <= high)
203 if (mapX + 1 < city.getWidth())
205 int tile = city.getTile(mapX + 1, mapY);
206 if (tile >= low && tile <= high)
209 if (mapY + 1 < city.getHeight())
211 int tile = city.getTile(mapX, mapY + 1);
212 if (tile >= low && tile <= high)
217 int tile = city.getTile(mapX - 1, mapY);
218 if (tile >= low && tile <= high)
229 RESIDENTIAL, COMMERCIAL, INDUSTRIAL;