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
24 #include <osg/ref_ptr>
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>
34 #include <simgear/math/sg_geodesy.hxx>
36 namespace osg { class PagedLOD; }
44 class FGAIModelData; // defined below
47 class FGAIBase : public SGReferenced {
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!!!
55 FGAIBase(object_type ot, bool enableHot);
58 virtual void readFromScenario(SGPropertyNode* scFileNode);
60 virtual bool init(bool search_in_AI_path=false);
61 virtual void initModel();
62 virtual void update(double dt);
64 virtual void unbind();
65 virtual void reinit() {}
68 void updateInterior();
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);
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);
105 bool setParentNode();
108 int _getSubID() const;
113 SGVec3d getCartPosAt(const SGVec3d& off) const;
114 SGVec3d getCartPos() const;
116 bool getGroundElevationM(const SGGeod& pos, double& elev,
117 const simgear::BVHMaterial** material) const;
120 double _getCartPosX() const;
121 double _getCartPosY() const;
122 double _getCartPosZ() const;
127 double _maxRangeInterior;
133 double _pitch_offset;
140 std::string _callsign;
141 std::string _submodel;
146 * Tied-properties helper, record nodes which are tied for easy un-tie-ing
148 template <typename T>
149 void tie(const char* aRelPath, const SGRawValue<T>& aRawValue)
151 _tiedProperties.Tie(props->getNode(aRelPath, true), aRawValue);
154 simgear::TiedPropertyList _tiedProperties;
155 SGPropertyNode_ptr _selected_ac;
156 SGPropertyNode_ptr props;
157 SGPropertyNode_ptr trigger_node;
158 SGPropertyNode_ptr model_removed; // where to report model removal
159 FGAIManager* manager;
161 // these describe the model's actual state
162 SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
163 double hdg; // True heading in degrees
164 double roll; // degrees, left is negative
165 double pitch; // degrees, nose-down is negative
166 double speed; // knots true airspeed
167 double altitude_ft; // feet above sea level
168 double vs; // vertical speed, feet per minute
169 double speed_north_deg_sec;
170 double speed_east_deg_sec;
171 double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
172 double altitude_agl_ft;
174 double ft_per_deg_lon;
175 double ft_per_deg_lat;
177 // these describe the model's desired state
178 double tgt_heading; // target heading, degrees true
179 double tgt_altitude_ft; // target altitude, *feet* above sea level
180 double tgt_speed; // target speed, KTAS
186 // these describe radar information for the user
187 bool in_range; // true if in range of the radar, otherwise false
188 double bearing; // true bearing from user to this model
189 double elevation; // elevation in degrees from user to this model
190 double range; // range from user to this model, nm
191 double rdot; // range rate, in knots
192 double horiz_offset; // look left/right from user to me, deg
193 double vert_offset; // look up/down from user to me, deg
194 double x_shift; // value used by radar display instrument
195 double y_shift; // value used by radar display instrument
196 double rotation; // value used by radar display instrument
197 double ht_diff; // value used by radar display instrument
199 std::string model_path; //Path to the 3D model
200 SGModelPlacement aip;
213 bool _impact_reported;
214 bool _collision_reported;
215 bool _expiry_reported;
221 double _impact_pitch;
223 double _impact_speed;
226 void CalculateMach();
227 double UpdateRadar(FGAIManager* manager);
230 void removeSoundFx();
232 static int _newAIModelID();
238 osg::ref_ptr<osg::PagedLOD> _model;
239 osg::ref_ptr<osg::PagedLOD> _interior;
241 osg::ref_ptr<FGAIModelData> _modeldata;
243 SGSharedPtr<FGFX> _fx;
246 object_type getType();
248 virtual const char* getTypeString(void) const { return "null"; }
250 bool isa( object_type otype );
252 void _setVS_fps( double _vs );
253 void _setAltitude( double _alt );
254 void _setLongitude( double longitude );
255 void _setLatitude ( double latitude );
256 void _setSubID( int s );
258 double _getAltitudeAGL(SGGeod inpos, double start);
260 double _getVS_fps() const;
261 double _getAltitude() const;
262 double _getLongitude() const;
263 double _getLatitude() const;
264 double _getElevationFt() const;
265 double _getRdot() const;
266 double _getH_offset() const;
267 double _getV_offset() const;
268 double _getX_shift() const;
269 double _getY_shift() const;
270 double _getRotation() const;
271 double _getSpeed() const;
272 double _getRoll() const;
273 double _getPitch() const;
274 double _getHeading() const;
275 double _get_speed_east_fps() const;
276 double _get_speed_north_fps() const;
277 double _get_SubPath() const;
278 double _getImpactLat() const;
279 double _getImpactLon() const;
280 double _getImpactElevFt() const;
281 double _getImpactHdg() const;
282 double _getImpactPitch() const;
283 double _getImpactRoll() const;
284 double _getImpactSpeed() const;
285 double _getXOffset() const;
286 double _getYOffset() const;
287 double _getZOffset() const;
288 //unsigned int _getCount() const;
290 bool _getServiceable() const;
291 bool _getFirstTime() const;
293 bool _getImpactData();
294 bool _getCollisionData();
295 bool _getExpiryData();
297 SGPropertyNode* _getProps() const;
299 const char* _getPath() const;
300 const char* _getSMPath() const;
301 const char* _getCallsign() const;
302 const char* _getTriggerNode() const;
303 const char* _getName() const;
304 const char* _getSubmodel() const;
307 // These are used in the Mach number calculations
310 double T; // temperature, degs farenheit
311 double p; // pressure lbs/sq ft
312 double a; // speed of sound at altitude (ft/s)
313 double Mach; // Mach number
315 static const double e;
316 static const double lbs_to_slugs;
318 inline double _getRange() { return range; };
319 inline double _getBearing() { return bearing; };
321 static bool _isNight();
323 std::string & getCallSign();
326 typedef SGSharedPtr<FGAIBase> FGAIBasePtr;
328 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
333 inline void FGAIBase::setPath(const char* model ) {
334 model_path.append(model);
337 inline void FGAIBase::setSMPath(const std::string& p) {
341 inline void FGAIBase::setServiceable(bool s) {
345 inline void FGAIBase::setSpeed( double speed_KTAS ) {
346 speed = tgt_speed = speed_KTAS;
349 inline void FGAIBase::setRadius( double radius ) {
350 turn_radius_ft = radius;
353 inline void FGAIBase::setHeading( double heading ) {
354 hdg = tgt_heading = heading;
357 inline void FGAIBase::setAltitude( double alt_ft ) {
358 altitude_ft = tgt_altitude_ft = alt_ft;
359 pos.setElevationFt(altitude_ft);
362 inline void FGAIBase::setAltitudeAGL( double alt_ft ) {
363 altitude_agl_ft = alt_ft;
366 inline void FGAIBase::setBank( double bank ) {
367 roll = tgt_roll = bank;
371 inline void FGAIBase::setPitch( double newpitch ) {
372 pitch = tgt_pitch = newpitch;
375 inline void FGAIBase::setLongitude( double longitude ) {
376 pos.setLongitudeDeg( longitude );
379 inline void FGAIBase::setLatitude ( double latitude ) {
380 pos.setLatitudeDeg( latitude );
383 inline void FGAIBase::setCallSign(const std::string& s) {
386 inline std::string& FGAIBase::getCallSign() {
390 inline void FGAIBase::setXoffset(double x) {
394 inline void FGAIBase::setYoffset(double y) {
398 inline void FGAIBase::setZoffset(double z) {
402 inline void FGAIBase::setPitchoffset(double p) {
406 inline void FGAIBase::setRolloffset(double r) {
410 inline void FGAIBase::setYawoffset(double y) {
414 inline void FGAIBase::setParentName(const std::string& p) {
418 inline void FGAIBase::setName(const std::string& n) {
422 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
424 inline bool FGAIBase::getDie() { return delete_me; }
426 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
428 inline void FGAIBase::calcRangeBearing(double lat, double lon, double lat2, double lon2,
429 double &range, double &bearing) const
431 // calculate the bearing and range of the second pos from the first
432 double az2, distance;
433 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
434 range = distance * SG_METER_TO_NM;
437 inline double FGAIBase::calcRelBearingDeg(double bearing, double heading){
438 double angle = bearing - heading;
439 SG_NORMALIZE_RANGE(angle, -180.0, 180.0);
443 inline double FGAIBase::calcTrueBearingDeg(double bearing, double heading){
444 double angle = bearing + heading;
445 SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
449 inline double FGAIBase::calcRecipBearingDeg(double bearing){
450 double angle = bearing - 180;
451 SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
455 inline void FGAIBase::setMaxSpeed(double m) {
460 #endif // _FG_AIBASE_HXX