/
Utils.nut
1895 lines (1597 loc) · 48.2 KB
/
Utils.nut
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
class Utils {
};
function Utils::ArrayHasItem(array, item) {
assert(typeof(item) == "array");
foreach (_, it in array) {
local i = 0;
do {
if (item[i] == it[0][i]) {
i++;
} else {
break;
}
} while (i < item.len());
if (i == item.len()) {
return true;
}
}
return false;
}
function Utils::ArrayGetValue(array, item) {
assert(Utils.ArrayHasItem(array, item));
foreach (_, it in array) {
local i = 0;
do {
if (item[i] == it[0][i]) {
i++;
} else {
break;
}
} while (i < item.len());
if (i == item.len()) {
return it[1];
}
}
assert(false);
}
function Utils::ArraySetValue(array, item, value) {
assert(Utils.ArrayHasItem(array, item));
foreach (_, it in array) {
local i = 0;
do {
if (item[i] == it[0][i]) {
i++;
} else {
break;
}
} while (i < item.len());
if (i == item.len()) {
it[1] = value;
return;
}
}
assert(false);
}
function Utils::CountBits(value) {
assert(typeof(value) == "integer");
local num;
for (num = 0; value != 0; num++) {
value = value & (value - 1);
}
return num;
}
function Utils::Clamp(a, min, max) {
assert(min <= max);
if (a <= min) return min;
if (a >= max) return max;
return a;
}
function Utils::TableListToAIList(table) {
local list = AIList();
foreach(x, y in table) {
list.AddItem(x, y);
}
return list;
}
function Utils::RemoveAIListFromTableList(ailist, table) {
for (local x = ailist.Begin(); !ailist.IsEnd(); x = ailist.Next()) {
table.rawdelete(x);
}
}
function Utils::ConvertKmhishSpeedToDisplaySpeed(speed) {
local velocity = AIGameSettings.GetValue("units_velocity");
local unit_str = " km-ish/h";
local converted_speed = speed;
if (velocity == 0) { // Imperial
unit_str = " mph";
converted_speed = ((speed * 10 * 1) >> 0) / 16;
} else if (velocity == 1) { // Metric
unit_str = " km/h";
converted_speed = ((speed * 10 * 103) >> 6) / 16;
} else if (velocity == 2) { // SI
unit_str = " m/s";
converted_speed = ((speed * 10 * 1831) >> 12) / 16;
}
return converted_speed + unit_str;
}
/// getNthItem
///
/// @param list - The list to get the item from.
/// @param n - The order of the item.
/// @return - The n-th item of the list, null if not found.
function Utils::getNthItem(list, n) {
if (list.Count() == 0) {
AILog.Warning("getNthItem: list is empty!");
return null;
}
if (n + 1 > list.Count()) {
AILog.Warning("getNthItem: list is too short!");
return null;
}
for (local item = list.Begin(); !list.IsEnd(); item = list.Next()) {
if (n == 0) {
//AILog.Info("getNthItem: Found: " + item + " " + list.GetValue(item));
return item;
}
n--;
}
return null;
}
function Utils::MyCID() {
return AICompany.ResolveCompanyID(AICompany.COMPANY_SELF);
}
function Utils::getValidOffsetTile(tile, offsetX, offsetY) {
local oldX = AIMap.GetTileX(tile);
local oldY = AIMap.GetTileY(tile);
local newX = oldX;
local newY = oldY;
if (offsetX > 0) {
for (local x = offsetX; x > 0; x--) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX + 1, newY))) {
newX = newX + 1;
}
}
} else {
for (local x = offsetX; x < 0; x++) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX - 1, newY))) {
newX = newX - 1;
}
}
}
if (offsetY > 0) {
for (local y = offsetY; y > 0; y--) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX, newY + 1))) {
newY = newY + 1;
}
}
} else {
for (local y = offsetY; y < 0; y++) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(newX, newY - 1))) {
newY = newY - 1;
}
}
}
return AIMap.GetTileIndex(newX, newY);
}
/// getOffsetTile
///
/// @param tile - The starting tile.
/// @param offsetX - The x-axis offset.
/// @param offsetY - The y-axis offset.
/// @return - The offset tile.
function Utils::getOffsetTile(tile, offsetX, offsetY) {
local oldX = AIMap.GetTileX(tile);
local oldY = AIMap.GetTileY(tile);
local newX = oldX + offsetX;
local newY = oldY + offsetY;
local freeform = AIMap.IsValidTile(0) ? 0 : 1;
if (((newX < freeform) || (newY < freeform)) // 0 if freeform_edges off
&& (((newX > AIMap.GetMapSizeX() - 2) || (newY > AIMap.GetMapSizeY() - 2)))) {
return AIMap.TILE_INVALID;
}
return AIMap.GetTileIndex(newX, newY);
}
/// getAdjacentTiles
///
/// @param tile - The starting tile.
/// @return - The AITileList of adjacent tiles.
function Utils::getAdjacentTiles(tile) {
local adjTiles = AITileList();
local offsetTile = Utils.getOffsetTile(tile, 0, -1)
if (offsetTile != AIMap.TILE_INVALID) {
adjTiles.AddTile(offsetTile);
}
offsetTile = Utils.getOffsetTile(tile, 1, 0)
if (offsetTile != AIMap.TILE_INVALID) {
adjTiles.AddTile(offsetTile);
}
offsetTile = Utils.getOffsetTile(tile, 0, 1)
if (offsetTile != AIMap.TILE_INVALID) {
adjTiles.AddTile(offsetTile);
}
offsetTile = Utils.getOffsetTile(tile, -1, 0)
if (offsetTile != AIMap.TILE_INVALID) {
adjTiles.AddTile(offsetTile);
}
return adjTiles;
};
function Utils::IsStationBuildableTile(tile) {
if ((AITile.GetSlope(tile) == AITile.SLOPE_FLAT/* || !AITile.IsCoastTile(tile) || !AITile.HasTransportType(tile, AITile.TRANSPORT_WATER) || AICompany.GetLoanAmount() == 0*/) &&
(AITile.IsBuildable(tile) || AIRoad.IsRoadTile(tile) && !AIRoad.IsDriveThroughRoadStationTile(tile) && !AIRail.IsLevelCrossingTile(tile))) {
return true;
}
return false;
}
function Utils::IsDockBuildableTile(tile, cheaper_route) {
local slope = AITile.GetSlope(tile);
if (slope == AITile.SLOPE_FLAT) return [false, null];
local offset = 0;
local tile2 = AIMap.TILE_INVALID;
if (AITile.IsBuildable(tile) && (
slope == AITile.SLOPE_NE && (offset = AIMap.GetTileIndex(1, 0)) ||
slope == AITile.SLOPE_SE && (offset = AIMap.GetTileIndex(0, -1)) ||
slope == AITile.SLOPE_SW && (offset = AIMap.GetTileIndex(-1, 0)) ||
slope == AITile.SLOPE_NW && (offset = AIMap.GetTileIndex(0, 1)))) {
tile2 = tile + offset;
if (!AIMap.IsValidTile(tile2)) return [false, null];
if (AITile.GetSlope(tile2) != AITile.SLOPE_FLAT) return [false, null];
if (!(!cheaper_route && AITile.IsBuildable(tile2) || AITile.IsWaterTile(tile2) && !AIMarine.IsWaterDepotTile(tile2) && !AIMarine.IsLockTile(tile2))) return [false, null];
local tile3 = tile2 + offset;
if (!AIMap.IsValidTile(tile3)) return [false, null];
if (AITile.GetSlope(tile3) != AITile.SLOPE_FLAT) return [false, null];
if (!(!cheaper_route && AITile.IsBuildable(tile3) || AITile.IsWaterTile(tile3) && !AIMarine.IsWaterDepotTile(tile3) && !AIMarine.IsLockTile(tile3))) return [false, null];
}
return [true, tile2];
}
function Utils::AreOtherStationsNearby(tile, cargoClass, stationId) {
local stationType = cargoClass == AICargo.CC_PASSENGERS ? AIStation.STATION_BUS_STOP : AIStation.STATION_TRUCK_STOP;
//check if there are other stations squareSize squares nearby
local squareSize = AIStation.GetCoverageRadius(stationType);
if (stationId == AIStation.STATION_NEW) {
squareSize = squareSize * 2;
}
local square = AITileList();
if (!AIController.GetSetting("is_friendly")) {
//don't care about enemy stations when is_friendly is off
square.AddRectangle(Utils.getValidOffsetTile(tile, (-1) * squareSize, (-1) * squareSize),
Utils.getValidOffsetTile(tile, squareSize, squareSize));
//if another road station of mine is nearby return true
for (local tile = square.Begin(); !square.IsEnd(); tile = square.Next()) {
if (Utils.isTileMyRoadStation(tile, cargoClass)) { //negate second expression to merge your stations
return true;
}
}
} else {
square.AddRectangle(Utils.getValidOffsetTile(tile, (-1) * squareSize, (-1) * squareSize),
Utils.getValidOffsetTile(tile, squareSize, squareSize));
//if any other station is nearby, except my own airports, return true
for (local tile = square.Begin(); !square.IsEnd(); tile = square.Next()) {
if (AITile.IsStationTile(tile)) {
if (AITile.GetOwner(tile) != Utils.MyCID()) {
return true;
} else {
local stationTiles = AITileList_StationType(AIStation.GetStationID(tile), stationType);
if (stationTiles.HasItem(tile)) {
return true;
}
}
}
}
}
return false;
}
function Utils::AreOtherDocksNearby(tile_north, tile_south) {
//check if there are other docks squareSize squares nearby
local squareSize = AIStation.GetCoverageRadius(AIStation.STATION_DOCK) * 2;
local square = AITileList();
if (!AIController.GetSetting("is_friendly")) {
squareSize = 2;
//don't care about enemy stations when is_friendly is off
square.AddRectangle(Utils.getValidOffsetTile(tile_north, (-1) * squareSize, (-1) * squareSize),
Utils.getValidOffsetTile(tile_south, squareSize, squareSize));
//if another dock of mine is nearby return true
for (local tile = square.Begin(); !square.IsEnd(); tile = square.Next()) {
if (Utils.isTileMyDock(tile)) { //negate second expression to merge your stations
return true;
}
}
} else {
square.AddRectangle(Utils.getValidOffsetTile(tile_north, (-1) * squareSize, (-1) * squareSize),
Utils.getValidOffsetTile(tile_south, squareSize, squareSize));
//if any other station is nearby, except my own airports, return true
for (local tile = square.Begin(); !square.IsEnd(); tile = square.Next()) {
if (AITile.IsStationTile(tile)) {
if (AITile.GetOwner(tile) != Utils.MyCID()) {
return true;
} else {
local stationTiles = AITileList_StationType(AIStation.GetStationID(tile), AIStation.STATION_DOCK);
if (stationTiles.HasItem(tile)) {
return true;
}
}
}
}
}
return false;
}
function Utils::isTileMyRoadStation(tile, cargoClass) {
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() &&
AIStation.HasStationType(AIStation.GetStationID(tile), cargoClass == AICargo.CC_PASSENGERS ? AIStation.STATION_BUS_STOP : AIStation.STATION_TRUCK_STOP)) {
return true;
}
return false;
}
function Utils::isTileMyDock(tile) {
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() && AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_DOCK)) {
return true;
}
return false;
}
function Utils::isTileMyStationWithoutAirport(tile) { // Checks RoadStation
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() &&
!AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_AIRPORT)) {
return true;
}
return false;
}
function Utils::isTileMyStationWithoutRoadStation(tile, cargoClass) { // Checks Airport
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() &&
!AIStation.HasStationType(AIStation.GetStationID(tile), cargoClass == AICargo.CC_PASSENGERS ? AIStation.STATION_BUS_STOP : AIStation.STATION_TRUCK_STOP)) {
return true;
}
return false;
}
function Utils::isTileMyStationWithoutRoadStationOfAnyType(tile) {
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() &&
!AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_BUS_STOP) &&
!AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_TRUCK_STOP)) {
return true;
}
return false;
}
function Utils::isTileMyStationWithoutDock(tile) {
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() &&
!AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_DOCK)) {
return true;
}
return false;
}
function Utils::isTileMyStationWithoutRailwayStation(tile) {
if (AITile.IsStationTile(tile) && AITile.GetOwner(tile) == Utils.MyCID() &&
!AIStation.HasStationType(AIStation.GetStationID(tile), AIStation.STATION_TRAIN)) {
return true;
}
return false;
}
/// getCargoId - Returns either mail cargo id, or passenger cargo id.
///
/// @param cargoClass - either AICargo.CC_MAIL, or AICargo.CC_PASSENGERS
/// @return - Cargo list.
function Utils::getCargoId(cargoClass) {
local cargoList = AICargoList();
cargoList.Sort(AIList.SORT_BY_ITEM, AIList.SORT_ASCENDING);
local cargoId = null;
for (cargoId = cargoList.Begin(); !cargoList.IsEnd(); cargoId = cargoList.Next()) {
if (AICargo.HasCargoClass(cargoId, cargoClass)) {
break;
}
}
/* both AICargo.CC_MAIL and AICargo.CC_PASSENGERS should return the first available cargo */
return cargoId;
}
function Utils::IsTownGrowing(town, cargo) {
// return true;
if (!AIGameSettings.GetValue("town_growth_rate")) return true; // no town grows, just work with it
local cargoList = AICargoList();
cargoList.Sort(AIList.SORT_BY_ITEM, AIList.SORT_ASCENDING);
local cargoRequired = AIList();
for (local cargo_type = cargoList.Begin(); !cargoList.IsEnd(); cargo_type = cargoList.Next()) {
local town_effect = AICargo.GetTownEffect(cargo_type);
// local class_name = "";
// for (local cc = 0; cc <= 15; cc++) {
// local cargo_class = 1 << cc;
// if (AICargo.HasCargoClass(cargo_type, cargo_class)) {
// if (class_name != "") class_name += ", ";
// switch(cargo_class) {
// case AICargo.CC_PASSENGERS: class_name += "CC_PASSENGERS"; break;
// case AICargo.CC_MAIL: class_name += "CC_MAIL"; break;
// case AICargo.CC_EXPRESS: class_name += "CC_EXPRESS"; break;
// case AICargo.CC_ARMOURED: class_name += "CC_ARMOURED"; break;
// case AICargo.CC_BULK: class_name += "CC_BULK"; break;
// case AICargo.CC_PIECE_GOODS: class_name += "CC_PIECE_GOODS"; break;
// case AICargo.CC_LIQUID: class_name += "CC_LIQUID"; break;
// case AICargo.CC_REFRIGERATED: class_name += "CC_REFRIGERATED"; break;
// case AICargo.CC_HAZARDOUS: class_name += "CC_HAZARDOUS"; break;
// case AICargo.CC_COVERED: class_name += "CC_COVERED"; break;
// case 1 << 15: class_name += "CC_SPECIAL"; break;
// default: class_name += "CC_NOAVAILABLE (" + cargo_class + ")"; break;
// }
// }
// }
// AILog.Info("Cargo " + cargo_type + ": " + AICargo.GetCargoLabel(cargo_type) + (class_name == "" ? "" : "; CargoClass = " + class_name));
if (town_effect != AICargo.TE_NONE) {
// local effect_name;
// switch(town_effect) {
// case AICargo.TE_PASSENGERS: effect_name = "TE_PASSENGERS"; break;
// case AICargo.TE_MAIL: effect_name = "TE_MAIL"; break;
// case AICargo.TE_GOODS: effect_name = "TE_GOODS"; break;
// case AICargo.TE_WATER: effect_name = "TE_WATER"; break;
// case AICargo.TE_FOOD: effect_name = "TE_FOOD"; break;
// }
// AILog.Info(" - Effect of " + AICargo.GetCargoLabel(cargo_type) + " in " + AITown.GetName(town) + " is " + effect_name);
local cargo_goal = AITown.GetCargoGoal(town, town_effect);
if (cargo_goal != 0) {
// AILog.Info(" - An amount of " + cargo_goal + " " + AICargo.GetCargoLabel(cargo_type) + " is required to grow " + AITown.GetName(town));
cargoRequired.AddItem(cargo_type, cargo_goal);
}
}
}
// AILog.Info(" ");
local num_cargo_types_required = cargoRequired.Count();
local result = null;
if (num_cargo_types_required == 0 || cargoRequired.HasItem(cargo) && num_cargo_types_required == 1) {
result = true;
} else {
result = false;
}
// AILog.Info("-- Result for town " + AITown.GetName(town) + ": " + result + " - " + num_cargo_types_required + " --");
return result;
}
function Utils::checkAdjacentNonRoadStation(stationTile, stationId)
{
if (stationId != AIStation.STATION_NEW) {
return stationId;
}
if (!AIController.GetSetting("station_spread") || !AIGameSettings.GetValue("distant_join_stations")) {
return AIStation.STATION_NEW;
}
local spread_rad = AIGameSettings.GetValue("station_spread") - 1;
local tileList = AITileList();
local spreadrectangle = [Utils.getValidOffsetTile(stationTile, (-1) * spread_rad, (-1) * spread_rad), Utils.getValidOffsetTile(stationTile, spread_rad, spread_rad)];
tileList.AddRectangle(spreadrectangle[0], spreadrectangle[1]);
local templist = AITileList();
for (local tile = tileList.Begin(); !tileList.IsEnd(); tile = tileList.Next()) {
if (Utils.isTileMyStationWithoutRoadStationOfAnyType(tile)) {
tileList.SetValue(tile, AIStation.GetStationID(tile));
} else {
templist.AddTile(tile);
}
}
tileList.RemoveList(templist);
local stationList = AIList();
for (local tile = tileList.Begin(); !tileList.IsEnd(); tileList.Next()) {
stationList.AddItem(tileList.GetValue(tile), AITile.GetDistanceManhattanToTile(tile, stationTile));
}
local spreadrectangle_top_x = AIMap.GetTileX(spreadrectangle[0]);
local spreadrectangle_top_y = AIMap.GetTileY(spreadrectangle[0]);
local spreadrectangle_bot_x = AIMap.GetTileX(spreadrectangle[1]);
local spreadrectangle_bot_y = AIMap.GetTileY(spreadrectangle[1]);
local list = AIList();
list.AddList(stationList);
for (local stationId = stationList.Begin(); !stationList.IsEnd(); stationId = stationList.Next()) {
local stationTiles = AITileList_StationType(stationId, AIStation.STATION_ANY);
local station_top_x = AIMap.GetTileX(AIBaseStation.GetLocation(stationId));
local station_top_y = AIMap.GetTileY(AIBaseStation.GetLocation(stationId));
local station_bot_x = station_top_x;
local station_bot_y = station_top_y;
for (local tile = stationTiles.Begin(); !stationTiles.IsEnd(); tile = stationTiles.Next()) {
local tile_x = AIMap.GetTileX(tile);
local tile_y = AIMap.GetTileY(tile);
if (tile_x < station_top_x) {
station_top_x = tile_x;
}
if (tile_x > station_bot_x) {
station_bot_x = tile_x;
}
if (tile_y < station_top_y) {
station_top_y = tile_y;
}
if (tile_y > station_bot_y) {
station_bot_y = tile_y;
}
}
if (spreadrectangle_top_x > station_top_x ||
spreadrectangle_top_y > station_top_y ||
spreadrectangle_bot_x < station_bot_x ||
spreadrectangle_bot_y < station_bot_y) {
list.RemoveItem(stationId);
}
}
list.Sort(AIList.SORT_BY_VALUE, true);
local adjacentStation = AIStation.STATION_NEW;
if (list.Count()) {
adjacentStation = list.Begin();
// AILog.Info("adjacentStation = " + AIStation.GetName(adjacentStation) + " ; stationtTile = " + AIMap.GetTileX(stationTile) + "," + AIMap.GetTileY(stationTile));
}
return adjacentStation;
}
function Utils::checkAdjacentNonDock(stationTile)
{
if (!AIController.GetSetting("station_spread") || !AIGameSettings.GetValue("distant_join_stations")) {
return AIStation.STATION_NEW;
}
local tileList = AITileList();
local spreadrectangle = Utils.expandAdjacentDockRect(stationTile);
tileList.AddRectangle(spreadrectangle[0], spreadrectangle[1]);
local templist = AITileList();
for (local tile = tileList.Begin(); !tileList.IsEnd(); tile = tileList.Next()) {
if (Utils.isTileMyStationWithoutDock(tile)) {
tileList.SetValue(tile, AIStation.GetStationID(tile));
} else {
templist.AddTile(tile);
}
}
tileList.RemoveList(templist);
local stationList = AIList();
for (local tile = tileList.Begin(); !tileList.IsEnd(); tileList.Next()) {
stationList.AddItem(tileList.GetValue(tile), AITile.GetDistanceManhattanToTile(tile, stationTile));
}
local spreadrectangle_top_x = AIMap.GetTileX(spreadrectangle[0]);
local spreadrectangle_top_y = AIMap.GetTileY(spreadrectangle[0]);
local spreadrectangle_bot_x = AIMap.GetTileX(spreadrectangle[1]);
local spreadrectangle_bot_y = AIMap.GetTileY(spreadrectangle[1]);
local list = AIList();
list.AddList(stationList);
for (local stationId = stationList.Begin(); !stationList.IsEnd(); stationId = stationList.Next()) {
local stationTiles = AITileList_StationType(stationId, AIStation.STATION_ANY);
local station_top_x = AIMap.GetTileX(AIBaseStation.GetLocation(stationId));
local station_top_y = AIMap.GetTileY(AIBaseStation.GetLocation(stationId));
local station_bot_x = station_top_x;
local station_bot_y = station_top_y;
for (local tile = stationTiles.Begin(); !stationTiles.IsEnd(); tile = stationTiles.Next()) {
local tile_x = AIMap.GetTileX(tile);
local tile_y = AIMap.GetTileY(tile);
if (tile_x < station_top_x) {
station_top_x = tile_x;
}
if (tile_x > station_bot_x) {
station_bot_x = tile_x;
}
if (tile_y < station_top_y) {
station_top_y = tile_y;
}
if (tile_y > station_bot_y) {
station_bot_y = tile_y;
}
}
if (spreadrectangle_top_x > station_top_x ||
spreadrectangle_top_y > station_top_y ||
spreadrectangle_bot_x < station_bot_x ||
spreadrectangle_bot_y < station_bot_y) {
list.RemoveItem(stationId);
}
}
list.Sort(AIList.SORT_BY_VALUE, true);
local adjacentStation = AIStation.STATION_NEW;
if (list.Count()) {
adjacentStation = list.Begin();
// AILog.Info("adjacentStation = " + AIStation.GetName(adjacentStation) + " ; stationtTile = " + AIMap.GetTileX(stationTile) + "," + AIMap.GetTileY(stationTile));
}
return adjacentStation;
}
function Utils::expandAdjacentDockRect(dockTile) {
local slope = AITile.GetSlope(dockTile);
if (slope != AITile.SLOPE_SW && slope != AITile.SLOPE_NW && slope != AITile.SLOPE_SE && slope != AITile.SLOPE_NE) return [dockTile, dockTile]; // shouldn't happen
local offset = 0;
local tile2 = AIMap.TILE_INVALID;
if (slope == AITile.SLOPE_NE) offset = AIMap.GetTileIndex(1, 0);
if (slope == AITile.SLOPE_SE) offset = AIMap.GetTileIndex(0, -1);
if (slope == AITile.SLOPE_SW) offset = AIMap.GetTileIndex(-1, 0);
if (slope == AITile.SLOPE_NW) offset = AIMap.GetTileIndex(0, 1);
tile2 = dockTile + offset;
local temp_tile1 = dockTile;
local temp_tile2 = tile2;
if (temp_tile1 > temp_tile2) {
local swap = temp_tile1;
temp_tile1 = temp_tile2;
temp_tile2 = swap;
}
local x_length = 1;
local y_length = 1;
if (temp_tile2 - temp_tile1 == 1) {
x_length = 2;
} else {
y_length = 2;
}
local spread_rad = AIGameSettings.GetValue("station_spread");
local dock_x = x_length;
local dock_y = y_length;
local remaining_x = spread_rad - dock_x;
local remaining_y = spread_rad - dock_y;
local tile_top_x = AIMap.GetTileX(temp_tile1);
local tile_top_y = AIMap.GetTileY(temp_tile1);
local tile_bot_x = tile_top_x + dock_x - 1;
local tile_bot_y = tile_top_y + dock_y - 1;
for (local x = remaining_x; x > 0; x--) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(tile_top_x - 1, tile_top_y))) {
tile_top_x = tile_top_x - 1;
}
if (AIMap.IsValidTile(AIMap.GetTileIndex(tile_bot_x + 1, tile_bot_y))) {
tile_bot_x = tile_bot_x + 1;
}
}
for (local y = remaining_y; y > 0; y--) {
if (AIMap.IsValidTile(AIMap.GetTileIndex(tile_top_x, tile_top_y - 1))) {
tile_top_y = tile_top_y - 1;
}
if (AIMap.IsValidTile(AIMap.GetTileIndex(tile_bot_x, tile_bot_y + 1))) {
tile_bot_y = tile_bot_y + 1;
}
}
// AILog.Info("spreadrectangle top = " + tile_top_x + "," + tile_top_y + " ; spreadrectangle bottom = " + tile_bot_x + "," + tile_bot_y);
return [AIMap.GetTileIndex(tile_top_x, tile_top_y), AIMap.GetTileIndex(tile_bot_x, tile_bot_y)];
}
/**
* Distance a vehicle engine runs when moving at its maximum speed for the given time
*/
function Utils::GetEngineTileDist(engine_id, days_in_transit)
{
local veh_type = AIEngine.GetVehicleType(engine_id);
/* Assuming going in axis, it is the same as distancemanhattan */
if (veh_type == AIVehicle.VT_ROAD) {
return ((AIEngine.GetMaxSpeed(engine_id) * 2 * 74 * days_in_transit * 3) / 4) / (192 * 16);
} else if (veh_type == AIVehicle.VT_WATER) {
return (AIEngine.GetMaxSpeed(engine_id) * 2 * 74 * days_in_transit) / (256 * 16);
} else {
assert(false);
}
}
function Utils::RemovingCanalBlocksConnection(tile)
{
local t_sw = tile + AIMap.GetTileIndex(1, 0);
local t_ne = tile + AIMap.GetTileIndex(-1, 0);
local t_se = tile + AIMap.GetTileIndex(0, 1);
local t_nw = tile + AIMap.GetTileIndex(0, -1);
if (AIMarine.IsLockTile(t_se) && AITile.GetSlope(t_se) == AITile.SLOPE_FLAT && Utils.CheckLockDirection(t_se, Utils.GetLockMiddleTile(t_se)) ||
AIMarine.IsLockTile(t_nw) && AITile.GetSlope(t_nw) == AITile.SLOPE_FLAT && Utils.CheckLockDirection(t_nw, Utils.GetLockMiddleTile(t_nw)) ||
AIMarine.IsLockTile(t_ne) && AITile.GetSlope(t_ne) == AITile.SLOPE_FLAT && Utils.CheckLockDirection(t_ne, Utils.GetLockMiddleTile(t_ne)) ||
AIMarine.IsLockTile(t_sw) && AITile.GetSlope(t_sw) == AITile.SLOPE_FLAT && Utils.CheckLockDirection(t_sw, Utils.GetLockMiddleTile(t_sw))) {
return true;
}
if (AIMarine.IsDockTile(t_se) && Utils.GetDockDockingTile(t_se) == tile ||
AIMarine.IsDockTile(t_nw) && Utils.GetDockDockingTile(t_nw) == tile ||
AIMarine.IsDockTile(t_ne) && Utils.GetDockDockingTile(t_ne) == tile ||
AIMarine.IsDockTile(t_sw) && Utils.GetDockDockingTile(t_sw) == tile) {
return true;
}
local t_e = tile + AIMap.GetTileIndex(-1, 1);
if (AIMarine.AreWaterTilesConnected(tile, t_se) && AIMarine.AreWaterTilesConnected(tile, t_ne)) {
if (!(AIMarine.AreWaterTilesConnected(t_e, t_se) && AIMarine.AreWaterTilesConnected(t_e, t_ne))) {
return true;
}
}
local t_n = tile + AIMap.GetTileIndex(-1, -1);
if (AIMarine.AreWaterTilesConnected(tile, t_nw) && AIMarine.AreWaterTilesConnected(tile, t_ne)) {
if (!(AIMarine.AreWaterTilesConnected(t_n, t_nw) && AIMarine.AreWaterTilesConnected(t_n, t_ne))) {
return true;
}
}
local t_s = tile + AIMap.GetTileIndex(1, 1);
if (AIMarine.AreWaterTilesConnected(tile, t_se) && AIMarine.AreWaterTilesConnected(tile, t_sw)) {
if (!(AIMarine.AreWaterTilesConnected(t_s, t_se) && AIMarine.AreWaterTilesConnected(t_s, t_sw))) {
return true;
}
}
local t_w = tile + AIMap.GetTileIndex(1, -1);
if (AIMarine.AreWaterTilesConnected(tile, t_nw) && AIMarine.AreWaterTilesConnected(tile, t_sw)) {
if (!(AIMarine.AreWaterTilesConnected(t_w, t_nw) && AIMarine.AreWaterTilesConnected(t_w, t_sw))) {
return true;
}
}
if (AIMarine.AreWaterTilesConnected(tile, t_se) && AIMarine.AreWaterTilesConnected(tile, t_nw)) {
if (AIMarine.AreWaterTilesConnected(t_s, t_se) && AIMarine.AreWaterTilesConnected(t_s, t_sw)) {
if (!(AIMarine.AreWaterTilesConnected(t_w, t_nw) && AIMarine.AreWaterTilesConnected(t_w, t_sw))) {
return true;
}
} else if (AIMarine.AreWaterTilesConnected(t_e, t_se) && AIMarine.AreWaterTilesConnected(t_e, t_ne)) {
if (!(AIMarine.AreWaterTilesConnected(t_n, t_nw) && AIMarine.AreWaterTilesConnected(t_n, t_ne))) {
return true;
}
} else {
return true;
}
}
if (AIMarine.AreWaterTilesConnected(tile, t_ne) && AIMarine.AreWaterTilesConnected(tile, t_sw)) {
if (AIMarine.AreWaterTilesConnected(t_e, t_se) && AIMarine.AreWaterTilesConnected(t_e, t_ne)) {
if (!(AIMarine.AreWaterTilesConnected(t_s, t_se) && AIMarine.AreWaterTilesConnected(t_s, t_sw))) {
return true;
}
} else if (AIMarine.AreWaterTilesConnected(t_n, t_nw) && AIMarine.AreWaterTilesConnected(t_n, t_ne)) {
if (!(AIMarine.AreWaterTilesConnected(t_w, t_nw) && AIMarine.AreWaterTilesConnected(t_w, t_sw))) {
return true;
}
} else {
return true;
}
}
return false;
}
/**
* Get the tile where ships can use to dock at the given dock.
* @param dock_tile A tile that is part of the dock.
* @return The tile where ships dock at the given dock.
*/
function Utils::GetDockDockingTile(dock_tile)
{
assert(AIMarine.IsDockTile(dock_tile));
local dock_slope;
if (AITile.GetSlope(dock_tile) == AITile.SLOPE_FLAT) {
foreach (offset in [AIMap.GetMapSizeX(), -AIMap.GetMapSizeX(), 1, -1]) {
local offset_tile = dock_tile + offset;
if (AIMarine.IsDockTile(offset_tile)) {
local slope = AITile.GetSlope(offset_tile);
if (slope == AITile.SLOPE_SW || slope == AITile.SLOPE_NW || slope == AITile.SLOPE_SE || slope == AITile.SLOPE_NE) {
dock_slope = offset_tile;
break;
}
}
}
} else {
dock_slope = dock_tile;
}
local slope = AITile.GetSlope(dock_slope);
if (slope == AITile.SLOPE_NE) {
return dock_slope + 2;
} else if (slope == AITile.SLOPE_SE) {
return dock_slope - 2 * AIMap.GetMapSizeX();
} else if (slope == AITile.SLOPE_SW) {
return dock_slope - 2;
} else if (slope == AITile.SLOPE_NW) {
return dock_slope + 2 * AIMap.GetMapSizeX();
}
}
/**
* Check whether the tile we're coming from is compatible with the axis of a
* lock planned in this location.
* @param prev_tile The tile we're coming from.
* @param middle_tile The tile of the middle part of a planned lock.
* @return true if the previous tile is compatible with the axis of the planned lock.
*/
function Utils::CheckLockDirection(prev_tile, middle_tile)
{
local slope = AITile.GetSlope(middle_tile);
assert(slope == AITile.SLOPE_SW || slope == AITile.SLOPE_NW || slope == AITile.SLOPE_SE || slope == AITile.SLOPE_NE);
if (slope == AITile.SLOPE_SW || slope == AITile.SLOPE_NE) {
return prev_tile == middle_tile + 1 || prev_tile == middle_tile - 1;
} else if (slope == AITile.SLOPE_SE || slope == AITile.SLOPE_NW) {
return prev_tile == middle_tile + AIMap.GetMapSizeX() || prev_tile == middle_tile - AIMap.GetMapSizeX();
}
return false;
}
/**
* Get the tile of the middle part of a lock.
* @param tile The tile of a part of a lock.
* @return The tile of the middle part of the same lock.
*/
function Utils::GetLockMiddleTile(tile)
{
assert(AIMarine.IsLockTile(tile));
local slope = AITile.GetSlope(tile);
if (slope == AITile.SLOPE_SW || slope == AITile.SLOPE_NW || slope == AITile.SLOPE_SE || slope == AITile.SLOPE_NE) return tile;
assert(slope == AITile.SLOPE_FLAT);
local other_end = Utils.GetOtherLockEnd(tile);
return tile - (tile - other_end) / 2;
}
/**
* Get the tile of the other end of a lock.
* @param tile The tile of the entrance of a lock.
* @return The tile of the other end of the same lock.
*/
function Utils::GetOtherLockEnd(tile)
{
assert(AIMarine.IsLockTile(tile) && AITile.GetSlope(tile) == AITile.SLOPE_FLAT);
foreach (offset in [AIMap.GetMapSizeX(), -AIMap.GetMapSizeX(), 1, -1]) {
local middle_tile = tile + offset;
local slope = AITile.GetSlope(middle_tile);
if (AIMarine.IsLockTile(middle_tile) && (slope == AITile.SLOPE_SW || slope == AITile.SLOPE_NW || slope == AITile.SLOPE_SE || slope == AITile.SLOPE_NE)) {
return middle_tile + offset;
}
}
}
/**
* Special check determining the possibility of a two tile sized
* aqueduct sharing the same edge to be built here.
* Checks wether the slopes are suitable and in the correct
* direction for such aqueduct.
* @param tile_a The starting tile of a two tile sized aqueduct.
* @param tile_b The ending tile of a two tile sized aqueduct.
* @return true if the slopes are suitable for a two tile sized aqueduct.
*/
function Utils::CheckAqueductSlopes(tile_a, tile_b)
{
if (AIMap.DistanceManhattan(tile_a, tile_b) != 1) return false;
local slope_a = AITile.GetSlope(tile_a);
local slope_b = AITile.GetSlope(tile_b);
if ((slope_a != AITile.SLOPE_SW && slope_a != AITile.SLOPE_NW && slope_a != AITile.SLOPE_SE && slope_a != AITile.SLOPE_NE) ||
(slope_b != AITile.SLOPE_SW && slope_b != AITile.SLOPE_NW && slope_b != AITile.SLOPE_SE && slope_b != AITile.SLOPE_NE)) {
return false;
}
if (AITile.GetComplementSlope(slope_a) != slope_b) return false;
local offset;
if (slope_a == AITile.SLOPE_NE) {
offset = 1;
} else if (slope_a == AITile.SLOPE_SE) {
offset = -AIMap.GetMapSizeX();
} else if (slope_a == AITile.SLOPE_SW) {
offset = -1;
} else if (slope_a == AITile.SLOPE_NW) {
offset = AIMap.GetMapSizeX();
}
return tile_a + offset == tile_b;
}
function Utils::estimateTownRectangle(town)
{
local townLocation = AITown.GetLocation(town);
local rectangleIncreaseKoeficient = 1;
local topCornerTile = townLocation;
local bottomCornerTile = townLocation;
local isMaxExpanded = false;
while(!isMaxExpanded) {
local maxExpandedCounter = 0;
for (local i = 0; i < 4; ++i) {
switch(i) {
case 0:
local offsetTile = Utils.getOffsetTile(topCornerTile, (-1) * rectangleIncreaseKoeficient, 0);
if (offsetTile == AIMap.TILE_INVALID) {
++maxExpandedCounter;
continue;
}
if (AITown.IsWithinTownInfluence(town, offsetTile)) {
topCornerTile = offsetTile;
}
else {
++maxExpandedCounter;
continue;
}
break;
case 1:
local offsetTile = Utils.getOffsetTile(bottomCornerTile, 0, rectangleIncreaseKoeficient);
if (offsetTile == AIMap.TILE_INVALID) {
++maxExpandedCounter;
continue;
}
if (AITown.IsWithinTownInfluence(town, offsetTile)) {
bottomCornerTile = offsetTile;
}
else {
++maxExpandedCounter;
continue;
}
break;
case 2:
local offsetTile = Utils.getOffsetTile(bottomCornerTile, rectangleIncreaseKoeficient, 0);
if (offsetTile == AIMap.TILE_INVALID) {
++maxExpandedCounter;
continue;
}
if (AITown.IsWithinTownInfluence(town, offsetTile)) {
bottomCornerTile = offsetTile;
}
else {
++maxExpandedCounter;
continue;
}
break;
case 3:
local offsetTile = Utils.getOffsetTile(topCornerTile, 0, (-1) * rectangleIncreaseKoeficient);