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