]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.hxx
navradio: set receiver's signal-quality-norm to 0 when navaid station
[flightgear.git] / src / AIModel / AIBase.hxx
1 // FGAIBase.hxx - abstract base class for AI objects
2 // Written by David Culp, started Nov 2003, based on
3 // David Luff's FGAIEntity class.
4 // - davidculp2@comcast.net 
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20 #ifndef _FG_AIBASE_HXX
21 #define _FG_AIBASE_HXX
22
23 #include <string>
24 #include <list>
25
26 #include <simgear/constants.h>
27 #include <simgear/math/SGMath.hxx>
28 #include <simgear/scene/model/placement.hxx>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/structure/SGSharedPtr.hxx>
31 #include <simgear/structure/SGReferenced.hxx>
32 #include <simgear/sg_inlines.h>
33
34 #include <simgear/math/sg_geodesy.hxx>
35
36
37 #include <Main/fg_props.hxx>
38
39
40 using std::string;
41 using std::list;
42
43 class SGMaterial;
44 class FGAIManager;
45 class FGAIFlightPlan;
46
47 class FGAIBase : public SGReferenced {
48
49 public:
50     enum object_type { otNull = 0, otAircraft, otShip, otCarrier, otBallistic,
51         otRocket, otStorm, otThermal, otStatic, otWingman, otGroundVehicle,
52         otEscort, otMultiplayer,
53         MAX_OBJECTS };  // Needs to be last!!!
54
55     FGAIBase(object_type ot, bool enableHot);
56     virtual ~FGAIBase();
57
58     virtual void readFromScenario(SGPropertyNode* scFileNode);
59
60     virtual bool init(bool search_in_AI_path=false);
61     virtual void initModel(osg::Node *node);
62     virtual void update(double dt);
63     virtual void bind();
64     virtual void unbind();
65     virtual void reinit() {}
66
67     void updateLOD();
68     void setManager(FGAIManager* mgr, SGPropertyNode* p);
69     void setPath( const char* model );
70     void setSMPath( const string& p );
71     void setCallSign(const string& );
72     void setSpeed( double speed_KTAS );
73     void setAltitude( double altitude_ft );
74     void setAltitudeAGL( double altitude_agl_ft );
75     void setHeading( double heading );
76     void setLatitude( double latitude );
77     void setLongitude( double longitude );
78     void setBank( double bank );
79     void setPitch( double newpitch );
80     void setRadius ( double radius );
81     void setXoffset( double x_offset );
82     void setYoffset( double y_offset );
83     void setZoffset( double z_offset );
84     void setPitchoffset( double x_offset );
85     void setRolloffset( double y_offset );
86     void setYawoffset( double z_offset );
87     void setServiceable ( bool serviceable );
88     void setDie( bool die );
89     void setCollisionData( bool i, double lat, double lon, double elev );
90     void setImpactData( bool d );
91     void setImpactLat( double lat );
92     void setImpactLon( double lon );
93     void setImpactElev( double e );
94     void setParentName(const string& p);
95     void setName(const string& n);
96     void setMaxSpeed(double kts);
97
98     void calcRangeBearing(double lat, double lon, double lat2, double lon2,
99         double &range, double &bearing) const;
100     double calcRelBearingDeg(double bearing, double heading);
101     double calcTrueBearingDeg(double bearing, double heading);
102     double calcRecipBearingDeg(double bearing);
103
104     bool setParentNode();
105
106     int getID() const;
107     int _getSubID() const;
108
109     bool getDie();
110
111     SGVec3d getCartPosAt(const SGVec3d& off) const;
112     SGVec3d getCartPos() const;
113
114     bool getGroundElevationM(const SGGeod& pos, double& elev,
115         const SGMaterial** material) const;
116
117     double _elevation_m;
118     const SGMaterial* _material;
119
120     double _getCartPosX() const;
121     double _getCartPosY() const;
122     double _getCartPosZ() const;
123
124     double _x_offset;
125     double _y_offset;
126     double _z_offset;
127
128     double _pitch_offset;
129     double _roll_offset;
130     double _yaw_offset;
131
132     double _max_speed;
133
134     string _path;
135     string _callsign;
136     string _submodel;
137     std::string _name;
138     string _parent;
139
140     SGGeod userpos;
141
142
143 protected:
144
145     SGPropertyNode_ptr _selected_ac;
146     SGPropertyNode_ptr props;
147     SGPropertyNode_ptr trigger_node;
148     SGPropertyNode_ptr model_removed; // where to report model removal
149     FGAIManager* manager;
150
151     // these describe the model's actual state
152     SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
153     double hdg;         // True heading in degrees
154     double roll;        // degrees, left is negative
155     double pitch;       // degrees, nose-down is negative
156     double speed;       // knots true airspeed
157     double altitude_ft; // feet above sea level
158     double vs;          // vertical speed, feet per minute
159     double speed_north_deg_sec;
160     double speed_east_deg_sec;
161     double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
162     double altitude_agl_ft;
163
164     double ft_per_deg_lon;
165     double ft_per_deg_lat;
166
167     // these describe the model's desired state
168     double tgt_heading;     // target heading, degrees true
169     double tgt_altitude_ft; // target altitude, *feet* above sea level
170     double tgt_speed;       // target speed, KTAS
171     double tgt_roll;
172     double tgt_pitch;
173     double tgt_yaw;
174     double tgt_vs;
175
176     // these describe radar information for the user
177     bool in_range;       // true if in range of the radar, otherwise false
178     double bearing;      // true bearing from user to this model
179     double elevation;    // elevation in degrees from user to this model
180     double range;        // range from user to this model, nm
181     double rdot;         // range rate, in knots
182     double horiz_offset; // look left/right from user to me, deg
183     double vert_offset;  // look up/down from user to me, deg
184     double x_shift;      // value used by radar display instrument
185     double y_shift;      // value used by radar display instrument
186     double rotation;     // value used by radar display instrument
187     double ht_diff;              // value used by radar display instrument
188
189     string model_path;     //Path to the 3D model
190     SGModelPlacement aip;
191
192     bool delete_me;
193     bool invisible;
194     bool no_roll;
195     bool serviceable;
196     bool _installed;
197     int _subID;
198
199     double life;
200
201     FGAIFlightPlan *fp;
202
203     bool _impact_reported;
204     bool _collision_reported;
205     bool _expiry_reported;
206
207     double _impact_lat;
208     double _impact_lon;
209     double _impact_elev;
210     double _impact_hdg;
211     double _impact_pitch;
212     double _impact_roll;
213     double _impact_speed;
214
215     void Transform();
216     void CalculateMach();
217     double UpdateRadar(FGAIManager* manager);
218
219     void removeModel();
220
221     static int _newAIModelID();
222
223 private:
224     int _refID;
225     object_type _otype;
226     bool _initialized;
227     osg::ref_ptr<osg::LOD> _model; //The 3D model LOD object
228
229 public:
230     object_type getType();
231
232     virtual const char* getTypeString(void) const { return "null"; }
233
234     bool isa( object_type otype );
235
236     void _setVS_fps( double _vs );
237     void _setAltitude( double _alt );
238     void _setLongitude( double longitude );
239     void _setLatitude ( double latitude );
240     void _setSubID( int s );
241     void _setUserPos();
242
243     double _getAltitudeAGL(SGGeod inpos, double start);
244
245     double _getVS_fps() const;
246     double _getAltitude() const;
247     double _getLongitude() const;
248     double _getLatitude() const;
249     double _getElevationFt() const;
250     double _getRdot() const;
251     double _getH_offset() const;
252     double _getV_offset() const;
253     double _getX_shift() const;
254     double _getY_shift() const;
255     double _getRotation() const;
256     double _getSpeed() const;
257     double _getRoll() const;
258     double _getPitch() const;
259     double _getHeading() const;
260     double _get_speed_east_fps() const;
261     double _get_speed_north_fps() const;
262     double _get_SubPath() const;
263     double _getImpactLat() const;
264     double _getImpactLon() const;
265     double _getImpactElevFt() const;
266     double _getImpactHdg() const;
267     double _getImpactPitch() const;
268     double _getImpactRoll() const;
269     double _getImpactSpeed() const;
270     double _getXOffset() const;
271     double _getYOffset() const;
272     double _getZOffset() const;
273     //unsigned int _getCount() const;
274
275     bool   _getServiceable() const;
276     bool   _getFirstTime() const;
277     bool   _getImpact();
278     bool   _getImpactData();
279     bool   _getCollisionData();
280     bool   _getExpiryData();
281
282     SGPropertyNode* _getProps() const;
283
284     const char* _getPath() const;
285     const char* _getSMPath() const;
286     const char* _getCallsign() const;
287     const char* _getTriggerNode() const;
288     const char* _getName() const;
289     const char* _getSubmodel() const;
290
291
292     // These are used in the Mach number calculations
293
294     double rho;
295     double T;                             // temperature, degs farenheit
296     double p;                             // pressure lbs/sq ft
297     double a;                             // speed of sound at altitude (ft/s)
298     double Mach;                          // Mach number
299
300     static const double e;
301     static const double lbs_to_slugs;
302
303     inline double _getRange() { return range; };
304     inline double _getBearing() { return bearing; };
305
306     static bool _isNight();
307
308      string & getCallSign();
309 };
310
311 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
312     manager = mgr;
313     props = p;
314 }
315
316 inline void FGAIBase::setPath(const char* model ) {
317     model_path.append(model);
318 }
319
320 inline void FGAIBase::setSMPath(const string& p) {
321     _path = p;
322 }
323
324 inline void FGAIBase::setServiceable(bool s) {
325     serviceable = s;
326 }
327
328 inline void FGAIBase::setSpeed( double speed_KTAS ) {
329     speed = tgt_speed = speed_KTAS;
330 }
331
332 inline void FGAIBase::setRadius( double radius ) {
333     turn_radius_ft = radius;
334 }
335
336 inline void FGAIBase::setHeading( double heading ) {
337     hdg = tgt_heading = heading;
338 }
339
340 inline void FGAIBase::setAltitude( double alt_ft ) {
341     altitude_ft = tgt_altitude_ft = alt_ft;
342     pos.setElevationFt(altitude_ft);
343 }
344
345 inline void FGAIBase::setAltitudeAGL( double alt_ft ) {
346     altitude_agl_ft = alt_ft;
347 }
348
349 inline void FGAIBase::setBank( double bank ) {
350     roll = tgt_roll = bank;
351     no_roll = false;
352 }
353
354 inline void FGAIBase::setPitch( double newpitch ) {
355     pitch = tgt_pitch = newpitch;
356 }
357
358 inline void FGAIBase::setLongitude( double longitude ) {
359     pos.setLongitudeDeg( longitude );
360 }
361
362 inline void FGAIBase::setLatitude ( double latitude ) {
363     pos.setLatitudeDeg( latitude );
364 }
365
366 inline void FGAIBase::setCallSign(const string& s) {
367     _callsign = s;
368 }
369 inline string& FGAIBase::getCallSign() {
370     return _callsign;
371 }
372
373 inline void FGAIBase::setXoffset(double x) {
374     _x_offset = x;
375 }
376
377 inline void FGAIBase::setYoffset(double y) {
378     _y_offset = y;
379 }
380
381 inline void FGAIBase::setZoffset(double z) {
382     _z_offset = z;
383 }
384
385 inline void FGAIBase::setPitchoffset(double p) {
386     _pitch_offset = p;
387 }
388
389 inline void FGAIBase::setRolloffset(double r) {
390     _roll_offset = r;
391
392
393 inline void FGAIBase::setYawoffset(double y) {
394     _yaw_offset = y;
395 }
396
397 inline void FGAIBase::setParentName(const string& p) {
398     _parent = p;
399 }
400
401 inline void FGAIBase::setName(const string& n) {
402     _name = n;
403 }
404
405 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
406
407 inline bool FGAIBase::getDie() { return delete_me; }
408
409 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
410
411 inline void FGAIBase::calcRangeBearing(double lat, double lon, double lat2, double lon2,
412                                   double &range, double &bearing) const
413 {
414     // calculate the bearing and range of the second pos from the first
415     double az2, distance;
416     geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
417     range = distance * SG_METER_TO_NM;
418 }
419
420 inline double FGAIBase::calcRelBearingDeg(double bearing, double heading){
421     double angle = bearing - heading;
422     SG_NORMALIZE_RANGE(angle, -180.0, 180.0);
423     return angle;
424 }
425
426 inline double FGAIBase::calcTrueBearingDeg(double bearing, double heading){
427     double angle = bearing + heading;
428     SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
429     return angle;
430 }
431
432 inline double FGAIBase::calcRecipBearingDeg(double bearing){
433     double angle = bearing - 180;
434     SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
435     return angle;
436 }
437
438 inline void FGAIBase::setMaxSpeed(double m) {
439     _max_speed = m;
440 }
441
442 #endif  // _FG_AIBASE_HXX