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