Rework of scripted actions and reset
authorbenblan <benblan@3ce38193-f967-438d-acd5-fc4e68e0da95>
Mon, 15 Sep 2014 09:03:54 +0000 (09:03 +0000)
committerbenblan <benblan@3ce38193-f967-438d-acd5-fc4e68e0da95>
Mon, 15 Sep 2014 09:03:54 +0000 (09:03 +0000)
freesynd/branches/rework-actions/src/core/gamesession.cpp
freesynd/branches/rework-actions/src/ia/action.cpp
freesynd/branches/rework-actions/src/ia/actions.h
freesynd/branches/rework-actions/src/ia/behaviour.cpp
freesynd/branches/rework-actions/src/ia/behaviour.h
freesynd/branches/rework-actions/src/menus/squadselection.cpp
freesynd/branches/rework-actions/src/mission.h
freesynd/branches/rework-actions/src/missionmanager.cpp
freesynd/branches/rework-actions/src/ped.cpp
freesynd/branches/rework-actions/src/ped.h
freesynd/branches/rework-actions/src/pedactions.cpp

index 9ae47292caffcd0edeca2cadbdeb2266b09216d0..16ad9e94e6274f30d3b62a01d0f7f0c4c1bfd84b 100644 (file)
@@ -38,7 +38,7 @@ Block g_Blocks[50] = {
     {"#CNTRY_20", 54000000, 54000000, 20, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, "10", 0, 0, 0},            // 4 - Scandinavia
     {"#CNTRY_18", 40000000, 40000000, 18, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, "6:12:13", 0, 0, 0},       // 5 - Ural
     {"#CNTRY_22", 54000000, 54000000, 22, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, "7:13", 0, 0, 0},          // 6 - Siberia
-    {"#CNTRY_12", 56000000, 56000000, 12, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, NULL, 0, 0, 0},            // 7 - KAMCHATKA
+    {"#CNTRY_12", 56000000, 56000000, 12, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, "0", 0, 0, 0},             // 7 - KAMCHATKA
     {"#CNTRY_21", 58000000, 58000000, 21, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, NULL, 0, 0, 0},            // 8 - YUKON
     {"#CNTRY_01", 48000000, 48000000, 1, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, "4", 0, 0, 0},              // 9 - West Eur.
     {"#CNTRY_15", 50000000, 50000000, 15, 30, 0, STAT_VERY_HAPPY, 0, 0, BLK_UNAVAIL, "11", 0, 0, 0},            // 10 - Central Eur.
index b987e505ce73c3dfa578559926d60fd8fc5d68d1..7eaaf03225df16cecce87e3963a9237bc79a0940 100644 (file)
@@ -298,18 +298,6 @@ bool EscapeAction::doExecute(int elapsed, Mission *pMission, PedInstance *pPed)
     return true;
 }
 
