]> git.lizzy.rs Git - minetest.git/blob - src/content_nodemeta.cpp
Implement sign using form field protocol
[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("infotext",
62                                 std::string("\"") + meta->getString("text") + "\"");
63                 meta->setString("formspec","field[text;;${text}]");
64                 return false;
65         }
66         else if(id == NODEMETA_CHEST) // ChestNodeMetadata
67         {
68                 meta->getInventory()->deSerialize(is);
69
70                 // Rename inventory list "0" to "main"
71                 Inventory *inv = meta->getInventory();
72                 if(!inv->getList("main") && inv->getList("0")){
73                         inv->addList("main", 8*4);
74                         *inv->getList("main") = *inv->getList("0");
75                         inv->deleteList("0");
76                 }
77
78                 meta->setString("formspec","invsize[8,9;]"
79                                 "list[current_name;main;0,0;8,4;]"
80                                 "list[current_player;main;0,5;8,4;]");
81                 return false;
82         }
83         else if(id == NODEMETA_LOCKABLE_CHEST) // LockingChestNodeMetadata
84         {
85                 meta->setString("owner", deSerializeString(is));
86                 meta->getInventory()->deSerialize(is);
87
88                 // Rename inventory list "0" to "main"
89                 Inventory *inv = meta->getInventory();
90                 if(!inv->getList("main") && inv->getList("0")){
91                         inv->addList("main", 8*4);
92                         *inv->getList("main") = *inv->getList("0");
93                         inv->deleteList("0");
94                 }
95
96                 meta->setString("formspec","invsize[8,9;]"
97                                 "list[current_name;main;0,0;8,4;]"
98                                 "list[current_player;main;0,5;8,4;]");
99                 return false;
100         }
101         else if(id == NODEMETA_FURNACE) // FurnaceNodeMetadata
102         {
103                 meta->getInventory()->deSerialize(is);
104                 int temp = 0;
105                 is>>temp;
106                 meta->setString("fuel_totaltime", ftos((float)temp/10));
107                 temp = 0;
108                 is>>temp;
109                 meta->setString("fuel_time", ftos((float)temp/10));
110                 temp = 0;
111                 is>>temp;
112                 //meta->setString("src_totaltime", ftos((float)temp/10));
113                 temp = 0;
114                 is>>temp;
115                 meta->setString("src_time", ftos((float)temp/10));
116
117                 meta->setString("formspec","invsize[8,9;]"
118                         "list[current_name;fuel;2,3;1,1;]"
119                         "list[current_name;src;2,1;1,1;]"
120                         "list[current_name;dst;5,1;2,2;]"
121                         "list[current_player;main;0,5;8,4;]");
122                 return true;
123         }
124         else
125         {
126                 throw SerializationError("Unknown legacy node metadata");
127         }
128 }
129
130 static bool content_nodemeta_deserialize_legacy_meta(
131                 std::istream &is, NodeMetadata *meta)
132 {
133         // Read id
134         s16 id = readS16(is);
135
136         // Read data
137         std::string data = deSerializeString(is);
138         std::istringstream tmp_is(data, std::ios::binary);
139         return content_nodemeta_deserialize_legacy_body(tmp_is, id, meta);
140 }
141
142 void content_nodemeta_deserialize_legacy(std::istream &is,
143                 NodeMetadataList *meta, NodeTimerList *timers,
144                 IGameDef *gamedef)
145 {
146         meta->clear();
147         timers->clear();
148
149         u16 version = readU16(is);
150
151         if(version > 1)
152         {
153                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
154                                 <<std::endl;
155                 throw SerializationError(__FUNCTION_NAME);
156         }
157
158         u16 count = readU16(is);
159
160         for(u16 i=0; i<count; i++)
161         {
162                 u16 p16 = readU16(is);
163
164                 v3s16 p(0,0,0);
165                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
166                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
167                 p.Y += p16 / MAP_BLOCKSIZE;
168                 p16 -= p.Y * MAP_BLOCKSIZE;
169                 p.X += p16;
170
171                 if(meta->get(p) != NULL)
172                 {
173                         infostream<<"WARNING: "<<__FUNCTION_NAME<<": "
174                                         <<"already set data at position"
175                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
176                                         <<std::endl;
177                         continue;
178                 }
179
180                 NodeMetadata *data = new NodeMetadata(gamedef);
181                 bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data);
182                 meta->set(p, data);
183
184                 if(need_timer)
185                         timers->set(p, NodeTimer(1., 0.));
186         }
187 }
188
189 void content_nodemeta_serialize_legacy(std::ostream &os, NodeMetadataList *meta)
190 {
191         // Sorry, I was too lazy to implement this. --kahrl
192         writeU16(os, 1); // version
193         writeU16(os, 0); // count
194 }
195
196 // END