BlockMap.cpp 797 B

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