chunkSystem.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #define GLM_ENABLE_EXPERIMENTAL
  3. #include <glm/gtx/hash.hpp>
  4. #include "blocks.h"
  5. #include <unordered_map>
  6. #include <glm/vec3.hpp>
  7. #include <glm/vec2.hpp>
  8. #include <optional>
  9. #include "multyPlayer/undoQueue.h"
  10. #include "chunk.h"
  11. #include <rendering/bigGpuBuffer.h>
  12. #include <gameplay/blocks/blocksWithData.h>
  13. #include <multyPlayer/splitUpdatesLogic.h>
  14. #include <vector>
  15. struct LightSystem;
  16. void bakeWorkerThread(int index, ThreadPool &threadPool);
  17. struct ChunkSystem
  18. {
  19. BigGpuBuffer gpuBuffer;
  20. struct ChunkSystemSettings
  21. {
  22. //max number of chunks the client can be waiting for at a time.
  23. //note that this number is not a hard limit, if the player moves or teleports it will always be able to request
  24. //more chunks, this is a hard limit only when the player s
  25. int maxWaitingSubmisions = 15;
  26. }chunkSystemSettings;
  27. bool shouldUpdateLights = 0;
  28. std::vector<Chunk*> loadedChunks;
  29. int squareSize = 4;
  30. std::vector<glm::ivec2> chunksToAddLight;
  31. glm::ivec2 lastPlayerPos = {};
  32. Block *getBlockAndData(glm::ivec3 blockPos, std::vector<unsigned char> &data, Chunk *& chunk);
  33. //[0 -> squareSize)
  34. Chunk *getChunksInMatrixSpaceUnsafe(int x, int z);
  35. Chunk *getChunkSafeFromMatrixSpace(int x, int z);
  36. Chunk *getChunkSafeFromBlockPos(int x, int z);
  37. //this is the chunk pos, so not in matrix space
  38. Chunk *getChunkSafeFromChunkPos(int x, int z);
  39. glm::ivec2 fromBlockPosToMatrixSpace(int x, int z);
  40. glm::ivec2 fromMatrixSpaceToChunkSpace(int x, int z);
  41. void setChunkAndNeighboursFlagDirtyFromBlockPos(int x, int z);
  42. void init(int squareDistance);
  43. void changeRenderDistance(int squareDistance, bool notifyServer);
  44. void cleanup(bool notifyServer);
  45. void update(glm::ivec3 playerBlockPosition, float deltaTime, UndoQueue &undoQueue, LightSystem &lightSystem,
  46. InteractionData &interaction, ThreadPool &threadPool, Renderer &renderer, ClientEntityManager &clientEntityManager);
  47. bool isChunkInRadius(glm::ivec2 playerPos, glm::ivec2 chunkPos);
  48. bool isChunkInRadiusAndBounds(glm::ivec2 playerPos, glm::ivec2 chunkPos);
  49. bool shouldRecieveEntity(glm::dvec3 entityPos);
  50. int lastX = 0, lastZ = 0, created = 0;
  51. glm::ivec3 lastPlayerBlockPosition = {};
  52. glm::ivec2 cornerPos = {};
  53. Block *getBlockSafe(int x, int y, int z);
  54. Block *getBlockSafe(glm::dvec3 pos);
  55. Block* getBlockSafeAndChunk(int x, int y, int z, Chunk* &chunk);
  56. void getBlockSafeWithNeigbhours(int x, int y, int z,
  57. Block *&center, Block *&front, Block *&back, Block *&top, Block *&bottom,
  58. Block *&left, Block *&right);
  59. void getBlockSafeWithNeigbhoursStopIfCenterFails(int x, int y, int z,
  60. Block *&center, Block *&front, Block *&back, Block *&top, Block *&bottom,
  61. Block *&left, Block *&right);
  62. Block *rayCast(glm::dvec3 from, glm::vec3 dir, glm::ivec3 &outPos, float maxDist
  63. , std::optional<glm::ivec3> &prevBlockForPlace, float &outDist);
  64. //a client places a block and sends a task to the server for it to be placed
  65. //returns true if succeeded
  66. bool placeBlockByClient(glm::ivec3 pos, unsigned char inventorySlot,
  67. UndoQueue &undoQueue, glm::dvec3 playerPos,
  68. LightSystem &lightSystem, PlayerInventory &inventory, bool decreaseCounter,
  69. int faceDirection, int topPartForSlabs, bool isOnWall,
  70. ClientEntityManager &clientEntityManager
  71. );
  72. //used by the client to place blocks in creative mode using copy paste stuff
  73. bool placeBlockByClientForce(glm::ivec3 pos, Block block,
  74. UndoQueue &undoQue, LightSystem &lightSystem,
  75. ClientEntityManager &clientEntityManager
  76. );
  77. //a client breaks a block and sends a task to the server for it to be blocked
  78. //returns true if succeeded
  79. bool breakBlockByClient(glm::ivec3 pos,
  80. UndoQueue &undoQueue, glm::dvec3 playerPos,
  81. LightSystem &lightSystem, ClientEntityManager &clientEntityManager
  82. );
  83. void placeBlockByServerAndRemoveFromUndoQueue(glm::ivec3 pos, Block block,
  84. LightSystem &lightSystem,
  85. InteractionData &playerInteraction, UndoQueue &undoQueue,
  86. ClientEntityManager &clientEntityManager,
  87. std::vector<unsigned char> *optionalData = 0);
  88. //just place the block, forcely by server
  89. void placeBlockNoClient(glm::ivec3 pos, Block block,
  90. LightSystem &lightSystem, std::vector<unsigned char> *optionalData,
  91. InteractionData &playerInteraction, ClientEntityManager &clientEntityManager);
  92. //internal use
  93. void changeBlockLightStuff(glm::ivec3 pos, int currentSkyLightLevel, int currentNormalLightLevel,
  94. BlockType oldType,
  95. BlockType newType, LightSystem &lightSystem);
  96. //unloads all loaded chunks
  97. void dropAllChunks(BigGpuBuffer *gpuBuffer, bool notifyServer);
  98. //unloads a chunk atn the specified index in the matrix vector,
  99. //ASSUMES THE CHUNK IS LOADED AND HAS GPU DATA
  100. void dropChunkAtIndexUnsafe(int index, BigGpuBuffer *gpuBuffer);
  101. void dropChunkAtIndexSafe(int index, BigGpuBuffer *gpuBuffer);
  102. };
  103. bool isChunkInRadius(glm::ivec2 playerPos, glm::ivec2 chunkPos, int squareSize);