You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

451 lines
16 KiB

  1. using Networking.Client;
  2. using Networking.Server;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. //public class MapManager : MonoBehaviour
  7. [CreateAssetMenu(menuName = "Major Project/Map Generation/Map Manager")]
  8. public class MapManager : ScriptableObject
  9. {
  10. public ClientList clients;
  11. public GameObject spawn4; //The section to use as a spawn-point for games with 2-5 players
  12. public GameObject spawn8; //The section to use a spawn-point for games with 5-8 players
  13. //In 5-player games, we choose between them at random
  14. public List<MapSection> sections; //The list of sections to choose from after starting
  15. //Split up the inspector lists to make them easier to manage. They'll be combined on initialisation
  16. public List<SectionList> sectionLists;
  17. public int minConns = 2; //The minimum number of valid connections between two map sections for them to be allowed to link up
  18. public List<MapSection> activeSections; //The list of sections that have been placed on the map (and not removed)
  19. MapSection lastSection; //Which map-section was most recently added?
  20. float startX = -16.0f; //The x-position of the current start of the track
  21. float endX; //The x-position of the current end of the track
  22. int totalSections; //How many sections have been added? Including ones that have been deleted
  23. int initialPlayerCount;
  24. int diffStart = 0; //Initial difficulty rating
  25. int diffCap = 5; //The highest the difficulty rating can go
  26. int difficulty; //Current difficulty rating
  27. /*int diffMin; //Minimum difficulty for a map section to be selected
  28. int diffMax; //Maximum difficulty for a map section to be selected
  29. int diffMinStart = 0; //Initial minimum difficulty for a map section to be selected
  30. int diffMaxStart = 1; //Initial maximum difficulty for a map section to be selected
  31. int diffMinMax = 4; //The highest diffMin can go - ensure that it doesn't go too high to have legal map sections
  32. */
  33. int widthMin; //The minimum widthIn that we want for a new map section
  34. int widthMax; //The maximum widthIn that we want for a new map section
  35. int widthMinMin = 3; //The minimum to which widthMin can be reduced
  36. int widthMaxMin = 5; //The minimum to which widthMax can be reduced
  37. public void init()
  38. {
  39. sections = new List<MapSection>();
  40. endX = startX;
  41. totalSections = 0;
  42. foreach (SectionList sectionList in sectionLists)
  43. {
  44. foreach (MapSection section in sectionList.sectionList)
  45. {
  46. sections.Add(section);
  47. }
  48. }
  49. //Debug.Log("sections.Count = " + sections.Count);
  50. foreach (MapSection section in sections)
  51. {
  52. //Debug.Log("Possible section: " + section.name);
  53. }
  54. initialPlayerCount = clients.ConnectedClients.Count;
  55. activeSections = new List<MapSection>();
  56. if (initialPlayerCount < 5)
  57. {
  58. addSection(spawn4.GetComponent<MapSection>());
  59. }
  60. else if (initialPlayerCount > 5)
  61. {
  62. addSection(spawn8.GetComponent<MapSection>());
  63. }
  64. else
  65. {
  66. if (Random.Range(0.0f, 1.0f) < 0.5f)
  67. {
  68. addSection(spawn4.GetComponent<MapSection>());
  69. }
  70. else
  71. {
  72. addSection(spawn8.GetComponent<MapSection>());
  73. }
  74. }
  75. widthMin = activeSections[0].widthOut - 2;
  76. widthMax = activeSections[0].widthOut + 2;
  77. difficulty = diffStart;
  78. //Set the maximum possible diffMin value to 1 less than the highest difficulty on the section list
  79. /*diffMinMax = 0;
  80. foreach (MapSection section in sections)
  81. {
  82. if (section.difficulty > diffMinMax)
  83. {
  84. diffMinMax = section.difficulty;
  85. }
  86. }
  87. diffMinMax--;*/
  88. switch (initialPlayerCount)
  89. {
  90. case 2:
  91. foreach (GameObject spawnBlock in lastSection.spawns2)
  92. {
  93. spawnBlock.GetComponent<Block>().isSpawnable = true;
  94. }
  95. break;
  96. case 3:
  97. foreach (GameObject spawnBlock in lastSection.spawns3)
  98. {
  99. spawnBlock.GetComponent<Block>().isSpawnable = true;
  100. }
  101. break;
  102. case 4:
  103. foreach (GameObject spawnBlock in lastSection.spawns4)
  104. {
  105. spawnBlock.GetComponent<Block>().isSpawnable = true;
  106. }
  107. break;
  108. case 5:
  109. foreach (GameObject spawnBlock in lastSection.spawns5)
  110. {
  111. spawnBlock.GetComponent<Block>().isSpawnable = true;
  112. }
  113. break;
  114. case 6:
  115. foreach (GameObject spawnBlock in lastSection.spawns6)
  116. {
  117. spawnBlock.GetComponent<Block>().isSpawnable = true;
  118. }
  119. break;
  120. case 7:
  121. foreach (GameObject spawnBlock in lastSection.spawns7)
  122. {
  123. spawnBlock.GetComponent<Block>().isSpawnable = true;
  124. }
  125. break;
  126. case 8:
  127. foreach (GameObject spawnBlock in lastSection.spawns8)
  128. {
  129. spawnBlock.GetComponent<Block>().isSpawnable = true;
  130. }
  131. break;
  132. default:
  133. foreach (GameObject spawnBlock in lastSection.spawns1)
  134. {
  135. spawnBlock.GetComponent<Block>().isSpawnable = true;
  136. }
  137. break;
  138. }
  139. checkForward();
  140. }
  141. void chooseNextSection()
  142. {
  143. //First, we determine which sections are valid
  144. List<MapSection> validSections = new List<MapSection>();
  145. updateCriteria(); //We update the section selection criteria for the current gamestate
  146. foreach (MapSection section in sections)
  147. {
  148. if (section.weight > 0 && checkSegments(section))
  149. {
  150. //If a segment is a valid continuation of the current most-recent segment, add it to the list
  151. //Sections with higher weights get more entries => higher chance of being picked
  152. for (int i = 0; i < section.weight; i++)
  153. {
  154. validSections.Add(section);
  155. }
  156. }
  157. }
  158. //Having generated our list, we choose a random segment from it
  159. foreach (MapSection section in validSections)
  160. {
  161. //Debug.Log("Valid section: " + section.name);
  162. }/**/
  163. //Debug.Log("Choosing section");
  164. MapSection nextSection = validSections[(int)Random.Range(0.0f, (float)validSections.Count)];
  165. //Debug.Log("Chosen section: " + nextSection.name);
  166. addSection(nextSection);
  167. }
  168. void addSection(MapSection section)
  169. {
  170. //Instantiate new section at x = endX
  171. Vector3 pos = new Vector3(endX, 0.0f, 0.0f);
  172. GameObject newSection = (GameObject)Instantiate(section.gameObject, pos, Quaternion.identity);
  173. //GameObject.Instantiate(section.gameObject, pos, Quaternion.identity);
  174. MapSection newSectionScript = newSection.GetComponent<MapSection>();
  175. newSectionScript.InitSection(activeSections.Count);
  176. newSection.name = newSectionScript.name;
  177. activeSections.Add(newSectionScript);
  178. lastSection = newSectionScript;
  179. endX += newSectionScript.length;
  180. totalSections++;
  181. }
  182. bool checkSegments(MapSection second)
  183. {
  184. return checkSegments(this.lastSection, second);
  185. }
  186. bool checkSegments(MapSection first, MapSection second)
  187. {
  188. int connections = 0;
  189. //Debug.Log("Checking " + first.name + ", " + second.name);
  190. //No more than one link section in a row
  191. if (first.length == 1 && second.length == 1)
  192. {
  193. return false;
  194. }
  195. //if (second.difficulty < diffMin || second.difficulty > diffMax)
  196. if (second.difficultyMax < difficulty || second.difficultyMin > difficulty) //Check that we're in the right difficulty range for this section
  197. {
  198. //Debug.Log("Difficulty = " + difficulty + ", max = " + second.difficultyMax + ", min = " + second.difficultyMin);
  199. return false;
  200. }
  201. if (second.widthIn < widthMin || second.widthIn > widthMax) //And that it's in the right width range
  202. {
  203. //Debug.Log("width = " + second.widthIn + ", max = " + widthMin + ", min = " + widthMax);
  204. return false;
  205. }
  206. ////Debug.Log("Checking sections: first = " + first.name + ", second = " + second.name);
  207. foreach (GameObject exit in first.exits)
  208. {
  209. foreach (GameObject entry in second.entrances)
  210. {
  211. //Debug.Log("Checking connections: exit = " + exit.transform.localPosition.z + ", " + exit.transform.localPosition.x+ ", entry = " + entry.transform.localPosition.z + ", " + entry.transform.localPosition.x);/**/
  212. if (checkConnection(exit, entry))
  213. {
  214. connections++;
  215. }
  216. }
  217. }
  218. ////Debug.Log("Connections = " + connections);
  219. if (connections >= minConns)
  220. {
  221. ////Debug.Log("Valid section!");
  222. }
  223. else
  224. {
  225. ////Debug.Log("Invalid section!");
  226. }
  227. return (connections >= minConns);
  228. }
  229. bool checkConnection(GameObject exit, GameObject entry)
  230. {
  231. /*//Debug.Log("Checking connections: exit = " + exit.transform.localPosition.z + ", " + exit.transform.localPosition.x
  232. + ", entry = " + entry.transform.localPosition.z + ", " + entry.transform.localPosition.x);*/
  233. //If the squares being checked don't line up, the connection is invalid
  234. if (exit.transform.localPosition.z != entry.transform.localPosition.z)
  235. {
  236. ////Debug.Log(exit.transform.localPosition.z + " != " + entry.transform.localPosition.z);
  237. return false;
  238. }
  239. //If both components require jumping (pits or water), the connection is invalid
  240. //It's technically possible to cross two water blocks, but we don't count that
  241. if (requiresJump(exit) && requiresJump(entry))
  242. {
  243. ////Debug.Log("Invalid connection - both water");
  244. return false;
  245. }
  246. ////Debug.Log("Exit.is_Walkable = " + exit.GetComponent<Block>().is_Walkable + ", Entry.is_Walkable = " + entry.GetComponent<Block>().is_Walkable);
  247. //Since we currently don't let people jump over walls, if either block is a wall, the connection is invalid
  248. if (isWall(exit) || isWall(entry))
  249. {
  250. ////Debug.Log("Invalid connection - not walkable");
  251. return false;
  252. }
  253. ////Debug.Log("Exit.isWater = " + exit.GetComponent<Block>().isWater + ", Entry.isWater = " + entry.GetComponent<Block>().isWater);
  254. ////Debug.Log("Valid connection!");
  255. //If we've passed all these tests, the connection is valid!
  256. return true;
  257. }
  258. bool requiresJump(GameObject block)
  259. {
  260. if (block.GetComponent<Block>() == null) //The object must be a pit trap
  261. {
  262. return true;
  263. }
  264. return block.GetComponent<Block>().isWater; //If it's not a pit, then whether it requires jumping depends on whether it's water or not
  265. }
  266. bool isWall(GameObject block)
  267. {
  268. if (block.GetComponent<Block>() == null)
  269. {
  270. return true;
  271. }
  272. return !(block.GetComponent<Block>().is_Walkable);
  273. }
  274. //Check whether it's time to extend the track forward
  275. void checkForward()
  276. {
  277. //We check if the end of the last section of track is in sight
  278. Vector3 trackEnd = new Vector3(endX, 0.0f); //Get the middle of the end of the last track section
  279. //If it is, then we add a new section
  280. if (checkView(trackEnd))
  281. {
  282. chooseNextSection();
  283. checkForward();
  284. }
  285. }
  286. //Check whether it's time to delete the oldest section of active track
  287. void checkBack()
  288. {
  289. //We check if the end of the first section of track is still in sight
  290. Vector3 firstSectionEnd = new Vector3(startX + activeSections[0].length, 0.0f); //Get the middle of the end of the first track section
  291. //If it's not, then we remove it
  292. if (!(checkView(firstSectionEnd)))
  293. {
  294. startX += activeSections[0].length;
  295. activeSections[0].destroySection();
  296. activeSections.RemoveAt(0);
  297. }
  298. }
  299. //Check whether a point is in sight or not
  300. bool checkView(Vector3 point)
  301. {
  302. Vector3 screenPoint = Camera.main.WorldToViewportPoint(point); //Map it into viewport space
  303. //The camera's field of view is represented by 0 > (x, y) < 1, with z being the distance from the camera
  304. return (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1);
  305. }
  306. //Checks in both directions for sections needing to be added or removed
  307. public void checkTrack()
  308. {
  309. checkForward();
  310. checkBack();
  311. }
  312. //Updates minimum and maximum difficulty, width, etc, based on current gamestate
  313. public void updateCriteria()
  314. {
  315. //Start with base values
  316. difficulty = diffStart;
  317. //By default, we can add a section 1 tile wider or narrower on either side than the last section
  318. widthMin = lastSection.widthOut - 2;
  319. widthMax = lastSection.widthOut + 2;
  320. /* Calculate min & max difficulties & width modifications
  321. * We recalculate from scratch each time (that is,
  322. * each time a section is added) so as to avoid having
  323. * to track which one-off increase has been applied
  324. * and which hasn't
  325. */
  326. //As the number of players shrinks, we ramp up the difficulty and contract the track
  327. if (initialPlayerCount > 0)
  328. {
  329. if (clients.ConnectedClients.Count <= (float)(0.5f * initialPlayerCount))
  330. {
  331. //Debug.Log("Initial players = " + initialPlayerCount + ", current players = " + clients.ConnectedClients.Count + ", player count at half or below");
  332. difficulty++;
  333. widthMin -= 2;
  334. widthMax -= 2;
  335. }
  336. if (clients.ConnectedClients.Count <= (float)(0.33f * initialPlayerCount))
  337. {
  338. //Debug.Log("Initial players = " + initialPlayerCount + ", current players = " + clients.ConnectedClients.Count + ", player count below 33%");
  339. difficulty += 2;
  340. widthMin -= 2;
  341. widthMax -= 2;
  342. }
  343. }
  344. //Ramp up the difficulty as the track extends
  345. difficulty += ((int)endX - (int)startX) / 7;
  346. /*diffMin += (totalSections - 2) / 2;
  347. diffMax += totalSections / 2;*/
  348. //Dropping the difficulty-based track narrowing for now - we have wide sections designed to be hard, we want them to show up
  349. //Once the difficulty has ramped up, we cause the track to steadily narrow
  350. //It can no longer widen, only remain constant or shrink
  351. //if (diffMin > 2)
  352. /*if (difficulty > 2)
  353. {
  354. widthMax--;
  355. }*/
  356. //Apply caps
  357. if (difficulty > diffCap)
  358. {
  359. difficulty = diffCap;
  360. }
  361. if (widthMin < widthMinMin)
  362. {
  363. widthMin = widthMinMin;
  364. }
  365. if (widthMax < widthMaxMin)
  366. {
  367. widthMax = widthMaxMin;
  368. }
  369. }
  370. // Update is called once per frame
  371. void Update()
  372. {
  373. checkTrack();
  374. }
  375. }