changed node metadata format to better accomodate future needs and problems
[oweals/minetest.git] / src / nodemetadata.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 NODEMETADATA_HEADER
21 #define NODEMETADATA_HEADER
22
23 #include "common_irrlicht.h"
24 #include <string>
25 #include <iostream>
26
27 /*
28         Used for storing:
29
30         Oven:
31                 - Item that is being burned
32                 - Burning time
33                 - Item stack that is being heated
34                 - Result item stack
35         
36         Sign:
37                 - Text
38 */
39
40 class Inventory;
41
42 class NodeMetadata
43 {
44 public:
45         typedef NodeMetadata* (*Factory)(std::istream&);
46
47         NodeMetadata();
48         virtual ~NodeMetadata();
49         
50         static NodeMetadata* deSerialize(std::istream &is);
51         void serialize(std::ostream &os);
52         
53         // This usually is the CONTENT_ value
54         virtual u16 typeId() const = 0;
55         virtual NodeMetadata* clone() = 0;
56         virtual void serializeBody(std::ostream &os) = 0;
57         virtual std::string infoText() {return "<todo: remove this text>";}
58         virtual Inventory* getInventory() {return NULL;}
59         // This is called always after the inventory is modified, before
60         // the changes are copied elsewhere
61         virtual void inventoryModified(){}
62
63 protected:
64         static void registerType(u16 id, Factory f);
65 private:
66         static core::map<u16, Factory> m_types;
67 };
68
69 class SignNodeMetadata : public NodeMetadata
70 {
71 public:
72         SignNodeMetadata(std::string text);
73         //~SignNodeMetadata();
74         
75         virtual u16 typeId() const;
76         static NodeMetadata* create(std::istream &is);
77         virtual NodeMetadata* clone();
78         virtual void serializeBody(std::ostream &os);
79         virtual std::string infoText();
80
81         std::string getText(){ return m_text; }
82         void setText(std::string t){ m_text = t; }
83
84 private:
85         std::string m_text;
86 };
87
88 class ChestNodeMetadata : public NodeMetadata
89 {
90 public:
91         ChestNodeMetadata();
92         ~ChestNodeMetadata();
93         
94         virtual u16 typeId() const;
95         static NodeMetadata* create(std::istream &is);
96         virtual NodeMetadata* clone();
97         virtual void serializeBody(std::ostream &os);
98         virtual std::string infoText();
99         virtual Inventory* getInventory() {return m_inventory;}
100
101 private:
102         Inventory *m_inventory;
103 };
104
105 class FurnaceNodeMetadata : public NodeMetadata
106 {
107 public:
108         FurnaceNodeMetadata();
109         ~FurnaceNodeMetadata();
110         
111         virtual u16 typeId() const;
112         virtual NodeMetadata* clone();
113         static NodeMetadata* create(std::istream &is);
114         virtual void serializeBody(std::ostream &os);
115         virtual std::string infoText();
116         virtual Inventory* getInventory() {return m_inventory;}
117         virtual void inventoryModified();
118
119 private:
120         Inventory *m_inventory;
121 };
122
123 /*
124         List of metadata of all the nodes of a block
125 */
126
127 class NodeMetadataList
128 {
129 public:
130         ~NodeMetadataList();
131
132         void serialize(std::ostream &os);
133         void deSerialize(std::istream &is);
134         
135         // Get pointer to data
136         NodeMetadata* get(v3s16 p);
137         // Deletes data
138         void remove(v3s16 p);
139         // Deletes old data and sets a new one
140         void set(v3s16 p, NodeMetadata *d);
141 private:
142         core::map<v3s16, NodeMetadata*> m_data;
143 };
144
145 #endif
146