public void FindPath(PathRequest request, Action <PathResult> callback) { Vector3[] waypoints = new Vector3[0]; bool pathSuccess = false; KNode startNode = grid.NodeFromWorldPoint(request.pathStart); KNode targetNode = grid.NodeFromWorldPoint(request.pathEnd); if (targetNode.walkable) //if (startNode.walkable && targetNode.walkable) { Heap <KNode> openSet = new Heap <KNode>(grid.MaxSize); HashSet <KNode> closedSet = new HashSet <KNode>(); openSet.Add(startNode); while (openSet.Count > 0) { KNode currentNode = openSet.RemoveFirst(); closedSet.Add(currentNode); if (currentNode == targetNode) { pathSuccess = true; break; } foreach (KNode neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour) + neighbour.movementPenalty; if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } else { openSet.UpdateItem(neighbour); } } } } } if (pathSuccess) { waypoints = RetracePath(startNode, targetNode); pathSuccess = waypoints.Length > 0; } callback(new PathResult(waypoints, pathSuccess, request.callback)); }
public List <KNode> GetNeighbours(KNode node) { List <KNode> neighbours = new List <KNode>(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x == 0 && y == 0) { continue; } int checkX = node.gridX + x; int checkY = node.gridY + y; if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY) { neighbours.Add(grid[checkX, checkY]); } } } return(neighbours); }
void CreateGrid() { grid = new KNode[gridSizeX, gridSizeY]; Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.up * gridWorldSize.y / 2; for (int x = 0; x < gridSizeX; x++) { for (int y = 0; y < gridSizeY; y++) { Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.up * (y * nodeDiameter + nodeRadius); bool walkable = !(Physics2D.OverlapCircle(worldPoint, nodeRadius, unwalkableMask)); int movementPenalty = 0; //raycast if (walkable) { Ray ray = new Ray(worldPoint + Vector3.up * 50, Vector3.down); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100, walkableMask)) { walkableRegionsDictionary.TryGetValue(hit.collider.gameObject.layer, out movementPenalty); } } grid[x, y] = new KNode(walkable, worldPoint, x, y, movementPenalty); } } }
private void Traversal(KNode <int> inputRoot, KNode <string> outputRoot) { if (inputRoot.Value % 15 == 0) { outputRoot.Value = "Fizzbuzz"; } else if (inputRoot.Value % 5 == 0) { outputRoot.Value = "Buzz"; } else if (inputRoot.Value % 3 == 0) { outputRoot.Value = "Fizz"; } else { outputRoot.Value = inputRoot.Value.ToString(); } if (inputRoot.Children != null) { foreach (var child in inputRoot.Children) { KNode <string> newNode = new KNode <string>(""); outputRoot.Children.Add(newNode); Traversal(child, newNode); } } }
public virtual void Add(string nodeValue) { var lastPos = ((Array)_list).Length; var node = new KNode(); node.Value = nodeValue; Array.Resize<KNode>(ref _list, lastPos + 1); _list[lastPos] = node; }
int GetDistance(KNode nodeA, KNode nodeB) { int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX); int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY); if (dstX > dstY) { return(14 * dstY + 10 * (dstX - dstY)); } return(14 * dstX + 10 * (dstY - dstX)); }
public KAryTree <string> FizzBuzzer(KAryTree <int> inputTree) { KAryTree <string> outputTree = new KAryTree <string>(3); KNode <string> outputRoot = new KNode <string>(); outputTree.Root = outputRoot; KNode <int> inputRoot = inputTree.Root; Traversal(inputRoot, outputRoot); return(outputTree); }
public void AddTimerEvent(float startTime, int time, float perTime, object param, Action <int, object> fun) { if (_node == null) { _node = new KNode <STUINFO>(new STUINFO(startTime, time, perTime, param, fun)); _current = _node; } else { _current.Next = new KNode <STUINFO>(new STUINFO(startTime, time, perTime, param, fun)); _current = _current.Next; } }
Vector3[] RetracePath(KNode startNode, KNode endNode) { List <KNode> path = new List <KNode>(); KNode currentNode = endNode; while (currentNode != startNode) { path.Add(currentNode); currentNode = currentNode.parent; } path.Add(startNode); Vector3[] waypoints = SimplifyPath(path); Array.Reverse(waypoints); return(waypoints); }
/// <summary> /// Helper method to build a tree to run fizz buzz on /// </summary> /// <returns></returns> private KAryTree <int> MakeFizzTree() { // last leaves KNode <int> n1 = new KNode <int>(7); KNode <int> n2 = new KNode <int>(13); KNode <int> n3 = new KNode <int>(30); KNode <int> n4 = new KNode <int>(10); KNode <int> n5 = new KNode <int>(3); KNode <int> n6 = new KNode <int>(8); KNode <int> n7 = new KNode <int>(6); // middle tier List <KNode <int> > children = new List <KNode <int> >() { n1, n2, n3 }; KNode <int> n8 = new KNode <int>(4, children); children = new List <KNode <int> >() { n4 }; KNode <int> n9 = new KNode <int>(5, children); children = new List <KNode <int> >() { n5, n6, n7 }; KNode <int> n10 = new KNode <int>(15, children); //root children = new List <KNode <int> >() { n8, n9, n10 }; KNode <int> n11 = new KNode <int>(3, children); KAryTree <int> newTree = new KAryTree <int>(3) { Root = n11 }; return(newTree); }
public bool DoTime(float time) { bool re = true; if (_start) { _dotimeLeave -= time; if (_dotimeLeave <= 0) { _dotimeLeave = _current.Data.perTime; try { if (_current.Data.fun != null) { _current.Data.fun(_id, _current.Data.param); } } catch { re = false; } _current.Data.time--; if (_current.Data.time < 1) { _current = _current.Next; if (_current != null) { _dotimeLeave = _current.Data.startTime; } else { re = false; } } } } return(re); }
public void Delete(KNode node) { if (node == null) { throw new NullReferenceException("node"); } if (this.Find(node.Value) == null) { throw new ArgumentOutOfRangeException(node.Value); } var clone = ((Array)_list).Clone(); var pos = 0; _list = new KNode[((Array)clone).Length - 1]; foreach (KNode item in (Array)clone) { if (!node.Equals(item)) { _list[pos] = item; pos++; } } }
static void TreeTest() { /* * BinarySearchTree<int> newTree = new BinarySearchTree<int>(); * newTree.Add(newTree.Root, 9); * newTree.Add(newTree.Root, 4); * newTree.Add(newTree.Root, 17); * newTree.Add(newTree.Root, 3); * newTree.Add(newTree.Root, 6); * newTree.Add(newTree.Root, 22); * newTree.Add(newTree.Root, 5); * newTree.Add(newTree.Root, 7); * newTree.Add(newTree.Root, 20); * * List<int> result = newTree.BreadthFirst(); * foreach (int value in result) * Console.WriteLine($"{value} -> "); * * BinarySearchTree<int> test = new BinarySearchTree<int>(); */ // last leaves KNode <int> n1 = new KNode <int>(7); KNode <int> n2 = new KNode <int>(13); KNode <int> n3 = new KNode <int>(30); KNode <int> n4 = new KNode <int>(10); KNode <int> n5 = new KNode <int>(3); KNode <int> n6 = new KNode <int>(8); KNode <int> n7 = new KNode <int>(8); // middle tier List <KNode <int> > children = new List <KNode <int> >() { n1, n2, n3 }; KNode <int> n8 = new KNode <int>(4, children); children = new List <KNode <int> >() { n4 }; KNode <int> n9 = new KNode <int>(5, children); children = new List <KNode <int> >() { n5, n6, n7 }; KNode <int> n10 = new KNode <int>(15, children); //root children = new List <KNode <int> >() { n8, n9, n10 }; KNode <int> n11 = new KNode <int>(3, children); KAryTree <int> newTree = new KAryTree <int>(3); newTree.Root = n11; Console.WriteLine(newTree.Root.Value); //List<KNode<int>> list = newTree.Root.Children; List <int> result = newTree.BreadthFirst(); foreach (int value in result) { Console.Write($"[ {value} ]=>"); } }
public void Start() { _start = true; _current = _node; _dotimeLeave = _current.Data.startTime; }
public KList(KNode[] list) { _list = list; }
public KNode() { this.Data = default(T); this.Next = null; }
public KNode(T item) { this.Data = item; this.Next = null; }