/
TrainBuilder.java
5224 lines (5038 loc) · 290 KB
/
TrainBuilder.java
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
package jmri.jmrit.operations.trains;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import jmri.InstanceManager;
import jmri.Version;
import jmri.jmrit.operations.locations.Location;
import jmri.jmrit.operations.locations.LocationManager;
import jmri.jmrit.operations.locations.Track;
import jmri.jmrit.operations.locations.schedules.ScheduleItem;
import jmri.jmrit.operations.rollingstock.RollingStock;
import jmri.jmrit.operations.rollingstock.cars.Car;
import jmri.jmrit.operations.rollingstock.cars.CarLoad;
import jmri.jmrit.operations.rollingstock.cars.CarLoads;
import jmri.jmrit.operations.rollingstock.cars.CarManager;
import jmri.jmrit.operations.rollingstock.cars.CarRoads;
import jmri.jmrit.operations.rollingstock.engines.Engine;
import jmri.jmrit.operations.rollingstock.engines.EngineManager;
import jmri.jmrit.operations.router.Router;
import jmri.jmrit.operations.routes.Route;
import jmri.jmrit.operations.routes.RouteLocation;
import jmri.jmrit.operations.setup.Control;
import jmri.jmrit.operations.setup.Setup;
import jmri.jmrit.operations.trains.timetable.TrainSchedule;
import jmri.jmrit.operations.trains.timetable.TrainScheduleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Builds a train and creates the train's manifest.
*
* @author Daniel Boudreau Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013,
* 2014, 2015
*/
public class TrainBuilder extends TrainCommon {
// report levels
protected static final String ONE = Setup.BUILD_REPORT_MINIMAL;
protected static final String THREE = Setup.BUILD_REPORT_NORMAL;
protected static final String FIVE = Setup.BUILD_REPORT_DETAILED;
protected static final String SEVEN = Setup.BUILD_REPORT_VERY_DETAILED;
private static final int DISPLAY_CAR_LIMIT_20 = 20; // build exception out of staging
private static final int DISPLAY_CAR_LIMIT_50 = 50;
private static final int DISPLAY_CAR_LIMIT_100 = 100;
// build variables shared between local routines
Date _startTime; // when the build report started
Train _train; // the train being built
int _numberCars = 0; // number of cars moved by this train
int _reqNumEngines = 0; // the number of engines required for this train
List<Engine> _engineList; // list of engines available for this train
Engine _leadEngine; // last lead engine found from getEngine
Engine _secondLeadEngine; // the lead engine in the second half of the train's route
Engine _thirdLeadEngine; // the lead engine in the third part of the train's route
int _carIndex; // index for carList
List<Car> _carList; // list of cars available for this train
List<RouteLocation> _routeList; // list of locations from departure to termination served by this train
Hashtable<String, Integer> _numOfBlocks; // Number of blocks of cars departing staging.
int _completedMoves; // the number of pick up car moves for a location
int _reqNumOfMoves; // the requested number of car moves for a location
Location _departLocation; // train departs this location
Track _departStageTrack; // departure staging track (null if not staging)
Location _terminateLocation; // train terminate at this location
Track _terminateStageTrack; // terminate staging track (null if not staging)
boolean _success; // true when enough cars have been picked up from a location
PrintWriter _buildReport; // build report for this train
List<Car> _notRoutable = new ArrayList<>(); // list of cars that couldn't be routed
List<Location> _modifiedLocations = new ArrayList<>(); // list of locations that have been modified
// managers
CarManager carManager = InstanceManager.getDefault(CarManager.class);
LocationManager locationManager = InstanceManager.getDefault(LocationManager.class);
EngineManager engineManager = InstanceManager.getDefault(EngineManager.class);
/**
* Build rules:
* <ol>
* <li>Need at least one location in route to build train
* <li>Select only locos and cars the that train can service
* <li>Optional TODO, train must depart with the required number of moves
* (cars)
* <li>If required, add caboose or car with FRED to train
* <li>When departing staging find a track matching train requirements
* <li>All cars and locos on one track must leave staging
* <li>Service locations based on train direction, location car types and
* roads
* <li>Ignore track direction when train is a local (serves one location)
* </ol>
*
* @param train the train that is to be built
* @return True if successful.
*
*/
public boolean build(Train train) {
this._train = train;
try {
build();
return true;
} catch (BuildFailedException e) {
buildFailed(e);
return false;
}
}
private void build() throws BuildFailedException {
_startTime = new Date();
log.debug("Building train ({})", _train.getName());
_train.setStatusCode(Train.CODE_BUILDING);
_train.setBuilt(false);
_train.setLeadEngine(null);
// backup the train's previous build report file
InstanceManager.getDefault(TrainManagerXml.class).savePreviousBuildStatusFile(_train.getName());
// create build report file
File file = InstanceManager.getDefault(TrainManagerXml.class).createTrainBuildReportFile(_train.getName());
try {
_buildReport = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
"UTF-8")), true); // NOI18N
} catch (IOException e) {
log.error("Can not open build report file: " + file.getName());
return;
}
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("BuildReportMsg"), new Object[]{
_train.getName(), _startTime}));
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("BuildReportVersion"), new Object[]{Version
.name()}));
// show the various build detail levels
addLine(_buildReport, THREE, Bundle.getMessage("buildReportLevelThree"));
addLine(_buildReport, FIVE, Bundle.getMessage("buildReportLevelFive"));
addLine(_buildReport, SEVEN, Bundle.getMessage("buildReportLevelSeven"));
if (Setup.getRouterBuildReportLevel().equals(Setup.BUILD_REPORT_DETAILED)) {
addLine(_buildReport, SEVEN, Bundle.getMessage("buildRouterReportLevelDetailed"));
} else if (Setup.getRouterBuildReportLevel().equals(Setup.BUILD_REPORT_VERY_DETAILED)) {
addLine(_buildReport, SEVEN, Bundle.getMessage("buildRouterReportLevelVeryDetailed"));
}
if (!Setup.getComment().trim().isEmpty()) {
addLine(_buildReport, ONE, BLANK_LINE);
addLine(_buildReport, ONE, Setup.getComment());
}
addLine(_buildReport, ONE, BLANK_LINE);
if (_train.getRoute() == null) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorRoute"),
new Object[]{_train.getName()}));
}
// get the train's route
_routeList = _train.getRoute().getLocationsBySequenceList();
if (_routeList.size() < 1) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorNeedRoute"),
new Object[]{_train.getName()}));
}
// train departs
_departLocation = locationManager.getLocationByName(_train.getTrainDepartsName());
if (_departLocation == null) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorNeedDepLoc"),
new Object[]{_train.getName()}));
}
// train terminates
_terminateLocation = locationManager.getLocationByName(_train.getTrainTerminatesName());
if (_terminateLocation == null) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorNeedTermLoc"),
new Object[]{_train.getName()}));
}
showTrainBuildOptions(); // show the build options for this train
showAndInitializeTrainRoute(); // show the train's route and initialize it
// is this train a switcher?
if (_train.isLocalSwitcher()) {
addLine(_buildReport, THREE, BLANK_LINE); // add line when in detailed report mode
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrainIsSwitcher"), new Object[]{
_train.getName(), TrainCommon.splitString(_train.getTrainDepartsName())}));
}
// set the engine requirements for this train
if (_train.getNumberEngines().equals(Train.AUTO)) {
_reqNumEngines = getAutoEngines();
} else if (_train.getNumberEngines().equals(Train.AUTO_HPT)) {
_reqNumEngines = 1; // get one loco for now, check HP requirements after train is built
} else {
_reqNumEngines = Integer.parseInt(_train.getNumberEngines());
}
showTrainRequirements();
// show road names that this train will service
if (!_train.getRoadOption().equals(Train.ALL_ROADS)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainRoads"), new Object[]{
_train.getName(), _train.getRoadOption(), formatStringToCommaSeparated(_train.getRoadNames())}));
}
// show owner names that this train will service
if (!_train.getOwnerOption().equals(Train.ALL_OWNERS)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainOwners"), new Object[]{
_train.getName(), _train.getOwnerOption(), formatStringToCommaSeparated(_train.getOwnerNames())}));
}
// show built dates serviced
if (!_train.getBuiltStartYear().equals(Train.NONE)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainBuiltAfter"), new Object[]{
_train.getName(), _train.getBuiltStartYear()}));
}
if (!_train.getBuiltEndYear().equals(Train.NONE)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainBuiltBefore"), new Object[]{
_train.getName(), _train.getBuiltEndYear()}));
}
// show engine types that this train will service
if (_reqNumEngines > 0) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainServicesEngineTypes"),
new Object[]{_train.getName()}));
addLine(_buildReport, FIVE, formatStringToCommaSeparated(_train.getLocoTypeNames()));
}
determineIfTrainTerminatesIntoStaging(); // determine if train is terminating into staging
determineIfTrainDepartsStagingAndLoadEngines(); // find departure track if staging, load engines and cabooses
// show car types and loads that this train will service
addLine(_buildReport, FIVE, BLANK_LINE); // add line when in detailed report mode
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainServicesCarTypes"),
new Object[]{_train.getName()}));
addLine(_buildReport, FIVE, formatStringToCommaSeparated(_train.getCarTypeNames()));
// show load names that this train will service
if (!_train.getLoadOption().equals(Train.ALL_LOADS)) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainLoads"), new Object[]{
_train.getName(), _train.getLoadOption(), formatStringToCommaSeparated(_train.getLoadNames())}));
}
// get list of cars for this route
_carList = carManager.getAvailableTrainList(_train);
// TODO: DAB this needs to be controlled by each train
if (_train.getNumberCarsRequested() > _carList.size() && Control.fullTrainOnly) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorNumReq"), new Object[]{
Integer.toString(_train.getNumberCarsRequested()), _train.getName(), Integer.toString(_carList.size())}));
}
// remove unwanted cars and list available cars by location
removeAndListCars();
addCabooseOrFredToTrain(); // do all caboose and FRED changes in the train's route
// done assigning cabooses and cars with FRED, remove the rest
removeCaboosesAndCarsWithFred();
saveCarFinalDestinations(); //save car's final destination and schedule id in case of train reset
blockCarsFromStaging(); // block cars from staging
// now find destinations for cars
addLine(_buildReport, THREE, BLANK_LINE); // add line when in normal report mode
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrain"), new Object[]{_train.getNumberCarsRequested(),
_train.getName(), _carList.size()}));
if (Setup.isBuildAggressive() && !_train.isBuildTrainNormalEnabled()) {
// perform a multiple pass build for this train, default is two passes
int passes = 0;
boolean firstPass = true;
while (passes++ < Setup.getNumberPasses()) {
placeCars(100 * passes / Setup.getNumberPasses(), firstPass);
firstPass = false;
}
} else {
placeCars(100, false);
}
// done assigning cars to train
_train.setCurrentLocation(_train.getTrainDepartsRouteLocation());
if (_numberCars < _train.getNumberCarsRequested()) {
_train.setStatusCode(Train.CODE_PARTIAL_BUILT);
addLine(_buildReport, ONE,
Train.PARTIAL_BUILT +
" " +
_train.getNumberCarsWorked() +
"/" +
_train.getNumberCarsRequested() +
" " +
Bundle.getMessage("cars"));
} else {
_train.setStatusCode(Train.CODE_BUILT);
addLine(_buildReport, ONE,
Train.BUILT + " " + _train.getNumberCarsWorked() + " " + Bundle.getMessage("cars"));
}
// check that engine assigned to the train has the appropriate HP
checkEngineHP();
// check to see if additional engines are needed for this train
checkNumnberOfEnginesNeeded();
// any cars not able to route?
if (_notRoutable.size() > 0) {
addLine(_buildReport, ONE, BLANK_LINE);
addLine(_buildReport, ONE, Bundle.getMessage("buildCarsNotRoutable"));
for (Car car : _notRoutable) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildCarNotRoutable"), new Object[]{
car.toString(), car.getLocationName(), car.getTrackName(), car.getFinalDestinationName(),
car.getFinalDestinationTrackName()}));
}
addLine(_buildReport, ONE, BLANK_LINE);
}
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTime"), new Object[]{
_train.getName(), new Date().getTime() - _startTime.getTime()}));
_buildReport.flush();
_buildReport.close();
// now make manifest
new TrainManifest(_train);
try {
new JsonManifest(_train).build();
} catch (IOException ex) {
log.error("Unable to create JSON manifest: {}", ex.getLocalizedMessage());
throw new BuildFailedException(ex);
}
if (Setup.isGenerateCsvManifestEnabled()) {
new TrainCsvManifest(_train);
}
_train.setBuilt(true);
// notify that locations have been modified by this train's build
for (Location location : _modifiedLocations) {
location.setStatus(Location.MODIFIED);
}
// now create and place train icon
_train.moveTrainIcon(_train.getTrainDepartsRouteLocation());
log.debug("Done building train ({})", _train.getName());
}
/**
* show train build options in detailed mode
*/
private void showTrainBuildOptions() {
ResourceBundle rb = ResourceBundle
.getBundle("jmri.jmrit.operations.setup.JmritOperationsSetupBundle");
addLine(_buildReport, FIVE, Bundle.getMessage("MenuItemBuildOptions") + ":");
if (Setup.isBuildAggressive()) {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildModeAggressive"));
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("BuildNumberPasses"),
new Object[]{Setup.getNumberPasses()}));
if (Setup.isStagingTrackImmediatelyAvail() && _departLocation.isStaging()) {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildStagingTrackAvail"));
}
} else {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildModeNormal"));
}
// show switcher options
if (_train.isLocalSwitcher()) {
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, rb.getString("BorderLayoutSwitcherService") + ":");
if (Setup.isLocalInterchangeMovesEnabled()) {
addLine(_buildReport, FIVE, rb.getString("AllowLocalInterchange"));
}
if (Setup.isLocalSpurMovesEnabled()) {
addLine(_buildReport, FIVE, rb.getString("AllowLocalSpur"));
}
if (Setup.isLocalYardMovesEnabled()) {
addLine(_buildReport, FIVE, rb.getString("AllowLocalYard"));
}
}
// show staging options
if (_departLocation.isStaging() || _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, Bundle.getMessage("buildStagingOptions"));
if (Setup.isTrainIntoStagingCheckEnabled() && _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, Bundle.getMessage("buildOptionRestrictStaging"));
}
if (Setup.isStagingTrackImmediatelyAvail() && _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("StagingAvailable"));
}
if (Setup.isAllowReturnToStagingEnabled() &&
_departLocation.isStaging() &&
_terminateLocation.isStaging() &&
_departLocation == _terminateLocation) {
addLine(_buildReport, FIVE, rb.getString("AllowCarsToReturn"));
}
if (Setup.isPromptFromStagingEnabled() && _departLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("PromptFromStaging"));
}
if (Setup.isPromptToStagingEnabled() && _terminateLocation.isStaging()) {
addLine(_buildReport, FIVE, rb.getString("PromptToStaging"));
}
}
// Car routing options
addLine(_buildReport, FIVE, BLANK_LINE);
addLine(_buildReport, FIVE, Bundle.getMessage("buildCarRoutingOptions"));
// warn if car routing is disabled
if (!Setup.isCarRoutingEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("RoutingDisabled"));
} else {
if (Setup.isCarRoutingViaYardsEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("RoutingViaYardsEnabled"));
}
if (Setup.isCarRoutingViaStagingEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("RoutingViaStagingEnabled"));
}
if (Setup.isOnlyActiveTrainsEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("OnlySelectedTrains"));
// list the selected trains
for (Train train : InstanceManager.getDefault(TrainManager.class).getTrainsByNameList()) {
if (train.isBuildEnabled()) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildTrainNameAndDesc"),
new Object[]{train.getName(), train.getDescription()}));
}
}
if (!_train.isBuildEnabled()) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildTrainNotSelected"),
new Object[]{_train.getName()}));
}
}
if (Setup.isCheckCarDestinationEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("CheckCarDestination"));
}
}
addLine(_buildReport, FIVE, BLANK_LINE);
// individual train build options
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildOptionsForTrain"),
new Object[]{_train.getName()}));
if (_train.isBuildTrainNormalEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("NormalModeWhenBuilding"));
}
if (_train.isSendCarsToTerminalEnabled()) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("SendToTerminal"),
new Object[]{_terminateLocation.getName()}));
}
if ((_train.isAllowReturnToStagingEnabled() || Setup.isAllowReturnToStagingEnabled()) &&
_departLocation.isStaging() &&
_departLocation == _terminateLocation) {
addLine(_buildReport, FIVE, Bundle.getMessage("AllowCarsToReturn"));
}
if (_train.isAllowLocalMovesEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("AllowLocalMoves"));
}
if (_train.isAllowThroughCarsEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("AllowThroughCars"));
}
if (_train.isServiceAllCarsWithFinalDestinationsEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("ServiceAllCars"));
}
if (_train.isSendCarsWithCustomLoadsToStagingEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("SendCustomToStaging"));
}
if (_train.isBuildConsistEnabled()) {
addLine(_buildReport, FIVE, Bundle.getMessage("BuildConsist"));
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("BuildConsistHPT"), new Object[]{
Setup.getHorsePowerPerTon()}));
}
addLine(_buildReport, ONE, BLANK_LINE); // add line
}
/**
* Show and initialize the train's route. Determines the number of car
* moves requested for this train. Also adjust the number of car moves
* if the random car moves option was selected.
* @throws BuildFailedException
*/
private void showAndInitializeTrainRoute() throws BuildFailedException {
int requestedCarMoves = 0; // how many cars were asked to be moved
// TODO: DAB control minimal build by each train
if (_train.getTrainDepartsRouteLocation().getMaxCarMoves() > _departLocation.getNumberRS() &&
Control.fullTrainOnly) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorCars"), new Object[]{
Integer.toString(_departLocation.getNumberRS()), _train.getTrainDepartsName(), _train.getName()}));
}
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrainRoute"), new Object[]{
_train.getName(), _train.getRoute().getName()}));
// get the number of requested car moves for this train
for (RouteLocation rl : _routeList) {
// check to see if there's a location for each stop in the route
Location location = locationManager.getLocationByName(rl.getName()); // this checks for a deleted location
if (location == null || rl.getLocation() == null) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorLocMissing"),
new Object[]{_train.getRoute().getName()}));
}
// train doesn't drop or pick up cars from staging locations found in middle of a route
if (location.isStaging() &&
rl != _train.getTrainDepartsRouteLocation() &&
rl != _train.getTrainTerminatesRouteLocation()) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildLocStaging"), new Object[]{rl
.getName()}));
rl.setCarMoves(rl.getMaxCarMoves()); // don't allow car moves for this location
// if a location is skipped, no car drops or pick ups
} else if (_train.skipsLocation(rl.getId())) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocSkippedMaxTrain"),
new Object[]{rl.getId(), rl.getName(), _train.getName(), rl.getMaxTrainLength(),
Setup.getLengthUnit().toLowerCase()}));
rl.setCarMoves(rl.getMaxCarMoves()); // don't allow car moves for this location
} else if (!rl.isDropAllowed() && !rl.isPickUpAllowed()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocNoDropsOrPickups"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxTrainLength(), Setup.getLengthUnit().toLowerCase()}));
rl.setCarMoves(rl.getMaxCarMoves()); // don't allow car moves for this location
} else {
// we're going to use this location, so initialize the route location
rl.setCarMoves(0); // clear the number of moves
requestedCarMoves += rl.getMaxCarMoves(); // add up the total number of car moves requested
// show the type of moves allowed at this location
if (location.isStaging() && rl.isPickUpAllowed() && rl == _train.getTrainDepartsRouteLocation()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildStagingDeparts"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves(), rl.getMaxTrainLength(),
Setup.getLengthUnit().toLowerCase()}));
} else if (location.isStaging() &&
rl.isDropAllowed() &&
rl == _train.getTrainTerminatesRouteLocation()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildStagingTerminates"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves()}));
} else if (rl == _train.getTrainTerminatesRouteLocation() &&
rl.isDropAllowed() &&
rl.isPickUpAllowed()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocTerminatesMoves"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves()}));
} else if (rl.isDropAllowed() && rl.isPickUpAllowed()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocRequestMoves"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves(), rl.getMaxTrainLength(),
Setup.getLengthUnit().toLowerCase()}));
} else if (!rl.isDropAllowed()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocRequestPickups"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves(), rl.getMaxTrainLength(),
Setup.getLengthUnit().toLowerCase()}));
} else if (rl == _train.getTrainTerminatesRouteLocation()) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocTerminates"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves()}));
} else {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildLocRequestDrops"),
new Object[]{rl.getId(), rl.getName(), rl.getMaxCarMoves(), rl.getMaxTrainLength(),
Setup.getLengthUnit().toLowerCase()}));
}
}
rl.setTrainWeight(0); // clear the total train weight
rl.setTrainLength(0); // and length
}
// check for random controls
for (RouteLocation rl : _routeList) {
if (rl.getRandomControl().equals(RouteLocation.DISABLED)) {
continue;
}
if (rl.getCarMoves() == 0 && rl.getMaxCarMoves() > 0) {
log.debug("Location ({}) has random control value {} and maximum moves {}", rl.getName(), rl
.getRandomControl(), rl.getMaxCarMoves());
try {
int value = Integer.parseInt(rl.getRandomControl());
// now adjust the number of available moves for this location
double random = Math.random();
log.debug("random {}", random);
int moves = (int) (random * ((rl.getMaxCarMoves() * value / 100) + 1));
log.debug("Reducing number of moves for location ({}) by {}", rl.getName(), moves);
rl.setCarMoves(moves);
requestedCarMoves = requestedCarMoves - moves;
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildRouteRandomControl"),
new Object[]{rl.getName(), rl.getId(), rl.getRandomControl(), rl.getMaxCarMoves(),
rl.getMaxCarMoves() - moves}));
} catch (NumberFormatException e) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorRandomControl"),
new Object[]{_train.getRoute().getName(), rl.getName(), rl.getRandomControl()}));
}
}
}
int numMoves = requestedCarMoves; // number of car moves
if (!_train.isLocalSwitcher()) {
requestedCarMoves = requestedCarMoves / 2; // only need half as many cars to meet requests
}
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildRouteRequest"), new Object[]{
_train.getRoute().getName(), Integer.toString(requestedCarMoves), Integer.toString(numMoves)}));
_train.setNumberCarsRequested(requestedCarMoves); // save number of car moves requested
}
/**
* Show how many engines are required for this train, and if a certain road
* name for the engine is requested. Show if there are any engine changes in
* the route, or if helper engines are needed. There can be up to 2 engine
* changes or helper requests. Show if caboose or FRED is needed for train,
* and if there's a road name requested. There can be up to 2 caboose
* changes in the route.
*/
private void showTrainRequirements() {
addLine(_buildReport, ONE, BLANK_LINE); // add line
addLine(_buildReport, ONE, Bundle.getMessage("TrainRequirements"));
if (_train.getNumberEngines().equals("0")) {
addLine(_buildReport, ONE, Bundle.getMessage("buildTrainReq0Engine"));
} else if (_train.getNumberEngines().equals("1")) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainReq1Engine"), new Object[]{
_train.getTrainDepartsName(), _train.getEngineModel(), _train.getEngineRoad()}));
} else {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainReqEngine"), new Object[]{
_train.getTrainDepartsName(), _train.getNumberEngines(), _train.getEngineModel(),
_train.getEngineRoad()}));
}
// show any required loco changes
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"), new Object[]{
_train.getSecondLegStartLocationName(), _train.getSecondLegNumberEngines(),
_train.getSecondLegEngineModel(), _train.getSecondLegEngineRoad()}));
}
if ((_train.getSecondLegOptions() & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainHelperEngines"), new Object[]{
_train.getSecondLegNumberEngines(), _train.getSecondLegStartLocationName(),
_train.getSecondLegEndLocationName(), _train.getSecondLegEngineModel(),
_train.getSecondLegEngineRoad()}));
}
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"), new Object[]{
_train.getThirdLegStartLocationName(), _train.getThirdLegNumberEngines(),
_train.getThirdLegEngineModel(), _train.getThirdLegEngineRoad()}));
}
if ((_train.getThirdLegOptions() & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainHelperEngines"), new Object[]{
_train.getThirdLegNumberEngines(), _train.getThirdLegStartLocationName(),
_train.getThirdLegEndLocationName(), _train.getThirdLegEngineModel(),
_train.getThirdLegEngineRoad()}));
}
// show caboose or FRED requirements
if ((_train.getRequirements() & Train.CABOOSE) == Train.CABOOSE) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainRequiresCaboose"),
new Object[]{_train.getTrainDepartsName(), _train.getCabooseRoad()}));
}
// show any caboose changes in the train's route
if ((_train.getSecondLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildCabooseChange"),
new Object[]{_train.getSecondLegStartLocation()}));
}
if ((_train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildCabooseChange"),
new Object[]{_train.getThirdLegStartLocation()}));
}
if ((_train.getRequirements() & Train.FRED) == Train.FRED) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTrainRequiresFRED"), new Object[]{
_train.getTrainDepartsName(), _train.getCabooseRoad()}));
}
addLine(_buildReport, ONE, BLANK_LINE); // add line
}
/**
* Figures out if the train terminates into staging, and if true, sets the
* termination track. Note if the train is returning back to the same track
* in staging _terminateStageTrack is null, and is loaded later when the
* departure track is determined.
* @throws BuildFailedException
*/
private void determineIfTrainTerminatesIntoStaging() throws BuildFailedException {
// does train terminate into staging?
_terminateStageTrack = null;
List<Track> stagingTracksTerminate = _terminateLocation.getTrackByMovesList(Track.STAGING);
if (stagingTracksTerminate.size() > 0) {
addLine(_buildReport, THREE, BLANK_LINE); // add line when in normal report mode
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildTerminateStaging"), new Object[]{
_terminateLocation.getName(), Integer.toString(stagingTracksTerminate.size())}));
if (stagingTracksTerminate.size() > 1 && Setup.isPromptToStagingEnabled()) {
_terminateStageTrack = promptToStagingDialog();
_startTime = new Date(); // reset build time since user can take awhile to pick
} else {
// is this train returning to the same staging in aggressive mode?
if (_departLocation == _terminateLocation &&
Setup.isBuildAggressive() &&
Setup.isStagingTrackImmediatelyAvail()) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildStagingReturn"),
new Object[]{_terminateLocation.getName()}));
} else {
for (Track track : stagingTracksTerminate) {
_terminateStageTrack = track;
if (checkTerminateStagingTrack(_terminateStageTrack)) {
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildStagingAvail"),
new Object[]{_terminateStageTrack.getName(), _terminateLocation.getName()}));
break;
}
_terminateStageTrack = null;
}
}
}
if (_terminateStageTrack == null) {
// is this train returning to the same staging in aggressive mode?
if (_departLocation == _terminateLocation &&
Setup.isBuildAggressive() &&
Setup.isStagingTrackImmediatelyAvail()) {
log.debug("Train is returning to same track in staging");
} else {
addLine(_buildReport, ONE, Bundle.getMessage("buildErrorStagingFullNote"));
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorStagingFull"),
new Object[]{_terminateLocation.getName()}));
}
}
}
}
/**
* Figures out if the train is departing staging, and if true, sets the
* departure track. Also sets the arrival track if the train is returning to
* the same departure track in staging. This routine also sets up the engine
* swaps in the train's route.
* @throws NumberFormatException
* @throws BuildFailedException
*/
private void determineIfTrainDepartsStagingAndLoadEngines() throws NumberFormatException, BuildFailedException {
// allow up to two engine and caboose swaps in the train's route
RouteLocation engineTerminatesFirstLeg = _train.getTrainTerminatesRouteLocation();
RouteLocation engineTerminatesSecondLeg = _train.getTrainTerminatesRouteLocation();
// Adjust where the locos will terminate
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES &&
_train.getSecondLegStartLocation() != null) {
engineTerminatesFirstLeg = _train.getSecondLegStartLocation();
}
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES &&
_train.getThirdLegStartLocation() != null) {
engineTerminatesSecondLeg = _train.getThirdLegStartLocation();
// No engine or caboose change at first leg?
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) != Train.CHANGE_ENGINES) {
engineTerminatesFirstLeg = _train.getThirdLegStartLocation();
}
}
// get list of engines for this route
_engineList = engineManager.getAvailableTrainList(_train);
// determine if train is departing staging
_departStageTrack = null;
List<Track> stagingTracks = _departLocation.getTrackByMovesList(Track.STAGING);
if (stagingTracks.size() > 0) {
addLine(_buildReport, THREE, BLANK_LINE); // add line when in normal report mode
addLine(_buildReport, ONE, MessageFormat.format(Bundle.getMessage("buildDepartStaging"), new Object[]{
_departLocation.getName(), Integer.toString(stagingTracks.size())}));
if (stagingTracks.size() > 1 && Setup.isPromptFromStagingEnabled()) {
_departStageTrack = promptFromStagingDialog();
_startTime = new Date(); // restart build timer
if (_departStageTrack == null) {
showTrainRequirements();
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorStagingEmpty"),
new Object[]{_departLocation.getName()}));
}
// load engines for this train
if (!getEngines(_reqNumEngines, _train.getEngineModel(), _train.getEngineRoad(), _train
.getTrainDepartsRouteLocation(), engineTerminatesFirstLeg)) {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorEngines"),
new Object[]{_reqNumEngines, _train.getTrainDepartsName(),
engineTerminatesFirstLeg.getName()}));
}
} else {
for (Track track : stagingTracks) {
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildStagingHas"),
new Object[]{track.getName(),
Integer.toString(track.getNumberEngines()),
Integer.toString(track.getNumberCars())}));
// is the departure track available?
if (!checkDepartureStagingTrack(track)) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildStagingTrackRestriction"),
new Object[]{track.getName(), _train.getName()}));
continue;
}
_departStageTrack = track;
// try each departure track for the required engines
if (getEngines(_reqNumEngines, _train.getEngineModel(), _train.getEngineRoad(), _train
.getTrainDepartsRouteLocation(), engineTerminatesFirstLeg)) {
addLine(_buildReport, SEVEN, Bundle.getMessage("buildDoneAssignEnginesStaging"));
break; // done!
}
_departStageTrack = null;
}
}
if (_departStageTrack == null) {
showTrainRequirements();
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorStagingEmpty"),
new Object[]{_departLocation.getName()}));
// departing staging and returning to same track?
} else if (_terminateStageTrack == null &&
_departLocation == _terminateLocation &&
Setup.isBuildAggressive() &&
Setup.isStagingTrackImmediatelyAvail()) {
_terminateStageTrack = _departStageTrack; // use the same track
}
} else {
// no staging tracks at this location, load engines for this train
if (_reqNumEngines > 0) {
addLine(_buildReport, FIVE, BLANK_LINE); // add line when in detailed report mode
}
if (!getEngines(_reqNumEngines, _train.getEngineModel(), _train.getEngineRoad(), _train
.getTrainDepartsRouteLocation(), engineTerminatesFirstLeg)) {
throw new BuildFailedException(MessageFormat
.format(Bundle.getMessage("buildErrorEngines"), new Object[]{_reqNumEngines,
_train.getTrainDepartsName(), engineTerminatesFirstLeg.getName()}));
}
}
_train.setTerminationTrack(_terminateStageTrack);
_train.setDepartureTrack(_departStageTrack);
// First engine change in route?
if ((_train.getSecondLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"),
new Object[]{_train.getSecondLegStartLocationName(), _train.getSecondLegNumberEngines(),
_train.getSecondLegEngineModel(), _train.getSecondLegEngineRoad()}));
if (getEngines(Integer.parseInt(_train.getSecondLegNumberEngines()), _train.getSecondLegEngineModel(),
_train.getSecondLegEngineRoad(), _train.getSecondLegStartLocation(), engineTerminatesSecondLeg)) {
_secondLeadEngine = _leadEngine;
} else {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorEngines"),
new Object[]{Integer.parseInt(_train.getSecondLegNumberEngines()),
_train.getSecondLegStartLocation(), engineTerminatesSecondLeg}));
}
}
// Second engine change in route?
if ((_train.getThirdLegOptions() & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
addLine(_buildReport, THREE, BLANK_LINE);
addLine(_buildReport, THREE, MessageFormat.format(Bundle.getMessage("buildTrainEngineChange"),
new Object[]{_train.getThirdLegStartLocationName(), _train.getThirdLegNumberEngines(),
_train.getThirdLegEngineModel(), _train.getThirdLegEngineRoad()}));
if (getEngines(Integer.parseInt(_train.getThirdLegNumberEngines()), _train.getThirdLegEngineModel(), _train
.getThirdLegEngineRoad(), _train.getThirdLegStartLocation(), _train
.getTrainTerminatesRouteLocation())) {
_thirdLeadEngine = _leadEngine;
} else {
throw new BuildFailedException(MessageFormat.format(Bundle.getMessage("buildErrorEngines"),
new Object[]{Integer.parseInt(_train.getThirdLegNumberEngines()),
_train.getThirdLegStartLocation(), _train.getTrainTerminatesRouteLocation()}));
}
}
if (_reqNumEngines > 0 && (!_train.isBuildConsistEnabled() || Setup.getHorsePowerPerTon() == 0)) {
addLine(_buildReport, SEVEN, MessageFormat.format(Bundle.getMessage("buildDoneAssingEnginesTrain"),
new Object[]{_train.getName()}));
}
}
/**
* Adds and removes cabooses or car with FRED in the train's route. Up to 2
* caboose changes.
*
* @throws BuildFailedException
*/
private void addCabooseOrFredToTrain() throws BuildFailedException {
// allow up to two caboose swaps in the train's route
RouteLocation cabooseOrFredTerminatesFirstLeg = _train.getTrainTerminatesRouteLocation();
RouteLocation cabooseOrFredTerminatesSecondLeg = _train.getTrainTerminatesRouteLocation();
// determine if there are any caboose changes
if ((_train.getSecondLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
cabooseOrFredTerminatesFirstLeg = _train.getSecondLegStartLocation();
} else if ((_train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
cabooseOrFredTerminatesFirstLeg = _train.getThirdLegStartLocation();
}
if ((_train.getThirdLegOptions() & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
(_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
cabooseOrFredTerminatesSecondLeg = _train.getThirdLegStartLocation();
}
// Do caboose changes in reverse order in case there isn't enough track space
// second caboose change?
if ((_train.getThirdLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE &&
_train.getThirdLegStartLocation() != null &&
_train.getTrainTerminatesRouteLocation() != null) {
getCaboose(_train.getThirdLegCabooseRoad(), _thirdLeadEngine, _train.getThirdLegStartLocation(), _train
.getTrainTerminatesRouteLocation(), true);
}
// first caboose change?
if ((_train.getSecondLegOptions() & Train.ADD_CABOOSE) == Train.ADD_CABOOSE &&
_train.getSecondLegStartLocation() != null &&
cabooseOrFredTerminatesSecondLeg != null) {
getCaboose(_train.getSecondLegCabooseRoad(), _secondLeadEngine, _train.getSecondLegStartLocation(),
cabooseOrFredTerminatesSecondLeg, true);
}
// departure caboose or car with FRED
getCaboose(_train.getCabooseRoad(), _train.getLeadEngine(), _train.getTrainDepartsRouteLocation(),
cabooseOrFredTerminatesFirstLeg, (_train.getRequirements() & Train.CABOOSE) == Train.CABOOSE);
getCarWithFred(_train.getCabooseRoad(), _train.getTrainDepartsRouteLocation(), cabooseOrFredTerminatesFirstLeg);
}
/**
* Ask which staging track the train is to depart on.
*
* @return The departure track the user selected.
*/
private Track promptFromStagingDialog() {
List<Track> tracksIn = _departLocation.getTrackList();
List<Track> validTracks = new ArrayList<>();
// only show valid tracks
for (Track track : tracksIn) {
if (checkDepartureStagingTrack(track)) {
validTracks.add(track);
}
}
if (validTracks.size() > 1) {
// need an object array for dialog window
Object[] tracks = new Object[validTracks.size()];
for (int i = 0; i < validTracks.size(); i++) {
tracks[i] = validTracks.get(i);
}
Track selected = (Track) JOptionPane.showInputDialog(null,
MessageFormat.format(Bundle.getMessage("TrainDepartingStaging"), new Object[]{_train.getName(),
_departLocation.getName()}),
Bundle.getMessage("SelectDepartureTrack"),
JOptionPane.QUESTION_MESSAGE, null, tracks, null);
if (selected != null) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildUserSelectedDeparture"),
new Object[]{selected.getName(), selected.getLocation().getName()}));
}
return selected;
} else if (validTracks.size() == 1) {
Track track = validTracks.get(0);
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildOnlyOneDepartureTrack"),
new Object[]{track.getName(), track.getLocation().getName()}));
return track;
}
return null; // no tracks available
}
/**
* Ask which staging track the train is to terminate on.
*
* @return The termination track selected by the user.
*/
private Track promptToStagingDialog() {
List<Track> tracksIn = _terminateLocation.getTrackByNameList(null);
List<Track> validTracks = new ArrayList<>();
// only show valid tracks
for (Track track : tracksIn) {
if (checkTerminateStagingTrack(track)) {
validTracks.add(track);
}
}
if (validTracks.size() > 1) {
// need an object array for dialog window
Object[] tracks = new Object[validTracks.size()];
for (int i = 0; i < validTracks.size(); i++) {
tracks[i] = validTracks.get(i);
}
Track selected = (Track) JOptionPane.showInputDialog(null, MessageFormat.format(Bundle
.getMessage("TrainTerminatingStaging"), new Object[]{_train.getName(),
_terminateLocation.getName()}),
Bundle.getMessage("SelectArrivalTrack"),
JOptionPane.QUESTION_MESSAGE, null, tracks, null);
if (selected != null) {
addLine(_buildReport, FIVE, MessageFormat.format(Bundle.getMessage("buildUserSelectedArrival"),
new Object[]{selected.getName(), selected.getLocation().getName()}));
}
return selected;
} else if (validTracks.size() == 1) {
return validTracks.get(0);
}
return null; // no tracks available
}
private boolean getEngines(int numberOfEngines, String model, String road, RouteLocation rl, RouteLocation rld)
throws BuildFailedException {
return getEngines(numberOfEngines, model, road, rl, rld, false);
}
/**
* Get the engines for this train at a route location. If departing from
* staging engines must come from that track. Finds the required number of
* engines in a consist, or if the option to build from single locos, builds
* a consist for the user. When true, engines successfully added to train
* for the leg requested.
*
* @param numberOfEngines Number of engines to assign to the train for this leg
* @param model Optional model name for the engines
* @param road Optional road name for the engines
* @param rl Departure route location for the engines
* @param rld Destination route location for the engines
* @param useBunit True if B unit engine is allowed
* @return true if correct number of engines found.
* @throws BuildFailedException
*/
private boolean getEngines(int numberOfEngines, String model, String road, RouteLocation rl, RouteLocation rld,