Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Perpetuum/Zones/Movements/WaypointMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,22 @@ public override void Start(Unit unit)
unit.Direction = unit.CurrentPosition.DirectionTo(_target);
unit.CurrentSpeed = 1.0;

_velocity = Vector2.Subtract(_target,unit.CurrentPosition.ToVector2());
_distance = Vector2.Abs(_velocity);
_velocity = Vector2.Normalize(_velocity);
// Vector to destination
var path = Vector2.Subtract(_target, unit.CurrentPosition.ToVector2());
// The absolute path along each coordinate
_distance = Vector2.Abs(path);
if (path.Length().IsApproximatelyEqual(0.0f))
{
// If the path is zero, then is already at the destination
Arrived = true;
// Velocity vector in the direction of the unit.
_velocity = MathHelper.DirectionToVector(unit.Direction);
}
else
{
// Velocity vector in the direction of the path.
_velocity = Vector2.Normalize(path);
}

base.Start(unit);
}
Expand Down
16 changes: 15 additions & 1 deletion src/Perpetuum/Zones/NpcSystem/AI/CombatAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class CombatAI : BaseAI
private const int UpdateFrequency = 1650;
private const int Sqrt2 = 141;
private const int Weight = 1000;
// Timer for periodically checking the main hostile target.
private readonly IntervalTimer updateHostileTimer = new IntervalTimer(UpdateFrequency, true);
private readonly IntervalTimer processHostilesTimer = new IntervalTimer(UpdateFrequency);
private readonly IntervalTimer primarySelectTimer = new IntervalTimer(UpdateFrequency);
private List<ModuleActivator> moduleActivators;
Expand Down Expand Up @@ -261,7 +263,17 @@ protected void UpdateHostile(TimeSpan time, bool moveThreatToPseudoThreat = true
return;
}

if (!mostHated.Unit.CurrentPosition.IsEqual2D(this.lastTargetPosition))
var forceCheckPrimary = false;
updateHostileTimer.Update(time);
if (updateHostileTimer.Passed)
{
updateHostileTimer.Reset();

// Forced check of the main hostile target.
forceCheckPrimary = movement?.Arrived ?? true;
}

if (!mostHated.Unit.CurrentPosition.IsEqual2D(this.lastTargetPosition) || forceCheckPrimary)
{
this.lastTargetPosition = mostHated.Unit.CurrentPosition;

Expand Down Expand Up @@ -393,6 +405,8 @@ private List<Point> FindNewAttackPosition(Unit hostile, CancellationToken cancel
var end = hostile.CurrentPosition.GetRandomPositionInRange2D(0, smartCreature.BestActionRange - 1).ToPoint();

smartCreature.StopMoving();
// Nulling movement so that the unit does not resume it at zero speed if the path is not found.
movement = null;

var maxNode = Math.Pow(smartCreature.HomeRange, 2) * Math.PI;
var priorityQueue = new PriorityQueue<Node>((int)maxNode);
Expand Down