mapsector.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "irr_v2d.h"
  19. #include "mapblock.h"
  20. #include <ostream>
  21. #include <map>
  22. #include <vector>
  23. class Map;
  24. class IGameDef;
  25. /*
  26. This is an Y-wise stack of MapBlocks.
  27. */
  28. #define MAPSECTOR_SERVER 0
  29. #define MAPSECTOR_CLIENT 1
  30. class MapSector
  31. {
  32. public:
  33. MapSector(Map *parent, v2s16 pos, IGameDef *gamedef);
  34. virtual ~MapSector();
  35. void deleteBlocks();
  36. v2s16 getPos() const
  37. {
  38. return m_pos;
  39. }
  40. MapBlock *getBlockNoCreateNoEx(s16 y);
  41. std::unique_ptr<MapBlock> createBlankBlockNoInsert(s16 y);
  42. MapBlock *createBlankBlock(s16 y);
  43. void insertBlock(std::unique_ptr<MapBlock> block);
  44. void deleteBlock(MapBlock *block);
  45. // Remove a block from the map and the sector without deleting it
  46. // Returns an owning ptr to block.
  47. std::unique_ptr<MapBlock> detachBlock(MapBlock *block);
  48. // This makes a copy of the internal collection.
  49. // Prefer getBlocks() if possible.
  50. void getBlocks(MapBlockVect &dest);
  51. // Get access to the internal collection
  52. // This is explicitly only allowed on a const object since modifying anything while iterating is unsafe.
  53. // The caller needs to make sure that this does not happen.
  54. const auto &getBlocks() const { return m_blocks; }
  55. const auto &getBlocks() = delete;
  56. bool empty() const { return m_blocks.empty(); }
  57. int size() const { return m_blocks.size(); }
  58. protected:
  59. // The pile of MapBlocks
  60. std::unordered_map<s16, std::unique_ptr<MapBlock>> m_blocks;
  61. Map *m_parent;
  62. // Position on parent (in MapBlock widths)
  63. v2s16 m_pos;
  64. IGameDef *m_gamedef;
  65. // Last-used block is cached here for quicker access.
  66. // Be sure to set this to nullptr when the cached block is deleted
  67. MapBlock *m_block_cache = nullptr;
  68. s16 m_block_cache_y;
  69. /*
  70. Private methods
  71. */
  72. MapBlock *getBlockBuffered(s16 y);
  73. };