-/*!
- * 
- * \param elapsed Time elapsed since last frame
- * \param pMission Mission data
- * \param pPed The ped executing the action.
- */
-bool ResetScriptedAction::doExecute(int elapsed, Mission *pMission, PedInstance *pPed) {
-    setSucceeded();
-    printf("ResetScriptedAction : Not implemented\n");
-    return true;
-}
-
 /*!
  * Class constructor.
  * \param pTarget The ped to follow.
index 128c7a0592cdb89dc9d42f918309557df9670053..ab585ef6a938d364be08b8d8fbab013ab8920dd3 100644 (file)
@@ -78,6 +78,8 @@ namespace fs_actions {
             kActTypeHit,
             //! Action of shooting
             kActTypeShoot,
+            //! Action of resetting scripted action
+            kActTypeReset,
             kActTypeUndefined
         };
 
@@ -273,14 +275,15 @@ namespace fs_actions {
     };
 
     /*!
-     * This action is used in scripted action to reset all actions.
+     * This action does nothing but its presence makes all scripted to start again.
+     * 
      */
     class ResetScriptedAction : public MovementAction {
     public:
         ResetScriptedAction():
-            MovementAction(kActTypeUndefined, kOrigScript, false, true) {}
+            MovementAction(kActTypeReset, kOrigScript, false, true) {}
     protected:
-        bool doExecute(int elapsed, Mission *pMission, PedInstance *pPed);
+        bool doExecute(int elapsed, Mission *pMission, PedInstance *pPed) { return true; }
     };
 
     /*!
index afb0377e43cf72c7c57f5fcb64583747b2520425..52a0f5fc465116f6ad23075a41ef80b1689c7f46 100644 (file)
@@ -139,7 +139,6 @@ void PersuaderBehaviourComponent::handleBehaviourEvent(Behaviour::BehaviourEvent
 PanicComponent::PanicComponent():
         BehaviourComponent(), scoutTimer_(500) {
     panicking_ = false;
-    pDefaultAction_ = NULL;
     // this component will be activated by event to
     // lower CPU consumption
     setEnabled(false);
@@ -172,7 +171,7 @@ void PanicComponent::execute(int elapsed, Mission *pMission, PedInstance *pPed)
         } else if (pAction == NULL) {
             // ped has finished panicking and there no reason to panick
             // so continue walking normaly
-            pPed->addMovementAction(pDefaultAction_, false);
+            pPed->restoreDefaultAction();
             panicking_ = false;
         }
     }
@@ -186,7 +185,7 @@ void PanicComponent::handleBehaviourEvent(Behaviour::BehaviourEvent evtType, Ped
     case Behaviour::kBehvEvtPanicDisabled:
         setEnabled(false);
         if (panicking_) {
-            pPed->addMovementAction(pDefaultAction_, false);
+            pPed->restoreDefaultAction();
             panicking_ = false;
         }
         break;
@@ -234,10 +233,4 @@ void  PanicComponent::runAway(PedInstance *pPed, PedInstance *pArmedPed, fs_acti
     pAction->setmaxDistanceToWalk(kDistanceToRun);
     pPed->addMovementAction(pAction, false);
     panicking_ = true;
-
-    if (pWalkAction != NULL) {
-        // save and reset the default action
-        pDefaultAction_ = pWalkAction;
-        pDefaultAction_->reset();
-    }
 }
index 4ec7db59a3fec4ddf9f327ed53ad224502cc2cf0..ff0327a72dfc7e33a94014efc02f9a80189c1b47 100644 (file)
@@ -163,8 +163,6 @@ private:
     fs_utils::Timer scoutTimer_;
     /*! True when ped is currently in panic.*/
     bool panicking_;
-    /*! */
-    fs_actions::MovementAction *pDefaultAction_;
 };
 
 #endif // IA_BEHAVIOUR_H_
\ No newline at end of file
index 615025322ec1c9c4b2bc32f53ec4c5fddedabdb6..b52224d66d91f483d528d41833fd14672c856e39 100644 (file)
@@ -266,7 +266,7 @@ void SquadSelection::enterOrLeaveVehicle(Vehicle *pVehicle, bool addAction) {
                 pVehicle->clearDestination();
                 // tells the driver to stop
                 VehicleInstance *pVi = dynamic_cast<VehicleInstance *>(pVehicle);
-                pVi->getDriver()->destroyAllActions();
+                pVi->getDriver()->destroyAllActions(false);
             }
             // Ped can get off only if vehicle is stopped 
             // (ie trains only stop in stations)
index f24f814c00b8cea4fe4764a005c8ba11cb5bde65..f7a745b3faf3486629dcb28bb2d360120e479393 100644 (file)
@@ -36,7 +36,6 @@
 #include "weapon.h"
 #include "map.h"
 #include "model/leveldata.h"
-//include "model/shot.h"
 #include "core/gameevent.h"
 
 class Vehicle;
index b20eef3bef001d0173fad5d095ffec2a99aef541..29158bc423025033062c3e68b3a524272d5d0997 100644 (file)
@@ -190,9 +190,16 @@ bool MissionManager::load_level_data(int n, LevelData::LevelDataAll &level_data)
 /*!
  *
  */
