Revert Core/Movement: Smooth movement

Port From (https://github.com/TrinityCore/TrinityCore/commit/2caec4f4d20b4c0f91abbcc60b756e00838c7bdd)
This commit is contained in:
hondacrx
2019-08-27 16:12:42 -04:00
parent 1794feb888
commit 6a77a4afd6
8 changed files with 488 additions and 493 deletions
+1 -1
View File
@@ -317,7 +317,7 @@ namespace Framework.Constants
RemovePower = 110, // PowerType, newPower RemovePower = 110, // PowerType, newPower
GameEventStop = 111, // GameEventId GameEventStop = 111, // GameEventId
GameEventStart = 112, // GameEventId GameEventStart = 112, // GameEventId
// Not used StartClosestWaypoint = 113, // wp1, wp2, wp3, wp4, wp5, wp6, wp7
MoveOffset = 114, MoveOffset = 114,
RandomSound = 115, // SoundId1, SoundId2, SoundId3, SoundId4, SoundId5, onlySelf RandomSound = 115, // SoundId1, SoundId2, SoundId3, SoundId4, SoundId5, onlySelf
SetCorpseDelay = 116, // timer SetCorpseDelay = 116, // timer
+196 -129
View File
@@ -31,11 +31,10 @@ namespace Game.AI
public npc_escortAI(Creature creature) : base(creature) public npc_escortAI(Creature creature) : base(creature)
{ {
m_uiPlayerGUID = ObjectGuid.Empty; m_uiPlayerGUID = ObjectGuid.Empty;
m_uiWPWaitTimer = 1000; m_uiWPWaitTimer = 2500;
m_uiPlayerCheckTimer = 0; m_uiPlayerCheckTimer = 1000;
m_uiEscortState = eEscortState.None; m_uiEscortState = eEscortState.None;
MaxPlayerDistance = 50; MaxPlayerDistance = 50;
LastWP = 0;
m_pQuestForEscort = null; m_pQuestForEscort = null;
m_bIsActiveAttacker = true; m_bIsActiveAttacker = true;
m_bIsRunning = false; m_bIsRunning = false;
@@ -45,8 +44,21 @@ namespace Game.AI
DespawnAtFar = true; DespawnAtFar = true;
ScriptWP = false; ScriptWP = false;
HasImmuneToNPCFlags = false; HasImmuneToNPCFlags = false;
m_bStarted = false; }
m_bEnded = false;
public override void AttackStart(Unit target)
{
if (target == null)
return;
if (me.Attack(target, true))
{
if (me.GetMotionMaster().GetCurrentMovementGeneratorType() == MovementGeneratorType.Point)
me.GetMotionMaster().MovementExpired();
if (IsCombatMovementAllowed())
me.GetMotionMaster().MoveChase(target);
}
} }
//see followerAI //see followerAI
@@ -89,16 +101,39 @@ namespace Game.AI
public override void MoveInLineOfSight(Unit who) public override void MoveInLineOfSight(Unit who)
{ {
if (me.GetVictim())
return;
if (me.HasReactState(ReactStates.Aggressive) && !me.HasUnitState(UnitState.Stunned) && who.isTargetableForAttack() && who.isInAccessiblePlaceFor(me)) if (me.HasReactState(ReactStates.Aggressive) && !me.HasUnitState(UnitState.Stunned) && who.isTargetableForAttack() && who.isInAccessiblePlaceFor(me))
{
if (HasEscortState(eEscortState.Escorting) && AssistPlayerInCombatAgainst(who)) if (HasEscortState(eEscortState.Escorting) && AssistPlayerInCombatAgainst(who))
return; return;
if (me.CanStartAttack(who, false)) if (!me.CanFly() && me.GetDistanceZ(who) > SharedConst.CreatureAttackRangeZ)
return;
if (me.IsHostileTo(who))
{
float fAttackRadius = me.GetAttackDistance(who);
if (me.IsWithinDistInMap(who, fAttackRadius) && me.IsWithinLOSInMap(who))
{
if (!me.GetVictim())
{
// Clear distracted state on combat
if (me.HasUnitState(UnitState.Distracted))
{
me.ClearUnitState(UnitState.Distracted);
me.GetMotionMaster().Clear();
}
AttackStart(who); AttackStart(who);
} }
else if (me.GetMap().IsDungeon())
{
who.SetInCombatWith(me);
me.AddThreat(who, 0.0f);
}
}
}
}
}
public override void JustDied(Unit killer) public override void JustDied(Unit killer)
{ {
@@ -126,13 +161,13 @@ namespace Game.AI
public override void JustRespawned() public override void JustRespawned()
{ {
RemoveEscortState(eEscortState.Escorting | eEscortState.Returning | eEscortState.Paused); m_uiEscortState = eEscortState.None;
if (!IsCombatMovementAllowed()) if (!IsCombatMovementAllowed())
SetCombatMovement(true); SetCombatMovement(true);
//add a small delay before going to first waypoint, normal in near all cases //add a small delay before going to first waypoint, normal in near all cases
m_uiWPWaitTimer = 1000; m_uiWPWaitTimer = 2500;
if (me.GetFaction() != me.GetCreatureTemplate().Faction) if (me.GetFaction() != me.GetCreatureTemplate().Faction)
me.RestoreFaction(); me.RestoreFaction();
@@ -142,7 +177,6 @@ namespace Game.AI
void ReturnToLastPoint() void ReturnToLastPoint()
{ {
me.SetWalk(false);
me.GetMotionMaster().MovePoint(0xFFFFFF, me.GetHomePosition()); me.GetMotionMaster().MovePoint(0xFFFFFF, me.GetHomePosition());
} }
@@ -193,22 +227,16 @@ namespace Game.AI
public override void UpdateAI(uint diff) public override void UpdateAI(uint diff)
{ {
//Waypoint Updating
if (HasEscortState(eEscortState.Escorting) && !me.GetVictim() && m_uiWPWaitTimer != 0 && !HasEscortState(eEscortState.Returning)) if (HasEscortState(eEscortState.Escorting) && !me.GetVictim() && m_uiWPWaitTimer != 0 && !HasEscortState(eEscortState.Returning))
{ {
if (m_uiWPWaitTimer <= diff) if (m_uiWPWaitTimer <= diff)
{ {
if (!HasEscortState(eEscortState.Paused)) //End of the line
if (WaypointList[CurrentWPIndex] == null)
{ {
m_uiWPWaitTimer = 0; m_uiWPWaitTimer = 0;
if (m_bEnded)
{
me.StopMoving();
me.GetMotionMaster().Clear(false);
me.GetMotionMaster().MoveIdle();
m_bEnded = false;
if (DespawnAtEnd) if (DespawnAtEnd)
{ {
Log.outDebug(LogFilter.Scripts, "EscortAI reached end of waypoints"); Log.outDebug(LogFilter.Scripts, "EscortAI reached end of waypoints");
@@ -220,48 +248,52 @@ namespace Game.AI
me.GetMotionMaster().MovePoint(EscortPointIds.Home, fRetX, fRetY, fRetZ); me.GetMotionMaster().MovePoint(EscortPointIds.Home, fRetX, fRetY, fRetZ);
m_uiWPWaitTimer = 0;
Log.outDebug(LogFilter.Scripts, $"EscortAI are returning home to spawn location: {EscortPointIds.Home}, {fRetX}, {fRetY}, {fRetZ}"); Log.outDebug(LogFilter.Scripts, $"EscortAI are returning home to spawn location: {EscortPointIds.Home}, {fRetX}, {fRetY}, {fRetZ}");
return;
} }
else if (m_bCanInstantRespawn)
if (m_bCanInstantRespawn)
{ {
me.setDeathState(DeathState.JustDied); me.setDeathState(DeathState.JustDied);
me.Respawn(); me.Respawn();
} }
else else
me.DespawnOrUnsummon(); me.DespawnOrUnsummon();
return;
} }
else else
{
Log.outDebug(LogFilter.Scripts, "EscortAI reached end of waypoints with Despawn off"); Log.outDebug(LogFilter.Scripts, "EscortAI reached end of waypoints with Despawn off");
RemoveEscortState(eEscortState.Escorting);
return; return;
} }
if (!m_bStarted) }
if (!HasEscortState(eEscortState.Paused))
{ {
m_bStarted = true; var currentWp = WaypointList[CurrentWPIndex];
me.GetMotionMaster().MovePath(_path, false); me.GetMotionMaster().MovePoint(currentWp.Id, currentWp.X, currentWp.Y, currentWp.Z);
} Log.outDebug(LogFilter.Scripts, $"EscortAI start waypoint {currentWp.Id} ({currentWp.X}, {currentWp.Y}, {currentWp.Z}).");
else
{ WaypointStart(currentWp.Id);
WaypointMovementGenerator move = (WaypointMovementGenerator)me.GetMotionMaster().top();
if (move != null) m_uiWPWaitTimer = 0;
WaypointStart(move.GetCurrentNode());
} }
} }
} }
else
m_uiWPWaitTimer -= diff;
}
//Check if player or any member of his group is within range //Check if player or any member of his group is within range
if (HasEscortState(eEscortState.Escorting) && !m_uiPlayerGUID.IsEmpty() && !me.GetVictim() && !HasEscortState(eEscortState.Returning)) if (HasEscortState(eEscortState.Escorting) && !m_uiPlayerGUID.IsEmpty() && !me.GetVictim() && !HasEscortState(eEscortState.Returning))
{ {
m_uiPlayerCheckTimer += diff; if (m_uiPlayerCheckTimer <= diff)
if (m_uiPlayerCheckTimer > 1000)
{ {
if (DespawnAtFar && !IsPlayerOrGroupInRange()) if (DespawnAtFar && !IsPlayerOrGroupInRange())
{ {
Log.outDebug(LogFilter.Scripts, "EscortAI failed because player/group was to far away or not found");
if (m_bCanInstantRespawn) if (m_bCanInstantRespawn)
{ {
me.setDeathState(DeathState.JustDied); me.setDeathState(DeathState.JustDied);
@@ -273,8 +305,10 @@ namespace Game.AI
return; return;
} }
m_uiPlayerCheckTimer = 0; m_uiPlayerCheckTimer = 1000;
} }
else
m_uiPlayerCheckTimer -= diff;
} }
UpdateEscortAI(diff); UpdateEscortAI(diff);
@@ -290,15 +324,9 @@ namespace Game.AI
public override void MovementInform(MovementGeneratorType moveType, uint pointId) public override void MovementInform(MovementGeneratorType moveType, uint pointId)
{ {
// no action allowed if there is no escort if (moveType != MovementGeneratorType.Point || !HasEscortState(eEscortState.Escorting))
if (!HasEscortState(eEscortState.Escorting))
return; return;
if (moveType == MovementGeneratorType.Point)
{
if (m_uiWPWaitTimer == 0)
m_uiWPWaitTimer = 1;
//Combat start position reached, continue waypoint movement //Combat start position reached, continue waypoint movement
if (pointId == EscortPointIds.LastPoint) if (pointId == EscortPointIds.LastPoint)
{ {
@@ -306,68 +334,43 @@ namespace Game.AI
me.SetWalk(!m_bIsRunning); me.SetWalk(!m_bIsRunning);
RemoveEscortState(eEscortState.Returning); RemoveEscortState(eEscortState.Returning);
if (m_uiWPWaitTimer == 0)
m_uiWPWaitTimer = 1;
} }
else if (pointId == EscortPointIds.Home) else if (pointId == EscortPointIds.Home)
{ {
Log.outDebug(LogFilter.Scripts, "EscortAI has returned to original home location and will continue from beginning of waypoint list."); Log.outDebug(LogFilter.Scripts, "EscortAI has returned to original home location and will continue from beginning of waypoint list.");
m_bStarted = false; CurrentWPIndex = 0;
m_uiWPWaitTimer = 1;
} }
} else
else if (moveType == MovementGeneratorType.Waypoint)
{ {
//Call WP function var currentWp = WaypointList[CurrentWPIndex];
WaypointReached(pointId); //Make sure that we are still on the right waypoint
if (currentWp.Id != pointId)
//End of the line
if (LastWP != 0 && LastWP == pointId)
{ {
LastWP = 0; Log.outError(LogFilter.Misc, $"TSCR ERROR: EscortAI reached waypoint out of order {pointId}, expected {currentWp.Id}, creature entry {me.GetEntry()}");
m_bStarted = false;
m_bEnded = true;
m_uiWPWaitTimer = 50;
return; return;
} }
Log.outDebug(LogFilter.Scripts, $"EscortAI Waypoint {pointId} reached"); Log.outDebug(LogFilter.Scripts, $"EscortAI Waypoint {currentWp.Id} reached");
WaypointMovementGenerator move = (WaypointMovementGenerator)me.GetMotionMaster().top(); //Call WP function
if (move != null) WaypointReached(currentWp.Id);
m_uiWPWaitTimer = (uint)move.GetTrackerTimer().GetExpiry();
//Call WP start function m_uiWPWaitTimer = currentWp.WaitTimeMs + 1;
if (m_uiWPWaitTimer == 0 && !HasEscortState(eEscortState.Paused) && move != null)
WaypointStart(move.GetCurrentNode());
if (m_bIsRunning) ++CurrentWPIndex;
me.SetWalk(false);
else
me.SetWalk(true);
} }
} }
public void AddWaypoint(uint id, float x, float y, float z, uint waitTime = 0) public void AddWaypoint(uint id, float x, float y, float z, uint waitTime = 0)
{ {
GridDefines.NormalizeMapCoord(ref x); Escort_Waypoint t = new Escort_Waypoint(id, x, y, z, waitTime);
GridDefines.NormalizeMapCoord(ref y);
WaypointNode wp = new WaypointNode(); WaypointList.Add(t);
wp.id = id;
wp.x = x;
wp.y = y;
wp.z = z;
wp.orientation = 0.0f;
wp.moveType = m_bIsRunning ? WaypointMoveType.Run : WaypointMoveType.Walk;
wp.delay = waitTime;
wp.eventId = 0;
wp.eventChance = 100;
_path.nodes.Add(wp);
LastWP = id;
ScriptWP = true; ScriptWP = true;
} }
@@ -378,29 +381,10 @@ namespace Game.AI
if (movePoints.Empty()) if (movePoints.Empty())
return; return;
LastWP = movePoints.Last().uiPointId;
foreach (var point in movePoints) foreach (var point in movePoints)
{ {
float x = point.fX; Escort_Waypoint wayPoint = new Escort_Waypoint(point.uiPointId, point.fX, point.fY, point.fZ, point.uiWaitTime);
float y = point.fY; WaypointList.Add(wayPoint);
float z = point.fZ;
GridDefines.NormalizeMapCoord(ref x);
GridDefines.NormalizeMapCoord(ref y);
WaypointNode wp = new WaypointNode();
wp.id = point.uiPointId;
wp.x = x;
wp.y = y;
wp.z = z;
wp.orientation = 0.0f;
wp.moveType = m_bIsRunning ? WaypointMoveType.Run : WaypointMoveType.Walk;
wp.delay = point.uiWaitTime;
wp.eventId = 0;
wp.eventChance = 100;
_path.nodes.Add(wp);
} }
} }
@@ -439,6 +423,20 @@ namespace Game.AI
return; return;
} }
if (!ScriptWP && resetWaypoints)
{
if (!WaypointList.Empty())
WaypointList.Clear();
FillPointMovementListForCreature();
}
if (WaypointList.Empty())
{
Log.outError(LogFilter.Scripts, $"EscortAI (script: {me.GetScriptName()}, creature entry: {me.GetEntry()}) starts with 0 waypoints (possible missing entry in script_waypoint. Quest: {(quest != null ? quest.Id : 0)}).");
return;
}
//set variables //set variables
m_bIsActiveAttacker = isActiveAttacker; m_bIsActiveAttacker = isActiveAttacker;
m_bIsRunning = run; m_bIsRunning = run;
@@ -449,16 +447,12 @@ namespace Game.AI
m_bCanInstantRespawn = instantRespawn; m_bCanInstantRespawn = instantRespawn;
m_bCanReturnToStart = canLoopPath; m_bCanReturnToStart = canLoopPath;
if (!ScriptWP && resetWaypoints) // sd2 never adds wp in script, but tc does
FillPointMovementListForCreature();
if (m_bCanReturnToStart && m_bCanInstantRespawn) if (m_bCanReturnToStart && m_bCanInstantRespawn)
Log.outDebug(LogFilter.Scripts, "EscortAI is set to return home after waypoint end and instant respawn at waypoint end. Creature will never despawn."); Log.outDebug(LogFilter.Scripts, "EscortAI is set to return home after waypoint end and instant respawn at waypoint end. Creature will never despawn.");
if (me.GetMotionMaster().GetCurrentMovementGeneratorType() == MovementGeneratorType.Waypoint) if (me.GetMotionMaster().GetCurrentMovementGeneratorType() == MovementGeneratorType.Waypoint)
{ {
me.StopMoving(); me.GetMotionMaster().MovementExpired();
me.GetMotionMaster().Clear(false);
me.GetMotionMaster().MoveIdle(); me.GetMotionMaster().MoveIdle();
Log.outDebug(LogFilter.Scripts, "EscortAI start with WAYPOINT_MOTION_TYPE, changed to MoveIdle."); Log.outDebug(LogFilter.Scripts, "EscortAI start with WAYPOINT_MOTION_TYPE, changed to MoveIdle.");
} }
@@ -474,14 +468,14 @@ namespace Game.AI
Log.outDebug(LogFilter.Scripts, $"EscortAI started. ActiveAttacker = {m_bIsActiveAttacker}, Run = {m_bIsRunning}, PlayerGUID = {m_uiPlayerGUID.ToString()}"); Log.outDebug(LogFilter.Scripts, $"EscortAI started. ActiveAttacker = {m_bIsActiveAttacker}, Run = {m_bIsRunning}, PlayerGUID = {m_uiPlayerGUID.ToString()}");
CurrentWPIndex = 0;
//Set initial speed //Set initial speed
if (m_bIsRunning) if (m_bIsRunning)
me.SetWalk(false); me.SetWalk(false);
else else
me.SetWalk(true); me.SetWalk(true);
m_bStarted = false;
AddEscortState(eEscortState.Escorting); AddEscortState(eEscortState.Escorting);
} }
@@ -491,17 +485,75 @@ namespace Game.AI
return; return;
if (on) if (on)
{
AddEscortState(eEscortState.Paused); AddEscortState(eEscortState.Paused);
me.StopMoving();
}
else else
{
RemoveEscortState(eEscortState.Paused); RemoveEscortState(eEscortState.Paused);
WaypointMovementGenerator move = (WaypointMovementGenerator)me.GetMotionMaster().top();
if (move != null)
move.GetTrackerTimer().Reset(1);
} }
bool SetNextWaypoint(uint pointId, float x, float y, float z, float orientation)
{
me.UpdatePosition(x, y, z, orientation);
return SetNextWaypoint(pointId, false, true);
}
bool SetNextWaypoint(uint pointId, bool setPosition, bool resetWaypointsOnFail)
{
if (!WaypointList.Empty())
WaypointList.Clear();
FillPointMovementListForCreature();
if (WaypointList.Empty())
return false;
int size = WaypointList.Count;
Escort_Waypoint waypoint = new Escort_Waypoint(0, 0, 0, 0, 0);
do
{
waypoint = WaypointList.First();
WaypointList.RemoveAt(0);
if (waypoint.Id == pointId)
{
if (setPosition)
me.UpdatePosition(waypoint.X, waypoint.Y, waypoint.Z, me.GetOrientation());
CurrentWPIndex = 0;
return true;
}
}
while (!WaypointList.Empty());
// we failed.
// we reset the waypoints in the start; if we pulled any, reset it again
if (resetWaypointsOnFail && size != WaypointList.Count)
{
if (!WaypointList.Empty())
WaypointList.Clear();
FillPointMovementListForCreature();
}
return false;
}
bool GetWaypointPosition(uint pointId, ref float x, ref float y, ref float z)
{
var waypoints = Global.ScriptMgr.GetPointMoveList(me.GetEntry());
if (waypoints == null)
return false;
foreach (var pointMove in waypoints)
{
if (pointMove.uiPointId == pointId)
{
x = pointMove.fX;
y = pointMove.fY;
z = pointMove.fZ;
return true;
}
}
return false;
} }
public virtual void WaypointReached(uint pointId) { } public virtual void WaypointReached(uint pointId) { }
@@ -518,7 +570,6 @@ namespace Game.AI
public bool GetAttack() { return m_bIsActiveAttacker; }//used in EnterEvadeMode override public bool GetAttack() { return m_bIsActiveAttacker; }//used in EnterEvadeMode override
public void SetCanAttack(bool attack) { m_bIsActiveAttacker = attack; } public void SetCanAttack(bool attack) { m_bIsActiveAttacker = attack; }
public ObjectGuid GetEventStarterGUID() { return m_uiPlayerGUID; } public ObjectGuid GetEventStarterGUID() { return m_uiPlayerGUID; }
public void SetWaitTimer(uint Timer) { m_uiWPWaitTimer = Timer; }
public Player GetPlayerForEscort() { return Global.ObjAccessor.GetPlayer(me, m_uiPlayerGUID); } public Player GetPlayerForEscort() { return Global.ObjAccessor.GetPlayer(me, m_uiPlayerGUID); }
@@ -530,12 +581,12 @@ namespace Game.AI
uint m_uiPlayerCheckTimer; uint m_uiPlayerCheckTimer;
eEscortState m_uiEscortState; eEscortState m_uiEscortState;
float MaxPlayerDistance; float MaxPlayerDistance;
uint LastWP;
WaypointPath _path = new WaypointPath();
Quest m_pQuestForEscort; //generally passed in Start() when regular escort script. Quest m_pQuestForEscort; //generally passed in Start() when regular escort script.
List<Escort_Waypoint> WaypointList = new List<Escort_Waypoint>();
int CurrentWPIndex;
bool m_bIsActiveAttacker; //obsolete, determined by faction. bool m_bIsActiveAttacker; //obsolete, determined by faction.
bool m_bIsRunning; //all creatures are walking by default (has flag MOVEMENTFLAG_WALK) bool m_bIsRunning; //all creatures are walking by default (has flag MOVEMENTFLAG_WALK)
bool m_bCanInstantRespawn; //if creature should respawn instantly after escort over (if not, database respawntime are used) bool m_bCanInstantRespawn; //if creature should respawn instantly after escort over (if not, database respawntime are used)
@@ -544,8 +595,6 @@ namespace Game.AI
bool DespawnAtFar; bool DespawnAtFar;
bool ScriptWP; bool ScriptWP;
bool HasImmuneToNPCFlags; bool HasImmuneToNPCFlags;
bool m_bStarted;
bool m_bEnded;
} }
public enum eEscortState public enum eEscortState
@@ -556,6 +605,24 @@ namespace Game.AI
Paused = 0x004 //will not proceed with waypoints before state is removed Paused = 0x004 //will not proceed with waypoints before state is removed
} }
class Escort_Waypoint
{
public Escort_Waypoint(uint id, float x, float y, float z, uint w)
{
Id = id;
X = x;
Y = y;
Z = z;
WaitTimeMs = w;
}
public uint Id;
public float X;
public float Y;
public float Z;
public uint WaitTimeMs;
}
struct EscortPointIds struct EscortPointIds
{ {
public const uint LastPoint = 0xFFFFFF; public const uint LastPoint = 0xFFFFFF;
+129 -152
View File
@@ -42,19 +42,19 @@ namespace Game.AI
mCurrentWPID = 0;//first wp id is 1 !! mCurrentWPID = 0;//first wp id is 1 !!
mWPReached = false; mWPReached = false;
mWPPauseTimer = 0; mWPPauseTimer = 0;
mOOCReached = false;
mEscortNPCFlags = 0;
mCanRepeatPath = false; mCanRepeatPath = false;
// Spawn in run mode // Spawn in run mode
me.SetWalk(false);
mRun = true; mRun = true;
m_Ended = false;
mLastOOCPos = me.GetPosition();
mCanAutoAttack = true; mCanAutoAttack = true;
mCanCombatMove = true; mCanCombatMove = true;
mForcedPaused = false; mForcedPaused = false;
mLastWPIDReached = 0;
mEscortQuestID = 0; mEscortQuestID = 0;
mDespawnTime = 0; mDespawnTime = 0;
@@ -102,6 +102,25 @@ namespace Game.AI
GetScript().OnReset(); GetScript().OnReset();
} }
WayPoint GetNextWayPoint()
{
if (mWayPoints.Empty())
return null;
mCurrentWPID++;
var wayPoint = mWayPoints.Find(p => p.Id == mCurrentWPID);
if (wayPoint != null)
{
mLastWP = wayPoint;
if (mLastWP.Id != mCurrentWPID)
{
Log.outError(LogFilter.Misc, "SmartAI.GetNextWayPoint: Got not expected waypoint id {mLastWP.id}, expected {mCurrentWPID}");
}
return wayPoint;
}
return null;
}
public void StartPath(bool run = false, uint path = 0, bool repeat = false, Unit invoker = null) public void StartPath(bool run = false, uint path = 0, bool repeat = false, Unit invoker = null)
{ {
if (me.IsInCombat())// no wp movement in combat if (me.IsInCombat())// no wp movement in combat
@@ -113,31 +132,25 @@ namespace Game.AI
if (HasEscortState(SmartEscortState.Escorting)) if (HasEscortState(SmartEscortState.Escorting))
StopPath(); StopPath();
SetRun(run);
if (path != 0) if (path != 0)
if (!LoadPath(path)) if (!LoadPath(path))
return; return;
if (_path.nodes.Empty()) if (mWayPoints.Empty())
return; return;
mCurrentWPID = 1; AddEscortState(SmartEscortState.Escorting);
m_Ended = false;
// Do not use AddEscortState, removing everything from previous cycle
mEscortState = SmartEscortState.Escorting;
mCanRepeatPath = repeat; mCanRepeatPath = repeat;
if (invoker && invoker.GetTypeId() == TypeId.Player) SetRun(run);
WayPoint wp = GetNextWayPoint();
if (wp != null)
{ {
mEscortNPCFlags = me.m_unitData.NpcFlags[0]; mLastOOCPos = me.GetPosition();
me.SetNpcFlags(NPCFlags.None); me.GetMotionMaster().MovePoint(wp.Id, wp.X, wp.Y, wp.Z);
GetScript().ProcessEventsFor(SmartEvents.WaypointStart, null, wp.Id, GetScript().GetPathId());
} }
GetScript().ProcessEventsFor(SmartEvents.WaypointStart, null, mCurrentWPID, GetScript().GetPathId());
me.GetMotionMaster().MovePath(_path, mCanRepeatPath);
} }
bool LoadPath(uint entry) bool LoadPath(uint entry)
@@ -145,36 +158,13 @@ namespace Game.AI
if (HasEscortState(SmartEscortState.Escorting)) if (HasEscortState(SmartEscortState.Escorting))
return false; return false;
var path = Global.SmartAIMgr.GetPath(entry); mWayPoints = Global.SmartAIMgr.GetPath(entry);
if (path.Empty()) if (mWayPoints == null)
{ {
GetScript().SetPathId(0); GetScript().SetPathId(0);
return false; return false;
} }
foreach (WayPoint waypoint in path)
{
float x = waypoint.x;
float y = waypoint.y;
float z = waypoint.z;
GridDefines.NormalizeMapCoord(ref x);
GridDefines.NormalizeMapCoord(ref y);
WaypointNode wp = new WaypointNode();
wp.id = waypoint.id;
wp.x = x;
wp.y = y;
wp.z = z;
wp.orientation = 0.0f;
wp.moveType = mRun ? WaypointMoveType.Run : WaypointMoveType.Walk;
wp.delay = 0;
wp.eventId = 0;
wp.eventChance = 100;
_path.nodes.Add(wp);
}
GetScript().SetPathId(entry); GetScript().SetPathId(entry);
return true; return true;
} }
@@ -186,19 +176,21 @@ namespace Game.AI
if (HasEscortState(SmartEscortState.Paused)) if (HasEscortState(SmartEscortState.Paused))
{ {
Log.outError(LogFilter.Server, $"SmartAI.PausePath: Creature entry {me.GetEntry()} wanted to pause waypoint (current waypoint: {mCurrentWPID}) movement while already paused, ignoring."); Log.outError(LogFilter.Server, $"SmartAI.PausePath: Creature entry {me.GetEntry()} wanted to pause waypoint movement while already paused, ignoring.");
return; return;
} }
mForcedPaused = forced;
mLastOOCPos = me.GetPosition();
AddEscortState(SmartEscortState.Paused); AddEscortState(SmartEscortState.Paused);
mWPPauseTimer = delay; mWPPauseTimer = delay;
if (forced && !mWPReached) if (forced)
{ {
mForcedPaused = forced;
SetRun(mRun); SetRun(mRun);
me.StopMoving(); me.StopMoving();//force stop
me.GetMotionMaster().MoveIdle();//force stop
} }
GetScript().ProcessEventsFor(SmartEvents.WaypointPaused, null, mCurrentWPID, GetScript().GetPathId()); GetScript().ProcessEventsFor(SmartEvents.WaypointPaused, null, mLastWP.Id, GetScript().GetPathId());
} }
public void StopPath(uint DespawnTime = 0, uint quest = 0, bool fail = false) public void StopPath(uint DespawnTime = 0, uint quest = 0, bool fail = false)
@@ -209,27 +201,33 @@ namespace Game.AI
if (quest != 0) if (quest != 0)
mEscortQuestID = quest; mEscortQuestID = quest;
if (mDespawnState != 2)
SetDespawnTime(DespawnTime); SetDespawnTime(DespawnTime);
//mDespawnTime = DespawnTime;
me.StopMoving(); mLastOOCPos = me.GetPosition();
me.GetMotionMaster().MovementExpired(false); me.StopMoving();//force stop
me.GetMotionMaster().MoveIdle(); me.GetMotionMaster().MoveIdle();
GetScript().ProcessEventsFor(SmartEvents.WaypointStopped, null, mCurrentWPID, GetScript().GetPathId()); GetScript().ProcessEventsFor(SmartEvents.WaypointStopped, null, mLastWP.Id, GetScript().GetPathId());
EndPath(fail); EndPath(fail);
} }
public void EndPath(bool fail = false) public void EndPath(bool fail = false)
{ {
RemoveEscortState(SmartEscortState.Escorting | SmartEscortState.Paused | SmartEscortState.Returning); GetScript().ProcessEventsFor(SmartEvents.WaypointEnded, null, mLastWP.Id, GetScript().GetPathId());
_path.nodes.Clear();
mWPPauseTimer = 0;
if (mEscortNPCFlags != 0) RemoveEscortState(SmartEscortState.Escorting | SmartEscortState.Paused | SmartEscortState.Returning);
mWayPoints = null;
mCurrentWPID = 0;
mWPPauseTimer = 0;
mLastWP = null;
if (mCanRepeatPath)
{ {
me.SetNpcFlags((NPCFlags)mEscortNPCFlags); if (IsAIControlled())
mEscortNPCFlags = 0; StartPath(mRun, GetScript().GetPathId(), true);
} }
else
GetScript().SetPathId(0);
List<WorldObject> targets = GetScript().GetTargetList(SharedConst.SmartEscortTargets); List<WorldObject> targets = GetScript().GetTargetList(SharedConst.SmartEscortTargets);
if (targets != null && mEscortQuestID != 0) if (targets != null && mEscortQuestID != 0)
@@ -275,36 +273,16 @@ namespace Game.AI
} }
} }
// End Path events should be only processed if it was SUCCESSFUL stop or stop called by SMART_ACTION_WAYPOINT_STOP
if (fail)
return;
GetScript().ProcessEventsFor(SmartEvents.WaypointEnded, null, mCurrentWPID, GetScript().GetPathId());
if (mCanRepeatPath)
{
if (IsAIControlled())
StartPath(mRun, GetScript().GetPathId(), mCanRepeatPath);
}
else
GetScript().SetPathId(0);
if (mDespawnState == 1) if (mDespawnState == 1)
StartDespawn(); StartDespawn();
} }
public void ResumePath() public void ResumePath()
{ {
GetScript().ProcessEventsFor(SmartEvents.WaypointResumed, null, mCurrentWPID, GetScript().GetPathId());
RemoveEscortState(SmartEscortState.Paused);
mForcedPaused = false;
mWPReached = false;
mWPPauseTimer = 0;
SetRun(mRun); SetRun(mRun);
WaypointMovementGenerator move = (WaypointMovementGenerator)me.GetMotionMaster().top(); if (mLastWP != null)
if (move != null) me.GetMotionMaster().MovePoint(mLastWP.Id, mLastWP.X, mLastWP.Y, mLastWP.Z);
move.GetTrackerTimer().Reset(1);
} }
void ReturnToLastOOCPos() void ReturnToLastOOCPos()
@@ -312,10 +290,8 @@ namespace Game.AI
if (!IsAIControlled()) if (!IsAIControlled())
return; return;
me.SetWalk(false); SetRun(mRun);
float x, y, z, o; me.GetMotionMaster().MovePoint(EventId.SmartEscortLastOCCPoint, mLastOOCPos);
me.GetHomePosition(out x, out y, out z, out o);
me.GetMotionMaster().MovePoint(EventId.SmartEscortLastOCCPoint, x, y, z);
} }
void UpdatePath(uint diff) void UpdatePath(uint diff)
@@ -327,12 +303,7 @@ namespace Game.AI
{ {
if (!IsEscortInvokerInRange()) if (!IsEscortInvokerInRange())
{ {
StopPath(0, mEscortQuestID, true); StopPath(mDespawnTime, mEscortQuestID, true);
// allow to properly hook out of range despawn action, which in most cases should perform the same operation as dying
GetScript().ProcessEventsFor(SmartEvents.Death, me);
me.DespawnOrUnsummon(1);
return;
} }
mEscortInvokerCheckTimer = 1000; mEscortInvokerCheckTimer = 1000;
} }
@@ -342,29 +313,56 @@ namespace Game.AI
// handle pause // handle pause
if (HasEscortState(SmartEscortState.Paused)) if (HasEscortState(SmartEscortState.Paused))
{ {
if (mWPPauseTimer <= diff) if (mWPPauseTimer < diff)
{
if (!me.IsInCombat() && !HasEscortState(SmartEscortState.Returning) && (mWPReached || mLastWPIDReached == EventId.SmartEscortLastOCCPoint || mForcedPaused))
{
GetScript().ProcessEventsFor(SmartEvents.WaypointResumed, null, mLastWP.Id, GetScript().GetPathId());
RemoveEscortState(SmartEscortState.Paused);
if (mForcedPaused)// if paused between 2 wps resend movement
{ {
if (!me.IsInCombat() && !HasEscortState(SmartEscortState.Returning) && (mWPReached || mForcedPaused))
ResumePath(); ResumePath();
mWPReached = false;
mForcedPaused = false;
}
if (mLastWPIDReached == EventId.SmartEscortLastOCCPoint)
mWPReached = true;
}
mWPPauseTimer = 0;
} }
else else
mWPPauseTimer -= diff; mWPPauseTimer -= diff;
} }
else if (m_Ended) // end path
{
m_Ended = false;
StopPath();
return;
}
if (HasEscortState(SmartEscortState.Returning)) if (HasEscortState(SmartEscortState.Returning))
{ {
if (mOOCReached)//reached OOC WP if (mWPReached)//reached OOC WP
{ {
mOOCReached = false;
RemoveEscortState(SmartEscortState.Returning); RemoveEscortState(SmartEscortState.Returning);
if (!HasEscortState(SmartEscortState.Paused)) if (!HasEscortState(SmartEscortState.Paused))
ResumePath(); ResumePath();
mWPReached = false;
}
}
if ((!me.HasReactState(ReactStates.Passive) && me.IsInCombat()) || HasEscortState(SmartEscortState.Paused | SmartEscortState.Returning))
return;
// handle next wp
if (mWPReached)//reached WP
{
mWPReached = false;
if (mCurrentWPID == GetWPCount())
{
EndPath();
}
else
{
WayPoint wp = GetNextWayPoint();
if (wp != null)
{
SetRun(mRun);
me.GetMotionMaster().MovePoint(wp.Id, wp.X, wp.Y, wp.Z);
}
} }
} }
} }
@@ -426,43 +424,22 @@ namespace Game.AI
void MovepointReached(uint id) void MovepointReached(uint id)
{ {
// override the id, path can be resumed any time and counter will reset if (id != EventId.SmartEscortLastOCCPoint && mLastWPIDReached != id)
// mCurrentWPID holds proper id GetScript().ProcessEventsFor(SmartEvents.WaypointReached, null, id);
// both point movement and escort generator can enter this function
if (id == EventId.SmartEscortLastOCCPoint)
{
mOOCReached = true;
return;
}
mCurrentWPID = id + 1; // in SmartAI increase by 1
mLastWPIDReached = id;
mWPReached = true; mWPReached = true;
GetScript().ProcessEventsFor(SmartEvents.WaypointReached, null, mCurrentWPID, GetScript().GetPathId());
if (HasEscortState(SmartEscortState.Paused))
me.StopMoving();
else if (HasEscortState(SmartEscortState.Escorting) && me.GetMotionMaster().GetCurrentMovementGeneratorType() == MovementGeneratorType.Waypoint)
{
mWPReached = false;
if (mCurrentWPID == _path.nodes.Count)
m_Ended = true;
else
SetRun(mRun);
}
} }
public override void MovementInform(MovementGeneratorType MovementType, uint Data) public override void MovementInform(MovementGeneratorType MovementType, uint Data)
{ {
if (MovementType == MovementGeneratorType.Point && Data == EventId.SmartEscortLastOCCPoint) if ((MovementType == MovementGeneratorType.Point && Data == EventId.SmartEscortLastOCCPoint) || MovementType == MovementGeneratorType.Follow)
me.ClearUnitState(UnitState.Evade); me.ClearUnitState(UnitState.Evade);
GetScript().ProcessEventsFor(SmartEvents.Movementinform, null, (uint)MovementType, Data); GetScript().ProcessEventsFor(SmartEvents.Movementinform, null, (uint)MovementType, Data);
if (!HasEscortState(SmartEscortState.Escorting)) if (MovementType != MovementGeneratorType.Point || !HasEscortState(SmartEscortState.Escorting))
return; return;
if (MovementType == MovementGeneratorType.Waypoint || (MovementType == MovementGeneratorType.Point && Data == EventId.SmartEscortLastOCCPoint))
MovepointReached(Data); MovepointReached(Data);
} }
@@ -509,7 +486,6 @@ namespace Game.AI
me.GetMotionMaster().MoveFollow(target, mFollowDist, mFollowAngle); me.GetMotionMaster().MoveFollow(target, mFollowDist, mFollowAngle);
// evade is not cleared in MoveFollow, so we can't keep it // evade is not cleared in MoveFollow, so we can't keep it
me.ClearUnitState(UnitState.Evade); me.ClearUnitState(UnitState.Evade);
GetScript().OnReset();
} }
else if (owner) else if (owner)
{ {
@@ -520,8 +496,8 @@ namespace Game.AI
me.GetMotionMaster().MoveTargetedHome(); me.GetMotionMaster().MoveTargetedHome();
} }
if (!me.HasUnitState(UnitState.Evade)) if (!HasEscortState(SmartEscortState.Escorting)) //dont mess up escort movement after combat
GetScript().OnReset(); SetRun(mRun);
} }
public override void MoveInLineOfSight(Unit who) public override void MoveInLineOfSight(Unit who)
@@ -625,13 +601,25 @@ namespace Game.AI
me.InterruptNonMeleeSpells(false); // must be before ProcessEvents me.InterruptNonMeleeSpells(false); // must be before ProcessEvents
GetScript().ProcessEventsFor(SmartEvents.Aggro, victim); GetScript().ProcessEventsFor(SmartEvents.Aggro, victim);
if (!IsAIControlled())
return;
mLastOOCPos = me.GetPosition();
SetRun(mRun);
if (me.GetMotionMaster().GetMotionSlotType(MovementSlot.Active) == MovementGeneratorType.Point)
me.GetMotionMaster().MovementExpired();
} }
public override void JustDied(Unit killer) public override void JustDied(Unit killer)
{ {
GetScript().ProcessEventsFor(SmartEvents.Death, killer); GetScript().ProcessEventsFor(SmartEvents.Death, killer);
if (HasEscortState(SmartEscortState.Escorting)) if (HasEscortState(SmartEscortState.Escorting))
{
EndPath(true); EndPath(true);
me.StopMoving();//force stop
me.GetMotionMaster().MoveIdle();
}
} }
public override void KilledUnit(Unit victim) public override void KilledUnit(Unit victim)
@@ -646,28 +634,12 @@ namespace Game.AI
public override void AttackStart(Unit who) public override void AttackStart(Unit who)
{ {
// dont allow charmed npcs to act on their own
if (me.HasUnitFlag(UnitFlags.PlayerControlled))
{
if (who && mCanAutoAttack)
me.Attack(who, true);
return;
}
if (who && me.Attack(who, me.IsWithinMeleeRange(who))) if (who && me.Attack(who, me.IsWithinMeleeRange(who)))
{ {
if (mCanCombatMove) if (mCanCombatMove)
{
SetRun(mRun);
MovementGeneratorType type = me.GetMotionMaster().GetMotionSlotType(MovementSlot.Active);
if (type == MovementGeneratorType.Waypoint || type == MovementGeneratorType.Point)
me.StopMoving();
me.GetMotionMaster().MoveChase(who); me.GetMotionMaster().MoveChase(who);
} }
} }
}
public override void SpellHit(Unit caster, SpellInfo spell) public override void SpellHit(Unit caster, SpellInfo spell)
{ {
@@ -852,9 +824,12 @@ namespace Game.AI
} }
else else
{ {
if (me.HasUnitState(UnitState.ConfusedMove | UnitState.FleeingMove))
return;
me.GetMotionMaster().MovementExpired();
me.GetMotionMaster().Clear(true);
me.StopMoving(); me.StopMoving();
if (me.GetMotionMaster().GetCurrentMovementGeneratorType() == MovementGeneratorType.Chase)
me.GetMotionMaster().Clear(false);
me.GetMotionMaster().MoveIdle(); me.GetMotionMaster().MoveIdle();
} }
} }
@@ -951,6 +926,8 @@ namespace Game.AI
public void StartDespawn() { mDespawnState = 2; } public void StartDespawn() { mDespawnState = 2; }
uint GetWPCount() { return (uint)mWayPoints?.Count; }
bool mIsCharmed; bool mIsCharmed;
uint mFollowCreditType; uint mFollowCreditType;
uint mFollowArrivedTimer; uint mFollowArrivedTimer;
@@ -961,13 +938,14 @@ namespace Game.AI
float mFollowAngle; float mFollowAngle;
SmartScript mScript = new SmartScript(); SmartScript mScript = new SmartScript();
List<WayPoint> mWayPoints;
SmartEscortState mEscortState; SmartEscortState mEscortState;
uint mCurrentWPID; uint mCurrentWPID;
uint mLastWPIDReached;
bool mWPReached; bool mWPReached;
bool mOOCReached;
bool m_Ended;
uint mWPPauseTimer; uint mWPPauseTimer;
uint mEscortNPCFlags; WayPoint mLastWP;
Position mLastOOCPos;//set on enter combat
bool mCanRepeatPath; bool mCanRepeatPath;
bool mRun; bool mRun;
bool mEvadeDisabled; bool mEvadeDisabled;
@@ -976,7 +954,6 @@ namespace Game.AI
bool mForcedPaused; bool mForcedPaused;
uint mInvincibilityHpLevel; uint mInvincibilityHpLevel;
WaypointPath _path = new WaypointPath();
uint mDespawnTime; uint mDespawnTime;
uint mRespawnTime; uint mRespawnTime;
uint mDespawnState; uint mDespawnState;
+14 -11
View File
@@ -248,13 +248,7 @@ namespace Game.AI
last_id++; last_id++;
WayPoint point = new WayPoint(); waypoint_map.Add(entry, new WayPoint(id, x, y, z));
point.id = id;
point.x = x;
point.y = y;
point.z = z;
waypoint_map.Add(entry, point);
last_entry = entry; last_entry = entry;
total++; total++;
@@ -1163,6 +1157,7 @@ namespace Game.AI
break; break;
} }
case SmartActions.StartClosestWaypoint:
case SmartActions.Follow: case SmartActions.Follow:
case SmartActions.SetOrientation: case SmartActions.SetOrientation:
case SmartActions.StoreTargetList: case SmartActions.StoreTargetList:
@@ -1544,10 +1539,18 @@ namespace Game.AI
public class WayPoint public class WayPoint
{ {
public uint id; public WayPoint(uint id, float x, float y, float z)
public float x; {
public float y; Id = id;
public float z; X = x;
Y = y;
Z = z;
}
public uint Id;
public float X;
public float Y;
public float Z;
} }
public class SmartScriptHolder public class SmartScriptHolder
@@ -2293,6 +2293,58 @@ namespace Game.AI
Global.GameEventMgr.StartEvent(eventId, true); Global.GameEventMgr.StartEvent(eventId, true);
break; break;
} }
case SmartActions.StartClosestWaypoint:
{
uint[] waypoints = new uint[SharedConst.SmartActionParamCount];
waypoints[0] = e.Action.closestWaypointFromList.wp1;
waypoints[1] = e.Action.closestWaypointFromList.wp2;
waypoints[2] = e.Action.closestWaypointFromList.wp3;
waypoints[3] = e.Action.closestWaypointFromList.wp4;
waypoints[4] = e.Action.closestWaypointFromList.wp5;
waypoints[5] = e.Action.closestWaypointFromList.wp6;
float distanceToClosest = float.MaxValue;
WayPoint closestWp = null;
var targets = GetTargets(e, unit);
if (targets != null)
{
foreach (var obj in targets)
{
Creature target = obj.ToCreature();
if (target != null)
{
if (IsSmart(target))
{
for (byte i = 0; i < SharedConst.SmartActionParamCount; i++)
{
if (waypoints[i] == 0)
continue;
var path = Global.SmartAIMgr.GetPath(waypoints[i]);
if (path == null || path.Empty())
continue;
WayPoint wp = path[0];
if (wp != null)
{
float distToThisPath = target.GetDistance(wp.X, wp.Y, wp.Z);
if (distToThisPath < distanceToClosest)
{
distanceToClosest = distToThisPath;
closestWp = wp;
}
}
}
if (closestWp != null)
((SmartAI)target.GetAI()).StartPath(false, closestWp.Id, true);
}
}
}
}
break;
}
case SmartActions.RandomSound: case SmartActions.RandomSound:
{ {
uint[] sounds = new uint[SharedConst.SmartActionParamCount - 1]; uint[] sounds = new uint[SharedConst.SmartActionParamCount - 1];
@@ -37,22 +37,11 @@ namespace Game.Movement
isArrivalDone = false; isArrivalDone = false;
pathId = pathid; pathId = pathid;
repeating = _repeating; repeating = _repeating;
loadedFromDB = true;
}
public WaypointMovementGenerator(WaypointPath _path, bool _repeating = true)
{
nextMoveTime = new TimeTrackerSmall(0);
isArrivalDone = false;
pathId = 0;
repeating = _repeating;
loadedFromDB = false;
path = _path;
} }
public override void DoReset(Creature creature) public override void DoReset(Creature creature)
{ {
if (!Stopped()) creature.AddUnitState(UnitState.Roaming | UnitState.RoamingMove);
StartMoveNow(creature); StartMoveNow(creature);
} }
@@ -65,13 +54,11 @@ namespace Game.Movement
public override void DoInitialize(Creature creature) public override void DoInitialize(Creature creature)
{ {
LoadPath(creature); LoadPath(creature);
creature.AddUnitState(UnitState.Roaming | UnitState.RoamingMove);
} }
public override bool DoUpdate(Creature creature, uint time_diff) public override bool DoUpdate(Creature creature, uint time_diff)
{ {
if (!creature || !creature.IsAlive())
return false;
// Waypoint movement can be switched on/off // Waypoint movement can be switched on/off
// This is quite handy for escort quests and other stuff // This is quite handy for escort quests and other stuff
if (creature.HasUnitState(UnitState.NotMove)) if (creature.HasUnitState(UnitState.NotMove))
@@ -81,13 +68,13 @@ namespace Game.Movement
} }
// prevent a crash at empty waypoint path. // prevent a crash at empty waypoint path.
if (path == null || path.nodes.Empty()) if (path == null || path.Empty())
return false; return false;
if (Stopped()) if (Stopped())
{ {
if (CanMove((int)time_diff)) if (CanMove((int)time_diff))
return StartMoveNow(creature); return StartMove(creature);
} }
else else
{ {
@@ -96,43 +83,13 @@ namespace Game.Movement
creature.SetHomePosition(creature.GetPosition()); creature.SetHomePosition(creature.GetPosition());
if (creature.IsStopped()) if (creature.IsStopped())
Stop(loadedFromDB ? WorldConfig.GetIntValue(WorldCfg.CreatureStopForPlayer) : 2 * Time.Hour * Time.InMilliseconds); Stop(WorldConfig.GetIntValue(WorldCfg.CreatureStopForPlayer));
else if (creature.moveSpline.Finalized()) else if (creature.moveSpline.Finalized())
{ {
OnArrived(creature); OnArrived(creature);
isArrivalDone = true;
if (!Stopped())
{
if (creature.IsStopped())
Stop(loadedFromDB ? WorldConfig.GetIntValue(WorldCfg.CreatureStopForPlayer) : 2 * Time.Hour * Time.InMilliseconds);
else
return StartMove(creature); return StartMove(creature);
} }
} }
else
{
// speed changed during path execution, calculate remaining path and launch it once more
if (recalculateSpeed)
{
recalculateSpeed = false;
if (!Stopped())
return StartMove(creature);
}
else
{
uint pointId = (uint)creature.moveSpline.currentPathIdx();
if (pointId > currentNode)
{
OnArrived(creature);
currentNode = pointId;
FormationMove(creature);
}
}
}
}
return true; return true;
} }
@@ -143,6 +100,20 @@ namespace Game.Movement
creature.GetAI().MovementInform(MovementGeneratorType.Waypoint, currentNode); creature.GetAI().MovementInform(MovementGeneratorType.Waypoint, currentNode);
} }
public override bool GetResetPosition(Unit u, out float x, out float y, out float z)
{
x = y = z = 0;
// prevent a crash at empty waypoint path.
if (path == null || path.Empty())
return false;
var node = path.LookupByIndex((int)currentNode);
x = node.x;
y = node.y;
z = node.z;
return true;
}
void Stop(int time) void Stop(int time)
{ {
nextMoveTime.Reset(time); nextMoveTime.Reset(time);
@@ -159,18 +130,15 @@ namespace Game.Movement
return nextMoveTime.Passed(); return nextMoveTime.Passed();
} }
bool StartMoveNow(Creature creature) void StartMoveNow(Creature creature)
{ {
nextMoveTime.Reset(0); nextMoveTime.Reset(0);
return StartMove(creature); StartMove(creature);
} }
bool StartMove(Creature creature) bool StartMove(Creature creature)
{ {
if (!creature || !creature.IsAlive()) if (path == null || path.Empty())
return false;
if (path == null || path.nodes.Empty())
return false; return false;
if (Stopped()) if (Stopped())
@@ -180,9 +148,9 @@ namespace Game.Movement
if (isArrivalDone) if (isArrivalDone)
{ {
if ((currentNode == path.nodes.Count - 1) && !repeating) // If that's our last waypoint if ((currentNode == path.Count - 1) && !repeating) // If that's our last waypoint
{ {
WaypointNode waypoint = path.nodes.LookupByIndex((int)currentNode); WaypointData waypoint = path.LookupByIndex((int)currentNode);
float x = waypoint.x; float x = waypoint.x;
float y = waypoint.y; float y = waypoint.y;
@@ -206,43 +174,21 @@ namespace Game.Movement
// else if (vehicle) - this should never happen, vehicle offsets are const // else if (vehicle) - this should never happen, vehicle offsets are const
} }
creature.GetMotionMaster().Initialize();
return false; return false;
} }
currentNode = (uint)((currentNode + 1) % path.nodes.Count); currentNode = (uint)((currentNode + 1) % path.Count);
} }
float finalOrient = 0.0f; var node = path.LookupByIndex((int)currentNode);
WaypointMoveType finalMove = WaypointMoveType.Walk;
List<Vector3> pathing = new List<Vector3>();
pathing.Add(new Vector3(creature.GetPositionX(), creature.GetPositionY(), creature.GetPositionZ()));
for (int i = (int)currentNode; i < path.nodes.Count; ++i)
{
WaypointNode waypoint = path.nodes.LookupByIndex(i);
pathing.Add(new Vector3(waypoint.x, waypoint.y, waypoint.z));
finalOrient = waypoint.orientation;
finalMove = waypoint.moveType;
if (waypoint.delay != 0)
break;
}
// if we have only 1 point, only current position, we shall return
if (pathing.Count < 2)
return false;
isArrivalDone = false; isArrivalDone = false;
recalculateSpeed = false;
creature.AddUnitState(UnitState.RoamingMove); creature.AddUnitState(UnitState.RoamingMove);
MoveSplineInit init = new MoveSplineInit(creature);
var node = path.nodes.LookupByIndex((int)currentNode);
Position formationDest = new Position(node.x, node.y, node.z, 0.0f); Position formationDest = new Position(node.x, node.y, node.z, 0.0f);
MoveSplineInit init = new MoveSplineInit(creature);
//! If creature is on transport, we assume waypoints set in DB are already transport offsets //! If creature is on transport, we assume waypoints set in DB are already transport offsets
if (transportPath) if (transportPath)
@@ -253,8 +199,15 @@ namespace Game.Movement
trans.CalculatePassengerPosition(ref formationDest.posX, ref formationDest.posY, ref formationDest.posZ, ref formationDest.Orientation); trans.CalculatePassengerPosition(ref formationDest.posX, ref formationDest.posY, ref formationDest.posZ, ref formationDest.Orientation);
} }
init.MovebyPath(pathing.ToArray(), (int)currentNode); //! Do not use formationDest here, MoveTo requires transport offsets due to DisableTransportPathTransformations() call
switch (finalMove) //! but formationDest contains global coordinates
init.MoveTo(node.x, node.y, node.z);
//! Accepts angles such as 0.00001 and -0.00001, 0 must be ignored, default value in waypoint table
if (node.orientation != 0 && node.delay != 0)
init.SetFacing(node.orientation);
switch (node.moveType)
{ {
case WaypointMoveType.Land: case WaypointMoveType.Land:
init.SetAnimation(AnimType.ToGround); init.SetAnimation(AnimType.ToGround);
@@ -270,27 +223,24 @@ namespace Game.Movement
break; break;
} }
if (finalOrient != 0.0f)
init.SetFacing(finalOrient);
init.Launch(); init.Launch();
//Call for creature group update //Call for creature group update
if (creature.GetFormation() != null && creature.GetFormation().getLeader() == creature) if (creature.GetFormation() != null && creature.GetFormation().getLeader() == creature)
{
creature.SetWalk(node.moveType != WaypointMoveType.Run);
creature.GetFormation().LeaderMoveTo(formationDest.posX, formationDest.posY, formationDest.posZ); creature.GetFormation().LeaderMoveTo(formationDest.posX, formationDest.posY, formationDest.posZ);
}
return true; return true;
} }
void LoadPath(Creature creature) void LoadPath(Creature creature)
{
if (loadedFromDB)
{ {
if (pathId == 0) if (pathId == 0)
pathId = creature.GetWaypointPath(); pathId = creature.GetWaypointPath();
path = Global.WaypointMgr.GetPath(pathId); path = Global.WaypointMgr.GetPath(pathId);
}
if (path == null) if (path == null)
{ {
@@ -299,58 +249,37 @@ namespace Game.Movement
return; return;
} }
if (!Stopped())
StartMoveNow(creature); StartMoveNow(creature);
} }
void OnArrived(Creature creature) void OnArrived(Creature creature)
{ {
if (path == null || path.nodes.Empty()) if (path == null || path.Empty())
return; return;
WaypointNode waypoint = path.nodes.LookupByIndex((int)currentNode); if (isArrivalDone)
if (waypoint.delay != 0) return;
{
creature.ClearUnitState(UnitState.RoamingMove);
Stop((int)waypoint.delay);
}
if (waypoint.eventId != 0 && RandomHelper.URand(0, 99) < waypoint.eventChance) isArrivalDone = true;
var current = path.LookupByIndex((int)currentNode);
if (current.eventId != 0 && RandomHelper.URand(0, 99) < current.eventChance)
{ {
Log.outDebug(LogFilter.Unit, "Creature movement start script {0} at point {1} for {2}.", waypoint.eventId, currentNode, creature.GetGUID()); Log.outDebug(LogFilter.Unit, "Creature movement start script {0} at point {1} for {2}.", current.eventId, currentNode, creature.GetGUID());
creature.ClearUnitState(UnitState.RoamingMove); creature.ClearUnitState(UnitState.RoamingMove);
creature.GetMap().ScriptsStart(ScriptsType.Waypoint, waypoint.eventId, creature, null); creature.GetMap().ScriptsStart(ScriptsType.Waypoint, current.eventId, creature, null);
} }
// Inform script // Inform script
MovementInform(creature); MovementInform(creature);
creature.UpdateWaypointID(currentNode); creature.UpdateWaypointID(currentNode);
creature.SetWalk(waypoint.moveType != WaypointMoveType.Run); if (current.delay != 0)
}
void FormationMove(Creature creature)
{ {
if (path == null || path.nodes.Empty()) creature.ClearUnitState(UnitState.RoamingMove);
return; Stop((int)current.delay);
bool transportPath = creature.GetTransport() != null;
WaypointNode waypoint = path.nodes.LookupByIndex((int)currentNode);
Position formationDest = new Position(waypoint.x, waypoint.y, waypoint.z, 0.0f);
//! If creature is on transport, we assume waypoints set in DB are already transport offsets
if (transportPath)
{
ITransport trans = creature.GetDirectTransport();
if (trans != null)
trans.CalculatePassengerPosition(ref formationDest.posX, ref formationDest.posY, ref formationDest.posZ, ref formationDest.Orientation);
} }
// Call for creature group update
if (creature.GetFormation() != null && creature.GetFormation().getLeader() == creature)
creature.GetFormation().LeaderMoveTo(formationDest.posX, formationDest.posY, formationDest.posZ);
} }
public override MovementGeneratorType GetMovementGeneratorType() public override MovementGeneratorType GetMovementGeneratorType()
@@ -358,20 +287,14 @@ namespace Game.Movement
return MovementGeneratorType.Waypoint; return MovementGeneratorType.Waypoint;
} }
public TimeTrackerSmall GetTrackerTimer() { return nextMoveTime; }
public void UnitSpeedChanged() { recalculateSpeed = true; }
public uint GetCurrentNode() { return currentNode; } public uint GetCurrentNode() { return currentNode; }
TimeTrackerSmall nextMoveTime; TimeTrackerSmall nextMoveTime;
bool recalculateSpeed;
bool isArrivalDone; bool isArrivalDone;
uint pathId; uint pathId;
bool repeating; bool repeating;
bool loadedFromDB; List<WaypointData> path;
WaypointPath path;
uint currentNode; uint currentNode;
} }
@@ -458,12 +381,12 @@ namespace Game.Movement
MoveSplineInit init = new MoveSplineInit(owner); MoveSplineInit init = new MoveSplineInit(owner);
uint end = GetPathAtMapEnd(); uint end = GetPathAtMapEnd();
init.args.path = new Vector3[end]; init.args.path = new Vector3[end];
for (int i = i_currentNode; i != end; ++i) for (int i = (int)GetCurrentNode(); i != end; ++i)
{ {
Vector3 vertice = new Vector3(i_path[i].Loc.X, i_path[i].Loc.Y, i_path[i].Loc.Z); Vector3 vertice = new Vector3(i_path[i].Loc.X, i_path[i].Loc.Y, i_path[i].Loc.Z);
init.args.path[i] = vertice; init.args.path[i] = vertice;
} }
init.SetFirstPointId(i_currentNode); init.SetFirstPointId((int)GetCurrentNode());
init.SetFly(); init.SetFly();
init.SetSmooth(); init.SetSmooth();
init.SetUncompressed(); init.SetUncompressed();
-5
View File
@@ -598,11 +598,6 @@ namespace Game.Movement
StartMovement(new WaypointMovementGenerator(path_id, repeatable), MovementSlot.Idle); StartMovement(new WaypointMovementGenerator(path_id, repeatable), MovementSlot.Idle);
} }
public void MovePath(WaypointPath path, bool repeatable)
{
StartMovement(new WaypointMovementGenerator(path, repeatable), MovementSlot.Idle);
}
void MoveRotate(uint time, RotateDirection direction) void MoveRotate(uint time, RotateDirection direction)
{ {
if (time == 0) if (time == 0)
+10 -32
View File
@@ -25,7 +25,7 @@ namespace Game
{ {
WaypointManager() WaypointManager()
{ {
_waypointStore = new Dictionary<uint, WaypointPath>(); _waypointStore = new MultiMap<uint, WaypointData>();
} }
public void Load() public void Load()
@@ -55,7 +55,7 @@ namespace Game
GridDefines.NormalizeMapCoord(ref x); GridDefines.NormalizeMapCoord(ref x);
GridDefines.NormalizeMapCoord(ref y); GridDefines.NormalizeMapCoord(ref y);
WaypointNode wp = new WaypointNode(); WaypointData wp = new WaypointData();
wp.id = result.Read<uint>(1); wp.id = result.Read<uint>(1);
wp.x = x; wp.x = x;
wp.y = y; wp.y = y;
@@ -73,10 +73,7 @@ namespace Game
wp.eventId = result.Read<uint>(8); wp.eventId = result.Read<uint>(8);
wp.eventChance = result.Read<byte>(9); wp.eventChance = result.Read<byte>(9);
if (!_waypointStore.ContainsKey(pathId)) _waypointStore.Add(pathId, wp);
_waypointStore[pathId] = new WaypointPath();
_waypointStore[pathId].nodes.Add(wp);
++count; ++count;
} while (result.NextRow()); } while (result.NextRow());
@@ -104,7 +101,7 @@ namespace Game
GridDefines.NormalizeMapCoord(ref x); GridDefines.NormalizeMapCoord(ref x);
GridDefines.NormalizeMapCoord(ref y); GridDefines.NormalizeMapCoord(ref y);
WaypointNode wp = new WaypointNode(); WaypointData wp = new WaypointData();
wp.id = result.Read<uint>(0); wp.id = result.Read<uint>(0);
wp.x = x; wp.x = x;
wp.y = y; wp.y = y;
@@ -122,27 +119,24 @@ namespace Game
wp.eventId = result.Read<uint>(7); wp.eventId = result.Read<uint>(7);
wp.eventChance = result.Read<byte>(8); wp.eventChance = result.Read<byte>(8);
if (!_waypointStore.ContainsKey(id)) _waypointStore.Add(id, wp);
_waypointStore[id] = new WaypointPath();
_waypointStore[id].nodes.Add(wp);
} }
while (result.NextRow()); while (result.NextRow());
} }
public WaypointPath GetPath(uint id) public List<WaypointData> GetPath(uint id)
{ {
return _waypointStore.LookupByKey(id); return _waypointStore.LookupByKey(id);
} }
Dictionary<uint, WaypointPath> _waypointStore; MultiMap<uint, WaypointData> _waypointStore;
} }
public class WaypointNode public class WaypointData
{ {
public WaypointNode() { moveType = WaypointMoveType.Run; } public WaypointData() { moveType = WaypointMoveType.Run; }
public WaypointNode(uint _id, float _x, float _y, float _z, float _orientation = 0.0f, uint _delay = 0) public WaypointData(uint _id, float _x, float _y, float _z, float _orientation = 0.0f, uint _delay = 0)
{ {
id = _id; id = _id;
x = _x; x = _x;
@@ -163,22 +157,6 @@ namespace Game
public byte eventChance; public byte eventChance;
} }
public class WaypointPath
{
public WaypointPath()
{
nodes = new List<WaypointNode>();
}
public WaypointPath(uint _id, List<WaypointNode> _nodes)
{
id = _id;
nodes = _nodes;
}
public List<WaypointNode> nodes;
public uint id;
}
public enum WaypointMoveType public enum WaypointMoveType
{ {
Walk, Walk,