Minetest  5.4.0
nodemetadata.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include <unordered_set>
23 #include "metadata.h"
24 
25 /*
26  NodeMetadata stores arbitary amounts of data for special blocks.
27  Used for furnaces, chests and signs.
28 
29  There are two interaction methods: inventory menu and text input.
30  Only one can be used for a single metadata, thus only inventory OR
31  text input should exist in a metadata.
32 */
33 
34 class Inventory;
35 class IItemDefManager;
36 
37 class NodeMetadata : public Metadata
38 {
39 public:
40  NodeMetadata(IItemDefManager *item_def_mgr);
41  ~NodeMetadata();
42 
43  void serialize(std::ostream &os, u8 version, bool disk=true) const;
44  void deSerialize(std::istream &is, u8 version);
45 
46  void clear();
47  bool empty() const;
48 
49  // The inventory
51  {
52  return m_inventory;
53  }
54 
55  inline bool isPrivate(const std::string &name) const
56  {
57  return m_privatevars.count(name) != 0;
58  }
59  void markPrivate(const std::string &name, bool set);
60 
61 private:
62  int countNonPrivate() const;
63 
65  std::unordered_set<std::string> m_privatevars;
66 };
67 
68 
69 /*
70  List of metadata of all the nodes of a block
71 */
72 
73 typedef std::map<v3s16, NodeMetadata *> NodeMetadataMap;
74 
76 {
77 public:
78  NodeMetadataList(bool is_metadata_owner = true) :
79  m_is_metadata_owner(is_metadata_owner)
80  {}
81 
83 
84  void serialize(std::ostream &os, u8 blockver, bool disk = true,
85  bool absolute_pos = false) const;
86  void deSerialize(std::istream &is, IItemDefManager *item_def_mgr,
87  bool absolute_pos = false);
88 
89  // Add all keys in this list to the vector keys
90  std::vector<v3s16> getAllKeys();
91  // Get pointer to data
93  // Deletes data
94  void remove(v3s16 p);
95  // Deletes old data and sets a new one
96  void set(v3s16 p, NodeMetadata *d);
97  // Deletes all
98  void clear();
99 
100  size_t size() const { return m_data.size(); }
101 
102  NodeMetadataMap::const_iterator begin()
103  {
104  return m_data.begin();
105  }
106 
107  NodeMetadataMap::const_iterator end()
108  {
109  return m_data.end();
110  }
111 
112 private:
113  int countNonEmpty() const;
114 
117 };
Definition: itemdef.h:105
Definition: inventory.h:282
Definition: metadata.h:28
Definition: nodemetadata.h:76
std::vector< v3s16 > getAllKeys()
Definition: nodemetadata.cpp:206
void set(v3s16 p, NodeMetadata *d)
Definition: nodemetadata.cpp:235
void deSerialize(std::istream &is, IItemDefManager *item_def_mgr, bool absolute_pos=false)
Definition: nodemetadata.cpp:153
NodeMetadataMap::const_iterator begin()
Definition: nodemetadata.h:102
bool m_is_metadata_owner
Definition: nodemetadata.h:115
NodeMetadataList(bool is_metadata_owner=true)
Definition: nodemetadata.h:78
void serialize(std::ostream &os, u8 blockver, bool disk=true, bool absolute_pos=false) const
Definition: nodemetadata.cpp:115
NodeMetadataMap m_data
Definition: nodemetadata.h:116
NodeMetadata * get(v3s16 p)
Definition: nodemetadata.cpp:217
size_t size() const
Definition: nodemetadata.h:100
~NodeMetadataList()
Definition: nodemetadata.cpp:201
void clear()
Definition: nodemetadata.cpp:241
int countNonEmpty() const
Definition: nodemetadata.cpp:251
NodeMetadataMap::const_iterator end()
Definition: nodemetadata.h:107
void remove(v3s16 p)
Definition: nodemetadata.cpp:225
Definition: nodemetadata.h:38
bool empty() const
Definition: nodemetadata.cpp:85
int countNonPrivate() const
Definition: nodemetadata.cpp:99
void markPrivate(const std::string &name, bool set)
Definition: nodemetadata.cpp:91
void clear()
Definition: nodemetadata.cpp:78
bool isPrivate(const std::string &name) const
Definition: nodemetadata.h:55
Inventory * m_inventory
Definition: nodemetadata.h:64
std::unordered_set< std::string > m_privatevars
Definition: nodemetadata.h:65
void deSerialize(std::istream &is, u8 version)
Definition: nodemetadata.cpp:61
Inventory * getInventory()
Definition: nodemetadata.h:50
NodeMetadata(IItemDefManager *item_def_mgr)
Definition: nodemetadata.cpp:34
void serialize(std::ostream &os, u8 version, bool disk=true) const
Definition: nodemetadata.cpp:43
~NodeMetadata()
Definition: nodemetadata.cpp:38
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
std::map< v3s16, NodeMetadata * > NodeMetadataMap
Definition: nodemetadata.h:73
std::string p(std::string path)
Definition: test_filepath.cpp:59