SpaceWorldGenerator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using GadgetCore.API;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using UnityEngine;
  8. using Random = System.Random;
  9. namespace SpacePlanet
  10. {
  11. public static class GameObjectSpaceWorldGeneratorExtension
  12. {
  13. public static void AddToWorld(this GameObject obj, GameObject g)
  14. {
  15. obj.transform.parent = g.transform;
  16. }
  17. public static void AddObjectToWorld(this GameObject obj, GameObject g, List<GameObject> objList)
  18. {
  19. obj.transform.parent = g.transform;
  20. objList.Add(obj);
  21. }
  22. }
  23. public class SpaceWorldGenerator
  24. {
  25. private static int W = 32; //128 + 64;
  26. private static int H = 32;//128 - 32;
  27. private static GameObject GetBaseBlock(float w, float h, float posX, float posY, Texture2D texture)
  28. {
  29. GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/midChunk0"), new Vector3((float)(242 + posX - 60), posY - 60, 5f), Quaternion.Euler(0f, 180f, 180f));
  30. gameObject.transform.localScale = new Vector3(w, h, 1);
  31. BoxCollider collider = gameObject.GetComponentInChildren<BoxCollider>();
  32. collider.size = new Vector3(2, 2, 5);
  33. Renderer renderer = gameObject.GetComponentInChildren<Renderer>();
  34. renderer.material = new Material(Shader.Find("Transparent/Diffuse"))
  35. {
  36. mainTexture = texture
  37. };
  38. return gameObject;
  39. }
  40. private static GameObject GetBorderBlock(float w, float h, float posX, float posY, float moveX, float moveY)
  41. {
  42. GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/midChunk0"), new Vector3((float)(242 + posX), posY, 5f), Quaternion.Euler(0f, 180f, 180f));
  43. gameObject.transform.localScale = new Vector3(w, h, 1);
  44. BoxCollider collider = gameObject.GetComponentInChildren<BoxCollider>();
  45. collider.isTrigger = true;
  46. collider.size = new Vector3(2, 2, 1);
  47. Renderer renderer = gameObject.GetComponentInChildren<Renderer>();
  48. renderer.material = new Material(Shader.Find("Transparent/Diffuse"))
  49. {
  50. mainTexture = null
  51. };
  52. return gameObject;
  53. }
  54. private static GameObject GetStarTextureObject(Texture2D texture)
  55. {
  56. GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/midChunk0"), new Vector3(0f, 0f, 20f), Quaternion.Euler(0f, 180f, 180f));
  57. gameObject.layer = 0;
  58. gameObject.transform.localScale = new Vector3(32, 32, 1);
  59. BoxCollider collider = gameObject.GetComponentInChildren<BoxCollider>();
  60. collider.isTrigger = true;
  61. collider.size = new Vector3(2, 2, 1);
  62. Renderer renderer = gameObject.GetComponentInChildren<Renderer>();
  63. renderer.material = new Material(Shader.Find("Transparent/Diffuse"))
  64. {
  65. mainTexture = texture
  66. };
  67. gameObject.AddComponent<StarTextureScript>();
  68. return gameObject;
  69. }
  70. private static float GetSizeForPixels(int px)
  71. {
  72. return 1f / 16f * px;
  73. }
  74. private static void MarkGeneration(int x, int y, int[][] arr)
  75. {
  76. if (x >= 0 && x < W && y >= 0 && y < H)
  77. arr[x][y] = 1;
  78. }
  79. private static bool CheckGeneration(int x, int y, int[][] arr)
  80. {
  81. if (x >= 0 && x < W && y >= 0 && y < H)
  82. if (arr[x][y] == 0)
  83. return true;
  84. else return false;
  85. return true;
  86. }
  87. private static IEnumerator SpawnEnemy(string s, Vector3 pos)
  88. {
  89. yield return new WaitForSeconds(1);
  90. Network.Instantiate(Resources.Load(s), pos, Quaternion.identity, 0);
  91. yield break;
  92. }
  93. public static void StartGenarateWorld(SpawnerScript s, int[] param)
  94. {
  95. s.StartCoroutine(GenarateWorld(s, param));
  96. }
  97. public static IEnumerator GenarateWorld(SpawnerScript s, int[] param)
  98. {
  99. yield return new WaitForSeconds(0.1f);
  100. long seed = 0;
  101. string seedString = "";
  102. for (int i = 1; i < param.Length; i++)
  103. {
  104. seed += param[i];
  105. seed *= 10;
  106. }
  107. for (int i = 1; i < param.Length; i++)
  108. seedString += " " + param[i];
  109. Core.logger.Log("Generating world with seed:" + seed + " - " + seed.GetHashCode() + " - " + seedString);
  110. Random random = new Random(seed.GetHashCode());
  111. Random serverRandom = new Random();
  112. //s.backLights.SetActive(true);
  113. s.mainLight.SetActive(true);
  114. s.mainLight.GetComponentInChildren<Light>().color = new Color(1f, 1f, 1f, 1f);
  115. Texture2D textureParalex0 = GadgetCoreAPI.LoadTexture2D("bSpacebg0.png");
  116. Texture2D textureMeteor1 = GadgetCoreAPI.LoadTexture2D("meteor1.png");
  117. Texture2D textureMeteor3 = GadgetCoreAPI.LoadTexture2D("meteor3.png");
  118. Texture2D textureMeteor4 = GadgetCoreAPI.LoadTexture2D("meteor4.png");
  119. Texture2D textureStars = GadgetCoreAPI.LoadTexture2D("stars.png");
  120. var fieldChunks = typeof(SpawnerScript).GetField("chunks", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
  121. var chunks = fieldChunks.GetValue(s) as GameObject[];
  122. var baseObject = new GameObject();
  123. var chunk = baseObject.AddComponent<Chunk>();
  124. chunks[0] = baseObject;
  125. var objects = new List<GameObject>();
  126. GetStarTextureObject(textureStars).AddToWorld(baseObject);
  127. int[][] genSpots = new int[W][];
  128. for (int i = 0; i < W; i++)
  129. genSpots[i] = new int[H];
  130. {
  131. int x = 15;
  132. int y = 10;
  133. for (int xOff = -2; xOff < 3; xOff++)
  134. for (int yOff = -2; yOff < 6; yOff++)
  135. MarkGeneration(x + xOff, y + yOff, genSpots);
  136. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  137. }
  138. {
  139. int r = 0;
  140. int overflow = 0;
  141. int x = random.Next(W / 2) + W / 2;
  142. int y = random.Next(H / 2) + H / 2;
  143. bool addLoopPortal = false;
  144. foreach (var gadget in GadgetCore.API.Gadgets.ListAllEnabledGadgets())
  145. if (gadget.Info?.ModName == "Loop Portal")
  146. {
  147. addLoopPortal = true;
  148. break;
  149. }
  150. while (overflow < 1000 && r < 3)
  151. {
  152. overflow++;
  153. bool noGen = false;
  154. for (int xOff = -1; xOff < 2; xOff++)
  155. for (int yOff = -1; yOff < 4; yOff++)
  156. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  157. noGen = true;
  158. if (!noGen)
  159. {
  160. for (int xOff = -1; xOff < 2; xOff++)
  161. for (int yOff = -1; yOff < 4; yOff++)
  162. MarkGeneration(x + xOff, y + yOff, genSpots);
  163. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  164. if (Network.isServer)
  165. {
  166. GameScript.endPortal[r] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.85f, 0f), Quaternion.identity, 0);
  167. GameScript.endPortalUA[r] = GameScript.endPortal[r].transform.GetChild(0).gameObject;
  168. GameScript.endPortal[r].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  169. GameScript.endPortalUA[r].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { 0, 0, r });
  170. GameScript.endPortal[r].AddToWorld(baseObject);
  171. }
  172. r++;
  173. }
  174. x = Mathf.Min(x + random.Next(5) - 2, W);
  175. y = Mathf.Min(y + random.Next(5) - 2, H);
  176. }
  177. while (addLoopPortal && overflow < 5000)
  178. {
  179. overflow++;
  180. bool noGen = false;
  181. for (int xOff = -1; xOff < 2; xOff++)
  182. for (int yOff = -1; yOff < 4; yOff++)
  183. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  184. noGen = true;
  185. if (!noGen)
  186. {
  187. for (int xOff = -1; xOff < 2; xOff++)
  188. for (int yOff = -1; yOff < 4; yOff++)
  189. MarkGeneration(x + xOff, y + yOff, genSpots);
  190. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  191. if (Network.isServer)
  192. {
  193. var i = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.85f, 0f), Quaternion.identity, 0);
  194. var iUA = i.transform.GetChild(0).gameObject;
  195. i.GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  196. iUA.GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { SpawnerScript.curBiome, 0, 4 });
  197. i.transform.parent = GameScript.endPortal[0].transform;
  198. }
  199. break;
  200. }
  201. x = Mathf.Min(x + random.Next(5) - 2, W);
  202. y = Mathf.Min(y + random.Next(5) - 2, H);
  203. }
  204. }
  205. {
  206. int r = 0;
  207. int overflow = 0;
  208. while (overflow < 15000 && r < 700)
  209. {
  210. overflow++;
  211. int i = random.Next(6);
  212. int x = random.Next(W);
  213. int y = random.Next(H);
  214. bool noGen = false;
  215. switch (i)
  216. {
  217. case 0:
  218. case 1:
  219. case 2:
  220. for (int xOff = -1; xOff < 2; xOff++)
  221. for (int yOff = -1; yOff < 2; yOff++)
  222. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  223. noGen = true;
  224. break;
  225. case 3:
  226. case 4:
  227. for (int xOff = -1; xOff < 2; xOff++)
  228. for (int yOff = -1; yOff < 2; yOff++)
  229. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  230. noGen = true;
  231. break;
  232. case 5:
  233. for (int xOff = -2; xOff < 3; xOff++)
  234. for (int yOff = -2; yOff < 3; yOff++)
  235. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  236. noGen = true;
  237. break;
  238. }
  239. if (!noGen)
  240. {
  241. r++;
  242. switch (i)
  243. {
  244. case 0:
  245. case 1:
  246. case 2:
  247. for (int xOff = -1; xOff < 2; xOff++)
  248. for (int yOff = -1; yOff < 2; yOff++)
  249. MarkGeneration(x + xOff, y + yOff, genSpots);
  250. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  251. if (Network.isServer)
  252. {
  253. var position = new Vector3((float)(242 + x * 4 - 60), y * 4 - 60 + GetSizeForPixels(54) + 0.425f, 0f);
  254. //s.StartCoroutine(SpawnEnemy("e/wasp", position + new Vector3(0, 2, 0)));
  255. //var emeny = (GameObject)Network.Instantiate((GameObject)Resources.Load("e/axelarkBall"), position, Quaternion.identity, 0);
  256. if(serverRandom.Next(2) == 0)
  257. {
  258. var obj = (GameObject)Network.Instantiate(Core.objectSpaceOre.Object, position, Quaternion.identity, 0);
  259. obj.AddObjectToWorld(baseObject, objects);
  260. }
  261. else
  262. {
  263. var obj = (GameObject)Network.Instantiate(Core.objectSpaceOreBig.Object, position, Quaternion.identity, 0);
  264. obj.AddObjectToWorld(baseObject, objects);
  265. }
  266. //(GameObject)Resources.Load("obj/bugspot0")
  267. //bugspot.GetComponentInChildren<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  268. //bugspot.GetComponentInChildren<EnemyScript>().enabled = true;
  269. //enemy.SetActive(true);
  270. //enemy.layer = 9;
  271. // Network.Instantiate(Resources.Load("e/pirate"), position + new Vector3(0, 2, 0), Quaternion.identity, 0);
  272. //s.StartCoroutine(SpawnEnemy("e/pirate", position + new Vector3(0, 2, 0)));
  273. }
  274. break;
  275. case 3:
  276. case 4:
  277. for (int xOff = -1; xOff < 2; xOff++)
  278. for (int yOff = -1; yOff < 2; yOff++)
  279. MarkGeneration(x + xOff, y + yOff, genSpots);
  280. GetBaseBlock(GetSizeForPixels(38), GetSizeForPixels(38), x * 4, y * 4, textureMeteor3).AddToWorld(baseObject);
  281. break;
  282. case 5:
  283. for (int xOff = -2; xOff < 3; xOff++)
  284. for (int yOff = -2; yOff < 3; yOff++)
  285. MarkGeneration(x + xOff, y + yOff, genSpots);
  286. GetBaseBlock(GetSizeForPixels(128), GetSizeForPixels(128), x * 4, y * 4, textureMeteor4).AddToWorld(baseObject);
  287. break;
  288. }
  289. }
  290. }
  291. }
  292. var objArray = objects.ToArray();
  293. chunk.objectiveSpawn = objArray;
  294. yield break;
  295. }
  296. public static void GenarateTown(SpawnerScript s, int[] param)
  297. {
  298. long seed = 0;
  299. string seedString = "";
  300. for (int i = 1; i < param.Length; i++)
  301. {
  302. seed += param[i];
  303. seed *= 10;
  304. }
  305. for (int i = 1; i < param.Length; i++)
  306. seedString += " " + param[i];
  307. Core.logger.Log("Generating town with seed:" + seed + " - " + seedString);
  308. Random random = new Random((int)seed);
  309. //s.backLights.SetActive(true);
  310. s.mainLight.SetActive(true);
  311. s.mainLight.GetComponentInChildren<Light>().color = new Color(1f, 1f, 1f, 1f);
  312. Texture2D textureParalex0 = GadgetCoreAPI.LoadTexture2D("bSpacebg0.png");
  313. Texture2D textureMeteor1 = GadgetCoreAPI.LoadTexture2D("meteor1.png");
  314. Texture2D textureMeteor3 = GadgetCoreAPI.LoadTexture2D("meteor3.png");
  315. Texture2D textureMeteor4 = GadgetCoreAPI.LoadTexture2D("meteor4.png");
  316. Texture2D textureStars = GadgetCoreAPI.LoadTexture2D("stars.png");
  317. var fieldChunks = typeof(SpawnerScript).GetField("chunks", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
  318. var chunks = fieldChunks.GetValue(s) as GameObject[];
  319. var baseObject = new GameObject();
  320. chunks[0] = baseObject;
  321. GetStarTextureObject(textureStars).AddToWorld(baseObject);
  322. int[][] genSpots = new int[W][];
  323. for (int i = 0; i < W; i++)
  324. genSpots[i] = new int[H];
  325. if (false)
  326. {
  327. int x = 15;
  328. int y = 10;
  329. for (int xOff = -2; xOff < 3; xOff++)
  330. for (int yOff = -2; yOff < 6; yOff++)
  331. MarkGeneration(x + xOff, y + yOff, genSpots);
  332. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  333. }
  334. {
  335. int x = 22;
  336. int y = 10;
  337. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  338. if (Network.isServer)
  339. {
  340. GameScript.endPortal[1] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.85f, 0f), Quaternion.identity, 0);
  341. GameScript.endPortalUA[1] = GameScript.endPortal[1].transform.GetChild(0).gameObject;
  342. GameScript.endPortal[1].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  343. GameScript.endPortalUA[1].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.spacePlanetId, 0, 1 });
  344. }
  345. }
  346. {
  347. int x = 15;
  348. int y = 10;
  349. GetBaseBlock(GetSizeForPixels(128), GetSizeForPixels(128), x * 4, y * 4, textureMeteor4).AddToWorld(baseObject);
  350. if (Network.isServer)
  351. {
  352. GameScript.endPortal[3] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(128) + 1.85f, 0f), Quaternion.identity, 0);
  353. GameScript.endPortalUA[3] = GameScript.endPortal[3].transform.GetChild(0).gameObject;
  354. GameScript.endPortal[3].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  355. GameScript.endPortalUA[3].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { 98, 0, 3 });
  356. }
  357. }
  358. if (false)
  359. {
  360. int r = 0;
  361. int overflow = 0;
  362. while (overflow < 15000 && r < 700)
  363. {
  364. overflow++;
  365. int i = random.Next(6);
  366. int x = random.Next(W);
  367. int y = random.Next(H);
  368. bool noGen = false;
  369. switch (i)
  370. {
  371. case 0:
  372. case 1:
  373. case 2:
  374. for (int xOff = -1; xOff < 2; xOff++)
  375. for (int yOff = -1; yOff < 2; yOff++)
  376. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  377. noGen = true;
  378. break;
  379. case 3:
  380. case 4:
  381. for (int xOff = -1; xOff < 2; xOff++)
  382. for (int yOff = -1; yOff < 2; yOff++)
  383. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  384. noGen = true;
  385. break;
  386. case 5:
  387. for (int xOff = -2; xOff < 3; xOff++)
  388. for (int yOff = -2; yOff < 3; yOff++)
  389. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  390. noGen = true;
  391. break;
  392. }
  393. if (!noGen)
  394. {
  395. r++;
  396. switch (i)
  397. {
  398. case 0:
  399. case 1:
  400. case 2:
  401. for (int xOff = -1; xOff < 2; xOff++)
  402. for (int yOff = -1; yOff < 2; yOff++)
  403. MarkGeneration(x + xOff, y + yOff, genSpots);
  404. GetBaseBlock(GetSizeForPixels(54), GetSizeForPixels(54), x * 4, y * 4, textureMeteor1).AddToWorld(baseObject);
  405. break;
  406. case 3:
  407. case 4:
  408. for (int xOff = -1; xOff < 2; xOff++)
  409. for (int yOff = -1; yOff < 2; yOff++)
  410. MarkGeneration(x + xOff, y + yOff, genSpots);
  411. GetBaseBlock(GetSizeForPixels(38), GetSizeForPixels(38), x * 4, y * 4, textureMeteor3).AddToWorld(baseObject);
  412. break;
  413. case 5:
  414. for (int xOff = -2; xOff < 3; xOff++)
  415. for (int yOff = -2; yOff < 3; yOff++)
  416. MarkGeneration(x + xOff, y + yOff, genSpots);
  417. GetBaseBlock(GetSizeForPixels(128), GetSizeForPixels(128), x * 4, y * 4, textureMeteor4).AddToWorld(baseObject);
  418. break;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. }