Chunk.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include <Array.h>
  3. #include <Datei.h>
  4. #include <Either.h>
  5. #include <Punkt.h>
  6. #include <Vec3.h>
  7. #include "Block.h"
  8. #include "Constants.h"
  9. #include "DoLaterHandler.h"
  10. #include "InformationObserver.h"
  11. #include "Tickable.h"
  12. class ChunkMap;
  13. class Chunk : public virtual Framework::ReferenceCounter,
  14. public Tickable
  15. {
  16. private:
  17. int dimensionId;
  18. Framework::Punkt location;
  19. Block*** blocks;
  20. Chunk* zNeighbours[4];
  21. unsigned short** blockIds;
  22. Framework::Either<Block*, int> zBlockNeighbor(
  23. Framework::Vec3<int> location, OUT Chunk** zNeighborChunk);
  24. bool added;
  25. Framework::Critical cs;
  26. Framework::RCArray<InformationObserver> observers;
  27. Framework::Array<int> lightSources;
  28. Framework::Array<Block*> tickSourcesEachTick;
  29. Framework::Array<Block*> tickSourcesAfterUpdate;
  30. bool worldUpdated;
  31. unsigned char* lightData;
  32. bool currentlyLoading;
  33. void addLightSource(int z, int index);
  34. void removeLightSource(int z, int index);
  35. void sendToClient(Framework::StreamWriter* zWriter, bool* instanceMap);
  36. void sendLightToClient(Framework::StreamWriter* zWriter, bool* instanceMap);
  37. void broadcastLightData(int z, int index, bool foreground);
  38. const Block* zBlockConst(int z, int index) const;
  39. bool isVisible(int z, int index) const;
  40. public:
  41. Chunk(Framework::Punkt location, int dimensionId);
  42. Chunk(Framework::Punkt location,
  43. int dimensionId,
  44. Framework::StreamReader* zReader);
  45. ~Chunk();
  46. void lock();
  47. void unlock();
  48. void tick(TickQueue* zQueue) override;
  49. void postTick() override;
  50. void notifyObservers(NetworkMessage* msg);
  51. void addObserver(Entity* zEntity, DoLaterHandler& laterHandler);
  52. void removeObserver(Entity* zEntity);
  53. void api(Framework::StreamReader* zRequest,
  54. Entity* zSource,
  55. DoLaterHandler& laterHandler);
  56. void initializeLightning();
  57. void updateLightSources();
  58. Framework::Either<Block*, int> zBlockAt(
  59. Framework::Vec3<int> cLocation) const;
  60. const Block* zBlockConst(Framework::Vec3<int> location) const;
  61. void instantiateBlock(Framework::Vec3<int> location);
  62. void generateBlock(Framework::Vec3<int> location);
  63. void putBlockAt(Framework::Vec3<int> location, Block* block);
  64. void putBlockTypeAt(Framework::Vec3<int> location, int type);
  65. void sendBlockInfo(Framework::Vec3<int> location);
  66. void setNeighbor(Direction dir, Chunk* zChunk);
  67. Chunk* zNeighbor(Direction dir) const;
  68. void load(Framework::StreamReader* zReader);
  69. void save(Framework::StreamWriter* zWriter);
  70. void removeUnusedBlocks();
  71. int getDimensionId() const;
  72. void onLoaded();
  73. void onUnloaded();
  74. Framework::Punkt getCenter() const;
  75. Framework::Vec3<int> getMin() const;
  76. Framework::Vec3<int> getMax() const;
  77. void prepareRemove();
  78. void setAdded();
  79. bool hasObservers() const;
  80. unsigned char* getLightData(Framework::Vec3<int> location) const;
  81. void setLightData(
  82. Framework::Vec3<int> location, unsigned char* data, bool foreground);
  83. int getBlockTypeAt(Framework::Vec3<int> location) const;
  84. int getBlockTypeAtWC(int x, int y, int z) const;
  85. void onEntityEnters(Entity* zEntity, Chunk* lastChunk);
  86. void onEntityLeaves(Entity* zEntity, Chunk* zNextChunk);
  87. bool hasObserver(int entityId) const;
  88. inline static int index(int x, int y)
  89. {
  90. return x * CHUNK_SIZE + y;
  91. }
  92. friend ChunkMap;
  93. };