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