Skip to content

Commit 8574ac4

Browse files
authored
enhancement: NPC random movement with walk ranges (#2739)
NPCs now wander longer paths using randomized walk ranges based on their sight distance, with smoother direction changes and better obstacle handling. MoveRandomly has been refactored as a method for better organization. Signed-off-by: Fernando Arzola <[email protected]>
1 parent 4b9c062 commit 8574ac4

File tree

1 file changed

+47
-41
lines changed
  • Intersect.Server.Core/Entities

1 file changed

+47
-41
lines changed

Intersect.Server.Core/Entities/Npc.cs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public Entity DamageMapHighest
6565

6666
//Moving
6767
public long LastRandomMove;
68+
private byte _randomMoveRange;
6869

6970
//Pathfinding
7071
private Pathfinder mPathFinder;
@@ -1158,54 +1159,25 @@ public override void Update(long timeMs)
11581159

11591160
CheckForResetLocation();
11601161

1161-
//Move randomly
1162-
if (targetMap != Guid.Empty)
1163-
{
1164-
return;
1165-
}
1166-
1167-
if (LastRandomMove >= Timing.Global.Milliseconds || IsCasting)
1168-
{
1169-
return;
1170-
}
1171-
1172-
if (Descriptor.Movement == (int)NpcMovement.StandStill)
1173-
{
1174-
LastRandomMove = Timing.Global.Milliseconds + Randomization.Next(1000, 3000);
1175-
1176-
return;
1177-
}
1178-
else if (Descriptor.Movement == (int)NpcMovement.TurnRandomly)
1162+
if (targetMap != Guid.Empty || LastRandomMove >= Timing.Global.Milliseconds || IsCasting)
11791163
{
1180-
ChangeDir(Randomization.NextDirection());
1181-
LastRandomMove = Timing.Global.Milliseconds + Randomization.Next(1000, 3000);
1182-
11831164
return;
11841165
}
11851166

1186-
var i = Randomization.Next(0, 1);
1187-
if (i == 0)
1167+
switch (Descriptor.Movement)
11881168
{
1189-
var direction = Randomization.NextDirection();
1190-
if (CanMoveInDirection(direction))
1191-
{
1192-
//check if NPC is snared or stunned
1193-
foreach (var status in CachedStatuses)
1194-
{
1195-
if (status.Type == SpellEffect.Stun ||
1196-
status.Type == SpellEffect.Snare ||
1197-
status.Type == SpellEffect.Sleep)
1198-
{
1199-
return;
1200-
}
1201-
}
1202-
1203-
Move(direction, null);
1204-
}
1169+
case (int)NpcMovement.StandStill:
1170+
LastRandomMove = Timing.Global.Milliseconds + Randomization.Next(1000, 3000);
1171+
return;
1172+
case (int)NpcMovement.TurnRandomly:
1173+
ChangeDir(Randomization.NextDirection());
1174+
LastRandomMove = Timing.Global.Milliseconds + Randomization.Next(1000, 3000);
1175+
return;
1176+
case (int)NpcMovement.MoveRandomly:
1177+
MoveRandomly();
1178+
break;
12051179
}
12061180

1207-
LastRandomMove = Timing.Global.Milliseconds + Randomization.Next(1000, 3000);
1208-
12091181
if (fleeing)
12101182
{
12111183
LastRandomMove = Timing.Global.Milliseconds + (long)GetMovementTime();
@@ -1242,6 +1214,40 @@ public override void Update(long timeMs)
12421214
}
12431215
}
12441216

1217+
private void MoveRandomly()
1218+
{
1219+
if (_randomMoveRange <= 0)
1220+
{
1221+
Dir = Randomization.NextDirection();
1222+
LastRandomMove = Timing.Global.Milliseconds + Randomization.Next(1000, 2000);
1223+
_randomMoveRange = (byte)Randomization.Next(0, Descriptor.SightRange + Randomization.Next(0, 3));
1224+
}
1225+
else if (CanMoveInDirection(Dir))
1226+
{
1227+
foreach (var status in CachedStatuses)
1228+
{
1229+
if (status.Type is SpellEffect.Stun or SpellEffect.Snare or SpellEffect.Sleep)
1230+
{
1231+
return;
1232+
}
1233+
}
1234+
1235+
Move(Dir, null);
1236+
LastRandomMove = Timing.Global.Milliseconds + (long)GetMovementTime();
1237+
1238+
if (_randomMoveRange <= Randomization.Next(0, 3))
1239+
{
1240+
Dir = Randomization.NextDirection();
1241+
}
1242+
1243+
_randomMoveRange--;
1244+
}
1245+
else
1246+
{
1247+
Dir = Randomization.NextDirection();
1248+
}
1249+
}
1250+
12451251
/// <summary>
12461252
/// Resets the NPCs position to be "pulled" from
12471253
/// </summary>

0 commit comments

Comments
 (0)