]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_item.cpp
Fix various copy instead of const ref reported by cppcheck (#5615)
[dragonfireclient.git] / src / script / lua_api / l_item.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 "lua_api/l_item.h"
21 #include "lua_api/l_itemstackmeta.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
25 #include "itemdef.h"
26 #include "nodedef.h"
27 #include "server.h"
28 #include "content_sao.h"
29 #include "inventory.h"
30 #include "log.h"
31
32
33 // garbage collector
34 int LuaItemStack::gc_object(lua_State *L)
35 {
36         LuaItemStack *o = *(LuaItemStack **)(lua_touserdata(L, 1));
37         delete o;
38         return 0;
39 }
40
41 // is_empty(self) -> true/false
42 int LuaItemStack::l_is_empty(lua_State *L)
43 {
44         NO_MAP_LOCK_REQUIRED;
45         LuaItemStack *o = checkobject(L, 1);
46         ItemStack &item = o->m_stack;
47         lua_pushboolean(L, item.empty());
48         return 1;
49 }
50
51 // get_name(self) -> string
52 int LuaItemStack::l_get_name(lua_State *L)
53 {
54         NO_MAP_LOCK_REQUIRED;
55         LuaItemStack *o = checkobject(L, 1);
56         ItemStack &item = o->m_stack;
57         lua_pushstring(L, item.name.c_str());
58         return 1;
59 }
60
61 // set_name(self, name)
62 int LuaItemStack::l_set_name(lua_State *L)
63 {
64         NO_MAP_LOCK_REQUIRED;
65         LuaItemStack *o = checkobject(L, 1);
66         ItemStack &item = o->m_stack;
67
68         bool status = true;
69         item.name = luaL_checkstring(L, 2);
70         if (item.name == "" || item.empty()) {
71                 item.clear();
72                 status = false;
73         }
74
75         lua_pushboolean(L, status);
76         return 1;
77 }
78
79 // get_count(self) -> number
80 int LuaItemStack::l_get_count(lua_State *L)
81 {
82         NO_MAP_LOCK_REQUIRED;
83         LuaItemStack *o = checkobject(L, 1);
84         ItemStack &item = o->m_stack;
85         lua_pushinteger(L, item.count);
86         return 1;
87 }
88
89 // set_count(self, number)
90 int LuaItemStack::l_set_count(lua_State *L)
91 {
92         NO_MAP_LOCK_REQUIRED;
93         LuaItemStack *o = checkobject(L, 1);
94         ItemStack &item = o->m_stack;
95
96         bool status;
97         lua_Integer count = luaL_checkinteger(L, 2);
98         if (count > 0 && count <= 65535) {
99                 item.count = count;
100                 status = true;
101         } else {
102                 item.clear();
103                 status = false;
104         }
105
106         lua_pushboolean(L, status);
107         return 1;
108 }
109
110 // get_wear(self) -> number
111 int LuaItemStack::l_get_wear(lua_State *L)
112 {
113         NO_MAP_LOCK_REQUIRED;
114         LuaItemStack *o = checkobject(L, 1);
115         ItemStack &item = o->m_stack;
116         lua_pushinteger(L, item.wear);
117         return 1;
118 }
119
120 // set_wear(self, number)
121 int LuaItemStack::l_set_wear(lua_State *L)
122 {
123         NO_MAP_LOCK_REQUIRED;
124         LuaItemStack *o = checkobject(L, 1);
125         ItemStack &item = o->m_stack;
126
127         bool status;
128         lua_Integer wear = luaL_checkinteger(L, 2);
129         if (wear <= 65535) {
130                 item.wear = wear;
131                 status = true;
132         } else {
133                 item.clear();
134                 status = false;
135         }
136
137         lua_pushboolean(L, status);
138         return 1;
139 }
140
141 // get_meta(self) -> string
142 int LuaItemStack::l_get_meta(lua_State *L)
143 {
144         NO_MAP_LOCK_REQUIRED;
145         LuaItemStack *o = checkobject(L, 1);
146         ItemStackMetaRef::create(L, &o->m_stack);
147         return 1;
148 }
149
150 // DEPRECATED
151 // get_metadata(self) -> string
152 int LuaItemStack::l_get_metadata(lua_State *L)
153 {
154         NO_MAP_LOCK_REQUIRED;
155         LuaItemStack *o = checkobject(L, 1);
156         ItemStack &item = o->m_stack;
157         const std::string &value = item.metadata.getString("");
158         lua_pushlstring(L, value.c_str(), value.size());
159         return 1;
160 }
161
162 // DEPRECATED
163 // set_metadata(self, string)
164 int LuaItemStack::l_set_metadata(lua_State *L)
165 {
166         NO_MAP_LOCK_REQUIRED;
167         LuaItemStack *o = checkobject(L, 1);
168         ItemStack &item = o->m_stack;
169
170         size_t len = 0;
171         const char *ptr = luaL_checklstring(L, 2, &len);
172         item.metadata.setString("", std::string(ptr, len));
173
174         lua_pushboolean(L, true);
175         return 1;
176 }
177
178 // clear(self) -> true
179 int LuaItemStack::l_clear(lua_State *L)
180 {
181         NO_MAP_LOCK_REQUIRED;
182         LuaItemStack *o = checkobject(L, 1);
183         o->m_stack.clear();
184         lua_pushboolean(L, true);
185         return 1;
186 }
187
188 // replace(self, itemstack or itemstring or table or nil) -> true
189 int LuaItemStack::l_replace(lua_State *L)
190 {
191         NO_MAP_LOCK_REQUIRED;
192         LuaItemStack *o = checkobject(L, 1);
193         o->m_stack = read_item(L, 2, getGameDef(L)->idef());
194         lua_pushboolean(L, true);
195         return 1;
196 }
197
198 // to_string(self) -> string
199 int LuaItemStack::l_to_string(lua_State *L)
200 {
201         NO_MAP_LOCK_REQUIRED;
202         LuaItemStack *o = checkobject(L, 1);
203         std::string itemstring = o->m_stack.getItemString();
204         lua_pushstring(L, itemstring.c_str());
205         return 1;
206 }
207
208 // to_table(self) -> table or nil
209 int LuaItemStack::l_to_table(lua_State *L)
210 {
211         NO_MAP_LOCK_REQUIRED;
212         LuaItemStack *o = checkobject(L, 1);
213         const ItemStack &item = o->m_stack;
214         if(item.empty())
215         {
216                 lua_pushnil(L);
217         }
218         else
219         {
220                 lua_newtable(L);
221                 lua_pushstring(L, item.name.c_str());
222                 lua_setfield(L, -2, "name");
223                 lua_pushinteger(L, item.count);
224                 lua_setfield(L, -2, "count");
225                 lua_pushinteger(L, item.wear);
226                 lua_setfield(L, -2, "wear");
227
228                 const std::string &metadata_str = item.metadata.getString("");
229                 lua_pushlstring(L, metadata_str.c_str(), metadata_str.size());
230                 lua_setfield(L, -2, "metadata");
231
232                 lua_newtable(L);
233                 const StringMap &fields = item.metadata.getStrings();
234                 for (StringMap::const_iterator it = fields.begin();
235                                 it != fields.end(); ++it) {
236                         const std::string &name = it->first;
237                         if (name.empty())
238                                 continue;
239                         const std::string &value = it->second;
240                         lua_pushlstring(L, name.c_str(), name.size());
241                         lua_pushlstring(L, value.c_str(), value.size());
242                         lua_settable(L, -3);
243                 }
244                 lua_setfield(L, -2, "meta");
245         }
246         return 1;
247 }
248
249 // get_stack_max(self) -> number
250 int LuaItemStack::l_get_stack_max(lua_State *L)
251 {
252         NO_MAP_LOCK_REQUIRED;
253         LuaItemStack *o = checkobject(L, 1);
254         ItemStack &item = o->m_stack;
255         lua_pushinteger(L, item.getStackMax(getGameDef(L)->idef()));
256         return 1;
257 }
258
259 // get_free_space(self) -> number
260 int LuaItemStack::l_get_free_space(lua_State *L)
261 {
262         NO_MAP_LOCK_REQUIRED;
263         LuaItemStack *o = checkobject(L, 1);
264         ItemStack &item = o->m_stack;
265         lua_pushinteger(L, item.freeSpace(getGameDef(L)->idef()));
266         return 1;
267 }
268
269 // is_known(self) -> true/false
270 // Checks if the item is defined.
271 int LuaItemStack::l_is_known(lua_State *L)
272 {
273         NO_MAP_LOCK_REQUIRED;
274         LuaItemStack *o = checkobject(L, 1);
275         ItemStack &item = o->m_stack;
276         bool is_known = item.isKnown(getGameDef(L)->idef());
277         lua_pushboolean(L, is_known);
278         return 1;
279 }
280
281 // get_definition(self) -> table
282 // Returns the item definition table from registered_items,
283 // or a fallback one (name="unknown")
284 int LuaItemStack::l_get_definition(lua_State *L)
285 {
286         NO_MAP_LOCK_REQUIRED;
287         LuaItemStack *o = checkobject(L, 1);
288         ItemStack &item = o->m_stack;
289
290         // Get registered_items[name]
291         lua_getglobal(L, "core");
292         lua_getfield(L, -1, "registered_items");
293         luaL_checktype(L, -1, LUA_TTABLE);
294         lua_getfield(L, -1, item.name.c_str());
295         if(lua_isnil(L, -1))
296         {
297                 lua_pop(L, 1);
298                 lua_getfield(L, -1, "unknown");
299         }
300         return 1;
301 }
302
303 // get_tool_capabilities(self) -> table
304 // Returns the effective tool digging properties.
305 // Returns those of the hand ("") if this item has none associated.
306 int LuaItemStack::l_get_tool_capabilities(lua_State *L)
307 {
308         NO_MAP_LOCK_REQUIRED;
309         LuaItemStack *o = checkobject(L, 1);
310         ItemStack &item = o->m_stack;
311         const ToolCapabilities &prop =
312                 item.getToolCapabilities(getGameDef(L)->idef());
313         push_tool_capabilities(L, prop);
314         return 1;
315 }
316
317 // add_wear(self, amount) -> true/false
318 // The range for "amount" is [0,65535]. Wear is only added if the item
319 // is a tool. Adding wear might destroy the item.
320 // Returns true if the item is (or was) a tool.
321 int LuaItemStack::l_add_wear(lua_State *L)
322 {
323         NO_MAP_LOCK_REQUIRED;
324         LuaItemStack *o = checkobject(L, 1);
325         ItemStack &item = o->m_stack;
326         int amount = lua_tointeger(L, 2);
327         bool result = item.addWear(amount, getGameDef(L)->idef());
328         lua_pushboolean(L, result);
329         return 1;
330 }
331
332 // add_item(self, itemstack or itemstring or table or nil) -> itemstack
333 // Returns leftover item stack
334 int LuaItemStack::l_add_item(lua_State *L)
335 {
336         NO_MAP_LOCK_REQUIRED;
337         LuaItemStack *o = checkobject(L, 1);
338         ItemStack &item = o->m_stack;
339         ItemStack newitem = read_item(L, -1, getGameDef(L)->idef());
340         ItemStack leftover = item.addItem(newitem, getGameDef(L)->idef());
341         create(L, leftover);
342         return 1;
343 }
344
345 // item_fits(self, itemstack or itemstring or table or nil) -> true/false, itemstack
346 // First return value is true iff the new item fits fully into the stack
347 // Second return value is the would-be-left-over item stack
348 int LuaItemStack::l_item_fits(lua_State *L)
349 {
350         NO_MAP_LOCK_REQUIRED;
351         LuaItemStack *o = checkobject(L, 1);
352         ItemStack &item = o->m_stack;
353         ItemStack newitem = read_item(L, 2, getGameDef(L)->idef());
354         ItemStack restitem;
355         bool fits = item.itemFits(newitem, &restitem, getGameDef(L)->idef());
356         lua_pushboolean(L, fits);  // first return value
357         create(L, restitem);       // second return value
358         return 2;
359 }
360
361 // take_item(self, takecount=1) -> itemstack
362 int LuaItemStack::l_take_item(lua_State *L)
363 {
364         NO_MAP_LOCK_REQUIRED;
365         LuaItemStack *o = checkobject(L, 1);
366         ItemStack &item = o->m_stack;
367         u32 takecount = 1;
368         if(!lua_isnone(L, 2))
369                 takecount = luaL_checkinteger(L, 2);
370         ItemStack taken = item.takeItem(takecount);
371         create(L, taken);
372         return 1;
373 }
374
375 // peek_item(self, peekcount=1) -> itemstack
376 int LuaItemStack::l_peek_item(lua_State *L)
377 {
378         NO_MAP_LOCK_REQUIRED;
379         LuaItemStack *o = checkobject(L, 1);
380         ItemStack &item = o->m_stack;
381         u32 peekcount = 1;
382         if(!lua_isnone(L, 2))
383                 peekcount = lua_tointeger(L, 2);
384         ItemStack peekaboo = item.peekItem(peekcount);
385         create(L, peekaboo);
386         return 1;
387 }
388
389 LuaItemStack::LuaItemStack(const ItemStack &item):
390         m_stack(item)
391 {
392 }
393
394 LuaItemStack::~LuaItemStack()
395 {
396 }
397
398 const ItemStack& LuaItemStack::getItem() const
399 {
400         return m_stack;
401 }
402 ItemStack& LuaItemStack::getItem()
403 {
404         return m_stack;
405 }
406
407 // LuaItemStack(itemstack or itemstring or table or nil)
408 // Creates an LuaItemStack and leaves it on top of stack
409 int LuaItemStack::create_object(lua_State *L)
410 {
411         NO_MAP_LOCK_REQUIRED;
412         ItemStack item = read_item(L, 1, getGameDef(L)->idef());
413         LuaItemStack *o = new LuaItemStack(item);
414         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
415         luaL_getmetatable(L, className);
416         lua_setmetatable(L, -2);
417         return 1;
418 }
419 // Not callable from Lua
420 int LuaItemStack::create(lua_State *L, const ItemStack &item)
421 {
422         NO_MAP_LOCK_REQUIRED;
423         LuaItemStack *o = new LuaItemStack(item);
424         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
425         luaL_getmetatable(L, className);
426         lua_setmetatable(L, -2);
427         return 1;
428 }
429
430 LuaItemStack* LuaItemStack::checkobject(lua_State *L, int narg)
431 {
432         luaL_checktype(L, narg, LUA_TUSERDATA);
433         void *ud = luaL_checkudata(L, narg, className);
434         if(!ud) luaL_typerror(L, narg, className);
435         return *(LuaItemStack**)ud;  // unbox pointer
436 }
437
438 void LuaItemStack::Register(lua_State *L)
439 {
440         lua_newtable(L);
441         int methodtable = lua_gettop(L);
442         luaL_newmetatable(L, className);
443         int metatable = lua_gettop(L);
444
445         lua_pushliteral(L, "__metatable");
446         lua_pushvalue(L, methodtable);
447         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
448
449         lua_pushliteral(L, "__index");
450         lua_pushvalue(L, methodtable);
451         lua_settable(L, metatable);
452
453         lua_pushliteral(L, "__gc");
454         lua_pushcfunction(L, gc_object);
455         lua_settable(L, metatable);
456
457         lua_pop(L, 1);  // drop metatable
458
459         luaL_openlib(L, 0, methods, 0);  // fill methodtable
460         lua_pop(L, 1);  // drop methodtable
461
462         // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil))
463         lua_register(L, className, create_object);
464 }
465
466 const char LuaItemStack::className[] = "ItemStack";
467 const luaL_Reg LuaItemStack::methods[] = {
468         luamethod(LuaItemStack, is_empty),
469         luamethod(LuaItemStack, get_name),
470         luamethod(LuaItemStack, set_name),
471         luamethod(LuaItemStack, get_count),
472         luamethod(LuaItemStack, set_count),
473         luamethod(LuaItemStack, get_wear),
474         luamethod(LuaItemStack, set_wear),
475         luamethod(LuaItemStack, get_meta),
476         luamethod(LuaItemStack, get_metadata),
477         luamethod(LuaItemStack, set_metadata),
478         luamethod(LuaItemStack, clear),
479         luamethod(LuaItemStack, replace),
480         luamethod(LuaItemStack, to_string),
481         luamethod(LuaItemStack, to_table),
482         luamethod(LuaItemStack, get_stack_max),
483         luamethod(LuaItemStack, get_free_space),
484         luamethod(LuaItemStack, is_known),
485         luamethod(LuaItemStack, get_definition),
486         luamethod(LuaItemStack, get_tool_capabilities),
487         luamethod(LuaItemStack, add_wear),
488         luamethod(LuaItemStack, add_item),
489         luamethod(LuaItemStack, item_fits),
490         luamethod(LuaItemStack, take_item),
491         luamethod(LuaItemStack, peek_item),
492         {0,0}
493 };
494
495 /*
496         ItemDefinition
497 */
498
499 // register_item_raw({lots of stuff})
500 int ModApiItemMod::l_register_item_raw(lua_State *L)
501 {
502         NO_MAP_LOCK_REQUIRED;
503         luaL_checktype(L, 1, LUA_TTABLE);
504         int table = 1;
505
506         // Get the writable item and node definition managers from the server
507         IWritableItemDefManager *idef =
508                         getServer(L)->getWritableItemDefManager();
509         IWritableNodeDefManager *ndef =
510                         getServer(L)->getWritableNodeDefManager();
511
512         // Check if name is defined
513         std::string name;
514         lua_getfield(L, table, "name");
515         if(lua_isstring(L, -1)){
516                 name = lua_tostring(L, -1);
517                 verbosestream<<"register_item_raw: "<<name<<std::endl;
518         } else {
519                 throw LuaError("register_item_raw: name is not defined or not a string");
520         }
521
522         // Check if on_use is defined
523
524         ItemDefinition def;
525         // Set a distinctive default value to check if this is set
526         def.node_placement_prediction = "__default";
527
528         // Read the item definition
529         read_item_definition(L, table, def, def);
530
531         // Default to having client-side placement prediction for nodes
532         // ("" in item definition sets it off)
533         if(def.node_placement_prediction == "__default"){
534                 if(def.type == ITEM_NODE)
535                         def.node_placement_prediction = name;
536                 else
537                         def.node_placement_prediction = "";
538         }
539
540         // Register item definition
541         idef->registerItem(def);
542
543         // Read the node definition (content features) and register it
544         if(def.type == ITEM_NODE){
545                 ContentFeatures f = read_content_features(L, table);
546                 content_t id = ndef->set(f.name, f);
547
548                 if(id > MAX_REGISTERED_CONTENT){
549                         throw LuaError("Number of registerable nodes ("
550                                         + itos(MAX_REGISTERED_CONTENT+1)
551                                         + ") exceeded (" + name + ")");
552                 }
553         }
554
555         return 0; /* number of results */
556 }
557
558 // unregister_item(name)
559 int ModApiItemMod::l_unregister_item_raw(lua_State *L)
560 {
561         NO_MAP_LOCK_REQUIRED;
562         std::string name = luaL_checkstring(L, 1);
563
564         IWritableItemDefManager *idef =
565                         getServer(L)->getWritableItemDefManager();
566
567         // Unregister the node
568         if (idef->get(name).type == ITEM_NODE) {
569                 IWritableNodeDefManager *ndef =
570                         getServer(L)->getWritableNodeDefManager();
571                 ndef->removeNode(name);
572         }
573
574         idef->unregisterItem(name);
575
576         return 0; /* number of results */
577 }
578
579 // register_alias_raw(name, convert_to_name)
580 int ModApiItemMod::l_register_alias_raw(lua_State *L)
581 {
582         NO_MAP_LOCK_REQUIRED;
583         std::string name = luaL_checkstring(L, 1);
584         std::string convert_to = luaL_checkstring(L, 2);
585
586         // Get the writable item definition manager from the server
587         IWritableItemDefManager *idef =
588                         getServer(L)->getWritableItemDefManager();
589
590         idef->registerAlias(name, convert_to);
591
592         return 0; /* number of results */
593 }
594
595 // get_content_id(name)
596 int ModApiItemMod::l_get_content_id(lua_State *L)
597 {
598         NO_MAP_LOCK_REQUIRED;
599         std::string name = luaL_checkstring(L, 1);
600
601         INodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
602         content_t c = ndef->getId(name);
603
604         lua_pushinteger(L, c);
605         return 1; /* number of results */
606 }
607
608 // get_name_from_content_id(name)
609 int ModApiItemMod::l_get_name_from_content_id(lua_State *L)
610 {
611         NO_MAP_LOCK_REQUIRED;
612         content_t c = luaL_checkint(L, 1);
613
614         INodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
615         const char *name = ndef->get(c).name.c_str();
616
617         lua_pushstring(L, name);
618         return 1; /* number of results */
619 }
620
621 void ModApiItemMod::Initialize(lua_State *L, int top)
622 {
623         API_FCT(register_item_raw);
624         API_FCT(unregister_item_raw);
625         API_FCT(register_alias_raw);
626         API_FCT(get_content_id);
627         API_FCT(get_name_from_content_id);
628 }