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

Function RoadVehCheckOvertake

src/roadveh_cmd.cpp:807–857  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

805}
806
807static void RoadVehCheckOvertake(RoadVehicle *v, RoadVehicle *u)
808{
809 OvertakeData od;
810
811 od.v = v;
812 od.u = u;
813
814 /* Trams can't overtake other trams */
815 if (RoadTypeIsTram(v->roadtype)) return;
816
817 /* Don't overtake in stations */
818 if (IsTileType(v->tile, MP_STATION) || IsTileType(u->tile, MP_STATION)) return;
819
820 /* For now, articulated road vehicles can't overtake anything. */
821 if (v->HasArticulatedPart()) return;
822
823 /* Vehicles are not driving in same direction || direction is not a diagonal direction */
824 if (v->direction != u->direction || !(v->direction & 1)) return;
825
826 /* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
827 if (v->state >= RVSB_IN_ROAD_STOP || !IsStraightRoadTrackdir((Trackdir)(v->state & RVSB_TRACKDIR_MASK))) return;
828
829 /* Can't overtake a vehicle that is moving faster than us. If the vehicle in front is
830 * accelerating, take the maximum speed for the comparison, else the current speed.
831 * Original acceleration always accelerates, so always use the maximum speed. */
832 int u_speed = (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL || u->GetAcceleration() > 0) ? u->GetCurrentMaxSpeed() : u->cur_speed;
833 if (u_speed >= v->GetCurrentMaxSpeed() &&
834 !u->vehstatus.Test(VehState::Stopped) &&
835 u->cur_speed != 0) {
836 return;
837 }
838
839 od.trackdir = DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
840
841 /* Are the current and the next tile suitable for overtaking?
842 * - Does the track continue along od.trackdir
843 * - No junctions
844 * - No barred levelcrossing
845 * - No other vehicles in the way
846 */
847 od.tile = v->tile;
848 if (CheckRoadBlockedForOvertaking(&od)) return;
849
850 od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
851 if (CheckRoadBlockedForOvertaking(&od)) return;
852
853 /* When the vehicle in front of us is stopped we may only take
854 * half the time to pass it than when the vehicle is moving. */
855 v->overtaking_ctr = (od.u->cur_speed == 0 || od.u->vehstatus.Test(VehState::Stopped)) ? RV_OVERTAKE_TIMEOUT / 2 : 0;
856 v->overtaking = RVSB_DRIVE_SIDE;
857}
858
859static void RoadZPosAffectSpeed(RoadVehicle *v, int old_z)
860{

Callers 1

Calls 11

RoadTypeIsTramFunction · 0.85
IsTileTypeFunction · 0.85
IsStraightRoadTrackdirFunction · 0.85
DiagDirToDiagTrackdirFunction · 0.85
DirToDiagDirFunction · 0.85
TileOffsByDiagDirFunction · 0.85
HasArticulatedPartMethod · 0.80
GetAccelerationMethod · 0.80
TestMethod · 0.80
GetCurrentMaxSpeedMethod · 0.45

Tested by

no test coverage detected