-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRailBuildManager.nut
More file actions
2409 lines (2171 loc) · 93.8 KB
/
Copy pathRailBuildManager.nut
File metadata and controls
2409 lines (2171 loc) · 93.8 KB
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
require("DoubleRailPathfinder.nut");
require("SingleRailPathfinder.nut");
require("RailReduxPathfinder.nut");
enum RailStationDir
{
NE,
SW,
NW,
SE,
};
class RailStation
{
m_tile = null;
m_dir = null;
m_num_plat = null;
m_length = null;
m_width = null;
m_height = null;
constructor(tile, dir, num_plat, length)
{
this.m_tile = tile;
this.m_dir = dir;
this.m_num_plat = num_plat;
this.m_length = length;
this.m_width = (dir == RailStationDir.NE || dir == RailStationDir.SW) ? length : num_plat;
this.m_height = (dir == RailStationDir.NE || dir == RailStationDir.SW) ? num_plat : length;
}
function GetValidRectangle(extra_range)
{
if (extra_range == 0) return [this.GetTopTile(), this.m_width, this.m_height];
local rail_station_tiles = AITileList();
rail_station_tiles.AddRectangle(this.GetTopTile(), this.GetBottomTile());
local offset_x;
local offset_y;
switch (this.m_dir) {
case RailStationDir.NE:
offset_x = -1;
offset_y = 0;
break;
case RailStationDir.SW:
offset_x = 1;
offset_y = 0;
break;
case RailStationDir.NW:
offset_x = 0;
offset_y = -1;
break;
case RailStationDir.SE:
offset_x = 0;
offset_y = 1;
break;
}
local new_top_tile = this.GetTopTile();
foreach (platform in [1, this.m_num_plat]) {
local entry_tile = this.GetEntryTile(platform);
local entry_tile_x = AIMap.GetTileX(entry_tile);
local entry_tile_y = AIMap.GetTileY(entry_tile);
local tile_x = entry_tile_x + offset_x * extra_range;
local tile_y = entry_tile_y + offset_y * extra_range;
local tile = AIMap.GetTileIndex(tile_x, tile_y);
if (!AIMap.IsValidTile(tile)) return null;
if (tile < new_top_tile) {
new_top_tile = tile;
}
}
local new_width = (this.m_dir == RailStationDir.NE || this.m_dir == RailStationDir.SW) ? this.m_length + extra_range : this.m_num_plat;
local new_height = (this.m_dir == RailStationDir.NE || this.m_dir == RailStationDir.SW) ? this.m_num_plat : this.m_length + extra_range;
return [new_top_tile, new_width, new_height];
}
function GetExitTile(platform, extra_range = 0)
{
switch (this.m_dir) {
case RailStationDir.NE:
return this.m_tile + AIMap.GetMapSizeX() * (platform - 1) - 1 - extra_range;
case RailStationDir.SW:
return this.m_tile + AIMap.GetMapSizeX() * (platform - 1) + this.m_length + extra_range;
case RailStationDir.NW:
return this.m_tile + (platform - 1) - AIMap.GetMapSizeX() - extra_range * AIMap.GetMapSizeX();
case RailStationDir.SE:
return this.m_tile + (platform - 1) + this.m_length * AIMap.GetMapSizeX() + extra_range * AIMap.GetMapSizeX();
}
}
function GetEntryTile(platform)
{
switch (this.m_dir) {
case RailStationDir.NE:
return this.m_tile + AIMap.GetMapSizeX() * (platform - 1);
case RailStationDir.SW:
return this.m_tile + AIMap.GetMapSizeX() * (platform - 1) + this.m_length - 1;
case RailStationDir.NW:
return this.m_tile + (platform - 1);
case RailStationDir.SE:
return this.m_tile + (platform - 1) + (this.m_length - 1) * AIMap.GetMapSizeX();
}
}
function GetTrackDirection()
{
switch (this.m_dir) {
case RailStationDir.NE:
case RailStationDir.SW:
return AIRail.RAILTRACK_NE_SW;
case RailStationDir.NW:
case RailStationDir.SE:
return AIRail.RAILTRACK_NW_SE;
}
}
function GetTopTile()
{
return this.m_tile;
}
function GetBottomTile()
{
switch (this.m_dir) {
case RailStationDir.NE:
case RailStationDir.SW:
return this.m_tile + AIMap.GetMapSizeX() * (this.m_num_plat - 1) + this.m_length - 1;
case RailStationDir.NW:
case RailStationDir.SE:
return this.m_tile + (this.m_num_plat - 1) + (this.m_length - 1) * AIMap.GetMapSizeX();
}
}
function CreateFromTile(tile, dir = null)
{
assert(AIRail.IsRailStationTile(tile));
assert(dir == null || dir == RailStationDir.NE || dir == RailStationDir.SW || dir = RailStationDir.NW || dir = RailStationDir.SE);
local rail_station_tiles = AITileList_StationType(AIStation.GetStationID(tile), AIStation.STATION_TRAIN);
rail_station_tiles.Sort(AIList.SORT_BY_ITEM, AIList.SORT_ASCENDING);
local top_tile = rail_station_tiles.Begin();
rail_station_tiles.Sort(AIList.SORT_BY_ITEM, AIList.SORT_DESCENDING);
local bot_tile = rail_station_tiles.Begin();
local rail_track = AIRail.GetRailStationDirection(top_tile);
local length = rail_track == AIRail.RAILTRACK_NE_SW ? AIMap.GetTileX(bot_tile) - AIMap.GetTileX(top_tile) + 1 : AIMap.GetTileY(bot_tile) - AIMap.GetTileY(top_tile) + 1;
local num_platforms = rail_track == AIRail.RAILTRACK_NE_SW ? AIMap.GetTileY(bot_tile) - AIMap.GetTileY(top_tile) + 1 : AIMap.GetTileX(bot_tile) - AIMap.GetTileX(top_tile) + 1;
if (dir == null) {
/* Try to guess direction based on the rail tracks at the exit */
switch (rail_track) {
case AIRail.RAILTRACK_NE_SW: {
local exit_tile_NE_1 = top_tile - 1;
local exit_tile_SW_1 = top_tile + length;
if (AIRail.IsRailTile(exit_tile_NE_1) && AITile.GetOwner(exit_tile_NE_1) == ::caches.m_my_company_id) {
local tracks = AIRail.GetRailTracks(exit_tile_NE_1);
if ((tracks & AIRail.RAILTRACK_NE_SW) != 0) {
dir = RailStationDir.NE;
break;
}
} else if (AIRail.IsRailTile(exit_tile_SW_1) && AITile.GetOwner(exit_tile_SW_1) == ::caches.m_my_company_id) {
local tracks = AIRail.GetRailTracks(exit_tile_SW_1);
if ((tracks & AIRail.RAILTRACK_NE_SW) != 0) {
dir = RailStationDir.SW;
break;
}
}
/* Could not guess station direction, just use any instead of crashing the script */
dir = RailStationDir.NE;
break;
}
case AIRail.RAILTRACK_NW_SE: {
local exit_tile_NW_1 = top_tile - AIMap.GetMapSizeX();
local exit_tile_SE_1 = top_tile + length * AIMap.GetMapSizeX();
if (AIRail.IsRailTile(exit_tile_NW_1) && AITile.GetOwner(exit_tile_NW_1) == ::caches.m_my_company_id) {
local tracks = AIRail.GetRailTracks(exit_tile_NW_1);
if ((tracks & AIRail.RAILTRACK_NW_SE) != 0) {
dir = RailStationDir.NW;
break;
}
} else if (AIRail.IsRailTile(exit_tile_SE_1) && AITile.GetOwner(exit_tile_SE_1) == ::caches.m_my_company_id) {
local tracks = AIRail.GetRailTracks(exit_tile_SE_1);
if ((tracks & AIRail.RAILTRACK_NW_SE) != 0) {
dir = RailStationDir.SE;
break;
}
}
/* Could not guess station direction, just use any instead of crashing the script */
dir = RailStationDir.NW;
break;
}
}
}
assert(dir != null);
return RailStation(top_tile, dir, num_platforms, length);
}
function GetPlatformLine(num)
{
assert(num == 1 || num == 2);
switch (this.m_dir) {
case RailStationDir.NE:
case RailStationDir.SE:
return num == 1 ? 1 : 2;
case RailStationDir.NW:
case RailStationDir.SW:
return num == 1 ? 2 : 1;
}
}
};
enum RailStructType
{
RAIL,
TUNNEL,
BRIDGE,
STATION,
DEPOT,
};
class RailStruct
{
m_tile = null;
m_struct = null;
m_rail_type = null;
m_tile2 = null;
m_tile3 = null;
constructor(tile, struct, rail_type = -1, tile2 = -1, tile3 = -1)
{
this.m_tile = tile;
this.m_struct = struct;
this.m_rail_type = rail_type;
this.m_tile2 = tile2;
this.m_tile3 = tile3;
}
function SetRail(tile, rail_type, tile2, tile3)
{
return {
m_tile = tile,
m_struct = RailStructType.RAIL,
m_rail_type = rail_type,
m_tile2 = tile2,
m_tile3 = tile3,
};
}
function SetStruct(tile, struct, rail_type, tile2 = -1)
{
assert(struct != RailStructType.RAIL);
assert(struct == RailStructType.DEPOT || tile2 != -1);
return {
m_tile = tile,
m_struct = struct,
m_rail_type = rail_type,
m_tile2 = tile2,
};
}
};
class RailBuildManager
{
/* These are saved */
m_city_from = -1;
m_city_to = -1;
m_station_from = -1;
m_station_to = -1;
m_depot_tile_from = -1;
m_depot_tile_to = -1;
m_bridge_tiles = null;
m_cargo_class = -1;
m_rail_type = -1;
m_best_routes_built = null;
m_station_from_dir = -1;
m_station_to_dir = -1;
m_built_tiles = null;
m_pathfinder_profile = -1;
m_built_ways = -1;
/* These are not saved */
m_pathfinder_instance = null;
m_pathfinder_tries = -1;
m_sent_to_depot_rail_group = null;
m_coverage_radius = -1;
m_cargo_type = -1;
m_route_dist = -1;
m_city_from_name = null;
m_city_to_name = null;
m_max_pathfinder_tries = -1;
function HasUnfinishedRoute()
{
return this.m_city_from != -1 && this.m_city_to != -1 && this.m_cargo_class != -1;
}
function SetRouteFinished()
{
this.m_city_from = -1;
this.m_city_to = -1;
this.m_station_from = -1;
this.m_station_to = -1;
this.m_depot_tile_from = -1;
this.m_depot_tile_to = -1;
this.m_bridge_tiles = null;
this.m_cargo_class = -1;
this.m_rail_type = -1;
this.m_best_routes_built = null;
this.m_station_from_dir = -1;
this.m_station_to_dir = -1;
this.m_built_tiles = null;
this.m_pathfinder_profile = -1;
this.m_built_ways = -1;
this.m_pathfinder_instance = null;
this.m_pathfinder_tries = -1;
this.m_sent_to_depot_rail_group = null;
this.m_coverage_radius = -1;
this.m_cargo_type = -1;
this.m_route_dist = -1;
this.m_city_from_name = null;
this.m_city_to_name = null;
this.m_max_pathfinder_tries = -1;
}
function RemoveFailedRouteStation(station_tile, station_dir, depot_tile = null)
{
if (station_tile != null) {
local rail_station = RailStation.CreateFromTile(station_tile, station_dir);
local top_tile = rail_station.GetTopTile();
local bot_tile = rail_station.GetBottomTile();
local entry_tile_2 = rail_station.GetEntryTile(2);
local exit_tile_2 = rail_station.GetExitTile(2);
local exit_tile_1 = rail_station.GetExitTile(1);
local entry_tile_1 = rail_station.GetEntryTile(1);
local exit_tile_1_1 = 2 * exit_tile_1 - entry_tile_1;
local exit_tile_2_1 = 2 * exit_tile_2 - entry_tile_2;
local counter = 0;
do {
if (!TestRemoveRailStationTileRectangle().TryRemove(top_tile, bot_tile, false)) {
++counter;
} else {
// AILog.Warning("Removed railway station tile at " + station_tile + " from tile " + top_tile + " to tile " + bot_tile);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetStruct(top_tile, RailStructType.STATION, this.m_rail_type, bot_tile));
// AILog.Error("Failed to remove railway station tile at " + station_tile + " from tile " + top_tile + " to tile " + bot_tile " - " + AIError.GetLastErrorString());
}
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(entry_tile_2, exit_tile_2, exit_tile_1_1)) {
++counter;
} else {
// AILog.Warning("Removed rail track crossing from platform 2 to 1 at tile " + exit_tile_2);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(exit_tile_2, this.m_rail_type, entry_tile_2, exit_tile_1_1));
// AILog.Error("Failed to remove rail track crossing from platform 2 to 1 at tile " + exit_tile_2);
}
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(entry_tile_1, exit_tile_1, exit_tile_2_1)) {
++counter;
} else {
// AILog.Warning("Removed rail track crossing from platform 1 to 2 at tile " + rail_station.GetExitTile(1));
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(exit_tile_1, this.m_rail_type, entry_tile_1, exit_tile_2_1));
// AILog.Error("Failed to remove rail track crossing from platform 1 to 2 at tile " + rail_station.GetExitTile(1));
}
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(entry_tile_2, exit_tile_2, exit_tile_2_1)) {
++counter;
} else {
// AILog.Warning("Removed rail track in front of platform 2 at tile " + exit_tile_2);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(exit_tile_2, this.m_rail_type, entry_tile_2, exit_tile_2_1));
// AILog.Error("Failed to remove rail track in front of platform 2 at tile " + exit_tile_2);
}
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(entry_tile_1, exit_tile_1, exit_tile_1_1)) {
++counter;
} else {
// AILog.Warning("Removed rail track in front of platform 1 at tile " + rail_station.GetExitTile(1));
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(exit_tile_1, this.m_rail_type, entry_tile_1, exit_tile_1_1));
// AILog.Error("Failed to remove rail track in front of platform 1 at tile " + rail_station.GetExitTile(1));
}
if (depot_tile != null) {
local depot_front_tile = AIRail.GetRailDepotFrontTile(depot_tile);
local depot_rail_a = abs(depot_tile - depot_front_tile) == 1 ? depot_front_tile - AIMap.GetMapSizeX() : depot_front_tile - 1;
local depot_rail_b = 2 * depot_front_tile - depot_rail_a;
local depot_rail_c = 2 * depot_front_tile - depot_tile;
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(depot_tile, depot_front_tile, depot_rail_a)) {
++counter;
} else {
// AILog.Warning("Removed rail track in front of depot towards the station at tile " + depot_front_tile);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(depot_front_tile, this.m_rail_type, depot_tile, depot_rail_a));
// AILog.Error("Failed to remove rail track in front of depot towards the station at tile " + depot_front_tile);
}
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(depot_tile, depot_front_tile, depot_rail_b)) {
++counter;
} else {
// AILog.Warning("Removed rail track in front of depot towards the railroad at tile " + depot_front_tile);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(depot_front_tile, this.m_rail_type, depot_tile, depot_rail_b));
// AILog.Error("Failed to remove rail track in front of depot towards the railroad at tile " + depot_front_tile);
}
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(depot_tile, depot_front_tile, depot_rail_c)) {
++counter;
} else {
// AILog.Warning("Removed rail track in front of depot accross the lines at tile " + depot_front_tile);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(depot_front_tile, this.m_rail_type, depot_tile, depot_rail_c));
// AILog.Error("Failed to remove rail track in front of depot towards accross the lines at tile " + depot_front_tile);
}
local counter = 0;
do {
if (!TestDemolishTile().TryDemolish(depot_tile)) {
++counter;
} else {
// AILog.Warning("Removed rail depot at tile " + depot_tile);
break;
}
AIController.Sleep(1);
} while (counter < 500);
if (counter == 500) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetStruct(depot_tile, RailStructType.DEPOT, this.m_rail_type));
// AILog.Error("Failed to remove rail depot at tile " + depot_tile);
}
}
}
}
function RemoveFailedRouteTracks(line = null)
{
assert(line == null || line == 0 || line == 1);
local lines = line == null ? [0, 1] : [line];
foreach (j in lines) {
while (this.m_built_tiles[j].len() != 0) {
local i = this.m_built_tiles[j].pop();
local tile = i.m_tile;
local struct = i.m_struct;
local rail_type = i.m_rail_type;
AIRail.SetCurrentRailType(rail_type);
if (struct == RailStructType.RAIL) {
if (AIRail.IsRailTile(tile)) {
local tile_from = i.m_tile2;
local tile_to = i.m_tile3;
if (!TestRemoveRail().TryRemove(tile_from, tile, tile_to)) {
// AILog.Info("Failed to remove rail at tile " + tile + ", connecting " + tile_from + " to " + tile_to + ".");
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetRail(tile, rail_type, tile_from, tile_to));
}
// } else {
// AILog.Info("No rail tile found to remove at tile " + tile + ", connecting " + tile_from + " to " + tile_to + ".");
}
} else if (struct == RailStructType.BRIDGE) {
local tile2 = i.m_tile2;
if (AIBridge.IsBridgeTile(tile) && AITile.HasTransportType(tile, AITile.TRANSPORT_RAIL) && AIBridge.GetOtherBridgeEnd(tile) == tile2) {
if (!TestRemoveBridge().TryRemove(tile)) {
// AILog.Info("Failed to demolish bridge at tiles " + tile + " and " + tile2 + ".");
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetStruct(tile, RailStructType.BRIDGE, rail_type, tile2));
}
// } else {
// AILog.Info("No bridge found to demolish at tiles " + tile + " and " + tile2 + ".");
}
} else if (struct == RailStructType.TUNNEL) {
local tile2 = i.m_tile2;
if (AITunnel.IsTunnelTile(tile) && AITile.HasTransportType(tile, AITile.TRANSPORT_RAIL) && AITunnel.GetOtherTunnelEnd(tile) == tile2) {
if (!TestRemoveTunnel().TryRemove(tile)) {
// AILog.Info("Failed to demolish tunnel at tiles " + tile + " and " + tile2 + ".");
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetStruct(tile, RailStructType.TUNNEL, rail_type, tile2));
}
// } else {
// AILog.Info("No tunnel found to demolish at tiles " + tile + " and " + tile2 + ".");
}
}
}
}
}
function BuildRailRoute(city_from, city_to, cargo_class, sent_to_depot_rail_group, best_routes_built, rail_type)
{
this.m_city_from = city_from;
this.m_city_to = city_to;
this.m_cargo_class = cargo_class;
this.m_sent_to_depot_rail_group = sent_to_depot_rail_group;
this.m_best_routes_built = best_routes_built;
this.m_rail_type = rail_type;
if (this.m_built_ways == -1) {
this.m_built_ways = 0;
}
if (this.m_pathfinder_profile == -1) {
this.m_pathfinder_profile = AIController.GetSetting("rail_pf_profile");
}
if (this.m_bridge_tiles == null) {
this.m_bridge_tiles = [];
}
if (this.m_pathfinder_tries == -1) {
this.m_pathfinder_tries = 0;
}
if (this.m_built_tiles == null) {
this.m_built_tiles = [[], []];
}
if (this.m_coverage_radius == -1) {
this.m_coverage_radius = AIStation.GetCoverageRadius(AIStation.STATION_TRAIN);
}
if (this.m_cargo_type == -1) {
this.m_cargo_type = Utils.GetCargoType(cargo_class);
}
if (this.m_route_dist == -1) {
this.m_route_dist = AIMap.DistanceManhattan(AITown.GetLocation(this.m_city_from), AITown.GetLocation(this.m_city_to));
}
if (this.m_city_from_name == null) {
this.m_city_from_name = AITown.GetName(this.m_city_from);
}
if (this.m_city_to_name == null) {
this.m_city_to_name = AITown.GetName(this.m_city_to);
}
local num_vehicles = AIGroup.GetNumVehicles(AIGroup.GROUP_ALL, AIVehicle.VT_RAIL);
if (num_vehicles >= AIGameSettings.GetValue("max_trains") || AIGameSettings.IsDisabledVehicleType(AIVehicle.VT_RAIL)) {
/* Don't terminate the route, or it may leave already built stations behind. */
return 0;
}
if (this.m_station_from == -1 || !AIRail.IsRailStationTile(this.m_station_from) || AITile.GetOwner(this.m_station_from) != ::caches.m_my_company_id) {
local station_from = this.BuildTownRailStation(this.m_city_from, this.m_city_to, this.m_best_routes_built, this.m_rail_type);
if (station_from == null) {
this.SetRouteFinished();
return null;
}
this.m_station_from = station_from[0];
this.m_station_from_dir = station_from[1];
}
if (this.m_depot_tile_from == -1 || !AIRail.IsRailDepotTile(this.m_depot_tile_from) || AITile.GetOwner(this.m_depot_tile_from) != ::caches.m_my_company_id) {
local depot_tile_from = this.BuildRouteRailDepot(this.m_station_from, this.m_station_from_dir);
if (depot_tile_from == null) {
this.RemoveFailedRouteStation(this.m_station_from, this.m_station_from_dir);
this.SetRouteFinished();
return null;
}
this.m_depot_tile_from = depot_tile_from;
}
if (this.m_station_to == -1 || !AIRail.IsRailStationTile(this.m_station_to) || AITile.GetOwner(this.m_station_to) != ::caches.m_my_company_id) {
local station_to = this.BuildTownRailStation(this.m_city_to, this.m_city_from, this.m_best_routes_built, this.m_rail_type);
if (station_to == null) {
this.RemoveFailedRouteStation(this.m_station_from, this.m_station_from_dir, this.m_depot_tile_from);
this.SetRouteFinished();
return null;
}
this.m_station_to = station_to[0];
this.m_station_to_dir = station_to[1];
}
if (this.m_depot_tile_to == -1 || !AIRail.IsRailDepotTile(this.m_depot_tile_to) || AITile.GetOwner(this.m_depot_tile_to) != ::caches.m_my_company_id) {
local depot_tile_to = this.BuildRouteRailDepot(this.m_station_to, this.m_station_to_dir);
if (depot_tile_to == null) {
this.RemoveFailedRouteStation(this.m_station_to, this.m_station_to_dir);
this.RemoveFailedRouteStation(this.m_station_from, this.m_station_from_dir, this.m_depot_tile_from);
this.SetRouteFinished();
return null;
}
this.m_depot_tile_to = depot_tile_to;
}
if (this.m_station_from != null && this.m_depot_tile_from != null && this.m_station_to != null && this.m_depot_tile_to != null) {
local rail_array;
if (this.m_pathfinder_profile == 2) rail_array = this.PathfindBuildRailRedux(this.m_pathfinder_instance);
if (this.m_pathfinder_profile == 1) rail_array = this.PathfindBuildDoubleRail(this.m_pathfinder_instance);
if (this.m_pathfinder_profile == 0) rail_array = this.PathfindBuildSingleRail(this.m_pathfinder_instance);
this.m_pathfinder_instance = rail_array[1];
if (rail_array[0] == null) {
if (this.m_pathfinder_instance != null) {
return 1;
}
if (this.m_built_tiles[0].len() != 0 || this.m_built_tiles[1].len() != 0) {
this.RemoveFailedRouteTracks();
}
} else if ((this.m_pathfinder_profile == 0 || this.m_pathfinder_profile == 2) && this.m_built_ways == 1) {
this.m_pathfinder_tries = 0;
return 1;
}
}
if (this.m_built_tiles[0].len() == 0 && this.m_built_tiles[1].len() == 0) {
this.RemoveFailedRouteStation(this.m_station_from, this.m_station_from_dir, this.m_depot_tile_from);
this.RemoveFailedRouteStation(this.m_station_to, this.m_station_to_dir, this.m_depot_tile_to);
this.SetRouteFinished();
return null;
}
if (this.m_built_ways == 2) {
local signals_built = this.BuildSignals();
if (signals_built == -1) {
this.RemoveFailedRouteStation(this.m_station_from, this.m_station_from_dir, this.m_depot_tile_from);
this.RemoveFailedRouteStation(this.m_station_to, this.m_station_to_dir, this.m_depot_tile_to);
this.RemoveFailedRouteTracks();
this.SetRouteFinished();
return null;
}
}
return RailRoute(this.m_city_from, this.m_city_to, this.m_station_from, this.m_station_to, this.m_depot_tile_from, this.m_depot_tile_to, this.m_bridge_tiles, this.m_cargo_class, this.m_sent_to_depot_rail_group, this.m_rail_type, this.m_station_from_dir, this.m_station_to_dir);
}
function AreOtherRailwayStationsNearby(rail_station)
{
local is_friendly = AIController.GetSetting("is_friendly");
local square_size = is_friendly ? this.m_coverage_radius * 2 : 2;
local tile_list = AITileList();
tile_list.AddRectangle(Utils.GetValidOffsetTile(rail_station.GetTopTile(), -1 * square_size, -1 * square_size), Utils.GetValidOffsetTile(rail_station.GetBottomTile(), square_size, square_size));
foreach (tile, _ in tile_list) {
if (AITile.IsStationTile(tile)) {
if (AITile.GetOwner(tile) == ::caches.m_my_company_id) {
/* if another railway station of mine is nearby return true */
if (AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_TRAIN)) {
return true;
}
} else if (is_friendly) {
return true;
}
/* don't care about enemy stations when is_friendly is off */
}
}
return false;
}
function GetAdjacentNonRailwayStationID(rail_station_rectangle, spread_rectangle)
{
local tile_list = AITileList();
tile_list.AddRectangle(spread_rectangle.tile_top, spread_rectangle.tile_bot);
foreach (tile, _ in tile_list) {
if (!AITile.IsStationTile(tile)) {
tile_list[tile] = null;
continue;
}
if (AITile.GetOwner(tile) != ::caches.m_my_company_id) {
tile_list[tile] = null;
continue;
}
local station_id = AIStation.GetStationID(tile);
if (AIStation.HasStationType(station_id, AIStation.STATION_TRAIN)) {
tile_list[tile] = null;
continue;
}
tile_list[tile] = station_id;
}
local station_list = AIList();
station_list.Sort(AIList.SORT_BY_VALUE, AIList.SORT_ASCENDING);
foreach (tile, station_id in tile_list) {
station_list[station_id] = rail_station_rectangle.DistanceManhattan(tile);
}
foreach (station_id, _ in station_list) {
local station_tiles = AITileList_StationType(station_id, AIStation.STATION_ANY);
foreach (tile, _ in station_tiles) {
if (!spread_rectangle.Contains(tile)) {
station_list[station_id] = null;
break;
}
}
}
return station_list.IsEmpty() ? AIStation.STATION_NEW : station_list.Begin();
}
function BuildTownRailStation(city_from, city_to, best_routes_built, rail_type)
{
AIRail.SetCurrentRailType(rail_type);
local randomize_towns = AIController.GetSetting("randomize_towns");
local max_station_spread = AIGameSettings.GetValue("station_spread");
local max_train_length = AIGameSettings.GetValue("max_train_length");
local platform_length = min(RailRoute.MAX_PLATFORM_LENGTH, min(max_station_spread, max_train_length));
local num_platforms = RailRoute.MAX_NUM_PLATFORMS;
local town_rectangle = Utils.EstimateTownRectangle(city_from);
town_rectangle = OrthogonalTileArea.CreateArea(town_rectangle[0], town_rectangle[1]);
local rail_stations = AIPriorityQueue();
foreach (dir in [RailStationDir.NE, RailStationDir.SW, RailStationDir.NW, RailStationDir.SE]) {
local width = (dir == RailStationDir.NE || dir == RailStationDir.SW) ? platform_length : num_platforms;
local height = (dir == RailStationDir.NE || dir == RailStationDir.SW) ? num_platforms : platform_length;
/* build square around @city_from and find suitable tiles for railway station */
local town_rectangle_expanded = clone town_rectangle;
town_rectangle_expanded.Expand(width - 1, height - 1, false);
town_rectangle_expanded.Expand(this.m_coverage_radius, this.m_coverage_radius);
local tile_list = AITileList();
tile_list.AddRectangle(town_rectangle_expanded.tile_top, town_rectangle_expanded.tile_bot);
foreach (tile, _ in tile_list) {
local rail_station = RailStation(tile, dir, num_platforms, platform_length);
local rail_station_tiles = AITileList();
rail_station_tiles.AddRectangle(rail_station.GetTopTile(), rail_station.GetBottomTile());
local rectangle = rail_station.GetValidRectangle(4); // 4 tiles free
if (rectangle == null) {
continue;
}
if (!AITile.IsBuildableRectangle(rectangle[0], rectangle[1], rectangle[2])) {
continue;
}
/* Check if all tiles are of the same height */
rectangle = rail_station.GetValidRectangle(1);
rectangle = OrthogonalTileArea(rectangle[0], rectangle[1], rectangle[2]);
local rectangle_area = AITileList();
rectangle_area.AddRectangle(rectangle.tile_top, rectangle.tile_bot);
foreach (tile2, _ in rectangle_area) {
rectangle_area[tile2] = AITile.GetMaxHeight(tile2);
}
rectangle_area.RemoveValue(rectangle_area.GetValue(rectangle_area.Begin()));
if (!rectangle_area.IsEmpty()) {
continue;
}
local exit_tile_1 = rail_station.GetExitTile(1);
local exit_tile_2 = rail_station.GetExitTile(2);
local depot_tile_1 = 2 * exit_tile_1 - exit_tile_2;
local depot_tile_2 = 2 * exit_tile_2 - exit_tile_1;
if (!AITile.IsBuildable(depot_tile_1) && !AITile.IsBuildable(depot_tile_2)) {
continue;
}
local offset = exit_tile_1 - rail_station.GetEntryTile(1);
local offset_2 = offset * 2;
local offset_3 = offset * 3;
local exit_turn_a_1 = depot_tile_1 + offset_2;
local exit_turn_a_2 = depot_tile_1 + offset_3;
local exit_turn_b_1 = depot_tile_2 + offset_3;
local exit_turn_b_2 = depot_tile_2 + offset_2;
local exit_front_a_2 = exit_tile_1 + offset_3;
local exit_front_b_1 = exit_tile_2 + offset_3;
if ((!AITile.IsBuildable(exit_turn_a_1) || !AITile.IsBuildable(exit_turn_a_2)) &&
(!AITile.IsBuildable(exit_turn_b_1) || !AITile.IsBuildable(exit_turn_b_2)) &&
(!AITile.IsBuildable(exit_turn_a_2) || !AITile.IsBuildable(exit_front_a_2)) &&
(!AITile.IsBuildable(exit_front_b_1) || !AITile.IsBuildable(exit_turn_b_1))) {
continue;
}
if (AITile.GetCargoAcceptance(tile, this.m_cargo_type, rail_station.m_width, rail_station.m_height, this.m_coverage_radius) < 10) {
continue;
}
if (this.AreOtherRailwayStationsNearby(rail_station)) {
continue;
}
local cargo_production = AITile.GetCargoProduction(tile, this.m_cargo_type, rail_station.m_width, rail_station.m_height, this.m_coverage_radius);
if (!randomize_towns && !best_routes_built && cargo_production < 12) {
continue;
}
local shortest_city_to_dist = AIMap.GetMapSizeX() + AIMap.GetMapSizeY();
for (local platform = 1; platform <= rail_station.m_num_plat; platform++) {
local city_to_dist = AITown.GetDistanceManhattanToTile(city_to, rail_station.GetEntryTile(platform));
if (city_to_dist < shortest_city_to_dist) {
shortest_city_to_dist = city_to_dist;
}
}
local shortest_city_from_dist = AIMap.GetMapSizeX() + AIMap.GetMapSizeY();
foreach (tile2, _ in rail_station_tiles) {
local city_from_dist = AITown.GetDistanceManhattanToTile(city_from, tile2);
if (city_from_dist < shortest_city_from_dist) {
shortest_city_from_dist = city_from_dist;
}
}
/* store as negative to make priority queue prioritize highest values */
rail_stations.Insert(rail_station, -((cargo_production << 26) | ((0x1FFF - shortest_city_from_dist) << 13) | (0x1FFF - shortest_city_to_dist)));
}
}
while (!rail_stations.IsEmpty()) {
local rail_station = rail_stations.Pop();
local top_tile = rail_station.GetTopTile();
if (AITile.GetClosestTown(top_tile) != city_from) {
continue;
}
local bot_tile = rail_station.GetBottomTile();
/* get adjacent tiles */
local rail_station_tiles = AITileList();
rail_station_tiles.AddRectangle(top_tile, bot_tile);
local adjacent_tile_list = AITileList();
adjacent_tile_list.AddList(rail_station_tiles);
foreach (tile in adjacent_tile_list) {
local adjacent_tile_list2 = Utils.GetAdjacentTiles(tile);
foreach (tile2 in adjacent_tile_list2) {
adjacent_tile_list[tile2] = 0;
}
}
adjacent_tile_list.RemoveRectangle(top_tile, bot_tile);
/* avoid blocking other station exits */
local blocking = false;
foreach (adjacent_tile, _ in adjacent_tile_list) {
if (!AITile.IsStationTile(adjacent_tile)) {
continue;
}
if (!AITile.HasTransportType(adjacent_tile, AITile.TRANSPORT_ROAD)) {
continue;
}
foreach (road_type, _ in AIRoadTypeList(AIRoad.ROADTRAMTYPES_ROAD | AIRoad.ROADTRAMTYPES_TRAM)) {
if (!AIRoad.HasRoadType(adjacent_tile, road_type)) {
continue;
}
SetCurrentRoadType(road_type);
if (!AIRoad.IsRoadStationTile(adjacent_tile)) {
continue;
}
if (!rail_station_tiles.HasTile(AIRoad.GetRoadStationFrontTile(adjacent_tile))) {
continue;
}
blocking = true;
break;
}
if (blocking) {
break;
}
}
if (blocking) {
continue;
}
local adjacent_station_id = AIStation.STATION_NEW;
if (max_station_spread && AIGameSettings.GetValue("distant_join_stations")) {
local remaining_x = max_station_spread - rail_station.m_width;
local remaining_y = max_station_spread - rail_station.m_height;
local rail_station_rectangle = OrthogonalTileArea(top_tile, rail_station.m_width, rail_station.m_height);
local spread_rectangle = clone rail_station_rectangle;
spread_rectangle.Expand(remaining_x, remaining_y);
adjacent_station_id = this.GetAdjacentNonRailwayStationID(rail_station_rectangle, spread_rectangle);
}
local entry_tile_2 = rail_station.GetEntryTile(2);
local exit_tile_2 = rail_station.GetExitTile(2);
local exit_tile_1 = rail_station.GetExitTile(1);
local entry_tile_1 = rail_station.GetEntryTile(1);
local exit_tile_1_1 = 2 * exit_tile_1 - entry_tile_1;
local exit_tile_2_1 = 2 * exit_tile_2 - entry_tile_2;
local counter = 0;
do {
if (!TestBuildRailStation().TryBuild(top_tile, rail_station.GetTrackDirection(), rail_station.m_num_plat, rail_station.m_length, adjacent_station_id)) {
++counter;
} else {
// AILog.Info("station built");
break;
}
AIController.Sleep(1);
} while (counter < 1);
if (counter == 1) {
continue;
} else {
/* Built rail station. Now build track infront of platform 1 */
local counter = 0;
do {
if (!AIRoad.IsRoadTile(exit_tile_1) && !TestBuildRail().TryBuild(entry_tile_1, exit_tile_1, exit_tile_1_1)) {
++counter;
} else {
// AILog.Info("built track infront of platform 1");
break;
}
AIController.Sleep(1);
} while (counter < 1);
if (counter == 1) {
local counter = 0;
do {
if (!TestRemoveRailStationTileRectangle().TryRemove(top_tile, bot_tile, false)) {
++counter;
} else {
break;
}
AIController.Sleep(1);
} while (counter < 1);
if (counter == 1) {
::scheduled_removals[AITile.TRANSPORT_RAIL].append(RailStruct.SetStruct(top_tile, RailStructType.STATION, this.m_rail_type, bot_tile));
continue;
}
} else {
/* Built track in front of platform 1. Now build track in front of platform 2 */
local counter = 0;
do {
if (!AIRoad.IsRoadTile(exit_tile_2) && !TestBuildRail().TryBuild(entry_tile_2, exit_tile_2, exit_tile_2_1)) {
++counter;
} else {
// AILog.Info("built track infront of platform 2");
break;
}
AIController.Sleep(1);
} while (counter < 1);
if (counter == 1) {
local counter = 0;
do {
if (!TestRemoveRail().TryRemove(entry_tile_1, exit_tile_1, exit_tile_1_1)) {