FS#105 - Trains may suddenly loose their power on (el)rails

Attached to Project: OpenTTD
Opened by Michael Lutz (michi_cc) - Saturday, 08 April 2006, 11:15 GMT
Type Bug
Category Vehicles → Electrified rails
Status Closed
Assigned To Celestar (Celestar)
Operating System All
Severity Medium
Priority Normal
Reported Version trunk
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No


I found a strange bug during testing the physics patch. As the fault appears to be in trunk code, I don't suspect the patch causes this.

I can't confirm it, but this bugs seems to happen only with trains (electric or not ) on elrail. No occurance noticed on normal rail.

The symptoms are that trains suddenly have 0 hp, which sometimes fixes itself magically. As the train power is cached and all, it normal not to encounter this symptom. The physics patch reuses a part of the power calculations for tractive effort (TE), which goes to 0 sometimes, too; only it's much more visible then as trains will suddenly move very slowly for no apparent reason.

Calculation and caching of these values happens in TrainCargoChanged(). To debug it, I added a line to GetTrainAcceleration() that calls TrainCargoChanged() every time power or TE are 0. It's very likely that bridges (and maybe tunnels) have something to with it, as I observed a train that passed below a bridge and fixed itself, while it wasn't able to do so on the about 15 tiles of elrail before.

I was able to pin it down to the following code part from train_cmd.c line 92 inside TrainPowerChanged(). In most cases, all test should be negative an no variable setto false. The bug occurs when one of these test produces a false positive.

if (IsBridgeTile(u->tile) && IsBridgeMiddle(u->tile) && DiagDirToAxis(DirToDiagDir(u->direction)) == GetBridgeAxis(u->tile)) {
if (!HasPowerOnRail(u->u.rail.railtype, GetRailTypeOnBridge(u->tile))) engine_has_power = false;
if (!HasPowerOnRail(v->u.rail.railtype, GetRailTypeOnBridge(u->tile))) wagon_has_power = false;
} else if (IsLevelCrossing(u->tile)) {
if (!HasPowerOnRail(u->u.rail.railtype, GetRailTypeCrossing(u->tile))) engine_has_power = false;
if (!HasPowerOnRail(v->u.rail.railtype, GetRailTypeCrossing(u->tile))) wagon_has_power = false;
} else {
if (!HasPowerOnRail(u->u.rail.railtype, GetRailType(u->tile))) engine_has_power = false;
if (!HasPowerOnRail(v->u.rail.railtype, GetRailType(u->tile))) wagon_has_power = false;
This task depends upon

Closed by  Celestar (Celestar)
Sunday, 09 April 2006, 18:02 GMT
Reason for closing:  Fixed
Comment by Johannes E. Krause (Eddi) - Saturday, 08 April 2006, 22:25 GMT
i got this problem, too. especially, i noticed that the same engines repeatedly get affected by this.

i have a savegame here.

Watch the BR 103 (Train 25, center of the screen, can't miss it) going downhill. right when it hits the bottom, the power changes to 0hp. (as there was talking about caching, i assume the problem occures earlier, but the symptom shows at this time)

it then runs out until it goes uphill at the next city, where it stops half way through, blocking all following traffic. reversing the train usually fixes the problem.

to reproduce: simply wait for the next 103 to come along that point of the track (there are 3 of them on that route)
Comment by Johannes E. Krause (Eddi) - Saturday, 08 April 2006, 22:36 GMT
forgot to add openttd.cfg...

also... the ICE1 seems to be affected by this also, only the effect is less, because it is dualheaded, and fixes itself quickly.
Comment by Michael Lutz (michi_cc) - Sunday, 09 April 2006, 13:39 GMT
It was reported on IRC that reversing the train may magically fix it, but could cause it as well.
Comment by ledow (ledow) - Sunday, 09 April 2006, 14:10 GMT
I've had this same problem on plain OpenTTD SVN (4335 and some revisions below that) with the realistic acceleration patch turned on. Reversing the train twice does usually fix it but I also notice strange ultra-slow acceleration symptoms too i.e. a train that has broken down or been told to reverse literally gaining about 1 or 2 mph per second, even though it's not on a hill and double-reversing the train sees it accelerate at normal speed (about five or six times faster?).
Comment by Johannes E. Krause (Eddi) - Sunday, 09 April 2006, 17:08 GMT
in my savegame, the problem gets fixed, when i put an elrail under the empty bridge tile (does not work with conventional rail), so it seems to check for the rail _under_ the bridge, not _on_ the bridge

as for the reversing train _causing_ 0hp problem, that was nowhere near a bridge, and i could not get this reproduceable... it seems to be related, but probably the occurence is at a completely different piece of code.