]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.hxx
Clear chat messages when an aircraft becomes inactive in the property tree.
[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, otMultiplayer,
48                        MAX_OBJECTS };   // Needs to be last!!!
49
50     FGAIBase(object_type ot);
51     virtual ~FGAIBase();
52
53     virtual void readFromScenario(SGPropertyNode* scFileNode);
54
55     virtual bool init(bool search_in_AI_path=false);
56     virtual void update(double dt);
57     virtual void bind();
58     virtual void unbind();
59     virtual void reinit() {}
60
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 );
83
84     int getID() const;
85     int _getSubID() const;
86
87     bool getDie();
88
89     SGVec3d getCartPosAt(const SGVec3d& off) const;
90     SGVec3d getCartPos() const;
91
92     double _getCartPosX() const;
93     double _getCartPosY() const;
94     double _getCartPosZ() const;
95
96     string _path;
97     string _callsign;
98     string _submodel;
99     string _name;
100
101 protected:
102
103     SGPropertyNode_ptr props;
104     SGPropertyNode_ptr trigger_node;
105     SGPropertyNode_ptr model_removed; // where to report model removal
106     FGAIManager* manager;
107
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
119
120     double ft_per_deg_lon;
121     double ft_per_deg_lat;
122
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
127     double tgt_roll;
128     double tgt_pitch;
129     double tgt_yaw;
130     double tgt_vs;
131
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
144
145     string model_path;     //Path to the 3D model
146     osg::ref_ptr<osg::Node> model; //The 3D model object
147     SGModelPlacement aip;
148
149     bool delete_me;
150     bool invisible;
151     bool no_roll;
152     bool serviceable;
153     int _subID;
154
155     double life;
156
157     FGAIFlightPlan *fp;
158
159     bool _impact_reported;
160     bool _collision_reported;
161
162     double _impact_lat;
163     double _impact_lon;
164     double _impact_elev;
165     double _impact_hdg;
166     double _impact_pitch;
167     double _impact_roll;
168     double _impact_speed;
169
170     void Transform();
171     void CalculateMach();
172     double UpdateRadar(FGAIManager* manager);
173
174     static int _newAIModelID();
175
176 private:
177     int _refID;
178     object_type _otype;
179
180 public:
181     object_type getType();
182
183     virtual const char* getTypeString(void) const { return "null"; }
184
185     bool isa( object_type otype );
186
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 );
192
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;
218
219     //unsigned int _getCount() const;
220
221     bool   _getServiceable() const;
222     bool   _getFirstTime() const;
223     bool   _getImpact();
224     bool   _getImpactData();
225     bool   _getCollisionData();
226
227     SGPropertyNode* _getProps() const;
228
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;
235
236
237     // These are used in the Mach number calculations
238
239     double rho;
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
244
245     static const double e;
246     static const double lbs_to_slugs;
247
248     inline double _getRange() { return range; };
249     inline double _getBearing() { return bearing; };
250
251     osg::Node* load3DModel(const string& fg_root,
252                             const string &path,
253                             SGPropertyNode *prop_root,
254                             double sim_time_sec);
255
256     static bool _isNight();
257 };
258
259 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
260     manager = mgr;
261     props = p;
262 }
263
264 inline void FGAIBase::setPath(const char* model ) {
265     model_path.append(model);
266 }
267
268 inline void FGAIBase::setSMPath(const string& p) {
269     _path = p;
270 }
271
272 inline void FGAIBase::setServiceable(bool s) {
273     serviceable = s;
274 }
275
276 inline void FGAIBase::setSpeed( double speed_KTAS ) {
277     speed = tgt_speed = speed_KTAS;
278 }
279
280 inline void FGAIBase::setRadius( double radius ) {
281     turn_radius_ft = radius;
282 }
283
284 inline void FGAIBase::setHeading( double heading ) {
285     hdg = tgt_heading = heading;
286 }
287
288 inline void FGAIBase::setAltitude( double alt_ft ) {
289     altitude_ft = tgt_altitude_ft = alt_ft;
290     pos.setElevationFt(altitude_ft);
291 }
292
293 inline void FGAIBase::setBank( double bank ) {
294     roll = tgt_roll = bank;
295     no_roll = false;
296 }
297
298 inline void FGAIBase::setPitch( double newpitch ) {
299     pitch = tgt_pitch = newpitch;
300 }
301
302 inline void FGAIBase::setLongitude( double longitude ) {
303     pos.setLongitudeDeg( longitude );
304 }
305
306 inline void FGAIBase::setLatitude ( double latitude ) {
307     pos.setLatitudeDeg( latitude );
308 }
309
310 inline void FGAIBase::setCallSign(const string& s) {
311     _callsign = s;
312 }
313
314
315 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
316
317 inline bool FGAIBase::getDie() { return delete_me; }
318
319 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
320
321 #endif  // _FG_AIBASE_HXX