Dimension.h 3.3 KB

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