utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[oweals/minetest.git] / src / mapblock_nodemod.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 MAPBLOCK_NODEMOD_HEADER
21 #define MAPBLOCK_NODEMOD_HEADER
22
23 enum NodeModType
24 {
25         NODEMOD_NONE,
26         NODEMOD_CHANGECONTENT, //param is content id
27         NODEMOD_CRACK // param is crack progression
28 };
29
30 struct NodeMod
31 {
32         NodeMod(enum NodeModType a_type=NODEMOD_NONE, u16 a_param=0)
33         {
34                 type = a_type;
35                 param = a_param;
36         }
37         bool operator==(const NodeMod &other)
38         {
39                 return (type == other.type && param == other.param);
40         }
41         enum NodeModType type;
42         u16 param;
43 };
44
45 class NodeModMap
46 {
47 public:
48         /*
49                 returns true if the mod was different last time
50         */
51         bool set(v3s16 p, const NodeMod &mod)
52         {
53                 // See if old is different, cancel if it is not different.
54                 core::map<v3s16, NodeMod>::Node *n = m_mods.find(p);
55                 if(n)
56                 {
57                         NodeMod old = n->getValue();
58                         if(old == mod)
59                                 return false;
60
61                         n->setValue(mod);
62                 }
63                 else
64                 {
65                         m_mods.insert(p, mod);
66                 }
67                 
68                 return true;
69         }
70         // Returns true if there was one
71         bool get(v3s16 p, NodeMod *mod)
72         {
73                 core::map<v3s16, NodeMod>::Node *n;
74                 n = m_mods.find(p);
75                 if(n == NULL)
76                         return false;
77                 if(mod)
78                         *mod = n->getValue();
79                 return true;
80         }
81         bool clear(v3s16 p)
82         {
83                 if(m_mods.find(p))
84                 {
85                         m_mods.remove(p);
86                         return true;
87                 }
88                 return false;
89         }
90         bool clear()
91         {
92                 if(m_mods.size() == 0)
93                         return false;
94                 m_mods.clear();
95                 return true;
96         }
97         void copy(NodeModMap &dest)
98         {
99                 dest.m_mods.clear();
100
101                 for(core::map<v3s16, NodeMod>::Iterator
102                                 i = m_mods.getIterator();
103                                 i.atEnd() == false; i++)
104                 {
105                         dest.m_mods.insert(i.getNode()->getKey(), i.getNode()->getValue());
106                 }
107         }
108
109 private:
110         core::map<v3s16, NodeMod> m_mods;
111 };
112
113 #endif
114