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