public GraphNode(Vector2_int a) { pos = a; visited = false; cost = int.MaxValue; // int.MaxValue, because the initial value for dijkstra's algorithm should be infinity adjacent = new List <GraphNode>(); previous = null; }
// this is not updating player, it is actually updating the object to show objects after player is updated public void UpdatePlayer(GameObject gameobj) { int x = (int)gameobj.transform.position.x; int y = (int)gameobj.transform.position.y; Player p = gameobj.GetComponent <Player>(); if (p == null) { return; } List <Vector2_int> list_tmp = new List <Vector2_int>(); for (int i = Math.Max(0, x - p.fooddistance); i <= Math.Min(col + 1, x + p.fooddistance); i++) { for (int j = Math.Max(0, y - p.fooddistance); j <= Math.Min(row + 1, y + p.fooddistance); j++) { Vector2_int tmp = new Vector2_int(i, j); if (objtoshow_dict.ContainsKey(tmp)) { objtoshow_dict[tmp].game.GetComponent <Renderer>().enabled = true; if (objtoshow_dict[tmp].game.GetComponent <Animator>() != null) { objtoshow_dict[tmp].game.GetComponent <Animator>().enabled = true; } list_tmp.Add(tmp); } } } //Debug.Log("list count" + list_tmp.Count); //Debug.Log("objecttoshow count:" + objtoshow_dict.Count); foreach (Vector2_int vint in changed_everyupdate) { if (!list_tmp.Contains(vint)) { objtoshow_dict[vint].SetRenderer(this, new PropertyChangedEventArgs("UpdatingAccordingtoLights")); } } changed_everyupdate = list_tmp; }
// this is the algortithm for returning the shortest path public List <GraphNode> Dijkstra(Vector2_int start, Vector2_int end) // return the shortest path { //Debug.Log("Marker 1"); if (start.x < 0 || start.x >= col || end.x < 0 || end.x >= col || start.y < 0 || start.y >= row || end.y < 0 || end.y >= row) // the position is not inside board { return(null); } GraphNode[][] graphNodes = GraphSetup(); Heap graphheap = new Heap(); graphheap.BuildHeap(graphNodes); List <GraphNode> result = null; //Debug.Log("Marker 3"); graphNodes[start.x][start.y].previous = null; graphNodes[start.x][start.y].cost = 0; graphheap.PercolateUp(graphNodes[start.x][start.y].mapping_index); //Debug.Log("Marker 4"); //FileStream fs = new FileStream("Running.log", FileMode.Append); //byte[] buffer = Encoding.ASCII.GetBytes("Marker 4 \n"); //fs.Write(buffer, 0, buffer.Length); //fs.Close(); GraphNode tmp = null; while (graphheap.currentSize > 0) { tmp = graphheap.DeleteMin(); if (tmp.cost == int.MaxValue || System.Object.ReferenceEquals(tmp, graphNodes[end.x][end.y]) || tmp.cost >= graphNodes[end.x][end.y].cost) // all infinite path to the end or already hit the last { break; } foreach (GraphNode node in tmp.adjacent) { if (node.mapping_index > 0 && !obstacle_level.ContainsKey(node.pos) && tmp.cost + 1 < node.cost) // it's in the unvisited set (graph heap), and not the wall tile, and really lowers the cost { node.cost = tmp.cost + 1; node.previous = tmp; graphheap.PercolateUp(node.mapping_index); //for(int j = 1; j < 5; j++) //{ // Debug.Log(" j th cost is " + graphheap.heap_arr[j].pos.x + " " + graphheap.heap_arr[j].pos.y + " " + graphheap.heap_arr[j].cost + " " + graphheap.heap_arr[j].visited); //} } } } //Debug.Log("Marker 5"); //fs = new FileStream("Running.log", FileMode.Append); //buffer = Encoding.ASCII.GetBytes("Marker 5 \n"); //fs.Write(buffer, 0, buffer.Length); if (graphNodes[end.x][end.y].cost != int.MaxValue) { tmp = graphNodes[end.x][end.y]; result = new List <GraphNode>(); while (tmp != null) { result.Insert(0, tmp); //Debug.Log("Path is " + tmp.cost + tmp.mapping_index + "\n"); //fs.Write(buffer, 0, buffer.Length); tmp = tmp.previous; } } //Debug.Log("Cost is " + graphNodes[end.x][end.y].cost); //buffer = Encoding.ASCII.GetBytes("Cost is " + graphNodes[end.x][end.y].cost + "\n"); //fs.Write(buffer, 0, buffer.Length); //fs.Close(); //GraphNodeSettoDefault(); return(result); }
void LayoutObjRandom(GameObject[] tileArr, int m1, int m2) { int objCount = Random.Range(m1, m2 + 1); for (int i = 0; i < objCount; i++) { Vector3 pos = RandomPosition(); GameObject ins = tileArr[Random.Range(0, tileArr.Length)]; GameObject instance = Instantiate(ins, pos, Quaternion.identity) as GameObject; //print(objCount.ToString()); //Debug.logger.Log( i ); if (System.Object.ReferenceEquals(tileArr, light)) // if it's light, then we want to take the adjacent 3by3 area into light { lights.Add(instance); instance.GetComponent <Light>().pos = pos; //Debug.logger.Log(i); } else // else, we want to display or not the object { if (instance.GetComponent <Animator>() != null) { instance.GetComponent <Animator>().enabled = false; } instance.GetComponent <Renderer>().enabled = false; if (System.Object.ReferenceEquals(tileArr, enemyTiles)) { continue; // might need some strategies for dealing with movable enemies, here. we avoid registering lights to enmies because // enemy is moving, registering is not efficient } List <GameObject> light_tmp = new List <GameObject>(); for (int j = 0; j < lights.Count; j++) { if (GameManager.Distance(instance, lights[j]) < adj) { light_tmp.Add(lights[j]); } } if (light_tmp.Count > 0) { objtoshow.Add(new ObjectToShow(instance, light_tmp.ToArray())); } else { objtoshow.Add(new ObjectToShow(instance)); } // if we're layouting the inside walls, we need to update the cost to it to make it int max if (System.Object.ReferenceEquals(tileArr, inwallTiles)) { Vector2_int int_tmp = new Vector2_int(pos.x, pos.y); //Debug.Log(int_tmp.x + " " + int_tmp.y); if (obstacle_level.ContainsKey(int_tmp)) { Debug.Log("Repeat at " + int_tmp.x + " " + int_tmp.y); } else { obstacle_level.Add(int_tmp, int.MaxValue); } } } } }