Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Source/ACE.Server/Command/Handlers/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,7 @@ private static void DoCopyChar(Session session, string existingCharName, uint ex
});
}


/// <summary>
/// Creates an object or objects in the world
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Source/ACE.Server/WorldObjects/Monster_Awareness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ partial class Creature
/// </summary>
public bool IsAwake = false;

/// <summary>
/// Cache for visible targets to reduce expensive lookups
/// </summary>
private List<Creature> _cachedVisibleTargets = new List<Creature>();
private double _lastTargetCacheTime = 0.0;
private const double TARGET_CACHE_DURATION = 0.5; // Cache for 0.5 seconds

/// <summary>
/// Transitions a monster from idle to awake state
/// </summary>
Expand Down Expand Up @@ -234,9 +241,19 @@ public virtual bool FindNextTarget()

/// <summary>
/// Returns a list of attackable targets currently visible to this monster
/// Uses caching to reduce expensive lookups
/// </summary>
public List<Creature> GetAttackTargets()
{
var currentTime = Timers.RunningTime;

// Check if cache is still valid
if (currentTime - _lastTargetCacheTime < TARGET_CACHE_DURATION)
{
return _cachedVisibleTargets;
}

// Cache expired, refresh it
var visibleTargets = new List<Creature>();
var listOfCreatures = PhysicsObj.ObjMaint.GetVisibleTargetsValuesOfTypeCreature();
foreach (var creature in listOfCreatures)
Expand Down Expand Up @@ -273,6 +290,10 @@ public List<Creature> GetAttackTargets()
visibleTargets.Add(creature);
}

// Update cache
_cachedVisibleTargets = visibleTargets;
_lastTargetCacheTime = currentTime;

return visibleTargets;
}

Expand Down
34 changes: 34 additions & 0 deletions Source/ACE.Server/WorldObjects/Monster_Navigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@ partial class Creature
public static readonly float MaxChaseRange = 96.0f;
public static readonly float MaxChaseRangeSq = MaxChaseRange * MaxChaseRange;

/// <summary>
/// Cache for physics calculations to reduce expensive operations
/// </summary>
private float _cachedDistanceToTarget = -1.0f;
private double _lastDistanceCacheTime = 0.0;
private const double DISTANCE_CACHE_DURATION = 0.25; // Cache for 0.25 seconds

/// <summary>
/// Cached distance calculation to reduce expensive physics operations
/// </summary>
public float GetCachedDistanceToTarget()
{
var currentTime = Timers.RunningTime;

// Check if cache is still valid
if (currentTime - _lastDistanceCacheTime < DISTANCE_CACHE_DURATION && _cachedDistanceToTarget >= 0)
{
return _cachedDistanceToTarget;
}

// Cache expired or invalid, calculate new distance
if (AttackTarget != null)
{
_cachedDistanceToTarget = (float)PhysicsObj.get_distance_to_object(AttackTarget.PhysicsObj, true);
}
else
{
_cachedDistanceToTarget = float.MaxValue;
}

_lastDistanceCacheTime = currentTime;
return _cachedDistanceToTarget;
}

/// <summary>
/// Determines if a monster is within melee range of target
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Server/WorldObjects/Monster_Tick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ACE.Server.WorldObjects
{
partial class Creature
{
protected const double monsterTickInterval = 0.2;
protected const double monsterTickInterval = 0.4;

public double NextMonsterTickTime;

Expand Down