]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.hxx
0d99103d120cdc210f3cb70da5f31f6c71582104
[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 SG_USING_STD(string);
37 SG_USING_STD(list);
38
39 class FGAIManager;
40 class FGAIFlightPlan;
41
42 class FGAIBase : public SGReferenced {
43
44 public:
45     enum object_type { otNull = 0, otAircraft, otShip, otCarrier, otBallistic,
46                        otRocket, otStorm, otThermal, otStatic, otMultiplayer,
47                        MAX_OBJECTS };   // Needs to be last!!!
48
49     FGAIBase(object_type ot);
50     virtual ~FGAIBase();
51
52     virtual void readFromScenario(SGPropertyNode* scFileNode);
53
54     virtual bool init(bool search_in_AI_path=false);
55     virtual void update(double dt);
56     virtual void bind();
57     virtual void unbind();
58     virtual void reinit() {}
59
60     void setManager(FGAIManager* mgr, SGPropertyNode* p);
61     void setPath( const char* model );
62     void setSMPath( const string& p );
63     void setSpeed( double speed_KTAS );
64     void setAltitude( double altitude_ft );
65     void setHeading( double heading );
66     void setLatitude( double latitude );
67     void setLongitude( double longitude );
68     void setBank( double bank );
69     void setPitch( double newpitch );
70     void setRadius ( double radius );
71     void setXoffset( double x_offset );
72     void setYoffset( double y_offset );
73     void setZoffset( double z_offset );
74     void setServiceable ( bool serviceable );
75     void setDie( bool die );
76
77     int getID() const;
78
79     bool getDie();
80
81     SGVec3d getCartPosAt(const SGVec3d& off) const;
82     SGVec3d getCartPos() const;
83
84     double _getCartPosX() const;
85     double _getCartPosY() const;
86     double _getCartPosZ() const;
87
88     string _path;
89
90 protected:
91     SGPropertyNode_ptr props;
92     SGPropertyNode_ptr model_removed; // where to report model removal
93     FGAIManager* manager;
94
95     // these describe the model's actual state
96     SGGeod pos; // WGS84 lat & lon in degrees, elev above sea-level in meters
97     double hdg;         // True heading in degrees
98     double roll;        // degrees, left is negative
99     double pitch;       // degrees, nose-down is negative
100     double speed;       // knots true airspeed
101     double altitude_ft; // feet above sea level
102     double vs;          // vertical speed, feet per minute
103     double speed_north_deg_sec;
104     double speed_east_deg_sec;
105     double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
106
107     double ft_per_deg_lon;
108     double ft_per_deg_lat;
109
110     // these describe the model's desired state
111     double tgt_heading;     // target heading, degrees true
112     double tgt_altitude_ft; // target altitude, *feet* above sea level
113     double tgt_speed;       // target speed, KTAS
114     double tgt_roll;
115     double tgt_pitch;
116     double tgt_yaw;
117     double tgt_vs;
118
119     // these describe radar information for the user
120     bool in_range;       // true if in range of the radar, otherwise false
121     double bearing;      // true bearing from user to this model
122     double elevation;    // elevation in degrees from user to this model
123     double range;        // range from user to this model, nm
124     double rdot;         // range rate, in knots
125     double horiz_offset; // look left/right from user to me, deg
126     double vert_offset;  // look up/down from user to me, deg
127     double x_shift;      // value used by radar display instrument
128     double y_shift;      // value used by radar display instrument
129     double rotation;     // value used by radar display instrument
130     double ht_diff;              // value used by radar display instrument
131
132     string model_path;     //Path to the 3D model
133     osg::ref_ptr<osg::Node> model; //The 3D model object
134     SGModelPlacement aip;
135
136     bool delete_me;
137     bool invisible;
138     bool no_roll;
139     bool serviceable;
140
141     double life;
142
143     FGAIFlightPlan *fp;
144
145     void Transform();
146     void CalculateMach();
147     double UpdateRadar(FGAIManager* manager);
148
149     static int _newAIModelID();
150
151 private:
152     const int _refID;
153     object_type _otype;
154
155 public:
156     object_type getType();
157
158     virtual const char* getTypeString(void) const { return "null"; }
159
160     bool isa( object_type otype );
161
162     void _setVS_fps( double _vs );
163     void _setAltitude( double _alt );
164     void _setLongitude( double longitude );
165     void _setLatitude ( double latitude );
166
167     double _getVS_fps() const;
168     double _getAltitude() const;
169     double _getLongitude() const;
170     double _getLatitude () const;
171     double _getBearing() const;
172     double _getElevation() const;
173     double _getRdot() const;
174     double _getH_offset() const;
175     double _getV_offset() const;
176     double _getX_shift() const;
177     double _getY_shift() const;
178     double _getRotation() const;
179     double _getSpeed() const;
180     double _getRoll() const;
181     double _getPitch() const;
182     double _getHeading() const;
183     double _get_speed_east_fps() const;
184     double _get_speed_north_fps() const;
185
186     bool   _getServiceable() const;
187     SGPropertyNode* _getProps() const;
188
189     const char* _getPath();
190     const char* _getCallsign();
191
192     // These are used in the Mach number calculations
193
194     double rho;
195     double T;                             // temperature, degs farenheit
196     double p;                             // pressure lbs/sq ft
197     double a;                             // speed of sound at altitude (ft/s)
198     double Mach;                          // Mach number
199
200     static const double e;
201     static const double lbs_to_slugs;
202
203     inline double _getRange() { return range; };
204     osg::Node* load3DModel(const string& fg_root,
205                             const string &path,
206                             SGPropertyNode *prop_root,
207                             double sim_time_sec);
208
209     static bool _isNight();
210 };
211
212 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
213     manager = mgr;
214     props = p;
215 }
216
217 inline void FGAIBase::setPath(const char* model ) {
218     model_path.append(model);
219 }
220
221 inline void FGAIBase::setSMPath(const string& p) {
222     _path = p;
223 }
224
225 inline void FGAIBase::setServiceable(bool s) {
226     serviceable = s;
227 }
228
229
230 inline void FGAIBase::setSpeed( double speed_KTAS ) {
231     speed = tgt_speed = speed_KTAS;
232 }
233
234 inline void FGAIBase::setRadius( double radius ) {
235     turn_radius_ft = radius;
236 }
237
238 inline void FGAIBase::setHeading( double heading ) {
239     hdg = tgt_heading = heading;
240 }
241
242 inline void FGAIBase::setAltitude( double alt_ft ) {
243     altitude_ft = tgt_altitude_ft = alt_ft;
244     pos.setElevationFt(altitude_ft);
245 }
246
247 inline void FGAIBase::setBank( double bank ) {
248     roll = tgt_roll = bank;
249     no_roll = false;
250 }
251
252 inline void FGAIBase::setPitch( double newpitch ) {
253     pitch = tgt_pitch = newpitch;
254 }
255
256 inline void FGAIBase::setLongitude( double longitude ) {
257     pos.setLongitudeDeg( longitude );
258 }
259 inline void FGAIBase::setLatitude ( double latitude ) {
260     pos.setLatitudeDeg( latitude );
261 }
262
263 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
264
265 inline bool FGAIBase::getDie() { return delete_me; }
266
267 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
268
269 #endif  // _FG_AIBASE_HXX