]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIBase.hxx
82c4de8867446b1642bee38aae9562d79b87637a
[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., 675 Mass Ave, Cambridge, MA 02139, 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/point3d.hxx>
28 #include <simgear/scene/model/placement.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #include <Main/fg_props.hxx>
32
33 SG_USING_STD(string);
34 SG_USING_STD(list);
35
36 class FGAIManager;
37 class FGAIFlightPlan;
38
39
40 typedef struct {
41    string callsign;
42
43    // can be aircraft, ship, storm, thermal or ballistic
44    string m_type;
45    string m_class;
46    string path;
47    string flightplan;
48
49    FGAIFlightPlan *fp;
50
51    double repeat;             // in seconds
52    double latitude;           // used if no flightplan defined
53    double longitude;          // used if no flightplan defined
54    double altitude;           // used if no flightplan defined
55    double speed;              // used if no flightplan defined
56    double heading;            // used if no flightplan defined
57    double roll;               // used if no flightplan defined
58    double azimuth;            // used by ballistic objects
59    double elevation;          // used by ballistic objects
60    double rudder;             // used by ship objects
61    double strength;           // used by thermal objects
62    double diameter;           // used by thermal objects
63    double height_msl;         // used by thermal objects
64    double eda;                // used by ballistic objects
65    double life;               // life span in seconds
66    double buoyancy;           // acceleration in ft per sec2
67    double wind_from_east;     // in feet per second
68    double wind_from_north;    // in feet per second
69    double cd;                 // coefficient of drag
70    bool wind;                 // if true, model reacts to parent wind
71    double mass;               // in slugs
72    bool aero_stabilised;      // if true, ballistic object aligns with trajectory
73    list<string> solid_objects;    // List of solid object names
74    list<string> wire_objects;     // List of wire object names
75    list<string> catapult_objects; // List of catapult object names
76    double radius;             // used by ship ojects, in feet
77    double x_offset;           // used by ship ojects, in meters
78    double y_offset;           // used by ship ojects, in meters
79    double z_offset;           // used by ship ojects, in meters   
80   string acType;              // used by aircraft objects
81   string company;             // used by aircraft objects
82 } FGAIModelEntity;
83
84
85 class FGAIBase {
86
87 public:
88
89     FGAIBase();
90     virtual ~FGAIBase();
91     virtual void update(double dt);
92     inline Point3D GetPos() { return(pos); }
93
94     enum object_type { otNull = 0, otAircraft, otShip, otBallistic,
95                        otRocket, otStorm, otThermal, 
96                        MAX_OBJECTS };   // Needs to be last!!!
97
98     virtual bool init();
99     virtual void bind();
100     virtual void unbind();
101
102     void setPath( const char* model );
103     void setSpeed( double speed_KTAS );
104     void setAltitude( double altitude_ft );
105     void setHeading( double heading );
106     void setLatitude( double latitude );
107     void setLongitude( double longitude );
108     void setBank( double bank );
109     void setRadius ( double radius );
110     void setXoffset( double x_offset );
111     void setYoffset( double y_offset );
112     void setZoffset( double z_offset );
113
114
115     void* getID();
116     void setDie( bool die );
117     bool getDie();
118
119 protected:
120
121     SGPropertyNode *props;
122     FGAIManager* manager;
123
124     // these describe the model's actual state
125     Point3D pos;        // WGS84 lat & lon in degrees, elev above sea-level in meters
126     double hdg;         // True heading in degrees
127     double roll;        // degrees, left is negative
128     double pitch;       // degrees, nose-down is negative
129     double speed;       // knots true airspeed
130     double altitude;    // meters above sea level
131     double vs;          // vertical speed, feet per minute  
132     double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
133
134     // these describe the flols 
135     Point3D flolspos; // WGS84 lat & lon in degrees, elev above sea-level in meters
136     double flols_x_offset;      // longitudinal distance, in meters
137     double flols_y_offset;      // lateral distance, in meters
138     double flols_z_offset;      // height, in meters
139     
140     double ft_per_deg_lon;
141     double ft_per_deg_lat;
142
143     // these describe the model's desired state
144     double tgt_heading;  // target heading, degrees true
145     double tgt_altitude; // target altitude, *feet* above sea level
146     double tgt_speed;    // target speed, KTAS
147     double tgt_roll;
148     double tgt_pitch;
149     double tgt_yaw;
150     double tgt_vs;
151
152     // these describe radar information for the user
153     bool in_range;       // true if in range of the radar, otherwise false
154     double bearing;      // true bearing from user to this model
155     double elevation;    // elevation in degrees from user to this model
156     double range;        // range from user to this model, nm
157     double rdot;         // range rate, in knots
158     double horiz_offset; // look left/right from user to me, deg
159     double vert_offset;  // look up/down from user to me, deg
160     double x_shift;      // value used by radar display instrument
161     double y_shift;      // value used by radar display instrument
162     double rotation;     // value used by radar display instrument
163
164
165     string model_path;     //Path to the 3D model
166     ssgBranch * model;     //The 3D model object
167     SGModelPlacement aip;
168     bool delete_me;
169     bool invisible;
170     bool no_roll;
171     double life;
172     FGAIFlightPlan *fp;
173
174     void Transform();
175
176     double UpdateRadar(FGAIManager* manager);
177
178     string _type_str;
179     object_type _otype;
180     int index;
181
182 public:
183
184     object_type getType();
185     bool isa( object_type otype );
186
187     double _getVS_fps() const;
188     void _setVS_fps( double _vs );
189
190     double _getAltitude() const;
191     void _setAltitude( double _alt );
192
193     void _setLongitude( double longitude );
194     void _setLatitude ( double latitude );
195
196     double _getLongitude() const;
197     double _getLatitude () const;
198
199     double _getBearing() const;
200     double _getElevation() const;
201     double _getRdot() const;
202     double _getH_offset() const;
203     double _getV_offset() const;
204     double _getX_shift() const;
205     double _getY_shift() const;
206     double _getRotation() const;
207
208     double rho;
209     double T;                             // temperature, degs farenheit
210     double p;                             // pressure lbs/sq ft
211         double a;                             // speed of sound at altitude (ft/s)
212         double Mach;                          // Mach number
213         
214     static const double e;
215     static const double lbs_to_slugs;
216
217     int _getID() const;
218
219     inline double _getRange() { return range; };
220   ssgBranch * load3DModel(const string& fg_root, 
221                           const string &path,
222                           SGPropertyNode *prop_root, 
223                           double sim_time_sec);
224
225     static bool _isNight();
226 };
227
228
229 inline void FGAIBase::setPath( const char* model ) {
230   model_path.append(model);
231 }
232
233 inline void FGAIBase::setSpeed( double speed_KTAS ) {
234   speed = tgt_speed = speed_KTAS;
235 }
236
237 inline void FGAIBase::setRadius( double radius ) {
238   turn_radius_ft = radius;
239 }
240
241 inline void FGAIBase::setXoffset( double x_offset ) {
242   flols_x_offset = x_offset;
243 }
244
245 inline void FGAIBase::setYoffset( double y_offset ) {
246   flols_y_offset = y_offset;
247 }
248
249 inline void FGAIBase::setZoffset( double z_offset ) {
250   flols_z_offset = z_offset;
251 }
252 inline void FGAIBase::setHeading( double heading ) {
253   hdg = tgt_heading = heading;
254 }
255
256 inline void FGAIBase::setAltitude( double altitude_ft ) {
257     altitude = tgt_altitude = altitude_ft;
258     pos.setelev(altitude * SG_FEET_TO_METER);
259 }
260
261 inline void FGAIBase::setBank( double bank ) {
262   roll = tgt_roll = bank;
263   no_roll = false;
264 }
265
266 inline void FGAIBase::setLongitude( double longitude ) {
267     pos.setlon( longitude );
268 }
269 inline void FGAIBase::setLatitude ( double latitude ) {
270     pos.setlat( latitude );
271 }
272
273 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
274 inline bool FGAIBase::getDie() { return delete_me; }
275
276 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
277
278 inline void* FGAIBase::getID() { return this; }
279
280
281
282 #endif  // _FG_AIBASE_HXX
283