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

Function GetAircraftFlightLevel

src/aircraft_cmd.cpp:769–805  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

767
768template <class T>
769int GetAircraftFlightLevel(T *v, bool takeoff)
770{
771 /* Aircraft is in flight. We want to enforce it being somewhere
772 * between the minimum and the maximum allowed altitude. */
773 int aircraft_min_altitude;
774 int aircraft_max_altitude;
775 GetAircraftFlightLevelBounds(v, &aircraft_min_altitude, &aircraft_max_altitude);
776 int aircraft_middle_altitude = (aircraft_min_altitude + aircraft_max_altitude) / 2;
777
778 /* If those assumptions would be violated, aircraft would behave fairly strange. */
779 assert(aircraft_min_altitude < aircraft_middle_altitude);
780 assert(aircraft_middle_altitude < aircraft_max_altitude);
781
782 int z = v->z_pos;
783 if (z < aircraft_min_altitude ||
784 (HasBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION) && z < aircraft_middle_altitude)) {
785 /* Ascend. And don't fly into that mountain right ahead.
786 * And avoid our aircraft become a stairclimber, so if we start
787 * correcting altitude, then we stop correction not too early. */
788 SetBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION);
789 z += takeoff ? 2 : 1;
790 } else if (!takeoff && (z > aircraft_max_altitude ||
791 (HasBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION) && z > aircraft_middle_altitude))) {
792 /* Descend lower. You are an aircraft, not an space ship.
793 * And again, don't stop correcting altitude too early. */
794 SetBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION);
795 z--;
796 } else if (HasBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION) && z >= aircraft_middle_altitude) {
797 /* Now, we have corrected altitude enough. */
798 ClrBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION);
799 } else if (HasBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION) && z <= aircraft_middle_altitude) {
800 /* Now, we have corrected altitude enough. */
801 ClrBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION);
802 }
803
804 return z;
805}
806
807template int GetAircraftFlightLevel(DisasterVehicle *v, bool takeoff);
808template int GetAircraftFlightLevel(Aircraft *v, bool takeoff);

Callers 8

AircraftControllerFunction · 0.85
DisasterTick_ZeppelinerFunction · 0.85
DisasterTick_UfoFunction · 0.85
DisasterTick_AircraftFunction · 0.85
DisasterTick_Big_UfoFunction · 0.85
AfterLoadGameFunction · 0.85
UpdateOldAircraftFunction · 0.85

Calls 4

HasBitFunction · 0.85
SetBitFunction · 0.85
ClrBitFunction · 0.85

Tested by

no test coverage detected