]> git.lizzy.rs Git - minetest.git/blob - src/itemdef.cpp
Add Client::getEnv() and remove some unnecessary wrappers
[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                                 u8 param1 = 0;
385                                 if(f.param_type == CPT_LIGHT)
386                                         param1 = 0xee;
387
388                                 /*
389                                         Make a mesh from the node
390                                 */
391                                 MeshMakeData mesh_make_data(gamedef);
392                                 MapNode mesh_make_node(id, param1, 0);
393                                 mesh_make_data.fillSingleNode(&mesh_make_node);
394                                 MapBlockMesh mapblock_mesh(&mesh_make_data);
395
396                                 scene::IMesh *node_mesh = mapblock_mesh.getMesh();
397                                 assert(node_mesh);
398                                 setMeshColor(node_mesh, video::SColor(255, 255, 255, 255));
399
400                                 /*
401                                         Scale and translate the mesh so it's a unit cube
402                                         centered on the origin
403                                 */
404                                 scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS));
405                                 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
406
407                                 /*
408                                         Draw node mesh into a render target texture
409                                 */
410                                 if(def->inventory_texture == NULL)
411                                 {
412                                         core::dimension2d<u32> dim(64,64);
413                                         std::string rtt_texture_name = "INVENTORY_"
414                                                 + def->name + "_RTT";
415                                         v3f camera_position(0, 1.0, -1.5);
416                                         camera_position.rotateXZBy(45);
417                                         v3f camera_lookat(0, 0, 0);
418                                         core::CMatrix4<f32> camera_projection_matrix;
419                                         // Set orthogonal projection
420                                         camera_projection_matrix.buildProjectionMatrixOrthoLH(
421                                                         1.65, 1.65, 0, 100);
422
423                                         video::SColorf ambient_light(0.2,0.2,0.2);
424                                         v3f light_position(10, 100, -50);
425                                         video::SColorf light_color(0.5,0.5,0.5);
426                                         f32 light_radius = 1000;
427
428                                         def->inventory_texture = generateTextureFromMesh(
429                                                 node_mesh, device, dim, rtt_texture_name,
430                                                 camera_position,
431                                                 camera_lookat,
432                                                 camera_projection_matrix,
433                                                 ambient_light,
434                                                 light_position,
435                                                 light_color,
436                                                 light_radius);
437
438                                         // render-to-target didn't work
439                                         if(def->inventory_texture == NULL)
440                                         {
441                                                 def->inventory_texture =
442                                                         tsrc->getTextureRaw(f.tname_tiles[0]);
443                                         }
444                                 }
445
446                                 /*
447                                         Use the node mesh as the wield mesh
448                                 */
449                                 if(def->wield_mesh == NULL)
450                                 {
451                                         // Scale to proper wield mesh proportions
452                                         scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0)
453                                                         * def->wield_scale);
454                                         def->wield_mesh = node_mesh;
455                                         def->wield_mesh->grab();
456                                 }
457
458                                 // falling outside of here deletes node_mesh
459                         }
460                 }
461 #endif
462         }
463         void serialize(std::ostream &os)
464         {
465                 writeU8(os, 0); // version
466                 u16 count = m_item_definitions.size();
467                 writeU16(os, count);
468                 for(std::map<std::string, ItemDefinition*>::const_iterator
469                                 i = m_item_definitions.begin();
470                                 i != m_item_definitions.end(); i++)
471                 {
472                         ItemDefinition *def = i->second;
473                         // Serialize ItemDefinition and write wrapped in a string
474                         std::ostringstream tmp_os(std::ios::binary);
475                         def->serialize(tmp_os);
476                         os<<serializeString(tmp_os.str());
477                 }
478                 writeU16(os, m_aliases.size());
479                 for(std::map<std::string, std::string>::const_iterator
480                         i = m_aliases.begin(); i != m_aliases.end(); i++)
481                 {
482                         os<<serializeString(i->first);
483                         os<<serializeString(i->second);
484                 }
485         }
486         void deSerialize(std::istream &is)
487         {
488                 // Clear everything
489                 clear();
490                 // Deserialize
491                 int version = readU8(is);
492                 if(version != 0)
493                         throw SerializationError("unsupported ItemDefManager version");
494                 u16 count = readU16(is);
495                 for(u16 i=0; i<count; i++)
496                 {
497                         // Deserialize a string and grab an ItemDefinition from it
498                         std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
499                         ItemDefinition def;
500                         def.deSerialize(tmp_is);
501                         // Register
502                         registerItem(def);
503                 }
504                 u16 num_aliases = readU16(is);
505                 for(u16 i=0; i<num_aliases; i++)
506                 {
507                         std::string name = deSerializeString(is);
508                         std::string convert_to = deSerializeString(is);
509                         registerAlias(name, convert_to);
510                 }
511         }
512 private:
513         // Key is name
514         std::map<std::string, ItemDefinition*> m_item_definitions;
515         // Aliases
516         std::map<std::string, std::string> m_aliases;
517 };
518
519 IWritableItemDefManager* createItemDefManager()
520 {
521         return new CItemDefManager();
522 }
523