]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.h
merged an old head into main branch
[dragonfireclient.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 "";}
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         // A step in time. Returns true if metadata changed.
63         virtual bool step(float dtime) {return false;}
64         virtual bool nodeRemovalDisabled(){return false;}
65
66 protected:
67         static void registerType(u16 id, Factory f);
68 private:
69         static core::map<u16, Factory> m_types;
70 };
71
72 class SignNodeMetadata : public NodeMetadata
73 {
74 public:
75         SignNodeMetadata(std::string text);
76         //~SignNodeMetadata();
77         
78         virtual u16 typeId() const;
79         static NodeMetadata* create(std::istream &is);
80         virtual NodeMetadata* clone();
81         virtual void serializeBody(std::ostream &os);
82         virtual std::string infoText();
83
84         std::string getText(){ return m_text; }
85         void setText(std::string t){ m_text = t; }
86
87 private:
88         std::string m_text;
89 };
90
91 class ChestNodeMetadata : public NodeMetadata
92 {
93 public:
94         ChestNodeMetadata();
95         ~ChestNodeMetadata();
96         
97         virtual u16 typeId() const;
98         static NodeMetadata* create(std::istream &is);
99         virtual NodeMetadata* clone();
100         virtual void serializeBody(std::ostream &os);
101         virtual std::string infoText();
102         virtual Inventory* getInventory() {return m_inventory;}
103
104         virtual bool nodeRemovalDisabled();
105         
106 private:
107         Inventory *m_inventory;
108 };
109
110 class FurnaceNodeMetadata : public NodeMetadata
111 {
112 public:
113         FurnaceNodeMetadata();
114         ~FurnaceNodeMetadata();
115         
116         virtual u16 typeId() const;
117         virtual NodeMetadata* clone();
118         static NodeMetadata* create(std::istream &is);
119         virtual void serializeBody(std::ostream &os);
120         virtual std::string infoText();
121         virtual Inventory* getInventory() {return m_inventory;}
122         virtual void inventoryModified();
123         virtual bool step(float dtime);
124
125 private:
126         Inventory *m_inventory;
127         float m_step_accumulator;
128         float m_fuel_totaltime;
129         float m_fuel_time;
130         float m_src_totaltime;
131         float m_src_time;
132 };
133
134 /*
135         List of metadata of all the nodes of a block
136 */
137
138 class InventoryManager;
139
140 class NodeMetadataList
141 {
142 public:
143         ~NodeMetadataList();
144
145         void serialize(std::ostream &os);
146         void deSerialize(std::istream &is);
147         
148         // Get pointer to data
149         NodeMetadata* get(v3s16 p);
150         // Deletes data
151         void remove(v3s16 p);
152         // Deletes old data and sets a new one
153         void set(v3s16 p, NodeMetadata *d);
154         
155         // A step in time. Returns true if something changed.
156         bool step(float dtime);
157
158 private:
159         core::map<v3s16, NodeMetadata*> m_data;
160 };
161
162 #endif
163