MeteroidGenerator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 Subworlds
  10. {
  11. public class MeteroidGenerator
  12. {
  13. private static readonly FieldInfo chunksField = typeof(SpawnerScript).GetField("chunks", BindingFlags.NonPublic | BindingFlags.Instance);
  14. private static readonly Material materialaBackLight = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Meteoroid/parallax.png") };
  15. private static readonly Material materialaParalexStars = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Meteoroid/bgStars.png") };
  16. private static readonly Material materialaParalexNone = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("Meteoroid/bgNone.png") };
  17. public static readonly FieldInfo networkStuffField = typeof(Chunk).GetField("networkStuff", BindingFlags.NonPublic | BindingFlags.Instance);
  18. public static readonly FieldInfo tempField = typeof(Chunk).GetField("temp", BindingFlags.NonPublic | BindingFlags.Instance);
  19. private static int W = 32; //128 + 64;
  20. private static int H = 32; //128 - 32;
  21. private static float GetSizeForPixels(int px)
  22. {
  23. return 1f / 16f * px;
  24. }
  25. private static void MarkGeneration(int x, int y, int[][] arr)
  26. {
  27. if (x >= 0 && x < W && y >= 0 && y < H)
  28. arr[x][y] = 1;
  29. }
  30. private static bool CheckGeneration(int x, int y, int[][] arr)
  31. {
  32. if (x >= 0 && x < W && y >= 0 && y < H)
  33. if (arr[x][y] == 0)
  34. return true;
  35. else return false;
  36. return true;
  37. }
  38. public static void GenarateTown(SpawnerScript s, int[] param)
  39. {
  40. GameScript.isTown = true;
  41. if (GameScript.districtLevel % 2 != 0)
  42. {
  43. GameScript.districtLevel++;
  44. }
  45. s.backLights.SetActive(true);
  46. s.mainLight.SetActive(false);
  47. s.backLights.GetComponent<Renderer>().material = materialaBackLight;
  48. //s.mainLight.GetComponentInChildren<Light>().color = new Color(0.05f, 0.05f, 0.05f, 0.1f);
  49. s.bg[0].GetComponent<Renderer>().material = materialaParalexStars;
  50. s.bg[1].GetComponent<Renderer>().material = materialaParalexNone;
  51. s.bg[2].GetComponent<Renderer>().material = materialaParalexNone;
  52. s.bg[3].GetComponent<Renderer>().material = materialaParalexNone;
  53. var chunks = (GameObject[])chunksField.GetValue(s);
  54. var num = 0;
  55. chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/Subworlds/MeteorBase"), new Vector3((float)(218 + 0 * 128 + 4 * 8), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
  56. num++;
  57. if (Network.isServer)
  58. {
  59. chunks[num] = new GameObject("chunk");
  60. var chunk = chunks[num].AddComponent<Chunk>();
  61. PopulateTown(chunk);
  62. num++;
  63. }
  64. }
  65. public static void Genarate(SpawnerScript s, int[] param)
  66. {
  67. GameScript.isTown = false;
  68. s.backLights.SetActive(true);
  69. s.mainLight.SetActive(false);
  70. s.backLights.GetComponent<Renderer>().material = materialaBackLight;
  71. s.bg[0].GetComponent<Renderer>().material = materialaParalexStars;
  72. s.bg[1].GetComponent<Renderer>().material = materialaParalexNone;
  73. s.bg[2].GetComponent<Renderer>().material = materialaParalexNone;
  74. s.bg[3].GetComponent<Renderer>().material = materialaParalexNone;
  75. //s.backLights.SetActive(true);
  76. s.mainLight.SetActive(true);
  77. s.mainLight.GetComponentInChildren<Light>().color = new Color(1f, 1f, 1f, 1f);
  78. var chunks = (GameObject[])chunksField.GetValue(s);
  79. var num = 0;
  80. try
  81. {
  82. if (Network.isServer)
  83. {
  84. chunks[num] = new GameObject("chunk");
  85. var chunk = chunks[num].AddComponent<Chunk>();
  86. PopulateWorld(chunk);
  87. num++;
  88. }
  89. }
  90. catch (System.Exception e) { Core.logger.LogConsole(e); }
  91. }
  92. private static List<int[]> spawnLocations = new List<int[]>();
  93. private static System.Random random = new System.Random();
  94. public static void PopulateTown(Chunk chunk)
  95. {
  96. var networkStuff = new GameObject[400];
  97. networkStuffField.SetValue(chunk, networkStuff);
  98. try
  99. {
  100. int temp = (int)tempField.GetValue(chunk);
  101. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npcStorage"), new Vector3(248f - 4 * 1, -6.118f, 0.3f), Quaternion.identity, 0);
  102. temp++;
  103. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc2103"), new Vector3(248f + 4 * 0, -6.118f, 0.3f), Quaternion.identity, 0);
  104. temp++;
  105. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc/npcQuest"), new Vector3(248f + 4 * 2, -6.118f, 0.3f), Quaternion.identity, 0);
  106. temp++;
  107. }
  108. catch (System.Exception e) { Core.logger.LogError(e); }
  109. GameScript.endPortal[0] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f + 4 * 4, -6.118f, 0f), Quaternion.identity, 0);
  110. GameScript.endPortalUA[0] = GameScript.endPortal[0].transform.GetChild(0).gameObject;
  111. GameScript.endPortal[0].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  112. GameScript.endPortalUA[0].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { Core.lastBiome == -1 ? Core.meteroidPlanet.GetID() : Core.lastBiome, 0, 0 });
  113. GameScript.endPortal[3] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(248f - 4 * 3, -6.118f, 0f), Quaternion.identity, 0);
  114. GameScript.endPortalUA[3] = GameScript.endPortal[3].transform.GetChild(0).gameObject;
  115. GameScript.endPortal[3].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  116. GameScript.endPortalUA[3].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { 98, 0, 3 });
  117. }
  118. public static void PopulateWorld(Chunk chunk)
  119. {
  120. var networkStuff = new GameObject[400];
  121. networkStuffField.SetValue(chunk, networkStuff);
  122. int temp = (int)tempField.GetValue(chunk);
  123. int[][] genSpots = new int[W][];
  124. for (int i = 0; i < W; i++)
  125. genSpots[i] = new int[H];
  126. {
  127. int x = 15;
  128. int y = 10;
  129. for (int xOff = -2; xOff < 3; xOff++)
  130. for (int yOff = -2; yOff < 6; yOff++)
  131. MarkGeneration(x + xOff, y + yOff, genSpots);
  132. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  133. temp++;
  134. }
  135. {
  136. int porterGenerated = 0;
  137. int overflow = 0;
  138. int x = random.Next(W / 2) + W / 2;
  139. int y = random.Next(H / 2) + H / 2;
  140. bool addLoopPortal = false;
  141. foreach (var gadget in GadgetCore.API.Gadgets.ListAllEnabledGadgets())
  142. if (gadget.Info?.ModName == "Loop Portal")
  143. {
  144. addLoopPortal = true;
  145. break;
  146. }
  147. while (overflow < 1000 && porterGenerated < 1)
  148. {
  149. overflow++;
  150. bool noGen = false;
  151. for (int xOff = -1; xOff < 2; xOff++)
  152. for (int yOff = -1; yOff < 4; yOff++)
  153. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  154. noGen = true;
  155. if (!noGen)
  156. {
  157. for (int xOff = -1; xOff < 2; xOff++)
  158. for (int yOff = -1; yOff < 4; yOff++)
  159. MarkGeneration(x + xOff, y + yOff, genSpots);
  160. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  161. temp++;
  162. GameScript.endPortal[porterGenerated] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.451f + 1, 0f), Quaternion.identity, 0);
  163. GameScript.endPortalUA[porterGenerated] = GameScript.endPortal[porterGenerated].transform.GetChild(0).gameObject;
  164. GameScript.endPortal[porterGenerated].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  165. GameScript.endPortalUA[porterGenerated].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { SpawnerScript.curBiome, 0, porterGenerated });
  166. porterGenerated++;
  167. }
  168. x = Mathf.Min(x + random.Next(5) - 2, W);
  169. y = Mathf.Min(y + random.Next(5) - 2, H);
  170. }
  171. //while (addLoopPortal && overflow < 5000)
  172. //{
  173. // overflow++;
  174. // bool noGen = false;
  175. // for (int xOff = -1; xOff < 2; xOff++)
  176. // for (int yOff = -1; yOff < 4; yOff++)
  177. // if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  178. // noGen = true;
  179. // if (!noGen)
  180. // {
  181. // for (int xOff = -1; xOff < 2; xOff++)
  182. // for (int yOff = -1; yOff < 4; yOff++)
  183. // MarkGeneration(x + xOff, y + yOff, genSpots);
  184. // networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  185. // temp++;
  186. // var i = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(242 + x * 4 - 60, y * 4 - 60 + GetSizeForPixels(54) + 1.451f + 1, 0f), Quaternion.identity, 0);
  187. // var iUA = i.transform.GetChild(0).gameObject;
  188. // i.GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
  189. // iUA.GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { SpawnerScript.curBiome, 0, 4 });
  190. // i.transform.parent = GameScript.endPortal[0].transform;
  191. // networkStuff[temp] = i;
  192. // temp++;
  193. // break;
  194. // }
  195. // x = Mathf.Min(x + random.Next(5) - 2, W);
  196. // y = Mathf.Min(y + random.Next(5) - 2, H);
  197. //}
  198. }
  199. {
  200. int r = 0;
  201. int overflow = 0;
  202. while (overflow < 15000 && r < 700)
  203. {
  204. overflow++;
  205. int i = random.Next(6);
  206. int x = random.Next(W);
  207. int y = random.Next(H);
  208. bool noGen = false;
  209. switch (i)
  210. {
  211. case 0:
  212. case 1:
  213. case 2:
  214. for (int xOff = -1; xOff < 2; xOff++)
  215. for (int yOff = -1; yOff < 2; yOff++)
  216. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  217. noGen = true;
  218. break;
  219. case 3:
  220. case 4:
  221. for (int xOff = -1; xOff < 2; xOff++)
  222. for (int yOff = -1; yOff < 2; yOff++)
  223. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  224. noGen = true;
  225. break;
  226. case 5:
  227. for (int xOff = -2; xOff < 3; xOff++)
  228. for (int yOff = -2; yOff < 3; yOff++)
  229. if (!CheckGeneration(x + xOff, y + yOff, genSpots))
  230. noGen = true;
  231. break;
  232. }
  233. if (!noGen)
  234. {
  235. r++;
  236. switch (i)
  237. {
  238. case 0:
  239. case 1:
  240. case 2:
  241. for (int xOff = -1; xOff < 2; xOff++)
  242. for (int yOff = -1; yOff < 2; yOff++)
  243. MarkGeneration(x + xOff, y + yOff, genSpots);
  244. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockNormal"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  245. temp++;
  246. var position = new Vector3((float)(242 + x * 4 - 60), y * 4 - 60 + GetSizeForPixels(54) + 1.049f, 0f);
  247. if (random.Next(2) == 0)
  248. {
  249. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/SpaceOre1"), position, Quaternion.identity, 0);
  250. temp++;
  251. }
  252. else
  253. {
  254. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("obj/Subworlds/SpaceOre2"), position, Quaternion.identity, 0);
  255. temp++;
  256. }
  257. break;
  258. case 3:
  259. case 4:
  260. for (int xOff = -1; xOff < 2; xOff++)
  261. for (int yOff = -1; yOff < 2; yOff++)
  262. MarkGeneration(x + xOff, y + yOff, genSpots);
  263. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockSmall"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  264. temp++;
  265. break;
  266. case 5:
  267. for (int xOff = -2; xOff < 3; xOff++)
  268. for (int yOff = -2; yOff < 3; yOff++)
  269. MarkGeneration(x + xOff, y + yOff, genSpots);
  270. networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("z/Subworlds/BaseBlockLarge"), new Vector3(242 + 4 * x - 60, y * 4 - 60, 5), Quaternion.identity, 0);
  271. temp++;
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }