-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairfinder.nut
1194 lines (993 loc) · 37.1 KB
/
pairfinder.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
/*
* When computing produced - transported, this
* factor gets multiplied with transported and
* then divided by 100.
*
* A value > 100 means that the AI will be
* discouraged to compete on resources with
* other AIs
*/
TRANSPORTED_FACTOR_PERCENT <- 150;
class PairFinder {
all_nodes = null;
vehicle_type_cargos = null; // table with TM_* as key => list of cargos that VT accepts
any_tm_cargoes = null; // => list of cargos than any VT can take
air_cargo_max_range = null; // => list of max air range per cargo
constructor()
{
all_nodes = [];
vehicle_type_cargos = null;
}
function GetTransportModes(); // a list of transport modes that are allowed to use
function UpdateVehicleCargoCapability(); // update member variables for which cargoes each vehicle type can transport
function AddNodesSorted(desperateness, connection_list);
// Helper functions to AddNodesSorted
function AddTownNodes(desperateness, connection_list, node_heap, bonus_cargos);
function AddIndustryNodes(desperateness, connection_list, node_heap, bonus_cargos);
// returns {
// pair -> [source node, dest node]
// transport_mode -> TM_*
// }
function FindTwoNodesToConnect(maxDistance, desperateness, connection_list);
static function TownDistanceIdealityValuator(town_id, node_tile);
static function IndustryDistanceIdealityValuator(industry_id, node_tile);
static function SquirrelListComperator_PairScore(pair1, pair2);
static function IsPairNearExistingNetwork(node1, node2, connection_list);
static function ExistingNetworkAreaPreCache(connection_list, desperateness);
}
function PairFinder::GetTransportModes()
{
// transport modes to try for each pair
local try_tm = [];
local use_air = Vehicle.GetVehiclesLeft(AIVehicle.VT_AIR) >= MIN_VEHICLES_TO_BUILD_NEW;
local use_road = Vehicle.GetVehiclesLeft(AIVehicle.VT_ROAD) >= MIN_VEHICLES_TO_BUILD_NEW;
local use_air_only = !use_road && use_air;
if(use_air)
try_tm.append(TM_AIR);
if(use_road)
try_tm.append(TM_ROAD);
return try_tm;
}
function PairFinder::UpdateVehicleCargoCapability()
{
local tm_list = this.GetTransportModes();
this.vehicle_type_cargos = {};
this.any_tm_cargoes = AIList();
foreach(tm in tm_list)
{
// Get a list of all cargoes that can be transported with current transport mode
local available_cargos = AICargoList();
available_cargos.Valuate(Engine.DoesEngineExistForCargo, TransportModeToVehicleType(tm), true, true, false);
available_cargos.KeepValue(1);
this.vehicle_type_cargos.rawset(tm, available_cargos);
// Add cargoes to list of cargoes for which there is at least one transport mode that can transport them
this.any_tm_cargoes.AddList(available_cargos);
if(tm == TM_AIR)
{
// Get the aircraft range of the suggested planning engine for each cargo
this.air_cargo_max_range = AIList();
foreach(cargo, _ in available_cargos)
{
local engine = Strategy.FindEngineModelToPlanFor(cargo, AIVehicle.VT_AIR, false, true, -1);
local max_range = AIEngine.GetMaximumOrderDistance(engine);
Log.Info("Max range for " + AICargo.GetCargoLabel(cargo) + " is " + max_range + " using " + AIEngine.GetName(engine), Log.LVL_DEBUG);
// Check that we can afford two airports + engine for this cargo
local airport_type = Strategy.GetAffordedAirportTypeForNewConnection(max_range, cargo);
if(airport_type != null)
{
this.air_cargo_max_range.AddItem(cargo, max_range);
}
else
{
this.air_cargo_max_range.AddItem(cargo, -1);
Log.Info(".. but 2 airports + 1 aircraft can't be afforded", Log.LVL_DEBUG);
}
}
}
}
}
function PairFinder::AddNodesSorted(desperateness, connection_list)
{
//// Get settings ////
/*
* 0 = Towns only
* 1 = Industries only
* 2 = Both towns and industries
*
* -1 = not set
*
* See info.nut
*/
local allowed_connection_types = AIController.GetSetting("connection_types");
// Default to only towns if a non-supported value is detected
// => backward compatibility
if(allowed_connection_types != 0 &&
allowed_connection_types != 1 &&
allowed_connection_types != 2)
{
allowed_connection_types = 0;
}
Log.Info("allowed connection types: " + allowed_connection_types, Log.LVL_DEBUG);
// Some cargos may have a bonus
// Only give a bonus if we have a profit so that the bonus doesn't
// cause the AI to get a first route with too low profit.
local my_company = AICompany.ResolveCompanyID(AICompany.COMPANY_SELF);
local profit = AICompany.GetQuarterlyIncome(my_company, AICompany.CURRENT_QUARTER) - AICompany.GetQuarterlyExpenses(my_company, AICompany.CURRENT_QUARTER);
local cargo_bonus = AIList();
if (profit > 2000 && AICompany.GetQuarterlyCompanyValue(my_company, AICompany.CURRENT_QUARTER) > 1000) {
local cargo_goal = g_no_car_goal.GetGoalCargoArray();
if (cargo_goal != null) {
foreach(cargo in cargo_goal) {
local to_transport = max(0, cargo.goal - cargo.transported);
local bonus = -1 * to_transport * 1000 / cargo.goal
cargo_bonus.AddItem(cargo.cargo, bonus);
}
}
}
//// Add Nodes to Fibonacci Heap ////
local node_heap = FibonacciHeap();
if(allowed_connection_types == 0 || allowed_connection_types == 2)
{
Log.Info("connect towns", Log.LVL_DEBUG);
AddTownNodes(desperateness, connection_list, node_heap, cargo_bonus);
}
if(allowed_connection_types == 1 || allowed_connection_types == 2)
{
// Temporarily code to require road for industry nodes for this release as the ongoing work to support aircraft
// at industries is not good enough yet
local use_road = Vehicle.GetVehiclesLeft(AIVehicle.VT_ROAD) >= MIN_VEHICLES_TO_BUILD_NEW;
if(use_road)
{
Log.Info("connect industries", Log.LVL_DEBUG);
AddIndustryNodes(desperateness, connection_list, node_heap, cargo_bonus);
}
}
//// Move nodes to Squirrel List ////
all_nodes.clear();
while(node_heap.Count() > 0)
{
local node = node_heap.Pop();
all_nodes.append(node);
}
}
function FilterCargoList(cargo_list, desperateness)
{
// In NoCarGoal games, only use non-goal cargos if we are desperate (or all goals
// have been completed)
if(g_no_car_goal.IsNoCarGoalGame() && desperateness <= 1)
{
local filter_completed = g_no_car_goal.GetNUncompletedGoals() > 0;
g_no_car_goal.FilterCargoList(cargo_list, filter_completed);
}
}
function PairFinder::AddTownNodes(desperateness, connection_list, node_heap, bonus_cargos)
{
local town_list = AITownList();
local allow_competition = AIController.GetSetting("allow_competition");
foreach(town_id, _ in town_list)
{
// Ignore towns with too low rating
if(!Town.TownRatingAllowStationBuilding(town_id)) {
Log.Info("Skip town with too low rating", Log.LVL_DEBUG);
continue;
}
// Add nodes for all cargos which can be produced/accepted by towns
local produced_cargo_list = Helper.GetTownProducedCargoList();
local accepted_cargo_list = Helper.GetTownAcceptedCargoList();
// Only consider cargos if there is an engine available to transport it
produced_cargo_list.KeepList(this.any_tm_cargoes);
accepted_cargo_list.KeepList(this.any_tm_cargoes);
/* produced_cargo_list.Valuate(Engine.DoesEngineExistForCargo, AIVehicle.VT_ROAD, true, true, false);
produced_cargo_list.KeepValue(1);
accepted_cargo_list.Valuate(Engine.DoesEngineExistForCargo, AIVehicle.VT_ROAD, true, true, false);
accepted_cargo_list.KeepValue(1);
*/
local cargo_list = AIList();
cargo_list.AddList(produced_cargo_list);
cargo_list.AddList(accepted_cargo_list);
// Filter list with respect to Goals
FilterCargoList(cargo_list, desperateness);
foreach(cargo_id, _ in cargo_list)
{
// Skip if the cargo is already transported from the town and competition is not allowed
if(!allow_competition && AITown.GetLastMonthSupplied(town_id, cargo_id) != 0)
continue;
local node = Node(town_id, -1, cargo_id);
// Only actually append the node if it accepts/produces the cargo - dummy nodes do not make anyone happy
if (node.IsCargoAccepted() || node.IsCargoProduced())
{
local bonus = 0;
if (bonus_cargos.HasItem(cargo_id)) bonus = bonus_cargos.GetValue(cargo_id);
node_heap.Insert(node, -1 * node.GetCargoValueAvailability() + bonus);
}
}
}
}
function PairFinder::AddIndustryNodes(desperateness, connection_list, node_heap, bonus_cargos)
{
local industry_list = AIIndustryList();
local cargo_list = AICargoList();
// Only consider cargos if there is an engine available to transport it
cargo_list.KeepList(this.any_tm_cargoes);
/* cargo_list.Valuate(Engine.DoesEngineExistForCargo, AIVehicle.VT_ROAD, true, true, false);
cargo_list.KeepValue(1);*/
// Filter list with respect to Goals
FilterCargoList(cargo_list, desperateness);
Log.Info("cargo_list size: " + cargo_list.Count(), Log.LVL_DEBUG);
if (cargo_list.IsEmpty()) return;
local t_start = 0;
local t_end = 0;
local num_industries_added = 0;
local MAX_NUM_INDUSTRIES = 1000;
local industry_list_length = industry_list.Count();
local add_all = industry_list_length <= MAX_NUM_INDUSTRIES;
local i_industry = -1;
Log.Info("industry_list size: " + industry_list_length, Log.LVL_DEBUG);
Log.Info("add all: " + add_all, Log.LVL_DEBUG);
local allow_competition = AIController.GetSetting("allow_competition");
Log.Info("allow competition: " + allow_competition, Log.LVL_DEBUG);
foreach(industry_id, _ in industry_list)
{
// Ignore industries in towns with too low rating
if(!Town.TownRatingAllowStationBuilding(AITile.GetClosestTown(AIIndustry.GetLocation(industry_id))))
continue;
if(!add_all)
{
++i_industry;
local add = AIBase.Chance(MAX_NUM_INDUSTRIES, industry_list_length);
// Check if the amount needed is less than or equal to the amount of industries left in the list
if(MAX_NUM_INDUSTRIES - num_industries_added <= industry_list_length - i_industry)
add_all = true; // if so, just add all the remaining industries
if(!add)
continue;
}
foreach(cargo_id, _ in cargo_list)
{
if (AIIndustry.IsCargoAccepted(industry_id, cargo_id) == AIIndustry.CAS_ACCEPTED || // is the cargo accepted
(
AIIndustry.GetLastMonthProduction(industry_id, cargo_id) > 0 // or cargo is it produced
&&
(allow_competition || AIIndustry.GetLastMonthTransported(industry_id, cargo_id) == 0) // and competition is allowed or there is no transportation
) )
{
local node = Node(-1, industry_id, cargo_id);
local bonus = 0;
if (bonus_cargos.HasItem(cargo_id)) bonus = bonus_cargos.GetValue(cargo_id);
node_heap.Insert(node, -1 * node.GetCargoValueAvailability() + bonus);
}
}
}
}
function PairFinder::CanAffordAirConnection()
{
foreach(cargo, range in this.air_cargo_max_range)
{
if(range != -1)
{
Log.Info("Pairfinder: Can afford air connection for " + AICargo.GetCargoLabel(cargo), Log.LVL_DEBUG);
return true;
}
}
Log.Info("Pairfinder: Can't afford air connections", Log.LVL_INFO);
return false;
}
function PairFinder::FindTwoNodesToConnect(desperateness, connection_list)
{
// Rebuild cargo/vehicle availability
this.UpdateVehicleCargoCapability();
// Rebuild the list of nodes
this.AddNodesSorted(desperateness, connection_list);
Log.Info("a total of " + all_nodes.len() + " nodes has been added", Log.LVL_SUB_DECISIONS);
if (Log.IsLevelAccepted(Log.LVL_DEBUG)) {
local s = "";
foreach(node in all_nodes) {
if (s != "") s += ", ";
s += node.GetName();
if (s.len() > 80) {
Log.Info("nodes: " + s, Log.LVL_DEBUG);
s = "";
}
}
Log.Info("nodes: " + s, Log.LVL_DEBUG);
Log.Info("all nodes has been sorted by cargo value availability", Log.LVL_DEBUG);
}
Log.Info("desperateness: " + desperateness, Log.LVL_DEBUG);
if(all_nodes.len() == 0)
{
Log.Info("No locations to transport between was found. This can happen if there are no buses/lories available or if there is nothing more to connect.", Log.LVL_SUB_DECISIONS);
return null;
}
// transport modes to try for each pair
local try_tm = [];
local use_air = Vehicle.GetVehiclesLeft(AIVehicle.VT_AIR) >= MIN_VEHICLES_TO_BUILD_NEW && this.CanAffordAirConnection();
local use_road = Vehicle.GetVehiclesLeft(AIVehicle.VT_ROAD) >= MIN_VEHICLES_TO_BUILD_NEW;
local use_air_only = !use_road && use_air;
if(use_air)
try_tm.append(TM_AIR);
if(use_road)
try_tm.append(TM_ROAD);
Log.Info("Pairfinder: Use air = " + use_air, Log.LVL_INFO);
Log.Info("Pairfinder: Use road = " + use_road, Log.LVL_INFO);
if(try_tm.len() == 0)
{
Log.Warning("Pairfinder: No transport mode possible", Log.LVL_INFO);
return null;
}
// Get min noise needed for aircraft
local airport_noise = 0;
if(use_air)
{
local airport_type_list = GetAirportTypeList_AllowedAndBuildable();
airport_type_list.Valuate(Airport.GetAirportTypeNoiseLevel);
airport_type_list.Sort(AIList.SORT_BY_VALUE, AIList.SORT_ASCENDING);
airport_noise = airport_type_list.GetValue(airport_type_list.Begin());
}
local aircraft_cargos = AICargoList();
aircraft_cargos.Valuate(Engine.DoesEngineExistForCargo, AIVehicle.VT_AIR);
aircraft_cargos.KeepValue(1);
// Get the ideal length
// TODO: Get ideal distance per cargo type for each vehicle type
/*{
local cargo_id = 0;
local engine_id = Strategy.FindEngineModelToPlanFor(cargo_id, AIVehicle.VT_ROAD);
local ideal_pax_distance = Engine.GetIdealTransportDistance(engine_id, cargo_id, -1);
//Log.Info("Plan for engine " + AIEngine.GetName(engine_id) + " which has an ideal transport distance of " + ideal_pax_distance);
}*/
local top_source_nodes = [];
local i = -1;
foreach(node in all_nodes)
{
if(!node.IsCargoProduced())
continue;
// Only accept nodes as sources if they are not in use for the given cargo
if(!Node.IsNodeInConnectionList(connection_list, node))
{
++i;
top_source_nodes.append(node);
Log.Info("top node: " + node.GetName(), Log.LVL_SUB_DECISIONS);
if (i >= 8 * (1 + desperateness)) break;
}
}
Log.Info("Top source node count: " + top_source_nodes.len(), Log.LVL_DEBUG);
local global_best_pairs = FibonacciHeap();
// Get the setting if the AI should expand new connections local to existing ones or simply look at the entire map.
local setting_expand_local = AIController.GetSetting("expand_local");
local existing_network_area_cached = null;
if(setting_expand_local)
{
existing_network_area_cached = PairFinder.ExistingNetworkAreaPreCache(connection_list, desperateness);
}
// Give a bonus to cargos that help achieving a goal
local bonus_cargos = AIList();
if(g_no_car_goal.IsNoCarGoalGame()) {
bonus_cargos = AICargoList();
FilterCargoList(bonus_cargos, 0);
}
// Find a good pair using one of the top-producing nodes
foreach(source_node in top_source_nodes)
{
local max_ideality_distance = 50; // accept max 50 tiles more/less than ideal distance
Log.Info("Source " + source_node.GetName(), Log.LVL_DEBUG);
// Look for nearby nodes that accept this cargo
local local_best_pairs = FibonacciHeap();
local i = -1;
foreach(dest_node in all_nodes)
{
++i;
if(dest_node.cargo_id != source_node.cargo_id) // only consider the node of the right cargo type of a given node
{
continue;
}
// In NoCarGoal games, only use non-goal cargos if we are desperate (or all goals
// have been completed)
if(g_no_car_goal.IsNoCarGoalGame() && desperateness <= 1 &&
g_no_car_goal.GetNUncompletedGoals() > 0 &&
!g_no_car_goal.IsGoalCargo(dest_node.cargo_id, true))
{
continue;
}
// Make sure to not connect the same node with itself
if((dest_node.town_id != -1 && dest_node.town_id == source_node.town_id) ||
(dest_node.industry_id != -1 && dest_node.industry_id == source_node.industry_id))
{
continue;
}
// If expand_local is enabled, reject all pairs that are not near the existing network.
// This uses a simple boundary box model that should execute fairly fast. (essentially 2 API calls and 4 comparisons)
if(setting_expand_local == 1 && !PairFinder.IsPairNearExistingNetwork(source_node, dest_node, existing_network_area_cached))
continue;
// Check that there exist no connection already between these two nodes
local existing_connection = Node.FindNodePairInConnectionList(connection_list, source_node, dest_node);
if(existing_connection != null)
{
// An existing non-failed or failed connection exist
continue;
}
// If the dest offers cargo, then make sure we don't transport from the destination as well.
if(dest_node.IsCargoProduced() && Node.IsNodeInConnectionList(connection_list, dest_node))
{
continue;
}
if(!dest_node.IsCargoAccepted())
continue;
// The selection of TM simply tries different TMs until a one that can handle the pair
// is found. Later either the order of tests or some other way it should be randomized or improved
local transport_mode = TM_INVALID;
local dist = AIMap.DistanceManhattan(dest_node.GetLocation(), source_node.GetLocation());
local possible_tms = [];
foreach(tm in try_tm)
{
if(tm == TM_AIR)
{
// Don't use aircraft if there is no aircraft that
// takes the given cargo
if(!aircraft_cargos.HasItem(source_node.cargo_id))
continue;
// Don't consider connections that are longer than max range - 20 tiles
// to allow for some bad luck based on station placement.
local order_dist = AIOrder.GetOrderDistance(AIVehicle.VT_AIR, dest_node.GetLocation(), source_node.GetLocation());
local max_engine_range = this.air_cargo_max_range.GetValue(source_node.cargo_id);
if(max_engine_range == -1) // -1 is PairFinder flag for cargos where 2 * airport + engine can't be afforded
continue;
if(max_engine_range != 0 && order_dist > max_engine_range - 20) // 0 is NoAI/OpenTTD flag for engines without limit
continue;
local budget_overrun = GetNoiseBudgetOverrun();
local dest_airport_noise = airport_noise;
local source_airport_noise = airport_noise;
if (dest_node.IsIndustry())
{
local dist = AIMap.DistanceManhattan(dest_node.GetLocation(), AITown.GetLocation(dest_node.GetClosestTown()))
dest_airport_noise /= max(1, (dist / 4));
}
if(source_node.IsIndustry())
{
local dist = AIMap.DistanceManhattan(source_node.GetLocation(), AITown.GetLocation(source_node.GetClosestTown()))
source_airport_noise /= max(1, (dist / 4));
}
// check that connections allow airport noise
if(AITown.GetAllowedNoise(source_node.GetClosestTown()) + budget_overrun < dest_airport_noise
|| AITown.GetAllowedNoise(dest_node.GetClosestTown()) + budget_overrun < source_airport_noise)
continue;
}
local dist_deviation = Helper.Abs(g_tm_stats[tm].ideal_construct_distance - dist); // 0 = correct dist and then increasing for how many tiles wrong the distance is
if (dist > g_tm_stats[tm].max_construct_distance * max(2, desperateness * 2) / 2)
continue;
if (dist_deviation > g_tm_stats[tm].max_construct_distance_deviation * max(2, desperateness * 2) / 2)
continue;
if (dist < g_tm_stats[tm].min_construct_distance * 2 / max(2, desperateness * 2 - 1))
continue;
// The pair can be connected using transport_mode
possible_tms.append(tm);
}
// Couldn't find any transport mode?
if(possible_tms.len() == 0)
continue;
// pick random transport mode
transport_mode = possible_tms[AIBase.RandRange(possible_tms.len())];
// Make sure picked transport mode is valid
if(transport_mode == TM_INVALID)
continue;
local score = 0;
// Penalty if the source industry can't increase production
local no_prod_increase_penalty = 0;
if(!AIIndustryType.ProductionCanIncrease( AIIndustry.GetIndustryType(source_node.industry_id) ))
{
no_prod_increase_penalty = 4000;
}
local prod_value = source_node.GetCargoValueAvailability();
// Can the same cargo be transported the other way around?
if(dest_node.GetCargoValueAvailability() > 0 && source_node.IsCargoAccepted())
{
local extra_value = dest_node.GetCargoValueAvailability();
// Also give penalty if the "dest" can't increase in case it produces cargo
if(AIIndustryType.ProductionCanIncrease( AIIndustry.GetIndustryType(dest_node.industry_id) ))
{
// Scale the penalty depending on how big the extra value is compared to the production by the main source.
no_prod_increase_penalty += (4000 * (extra_value.tofloat() / prod_value.tofloat()) ).tointeger();
}
prod_value += extra_value;
}
// Check if the destination is already supplyed with cargo => bonus score
local bonus = 0;
if(dest_node.IsIndustry() &&
ClueHelper.IsIndustryInConnectionList(connection_list, dest_node.industry_id, -1)) // -1 = any cargo
{
// Add a small bonus if we already supply the industry with some goods = has existing station
bonus += 200;
// Add a bigger bonus if the industry also produces some cargo (that can be transported)
if(AICargoList_IndustryProducing(dest_node.industry_id))
bonus += 2000;
}
// Give a significant bonus to cargos that help achieving a goal.
if (bonus_cargos.HasItem(source_node.cargo_id)) {
bonus += 3000;
}
// Make a combined score of the production value and distance deviation
local dist_deviation = Helper.Abs(g_tm_stats[transport_mode].ideal_construct_distance - dist); // 0 = correct dist and then increasing for how many tiles wrong the distance is
score = prod_value + (70 - dist_deviation) * 2 + bonus;
// if(transport_mode == TM_AIR)
// score += 1000; // at 2000, several air-links are built, but they aren't as good as the road links from an economical point of view.
local_best_pairs.Insert([source_node, dest_node, score, transport_mode], -score);
}
// Copy the 5 best "local best" pairs to the "global best" list
for(local j = 0; j < 5 && local_best_pairs.Count() > 0; ++j)
{
local pair = local_best_pairs.Pop();
local score = pair[2] + AIBase.RandRange(pair[2] / 3); // Randomize the scores a bit
global_best_pairs.Insert(pair, -score);
}
}
Log.Info("there is " + global_best_pairs.Count() + " 'global best pairs'", Log.LVL_SUB_DECISIONS);
// Get the best pair of the best
local best_pair = global_best_pairs.Pop();
if(best_pair == null)
{
// Failed to find a connection => remove a failed connection
// Remove out dated failed connections
Node.RemoveOldFailedConnections(connection_list, /* min age: */ 5 * 365, /* min num: */ 1);
return null;
}
Log.Info("top-list: ", Log.LVL_DEBUG);
Log.Info(best_pair[0].GetName() + " - " + best_pair[1].GetName() + " using " + TransportModeToString(best_pair[3]) + " => score: " + best_pair[2], Log.LVL_DEBUG);
while(global_best_pairs.Count() > 0)
{
local pair = global_best_pairs.Pop();
Log.Info(pair[0].GetName() + " - " + pair[1].GetName() + " using " + TransportModeToString(pair[3]) + " => score: " + pair[2], Log.LVL_DEBUG);
}
Log.Info("best pair score: " + best_pair[2] + " (" + best_pair[0].GetName() + " - " + best_pair[1].GetName() + ")", Log.LVL_SUB_DECISIONS);
return {
pair = [best_pair[0], best_pair[1]], // [source node, dest node]
transport_mode = best_pair[3],
};
}
/*
* Compute the bounding box of the current network in order to speedup the
* IsPairNearExistingNetwork function that is called once for every pair that
* make it to the top N pairs.
*/
/* static */ function PairFinder::ExistingNetworkAreaPreCache(connection_list, desperateness)
{
if(connection_list == null || connection_list.len() == 0)
return null;
local min_x = 100000;
local min_y = 100000;
local max_x = 0;
local max_y = 0;
foreach(conn in connection_list)
{
local loc_list = [conn.node[0].GetLocation(), conn.node[1].GetLocation()];
foreach(loc in loc_list)
{
local x = AIMap.GetTileX(loc);
local y = AIMap.GetTileY(loc);
if(x < min_x)
min_x = x;
if(x > max_x)
max_x = x;
if(y < min_y)
min_y = y;
if(y > max_y)
max_y = y;
}
}
local allowed_distance = desperateness > 1 ? desperateness * 20 : desperateness * 5;
min_x -= allowed_distance;
min_y -= allowed_distance;
max_x += allowed_distance;
max_y += allowed_distance;
local network_boundary = {
min_x = min_x,
min_y = min_y,
max_x = max_x,
max_y = max_y
};
Helper.SetSign( AIMap.GetTileIndex(min_x, min_y), "min xy" );
Helper.SetSign( AIMap.GetTileIndex(max_x, max_y), "max xy" );
return {
boundary = network_boundary,
connection_list = connection_list
};
}
/* static */ function PairFinder::IsPairNearExistingNetwork(node1, node2, pre_cached_existing_network)
{
if(node1 == null || node2 == null)
return false;
// If this is the first connection, it will be reported as it is within an existing
// network area so that all locations are valid for the first connection.
if(pre_cached_existing_network == null)
return true;
local loc_list = [node1.GetLocation(), node2.GetLocation()];
local boundary = pre_cached_existing_network.boundary;
local inside = false;
foreach(loc in loc_list)
{
local x = AIMap.GetTileX(loc);
local y = AIMap.GetTileY(loc);
if(x >= boundary.min_x && x <= boundary.max_x &&
y >= boundary.min_y && y <= boundary.max_y)
inside = true;
}
// It is enough that one of the two nodes is inside or nearby the boundary box of the current network.
return inside;
}
/* static */ function PairFinder::TownDistanceIdealityValuator(town_id, node_tile)
{
return Helper.Abs(300 - AIMap.GetDistanceManhattan(AITown.GetLocation(town_id), node_tile));
}
/* static */ function PairFinder::IndustryDistanceIdealityValuator(industry_id, node_tile)
{
return Helper.Abs(300 - AIMap.GetDistanceManhattan(AIIndustry.GetLocation(town_id), node_tile));
}
/* static */ function PairFinder::SquirrelListComperator_PairScore(pair1, pair2)
{
if (pair1[2] < pair2[2])
return 1;
if (pair1[2] > pair2[2])
return -1;
return 0;
}
class Node {
town_id = null;
industry_id = null;
node_location = null; // for detecting moved industries (close down + reused id)
cargo_id = null;
constructor(townId, industryId, cargoId)
{
this.town_id = townId;
this.industry_id = industryId;
this.cargo_id = cargoId;
// Store the original location of industry/town in this.node_location
if(AIIndustry.IsValidIndustry(industryId))
this.node_location = AIIndustry.GetLocation(industryId);
else if(AITown.IsValidTown(townId))
this.node_location = AITown.GetLocation(townId);
else
this.node_location = -1;
}
function SaveToString();
static function CreateFromSaveString(save_str);
// Compares town/industry id + cargo_id
function IsEqualTo(node);
// If no id is given (id == null), then the functions check if the node is any town/industry. Else it checks if it is a specific town id or industry id.
function IsTown(town_id = null);
function IsIndustry(industry_id = null);
function HasNodeDissapeared();
function GetName();
function GetLocation();
function GetClosestTown();
/*function GetDistanceManhattan(tile);*/
// Checks the LastMonthProduction + Transported
function GetLastMonthProduction();
function GetCargoAvailability();
function GetCargoValueAvailability();
// Checks if cargo is accepted or produced at all
function IsCargoAccepted(); // boolean
function IsCargoProduced(); // boolean
static function SquirrelListComperator_CargoValueAvailability(node1, node2);
static function IsNodeInConnectionList(connection_list, node);
static function FindNodePairInConnectionList(connection_list, node1, node2); // returns the matching connection object or null
static function RemoveOldFailedConnections(connection_list, min_age, min_num); // remove old failed connections so that a new attempt can be made
}
function Node::IsEqualTo(node)
{
// The idea is that if both nodes has invalid town or industry their value shouldn't
// be compared. It is enough to check one of them for validity though.
return (AITown.IsValidTown(this.town_id) && this.town_id == node.town_id) ||
(AIIndustry.IsValidIndustry(this.industry_id) && this.industry_id == node.industry_id);
}
function Node::IsTown(town_id = null)
{
return AITown.IsValidTown(this.town_id) && (town_id == null || this.town_id == town_id);
}
function Node::IsIndustry(industry_id = null)
{
return AIIndustry.IsValidIndustry(this.industry_id) && (industry_id == null || this.industry_id == industry_id);
}
function Node::HasNodeDissapeared()
{
// Return true if
// - neither industry nor town
// - or if the industry id exists, but at another location than the original location
return !IsTown() && (!IsIndustry() || AIIndustry.GetLocation(this.industry_id) != this.node_location);
}
function Node::GetName()
{
if(IsTown())
return AITown.GetName(this.town_id) + " - " + AICargo.GetCargoLabel(this.cargo_id);
if(IsIndustry())
return AIIndustry.GetName(this.industry_id) + " - " + AICargo.GetCargoLabel(this.cargo_id);
return "<is neither a town nor an industry>";
}
function Node::GetLocation()
{
if(IsTown())
return AITown.GetLocation(this.town_id);
if(IsIndustry())
return AIIndustry.GetLocation(this.industry_id);
return -1;
}
function Node::GetClosestTown()
{
if(IsTown())
return this.town_id;
if(IsIndustry())
return AITile.GetClosestTown(AIIndustry.GetLocation(this.industry_id));
return -1;
}
/*function Node::GetDistanceManhattan(tile)
{
local self_loc = this.GetLocation();
return AIMap.DistanceManhattan(self_loc, tile);
}*/
function Node::GetLastMonthProduction()
{
local api = null;
local self_id = -1;
if(this.IsTown())
{
api = AITown;
self_id = town_id;
}
else if(this.IsIndustry())
{
api = AIIndustry;
self_id = industry_id;
}
else
{
return 0;
}
return api.GetLastMonthProduction(self_id, this.cargo_id);
}
function Node::GetCargoAvailability()
{
local api = null;
local self_id = -1;
if(this.IsTown())
{
api = AITown;
self_id = town_id;
}
else if(this.IsIndustry())
{
api = AIIndustry;
self_id = industry_id;
}
else
{
return 0;
}
return api.GetLastMonthTransportedPercentage(self_id, this.cargo_id);
}
// returns [value, cargo_id] for the cargo with higest value availability
function Node::GetCargoValueAvailability()
{
local api = null;
local self_id = -1;
local transported = 0;
if(this.IsTown())
{
api = AITown;
self_id = town_id;
transported = AITown.GetLastMonthSupplied(town_id, this.cargo_id);
}
else if(this.IsIndustry())
{
api = AIIndustry;
self_id = industry_id;
transported = AIIndustry.GetLastMonthTransported(industry_id, this.cargo_id);
}
else
{
return 0;
}
return (api.GetLastMonthProduction(self_id, this.cargo_id) - (transported * TRANSPORTED_FACTOR_PERCENT) / 100) *
AICargo.GetCargoIncome(this.cargo_id, 150, 20); // assume 150 tiles and 20 days in transit
}
/* static */ function Node::SquirrelListComperator_CargoValueAvailability(node1, node2)
{
local a = node1.GetCargoValueAvailability();
local b = node2.GetCargoValueAvailability();
if (a < b)
return 1;
if (a > b)
return -1;
return 0;
}
function Node::IsCargoAccepted()
{
if(this.IsTown())
{
// Hack: Use a list of cargos that towns accept eg PAX, MAIL, GOODS
// and reduce any other cargo so that we don't by mistake accept a cargo because of an industry
local town_cargos = Helper.GetTownAcceptedCargoList();
if(!town_cargos.HasItem(this.cargo_id))
return false;
// Second, check that the town in question actually accept the cargo
local town_location = AITown.GetLocation(this.town_id);
local acceptance = AITile.GetCargoAcceptance(town_location, this.cargo_id, 1, 1, 10);
return acceptance >= 8;
}
else if(this.IsIndustry())
{
return AIIndustry.IsCargoAccepted(this.industry_id, this.cargo_id) != AIIndustry.CAS_NOT_ACCEPTED;
}
else
{
return false;
}
}
function Node::IsCargoProduced()
{
if (this.IsTown())
return AITile.GetCargoProduction(this.GetLocation(), this.cargo_id, 1, 1, 5) > 0; // check a radius of 5 tiles for production
return Industry.IsCargoProduced(this.industry_id, this.cargo_id);
}
function Node::SaveToString()