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/misc/sg_path.hxx>
30 #include <simgear/structure/SGSharedPtr.hxx>
31 #include <simgear/structure/SGReferenced.hxx>
33 #include <Main/fg_props.hxx>
43 class FGAIBase : public SGReferenced {
46 enum object_type { otNull = 0, otAircraft, otShip, otCarrier, otBallistic,
47 otRocket, otStorm, otThermal, otStatic, otWingman, otGroundVehicle,
48 otEscort, otMultiplayer,
49 MAX_OBJECTS }; // Needs to be last!!!
51 FGAIBase(object_type ot);
54 virtual void readFromScenario(SGPropertyNode* scFileNode);
56 virtual bool init(bool search_in_AI_path=false);
57 virtual void initModel(osg::Node *node);
58 virtual void update(double dt);
60 virtual void unbind();
61 virtual void reinit() {}
63 void setManager(FGAIManager* mgr, SGPropertyNode* p);
64 void setPath( const char* model );
65 void setSMPath( const string& p );
66 void setCallSign(const string& );
67 void setSpeed( double speed_KTAS );
68 void setAltitude( double altitude_ft );
69 void setAltitudeAGL( double altitude_agl_ft );
70 void setHeading( double heading );
71 void setLatitude( double latitude );
72 void setLongitude( double longitude );
73 void setBank( double bank );
74 void setPitch( double newpitch );
75 void setRadius ( double radius );
76 void setXoffset( double x_offset );
77 void setYoffset( double y_offset );
78 void setZoffset( double z_offset );
79 void setPitchoffset( double x_offset );
80 void setRolloffset( double y_offset );
81 void setYawoffset( double z_offset );
82 void setServiceable ( bool serviceable );
83 void setDie( bool die );
84 void setCollisionData( bool i, double lat, double lon, double elev );
85 void setImpactData( bool d );
86 void setImpactLat( double lat );
87 void setImpactLon( double lon );
88 void setImpactElev( double e );
89 void setParentName(const string& p);
90 void setName(const string& n);
94 int _getSubID() const;
98 SGVec3d getCartPosAt(const SGVec3d& off) const;
99 SGVec3d getCartPos() const;
101 bool getGroundElevationM(const SGGeod& pos, double& elev,
102 const SGMaterial** material) const;
105 const SGMaterial* _material;
107 double _getCartPosX() const;
108 double _getCartPosY() const;
109 double _getCartPosZ() const;
115 double _pitch_offset;
130 SGPropertyNode_ptr _selected_ac;
131 SGPropertyNode_ptr props;
132 SGPropertyNode_ptr trigger_node;
133 SGPropertyNode_ptr model_removed; // where to report model removal
134 FGAIManager* manager;
136 // these describe the model's actual state
137 SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
138 double hdg; // True heading in degrees
139 double roll; // degrees, left is negative
140 double pitch; // degrees, nose-down is negative
141 double speed; // knots true airspeed
142 double altitude_ft; // feet above sea level
143 double vs; // vertical speed, feet per minute
144 double speed_north_deg_sec;
145 double speed_east_deg_sec;
146 double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
147 double altitude_agl_ft;
149 double ft_per_deg_lon;
150 double ft_per_deg_lat;
152 // these describe the model's desired state
153 double tgt_heading; // target heading, degrees true
154 double tgt_altitude_ft; // target altitude, *feet* above sea level
155 double tgt_speed; // target speed, KTAS
161 // these describe radar information for the user
162 bool in_range; // true if in range of the radar, otherwise false
163 double bearing; // true bearing from user to this model
164 double elevation; // elevation in degrees from user to this model
165 double range; // range from user to this model, nm
166 double rdot; // range rate, in knots
167 double horiz_offset; // look left/right from user to me, deg
168 double vert_offset; // look up/down from user to me, deg
169 double x_shift; // value used by radar display instrument
170 double y_shift; // value used by radar display instrument
171 double rotation; // value used by radar display instrument
172 double ht_diff; // value used by radar display instrument
174 string model_path; //Path to the 3D model
175 osg::ref_ptr<osg::Node> model; //The 3D model object
176 SGModelPlacement aip;
188 bool _impact_reported;
189 bool _collision_reported;
190 bool _expiry_reported;
196 double _impact_pitch;
198 double _impact_speed;
201 void CalculateMach();
202 double UpdateRadar(FGAIManager* manager);
204 static int _newAIModelID();
212 object_type getType();
214 virtual const char* getTypeString(void) const { return "null"; }
216 bool isa( object_type otype );
218 void _setVS_fps( double _vs );
219 void _setAltitude( double _alt );
220 void _setLongitude( double longitude );
221 void _setLatitude ( double latitude );
222 void _setSubID( int s );
225 double _getAltitudeAGL(SGGeod inpos, double start);
227 double _getVS_fps() const;
228 double _getAltitude() const;
229 double _getLongitude() const;
230 double _getLatitude() const;
231 double _getElevationFt() const;
232 double _getRdot() const;
233 double _getH_offset() const;
234 double _getV_offset() const;
235 double _getX_shift() const;
236 double _getY_shift() const;
237 double _getRotation() const;
238 double _getSpeed() const;
239 double _getRoll() const;
240 double _getPitch() const;
241 double _getHeading() const;
242 double _get_speed_east_fps() const;
243 double _get_speed_north_fps() const;
244 double _get_SubPath() const;
245 double _getImpactLat() const;
246 double _getImpactLon() const;
247 double _getImpactElevFt() const;
248 double _getImpactHdg() const;
249 double _getImpactPitch() const;
250 double _getImpactRoll() const;
251 double _getImpactSpeed() const;
252 double _getXOffset() const;
253 double _getYOffset() const;
254 double _getZOffset() const;
255 //unsigned int _getCount() const;
257 bool _getServiceable() const;
258 bool _getFirstTime() const;
260 bool _getImpactData();
261 bool _getCollisionData();
262 bool _getExpiryData();
264 SGPropertyNode* _getProps() const;
266 const char* _getPath() const;
267 const char* _getSMPath() const;
268 const char* _getCallsign() const;
269 const char* _getTriggerNode() const;
270 const char* _getName() const;
271 const char* _getSubmodel() const;
274 // These are used in the Mach number calculations
277 double T; // temperature, degs farenheit
278 double p; // pressure lbs/sq ft
279 double a; // speed of sound at altitude (ft/s)
280 double Mach; // Mach number
282 static const double e;
283 static const double lbs_to_slugs;
285 inline double _getRange() { return range; };
286 inline double _getBearing() { return bearing; };
288 virtual osg::Node* load3DModel(const string &path,
289 SGPropertyNode *prop_root);
291 static bool _isNight();
294 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
299 inline void FGAIBase::setPath(const char* model ) {
300 model_path.append(model);
303 inline void FGAIBase::setSMPath(const string& p) {
307 inline void FGAIBase::setServiceable(bool s) {
311 inline void FGAIBase::setSpeed( double speed_KTAS ) {
312 speed = tgt_speed = speed_KTAS;
315 inline void FGAIBase::setRadius( double radius ) {
316 turn_radius_ft = radius;
319 inline void FGAIBase::setHeading( double heading ) {
320 hdg = tgt_heading = heading;
323 inline void FGAIBase::setAltitude( double alt_ft ) {
324 altitude_ft = tgt_altitude_ft = alt_ft;
325 pos.setElevationFt(altitude_ft);
328 inline void FGAIBase::setAltitudeAGL( double alt_ft ) {
329 altitude_agl_ft = alt_ft;
332 inline void FGAIBase::setBank( double bank ) {
333 roll = tgt_roll = bank;
337 inline void FGAIBase::setPitch( double newpitch ) {
338 pitch = tgt_pitch = newpitch;
341 inline void FGAIBase::setLongitude( double longitude ) {
342 pos.setLongitudeDeg( longitude );
345 inline void FGAIBase::setLatitude ( double latitude ) {
346 pos.setLatitudeDeg( latitude );
349 inline void FGAIBase::setCallSign(const string& s) {
352 inline void FGAIBase::setXoffset(double x) {
356 inline void FGAIBase::setYoffset(double y) {
360 inline void FGAIBase::setZoffset(double z) {
364 inline void FGAIBase::setPitchoffset(double p) {
368 inline void FGAIBase::setRolloffset(double r) {
372 inline void FGAIBase::setYawoffset(double y) {
376 inline void FGAIBase::setParentName(const string& p) {
380 inline void FGAIBase::setName(const string& n) {
384 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
386 inline bool FGAIBase::getDie() { return delete_me; }
388 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
390 #endif // _FG_AIBASE_HXX