OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

rail_cmd.cpp
Go to the documentation of this file.
1 /* $Id: rail_cmd.cpp 27253 2015-04-26 10:26:07Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "stdafx.h"
13 #include "cmd_helper.h"
14 #include "viewport_func.h"
15 #include "command_func.h"
16 #include "depot_base.h"
17 #include "pathfinder/yapf/yapf_cache.h"
18 #include "newgrf_debug.h"
19 #include "newgrf_railtype.h"
20 #include "train.h"
21 #include "autoslope.h"
22 #include "water.h"
23 #include "tunnelbridge_map.h"
24 #include "vehicle_func.h"
25 #include "sound_func.h"
26 #include "tunnelbridge.h"
27 #include "elrail_func.h"
28 #include "town.h"
29 #include "pbs.h"
30 #include "company_base.h"
31 #include "core/backup_type.hpp"
32 #include "date_func.h"
33 #include "strings_func.h"
34 #include "company_gui.h"
35 #include "object_map.h"
36
37 #include "table/strings.h"
38 #include "table/railtypes.h"
39 #include "table/track_land.h"
40
41 #include "safeguards.h"
42
44 typedef SmallVector<Train *, 16> TrainList;
45
46 RailtypeInfo _railtypes[RAILTYPE_END];
47
48 assert_compile(sizeof(_original_railtypes) <= sizeof(_railtypes));
49
51 enum SignalOffsets {
52 SIGNAL_TO_SOUTHWEST,
53 SIGNAL_TO_NORTHEAST,
54 SIGNAL_TO_SOUTHEAST,
55 SIGNAL_TO_NORTHWEST,
56 SIGNAL_TO_EAST,
57 SIGNAL_TO_WEST,
58 SIGNAL_TO_SOUTH,
59 SIGNAL_TO_NORTH,
60 };
61
65 void ResetRailTypes()
66 {
67 memset(_railtypes, 0, sizeof(_railtypes));
68 memcpy(_railtypes, _original_railtypes, sizeof(_original_railtypes));
69 }
70
71 void ResolveRailTypeGUISprites(RailtypeInfo *rti)
72 {
73 SpriteID cursors_base = GetCustomRailSprite(rti, INVALID_TILE, RTSG_CURSORS);
74 if (cursors_base != 0) {
75 rti->gui_sprites.build_ns_rail = cursors_base + 0;
76 rti->gui_sprites.build_x_rail = cursors_base + 1;
77 rti->gui_sprites.build_ew_rail = cursors_base + 2;
78 rti->gui_sprites.build_y_rail = cursors_base + 3;
79 rti->gui_sprites.auto_rail = cursors_base + 4;
80 rti->gui_sprites.build_depot = cursors_base + 5;
81 rti->gui_sprites.build_tunnel = cursors_base + 6;
82 rti->gui_sprites.convert_rail = cursors_base + 7;
83 rti->cursor.rail_ns = cursors_base + 8;
84 rti->cursor.rail_swne = cursors_base + 9;
85 rti->cursor.rail_ew = cursors_base + 10;
86 rti->cursor.rail_nwse = cursors_base + 11;
87 rti->cursor.autorail = cursors_base + 12;
88 rti->cursor.depot = cursors_base + 13;
89 rti->cursor.tunnel = cursors_base + 14;
90 rti->cursor.convert = cursors_base + 15;
91 }
92
93 /* Array of default GUI signal sprite numbers. */
94 const SpriteID _signal_lookup[2][SIGTYPE_END] = {
95 {SPR_IMG_SIGNAL_ELECTRIC_NORM, SPR_IMG_SIGNAL_ELECTRIC_ENTRY, SPR_IMG_SIGNAL_ELECTRIC_EXIT,
96 SPR_IMG_SIGNAL_ELECTRIC_COMBO, SPR_IMG_SIGNAL_ELECTRIC_PBS, SPR_IMG_SIGNAL_ELECTRIC_PBS_OWAY},
97
98 {SPR_IMG_SIGNAL_SEMAPHORE_NORM, SPR_IMG_SIGNAL_SEMAPHORE_ENTRY, SPR_IMG_SIGNAL_SEMAPHORE_EXIT,
99 SPR_IMG_SIGNAL_SEMAPHORE_COMBO, SPR_IMG_SIGNAL_SEMAPHORE_PBS, SPR_IMG_SIGNAL_SEMAPHORE_PBS_OWAY},
100 };
101
102 for (SignalType type = SIGTYPE_NORMAL; type < SIGTYPE_END; type = (SignalType)(type + 1)) {
103 for (SignalVariant var = SIG_ELECTRIC; var <= SIG_SEMAPHORE; var = (SignalVariant)(var + 1)) {
104 SpriteID red = GetCustomSignalSprite(rti, INVALID_TILE, type, var, SIGNAL_STATE_RED, true);
105 SpriteID green = GetCustomSignalSprite(rti, INVALID_TILE, type, var, SIGNAL_STATE_GREEN, true);
106 rti->gui_sprites.signals[type][var][0] = (red != 0) ? red + SIGNAL_TO_SOUTH : _signal_lookup[var][type];
107 rti->gui_sprites.signals[type][var][1] = (green != 0) ? green + SIGNAL_TO_SOUTH : _signal_lookup[var][type] + 1;
108 }
109 }
110 }
111
115 void InitRailTypes()
116 {
117 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
118 RailtypeInfo *rti = &_railtypes[rt];
119 ResolveRailTypeGUISprites(rti);
120 }
121 }
122
126 RailType AllocateRailType(RailTypeLabel label)
127 {
128 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
129 RailtypeInfo *rti = &_railtypes[rt];
130
131 if (rti->label == 0) {
132 /* Set up new rail type */
133 memcpy(rti, &_railtypes[RAILTYPE_RAIL], sizeof(*rti));
134 rti->label = label;
135 /* Clear alternate label list. Can't use Reset() here as that would free
136 * the data pointer of RAILTYPE_RAIL and not our new rail type. */
137 new (&rti->alternate_labels) RailTypeLabelList;
138
139 /* Make us compatible with ourself. */
140 rti->powered_railtypes = (RailTypes)(1 << rt);
141 rti->compatible_railtypes = (RailTypes)(1 << rt);
142
143 /* We also introduce ourself. */
144 rti->introduces_railtypes = (RailTypes)(1 << rt);
145
146 /* Default sort order; order of allocation, but with some
147 * offsets so it's easier for NewGRF to pick a spot without
148 * changing the order of other (original) rail types.
149 * The << is so you can place other railtypes in between the
150 * other railtypes, the 7 is to be able to place something
151 * before the first (default) rail type. */
152 rti->sorting_order = rt << 4 | 7;
153 return rt;
154 }
155 }
156
157 return INVALID_RAILTYPE;
158 }
159
160 static const byte _track_sloped_sprites[14] = {
161 14, 15, 22, 13,
162 0, 21, 17, 12,
163 23, 0, 18, 20,
164 19, 16
165 };
166
167
168 /* 4
169 * ---------
170 * |\ /|
171 * | \ 1/ |
172 * | \ / |
173 * | \ / |
174 * 16| \ |32
175 * | / \2 |
176 * | / \ |
177 * | / \ |
178 * |/ \|
179 * ---------
180 * 8
181 */
182
183
184
185 /* MAP2 byte: abcd???? => Signal On? Same coding as map3lo
186 * MAP3LO byte: abcd???? => Signal Exists?
187 * a and b are for diagonals, upper and left,
188 * one for each direction. (ie a == NE->SW, b ==
189 * SW->NE, or v.v., I don't know. b and c are
190 * similar for lower and right.
191 * MAP2 byte: ????abcd => Type of ground.
192 * MAP3LO byte: ????abcd => Type of rail.
193 * MAP5: 00abcdef => rail
194 * 01abcdef => rail w/ signals
195 * 10uuuuuu => unused
196 * 11uuuudd => rail depot
197 */
198
207 static CommandCost EnsureNoTrainOnTrack(TileIndex tile, Track track)
208 {
209 TrackBits rail_bits = TrackToTrackBits(track);
210 return EnsureNoTrainOnTrackBits(tile, rail_bits);
211 }
212
220 static CommandCost CheckTrackCombination(TileIndex tile, TrackBits to_build, uint flags)
221 {
222 if (!IsPlainRail(tile)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
223
224 /* So, we have a tile with tracks on it (and possibly signals). Let's see
225 * what tracks first */
226 TrackBits current = GetTrackBits(tile); // The current track layout.
227 TrackBits future = current | to_build; // The track layout we want to build.
228
229 /* Are we really building something new? */
230 if (current == future) {
231 /* Nothing new is being built */
232 return_cmd_error(STR_ERROR_ALREADY_BUILT);
233 }
234
235 /* Let's see if we may build this */
236 if ((flags & DC_NO_RAIL_OVERLAP) || HasSignals(tile)) {
237 /* If we are not allowed to overlap (flag is on for ai companies or we have
238 * signals on the tile), check that */
239 if (future != TRACK_BIT_HORZ && future != TRACK_BIT_VERT) {
240 return_cmd_error((flags & DC_NO_RAIL_OVERLAP) ? STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION : STR_ERROR_MUST_REMOVE_SIGNALS_FIRST);
241 }
242 }
243 /* Normally, we may overlap and any combination is valid */
244 return CommandCost();
245 }
246
247
249 static const TrackBits _valid_tracks_without_foundation[15] = {
250 TRACK_BIT_ALL,
251 TRACK_BIT_RIGHT,
252 TRACK_BIT_UPPER,
253 TRACK_BIT_X,
254
255 TRACK_BIT_LEFT,
256 TRACK_BIT_NONE,
257 TRACK_BIT_Y,
258 TRACK_BIT_LOWER,
259
260 TRACK_BIT_LOWER,
261 TRACK_BIT_Y,
262 TRACK_BIT_NONE,
263 TRACK_BIT_LEFT,
264
265 TRACK_BIT_X,
266 TRACK_BIT_UPPER,
267 TRACK_BIT_RIGHT,
268 };
269
271 static const TrackBits _valid_tracks_on_leveled_foundation[15] = {
272 TRACK_BIT_NONE,
273 TRACK_BIT_LEFT,
274 TRACK_BIT_LOWER,
275 TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_LEFT,
276
277 TRACK_BIT_RIGHT,
278 TRACK_BIT_ALL,
279 TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_RIGHT,
280 TRACK_BIT_ALL,
281
282 TRACK_BIT_UPPER,
283 TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_LEFT,
284 TRACK_BIT_ALL,
285 TRACK_BIT_ALL,
286
287 TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_RIGHT,
288 TRACK_BIT_ALL,
289 TRACK_BIT_ALL
290 };
291
299 Foundation GetRailFoundation(Slope tileh, TrackBits bits)
300 {
301 if (bits == TRACK_BIT_NONE) return FOUNDATION_NONE;
302
303 if (IsSteepSlope(tileh)) {
304 /* Test for inclined foundations */
305 if (bits == TRACK_BIT_X) return FOUNDATION_INCLINED_X;
306 if (bits == TRACK_BIT_Y) return FOUNDATION_INCLINED_Y;
307
308 /* Get higher track */
309 Corner highest_corner = GetHighestSlopeCorner(tileh);
310 TrackBits higher_track = CornerToTrackBits(highest_corner);
311
312 /* Only higher track? */
313 if (bits == higher_track) return HalftileFoundation(highest_corner);
314
315 /* Overlap with higher track? */
316 if (TracksOverlap(bits | higher_track)) return FOUNDATION_INVALID;
317
318 /* either lower track or both higher and lower track */
319 return ((bits & higher_track) != 0 ? FOUNDATION_STEEP_BOTH : FOUNDATION_STEEP_LOWER);
320 } else {
321 if ((~_valid_tracks_without_foundation[tileh] & bits) == 0) return FOUNDATION_NONE;
322
323 bool valid_on_leveled = ((~_valid_tracks_on_leveled_foundation[tileh] & bits) == 0);
324
325 Corner track_corner;
326 switch (bits) {
327 case TRACK_BIT_LEFT: track_corner = CORNER_W; break;
328 case TRACK_BIT_LOWER: track_corner = CORNER_S; break;
329 case TRACK_BIT_RIGHT: track_corner = CORNER_E; break;
330 case TRACK_BIT_UPPER: track_corner = CORNER_N; break;
331
332 case TRACK_BIT_HORZ:
333 if (tileh == SLOPE_N) return HalftileFoundation(CORNER_N);
334 if (tileh == SLOPE_S) return HalftileFoundation(CORNER_S);
335 return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
336
337 case TRACK_BIT_VERT:
338 if (tileh == SLOPE_W) return HalftileFoundation(CORNER_W);
339 if (tileh == SLOPE_E) return HalftileFoundation(CORNER_E);
340 return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
341
342 case TRACK_BIT_X:
343 if (IsSlopeWithOneCornerRaised(tileh)) return FOUNDATION_INCLINED_X;
344 return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
345
346 case TRACK_BIT_Y:
347 if (IsSlopeWithOneCornerRaised(tileh)) return FOUNDATION_INCLINED_Y;
348 return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
349
350 default:
351 return (valid_on_leveled ? FOUNDATION_LEVELED : FOUNDATION_INVALID);
352 }
353 /* Single diagonal track */
354
355 /* Track must be at least valid on leveled foundation */
356 if (!valid_on_leveled) return FOUNDATION_INVALID;
357
358 /* If slope has three raised corners, build leveled foundation */
359 if (IsSlopeWithThreeCornersRaised(tileh)) return FOUNDATION_LEVELED;
360
361 /* If neighboured corners of track_corner are lowered, build halftile foundation */
362 if ((tileh & SlopeWithThreeCornersRaised(OppositeCorner(track_corner))) == SlopeWithOneCornerRaised(track_corner)) return HalftileFoundation(track_corner);
363
364 /* else special anti-zig-zag foundation */
365 return SpecialRailFoundation(track_corner);
366 }
367 }
368
369
379 static CommandCost CheckRailSlope(Slope tileh, TrackBits rail_bits, TrackBits existing, TileIndex tile)
380 {
381 /* don't allow building on the lower side of a coast */
382 if (GetFloodingBehaviour(tile) != FLOOD_NONE) {
383 if (!IsSteepSlope(tileh) && ((~_valid_tracks_on_leveled_foundation[tileh] & (rail_bits | existing)) != 0)) return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
384 }
385
386 Foundation f_new = GetRailFoundation(tileh, rail_bits | existing);
387
388 /* check track/slope combination */
389 if ((f_new == FOUNDATION_INVALID) ||
390 ((f_new != FOUNDATION_NONE) && (!_settings_game.construction.build_on_slopes))) {
391 return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
392 }
393
394 Foundation f_old = GetRailFoundation(tileh, existing);
395 return CommandCost(EXPENSES_CONSTRUCTION, f_new != f_old ? _price[PR_BUILD_FOUNDATION] : (Money)0);
396 }
397
398 /* Validate functions for rail building */
399 static inline bool ValParamTrackOrientation(Track track)
400 {
401 return IsValidTrack(track);
402 }
403
413 CommandCost CmdBuildSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
414 {
415 RailType railtype = Extract<RailType, 0, 4>(p1);
416 Track track = Extract<Track, 0, 3>(p2);
417 CommandCost cost(EXPENSES_CONSTRUCTION);
418
419 if (!ValParamRailtype(railtype) || !ValParamTrackOrientation(track)) return CMD_ERROR;
420
421 Slope tileh = GetTileSlope(tile);
422 TrackBits trackbit = TrackToTrackBits(track);
423
424 switch (GetTileType(tile)) {
425 case MP_RAILWAY: {
426 CommandCost ret = CheckTileOwnership(tile);
427 if (ret.Failed()) return ret;
428
429 if (!IsPlainRail(tile)) return CMD_ERROR;
430
431 if (!IsCompatibleRail(GetRailType(tile), railtype)) return_cmd_error(STR_ERROR_IMPOSSIBLE_TRACK_COMBINATION);
432
433 ret = CheckTrackCombination(tile, trackbit, flags);
434 if (ret.Succeeded()) ret = EnsureNoTrainOnTrack(tile, track);
435 if (ret.Failed()) return ret;
436
437 ret = CheckRailSlope(tileh, trackbit, GetTrackBits(tile), tile);
438 if (ret.Failed()) return ret;
439 cost.AddCost(ret);
440
441 /* If the rail types don't match, try to convert only if engines of
442 * the new rail type are not powered on the present rail type and engines of
443 * the present rail type are powered on the new rail type. */
444 if (GetRailType(tile) != railtype && !HasPowerOnRail(railtype, GetRailType(tile))) {
445 if (HasPowerOnRail(GetRailType(tile), railtype)) {
446 ret = DoCommand(tile, tile, railtype, flags, CMD_CONVERT_RAIL);
447 if (ret.Failed()) return ret;
448 cost.AddCost(ret);
449 } else {
450 return CMD_ERROR;
451 }
452 }
453
454 if (flags & DC_EXEC) {
455 SetRailGroundType(tile, RAIL_GROUND_BARREN);
456 TrackBits bits = GetTrackBits(tile);
457 SetTrackBits(tile, bits | trackbit);
458 /* Subtract old infrastructure count. */
459 uint pieces = CountBits(bits);
460 if (TracksOverlap(bits)) pieces *= pieces;
461 Company::Get(GetTileOwner(tile))->infrastructure.rail[GetRailType(tile)] -= pieces;
462 /* Add new infrastructure count. */
463 pieces = CountBits(bits | trackbit);
464 if (TracksOverlap(bits | trackbit)) pieces *= pieces;
465 Company::Get(GetTileOwner(tile))->infrastructure.rail[GetRailType(tile)] += pieces;
466 DirtyCompanyInfrastructureWindows(GetTileOwner(tile));
467 }
468 break;
469 }
470
471 case MP_ROAD: {
472 /* Level crossings may only be built on these slopes */
473 if (!HasBit(VALID_LEVEL_CROSSING_SLOPES, tileh)) return_cmd_error(STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
474
475 CommandCost ret = EnsureNoVehicleOnGround(tile);
476 if (ret.Failed()) return ret;
477
478 if (IsNormalRoad(tile)) {
479 if (HasRoadWorks(tile)) return_cmd_error(STR_ERROR_ROAD_WORKS_IN_PROGRESS);
480
481 if (GetDisallowedRoadDirections(tile) != DRD_NONE) return_cmd_error(STR_ERROR_CROSSING_ON_ONEWAY_ROAD);
482
483 if (RailNoLevelCrossings(railtype)) return_cmd_error(STR_ERROR_CROSSING_DISALLOWED);
484
485 RoadTypes roadtypes = GetRoadTypes(tile);
486 RoadBits road = GetRoadBits(tile, ROADTYPE_ROAD);
487 RoadBits tram = GetRoadBits(tile, ROADTYPE_TRAM);
488 if ((track == TRACK_X && (road | tram) == ROAD_Y) ||
489 (track == TRACK_Y && (road | tram) == ROAD_X)) {
490 switch (roadtypes) {
491 default: break;
492 case ROADTYPES_TRAM:
493 /* Tram crossings must always have road. */
494 if (flags & DC_EXEC) {
495 SetRoadOwner(tile, ROADTYPE_ROAD, _current_company);
496 Company *c = Company::GetIfValid(_current_company);
497 if (c != NULL) {
498 /* A full diagonal tile has two road bits. */
499 c->infrastructure.road[ROADTYPE_ROAD] += 2;
500 DirtyCompanyInfrastructureWindows(c->index);
501 }
502 }
503 roadtypes |= ROADTYPES_ROAD;
504 cost.AddCost(2 * _price[PR_BUILD_ROAD]);
505 break;
506
507 case ROADTYPES_ALL:
508 if (road != tram) return CMD_ERROR;
509 break;
510 }
511
512 road |= tram;
513
514 if (flags & DC_EXEC) {
515 MakeRoadCrossing(tile, GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM), _current_company, (track == TRACK_X ? AXIS_Y : AXIS_X), railtype, roadtypes, GetTownIndex(tile));
516 UpdateLevelCrossing(tile, false);
517 Company::Get(_current_company)->infrastructure.rail[railtype] += LEVELCROSSING_TRACKBIT_FACTOR;
518 DirtyCompanyInfrastructureWindows(_current_company);
519 }
520 break;
521 }
522 }
523
524 if (IsLevelCrossing(tile) && GetCrossingRailBits(tile) == trackbit) {
525 return_cmd_error(STR_ERROR_ALREADY_BUILT);
526 }
527 /* FALL THROUGH */
528 }
529
530 default: {
531 /* Will there be flat water on the lower halftile? */
532 bool water_ground = IsTileType(tile, MP_WATER) && IsSlopeWithOneCornerRaised(tileh);
533
534 CommandCost ret = CheckRailSlope(tileh, trackbit, TRACK_BIT_NONE, tile);
535 if (ret.Failed()) return ret;
536 cost.AddCost(ret);
537
538 ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
539 if (ret.Failed()) return ret;
540 cost.AddCost(ret);
541
542 if (water_ground) {
543 cost.AddCost(-_price[PR_CLEAR_WATER]);
544 cost.AddCost(_price[PR_CLEAR_ROUGH]);
545 }
546
547 if (flags & DC_EXEC) {
548 MakeRailNormal(tile, _current_company, trackbit, railtype);
549 if (water_ground) SetRailGroundType(tile, RAIL_GROUND_WATER);
550 Company::Get(_current_company)->infrastructure.rail[railtype]++;
551 DirtyCompanyInfrastructureWindows(_current_company);
552 }
553 break;
554 }
555 }
556
557 if (flags & DC_EXEC) {
558 MarkTileDirtyByTile(tile);
559 AddTrackToSignalBuffer(tile, track, _current_company);
560 YapfNotifyTrackLayoutChange(tile, track);
561 }
562
563 cost.AddCost(RailBuildCost(railtype));
564 return cost;
565 }
566
576 CommandCost CmdRemoveSingleRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
577 {
578 Track track = Extract<Track, 0, 3>(p2);
579 CommandCost cost(EXPENSES_CONSTRUCTION);
580 bool crossing = false;
581
582 if (!ValParamTrackOrientation(track)) return CMD_ERROR;
583 TrackBits trackbit = TrackToTrackBits(track);
584
585 /* Need to read tile owner now because it may change when the rail is removed
586 * Also, in case of floods, _current_company != owner
587 * There may be invalid tiletype even in exec run (when removing long track),
588 * so do not call GetTileOwner(tile) in any case here */
589 Owner owner = INVALID_OWNER;
590
591 Train *v = NULL;
592
593 switch (GetTileType(tile)) {
594 case MP_ROAD: {
595 if (!IsLevelCrossing(tile) || GetCrossingRailBits(tile) != trackbit) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
596
597 if (_current_company != OWNER_WATER) {
598 CommandCost ret = CheckTileOwnership(tile);
599 if (ret.Failed()) return ret;
600 }
601
602 if (!(flags & DC_BANKRUPT)) {
603 CommandCost ret = EnsureNoVehicleOnGround(tile);
604 if (ret.Failed()) return ret;
605 }
606
607 cost.AddCost(RailClearCost(GetRailType(tile)));
608
609 if (flags & DC_EXEC) {
610 if (HasReservedTracks(tile, trackbit)) {
611 v = GetTrainForReservation(tile, track);
612 if (v != NULL) FreeTrainTrackReservation(v);
613 }
614 owner = GetTileOwner(tile);
615 Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= LEVELCROSSING_TRACKBIT_FACTOR;
616 DirtyCompanyInfrastructureWindows(owner);
617 MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypes(tile), GetTownIndex(tile), GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM));
618 DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile);
619 }
620 break;
621 }
622
623 case MP_RAILWAY: {
624 TrackBits present;
625 /* There are no rails present at depots. */
626 if (!IsPlainRail(tile)) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
627
628 if (_current_company != OWNER_WATER) {
629 CommandCost ret = CheckTileOwnership(tile);
630 if (ret.Failed()) return ret;
631 }
632
633 CommandCost ret = EnsureNoTrainOnTrack(tile, track);
634 if (ret.Failed()) return ret;
635
636 present = GetTrackBits(tile);
637 if ((present & trackbit) == 0) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
638 if (present == (TRACK_BIT_X | TRACK_BIT_Y)) crossing = true;
639
640 cost.AddCost(RailClearCost(GetRailType(tile)));
641
642 /* Charge extra to remove signals on the track, if they are there */
643 if (HasSignalOnTrack(tile, track)) {
644 cost.AddCost(DoCommand(tile, track, 0, flags, CMD_REMOVE_SIGNALS));
645 }
646
647 if (flags & DC_EXEC) {
648 if (HasReservedTracks(tile, trackbit)) {
649 v = GetTrainForReservation(tile, track);
650 if (v != NULL) FreeTrainTrackReservation(v);
651 }
652
653 owner = GetTileOwner(tile);
654
655 /* Subtract old infrastructure count. */
656 uint pieces = CountBits(present);
657 if (TracksOverlap(present)) pieces *= pieces;
658 Company::Get(owner)->infrastructure.rail[GetRailType(tile)] -= pieces;
659 /* Add new infrastructure count. */
660 present ^= trackbit;
661 pieces = CountBits(present);
662 if (TracksOverlap(present)) pieces *= pieces;
663 Company::Get(owner)->infrastructure.rail[GetRailType(tile)] += pieces;
664 DirtyCompanyInfrastructureWindows(owner);
665
666 if (present == 0) {
667 Slope tileh = GetTileSlope(tile);
668 /* If there is flat water on the lower halftile, convert the tile to shore so the water remains */
669 if (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh)) {
670 MakeShore(tile);
671 } else {
672 DoClearSquare(tile);
673 }
674 DeleteNewGRFInspectWindow(GSF_RAILTYPES, tile);
675 } else {
676 SetTrackBits(tile, present);
677 SetTrackReservation(tile, GetRailReservationTrackBits(tile) & present);
678 }
679 }
680 break;
681 }
682
683 default: return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
684 }
685
686 if (flags & DC_EXEC) {
687 /* if we got that far, 'owner' variable is set correctly */
688 assert(Company::IsValidID(owner));
689
690 MarkTileDirtyByTile(tile);
691 if (crossing) {
692 /* crossing is set when only TRACK_BIT_X and TRACK_BIT_Y are set. As we
693 * are removing one of these pieces, we'll need to update signals for
694 * both directions explicitly, as after the track is removed it won't
695 * 'connect' with the other piece. */
696 AddTrackToSignalBuffer(tile, TRACK_X, owner);
697 AddTrackToSignalBuffer(tile, TRACK_Y, owner);
698 YapfNotifyTrackLayoutChange(tile, TRACK_X);
699 YapfNotifyTrackLayoutChange(tile, TRACK_Y);
700 } else {
701 AddTrackToSignalBuffer(tile, track, owner);
702 YapfNotifyTrackLayoutChange(tile, track);
703 }
704
705 if (v != NULL) TryPathReserve(v, true);
706 }
707
708 return cost;
709 }
710
711
719 bool FloodHalftile(TileIndex t)
720 {
721 assert(IsPlainRailTile(t));
722
723 bool flooded = false;
724 if (GetRailGroundType(t) == RAIL_GROUND_WATER) return flooded;
725
726 Slope tileh = GetTileSlope(t);
727 TrackBits rail_bits = GetTrackBits(t);
728
729 if (IsSlopeWithOneCornerRaised(tileh)) {
730 TrackBits lower_track = CornerToTrackBits(OppositeCorner(GetHighestSlopeCorner(tileh)));
731
732 TrackBits to_remove = lower_track & rail_bits;
733 if (to_remove != 0) {
734 Backup<CompanyByte> cur_company(_current_company, OWNER_WATER, FILE_LINE);
735 flooded = DoCommand(t, 0, FIND_FIRST_BIT(to_remove), DC_EXEC, CMD_REMOVE_SINGLE_RAIL).Succeeded();
736 cur_company.Restore();
737 if (!flooded) return flooded; // not yet floodable
738 rail_bits = rail_bits & ~to_remove;
739 if (rail_bits == 0) {
740 MakeShore(t);
741 MarkTileDirtyByTile(t);
742 return flooded;
743 }
744 }
745
746 if (IsNonContinuousFoundation(GetRailFoundation(tileh, rail_bits))) {
747 flooded = true;
748 SetRailGroundType(t, RAIL_GROUND_WATER);
749 MarkTileDirtyByTile(t);
750 }
751 } else {
752 /* Make shore on steep slopes and 'three-corners-raised'-slopes. */
753 if (ApplyFoundationToSlope(GetRailFoundation(tileh, rail_bits), &tileh) == 0) {
754 if (IsSteepSlope(tileh) || IsSlopeWithThreeCornersRaised(tileh)) {
755 flooded = true;
756 SetRailGroundType(t, RAIL_GROUND_WATER);
757 MarkTileDirtyByTile(t);
758 }
759 }
760 }
761 return flooded;
762 }
763
764 static const TileIndexDiffC _trackdelta[] = {
765 { -1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, 1 },
766 { 0, 0 },
767 { 0, 0 },
768 { 1, 0 }, { 0, -1 }, { 0, -1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
769 { 0, 0 },
770 { 0, 0 }
771 };
772
773
774 static CommandCost ValidateAutoDrag(Trackdir *trackdir, TileIndex start, TileIndex end)
775 {
776 int x = TileX(start);
777 int y = TileY(start);
778 int ex = TileX(end);
779 int ey = TileY(end);
780
781 if (!ValParamTrackOrientation(TrackdirToTrack(*trackdir))) return CMD_ERROR;
782
783 /* calculate delta x,y from start to end tile */
784 int dx = ex - x;
785 int dy = ey - y;
786
787 /* calculate delta x,y for the first direction */
788 int trdx = _trackdelta[*trackdir].x;
789 int trdy = _trackdelta[*trackdir].y;
790
791 if (!IsDiagonalTrackdir(*trackdir)) {
792 trdx += _trackdelta[*trackdir ^ 1].x;
793 trdy += _trackdelta[*trackdir ^ 1].y;
794 }
795
796 /* validate the direction */
797 while ((trdx <= 0 && dx > 0) ||
798 (trdx >= 0 && dx < 0) ||
799 (trdy <= 0 && dy > 0) ||
800 (trdy >= 0 && dy < 0)) {
801 if (!HasBit(*trackdir, 3)) { // first direction is invalid, try the other
802 SetBit(*trackdir, 3); // reverse the direction
803 trdx = -trdx;
804 trdy = -trdy;
805 } else { // other direction is invalid too, invalid drag
806 return CMD_ERROR;
807 }
808 }
809
810 /* (for diagonal tracks, this is already made sure of by above test), but:
811 * for non-diagonal tracks, check if the start and end tile are on 1 line */
812 if (!IsDiagonalTrackdir(*trackdir)) {
813 trdx = _trackdelta[*trackdir].x;
814 trdy = _trackdelta[*trackdir].y;
815 if (abs(dx) != abs(dy) && abs(dx) + abs(trdy) != abs(dy) + abs(trdx)) return CMD_ERROR;
816 }
817
818 return CommandCost();
819 }
820
834 static CommandCost CmdRailTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
835 {
836 CommandCost total_cost(EXPENSES_CONSTRUCTION);
837 Track track = Extract<Track, 4, 3>(p2);
838 bool remove = HasBit(p2, 7);
839 RailType railtype = Extract<RailType, 0, 4>(p2);
840
841 if ((!remove && !ValParamRailtype(railtype)) || !ValParamTrackOrientation(track)) return CMD_ERROR;
842 if (p1 >= MapSize()) return CMD_ERROR;
843 TileIndex end_tile = p1;
844 Trackdir trackdir = TrackToTrackdir(track);
845
846 CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
847 if (ret.Failed()) return ret;
848
849 bool had_success = false;
850 CommandCost last_error = CMD_ERROR;
851 for (;;) {
852 CommandCost ret = DoCommand(tile, remove ? 0 : railtype, TrackdirToTrack(trackdir), flags, remove ? CMD_REMOVE_SINGLE_RAIL : CMD_BUILD_SINGLE_RAIL);
853
854 if (ret.Failed()) {
855 last_error = ret;
856 if (last_error.GetErrorMessage() != STR_ERROR_ALREADY_BUILT && !remove) {
857 if (HasBit(p2, 8)) return last_error;
858 break;
859 }
860
861 /* Ownership errors are more important. */
862 if (last_error.GetErrorMessage() == STR_ERROR_OWNED_BY && remove) break;
863 } else {
864 had_success = true;
865 total_cost.AddCost(ret);
866 }
867
868 if (tile == end_tile) break;
869
870 tile += ToTileIndexDiff(_trackdelta[trackdir]);
871
872 /* toggle railbit for the non-diagonal tracks */
873 if (!IsDiagonalTrackdir(trackdir)) ToggleBit(trackdir, 0);
874 }
875
876 if (had_success) return total_cost;
877 return last_error;
878 }
879
894 CommandCost CmdBuildRailroadTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
895 {
896 return CmdRailTrackHelper(tile, flags, p1, ClrBit(p2, 7), text);
897 }
898
913 CommandCost CmdRemoveRailroadTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
914 {
915 return CmdRailTrackHelper(tile, flags, p1, SetBit(p2, 7), text);
916 }
917
930 CommandCost CmdBuildTrainDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
931 {
932 /* check railtype and valid direction for depot (0 through 3), 4 in total */
933 RailType railtype = Extract<RailType, 0, 4>(p1);
934 if (!ValParamRailtype(railtype)) return CMD_ERROR;
935
936 Slope tileh = GetTileSlope(tile);
937
938 DiagDirection dir = Extract<DiagDirection, 0, 2>(p2);
939
940 /* Prohibit construction if
941 * The tile is non-flat AND
942 * 1) build-on-slopes is disabled
943 * 2) the tile is steep i.e. spans two height levels
944 * 3) the exit points in the wrong direction
945 */
946
947 if (tileh != SLOPE_FLAT && (
948 !_settings_game.construction.build_on_slopes ||
949 !CanBuildDepotByTileh(dir, tileh)
950 )) {
951 return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
952 }
953
954 CommandCost cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
955 if (cost.Failed()) return cost;
956
957 if (IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
958
959 if (!Depot::CanAllocateItem()) return CMD_ERROR;
960
961 if (flags & DC_EXEC) {
962 Depot *d = new Depot(tile);
963 d->build_date = _date;
964
965 MakeRailDepot(tile, _current_company, d->index, dir, railtype);
966 MarkTileDirtyByTile(tile);
967 MakeDefaultName(d);
968
969 Company::Get(_current_company)->infrastructure.rail[railtype]++;
970 DirtyCompanyInfrastructureWindows(_current_company);
971
972 AddSideToSignalBuffer(tile, INVALID_DIAGDIR, _current_company);
973 YapfNotifyTrackLayoutChange(tile, DiagDirToDiagTrack(dir));
974 }
975
976 cost.AddCost(_price[PR_BUILD_DEPOT_TRAIN]);
977 cost.AddCost(RailBuildCost(railtype));
978 return cost;
979 }
980
1002 CommandCost CmdBuildSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1003 {
1004 Track track = Extract<Track, 0, 3>(p1);
1005 bool ctrl_pressed = HasBit(p1, 3); // was the CTRL button pressed
1006 SignalVariant sigvar = (ctrl_pressed ^ HasBit(p1, 4)) ? SIG_SEMAPHORE : SIG_ELECTRIC; // the signal variant of the new signal
1007 SignalType sigtype = Extract<SignalType, 5, 3>(p1); // the signal type of the new signal
1008 bool convert_signal = HasBit(p1, 8); // convert button pressed
1009 SignalType cycle_start = Extract<SignalType, 9, 3>(p1);
1010 SignalType cycle_stop = Extract<SignalType, 12, 3>(p1);
1011 uint num_dir_cycle = GB(p1, 15, 2);
1012
1013 if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
1014 if (cycle_start > cycle_stop || cycle_stop > SIGTYPE_LAST) return CMD_ERROR;
1015
1016 /* You can only build signals on plain rail tiles, and the selected track must exist */
1017 if (!ValParamTrackOrientation(track) || !IsPlainRailTile(tile) ||
1018 !HasTrack(tile, track)) {
1019 return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
1020 }
1021 /* Protect against invalid signal copying */
1022 if (p2 != 0 && (p2 & SignalOnTrack(track)) == 0) return CMD_ERROR;
1023
1024 CommandCost ret = CheckTileOwnership(tile);
1025 if (ret.Failed()) return ret;
1026
1027 /* See if this is a valid track combination for signals (no overlap) */
1028 if (TracksOverlap(GetTrackBits(tile))) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
1029
1030 /* In case we don't want to change an existing signal, return without error. */
1031 if (HasBit(p1, 17) && HasSignalOnTrack(tile, track)) return CommandCost();
1032
1033 /* you can not convert a signal if no signal is on track */
1034 if (convert_signal && !HasSignalOnTrack(tile, track)) return_cmd_error(STR_ERROR_THERE_ARE_NO_SIGNALS);
1035
1036 CommandCost cost;
1037 if (!HasSignalOnTrack(tile, track)) {
1038 /* build new signals */
1039 cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS]);
1040 } else {
1041 if (p2 != 0 && sigvar != GetSignalVariant(tile, track)) {
1042 /* convert signals <-> semaphores */
1043 cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS] + _price[PR_CLEAR_SIGNALS]);
1044
1045 } else if (convert_signal) {
1046 /* convert button pressed */
1047 if (ctrl_pressed || GetSignalVariant(tile, track) != sigvar) {
1048 /* convert electric <-> semaphore */
1049 cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS] + _price[PR_CLEAR_SIGNALS]);
1050 } else {
1051 /* it is free to change signal type: normal-pre-exit-combo */
1052 cost = CommandCost();
1053 }
1054
1055 } else {
1056 /* it is free to change orientation/pre-exit-combo signals */
1057 cost = CommandCost();
1058 }
1059 }
1060
1061 if (flags & DC_EXEC) {
1062 Train *v = NULL;
1063 /* The new/changed signal could block our path. As this can lead to
1064 * stale reservations, we clear the path reservation here and try
1065 * to redo it later on. */
1066 if (HasReservedTracks(tile, TrackToTrackBits(track))) {
1067 v = GetTrainForReservation(tile, track);
1068 if (v != NULL) FreeTrainTrackReservation(v);
1069 }
1070
1071 if (!HasSignals(tile)) {
1072 /* there are no signals at all on this tile yet */
1073 SetHasSignals(tile, true);
1074 SetSignalStates(tile, 0xF); // all signals are on
1075 SetPresentSignals(tile, 0); // no signals built by default
1076 SetSignalType(tile, track, sigtype);
1077 SetSignalVariant(tile, track, sigvar);
1078 }
1079
1080 /* Subtract old signal infrastructure count. */
1081 Company::Get(GetTileOwner(tile))->infrastructure.signal -= CountBits(GetPresentSignals(tile));
1082
1083 if (p2 == 0) {
1084 if (!HasSignalOnTrack(tile, track)) {
1085 /* build new signals */
1086 SetPresentSignals(tile, GetPresentSignals(tile) | (IsPbsSignal(sigtype) ? KillFirstBit(SignalOnTrack(track)) : SignalOnTrack(track)));
1087 SetSignalType(tile, track, sigtype);
1088 SetSignalVariant(tile, track, sigvar);
1089 while (num_dir_cycle-- > 0) CycleSignalSide(tile, track);
1090 } else {
1091 if (convert_signal) {
1092 /* convert signal button pressed */
1093 if (ctrl_pressed) {
1094 /* toggle the present signal variant: SIG_ELECTRIC <-> SIG_SEMAPHORE */
1095 SetSignalVariant(tile, track, (GetSignalVariant(tile, track) == SIG_ELECTRIC) ? SIG_SEMAPHORE : SIG_ELECTRIC);
1096 /* Query current signal type so the check for PBS signals below works. */
1097 sigtype = GetSignalType(tile, track);
1098 } else {
1099 /* convert the present signal to the chosen type and variant */
1100 SetSignalType(tile, track, sigtype);
1101 SetSignalVariant(tile, track, sigvar);
1102 if (IsPbsSignal(sigtype) && (GetPresentSignals(tile) & SignalOnTrack(track)) == SignalOnTrack(track)) {
1103 SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | KillFirstBit(SignalOnTrack(track)));
1104 }
1105 }
1106
1107 } else if (ctrl_pressed) {
1108 /* cycle between cycle_start and cycle_end */
1109 sigtype = (SignalType)(GetSignalType(tile, track) + 1);
1110
1111 if (sigtype < cycle_start || sigtype > cycle_stop) sigtype = cycle_start;
1112
1113 SetSignalType(tile, track, sigtype);
1114 if (IsPbsSignal(sigtype) && (GetPresentSignals(tile) & SignalOnTrack(track)) == SignalOnTrack(track)) {
1115 SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | KillFirstBit(SignalOnTrack(track)));
1116 }
1117 } else {
1118 /* cycle the signal side: both -> left -> right -> both -> ... */
1119 CycleSignalSide(tile, track);
1120 /* Query current signal type so the check for PBS signals below works. */
1121 sigtype = GetSignalType(tile, track);
1122 }
1123 }
1124 } else {
1125 /* If CmdBuildManySignals is called with copying signals, just copy the
1126 * direction of the first signal given as parameter by CmdBuildManySignals */
1127 SetPresentSignals(tile, (GetPresentSignals(tile) & ~SignalOnTrack(track)) | (p2 & SignalOnTrack(track)));
1128 SetSignalVariant(tile, track, sigvar);
1129 SetSignalType(tile, track, sigtype);
1130 }
1131
1132 /* Add new signal infrastructure count. */
1133 Company::Get(GetTileOwner(tile))->infrastructure.signal += CountBits(GetPresentSignals(tile));
1134 DirtyCompanyInfrastructureWindows(GetTileOwner(tile));
1135
1136 if (IsPbsSignal(sigtype)) {
1137 /* PBS signals should show red unless they are on reserved tiles without a train. */
1138 uint mask = GetPresentSignals(tile) & SignalOnTrack(track);
1139 SetSignalStates(tile, (GetSignalStates(tile) & ~mask) | ((HasBit(GetRailReservationTrackBits(tile), track) && EnsureNoVehicleOnGround(tile).Succeeded() ? UINT_MAX : 0) & mask));
1140 }
1141 MarkTileDirtyByTile(tile);
1142 AddTrackToSignalBuffer(tile, track, _current_company);
1143 YapfNotifyTrackLayoutChange(tile, track);
1144 if (v != NULL) {
1145 /* Extend the train's path if it's not stopped or loading, or not at a safe position. */
1146 if (!(((v->vehstatus & VS_STOPPED) && v->cur_speed == 0) || v->current_order.IsType(OT_LOADING)) ||
1147 !IsSafeWaitingPosition(v, v->tile, v->GetVehicleTrackdir(), true, _settings_game.pf.forbid_90_deg)) {
1148 TryPathReserve(v, true);
1149 }
1150 }
1151 }
1152
1153 return cost;
1154 }
1155
1156 static bool CheckSignalAutoFill(TileIndex &tile, Trackdir &trackdir, int &signal_ctr, bool remove)
1157 {
1158 tile = AddTileIndexDiffCWrap(tile, _trackdelta[trackdir]);
1159 if (tile == INVALID_TILE) return false;
1160
1161 /* Check for track bits on the new tile */
1162 TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
1163
1164 if (TracksOverlap(TrackdirBitsToTrackBits(trackdirbits))) return false;
1165 trackdirbits &= TrackdirReachesTrackdirs(trackdir);
1166
1167 /* No track bits, must stop */
1168 if (trackdirbits == TRACKDIR_BIT_NONE) return false;
1169
1170 /* Get the first track dir */
1171 trackdir = RemoveFirstTrackdir(&trackdirbits);
1172
1173 /* Any left? It's a junction so we stop */
1174 if (trackdirbits != TRACKDIR_BIT_NONE) return false;
1175
1176 switch (GetTileType(tile)) {
1177 case MP_RAILWAY:
1178 if (IsRailDepot(tile)) return false;
1179 if (!remove && HasSignalOnTrack(tile, TrackdirToTrack(trackdir))) return false;
1180 signal_ctr++;
1181 if (IsDiagonalTrackdir(trackdir)) {
1182 signal_ctr++;
1183 /* Ensure signal_ctr even so X and Y pieces get signals */
1184 ClrBit(signal_ctr, 0);
1185 }
1186 return true;
1187
1188 case MP_ROAD:
1189 if (!IsLevelCrossing(tile)) return false;
1190 signal_ctr += 2;
1191 return true;
1192
1193 case MP_TUNNELBRIDGE: {
1194 TileIndex orig_tile = tile; // backup old value
1195
1196 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) return false;
1197 if (GetTunnelBridgeDirection(tile) != TrackdirToExitdir(trackdir)) return false;
1198
1199 /* Skip to end of tunnel or bridge
1200 * note that tile is a parameter by reference, so it must be updated */
1201 tile = GetOtherTunnelBridgeEnd(tile);
1202
1203 signal_ctr += (GetTunnelBridgeLength(orig_tile, tile) + 2) * 2;
1204 return true;
1205 }
1206
1207 default: return false;
1208 }
1209 }
1210
1228 static CommandCost CmdSignalTrackHelper(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1229 {
1230 CommandCost total_cost(EXPENSES_CONSTRUCTION);
1231 TileIndex start_tile = tile;
1232
1233 Track track = Extract<Track, 0, 3>(p2);
1234 bool mode = HasBit(p2, 3);
1235 bool semaphores = HasBit(p2, 4);
1236 bool remove = HasBit(p2, 5);
1237 bool autofill = HasBit(p2, 6);
1238 bool minimise_gaps = HasBit(p2, 10);
1239 byte signal_density = GB(p2, 24, 8);
1240
1241 if (p1 >= MapSize() || !ValParamTrackOrientation(track)) return CMD_ERROR;
1242 TileIndex end_tile = p1;
1243 if (signal_density == 0 || signal_density > 20) return CMD_ERROR;
1244
1245 if (!IsPlainRailTile(tile)) return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
1246
1247 /* for vertical/horizontal tracks, double the given signals density
1248 * since the original amount will be too dense (shorter tracks) */
1249 signal_density *= 2;
1250
1251 Trackdir trackdir = TrackToTrackdir(track);
1252 CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
1253 if (ret.Failed()) return ret;
1254
1255 track = TrackdirToTrack(trackdir); // trackdir might have changed, keep track in sync
1256 Trackdir start_trackdir = trackdir;
1257
1258 /* Must start on a valid track to be able to avoid loops */
1259 if (!HasTrack(tile, track)) return CMD_ERROR;
1260
1261 SignalType sigtype = (SignalType)GB(p2, 7, 3);
1262 if (sigtype > SIGTYPE_LAST) return CMD_ERROR;
1263
1264 byte signals;
1265 /* copy the signal-style of the first rail-piece if existing */
1266 if (HasSignalOnTrack(tile, track)) {
1267 signals = GetPresentSignals(tile) & SignalOnTrack(track);
1268 assert(signals != 0);
1269
1270 /* copy signal/semaphores style (independent of CTRL) */
1271 semaphores = GetSignalVariant(tile, track) != SIG_ELECTRIC;
1272
1273 sigtype = GetSignalType(tile, track);
1274 /* Don't but copy entry or exit-signal type */
1275 if (sigtype == SIGTYPE_ENTRY || sigtype == SIGTYPE_EXIT) sigtype = SIGTYPE_NORMAL;
1276 } else { // no signals exist, drag a two-way signal stretch
1277 signals = IsPbsSignal(sigtype) ? SignalAlongTrackdir(trackdir) : SignalOnTrack(track);
1278 }
1279
1280 byte signal_dir = 0;
1281 if (signals & SignalAlongTrackdir(trackdir)) SetBit(signal_dir, 0);
1282 if (signals & SignalAgainstTrackdir(trackdir)) SetBit(signal_dir, 1);
1283
1284 /* signal_ctr - amount of tiles already processed
1285 * last_used_ctr - amount of tiles before previously placed signal
1286 * signals_density - setting to put signal on every Nth tile (double space on |, -- tracks)
1287 * last_suitable_ctr - amount of tiles before last possible signal place
1288 * last_suitable_tile - last tile where it is possible to place a signal
1289 * last_suitable_trackdir - trackdir of the last tile
1290 **********
1291 * trackdir - trackdir to build with autorail
1292 * semaphores - semaphores or signals
1293 * signals - is there a signal/semaphore on the first tile, copy its style (two-way/single-way)
1294 * and convert all others to semaphore/signal
1295 * remove - 1 remove signals, 0 build signals */
1296 int signal_ctr = 0;
1297 int last_used_ctr = INT_MIN; // initially INT_MIN to force building/removing at the first tile
1298 int last_suitable_ctr = 0;
1299 TileIndex last_suitable_tile = INVALID_TILE;
1300 Trackdir last_suitable_trackdir = INVALID_TRACKDIR;
1301 CommandCost last_error = CMD_ERROR;
1302 bool had_success = false;
1303 for (;;) {
1304 /* only build/remove signals with the specified density */
1305 if (remove || minimise_gaps || signal_ctr % signal_density == 0) {
1306 uint32 p1 = GB(TrackdirToTrack(trackdir), 0, 3);
1307 SB(p1, 3, 1, mode);
1308 SB(p1, 4, 1, semaphores);
1309 SB(p1, 5, 3, sigtype);
1310 if (!remove && signal_ctr == 0) SetBit(p1, 17);
1311
1312 /* Pick the correct orientation for the track direction */
1313 signals = 0;
1314 if (HasBit(signal_dir, 0)) signals |= SignalAlongTrackdir(trackdir);
1315 if (HasBit(signal_dir, 1)) signals |= SignalAgainstTrackdir(trackdir);
1316
1317 /* Test tiles in between for suitability as well if minimising gaps. */
1318 bool test_only = !remove && minimise_gaps && signal_ctr < (last_used_ctr + signal_density);
1319 CommandCost ret = DoCommand(tile, p1, signals, test_only ? flags & ~DC_EXEC : flags, remove ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
1320
1321 if (ret.Succeeded()) {
1322 /* Remember last track piece where we can place a signal. */
1323 last_suitable_ctr = signal_ctr;
1324 last_suitable_tile = tile;
1325 last_suitable_trackdir = trackdir;
1326 } else if (!test_only && last_suitable_tile != INVALID_TILE) {
1327 /* If a signal can't be placed, place it at the last possible position. */
1328 SB(p1, 0, 3, TrackdirToTrack(last_suitable_trackdir));
1329 ClrBit(p1, 17);
1330
1331 /* Pick the correct orientation for the track direction. */
1332 signals = 0;
1333 if (HasBit(signal_dir, 0)) signals |= SignalAlongTrackdir(last_suitable_trackdir);
1334 if (HasBit(signal_dir, 1)) signals |= SignalAgainstTrackdir(last_suitable_trackdir);
1335
1336 ret = DoCommand(last_suitable_tile, p1, signals, flags, remove ? CMD_REMOVE_SIGNALS : CMD_BUILD_SIGNALS);
1337 }
1338
1339 /* Collect cost. */
1340 if (!test_only) {
1341 /* Be user-friendly and try placing signals as much as possible */
1342 if (ret.Succeeded()) {
1343 had_success = true;
1344 total_cost.AddCost(ret);
1345 last_used_ctr = last_suitable_ctr;
1346 last_suitable_tile = INVALID_TILE;
1347 } else {
1348 /* The "No railway" error is the least important one. */
1349 if (ret.GetErrorMessage() != STR_ERROR_THERE_IS_NO_RAILROAD_TRACK ||
1350 last_error.GetErrorMessage() == INVALID_STRING_ID) {
1351 last_error = ret;
1352 }
1353 }
1354 }
1355 }
1356
1357 if (autofill) {
1358 if (!CheckSignalAutoFill(tile, trackdir, signal_ctr, remove)) break;
1359
1360 /* Prevent possible loops */
1361 if (tile == start_tile && trackdir == start_trackdir) break;
1362 } else {
1363 if (tile == end_tile) break;
1364
1365 tile += ToTileIndexDiff(_trackdelta[trackdir]);
1366 signal_ctr++;
1367
1368 /* toggle railbit for the non-diagonal tracks (|, -- tracks) */
1369 if (IsDiagonalTrackdir(trackdir)) {
1370 signal_ctr++;
1371 } else {
1372 ToggleBit(trackdir, 0);
1373 }
1374 }
1375 }
1376
1377 return had_success ? total_cost : last_error;
1378 }
1379
1398 CommandCost CmdBuildSignalTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1399 {
1400 return CmdSignalTrackHelper(tile, flags, p1, p2, text);
1401 }
1402
1415 CommandCost CmdRemoveSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1416 {
1417 Track track = Extract<Track, 0, 3>(p1);
1418
1419 if (!ValParamTrackOrientation(track) || !IsPlainRailTile(tile) || !HasTrack(tile, track)) {
1420 return_cmd_error(STR_ERROR_THERE_IS_NO_RAILROAD_TRACK);
1421 }
1422 if (!HasSignalOnTrack(tile, track)) {
1423 return_cmd_error(STR_ERROR_THERE_ARE_NO_SIGNALS);
1424 }
1425
1426 /* Only water can remove signals from anyone */
1427 if (_current_company != OWNER_WATER) {
1428 CommandCost ret = CheckTileOwnership(tile);
1429 if (ret.Failed()) return ret;
1430 }
1431
1432 /* Do it? */
1433 if (flags & DC_EXEC) {
1434 Train *v = NULL;
1435 if (HasReservedTracks(tile, TrackToTrackBits(track))) {
1436 v = GetTrainForReservation(tile, track);
1437 } else if (IsPbsSignal(GetSignalType(tile, track))) {
1438 /* PBS signal, might be the end of a path reservation. */
1439 Trackdir td = TrackToTrackdir(track);
1440 for (int i = 0; v == NULL && i < 2; i++, td = ReverseTrackdir(td)) {
1441 /* Only test the active signal side. */
1442 if (!HasSignalOnTrackdir(tile, ReverseTrackdir(td))) continue;
1443 TileIndex next = TileAddByDiagDir(tile, TrackdirToExitdir(td));
1444 TrackBits tracks = TrackdirBitsToTrackBits(TrackdirReachesTrackdirs(td));
1445 if (HasReservedTracks(next, tracks)) {
1446 v = GetTrainForReservation(next, TrackBitsToTrack(GetReservedTrackbits(next) & tracks));
1447 }
1448 }
1449 }
1450 Company::Get(GetTileOwner(tile))->infrastructure.signal -= CountBits(GetPresentSignals(tile));
1451 SetPresentSignals(tile, GetPresentSignals(tile) & ~SignalOnTrack(track));
1452 Company::Get(GetTileOwner(tile))->infrastructure.signal += CountBits(GetPresentSignals(tile));
1453 DirtyCompanyInfrastructureWindows(GetTileOwner(tile));
1454
1455 /* removed last signal from tile? */
1456 if (GetPresentSignals(tile) == 0) {
1457 SetSignalStates(tile, 0);
1458 SetHasSignals(tile, false);
1459 SetSignalVariant(tile, INVALID_TRACK, SIG_ELECTRIC); // remove any possible semaphores
1460 }
1461
1462 AddTrackToSignalBuffer(tile, track, GetTileOwner(tile));
1463 YapfNotifyTrackLayoutChange(tile, track);
1464 if (v != NULL) TryPathReserve(v, false);
1465
1466 MarkTileDirtyByTile(tile);
1467 }
1468
1469 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_SIGNALS]);
1470 }
1471
1490 CommandCost CmdRemoveSignalTrack(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1491 {
1492 return CmdSignalTrackHelper(tile, flags, p1, SetBit(p2, 5), text); // bit 5 is remove bit
1493 }
1494
1496 static Vehicle *UpdateTrainPowerProc(Vehicle *v, void *data)
1497 {
1498 if (v->type != VEH_TRAIN) return NULL;
1499
1500 TrainList *affected_trains = static_cast<TrainList*>(data);
1501 affected_trains->Include(Train::From(v)->First());
1502
1503 return NULL;
1504 }
1505
1518 CommandCost CmdConvertRail(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1519 {
1520 RailType totype = Extract<RailType, 0, 4>(p2);
1521 TileIndex area_start = p1;
1522 TileIndex area_end = tile;
1523 bool diagonal = HasBit(p2, 4);
1524
1525 if (!ValParamRailtype(totype)) return CMD_ERROR;
1526 if (area_start >= MapSize()) return CMD_ERROR;
1527
1528 TrainList affected_trains;
1529
1530 CommandCost cost(EXPENSES_CONSTRUCTION);
1531 CommandCost error = CommandCost(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK); // by default, there is no track to convert.
1532
1533 TileIterator *iter = diagonal ? (TileIterator *)new DiagonalTileIterator(area_start, area_end) : new OrthogonalTileIterator(area_start, area_end);
1534 for (; (tile = *iter) != INVALID_TILE; ++(*iter)) {
1535 TileType tt = GetTileType(tile);
1536
1537 /* Check if there is any track on tile */
1538 switch (tt) {
1539 case MP_RAILWAY:
1540 break;
1541 case MP_STATION:
1542 if (!HasStationRail(tile)) continue;
1543 break;
1544 case MP_ROAD:
1545 if (!IsLevelCrossing(tile)) continue;
1546 if (RailNoLevelCrossings(totype)) {
1547 error.MakeError(STR_ERROR_CROSSING_DISALLOWED);
1548 continue;
1549 }
1550 break;
1551 case MP_TUNNELBRIDGE:
1552 if (GetTunnelBridgeTransportType(tile) != TRANSPORT_RAIL) continue;
1553 break;
1554 default: continue;
1555 }
1556
1557 /* Original railtype we are converting from */
1558 RailType type = GetRailType(tile);
1559
1560 /* Converting to the same type or converting 'hidden' elrail -> rail */
1561 if (type == totype || (_settings_game.vehicle.disable_elrails && totype == RAILTYPE_RAIL && type == RAILTYPE_ELECTRIC)) continue;
1562
1563 /* Trying to convert other's rail */
1564 CommandCost ret = CheckTileOwnership(tile);
1565 if (ret.Failed()) {
1566 error = ret;
1567 continue;
1568 }
1569
1570 SmallVector<Train *, 2> vehicles_affected;
1571
1572 /* Vehicle on the tile when not converting Rail <-> ElRail
1573 * Tunnels and bridges have special check later */
1574 if (tt != MP_TUNNELBRIDGE) {
1575 if (!IsCompatibleRail(type, totype)) {
1576 CommandCost ret = EnsureNoVehicleOnGround(tile);
1577 if (ret.Failed()) {
1578 error = ret;
1579 continue;
1580 }
1581 }
1582 if (flags & DC_EXEC) { // we can safely convert, too
1583 TrackBits reserved = GetReservedTrackbits(tile);
1584 Track track;
1585 while ((track = RemoveFirstTrack(&reserved)) != INVALID_TRACK) {
1586 Train *v = GetTrainForReservation(tile, track);
1587 if (v != NULL && !HasPowerOnRail(v->railtype, totype)) {
1588 /* No power on new rail type, reroute. */
1589 FreeTrainTrackReservation(v);
1590 *vehicles_affected.Append() = v;
1591 }
1592 }
1593
1594 /* Update the company infrastructure counters. */
1595 if (!IsRailStationTile(tile) || !IsStationTileBlocked(tile)) {
1596 Company *c = Company::Get(GetTileOwner(tile));
1597 uint num_pieces = IsLevelCrossingTile(tile) ? LEVELCROSSING_TRACKBIT_FACTOR : 1;
1598 if (IsPlainRailTile(tile)) {
1599 TrackBits bits = GetTrackBits(tile);
1600 num_pieces = CountBits(bits);
1601 if (TracksOverlap(bits)) num_pieces *= num_pieces;
1602 }
1603 c->infrastructure.rail[type] -= num_pieces;
1604 c->infrastructure.rail[totype] += num_pieces;
1605 DirtyCompanyInfrastructureWindows(c->index);
1606 }
1607
1608 SetRailType(tile, totype);
1609 MarkTileDirtyByTile(tile);
1610 /* update power of train on this tile */
1611 FindVehicleOnPos(tile, &affected_trains, &UpdateTrainPowerProc);
1612 }
1613 }
1614
1615 switch (tt) {
1616 case MP_RAILWAY:
1617 switch (GetRailTileType(tile)) {
1618 case RAIL_TILE_DEPOT:
1619 if (flags & DC_EXEC) {
1620 /* notify YAPF about the track layout change */
1621 YapfNotifyTrackLayoutChange(tile, GetRailDepotTrack(tile));
1622
1623 /* Update build vehicle window related to this depot */
1624 InvalidateWindowData(WC_VEHICLE_DEPOT, tile);
1625 InvalidateWindowData(WC_BUILD_VEHICLE, tile);
1626 }
1627 cost.AddCost(RailConvertCost(type, totype));
1628 break;
1629
1630 default: // RAIL_TILE_NORMAL, RAIL_TILE_SIGNALS
1631 if (flags & DC_EXEC) {
1632 /* notify YAPF about the track layout change */
1633 TrackBits tracks = GetTrackBits(tile);
1634 while (tracks != TRACK_BIT_NONE) {
1635 YapfNotifyTrackLayoutChange(tile, RemoveFirstTrack(&tracks));
1636 }
1637 }
1638 cost.AddCost(RailConvertCost(type, totype) * CountBits(GetTrackBits(tile)));
1639 break;
1640 }
1641 break;
1642
1643 case MP_TUNNELBRIDGE: {
1644 TileIndex endtile = GetOtherTunnelBridgeEnd(tile);
1645
1646 /* If both ends of tunnel/bridge are in the range, do not try to convert twice -
1647 * it would cause assert because of different test and exec runs */
1648 if (endtile < tile) {
1649 if (diagonal) {
1650 if (DiagonalTileArea(area_start, area_end).Contains(endtile)) continue;
1651 } else {
1652 if (OrthogonalTileArea(area_start, area_end).Contains(endtile)) continue;
1653 }
1654 }
1655
1656 /* When not converting rail <-> el. rail, any vehicle cannot be in tunnel/bridge */
1657 if (!IsCompatibleRail(GetRailType(tile), totype)) {
1658 CommandCost ret = TunnelBridgeIsFree(tile, endtile);
1659 if (ret.Failed()) {
1660 error = ret;
1661 continue;
1662 }
1663 }
1664
1665 if (flags & DC_EXEC) {
1666 Track track = DiagDirToDiagTrack(GetTunnelBridgeDirection(tile));
1667 if (HasTunnelBridgeReservation(tile)) {
1668 Train *v = GetTrainForReservation(tile, track);
1669 if (v != NULL && !HasPowerOnRail(v->railtype, totype)) {
1670 /* No power on new rail type, reroute. */
1671 FreeTrainTrackReservation(v);
1672 *vehicles_affected.Append() = v;
1673 }
1674 }
1675
1676 /* Update the company infrastructure counters. */
1677 uint num_pieces = (GetTunnelBridgeLength(tile, endtile) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
1678 Company *c = Company::Get(GetTileOwner(tile));
1679 c->infrastructure.rail[GetRailType(tile)] -= num_pieces;
1680 c->infrastructure.rail[totype] += num_pieces;
1681 DirtyCompanyInfrastructureWindows(c->index);
1682
1683 SetRailType(tile, totype);
1684 SetRailType(endtile, totype);
1685
1686 FindVehicleOnPos(tile, &affected_trains, &UpdateTrainPowerProc);
1687 FindVehicleOnPos(endtile, &affected_trains, &UpdateTrainPowerProc);
1688
1689 YapfNotifyTrackLayoutChange(tile, track);
1690 YapfNotifyTrackLayoutChange(endtile, track);
1691
1692 if (IsBridge(tile)) {
1693 MarkBridgeDirty(tile);
1694 } else {
1695 MarkTileDirtyByTile(tile);
1696 MarkTileDirtyByTile(endtile);
1697 }
1698 }
1699
1700 cost.AddCost((GetTunnelBridgeLength(tile, endtile) + 2) * RailConvertCost(type, totype));
1701 break;
1702 }
1703
1704 default: // MP_STATION, MP_ROAD
1705 if (flags & DC_EXEC) {
1706 Track track = ((tt == MP_STATION) ? GetRailStationTrack(tile) : GetCrossingRailTrack(tile));
1707 YapfNotifyTrackLayoutChange(tile, track);
1708 }
1709
1710 cost.AddCost(RailConvertCost(type, totype));
1711 break;
1712 }
1713
1714 for (uint i = 0; i < vehicles_affected.Length(); ++i) {
1715 TryPathReserve(vehicles_affected[i], true);
1716 }
1717 }
1718
1719 if (flags & DC_EXEC) {
1720 /* Railtype changed, update trains as when entering different track */
1721 for (Train **v = affected_trains.Begin(); v != affected_trains.End(); v++) {
1722 (*v)->ConsistChanged(CCF_TRACK);
1723 }
1724 }
1725
1726 delete iter;
1727 return (cost.GetCost() == 0) ? error : cost;
1728 }
1729
1730 static CommandCost RemoveTrainDepot(TileIndex tile, DoCommandFlag flags)
1731 {
1732 if (_current_company != OWNER_WATER) {
1733 CommandCost ret = CheckTileOwnership(tile);
1734 if (ret.Failed()) return ret;
1735 }
1736
1737 CommandCost ret = EnsureNoVehicleOnGround(tile);
1738 if (ret.Failed()) return ret;
1739
1740 if (flags & DC_EXEC) {
1741 /* read variables before the depot is removed */
1742 DiagDirection dir = GetRailDepotDirection(tile);
1743 Owner owner = GetTileOwner(tile);
1744 Train *v = NULL;
1745
1746 if (HasDepotReservation(tile)) {
1747 v = GetTrainForReservation(tile, DiagDirToDiagTrack(dir));
1748 if (v != NULL) FreeTrainTrackReservation(v);
1749 }
1750
1751 Company::Get(owner)->infrastructure.rail[GetRailType(tile)]--;
1752 DirtyCompanyInfrastructureWindows(owner);
1753
1754 delete Depot::GetByTile(tile);
1755 DoClearSquare(tile);
1756 AddSideToSignalBuffer(tile, dir, owner);
1757 YapfNotifyTrackLayoutChange(tile, DiagDirToDiagTrack(dir));
1758 if (v != NULL) TryPathReserve(v, true);
1759 }
1760
1761 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_DEPOT_TRAIN]);
1762 }
1763
1764 static CommandCost ClearTile_Track(TileIndex tile, DoCommandFlag flags)
1765 {
1766 CommandCost cost(EXPENSES_CONSTRUCTION);
1767
1768 if (flags & DC_AUTO) {
1769 if (!IsTileOwner(tile, _current_company)) {
1770 return_cmd_error(STR_ERROR_AREA_IS_OWNED_BY_ANOTHER);
1771 }
1772
1773 if (IsPlainRail(tile)) {
1774 return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
1775 } else {
1776 return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
1777 }
1778 }
1779
1780 switch (GetRailTileType(tile)) {
1781 case RAIL_TILE_SIGNALS:
1782 case RAIL_TILE_NORMAL: {
1783 Slope tileh = GetTileSlope(tile);
1784 /* Is there flat water on the lower halftile that gets cleared expensively? */
1785 bool water_ground = (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh));
1786
1787 TrackBits tracks = GetTrackBits(tile);
1788 while (tracks != TRACK_BIT_NONE) {
1789 Track track = RemoveFirstTrack(&tracks);
1790 CommandCost ret = DoCommand(tile, 0, track, flags, CMD_REMOVE_SINGLE_RAIL);
1791 if (ret.Failed()) return ret;
1792 cost.AddCost(ret);
1793 }
1794
1795 /* When bankrupting, don't make water dirty, there could be a ship on lower halftile.
1796 * Same holds for non-companies clearing the tile, e.g. disasters. */
1797 if (water_ground && !(flags & DC_BANKRUPT) && Company::IsValidID(_current_company)) {
1798 CommandCost ret = EnsureNoVehicleOnGround(tile);
1799 if (ret.Failed()) return ret;
1800
1801 /* The track was removed, and left a coast tile. Now also clear the water. */
1802 if (flags & DC_EXEC) DoClearSquare(tile);
1803 cost.AddCost(_price[PR_CLEAR_WATER]);
1804 }
1805
1806 return cost;
1807 }
1808
1809 case RAIL_TILE_DEPOT:
1810 return RemoveTrainDepot(tile, flags);
1811
1812 default:
1813 return CMD_ERROR;
1814 }
1815 }
1816
1821 static uint GetSaveSlopeZ(uint x, uint y, Track track)
1822 {
1823 switch (track) {
1824 case TRACK_UPPER: x &= ~0xF; y &= ~0xF; break;
1825 case TRACK_LOWER: x |= 0xF; y |= 0xF; break;
1826 case TRACK_LEFT: x |= 0xF; y &= ~0xF; break;
1827 case TRACK_RIGHT: x &= ~0xF; y |= 0xF; break;
1828 default: break;
1829 }
1830 return GetSlopePixelZ(x, y);
1831 }
1832
1833 static void DrawSingleSignal(TileIndex tile, const RailtypeInfo *rti, Track track, SignalState condition, SignalOffsets image, uint pos)
1834 {
1835 bool side;
1836 switch (_settings_game.construction.train_signal_side) {
1837 case 0: side = false; break; // left
1838 case 2: side = true; break; // right
1839 default: side = _settings_game.vehicle.road_side != 0; break; // driving side
1840 }
1841 static const Point SignalPositions[2][12] = {
1842 { // Signals on the left side
1843 /* LEFT LEFT RIGHT RIGHT UPPER UPPER */
1844 { 8, 5}, {14, 1}, { 1, 14}, { 9, 11}, { 1, 0}, { 3, 10},
1845 /* LOWER LOWER X X Y Y */
1846 {11, 4}, {14, 14}, {11, 3}, { 4, 13}, { 3, 4}, {11, 13}
1847 }, { // Signals on the right side
1848 /* LEFT LEFT RIGHT RIGHT UPPER UPPER */
1849 {14, 1}, {12, 10}, { 4, 6}, { 1, 14}, {10, 4}, { 0, 1},
1850 /* LOWER LOWER X X Y Y */
1851 {14, 14}, { 5, 12}, {11, 13}, { 4, 3}, {13, 4}, { 3, 11}
1852 }
1853 };
1854
1855 uint x = TileX(tile) * TILE_SIZE + SignalPositions[side][pos].x;
1856 uint y = TileY(tile) * TILE_SIZE + SignalPositions[side][pos].y;
1857
1858 SignalType type = GetSignalType(tile, track);
1859 SignalVariant variant = GetSignalVariant(tile, track);
1860
1861 SpriteID sprite = GetCustomSignalSprite(rti, tile, type, variant, condition);
1862 if (sprite != 0) {
1863 sprite += image;
1864 } else {
1865 /* Normal electric signals are stored in a different sprite block than all other signals. */
1866 sprite = (type == SIGTYPE_NORMAL && variant == SIG_ELECTRIC) ? SPR_ORIGINAL_SIGNALS_BASE : SPR_SIGNALS_BASE - 16;
1867 sprite += type * 16 + variant * 64 + image * 2 + condition + (type > SIGTYPE_LAST_NOPBS ? 64 : 0);
1868 }
1869
1870 AddSortableSpriteToDraw(sprite, PAL_NONE, x, y, 1, 1, BB_HEIGHT_UNDER_BRIDGE, GetSaveSlopeZ(x, y, track));
1871 }
1872
1873 static uint32 _drawtile_track_palette;
1874
1875
1876 static void DrawTrackFence_NW(const TileInfo *ti, SpriteID base_image)
1877 {
1878 RailFenceOffset rfo = RFO_FLAT_X;
1879 if (ti->tileh & SLOPE_NW) rfo = (ti->tileh & SLOPE_W) ? RFO_SLOPE_SW : RFO_SLOPE_NE;
1880 AddSortableSpriteToDraw(base_image + rfo, _drawtile_track_palette,
1881 ti->x, ti->y + 1, 16, 1, 4, ti->z);
1882 }
1883
1884 static void DrawTrackFence_SE(const TileInfo *ti, SpriteID base_image)
1885 {
1886 RailFenceOffset rfo = RFO_FLAT_X;
1887 if (ti->tileh & SLOPE_SE) rfo = (ti->tileh & SLOPE_S) ? RFO_SLOPE_SW : RFO_SLOPE_NE;
1888 AddSortableSpriteToDraw(base_image + rfo, _drawtile_track_palette,
1889 ti->x, ti->y + TILE_SIZE - 1, 16, 1, 4, ti->z);
1890 }
1891
1892 static void DrawTrackFence_NW_SE(const TileInfo *ti, SpriteID base_image)
1893 {
1894 DrawTrackFence_NW(ti, base_image);
1895 DrawTrackFence_SE(ti, base_image);
1896 }
1897
1898 static void DrawTrackFence_NE(const TileInfo *ti, SpriteID base_image)
1899 {
1900 RailFenceOffset rfo = RFO_FLAT_Y;
1901 if (ti->tileh & SLOPE_NE) rfo = (ti->tileh & SLOPE_E) ? RFO_SLOPE_SE : RFO_SLOPE_NW;
1902 AddSortableSpriteToDraw(base_image + rfo, _drawtile_track_palette,
1903 ti->x + 1, ti->y, 1, 16, 4, ti->z);
1904 }
1905
1906 static void DrawTrackFence_SW(const TileInfo *ti, SpriteID base_image)
1907 {
1908 RailFenceOffset rfo = RFO_FLAT_Y;
1909 if (ti->tileh & SLOPE_SW) rfo = (ti->tileh & SLOPE_S) ? RFO_SLOPE_SE : RFO_SLOPE_NW;
1910 AddSortableSpriteToDraw(base_image + rfo, _drawtile_track_palette,
1911 ti->x + TILE_SIZE - 1, ti->y, 1, 16, 4, ti->z);
1912 }
1913
1914 static void DrawTrackFence_NE_SW(const TileInfo *ti, SpriteID base_image)
1915 {
1916 DrawTrackFence_NE(ti, base_image);
1917 DrawTrackFence_SW(ti, base_image);
1918 }
1919
1923 static void DrawTrackFence_NS_1(const TileInfo *ti, SpriteID base_image)
1924 {
1925 int z = ti->z + GetSlopePixelZInCorner(RemoveHalftileSlope(ti->tileh), CORNER_W);
1926 AddSortableSpriteToDraw(base_image + RFO_FLAT_VERT, _drawtile_track_palette,
1927 ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, 4, z);
1928 }
1929
1933 static void DrawTrackFence_NS_2(const TileInfo *ti, SpriteID base_image)
1934 {
1935 int z = ti->z + GetSlopePixelZInCorner(RemoveHalftileSlope(ti->tileh), CORNER_E);
1936 AddSortableSpriteToDraw(base_image + RFO_FLAT_VERT, _drawtile_track_palette,
1937 ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, 4, z);
1938 }
1939
1943 static void DrawTrackFence_WE_1(const TileInfo *ti, SpriteID base_image)
1944 {
1945 int z = ti->z + GetSlopePixelZInCorner(RemoveHalftileSlope(ti->tileh), CORNER_N);
1946 AddSortableSpriteToDraw(base_image + RFO_FLAT_HORZ, _drawtile_track_palette,
1947 ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, 4, z);
1948 }
1949
1953 static void DrawTrackFence_WE_2(const TileInfo *ti, SpriteID base_image)
1954 {
1955 int z = ti->z + GetSlopePixelZInCorner(RemoveHalftileSlope(ti->tileh), CORNER_S);
1956 AddSortableSpriteToDraw(base_image + RFO_FLAT_HORZ, _drawtile_track_palette,
1957 ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, 4, z);
1958 }
1959
1960
1961 static void DrawTrackDetails(const TileInfo *ti, const RailtypeInfo *rti)
1962 {
1963 /* Base sprite for track fences.
1964 * Note: Halftile slopes only have fences on the upper part. */
1965 SpriteID base_image = GetCustomRailSprite(rti, ti->tile, RTSG_FENCES, IsHalftileSlope(ti->tileh) ? TCX_UPPER_HALFTILE : TCX_NORMAL);
1966 if (base_image == 0) base_image = SPR_TRACK_FENCE_FLAT_X;
1967
1968 switch (GetRailGroundType(ti->tile)) {
1969 case RAIL_GROUND_FENCE_NW: DrawTrackFence_NW(ti, base_image); break;
1970 case RAIL_GROUND_FENCE_SE: DrawTrackFence_SE(ti, base_image); break;
1971 case RAIL_GROUND_FENCE_SENW: DrawTrackFence_NW_SE(ti, base_image); break;
1972 case RAIL_GROUND_FENCE_NE: DrawTrackFence_NE(ti, base_image); break;
1973 case RAIL_GROUND_FENCE_SW: DrawTrackFence_SW(ti, base_image); break;
1974 case RAIL_GROUND_FENCE_NESW: DrawTrackFence_NE_SW(ti, base_image); break;
1975 case RAIL_GROUND_FENCE_VERT1: DrawTrackFence_NS_1(ti, base_image); break;
1976 case RAIL_GROUND_FENCE_VERT2: DrawTrackFence_NS_2(ti, base_image); break;
1977 case RAIL_GROUND_FENCE_HORIZ1: DrawTrackFence_WE_1(ti, base_image); break;
1978 case RAIL_GROUND_FENCE_HORIZ2: DrawTrackFence_WE_2(ti, base_image); break;
1979 case RAIL_GROUND_WATER: {
1980 Corner track_corner;
1981 if (IsHalftileSlope(ti->tileh)) {
1982 /* Steep slope or one-corner-raised slope with halftile foundation */
1983 track_corner = GetHalftileSlopeCorner(ti->tileh);
1984 } else {
1985 /* Three-corner-raised slope */
1986 track_corner = OppositeCorner(GetHighestSlopeCorner(ComplementSlope(ti->tileh)));
1987 }
1988 switch (track_corner) {
1989 case CORNER_W: DrawTrackFence_NS_1(ti, base_image); break;
1990 case CORNER_S: DrawTrackFence_WE_2(ti, base_image); break;
1991 case CORNER_E: DrawTrackFence_NS_2(ti, base_image); break;
1992 case CORNER_N: DrawTrackFence_WE_1(ti, base_image); break;
1993 default: NOT_REACHED();
1994 }
1995 break;
1996 }
1997 default: break;
1998 }
1999 }
2000
2001 /* SubSprite for drawing the track halftile of 'three-corners-raised'-sloped rail sprites. */
2002 static const int INF = 1000; // big number compared to tilesprite size
2003 static const SubSprite _halftile_sub_sprite[4] = {
2004 { -INF , -INF , 32 - 33, INF }, // CORNER_W, clip 33 pixels from right
2005 { -INF , 0 + 7, INF , INF }, // CORNER_S, clip 7 pixels from top
2006 { -31 + 33, -INF , INF , INF }, // CORNER_E, clip 33 pixels from left
2007 { -INF , -INF , INF , 30 - 23 } // CORNER_N, clip 23 pixels from bottom
2008 };
2009
2010 static inline void DrawTrackSprite(SpriteID sprite, PaletteID pal, const TileInfo *ti, Slope s)
2011 {
2012 DrawGroundSprite(sprite, pal, NULL, 0, (ti->tileh & s) ? -8 : 0);
2013 }
2014
2015 static void DrawTrackBitsOverlay(TileInfo *ti, TrackBits track, const RailtypeInfo *rti)
2016 {
2017 RailGroundType rgt = GetRailGroundType(ti->tile);
2018 Foundation f = GetRailFoundation(ti->tileh, track);
2019 Corner halftile_corner = CORNER_INVALID;
2020
2021 if (IsNonContinuousFoundation(f)) {
2022 /* Save halftile corner */
2023 halftile_corner = (f == FOUNDATION_STEEP_BOTH ? GetHighestSlopeCorner(ti->tileh) : GetHalftileFoundationCorner(f));
2024 /* Draw lower part first */
2025 track &= ~CornerToTrackBits(halftile_corner);
2026 f = (f == FOUNDATION_STEEP_BOTH ? FOUNDATION_STEEP_LOWER : FOUNDATION_NONE);
2027 }
2028
2029 DrawFoundation(ti, f);
2030 /* DrawFoundation modifies ti */
2031
2032 /* Draw ground */
2033 if (rgt == RAIL_GROUND_WATER) {
2034 if (track != TRACK_BIT_NONE || IsSteepSlope(ti->tileh)) {
2035 /* three-corner-raised slope or steep slope with track on upper part */
2036 DrawShoreTile(ti->tileh);
2037 } else {
2038 /* single-corner-raised slope with track on upper part */
2039 DrawGroundSprite(SPR_FLAT_WATER_TILE, PAL_NONE);
2040 }
2041 } else {
2042 SpriteID image;
2043
2044 switch (rgt) {
2045 case RAIL_GROUND_BARREN: image = SPR_FLAT_BARE_LAND; break;
2046 case RAIL_GROUND_ICE_DESERT: image = SPR_FLAT_SNOW_DESERT_TILE; break;
2047 default: image = SPR_FLAT_GRASS_TILE; break;
2048 }
2049
2050 image += SlopeToSpriteOffset(ti->tileh);
2051
2052 DrawGroundSprite(image, PAL_NONE);
2053 }
2054
2055 SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
2056 SpriteID ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND);
2057 TrackBits pbs = _settings_client.gui.show_track_reservation ? GetRailReservationTrackBits(ti->tile) : TRACK_BIT_NONE;
2058
2059 if (track == TRACK_BIT_NONE) {
2060 /* Half-tile foundation, no track here? */
2061 } else if (ti->tileh == SLOPE_NW && track == TRACK_BIT_Y) {
2062 DrawGroundSprite(ground + RTO_SLOPE_NW, PAL_NONE);
2063 if (pbs != TRACK_BIT_NONE) DrawGroundSprite(overlay + RTO_SLOPE_NW, PALETTE_CRASH);
2064 } else if (ti->tileh == SLOPE_NE && track == TRACK_BIT_X) {
2065 DrawGroundSprite(ground + RTO_SLOPE_NE, PAL_NONE);
2066 if (pbs != TRACK_BIT_NONE) DrawGroundSprite(overlay + RTO_SLOPE_NE, PALETTE_CRASH);
2067 } else if (ti->tileh == SLOPE_SE && track == TRACK_BIT_Y) {
2068 DrawGroundSprite(ground + RTO_SLOPE_SE, PAL_NONE);
2069 if (pbs != TRACK_BIT_NONE) DrawGroundSprite(overlay + RTO_SLOPE_SE, PALETTE_CRASH);
2070 } else if (ti->tileh == SLOPE_SW && track == TRACK_BIT_X) {
2071 DrawGroundSprite(ground + RTO_SLOPE_SW, PAL_NONE);
2072 if (pbs != TRACK_BIT_NONE) DrawGroundSprite(overlay + RTO_SLOPE_SW, PALETTE_CRASH);
2073 } else {
2074 switch (track) {
2075 /* Draw single ground sprite when not overlapping. No track overlay
2076 * is necessary for these sprites. */
2077 case TRACK_BIT_X: DrawGroundSprite(ground + RTO_X, PAL_NONE); break;
2078 case TRACK_BIT_Y: DrawGroundSprite(ground + RTO_Y, PAL_NONE); break;
2079 case TRACK_BIT_UPPER: DrawTrackSprite(ground + RTO_N, PAL_NONE, ti, SLOPE_N); break;
2080 case TRACK_BIT_LOWER: DrawTrackSprite(ground + RTO_S, PAL_NONE, ti, SLOPE_S); break;
2081 case TRACK_BIT_RIGHT: DrawTrackSprite(ground + RTO_E, PAL_NONE, ti, SLOPE_E); break;
2082 case TRACK_BIT_LEFT: DrawTrackSprite(ground + RTO_W, PAL_NONE, ti, SLOPE_W); break;
2083 case TRACK_BIT_CROSS: DrawGroundSprite(ground + RTO_CROSSING_XY, PAL_NONE); break;
2084 case TRACK_BIT_HORZ: DrawTrackSprite(ground + RTO_N, PAL_NONE, ti, SLOPE_N);
2085 DrawTrackSprite(ground + RTO_S, PAL_NONE, ti, SLOPE_S); break;
2086 case TRACK_BIT_VERT: DrawTrackSprite(ground + RTO_E, PAL_NONE, ti, SLOPE_E);
2087 DrawTrackSprite(ground + RTO_W, PAL_NONE, ti, SLOPE_W); break;
2088
2089 default:
2090 /* We're drawing a junction tile */
2091 if ((track & TRACK_BIT_3WAY_NE) == 0) {
2092 DrawGroundSprite(ground + RTO_JUNCTION_SW, PAL_NONE);
2093 } else if ((track & TRACK_BIT_3WAY_SW) == 0) {
2094 DrawGroundSprite(ground + RTO_JUNCTION_NE, PAL_NONE);
2095 } else if ((track & TRACK_BIT_3WAY_NW) == 0) {
2096 DrawGroundSprite(ground + RTO_JUNCTION_SE, PAL_NONE);
2097 } else if ((track & TRACK_BIT_3WAY_SE) == 0) {
2098 DrawGroundSprite(ground + RTO_JUNCTION_NW, PAL_NONE);
2099 } else {
2100 DrawGroundSprite(ground + RTO_JUNCTION_NSEW, PAL_NONE);
2101 }
2102
2103 /* Mask out PBS bits as we shall draw them afterwards anyway. */
2104 track &= ~pbs;
2105
2106 /* Draw regular track bits */
2107 if (track & TRACK_BIT_X) DrawGroundSprite(overlay + RTO_X, PAL_NONE);
2108 if (track & TRACK_BIT_Y) DrawGroundSprite(overlay + RTO_Y, PAL_NONE);
2109 if (track & TRACK_BIT_UPPER) DrawGroundSprite(overlay + RTO_N, PAL_NONE);
2110 if (track & TRACK_BIT_LOWER) DrawGroundSprite(overlay + RTO_S, PAL_NONE);
2111 if (track & TRACK_BIT_RIGHT) DrawGroundSprite(overlay + RTO_E, PAL_NONE);
2112 if (track & TRACK_BIT_LEFT) DrawGroundSprite(overlay + RTO_W, PAL_NONE);
2113 }
2114
2115 /* Draw reserved track bits */
2116 if (pbs & TRACK_BIT_X) DrawGroundSprite(overlay + RTO_X, PALETTE_CRASH);
2117 if (pbs & TRACK_BIT_Y) DrawGroundSprite(overlay + RTO_Y, PALETTE_CRASH);
2118 if (pbs & TRACK_BIT_UPPER) DrawTrackSprite(overlay + RTO_N, PALETTE_CRASH, ti, SLOPE_N);
2119 if (pbs & TRACK_BIT_LOWER) DrawTrackSprite(overlay + RTO_S, PALETTE_CRASH, ti, SLOPE_S);
2120 if (pbs & TRACK_BIT_RIGHT) DrawTrackSprite(overlay + RTO_E, PALETTE_CRASH, ti, SLOPE_E);
2121 if (pbs & TRACK_BIT_LEFT) DrawTrackSprite(overlay + RTO_W, PALETTE_CRASH, ti, SLOPE_W);
2122 }
2123
2124 if (IsValidCorner(halftile_corner)) {
2125 DrawFoundation(ti, HalftileFoundation(halftile_corner));
2126 overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY, TCX_UPPER_HALFTILE);
2127 ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND, TCX_UPPER_HALFTILE);
2128
2129 /* Draw higher halftile-overlay: Use the sloped sprites with three corners raised. They probably best fit the lightning. */
2130 Slope fake_slope = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
2131
2132 SpriteID image;
2133 switch (rgt) {
2134 case RAIL_GROUND_BARREN: image = SPR_FLAT_BARE_LAND; break;
2135 case RAIL_GROUND_ICE_DESERT:
2136 case RAIL_GROUND_HALF_SNOW: image = SPR_FLAT_SNOW_DESERT_TILE; break;
2137 default: image = SPR_FLAT_GRASS_TILE; break;
2138 }
2139
2140 image += SlopeToSpriteOffset(fake_slope);
2141
2142 DrawGroundSprite(image, PAL_NONE, &(_halftile_sub_sprite[halftile_corner]));
2143
2144 track = CornerToTrackBits(halftile_corner);
2145
2146 int offset;
2147 switch (track) {
2148 default: NOT_REACHED();
2149 case TRACK_BIT_UPPER: offset = RTO_N; break;
2150 case TRACK_BIT_LOWER: offset = RTO_S; break;
2151 case TRACK_BIT_RIGHT: offset = RTO_E; break;
2152 case TRACK_BIT_LEFT: offset = RTO_W; break;
2153 }
2154
2155 DrawTrackSprite(ground + offset, PAL_NONE, ti, fake_slope);
2156 if (_settings_client.gui.show_track_reservation && HasReservedTracks(ti->tile, track)) {
2157 DrawTrackSprite(overlay + offset, PALETTE_CRASH, ti, fake_slope);
2158 }
2159 }
2160 }
2161
2167 static void DrawTrackBits(TileInfo *ti, TrackBits track)
2168 {
2169 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
2170
2171 if (rti->UsesOverlay()) {
2172 DrawTrackBitsOverlay(ti, track, rti);
2173 return;
2174 }
2175
2176 RailGroundType rgt = GetRailGroundType(ti->tile);
2177 Foundation f = GetRailFoundation(ti->tileh, track);
2178 Corner halftile_corner = CORNER_INVALID;
2179
2180 if (IsNonContinuousFoundation(f)) {
2181 /* Save halftile corner */
2182 halftile_corner = (f == FOUNDATION_STEEP_BOTH ? GetHighestSlopeCorner(ti->tileh) : GetHalftileFoundationCorner(f));
2183 /* Draw lower part first */
2184 track &= ~CornerToTrackBits(halftile_corner);
2185 f = (f == FOUNDATION_STEEP_BOTH ? FOUNDATION_STEEP_LOWER : FOUNDATION_NONE);
2186 }
2187
2188 DrawFoundation(ti, f);
2189 /* DrawFoundation modifies ti */
2190
2191 SpriteID image;
2192 PaletteID pal = PAL_NONE;
2193 const SubSprite *sub = NULL;
2194 bool junction = false;
2195
2196 /* Select the sprite to use. */
2197 if (track == 0) {
2198 /* Clear ground (only track on halftile foundation) */
2199 if (rgt == RAIL_GROUND_WATER) {
2200 if (IsSteepSlope(ti->tileh)) {
2201 DrawShoreTile(ti->tileh);
2202 image = 0;
2203 } else {
2204 image = SPR_FLAT_WATER_TILE;
2205 }
2206 } else {
2207 switch (rgt) {
2208 case RAIL_GROUND_BARREN: image = SPR_FLAT_BARE_LAND; break;
2209 case RAIL_GROUND_ICE_DESERT: image = SPR_FLAT_SNOW_DESERT_TILE; break;
2210 default: image = SPR_FLAT_GRASS_TILE; break;
2211 }
2212 image += SlopeToSpriteOffset(ti->tileh);
2213 }
2214 } else {
2215 if (ti->tileh != SLOPE_FLAT) {
2216 /* track on non-flat ground */
2217 image = _track_sloped_sprites[ti->tileh - 1] + rti->base_sprites.track_y;
2218 } else {
2219 /* track on flat ground */
2220 (image = rti->base_sprites.track_y, track == TRACK_BIT_Y) ||
2221 (image++, track == TRACK_BIT_X) ||
2222 (image++, track == TRACK_BIT_UPPER) ||
2223 (image++, track == TRACK_BIT_LOWER) ||
2224 (image++, track == TRACK_BIT_RIGHT) ||
2225 (image++, track == TRACK_BIT_LEFT) ||
2226 (image++, track == TRACK_BIT_CROSS) ||
2227
2228 (image = rti->base_sprites.track_ns, track == TRACK_BIT_HORZ) ||
2229 (image++, track == TRACK_BIT_VERT) ||
2230
2231 (junction = true, false) ||
2232 (image = rti->base_sprites.ground, (track & TRACK_BIT_3WAY_NE) == 0) ||
2233 (image++, (track & TRACK_BIT_3WAY_SW) == 0) ||
2234 (image++, (track & TRACK_BIT_3WAY_NW) == 0) ||
2235 (image++, (track & TRACK_BIT_3WAY_SE) == 0) ||
2236 (image++, true);
2237 }
2238
2239 switch (rgt) {
2240 case RAIL_GROUND_BARREN: pal = PALETTE_TO_BARE_LAND; break;
2241 case RAIL_GROUND_ICE_DESERT: image += rti->snow_offset; break;
2242 case RAIL_GROUND_WATER: {
2243 /* three-corner-raised slope */
2244 DrawShoreTile(ti->tileh);
2245 Corner track_corner = OppositeCorner(GetHighestSlopeCorner(ComplementSlope(ti->tileh)));
2246 sub = &(_halftile_sub_sprite[track_corner]);
2247 break;
2248 }
2249 default: break;
2250 }
2251 }
2252
2253 if (image != 0) DrawGroundSprite(image, pal, sub);
2254
2255 /* Draw track pieces individually for junction tiles */
2256 if (junction) {
2257 if (track & TRACK_BIT_X) DrawGroundSprite(rti->base_sprites.single_x, PAL_NONE);
2258 if (track & TRACK_BIT_Y) DrawGroundSprite(rti->base_sprites.single_y, PAL_NONE);
2259 if (track & TRACK_BIT_UPPER) DrawGroundSprite(rti->base_sprites.single_n, PAL_NONE);
2260 if (track & TRACK_BIT_LOWER) DrawGroundSprite(rti->base_sprites.single_s, PAL_NONE);
2261 if (track & TRACK_BIT_LEFT) DrawGroundSprite(rti->base_sprites.single_w, PAL_NONE);
2262 if (track & TRACK_BIT_RIGHT) DrawGroundSprite(rti->base_sprites.single_e, PAL_NONE);
2263 }
2264
2265 /* PBS debugging, draw reserved tracks darker */
2266 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation) {
2267 /* Get reservation, but mask track on halftile slope */
2268 TrackBits pbs = GetRailReservationTrackBits(ti->tile) & track;
2269 if (pbs & TRACK_BIT_X) {
2270 if (ti->tileh == SLOPE_FLAT || ti->tileh == SLOPE_ELEVATED) {
2271 DrawGroundSprite(rti->base_sprites.single_x, PALETTE_CRASH);
2272 } else {
2273 DrawGroundSprite(_track_sloped_sprites[ti->tileh - 1] + rti->base_sprites.single_sloped - 20, PALETTE_CRASH);
2274 }
2275 }
2276 if (pbs & TRACK_BIT_Y) {
2277 if (ti->tileh == SLOPE_FLAT || ti->tileh == SLOPE_ELEVATED) {
2278 DrawGroundSprite(rti->base_sprites.single_y, PALETTE_CRASH);
2279 } else {
2280 DrawGroundSprite(_track_sloped_sprites[ti->tileh - 1] + rti->base_sprites.single_sloped - 20, PALETTE_CRASH);
2281 }
2282 }
2283 if (pbs & TRACK_BIT_UPPER) DrawGroundSprite(rti->base_sprites.single_n, PALETTE_CRASH, NULL, 0, ti->tileh & SLOPE_N ? -(int)TILE_HEIGHT : 0);
2284 if (pbs & TRACK_BIT_LOWER) DrawGroundSprite(rti->base_sprites.single_s, PALETTE_CRASH, NULL, 0, ti->tileh & SLOPE_S ? -(int)TILE_HEIGHT : 0);
2285 if (pbs & TRACK_BIT_LEFT) DrawGroundSprite(rti->base_sprites.single_w, PALETTE_CRASH, NULL, 0, ti->tileh & SLOPE_W ? -(int)TILE_HEIGHT : 0);
2286 if (pbs & TRACK_BIT_RIGHT) DrawGroundSprite(rti->base_sprites.single_e, PALETTE_CRASH, NULL, 0, ti->tileh & SLOPE_E ? -(int)TILE_HEIGHT : 0);
2287 }
2288
2289 if (IsValidCorner(halftile_corner)) {
2290 DrawFoundation(ti, HalftileFoundation(halftile_corner));
2291
2292 /* Draw higher halftile-overlay: Use the sloped sprites with three corners raised. They probably best fit the lightning. */
2293 Slope fake_slope = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
2294 image = _track_sloped_sprites[fake_slope - 1] + rti->base_sprites.track_y;
2295 pal = PAL_NONE;
2296 switch (rgt) {
2297 case RAIL_GROUND_BARREN: pal = PALETTE_TO_BARE_LAND; break;
2298 case RAIL_GROUND_ICE_DESERT:
2299 case RAIL_GROUND_HALF_SNOW: image += rti->snow_offset; break; // higher part has snow in this case too
2300 default: break;
2301 }
2302 DrawGroundSprite(image, pal, &(_halftile_sub_sprite[halftile_corner]));
2303
2304 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasReservedTracks(ti->tile, CornerToTrackBits(halftile_corner))) {
2305 static const byte _corner_to_track_sprite[] = {3, 1, 2, 0};
2306 DrawGroundSprite(_corner_to_track_sprite[halftile_corner] + rti->base_sprites.single_n, PALETTE_CRASH, NULL, 0, -(int)TILE_HEIGHT);
2307 }
2308 }
2309 }
2310
2311 static void DrawSignals(TileIndex tile, TrackBits rails, const RailtypeInfo *rti)
2312 {
2313 #define MAYBE_DRAW_SIGNAL(x, y, z, t) if (IsSignalPresent(tile, x)) DrawSingleSignal(tile, rti, t, GetSingleSignalState(tile, x), y, z)
2314
2315 if (!(rails & TRACK_BIT_Y)) {
2316 if (!(rails & TRACK_BIT_X)) {
2317 if (rails & TRACK_BIT_LEFT) {
2318 MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_NORTH, 0, TRACK_LEFT);
2319 MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_SOUTH, 1, TRACK_LEFT);
2320 }
2321 if (rails & TRACK_BIT_RIGHT) {
2322 MAYBE_DRAW_SIGNAL(0, SIGNAL_TO_NORTH, 2, TRACK_RIGHT);
2323 MAYBE_DRAW_SIGNAL(1, SIGNAL_TO_SOUTH, 3, TRACK_RIGHT);
2324 }
2325 if (rails & TRACK_BIT_UPPER) {
2326 MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_WEST, 4, TRACK_UPPER);
2327 MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_EAST, 5, TRACK_UPPER);
2328 }
2329 if (rails & TRACK_BIT_LOWER) {
2330 MAYBE_DRAW_SIGNAL(1, SIGNAL_TO_WEST, 6, TRACK_LOWER);
2331 MAYBE_DRAW_SIGNAL(0, SIGNAL_TO_EAST, 7, TRACK_LOWER);
2332 }
2333 } else {
2334 MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_SOUTHWEST, 8, TRACK_X);
2335 MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_NORTHEAST, 9, TRACK_X);
2336 }
2337 } else {
2338 MAYBE_DRAW_SIGNAL(3, SIGNAL_TO_SOUTHEAST, 10, TRACK_Y);
2339 MAYBE_DRAW_SIGNAL(2, SIGNAL_TO_NORTHWEST, 11, TRACK_Y);
2340 }
2341 }
2342
2343 static void DrawTile_Track(TileInfo *ti)
2344 {
2345 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
2346
2347 _drawtile_track_palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
2348
2349 if (IsPlainRail(ti->tile)) {
2350 TrackBits rails = GetTrackBits(ti->tile);
2351
2352 DrawTrackBits(ti, rails);
2353
2354 if (HasBit(_display_opt, DO_FULL_DETAIL)) DrawTrackDetails(ti, rti);
2355
2356 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
2357
2358 if (HasSignals(ti->tile)) DrawSignals(ti->tile, rails, rti);
2359 } else {
2360 /* draw depot */
2361 const DrawTileSprites *dts;
2362 PaletteID pal = PAL_NONE;
2363 SpriteID relocation;
2364
2365 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
2366
2367 if (IsInvisibilitySet(TO_BUILDINGS)) {
2368 /* Draw rail instead of depot */
2369 dts = &_depot_invisible_gfx_table[GetRailDepotDirection(ti->tile)];
2370 } else {
2371 dts = &_depot_gfx_table[GetRailDepotDirection(ti->tile)];
2372 }
2373
2374 SpriteID image;
2375 if (rti->UsesOverlay()) {
2376 image = SPR_FLAT_GRASS_TILE;
2377 } else {
2378 image = dts->ground.sprite;
2379 if (image != SPR_FLAT_GRASS_TILE) image += rti->GetRailtypeSpriteOffset();
2380 }
2381
2382 /* adjust ground tile for desert
2383 * don't adjust for snow, because snow in depots looks weird */
2384 if (IsSnowRailGround(ti->tile) && _settings_game.game_creation.landscape == LT_TROPIC) {
2385 if (image != SPR_FLAT_GRASS_TILE) {
2386 image += rti->snow_offset; // tile with tracks
2387 } else {
2388 image = SPR_FLAT_SNOW_DESERT_TILE; // flat ground
2389 }
2390 }
2391
2392 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, _drawtile_track_palette));
2393
2394 if (rti->UsesOverlay()) {
2395 SpriteID ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND);
2396
2397 switch (GetRailDepotDirection(ti->tile)) {
2398 case DIAGDIR_NE: if (!IsInvisibilitySet(TO_BUILDINGS)) break; // else FALL THROUGH
2399 case DIAGDIR_SW: DrawGroundSprite(ground + RTO_X, PAL_NONE); break;
2400 case DIAGDIR_NW: if (!IsInvisibilitySet(TO_BUILDINGS)) break; // else FALL THROUGH
2401 case DIAGDIR_SE: DrawGroundSprite(ground + RTO_Y, PAL_NONE); break;
2402 default: break;
2403 }
2404
2405 if (_settings_client.gui.show_track_reservation && HasDepotReservation(ti->tile)) {
2406 SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
2407
2408 switch (GetRailDepotDirection(ti->tile)) {
2409 case DIAGDIR_NE: if (!IsInvisibilitySet(TO_BUILDINGS)) break; // else FALL THROUGH
2410 case DIAGDIR_SW: DrawGroundSprite(overlay + RTO_X, PALETTE_CRASH); break;
2411 case DIAGDIR_NW: if (!IsInvisibilitySet(TO_BUILDINGS)) break; // else FALL THROUGH
2412 case DIAGDIR_SE: DrawGroundSprite(overlay + RTO_Y, PALETTE_CRASH); break;
2413 default: break;
2414 }
2415 }
2416 } else {
2417 /* PBS debugging, draw reserved tracks darker */
2418 if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasDepotReservation(ti->tile)) {
2419 switch (GetRailDepotDirection(ti->tile)) {
2420 case DIAGDIR_NE: if (!IsInvisibilitySet(TO_BUILDINGS)) break; // else FALL THROUGH
2421 case DIAGDIR_SW: DrawGroundSprite(rti->base_sprites.single_x, PALETTE_CRASH); break;
2422 case DIAGDIR_NW: if (!IsInvisibilitySet(TO_BUILDINGS)) break; // else FALL THROUGH
2423 case DIAGDIR_SE: DrawGroundSprite(rti->base_sprites.single_y, PALETTE_CRASH); break;
2424 default: break;
2425 }
2426 }
2427 }
2428 int depot_sprite = GetCustomRailSprite(rti, ti->tile, RTSG_DEPOT);
2429 relocation = depot_sprite != 0 ? depot_sprite - SPR_RAIL_DEPOT_SE_1 : rti->GetRailtypeSpriteOffset();
2430
2431 if (HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
2432
2433 DrawRailTileSeq(ti, dts, TO_BUILDINGS, relocation, 0, _drawtile_track_palette);
2434 }
2435 DrawBridgeMiddle(ti);
2436 }
2437
2438 void DrawTrainDepotSprite(int x, int y, int dir, RailType railtype)
2439 {
2440 const DrawTileSprites *dts = &_depot_gfx_table[dir];
2441 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
2442 SpriteID image = rti->UsesOverlay() ? SPR_FLAT_GRASS_TILE : dts->ground.sprite;
2443 uint32 offset = rti->GetRailtypeSpriteOffset();
2444
2445 if (image != SPR_FLAT_GRASS_TILE) image += offset;
2446 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
2447
2448 DrawSprite(image, PAL_NONE, x, y);
2449
2450 if (rti->UsesOverlay()) {
2451 SpriteID ground = GetCustomRailSprite(rti, INVALID_TILE, RTSG_GROUND);
2452
2453 switch (dir) {
2454 case DIAGDIR_SW: DrawSprite(ground + RTO_X, PAL_NONE, x, y); break;
2455 case DIAGDIR_SE: DrawSprite(ground + RTO_Y, PAL_NONE, x, y); break;
2456 default: break;
2457 }
2458 }
2459 int depot_sprite = GetCustomRailSprite(rti, INVALID_TILE, RTSG_DEPOT);
2460 if (depot_sprite != 0) offset = depot_sprite - SPR_RAIL_DEPOT_SE_1;
2461
2462 DrawRailTileSeqInGUI(x, y, dts, offset, 0, palette);
2463 }
2464
2465 static int GetSlopePixelZ_Track(TileIndex tile, uint x, uint y)
2466 {
2467 if (IsPlainRail(tile)) {
2468 int z;
2469 Slope tileh = GetTilePixelSlope(tile, &z);
2470 if (tileh == SLOPE_FLAT) return z;
2471
2472 z += ApplyPixelFoundationToSlope(GetRailFoundation(tileh, GetTrackBits(tile)), &tileh);
2473 return z + GetPartialPixelZ(x & 0xF, y & 0xF, tileh);
2474 } else {
2475 return GetTileMaxPixelZ(tile);
2476 }
2477 }
2478
2479 static Foundation GetFoundation_Track(TileIndex tile, Slope tileh)
2480 {
2481 return IsPlainRail(tile) ? GetRailFoundation(tileh, GetTrackBits(tile)) : FlatteningFoundation(tileh);
2482 }
2483
2484 static void TileLoop_Track(TileIndex tile)
2485 {
2486 RailGroundType old_ground = GetRailGroundType(tile);
2487 RailGroundType new_ground;
2488
2489 if (old_ground == RAIL_GROUND_WATER) {
2490 TileLoop_Water(tile);
2491 return;
2492 }
2493
2494 switch (_settings_game.game_creation.landscape) {
2495 case LT_ARCTIC: {
2496 int z;
2497 Slope slope = GetTileSlope(tile, &z);
2498 bool half = false;
2499
2500 /* for non-flat track, use lower part of track
2501 * in other cases, use the highest part with track */
2502 if (IsPlainRail(tile)) {
2503 TrackBits track = GetTrackBits(tile);
2504 Foundation f = GetRailFoundation(slope, track);
2505
2506 switch (f) {
2507 case FOUNDATION_NONE:
2508 /* no foundation - is the track on the upper side of three corners raised tile? */
2509 if (IsSlopeWithThreeCornersRaised(slope)) z++;
2510 break;
2511
2512 case FOUNDATION_INCLINED_X:
2513 case FOUNDATION_INCLINED_Y:
2514 /* sloped track - is it on a steep slope? */
2515 if (IsSteepSlope(slope)) z++;
2516 break;
2517
2518 case FOUNDATION_STEEP_LOWER:
2519 /* only lower part of steep slope */
2520 z++;
2521 break;
2522
2523 default:
2524 /* if it is a steep slope, then there is a track on higher part */
2525 if (IsSteepSlope(slope)) z++;
2526 z++;
2527 break;
2528 }
2529
2530 half = IsInsideMM(f, FOUNDATION_STEEP_BOTH, FOUNDATION_HALFTILE_N + 1);
2531 } else {
2532 /* is the depot on a non-flat tile? */
2533 if (slope != SLOPE_FLAT) z++;
2534 }
2535
2536 /* 'z' is now the lowest part of the highest track bit -
2537 * for sloped track, it is 'z' of lower part
2538 * for two track bits, it is 'z' of higher track bit
2539 * For non-continuous foundations (and STEEP_BOTH), 'half' is set */
2540 if (z > GetSnowLine()) {
2541 if (half && z - GetSnowLine() == 1) {
2542 /* track on non-continuous foundation, lower part is not under snow */
2543 new_ground = RAIL_GROUND_HALF_SNOW;
2544 } else {
2545 new_ground = RAIL_GROUND_ICE_DESERT;
2546 }
2547 goto set_ground;
2548 }
2549 break;
2550 }
2551
2552 case LT_TROPIC:
2553 if (GetTropicZone(tile) == TROPICZONE_DESERT) {
2554 new_ground = RAIL_GROUND_ICE_DESERT;
2555 goto set_ground;
2556 }
2557 break;
2558 }
2559
2560 new_ground = RAIL_GROUND_GRASS;
2561
2562 if (IsPlainRail(tile) && old_ground != RAIL_GROUND_BARREN) { // wait until bottom is green
2563 /* determine direction of fence */
2564 TrackBits rail = GetTrackBits(tile);
2565
2566 Owner owner = GetTileOwner(tile);
2567 byte fences = 0;
2568
2569 for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
2570 static const TrackBits dir_to_trackbits[DIAGDIR_END] = {TRACK_BIT_3WAY_NE, TRACK_BIT_3WAY_SE, TRACK_BIT_3WAY_SW, TRACK_BIT_3WAY_NW};
2571
2572 /* Track bit on this edge => no fence. */
2573 if ((rail & dir_to_trackbits[d]) != TRACK_BIT_NONE) continue;
2574
2575 TileIndex tile2 = tile + TileOffsByDiagDir(d);
2576
2577 /* Show fences if it's a house, industry, object, road, tunnelbridge or not owned by us. */
2578 if (!IsValidTile(tile2) || IsTileType(tile2, MP_HOUSE) || IsTileType(tile2, MP_INDUSTRY) ||
2579 IsTileType(tile2, MP_ROAD) || (IsTileType(tile2, MP_OBJECT) && !IsObjectType(tile2, OBJECT_OWNED_LAND)) || IsTileType(tile2, MP_TUNNELBRIDGE) || !IsTileOwner(tile2, owner)) {
2580 fences |= 1 << d;
2581 }
2582 }
2583
2584 switch (fences) {
2585 case 0: break;
2586 case (1 << DIAGDIR_NE): new_ground = RAIL_GROUND_FENCE_NE; break;
2587 case (1 << DIAGDIR_SE): new_ground = RAIL_GROUND_FENCE_SE; break;
2588 case (1 << DIAGDIR_SW): new_ground = RAIL_GROUND_FENCE_SW; break;
2589 case (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_NW; break;
2590 case (1 << DIAGDIR_NE) | (1 << DIAGDIR_SW): new_ground = RAIL_GROUND_FENCE_NESW; break;
2591 case (1 << DIAGDIR_SE) | (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_SENW; break;
2592 case (1 << DIAGDIR_NE) | (1 << DIAGDIR_SE): new_ground = RAIL_GROUND_FENCE_VERT1; break;
2593 case (1 << DIAGDIR_NE) | (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_HORIZ2; break;
2594 case (1 << DIAGDIR_SE) | (1 << DIAGDIR_SW): new_ground = RAIL_GROUND_FENCE_HORIZ1; break;
2595 case (1 << DIAGDIR_SW) | (1 << DIAGDIR_NW): new_ground = RAIL_GROUND_FENCE_VERT2; break;
2596 default: NOT_REACHED();
2597 }
2598 }
2599
2600 set_ground:
2601 if (old_ground != new_ground) {
2602 SetRailGroundType(tile, new_ground);
2603 MarkTileDirtyByTile(tile);
2604 }
2605 }
2606
2607
2608 static TrackStatus GetTileTrackStatus_Track(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
2609 {
2610 /* Case of half tile slope with water. */
2611 if (mode == TRANSPORT_WATER && IsPlainRail(tile) && GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(GetTileSlope(tile))) {
2612 TrackBits tb = GetTrackBits(tile);
2613 switch (tb) {
2614 default: NOT_REACHED();
2615 case TRACK_BIT_UPPER: tb = TRACK_BIT_LOWER; break;
2616 case TRACK_BIT_LOWER: tb = TRACK_BIT_UPPER; break;
2617 case TRACK_BIT_LEFT: tb = TRACK_BIT_RIGHT; break;
2618 case TRACK_BIT_RIGHT: tb = TRACK_BIT_LEFT; break;
2619 }
2620 return CombineTrackStatus(TrackBitsToTrackdirBits(tb), TRACKDIR_BIT_NONE);
2621 }
2622
2623 if (mode != TRANSPORT_RAIL) return 0;
2624
2625 TrackBits trackbits = TRACK_BIT_NONE;
2626 TrackdirBits red_signals = TRACKDIR_BIT_NONE;
2627
2628 switch (GetRailTileType(tile)) {
2629 default: NOT_REACHED();
2630 case RAIL_TILE_NORMAL:
2631 trackbits = GetTrackBits(tile);
2632 break;
2633
2634 case RAIL_TILE_SIGNALS: {
2635 trackbits = GetTrackBits(tile);
2636 byte a = GetPresentSignals(tile);
2637 uint b = GetSignalStates(tile);
2638
2639 b &= a;
2640
2641 /* When signals are not present (in neither direction),
2642 * we pretend them to be green. Otherwise, it depends on
2643 * the signal type. For signals that are only active from
2644 * one side, we set the missing signals explicitly to
2645 * `green'. Otherwise, they implicitly become `red'. */
2646 if (!IsOnewaySignal(tile, TRACK_UPPER) || (a & SignalOnTrack(TRACK_UPPER)) == 0) b |= ~a & SignalOnTrack(TRACK_UPPER);
2647 if (!IsOnewaySignal(tile, TRACK_LOWER) || (a & SignalOnTrack(TRACK_LOWER)) == 0) b |= ~a & SignalOnTrack(TRACK_LOWER);
2648
2649 if ((b & 0x8) == 0) red_signals |= (TRACKDIR_BIT_LEFT_N | TRACKDIR_BIT_X_NE | TRACKDIR_BIT_Y_SE | TRACKDIR_BIT_UPPER_E);
2650 if ((b & 0x4) == 0) red_signals |= (TRACKDIR_BIT_LEFT_S | TRACKDIR_BIT_X_SW | TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_UPPER_W);
2651 if ((b & 0x2) == 0) red_signals |= (TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_E);
2652 if ((b & 0x1) == 0) red_signals |= (TRACKDIR_BIT_RIGHT_S | TRACKDIR_BIT_LOWER_W);
2653
2654 break;
2655 }
2656
2657 case RAIL_TILE_DEPOT: {
2658 DiagDirection dir = GetRailDepotDirection(tile);
2659
2660 if (side != INVALID_DIAGDIR && side != dir) break;
2661
2662 trackbits = DiagDirToDiagTrackBits(dir);
2663 break;
2664 }
2665 }
2666
2667 return CombineTrackStatus(TrackBitsToTrackdirBits(trackbits), red_signals);
2668 }
2669
2670 static bool ClickTile_Track(TileIndex tile)
2671 {
2672 if (!IsRailDepot(tile)) return false;
2673
2674 ShowDepotWindow(tile, VEH_TRAIN);
2675 return true;
2676 }
2677
2678 static void GetTileDesc_Track(TileIndex tile, TileDesc *td)
2679 {
2680 const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
2681 td->rail_speed = rti->max_speed;
2682 td->owner[0] = GetTileOwner(tile);
2683 SetDParamX(td->dparam, 0, rti->strings.name);
2684 switch (GetRailTileType(tile)) {
2685 case RAIL_TILE_NORMAL:
2686 td->str = STR_LAI_RAIL_DESCRIPTION_TRACK;
2687 break;
2688
2689 case RAIL_TILE_SIGNALS: {
2690 static const StringID signal_type[6][6] = {
2691 {
2692 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_SIGNALS,
2693 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PRESIGNALS,
2694 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_EXITSIGNALS,
2695 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_COMBOSIGNALS,
2696 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PBSSIGNALS,
2697 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_NOENTRYSIGNALS
2698 },
2699 {
2700 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PRESIGNALS,
2701 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRESIGNALS,
2702 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_EXITSIGNALS,
2703 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_COMBOSIGNALS,
2704 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_PBSSIGNALS,
2705 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_NOENTRYSIGNALS
2706 },
2707 {
2708 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_EXITSIGNALS,
2709 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_EXITSIGNALS,
2710 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXITSIGNALS,
2711 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_COMBOSIGNALS,
2712 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_PBSSIGNALS,
2713 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_NOENTRYSIGNALS
2714 },
2715 {
2716 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_COMBOSIGNALS,
2717 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_COMBOSIGNALS,
2718 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_COMBOSIGNALS,
2719 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBOSIGNALS,
2720 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_PBSSIGNALS,
2721 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_NOENTRYSIGNALS
2722 },
2723 {
2724 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_PBSSIGNALS,
2725 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_PBSSIGNALS,
2726 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_PBSSIGNALS,
2727 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_PBSSIGNALS,
2728 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PBSSIGNALS,
2729 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PBS_NOENTRYSIGNALS
2730 },
2731 {
2732 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NORMAL_NOENTRYSIGNALS,
2733 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PRE_NOENTRYSIGNALS,
2734 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_EXIT_NOENTRYSIGNALS,
2735 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_COMBO_NOENTRYSIGNALS,
2736 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_PBS_NOENTRYSIGNALS,
2737 STR_LAI_RAIL_DESCRIPTION_TRACK_WITH_NOENTRYSIGNALS
2738 }
2739 };
2740
2741 SignalType primary_signal;
2742 SignalType secondary_signal;
2743 if (HasSignalOnTrack(tile, TRACK_UPPER)) {
2744 primary_signal = GetSignalType(tile, TRACK_UPPER);
2745 secondary_signal = HasSignalOnTrack(tile, TRACK_LOWER) ? GetSignalType(tile, TRACK_LOWER) : primary_signal;
2746 } else {
2747 secondary_signal = primary_signal = GetSignalType(tile, TRACK_LOWER);
2748 }
2749
2750 td->str = signal_type[secondary_signal][primary_signal];
2751 break;
2752 }
2753
2754 case RAIL_TILE_DEPOT:
2755 td->str = STR_LAI_RAIL_DESCRIPTION_TRAIN_DEPOT;
2756 if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) {
2757 if (td->rail_speed > 0) {
2758 td->rail_speed = min(td->rail_speed, 61);
2759 } else {
2760 td->rail_speed = 61;
2761 }
2762 }
2763 td->build_date = Depot::GetByTile(tile)->build_date;
2764 break;
2765
2766 default:
2767 NOT_REACHED();
2768 }
2769 }
2770
2771 static void ChangeTileOwner_Track(TileIndex tile, Owner old_owner, Owner new_owner)
2772 {
2773 if (!IsTileOwner(tile, old_owner)) return;
2774
2775 if (new_owner != INVALID_OWNER) {
2776 /* Update company infrastructure counts. No need to dirty windows here, we'll redraw the whole screen anyway. */
2777 uint num_pieces = 1;
2778 if (IsPlainRail(tile)) {
2779 TrackBits bits = GetTrackBits(tile);
2780 num_pieces = CountBits(bits);
2781 if (TracksOverlap(bits)) num_pieces *= num_pieces;
2782 }
2783 RailType rt = GetRailType(tile);
2784 Company::Get(old_owner)->infrastructure.rail[rt] -= num_pieces;
2785 Company::Get(new_owner)->infrastructure.rail[rt] += num_pieces;
2786
2787 if (HasSignals(tile)) {
2788 uint num_sigs = CountBits(GetPresentSignals(tile));
2789 Company::Get(old_owner)->infrastructure.signal -= num_sigs;
2790 Company::Get(new_owner)->infrastructure.signal += num_sigs;
2791 }
2792
2793 SetTileOwner(tile, new_owner);
2794 } else {
2795 DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
2796 }
2797 }
2798
2799 static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 };
2800 static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 };
2801 static const int8 _deltacoord_leaveoffset[8] = {
2802 -1, 0, 1, 0, /* x */
2803 0, 1, 0, -1 /* y */
2804 };
2805
2806
2813 int TicksToLeaveDepot(const Train *v)
2814 {
2815 DiagDirection dir = GetRailDepotDirection(v->tile);
2816 int length = v->CalcNextVehicleOffset();
2817
2818 switch (dir) {
2819 case DIAGDIR_NE: return ((int)(v->x_pos & 0x0F) - ((_fractcoords_enter[dir] & 0x0F) - (length + 1)));
2820 case DIAGDIR_SE: return -((int)(v->y_pos & 0x0F) - ((_fractcoords_enter[dir] >> 4) + (length + 1)));
2821 case DIAGDIR_SW: return -((int)(v->x_pos & 0x0F) - ((_fractcoords_enter[dir] & 0x0F) + (length + 1)));
2822 default:
2823 case DIAGDIR_NW: return ((int)(v->y_pos & 0x0F) - ((_fractcoords_enter[dir] >> 4) - (length + 1)));
2824 }
2825
2826 return 0; // make compilers happy
2827 }
2828
2833 static VehicleEnterTileStatus VehicleEnter_Track(Vehicle *u, TileIndex tile, int x, int y)
2834 {
2835 /* this routine applies only to trains in depot tiles */
2836 if (u->type != VEH_TRAIN || !IsRailDepotTile(tile)) return VETSB_CONTINUE;
2837
2838 Train *v = Train::From(u);
2839
2840 /* depot direction */
2841 DiagDirection dir = GetRailDepotDirection(tile);
2842
2843 /* Calculate the point where the following wagon should be activated. */
2844 int length = v->CalcNextVehicleOffset();
2845
2846 byte fract_coord_leave =
2847 ((_fractcoords_enter[dir] & 0x0F) + // x
2848 (length + 1) * _deltacoord_leaveoffset[dir]) +
2849 (((_fractcoords_enter[dir] >> 4) + // y
2850 ((length + 1) * _deltacoord_leaveoffset[dir + 4])) << 4);
2851
2852 byte fract_coord = (x & 0xF) + ((y & 0xF) << 4);
2853
2854 if (_fractcoords_behind[dir] == fract_coord) {
2855 /* make sure a train is not entering the tile from behind */
2856 return VETSB_CANNOT_ENTER;
2857 } else if (_fractcoords_enter[dir] == fract_coord) {
2858 if (DiagDirToDir(ReverseDiagDir(dir)) == v->direction) {
2859 /* enter the depot */
2860 v->track = TRACK_BIT_DEPOT,
2861 v->vehstatus |= VS_HIDDEN; // hide it
2862 v->direction = ReverseDir(v->direction);
2863 if (v->Next() == NULL) VehicleEnterDepot(v->First());
2864 v->tile = tile;
2865
2866 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
2867 return VETSB_ENTERED_WORMHOLE;
2868 }
2869 } else if (fract_coord_leave == fract_coord) {
2870 if (DiagDirToDir(dir) == v->direction) {
2871 /* leave the depot? */
2872 if ((v = v->Next()) != NULL) {
2873 v->vehstatus &= ~VS_HIDDEN;
2874 v->track = (DiagDirToAxis(dir) == AXIS_X ? TRACK_BIT_X : TRACK_BIT_Y);
2875 }
2876 }
2877 }
2878
2879 return VETSB_CONTINUE;
2880 }
2881
2893 static CommandCost TestAutoslopeOnRailTile(TileIndex tile, uint flags, int z_old, Slope tileh_old, int z_new, Slope tileh_new, TrackBits rail_bits)
2894 {
2895 if (!_settings_game.construction.build_on_slopes || !AutoslopeEnabled()) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
2896
2897 /* Is the slope-rail_bits combination valid in general? I.e. is it safe to call GetRailFoundation() ? */
2898 if (CheckRailSlope(tileh_new, rail_bits, TRACK_BIT_NONE, tile).Failed()) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
2899
2900 /* Get the slopes on top of the foundations */
2901 z_old += ApplyFoundationToSlope(GetRailFoundation(tileh_old, rail_bits), &tileh_old);
2902 z_new += ApplyFoundationToSlope(GetRailFoundation(tileh_new, rail_bits), &tileh_new);
2903
2904 Corner track_corner;
2905 switch (rail_bits) {
2906 case TRACK_BIT_LEFT: track_corner = CORNER_W; break;
2907 case TRACK_BIT_LOWER: track_corner = CORNER_S; break;
2908 case TRACK_BIT_RIGHT: track_corner = CORNER_E; break;
2909 case TRACK_BIT_UPPER: track_corner = CORNER_N; break;
2910
2911 /* Surface slope must not be changed */
2912 default:
2913 if (z_old != z_new || tileh_old != tileh_new) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
2914 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
2915 }
2916
2917 /* The height of the track_corner must not be changed. The rest ensures GetRailFoundation() already. */
2918 z_old += GetSlopeZInCorner(RemoveHalftileSlope(tileh_old), track_corner);
2919 z_new += GetSlopeZInCorner(RemoveHalftileSlope(tileh_new), track_corner);
2920 if (z_old != z_new) return_cmd_error(STR_ERROR_MUST_REMOVE_RAILROAD_TRACK);
2921
2922 CommandCost cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
2923 /* Make the ground dirty, if surface slope has changed */
2924 if (tileh_old != tileh_new) {
2925 /* If there is flat water on the lower halftile add the cost for clearing it */
2926 if (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh_old)) cost.AddCost(_price[PR_CLEAR_WATER]);
2927 if ((flags & DC_EXEC) != 0) SetRailGroundType(tile, RAIL_GROUND_BARREN);
2928 }
2929 return cost;
2930 }
2931
2935 static Vehicle *EnsureNoShipProc(Vehicle *v, void *data)
2936 {
2937 return v->type == VEH_SHIP ? v : NULL;
2938 }
2939
2940 static CommandCost TerraformTile_Track(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
2941 {
2942 int z_old;
2943 Slope tileh_old = GetTileSlope(tile, &z_old);
2944 if (IsPlainRail(tile)) {
2945 TrackBits rail_bits = GetTrackBits(tile);
2946 /* Is there flat water on the lower halftile that must be cleared expensively? */
2947 bool was_water = (GetRailGroundType(tile) == RAIL_GROUND_WATER && IsSlopeWithOneCornerRaised(tileh_old));
2948
2949 /* Allow clearing the water only if there is no ship */
2950 if (was_water && HasVehicleOnPos(tile, NULL, &EnsureNoShipProc)) return_cmd_error(STR_ERROR_SHIP_IN_THE_WAY);
2951
2952 /* First test autoslope. However if it succeeds we still have to test the rest, because non-autoslope terraforming is cheaper. */
2953 CommandCost autoslope_result = TestAutoslopeOnRailTile(tile, flags, z_old, tileh_old, z_new, tileh_new, rail_bits);
2954
2955 /* When there is only a single horizontal/vertical track, one corner can be terraformed. */
2956 Corner allowed_corner;
2957 switch (rail_bits) {
2958 case TRACK_BIT_RIGHT: allowed_corner = CORNER_W; break;
2959 case TRACK_BIT_UPPER: allowed_corner = CORNER_S; break;
2960 case TRACK_BIT_LEFT: allowed_corner = CORNER_E; break;
2961 case TRACK_BIT_LOWER: allowed_corner = CORNER_N; break;
2962 default: return autoslope_result;
2963 }
2964
2965 Foundation f_old = GetRailFoundation(tileh_old, rail_bits);
2966
2967 /* Do not allow terraforming if allowed_corner is part of anti-zig-zag foundations */
2968 if (tileh_old != SLOPE_NS && tileh_old != SLOPE_EW && IsSpecialRailFoundation(f_old)) return autoslope_result;
2969
2970 /* Everything is valid, which only changes allowed_corner */
2971 for (Corner corner = (Corner)0; corner < CORNER_END; corner = (Corner)(corner + 1)) {
2972 if (allowed_corner == corner) continue;
2973 if (z_old + GetSlopeZInCorner(tileh_old, corner) != z_new + GetSlopeZInCorner(tileh_new, corner)) return autoslope_result;
2974 }
2975
2976 /* Make the ground dirty */
2977 if ((flags & DC_EXEC) != 0) SetRailGroundType(tile, RAIL_GROUND_BARREN);
2978
2979 /* allow terraforming */
2980 return CommandCost(EXPENSES_CONSTRUCTION, was_water ? _price[PR_CLEAR_WATER] : (Money)0);
2981 } else if (_settings_game.construction.build_on_slopes && AutoslopeEnabled() &&
2982 AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, GetRailDepotDirection(tile))) {
2983 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
2984 }
2985 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
2986 }
2987
2988
2989 extern const TileTypeProcs _tile_type_rail_procs = {
2990 DrawTile_Track, // draw_tile_proc
2991 GetSlopePixelZ_Track, // get_slope_z_proc
2992 ClearTile_Track, // clear_tile_proc
2993 NULL, // add_accepted_cargo_proc
2994 GetTileDesc_Track, // get_tile_desc_proc
2995 GetTileTrackStatus_Track, // get_tile_track_status_proc
2996 ClickTile_Track, // click_tile_proc
2997 NULL, // animate_tile_proc
2998 TileLoop_Track, // tile_loop_proc
2999 ChangeTileOwner_Track, // change_tile_owner_proc
3000 NULL, // add_produced_cargo_proc
3001 VehicleEnter_Track, // vehicle_enter_tile_proc
3002 GetFoundation_Track, // get_foundation_proc
3003 TerraformTile_Track, // terraform_tile_proc
3004 };