I'm really looking forward to PBS. For a time I worked on a 3D train management game and completed this aspect. I doubt there is anything here that the Devs don't already know, but here is how I did it.
Not all of this can exactly apply to OpenTTD because OpenTTD needs to be a bit more flexible.
Sequence.
1. Train about to depart works out the route to its next destination and records it. (OpenTTD, may only need to route 4 blocks)
2. (if a signal exists) The train scans along the route until one or thew following is true: The tracks 'reserved for' value is none zero or it has reached a fourth signal. (OpenTTD, may 'timeout' ealier if too long between signals - would leave a permanent yellow state and user is prompted not to be so mean!)
Each of the (max 4) signals is recorded.
3. A path is reserved ONLY up to the last good signal (IE entire blocks are reserved only)
4. Each signal is set to a color. How this bit works:
If the last signal reached is red, then the next nearest signal is yellow, then double yellow (if 4 aspect) and then green.
So if the train reserves two blocks the Last Signal is red, the next signal is yellow and the signal in front of the train is double yellow.
5. Rear setting.
This is the bit that prompted me to make this post. I thought long and hard about how much processing time would be eaten by working back along all the junctions recursively... until I realised.. i didn't have to!
When the train leaves a section, a marker is 'sent out' along the track setting the signal colors to: Red, then Yellow, then green.
If this marker reaches a junction.. it stops!
This will have the effect that all signals gaurding a junction will always be on red. Which is exactly how it working in UK (at least) signalling.
But signals on a normal stretch will be left on green.
6. User
If the user modifies a reserved track the train's path is revoked. The train tries to set a new path. If it fails it simply shut down (OpenTTD suggestion: Tell the train route revoked. The train removes its path (BEFORE editing takes place). The train returns TRUE if it is not too close and can safely slow down and stop. If FALSE the user has a "Track in use" message OR do a cartoon 'sudden stop'
OpenTTD specific modifications:
The train chooses a route and allocates as far as possible up to 3 or 4 blocks (depending on the type of signal). As described in section 2 it reserves as much as possible.
When each train is processed OpenTTD checks if the train has allocated the required number of blocks ahead, if not it then pathfinds from the last block its allocated again. In other words it pathfinds in advance.
Allocation of track.
In my game the actual Train ID is recorded on the track for two reasons:
1. To revert the path.
2. For delay attributation (doesnt apply to openTTD)
Im not sure if you have 16 bits per tile free. If not then reservations will have to be a flag, and will have to iterate though all trains to find out which has allocated a section if its being edited.
Like I said, you probably know all this, but just wanted to throw in everything I worked out as I wont be making my game.
Oh and I called it semiautomatic signalling after the real type.
