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