public NavCubeChunk LoadOrCreateChunk(Vector3Int currentChunkPos) { NavCubeChunk ncc = GetChunk(currentChunkPos); if (ncc != null) { ncc.world = this; return(ncc); } ncc = new NavCubeChunk(this); ncc.position = currentChunkPos.Copy(); chunks.Add(ncc); return(ncc); }
public List <Vector3Int> GetPath(Vector3Int start, Vector3Int end, NavCubeType type = NavCubeType.Walking, int maxResolveTry = 4096, NavCubeAgent agent = null) { closedList = new List <NavCubeNode>(); openList = new List <NavCubeNode>(); List <Vector3Int> path = null; if (agent != null) { for (int i = 0; i < agent.height; i++) { int ii = i - agent.offset; if (GetData(start + Vector3Int.up * ii).type == NavCubeType.Blocked || GetData(end + Vector3Int.up * ii).type == NavCubeType.Blocked) { return(null); } } } else { if (GetData(start).type == NavCubeType.Blocked || GetData(end).type == NavCubeType.Blocked) { return(null); } } openList.Add(new NavCubeNode(start.Copy(), 0.0f, 0.0f)); NavCubeNode current = null; while (openList.Count > 0) { current = openList[0]; openList.RemoveAt(0); if (current.Equals(end)) { break; } if (agent == null || (agent.movedir & 1) == 1) { AddNeighbour(current, end, new Vector3Int(1, 0, 0), NavCubeNode.Cost100, type, agent); AddNeighbour(current, end, new Vector3Int(-1, 0, 0), NavCubeNode.Cost100, type, agent); AddNeighbour(current, end, new Vector3Int(0, 1, 0), NavCubeNode.Cost100, type, agent); AddNeighbour(current, end, new Vector3Int(0, -1, 0), NavCubeNode.Cost100, type, agent); AddNeighbour(current, end, new Vector3Int(0, 0, 1), NavCubeNode.Cost100, type, agent); AddNeighbour(current, end, new Vector3Int(0, 0, -1), NavCubeNode.Cost100, type, agent); } if (agent == null || (agent.movedir & 2) == 2) { AddNeighbour(current, end, new Vector3Int(1, -1, 0), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(-1, -1, 0), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(0, -1, 1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(0, -1, -1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(-1, 0, -1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(1, 0, -1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(1, 0, 1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(-1, 0, 1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(1, 1, 0), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(-1, 1, 0), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(0, 1, 1), NavCubeNode.Cost101, type, agent); AddNeighbour(current, end, new Vector3Int(0, 1, -1), NavCubeNode.Cost101, type, agent); } if (agent == null || (agent.movedir & 4) == 4) { AddNeighbour(current, end, new Vector3Int(-1, -1, -1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(1, -1, -1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(1, -1, 1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(-1, -1, 1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(-1, 1, -1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(1, 1, -1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(1, 1, 1), NavCubeNode.Cost111, type, agent); AddNeighbour(current, end, new Vector3Int(-1, 1, 1), NavCubeNode.Cost111, type, agent); } AddPathLink(current, end, type, agent); openList = openList.OrderBy(x => x.total).ToList <NavCubeNode>(); closedList.Add(current); if (closedList.Count >= maxResolveTry) { break; } } if (current != null && current.Equals(end)) { Debug.Log($"Path found ! (Search Stack: {closedList.Count})"); path = new List <Vector3Int>(); NavCubeNode node = current; while (node != null) { path.Insert(0, node.position); node = node.parent; } } else { Debug.Log("Path not found !"); } closedList.Clear(); openList.Clear(); return(path); }