2023-03-14 08:02:43 +03:00
|
|
|
/*
|
|
|
|
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
|
|
|
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
|
2023-03-15 17:00:29 +03:00
|
|
|
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
|
2023-03-14 08:02:43 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2024-02-06 15:43:54 +03:00
|
|
|
using System.Linq;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:09:10 +03:00
|
|
|
namespace DotRecast.Detour
|
|
|
|
{
|
2023-06-08 15:38:02 +03:00
|
|
|
public class DtNodePool
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 07:24:09 +03:00
|
|
|
private readonly Dictionary<long, List<DtNode>> m_map;
|
2024-02-04 18:45:16 +03:00
|
|
|
|
|
|
|
private int m_nodeCount;
|
2024-02-04 07:24:09 +03:00
|
|
|
private readonly List<DtNode> m_nodes;
|
2023-03-16 19:09:10 +03:00
|
|
|
|
2023-06-08 15:38:02 +03:00
|
|
|
public DtNodePool()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 07:24:09 +03:00
|
|
|
m_map = new Dictionary<long, List<DtNode>>();
|
|
|
|
m_nodes = new List<DtNode>();
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-05-05 02:44:48 +03:00
|
|
|
public void Clear()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
m_map.Clear();
|
2024-02-04 18:45:16 +03:00
|
|
|
m_nodeCount = 0;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2024-02-06 15:43:54 +03:00
|
|
|
public int GetNodeCount()
|
|
|
|
{
|
|
|
|
return m_nodeCount;
|
|
|
|
}
|
|
|
|
|
2024-02-01 18:29:24 +03:00
|
|
|
public int FindNodes(long id, out List<DtNode> nodes)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-01 18:29:24 +03:00
|
|
|
var hasNode = m_map.TryGetValue(id, out nodes);
|
|
|
|
if (hasNode)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-01 18:29:24 +03:00
|
|
|
return nodes.Count;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2024-02-01 18:29:24 +03:00
|
|
|
return 0;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-06-08 15:38:02 +03:00
|
|
|
public DtNode FindNode(long id)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 18:45:16 +03:00
|
|
|
m_map.TryGetValue(id, out var nodes);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (nodes != null && 0 != nodes.Count)
|
|
|
|
{
|
|
|
|
return nodes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-06-08 15:38:02 +03:00
|
|
|
public DtNode GetNode(long id, int state)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 18:45:16 +03:00
|
|
|
m_map.TryGetValue(id, out var nodes);
|
2023-03-16 19:48:49 +03:00
|
|
|
if (nodes != null)
|
|
|
|
{
|
2023-06-08 15:38:02 +03:00
|
|
|
foreach (DtNode node in nodes)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
|
|
|
if (node.state == state)
|
|
|
|
{
|
|
|
|
return node;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-21 08:35:45 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nodes = new List<DtNode>();
|
|
|
|
m_map.Add(id, nodes);
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-07-21 08:35:45 +03:00
|
|
|
return Create(id, state, nodes);
|
2023-03-14 08:02:43 +03:00
|
|
|
}
|
|
|
|
|
2023-07-21 08:35:45 +03:00
|
|
|
private DtNode Create(long id, int state, List<DtNode> nodes)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 18:45:16 +03:00
|
|
|
if (m_nodes.Count <= m_nodeCount)
|
|
|
|
{
|
|
|
|
var newNode = new DtNode(m_nodeCount);
|
|
|
|
m_nodes.Add(newNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = m_nodeCount;
|
|
|
|
m_nodeCount++;
|
|
|
|
var node = m_nodes[i];
|
|
|
|
node.pidx = 0;
|
|
|
|
node.cost = 0;
|
|
|
|
node.total = 0;
|
2023-03-16 19:48:49 +03:00
|
|
|
node.id = id;
|
|
|
|
node.state = state;
|
2024-02-04 18:45:16 +03:00
|
|
|
node.flags = 0;
|
|
|
|
node.shortcut = null;
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-03-16 19:48:49 +03:00
|
|
|
nodes.Add(node);
|
|
|
|
return node;
|
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-06-08 15:38:02 +03:00
|
|
|
public int GetNodeIdx(DtNode node)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 18:45:16 +03:00
|
|
|
return node != null
|
|
|
|
? node.ptr + 1
|
|
|
|
: 0;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-06-08 15:38:02 +03:00
|
|
|
public DtNode GetNodeAtIdx(int idx)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-04 18:45:16 +03:00
|
|
|
return idx != 0
|
|
|
|
? m_nodes[idx - 1]
|
|
|
|
: null;
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-14 08:02:43 +03:00
|
|
|
|
2023-06-08 15:38:02 +03:00
|
|
|
public DtNode GetNode(long refs)
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2023-05-05 02:44:48 +03:00
|
|
|
return GetNode(refs, 0);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
2023-03-16 19:09:10 +03:00
|
|
|
|
2024-02-06 15:43:54 +03:00
|
|
|
public IEnumerable<DtNode> AsEnumerable()
|
2023-03-16 19:48:49 +03:00
|
|
|
{
|
2024-02-06 15:43:54 +03:00
|
|
|
return m_nodes.Take(m_nodeCount);
|
2023-03-16 19:48:49 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-04 07:24:09 +03:00
|
|
|
}
|