BlockStorage.h 866 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef BLOCKSTORAGE_H
  2. #define BLOCKSTORAGE_H
  3. #include "common/block/BlockTypes.h"
  4. #include "common/world/BlockMap.h"
  5. template<int S, int H>
  6. class BlockStorage final {
  7. static constexpr int SIZE = 1 << S;
  8. static constexpr int HEIGHT = 1 << H;
  9. static constexpr int SIZE_MASK = SIZE - 1;
  10. BlockMap<SIZE * SIZE * HEIGHT, 1> data;
  11. public:
  12. BlockStorage() : data(0) {
  13. }
  14. BlockId get(int x, int y, int z) const {
  15. if(y < 0 || y >= HEIGHT) {
  16. return 0;
  17. }
  18. x &= SIZE_MASK;
  19. z &= SIZE_MASK;
  20. return data.get(x * SIZE * SIZE + y * SIZE + z);
  21. }
  22. void set(int x, int y, int z, BlockId id) {
  23. data.set(x * SIZE * SIZE + y * SIZE + z, id);
  24. }
  25. constexpr int getSize() const {
  26. return SIZE;
  27. }
  28. constexpr int getHeight() const {
  29. return HEIGHT;
  30. }
  31. };
  32. #endif