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