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.

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