author | truelight |
Sun, 05 Sep 2004 16:15:22 +0000 | |
changeset 159 | 139cf78bfb28 |
parent 149 | 5f7d4b21df01 |
child 201 | c40d343115f8 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "vehicle.h" |
|
4 |
#include "viewport.h" |
|
5 |
#include "command.h" |
|
6 |
#include "player.h" |
|
7 |
#include "town.h" |
|
8 |
#include "gfx.h" |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
9 |
#include "table/directions.h" |
0 | 10 |
|
22 | 11 |
/* When true, GetTrackStatus for roads will treat roads under reconstruction |
12 |
* as normal roads instead of impassable. This is used when detecting whether |
|
13 |
* a road can be removed. This is of course ugly, but I don't know a better |
|
14 |
* solution just like that... */ |
|
0 | 15 |
static bool _road_special_gettrackstatus; |
16 |
||
17 |
void RoadVehEnterDepot(Vehicle *v); |
|
18 |
||
19 |
||
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
72
diff
changeset
|
20 |
bool HasTileRoadAt(uint tile, int i) |
0 | 21 |
{ |
22 |
int mask; |
|
23 |
byte b; |
|
24 |
||
25 |
switch(GET_TILETYPE(tile)) { |
|
26 |
case MP_STREET: |
|
27 |
b = _map5[tile]; |
|
28 |
||
29 |
if ((b & 0xF0) == 0) { |
|
30 |
} else if ((b & 0xF0) == 0x10) { |
|
31 |
b = (b&8)?5:10; |
|
32 |
} else if ((b & 0xF0) == 0x20) { |
|
33 |
return (~b & 3) == i; |
|
34 |
} else |
|
35 |
return false; |
|
36 |
break; |
|
37 |
||
38 |
case MP_STATION: |
|
39 |
b = _map5[tile]; |
|
40 |
if (!IS_BYTE_INSIDE(b, 0x43, 0x43+8)) |
|
41 |
return false; |
|
42 |
return ((~(b - 0x43) & 3) == i); |
|
43 |
||
44 |
case MP_TUNNELBRIDGE: |
|
45 |
mask = GetRoadBitsByTile(tile); |
|
46 |
b = 10; if (mask & 1) break; |
|
47 |
b = 5; if (mask & 2) break; |
|
48 |
return false; |
|
49 |
||
50 |
default: |
|
51 |
return false; |
|
52 |
} |
|
53 |
||
54 |
return HASBIT(b, i); |
|
55 |
} |
|
56 |
||
57 |
static bool CheckAllowRemoveRoad(uint tile, uint br, bool *edge_road) |
|
58 |
{ |
|
59 |
int blocks; |
|
60 |
byte owner; |
|
61 |
uint n; |
|
62 |
*edge_road = true; |
|
63 |
||
64 |
if (_game_mode == GM_EDITOR) |
|
65 |
return true; |
|
66 |
||
67 |
blocks = GetRoadBitsByTile(tile); |
|
68 |
if (blocks == 0) |
|
69 |
return true; |
|
70 |
||
71 |
// Only do the special processing for actual players. |
|
72 |
if (_current_player >= MAX_PLAYERS) |
|
73 |
return true; |
|
74 |
||
75 |
// A railway crossing has the road owner in the map3_lo byte. |
|
76 |
if (IS_TILETYPE(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) { |
|
77 |
owner = _map3_lo[tile]; |
|
78 |
} else { |
|
79 |
owner = _map_owner[tile]; |
|
80 |
} |
|
81 |
// Only do the special processing if the road is owned |
|
82 |
// by a town |
|
83 |
if (owner != OWNER_TOWN) { |
|
84 |
return owner == OWNER_NONE || CheckOwnership(owner); |
|
85 |
} |
|
86 |
||
87 |
if (_cheats.magic_bulldozer.value) |
|
88 |
return true; |
|
89 |
||
90 |
// Get a bitmask of which neighbouring roads has a tile |
|
91 |
n = 0; |
|
92 |
if (blocks&0x25 && HasTileRoadAt(TILE_ADDXY(tile,-1, 0), 1)) n |= 8; |
|
93 |
if (blocks&0x2A && HasTileRoadAt(TILE_ADDXY(tile, 0, 1), 0)) n |= 4; |
|
94 |
if (blocks&0x19 && HasTileRoadAt(TILE_ADDXY(tile, 1, 0), 3)) n |= 2; |
|
95 |
if (blocks&0x16 && HasTileRoadAt(TILE_ADDXY(tile, 0,-1), 2)) n |= 1; |
|
96 |
||
97 |
// If 0 or 1 bits are set in n, or if no bits that match the bits to remove, |
|
98 |
// then allow it |
|
99 |
if ((n & (n-1)) != 0 && (n & br) != 0) { |
|
100 |
Town *t; |
|
101 |
*edge_road = false; |
|
102 |
// you can remove all kind of roads with extra dynamite |
|
103 |
if (_patches.extra_dynamite) |
|
104 |
return true; |
|
105 |
||
106 |
t = ClosestTownFromTile(tile, (uint)-1); |
|
107 |
SET_DPARAM16(0, t->index); |
|
108 |
_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; |
|
109 |
return false; |
|
110 |
} |
|
111 |
||
112 |
return true; |
|
113 |
} |
|
114 |
||
115 |
bool IsRoadDepotTile(TileIndex tile) |
|
116 |
{ |
|
117 |
return IS_TILETYPE(tile, MP_STREET) && |
|
118 |
(_map5[tile] & 0xF0) == 0x20; |
|
119 |
} |
|
120 |
||
121 |
uint GetRoadBitsByTile(TileIndex tile) |
|
122 |
{ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
123 |
uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD); |
0 | 124 |
return (byte)(r | (r >> 8)); |
125 |
} |
|
126 |
||
127 |
// cost for removing inner/edge -roads |
|
128 |
static const uint16 _road_remove_cost[2] = {50, 18}; |
|
129 |
||
130 |
/* Delete a piece of road |
|
131 |
* p1 = piece type |
|
132 |
*/ |
|
133 |
int32 CmdRemoveRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
134 |
{ |
|
135 |
TileInfo ti; |
|
136 |
int32 cost; |
|
137 |
uint tile; |
|
138 |
Town *t; |
|
139 |
/* true if the roadpiece was always removeable, |
|
140 |
false if it was a center piece. Affects town ratings drop |
|
141 |
*/ |
|
142 |
bool edge_road; |
|
143 |
||
144 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
145 |
||
146 |
FindLandscapeHeight(&ti, x, y); |
|
147 |
tile = ti.tile; |
|
148 |
t = ClosestTownFromTile(tile, (uint)-1); // needed for town rating penalty |
|
149 |
||
150 |
// allow deleting road under bridge |
|
151 |
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) |
|
152 |
return CMD_ERROR; |
|
153 |
||
154 |
{ |
|
155 |
bool b; |
|
156 |
_road_special_gettrackstatus = true; |
|
157 |
b = CheckAllowRemoveRoad(tile, p1, &edge_road); |
|
158 |
_road_special_gettrackstatus = false; |
|
159 |
if (!b) |
|
160 |
return CMD_ERROR; |
|
161 |
} |
|
162 |
||
163 |
if (ti.type == MP_TUNNELBRIDGE) { |
|
164 |
if (!EnsureNoVehicleZ(tile, GET_TILEHEIGHT(tile))) |
|
165 |
return CMD_ERROR; |
|
166 |
||
167 |
if ((ti.map5 & 0xE9) == 0xE8) { |
|
168 |
if (p1 & 10) goto return_error; |
|
169 |
} else if ((ti.map5 & 0xE9) == 0xE9) { |
|
170 |
if (p1 & 5) goto return_error; |
|
171 |
} else |
|
172 |
goto return_error; |
|
173 |
||
174 |
cost = _price.remove_road * 2; |
|
175 |
||
176 |
if (flags & DC_EXEC) { |
|
177 |
_map5[tile] = ti.map5 & 0xC7; |
|
178 |
_map_owner[tile] = OWNER_NONE; |
|
179 |
MarkTileDirtyByTile(tile); |
|
180 |
} |
|
181 |
return cost; |
|
182 |
} else if (ti.type == MP_STREET) { |
|
183 |
if (!(ti.map5 & 0xF0)) { |
|
184 |
uint c = p1, t2; |
|
185 |
||
186 |
if (ti.tileh != 0 && (ti.map5 == 5 || ti.map5 == 10)) { |
|
187 |
c |= (c & 0xC) >> 2; |
|
188 |
c |= (c & 0x3) << 2; |
|
189 |
} |
|
190 |
||
191 |
// limit the bits to delete to the existing bits. |
|
192 |
if ((c &= ti.map5) == 0) goto return_error; |
|
193 |
||
194 |
// calculate the cost |
|
195 |
t2 = c; |
|
196 |
cost = 0; |
|
197 |
do { |
|
198 |
if (t2&1) cost += _price.remove_road; |
|
199 |
} while(t2>>=1); |
|
200 |
||
201 |
// check if you're allowed to remove the street owned by a town |
|
202 |
// removal allowance depends on difficulty setting |
|
203 |
if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) { |
|
204 |
if (!CheckforTownRating(tile, flags, t, ROAD_REMOVE)) |
|
205 |
return CMD_ERROR; |
|
206 |
} |
|
207 |
||
208 |
if (flags & DC_EXEC) { |
|
209 |
// checks if the owner is town than decrease town rating by 50 until you have |
|
210 |
// a "Poor" town rating |
|
211 |
if(_map_owner[tile] == OWNER_TOWN && _game_mode != GM_EDITOR) |
|
212 |
ChangeTownRating(t, -_road_remove_cost[(byte)edge_road], -100); |
|
213 |
||
214 |
_map5[tile] ^= c; |
|
215 |
if ((_map5[tile]&0xF) == 0) |
|
216 |
DoClearSquare(tile); |
|
217 |
else |
|
218 |
MarkTileDirtyByTile(tile); |
|
219 |
||
220 |
} |
|
221 |
return cost; |
|
222 |
} else if (!(ti.map5 & 0xE0)) { |
|
223 |
byte c; |
|
224 |
||
225 |
if (!(ti.map5 & 8)) { |
|
226 |
c = 2; |
|
227 |
if (p1 & 5) goto return_error; |
|
228 |
} else { |
|
229 |
c = 1; |
|
230 |
if (p1 & 10) goto return_error; |
|
231 |
} |
|
232 |
||
233 |
cost = _price.remove_road * 2; |
|
234 |
if (flags & DC_EXEC) { |
|
235 |
ModifyTile(tile, |
|
236 |
MP_SETTYPE(MP_RAILWAY) | |
|
237 |
MP_MAP2_CLEAR | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5, |
|
238 |
_map3_hi[tile] & 0xF, /* map3_lo */ |
|
239 |
c /* map5 */ |
|
240 |
); |
|
241 |
} |
|
242 |
return cost; |
|
243 |
} else |
|
244 |
goto return_error; |
|
245 |
||
246 |
} else { |
|
247 |
return_error:; |
|
248 |
return_cmd_error(INVALID_STRING_ID); |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
||
253 |
enum { |
|
254 |
ROAD_NW = 1, // NW road track |
|
255 |
ROAD_SW = 2, // SW road track |
|
256 |
ROAD_SE = 4, // SE road track |
|
257 |
ROAD_NE = 8, // NE road track |
|
258 |
ROAD_ALL = (ROAD_NW | ROAD_SW | ROAD_SE | ROAD_NE) |
|
259 |
}; |
|
260 |
||
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
261 |
static const byte _valid_tileh_slopes_road[3][15] = { |
0 | 262 |
// set of normal ones |
263 |
{ |
|
264 |
ROAD_ALL, 0, 0, |
|
265 |
ROAD_SW | ROAD_NE, 0, 0, // 3, 4, 5 |
|
266 |
ROAD_NW | ROAD_SE, 0, 0, |
|
267 |
ROAD_NW | ROAD_SE, 0, 0, // 9, 10, 11 |
|
268 |
ROAD_SW | ROAD_NE, 0, 0 |
|
269 |
}, |
|
270 |
// allowed road for an evenly raised platform |
|
271 |
{ |
|
272 |
0, |
|
273 |
ROAD_SW | ROAD_NW, |
|
274 |
ROAD_SW | ROAD_SE, |
|
275 |
ROAD_NW | ROAD_SE | ROAD_SW, |
|
276 |
||
277 |
ROAD_SE | ROAD_NE, // 4 |
|
278 |
ROAD_ALL, |
|
279 |
ROAD_SW | ROAD_NE | ROAD_SE, |
|
280 |
ROAD_ALL, |
|
281 |
||
282 |
ROAD_NW | ROAD_NE, // 8 |
|
283 |
ROAD_SW | ROAD_NE | ROAD_NW, |
|
284 |
ROAD_ALL, |
|
285 |
ROAD_ALL, |
|
286 |
||
287 |
ROAD_NW | ROAD_SE | ROAD_NE, // 12 |
|
288 |
ROAD_ALL, |
|
289 |
ROAD_ALL |
|
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
290 |
}, |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
291 |
// valid railway crossings on slopes |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
292 |
{ |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
293 |
1, 0, 0, // 0, 1, 2 |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
294 |
0, 0, 1, // 3, 4, 5 |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
295 |
0, 1, 0, // 6, 7, 8 |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
296 |
0, 1, 1, // 9, 10, 11 |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
297 |
0, 1, 1, // 12, 13, 14 |
0 | 298 |
} |
299 |
}; |
|
300 |
||
301 |
||
302 |
static uint32 CheckRoadSlope(int tileh, byte *pieces, byte existing) |
|
303 |
{ |
|
304 |
if (!(tileh & 0x10)) { |
|
305 |
byte road_bits = *pieces | existing; |
|
306 |
||
307 |
// no special foundation |
|
308 |
if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) { |
|
309 |
// force that all bits are set when we have slopes |
|
310 |
if (tileh != 0) *pieces |= _valid_tileh_slopes_road[0][tileh]; |
|
311 |
return 0; // no extra cost |
|
312 |
} |
|
313 |
||
314 |
// foundation is used. Whole tile is leveled up |
|
315 |
if ((~_valid_tileh_slopes_road[1][tileh] & road_bits) == 0) { |
|
316 |
return existing ? 0 : _price.terraform; |
|
317 |
} |
|
318 |
||
319 |
// partly leveled up tile, only if there's no road on that tile |
|
320 |
if ( !existing && (tileh == 1 || tileh == 2 || tileh == 4 || tileh == 8) ) { |
|
321 |
// force full pieces. |
|
322 |
*pieces |= (*pieces & 0xC) >> 2; |
|
323 |
*pieces |= (*pieces & 0x3) << 2; |
|
72
24bdf837198c
(svn r73) -Fix: Wrong building of road-slopes for a future AI/Town
darkvater
parents:
39
diff
changeset
|
324 |
return (*pieces == (ROAD_NE|ROAD_SW) || *pieces == (ROAD_SE|ROAD_NW)) ? _price.terraform : CMD_ERROR; |
0 | 325 |
} |
326 |
} |
|
327 |
return CMD_ERROR; |
|
328 |
} |
|
329 |
||
330 |
/* Build a piece of road |
|
331 |
* p1 = piece flags |
|
332 |
*/ |
|
333 |
||
334 |
int32 CmdBuildRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
335 |
{ |
|
336 |
TileInfo ti; |
|
337 |
int32 cost; |
|
338 |
byte pieces = (byte)p1, existing = 0; |
|
339 |
uint tile; |
|
340 |
||
341 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
342 |
||
343 |
FindLandscapeHeight(&ti, x, y); |
|
344 |
tile = ti.tile; |
|
345 |
||
346 |
// allow building road under bridge |
|
347 |
if (ti.type != MP_TUNNELBRIDGE && !EnsureNoVehicle(tile)) |
|
348 |
return CMD_ERROR; |
|
349 |
||
350 |
if (ti.type == MP_STREET) { |
|
351 |
if (!(ti.map5 & 0xF0)) { |
|
352 |
if ( ((pieces) & (byte)(ti.map5)) == (pieces)) |
|
353 |
return_cmd_error(STR_1007_ALREADY_BUILT); |
|
354 |
existing = ti.map5; |
|
355 |
} else { |
|
356 |
if (!(ti.map5 & 0xE0) && pieces != ((ti.map5 & 8) ? 5 : 10)) |
|
357 |
return_cmd_error(STR_1007_ALREADY_BUILT); |
|
358 |
goto do_clear; |
|
359 |
} |
|
360 |
} else if (ti.type == MP_RAILWAY) { |
|
361 |
byte m5; |
|
362 |
||
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
363 |
if (ti.tileh & 0x10) // very steep tile |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
364 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
365 |
|
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
366 |
if(!_valid_tileh_slopes_road[2][ti.tileh]) // prevent certain slopes |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
367 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
368 |
|
0 | 369 |
if (ti.map5 == 2) { |
370 |
if (pieces & 5) goto do_clear; |
|
371 |
m5 = 0x10; |
|
372 |
} else if (ti.map5 == 1) { |
|
373 |
if (pieces & 10) goto do_clear; |
|
374 |
m5 = 0x18; |
|
375 |
} else |
|
376 |
goto do_clear; |
|
377 |
||
378 |
if (flags & DC_EXEC) { |
|
379 |
ModifyTile(tile, |
|
380 |
MP_SETTYPE(MP_STREET) | |
|
381 |
MP_MAP2_CLEAR | MP_MAP3LO | MP_MAP3HI | MP_MAP5, |
|
382 |
_current_player, /* map3_lo */ |
|
383 |
_map3_lo[tile] & 0xF, /* map3_hi */ |
|
384 |
m5 /* map5 */ |
|
385 |
); |
|
386 |
} |
|
387 |
return _price.build_road * 2; |
|
388 |
} else if (ti.type == MP_TUNNELBRIDGE) { |
|
389 |
||
390 |
/* check for flat land */ |
|
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
391 |
if (ti.tileh & 0x10) // very steep tile |
0 | 392 |
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION); |
393 |
||
394 |
/* is this middle part of a bridge? */ |
|
395 |
if ((ti.map5 & 0xC0) != 0xC0) |
|
396 |
goto do_clear; |
|
397 |
||
398 |
/* only allow roads pertendicular to bridge */ |
|
399 |
if ((pieces & 5) == (ti.map5 & 0x01)) |
|
400 |
goto do_clear; |
|
401 |
||
402 |
/* check if clear land under bridge */ |
|
403 |
if ((ti.map5 & 0xF8) == 0xE8) /* road under bridge */ |
|
404 |
return_cmd_error(STR_1007_ALREADY_BUILT); |
|
405 |
else if ((ti.map5 & 0xE0) == 0xE0) /* other transport route under bridge */ |
|
406 |
return_cmd_error(STR_1008_MUST_REMOVE_RAILROAD_TRACK); |
|
407 |
else if ((ti.map5 & 0xF8) == 0xC8) /* water under bridge */ |
|
408 |
return_cmd_error(STR_3807_CAN_T_BUILD_ON_WATER); |
|
409 |
||
410 |
/* all checked, can build road now! */ |
|
411 |
cost = _price.build_road * 2; |
|
412 |
if (flags & DC_EXEC) { |
|
413 |
ModifyTile(tile, |
|
414 |
MP_MAPOWNER_CURRENT | MP_MAP5, |
|
415 |
(ti.map5 & 0xC7) | 0x28 // map5 |
|
416 |
); |
|
417 |
} |
|
418 |
return cost; |
|
419 |
} else { |
|
420 |
do_clear:; |
|
421 |
if (DoCommandByTile(tile, 0, 0, flags & ~DC_EXEC, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) |
|
422 |
return CMD_ERROR; |
|
423 |
} |
|
424 |
||
425 |
cost = CheckRoadSlope(ti.tileh, &pieces, existing); |
|
426 |
if (cost == CMD_ERROR) return_cmd_error(STR_1800_LAND_SLOPED_IN_WRONG_DIRECTION); |
|
427 |
||
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
72
diff
changeset
|
428 |
if (cost && (!_patches.build_on_slopes || (!_patches.ainew_active && _is_ai_player))) |
0 | 429 |
return CMD_ERROR; |
430 |
||
431 |
if (!(ti.type == MP_STREET && (ti.map5 & 0xF0) == 0)) { |
|
432 |
cost += DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); |
|
433 |
} else { |
|
434 |
// Don't put the pieces that already exist |
|
435 |
pieces &= ~ti.map5; |
|
436 |
} |
|
437 |
||
438 |
{ |
|
439 |
byte t = pieces; |
|
440 |
while (t) { |
|
441 |
if (t & 1) cost += _price.build_road; |
|
442 |
t >>= 1; |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
if (flags & DC_EXEC) { |
|
447 |
if (ti.type != MP_STREET) { |
|
448 |
_map_type_and_height[tile] &= 0xF; |
|
449 |
_map_type_and_height[tile] |= MP_STREET << 4; |
|
450 |
_map5[tile] = 0; |
|
451 |
_map_owner[tile] = _current_player; |
|
452 |
} |
|
453 |
||
454 |
_map5[tile] |= (byte)pieces; |
|
455 |
||
456 |
MarkTileDirtyByTile(tile); |
|
457 |
} |
|
458 |
return cost; |
|
459 |
} |
|
460 |
||
461 |
int32 DoConvertStreetRail(uint tile, uint totype, bool exec) |
|
462 |
{ |
|
463 |
// not a railroad crossing? |
|
464 |
if ((_map5[tile] & 0xF0) != 0x10) return CMD_ERROR; |
|
465 |
||
466 |
// not owned by me? |
|
467 |
if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile)) return CMD_ERROR; |
|
468 |
||
469 |
// tile is already of requested type? |
|
470 |
if ( (uint)(_map3_hi[tile] & 0xF) == totype) return CMD_ERROR; |
|
471 |
||
472 |
if (exec) { |
|
473 |
// change type. |
|
474 |
_map3_hi[tile] = (_map3_hi[tile] & 0xF0) + totype; |
|
475 |
MarkTileDirtyByTile(tile); |
|
476 |
} |
|
477 |
||
478 |
return _price.build_rail >> 1; |
|
479 |
} |
|
480 |
||
481 |
||
482 |
// Build a long piece of road. |
|
483 |
// x,y = end tile |
|
484 |
// p1 = start tile |
|
485 |
// p2&1 = start tile starts in the 2nd half |
|
486 |
// p2&2 = end tile starts in the 2nd half |
|
487 |
// p2&4 = direction (0 = along x, 1=along y) |
|
488 |
int32 CmdBuildLongRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
489 |
{ |
|
490 |
uint start_tile, end_tile, tile; |
|
491 |
int mode; |
|
492 |
int32 cost,ret; |
|
493 |
||
494 |
start_tile = p1; |
|
495 |
end_tile = TILE_FROM_XY(x, y); |
|
496 |
||
497 |
if (start_tile > end_tile || (start_tile == end_tile && (p2&1))) { |
|
498 |
uint t = start_tile; start_tile = end_tile; end_tile = t; |
|
499 |
p2 ^= IS_INT_INSIDE(p2&3, 1, 3) ? 3 : 0; |
|
500 |
} |
|
501 |
||
502 |
cost = 0; |
|
503 |
tile = start_tile; |
|
504 |
// Start tile is the small number. |
|
505 |
for(;;) { |
|
506 |
mode = (p2&4) ? 5 : 10; |
|
507 |
||
508 |
if (tile == start_tile && (p2&1)) |
|
509 |
mode &= (4+2); |
|
510 |
else if (tile == end_tile && !(p2&2)) |
|
511 |
mode &= (1+8); |
|
512 |
||
513 |
ret = DoCommandByTile(tile, mode, 0, flags, CMD_BUILD_ROAD); |
|
514 |
if (ret == CMD_ERROR) { |
|
515 |
if (_error_message != STR_1007_ALREADY_BUILT) |
|
516 |
return CMD_ERROR; |
|
517 |
} else { |
|
518 |
cost += ret; |
|
519 |
} |
|
520 |
||
521 |
if (tile == end_tile) |
|
522 |
break; |
|
523 |
||
524 |
tile += (p2&4)?TILE_XY(0,1):TILE_XY(1,0); |
|
525 |
} |
|
526 |
||
527 |
// already built? |
|
528 |
if (cost == 0) |
|
529 |
return CMD_ERROR; |
|
530 |
||
531 |
return cost; |
|
532 |
} |
|
533 |
||
534 |
// Remove a long piece of road. |
|
535 |
// x,y = end tile |
|
536 |
// p1 = start tile |
|
537 |
// p2&1 = start tile starts in the 2nd half |
|
538 |
// p2&2 = end tile starts in the 2nd half |
|
539 |
// p2&4 = direction (0 = along x, 1=along y) |
|
540 |
int32 CmdRemoveLongRoad(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
541 |
{ |
|
542 |
uint start_tile, end_tile, tile; |
|
543 |
int32 cost,ret; |
|
544 |
||
545 |
start_tile = p1; |
|
546 |
end_tile = TILE_FROM_XY(x, y); |
|
547 |
||
548 |
if (start_tile > end_tile || (start_tile == end_tile && (p2&1))) { |
|
549 |
uint t = start_tile; start_tile = end_tile; end_tile = t; |
|
550 |
p2 ^= IS_INT_INSIDE(p2&3, 1, 3) ? 3 : 0; |
|
551 |
} |
|
552 |
||
553 |
cost = 0; |
|
554 |
tile = start_tile; |
|
555 |
// Start tile is the small number. |
|
556 |
for(;;) { |
|
557 |
uint bits = (p2 & 4) ? ROAD_SE | ROAD_NW : ROAD_SW | ROAD_NE; |
|
558 |
if (tile == end_tile && !(p2&2)) bits &= ROAD_NW | ROAD_NE; |
|
559 |
if (tile == start_tile && (p2&1)) bits &= ROAD_SE | ROAD_SW; |
|
560 |
||
561 |
// try to remove the halves. |
|
562 |
if (bits) { |
|
563 |
ret = DoCommandByTile(tile, bits, 0, flags, CMD_REMOVE_ROAD); |
|
564 |
if (ret != CMD_ERROR) |
|
565 |
cost += ret; |
|
566 |
} |
|
567 |
||
568 |
if (tile == end_tile) |
|
569 |
break; |
|
570 |
||
571 |
tile += (p2&4)?TILE_XY(0,1):TILE_XY(1,0); |
|
572 |
} |
|
573 |
||
574 |
// already built? |
|
575 |
if (cost == 0) |
|
576 |
return CMD_ERROR; |
|
577 |
||
578 |
return cost; |
|
579 |
} |
|
580 |
||
581 |
/* Build a road depot |
|
582 |
* p1 - direction (0-3) |
|
583 |
* p2 - unused |
|
584 |
*/ |
|
585 |
||
586 |
int32 CmdBuildRoadDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
587 |
{ |
|
588 |
TileInfo ti; |
|
589 |
int32 cost; |
|
590 |
Depot *dep; |
|
591 |
uint tile; |
|
592 |
||
593 |
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); |
|
594 |
||
595 |
FindLandscapeHeight(&ti, x, y); |
|
596 |
||
597 |
tile = ti.tile; |
|
598 |
||
599 |
if (!EnsureNoVehicle(tile)) |
|
600 |
return CMD_ERROR; |
|
601 |
||
602 |
if (ti.tileh != 0) { |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
72
diff
changeset
|
603 |
if (!_patches.build_on_slopes || (ti.tileh & 0x10 || !((0x4C >> p1) & ti.tileh) )) |
0 | 604 |
return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); |
605 |
} |
|
606 |
||
607 |
cost = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); |
|
608 |
if (cost == CMD_ERROR) |
|
609 |
return CMD_ERROR; |
|
610 |
||
611 |
dep = AllocateDepot(); |
|
612 |
if (dep == NULL) |
|
613 |
return CMD_ERROR; |
|
614 |
||
615 |
if (flags & DC_EXEC) { |
|
616 |
if (_current_player == _local_player) |
|
617 |
_last_built_road_depot_tile = (TileIndex)tile; |
|
618 |
||
619 |
dep->xy = tile; |
|
620 |
dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index; |
|
621 |
||
622 |
ModifyTile(tile, |
|
623 |
MP_SETTYPE(MP_STREET) | |
|
624 |
MP_MAPOWNER_CURRENT | MP_MAP5, |
|
625 |
(p1 | 0x20) /* map5 */ |
|
626 |
); |
|
627 |
||
628 |
} |
|
629 |
return cost + _price.build_road_depot; |
|
630 |
} |
|
631 |
||
632 |
static int32 RemoveRoadDepot(uint tile, uint32 flags) |
|
633 |
{ |
|
149
5f7d4b21df01
(svn r150) -Fix: [1010833] Turning on the magic bulldozer removes oil rigs
darkvater
parents:
84
diff
changeset
|
634 |
if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) |
0 | 635 |
return CMD_ERROR; |
636 |
||
637 |
if (!EnsureNoVehicle(tile)) |
|
638 |
return CMD_ERROR; |
|
639 |
||
640 |
if (flags & DC_EXEC) { |
|
641 |
DoDeleteDepot(tile); |
|
642 |
} |
|
643 |
||
644 |
return _price.remove_road_depot; |
|
645 |
} |
|
646 |
||
647 |
#define M(x) (1<<(x)) |
|
648 |
||
649 |
static int32 ClearTile_Road(uint tile, byte flags) { |
|
650 |
int32 ret; |
|
651 |
byte m5 = _map5[tile]; |
|
652 |
||
653 |
if ( (m5 & 0xF0) == 0) { |
|
654 |
byte b = m5 & 0xF; |
|
655 |
||
656 |
if (! ((1 << b) & (M(1)|M(2)|M(4)|M(8))) ) { |
|
657 |
if ( (!(flags & DC_AI_BUILDING) || _map_owner[tile]!=OWNER_TOWN) && flags&DC_AUTO) |
|
658 |
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); |
|
659 |
} |
|
660 |
return DoCommandByTile(tile, b, 0, flags, CMD_REMOVE_ROAD); |
|
661 |
} else if ( (m5 & 0xE0) == 0) { |
|
662 |
if (flags & DC_AUTO) |
|
663 |
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST); |
|
664 |
||
665 |
ret = DoCommandByTile(tile, (m5&8)?5:10, 0, flags, CMD_REMOVE_ROAD); |
|
666 |
if (ret == CMD_ERROR) |
|
667 |
return CMD_ERROR; |
|
668 |
||
669 |
if (flags & DC_EXEC) { |
|
670 |
DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); |
|
671 |
} |
|
672 |
||
673 |
return ret; |
|
674 |
} else { |
|
675 |
if (flags & DC_AUTO) |
|
676 |
return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); |
|
677 |
return RemoveRoadDepot(tile,flags); |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
||
682 |
typedef struct DrawRoadTileStruct { |
|
683 |
uint16 image; |
|
684 |
byte subcoord_x; |
|
685 |
byte subcoord_y; |
|
686 |
} DrawRoadTileStruct; |
|
687 |
||
688 |
typedef struct DrawRoadSeqStruct { |
|
689 |
uint32 image; |
|
690 |
byte subcoord_x; |
|
691 |
byte subcoord_y; |
|
692 |
byte width; |
|
693 |
byte height; |
|
694 |
} DrawRoadSeqStruct; |
|
695 |
||
696 |
#include "table/road_land.h" |
|
697 |
||
698 |
||
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
72
diff
changeset
|
699 |
uint GetRoadFoundation(uint tileh, uint bits) { |
0 | 700 |
int i; |
701 |
// normal level sloped building |
|
702 |
if ((~_valid_tileh_slopes_road[1][tileh] & bits) == 0) |
|
703 |
return tileh; |
|
704 |
||
705 |
// inclined sloped building |
|
706 |
if ( ((i=0, tileh == 1) || (i+=2, tileh == 2) || (i+=2, tileh == 4) || (i+=2, tileh == 8)) && |
|
707 |
((bits == (ROAD_SW | ROAD_NE)) || (i++, bits == (ROAD_NW | ROAD_SE)))) |
|
708 |
return i + 15; |
|
709 |
||
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
710 |
// rail crossing |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
711 |
if ((bits & 0x10) && _valid_tileh_slopes_road[2][tileh]) |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
712 |
return tileh; |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
713 |
|
0 | 714 |
return 0; |
715 |
} |
|
716 |
||
717 |
const byte _road_sloped_sprites[14] = { |
|
718 |
0, 0, 2, 0, |
|
719 |
0, 1, 0, 0, |
|
720 |
3, 0, 0, 0, |
|
721 |
0, 0 |
|
722 |
}; |
|
723 |
||
724 |
static void DrawTile_Road(TileInfo *ti) |
|
725 |
{ |
|
726 |
uint32 image; |
|
727 |
byte m2; |
|
728 |
const byte *s; |
|
729 |
||
730 |
if ( (ti->map5 & 0xF0) == 0) { // if it is a road the upper 4 bits are 0 |
|
731 |
const DrawRoadTileStruct *drts; |
|
732 |
||
733 |
if (ti->tileh != 0) { |
|
734 |
int f = GetRoadFoundation(ti->tileh, ti->map5 & 0xF); |
|
735 |
if (f) DrawFoundation(ti, f); |
|
736 |
||
737 |
// default sloped sprites.. |
|
738 |
if (ti->tileh != 0) { |
|
739 |
image = _road_sloped_sprites[ti->tileh - 1] + 0x53F; |
|
740 |
} else { |
|
741 |
image = _road_tile_sprites_1[ti->map5 & 0xF]; |
|
742 |
} |
|
743 |
} else { |
|
744 |
image = _road_tile_sprites_1[ti->map5 & 0xF]; |
|
745 |
} |
|
746 |
||
747 |
m2 = _map2[ti->tile] & 7; |
|
748 |
||
749 |
if (m2 == 0) image |= 0x3178000; |
|
750 |
||
751 |
if (_map3_hi[ti->tile] & 0x80) { |
|
752 |
image += 19; |
|
753 |
} else if (m2 > 1 && m2 != 6) { |
|
754 |
image -= 19; /* pavement along the road? */ |
|
755 |
} |
|
756 |
||
757 |
DrawGroundSprite(image); |
|
758 |
||
759 |
if (!(_display_opt & DO_FULL_DETAIL) || _cur_dpi->zoom == 2) |
|
760 |
return; |
|
761 |
||
762 |
if (m2 >= 6) { |
|
763 |
// roadwork |
|
764 |
DrawGroundSprite(0x586 + ((ti->map5&8)!=0 ? 0 : 1)); |
|
765 |
return; |
|
766 |
} |
|
767 |
||
768 |
drts = (const DrawRoadTileStruct*)_road_display_table[m2][ti->map5 & 0xF]; |
|
769 |
||
770 |
while ((image = drts->image) != 0) { |
|
771 |
int x = ti->x | drts->subcoord_x; |
|
772 |
int y = ti->y | drts->subcoord_y; |
|
773 |
byte z = ti->z; |
|
774 |
if (ti->tileh != 0) z = GetSlopeZ(x, y); |
|
775 |
AddSortableSpriteToDraw(image, x, y, 2, 2, 0x10, z); |
|
776 |
drts++; |
|
777 |
} |
|
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
778 |
} else if ( (ti->map5 & 0xE0) == 0) { // railroad crossing |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
779 |
int f = GetRoadFoundation(ti->tileh, ti->map5 & 0xF); |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
780 |
if (f) DrawFoundation(ti, f); |
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
781 |
|
0 | 782 |
image = 0x55B; |
783 |
||
784 |
if ( (ti->map5 & 8) != 0) |
|
785 |
image--; |
|
786 |
||
787 |
if ( (ti->map5 & 4) != 0) |
|
788 |
image += 2; |
|
789 |
||
790 |
if ( _map3_hi[ti->tile] & 0x80) { |
|
791 |
image += 8; |
|
792 |
} else { |
|
793 |
m2 = _map2[ti->tile] & 7; |
|
794 |
if (m2 == 0) image |= 0x3178000; |
|
795 |
if (m2 > 1) image += 4; |
|
796 |
} |
|
797 |
||
798 |
DrawGroundSprite(image + (_map3_hi[ti->tile] & 0xF) * 12); |
|
799 |
} else { |
|
800 |
uint32 ormod; |
|
801 |
int player; |
|
802 |
const DrawRoadSeqStruct *drss; |
|
803 |
||
804 |
if (ti->tileh != 0) { DrawFoundation(ti, ti->tileh); } |
|
805 |
||
806 |
ormod = 0x315; |
|
807 |
player = _map_owner[ti->tile]; |
|
808 |
if (player < MAX_PLAYERS) |
|
809 |
ormod = PLAYER_SPRITE_COLOR(player); |
|
810 |
||
811 |
s = _road_display_datas[ti->map5 & 0xF]; |
|
812 |
||
813 |
DrawGroundSprite(*(uint32*)s); |
|
814 |
s += sizeof(uint32); |
|
815 |
drss = (DrawRoadSeqStruct*)s; |
|
816 |
||
817 |
while ((image=drss->image) != 0) { |
|
818 |
if (image & 0x8000) |
|
819 |
image |= ormod; |
|
820 |
||
821 |
AddSortableSpriteToDraw(image, ti->x | drss->subcoord_x, |
|
822 |
ti->y | drss->subcoord_y, drss->width, drss->height, 0x14, ti->z); |
|
823 |
drss++; |
|
824 |
} |
|
825 |
} |
|
826 |
} |
|
827 |
||
828 |
void DrawRoadDepotSprite(int x, int y, int image) |
|
829 |
{ |
|
830 |
uint32 ormod; |
|
831 |
const DrawRoadSeqStruct *dtss; |
|
832 |
const byte *t; |
|
833 |
||
834 |
ormod = PLAYER_SPRITE_COLOR(_local_player); |
|
835 |
||
836 |
t = _road_display_datas[image]; |
|
837 |
||
838 |
x+=33; |
|
839 |
y+=17; |
|
840 |
||
841 |
DrawSprite(*(uint32*)t, x, y); |
|
842 |
t += sizeof(uint32); |
|
843 |
||
844 |
for(dtss = (DrawRoadSeqStruct *)t; dtss->image != 0; dtss++) { |
|
845 |
Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0); |
|
846 |
||
847 |
image = dtss->image; |
|
848 |
if (image & 0x8000) |
|
849 |
image |= ormod; |
|
850 |
||
851 |
DrawSprite(image, x + pt.x, y + pt.y); |
|
852 |
} |
|
853 |
} |
|
854 |
||
855 |
uint GetSlopeZ_Road(TileInfo *ti) |
|
856 |
{ |
|
857 |
uint z = ti->z; |
|
858 |
int th = ti->tileh; |
|
859 |
||
860 |
// check if it's a foundation |
|
861 |
if (ti->tileh != 0) { |
|
13
3e7c1f74a996
(svn r14) Fix: railroad crossings on slopes are now possible
dominik
parents:
0
diff
changeset
|
862 |
if ((ti->map5 & 0xE0) == 0) { /* road or crossing */ |
0 | 863 |
uint f = GetRoadFoundation(ti->tileh, ti->map5 & 0x3F); |
864 |
if (f != 0) { |
|
865 |
if (f < 15) { |
|
866 |
// leveled foundation |
|
867 |
return z + 8; |
|
868 |
} |
|
869 |
// inclined foundation |
|
870 |
th = _inclined_tileh[f - 15]; |
|
871 |
} |
|
872 |
} else if ((ti->map5 & 0xF0) == 0x20) { |
|
873 |
// depot |
|
874 |
return z + 8; |
|
875 |
} |
|
876 |
return GetPartialZ(ti->x&0xF, ti->y&0xF, th) + z; |
|
877 |
} |
|
39 | 878 |
return z; // normal Z if no slope |
879 |
} |
|
880 |
||
881 |
uint GetSlopeTileh_Road(TileInfo *ti) |
|
882 |
{ |
|
883 |
// check if it's a foundation |
|
884 |
if (ti->tileh != 0) { |
|
885 |
if ((ti->map5 & 0xE0) == 0) { /* road or crossing */ |
|
886 |
uint f = GetRoadFoundation(ti->tileh, ti->map5 & 0x3F); |
|
887 |
if (f != 0) { |
|
888 |
if (f < 15) { |
|
889 |
// leveled foundation |
|
890 |
return 0; |
|
891 |
} |
|
892 |
// inclined foundation |
|
893 |
return _inclined_tileh[f - 15]; |
|
894 |
} |
|
895 |
} else if ((ti->map5 & 0xF0) == 0x20) { |
|
896 |
// depot |
|
897 |
return 0; |
|
898 |
} |
|
899 |
} |
|
900 |
return ti->tileh; |
|
0 | 901 |
} |
902 |
||
903 |
static void GetAcceptedCargo_Road(uint tile, AcceptedCargo *ac) |
|
904 |
{ |
|
905 |
/* not used */ |
|
906 |
} |
|
907 |
||
908 |
static void AnimateTile_Road(uint tile) |
|
909 |
{ |
|
910 |
if ((_map5[tile] & 0xF0) == 0x10) { |
|
911 |
MarkTileDirtyByTile(tile); |
|
912 |
} |
|
913 |
} |
|
914 |
||
915 |
static const byte _town_road_types[5][2] = { |
|
916 |
{1,1}, |
|
917 |
{2,2}, |
|
918 |
{2,2}, |
|
919 |
{5,5}, |
|
920 |
{3,2}, |
|
921 |
}; |
|
922 |
||
923 |
static const byte _town_road_types_2[5][2] = { |
|
924 |
{1,1}, |
|
925 |
{2,2}, |
|
926 |
{3,2}, |
|
927 |
{3,2}, |
|
928 |
{3,2}, |
|
929 |
}; |
|
930 |
||
931 |
||
932 |
static void TileLoop_Road(uint tile) |
|
933 |
{ |
|
934 |
Town *t; |
|
935 |
int grp; |
|
936 |
||
937 |
if (_opt.landscape == LT_HILLY) { |
|
938 |
// Fix snow style if the road is above the snowline |
|
939 |
if ((_map3_hi[tile] & 0x80) != ((GetTileZ(tile) > _opt.snow_line) ? 0x80 : 0x00)) { |
|
940 |
_map3_hi[tile] ^= 0x80; |
|
941 |
MarkTileDirtyByTile(tile); |
|
942 |
} |
|
943 |
} else if (_opt.landscape == LT_DESERT) { |
|
944 |
// Fix desert style |
|
945 |
if (GetMapExtraBits(tile) == 1 && !(_map3_hi[tile] & 0x80)) { |
|
946 |
_map3_hi[tile] |= 0x80; |
|
947 |
MarkTileDirtyByTile(tile); |
|
948 |
} |
|
949 |
} |
|
950 |
||
951 |
if (_map5[tile] & 0xE0) |
|
952 |
return; |
|
953 |
||
954 |
if ((_map2[tile] & 7) < 6) { |
|
955 |
t = ClosestTownFromTile(tile, (uint)-1); |
|
956 |
grp = 0; |
|
957 |
if (t != NULL) { |
|
958 |
// If in the scenario editor, set the owner to a town. |
|
959 |
if (_game_mode == GM_EDITOR) { |
|
960 |
_map_owner[tile] = OWNER_TOWN; |
|
961 |
} |
|
962 |
||
963 |
grp = GetTownRadiusGroup(t, tile); |
|
964 |
||
965 |
// Show an animation to indicate road work |
|
966 |
if (t->road_build_months != 0 && |
|
967 |
!(GetTileDist(t->xy, tile) >= 8 && grp==0) && |
|
968 |
(_map5[tile]==5 || _map5[tile]==10)) { |
|
969 |
if (GetTileSlope(tile, NULL) == 0 && EnsureNoVehicle(tile) && CHANCE16(1,20)) { |
|
970 |
_map2[tile] = ((_map2[tile]&7) <= 1) ? 6 : 7; |
|
971 |
||
972 |
SndPlayTileFx(0x1F,tile); |
|
973 |
CreateEffectVehicleAbove( |
|
974 |
GET_TILE_X(tile) * 16 + 7, |
|
975 |
GET_TILE_Y(tile) * 16 + 7, |
|
976 |
0, |
|
977 |
EV_ROADWORK); |
|
978 |
MarkTileDirtyByTile(tile); |
|
979 |
return; |
|
980 |
} |
|
981 |
} |
|
982 |
} |
|
983 |
||
984 |
{ |
|
985 |
const byte *p = (_opt.landscape == LT_CANDY) ? _town_road_types_2[grp] : _town_road_types[grp]; |
|
986 |
byte b = _map2[tile] & 7; |
|
987 |
||
988 |
if (b == p[0]) |
|
989 |
return; |
|
990 |
||
991 |
if (b == p[1]) { |
|
992 |
b = p[0]; |
|
993 |
} else if (b == 0) { |
|
994 |
b = p[1]; |
|
995 |
} else { |
|
996 |
b = 0; |
|
997 |
} |
|
998 |
_map2[tile] = (_map2[tile] & ~7) | b; |
|
999 |
MarkTileDirtyByTile(tile); |
|
1000 |
} |
|
1001 |
} else { |
|
1002 |
// Handle road work |
|
1003 |
||
1004 |
byte b = _map2[tile]; |
|
1005 |
if (b < 0x80) { |
|
1006 |
_map2[tile] = b + 8; |
|
1007 |
return; |
|
1008 |
} |
|
1009 |
_map2[tile] = ((b&7) == 6) ? 1 : 2; |
|
1010 |
MarkTileDirtyByTile(tile); |
|
1011 |
} |
|
1012 |
} |
|
1013 |
||
1014 |
void ShowRoadDepotWindow(uint tile); |
|
1015 |
||
1016 |
static void ClickTile_Road(uint tile) |
|
1017 |
{ |
|
1018 |
if ((_map5[tile] & 0xF0) == 0x20) { |
|
1019 |
ShowRoadDepotWindow(tile); |
|
1020 |
} |
|
1021 |
} |
|
1022 |
||
1023 |
static const byte _road_trackbits[16] = { |
|
1024 |
0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F, |
|
1025 |
}; |
|
1026 |
||
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1027 |
static uint32 GetTileTrackStatus_Road(uint tile, TransportType mode) { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1028 |
if (mode == TRANSPORT_RAIL) { |
0 | 1029 |
if ((_map5[tile] & 0xF0) != 0x10) |
1030 |
return 0; |
|
1031 |
return _map5[tile] & 8 ? 0x101 : 0x202; |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1032 |
} else if (mode == TRANSPORT_ROAD) { |
0 | 1033 |
byte b = _map5[tile]; |
1034 |
if ((b & 0xF0) == 0) { |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1035 |
/* Ordinary road */ |
0 | 1036 |
if (!_road_special_gettrackstatus && (_map2[tile]&7) >= 6) |
1037 |
return 0; |
|
1038 |
return _road_trackbits[b&0xF] * 0x101; |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1039 |
} else if ((b&0xF0) == 0x10) { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1040 |
/* Crossing */ |
0 | 1041 |
uint32 r = 0x101; |
1042 |
if (b&8) r <<= 1; |
|
1043 |
||
1044 |
if (b&4) { |
|
1045 |
r *= 0x10001; |
|
1046 |
} |
|
1047 |
return r; |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1048 |
} else if ((b&0xF0) == 0x20) { |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1049 |
/* Depot */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1050 |
/* We reverse the dir because it points out of the |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1051 |
* exit, and we want to get in. Maybe we should return |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1052 |
* both dirs here? */ |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1053 |
byte dir = _reverse_dir[b&3]; |
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
149
diff
changeset
|
1054 |
return 1 << _dir_to_straight_trackdir[dir]; |
0 | 1055 |
} |
1056 |
} |
|
1057 |
return 0; |
|
1058 |
} |
|
1059 |
||
1060 |
static const StringID _road_tile_strings[] = { |
|
1061 |
STR_1818_ROAD_RAIL_LEVEL_CROSSING, |
|
1062 |
STR_1817_ROAD_VEHICLE_DEPOT, |
|
1063 |
||
1064 |
STR_1814_ROAD, |
|
1065 |
STR_1814_ROAD, |
|
1066 |
STR_1814_ROAD, |
|
1067 |
STR_1815_ROAD_WITH_STREETLIGHTS, |
|
1068 |
STR_1814_ROAD, |
|
1069 |
STR_1816_TREE_LINED_ROAD, |
|
1070 |
STR_1814_ROAD, |
|
1071 |
STR_1814_ROAD, |
|
1072 |
}; |
|
1073 |
||
1074 |
static void GetTileDesc_Road(uint tile, TileDesc *td) |
|
1075 |
{ |
|
1076 |
int i = (_map5[tile] >> 4); |
|
1077 |
if (i == 0) |
|
1078 |
i = (_map2[tile] & 7) + 3; |
|
1079 |
td->str = _road_tile_strings[i - 1]; |
|
1080 |
td->owner = _map_owner[tile]; |
|
1081 |
} |
|
1082 |
||
1083 |
static const byte _roadveh_enter_depot_unk0[4] = { |
|
1084 |
8, 9, 0, 1 |
|
1085 |
}; |
|
1086 |
||
1087 |
static uint32 VehicleEnter_Road(Vehicle *v, uint tile, int x, int y) |
|
1088 |
{ |
|
1089 |
if ((_map5[tile] & 0xF0) == 0x10) { |
|
1090 |
if (v->type == VEH_Train && (_map5[tile] & 4) == 0) { |
|
1091 |
/* train crossing a road */ |
|
1092 |
SndPlayVehicleFx(12, v); |
|
1093 |
_map5[tile] |= 4; |
|
1094 |
MarkTileDirtyByTile(tile); |
|
1095 |
} |
|
1096 |
} else if ((_map5[tile]&0xF0) == 0x20){ |
|
1097 |
if (v->type == VEH_Road && v->u.road.frame == 11) { |
|
1098 |
if (_roadveh_enter_depot_unk0[_map5[tile]&3] == v->u.road.state) { |
|
1099 |
RoadVehEnterDepot(v); |
|
1100 |
return 4; |
|
1101 |
} |
|
1102 |
} |
|
1103 |
} |
|
1104 |
return 0; |
|
1105 |
} |
|
1106 |
||
1107 |
static void VehicleLeave_Road(Vehicle *v, uint tile, int x, int y) |
|
1108 |
{ |
|
1109 |
if ((_map5[tile] & 0xF0) == 0x10 && v->type == VEH_Train && v->next == NULL) { |
|
1110 |
_map5[tile] &= ~4; |
|
1111 |
MarkTileDirtyByTile(tile); |
|
1112 |
} |
|
1113 |
} |
|
1114 |
||
1115 |
static void ChangeTileOwner_Road(uint tile, byte old_player, byte new_player) |
|
1116 |
{ |
|
1117 |
byte b; |
|
1118 |
||
1119 |
// road/rail crossing where the road is owned by the current player? |
|
1120 |
if (old_player == _map3_lo[tile] && (_map5[tile]&0xF0) == 0x10) { |
|
1121 |
_map3_lo[tile] = (new_player == 0xFF) ? OWNER_NONE : new_player; |
|
1122 |
} |
|
1123 |
||
1124 |
if (_map_owner[tile] != old_player) |
|
1125 |
return; |
|
1126 |
||
1127 |
if (new_player != 255) { |
|
1128 |
_map_owner[tile] = new_player; |
|
1129 |
} else { |
|
1130 |
b = _map5[tile]&0xF0; |
|
1131 |
if (b == 0) { |
|
1132 |
_map_owner[tile] = OWNER_NONE; |
|
1133 |
} else if (b == 0x10) { |
|
1134 |
_map5[tile] = (_map5[tile]&8) ? 0x5 : 0xA; |
|
1135 |
_map_owner[tile] = _map3_lo[tile]; |
|
1136 |
_map3_lo[tile] = 0; |
|
1137 |
_map3_hi[tile] &= 0x80; |
|
1138 |
} else { |
|
1139 |
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); |
|
1140 |
} |
|
1141 |
} |
|
1142 |
} |
|
1143 |
||
1144 |
void InitializeRoad() |
|
1145 |
{ |
|
1146 |
_last_built_road_depot_tile = 0; |
|
1147 |
} |
|
1148 |
||
1149 |
const TileTypeProcs _tile_type_road_procs = { |
|
1150 |
DrawTile_Road, /* draw_tile_proc */ |
|
1151 |
GetSlopeZ_Road, /* get_slope_z_proc */ |
|
1152 |
ClearTile_Road, /* clear_tile_proc */ |
|
1153 |
GetAcceptedCargo_Road, /* get_accepted_cargo_proc */ |
|
1154 |
GetTileDesc_Road, /* get_tile_desc_proc */ |
|
1155 |
GetTileTrackStatus_Road, /* get_tile_track_status_proc */ |
|
1156 |
ClickTile_Road, /* click_tile_proc */ |
|
1157 |
AnimateTile_Road, /* animate_tile_proc */ |
|
1158 |
TileLoop_Road, /* tile_loop_clear */ |
|
1159 |
ChangeTileOwner_Road, /* change_tile_owner_clear */ |
|
1160 |
NULL, /* get_produced_cargo_proc */ |
|
1161 |
VehicleEnter_Road, /* vehicle_enter_tile_proc */ |
|
1162 |
VehicleLeave_Road, /* vehicle_leave_tile_proc */ |
|
39 | 1163 |
GetSlopeTileh_Road, /* get_slope_tileh_proc */ |
0 | 1164 |
}; |