Dimension.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <InMemoryBuffer.h>
  3. #include <Punkt.h>
  4. #include <Thread.h>
  5. #include <Trie.h>
  6. #include <Either.h>
  7. #include <Critical.h>
  8. #include "Constants.h"
  9. #include "MultiblockStructure.h"
  10. #include "Chunk.h"
  11. class DimensionMap;
  12. class NetworkMessage;
  13. class Block;
  14. class DimensionEnum
  15. {
  16. public:
  17. static const int OVERWORLD = 0;
  18. };
  19. struct RequestQueue
  20. {
  21. Framework::InMemoryBuffer* request;
  22. Framework::Punkt chunkCenter;
  23. int sourceId;
  24. };
  25. class Dimension : public Framework::Thread
  26. {
  27. private:
  28. __int64 nextStructureId;
  29. int dimensionId;
  30. float gravity;
  31. Framework::RCTrie<Chunk>* chunks;
  32. Framework::Array<Chunk*> chunkList;
  33. Framework::Critical chunkCs;
  34. Framework::RCArray<Entity>* entities;
  35. Framework::Array<RequestQueue> waitingRequests;
  36. Framework::Critical cs;
  37. Framework::Array<Framework::Vec3<int>> lightUpdateQueue;
  38. Framework::Array<Framework::Vec3<int>> priorizedLightUpdateQueue;
  39. Framework::Critical lightCs;
  40. Framework::Critical prioLightCs;
  41. Framework::Critical structurCs;
  42. Framework::RCArray<Chunk> removedChunks;
  43. Framework::Critical removedChunksCs;
  44. Framework::RCArray<MultiblockStructure> structures;
  45. DimensionMap* map;
  46. bool stop;
  47. double currentDayTime;
  48. double nightDuration;
  49. double nightTransitionDuration;
  50. double dayDuration;
  51. void getAddrOf(Framework::Punkt cPos, char* addr) const;
  52. void getAddrOfWorld(Framework::Punkt wPos, char* addr) const;
  53. void saveStructure(MultiblockStructure* zStructure) const;
  54. public:
  55. Dimension(int id);
  56. ~Dimension();
  57. void configureDayNightCyncle(double nightDuration,
  58. double nightTransitionDuration,
  59. double dayDuration);
  60. void api(Framework::InMemoryBuffer* zRequest,
  61. NetworkMessage* zResponse,
  62. Entity* zSource);
  63. void tickEntities();
  64. void thread() override;
  65. Framework::Either<Block*, int> zBlock(Framework::Vec3<int> location);
  66. Block* zRealBlockInstance(Framework::Vec3<int> location);
  67. const Block* zBlockOrDefault(Framework::Vec3<int> location);
  68. int getBlockType(Framework::Vec3<int> location);
  69. void placeBlock(
  70. Framework::Vec3<int> location, Framework::Either<Block*, int> block);
  71. void sendBlockInfo(Framework::Vec3<int> location);
  72. void addEntity(Entity* entity);
  73. void setChunk(Chunk* chunk, Framework::Punkt center);
  74. void save(Framework::Text worldDir) const;
  75. int getDimensionId() const;
  76. bool hasChunck(int x, int y);
  77. bool reviveChunk(int x, int y);
  78. Chunk* zChunk(Framework::Punkt wPos) const;
  79. float getGravity() const;
  80. void removeOldChunks();
  81. Entity* zEntity(int id);
  82. Entity* zNearestEntity(
  83. Framework::Vec3<float> pos, std::function<bool(Entity*)> filter);
  84. void removeEntity(int id);
  85. void removeSubscriptions(Entity* zEntity);
  86. void updateLightning(Framework::Vec3<int> location);
  87. void updateLightningWithoutWait(Framework::Vec3<int> location);
  88. void updateLightAtChunkBorders(Framework::Punkt chunkCenter);
  89. __int64 getNextStructureId();
  90. void addStructure(MultiblockStructure* structure);
  91. MultiblockStructure* zStructureByPosition(
  92. Framework::Vec3<int> uniquePosition);
  93. MultiblockStructure* zStructureById(__int64 id);
  94. void requestStopAndWait();
  95. void updateMap(int x, int y, int height);
  96. int getChunkCount();
  97. double getCurrentDayTime() const;
  98. double getNightDuration() const;
  99. double getNightTransitionDuration() const;
  100. double getDayDuration() const;
  101. inline static Framework::Vec3<int> chunkCoordinates(
  102. Framework::Vec3<int> worldLocation)
  103. {
  104. Framework::Vec3<int> result = worldLocation;
  105. result.x = result.x % CHUNK_SIZE;
  106. result.y = result.y % CHUNK_SIZE;
  107. if (result.x < 0) result.x += CHUNK_SIZE;
  108. if (result.y < 0) result.y += CHUNK_SIZE;
  109. return result;
  110. }
  111. friend DimensionMap;
  112. };