-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrailmanager.nut
1402 lines (1294 loc) · 56.4 KB
/
railmanager.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
/**
* This file is part of WormAI: An OpenTTD AI.
*
* @file railmanager.nut Class containing the Rail Manager for WormAI.
* Based on code from SimpleAI.
*
* License: GNU GPL - version 2 (see license.txt)
* Author: Wormnest (Jacob Boerema)
* Copyright: Jacob Boerema, 2016.
*
*/
/**
* Define the WormRailManager class which handles trains.
*/
class WormRailManager
{
/** Building stages, needed to recover a savegame. */
static BS_NOTHING = 0;
static BS_BUILDING = 1;
static BS_REMOVING = 2;
static BS_ELECTRIFYING = 3;
/** Reasons to send a vehicle to a depot. */
static TD_SELL = 1;
static TD_REPLACE = 2;
static TD_ATTACH_WAGONS = 3;
/* Variables used by WormRailManager */
/* 1. Variables that will be saved in a savegame. (TODO) */
routes = null; ///< An array containing all our routes
groups = null; ///< The list of vehicle groups
serviced = null; ///< Industry/town - cargo pairs already serviced
railbridges = null; ///< The list of rail bridges
engine_blacklist = null; ///< The blacklist of train engines
buildingstage = null; ///< The current building stage
lastroute = null; ///< The date the last route was built
removelist = null; ///< An array used to continue rail removal and electrification
todepotlist = null; ///< A list of vehicles heading for the depot
route_without_trains = -1; ///< Group number or -1 of unfinished route that needs trains added
last_route = null; ///< route info table of last completed route. Needed if we still need to buy trains for this route.
/* 2. Variables that will NOT be saved. */
_current_railtype = 0; ///< The railtype we are currently using.
_planner = null; ///< The route planner class object.
_optimal_train_lengths = null; ///< For each group the current optimal trainlength, stored so we don't have to recompute it all the time.
/** Create an instance of WormRailManager and initialize our variables. */
constructor()
{
routes = [];
removelist = [];
groups = AIList();
serviced = AIList();
railbridges = AITileList();
engine_blacklist = AIList();
todepotlist = AIList();
_current_railtype = AIRail.RAILTYPE_INVALID;
_planner = WormPlanner(this);
route_without_trains = -1;
last_route = null;
_optimal_train_lengths = AIList(); /// @todo: Needs to be initialized when loading a Save!
AILog.Info("[RailManager initialized]");
}
/**
* Set the name of a vehicle group.
* @param group The GroupID of the group.
* @param crg The cargo transported.
* @param stasrc The source station.
* @note Taken from SimpleAI.
* @todo Move to a different unit, should be accessible from other managers too.
*/
static function SetGroupName(group, crg, stasrc);
/**
* Checks whether a given rectangle is within the influence of a given town.
* @param tile The topmost tile of the rectangle.
* @param town_id The TownID of the town to be checked.
* @param width The width of the rectangle.
* @param height The height of the rectangle.
* @return True if the rectangle is within the influence of the town.
* @todo Needs to be moved to a different unit. (Town?)
* @note Taken from SimpleAI.
*/
static function IsRectangleWithinTownInfluence(tile, town_id, width, height);
/**
* Updates the current rail type of the AI based on the maximum number of cargoes transportable.
*/
function UpdateRailType();
/**
* Sets the current rail type of the AI based on the maximum number of cargoes transportable.
* @return The new railtype that has been set.
* @todo Possible better evaluation what rail type is the most profitable.
*/
static function SetRailType();
/**
* Main function for building a railway which decides all the details.
*/
function BuildRailway();
/**
* Register the new route into the database.
* @param route_data A WormRoute class object containing info about the route.
* @param station_data A WormStation class object containing info about the station.
* @param vehtype The type of vehicle using this route. Currently always VT_RAIL.
* @param group The vehicle group for the vehicles using this route.
* @param train_length The train_length we start with measure in 1/16th of a tile.
* @return The new route registered.
*/
function RegisterRoute(route_data, station_data, vehtype, group, train_length);
/**
* Check if the specified cargo is currently produced by the specified station.
* For now we simply check if it has a cargo rating, but only for stations at least
* 1 year old since it can take a while to get a cargo rating.
* @param statinID The ID of the station.
* @param cargoID The ID of the cargo.
*/
function IsCargoProducedByStation(stationID, cargoID);
/**
* Check if the specified cargo is currently accepted by the specified station.
* @param statinID The ID of the station.
* @param cargoID The ID of the cargo.
*/
function IsCargoAcceptedByStation(stationID, cargoID);
/**
* Sell all trains that belong to the specified route.
* @param route The route which has the trains that need to be sold.
*/
function SellAllTrains(route);
/**
* Checks all routes. Empty routes are removed, new vehicles are added if needed, old vehicles are replaced,
* vehicles are restarted if sitting in the depot for no reason, rails are electrified, short trains are lengthened.
*/
function CheckRoutes();
/**
* Get the optimal train length for this route.
* @param route The route info.
* @return The optimal length in 1/16 of a tile.
*/
function GetOptimalTrainLength(route);
/**
* Get the minimum and maximum length of trains in the supplied list of vehicles.
* @param vehicles AIVehicleList containing the vehicles.
* @return A table giving "min_length" and "max_length" of the shortest and longest vehicle in the list.
* @note The length is given in 1/16 of a tile, thus a one tile train has length 16.
* @pre vehicles should not be null.
*/
function GetMinMaxTrainLength(vehicles);
/**
* Checks ungrouped vehicles. Under normal conditions all vehicles should be grouped.
*/
function CheckDefaultGroup();
/**
* Check if we should add another train to the specified group.
* @param route The route info table.
* @param vehicle_count The current number of vehicles in this group.
* @param first_vehicle The first vehicle in this group.
* @return True if we added a train, otherwise false.
*/
function CheckAddTrainToGroup(route, vehicle_count, first_vehicle);
/**
* Select optimal wagon and train engine for the specified route and buy it if possible.
* @param route The route info table.
* @return True if we added a train, otherwise false.
*/
function SelectAndAddTrain(route);
/**
* Adds a new vehicle to an existing route.
* @param route The route to which the new vehicle will be added.
* @param mainvehicle An already existing vehicle on the route to share orders with.
* @param engine The EngineID of the new vehicle. In case of trains it is the EngineID of the locomotive.
* @param wagon The EngineID of the train wagons.
* @return True if the action succeeded.
*/
function AddVehicle(route, mainvehicle, engine, wagon);
/**
* Replaces an old vehicle with a newer model if it is already in the depot.
* @param vehicle The vehicle to be replaced.
*/
function ReplaceVehicle(vehicle);
/**
* Handle a vehicle that is stopped in depot for selling, replacement or attaching wagons.
* @param vehicle The vehicle stopped in depot.
*/
function HandleVehicleInDepot(vehicle);
/**
* Check train profits and send unprofitable ones to depot to be sold.
*/
function CheckTrainProfits();
/**
* Check train profits and send unprofitable ones to depot to be sold.
* @param vehicle The vehicle that should be sent to depot to be sold.
*/
function SendTrainToDepotForSelling(vehicle);
/**
* Save all data that WormRailManager needs in table.
* @param table The table to store the data in.
* @pre table should be non null.
*/
function SaveData(table);
/**
* Load all data that WormRailManager from the table.
* @param table The table that has all the data.
* @param worm_save_version WormAI save data version.
* @pre table should be non null.
*/
function LoadData(table, worm_save_version);
/**
* Do any necessary processing after a savegame has been loaded.
* Currently recomputes the values for the @ref _optimal_train_lengths list.
*/
function AfterLoading();
}
function WormRailManager::SetGroupName(group, crg, stasrc)
{
local groupname = AICargo.GetCargoLabel(crg) + " - " + AIStation.GetName(stasrc);
if (groupname.len() > 30) groupname = groupname.slice(0, 30);
if (!AIGroup.SetName(group, groupname)) {
// Shorten the name if it is too long (Unicode character problems)
while (AIError.GetLastError() == AIError.ERR_PRECONDITION_STRING_TOO_LONG) {
groupname = groupname.slice(0, groupname.len() - 1);
AIGroup.SetName(group, groupname);
}
}
}
function WormRailManager::IsRectangleWithinTownInfluence(tile, town_id, width, height)
{
if (width <= 1 && height <= 1) return AITile.IsWithinTownInfluence(tile, town_id);
local offsetX = AIMap.GetTileIndex(width - 1, 0);
local offsetY = AIMap.GetTileIndex(0, height - 1);
return AITile.IsWithinTownInfluence(tile, town_id) ||
AITile.IsWithinTownInfluence(tile + offsetX + offsetY, town_id) ||
AITile.IsWithinTownInfluence(tile + offsetX, town_id) ||
AITile.IsWithinTownInfluence(tile + offsetY, town_id);
}
function WormRailManager::UpdateRailType()
{
local new_railtype = WormRailManager.SetRailType();
if (new_railtype != _current_railtype) {
_current_railtype = new_railtype;
AILog.Warning("Changed rail type we use to " + AIRail.GetName(_current_railtype));
}
}
function WormRailManager::SetRailType()
{
local railtypes = AIRailTypeList();
local cargoes = AICargoList();
local max_cargoes = 0;
local current_railtype = AIRail.RAILTYPE_INVALID;
// Check each rail type for the number of available cargoes
foreach (railtype, dummy in railtypes) {
// Avoid the universal rail in NUTS and other similar ones
local buildcost = AIRail.GetBuildCost(railtype, AIRail.BT_TRACK);
if (buildcost > WormMoney.InflationCorrection(2000)) continue;
current_railtype = AIRail.GetCurrentRailType();
AIRail.SetCurrentRailType(railtype);
local num_cargoes = 0;
// Count the number of available cargoes
foreach (cargo, dummy2 in cargoes) {
if (WormRailBuilder.ChooseWagon(cargo, null) != null) num_cargoes++;
}
if (num_cargoes > max_cargoes) {
max_cargoes = num_cargoes;
current_railtype = railtype;
}
AIRail.SetCurrentRailType(current_railtype);
}
return current_railtype;
}
function WormRailManager::BuildRailway()
{
/* Don't try to build a new rail line if we have reached the train limit. */
local trains = AIVehicleList();
trains.Valuate(AIVehicle.GetVehicleType);
trains.KeepValue(AIVehicle.VT_RAIL);
if (trains.Count() >= AIGameSettings.GetValue("vehicle.max_trains")) {
AILog.Warning("Not building a new rail route. We already have the maximum number of trains.");
return false;
}
/* Don't try to build a new railway if there is an unfinished route without trains. */
if (route_without_trains > -1) {
local result = SelectAndAddTrain(last_route);
if (result) {
route_without_trains = -1;
AILog.Warning("Train for last route got built. Route finished.");
}
else
AILog.Warning("Still can't add a train to the last route due to lack of money.");
return result;
}
if (!WormMoney.GetMoney(WormMoney.InflationCorrection(50000))) {
AILog.Info("Can't get enough money to build a rail route.");
return false;
}
/* Plan which route with which cargo we are going to build. */
if (!_planner.PlanRailRoute()) return false;
buildingstage = BS_NOTHING;
/* Show info about chosen route. */
local srcname, dstname = null;
local subsidy = "";
if (_planner.route.SourceIsTown) srcname = AITown.GetName(_planner.route.SourceID);
else srcname = AIIndustry.GetName(_planner.route.SourceID);
if (_planner.route.DestIsTown) dstname = AITown.GetName(_planner.route.DestID);
else dstname = AIIndustry.GetName(_planner.route.DestID);
if (_planner.route.IsSubsidy) subsidy = " [going for subsidy]";
AILog.Info(AICargo.GetCargoLabel(_planner.route.Cargo) + " from " + srcname + " to " + dstname + subsidy);
/* Choose wagon and train engine. */
local wagon = WormRailBuilder.ChooseWagon(_planner.route.Cargo, engine_blacklist);
if (wagon == null) {
AILog.Warning("No suitable wagon available!");
return false;
} else {
AILog.Info("Chosen wagon: " + AIEngine.GetName(wagon));
}
if (!_planner.route.double) AILog.Info("Using single rail");
else AILog.Info("Using double rail");
/* Determine the size of the train station. */
local platform_length = null;
/// @todo replace number by a definied constant or variable depending on date and other factors
if (_planner.route.double || _planner.route.distance_manhattan > 50) platform_length = 3;
else platform_length = 2;
local cur_money = AICompany.GetBankBalance(AICompany.COMPANY_SELF);
if (AICompany.GetLoanAmount() == 0) {
if (cur_money > WormMoney.InflationCorrection(50000))
platform_length++;
if (cur_money > WormMoney.InflationCorrection(500000))
platform_length++;
if (cur_money > WormMoney.InflationCorrection(2500000))
platform_length++;
if (cur_money > WormMoney.InflationCorrection(5000000))
platform_length++;
// if (cur_money > WormMoney.InflationCorrection(10000000))
// platform_length++;
}
else {
if (cur_money > WormMoney.InflationCorrection(250000))
platform_length = 4;
}
local max_train_length = AIGameSettings.GetValue("max_train_length");
local station_spread = AIGameSettings.GetValue("station_spread");
// Don't make platforms longer than max allowed train length
if (platform_length > max_train_length)
platform_length = max_train_length;
// We need to check station spread too since it seems you can set it lower than max_train_length
if (platform_length > station_spread)
platform_length = station_spread;
// AILog.Warning("[DEBUG] Selected platform length: " + platform_length);
// Don't make the trains too long when starting. Lengthen them later when there's more cargo waiting.
local starting_train_size = platform_length;
if (starting_train_size > 3) starting_train_size = 3;
/* Check if there is a suitable engine available. */
local engine = WormRailBuilder.ChooseTrainEngine(_planner.route.Cargo,
_planner.route.distance_manhattan, wagon, platform_length * 2 - 1, engine_blacklist);
if (engine == null) {
AILog.Warning("No suitable train engine available!");
return false;
} else {
AILog.Info("Chosen train engine: " + AIEngine.GetName(engine));
}
local trains = null; // Number of trains to add to this route
local station_data = WormStation();
if (!_planner.route.double) {
/* Single rail */
trains = 1;
local start, end = null;
// Build the source station
if (WormRailBuilder.BuildRailStation(true, trains, platform_length, _planner.route, station_data, this)) {
end = [station_data.frontfront, station_data.stafront];
buildingstage = BS_BUILDING;
AILog.Info("New station successfully built: " + AIStation.GetName(station_data.stasrc));
} else {
AILog.Warning("Could not build source station at " + srcname);
return false;
}
// Build the destination station
if (WormRailBuilder.BuildRailStation(false, trains, platform_length, _planner.route, station_data, this)) {
start = [station_data.frontfront, station_data.stafront];
AILog.Info("New station successfully built: " + AIStation.GetName(station_data.stadst));
} else {
AILog.Warning("Could not build destination station at " + dstname);
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
buildingstage = BS_NOTHING;
return false;
}
// Build the rail
if (WormRailBuilder.BuildRail(start, end, railbridges)) {
AILog.Info("Rail built successfully!");
} else {
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
WormRailBuilder.DeleteRailStation(station_data.stadst, this);
buildingstage = BS_NOTHING;
return false;
}
} else {
/* Double rail */
/// @todo Add support for more or less passing lanes depending on route size.
trains = 2;
local start, end = null;
local temp_ps = null;
// Passing lane starting/ending points. SimpleAI has them as class variables but they seem
// to be only used here.
local ps1_entry = null;
local ps1_exit = null;
local ps2_entry = null;
local ps2_exit = null;
// Build the source station
if (WormRailBuilder.BuildRailStation(true, trains, platform_length, _planner.route, station_data, this)) {
end = [station_data.morefront, station_data.frontfront];
buildingstage = BS_BUILDING;
AILog.Info("New station successfully built: " + AIStation.GetName(station_data.stasrc));
} else {
AILog.Warning("Could not build source station at " + srcname);
return false;
}
// Build the destination station
if (WormRailBuilder.BuildRailStation(false, trains, platform_length, _planner.route, station_data, this)) {
start = [station_data.morefront, station_data.frontfront];
AILog.Info("New station successfully built: " + AIStation.GetName(station_data.stadst));
} else {
AILog.Warning("Could not build destination station at " + dstname);
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
buildingstage = BS_NOTHING;
return false;
}
// Build the first passing lane section
temp_ps = WormRailBuilder.BuildPassingLaneSection(true, platform_length, station_data, this);
if (temp_ps == null) {
AILog.Warning("Could not build first passing lane section");
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
WormRailBuilder.DeleteRailStation(station_data.stadst, this);
buildingstage = BS_NOTHING;
return false;
} else {
AILog.Info("First passing lane built successfully!");
if (AIMap.DistanceManhattan(end[0], temp_ps[0][0]) < AIMap.DistanceManhattan(end[0], temp_ps[1][0])) {
ps1_entry = [temp_ps[0][0], temp_ps[0][1]];
ps1_exit = [temp_ps[1][0], temp_ps[1][1]];
} else {
ps1_entry = [temp_ps[1][0], temp_ps[1][1]];
ps1_exit = [temp_ps[0][0], temp_ps[0][1]];
}
}
// Build the second passing lane section
temp_ps = WormRailBuilder.BuildPassingLaneSection(false, platform_length, station_data, this);
if (temp_ps == null) {
AILog.Warning("Could not build second passing lane section");
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
WormRailBuilder.DeleteRailStation(station_data.stadst, this);
WormRailBuilder.RemoveRailLine(ps1_entry[1], this);
buildingstage = BS_NOTHING;
return false;
} else {
AILog.Info("Second passing lane built successfully!");
if (AIMap.DistanceManhattan(start[0], temp_ps[0][0]) < AIMap.DistanceManhattan(start[0], temp_ps[1][0])) {
ps2_entry = [temp_ps[1][0], temp_ps[1][1]];
ps2_exit = [temp_ps[0][0], temp_ps[0][1]];
} else {
ps2_entry = [temp_ps[0][0], temp_ps[0][1]];
ps2_exit = [temp_ps[1][0], temp_ps[1][1]];
}
}
// Build the rail between the source station and the first passing lane section
if (WormRailBuilder.BuildRail(ps1_entry, end, railbridges)) {
AILog.Info("Rail between source and first passing lane built successfully!");
} else {
WormRailBuilder.DeleteRailStation(station_data.stadst, this);
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
WormRailBuilder.RemoveRailLine(ps1_entry[1], this);
WormRailBuilder.RemoveRailLine(ps2_entry[1], this);
buildingstage = BS_NOTHING;
return false;
}
// Build the rail between the two passing lane sections
if (WormRailBuilder.BuildRail(ps2_entry, ps1_exit, railbridges)) {
AILog.Info("Rail between both passing lanes built successfully!");
} else {
WormRailBuilder.DeleteRailStation(station_data.stadst, this);
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
WormRailBuilder.RemoveRailLine(ps2_entry[1], this);
buildingstage = BS_NOTHING;
return false;
}
// Build the rail between the second passing lane section and the destination station
if (WormRailBuilder.BuildRail(start, ps2_exit, railbridges)) {
AILog.Info("Rail between second passing lane and destination built successfully!");
} else {
WormRailBuilder.DeleteRailStation(station_data.stadst, this);
WormRailBuilder.DeleteRailStation(station_data.stasrc, this);
buildingstage = BS_NOTHING;
return false;
}
}
buildingstage = BS_NOTHING;
// For now always rail here
local vehtype = AIVehicle.VT_RAIL;
/* Set up a group for this rail route. */
local group = AIGroup.CreateGroup(vehtype);
this.SetGroupName(group, _planner.route.Cargo, station_data.stasrc);
local build_result = ALL_OK;
// If destination is industry check if our destination station still gets cargo.
if (!_planner.route.DestIsTown &&
!WormRailManager.IsCargoAcceptedByStation(station_data.stadst, _planner.route.Cargo)) {
build_result = ERROR_INDUSTRY_DISAPPEARED;
AILog.Warning("New route marked for deletion because the accepting industry disappeared before we could use it!");
} else {
/* Create trains for this route. */
build_result = WormRailBuilder.BuildAndStartTrains(trains, 2 * starting_train_size - 2, engine, wagon, null, group,
_planner.route.Cargo, station_data, engine_blacklist);
}
/* To make sure the route gets deleted it needs to first be registered even in case of ERROR_INDUSTRY_DISAPPEARED! */
last_route = WormRailManager.RegisterRoute(_planner.route, station_data, vehtype, group, starting_train_size*16);
// Retry if route was abandoned due to blacklisting
local vehicles = AIVehicleList_Group(group);
if (vehicles.Count() == 0 && vehtype == AIVehicle.VT_RAIL) {
if (build_result == ERROR_BUILD_TRAIN_BLACKLISTED) {
AILog.Info("The new route may be empty because of blacklisting, retrying...")
// Choose wagon and locomotive
local wagon = WormRailBuilder.ChooseWagon(_planner.route.Cargo, engine_blacklist);
if (wagon == null) {
AILog.Warning("No suitable wagon available!");
return false;
} else {
AILog.Info("Chosen wagon: " + AIEngine.GetName(wagon));
}
local engine = WormRailBuilder.ChooseTrainEngine(_planner.route.Cargo, _planner.route.distance_manhattan,
wagon, platform_length * 2 - 1, engine_blacklist);
if (engine == null) {
AILog.Warning("No suitable engine available!");
return false;
} else {
AILog.Info("Chosen engine: " + AIEngine.GetName(engine));
}
/* Wormnest: it seems easier to call BuildAndStartTrains again...
local manager = cManager(root);
manager.AddVehicle(last_route, null, engine, wagon);
if (_planner.route.double) manager.AddVehicle(last_route, null, engine, wagon);
*/
/// @todo check first if we are below max no. of trains...
AILog.Info("Trying again to build trains for this route.");
build_result = WormRailBuilder.BuildAndStartTrains(trains, 2 * starting_train_size - 2, engine, wagon, null, group,
_planner.route.Cargo, station_data, engine_blacklist);
}
if (build_result == ERROR_NOT_ENOUGH_MONEY) {
/* We will try to add trains when we have money. */
AILog.Warning("We built a new route but couldn't add trains yet due to lack of money.");
route_without_trains = group;
return true;
}
else if (build_result != ALL_OK)
return false;
}
route_without_trains = -1;
AILog.Info("New route done!");
station_data = null;
return true;
}
function WormRailManager::RegisterRoute(route_data, station_data, vehtype, group, train_length)
{
/* Table with info about a completed route. */
local route = {
src = null ///< ID of source town or industry
dst = null ///< ID of destination town or industry
stasrc = null ///< Station ID of source station
stadst = null ///< Station ID of destination station
homedepot = null ///< The depot at the source station
group = null ///< The group ID of this route
crg = null ///< The cargo selected to be transported
vehtype = null ///< The vehicle type selected for this route
railtype = null ///< The rail type used for the rail tracks on this route
maxvehicles = null ///< The maximum number of vehicles this route can handle currently
town2town = null ///< Whether this route is a town to town route or not.
}
/// @todo Also save IsSubsidy, ...
route.src = route_data.SourceID;
route.dst = route_data.DestID;
route.stasrc = station_data.stasrc;
route.stadst = station_data.stadst;
route.homedepot = station_data.homedepot;
route.group = group;
route.crg = route_data.Cargo;
route.vehtype = vehtype;
route.railtype = AIRail.GetCurrentRailType();
switch (vehtype) {
/* This part was taken from SimpleAI and is disabled for now...
case AIVehicle.VT_ROAD:
route.maxvehicles = AIController.GetSetting("max_roadvehs");
break;
*/
case AIVehicle.VT_RAIL:
// @todo: This should be adapted for longer routes but for that we also need more passing lanes!
route.maxvehicles = route_data.double ? (route_data.distance_manhattan > 150 ? 5 :
(route_data.distance_manhattan > 100 ? 4 : 3)) : 1;
break;
case AIVehicle.VT_AIR:
route.maxvehicles = 0;
break;
}
route.town2town = (route_data.SourceIsTown && route_data.DestIsTown);
routes.push(route);
serviced.AddItem(route_data.SourceID * 256 + route_data.Cargo, 0);
groups.AddItem(group, routes.len() - 1);
_optimal_train_lengths.AddItem(group, train_length);
lastroute = AIDate.GetCurrentDate();
return route;
}
function WormRailManager::IsCargoProducedByStation(stationID, cargoID)
{
/* For now simply check if it has a cargo rating. */
/* But only for stations at least 1 year old since it can take a while to get a cargo rating. */
if (AIBaseStation.GetConstructionDate(stationID) + 365 > AIDate.GetCurrentDate() ||
AIStation.HasCargoRating(stationID, cargoID))
return true;
return false;
}
function WormRailManager::IsCargoAcceptedByStation(stationID, cargoID)
{
local cargolist = AICargoList_StationAccepting(stationID);
foreach (cargo, dummy in cargolist) {
if (cargo == cargoID)
return true;
}
return false;
}
function WormRailManager::SellAllTrains(route)
{
local trainlist = AIVehicleList_Station (route.stasrc);
foreach (train, dummy in trainlist) {
WormRailManager.SendTrainToDepotForSelling(train);
}
}
function WormRailManager::CheckRoutes()
{
/* Since routes are used in a loop as index we can not remove them inside this loop.
* Thus we store routes to be removed in a temp array. */
// Now disabled since the order of routes shouldn't change see bottom of this function.
// local remove_routes = [];
foreach (idx, route in routes) {
// Skip deleted routes
if (route.vehtype == null) continue;
AILog.Info(".... checking route " + idx + ", " + AIStation.GetName(route.stasrc) + " - " + AIStation.GetName(route.stadst));
local vehicles = AIVehicleList_Group(route.group);
/* Empty route */
/// @todo if vehicle count = 0 because we didn't have enough money to buy trains then
/// @todo we should (try to) add trains or wait until we have more money
/// @todo Maybe add a status to routes like [nomoneyfortrains, unprofitable, ...]
if (vehicles.Count() == 0) {
/// Only remove route if we're not waiting for trains to be added
if (route_without_trains != route.group) {
AILog.Info("Removing empty route: " + AIStation.GetName(route.stasrc) + " - " + AIStation.GetName(route.stadst));
route.vehtype = null;
_optimal_train_lengths.RemoveItem(route.group);
groups.RemoveItem(route.group);
AIGroup.DeleteGroup(route.group);
serviced.RemoveItem(route.src * 256 + route.crg);
// Connected rails will automatically be removed
WormRailBuilder.DeleteRailStation(route.stasrc, this);
WormRailBuilder.DeleteRailStation(route.stadst, this);
/* route index that should be removed after finishing the foreach loop */
// remove_routes.append(idx);
}
continue;
}
/* Electrifying rails */
if ((AIRail.TrainHasPowerOnRail(route.railtype, AIRail.GetCurrentRailType())) && (route.railtype != AIRail.GetCurrentRailType())) {
// Check if we can afford it
if (WormMoney.GetMaxBankBalance() > WormMoney.InflationCorrection(30000)) {
if (AICompany.GetBankBalance(AICompany.COMPANY_SELF) < WormMoney.InflationCorrection(30000)) {
WormMoney.GetMoney(WormMoney.InflationCorrection(30000), WormMoney.WM_SILENT);
}
AILog.Info("Electrifying rail line: " + AIStation.GetName(route.stasrc) + " - " + AIStation.GetName(route.stadst));
route.railtype = AIRail.GetCurrentRailType();
WormRailBuilder.ElectrifyRail(AIStation.GetLocation(route.stasrc), this);
}
}
/* Adding trains */
CheckAddTrainToGroup(route, vehicles.Count(), vehicles.Begin());
/** see above replacement
if (vehicles.Count() == 1 && route.maxvehicles == 2 && (!AIVehicle.IsStoppedInDepot(vehicles.Begin()))) {
if (AIVehicle.GetProfitThisYear(vehicles.Begin()) <= 0) continue;
if (AIStation.GetCargoWaiting(route.stasrc, route.crg) > 150) {
local railtype = AIRail.GetCurrentRailType();
AIRail.SetCurrentRailType(route.railtype);
local wagon = WormRailBuilder.ChooseWagon(route.crg, engine_blacklist);
if (wagon == null) {
AIRail.SetCurrentRailType(railtype);
return false;
}
local platform = WormRailBuilder.GetRailStationPlatformLength(route.stasrc);
local engine = WormRailBuilder.ChooseTrainEngine(route.crg, AIMap.DistanceManhattan(AIStation.GetLocation(route.stasrc),
AIStation.GetLocation(route.stadst)), wagon, platform * 2 - 1, engine_blacklist);
if (engine == null) {
AIRail.SetCurrentRailType(railtype);
return false;
}
// Check if we can afford it
if (WormMoney.GetMaxBankBalance() > (WormMoney.GetMinimumCashNeeded() + AIEngine.GetPrice(engine) +
4 * AIEngine.GetPrice(wagon))) {
if (WormRailManager.AddVehicle(route, vehicles.Begin(), engine, wagon)) {
AILog.Info("Added train to route: " + AIStation.GetName(route.stasrc) + " - " + AIStation.GetName(route.stadst));
}
}
AIRail.SetCurrentRailType(railtype);
}
}
*/
/* Replacing old vehicles */
vehicles.Valuate(AIVehicle.GetAgeLeft);
vehicles.KeepBelowValue(0);
foreach (vehicle, dummy in vehicles) {
if (todepotlist.HasItem(vehicle)) continue;
local railtype = AIRail.GetCurrentRailType();
// Choose a new model
AIRail.SetCurrentRailType(route.railtype);
local wagon = WormRailBuilder.ChooseWagon(route.crg, engine_blacklist);
if (wagon == null) continue;
/// @todo Don't use platform length but compute optimal length for this group!
local platform = WormRailBuilder.GetRailStationPlatformLength(route.stasrc);
local engine = WormRailBuilder.ChooseTrainEngine(route.crg, AIMap.DistanceManhattan(AIStation.GetLocation(route.stasrc),
AIStation.GetLocation(route.stadst)), wagon, platform * 2 - 1, engine_blacklist);
AIRail.SetCurrentRailType(railtype);
if (engine == null) continue;
// Replace it only if we can afford it
if (WormMoney.GetMaxBankBalance() > (WormMoney.GetMinimumCashNeeded() + AIEngine.GetPrice(engine) +
5 * AIEngine.GetPrice(wagon))) {
AILog.Info(AIVehicle.GetName(vehicle) + " is getting old, sending it to the depot...");
/* Make sure it's not stopped in depot yet for some reason. */
if (!(AIVehicle.GetState(vehicle) == AIVehicle.VS_IN_DEPOT)) {
if (!AIVehicle.SendVehicleToDepot(vehicle)) {
// Maybe the train only needs to be reversed to find a depot
AIVehicle.ReverseVehicle(vehicle);
AIController.Sleep(75);
if (!AIVehicle.SendVehicleToDepot(vehicle)) break;
}
}
todepotlist.AddItem(vehicle, TD_REPLACE);
}
}
/* Lengthening short trains */
local optimal_length = GetOptimalTrainLength(route);
_optimal_train_lengths.SetValue(route.group, optimal_length);
// AILog.Warning("[DEBUG] Optimal train length = " + optimal_length + ", in half tiles = "
// + (optimal_length/8) + ", in tiles = " + (optimal_length/16));
local train_len_limit = optimal_length -7;
vehicles = AIVehicleList_Group(route.group);
local platform = WormRailBuilder.GetRailStationPlatformLength(route.stasrc);
// local train_len_limit = platform * 16 -7; // Move computation out of the loop.
local cargo_waiting = AIStation.GetCargoWaiting(route.stasrc, route.crg);
// Only lengthen if there is a reasonable amount of cargo waiting
if (cargo_waiting > 150) {
foreach (train, dummy in vehicles) {
if (todepotlist.HasItem(train)) continue;
// Don't lengthen train that doesn't make a profit
if (AIVehicle.GetProfitThisYear(train) <= 0) continue;
// The train should fill its platform
if (AIVehicle.GetLength(train) < train_len_limit) {
local railtype = AIRail.GetCurrentRailType();
AIRail.SetCurrentRailType(route.railtype);
local wagon = WormRailBuilder.ChooseWagon(route.crg, engine_blacklist);
if (wagon == null) break;
// Check if we can afford it
if (WormMoney.GetMaxBankBalance() > (WormMoney.GetMinimumCashNeeded() + 5 * AIEngine.GetPrice(wagon))) {
AILog.Info(AIVehicle.GetName(train) + " is short, sending it to the depot to attach more wagons...");
if (!AIVehicle.SendVehicleToDepot(train)) {
AIVehicle.ReverseVehicle(train);
AIController.Sleep(75);
if (!AIVehicle.SendVehicleToDepot(train)) break;
}
todepotlist.AddItem(train, TD_ATTACH_WAGONS);
}
AIRail.SetCurrentRailType(railtype);
}
}
}
/* Checking vehicles in depot */
/// @todo maybe do this for all trains in one go instead of per group probably more efficient
//AILog.Info("[DEBUG] Check vehicles in depot...");
vehicles = AIVehicleList_Group(route.group);
vehicles.Valuate(AIVehicle.IsStoppedInDepot);
vehicles.KeepValue(1);
//if (vehicles.Count() > 0)
// AILog.Info("[DEBUG] There are " + vehicles.Count() + " vehicles in depot.");
foreach (vehicle, dummy in vehicles) {
// A vehicle has probably been sitting there for ages if its current year/last year profits are both 0, and it's at least 2 months old
/*
if (AIVehicle.GetProfitThisYear(vehicle) != 0 || AIVehicle.GetProfitLastYear(vehicle) != 0 || AIVehicle.GetAge(vehicle) < 60) continue;
if (todepotlist.HasItem(vehicle)) {
todepotlist.RemoveItem(vehicle);
AIVehicle.StartStopVehicle(vehicle);
} else {
// Sell it if we have no idea how it got there
AILog.Warning("Sold " + AIVehicle.GetName(vehicle) + ", as it has been sitting in the depot for ages.");
AIVehicle.SellWagonChain(vehicle, 0);
}
*/
/* We handle it different than SimpleAI for now: */
HandleVehicleInDepot(vehicle);
}
if (route_without_trains != route.group) {
vehicles = AIVehicleList_Group(route.group);
if (vehicles.Count() > 0) {
/* Check for routes where the cargo isn't accepted at destination (industry removed). */
if (!WormRailManager.IsCargoAcceptedByStation(route.stadst, route.crg)) {
AILog.Warning("Cargo " + AICargo.GetCargoLabel(route.crg) + " is not accepted anymore by station " +
AIStation.GetName(route.stadst) + ". Selling all vehicles.");
WormRailManager.SellAllTrains(route);
}
if (!route.town2town) {
/* Check for routes where the cargo isn't produced near destination (industry removed). */
if (!WormRailManager.IsCargoProducedByStation(route.stasrc, route.crg)) {
AILog.Warning("Cargo " + AICargo.GetCargoLabel(route.crg) + " is not produced anymore near station " +
AIStation.GetName(route.stasrc) + ". Selling all vehicles.");
WormRailManager.SellAllTrains(route);
}
} else {
if (!WormRailManager.IsCargoAcceptedByStation(route.stasrc, route.crg)) {
AILog.Warning("Cargo " + AICargo.GetCargoLabel(route.crg) + " is not accepted anymore by station " +
AIStation.GetName(route.stasrc) + ". Selling all vehicles.");
WormRailManager.SellAllTrains(route);
}
}
}
}
}
/* Were there any routes removed? */
/* WARNING: Since groups stores an index into routes for each group we can't go changing this
* by removing unused routes.
* @todo FIGURE out if there is another solution to this.
*/
// if (remove_routes.len() > 0) {
// foreach (removed_count, route_idx in remove_routes) {
// /* Remove unused routes from our array. */
// /* Because removing items changes the indexes we need to take that into account too. */
// /* This assumes that the lowest numbered array indexes will be removed first. */
// AILog.Info("DEBUG: Removed route " + route_idx + " (orignal idx), " + (route_idx-removed_count) +
// " (computed idx)");
// routes.remove(route_idx-removed_count);
// }
// }
// Check ugrouped vehicles as well. There should be none after all...
WormRailManager.CheckDefaultGroup();
}
/**
* Get the optimal train length for this route.
* @param route The route info.
* @return The optimal length in 1/16 of a tile.
*/
function WormRailManager::GetOptimalTrainLength(route)
{
// Determine absolute maximum train length in tiles
local platform = WormRailBuilder.GetRailStationPlatformLength(route.stasrc);
local vehicles = AIVehicleList_Group(route.group);
local min_max_len = GetMinMaxTrainLength(vehicles);
// AILog.Warning("[DEBUG] min/max length " + min_max_len.min_length + " / " + min_max_len.max_length +
// ", platform = " + (platform*16));
// If minimum length of a train is already the platform length we don't need to look further.
// Since standard train wagons and engines are half a tile long we measure in eigth parts
local min_len8 = (min_max_len.min_length+7)/8;
local max_len8 = (min_max_len.max_length+7)/8;
local platform8 = platform*2;
if (min_len8 == platform8)
return platform*16;
// If trains are not all equal in length (counted in half tiles) then we first want them all the same size.
if (min_len8 < max_len8)
return max_len8*8;
/// @todo We should get the amount of cargo our average wagon can carry. Assume 25 for now.
local veh_count = vehicles.Count();
// Safety check
if (veh_count == 0) {
AILog.Warning("No vehicles in this group!");
return 3*16; // Default = 3 tiles
}
local cargo_waiting = AIStation.GetCargoWaiting(route.stasrc, route.crg);
local per_vehicle8 = cargo_waiting / 25 / veh_count;
// Since we want to increase size gradually don't increase with more than 1 whole tile at a time.
if (per_vehicle8 > 2)
per_vehicle8 = 2;
// If total is more than platform length return platform length
local optimal_length = max_len8+per_vehicle8;
if (optimal_length > platform8)
optimal_length = platform8;
return optimal_length*8;
}
function WormRailManager::GetMinMaxTrainLength(vehicles)
{
local result = {
min_length = 1024, // 64 (max station spread) * 16
max_length = 0
}
// Since our groups usually have only a small amount of vehicles iterating the list seems best.
foreach (vehicle, dummy in vehicles) {
local length = AIVehicle.GetLength(vehicle);
if (length < result.min_length) result.min_length = length;
if (length > result.max_length) result.max_length = length;
}
return result;
}
function WormRailManager::CheckDefaultGroup()
{
local vehicles = AIVehicleList_DefaultGroup(AIVehicle.VT_RAIL);
vehicles.Valuate(AIVehicle.IsStoppedInDepot);
vehicles.KeepValue(1);
foreach (vehicle, dummy in vehicles) {
// Check for vehicles sitting in the depot.
if (AIVehicle.GetProfitThisYear(vehicle) != 0 || AIVehicle.GetProfitLastYear(vehicle) != 0 || AIVehicle.GetAge(vehicle) < 60) continue;
if (todepotlist.HasItem(vehicle)) {
todepotlist.RemoveItem(vehicle);
AIVehicle.StartStopVehicle(vehicle);
} else {
AILog.Warning("Sold " + AIVehicle.GetName(vehicle) + ", as it has been sitting in the depot for ages.");
AIVehicle.SellWagonChain(vehicle, 0);
}
}
}
function WormRailManager::CheckAddTrainToGroup(route, vehicle_count, first_vehicle)
{
if (vehicle_count == 1 && AIVehicle.IsStoppedInDepot(first_vehicle))
return false;
if (vehicle_count < route.maxvehicles) {
/// @todo remove profit check? What if first vehicle is an older type and the others are making a profit?
// However this is used to not add a second vehicle in the first year a route was added?
if (AIVehicle.GetProfitThisYear(first_vehicle) <= 0) return false;
if (AIStation.GetCargoWaiting(route.stasrc, route.crg) > 150) {
// We may want to increase train sizes first before adding a train