nodemetadata.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Minetest
  3. Copyright (C) 2010-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 <unordered_set>
  18. #include "metadata.h"
  19. /*
  20. NodeMetadata stores arbitary amounts of data for special blocks.
  21. Used for furnaces, chests and signs.
  22. There are two interaction methods: inventory menu and text input.
  23. Only one can be used for a single metadata, thus only inventory OR
  24. text input should exist in a metadata.
  25. */
  26. class Inventory;
  27. class IItemDefManager;
  28. class NodeMetadata : public SimpleMetadata
  29. {
  30. public:
  31. NodeMetadata(IItemDefManager *item_def_mgr);
  32. ~NodeMetadata();
  33. void serialize(std::ostream &os, u8 version, bool disk=true) const;
  34. void deSerialize(std::istream &is, u8 version);
  35. void clear();
  36. bool empty() const;
  37. // The inventory
  38. Inventory *getInventory()
  39. {
  40. return m_inventory;
  41. }
  42. inline bool isPrivate(const std::string &name) const
  43. {
  44. return m_privatevars.count(name) != 0;
  45. }
  46. void markPrivate(const std::string &name, bool set);
  47. private:
  48. int countNonPrivate() const;
  49. Inventory *m_inventory;
  50. std::unordered_set<std::string> m_privatevars;
  51. };
  52. /*
  53. List of metadata of all the nodes of a block
  54. */
  55. typedef std::map<v3s16, NodeMetadata *> NodeMetadataMap;
  56. class NodeMetadataList
  57. {
  58. public:
  59. NodeMetadataList(bool is_metadata_owner = true) :
  60. m_is_metadata_owner(is_metadata_owner)
  61. {}
  62. ~NodeMetadataList();
  63. void serialize(std::ostream &os, u8 blockver, bool disk = true,
  64. bool absolute_pos = false, bool include_empty = false) const;
  65. void deSerialize(std::istream &is, IItemDefManager *item_def_mgr,
  66. bool absolute_pos = false);
  67. // Add all keys in this list to the vector keys
  68. std::vector<v3s16> getAllKeys();
  69. // Get pointer to data
  70. NodeMetadata *get(v3s16 p);
  71. // Deletes data
  72. void remove(v3s16 p);
  73. // Deletes old data and sets a new one
  74. void set(v3s16 p, NodeMetadata *d);
  75. // Deletes all
  76. void clear();
  77. size_t size() const { return m_data.size(); }
  78. NodeMetadataMap::const_iterator begin()
  79. {
  80. return m_data.begin();
  81. }
  82. NodeMetadataMap::const_iterator end()
  83. {
  84. return m_data.end();
  85. }
  86. private:
  87. int countNonEmpty() const;
  88. bool m_is_metadata_owner;
  89. NodeMetadataMap m_data;
  90. };