]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.hxx
Make AI Objects of type Wingman, Escort, and GroundVehicle able to be stationed or...
[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/scene/model/placement.hxx>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/structure/SGSharedPtr.hxx>
31 #include <simgear/structure/SGReferenced.hxx>
32
33 #include <Main/fg_props.hxx>
34
35
36 using std::string;
37 using std::list;
38
39 class SGMaterial;
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, otGroundVehicle,
48         otEscort, 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 initModel(osg::Node *node);
58     virtual void update(double dt);
59     virtual void bind();
60     virtual void unbind();
61     virtual void reinit() {}
62
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);
91     bool setParentNode();
92
93     int getID() const;
94     int _getSubID() const;
95
96     bool getDie();
97
98     SGVec3d getCartPosAt(const SGVec3d& off) const;
99     SGVec3d getCartPos() const;
100
101     bool getGroundElevationM(const SGGeod& pos, double& elev,
102         const SGMaterial** material) const;
103
104     double _elevation_m;
105     const SGMaterial* _material;
106
107     double _getCartPosX() const;
108     double _getCartPosY() const;
109     double _getCartPosZ() const;
110
111     double _x_offset;
112     double _y_offset;
113     double _z_offset;
114
115     double _pitch_offset;
116     double _roll_offset;
117     double _yaw_offset;
118
119     string _path;
120     string _callsign;
121     string _submodel;
122     string _name;
123     string _parent;
124
125     SGGeod userpos;
126
127
128 protected:
129
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;
135
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;
148
149     double ft_per_deg_lon;
150     double ft_per_deg_lat;
151
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
156     double tgt_roll;
157     double tgt_pitch;
158     double tgt_yaw;
159     double tgt_vs;
160
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
173
174     string model_path;     //Path to the 3D model
175     osg::ref_ptr<osg::Node> model; //The 3D model object
176     SGModelPlacement aip;
177
178     bool delete_me;
179     bool invisible;
180     bool no_roll;
181     bool serviceable;
182     int _subID;
183
184     double life;
185
186     FGAIFlightPlan *fp;
187
188     bool _impact_reported;
189     bool _collision_reported;
190     bool _expiry_reported;
191
192     double _impact_lat;
193     double _impact_lon;
194     double _impact_elev;
195     double _impact_hdg;
196     double _impact_pitch;
197     double _impact_roll;
198     double _impact_speed;
199
200     void Transform();
201     void CalculateMach();
202     double UpdateRadar(FGAIManager* manager);
203
204     static int _newAIModelID();
205
206 private:
207     int _refID;
208     object_type _otype;
209     bool _initialized;
210
211 public:
212     object_type getType();
213
214     virtual const char* getTypeString(void) const { return "null"; }
215
216     bool isa( object_type otype );
217
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 );
223     void _setUserPos();
224
225     double _getAltitudeAGL(SGGeod inpos, double start);
226
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;
256
257     bool   _getServiceable() const;
258     bool   _getFirstTime() const;
259     bool   _getImpact();
260     bool   _getImpactData();
261     bool   _getCollisionData();
262     bool   _getExpiryData();
263
264     SGPropertyNode* _getProps() const;
265
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;
272
273
274     // These are used in the Mach number calculations
275
276     double rho;
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
281
282     static const double e;
283     static const double lbs_to_slugs;
284
285     inline double _getRange() { return range; };
286     inline double _getBearing() { return bearing; };
287
288     virtual osg::Node* load3DModel(const string &path,
289         SGPropertyNode *prop_root);
290
291     static bool _isNight();
292 };
293
294 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
295     manager = mgr;
296     props = p;
297 }
298
299 inline void FGAIBase::setPath(const char* model ) {
300     model_path.append(model);
301 }
302
303 inline void FGAIBase::setSMPath(const string& p) {
304     _path = p;
305 }
306
307 inline void FGAIBase::setServiceable(bool s) {
308     serviceable = s;
309 }
310
311 inline void FGAIBase::setSpeed( double speed_KTAS ) {
312     speed = tgt_speed = speed_KTAS;
313 }
314
315 inline void FGAIBase::setRadius( double radius ) {
316     turn_radius_ft = radius;
317 }
318
319 inline void FGAIBase::setHeading( double heading ) {
320     hdg = tgt_heading = heading;
321 }
322
323 inline void FGAIBase::setAltitude( double alt_ft ) {
324     altitude_ft = tgt_altitude_ft = alt_ft;
325     pos.setElevationFt(altitude_ft);
326 }
327
328 inline void FGAIBase::setAltitudeAGL( double alt_ft ) {
329     altitude_agl_ft = alt_ft;
330 }
331
332 inline void FGAIBase::setBank( double bank ) {
333     roll = tgt_roll = bank;
334     no_roll = false;
335 }
336
337 inline void FGAIBase::setPitch( double newpitch ) {
338     pitch = tgt_pitch = newpitch;
339 }
340
341 inline void FGAIBase::setLongitude( double longitude ) {
342     pos.setLongitudeDeg( longitude );
343 }
344
345 inline void FGAIBase::setLatitude ( double latitude ) {
346     pos.setLatitudeDeg( latitude );
347 }
348
349 inline void FGAIBase::setCallSign(const string& s) {
350     _callsign = s;
351 }
352 inline void FGAIBase::setXoffset(double x) {
353     _x_offset = x;
354 }
355
356 inline void FGAIBase::setYoffset(double y) {
357     _y_offset = y;
358 }
359
360 inline void FGAIBase::setZoffset(double z) {
361     _z_offset = z;
362 }
363
364 inline void FGAIBase::setPitchoffset(double p) {
365     _pitch_offset = p;
366 }
367
368 inline void FGAIBase::setRolloffset(double r) {
369     _roll_offset = r;
370
371
372 inline void FGAIBase::setYawoffset(double y) {
373     _yaw_offset = y;
374 }
375
376 inline void FGAIBase::setParentName(const string& p) {
377     _parent = p;
378 }
379
380 inline void FGAIBase::setName(const string& n) {
381     _name = n;
382 }
383
384 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
385
386 inline bool FGAIBase::getDie() { return delete_me; }
387
388 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
389
390 #endif  // _FG_AIBASE_HXX