Changed agents management from list to dictionary in `Crowd`

This commit is contained in:
ikpil 2024-07-07 12:38:09 +09:00
parent ab2c520076
commit 828b9644cc
3 changed files with 25 additions and 15 deletions

View File

@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Changed to use Span<byte> and stackalloc for improved performance and memory management in `RcLayers.BuildHeightfieldLayers()` - Changed to use Span<byte> and stackalloc for improved performance and memory management in `RcLayers.BuildHeightfieldLayers()`
- Changed vertCount and triCount to byte in `DtPolyDetail` - Changed vertCount and triCount to byte in `DtPolyDetail`
- Changed `new float[]` to `stackalloc float[]` in `DtConvexConvexIntersections.Intersect()` - Changed `new float[]` to `stackalloc float[]` in `DtConvexConvexIntersections.Intersect()`
- Changed agents management from list to dictionary in `DtCrowd`
### Removed ### Removed
- Removed RcMeshDetails.VdistSq2(float[], float[]) - Removed RcMeshDetails.VdistSq2(float[], float[])

View File

@ -120,8 +120,9 @@ namespace DotRecast.Detour.Crowd
/// @ingroup crowd /// @ingroup crowd
public class DtCrowd public class DtCrowd
{ {
private readonly RcAtomicInteger _agentId = new RcAtomicInteger(); private readonly RcAtomicInteger _agentIdx;
private readonly List<DtCrowdAgent> _agents; private readonly Dictionary<int, DtCrowdAgent> _agents;
private readonly List<DtCrowdAgent> _activeAgents;
private readonly DtPathQueue _pathQ; private readonly DtPathQueue _pathQ;
@ -170,7 +171,9 @@ namespace DotRecast.Detour.Crowd
// Allocate temp buffer for merging paths. // Allocate temp buffer for merging paths.
_maxPathResult = DtCrowdConst.MAX_PATH_RESULT; _maxPathResult = DtCrowdConst.MAX_PATH_RESULT;
_pathQ = new DtPathQueue(config); _pathQ = new DtPathQueue(config);
_agents = new List<DtCrowdAgent>(); _agentIdx = new RcAtomicInteger(0);
_agents = new Dictionary<int, DtCrowdAgent>();
_activeAgents = new List<DtCrowdAgent>();
// The navQuery is mostly used for local searches, no need for large node pool. // The navQuery is mostly used for local searches, no need for large node pool.
SetNavMesh(nav); SetNavMesh(nav);
@ -235,11 +238,10 @@ namespace DotRecast.Detour.Crowd
/// @return The index of the agent in the agent pool. Or -1 if the agent could not be added. /// @return The index of the agent in the agent pool. Or -1 if the agent could not be added.
public DtCrowdAgent AddAgent(RcVec3f pos, DtCrowdAgentParams option) public DtCrowdAgent AddAgent(RcVec3f pos, DtCrowdAgentParams option)
{ {
int idx = _agentId.GetAndIncrement(); int idx = _agentIdx.GetAndIncrement();
DtCrowdAgent ag = new DtCrowdAgent(idx); DtCrowdAgent ag = new DtCrowdAgent(idx);
ag.corridor.Init(_maxPathResult); ag.corridor.Init(_maxPathResult);
_agents.Add(ag); AddAgent(ag);
UpdateAgentParameters(ag, option); UpdateAgentParameters(ag, option);
// Find nearest position on navmesh and place the agent there. // Find nearest position on navmesh and place the agent there.
@ -278,15 +280,22 @@ namespace DotRecast.Detour.Crowd
return ag; return ag;
} }
/** // Add the agent from the crowd.
* Removes the agent from the crowd. public void AddAgent(DtCrowdAgent agent)
* {
* @param agent if (_agents.TryAdd(agent.idx, agent))
* Agent to be removed {
*/ _activeAgents.Add(agent);
}
}
// Removes the agent from the crowd.
public void RemoveAgent(DtCrowdAgent agent) public void RemoveAgent(DtCrowdAgent agent)
{ {
_agents.Remove(agent); if (_agents.Remove(agent.idx))
{
_activeAgents.Remove(agent);
}
} }
private bool RequestMoveTargetReplan(DtCrowdAgent ag, long refs, RcVec3f pos) private bool RequestMoveTargetReplan(DtCrowdAgent ag, long refs, RcVec3f pos)
@ -358,7 +367,7 @@ namespace DotRecast.Detour.Crowd
*/ */
public IList<DtCrowdAgent> GetActiveAgents() public IList<DtCrowdAgent> GetActiveAgents()
{ {
return _agents; return _activeAgents;
} }
public RcVec3f GetQueryExtents() public RcVec3f GetQueryExtents()

View File

@ -28,7 +28,7 @@ namespace DotRecast.Detour.Crowd
/// @ingroup crowd /// @ingroup crowd
public class DtCrowdAgent public class DtCrowdAgent
{ {
public readonly long idx; public readonly int idx;
/// The type of mesh polygon the agent is traversing. (See: #CrowdAgentState) /// The type of mesh polygon the agent is traversing. (See: #CrowdAgentState)
public DtCrowdAgentState state; public DtCrowdAgentState state;