Chunk.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 index);
  34. void removeLightSource(int index);
  35. void sendLightToClient(Framework::StreamWriter* zWriter);
  36. void broadcastLightData(int index, bool foreground);
  37. const Block* zBlockConst(int index) const;
  38. bool isVisible(int index) const;
  39. public:
  40. Chunk(Framework::Punkt location, int dimensionId);
  41. Chunk(Framework::Punkt location,
  42. int dimensionId,
  43. Framework::StreamReader* zReader);
  44. ~Chunk();
  45. void lock();
  46. void unlock();
  47. void tick(TickQueue* zQueue) override;
  48. void postTick() override;
  49. void notifyObservers(NetworkMessage* msg);
  50. void addObserver(Entity* zEntity, DoLaterHandler& laterHandler);
  51. void removeObserver(Entity* zEntity);
  52. void api(Framework::StreamReader* zRequest,
  53. Entity* zSource,
  54. DoLaterHandler& laterHandler);
  55. void initializeLightning();
  56. Framework::Either<Block*, int> zBlockAt(
  57. Framework::Vec3<int> cLocation) const;
  58. const Block* zBlockConst(Framework::Vec3<int> location) const;
  59. void instantiateBlock(Framework::Vec3<int> location);
  60. void generateBlock(Framework::Vec3<int> location);
  61. void putBlockAt(Framework::Vec3<int> location, Block* block);
  62. void putBlockTypeAt(Framework::Vec3<int> location, int type);
  63. void sendBlockInfo(Framework::Vec3<int> location);
  64. void setNeighbor(Direction dir, Chunk* zChunk);
  65. void load(Framework::StreamReader* zReader);
  66. void save(Framework::StreamWriter* zWriter);
  67. void sendToClient(Framework::StreamWriter* zWriter);
  68. void removeUnusedBlocks();
  69. int getDimensionId() const;
  70. void onLoaded();
  71. void onUnloaded();
  72. Framework::Punkt getCenter() const;
  73. Framework::Vec3<int> getMin() const;
  74. Framework::Vec3<int> getMax() const;
  75. void prepareRemove();
  76. void setAdded();
  77. bool hasObservers() const;
  78. unsigned char* getLightData(Framework::Vec3<int> location) const;
  79. void setLightData(
  80. Framework::Vec3<int> location, unsigned char* data, bool foreground);
  81. int getBlockTypeAt(Framework::Vec3<int> location) const;
  82. inline static int index(Framework::Vec3<int> localLocation)
  83. {
  84. return (localLocation.x * CHUNK_SIZE + localLocation.y) * WORLD_HEIGHT
  85. + localLocation.z;
  86. }
  87. friend ChunkMap;
  88. };