]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_nodemeta.cpp
Make Lint Happy
[dragonfireclient.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 "nodemetadata.h"
22 #include "nodetimer.h"
23 #include "inventory.h"
24 #include "log.h"
25 #include "serialization.h"
26 #include "util/serialize.h"
27 #include "util/string.h"
28 #include "constants.h" // MAP_BLOCKSIZE
29 #include <sstream>
30
31 #define NODEMETA_GENERIC 1
32 #define NODEMETA_SIGN 14
33 #define NODEMETA_CHEST 15
34 #define NODEMETA_FURNACE 16
35 #define NODEMETA_LOCKABLE_CHEST 17
36
37 // Returns true if node timer must be set
38 static bool content_nodemeta_deserialize_legacy_body(
39                 std::istream &is, s16 id, NodeMetadata *meta)
40 {
41         meta->clear();
42
43         if (id == NODEMETA_GENERIC) // GenericNodeMetadata (0.4-dev)
44         {
45                 meta->getInventory()->deSerialize(is);
46                 deSerializeLongString(is); // m_text
47                 deSerializeString(is);     // m_owner
48
49                 meta->setString("infotext", deSerializeString(is));
50                 meta->setString("formspec", deSerializeString(is));
51                 readU8(is); // m_allow_text_input
52                 readU8(is); // m_allow_removal
53                 readU8(is); // m_enforce_owner
54
55                 int num_vars = readU32(is);
56                 for (int i = 0; i < num_vars; i++) {
57                         std::string name = deSerializeString(is);
58                         std::string var = deSerializeLongString(is);
59                         meta->setString(name, var);
60                 }
61                 return false;
62         } else if (id == NODEMETA_SIGN) // SignNodeMetadata
63         {
64                 meta->setString("text", deSerializeString(is));
65                 // meta->setString("infotext","\"${text}\"");
66                 meta->setString("infotext",
67                                 std::string("\"") + meta->getString("text") + "\"");
68                 meta->setString("formspec", "field[text;;${text}]");
69                 return false;
70         } else if (id == NODEMETA_CHEST) // ChestNodeMetadata
71         {
72                 meta->getInventory()->deSerialize(is);
73
74                 // Rename inventory list "0" to "main"
75                 Inventory *inv = meta->getInventory();
76                 if (!inv->getList("main") && inv->getList("0")) {
77                         inv->getList("0")->setName("main");
78                 }
79                 assert(inv->getList("main") && !inv->getList("0"));
80
81                 meta->setString("formspec", "size[8,9]"
82                                             "list[current_name;main;0,0;8,4;]"
83                                             "list[current_player;main;0,5;8,4;]");
84                 return false;
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         } 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", "size[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         } else {
124                 throw SerializationError("Unknown legacy node metadata");
125         }
126 }
127
128 static bool content_nodemeta_deserialize_legacy_meta(std::istream &is, NodeMetadata *meta)
129 {
130         // Read id
131         s16 id = readS16(is);
132
133         // Read data
134         std::string data = deSerializeString(is);
135         std::istringstream tmp_is(data, std::ios::binary);
136         return content_nodemeta_deserialize_legacy_body(tmp_is, id, meta);
137 }
138
139 void content_nodemeta_deserialize_legacy(std::istream &is, NodeMetadataList *meta,
140                 NodeTimerList *timers, IItemDefManager *item_def_mgr)
141 {
142         meta->clear();
143         timers->clear();
144
145         u16 version = readU16(is);
146
147         if (version > 1) {
148                 infostream << FUNCTION_NAME << ": version " << version << " not supported"
149                            << std::endl;
150                 throw SerializationError(FUNCTION_NAME);
151         }
152
153         u16 count = readU16(is);
154
155         for (u16 i = 0; i < count; i++) {
156                 u16 p16 = readU16(is);
157
158                 v3s16 p(0, 0, 0);
159                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
160                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
161                 p.Y += p16 / MAP_BLOCKSIZE;
162                 p16 -= p.Y * MAP_BLOCKSIZE;
163                 p.X += p16;
164
165                 if (meta->get(p) != NULL) {
166                         warningstream << FUNCTION_NAME << ": "
167                                       << "already set data at position"
168                                       << "(" << p.X << "," << p.Y << "," << p.Z
169                                       << "): Ignoring." << std::endl;
170                         continue;
171                 }
172
173                 NodeMetadata *data = new NodeMetadata(item_def_mgr);
174                 bool need_timer = content_nodemeta_deserialize_legacy_meta(is, data);
175                 meta->set(p, data);
176
177                 if (need_timer)
178                         timers->set(NodeTimer(1., 0., p));
179         }
180 }