MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ChangeTrainDirRandomly

Function ChangeTrainDirRandomly

src/train_cmd.cpp:3714–3735  ·  view source on GitHub ↗

* Rotate all vehicles of a (crashed) train chain randomly to animate the crash. * @param v First crashed vehicle. */

Source from the content-addressed store, hash-verified

3712 * @param v First crashed vehicle.
3713 */
3714static void ChangeTrainDirRandomly(Train *v)
3715{
3716 static const DirDiff delta[] = {
3717 DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
3718 };
3719
3720 do {
3721 /* We don't need to twist around vehicles if they're not visible */
3722 if (!v->vehstatus.Test(VehState::Hidden)) {
3723 v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
3724 /* Refrain from updating the z position of the vehicle when on
3725 * a bridge, because UpdateInclination() will put the vehicle under
3726 * the bridge in that case */
3727 if (v->track != TRACK_BIT_WORMHOLE) {
3728 v->UpdatePosition();
3729 v->UpdateInclination(false, true);
3730 } else {
3731 v->UpdateViewport(false, true);
3732 }
3733 }
3734 } while ((v = v->Next()) != nullptr);
3735}
3736
3737/**
3738 * Handle a crashed train.

Callers 1

HandleCrashedTrainFunction · 0.85

Calls 8

ChangeDirFunction · 0.85
GBFunction · 0.85
TestMethod · 0.80
UpdateInclinationMethod · 0.80
RandomFunction · 0.50
UpdatePositionMethod · 0.45
UpdateViewportMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected