1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
|
/* $Id$ */
/** @file road_cmd.cpp */
#include "stdafx.h"
#include "openttd.h"
#include "bridge_map.h"
#include "bridge.h"
#include "cmd_helper.h"
#include "rail_map.h"
#include "road_map.h"
#include "sprite.h"
#include "table/sprites.h"
#include "table/strings.h"
#include "functions.h"
#include "map.h"
#include "landscape.h"
#include "tile.h"
#include "town_map.h"
#include "vehicle.h"
#include "viewport.h"
#include "command.h"
#include "player.h"
#include "town.h"
#include "gfx.h"
#include "sound.h"
#include "yapf/yapf.h"
#include "depot.h"
#include "newgrf.h"
static uint CountRoadBits(RoadBits r)
{
uint count = 0;
if (r & ROAD_NW) ++count;
if (r & ROAD_SW) ++count;
if (r & ROAD_SE) ++count;
if (r & ROAD_NE) ++count;
return count;
}
bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, bool *edge_road, RoadType rt)
{
RoadBits present;
RoadBits n;
*edge_road = true;
if (_game_mode == GM_EDITOR || remove == ROAD_NONE) return true;
/* Only do the special processing for actual players. */
if (!IsValidPlayer(_current_player)) return true;
/* Only do the special processing if the road is owned
* by a town */
if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
if (_cheats.magic_bulldozer.value) return true;
/* Get a bitmask of which neighbouring roads has a tile */
n = ROAD_NONE;
present = GetAnyRoadBits(tile, rt);
if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile, -1, 0), rt) & ROAD_SW) n |= ROAD_NE;
if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1), rt) & ROAD_NW) n |= ROAD_SE;
if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0), rt) & ROAD_NE) n |= ROAD_SW;
if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0, -1), rt) & ROAD_SE) n |= ROAD_NW;
/* If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
* then allow it */
if ((n & (n - 1)) != 0 && (n & remove) != 0) {
Town *t;
*edge_road = false;
/* you can remove all kind of roads with extra dynamite */
if (_patches.extra_dynamite) return true;
t = ClosestTownFromTile(tile, (uint)-1);
SetDParam(0, t->index);
_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
return false;
}
return true;
}
static bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, bool *edge_road, RoadType rt)
{
return CheckAllowRemoveRoad(tile, remove, GetRoadOwner(tile, rt), edge_road, rt);
}
/** Delete a piece of road.
* @param tile tile where to remove road from
* @param flags operation to perform
* @param p1 bit 0..3 road pieces to remove (RoadBits)
* bit 4..5 road type
* @param p2 unused
*/
int32 CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
/* cost for removing inner/edge -roads */
static const uint16 road_remove_cost[2] = {50, 18};
Town *t;
/* true if the roadpiece was always removeable,
* false if it was a center piece. Affects town ratings drop */
bool edge_road;
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
RoadType rt = (RoadType)GB(p1, 4, 2);
if (!IsTileType(tile, MP_STREET) || !IsValidRoadType(rt)) return CMD_ERROR;
Owner owner = GetRoadOwner(tile, rt);
if (owner == OWNER_TOWN && _game_mode != GM_EDITOR) {
t = GetTownByTile(tile);
} else {
t = NULL;
}
RoadBits pieces = Extract<RoadBits, 0>(p1);
RoadTypes rts = GetRoadTypes(tile);
/* The tile doesn't have the given road type */
if (!HASBIT(rts, rt)) return CMD_ERROR;
if (!CheckAllowRemoveRoad(tile, pieces, &edge_road, rt)) return CMD_ERROR;
if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
/* check if you're allowed to remove the street owned by a town
* removal allowance depends on difficulty setting */
if (!CheckforTownRating(flags, t, ROAD_REMOVE)) return CMD_ERROR;
switch (GetRoadTileType(tile)) {
case ROAD_TILE_NORMAL: {
RoadBits present = GetRoadBits(tile, rt);
RoadBits c = pieces;
if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
if (GetTileSlope(tile, NULL) != SLOPE_FLAT &&
(present == ROAD_Y || present == ROAD_X)) {
c |= (RoadBits)((c & 0xC) >> 2);
c |= (RoadBits)((c & 0x3) << 2);
}
/* limit the bits to delete to the existing bits. */
c &= present;
if (c == ROAD_NONE) return CMD_ERROR;
if (flags & DC_EXEC) {
ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
present ^= c;
if (present == ROAD_NONE) {
RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
if (rts == ROADTYPES_NONE) {
DoClearSquare(tile);
} else {
SetRoadBits(tile, ROAD_NONE, rt);
SetRoadTypes(tile, rts);
}
} else {
SetRoadBits(tile, present, rt);
MarkTileDirtyByTile(tile);
}
}
return CountRoadBits(c) * _price.remove_road;
}
case ROAD_TILE_CROSSING: {
if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) {
return CMD_ERROR;
}
if (flags & DC_EXEC) {
if (rt == ROADTYPE_ROAD) {
ChangeTownRating(t, -road_remove_cost[(byte)edge_road], RATING_ROAD_MINIMUM);
}
RoadTypes rts = GetRoadTypes(tile) & ComplementRoadTypes(RoadTypeToRoadTypes(rt));
if (rts == ROADTYPES_NONE) {
MakeRailNormal(tile, GetTileOwner(tile), GetCrossingRailBits(tile), GetRailType(tile));
} else {
SetRoadTypes(tile, rts);
}
MarkTileDirtyByTile(tile);
YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
}
return _price.remove_road * 2;
}
default:
case ROAD_TILE_DEPOT:
return CMD_ERROR;
}
}
static const RoadBits _valid_tileh_slopes_road[][15] = {
/* set of normal ones */
{
ROAD_ALL, ROAD_NONE, ROAD_NONE,
ROAD_X, ROAD_NONE, ROAD_NONE, // 3, 4, 5
ROAD_Y, ROAD_NONE, ROAD_NONE,
ROAD_Y, ROAD_NONE, ROAD_NONE, // 9, 10, 11
ROAD_X, ROAD_NONE, ROAD_NONE
},
/* allowed road for an evenly raised platform */
{
ROAD_NONE,
ROAD_SW | ROAD_NW,
ROAD_SW | ROAD_SE,
ROAD_Y | ROAD_SW,
ROAD_SE | ROAD_NE, // 4
ROAD_ALL,
ROAD_X | ROAD_SE,
ROAD_ALL,
ROAD_NW | ROAD_NE, // 8
ROAD_X | ROAD_NW,
ROAD_ALL,
ROAD_ALL,
ROAD_Y | ROAD_NE, // 12
ROAD_ALL,
ROAD_ALL
},
};
static uint32 CheckRoadSlope(Slope tileh, RoadBits* pieces, RoadBits existing)
{
RoadBits road_bits;
if (IsSteepSlope(tileh)) {
if (existing == 0) {
/* force full pieces. */
*pieces |= (RoadBits)((*pieces & 0xC) >> 2);
*pieces |= (RoadBits)((*pieces & 0x3) << 2);
if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
}
return CMD_ERROR;
}
road_bits = *pieces | existing;
/* no special foundation */
if ((~_valid_tileh_slopes_road[0][tileh] & road_bits) == 0) {
/* force that all bits are set when we have slopes */
if (tileh != SLOPE_FLAT) *pieces |= _valid_tileh_slopes_road[0][tileh];
return 0; // no extra cost
}
/* foundation is used. Whole tile is leveled up */
if ((~_valid_tileh_slopes_road[1][tileh] & road_bits) == 0) {
return existing != 0 ? 0 : _price.terraform;
}
/* partly leveled up tile, only if there's no road on that tile */
if (existing == 0 && (tileh == SLOPE_W || tileh == SLOPE_S || tileh == SLOPE_E || tileh == SLOPE_N)) {
/* force full pieces. */
*pieces |= (RoadBits)((*pieces & 0xC) >> 2);
*pieces |= (RoadBits)((*pieces & 0x3) << 2);
if (*pieces == ROAD_X || *pieces == ROAD_Y) return _price.terraform;
}
return CMD_ERROR;
}
/** Build a piece of road.
* @param tile tile where to build road
* @param flags operation to perform
* @param p1 bit 0..3 road pieces to build (RoadBits)
* bit 4..5 road type
* @param p2 the town that is building the road (0 if not applicable)
*/
int32 CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
int32 cost = 0;
int32 ret;
RoadBits existing = ROAD_NONE;
Slope tileh;
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
* if a non-player is building the road */
if ((IsValidPlayer(_current_player) && p2 != 0) || (_current_player == OWNER_TOWN && !IsValidTownID(p2))) return CMD_ERROR;
RoadBits pieces = Extract<RoadBits, 0>(p1);
RoadType rt = (RoadType)GB(p1, 4, 2);
if (!IsValidRoadType(rt)) return CMD_ERROR;
tileh = GetTileSlope(tile, NULL);
switch (GetTileType(tile)) {
case MP_STREET:
switch (GetRoadTileType(tile)) {
case ROAD_TILE_NORMAL:
if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
if (!HASBIT(GetRoadTypes(tile), rt)) break;
existing = GetRoadBits(tile, rt);
if ((existing & pieces) == pieces) {
return_cmd_error(STR_1007_ALREADY_BUILT);
}
if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
break;
case ROAD_TILE_CROSSING:
if (HASBIT(GetRoadTypes(tile), rt)) return_cmd_error(STR_1007_ALREADY_BUILT);
if (pieces & ComplementRoadBits(GetCrossingRoadBits(tile))) goto do_clear;
break;
default:
case ROAD_TILE_DEPOT:
goto do_clear;
}
break;
case MP_RAILWAY: {
Axis roaddir;
if (IsSteepSlope(tileh)) {
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
}
#define M(x) (1 << (x))
/* Level crossings may only be built on these slopes */
if (!HASBIT(M(SLOPE_SEN) | M(SLOPE_ENW) | M(SLOPE_NWS) | M(SLOPE_NS) | M(SLOPE_WSE) | M(SLOPE_EW) | M(SLOPE_FLAT), tileh)) {
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
}
#undef M
if (GetRailTileType(tile) != RAIL_TILE_NORMAL) goto do_clear;
switch (GetTrackBits(tile)) {
case TRACK_BIT_X:
if (pieces & ROAD_X) goto do_clear;
roaddir = AXIS_Y;
break;
case TRACK_BIT_Y:
if (pieces & ROAD_Y) goto do_clear;
roaddir = AXIS_X;
break;
default: goto do_clear;
}
if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
if (flags & DC_EXEC) {
YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
MakeRoadCrossing(tile, _current_player, _current_player, _current_player, GetTileOwner(tile), roaddir, GetRailType(tile), RoadTypeToRoadTypes(rt), p2);
MarkTileDirtyByTile(tile);
}
return _price.build_road * 2;
}
default:
do_clear:;
ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
if (CmdFailed(ret)) return ret;
cost += ret;
}
ret = CheckRoadSlope(tileh, &pieces, existing);
/* Return an error if we need to build a foundation (ret != 0) but the
* current patch-setting is turned off (or stupid AI@work) */
if (CmdFailed(ret) || (ret != 0 && (!_patches.build_on_slopes || _is_old_ai_player)))
return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
cost += ret;
if (IsTileType(tile, MP_STREET)) {
/* Don't put the pieces that already exist */
pieces &= ComplementRoadBits(existing);
}
cost += CountRoadBits(pieces) * _price.build_road;
if (flags & DC_EXEC) {
if (IsTileType(tile, MP_STREET)) {
if (existing == ROAD_NONE) {
SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
SetRoadOwner(tile, rt, _current_player);
}
SetRoadBits(tile, existing | pieces, rt);
} else {
MakeRoadNormal(tile, pieces, RoadTypeToRoadTypes(rt), p2, _current_player, _current_player, _current_player);
}
MarkTileDirtyByTile(tile);
}
return cost;
}
/**
* Switches the rail type on a level crossing.
* @param tile The tile on which the railtype is to be convert.
* @param totype The railtype we want to convert to
* @param exec Switches between test and execute mode
* @return The cost and state of the operation
* @retval CMD_ERROR An error occured during the operation.
*/
int32 DoConvertStreetRail(TileIndex tile, RailType totype, bool exec)
{
/* not a railroad crossing? */
if (!IsLevelCrossing(tile)) return CMD_ERROR;
/* not owned by me? */
if (!CheckTileOwnership(tile) || !EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
if (GetRailType(tile) == totype) return CMD_ERROR;
/* 'hidden' elrails can't be downgraded to normal rail when elrails are disabled */
if (_patches.disable_elrails && totype == RAILTYPE_RAIL && GetRailType(tile) == RAILTYPE_ELECTRIC) return CMD_ERROR;
if (exec) {
SetRailType(tile, totype);
MarkTileDirtyByTile(tile);
YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetCrossingRailBits(tile)));
}
return _price.build_rail / 2;
}
/** Build a long piece of road.
* @param end_tile end tile of drag
* @param flags operation to perform
* @param p1 start tile of drag
* @param p2 various bitstuffed elements
* - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1)
* - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2)
* - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4)
* - p2 = (bit 3 + 4) - road type
*/
int32 CmdBuildLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
{
TileIndex start_tile, tile;
int32 cost, ret;
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
if (p1 >= MapSize()) return CMD_ERROR;
start_tile = p1;
RoadType rt = (RoadType)GB(p2, 3, 2);
if (!IsValidRoadType(rt)) return CMD_ERROR;
/* Only drag in X or Y direction dictated by the direction variable */
if (!HASBIT(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
if (HASBIT(p2, 2) && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
/* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
if (start_tile > end_tile || (start_tile == end_tile && HASBIT(p2, 0))) {
TileIndex t = start_tile;
start_tile = end_tile;
end_tile = t;
p2 ^= IS_INT_INSIDE(p2 & 3, 1, 3) ? 3 : 0;
}
cost = 0;
tile = start_tile;
/* Start tile is the small number. */
for (;;) {
RoadBits bits = HASBIT(p2, 2) ? ROAD_Y : ROAD_X;
if (tile == end_tile && !HASBIT(p2, 1)) bits &= ROAD_NW | ROAD_NE;
if (tile == start_tile && HASBIT(p2, 0)) bits &= ROAD_SE | ROAD_SW;
ret = DoCommand(tile, rt << 4 | bits, 0, flags, CMD_BUILD_ROAD);
if (CmdFailed(ret)) {
if (_error_message != STR_1007_ALREADY_BUILT) return CMD_ERROR;
_error_message = INVALID_STRING_ID;
} else {
cost += ret;
}
if (tile == end_tile) break;
tile += HASBIT(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
}
return (cost == 0) ? CMD_ERROR : cost;
}
/** Remove a long piece of road.
* @param end_tile end tile of drag
* @param flags operation to perform
* @param p1 start tile of drag
* @param p2 various bitstuffed elements
* - p2 = (bit 0) - start tile starts in the 2nd half of tile (p2 & 1)
* - p2 = (bit 1) - end tile starts in the 2nd half of tile (p2 & 2)
* - p2 = (bit 2) - direction: 0 = along x-axis, 1 = along y-axis (p2 & 4)
* - p2 = (bit 3 + 4) - road type
*/
int32 CmdRemoveLongRoad(TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
{
TileIndex start_tile, tile;
int32 cost, ret;
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
if (p1 >= MapSize()) return CMD_ERROR;
start_tile = p1;
RoadType rt = (RoadType)GB(p2, 3, 2);
if (!IsValidRoadType(rt)) return CMD_ERROR;
/* Only drag in X or Y direction dictated by the direction variable */
if (!HASBIT(p2, 2) && TileY(start_tile) != TileY(end_tile)) return CMD_ERROR; // x-axis
if (HASBIT(p2, 2) && TileX(start_tile) != TileX(end_tile)) return CMD_ERROR; // y-axis
/* Swap start and ending tile, also the half-tile drag var (bit 0 and 1) */
if (start_tile > end_tile || (start_tile == end_tile && HASBIT(p2, 0))) {
TileIndex t = start_tile;
start_tile = end_tile;
end_tile = t;
p2 ^= IS_INT_INSIDE(p2 & 3, 1, 3) ? 3 : 0;
}
cost = 0;
tile = start_tile;
/* Start tile is the small number. */
for (;;) {
RoadBits bits = HASBIT(p2, 2) ? ROAD_Y : ROAD_X;
if (tile == end_tile && !HASBIT(p2, 1)) bits &= ROAD_NW | ROAD_NE;
if (tile == start_tile && HASBIT(p2, 0)) bits &= ROAD_SE | ROAD_SW;
/* try to remove the halves. */
if (bits != 0) {
ret = DoCommand(tile, rt << 4 | bits, 0, flags, CMD_REMOVE_ROAD);
if (!CmdFailed(ret)) cost += ret;
}
if (tile == end_tile) break;
tile += HASBIT(p2, 2) ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
}
return (cost == 0) ? CMD_ERROR : cost;
}
/** Build a road depot.
* @param tile tile where to build the depot
* @param flags operation to perform
* @param p1 bit 0..1 entrance direction (DiagDirection)
* bit 2..3 road type
* @param p2 unused
*
* @todo When checking for the tile slope,
* distingush between "Flat land required" and "land sloped in wrong direction"
*/
int32 CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
int32 cost;
Depot *dep;
Slope tileh;
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
DiagDirection dir = Extract<DiagDirection, 0>(p1);
RoadType rt = (RoadType)GB(p1, 2, 2);
if (!IsValidRoadType(rt)) return CMD_ERROR;
tileh = GetTileSlope(tile, NULL);
if (tileh != SLOPE_FLAT && (
!_patches.build_on_slopes ||
IsSteepSlope(tileh) ||
!CanBuildDepotByTileh(dir, tileh)
)) {
return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
}
cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
if (CmdFailed(cost)) return CMD_ERROR;
if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
dep = AllocateDepot();
if (dep == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
dep->xy = tile;
dep->town_index = ClosestTownFromTile(tile, (uint)-1)->index;
MakeRoadDepot(tile, _current_player, dir, rt);
MarkTileDirtyByTile(tile);
}
return cost + _price.build_road_depot;
}
static int32 RemoveRoadDepot(TileIndex tile, uint32 flags)
{
if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER)
return CMD_ERROR;
if (!EnsureNoVehicle(tile)) return CMD_ERROR;
if (flags & DC_EXEC) DeleteDepot(GetDepotByTile(tile));
return _price.remove_road_depot;
}
static int32 ClearTile_Road(TileIndex tile, byte flags)
{
switch (GetRoadTileType(tile)) {
case ROAD_TILE_NORMAL: {
RoadBits b = GetAllRoadBits(tile);
#define M(x) (1 << (x))
/* Clear the road if only one piece is on the tile OR the AI tries
* to clear town road OR we are not using the DC_AUTO flag */
if ((M(b) & (M(ROAD_NW) | M(ROAD_SW) | M(ROAD_SE) | M(ROAD_NE))) ||
((flags & DC_AI_BUILDING) && IsTileOwner(tile, OWNER_TOWN)) ||
!(flags & DC_AUTO)
) {
return DoCommand(tile, b, 0, flags, CMD_REMOVE_ROAD);
}
return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
}
#undef M
case ROAD_TILE_CROSSING: {
int32 ret;
if (flags & DC_AUTO) return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
ret = DoCommand(tile, GetCrossingRoadBits(tile), 0, flags, CMD_REMOVE_ROAD);
if (CmdFailed(ret)) return CMD_ERROR;
if (flags & DC_EXEC) {
DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
}
return ret;
}
default:
case ROAD_TILE_DEPOT:
if (flags & DC_AUTO) {
return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
}
return RemoveRoadDepot(tile, flags);
}
}
struct DrawRoadTileStruct {
uint16 image;
byte subcoord_x;
byte subcoord_y;
};
#include "table/road_land.h"
uint GetRoadFoundation(Slope tileh, RoadBits bits)
{
uint i;
/* normal level sloped building */
if (!IsSteepSlope(tileh) &&
(~_valid_tileh_slopes_road[1][tileh] & bits) == 0) {
return tileh;
}
/* inclined sloped building */
switch (bits) {
case ROAD_X: i = 0; break;
case ROAD_Y: i = 1; break;
default: return 0;
}
switch (tileh) {
case SLOPE_W:
case SLOPE_STEEP_W: i += 0; break;
case SLOPE_S:
case SLOPE_STEEP_S: i += 2; break;
case SLOPE_E:
case SLOPE_STEEP_E: i += 4; break;
case SLOPE_N:
case SLOPE_STEEP_N: i += 6; break;
default: return 0;
}
return i + 15;
}
const byte _road_sloped_sprites[14] = {
0, 0, 2, 0,
0, 1, 0, 0,
3, 0, 0, 0,
0, 0
};
/**
* Whether to draw unpaved roads regardless of the town zone.
* By default, OpenTTD always draws roads as unpaved if they are on a desert
* tile or above the snowline. Newgrf files, however, can set a bit that allows
* paved roads to be built on desert tiles as they would be on grassy tiles.
*
* @param tile The tile the road is on
* @param roadside What sort of road this is
* @return True if the road should be drawn unpaved regardless of the roadside.
*/
static bool AlwaysDrawUnpavedRoads(TileIndex tile, Roadside roadside)
{
return (IsOnSnow(tile) &&
!(_opt.landscape == LT_TROPIC && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) &&
roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
}
/**
* Draw ground sprite and road pieces
* @param ti TileInfo
*/
static void DrawRoadBits(TileInfo* ti)
{
RoadBits road = GetRoadBits(ti->tile, ROADTYPE_ROAD);
const DrawRoadTileStruct *drts;
SpriteID image = 0;
SpriteID pal = PAL_NONE;
Roadside roadside;
if (ti->tileh != SLOPE_FLAT) {
int foundation = GetRoadFoundation(ti->tileh, road);
if (foundation != 0) DrawFoundation(ti, foundation);
/* DrawFoundation() modifies ti.
* Default sloped sprites.. */
if (ti->tileh != SLOPE_FLAT) image = _road_sloped_sprites[ti->tileh - 1] + 0x53F;
}
if (image == 0) image = _road_tile_sprites_1[road];
roadside = GetRoadside(ti->tile);
if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
image += 19;
} else {
switch (roadside) {
case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
case ROADSIDE_GRASS: break;
case ROADSIDE_GRASS_ROAD_WORKS: break;
default: image -= 19; break; // Paved
}
}
DrawGroundSprite(image, pal);
if (HasRoadWorks(ti->tile)) {
/* Road works */
DrawGroundSprite(road & ROAD_X ? SPR_EXCAVATION_X : SPR_EXCAVATION_Y, PAL_NONE);
return;
}
/* Return if full detail is disabled, or we are zoomed fully out. */
if (!HASBIT(_display_opt, DO_FULL_DETAIL) || _cur_dpi->zoom > ZOOM_LVL_DETAIL) return;
/* Draw extra details. */
for (drts = _road_display_table[roadside][road]; drts->image != 0; drts++) {
int x = ti->x | drts->subcoord_x;
int y = ti->y | drts->subcoord_y;
byte z = ti->z;
if (ti->tileh != SLOPE_FLAT) z = GetSlopeZ(x, y);
AddSortableSpriteToDraw(drts->image, PAL_NONE, x, y, 2, 2, 0x10, z);
}
}
static void DrawTile_Road(TileInfo *ti)
{
switch (GetRoadTileType(ti->tile)) {
case ROAD_TILE_NORMAL:
DrawRoadBits(ti);
break;
case ROAD_TILE_CROSSING: {
SpriteID image;
SpriteID pal = PAL_NONE;
Roadside roadside = GetRoadside(ti->tile);
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh);
image = GetRailTypeInfo(GetRailType(ti->tile))->base_sprites.crossing;
if (GetCrossingRoadAxis(ti->tile) == AXIS_X) image++;
if (IsCrossingBarred(ti->tile)) image += 2;
if (AlwaysDrawUnpavedRoads(ti->tile, roadside)) {
image += 8;
} else {
switch (roadside) {
case ROADSIDE_BARREN: pal = PALETTE_TO_BARE_LAND; break;
case ROADSIDE_GRASS: break;
default: image += 4; break; // Paved
}
}
DrawGroundSprite(image, pal);
if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC) DrawCatenary(ti);
break;
}
default:
case ROAD_TILE_DEPOT: {
const DrawTileSprites* dts;
const DrawTileSeqStruct* dtss;
SpriteID palette;
if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh);
palette = PLAYER_SPRITE_COLOR(GetTileOwner(ti->tile));
dts = &_road_depot[GetRoadDepotDirection(ti->tile)];
DrawGroundSprite(dts->ground_sprite, PAL_NONE);
for (dtss = dts->seq; dtss->image != 0; dtss++) {
SpriteID image = dtss->image;
SpriteID pal;
if (HASBIT(_transparent_opt, TO_BUILDINGS)) {
SETBIT(image, PALETTE_MODIFIER_TRANSPARENT);
pal = PALETTE_TO_TRANSPARENT;
} else if (HASBIT(image, PALETTE_MODIFIER_COLOR)) {
pal = palette;
} else {
pal = PAL_NONE;
}
AddSortableSpriteToDraw(
image, pal,
ti->x + dtss->delta_x, ti->y + dtss->delta_y,
dtss->size_x, dtss->size_y,
dtss->size_z, ti->z
);
}
break;
}
}
DrawBridgeMiddle(ti);
}
void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
{
SpriteID palette = PLAYER_SPRITE_COLOR(_local_player);
const DrawTileSprites* dts = &_road_depot[dir];
const DrawTileSeqStruct* dtss;
x += 33;
y += 17;
DrawSprite(dts->ground_sprite, PAL_NONE, x, y);
for (dtss = dts->seq; dtss->image != 0; dtss++) {
Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
SpriteID image = dtss->image;
DrawSprite(image, HASBIT(image, PALETTE_MODIFIER_COLOR) ? palette : PAL_NONE, x + pt.x, y + pt.y);
}
}
static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
{
uint z;
Slope tileh = GetTileSlope(tile, &z);
if (tileh == SLOPE_FLAT) return z;
if (GetRoadTileType(tile) == ROAD_TILE_NORMAL) {
uint f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
if (f != 0) {
if (IsSteepSlope(tileh)) {
z += TILE_HEIGHT;
} else if (f < 15) {
return z + TILE_HEIGHT; // leveled foundation
}
tileh = _inclined_tileh[f - 15]; // inclined foundation
}
return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
} else {
return z + TILE_HEIGHT;
}
}
static Slope GetSlopeTileh_Road(TileIndex tile, Slope tileh)
{
if (tileh == SLOPE_FLAT) return SLOPE_FLAT;
if (GetRoadTileType(tile) == ROAD_TILE_NORMAL) {
uint f = GetRoadFoundation(tileh, GetAllRoadBits(tile));
if (f == 0) return tileh;
if (f < 15) return SLOPE_FLAT; // leveled foundation
return _inclined_tileh[f - 15]; // inclined foundation
} else {
return SLOPE_FLAT;
}
}
static void GetAcceptedCargo_Road(TileIndex tile, AcceptedCargo ac)
{
/* not used */
}
static void AnimateTile_Road(TileIndex tile)
{
if (IsLevelCrossing(tile)) MarkTileDirtyByTile(tile);
}
static const Roadside _town_road_types[][2] = {
{ ROADSIDE_GRASS, ROADSIDE_GRASS },
{ ROADSIDE_PAVED, ROADSIDE_PAVED },
{ ROADSIDE_PAVED, ROADSIDE_PAVED },
{ ROADSIDE_TREES, ROADSIDE_TREES },
{ ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
};
static const Roadside _town_road_types_2[][2] = {
{ ROADSIDE_GRASS, ROADSIDE_GRASS },
{ ROADSIDE_PAVED, ROADSIDE_PAVED },
{ ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
{ ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED },
{ ROADSIDE_STREET_LIGHTS, ROADSIDE_PAVED }
};
static void TileLoop_Road(TileIndex tile)
{
switch (_opt.landscape) {
case LT_ARCTIC:
if (IsOnSnow(tile) != (GetTileZ(tile) > GetSnowLine())) {
ToggleSnow(tile);
MarkTileDirtyByTile(tile);
}
break;
case LT_TROPIC:
if (GetTropicZone(tile) == TROPICZONE_DESERT && !IsOnDesert(tile)) {
ToggleDesert(tile);
MarkTileDirtyByTile(tile);
}
break;
}
if (GetRoadTileType(tile) == ROAD_TILE_DEPOT) return;
if (!HasRoadWorks(tile)) {
const Town* t = ClosestTownFromTile(tile, (uint)-1);
int grp = 0;
if (t != NULL) {
grp = GetTownRadiusGroup(t, tile);
/* Show an animation to indicate road work */
if (t->road_build_months != 0 &&
(DistanceManhattan(t->xy, tile) < 8 || grp != 0) &&
GetRoadTileType(tile) == ROAD_TILE_NORMAL && (GetAllRoadBits(tile) == ROAD_X || GetAllRoadBits(tile) == ROAD_Y)) {
if (GetTileSlope(tile, NULL) == SLOPE_FLAT && EnsureNoVehicleOnGround(tile) && CHANCE16(1, 20)) {
StartRoadWorks(tile);
SndPlayTileFx(SND_21_JACKHAMMER, tile);
CreateEffectVehicleAbove(
TileX(tile) * TILE_SIZE + 7,
TileY(tile) * TILE_SIZE + 7,
0,
EV_BULLDOZER);
MarkTileDirtyByTile(tile);
return;
}
}
}
{
/* Adjust road ground type depending on 'grp' (grp is the distance to the center) */
const Roadside* new_rs = (_opt.landscape == LT_TOYLAND) ? _town_road_types_2[grp] : _town_road_types[grp];
Roadside cur_rs = GetRoadside(tile);
/* We have our desired type, do nothing */
if (cur_rs == new_rs[0]) return;
/* We have the pre-type of the desired type, switch to the desired type */
if (cur_rs == new_rs[1]) {
cur_rs = new_rs[0];
/* We have barren land, install the pre-type */
} else if (cur_rs == ROADSIDE_BARREN) {
cur_rs = new_rs[1];
/* We're totally off limits, remove any installation and make barren land */
} else {
cur_rs = ROADSIDE_BARREN;
}
SetRoadside(tile, cur_rs);
MarkTileDirtyByTile(tile);
}
} else if (IncreaseRoadWorksCounter(tile)) {
TerminateRoadWorks(tile);
MarkTileDirtyByTile(tile);
}
}
static void ClickTile_Road(TileIndex tile)
{
if (GetRoadTileType(tile) == ROAD_TILE_DEPOT) ShowDepotWindow(tile, VEH_ROAD);
}
static const byte _road_trackbits[16] = {
0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
};
static uint32 GetTileTrackStatus_Road(TileIndex tile, TransportType mode)
{
RoadType rt = ROADTYPE_ROAD;
switch (mode) {
case TRANSPORT_RAIL:
if (!IsLevelCrossing(tile)) return 0;
return GetCrossingRailBits(tile) * 0x101;
case TRANSPORT_ROAD:
if (!HASBIT(GetRoadTypes(tile), rt)) return 0;
switch (GetRoadTileType(tile)) {
case ROAD_TILE_NORMAL:
return HasRoadWorks(tile) ? 0 : _road_trackbits[GetRoadBits(tile, rt)] * 0x101;
case ROAD_TILE_CROSSING: {
uint32 r = AxisToTrackBits(GetCrossingRoadAxis(tile)) * 0x101;
if (IsCrossingBarred(tile)) r *= 0x10001;
return r;
}
default:
case ROAD_TILE_DEPOT:
return AxisToTrackBits(DiagDirToAxis(GetRoadDepotDirection(tile))) * 0x101;
}
break;
default: break;
}
return 0;
}
static const StringID _road_tile_strings[] = {
STR_1814_ROAD,
STR_1814_ROAD,
STR_1814_ROAD,
STR_1815_ROAD_WITH_STREETLIGHTS,
STR_1814_ROAD,
STR_1816_TREE_LINED_ROAD,
STR_1814_ROAD,
STR_1814_ROAD,
};
static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
{
td->owner = GetTileOwner(tile);
switch (GetRoadTileType(tile)) {
case ROAD_TILE_CROSSING: td->str = STR_1818_ROAD_RAIL_LEVEL_CROSSING; break;
case ROAD_TILE_DEPOT: td->str = STR_1817_ROAD_VEHICLE_DEPOT; break;
default: td->str = _road_tile_strings[GetRoadside(tile)]; break;
}
}
static const byte _roadveh_enter_depot_unk0[4] = {
8, 9, 0, 1
};
static uint32 VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
{
switch (GetRoadTileType(tile)) {
case ROAD_TILE_CROSSING:
if (v->type == VEH_TRAIN && !IsCrossingBarred(tile)) {
/* train crossing a road */
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
BarCrossing(tile);
MarkTileDirtyByTile(tile);
}
break;
case ROAD_TILE_DEPOT:
if (v->type == VEH_ROAD &&
v->u.road.frame == 11 &&
_roadveh_enter_depot_unk0[GetRoadDepotDirection(tile)] == v->u.road.state) {
VehicleEnterDepot(v);
return VETSB_ENTERED_WORMHOLE;
}
break;
default: break;
}
return VETSB_CONTINUE;
}
static void ChangeTileOwner_Road(TileIndex tile, PlayerID old_player, PlayerID new_player)
{
if (GetRoadTileType(tile) == ROAD_TILE_DEPOT) {
DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
return;
}
for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
if (!HASBIT(GetRoadTypes(tile), rt)) continue;
if (GetRoadOwner(tile, rt) == old_player) {
SetRoadOwner(tile, rt, new_player == PLAYER_SPECTATOR ? OWNER_NONE : new_player);
if (rt == ROADTYPE_TRAM) {
DoCommand(tile, ROADTYPE_TRAM << 4 | GetRoadBits(tile, ROADTYPE_ROAD), 0, DC_EXEC, CMD_REMOVE_ROAD);
}
}
}
if (IsLevelCrossing(tile)) {
MakeRoadNormal(tile, GetCrossingRoadBits(tile), GetRoadTypes(tile), GetTownIndex(tile), GetRoadOwner(tile, ROADTYPE_ROAD), GetRoadOwner(tile, ROADTYPE_TRAM), GetRoadOwner(tile, ROADTYPE_HWAY));
}
}
extern const TileTypeProcs _tile_type_road_procs = {
DrawTile_Road, /* draw_tile_proc */
GetSlopeZ_Road, /* get_slope_z_proc */
ClearTile_Road, /* clear_tile_proc */
GetAcceptedCargo_Road, /* get_accepted_cargo_proc */
GetTileDesc_Road, /* get_tile_desc_proc */
GetTileTrackStatus_Road, /* get_tile_track_status_proc */
ClickTile_Road, /* click_tile_proc */
AnimateTile_Road, /* animate_tile_proc */
TileLoop_Road, /* tile_loop_clear */
ChangeTileOwner_Road, /* change_tile_owner_clear */
NULL, /* get_produced_cargo_proc */
VehicleEnter_Road, /* vehicle_enter_tile_proc */
GetSlopeTileh_Road, /* get_slope_tileh_proc */
};
|