]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.hxx
0bdd6f85b0711ae990ed42e6700de7668e540876
[flightgear.git] / src / AIModel / AIBase.hxx
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
5 //
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.
10 //
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.
15 //
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.
19
20 #ifndef _FG_AIBASE_HXX
21 #define _FG_AIBASE_HXX
22
23 #include <string>
24 #include <list>
25
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>
33
34 #include <Main/fg_props.hxx>
35
36
37 SG_USING_STD(string);
38 SG_USING_STD(list);
39
40 class FGAIManager;
41 class FGAIFlightPlan;
42
43 class FGAIBase : public SGReferenced {
44
45 public:
46     enum object_type { otNull = 0, otAircraft, otShip, otCarrier, otBallistic,
47                        otRocket, otStorm, otThermal, otStatic, otWingman,
48                        otMultiplayer,
49                        MAX_OBJECTS };   // Needs to be last!!!
50
51     FGAIBase(object_type ot);
52     virtual ~FGAIBase();
53
54     virtual void readFromScenario(SGPropertyNode* scFileNode);
55
56     virtual bool init(bool search_in_AI_path=false);
57     virtual void update(double dt);
58     virtual void bind();
59     virtual void unbind();
60     virtual void reinit() {}
61
62     void setManager(FGAIManager* mgr, SGPropertyNode* p);
63     void setPath( const char* model );
64     void setSMPath( const string& p );
65     void setCallSign(const string& );
66     void setSpeed( double speed_KTAS );
67     void setAltitude( double altitude_ft );
68     void setHeading( double heading );
69     void setLatitude( double latitude );
70     void setLongitude( double longitude );
71     void setBank( double bank );
72     void setPitch( double newpitch );
73     void setRadius ( double radius );
74     void setXoffset( double x_offset );
75     void setYoffset( double y_offset );
76     void setZoffset( double z_offset );
77     void setPitchoffset( double x_offset );
78     void setRolloffset( double y_offset );
79     void setYawoffset( double z_offset );
80     void setServiceable ( bool serviceable );
81     void setDie( bool die );
82     void setCollisionData( bool i, double lat, double lon, double elev );
83     void setImpactData( bool d );
84     void setImpactLat( double lat );
85     void setImpactLon( double lon );
86     void setImpactElev( double e );
87
88     int getID() const;
89     int _getSubID() const;
90
91     bool getDie();
92
93     SGVec3d getCartPosAt(const SGVec3d& off) const;
94     SGVec3d getCartPos() const;
95
96     double _getCartPosX() const;
97     double _getCartPosY() const;
98     double _getCartPosZ() const;
99
100     double _x_offset;
101     double _y_offset;
102     double _z_offset;
103
104     double _pitch_offset;
105     double _roll_offset;
106     double _yaw_offset;
107
108     string _path;
109     string _callsign;
110     string _submodel;
111     string _name;
112
113     SGGeod userpos;
114
115
116 protected:
117
118     SGPropertyNode_ptr props;
119     SGPropertyNode_ptr trigger_node;
120     SGPropertyNode_ptr model_removed; // where to report model removal
121     FGAIManager* manager;
122
123     // these describe the model's actual state
124     SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
125     double hdg;         // True heading in degrees
126     double roll;        // degrees, left is negative
127     double pitch;       // degrees, nose-down is negative
128     double speed;       // knots true airspeed
129     double altitude_ft; // feet above sea level
130     double vs;          // vertical speed, feet per minute
131     double speed_north_deg_sec;
132     double speed_east_deg_sec;
133     double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
134
135     double ft_per_deg_lon;
136     double ft_per_deg_lat;
137
138     // these describe the model's desired state
139     double tgt_heading;     // target heading, degrees true
140     double tgt_altitude_ft; // target altitude, *feet* above sea level
141     double tgt_speed;       // target speed, KTAS
142     double tgt_roll;
143     double tgt_pitch;
144     double tgt_yaw;
145     double tgt_vs;
146
147     // these describe radar information for the user
148     bool in_range;       // true if in range of the radar, otherwise false
149     double bearing;      // true bearing from user to this model
150     double elevation;    // elevation in degrees from user to this model
151     double range;        // range from user to this model, nm
152     double rdot;         // range rate, in knots
153     double horiz_offset; // look left/right from user to me, deg
154     double vert_offset;  // look up/down from user to me, deg
155     double x_shift;      // value used by radar display instrument
156     double y_shift;      // value used by radar display instrument
157     double rotation;     // value used by radar display instrument
158     double ht_diff;              // value used by radar display instrument
159
160     string model_path;     //Path to the 3D model
161     osg::ref_ptr<osg::Node> model; //The 3D model object
162     SGModelPlacement aip;
163
164     bool delete_me;
165     bool invisible;
166     bool no_roll;
167     bool serviceable;
168     int _subID;
169
170     double life;
171
172     FGAIFlightPlan *fp;
173
174     bool _impact_reported;
175     bool _collision_reported;
176
177     double _impact_lat;
178     double _impact_lon;
179     double _impact_elev;
180     double _impact_hdg;
181     double _impact_pitch;
182     double _impact_roll;
183     double _impact_speed;
184
185     void Transform();
186     void CalculateMach();
187     double UpdateRadar(FGAIManager* manager);
188
189     static int _newAIModelID();
190
191 private:
192     int _refID;
193     object_type _otype;
194
195 public:
196     object_type getType();
197
198     virtual const char* getTypeString(void) const { return "null"; }
199
200     bool isa( object_type otype );
201
202     void _setVS_fps( double _vs );
203     void _setAltitude( double _alt );
204     void _setLongitude( double longitude );
205     void _setLatitude ( double latitude );
206     void _setSubID( int s );
207     void _setUserPos();
208
209     double _getVS_fps() const;
210     double _getAltitude() const;
211     double _getLongitude() const;
212     double _getLatitude() const;
213     double _getElevationFt() const;
214     double _getRdot() const;
215     double _getH_offset() const;
216     double _getV_offset() const;
217     double _getX_shift() const;
218     double _getY_shift() const;
219     double _getRotation() const;
220     double _getSpeed() const;
221     double _getRoll() const;
222     double _getPitch() const;
223     double _getHeading() const;
224     double _get_speed_east_fps() const;
225     double _get_speed_north_fps() const;
226     double _get_SubPath() const;
227     double _getImpactLat() const;
228     double _getImpactLon() const;
229     double _getImpactElevFt() const;
230     double _getImpactHdg() const;
231     double _getImpactPitch() const;
232     double _getImpactRoll() const;
233     double _getImpactSpeed() const;
234     double _getXOffset() const;
235     double _getYOffset() const;
236     double _getZOffset() const;
237     //unsigned int _getCount() const;
238
239     bool   _getServiceable() const;
240     bool   _getFirstTime() const;
241     bool   _getImpact();
242     bool   _getImpactData();
243     bool   _getCollisionData();
244
245     SGPropertyNode* _getProps() const;
246
247     const char* _getPath() const;
248     const char* _getSMPath() const;
249     const char* _getCallsign() const;
250     const char* _getTriggerNode() const;
251     const char* _getName() const;
252     const char* _getSubmodel() const;
253
254
255     // These are used in the Mach number calculations
256
257     double rho;
258     double T;                             // temperature, degs farenheit
259     double p;                             // pressure lbs/sq ft
260     double a;                             // speed of sound at altitude (ft/s)
261     double Mach;                          // Mach number
262
263     static const double e;
264     static const double lbs_to_slugs;
265
266     inline double _getRange() { return range; };
267     inline double _getBearing() { return bearing; };
268
269     osg::Node* load3DModel(const string& fg_root,
270                             const string &path,
271                             SGPropertyNode *prop_root,
272                             double sim_time_sec);
273
274     static bool _isNight();
275 };
276
277 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
278     manager = mgr;
279     props = p;
280 }
281
282 inline void FGAIBase::setPath(const char* model ) {
283     model_path.append(model);
284 }
285
286 inline void FGAIBase::setSMPath(const string& p) {
287     _path = p;
288 }
289
290 inline void FGAIBase::setServiceable(bool s) {
291     serviceable = s;
292 }
293
294 inline void FGAIBase::setSpeed( double speed_KTAS ) {
295     speed = tgt_speed = speed_KTAS;
296 }
297
298 inline void FGAIBase::setRadius( double radius ) {
299     turn_radius_ft = radius;
300 }
301
302 inline void FGAIBase::setHeading( double heading ) {
303     hdg = tgt_heading = heading;
304 }
305
306 inline void FGAIBase::setAltitude( double alt_ft ) {
307     altitude_ft = tgt_altitude_ft = alt_ft;
308     pos.setElevationFt(altitude_ft);
309 }
310
311 inline void FGAIBase::setBank( double bank ) {
312     roll = tgt_roll = bank;
313     no_roll = false;
314 }
315
316 inline void FGAIBase::setPitch( double newpitch ) {
317     pitch = tgt_pitch = newpitch;
318 }
319
320 inline void FGAIBase::setLongitude( double longitude ) {
321     pos.setLongitudeDeg( longitude );
322 }
323
324 inline void FGAIBase::setLatitude ( double latitude ) {
325     pos.setLatitudeDeg( latitude );
326 }
327
328 inline void FGAIBase::setCallSign(const string& s) {
329     _callsign = s;
330 }
331 inline void FGAIBase::setXoffset(double x) {
332     _x_offset = x;
333 }
334
335 inline void FGAIBase::setYoffset(double y) {
336     _y_offset = y;
337 }
338
339 inline void FGAIBase::setZoffset(double z) {
340     _z_offset = z;
341 }
342
343 inline void FGAIBase::setPitchoffset(double p) {
344     _pitch_offset = p;
345 }
346
347 inline void FGAIBase::setRolloffset(double r) {
348     _roll_offset = r;
349
350
351 inline void FGAIBase::setYawoffset(double y) {
352     _yaw_offset = y;
353 }
354
355 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
356
357 inline bool FGAIBase::getDie() { return delete_me; }
358
359 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
360
361 #endif  // _FG_AIBASE_HXX