TG:Logic/Work order logic

The work order logic is one of the core aspects of the game, and must therefore be carefully considered.

Preliminary thoughts
Work orders are going to be interdependent. Unlike OpenTTD's orders list, which define all the normal operations of a train (and which don't/cannot exist here), WOs define units of work that must be completed, which units of work may be dependent on previous work having been done. A work order can define a complete trip from A to B and back again, or even a complex schedule for a passenger train, but most work orders will define only part of the trips rolling stock will end up travelling.

The initial reason for this implementation is realism -- in the prototype (i.e. real train operations), a cargo wagon will travel through about three trains between being loaded and unloaded (though occasionally there are complete trains that make round-trips). The reason for this is to cut down on the number of (expensive!!!) locomotives:
 * cheap ones can be relatively powerful but extremely slow, so they can shunt wagons around a yard (and make short trips to pick up freight nearby, but not so far that they'll foul up the main line); whereas:
 * expensive ones powerful enough to pull a large number of wagons at main line speeds should always be used to the fullest of their capacity -- spending them on short trains is a waste of power.

Thus, we end up with a situation where a freight wagon will be loaded, carried to a yard by one engine (slow, semi-powerful), then attached to a longer train with a second engine (fast, powerful) which carries them to another yard, whereupon they will be detached and carried to the final destination by a third engine (another slow, semi-powerful one). Incidentally, this also gives us two classes of engines: shunters and mainline engines (and we can further subdivide short-run freight engines from shunters -- slightly higher speed/power, but not quite up to mainline standards).

This also translates to work that will be performed by different engines, depending on what is available -- a mainline engine can do the work of a shunter if necessary, but if a shunter is available and there is a long train to take to a distant yard, the ML engine should do that instead.

There are two ways to approach this problem: either have work orders be complex, long, and involve multiple engine switches as well as many wagon changes, or have them be short, simple, and inter-dependent. The latter is an easier choice for both implementer and player.

Simple work order
This is what you'll be setting up (or allowing auto-setup) in the early game:
 * Work order 1 (any engine)
 * On the 1st of the month,
 * Pick up 5 empty 3-bay hoppers from NearAlpha yard.
 * Deliver 5 of the wagons from Work Order 1.2 to Alpha coal mine loading track.
 * Pick up 5 loaded 3-bay hoppers from Alpha coal mine loading track.
 * Deliver 5 of the wagons from Work Order 1.4 to Beta power station unloading track.
 * Pick up 5 empty 3-bay hoppers from Beta power station unloading track.
 * Disperse 5 of the wagons from Work Order 1.6 to NearAlpha yard.

Extensible order set
The following set of work orders will fulfill a contract to deliver 5 wagons' worth of coal from coal mine Alpha to power station Baker:
 * Work order 1 (prefer using a shunter, ML engines are allowed to fulfill this order):
 * On the 1st of the month,
 * Pick up 5 empty 3-bay hopper wagons from NearAlpha yard.
 * Deliver 5 of the wagons from WO 1.2 to an empty Alpha coal mine loading track.


 * Work order 2 (use the engine that just completed WO 1):
 * When work order 1 is complete,
 * Pick up 5 loaded 3-bay hopper wagons from Alpha coal mine loading track.
 * Attach 5 of the wagons from WO 2.2 to train on track 2 of NearAlpha yard (form train, if none present).


 * Work order 3 (ML engines only):
 * When work order 2 is complete,
 * Using as many engines as necessary to run train on track 2 of NearAlpha yard at full speed,
 * Take train from track 2 of NearAlpha yard to track 5 of NearBeta yard.


 * Work order 4 (prefer using a shunter, ML engines are allowed to fulfill this order):
 * When work order 3 is complete,
 * Detach 5 wagons loaded at Alpha coal mine from train on track 5 of NearBeta yard.
 * Deliver 5 wagons from WO 4.2 to an empty Beta power station unloading track.


 * Work order 5 (use the engine that just completed WO 4):
 * When work order 4 is complete,
 * Pick up 5 empty 3-bay hopper wagons from Beta power station unloading tracks.
 * Attach 8 wagons from WO 5.2 to train on track 2 of NearBeta yard (form train, if none present).


 * Work order 6 (ML engines only):
 * When work order 5 is complete,
 * Using as many engines as necessary to run train on track 2 of NearBeta yard at full speed,
 * Take train from track 2 of NearBeta yard to track 5 of NearAlpha yard.


 * Work order 7 (prefer using a shunter, ML engines are allowed to fulfill this order):
 * When work order 6 is complete,
 * Detach 5 empty 3-bay hopper wagons from train at track 5.
 * Disperse 5 wagons from WO 7.2 to tracks at NearAlpha yard.

Conclusion
This order set essentially does the same things as the previous single work order did, but allows us to use multiple engine types to perform the same work. Note that since ML engines are allowed to fulfill the shunting orders, you can switch to this order set even if you only have the one engine you first bought, and when you get shunters for your two yards, they will automatically be preferred and take over.

The other reason to complicate the work orders like this is that it allows you to take on a few other jobs, like we'll do in the next order set.

Extended extensible order set
We've now taken another contract to carry 3 wagons' worth of coal from Coal Mine Gamma, which is on the same spur as Coal Mine Alpha, just a few steps further down the line. The wagons are also going to Power Station Beta (this is why we took the contract), so it was a natural expansion. Now watch how the extensible order set accommodates this:


 * Work order 1 (prefer using a shunter, ML engines are allowed to fulfill this order):
 * On the 1st of the month,
 * Pick up 8 empty 3-bay hopper wagons from NearAlpha yard.
 * Deliver 5 of the wagons from WO 1.2 to an empty Alpha coal mine loading track.
 * Deliver 3 of the wagons from WO 1.2 to an empty Gamma coal mine loading track.

(The work order got a changed number of empty wagons picked up, and an additional delivery tacked on the end)


 * Work order 2 (use the engine that just completed WO 1):
 * When work order 1 is complete,
 * Pick up 3 loaded 3-bay hopper wagons from Gamma coal mine loading track.
 * Pick up 5 loaded 3-bay hopper wagons from Alpha coal mine loading track.
 * Attach 5 wagons from WO 2.3 to train on track 2 of NearAlpha yard (form train, if none present).
 * Attach 3 wagons from WO 2.2 to train on track 2 of NearAlpha yard.

(This work order got an extra pickup and an extra attach order. The AI should do both of them more or less simultaneously. Order might matter, depending on how clever the AI ends up being.)


 * Work order 3 remains identical to the existing one.


 * Work order 4 (prefer using a shunter, ML engines are allowed to fulfill this order):
 * When work order 3 is complete,
 * Detach 5 wagons loaded at Alpha coal mine from train on track 5 of NearBeta yard.
 * Detach 3 wagons loaded at Gamma coal mine from train on track 5 of NearBeta yard.
 * Deliver 5 wagons from WO 4.2 to an empty Beta power station unloading track.
 * Deliver 3 wagons from WO 4.3 to the same Beta power station unloading track as WO 4.4.

(This work order gets an extra detach, and then references WO 4.4 when doing the second deliver to track)


 * Work order 5 (use the engine that just completed WO 4):
 * When work order 4 is complete,
 * Pick up 8 empty 3-bay hopper wagons from Beta power station unloading tracks.
 * Attach 8 wagons from WO 5.2 to train on track 2 of NearBeta yard (form train, if none present).

(Here we just changed some numbers from 5 to 8. We don't need to differentiate between them anymore)


 * Work order 6 remains identical to the existing one.


 * Work order 7 (prefer using a shunter, ML engines are allowed to fulfill this order):
 * When work order 6 is complete,
 * Detach 8 empty 3-bay hopper wagons from train at track 5.
 * Disperse 8 wagons from WO 7.2 to tracks at NearAlpha yard.

(Here, again, we just changed the numbers from 5 wagons to 8 wagons.)

Conclusion
As we can see, the order set required minimal changes to accommodate the new contract, and it requires essentially the same number of engines to fulfill, as well. 8 wagons is close to straining the average yard shunter, though, so on the next contract, we may need to break work order 1 into two separate ones, to be fulfilled in parallel. Alternatively, we may not take another contract from this side, but instead take one for the return trip -- lengthening the trains carried by both WO 3 and 6, but otherwise manageable by other shunters with different work orders altogether.