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 setCallSign(const string& );
65 void setSpeed( double speed_KTAS );
66 void setAltitude( double altitude_ft );
67 void setHeading( double heading );
68 void setLatitude( double latitude );
69 void setLongitude( double longitude );
70 void setBank( double bank );
71 void setPitch( double newpitch );
72 void setRadius ( double radius );
73 void setXoffset( double x_offset );
74 void setYoffset( double y_offset );
75 void setZoffset( double z_offset );
76 void setServiceable ( bool serviceable );
77 void setDie( bool die );
78 void setCollisionData( bool i, double lat, double lon, double elev );
79 void setImpactData( bool d );
80 void setImpactLat( double lat );
81 void setImpactLon( double lon );
82 void setImpactElev( double e );
85 int _getSubID() const;
89 SGVec3d getCartPosAt(const SGVec3d& off) const;
90 SGVec3d getCartPos() const;
92 double _getCartPosX() const;
93 double _getCartPosY() const;
94 double _getCartPosZ() const;
103 SGPropertyNode_ptr props;
104 SGPropertyNode_ptr trigger_node;
105 SGPropertyNode_ptr model_removed; // where to report model removal
106 FGAIManager* manager;
108 // these describe the model's actual state
109 SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
110 double hdg; // True heading in degrees
111 double roll; // degrees, left is negative
112 double pitch; // degrees, nose-down is negative
113 double speed; // knots true airspeed
114 double altitude_ft; // feet above sea level
115 double vs; // vertical speed, feet per minute
116 double speed_north_deg_sec;
117 double speed_east_deg_sec;
118 double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
120 double ft_per_deg_lon;
121 double ft_per_deg_lat;
123 // these describe the model's desired state
124 double tgt_heading; // target heading, degrees true
125 double tgt_altitude_ft; // target altitude, *feet* above sea level
126 double tgt_speed; // target speed, KTAS
132 // these describe radar information for the user
133 bool in_range; // true if in range of the radar, otherwise false
134 double bearing; // true bearing from user to this model
135 double elevation; // elevation in degrees from user to this model
136 double range; // range from user to this model, nm
137 double rdot; // range rate, in knots
138 double horiz_offset; // look left/right from user to me, deg
139 double vert_offset; // look up/down from user to me, deg
140 double x_shift; // value used by radar display instrument
141 double y_shift; // value used by radar display instrument
142 double rotation; // value used by radar display instrument
143 double ht_diff; // value used by radar display instrument
145 string model_path; //Path to the 3D model
146 osg::ref_ptr<osg::Node> model; //The 3D model object
147 SGModelPlacement aip;
159 bool _impact_reported;
160 bool _collision_reported;
166 double _impact_pitch;
168 double _impact_speed;
171 void CalculateMach();
172 double UpdateRadar(FGAIManager* manager);
174 static int _newAIModelID();
181 object_type getType();
183 virtual const char* getTypeString(void) const { return "null"; }
185 bool isa( object_type otype );
187 void _setVS_fps( double _vs );
188 void _setAltitude( double _alt );
189 void _setLongitude( double longitude );
190 void _setLatitude ( double latitude );
191 void _setSubID( int s );
193 double _getVS_fps() const;
194 double _getAltitude() const;
195 double _getLongitude() const;
196 double _getLatitude() const;
197 double _getElevationFt() const;
198 double _getRdot() const;
199 double _getH_offset() const;
200 double _getV_offset() const;
201 double _getX_shift() const;
202 double _getY_shift() const;
203 double _getRotation() const;
204 double _getSpeed() const;
205 double _getRoll() const;
206 double _getPitch() const;
207 double _getHeading() const;
208 double _get_speed_east_fps() const;
209 double _get_speed_north_fps() const;
210 double _get_SubPath() const;
211 double _getImpactLat() const;
212 double _getImpactLon() const;
213 double _getImpactElevFt() const;
214 double _getImpactHdg() const;
215 double _getImpactPitch() const;
216 double _getImpactRoll() const;
217 double _getImpactSpeed() const;
219 //unsigned int _getCount() const;
221 bool _getServiceable() const;
222 bool _getFirstTime() const;
224 bool _getImpactData();
225 bool _getCollisionData();
227 SGPropertyNode* _getProps() const;
229 const char* _getPath() const;
230 const char* _getSMPath() const;
231 const char* _getCallsign() const;
232 const char* _getTriggerNode() const;
233 const char* _getName() const;
234 const char* _getSubmodel() const;
237 // These are used in the Mach number calculations
240 double T; // temperature, degs farenheit
241 double p; // pressure lbs/sq ft
242 double a; // speed of sound at altitude (ft/s)
243 double Mach; // Mach number
245 static const double e;
246 static const double lbs_to_slugs;
248 inline double _getRange() { return range; };
249 inline double _getBearing() { return bearing; };
251 osg::Node* load3DModel(const string& fg_root,
253 SGPropertyNode *prop_root,
254 double sim_time_sec);
256 static bool _isNight();
259 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
264 inline void FGAIBase::setPath(const char* model ) {
265 model_path.append(model);
268 inline void FGAIBase::setSMPath(const string& p) {
272 inline void FGAIBase::setServiceable(bool s) {
276 inline void FGAIBase::setSpeed( double speed_KTAS ) {
277 speed = tgt_speed = speed_KTAS;
280 inline void FGAIBase::setRadius( double radius ) {
281 turn_radius_ft = radius;
284 inline void FGAIBase::setHeading( double heading ) {
285 hdg = tgt_heading = heading;
288 inline void FGAIBase::setAltitude( double alt_ft ) {
289 altitude_ft = tgt_altitude_ft = alt_ft;
290 pos.setElevationFt(altitude_ft);
293 inline void FGAIBase::setBank( double bank ) {
294 roll = tgt_roll = bank;
298 inline void FGAIBase::setPitch( double newpitch ) {
299 pitch = tgt_pitch = newpitch;
302 inline void FGAIBase::setLongitude( double longitude ) {
303 pos.setLongitudeDeg( longitude );
306 inline void FGAIBase::setLatitude ( double latitude ) {
307 pos.setLatitudeDeg( latitude );
310 inline void FGAIBase::setCallSign(const string& s) {
315 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
317 inline bool FGAIBase::getDie() { return delete_me; }
319 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
321 #endif // _FG_AIBASE_HXX