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 setHeading( double heading );
70 void setLatitude( double latitude );
71 void setLongitude( double longitude );
72 void setBank( double bank );
73 void setPitch( double newpitch );
74 void setRadius ( double radius );
75 void setXoffset( double x_offset );
76 void setYoffset( double y_offset );
77 void setZoffset( double z_offset );
78 void setPitchoffset( double x_offset );
79 void setRolloffset( double y_offset );
80 void setYawoffset( double z_offset );
81 void setServiceable ( bool serviceable );
82 void setDie( bool die );
83 void setCollisionData( bool i, double lat, double lon, double elev );
84 void setImpactData( bool d );
85 void setImpactLat( double lat );
86 void setImpactLon( double lon );
87 void setImpactElev( double e );
90 int _getSubID() const;
94 SGVec3d getCartPosAt(const SGVec3d& off) const;
95 SGVec3d getCartPos() const;
97 bool getGroundElevationM(const SGGeod& pos, double& elev,
98 const SGMaterial** material) const;
100 double _getCartPosX() const;
101 double _getCartPosY() const;
102 double _getCartPosZ() const;
108 double _pitch_offset;
122 SGPropertyNode_ptr props;
123 SGPropertyNode_ptr trigger_node;
124 SGPropertyNode_ptr model_removed; // where to report model removal
125 FGAIManager* manager;
127 // these describe the model's actual state
128 SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
129 double hdg; // True heading in degrees
130 double roll; // degrees, left is negative
131 double pitch; // degrees, nose-down is negative
132 double speed; // knots true airspeed
133 double altitude_ft; // feet above sea level
134 double vs; // vertical speed, feet per minute
135 double speed_north_deg_sec;
136 double speed_east_deg_sec;
137 double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
139 double ft_per_deg_lon;
140 double ft_per_deg_lat;
142 // these describe the model's desired state
143 double tgt_heading; // target heading, degrees true
144 double tgt_altitude_ft; // target altitude, *feet* above sea level
145 double tgt_speed; // target speed, KTAS
151 // these describe radar information for the user
152 bool in_range; // true if in range of the radar, otherwise false
153 double bearing; // true bearing from user to this model
154 double elevation; // elevation in degrees from user to this model
155 double range; // range from user to this model, nm
156 double rdot; // range rate, in knots
157 double horiz_offset; // look left/right from user to me, deg
158 double vert_offset; // look up/down from user to me, deg
159 double x_shift; // value used by radar display instrument
160 double y_shift; // value used by radar display instrument
161 double rotation; // value used by radar display instrument
162 double ht_diff; // value used by radar display instrument
164 string model_path; //Path to the 3D model
165 osg::ref_ptr<osg::Node> model; //The 3D model object
166 SGModelPlacement aip;
178 bool _impact_reported;
179 bool _collision_reported;
180 bool _expiry_reported;
186 double _impact_pitch;
188 double _impact_speed;
191 void CalculateMach();
192 double UpdateRadar(FGAIManager* manager);
194 static int _newAIModelID();
202 object_type getType();
204 virtual const char* getTypeString(void) const { return "null"; }
206 bool isa( object_type otype );
208 void _setVS_fps( double _vs );
209 void _setAltitude( double _alt );
210 void _setLongitude( double longitude );
211 void _setLatitude ( double latitude );
212 void _setSubID( int s );
215 double _getVS_fps() const;
216 double _getAltitude() const;
217 double _getLongitude() const;
218 double _getLatitude() const;
219 double _getElevationFt() const;
220 double _getRdot() const;
221 double _getH_offset() const;
222 double _getV_offset() const;
223 double _getX_shift() const;
224 double _getY_shift() const;
225 double _getRotation() const;
226 double _getSpeed() const;
227 double _getRoll() const;
228 double _getPitch() const;
229 double _getHeading() const;
230 double _get_speed_east_fps() const;
231 double _get_speed_north_fps() const;
232 double _get_SubPath() const;
233 double _getImpactLat() const;
234 double _getImpactLon() const;
235 double _getImpactElevFt() const;
236 double _getImpactHdg() const;
237 double _getImpactPitch() const;
238 double _getImpactRoll() const;
239 double _getImpactSpeed() const;
240 double _getXOffset() const;
241 double _getYOffset() const;
242 double _getZOffset() const;
243 //unsigned int _getCount() const;
245 bool _getServiceable() const;
246 bool _getFirstTime() const;
248 bool _getImpactData();
249 bool _getCollisionData();
250 bool _getExpiryData();
252 SGPropertyNode* _getProps() const;
254 const char* _getPath() const;
255 const char* _getSMPath() const;
256 const char* _getCallsign() const;
257 const char* _getTriggerNode() const;
258 const char* _getName() const;
259 const char* _getSubmodel() const;
262 // These are used in the Mach number calculations
265 double T; // temperature, degs farenheit
266 double p; // pressure lbs/sq ft
267 double a; // speed of sound at altitude (ft/s)
268 double Mach; // Mach number
270 static const double e;
271 static const double lbs_to_slugs;
273 inline double _getRange() { return range; };
274 inline double _getBearing() { return bearing; };
276 virtual osg::Node* load3DModel(const string &path,
277 SGPropertyNode *prop_root);
279 static bool _isNight();
282 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
287 inline void FGAIBase::setPath(const char* model ) {
288 model_path.append(model);
291 inline void FGAIBase::setSMPath(const string& p) {
295 inline void FGAIBase::setServiceable(bool s) {
299 inline void FGAIBase::setSpeed( double speed_KTAS ) {
300 speed = tgt_speed = speed_KTAS;
303 inline void FGAIBase::setRadius( double radius ) {
304 turn_radius_ft = radius;
307 inline void FGAIBase::setHeading( double heading ) {
308 hdg = tgt_heading = heading;
311 inline void FGAIBase::setAltitude( double alt_ft ) {
312 altitude_ft = tgt_altitude_ft = alt_ft;
313 pos.setElevationFt(altitude_ft);
316 inline void FGAIBase::setBank( double bank ) {
317 roll = tgt_roll = bank;
321 inline void FGAIBase::setPitch( double newpitch ) {
322 pitch = tgt_pitch = newpitch;
325 inline void FGAIBase::setLongitude( double longitude ) {
326 pos.setLongitudeDeg( longitude );
329 inline void FGAIBase::setLatitude ( double latitude ) {
330 pos.setLatitudeDeg( latitude );
333 inline void FGAIBase::setCallSign(const string& s) {
336 inline void FGAIBase::setXoffset(double x) {
340 inline void FGAIBase::setYoffset(double y) {
344 inline void FGAIBase::setZoffset(double z) {
348 inline void FGAIBase::setPitchoffset(double p) {
352 inline void FGAIBase::setRolloffset(double r) {
356 inline void FGAIBase::setYawoffset(double y) {
360 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
362 inline bool FGAIBase::getDie() { return delete_me; }
364 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
366 #endif // _FG_AIBASE_HXX