]> git.mxchange.org Git - flightgear.git/blobdiff - src/AIModel/AIBase.hxx
Don't duplicate userpos in in AIObjects.
[flightgear.git] / src / AIModel / AIBase.hxx
index 1ab3a640eacb314cf880051a90950eeba878d9ae..70e7c97184c834ab0ebbea4ad8349cbf52661647 100644 (file)
@@ -1,7 +1,7 @@
 // FGAIBase.hxx - abstract base class for AI objects
 // Written by David Culp, started Nov 2003, based on
 // David Luff's FGAIEntity class.
-// - davidculp2@comcast.net
+// - davidculp2@comcast.net 
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 #define _FG_AIBASE_HXX
 
 #include <string>
-#include <list>
 
 #include <simgear/constants.h>
-#include <simgear/math/SGMath.hxx>
 #include <simgear/scene/model/placement.hxx>
+#include <simgear/scene/model/modellib.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/structure/SGSharedPtr.hxx>
 #include <simgear/structure/SGReferenced.hxx>
+#include <simgear/props/tiedpropertylist.hxx>
+#include <simgear/sg_inlines.h>
+
+#include <simgear/math/sg_geodesy.hxx>
 
 #include <Main/fg_props.hxx>
 
 
 using std::string;
-using std::list;
 
-class SGMaterial;
+namespace simgear {
+class BVHMaterial;
+}
 class FGAIManager;
 class FGAIFlightPlan;
+class FGFX;
+class FGNasalModelDataProxy;
+class FGAIModelData;    // defined below
+
 
-class FGAIBase : public osg::Referenced {
+class FGAIBase : public SGReferenced {
 
 public:
     enum object_type { otNull = 0, otAircraft, otShip, otCarrier, otBallistic,
-                       otRocket, otStorm, otThermal, otStatic, otWingman, otGroundVehicle,
-                       otEscort, otMultiplayer,
-                       MAX_OBJECTS };  // Needs to be last!!!
+        otRocket, otStorm, otThermal, otStatic, otWingman, otGroundVehicle,
+        otEscort, otMultiplayer,
+        MAX_OBJECTS };  // Needs to be last!!!
 
-    FGAIBase(object_type ot);
+    FGAIBase(object_type ot, bool enableHot);
     virtual ~FGAIBase();
 
     virtual void readFromScenario(SGPropertyNode* scFileNode);
@@ -60,12 +68,14 @@ public:
     virtual void unbind();
     virtual void reinit() {}
 
+    void updateLOD();
     void setManager(FGAIManager* mgr, SGPropertyNode* p);
     void setPath( const char* model );
     void setSMPath( const string& p );
     void setCallSign(const string& );
     void setSpeed( double speed_KTAS );
     void setAltitude( double altitude_ft );
+    void setAltitudeAGL( double altitude_agl_ft );
     void setHeading( double heading );
     void setLatitude( double latitude );
     void setLongitude( double longitude );
@@ -85,6 +95,17 @@ public:
     void setImpactLat( double lat );
     void setImpactLon( double lon );
     void setImpactElev( double e );
+    void setParentName(const string& p);
+    void setName(const string& n);
+    void setMaxSpeed(double kts);
+
+    void calcRangeBearing(double lat, double lon, double lat2, double lon2,
+        double &range, double &bearing) const;
+    double calcRelBearingDeg(double bearing, double heading);
+    double calcTrueBearingDeg(double bearing, double heading);
+    double calcRecipBearingDeg(double bearing);
+
+    bool setParentNode();
 
     int getID() const;
     int _getSubID() const;
@@ -95,7 +116,9 @@ public:
     SGVec3d getCartPos() const;
 
     bool getGroundElevationM(const SGGeod& pos, double& elev,
-                             const SGMaterial** material) const;
+                             const simgear::BVHMaterial** material) const;
+
+    double _elevation_m;
 
     double _getCartPosX() const;
     double _getCartPosY() const;
@@ -109,32 +132,43 @@ public:
     double _roll_offset;
     double _yaw_offset;
 
+    double _max_speed;
+
     string _path;
     string _callsign;
     string _submodel;
-    string _name;
-
-    SGGeod userpos;
-
+    std::string _name;
+    string _parent;
 
 protected:
-
+    /**
+     * Tied-properties helper, record nodes which are tied for easy un-tie-ing
+     */
+    template <typename T>
+    void tie(const char* aRelPath, const SGRawValue<T>& aRawValue)
+    {
+        _tiedProperties.Tie(props->getNode(aRelPath, true), aRawValue);
+    }
+
+    simgear::TiedPropertyList _tiedProperties;
+    SGPropertyNode_ptr _selected_ac;
     SGPropertyNode_ptr props;
     SGPropertyNode_ptr trigger_node;
     SGPropertyNode_ptr model_removed; // where to report model removal
     FGAIManager* manager;
 
     // these describe the model's actual state
-    SGGeod pos;        // WGS84 lat & lon in degrees, elev above sea-level in meters
-    double hdg;                // True heading in degrees
-    double roll;       // degrees, left is negative
-    double pitch;      // degrees, nose-down is negative
+    SGGeod pos;         // WGS84 lat & lon in degrees, elev above sea-level in meters
+    double hdg;         // True heading in degrees
+    double roll;        // degrees, left is negative
+    double pitch;       // degrees, nose-down is negative
     double speed;       // knots true airspeed
     double altitude_ft; // feet above sea level
     double vs;          // vertical speed, feet per minute
     double speed_north_deg_sec;
     double speed_east_deg_sec;
     double turn_radius_ft; // turn radius ft at 15 kts rudder angle 15 degrees
