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