ba4a0e5137df529cd81551590cfe08de13d88d4b
[oweals/minetest.git] / src / content_nodemeta.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2012 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 #include "content_nodemeta.h"
21 #include "inventory.h"
22 #include "log.h"
23 #include "utility.h"
24
25 #define NODEMETA_GENERIC 1
26 #define NODEMETA_SIGN 14
27 #define NODEMETA_CHEST 15
28 #define NODEMETA_FURNACE 16
29 #define NODEMETA_LOCKABLE_CHEST 17
30
31 // Returns true if node timer must be set
32 static bool content_nodemeta_deserialize_legacy_body(
33                 std::istream &is, s16 id, NodeMetadata *meta)
34 {
35         meta->clear();
36
37         if(id == NODEMETA_GENERIC) // GenericNodeMetadata (0.4-dev)
38         {
39                 meta->getInventory()->deSerialize(is);
40                 deSerializeLongString(is);  // m_text
41                 deSerializeString(is);  // m_owner
42
43                 meta->setString("infotext",deSerializeString(is));
44                 meta->setString("formspec",deSerializeString(is));
45                 readU8(is);  // m_allow_text_input
46                 readU8(is);  // m_allow_removal
47                 readU8(is);  // m_enforce_owner
48
49                 int num_vars = readU32(is);
50                 for(int i=0; i<num_vars; i++){
51                         std::string name = deSerializeString(is);
52                         std::string var = deSerializeLongString(is);
53                         meta->setString(name, var);
54                 }
55                 return false;
56         }
57         else if(id == NODEMETA_SIGN) // SignNodeMetadata
58         {
59                 meta->setString("text", deSerializeLongString(is));
60                 meta->setString("infotext","\"${text}\"");
61                 meta->setString("formspec","field[text;;${text}]");
62                 return false;
63         }
64         else if(id == NODEMETA_CHEST) // ChestNodeMetadata
65         {
66                 meta->getInventory()->deSerialize(is);
67
68                 // Rename inventory list "0" to "main"
69                 Inventory *inv = meta->getInventory();
70                 if(!inv->getList("main") && inv->getList("0")){
71                         inv->addList("main", 8*4);
72                         *inv->getList("main") = *inv->getList("0");
73                         inv->deleteList("0");
74                 }
75
76                 meta->setString("formspec","invsize[8,9;]"
77                                 "list[current_name;main;0,0;8,4;]"
78                                 "list[current_player;main;0,5;8,4;]");
79                 return false;
80         }
81         else if(id == NODEMETA_LOCKABLE_CHEST) // LockingChestNodeMetadata
82         {
83                 meta->setString("owner", deSerializeString(is));
84                 meta->getInventory()->deSerialize(is);
85
86                 // Rename inventory list "0" to "main"
87                 Inventory *inv = meta->getInventory();
88                 if(!inv->getList("main") && inv->getList("0")){
89                         inv->addList("main", 8*4);
90                         *inv->getList("main") = *inv->getList("0");
91                         inv->deleteList("0");
92                 }
93
94                 meta->setString("formspec","invsize[8,9;]"
95                                 "list[current_name;main;0,0;8,4;]"
96                                 "list[current_player;main;0,5;8,4;]");
97                 return false;
98         }
99         else if(id == NODEMETA_FURNACE) // FurnaceNodeMetadata
100         {
101                 meta->getInventory()->deSerialize(is);
102                 int temp = 0;
103                 is>>temp;
104                 meta->setString("fuel_totaltime", ftos((float)temp/10));
105                 temp = 0;
106                 is>>temp;
107                 meta->setString("fuel_time", ftos((float)temp/10));
108                 temp = 0;
109                 is>>temp;
110                 //meta->setString("src_totaltime", ftos((float)temp/10));
111                 temp = 0;
112                 is>>temp;
113                 meta->setString("src_time", ftos((float)temp/10));
114
115                 meta->setString("formspec","invsize[8,9;]"
116                         "list[current_name;fuel;2,3;1,1;]"
117                         "list[current_name;src;2,1;1,1;]"
118                         "list[current_name;dst;5,1;2,2;]"
119                         "list[current_player;main;0,5;8,4;]");
120                 return true;
121         }
122         else
123         {
124                 throw SerializationError("Unknown legacy node metadata");
125         }
126 }
127
128 static bool content_nodemeta_deserialize_legacy_meta(
129                 std::istream &is, NodeMetadata *meta)
130 {
131         // Read id
132         s16 id = readS16(is);
133
134         // Read data
135         std::string data = deSerializeString(is);
136         std::istringstream tmp_is(data, std::ios::binary);
137         return content_nodemeta_deserialize_legacy_body(tmp_is, id, meta);
138 }
139
140 void content_nodemeta_deserialize_legacy(std::istream &is,
141                 NodeMetadataList *meta, NodeTimerList *timers,
142                 IGameDef *gamedef)
143 {
144         meta->clear();
145         timers->clear();
146
147         u16 version = readU16(is);
148
149         if(version > 1)
150         {
151                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
152                                 <<std::endl;
153                 throw SerializationError(__FUNCTION_NAME);
154         }
155
156         u16 count = readU16(is);
157
158         for(u16 i=0; i<count; i++)
159         {
160                 u16 p16 = readU16(is);
161
162                 v3s16 p(0,0,0);
163                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
164                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
165                 p.Y += p16 / MAP_BLOCKSIZE;
166                 p16 -= p.Y * MAP_BLOCKSIZE;
167                 p.X += p16;
168
169                 if(meta->get(p) != NULL)
170                 {
171                         infostream<<"WARNING: "<<__FUNCTION_NAME<<": "
172                                         <<"already set data at position"
173                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
174                                         <<std::endl;
175                         continue;
176                 }
177
178                 NodeMetadata *data = new NodeMetadata(gamedef);
179                 bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data);
180                 meta->set(p, data);
181
182                 if(need_timer)
183                         timers->set(p, NodeTimer(1., 0.));
184         }
185 }
186
187 void content_nodemeta_serialize_legacy(std::ostream &os, NodeMetadataList *meta)
188 {
189         // Sorry, I was too lazy to implement this. --kahrl
190         writeU16(os, 1); // version
191         writeU16(os, 0); // count
192 }
193
194 // END