]> git.lizzy.rs Git - minetest.git/blob - src/itemdef.cpp
Clean up log messages everywhere
[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                 m_item_definitions[def.name] = new ItemDefinition(def);
295
296                 // Remove conflicting alias if it exists
297                 bool alias_removed = (m_aliases.erase(def.name) != 0);
298                 if(alias_removed)
299                         infostream<<"ItemDefManager: erased alias "<<def.name
300                                         <<" because item was defined"<<std::endl;
301         }
302         virtual void registerAlias(const std::string &name,
303                         const std::string &convert_to)
304         {
305                 if(m_item_definitions.find(name) == m_item_definitions.end())
306                 {
307                         verbosestream<<"ItemDefManager: setting alias "<<name
308                                 <<" -> "<<convert_to<<std::endl;
309                         m_aliases[name] = convert_to;
310                 }
311         }
312
313         virtual void updateTexturesAndMeshes(IGameDef *gamedef)
314         {
315 #ifndef SERVER
316                 infostream<<"ItemDefManager::updateTexturesAndMeshes(): Updating "
317                                 <<"textures and meshes in item definitions"<<std::endl;
318
319                 ITextureSource *tsrc = gamedef->getTextureSource();
320                 INodeDefManager *nodedef = gamedef->getNodeDefManager();
321                 IrrlichtDevice *device = tsrc->getDevice();
322                 video::IVideoDriver *driver = device->getVideoDriver();
323
324                 for(std::map<std::string, ItemDefinition*>::iterator
325                                 i = m_item_definitions.begin();
326                                 i != m_item_definitions.end(); i++)
327                 {
328                         ItemDefinition *def = i->second;
329
330                         bool need_node_mesh = false;
331
332                         // Create an inventory texture
333                         def->inventory_texture = NULL;
334                         if(def->inventory_image != "")
335                         {
336                                 def->inventory_texture = tsrc->getTextureRaw(def->inventory_image);
337                         }
338                         else if(def->type == ITEM_NODE)
339                         {
340                                 need_node_mesh = true;
341                         }
342
343                         // Create a wield mesh
344                         if(def->wield_mesh != NULL)
345                         {
346                                 def->wield_mesh->drop();
347                                 def->wield_mesh = NULL;
348                         }
349                         if(def->type == ITEM_NODE && def->wield_image == "")
350                         {
351                                 need_node_mesh = true;
352                         }
353                         else if(def->wield_image != "" || def->inventory_image != "")
354                         {
355                                 // Extrude the wield image into a mesh
356
357                                 std::string imagename;
358                                 if(def->wield_image != "")
359                                         imagename = def->wield_image;
360                                 else
361                                         imagename = def->inventory_image;
362
363                                 def->wield_mesh = createExtrudedMesh(
364                                                 tsrc->getTextureRaw(imagename),
365                                                 driver,
366                                                 def->wield_scale * v3f(40.0, 40.0, 4.0));
367                                 if(def->wield_mesh == NULL)
368                                 {
369                                         infostream<<"ItemDefManager: WARNING: "
370                                                 <<"updateTexturesAndMeshes(): "
371                                                 <<"Unable to create extruded mesh for item "
372                                                 <<def->name<<std::endl;
373                                 }
374                         }
375
376                         if(need_node_mesh)
377                         {
378                                 /*
379                                         Get node properties
380                                 */
381                                 content_t id = nodedef->getId(def->name);
382                                 const ContentFeatures &f = nodedef->get(id);
383
384                                 /*
385                                         Make a mesh from the node
386                                 */
387                                 MeshMakeData mesh_make_data;
388                                 MapNode mesh_make_node(
389                                         id,
390                                         (f.param_type == CPT_LIGHT) ? 0xee : 0,
391                                         0);
392                                 mesh_make_data.fillSingleNode(1000, &mesh_make_node);
393                                 scene::IMesh *node_mesh =
394                                         makeMapBlockMesh(&mesh_make_data, gamedef);
395                                 setMeshColor(node_mesh, video::SColor(255, 255, 255, 255));
396
397                                 /*
398                                         Scale and translate the mesh so it's a unit cube
399                                         centered on the origin
400                                 */
401                                 scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS));
402                                 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
403
404                                 /*
405                                         Draw node mesh into a render target texture
406                                 */
407                                 if(def->inventory_texture == NULL && node_mesh != NULL)
408                                 {
409                                         core::dimension2d<u32> dim(64,64);
410                                         std::string rtt_texture_name = "INVENTORY_"
411                                                 + def->name + "_RTT";
412                                         v3f camera_position(0, 1.0, -1.5);
413                                         camera_position.rotateXZBy(45);
414                                         v3f camera_lookat(0, 0, 0);
415                                         core::CMatrix4<f32> camera_projection_matrix;
416                                         // Set orthogonal projection
417                                         camera_projection_matrix.buildProjectionMatrixOrthoLH(
418                                                         1.65, 1.65, 0, 100);
419
420                                         video::SColorf ambient_light(0.2,0.2,0.2);
421                                         v3f light_position(10, 100, -50);
422                                         video::SColorf light_color(0.5,0.5,0.5);
423                                         f32 light_radius = 1000;
424
425                                         def->inventory_texture = generateTextureFromMesh(
426                                                 node_mesh, device, dim, rtt_texture_name,
427                                                 camera_position,
428                                                 camera_lookat,
429                                                 camera_projection_matrix,
430                                                 ambient_light,
431                                                 light_position,
432                                                 light_color,
433                                                 light_radius);
434
435                                         // render-to-target didn't work
436                                         if(def->inventory_texture == NULL)
437                                         {
438                                                 def->inventory_texture =
439                                                         tsrc->getTextureRaw(f.tname_tiles[0]);
440                                         }
441                                 }
442
443                                 /*
444                                         Use the node mesh as the wield mesh
445                                 */
446                                 if(def->wield_mesh == NULL && node_mesh != NULL)
447                                 {
448                                         // Scale to proper wield mesh proportions
449                                         scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0)
450                                                         * def->wield_scale);
451                                         def->wield_mesh = node_mesh;
452                                         def->wield_mesh->grab();
453                                 }
454
455
456                                 if(node_mesh != NULL)
457                                         node_mesh->drop();
458                         }
459                 }
460 #endif
461         }
462         void serialize(std::ostream &os)
463         {
464                 writeU8(os, 0); // version
465                 u16 count = m_item_definitions.size();
466                 writeU16(os, count);
467                 for(std::map<std::string, ItemDefinition*>::const_iterator
468                                 i = m_item_definitions.begin();
469                                 i != m_item_definitions.end(); i++)
470                 {
471                         ItemDefinition *def = i->second;
472                         // Serialize ItemDefinition and write wrapped in a string
473                         std::ostringstream tmp_os(std::ios::binary);
474                         def->serialize(tmp_os);
475                         os<<serializeString(tmp_os.str());
476                 }
477                 writeU16(os, m_aliases.size());
478                 for(std::map<std::string, std::string>::const_iterator
479                         i = m_aliases.begin(); i != m_aliases.end(); i++)
480                 {
481                         os<<serializeString(i->first);
482                         os<<serializeString(i->second);
483                 }
484         }
485         void deSerialize(std::istream &is)
486         {
487                 // Clear everything
488                 clear();
489                 // Deserialize
490                 int version = readU8(is);
491                 if(version != 0)
492                         throw SerializationError("unsupported ItemDefManager version");
493                 u16 count = readU16(is);
494                 for(u16 i=0; i<count; i++)
495                 {
496                         // Deserialize a string and grab an ItemDefinition from it
497                         std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
498                         ItemDefinition def;
499                         def.deSerialize(tmp_is);
500                         // Register
501                         registerItem(def);
502                 }
503                 u16 num_aliases = readU16(is);
504                 for(u16 i=0; i<num_aliases; i++)
505                 {
506                         std::string name = deSerializeString(is);
507                         std::string convert_to = deSerializeString(is);
508                         registerAlias(name, convert_to);
509                 }
510         }
511 private:
512         // Key is name
513         std::map<std::string, ItemDefinition*> m_item_definitions;
514         // Aliases
515         std::map<std::string, std::string> m_aliases;
516 };
517
518 IWritableItemDefManager* createItemDefManager()
519 {
520         return new CItemDefManager();
521 }
522