Add core.find_nodes_with_meta() script API
[oweals/minetest.git] / src / nodemetadata.h
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 #ifndef NODEMETADATA_HEADER
21 #define NODEMETADATA_HEADER
22
23 #include "irr_v3d.h"
24 #include <string>
25 #include <iostream>
26 #include <vector>
27 #include <map>
28
29 /*
30         NodeMetadata stores arbitary amounts of data for special blocks.
31         Used for furnaces, chests and signs.
32
33         There are two interaction methods: inventory menu and text input.
34         Only one can be used for a single metadata, thus only inventory OR
35         text input should exist in a metadata.
36 */
37
38 class Inventory;
39 class IGameDef;
40
41 class NodeMetadata
42 {
43 public:
44         NodeMetadata(IGameDef *gamedef);
45         ~NodeMetadata();
46
47         void serialize(std::ostream &os) const;
48         void deSerialize(std::istream &is);
49
50         void clear();
51
52         // Generic key/value store
53         std::string getString(const std::string &name, unsigned short recursion = 0) const;
54         void setString(const std::string &name, const std::string &var);
55         // Support variable names in values
56         std::string resolveString(const std::string &str, unsigned short recursion = 0) const;
57         std::map<std::string, std::string> getStrings() const
58         {
59                 return m_stringvars;
60         }
61
62         // The inventory
63         Inventory* getInventory()
64         {
65                 return m_inventory;
66         }
67
68 private:
69         std::map<std::string, std::string> m_stringvars;
70         Inventory *m_inventory;
71 };
72
73
74 /*
75         List of metadata of all the nodes of a block
76 */
77
78 class NodeMetadataList
79 {
80 public:
81         ~NodeMetadataList();
82
83         void serialize(std::ostream &os) const;
84         void deSerialize(std::istream &is, IGameDef *gamedef);
85
86         // Add all keys in this list to the vector keys
87         std::vector<v3s16> getAllKeys();
88         // Get pointer to data
89         NodeMetadata *get(v3s16 p);
90         // Deletes data
91         void remove(v3s16 p);
92         // Deletes old data and sets a new one
93         void set(v3s16 p, NodeMetadata *d);
94         // Deletes all
95         void clear();
96
97 private:
98         std::map<v3s16, NodeMetadata *> m_data;
99 };
100
101 #endif
102