You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

636 lines
17 KiB

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