Update Readme.md
[AI-Pathfinding.git] / Assets / ClusterFunctions.cs
blob40e61bc0b654df3dd3fb6a1b504bcfb6ffb46a2f
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
5 public class ClusterFunctions {
7 public class LookupTable
9 public class LookupEntry
11 public string base_obj;
12 public string target_obj;
13 public float heuristic_value;
15 public LookupEntry(string b_obj, string t_obj, float heuristic)
17 base_obj = b_obj;
18 target_obj = t_obj;
19 heuristic_value = heuristic;
22 public string prettyString()
24 return ("base_obj: " + base_obj + "\ntarget_obj: " + target_obj + "\nheuristic_value: " + heuristic_value);
28 public List<LookupEntry> lookup_table = new List<LookupEntry>();
30 public LookupEntry returnEntry(string b_obj, string t_obj)
32 foreach(LookupEntry le in lookup_table)
34 if(b_obj == le.base_obj && t_obj == le.target_obj)
36 return le;
39 Debug.Log("No lookup entry found");
40 return null;
45 public static LookupTable tile_lookup_table = new LookupTable();
46 public static LookupTable pov_lookup_table = new LookupTable();
48 public static Dictionary<string, List<GameObject>> room_tile_association = new Dictionary<string, List<GameObject>>();
49 public static Dictionary<string, GameObject> tile_centers = new Dictionary<string, GameObject>();
50 public static Dictionary<string, List<GameObject>> room_pov_association = new Dictionary<string, List<GameObject>>();
51 public static Dictionary<string, GameObject> pov_centers = new Dictionary<string, GameObject>();
53 static GameObject[] rooms = GameObject.FindGameObjectsWithTag("Room");
54 static GameObject[] tiles = GameObject.FindGameObjectsWithTag("TileNode");
55 static GameObject[] povs = GameObject.FindGameObjectsWithTag("PoVNode");
57 public static void buildLookupTable()
59 foreach (GameObject room in rooms)
61 List<GameObject> room_tiles = new List<GameObject>();
62 float closest_so_far = float.MaxValue;
63 tile_centers.Add(room.name, null);
64 foreach (GameObject tile in tiles)
66 if (room.GetComponent<BoxCollider>().bounds.Contains(tile.transform.position))
68 room_tiles.Add(tile);
69 float dist_from_cent = Vector3.Distance(tile.transform.position, room.GetComponent<BoxCollider>().bounds.center);
70 if (dist_from_cent < closest_so_far)
72 closest_so_far = dist_from_cent;
73 tile_centers[room.name] = tile;
77 room_tile_association.Add(room.name, room_tiles);
79 List<GameObject> room_povs = new List<GameObject>();
80 closest_so_far = float.MaxValue;
81 pov_centers.Add(room.name, null);
82 foreach (GameObject pov in povs)
84 if (room.GetComponent<BoxCollider>().bounds.Contains(pov.transform.position))
86 room_povs.Add(pov);
87 float dist_from_cent = Vector3.Distance(pov.transform.position, room.GetComponent<BoxCollider>().bounds.center);
88 if (dist_from_cent < closest_so_far)
90 closest_so_far = dist_from_cent;
91 pov_centers[room.name] = pov;
95 room_pov_association.Add(room.name, room_povs);
98 foreach(KeyValuePair<string, GameObject> tile_origin in tile_centers)
100 foreach (KeyValuePair<string, GameObject> tile_hunting in tile_centers)
102 if (tile_origin.Key != tile_hunting.Key)
104 List<Connection_Data> open_nodes = new List<Connection_Data>();
105 List<Connection_Data> closed_nodes = new List<Connection_Data>();
106 GameObject.FindGameObjectWithTag("GameController")
107 .GetComponent<Map_Gen>()
108 .itterativeAStar(
109 tile_origin.Value.GetComponent<Connection_List>(),
110 tile_hunting.Value.GetComponent<Connection_List>(), open_nodes, closed_nodes, Map_Gen.Heuristic_Search.Euclidian, Map_Gen.Categories.Tile);
111 //last closed node contains the end, but tile_hunting should have data anyways
112 LookupTable.LookupEntry table_entry = new LookupTable.LookupEntry(tile_origin.Key, tile_hunting.Key, tile_hunting.Value.GetComponent<Connection_List>().cost_so_far);
113 ClusterFunctions.tile_lookup_table.lookup_table.Add(table_entry);
114 GameObject.FindGameObjectWithTag("GameController")
115 .GetComponent<Map_Gen>().resetGraphs();
121 foreach (KeyValuePair<string, GameObject> pov_origin in pov_centers)
123 foreach (KeyValuePair<string, GameObject> pov_hunting in pov_centers)
125 if (pov_origin.Key != pov_hunting.Key)
127 List<Connection_Data> open_nodes = new List<Connection_Data>();
128 List<Connection_Data> closed_nodes = new List<Connection_Data>();
130 GameObject.FindGameObjectWithTag("GameController")
131 .GetComponent<Map_Gen>()
132 .itterativeAStar(
133 pov_origin.Value.GetComponent<Connection_List>(),
134 pov_hunting.Value.GetComponent<Connection_List>(), open_nodes, closed_nodes, Map_Gen.Heuristic_Search.Euclidian, Map_Gen.Categories.PoV);
136 //last closed node contains the end, but pov_hunting should have data anyways
137 LookupTable.LookupEntry table_entry = new LookupTable.LookupEntry(pov_origin.Key, pov_hunting.Key, pov_hunting.Value.GetComponent<Connection_List>().cost_so_far);
138 ClusterFunctions.pov_lookup_table.lookup_table.Add(table_entry);
139 GameObject.FindGameObjectWithTag("GameController")
140 .GetComponent<Map_Gen>().resetGraphs();
145 foreach(LookupTable.LookupEntry le in pov_lookup_table.lookup_table)
147 Debug.Log(le.prettyString());
149 foreach (LookupTable.LookupEntry le in tile_lookup_table.lookup_table)
151 Debug.Log(le.prettyString());