Intro:
I would like to present you with a piece of code to facilitate future development for
custom bridge heads, tunnels bridges with signals and underground railways.
Reason:
During development of my signals in wormhole patch the most difficult thing was that bridge and
tunnel ramps act differently.
This made it very hard to determine if a vehicle is actually on the ramp or not.
Writing fast and simple code for that situation is difficult and many courageous attempts have failed.
Even my patch is not good enough for trunk but it shows a direction.
By showing this patch I am trying to show a possible beginning.
Another reason for this patch is to make it easier to change the exact point where visibility
of vehicle changes when entering or leaving tunnel.
If you really study the table you can see that by readjusting those points even faster code is
achievable.
This however is not possible without some graphical adjustments in the sprites.

And last but not least my time is getting more and more limited so I am trying to pass on my
knowledge.
Story:
In ttd in ottd bridges work completely different.
In ttd bridges are actually drawn on the map and vehicles simply step from the ramp onto the bridge and
bridges are simply another 'rail type'.
So vehicles can do all necessary stuff right on the moment when they step from the ramp onto the bridge.
In ottd vehicles are send into the wormhole on the incorrect location. The correct location should be
the moment that they are about to enter the wormhole, eg leave ramp (wormhole = space between ramps).
Only like that it is easy to determine if vehicle is on ramp or in wormhole.(TRACK_BIT_WORMHOLE)
Like this you can make the ramps a sort of 'safe tile'.
If you look at the VehicleEnter_TunnelBridge code you can see that this a very fast piece of code and
to improve on that code was only possible by doing a complete rewrite.
When you have a look at the code you can see that only on the very last moment the vehicle enters the
wormhole.
This is critical for trains because of signals and in this patch is only applied for trains.
To do it for road vehicles or even ships would only require minor changes.
/* First handle trains entering wormhole */
if (v->vehstatus & VS_AIRCRAFT_BROKEN){
v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
v->track = TRACK_BIT_WORMHOLE;
goto wormhole_state;
}
I used the flag VS_AIRCRAFT_BROKEN that is only used for aircraft's.
I did not ad other flag name (VS_ENTER_WORMHOLE) in order to reduce compile time.

I am aware of the "if it ain't broke don't fix it" slogan but still I would like some people
to have a look and test it in order to get some feedback.
You can see in FS3304 that I have been struggling with this problem already some time.
The patch:
The patch is broken up in two and are supplied in FS3304.
One patch to rectify old safe games
and one that is a complete rewrite of the VehicleEnter_TunnelBridge proc with a few extra
lines in train_cmd.cpp to adjust train reversing and cleanup of redundant code.
Additional:
proposed addition in vehicle_base.h
enum VehStatus {
VS_HIDDEN = 0x01,
VS_STOPPED = 0x02,
VS_UNCLICKABLE = 0x04,
VS_DEFPAL = 0x08,
VS_TRAIN_SLOWING = 0x10,
VS_SHADOW = 0x20,
VS_AIRCRAFT_BROKEN = 0x40,
VS_ENTER_WORMHOLE = 0x40, <----
VS_CRASHED = 0x80,
};
and change VS_AIRCRAFT_BROKEN for VS_ENTER_WORMHOLE in train_cmd.cpp and tunnelbridge_cmd.cpp
to ditinguish between the two.
Future:
Try to get this patch in trunk after extensive testing.
Another big job awaits before bridge heads and signals etc. could become reality and that is
making a 'tunnel bridge pool'.( I agree devellopers)
Properly done I think it could reduce overhead.
I can somehow visualise it but I am not able to do it.

just a small example to give some ideas:
1: index equal to 16 bits m2 in map array
2: wormhole entrance tile 32 bits
3: wormhole exit tile 32 bits
4: wormhole length 32 bits
5: axis or direction 8 bits
6: flags 16 bits
7: pointer 32 bits to 8 array structue or whatever.
8: underground stations or signals or something else.
Entrance could be defined as NE and NW ramp, and exit as SW and SE ramp axis.
so the moment you know the axis you will be able to find entrance and exit tile.
this data could be used to eliminate the GetOtherTunnelEnd and GetOtherBridgeEnd procs.
Hoping to have been of any help
HackaLittleBit