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