19ce80a42ca4ec8c2c30c886e6102cadd41cb5af
[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 "irrlichttypes.h"
24 #include <string>
25 #include <iostream>
26
27 /*
28         NodeMetadata stores arbitary amounts of data for special blocks.
29         Used for furnaces, chests and signs.
30
31         There are two interaction methods: inventory menu and text input.
32         Only one can be used for a single metadata, thus only inventory OR
33         text input should exist in a metadata.
34 */
35
36 class Inventory;
37 class IGameDef;
38
39 class NodeMetadata
40 {
41 public:
42         typedef NodeMetadata* (*Factory)(std::istream&, IGameDef *gamedef);
43         typedef NodeMetadata* (*Factory2)(IGameDef *gamedef);
44
45         NodeMetadata(IGameDef *gamedef);
46         virtual ~NodeMetadata();
47         
48         static NodeMetadata* create(const std::string &name, IGameDef *gamedef);
49         static NodeMetadata* deSerialize(std::istream &is, IGameDef *gamedef);
50         void serialize(std::ostream &os);
51         
52         virtual u16 typeId() const = 0;
53         virtual const char* typeName() const = 0;
54         virtual NodeMetadata* clone(IGameDef *gamedef) = 0;
55         virtual void serializeBody(std::ostream &os) = 0;
56
57         // Called on client-side; shown on screen when pointed at
58         virtual std::string infoText() {return "";}
59         
60         //
61         virtual Inventory* getInventory() {return NULL;}
62         // Called always after the inventory is modified, before the changes
63         // are copied elsewhere
64         virtual void inventoryModified(){}
65
66         // A step in time. Shall return true if metadata changed.
67         virtual bool step(float dtime) {return false;}
68
69         // Whether the related node and this metadata cannot be removed
70         virtual bool nodeRemovalDisabled(){return false;}
71         // If non-empty, player can interact by using an inventory view
72         // See format in guiInventoryMenu.cpp.
73         virtual std::string getInventoryDrawSpecString(){return "";}
74
75         // If true, player can interact by writing text
76         virtual bool allowsTextInput(){ return false; }
77         // Get old text for player interaction
78         virtual std::string getText(){ return ""; }
79         // Set player-written text
80         virtual void setText(const std::string &t){}
81
82         // If returns non-empty, only given player can modify text/inventory
83         virtual std::string getOwner(){ return std::string(""); }
84         // The name of the player who placed the node
85         virtual void setOwner(std::string t){}
86
87         /* Interface for GenericNodeMetadata */
88
89         virtual void setInfoText(const std::string &text){};
90         virtual void setInventoryDrawSpec(const std::string &text){};
91         virtual void setAllowTextInput(bool b){};
92
93         virtual void setRemovalDisabled(bool b){};
94         virtual void setEnforceOwner(bool b){};
95
96         virtual bool isInventoryModified(){return false;};
97         virtual void resetInventoryModified(){};
98         virtual bool isTextModified(){return false;};
99         virtual void resetTextModified(){};
100
101         virtual void setString(const std::string &name, const std::string &var){}
102         virtual std::string getString(const std::string &name){return "";}
103
104 protected:
105         static void registerType(u16 id, const std::string &name, Factory f,
106                         Factory2 f2);
107         IGameDef *m_gamedef;
108 private:
109         static core::map<u16, Factory> m_types;
110         static core::map<std::string, Factory2> m_names;
111 };
112
113 /*
114         List of metadata of all the nodes of a block
115 */
116
117 class NodeMetadataList
118 {
119 public:
120         ~NodeMetadataList();
121
122         void serialize(std::ostream &os);
123         void deSerialize(std::istream &is, IGameDef *gamedef);
124         
125         // Get pointer to data
126         NodeMetadata* get(v3s16 p);
127         // Deletes data
128         void remove(v3s16 p);
129         // Deletes old data and sets a new one
130         void set(v3s16 p, NodeMetadata *d);
131         
132         // A step in time. Returns true if something changed.
133         bool step(float dtime);
134
135 private:
136         core::map<v3s16, NodeMetadata*> m_data;
137 };
138
139 #endif
140