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