Skip to content

Commit

Permalink
Fix dbscript handling with 0 delay.
Browse files Browse the repository at this point in the history
I wrongly assumed that i should take care only of first script in the list to handle db script with 0 delay.
There can be more than one with 0 as delay.
I also forget to take into accout the result of the script. If a step in the script fail we should stop the rest of the script.

Thank @Grz3s for pointing and helping.
  • Loading branch information
cyberium committed Mar 17, 2020
1 parent eb20292 commit 7d5e724
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/game/Maps/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1799,17 +1799,29 @@ bool Map::ScriptsStart(ScriptMapMapName const& scripts, uint32 id, Object* sourc

///- Schedule script execution for all scripts in the script map
ScriptMap const& scriptMap = scriptInfoMapMapItr->second;
for (const auto& scriptInfoItr : scriptMap)

// handle first all scripts with 0 delay
auto scriptInfoItr = scriptMap.begin();
while (scriptInfoItr != scriptMap.end())
{
auto const& scriptInfo = scriptInfoItr.second;
ScriptAction sa(scripts.first, this, sourceGuid, targetGuid, ownerGuid, &scriptInfo);
auto const& scriptInfo = scriptInfoItr->second;
if (scriptInfo.delay)
{
m_scriptSchedule.emplace(GetCurrentClockTime() + std::chrono::milliseconds(scriptInfoItr.first), sa);
sScriptMgr.IncreaseScheduledScriptsCount();
}
else
sa.HandleScriptStep();
break;

ScriptAction sa(scripts.first, this, sourceGuid, targetGuid, ownerGuid, &scriptInfo);
if (sa.HandleScriptStep())
return true; // script failed we should not continue further (command 31 or any error)

++scriptInfoItr;
}

// add delayed script to the script scheduler
for (; scriptInfoItr != scriptMap.end(); ++scriptInfoItr)
{
auto const& scriptInfo = scriptInfoItr->second;
ScriptAction sa(scripts.first, this, sourceGuid, targetGuid, ownerGuid, &scriptInfo);
m_scriptSchedule.emplace(GetCurrentClockTime() + std::chrono::milliseconds(scriptInfoItr->first), sa);
sScriptMgr.IncreaseScheduledScriptsCount();
}

return true;
Expand Down
14 changes: 13 additions & 1 deletion src/game/MotionGenerators/WaypointMovementGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ void WaypointMovementGenerator<Creature>::OnArrived(Creature& creature)
// save position and orientation in case of GetResetPosition() call
creature.GetPosition(m_resetPoint);

// AI can modify node delay so we have to compute it
int32 newWaitTime = node.delay + m_scriptTime;
m_scriptTime = 0;

newWaitTime = newWaitTime > 0 ? newWaitTime : 0;

// Wait delay ms
Stop(node.delay);
Stop(newWaitTime);
}

void WaypointMovementGenerator<Creature>::StartMove(Creature& creature)
Expand Down Expand Up @@ -382,10 +388,16 @@ void WaypointMovementGenerator<Creature>::AddToWaypointPauseTime(int32 waitTimeD
{
if (!i_nextMoveTime.Passed())
{
// creature is stopped already
// Prevent <= 0, the code in Update requires to catch the change from moving to not moving
int32 newWaitTime = i_nextMoveTime.GetExpiry() + waitTimeDiff;
i_nextMoveTime.Reset(newWaitTime > 0 ? newWaitTime : 1);
}
else
{
// creature is not stopped yet (script with 0 delay may be calling this)
m_scriptTime = waitTimeDiff;
}
}

bool WaypointMovementGenerator<Creature>::SetNextWaypoint(uint32 pointId)
Expand Down
3 changes: 2 additions & 1 deletion src/game/MotionGenerators/WaypointMovementGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WaypointMovementGenerator<Creature>
{
public:
WaypointMovementGenerator(Creature&) :
i_nextMoveTime(0), m_isArrivalDone(false), m_lastReachedWaypoint(0), m_pathId(0), m_PathOrigin(), m_nextNodeSplineIdx(-1)
i_nextMoveTime(0), m_isArrivalDone(false), m_lastReachedWaypoint(0), m_pathId(0), m_PathOrigin(), m_nextNodeSplineIdx(-1), m_scriptTime(0)
{}
~WaypointMovementGenerator() { i_path = nullptr; }
void Initialize(Creature& creature);
Expand Down Expand Up @@ -100,6 +100,7 @@ class WaypointMovementGenerator<Creature>
ShortTimeTracker i_nextMoveTime;
bool m_isArrivalDone;
int32 m_nextNodeSplineIdx;
int32 m_scriptTime;
uint32 m_lastReachedWaypoint;
WorldLocation m_resetPoint;

Expand Down

0 comments on commit 7d5e724

Please sign in to comment.