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