Skip to content

Commit

Permalink
Fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Jun 9, 2024
1 parent f59ed07 commit dc292cd
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 68 deletions.
3 changes: 0 additions & 3 deletions GameServer/Territory/Territory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 23 additions & 24 deletions GameServer/_scripts/AmteScripts/GameObjects/FollowingFriendMob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -112,6 +112,7 @@ public override bool WhisperReceive(GameLiving source, string str)
}
return true;
}

public override bool AddToWorld()
{
WaitingInArea = false;
Expand Down Expand Up @@ -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)
{
Expand All @@ -225,15 +226,15 @@ public void ResetFriendMobs()
foreach (FollowingFriendMob mob in group.NPCs.OfType<FollowingFriendMob>())
{
if (mob.PlayerFollow != null)
mob.ResetFriendMob();
mob.ResetSelf();
}
}
}

ResetFriendMob();
ResetSelf();
}

public void ResetFriendMob()
private void ResetSelf()
{
if (WaitingInArea == true && PlayerFollow != null)
{
Expand All @@ -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();
Expand Down Expand Up @@ -316,7 +313,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer)
if (PlayerFollow == null)
{
Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(null));
Reset();
ResetFollow();
return 0;
}

Expand All @@ -330,7 +327,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer)
PlayerFollow = null;
StopFollowing();
Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(playerFollow));
Reset();
ResetFollow();
return 0;
}

Expand All @@ -340,7 +337,7 @@ protected override int FollowTimerCallback(RegionTimer callingTimer)
PlayerFollow = null;
StopFollowing();
Notify(GameNPCEvent.FollowLostTarget, this, new FollowLostTargetEventArgs(playerFollow));
Reset();
ResetFollow();
return 0;
}
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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;
}

Expand Down Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions GameServer/gameobjects/GameLiving.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6004,6 +6004,12 @@ public virtual void UpdateHealthManaEndu()
/// </summary>
public int MovementStartTick
=> Motion.StartTimeInMilliSeconds;

public override Position Position
{
get => Motion.CurrentPosition;
set => Motion = Motion.Create(value, Motion.Destination, Motion.Speed);
}

/// <summary>
/// True if the living is moving, else false.
Expand Down
46 changes: 12 additions & 34 deletions GameServer/gameobjects/GameNPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<GameNPC> goToNextNodeCallback)
Expand All @@ -1611,7 +1586,7 @@ private void WalkTo(Coordinate destination, short speed, Action<GameNPC> goToNex
if (speed <= 0)
return;

Motion = Geometry.Motion.Create(Position, destination,speed);
Motion = Geometry.Motion.Create(Position, destination, speed);

if ((int)Motion.RemainingDistance == 0)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion GameServer/gameobjects/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions GameServer/packets/Server/PacketLib1124.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions GameServer/quests/JsonQuests/Goals/BringAFriendGoal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public override Dictionary<string, object> GetDatabaseJsonObject()
public override void AbortGoal(PlayerQuest questData)
{
if (lastFriend != null)
if (hasMobGroup)
lastFriend.ResetFriendMobs();
else lastFriend.ResetFriendMob();
lastFriend.ResetFollow();
base.AbortGoal(questData);
}

Expand Down
5 changes: 4 additions & 1 deletion GameServer/world/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit dc292cd

Please sign in to comment.