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