Chunk.h 2.9 KB

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