Chunk.h 3.0 KB

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