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/math/point3d.hxx>
29 #include <simgear/scene/model/placement.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/structure/SGSharedPtr.hxx>
32 #include <simgear/structure/SGReferenced.hxx>
34 #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, otMultiplayer,
48 MAX_OBJECTS }; // Needs to be last!!!
50 FGAIBase(object_type ot);
53 virtual void readFromScenario(SGPropertyNode* scFileNode);
55 virtual bool init(bool search_in_AI_path=false);
56 virtual void update(double dt);
58 virtual void unbind();
59 virtual void reinit() {}
61 void setManager(FGAIManager* mgr, SGPropertyNode* p);
62 void setPath( const char* model );
63 void setSMPath( const string& p );
64 void setSpeed( double speed_KTAS );
65 void setAltitude( double altitude_ft );
66 void setHeading( double heading );
67 void setLatitude( double latitude );
68 void setLongitude( double longitude );
69 void setBank( double bank );
70 void setPitch( double newpitch );
71 void setRadius ( double radius );
72 void setXoffset( double x_offset );
73 void setYoffset( double y_offset );
74 void setZoffset( double z_offset );
75 void setServiceable ( bool serviceable );
76 void setDie( bool die );
77 void setCollisionData( bool i, double lat, double lon, double elev );
78 void setImpactData( bool d );
79 void setImpactLat( double lat );
80 void setImpactLon( double lon );
81 void setImpactElev( double e );
84 int _getSubID() const;
88 SGVec3d getCartPosAt(const SGVec3d& off) const;
89 SGVec3d getCartPos() const;
91 double _getCartPosX() const;
92 double _getCartPosY() const;
93 double _getCartPosZ() const;
102 SGPropertyNode_ptr props;
103 SGPropertyNode_ptr trigger_node;
104 SGPropertyNode_ptr model_removed; // where to report model removal
105 FGAIManager* manager;
107 // these describe the model's actual state
108 SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
109 double hdg; // True heading in degrees
110 double roll; // degrees, left is negative
111 double pitch; // degrees, nose-down is negative
112 double speed; // knots true airspeed
113 double altitude_ft; // feet above sea level
114 double vs; // vertical speed, feet per minute
115 double speed_north_deg_sec;
116 double speed_east_deg_sec;
117 double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
119 double ft_per_deg_lon;
120 double ft_per_deg_lat;
122 // these describe the model's desired state
123 double tgt_heading; // target heading, degrees true
124 double tgt_altitude_ft; // target altitude, *feet* above sea level
125 double tgt_speed; // target speed, KTAS
131 // these describe radar information for the user
132 bool in_range; // true if in range of the radar, otherwise false
133 double bearing; // true bearing from user to this model
134 double elevation; // elevation in degrees from user to this model
135 double range; // range from user to this model, nm
136 double rdot; // range rate, in knots
137 double horiz_offset; // look left/right from user to me, deg
138 double vert_offset; // look up/down from user to me, deg
139 double x_shift; // value used by radar display instrument
140 double y_shift; // value used by radar display instrument
141 double rotation; // value used by radar display instrument
142 double ht_diff; // value used by radar display instrument
144 string model_path; //Path to the 3D model
145 osg::ref_ptr<osg::Node> model; //The 3D model object
146 SGModelPlacement aip;
158 bool _impact_reported;
159 bool _collision_reported;
165 double _impact_pitch;
167 double _impact_speed;
170 void CalculateMach();
171 double UpdateRadar(FGAIManager* manager);
173 static int _newAIModelID();
180 object_type getType();
182 virtual const char* getTypeString(void) const { return "null"; }
184 bool isa( object_type otype );
186 void _setVS_fps( double _vs );
187 void _setAltitude( double _alt );
188 void _setLongitude( double longitude );
189 void _setLatitude ( double latitude );
190 void _setSubID( int s );
192 double _getVS_fps() const;
193 double _getAltitude() const;
194 double _getLongitude() const;
195 double _getLatitude() const;
196 double _getElevationFt() const;
197 double _getRdot() const;
198 double _getH_offset() const;
199 double _getV_offset() const;
200 double _getX_shift() const;
201 double _getY_shift() const;
202 double _getRotation() const;
203 double _getSpeed() const;
204 double _getRoll() const;
205 double _getPitch() const;
206 double _getHeading() const;
207 double _get_speed_east_fps() const;
208 double _get_speed_north_fps() const;
209 double _get_SubPath() const;
210 double _getImpactLat() const;
211 double _getImpactLon() const;
212 double _getImpactElevFt() const;
213 double _getImpactHdg() const;
214 double _getImpactPitch() const;
215 double _getImpactRoll() const;
216 double _getImpactSpeed() const;
218 //unsigned int _getCount() const;
220 bool _getServiceable() const;
221 bool _getFirstTime() const;
223 bool _getImpactData();
224 bool _getCollisionData();
226 SGPropertyNode* _getProps() const;
228 const char* _getPath();
229 const char* _getCallsign();
230 const char* _getTriggerNode();
231 const char* _getName();
232 const char* _getSubmodel();
235 // These are used in the Mach number calculations
238 double T; // temperature, degs farenheit
239 double p; // pressure lbs/sq ft
240 double a; // speed of sound at altitude (ft/s)
241 double Mach; // Mach number
243 static const double e;
244 static const double lbs_to_slugs;
246 inline double _getRange() { return range; };
247 inline double _getBearing() { return bearing; };
249 osg::Node* load3DModel(const string& fg_root,
251 SGPropertyNode *prop_root,
252 double sim_time_sec);
254 static bool _isNight();
257 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
262 inline void FGAIBase::setPath(const char* model ) {
263 model_path.append(model);
266 inline void FGAIBase::setSMPath(const string& p) {
270 inline void FGAIBase::setServiceable(bool s) {
274 inline void FGAIBase::setSpeed( double speed_KTAS ) {
275 speed = tgt_speed = speed_KTAS;
278 inline void FGAIBase::setRadius( double radius ) {
279 turn_radius_ft = radius;
282 inline void FGAIBase::setHeading( double heading ) {
283 hdg = tgt_heading = heading;
286 inline void FGAIBase::setAltitude( double alt_ft ) {
287 altitude_ft = tgt_altitude_ft = alt_ft;
288 pos.setElevationFt(altitude_ft);
291 inline void FGAIBase::setBank( double bank ) {
292 roll = tgt_roll = bank;
296 inline void FGAIBase::setPitch( double newpitch ) {
297 pitch = tgt_pitch = newpitch;
300 inline void FGAIBase::setLongitude( double longitude ) {
301 pos.setLongitudeDeg( longitude );
304 inline void FGAIBase::setLatitude ( double latitude ) {
305 pos.setLatitudeDeg( latitude );
308 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
310 inline bool FGAIBase::getDie() { return delete_me; }
312 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
314 #endif // _FG_AIBASE_HXX