Fix map deserialization and remove old serialization code
[oweals/minetest.git] / src / content_mapnode.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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_mapnode.h"
21
22 #include "irrlichttypes_bloated.h"
23 #include "mapnode.h"
24 #include "nodedef.h"
25 #include "nameidmapping.h"
26 #include <map>
27
28 /*
29         Legacy node content type IDs
30         Ranges:
31         0x000...0x07f (0...127): param2 is fully usable
32         126 and 127 are reserved (CONTENT_AIR and CONTENT_IGNORE).
33         0x800...0xfff (2048...4095): higher 4 bits of param2 are not usable
34 */
35 #define CONTENT_STONE 0
36 #define CONTENT_WATER 2
37 #define CONTENT_TORCH 3
38 #define CONTENT_WATERSOURCE 9
39 #define CONTENT_SIGN_WALL 14
40 #define CONTENT_CHEST 15
41 #define CONTENT_FURNACE 16
42 #define CONTENT_LOCKABLE_CHEST 17
43 #define CONTENT_FENCE 21
44 #define CONTENT_RAIL 30
45 #define CONTENT_LADDER 31
46 #define CONTENT_LAVA 32
47 #define CONTENT_LAVASOURCE 33
48 #define CONTENT_GRASS 0x800 //1
49 #define CONTENT_TREE 0x801 //4
50 #define CONTENT_LEAVES 0x802 //5
51 #define CONTENT_GRASS_FOOTSTEPS 0x803 //6
52 #define CONTENT_MESE 0x804 //7
53 #define CONTENT_MUD 0x805 //8
54 #define CONTENT_CLOUD 0x806 //10
55 #define CONTENT_COALSTONE 0x807 //11
56 #define CONTENT_WOOD 0x808 //12
57 #define CONTENT_SAND 0x809 //13
58 #define CONTENT_COBBLE 0x80a //18
59 #define CONTENT_STEEL 0x80b //19
60 #define CONTENT_GLASS 0x80c //20
61 #define CONTENT_MOSSYCOBBLE 0x80d //22
62 #define CONTENT_GRAVEL 0x80e //23
63 #define CONTENT_SANDSTONE 0x80f //24
64 #define CONTENT_CACTUS 0x810 //25
65 #define CONTENT_BRICK 0x811 //26
66 #define CONTENT_CLAY 0x812 //27
67 #define CONTENT_PAPYRUS 0x813 //28
68 #define CONTENT_BOOKSHELF 0x814 //29
69 #define CONTENT_JUNGLETREE 0x815
70 #define CONTENT_JUNGLEGRASS 0x816
71 #define CONTENT_NC 0x817
72 #define CONTENT_NC_RB 0x818
73 #define CONTENT_APPLE 0x819
74 #define CONTENT_SAPLING 0x820
75
76 /*
77         A conversion table for backwards compatibility.
78         Maps <=v19 content types to current ones.
79         Should never be touched.
80 */
81 content_t trans_table_19[21][2] = {
82         {CONTENT_GRASS, 1},
83         {CONTENT_TREE, 4},
84         {CONTENT_LEAVES, 5},
85         {CONTENT_GRASS_FOOTSTEPS, 6},
86         {CONTENT_MESE, 7},
87         {CONTENT_MUD, 8},
88         {CONTENT_CLOUD, 10},
89         {CONTENT_COALSTONE, 11},
90         {CONTENT_WOOD, 12},
91         {CONTENT_SAND, 13},
92         {CONTENT_COBBLE, 18},
93         {CONTENT_STEEL, 19},
94         {CONTENT_GLASS, 20},
95         {CONTENT_MOSSYCOBBLE, 22},
96         {CONTENT_GRAVEL, 23},
97         {CONTENT_SANDSTONE, 24},
98         {CONTENT_CACTUS, 25},
99         {CONTENT_BRICK, 26},
100         {CONTENT_CLAY, 27},
101         {CONTENT_PAPYRUS, 28},
102         {CONTENT_BOOKSHELF, 29},
103 };
104
105 MapNode mapnode_translate_to_internal(MapNode n_from, u8 version)
106 {
107         MapNode result = n_from;
108         if(version <= 19)
109         {
110                 content_t c_from = n_from.getContent();
111                 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
112                 {
113                         if(trans_table_19[i][1] == c_from)
114                         {
115                                 result.setContent(trans_table_19[i][0]);
116                                 break;
117                         }
118                 }
119         }
120         return result;
121 }
122
123 void content_mapnode_get_name_id_mapping(NameIdMapping *nimap)
124 {
125         nimap->set(0, "default:stone");
126         nimap->set(2, "default:water_flowing");
127         nimap->set(3, "default:torch");
128         nimap->set(9, "default:water_source");
129         nimap->set(14, "default:sign_wall");
130         nimap->set(15, "default:chest");
131         nimap->set(16, "default:furnace");
132         nimap->set(17, "default:chest_locked");
133         nimap->set(21, "default:fence_wood");
134         nimap->set(30, "default:rail");
135         nimap->set(31, "default:ladder");
136         nimap->set(32, "default:lava_flowing");
137         nimap->set(33, "default:lava_source");
138         nimap->set(0x800, "default:dirt_with_grass");
139         nimap->set(0x801, "default:tree");
140         nimap->set(0x802, "default:leaves");
141         nimap->set(0x803, "default:dirt_with_grass_footsteps");
142         nimap->set(0x804, "default:mese");
143         nimap->set(0x805, "default:dirt");
144         nimap->set(0x806, "default:cloud");
145         nimap->set(0x807, "default:coalstone");
146         nimap->set(0x808, "default:wood");
147         nimap->set(0x809, "default:sand");
148         nimap->set(0x80a, "default:cobble");
149         nimap->set(0x80b, "default:steelblock");
150         nimap->set(0x80c, "default:glass");
151         nimap->set(0x80d, "default:mossycobble");
152         nimap->set(0x80e, "default:gravel");
153         nimap->set(0x80f, "default:sandstone");
154         nimap->set(0x810, "default:cactus");
155         nimap->set(0x811, "default:brick");
156         nimap->set(0x812, "default:clay");
157         nimap->set(0x813, "default:papyrus");
158         nimap->set(0x814, "default:bookshelf");
159         nimap->set(0x815, "default:jungletree");
160         nimap->set(0x816, "default:junglegrass");
161         nimap->set(0x817, "default:nyancat");
162         nimap->set(0x818, "default:nyancat_rainbow");
163         nimap->set(0x819, "default:apple");
164         nimap->set(0x820, "default:sapling");
165         // Static types
166         nimap->set(CONTENT_IGNORE, "ignore");
167         nimap->set(CONTENT_AIR, "air");
168 }
169
170 class NewNameGetter
171 {
172 public:
173         NewNameGetter()
174         {
175                 old_to_new["CONTENT_STONE"] = "default:stone";
176                 old_to_new["CONTENT_WATER"] = "default:water_flowing";
177                 old_to_new["CONTENT_TORCH"] = "default:torch";
178                 old_to_new["CONTENT_WATERSOURCE"] = "default:water_source";
179                 old_to_new["CONTENT_SIGN_WALL"] = "default:sign_wall";
180                 old_to_new["CONTENT_CHEST"] = "default:chest";
181                 old_to_new["CONTENT_FURNACE"] = "default:furnace";
182                 old_to_new["CONTENT_LOCKABLE_CHEST"] = "default:locked_chest";
183                 old_to_new["CONTENT_FENCE"] = "default:wooden_fence";
184                 old_to_new["CONTENT_RAIL"] = "default:rail";
185                 old_to_new["CONTENT_LADDER"] = "default:ladder";
186                 old_to_new["CONTENT_LAVA"] = "default:lava_flowing";
187                 old_to_new["CONTENT_LAVASOURCE"] = "default:lava_source";
188                 old_to_new["CONTENT_GRASS"] = "default:dirt_with_grass";
189                 old_to_new["CONTENT_TREE"] = "default:tree";
190                 old_to_new["CONTENT_LEAVES"] = "default:leaves";
191                 old_to_new["CONTENT_GRASS_FOOTSTEPS"] = "default:dirt_with_grass_footsteps";
192                 old_to_new["CONTENT_MESE"] = "default:mese";
193                 old_to_new["CONTENT_MUD"] = "default:dirt";
194                 old_to_new["CONTENT_CLOUD"] = "default:cloud";
195                 old_to_new["CONTENT_COALSTONE"] = "default:coalstone";
196                 old_to_new["CONTENT_WOOD"] = "default:wood";
197                 old_to_new["CONTENT_SAND"] = "default:sand";
198                 old_to_new["CONTENT_COBBLE"] = "default:cobble";
199                 old_to_new["CONTENT_STEEL"] = "default:steel";
200                 old_to_new["CONTENT_GLASS"] = "default:glass";
201                 old_to_new["CONTENT_MOSSYCOBBLE"] = "default:mossycobble";
202                 old_to_new["CONTENT_GRAVEL"] = "default:gravel";
203                 old_to_new["CONTENT_SANDSTONE"] = "default:sandstone";
204                 old_to_new["CONTENT_CACTUS"] = "default:cactus";
205                 old_to_new["CONTENT_BRICK"] = "default:brick";
206                 old_to_new["CONTENT_CLAY"] = "default:clay";
207                 old_to_new["CONTENT_PAPYRUS"] = "default:papyrus";
208                 old_to_new["CONTENT_BOOKSHELF"] = "default:bookshelf";
209                 old_to_new["CONTENT_JUNGLETREE"] = "default:jungletree";
210                 old_to_new["CONTENT_JUNGLEGRASS"] = "default:junglegrass";
211                 old_to_new["CONTENT_NC"] = "default:nyancat";
212                 old_to_new["CONTENT_NC_RB"] = "default:nyancat_rainbow";
213                 old_to_new["CONTENT_APPLE"] = "default:apple";
214                 old_to_new["CONTENT_SAPLING"] = "default:sapling";
215                 // Just in case
216                 old_to_new["CONTENT_IGNORE"] = "ignore";
217                 old_to_new["CONTENT_AIR"] = "air";
218         }
219         std::string get(const std::string &old)
220         {
221                 std::map<std::string, std::string>::const_iterator i;
222                 i = old_to_new.find(old);
223                 if(i == old_to_new.end())
224                         return "";
225                 return i->second;
226         }
227 private:
228         std::map<std::string, std::string> old_to_new;
229 };
230
231 NewNameGetter newnamegetter;
232
233 std::string content_mapnode_get_new_name(const std::string &oldname)
234 {
235         return newnamegetter.get(oldname);
236 }
237
238 content_t legacy_get_id(const std::string &oldname, INodeDefManager *ndef)
239 {
240         std::string newname = content_mapnode_get_new_name(oldname);
241         if(newname == "")
242                 return CONTENT_IGNORE;
243         content_t id;
244         bool found = ndef->getId(newname, id);
245         if(!found)
246                 return CONTENT_IGNORE;
247         return id;
248 }
249