Add ObjectRef:punch and ObjectRef:right_click to Lua API
[oweals/minetest.git] / src / craftitemdef.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Kahrl <kahrl@gmx.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "craftitemdef.h"
22 #include "irrlichttypes.h"
23 #include "log.h"
24 #include <sstream>
25 #include "utility.h"
26
27 CraftItemDefinition::CraftItemDefinition():
28         imagename(""),
29         cookresult_item(""),
30         furnace_cooktime(3.0),
31         furnace_burntime(-1.0),
32         usable(false),
33         liquids_pointable(false),
34         dropcount(-1),
35         stack_max(99)
36 {}
37
38 std::string CraftItemDefinition::dump()
39 {
40         std::ostringstream os(std::ios::binary);
41         os<<"imagename="<<imagename;
42         os<<", cookresult_item="<<cookresult_item;
43         os<<", furnace_cooktime="<<furnace_cooktime;
44         os<<", furnace_burntime="<<furnace_burntime;
45         os<<", usable="<<usable;
46         os<<", liquids_pointable="<<liquids_pointable;
47         os<<", dropcount="<<dropcount;
48         os<<", stack_max="<<stack_max;
49         return os.str();
50 }
51
52 void CraftItemDefinition::serialize(std::ostream &os)
53 {
54         writeU8(os, 0); // version
55         os<<serializeString(imagename);
56         os<<serializeString(cookresult_item);
57         writeF1000(os, furnace_cooktime);
58         writeF1000(os, furnace_burntime);
59         writeU8(os, usable);
60         writeU8(os, liquids_pointable);
61         writeS16(os, dropcount);
62         writeS16(os, stack_max);
63 }
64
65 void CraftItemDefinition::deSerialize(std::istream &is)
66 {
67         int version = readU8(is);
68         if(version != 0) throw SerializationError(
69                         "unsupported CraftItemDefinition version");
70         imagename = deSerializeString(is);
71         cookresult_item = deSerializeString(is);
72         furnace_cooktime = readF1000(is);
73         furnace_burntime = readF1000(is);
74         usable = readU8(is);
75         liquids_pointable = readU8(is);
76         dropcount = readS16(is);
77         stack_max = readS16(is);
78 }
79
80 class CCraftItemDefManager: public IWritableCraftItemDefManager
81 {
82 public:
83         virtual ~CCraftItemDefManager()
84         {
85                 clear();
86         }
87         virtual const CraftItemDefinition* getCraftItemDefinition(const std::string &itemname) const
88         {
89                 core::map<std::string, CraftItemDefinition*>::Node *n;
90                 n = m_item_definitions.find(itemname);
91                 if(n == NULL)
92                         return NULL;
93                 return n->getValue();
94         }
95         virtual std::string getImagename(const std::string &itemname) const
96         {
97                 const CraftItemDefinition *def = getCraftItemDefinition(itemname);
98                 if(def == NULL)
99                         return "";
100                 return def->imagename;
101         }
102         virtual bool registerCraftItem(std::string itemname, const CraftItemDefinition &def)
103         {
104                 infostream<<"registerCraftItem: registering CraftItem \""<<itemname<<"\""<<std::endl;
105                 /*core::map<std::string, CraftItemDefinition*>::Node *n;
106                 n = m_item_definitions.find(itemname);
107                 if(n != NULL){
108                         errorstream<<"registerCraftItem: registering item \""<<toolname
109                                         <<"\" failed: name is already registered"<<std::endl;
110                         return false;
111                 }*/
112                 m_item_definitions[itemname] = new CraftItemDefinition(def);
113                 return true;
114         }
115         virtual void clear()
116         {
117                 for(core::map<std::string, CraftItemDefinition*>::Iterator
118                                 i = m_item_definitions.getIterator();
119                                 i.atEnd() == false; i++){
120                         delete i.getNode()->getValue();
121                 }
122                 m_item_definitions.clear();
123         }
124         virtual void serialize(std::ostream &os)
125         {
126                 writeU8(os, 0); // version
127                 u16 count = m_item_definitions.size();
128                 writeU16(os, count);
129                 for(core::map<std::string, CraftItemDefinition*>::Iterator
130                                 i = m_item_definitions.getIterator();
131                                 i.atEnd() == false; i++){
132                         std::string name = i.getNode()->getKey();
133                         CraftItemDefinition *def = i.getNode()->getValue();
134                         // Serialize name
135                         os<<serializeString(name);
136                         // Serialize CraftItemDefinition and write wrapped in a string
137                         std::ostringstream tmp_os(std::ios::binary);
138                         def->serialize(tmp_os);
139                         os<<serializeString(tmp_os.str());
140                 }
141         }
142         virtual void deSerialize(std::istream &is)
143         {
144                 // Clear everything
145                 clear();
146                 // Deserialize
147                 int version = readU8(is);
148                 if(version != 0) throw SerializationError(
149                                 "unsupported CraftItemDefManager version");
150                 u16 count = readU16(is);
151                 for(u16 i=0; i<count; i++){
152                         // Deserialize name
153                         std::string name = deSerializeString(is);
154                         // Deserialize a string and grab a CraftItemDefinition from it
155                         std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
156                         CraftItemDefinition def;
157                         def.deSerialize(tmp_is);
158                         // Register
159                         registerCraftItem(name, def);
160                 }
161         }
162 private:
163         // Key is name
164         core::map<std::string, CraftItemDefinition*> m_item_definitions;
165 };
166
167 IWritableCraftItemDefManager* createCraftItemDefManager()
168 {
169         return new CCraftItemDefManager();
170 }