FlightGear next
AIAircraft.hxx
Go to the documentation of this file.
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 - davidculp2@comcast.net
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#include "AIConstants.hxx"
16
17
18class PerformanceData;
19class FGAISchedule;
20class FGAIFlightPlan;
21class FGATCController;
23class FGAIWaypoint;
24class sg_ofstream;
25
27{
28public:
29 explicit FGAIAircraft(FGAISchedule* ref = 0);
30 virtual ~FGAIAircraft();
31
32 std::string_view getTypeString(void) const override { return "aircraft"; }
33 void readFromScenario(SGPropertyNode* scFileNode) override;
34
35 void bind() override;
36 void update(double dt) override;
37 void unbind() override;
38
39 void setPerformance(const std::string& acType, const std::string& perfString);
40
41 void setFlightPlan(const std::string& fp, bool repat = false);
42
43#if 0
44 void initializeFlightPlan();
45#endif
46
48 {
49 return fp.get();
50 };
51 void ProcessFlightPlan(double dt, time_t now);
52 time_t checkForArrivalTime(const std::string& wptName);
53 time_t calcDeparture();
54
55 void AccelTo(double speed);
56 void PitchTo(double angle);
57 void RollTo(double angle);
58
59#if 0
60 void YawTo(double angle);
61#endif
62
63 void ClimbTo(double altitude);
64 void TurnTo(double heading);
65
66 void getGroundElev(double dt); // TODO: these 3 really need to be public?
67 void doGroundAltitude();
68 bool loadNextLeg(double dist = 0);
70 double getBearing(double crse);
71
72 void setAcType(const std::string& ac) { acType = ac; };
73 const std::string& getAcType() const { return acType; }
74
75 const std::string& getCompany() const { return company; }
76 void setCompany(const std::string& comp) { company = comp; };
77
78 //ATC
79 void announcePositionToController(); // TODO: have to be public?
80 void processATC(const FGATCInstruction& instruction);
81 void setTaxiClearanceRequest(bool arg) { needsTaxiClearance = arg; };
82 bool getTaxiClearanceRequest() { return needsTaxiClearance; };
83 FGAISchedule* getTrafficRef() { return trafficRef; };
84 void setTrafficRef(FGAISchedule* ref) { trafficRef = ref; };
86
87 const std::string& GetTransponderCode() { return transponderCode; };
88 void SetTransponderCode(const std::string& tc) { transponderCode = tc; };
89
90 // included as performance data needs them, who else?
91 inline PerformanceData* getPerformance() { return _performance; };
92 inline bool onGround() const { return no_roll; };
93 inline double getSpeed() const { return speed; };
94 inline double getRoll() const { return roll; };
95 inline double getPitch() const { return pitch; };
96 inline double getAltitude() const { return altitude_ft; };
97 inline double getVerticalSpeedFPM() const { return vs_fps * 60; };
98 inline double altitudeAGL() const { return props->getFloatValue("position/altitude-agl-ft"); };
99 inline double airspeed() const { return props->getFloatValue("velocities/airspeed-kt"); };
100 const std::string& atGate();
101 std::string acwakecategory;
102
103 void checkTcas();
104 double calcVerticalSpeed(double vert_ft, double dist_m, double speed, double error);
105
106 FGATCController* getATCController() { return controller; };
107
108 void clearATCController();
109 bool isBlockedBy(FGAIAircraft* other);
110 void dumpCSVHeader(const std::unique_ptr<sg_ofstream>& o);
111 void dumpCSV(const std::unique_ptr<sg_ofstream>& o, int lineIndex);
112
113protected:
114 void Run(double dt);
115
116private:
117 FGAISchedule* trafficRef;
118 FGATCController *controller,
119 *prevController,
120 *towerController; // Only needed to make a pre-announcement
121
122 bool hdg_lock;
123 bool alt_lock;
124 double dt_count;
125 double dt_elev_count;
126 double headingChangeRate;
127 double headingError;
128 double minBearing;
129 double speedFraction;
130
132 double groundTargetSpeed;
133 double groundOffset;
134
135 bool use_perf_vs;
136 SGPropertyNode_ptr refuel_node;
137 SGPropertyNode_ptr tcasThreatNode;
138 SGPropertyNode_ptr tcasRANode;
139
140 // helpers for Run
141 // TODO: sort out which ones are better protected virtuals to allow
142 //subclasses to override specific behaviour
143 bool fpExecutable(time_t now);
144 void handleFirstWaypoint(void);
145 bool leadPointReached(FGAIWaypoint* curr, FGAIWaypoint* next, int nextTurnAngle);
146 bool handleAirportEndPoints(FGAIWaypoint* prev, time_t now);
147 bool reachedEndOfCruise(double&);
148 bool aiTrafficVisible(void);
149 void controlHeading(FGAIWaypoint* curr,
150 FGAIWaypoint* next);
151 void controlSpeed(FGAIWaypoint* curr, FGAIWaypoint* next);
152
153 void updatePrimaryTargetValues(double dt, bool& flightplanActive, bool& aiOutOfSight);
154 void updateSecondaryTargetValues(double dt);
155 void updateHeading(double dt);
156 void updateBankAngleTarget();
157 void updateVerticalSpeedTarget(double dt);
158 void updatePitchAngleTarget();
159 void updateActualState(double dt);
160 void updateModelProperties(double dt);
162 void updateUserFlightPlan(double dt);
163
165 int determineNextLeg(int leg);
166 void handleATCRequests(double dt);
167
168 inline bool isStationary()
169 {
170 return ((fabs(speed) <= 0.0001) && (fabs(tgt_speed) <= 0.0001));
171 }
172
173 inline bool needGroundElevation()
174 {
175 if (!isStationary())
176 _needsGroundElevation = true;
177 return _needsGroundElevation;
178 }
179
180 double sign(double x);
181
182#if 0
183 std::string getTimeString(int timeOffset);
184#endif
185
186 void lazyInitControlsNodes();
187
188 std::string acType;
189 std::string company;
190 std::string transponderCode;
191
192 int spinCounter;
193
195 const int AI_STUCK_LIMIT = 100000;
196 int stuckCounter = 0;
197 bool tracked = false;
202 bool repositioned = false;
203 double prevSpeed;
204 double prev_dist_to_go;
205
206 bool holdPos = false;
207 int waitsForId = 0;
208
209 const char* _getTransponderCode() const;
210
211 bool needsTaxiClearance = false;
212 bool _needsGroundElevation = true;
213 time_t timeElapsed{0};
214
215 PerformanceData* _performance; // the performance data for this aircraft
216
217#if 0
218 void assertSpeed(double speed);
219#endif
220
221 struct
222 {
224 std::string startWptName;
225 std::string finalWptName;
226 } trackCache;
227
228 // these are init-ed on first use by lazyInitControlsNodes()
229 SGPropertyNode_ptr _controlsLateralModeNode,
230 _controlsVerticalModeNode,
231 _controlsTargetHeadingNode,
232 _controlsTargetRollNode,
233 _controlsTargetAltitude,
234 _controlsTargetPitch,
235 _controlsTargetSpeed;
236
237 std::unique_ptr<sg_ofstream> csvFile;
238 long csvIndex{0};
239};
double altitude
Definition ADA.cxx:46
void dumpCSVHeader(const std::unique_ptr< sg_ofstream > &o)
void clearATCController()
void setPerformance(const std::string &acType, const std::string &perfString)
double getVerticalSpeedFPM() const
double getBearing(double crse)
Returns a normalised bearing.
std::string startWptName
double getRoll() const
PerformanceData * getPerformance()
void setTrafficRef(FGAISchedule *ref)
FGAIAircraft(FGAISchedule *ref=0)
void dumpCSV(const std::unique_ptr< sg_ofstream > &o, int lineIndex)
void getGroundElev(double dt)
time_t checkForArrivalTime(const std::string &wptName)
void setAcType(const std::string &ac)
void Run(double dt)
virtual ~FGAIAircraft()
double getAltitude() const
FGAIFlightPlan * GetFlightPlan() const
std::string_view getTypeString(void) const override
double remainingLength
double getSpeed() const
bool loadNextLeg(double dist=0)
double getPitch() const
void TurnTo(double heading)
double airspeed() const
void doGroundAltitude()
void ProcessFlightPlan(double dt, time_t now)
FGATCController * getATCController()
double altitudeAGL() const
void unbind() override
void processATC(const FGATCInstruction &instruction)
Process ATC instructions and report back.
const std::string & getCompany() const
void PitchTo(double angle)
void resetPositionFromFlightPlan()
void AccelTo(double speed)
void setTaxiClearanceRequest(bool arg)
bool onGround() const
FGAISchedule * getTrafficRef()
void RollTo(double angle)
void setCompany(const std::string &comp)
void update(double dt) override
const std::string & getAcType() const
std::string acwakecategory
double calcVerticalSpeed(double vert_ft, double dist_m, double speed, double error)
bool getTaxiClearanceRequest()
const std::string & GetTransponderCode()
bool isBlockedBy(FGAIAircraft *other)
void SetTransponderCode(const std::string &tc)
void bind() override
void ClimbTo(double altitude)
std::string finalWptName
void readFromScenario(SGPropertyNode *scFileNode) override
time_t calcDeparture()
void announcePositionToController()
const std::string & atGate()
void setFlightPlan(const std::string &fp, bool repat=false)
void scheduleForATCTowerRunwayControl()
FGAIBaseAircraft(object_type otype=object_type::otAircraft)
double altitude_ft
Definition AIBase.hxx:218
double speed
Definition AIBase.hxx:216
std::unique_ptr< FGAIFlightPlan > fp
Definition AIBase.hxx:264
double vs_fps
Definition AIBase.hxx:219
double tgt_speed
Definition AIBase.hxx:231
double pitch
Definition AIBase.hxx:215
double roll
Definition AIBase.hxx:214
SGPropertyNode_ptr props
Definition AIBase.hxx:205
bool no_roll
Definition AIBase.hxx:257
class FGATCController NOTE: this class serves as an abstraction layer for all sorts of ATC controller...
Data storage for aircraft performance data.