/Scripts/InfiniteWorld.cs

https://bitbucket.org/Tsarpf/yacmbcp · C# · 379 lines · 287 code · 59 blank · 33 comment · 61 complexity · ba65f7faefe4d42490cd1f627a1d3734 MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. public static class InfiniteWorld
  6. {
  7. static int3 chunkSize = new int3
  8. ((int)WorldGameObject.chunkSize.x,
  9. (int)WorldGameObject.chunkSize.y,
  10. (int)WorldGameObject.chunkSize.z);
  11. static int3 worldChunkSize = new int3
  12. ((int)WorldGameObject.worldChunkSize.x,
  13. (int)WorldGameObject.worldChunkSize.y,
  14. (int)WorldGameObject.worldChunkSize.z);
  15. public static Vector3 playerPosLast;
  16. public static Vector3 playerPosNew;
  17. public static int3 ChunkArrayOffset = new int3(0, 0, 0);
  18. public static GameObject player;
  19. public static GUIText guiTextTarget;
  20. //public static GUIText guiText2;
  21. static public Vector3 getPlayerChunkPos()
  22. {
  23. Vector3 playerPos = player.transform.position;
  24. int x = Mathf.CeilToInt(playerPos.x) - 1;
  25. int y = Mathf.CeilToInt(playerPos.y) - 1;
  26. int z = Mathf.CeilToInt(playerPos.z) - 1;
  27. x = x / chunkSize.x;
  28. y = y / chunkSize.y;
  29. z = z / chunkSize.z;
  30. //int3 offset = ChunkRowUtils.ChunkArrayOffset;
  31. return new Vector3(x, y, z);
  32. }
  33. static private Vector3 getPlayerBlockPos()
  34. {
  35. int x = Mathf.CeilToInt(player.transform.position.x) - 1;
  36. int y = Mathf.CeilToInt(player.transform.position.y) - 1;
  37. int z = Mathf.CeilToInt(player.transform.position.z) - 1;
  38. Vector3 blockCoords = new Vector3();
  39. blockCoords.x = x % chunkSize.x;
  40. blockCoords.y = y % chunkSize.y;
  41. blockCoords.z = z % chunkSize.z;
  42. return blockCoords;
  43. }
  44. public static void playerPositionChunkwiseChecking()
  45. {
  46. playerPosNew = getPlayerChunkPos();
  47. if (playerPosNew != playerPosLast)
  48. {
  49. //We have moved to a new chunk compared to last frame
  50. int x = (int)(playerPosNew.x - playerPosLast.x);
  51. int y = (int)(playerPosNew.y - playerPosLast.y);
  52. int z = (int)(playerPosNew.z - playerPosLast.z);
  53. //Debug.Log( x + " " + y + " " + z);
  54. InfiniteWorld.AddToDirQueue(new int3(x, y, z));
  55. ThreadPool.QueueUserWorkItem(new WaitCallback(shiftAndReplace));
  56. //Thread shiftThread = new Thread(new ThreadStart(shiftAndReplace));
  57. //shiftThread.Start();
  58. }
  59. playerPosLast = playerPosNew;
  60. }
  61. public static void shiftAndReplace(System.Object stateInfo)
  62. {
  63. //ThreadPool.SetMaxThreads(8, 8);
  64. ThreadPool.SetMinThreads(4, 4);
  65. int3 dir = GetFromDirQueue();
  66. Chunk[, ,] chunks = WorldGameObject.chunks;
  67. lock (WorldGameObject.chunks)
  68. {
  69. xdirFunction(dir.x, ChunkArrayOffset, chunks);
  70. ydirFunction(dir.y, ChunkArrayOffset, chunks);
  71. zdirFunction(dir.z, ChunkArrayOffset, chunks);
  72. }
  73. //foreach (Thread thread in threadList)
  74. //{
  75. // thread.Join();
  76. //}
  77. //threadList.Clear();
  78. //ThreadPool.SetMinThreads(0, 0);
  79. }
  80. private static void xdirFunction(int xdir, int3 offset, Chunk[, ,] chunks)
  81. {
  82. if (xdir == 1)
  83. {
  84. ChunkArrayOffset.x += 1;
  85. for (int x = 0; x < worldChunkSize.x; x++)
  86. {
  87. for (int y = 0; y < worldChunkSize.y; y++)
  88. {
  89. for (int z = 0; z < worldChunkSize.z; z++)
  90. {
  91. if (x == 0)
  92. {
  93. AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
  94. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x + 1, y, z].addChunkDrawData));
  95. }
  96. if (x == worldChunkSize.x - 1)
  97. {
  98. int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
  99. WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
  100. ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
  101. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x - 1, y, z].addChunkDrawData));
  102. }
  103. else
  104. {
  105. chunks[x, y, z] = chunks[x + 1, y, z];
  106. }
  107. /*
  108. //newstuff after this
  109. if (x == worldChunkSize.x - 1)
  110. {
  111. }
  112. */
  113. }
  114. }
  115. }
  116. }
  117. else if (xdir == -1)
  118. {
  119. ChunkArrayOffset.x -= 1;
  120. for (int x = worldChunkSize.x - 1; x >= 0; x--)
  121. {
  122. for (int y = worldChunkSize.y - 1; y >= 0; y--)
  123. {
  124. for (int z = worldChunkSize.z - 1; z >= 0; z--)
  125. {
  126. if (x == (worldChunkSize.x - 1))
  127. {
  128. AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
  129. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x - 1, y, z].addChunkDrawData));
  130. }
  131. if (x == 0)
  132. {
  133. int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
  134. WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
  135. ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
  136. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x + 1, y, z].addChunkDrawData));
  137. }
  138. else
  139. {
  140. chunks[x, y, z] = chunks[x - 1, y, z];
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. private static void ydirFunction(int ydir, int3 offset, Chunk[, ,] chunks)
  148. {
  149. if (ydir == 1)
  150. {
  151. ChunkArrayOffset.y += 1;
  152. for (int x = 0; x < worldChunkSize.x; x++)
  153. {
  154. for (int y = 0; y < worldChunkSize.y; y++)
  155. {
  156. for (int z = 0; z < worldChunkSize.z; z++)
  157. {
  158. if (y == 0)
  159. {
  160. AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
  161. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y + 1, z].addChunkDrawData));
  162. }
  163. if (y == worldChunkSize.y - 1)
  164. {
  165. int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
  166. WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
  167. ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
  168. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y - 1, z].addChunkDrawData));
  169. }
  170. else
  171. {
  172. chunks[x, y, z] = chunks[x, y + 1, z];
  173. }
  174. }
  175. }
  176. }
  177. }
  178. else if (ydir == -1)
  179. {
  180. ChunkArrayOffset.y -= 1;
  181. for (int x = worldChunkSize.x - 1; x >= 0; x--)
  182. {
  183. for (int y = worldChunkSize.y - 1; y >= 0; y--)
  184. {
  185. for (int z = worldChunkSize.z - 1; z >= 0; z--)
  186. {
  187. if (y == (worldChunkSize.y - 1))
  188. {
  189. AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
  190. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y - 1, z].addChunkDrawData));
  191. }
  192. if (y == 0)
  193. {
  194. int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
  195. WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
  196. ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
  197. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y + 1, z].addChunkDrawData));
  198. }
  199. else
  200. {
  201. chunks[x, y, z] = chunks[x, y - 1, z];
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. private static void zdirFunction(int zdir, int3 offset, Chunk[, ,] chunks)
  209. {
  210. if (zdir == 1)
  211. {
  212. ChunkArrayOffset.z += 1;
  213. for (int x = 0; x < worldChunkSize.x; x++)
  214. {
  215. for (int y = 0; y < worldChunkSize.y; y++)
  216. {
  217. for (int z = 0; z < worldChunkSize.z; z++)
  218. {
  219. if (z == 0)
  220. {
  221. AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
  222. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z + 1].addChunkDrawData));
  223. }
  224. if (z == worldChunkSize.z - 1)
  225. {
  226. int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
  227. WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
  228. ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
  229. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z - 1].addChunkDrawData));
  230. }
  231. else
  232. {
  233. chunks[x, y, z] = chunks[x, y, z + 1];
  234. }
  235. }
  236. }
  237. }
  238. }
  239. else if (zdir == -1)
  240. {
  241. ChunkArrayOffset.z -= 1;
  242. for (int x = worldChunkSize.x - 1; x >= 0; x--)
  243. {
  244. for (int y = worldChunkSize.y - 1; y >= 0; y--)
  245. {
  246. for (int z = worldChunkSize.z - 1; z >= 0; z--)
  247. {
  248. if (z == (worldChunkSize.z - 1))
  249. {
  250. AddToRemoveQueue(WorldGameObject.chunks[x, y, z]);
  251. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z - 1].addChunkDrawData));
  252. }
  253. if (z == 0)
  254. {
  255. int3 pos = new int3(x + ChunkArrayOffset.x, y + ChunkArrayOffset.y, z + ChunkArrayOffset.z);
  256. WorldGameObject.chunks[x, y, z] = new Chunk(pos, offset);
  257. ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z].addChunkDrawData));
  258. //ThreadPool.QueueUserWorkItem(new WaitCallback(WorldGameObject.chunks[x, y, z + 1].addChunkDrawData));
  259. }
  260. else
  261. {
  262. chunks[x, y, z] = chunks[x, y, z - 1];
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. public static Queue qRemoveChunk = new Queue(32);
  270. public static void AddToRemoveQueue(Chunk chunk)
  271. {
  272. lock (qRemoveChunk)
  273. {
  274. qRemoveChunk.Enqueue(chunk);
  275. }
  276. }
  277. public static Chunk GetFromRemoveQueue()
  278. {
  279. Chunk chunk;
  280. lock (qRemoveChunk)
  281. {
  282. if (qRemoveChunk.Count > 0)
  283. {
  284. chunk = (Chunk)qRemoveChunk.Dequeue();
  285. }
  286. else
  287. {
  288. chunk = null;
  289. }
  290. }
  291. return chunk;
  292. }
  293. public static Queue qShiftDirection = new Queue((int)WorldGameObject.worldChunkSize.x * (int)worldChunkSize.z + 10);
  294. public static void AddToDirQueue(int3 dir)
  295. {
  296. lock (qShiftDirection)
  297. {
  298. qShiftDirection.Enqueue(dir);
  299. }
  300. }
  301. public static int3 GetFromDirQueue()
  302. {
  303. int3 dir;
  304. lock (qShiftDirection)
  305. {
  306. if (qShiftDirection.Count > 0)
  307. {
  308. dir = (int3)qShiftDirection.Dequeue();
  309. }
  310. else
  311. {
  312. Debug.Log("GetFromDirQueue was null");
  313. dir = new int3(0, 0, 0);
  314. }
  315. }
  316. return dir;
  317. }
  318. }