]> git.mxchange.org Git - flightgear.git/commitdiff
#510: Fix disappearing AIShips.
authorThorstenB <brehmt@gmail.com>
Fri, 9 Dec 2011 16:06:19 +0000 (17:06 +0100)
committerThorstenB <brehmt@gmail.com>
Fri, 9 Dec 2011 16:06:19 +0000 (17:06 +0100)
Untangle AI reinit and init methods. Some code in init hooks expects to be
called once only. Derived classes should not redirect their reinit to init,
even if this seems fine for the methods of the derived class itself. This
also triggers the init methods of all base classes, which may not expect
multiple calls to their init methods (or to "init" on "reinit").

src/AIModel/AIBallistic.cxx
src/AIModel/AIBallistic.hxx
src/AIModel/AIBase.cxx
src/AIModel/AIEscort.cxx
src/AIModel/AIEscort.hxx
src/AIModel/AIGroundVehicle.cxx
src/AIModel/AIGroundVehicle.hxx
src/AIModel/AIShip.cxx
src/AIModel/AIShip.hxx
src/AIModel/AIWingman.cxx
src/AIModel/AIWingman.hxx

index 3fc3e8022660d7746d1d5d06ae3e405301882865..1df938c0b2817f2aa85d8cd1d84a4058fa42200a 100644 (file)
@@ -126,7 +126,11 @@ void FGAIBallistic::readFromScenario(SGPropertyNode* scFileNode) {
 
 bool FGAIBallistic::init(bool search_in_AI_path) {
     FGAIBase::init(search_in_AI_path);
+    reinit();
+    return true;
+}
 
+void FGAIBallistic::reinit() {
     _impact_reported = false;
     _collision_reported = false;
     _expiry_reported = false;
@@ -179,7 +183,7 @@ bool FGAIBallistic::init(bool search_in_AI_path) {
 
     setParentNodes(_selected_ac);
 
-    return true;
+    FGAIBase::reinit();
 }
 
 void FGAIBallistic::bind() {
index f798347c93c884673d5e958c807eb4cf3b985f10..70ea502cfb4ced9ba40283fe0c9efeab6e567bec 100644 (file)
@@ -46,10 +46,10 @@ public:
     bool init(bool search_in_AI_path=false);
     virtual void bind();
     virtual void unbind();
+    virtual void reinit();
+    virtual void update(double dt);
 
-    void update(double dt);
-
-    FGAIBallistic *ballistic;
+    virtual const char* getTypeString(void) const { return "ballistic"; }
 
     void Run(double dt);
 
@@ -114,7 +114,8 @@ public:
 //    bool getFormate() const;
     bool getSlavedLoad() const;
 
-    virtual const char* getTypeString(void) const { return "ballistic"; }
+    FGAIBallistic *ballistic;
+
     static const double slugs_to_kgs; //conversion factor
     static const double slugs_to_lbs; //conversion factor
 
@@ -170,8 +171,6 @@ public:
 
 private:
 
-    virtual void reinit() { init(); }
-
     bool   _aero_stabilised; // if true, object will align with trajectory
     double _drag_area;       // equivalent drag area in ft2
     double _life_timer;      // seconds
index e307a1d83c1e3b75d698e32289585d9a794b9974..c6a967f8d578e5ddc9f6f6adfe6371912a35c4fd 100644 (file)
@@ -288,8 +288,14 @@ void FGAIBase::Transform() {
 
 }
 
-bool FGAIBase::init(bool search_in_AI_path) {
-    
+bool FGAIBase::init(bool search_in_AI_path)
+{
+    if (_model.valid())
+    {
+        SG_LOG(SG_AI, SG_WARN, "AIBase: Cannot initialize a model multiple times! " << model_path);
+        return false;
+    }
+
     string f;
     if(search_in_AI_path)
     {
@@ -318,12 +324,6 @@ bool FGAIBase::init(bool search_in_AI_path) {
     _aimodel = new FGAIModelData(props);
     osg::Node * mdl = SGModelLib::loadDeferredModel(f, props, _aimodel);
 
-    if (_model.valid())
-    {
-        // reinit, dump the old model
-        removeModel();
-    }
-
     _model = new osg::LOD;
     _model->setName("AI-model range animation node");
 
index 680b5113f992920e112b3c3ea1dbe00edfaab692..c88e9af334adf5afac1530819877a71a8c438e56 100644 (file)
@@ -134,7 +134,11 @@ void FGAIEscort::unbind() {
 bool FGAIEscort::init(bool search_in_AI_path) {
     if (!FGAIShip::init(search_in_AI_path))
         return false;
+    reinit();
+    return true;
+}
 
+void FGAIEscort::reinit() {
     invisible = false;
     no_roll = false;
 
@@ -147,7 +151,7 @@ bool FGAIEscort::init(bool search_in_AI_path) {
         hdg = _parent_hdg;
     }
 
-    return true;
+    FGAIShip::reinit();
 }
 
 void FGAIEscort::update(double dt) {
index 96e997962c9d6c2dcafcf5176d5cc37abea71597..a300f645eccee1f529ed8dffb17de86a6c5de550 100644 (file)
@@ -39,17 +39,16 @@ public:
     virtual ~FGAIEscort();
 
     virtual void readFromScenario(SGPropertyNode* scFileNode);
+
+    bool init(bool search_in_AI_path=false);
     virtual void bind();
     virtual void unbind();
-    virtual const char* getTypeString(void) const { return "escort"; }
+    virtual void reinit();
+    virtual void update (double dt);
 
-    bool init(bool search_in_AI_path=false);
+    virtual const char* getTypeString(void) const { return "escort"; }
 
 private:
-
-    virtual void reinit() { init(); }
-    virtual void update (double dt);
-
     void setStnRange(double r);
     void setStnBrg(double y);
     void setStationSpeed();
index 4708bd30f693c9110f817981796635b017649fbc..b39a1e19b272b02f8974d909e89d615d51e7fbb9 100644 (file)
@@ -142,7 +142,11 @@ void FGAIGroundVehicle::unbind() {
 bool FGAIGroundVehicle::init(bool search_in_AI_path) {
     if (!FGAIShip::init(search_in_AI_path))
         return false;
+    reinit();
+    return true;
+}
 
+void FGAIGroundVehicle::reinit() {
     invisible = false;
     _limit = 200;
     no_roll = true;
@@ -162,7 +166,7 @@ bool FGAIGroundVehicle::init(bool search_in_AI_path) {
         setParent();
     }
 
-    return true;
+    FGAIShip::reinit();
 }
 
 void FGAIGroundVehicle::update(double dt) {
index 70c06f290d8946acfecae487e8bb8ffee358cf5f..08491bad6825a4901fbe1939d956b33791e5746f 100644 (file)
@@ -37,17 +37,17 @@ public:
     virtual ~FGAIGroundVehicle();
 
     virtual void readFromScenario(SGPropertyNode* scFileNode);
+
+    bool init(bool search_in_AI_path=false);
     virtual void bind();
     virtual void unbind();
-    virtual const char* getTypeString(void) const { return "groundvehicle"; }
+    virtual void reinit();
+    virtual void update (double dt);
 
-    bool init(bool search_in_AI_path=false);
+    virtual const char* getTypeString(void) const { return "groundvehicle"; }
 
 private:
 
-    virtual void reinit() { init(); }
-    virtual void update (double dt);
-
     void setNoRoll(bool nr);
     void setContactX1offset(double x1);
     void setContactX2offset(double x2);
index c6cfa7a0533d9d320465f065874f2fe45a383a29..df0ce649ba8369104c17e83c213cd25a333817a5 100644 (file)
@@ -113,6 +113,12 @@ void FGAIShip::readFromScenario(SGPropertyNode* scFileNode) {
 }
 
 bool FGAIShip::init(bool search_in_AI_path) {
+    reinit();
+    return FGAIBase::init(search_in_AI_path);
+}
+
+void FGAIShip::reinit()
+{
     prev = 0; // the one behind you
     curr = 0; // the one ahead
     next = 0; // the next plus 1
@@ -134,7 +140,7 @@ bool FGAIShip::init(bool search_in_AI_path) {
     if (fp)
         _fp_init = initFlightPlan();
 
-    return FGAIBase::init(search_in_AI_path);
+    FGAIBase::reinit();
 }
 
 void FGAIShip::bind() {
index f82a14f8d1be975419095ccc35fcb3c9bd8ec319..5c3c3b7f25d55046512a98e1f470fbdc8e797390 100644 (file)
@@ -41,8 +41,9 @@ public:
     virtual void bind();
     virtual void unbind();
     virtual void update(double dt);
+    virtual void reinit();
+
     void setFlightPlan(FGAIFlightPlan* f);
-//    void setName(const string&);
     void setRudder(float r);
     void setRoll(double rl);
     void ProcessFlightPlan( double dt);
@@ -90,14 +91,9 @@ public:
 
 protected:
 
-//    string _name; // The name of this ship.
-
 private:
 
 
-
-    virtual void reinit() { init(); }
-
     void setRepeat(bool r);
     void setRestart(bool r);
     void setMissed(bool m);
index 370776e2ce58db957a154ced30cdb4e3373dcadb..9ba01042a413a8ed6ca7615ef32587b3598399a0 100644 (file)
@@ -203,7 +203,11 @@ void FGAIWingman::unbind() {
 bool FGAIWingman::init(bool search_in_AI_path) {
     if (!FGAIBallistic::init(search_in_AI_path))
         return false;
+    reinit();
+    return true;
+}
 
+void FGAIWingman::reinit() {
     invisible = false;
 
     _tgt_x_offset = _x_offset;
@@ -223,7 +227,8 @@ bool FGAIWingman::init(bool search_in_AI_path) {
 
     props->setStringValue("submodels/path", _path.c_str());
     user_WoW_node      = fgGetNode("gear/gear[1]/wow", true);
-    return true;
+
+    FGAIBallistic::reinit();
 }
 
 void FGAIWingman::update(double dt) {
index 42c51094c333303120aa0660931478cf0b4afa42..c291069ba3bd773ba5f7969e2918272783b94325 100644 (file)
@@ -35,17 +35,16 @@ public:
     virtual ~FGAIWingman();
 
     virtual void readFromScenario(SGPropertyNode* scFileNode);
+
+    bool init(bool search_in_AI_path=false);
     virtual void bind();
     virtual void unbind();
-    virtual const char* getTypeString(void) const { return "wingman"; }
+    virtual void reinit();
+    virtual void update (double dt);
 
-    bool init(bool search_in_AI_path=false);
+    virtual const char* getTypeString(void) const { return "wingman"; }
 
 private:
-
-    virtual void reinit() { init(); }
-    virtual void update (double dt);
-
     void formateToAC(double dt);
     void Break(double dt);
     void Join(double dt);