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