Skip to content

Commit

Permalink
Changed agents management from list to dictionary in Crowd
Browse files Browse the repository at this point in the history
  • Loading branch information
ikpil committed Jul 7, 2024
1 parent ab2c520 commit 828b964
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 vertCount and triCount to byte in `DtPolyDetail`
- Changed `new float[]` to `stackalloc float[]` in `DtConvexConvexIntersections.Intersect()`
- Changed agents management from list to dictionary in `DtCrowd`

### Removed
- Removed RcMeshDetails.VdistSq2(float[], float[])
Expand Down
37 changes: 23 additions & 14 deletions src/DotRecast.Detour.Crowd/DtCrowd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ the cost of more difficult steering in tight spaces.
/// @ingroup crowd
public class DtCrowd
{
private readonly RcAtomicInteger _agentId = new RcAtomicInteger();
private readonly List<DtCrowdAgent> _agents;
private readonly RcAtomicInteger _agentIdx;
private readonly Dictionary<int, DtCrowdAgent> _agents;
private readonly List<DtCrowdAgent> _activeAgents;

private readonly DtPathQueue _pathQ;

Expand Down Expand Up @@ -170,7 +171,9 @@ public DtCrowd(DtCrowdConfig config, DtNavMesh nav, Func<int, IDtQueryFilter> qu
// Allocate temp buffer for merging paths.
_maxPathResult = DtCrowdConst.MAX_PATH_RESULT;
_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.
SetNavMesh(nav);
Expand Down Expand Up @@ -235,11 +238,10 @@ public void UpdateAgentParameters(DtCrowdAgent agent, DtCrowdAgentParams option)
/// @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)
{
int idx = _agentId.GetAndIncrement();
int idx = _agentIdx.GetAndIncrement();
DtCrowdAgent ag = new DtCrowdAgent(idx);
ag.corridor.Init(_maxPathResult);
_agents.Add(ag);

AddAgent(ag);
UpdateAgentParameters(ag, option);

// Find nearest position on navmesh and place the agent there.
Expand Down Expand Up @@ -278,15 +280,22 @@ public DtCrowdAgent AddAgent(RcVec3f pos, DtCrowdAgentParams option)
return ag;
}

/**
* Removes the agent from the crowd.
*
* @param agent
* Agent to be removed
*/
// Add the agent from the crowd.
public void AddAgent(DtCrowdAgent agent)
{
if (_agents.TryAdd(agent.idx, agent))
{
_activeAgents.Add(agent);
}
}

// Removes the agent from the crowd.
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)
Expand Down Expand Up @@ -358,7 +367,7 @@ public bool ResetMoveTarget(DtCrowdAgent agent)
*/
public IList<DtCrowdAgent> GetActiveAgents()
{
return _agents;
return _activeAgents;
}

public RcVec3f GetQueryExtents()
Expand Down
2 changes: 1 addition & 1 deletion src/DotRecast.Detour.Crowd/DtCrowdAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace DotRecast.Detour.Crowd
/// @ingroup crowd
public class DtCrowdAgent
{
public readonly long idx;
public readonly int idx;

/// The type of mesh polygon the agent is traversing. (See: #CrowdAgentState)
public DtCrowdAgentState state;
Expand Down

0 comments on commit 828b964

Please sign in to comment.