| 767 | |
| 768 | template <class T> |
| 769 | int 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 | |
| 807 | template int GetAircraftFlightLevel(DisasterVehicle *v, bool takeoff); |
| 808 | template int GetAircraftFlightLevel(Aircraft *v, bool takeoff); |
no test coverage detected