List <grid_node> potential_movement(grid_node n) { List <grid_node> p = new List <grid_node>(); p.Add(n); foreach (grid_node nb in sg.my_neighbours(n)) { p.Add(nb); } return(p); }
//===============================Random move===================================== //===============================Simple Greedy Move========================= void IQ1_move() { /*this relatively clever movement is examine the whole grid * and use A* to move towards the exit as well as keep distance * with all the evadors; * * the Heuristic function that I am using is: * H(n)=Manhattan(n.grid_pos,exit)-Min(Manhattan(n.grid_pos,persuer)[]) */ int threat = 0; grid_node candidate = null; foreach (grid_node n in sg.my_neighbours(current_node)) { if (sg.walkable(n) && (!n.occupied)) { if (candidate == null) { candidate = n; threat = evaluate_evador(n); } else { candidate = evaluate_evador(n) > threat?n:candidate; } } } if (candidate != null) { move_to_grid(sg, candidate); if (candidate.state == SquareGrid.grid_stat.exit) { StageController.instance.score_evador += 1; //remove self from board current_node.occupied = false; explode(Color.green); DestroyImmediate(gameObject); } } }
grid_node astar(SquareGrid sg, grid_node startNode) { //* the Heuristic function that I am using is: //* Manhattan(n.grid_pos,exit) grid_node candidate = null; grid_node targetNode = sg.nodes.Find(n => n.state == SquareGrid.grid_stat.exit); //Is that right?? Heap <grid_node> openSet = new Heap <grid_node>(sg.Width * sg.Height); //Is that right?? //Heap<grid_node> openSet = new Heap<grid_node>(20); //HashSet<grid_node> closedSet = new HashSet<grid_node>(); List <grid_node> closedSet = new List <grid_node>(); openSet.Add(startNode); while (openSet.count > 0) { grid_node currentNode = openSet.remove_first(); closedSet.Add(currentNode); if (currentNode == targetNode) { //RetracePath(startNode,targetNode); candidate = closedSet[1]; //the next step?? break; } foreach (grid_node n in sg.my_neighbours(currentNode)) { if (!sg.walkable(n) || closedSet.Contains(n) || n.occupied) { continue; } int newMovementCostToNeighbour = currentNode.gCost + Manhattan(currentNode.grid_position, n.grid_position); if (newMovementCostToNeighbour < n.gCost || !openSet.Contains(n)) { n.gCost = newMovementCostToNeighbour; n.hCost = Manhattan(n.grid_position, targetNode.grid_position); n.parent = currentNode; if (!openSet.Contains(n)) { openSet.Add(n); } } } } candidate = closedSet.Count > 1?closedSet[1]:startNode; return(candidate); }
//This is also bullshit. void Update() { if (Input.GetMouseButtonDown(0)) { Ray r = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(r, out hit)) { grid_node n = hit.collider.gameObject.GetComponent <grid_node>(); List <grid_node> nbs = g.my_neighbours(n); IEnumerator c = flash_them(nbs); StartCoroutine(c); } } }
grid_node next_closest_to_target(grid_node myself, grid_node target) { int min_dist = 0; grid_node candidate = null; foreach (grid_node n in sg.my_neighbours(myself)) { if (candidate == null) { candidate = n; min_dist = Manhattan(n.grid_position, target.grid_position); } else { candidate = Manhattan(n.grid_position, target.grid_position) < min_dist?n:candidate; } } return(candidate); }