-void MissionManager::hackMissions(int n, uint8 *data) {
-    // Kenya map, adding additional walking points to scenarios
-    if (n == 40) {
+void MissionManager::hackMissions(int missionId, uint8 *data) {
+    if (missionId == 10) { // Western Europe
+        // Change the second destination of the car for ped #168
+        // as in original scenario that destination seems non walkable
+        uint8 *scen_start = data + 97128 + 8 * 8;
+        scen_start[4] = 74;
+        scen_start[5] = 120;
+        scen_start[6] = 2;
+    }else if (missionId == 40) {
+        // Kenya map, adding additional walking points to scenarios
         uint8 *scen_start = data + 97128 + 8 * 86;
         // coord offsets changing for next point
         scen_start[4] = ((scen_start[4] & 0xFE) | 1);
@@ -690,10 +697,11 @@ void MissionManager::createPeds(const LevelData::LevelDataAll &level_data, DataI
                                 p->createActQWait(as, 3000);
                                 as.actions.back().group_desc = PedInstance::gd_mStandWalk;
                             }
-                            if (v)
+                            if (v) {
                                 p->createActQUsingCar(as, &pn, v);
-                            else
+                            } else {
                                 p->createActQWalking(as, &pn, NULL, p->direction(), 0, true);
+                            }
                             //p->createActQWalking(as, &pn, NULL, -1);
                             if ((!notInVehicle) && offset_nxt == 0)
                                 p->createActQResetActionQueue(as);
@@ -795,7 +803,7 @@ void MissionManager::createScriptedActionsForPed(Mission *pMission, DataIndex &d
                 pPed->addActionTrigger(6 * 256, pn);
             }
             if (v) {
-                LOG(Log::k_FLG_GAME, "MissionManager","createScriptedActionsForPed", (" - Drive car to (%d, %d, %d)", pn.tileX(), pn.tileY(), pn.tileZ()))
+                LOG(Log::k_FLG_GAME, "MissionManager","createScriptedActionsForPed", (" - Drive car to (%d, %d, %d) (%d, %d)", pn.tileX(), pn.tileY(), pn.tileZ(), pn.offX(), pn.offY()))
                 VehicleInstance *pCar = dynamic_cast<VehicleInstance *>(v);
                 pPed->addActionDriveVehicle(fs_actions::kOrigScript, pCar, pn, true);
             } else {
@@ -821,7 +829,7 @@ void MissionManager::createScriptedActionsForPed(Mission *pMission, DataIndex &d
             } else {
                 PathNode pn(v->tileX(), v->tileY(), v->tileZ(),
                     v->offX(), v->offY());
-                LOG(Log::k_FLG_GAME, "MissionManager","createScriptedActionsForPed", (" - Drive to (%d, %d, %d)", pn.tileX(), pn.tileY(), pn.tileZ()))
+                LOG(Log::k_FLG_GAME, "MissionManager","createScriptedActionsForPed", (" - Drive to (%d, %d, %d) (%d, %d)", pn.tileX(), pn.tileY(), pn.tileZ(), pn.offX(), pn.offY()))
                 VehicleInstance *pCar = dynamic_cast<VehicleInstance *>(v);
                 pPed->addActionDriveVehicle(fs_actions::kOrigScript, pCar, pn, true);
             }
index b27626bb294b03bf0be82f15a341d9ef76c93611..43cc0749f75ad6a1ee298951a4f7f6b47d4b711c 100644 (file)
@@ -448,18 +448,25 @@ bool PedInstance::executeAction(int elapsed, Mission *pMission) {
         if (currentAction_->isFinished()) {
             if (health_ == 0) {
                 // Ped may have died during execution of a HitAction.
-                destroyAllActions();
+                destroyAllActions(true);
                 destroyUseWeaponAction();
             } else {
                 // current action is finished : go to next one
                 fs_actions::MovementAction *pNext = currentAction_->next();
-                delete currentAction_;
+
+                if (currentAction_->origin() != fs_actions::kOrigScript &&
+                        currentAction_->origin() != fs_actions::kOrigDefault) {
+                    delete currentAction_;
+                }
                 currentAction_ = pNext;
                 // If next action was suspended, resume it
                 if (currentAction_ != NULL && currentAction_->isSuspended()) {
                     currentAction_->resume(pMission, this);
                 }
             }
+        } else if (currentAction_->type() == fs_actions::Action::kActTypeReset) {
+            restoreDefaultAction();
+            break;
         } else {
             // current action is still running, so stop iterate now
             // we will continue next time
@@ -2086,6 +2093,7 @@ PedInstance::PedInstance(Ped *ped, uint16 id, int m, bool isOur) :
 
     behaviour_.setOwner(this);
     currentAction_ = NULL;
+    defaultAction_ = NULL;
     pUseWeaponAction_ = NULL;
     panicImmuned_ = false;
 }
@@ -2102,7 +2110,7 @@ PedInstance::~PedInstance()
     delete intelligence_;
     intelligence_ = NULL;
 
-    destroyAllActions();
+    destroyAllActions(true);
     destroyUseWeaponAction();
 }
 
@@ -2703,7 +2711,7 @@ bool PedInstance::handleDamage(ShootableMapObject::DamageInflictType *d) {
         health_ = 0;
         drop_actions_ = true;
         clearDestination();
-        destroyAllActions();
+        destroyAllActions(true);
         switchActionStateTo(PedInstance::pa_smDead);
         if ((desc_state_ & pd_smControlled) != 0 && owner_)
             owner_->rmvPersuaded(this);
index acbec609a8162db05a48b75eb9b002f8ba9d3d00..e6c6a61e0dc98ae877c16db359352951cd2c49ea 100644 (file)
@@ -309,13 +309,15 @@ public:
     //! Returns the ped's current movement action
     fs_actions::MovementAction * currentAction() { return currentAction_; }
     //! Removes all ped's actions
-    void destroyAllActions();
+    void destroyAllActions(bool includeDefault);
     //! Removes ped's action of using weapon
     void destroyUseWeaponAction();
     //! Execute the current action if any
     bool executeAction(int elapsed, Mission *pMission);
     //! Execute a weapon action if any
     bool executeUseWeaponAction(int elapsed, Mission *pMission);
+    //! Set the default action as the current one
+    void restoreDefaultAction();
     
     //! Adds action to walk to a given destination
     void addActionWalk(const PathNode &tpn, fs_actions::CreatOrigin origin, bool appendAction);
@@ -917,6 +919,11 @@ protected:
     Behaviour behaviour_;
     /*! Current action*/
     fs_actions::MovementAction *currentAction_;
+    /*! Default and scripted actions are actions that can be restored.
+     * A ped can have either one default action or several scripted actions but
+     * not both.
+     */
+    fs_actions::MovementAction *defaultAction_;
     /*! Current action of using a weapon.*/
     fs_actions::UseWeaponAction *pUseWeaponAction_;
 
index 15ce02dddc7f18f2fda66a043da318cf7f0d1186..82c7147e1edf1a08ff5bd6878439cea6efa3c22e 100644 (file)
  * Adds the given action to the list of actions.
  * If appendAction is true, the action is added after all existing actions.
  * Else existing actions are destroyed and new action becomes the only one.
- * \param pAction The action to add
+ * \param pActionToAdd The action to add
  * \param appendAction If true action is append after all existing actions.
+ * \param addToDefault If true adds the action to the list of default actions.
  */
-void PedInstance::addMovementAction(fs_actions::MovementAction *pAction, bool appendAction) {
+void PedInstance::addMovementAction(fs_actions::MovementAction *pActionToAdd, bool appendAction) {
     if (currentAction_ == NULL) {
-        currentAction_ = pAction;
+        currentAction_ = pActionToAdd;
     } else if (appendAction) {
         fs_actions::MovementAction *pAct = currentAction_;
         while(pAct->next()) {
             pAct = pAct->next();
         }
-        pAct->setNext(pAction);
+        pAct->setNext(pActionToAdd);
     } else {
-        if (currentAction_->origin() != fs_actions::kOrigScript &&
-            currentAction_->origin() != fs_actions::kOrigDefault) {
-            // for scripted and default actions, just forget reference
-            // actions will be reused
-            destroyAllActions();
-        }
-        currentAction_ = pAction;
+        // for scripted and default actions, just forget reference because
+        // those actions will be reused
+        destroyAllActions(false);
+        currentAction_ = pActionToAdd;
+    }
+
+    if ((pActionToAdd->origin() == fs_actions::kOrigDefault ||
+            pActionToAdd->origin() == fs_actions::kOrigScript) &&
+            defaultAction_ == NULL) {
+        defaultAction_ = pActionToAdd;
     }
 }
 
 /*!
- * Removes all actions.
+ * Deletes all actions in the ped's list.
+ * \param includeDefault
  */
-void PedInstance::destroyAllActions() {
+void PedInstance::destroyAllActions(bool includeDefault) {
     while (currentAction_ != NULL) {
         fs_actions::MovementAction *pNext = currentAction_->next();
-        delete currentAction_;
+
+        if (currentAction_->origin() != fs_actions::kOrigScript &&
+                currentAction_->origin() != fs_actions::kOrigDefault) {
+            // for scripted and default actions, just forget reference because
+            // those actions are stored in the defaultActionLst field
+            delete currentAction_;
+        }
+
         currentAction_ = pNext;
     }
+
+    if (includeDefault) {
+        while (defaultAction_ != NULL) {
+            fs_actions::MovementAction *pNext = defaultAction_->next();
+            delete defaultAction_;
+
+            defaultAction_ = pNext;
+        }
+    }
 }
 
 /*!
@@ -72,6 +93,23 @@ void PedInstance::destroyUseWeaponAction() {
     }
 }
 
+/*!
+ * Set the default action as the current one.
+ */
+void PedInstance::restoreDefaultAction() {
+    if (defaultAction_) {
+        // before restoring, reset all default actions
+        fs_actions::MovementAction *pAction = defaultAction_;
+        while (pAction != NULL) {
+            pAction->reset();
+            pAction = pAction->next();
+        }
+
+        destroyAllActions(false);
+        currentAction_ = defaultAction_;
+    }
+}
+
 /*!
  * Adds the action to walk.
  * \param tpn Destination point