BlockMap.cpp 736 B

1234567891011121314151617181920212223242526272829
  1. #include "common/world/BlockMap.h"
  2. static constexpr BlockId EMPTY_BLOCK = 65535;
  3. BlockMap::BlockMap(int length, BlockId id) : blocks(length, 1), map(2, EMPTY_BLOCK) {
  4. map[0] = id;
  5. }
  6. BlockId BlockMap::get(int index) const {
  7. return map[blocks.get(index)];
  8. }
  9. void BlockMap::set(int index, BlockId id) {
  10. for(int i = 0; i < map.getLength(); i++) {
  11. if(map[i] == id) {
  12. blocks.set(index, i);
  13. return;
  14. } else if(map[i] == EMPTY_BLOCK) {
  15. map[i] = id;
  16. blocks.set(index, i);
  17. return;
  18. }
  19. }
  20. blocks.resize(blocks.getBits() + 1);
  21. int i = map.getLength();
  22. map.grow(map.getLength(), EMPTY_BLOCK);
  23. map[i] = id;
  24. blocks.set(index, i);
  25. }