Tune caves
[oweals/minetest.git] / src / itemdef.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Kahrl <kahrl@gmx.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "itemdef.h"
22
23 #include "gamedef.h"
24 #include "nodedef.h"
25 #include "tool.h"
26 #include "inventory.h"
27 #ifndef SERVER
28 #include "mapblock_mesh.h"
29 #include "mesh.h"
30 #include "tile.h"
31 #endif
32 #include "log.h"
33 #include "utility.h"
34 #include <map>
35 #include <set>
36
37 /*
38         ItemDefinition
39 */
40 ItemDefinition::ItemDefinition()
41 {
42         resetInitial();
43 }
44
45 ItemDefinition::ItemDefinition(const ItemDefinition &def)
46 {
47         resetInitial();
48         *this = def;
49 }
50
51 ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
52 {
53         if(this == &def)
54                 return *this;
55
56         reset();
57
58         type = def.type;
59         name = def.name;
60         description = def.description;
61         inventory_image = def.inventory_image;
62         wield_image = def.wield_image;
63         wield_scale = def.wield_scale;
64         stack_max = def.stack_max;
65         usable = def.usable;
66         liquids_pointable = def.liquids_pointable;
67         if(def.tool_capabilities)
68         {
69                 tool_capabilities = new ToolCapabilities(
70                                 *def.tool_capabilities);
71         }
72         groups = def.groups;
73 #ifndef SERVER
74         inventory_texture = def.inventory_texture;
75         if(def.wield_mesh)
76         {
77                 wield_mesh = def.wield_mesh;
78                 wield_mesh->grab();
79         }
80 #endif
81         return *this;
82 }
83
84 ItemDefinition::~ItemDefinition()
85 {
86         reset();
87 }
88
89 void ItemDefinition::resetInitial()
90 {
91         // Initialize pointers to NULL so reset() does not delete undefined pointers
92         tool_capabilities = NULL;
93 #ifndef SERVER
94         inventory_texture = NULL;
95         wield_mesh = NULL;
96 #endif
97         reset();
98 }
99
100 void ItemDefinition::reset()
101 {
102         type = ITEM_NONE;
103         name = "";
104         description = "";
105         inventory_image = "";
106         wield_image = "";
107         wield_scale = v3f(1.0, 1.0, 1.0);
108         stack_max = 99;
109         usable = false;
110         liquids_pointable = false;
111         if(tool_capabilities)
112         {
113                 delete tool_capabilities;
114                 tool_capabilities = NULL;
115         }
116         groups.clear();
117
118 #ifndef SERVER
119         inventory_texture = NULL;
120         if(wield_mesh)
121         {
122                 wield_mesh->drop();
123                 wield_mesh = NULL;
124         }
125 #endif
126 }
127
128 void ItemDefinition::serialize(std::ostream &os) const
129 {
130         writeU8(os, 1); // version
131         writeU8(os, type);
132         os<<serializeString(name);
133         os<<serializeString(description);
134         os<<serializeString(inventory_image);
135         os<<serializeString(wield_image);
136         writeV3F1000(os, wield_scale);
137         writeS16(os, stack_max);
138         writeU8(os, usable);
139         writeU8(os, liquids_pointable);
140         std::string tool_capabilities_s = "";
141         if(tool_capabilities){
142                 std::ostringstream tmp_os(std::ios::binary);
143                 tool_capabilities->serialize(tmp_os);
144                 tool_capabilities_s = tmp_os.str();
145         }
146         os<<serializeString(tool_capabilities_s);
147         writeU16(os, groups.size());
148         for(std::map<std::string, int>::const_iterator
149                         i = groups.begin(); i != groups.end(); i++){
150                 os<<serializeString(i->first);
151                 writeS16(os, i->second);
152         }
153 }
154
155 void ItemDefinition::deSerialize(std::istream &is)
156 {
157         // Reset everything
158         reset();
159
160         // Deserialize
161         int version = readU8(is);
162         if(version != 1)
163                 throw SerializationError("unsupported ItemDefinition version");
164         type = (enum ItemType)readU8(is);
165         name = deSerializeString(is);
166         description = deSerializeString(is);
167         inventory_image = deSerializeString(is);
168         wield_image = deSerializeString(is);
169         wield_scale = readV3F1000(is);
170         stack_max = readS16(is);
171         usable = readU8(is);
172         liquids_pointable = readU8(is);
173         std::string tool_capabilities_s = deSerializeString(is);
174         if(!tool_capabilities_s.empty())
175         {
176                 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
177                 tool_capabilities = new ToolCapabilities;
178                 tool_capabilities->deSerialize(tmp_is);
179         }
180         groups.clear();
181         u32 groups_size = readU16(is);
182         for(u32 i=0; i<groups_size; i++){
183                 std::string name = deSerializeString(is);
184                 int value = readS16(is);
185                 groups[name] = value;
186         }
187 }
188
189 /*
190         CItemDefManager
191 */
192
193 // SUGG: Support chains of aliases?
194
195 class CItemDefManager: public IWritableItemDefManager
196 {
197 public:
198         CItemDefManager()
199         {
200                 clear();
201         }
202         virtual ~CItemDefManager()
203         {
204         }
205         virtual const ItemDefinition& get(const std::string &name_) const
206         {
207                 // Convert name according to possible alias
208                 std::string name = getAlias(name_);
209                 // Get the definition
210                 std::map<std::string, ItemDefinition*>::const_iterator i;
211                 i = m_item_definitions.find(name);
212                 if(i == m_item_definitions.end())
213                         i = m_item_definitions.find("unknown");
214                 assert(i != m_item_definitions.end());
215                 return *(i->second);
216         }
217         virtual std::string getAlias(const std::string &name) const
218         {
219                 std::map<std::string, std::string>::const_iterator i;
220                 i = m_aliases.find(name);
221                 if(i != m_aliases.end())
222                         return i->second;
223                 return name;
224         }
225         virtual std::set<std::string> getAll() const
226         {
227                 std::set<std::string> result;
228                 for(std::map<std::string, ItemDefinition*>::const_iterator
229                                 i = m_item_definitions.begin();
230                                 i != m_item_definitions.end(); i++)
231                 {
232                         result.insert(i->first);
233                 }
234                 for(std::map<std::string, std::string>::const_iterator
235                                 i = m_aliases.begin();
236                                 i != m_aliases.end(); i++)
237                 {
238                         result.insert(i->first);
239                 }
240                 return result;
241         }
242         virtual bool isKnown(const std::string &name_) const
243         {
244                 // Convert name according to possible alias
245                 std::string name = getAlias(name_);
246                 // Get the definition
247                 std::map<std::string, ItemDefinition*>::const_iterator i;
248                 return m_item_definitions.find(name) != m_item_definitions.end();
249         }
250         void clear()
251         {
252                 for(std::map<std::string, ItemDefinition*>::const_iterator
253                                 i = m_item_definitions.begin();
254                                 i != m_item_definitions.end(); i++)
255                 {
256                         delete i->second;
257                 }
258                 m_item_definitions.clear();
259                 m_aliases.clear();
260
261                 // Add the four builtin items:
262                 //   "" is the hand
263                 //   "unknown" is returned whenever an undefined item is accessed
264                 //   "air" is the air node
265                 //   "ignore" is the ignore node
266
267                 ItemDefinition* hand_def = new ItemDefinition;
268                 hand_def->name = "";
269                 hand_def->wield_image = "wieldhand.png";
270                 hand_def->tool_capabilities = new ToolCapabilities;
271                 m_item_definitions.insert(std::make_pair("", hand_def));
272
273                 ItemDefinition* unknown_def = new ItemDefinition;
274                 unknown_def->name = "unknown";
275                 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
276
277                 ItemDefinition* air_def = new ItemDefinition;
278                 air_def->type = ITEM_NODE;
279                 air_def->name = "air";
280                 m_item_definitions.insert(std::make_pair("air", air_def));
281
282                 ItemDefinition* ignore_def = new ItemDefinition;
283                 ignore_def->type = ITEM_NODE;
284                 ignore_def->name = "ignore";
285                 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
286         }
287         virtual void registerItem(const ItemDefinition &def)
288         {
289                 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
290                 // Ensure that the "" item (the hand) always has ToolCapabilities
291                 if(def.name == "")
292                         assert(def.tool_capabilities != NULL);
293                 
294                 if(m_item_definitions.count(def.name) == 0)
295                         m_item_definitions[def.name] = new ItemDefinition(def);
296                 else
297                         *(m_item_definitions[def.name]) = def;
298
299                 // Remove conflicting alias if it exists
300                 bool alias_removed = (m_aliases.erase(def.name) != 0);
301                 if(alias_removed)
302                         infostream<<"ItemDefManager: erased alias "<<def.name
303                                         <<" because item was defined"<<std::endl;
304         }
305         virtual void registerAlias(const std::string &name,
306                         const std::string &convert_to)
307         {
308                 if(m_item_definitions.find(name) == m_item_definitions.end())
309                 {
310                         verbosestream<<"ItemDefManager: setting alias "<<name
311                                 <<" -> "<<convert_to<<std::endl;
312                         m_aliases[name] = convert_to;
313                 }
314         }
315
316         virtual void updateTexturesAndMeshes(IGameDef *gamedef)
317         {
318 #ifndef SERVER
319                 infostream<<"ItemDefManager::updateTexturesAndMeshes(): Updating "
320                                 <<"textures and meshes in item definitions"<<std::endl;
321
322                 ITextureSource *tsrc = gamedef->getTextureSource();
323                 INodeDefManager *nodedef = gamedef->getNodeDefManager();
324                 IrrlichtDevice *device = tsrc->getDevice();
325                 video::IVideoDriver *driver = device->getVideoDriver();
326
327                 for(std::map<std::string, ItemDefinition*>::iterator
328                                 i = m_item_definitions.begin();
329                                 i != m_item_definitions.end(); i++)
330                 {
331                         ItemDefinition *def = i->second;
332
333                         bool need_node_mesh = false;
334
335                         // Create an inventory texture
336                         def->inventory_texture = NULL;
337                         if(def->inventory_image != "")
338                         {
339                                 def->inventory_texture = tsrc->getTextureRaw(def->inventory_image);
340                         }
341                         else if(def->type == ITEM_NODE)
342                         {
343                                 need_node_mesh = true;
344                         }
345
346                         // Create a wield mesh
347                         if(def->wield_mesh != NULL)
348                         {
349                                 def->wield_mesh->drop();
350                                 def->wield_mesh = NULL;
351                         }
352                         if(def->type == ITEM_NODE && def->wield_image == "")
353                         {
354                                 need_node_mesh = true;
355                         }
356                         else if(def->wield_image != "" || def->inventory_image != "")
357                         {
358                                 // Extrude the wield image into a mesh
359
360                                 std::string imagename;
361                                 if(def->wield_image != "")
362                                         imagename = def->wield_image;
363                                 else
364                                         imagename = def->inventory_image;
365
366                                 def->wield_mesh = createExtrudedMesh(
367                                                 tsrc->getTextureRaw(imagename),
368                                                 driver,
369                                                 def->wield_scale * v3f(40.0, 40.0, 4.0));
370                                 if(def->wield_mesh == NULL)
371                                 {
372                                         infostream<<"ItemDefManager: WARNING: "
373                                                 <<"updateTexturesAndMeshes(): "
374                                                 <<"Unable to create extruded mesh for item "
375                                                 <<def->name<<std::endl;
376                                 }
377                         }
378
379                         if(need_node_mesh)
380                         {
381                                 /*
382                                         Get node properties
383                                 */
384                                 content_t id = nodedef->getId(def->name);
385                                 const ContentFeatures &f = nodedef->get(id);
386
387                                 u8 param1 = 0;
388                                 if(f.param_type == CPT_LIGHT)
389                                         param1 = 0xee;
390
391                                 /*
392                                         Make a mesh from the node
393                                 */
394                                 MeshMakeData mesh_make_data(gamedef);
395                                 MapNode mesh_make_node(id, param1, 0);
396                                 mesh_make_data.fillSingleNode(&mesh_make_node);
397                                 MapBlockMesh mapblock_mesh(&mesh_make_data);
398
399                                 scene::IMesh *node_mesh = mapblock_mesh.getMesh();
400                                 assert(node_mesh);
401                                 setMeshColor(node_mesh, video::SColor(255, 255, 255, 255));
402
403                                 /*
404                                         Scale and translate the mesh so it's a unit cube
405                                         centered on the origin
406                                 */
407                                 scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS));
408                                 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
409
410                                 /*
411                                         Draw node mesh into a render target texture
412                                 */
413                                 if(def->inventory_texture == NULL)
414                                 {
415                                         core::dimension2d<u32> dim(64,64);
416                                         std::string rtt_texture_name = "INVENTORY_"
417                                                 + def->name + "_RTT";
418                                         v3f camera_position(0, 1.0, -1.5);
419                                         camera_position.rotateXZBy(45);
420                                         v3f camera_lookat(0, 0, 0);
421                                         core::CMatrix4<f32> camera_projection_matrix;
422                                         // Set orthogonal projection
423                                         camera_projection_matrix.buildProjectionMatrixOrthoLH(
424                                                         1.65, 1.65, 0, 100);
425
426                                         video::SColorf ambient_light(0.2,0.2,0.2);
427                                         v3f light_position(10, 100, -50);
428                                         video::SColorf light_color(0.5,0.5,0.5);
429                                         f32 light_radius = 1000;
430
431                                         def->inventory_texture = generateTextureFromMesh(
432                                                 node_mesh, device, dim, rtt_texture_name,
433                                                 camera_position,
434                                                 camera_lookat,
435                                                 camera_projection_matrix,
436                                                 ambient_light,
437                                                 light_position,
438                                                 light_color,
439                                                 light_radius);
440
441                                         // render-to-target didn't work
442                                         if(def->inventory_texture == NULL)
443                                         {
444                                                 def->inventory_texture =
445                                                         tsrc->getTextureRaw(f.tname_tiles[0]);
446                                         }
447                                 }
448
449                                 /*
450                                         Use the node mesh as the wield mesh
451                                 */
452                                 if(def->wield_mesh == NULL)
453                                 {
454                                         // Scale to proper wield mesh proportions
455                                         scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0)
456                                                         * def->wield_scale);
457                                         def->wield_mesh = node_mesh;
458                                         def->wield_mesh->grab();
459                                 }
460
461                                 // falling outside of here deletes node_mesh
462                         }
463                 }
464 #endif
465         }
466         void serialize(std::ostream &os)
467         {
468                 writeU8(os, 0); // version
469                 u16 count = m_item_definitions.size();
470                 writeU16(os, count);
471                 for(std::map<std::string, ItemDefinition*>::const_iterator
472                                 i = m_item_definitions.begin();
473                                 i != m_item_definitions.end(); i++)
474                 {
475                         ItemDefinition *def = i->second;
476                         // Serialize ItemDefinition and write wrapped in a string
477                         std::ostringstream tmp_os(std::ios::binary);
478                         def->serialize(tmp_os);
479                         os<<serializeString(tmp_os.str());
480                 }
481                 writeU16(os, m_aliases.size());
482                 for(std::map<std::string, std::string>::const_iterator
483                         i = m_aliases.begin(); i != m_aliases.end(); i++)
484                 {
485                         os<<serializeString(i->first);
486                         os<<serializeString(i->second);
487                 }
488         }
489         void deSerialize(std::istream &is)
490         {
491                 // Clear everything
492                 clear();
493                 // Deserialize
494                 int version = readU8(is);
495                 if(version != 0)
496                         throw SerializationError("unsupported ItemDefManager version");
497                 u16 count = readU16(is);
498                 for(u16 i=0; i<count; i++)
499                 {
500                         // Deserialize a string and grab an ItemDefinition from it
501                         std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
502                         ItemDefinition def;
503                         def.deSerialize(tmp_is);
504                         // Register
505                         registerItem(def);
506                 }
507                 u16 num_aliases = readU16(is);
508                 for(u16 i=0; i<num_aliases; i++)
509                 {
510                         std::string name = deSerializeString(is);
511                         std::string convert_to = deSerializeString(is);
512                         registerAlias(name, convert_to);
513                 }
514         }
515 private:
516         // Key is name
517         std::map<std::string, ItemDefinition*> m_item_definitions;
518         // Aliases
519         std::map<std::string, std::string> m_aliases;
520 };
521
522 IWritableItemDefManager* createItemDefManager()
523 {
524         return new CItemDefManager();
525 }
526