From dc292cd6e7cc64d9440b3a27b3292ad74cd669f0 Mon Sep 17 00:00:00 2001 From: Amber Ehrlich Date: Sun, 9 Jun 2024 18:01:06 -0400 Subject: [PATCH] Fix some issues --- GameServer/Territory/Territory.cs | 3 -- .../GameObjects/FollowingFriendMob.cs | 47 +++++++++---------- GameServer/gameobjects/GameLiving.cs | 6 +++ GameServer/gameobjects/GameNPC.cs | 46 +++++------------- GameServer/gameobjects/GameObject.cs | 2 +- GameServer/packets/Server/PacketLib1124.cs | 2 - .../JsonQuests/Goals/BringAFriendGoal.cs | 4 +- GameServer/world/Region.cs | 5 +- 8 files changed, 47 insertions(+), 68 deletions(-) diff --git a/GameServer/Territory/Territory.cs b/GameServer/Territory/Territory.cs index 17310f74..f23ab081 100644 --- a/GameServer/Territory/Territory.cs +++ b/GameServer/Territory/Territory.cs @@ -1078,10 +1078,7 @@ public GameNPC AddMercenary(GamePlayer buyer, NpcTemplate template) npc = (GameNPC)gasm.CreateInstance(template.ClassType, false); // Propagate exception to the caller npc.LoadTemplate(template); } - npc.CurrentRegion = buyer.CurrentRegion; - npc.CurrentRegionID = buyer.CurrentRegionID; npc.Position = buyer.Position; - npc.Heading = buyer.Heading; npc.GuildName = OwnerGuild?.Name ?? template.GuildName ?? string.Empty; npc.Flags |= GameNPC.eFlags.MERCENARY | GameNPC.eFlags.NORESPAWN; npc.FlagsDb = (uint)npc.Flags; diff --git a/GameServer/_scripts/AmteScripts/GameObjects/FollowingFriendMob.cs b/GameServer/_scripts/AmteScripts/GameObjects/FollowingFriendMob.cs index c0bd2fb9..822b5889 100644 --- a/GameServer/_scripts/AmteScripts/GameObjects/FollowingFriendMob.cs +++ b/GameServer/_scripts/AmteScripts/GameObjects/FollowingFriendMob.cs @@ -93,7 +93,7 @@ public override bool WhisperReceive(GameLiving source, string str) player.Out.SendMessage(text, eChatType.CT_System, eChatLoc.CL_PopupWindow); } Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(player)); - Reset(); + ResetFollow(); } } else @@ -112,6 +112,7 @@ public override bool WhisperReceive(GameLiving source, string str) } return true; } + public override bool AddToWorld() { WaitingInArea = false; @@ -213,10 +214,10 @@ public override void DeleteFromDatabase() private void ResetTimer_Elapsed(object sender, ElapsedEventArgs e) { - ResetFriendMobs(); + ResetFollow(); } - public void ResetFriendMobs() + public void ResetFollow() { if (MobGroups != null) { @@ -225,15 +226,15 @@ public void ResetFriendMobs() foreach (FollowingFriendMob mob in group.NPCs.OfType()) { if (mob.PlayerFollow != null) - mob.ResetFriendMob(); + mob.ResetSelf(); } } } - - ResetFriendMob(); + + ResetSelf(); } - public void ResetFriendMob() + private void ResetSelf() { if (WaitingInArea == true && PlayerFollow != null) { @@ -248,16 +249,12 @@ public void ResetFriendMob() AddToWorld(); } - public override void Reset() - { - ResetFriendMobs(); - } - public override void Die(GameObject killer) { base.Die(killer); - ResetFriendMobs(); + ResetFollow(); } + public override void SaveIntoDatabase() { base.SaveIntoDatabase(); @@ -316,7 +313,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) if (PlayerFollow == null) { Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(null)); - Reset(); + ResetFollow(); return 0; } @@ -330,7 +327,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) PlayerFollow = null; StopFollowing(); Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(playerFollow)); - Reset(); + ResetFollow(); return 0; } @@ -340,7 +337,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) PlayerFollow = null; StopFollowing(); Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(playerFollow)); - Reset(); + ResetFollow(); return 0; } } @@ -361,7 +358,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) if (distanceToTarget <= m_followMinDist) { // Within minimum distance, nothing to do - //StopMoving(); + StopMoving(); TurnTo(followTarget); if (!wasInRange) @@ -387,6 +384,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) if (distanceToTarget <= m_followMinDist) { // Within minimum distance, nothing to do + StopMoving(); TurnTo(followTarget); if (!wasInRange) @@ -411,7 +409,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) targetPos += Vector.Create(Angle.Radians(angle), Util.Random(200, 300)); WaitingInArea = true; followTarget.Notify(GameLivingEvent.BringAFriend, followTarget, new BringAFriendArgs(this, true)); - PathTo(targetPos, 130); + WalkTo(targetPos, 130); return 0; } else if (WaitingInArea) @@ -420,18 +418,19 @@ protected override int FollowTimerCallback(RegionTimer callingTimer) } // follow on distance - diff = (diff / distanceToTarget) * m_followMinDist; - var newPos = followTarget.Coordinate - diff; + var distanceFactor = m_followMinDist / distanceToTarget; + var followOffset = diff * distanceFactor; + var newPos = followTarget.Coordinate - followOffset; if (followTarget == PlayerFollow) { var speed = MaxSpeed; if (IsWithinRadius(followTarget, 200)) speed = 0; - PathTo(newPos, speed); + WalkTo(newPos, speed); } else - PathTo(newPos, MaxSpeed); + WalkTo(newPos, MaxSpeed); return ServerProperties.Properties.GAMENPC_FOLLOWCHECK_TIME; } @@ -509,9 +508,9 @@ public override void Think() FollowingFriendMob body = (FollowingFriendMob)Body; //if player quits the game - if (body.PlayerFollow != null && body.PlayerFollow.ObjectState == eObjectState.Deleted) + if (body.PlayerFollow is { ObjectState: eObjectState.Deleted }) { - body.ResetFriendMobs(); + body.ResetFollow(); return; } if (!Body.IsCasting && CheckSpells(eCheckSpellType.Defensive)) diff --git a/GameServer/gameobjects/GameLiving.cs b/GameServer/gameobjects/GameLiving.cs index c663dff5..c9f97258 100644 --- a/GameServer/gameobjects/GameLiving.cs +++ b/GameServer/gameobjects/GameLiving.cs @@ -6004,6 +6004,12 @@ public virtual void UpdateHealthManaEndu() /// public int MovementStartTick => Motion.StartTimeInMilliSeconds; + + public override Position Position + { + get => Motion.CurrentPosition; + set => Motion = Motion.Create(value, Motion.Destination, Motion.Speed); + } /// /// True if the living is moving, else false. diff --git a/GameServer/gameobjects/GameNPC.cs b/GameServer/gameobjects/GameNPC.cs index d49deaa1..b5d2c99e 100644 --- a/GameServer/gameobjects/GameNPC.cs +++ b/GameServer/gameobjects/GameNPC.cs @@ -1455,13 +1455,18 @@ public virtual void WalkTo(Coordinate destination, short speed) if (speed <= 0) return; + Motion = Motion.Create(Position, destination, speed); + if ((int)Motion.RemainingDistance == 0) { _OnArrivedAtTarget(); return; } + + CancelWalkToTimer(); - _StartWalk(destination, speed); + Notify(GameNPCEvent.WalkTo, this, new WalkToEventArgs(destination, speed)); + StartArriveAtTargetAction((int)(Motion.RemainingDistance * 1000 / speed)); } private void StartArriveAtTargetAction(int requiredTicks) @@ -1567,37 +1572,7 @@ public void PathTo(Coordinate destination, short speed) } // Do the actual pathing bit: Walk towards the next pathing node - _WalkToPathNode(nextMotionTarget, speed); - } - - private void _WalkToPathNode(Coordinate node, short speed) - { - if (IsTurningDisabled) - return; - - if (speed > MaxSpeed) - speed = MaxSpeed; - - if (speed <= 0) - return; - - _StartWalk(node, speed); - } - - private void _StartWalk(Coordinate target, short speed) - { - CancelWalkToTimer(); - - if (IsMoving) - { - Position = Position; - } - - Motion = Motion.Create(Position, target, speed); - - var notifyDestination = TargetObject != null ? TargetObject.Coordinate : Coordinate.Nowhere; - Notify(GameNPCEvent.WalkTo, this, new WalkToEventArgs(notifyDestination, speed)); - StartArriveAtTargetAction((int)(Motion.RemainingDistance * 1000 / speed)); + WalkTo(nextMotionTarget, speed, npc => npc.PathTo(destination, speed)); } private void WalkTo(Coordinate destination, short speed, Action goToNextNodeCallback) @@ -1611,7 +1586,7 @@ private void WalkTo(Coordinate destination, short speed, Action goToNex if (speed <= 0) return; - Motion = Geometry.Motion.Create(Position, destination,speed); + Motion = Geometry.Motion.Create(Position, destination, speed); if ((int)Motion.RemainingDistance == 0) { @@ -3250,7 +3225,7 @@ public virtual void Reset() //Tolerance to check if we need to go home AGAIN, otherwise we might be told to go home //for a few units only and this may end before the next Arrive-At-Target Event is fired and in this case //We would never lose the state "IsReturningHome", which is then followed by other erros related to agro again to players - else if (!Util.IsNearDistance(Position.Coordinate, SpawnPosition.Coordinate, GameNPC.CONST_WALKTOTOLERANCE)) + else if (!IsWithinRadius(SpawnPosition.Coordinate, GameNPC.CONST_WALKTOTOLERANCE)) { WalkToSpawn(); return; @@ -3265,6 +3240,9 @@ public virtual void Reset() if (Orientation != SpawnPosition.Orientation) TurnTo(SpawnPosition.Orientation); + IsReturningHome = false; + IsResetting = false; + Notify(GameNPCEvent.NPCReset, this, EventArgs.Empty); } diff --git a/GameServer/gameobjects/GameObject.cs b/GameServer/gameobjects/GameObject.cs index e55abd46..ecc15a73 100644 --- a/GameServer/gameobjects/GameObject.cs +++ b/GameServer/gameobjects/GameObject.cs @@ -780,7 +780,7 @@ public virtual bool AddToWorld() // Should it be the case, currentZone will be null as well. if (CurrentZone == null || m_ObjectState == eObjectState.Active) { - Console.WriteLine($"AddToWorld: Zone does not exist"); + Console.WriteLine($"AddToWorld: Zone does not exist ({this.GetType()} ID: {InternalID})"); return false; } diff --git a/GameServer/packets/Server/PacketLib1124.cs b/GameServer/packets/Server/PacketLib1124.cs index d90404b9..0f28da39 100644 --- a/GameServer/packets/Server/PacketLib1124.cs +++ b/GameServer/packets/Server/PacketLib1124.cs @@ -199,8 +199,6 @@ public override void SendNPCCreate(GameNPC npc) pak.WriteInt((uint)npc.Position.X); pak.WriteInt((uint)npc.Position.Y); pak.WriteShort(speedZ); - pak.WriteShort(npc.Model); - pak.WriteByte(npc.Size); var model = npc.Model; var size = npc.Size; diff --git a/GameServer/quests/JsonQuests/Goals/BringAFriendGoal.cs b/GameServer/quests/JsonQuests/Goals/BringAFriendGoal.cs index 651a74d9..c835b993 100644 --- a/GameServer/quests/JsonQuests/Goals/BringAFriendGoal.cs +++ b/GameServer/quests/JsonQuests/Goals/BringAFriendGoal.cs @@ -60,9 +60,7 @@ public override Dictionary GetDatabaseJsonObject() public override void AbortGoal(PlayerQuest questData) { if (lastFriend != null) - if (hasMobGroup) - lastFriend.ResetFriendMobs(); - else lastFriend.ResetFriendMob(); + lastFriend.ResetFollow(); base.AbortGoal(questData); } diff --git a/GameServer/world/Region.cs b/GameServer/world/Region.cs index 2503b295..3c1f6700 100644 --- a/GameServer/world/Region.cs +++ b/GameServer/world/Region.cs @@ -1283,7 +1283,10 @@ public Zone GetZone(Coordinate coordinate) { var isInZone = zone.Offset.X <= coordinate.X && zone.Offset.Y <= coordinate.Y && (zone.Offset.X + zone.Width) > coordinate.X && (zone.Offset.Y + zone.Height) > coordinate.Y; - if (isInZone) return zone; + if (isInZone) + { + return zone; + } } return null; }