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