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.

455 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. //if (second.difficulty < diffMin || second.difficulty > diffMax)
  191. if (second.difficultyMax < difficulty || second.difficultyMin > difficulty) //Check that we're in the right difficulty range for this section
  192. {
  193. Debug.Log("Difficulty = " + difficulty + ", max = " + second.difficultyMax + ", min = " + second.difficultyMin);
  194. return false;
  195. }
  196. if (second.widthIn < widthMin || second.widthIn > widthMax) //And that it's in the right width range
  197. {
  198. Debug.Log("width = " + second.widthIn + ", max = " + widthMin + ", min = " + widthMax);
  199. return false;
  200. }
  201. //Debug.Log("Checking sections: first = " + first.name + ", second = " + second.name);
  202. foreach (GameObject exit in first.exits)
  203. {
  204. foreach (GameObject entry in second.entrances)
  205. {
  206. Debug.Log("Checking connections: exit = " + exit.transform.localPosition.z + ", " + exit.transform.localPosition.x
  207. + ", entry = " + entry.transform.localPosition.z + ", " + entry.transform.localPosition.x);/**/
  208. if (checkConnection(exit, entry))
  209. {
  210. connections++;
  211. }
  212. }
  213. }
  214. //Debug.Log("Connections = " + connections);
  215. if (connections >= minConns)
  216. {
  217. //Debug.Log("Valid section!");
  218. }
  219. else
  220. {
  221. //Debug.Log("Invalid section!");
  222. }
  223. return (connections >= minConns);
  224. }
  225. bool checkConnection(GameObject exit, GameObject entry)
  226. {
  227. /*Debug.Log("Checking connections: exit = " + exit.transform.localPosition.z + ", " + exit.transform.localPosition.x
  228. + ", entry = " + entry.transform.localPosition.z + ", " + entry.transform.localPosition.x);*/
  229. //If the squares being checked don't line up, the connection is invalid
  230. if (exit.transform.localPosition.z != entry.transform.localPosition.z)
  231. {
  232. //Debug.Log(exit.transform.localPosition.z + " != " + entry.transform.localPosition.z);
  233. return false;
  234. }
  235. //If both components require jumping (pits or water), the connection is invalid
  236. //It's technically possible to cross two water blocks, but we don't count that
  237. if (requiresJump(exit) && requiresJump(entry))
  238. {
  239. //Debug.Log("Invalid connection - both water");
  240. return false;
  241. }
  242. //Debug.Log("Exit.is_Walkable = " + exit.GetComponent<Block>().is_Walkable + ", Entry.is_Walkable = " + entry.GetComponent<Block>().is_Walkable);
  243. //Since we currently don't let people jump over walls, if either block is a wall, the connection is invalid
  244. if (isWall(exit) || isWall(entry))
  245. {
  246. //Debug.Log("Invalid connection - not walkable");
  247. return false;
  248. }
  249. //Debug.Log("Exit.isWater = " + exit.GetComponent<Block>().isWater + ", Entry.isWater = " + entry.GetComponent<Block>().isWater);
  250. //Debug.Log("Valid connection!");
  251. //If we've passed all these tests, the connection is valid!
  252. return true;
  253. }
  254. bool requiresJump(GameObject block)
  255. {
  256. if (block.GetComponent<Block>() == null) //The object must be a pit trap
  257. {
  258. return true;
  259. }
  260. return block.GetComponent<Block>().isWater; //If it's not a pit, then whether it requires jumping depends on whether it's water or not
  261. }
  262. bool isWall(GameObject block)
  263. {
  264. if (block.GetComponent<Block>() == null)
  265. {
  266. return true;
  267. }
  268. return !(block.GetComponent<Block>().is_Walkable);
  269. }
  270. //Check whether it's time to extend the track forward
  271. void checkForward()
  272. {
  273. //We check if the end of the last section of track is in sight
  274. Vector3 trackEnd = new Vector3(endX, 0.0f); //Get the middle of the end of the last track section
  275. //If it is, then we add a new section
  276. if (checkView(trackEnd))
  277. {
  278. chooseNextSection();
  279. checkForward();
  280. }
  281. }
  282. //Check whether it's time to delete the oldest section of active track
  283. void checkBack()
  284. {
  285. //We check if the end of the first section of track is still in sight
  286. Vector3 firstSectionEnd = new Vector3(startX + activeSections[0].length, 0.0f); //Get the middle of the end of the first track section
  287. //If it's not, then we remove it
  288. if (!(checkView(firstSectionEnd)))
  289. {
  290. startX += activeSections[0].length;
  291. activeSections[0].destroySection();
  292. activeSections.RemoveAt(0);
  293. }
  294. }
  295. //Check whether a point is in sight or not
  296. bool checkView(Vector3 point)
  297. {
  298. Vector3 screenPoint = Camera.main.WorldToViewportPoint(point); //Map it into viewport space
  299. //The camera's field of view is represented by 0 > (x, y) < 1, with z being the distance from the camera
  300. return (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1);
  301. }
  302. //Checks in both directions for sections needing to be added or removed
  303. public void checkTrack()
  304. {
  305. checkForward();
  306. checkBack();
  307. }
  308. //Updates minimum and maximum difficulty, width, etc, based on current gamestate
  309. public void updateCriteria()
  310. {
  311. //Start with base values
  312. difficulty = diffStart;
  313. //By default, we can add a section 1 tile wider or narrower on either side than the last section
  314. widthMin = lastSection.widthOut - 2;
  315. widthMax = lastSection.widthOut + 2;
  316. /* Calculate min & max difficulties & width modifications
  317. * We recalculate from scratch each time (that is,
  318. * each time a section is added) so as to avoid having
  319. * to track which one-off increase has been applied
  320. * and which hasn't
  321. */
  322. /*diffMin = diffMinStart;
  323. diffMax = diffMaxStart;*/
  324. //As the number of players shrinks, we ramp up the difficulty and contract the track
  325. if (initialPlayerCount > 0)
  326. {
  327. if (clients.ConnectedClients.Count <= (float)(0.5f * initialPlayerCount))
  328. {
  329. Debug.Log("Initial players = " + initialPlayerCount + ", current players = " + clients.ConnectedClients.Count + ", player count at half or below");
  330. /*diffMin++;
  331. diffMax++;*/
  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. /*diffMin++;
  340. diffMax += 2;*/
  341. difficulty += 2;
  342. widthMin -= 2;
  343. widthMax -= 2;
  344. }
  345. }
  346. //Ramp up the difficulty as the track extends
  347. difficulty += ((int)endX - (int)startX) / 10;
  348. /*diffMin += (totalSections - 2) / 2;
  349. diffMax += totalSections / 2;*/
  350. //Dropping the difficulty-based track narrowing for now - we have wide sections designed to be hard, we want them to show up
  351. //Once the difficulty has ramped up, we cause the track to steadily narrow
  352. //It can no longer widen, only remain constant or shrink
  353. //if (diffMin > 2)
  354. /*if (difficulty > 2)
  355. {
  356. widthMax--;
  357. }*/
  358. //Apply caps
  359. //if (diffMin > diffMinMax)
  360. if (difficulty > diffCap)
  361. {
  362. //diffMin = diffMinMax;
  363. difficulty = diffCap;
  364. }
  365. if (widthMin < widthMinMin)
  366. {
  367. widthMin = widthMinMin;
  368. }
  369. if (widthMax < widthMaxMin)
  370. {
  371. widthMax = widthMaxMin;
  372. }
  373. }
  374. // Update is called once per frame
  375. void Update()
  376. {
  377. checkTrack();
  378. }
  379. }