+    double altitude_agl_ft;
 
     double ft_per_deg_lon;
     double ft_per_deg_lat;
@@ -159,16 +193,16 @@ protected:
     double x_shift;      // value used by radar display instrument
     double y_shift;      // value used by radar display instrument
     double rotation;     // value used by radar display instrument
-    double ht_diff;             // value used by radar display instrument
+    double ht_diff;      // value used by radar display instrument
 
-    string model_path;    //Path to the 3D model
-    osg::ref_ptr<osg::Node> model; //The 3D model object
+    string model_path;   //Path to the 3D model
     SGModelPlacement aip;
 
     bool delete_me;
     bool invisible;
     bool no_roll;
     bool serviceable;
+    bool _installed;
     int _subID;
 
     double life;
@@ -177,6 +211,7 @@ protected:
 
     bool _impact_reported;
     bool _collision_reported;
+    bool _expiry_reported;
 
     double _impact_lat;
     double _impact_lon;
@@ -190,12 +225,20 @@ protected:
     void CalculateMach();
     double UpdateRadar(FGAIManager* manager);
 
+    void removeModel();
+    void removeSoundFx();
+
     static int _newAIModelID();
 
 private:
     int _refID;
     object_type _otype;
     bool _initialized;
+    osg::ref_ptr<osg::LOD> _model; //The 3D model LOD object
+
+    osg::ref_ptr<FGAIModelData> _modeldata;
+
+    SGSharedPtr<FGFX>  _fx;
 
 public:
     object_type getType();
@@ -209,7 +252,8 @@ public:
     void _setLongitude( double longitude );
     void _setLatitude ( double latitude );
     void _setSubID( int s );
-    void _setUserPos();
+
+    double _getAltitudeAGL(SGGeod inpos, double start);
 
     double _getVS_fps() const;
     double _getAltitude() const;
@@ -246,6 +290,7 @@ public:
     bool   _getImpact();
     bool   _getImpactData();
     bool   _getCollisionData();
+    bool   _getExpiryData();
 
     SGPropertyNode* _getProps() const;
 
@@ -271,10 +316,9 @@ public:
     inline double _getRange() { return range; };
     inline double _getBearing() { return bearing; };
 
-    virtual osg::Node* load3DModel(const string &path,
-                           SGPropertyNode *prop_root);
     static bool _isNight();
+
+     string & getCallSign();
 };
 
 inline void FGAIBase::setManager(FGAIManager* mgr, SGPropertyNode* p) {
@@ -311,6 +355,10 @@ inline void FGAIBase::setAltitude( double alt_ft ) {
     pos.setElevationFt(altitude_ft);
 }
 
+inline void FGAIBase::setAltitudeAGL( double alt_ft ) {
+    altitude_agl_ft = alt_ft;
+}
+
 inline void FGAIBase::setBank( double bank ) {
     roll = tgt_roll = bank;
     no_roll = false;
@@ -331,6 +379,10 @@ inline void FGAIBase::setLatitude ( double latitude ) {
 inline void FGAIBase::setCallSign(const string& s) {
     _callsign = s;
 }
+inline string& FGAIBase::getCallSign() {
+    return _callsign;
+}
+
 inline void FGAIBase::setXoffset(double x) {
     _x_offset = x;
 }
@@ -355,10 +407,73 @@ inline void FGAIBase::setYawoffset(double y) {
     _yaw_offset = y;
 }
 
+inline void FGAIBase::setParentName(const string& p) {
+    _parent = p;
+}
+
+inline void FGAIBase::setName(const string& n) {
+    _name = n;
+}
+
 inline void FGAIBase::setDie( bool die ) { delete_me = die; }
 
 inline bool FGAIBase::getDie() { return delete_me; }
 
 inline FGAIBase::object_type FGAIBase::getType() { return _otype; }
 
-#endif // _FG_AIBASE_HXX
+inline void FGAIBase::calcRangeBearing(double lat, double lon, double lat2, double lon2,
+                                  double &range, double &bearing) const
+{
+    // calculate the bearing and range of the second pos from the first
+    double az2, distance;
+    geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
+    range = distance * SG_METER_TO_NM;
+}
+
+inline double FGAIBase::calcRelBearingDeg(double bearing, double heading){
+    double angle = bearing - heading;
+    SG_NORMALIZE_RANGE(angle, -180.0, 180.0);
+    return angle;
+}
+
+inline double FGAIBase::calcTrueBearingDeg(double bearing, double heading){
+    double angle = bearing + heading;
+    SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
+    return angle;
+}
+
+inline double FGAIBase::calcRecipBearingDeg(double bearing){
+    double angle = bearing - 180;
+    SG_NORMALIZE_RANGE(angle, 0.0, 360.0);
+    return angle;
+}
+
+inline void FGAIBase::setMaxSpeed(double m) {
+    _max_speed = m;
+}
+
+
+class FGAIModelData : public simgear::SGModelData {
+public:
+    FGAIModelData(SGPropertyNode *root = 0);
+    ~FGAIModelData();
+
+    /** osg callback, thread-safe */
+    void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *n);
+
+    /** init hook to be called after model is loaded.
+     * Not thread-safe. Call from main thread only. */
+    void init(void) { _initialized = true; }
+
+    bool needInitilization(void) { return _ready && !_initialized;}
+    bool isInitialized(void) { return _initialized;}
+    inline std::string& get_sound_path() { return _fxpath;}
+
+private:
+    FGNasalModelDataProxy *_nasal;
+    std::string _fxpath;
+    bool _ready;
+    bool _initialized;
+};
+
+#endif // _FG_AIBASE_HXX