| 1 | /* |
| 2 | * SPDX-FileName: AIAircraft.hxx |
| 3 | * SPDX-FileComment: AIBase derived class creates an AI aircraft |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2003 David P. Culp - [email protected] |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <iostream> |
| 11 | #include <string> |
| 12 | #include <string_view> |
| 13 | |
| 14 | #include "AIBaseAircraft.hxx" |
| 15 | |
| 16 | |
| 17 | class PerformanceData; |
| 18 | class FGAISchedule; |
| 19 | class FGAIFlightPlan; |
| 20 | class FGATCController; |
| 21 | class FGATCInstruction; |
| 22 | class FGAIWaypoint; |
| 23 | class sg_ofstream; |
| 24 | |
| 25 | namespace AILeg { |
| 26 | enum Type { |
| 27 | STARTUP_PUSHBACK = 1, |
| 28 | TAXI = 2, |
| 29 | TAKEOFF = 3, |
| 30 | CLIMB = 4, |
| 31 | CRUISE = 5, |
| 32 | APPROACH = 6, |
| 33 | HOLD = 7, |
| 34 | LANDING = 8, |
| 35 | PARKING_TAXI = 9, |
| 36 | PARKING = 10 |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | // 1 = joined departure queue; 2 = Passed DepartureHold waypoint; handover control to tower; 0 = any other state. |
| 41 | namespace AITakeOffStatus { |
| 42 | enum Type { |
| 43 | NONE = 0, |
| 44 | QUEUED = 1, // joined departure queue |
| 45 | CLEARED_FOR_TAKEOFF = 2 // Passed DepartureHold waypoint; handover control to tower; |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | class FGAIAircraft : public FGAIBaseAircraft |
| 50 | { |
| 51 | public: |
| 52 | explicit FGAIAircraft(FGAISchedule* ref = 0); |
| 53 | virtual ~FGAIAircraft(); |
| 54 | |
| 55 | std::string_view getTypeString(void) const override { return "aircraft"; } |
| 56 | void readFromScenario(SGPropertyNode* scFileNode) override; |
| 57 | |
| 58 | void bind() override; |
| 59 | void update(double dt) override; |
| 60 | void unbind() override; |
| 61 | |
| 62 | void setPerformance(const std::string& acType, const std::string& perfString); |
| 63 | |
| 64 | void setFlightPlan(const std::string& fp, bool repat = false); |
| 65 | |
| 66 | #if 0 |
| 67 | void initializeFlightPlan(); |
| 68 | #endif |
| 69 | |
| 70 | FGAIFlightPlan* GetFlightPlan() const |
| 71 | { |
| 72 | return fp.get(); |
| 73 | }; |
| 74 | void ProcessFlightPlan(double dt, time_t now); |
| 75 | time_t checkForArrivalTime(const std::string& wptName); |
| 76 | time_t calcDeparture(); |
| 77 | |
| 78 | void AccelTo(double speed); |
| 79 | void PitchTo(double angle); |
| 80 | void RollTo(double angle); |
| 81 | |
| 82 | #if 0 |
| 83 | void YawTo(double angle); |
| 84 | #endif |
| 85 | |
| 86 | void ClimbTo(double altitude); |
| 87 | void TurnTo(double heading); |
| 88 | |
| 89 | void getGroundElev(double dt); // TODO: these 3 really need to be public? |
| 90 | void doGroundAltitude(); |
| 91 | bool loadNextLeg(double dist = 0); |
| 92 | void resetPositionFromFlightPlan(); |
| 93 | double getBearing(double crse); |
| 94 | |
| 95 | void setAcType(const std::string& ac) { acType = ac; }; |
| 96 | const std::string& getAcType() const { return acType; } |
| 97 | |
| 98 | const std::string& getCompany() const { return company; } |
| 99 | void setCompany(const std::string& comp) { company = comp; }; |
| 100 | |
| 101 | //ATC |
| 102 | void announcePositionToController(); // TODO: have to be public? |
| 103 | void processATC(const FGATCInstruction& instruction); |
| 104 | void setTaxiClearanceRequest(bool arg) { needsTaxiClearance = arg; }; |
| 105 | bool getTaxiClearanceRequest() { return needsTaxiClearance; }; |
| 106 | FGAISchedule* getTrafficRef() { return trafficRef; }; |
| 107 | void setTrafficRef(FGAISchedule* ref) { trafficRef = ref; }; |
| 108 | void resetTakeOffStatus() { takeOffStatus = AITakeOffStatus::NONE; }; |
| 109 | void setTakeOffStatus(int status) { takeOffStatus = status; }; |
| 110 | int getTakeOffStatus() { return takeOffStatus; }; |
| 111 | void setTakeOffSlot(time_t timeSlot) { takeOffTimeSlot = timeSlot; }; |
| 112 | time_t getTakeOffSlot() { return takeOffTimeSlot; }; |
| 113 | void scheduleForATCTowerDepartureControl(); |
| 114 | |
| 115 | const std::string& GetTransponderCode() { return transponderCode; }; |
| 116 | void SetTransponderCode(const std::string& tc) { transponderCode = tc; }; |
| 117 | |
| 118 | // included as performance data needs them, who else? |
| 119 | inline PerformanceData* getPerformance() { return _performance; }; |
| 120 | inline bool onGround() const { return no_roll; }; |
| 121 | inline double getSpeed() const { return speed; }; |
| 122 | inline double getRoll() const { return roll; }; |
| 123 | inline double getPitch() const { return pitch; }; |
| 124 | inline double getAltitude() const { return altitude_ft; }; |
| 125 | inline double getVerticalSpeedFPM() const { return vs_fps * 60; }; |
| 126 | inline double altitudeAGL() const { return props->getFloatValue("position/altitude-agl-ft"); }; |
| 127 | inline double airspeed() const { return props->getFloatValue("velocities/airspeed-kt"); }; |
| 128 | const std::string& atGate(); |
| 129 | std::string acwakecategory; |
| 130 | |
| 131 | void checkTcas(); |
| 132 | double calcVerticalSpeed(double vert_ft, double dist_m, double speed, double error); |
| 133 | |
| 134 | FGATCController* getATCController() { return controller; }; |
| 135 | |
| 136 | void clearATCController(); |
| 137 | bool isBlockedBy(FGAIAircraft* other); |
| 138 | void dumpCSVHeader(const std::unique_ptr<sg_ofstream>& o); |
| 139 | void dumpCSV(const std::unique_ptr<sg_ofstream>& o, int lineIndex); |
| 140 | |
| 141 | protected: |
| 142 | void Run(double dt); |
| 143 | |
| 144 | private: |
| 145 | FGAISchedule* trafficRef; |
| 146 | FGATCController *controller, |
| 147 | *prevController, |
| 148 | *towerController; // Only needed to make a pre-announcement |
| 149 | |
| 150 | bool hdg_lock; |
| 151 | bool alt_lock; |
| 152 | double dt_count; |
| 153 | double dt_elev_count; |
| 154 | double headingChangeRate; |
| 155 | double headingError; |
| 156 | double minBearing; |
| 157 | double speedFraction; |
| 158 | |
| 159 | /**Zero if FP is not active*/ |
| 160 | double groundTargetSpeed; |
| 161 | double groundOffset; |
| 162 | |
| 163 | bool use_perf_vs; |
| 164 | SGPropertyNode_ptr refuel_node; |
| 165 | SGPropertyNode_ptr tcasThreatNode; |
| 166 | SGPropertyNode_ptr tcasRANode; |
| 167 | |
| 168 | // helpers for Run |
| 169 | // TODO: sort out which ones are better protected virtuals to allow |
| 170 | //subclasses to override specific behaviour |
| 171 | bool fpExecutable(time_t now); |
| 172 | void handleFirstWaypoint(void); |
| 173 | bool leadPointReached(FGAIWaypoint* curr, FGAIWaypoint* next, int nextTurnAngle); |
| 174 | bool handleAirportEndPoints(FGAIWaypoint* prev, time_t now); |
| 175 | bool reachedEndOfCruise(double&); |
| 176 | bool aiTrafficVisible(void); |
| 177 | void controlHeading(FGAIWaypoint* curr, |
| 178 | FGAIWaypoint* next); |
| 179 | void controlSpeed(FGAIWaypoint* curr, FGAIWaypoint* next); |
| 180 | |
| 181 | void updatePrimaryTargetValues(double dt, bool& flightplanActive, bool& aiOutOfSight); |
| 182 | void updateSecondaryTargetValues(double dt); |
| 183 | void updateHeading(double dt); |
| 184 | void updateBankAngleTarget(); |
| 185 | void updateVerticalSpeedTarget(double dt); |
| 186 | void updatePitchAngleTarget(); |
| 187 | void updateActualState(double dt); |
| 188 | void updateModelProperties(double dt); |
| 189 | /**Handle special cases for the User AI shadow*/ |
| 190 | void updateUserFlightPlan(double dt); |
| 191 | |
| 192 | /**Calculate the next leg (hold or not)*/ |
| 193 | int determineNextLeg(int leg); |
| 194 | void handleATCRequests(double dt); |
| 195 | |
| 196 | inline bool isStationary() |
| 197 | { |
| 198 | return ((fabs(speed) <= 0.0001) && (fabs(tgt_speed) <= 0.0001)); |
| 199 | } |
| 200 | |
| 201 | inline bool needGroundElevation() |
| 202 | { |
| 203 | if (!isStationary()) |
| 204 | _needsGroundElevation = true; |
| 205 | return _needsGroundElevation; |
| 206 | } |
| 207 | |
| 208 | double sign(double x); |
| 209 | |
| 210 | #if 0 |
| 211 | std::string getTimeString(int timeOffset); |
| 212 | #endif |
| 213 | |
| 214 | void lazyInitControlsNodes(); |
| 215 | |
| 216 | std::string acType; |
| 217 | std::string company; |
| 218 | std::string transponderCode; |
| 219 | |
| 220 | int spinCounter; |
| 221 | |
| 222 | /**Kills a flight when it's stuck */ |
| 223 | const int AI_STUCK_LIMIT = 100; |
| 224 | int stuckCounter = 0; |
| 225 | bool tracked = false; |
| 226 | /** |
| 227 | * Signals a reset to leg 1 at a different airport. |
| 228 | * The leg loading happens at a different place than the parking loading. |
| 229 | * */ |
| 230 | bool repositioned = false; |
| 231 | double prevSpeed; |
| 232 | double prev_dist_to_go; |
| 233 | |
| 234 | bool holdPos = false; |
| 235 | |
| 236 | const char* _getTransponderCode() const; |
| 237 | |
| 238 | bool needsTaxiClearance = false; |
| 239 | bool _needsGroundElevation = true; |
| 240 | int takeOffStatus; // 1 = joined departure queue; 2 = Passed DepartureHold waypoint; handover control to tower; 0 = any other state. |
| 241 | time_t takeOffTimeSlot{0}; |
| 242 | time_t timeElapsed{0}; |
| 243 | |
| 244 | PerformanceData* _performance; // the performance data for this aircraft |
| 245 | |
| 246 | #if 0 |
| 247 | void assertSpeed(double speed); |
| 248 | #endif |
| 249 | |
| 250 | struct |
| 251 | { |
| 252 | double remainingLength; |
| 253 | std::string startWptName; |
| 254 | std::string finalWptName; |
| 255 | } trackCache; |
| 256 | |
| 257 | // these are init-ed on first use by lazyInitControlsNodes() |
| 258 | SGPropertyNode_ptr _controlsLateralModeNode, |
| 259 | _controlsVerticalModeNode, |
| 260 | _controlsTargetHeadingNode, |
| 261 | _controlsTargetRollNode, |
| 262 | _controlsTargetAltitude, |
| 263 | _controlsTargetPitch, |
| 264 | _controlsTargetSpeed; |
| 265 | |
| 266 | std::unique_ptr<sg_ofstream> csvFile; |
| 267 | long csvIndex{0}; |
| 268 | }; |