private void rangeFunction(Node lookAt) { if (lookAt.isWalkable()) { // adds node to range pairs if not present already. if (!nodesInRangeSet.Contains(lookAt)) { nodesInRangeSet.Add(lookAt); nodesInRange.Add(lookAt); } bool frendType = uRef.isEnemy(); // targets! if (lookAt.Occupied && lookAt.Occupier.isEnemy() != frendType) { currentTargets.Add(new Node.NodePointer(shootingFrom, lookAt, Node.range(shootingFrom, lookAt))); } if (reverseNodesInRangeSetCost.ContainsKey(lookAt)) { if (reverseNodesInRangeSetCost[lookAt] > shootingFrom.realCost) reverseNodesInRangeSetCost[lookAt] = (int) shootingFrom.realCost; } else reverseNodesInRangeSetCost.Add(lookAt, (int) (shootingFrom.realCost)); } }
/// <summary> /// Unlike closestMostValidNode; BFS does up to a full BFS /// to find the closest unocupied & walkable node to a given location. /// Only use when not wanting to take into account edge costs. /// If no nodes found that are valid, returns the start node. /// Uses Nodes directly instead of converting from Vector2. /// </summary> /// <param name="startLoc">The location to start looking from.</param> /// <returns>The first unocupied/valid walkable tile found. If no nodes found that are valid, returns the start node.</returns> private Node BFSUnoccupiedAndValid(Node startNode) { if (!startNode.Occupied && startNode.isWalkable()) return startNode; initializePathfinding(true); Queue<Node> listOfNodes = new Queue<Node>(); listOfNodes.Enqueue(startNode); // Can't use visited, as we're already using that hack in initializePathfinding... startNode.realCost = -1; while (listOfNodes.Count > 0) { Node found = listOfNodes.Dequeue(); if (!found.Occupied && found.isWalkable()) return found; foreach (Edge e in found.getEdges()) { Node candidate = e.getNode(); if (candidate.realCost > 0) listOfNodes.Enqueue(candidate); // Can't use visited, as we're already using that hack in initializePathfinding... candidate.realCost = -1; } } return startNode; }