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
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.
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.
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.
20 #ifndef _FG_AIBASE_HXX
21 #define _FG_AIBASE_HXX
26 #include <simgear/constants.h>
27 #include <simgear/math/SGMath.hxx>
28 #include <simgear/scene/model/placement.hxx>
29 #include <simgear/scene/model/modellib.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/structure/SGSharedPtr.hxx>
32 #include <simgear/structure/SGReferenced.hxx>
33 #include <simgear/sg_inlines.h>
35 #include <simgear/math/sg_geodesy.hxx>
37 #include <Main/fg_props.hxx>
47 class FGNasalModelData;
48 class FGAIModelData; // defined below
51 class FGAIBase : public SGReferenced {
54 enum object_type { otNull = 0, otAircraft, otShip, otCarrier, otBallistic,
55 otRocket, otStorm, otThermal, otStatic, otWingman, otGroundVehicle,
56 otEscort, otMultiplayer,
57 MAX_OBJECTS }; // Needs to be last!!!
59 FGAIBase(object_type ot, bool enableHot);
62 virtual void readFromScenario(SGPropertyNode* scFileNode);
64 virtual bool init(bool search_in_AI_path=false);
65 virtual void initModel(osg::Node *node);
66 virtual void update(double dt);
68 virtual void unbind();
69 virtual void reinit() {}
72 void setManager(FGAIManager* mgr, SGPropertyNode* p);
73 void setPath( const char* model );
74 void setSMPath( const string& p );
75 void setCallSign(const string& );
76 void setSpeed( double speed_KTAS );
77 void setAltitude( double altitude_ft );
78 void setAltitudeAGL( double altitude_agl_ft );
79 void setHeading( double heading );
80 void setLatitude( double latitude );
81 void setLongitude( double longitude );
82 void setBank( double bank );
83 void setPitch( double newpitch );
84 void setRadius ( double radius );
85 void setXoffset( double x_offset );
86 void setYoffset( double y_offset );
87 void setZoffset( double z_offset );
88 void setPitchoffset( double x_offset );
89 void setRolloffset( double y_offset );
90 void setYawoffset( double z_offset );
91 void setServiceable ( bool serviceable );
92 void setDie( bool die );
93 void setCollisionData( bool i, double lat, double lon, double elev );
94 void setImpactData( bool d );
95 void setImpactLat( double lat );
96 void setImpactLon( double lon );
97 void setImpactElev( double e );
98 void setParentName(const string& p);
99 void setName(const string& n);
100 void setMaxSpeed(double kts);
102 void calcRangeBearing(double lat, double lon, double lat2, double lon2,
103 double &range, double &bearing) const;
104 double calcRelBearingDeg(double bearing, double heading);
105 double calcTrueBearingDeg(double bearing, double heading);
106 double calcRecipBearingDeg(double bearing);
108 bool setParentNode();
111 int _getSubID() const;
115 SGVec3d getCartPosAt(const SGVec3d& off) const;
116 SGVec3d getCartPos() const;
118 bool getGroundElevationM(const SGGeod& pos, double& elev,
119 const SGMaterial** material) const;
122 const SGMaterial* _material;
124 double _getCartPosX() const;
125 double _getCartPosY() const;
126 double _getCartPosZ() const;
132 double _pitch_offset;
149 SGPropertyNode_ptr _selected_ac;
150 SGPropertyNode_ptr props;
151 SGPropertyNode_ptr trigger_node;
152 SGPropertyNode_ptr model_removed; // where to report model removal
153 FGAIManager* manager;
155 // these describe the model's actual state
156 SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
157 double hdg; // True heading in degrees
158 double roll; // degrees, left is negative
159 double pitch; // degrees, nose-down is negative
160 double speed; // knots true airspeed
161 double altitude_ft; // feet above sea level
162 double vs; // vertical speed, feet per minute
163 double speed_north_deg_sec;
164 double speed_east_deg_sec;
165 double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
166 double altitude_agl_ft;
168 double ft_per_deg_lon;
169 double ft_per_deg_lat;
171 // these describe the model's desired state
172 double tgt_heading; // target heading, degrees true
173 double tgt_altitude_ft; // target altitude, *feet* above sea level
174 double tgt_speed; // target speed, KTAS
180 // these describe radar information for the user
181 bool in_range; // true if in range of the radar, otherwise false
182 double bearing; // true bearing from user to this model
183 double elevation; // elevation in degrees from user to this model
184 double range; // range from user to this model, nm
185 double rdot; // range rate, in knots
186 double horiz_offset; // look left/right from user to me, deg
187 double vert_offset; // look up/down from user to me, deg
188 double x_shift; // value used by radar display instrument
189 double y_shift; // value used by radar display instrument
190 double rotation; // value used by radar display instrument
191 double ht_diff; // value used by radar display instrument
193 string model_path; //Path to the 3D model
194 SGModelPlacement aip;
207 bool _impact_reported;
208 bool _collision_reported;
209 bool _expiry_reported;
215 double _impact_pitch;
217 double _impact_speed;
220 void CalculateMach();
221 double UpdateRadar(FGAIManager* manager);
225 static int _newAIModelID();
231 osg::ref_ptr<osg::LOD> _model; //The 3D model LOD object
233 osg::ref_ptr<FGAIModelData> _aimodel;
236 SGSharedPtr<FGFX> _fx;
239 object_type getType();
241 virtual const char* getTypeString(void) const { return "null"; }
243 bool isa( object_type otype );
245 void _setVS_fps( double _vs );
246 void _setAltitude( double _alt );
247 void _setLongitude( double longitude );
248 void _setLatitude ( double latitude );
249 void _setSubID( int s );
252 double _getAltitudeAGL(SGGeod inpos, double start);
254 double _getVS_fps() const;
255 double _getAltitude() const;
256 double _getLongitude() const;
257 double _getLatitude() const;
258 double _getElevationFt() const;
259 double _getRdot() const;
260 double _getH_offset() const;
261 double _getV_offset() const;
262 double _getX_shift() const;
263 double _getY_shift() const;
264 double _getRotation() const;
265 double _getSpeed() const;
266 double _getRoll() const;
267 double _getPitch() const;
268 double _getHeading() const;
269 double _get_speed_east_fps() const;
270 double _get_speed_north_fps() const;
271 double _get_SubPath() const;
272 double _getImpactLat() const;
273 double _getImpactLon() const;
274 double _getImpactElevFt() const;
275 double _getImpactHdg() const;
276 double _getImpactPitch() const;
277 double _getImpactRoll() const;
278 double _getImpactSpeed() const;
279 double _getXOffset() const;
280 double _getYOffset() const;
281 double _getZOffset() const;
282 //unsigned int _getCount() const;
284 bool _getServiceable() const;
285 bool _getFirstTime() const;
287 bool _getImpactData();
288 bool _getCollisionData();
289 bool _getExpiryData();
291 SGPropertyNode* _getProps() const;
293 const char* _getPath() const;
294 const char* _getSMPath() const;
295 const char* _getCallsign() const;
296 const char* _getTriggerNode() const;
297 const char* _getName() const;
298 const char* _getSubmodel() const;
301 // These are used in the Mach number calculations
304 double T; // temperature, degs farenheit
305 double p; // pressure lbs/sq ft
306 double a; // speed of sound at altitude (ft/s)
307 double Mach; // Mach number
309 static const double e;
310 static const double lbs_to_slugs;
312 inline double _getRange() { return range; };
313 inline double _getBearing() { return bearing; };
315 static bool _isNight();
317 string & getCallSign();
320 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
325 inline void FGAIBase::setPath(const char* model ) {
326 model_path.append(model);
329 inline void FGAIBase::setSMPath(const string& p) {
333 inline void FGAIBase::setServiceable(bool s) {
337 inline void FGAIBase::setSpeed( double speed_KTAS ) {
338 speed = tgt_speed = speed_KTAS;
341 inline void FGAIBase::setRadius( double radius ) {
342 turn_radius_ft = radius;
345 inline void FGAIBase::setHeading( double heading ) {
346 hdg = tgt_heading = heading;
349 inline void FGAIBase::setAltitude( double alt_ft ) {
350 altitude_ft = tgt_altitude_ft = alt_ft;
351 pos.setElevationFt(altitude_ft);
354 inline void FGAIBase::setAltitudeAGL( double alt_ft ) {
355 altitude_agl_ft = alt_ft;
358 inline void FGAIBase::setBank( double bank ) {
359 roll = tgt_roll = bank;
363 inline void FGAIBase::setPitch( double newpitch ) {
364 pitch = tgt_pitch = newpitch;
367 inline void FGAIBase::setLongitude( double longitude ) {
368 pos.setLongitudeDeg( longitude );
371 inline void FGAIBase::setLatitude ( double latitude ) {
372 pos.setLatitudeDeg( latitude );
375 inline void FGAIBase::setCallSign(const string& s) {
378 inline string& FGAIBase::getCallSign() {
382 inline void FGAIBase::setXoffset(double x) {
386 inline void FGAIBase::setYoffset(double y) {
390 inline void FGAIBase::setZoffset(double z) {
394 inline void FGAIBase::setPitchoffset(double p) {
398 inline void FGAIBase::setRolloffset(double r) {
402 inline void FGAIBase::setYawoffset(double y) {
406 inline void FGAIBase::setParentName(const string& p) {
410 inline void FGAIBase::setName(const string& n) {
414 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
416 inline bool FGAIBase::getDie() { return delete_me; }
418 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
420 inline void FGAIBase::calcRangeBearing(double lat, double lon, double lat2, double lon2,
421 double &range, double &bearing) const
423 // calculate the bearing and range of the second pos from the first
424 double az2, distance;
425 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
426 range = distance * SG_METER_TO_NM;
429 inline double FGAIBase::calcRelBearingDeg(double bearing, double heading){
430 double angle = bearing - heading;
431 SG_NORMALIZE_RANGE(angle, -180.0, 180.0);
435 inline double FGAIBase::calcTrueBearingDeg(double bearing, double heading){
436 double angle = bearing + heading;
437 SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
441 inline double FGAIBase::calcRecipBearingDeg(double bearing){
442 double angle = bearing - 180;
443 SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
447 inline void FGAIBase::setMaxSpeed(double m) {
452 class FGAIModelData : public simgear::SGModelData {
454 FGAIModelData(SGPropertyNode *root = 0);
456 void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n);
457 inline string& get_sound_path() { return _path; };
460 FGNasalModelData *_nasal;
464 #endif // _FG_AIBASE_HXX