BlockStorage.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "common/world/BlockStorage.h"
  2. BlockStorage::BlockStorage(int sizeBits, int heightBits)
  3. : size(1 << sizeBits), height(1 << heightBits), sizeMask(size - 1) {
  4. maps.resize((size * size * height) / SEGMENT);
  5. }
  6. BlockId BlockStorage::get(int x, int y, int z) const {
  7. if(y < 0 || y >= height) {
  8. return y < 0;
  9. }
  10. x &= (size - 1);
  11. z &= (size - 1);
  12. int index = y * size * size + x * size + z;
  13. int segmentIndex = index >> SEGMENT_BITS;
  14. if(maps[segmentIndex] == nullptr) {
  15. return 0;
  16. }
  17. return maps[segmentIndex]->get(index & SEGMENT_MASK);
  18. }
  19. void BlockStorage::set(int x, int y, int z, BlockId id) {
  20. if(y < 0 || y >= height) {
  21. return;
  22. }
  23. x &= (size - 1);
  24. z &= (size - 1);
  25. int index = y * size * size + x * size + z;
  26. int segmentIndex = index >> SEGMENT_BITS;
  27. if(maps[segmentIndex] == nullptr) {
  28. if(id == 0) {
  29. return;
  30. }
  31. maps[segmentIndex] = new BlockMap(SEGMENT, 0);
  32. }
  33. maps[segmentIndex]->set(index & SEGMENT_MASK, id);
  34. }
  35. int BlockStorage::getSize() const {
  36. return size;
  37. }
  38. int BlockStorage::getHeight() const {
  39. return height;
  40. }