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