item.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. -- Minetest: builtin/item.lua
  2. --
  3. -- Item definition helpers
  4. --
  5. function minetest.inventorycube(img1, img2, img3)
  6. img2 = img2 or img1
  7. img3 = img3 or img1
  8. return "[inventorycube"
  9. .. "{" .. img1:gsub("%^", "&")
  10. .. "{" .. img2:gsub("%^", "&")
  11. .. "{" .. img3:gsub("%^", "&")
  12. end
  13. function minetest.get_pointed_thing_position(pointed_thing, above)
  14. if pointed_thing.type == "node" then
  15. if above then
  16. -- The position where a node would be placed
  17. return pointed_thing.above
  18. else
  19. -- The position where a node would be dug
  20. return pointed_thing.under
  21. end
  22. elseif pointed_thing.type == "object" then
  23. obj = pointed_thing.ref
  24. if obj ~= nil then
  25. return obj:getpos()
  26. else
  27. return nil
  28. end
  29. else
  30. return nil
  31. end
  32. end
  33. function minetest.dir_to_facedir(dir)
  34. if math.abs(dir.x) > math.abs(dir.z) then
  35. if dir.x < 0 then
  36. return 3
  37. else
  38. return 1
  39. end
  40. else
  41. if dir.z < 0 then
  42. return 2
  43. else
  44. return 0
  45. end
  46. end
  47. end
  48. function minetest.dir_to_wallmounted(dir)
  49. if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
  50. if dir.y < 0 then
  51. return 1
  52. else
  53. return 0
  54. end
  55. elseif math.abs(dir.x) > math.abs(dir.z) then
  56. if dir.x < 0 then
  57. return 3
  58. else
  59. return 2
  60. end
  61. else
  62. if dir.z < 0 then
  63. return 5
  64. else
  65. return 4
  66. end
  67. end
  68. end
  69. function minetest.get_node_drops(nodename, toolname)
  70. local drop = ItemStack({name=nodename}):get_definition().drop
  71. if drop == nil then
  72. -- default drop
  73. return {ItemStack({name=nodename})}
  74. elseif type(drop) == "string" then
  75. -- itemstring drop
  76. return {ItemStack(drop)}
  77. elseif drop.items == nil then
  78. -- drop = {} to disable default drop
  79. return {}
  80. end
  81. -- Extended drop table
  82. local got_items = {}
  83. local got_count = 0
  84. local _, item, tool
  85. for _, item in ipairs(drop.items) do
  86. local good_rarity = true
  87. local good_tool = true
  88. if item.rarity ~= nil then
  89. good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
  90. end
  91. if item.tools ~= nil then
  92. good_tool = false
  93. for _, tool in ipairs(item.tools) do
  94. if tool:sub(1, 1) == '~' then
  95. good_tool = toolname:find(tool:sub(2)) ~= nil
  96. else
  97. good_tool = toolname == tool
  98. end
  99. if good_tool then
  100. break
  101. end
  102. end
  103. end
  104. if good_rarity and good_tool then
  105. got_count = got_count + 1
  106. for _, add_item in ipairs(item.items) do
  107. got_items[#got_items+1] = add_item
  108. end
  109. if drop.max_items ~= nil and got_count == drop.max_items then
  110. break
  111. end
  112. end
  113. end
  114. return got_items
  115. end
  116. function minetest.item_place_node(itemstack, placer, pointed_thing)
  117. local item = itemstack:peek_item()
  118. local def = itemstack:get_definition()
  119. if def.type == "node" and pointed_thing.type == "node" then
  120. local pos = pointed_thing.above
  121. local oldnode = minetest.env:get_node(pos)
  122. local olddef = ItemStack({name=oldnode.name}):get_definition()
  123. if not olddef.buildable_to then
  124. minetest.log("info", placer:get_player_name() .. " tried to place"
  125. .. " node in invalid position " .. minetest.pos_to_string(pos)
  126. .. ", replacing " .. oldnode.name)
  127. return
  128. end
  129. minetest.log("action", placer:get_player_name() .. " places node "
  130. .. def.name .. " at " .. minetest.pos_to_string(pos))
  131. local newnode = {name = def.name, param1 = 0, param2 = 0}
  132. -- Calculate direction for wall mounted stuff like torches and signs
  133. if def.paramtype2 == 'wallmounted' then
  134. local under = pointed_thing.under
  135. local above = pointed_thing.above
  136. local dir = {x = under.x - above.x, y = under.y - above.y, z = under.z - above.z}
  137. newnode.param2 = minetest.dir_to_wallmounted(dir)
  138. -- Calculate the direction for furnaces and chests and stuff
  139. elseif def.paramtype2 == 'facedir' then
  140. local playerpos = placer:getpos()
  141. local dir = {x = pos.x - playerpos.x, y = pos.y - playerpos.y, z = pos.z - playerpos.z}
  142. newnode.param2 = minetest.dir_to_facedir(dir)
  143. minetest.log("action", "facedir: " .. newnode.param2)
  144. end
  145. -- Add node and update
  146. minetest.env:add_node(pos, newnode)
  147. -- Run callback
  148. if def.after_place_node then
  149. def.after_place_node(pos, placer)
  150. end
  151. -- Run script hook (deprecated)
  152. local _, callback
  153. for _, callback in ipairs(minetest.registered_on_placenodes) do
  154. callback(pos, newnode, placer)
  155. end
  156. itemstack:take_item()
  157. end
  158. return itemstack
  159. end
  160. function minetest.item_place_object(itemstack, placer, pointed_thing)
  161. local pos = minetest.get_pointed_thing_position(pointed_thing, true)
  162. if pos ~= nil then
  163. local item = itemstack:take_item()
  164. minetest.env:add_item(pos, item)
  165. end
  166. return itemstack
  167. end
  168. function minetest.item_place(itemstack, placer, pointed_thing)
  169. if itemstack:get_definition().type == "node" then
  170. return minetest.item_place_node(itemstack, placer, pointed_thing)
  171. else
  172. return minetest.item_place_object(itemstack, placer, pointed_thing)
  173. end
  174. end
  175. function minetest.item_drop(itemstack, dropper, pos)
  176. if dropper.get_player_name then
  177. local v = dropper:get_look_dir()
  178. local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
  179. local obj = minetest.env:add_item(p, itemstack)
  180. v.x = v.x*2
  181. v.y = v.y*2 + 1
  182. v.z = v.z*2
  183. obj:setvelocity(v)
  184. else
  185. minetest.env:add_item(pos, itemstack)
  186. end
  187. return ""
  188. end
  189. function minetest.item_eat(hp_change, replace_with_item)
  190. return function(itemstack, user, pointed_thing) -- closure
  191. if itemstack:take_item() ~= nil then
  192. user:set_hp(user:get_hp() + hp_change)
  193. itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
  194. end
  195. return itemstack
  196. end
  197. end
  198. function minetest.node_punch(pos, node, puncher)
  199. -- Run script hook
  200. local _, callback
  201. for _, callback in ipairs(minetest.registered_on_punchnodes) do
  202. callback(pos, node, puncher)
  203. end
  204. end
  205. function minetest.node_dig(pos, node, digger)
  206. minetest.debug("node_dig")
  207. local def = ItemStack({name=node.name}):get_definition()
  208. if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
  209. minetest.debug("not diggable")
  210. minetest.log("info", digger:get_player_name() .. " tried to dig "
  211. .. node.name .. " which is not diggable "
  212. .. minetest.pos_to_string(pos))
  213. return
  214. end
  215. minetest.log('action', digger:get_player_name() .. " digs "
  216. .. node.name .. " at " .. minetest.pos_to_string(pos))
  217. if not minetest.setting_getbool("creative_mode") then
  218. local wielded = digger:get_wielded_item()
  219. local drops = minetest.get_node_drops(node.name, wielded:get_name())
  220. -- Wear out tool
  221. tp = wielded:get_tool_capabilities()
  222. dp = minetest.get_dig_params(def.groups, tp)
  223. wielded:add_wear(dp.wear)
  224. digger:set_wielded_item(wielded)
  225. -- Add dropped items to object's inventory
  226. if digger:get_inventory() then
  227. local _, dropped_item
  228. for _, dropped_item in ipairs(drops) do
  229. digger:get_inventory():add_item("main", dropped_item)
  230. end
  231. end
  232. end
  233. local oldnode = nil
  234. local oldmetadata = nil
  235. if def.after_dig_node then
  236. oldnode = node;
  237. oldmetadata = minetest.env:get_meta(pos):to_table()
  238. end
  239. -- Remove node and update
  240. minetest.env:remove_node(pos)
  241. -- Run callback
  242. if def.after_dig_node then
  243. def.after_dig_node(pos, oldnode, oldmetadata, digger)
  244. end
  245. -- Run script hook (deprecated)
  246. local _, callback
  247. for _, callback in ipairs(minetest.registered_on_dignodes) do
  248. callback(pos, node, digger)
  249. end
  250. end
  251. function minetest.node_metadata_inventory_move_allow_all(pos, from_list,
  252. from_index, to_list, to_index, count, player)
  253. minetest.log("verbose", "node_metadata_inventory_move_allow_all")
  254. local meta = minetest.env:get_meta(pos)
  255. local inv = meta:get_inventory()
  256. local from_stack = inv:get_stack(from_list, from_index)
  257. local taken_items = from_stack:take_item(count)
  258. inv:set_stack(from_list, from_index, from_stack)
  259. local to_stack = inv:get_stack(to_list, to_index)
  260. to_stack:add_item(taken_items)
  261. inv:set_stack(to_list, to_index, to_stack)
  262. end
  263. function minetest.node_metadata_inventory_offer_allow_all(pos, listname, index, stack, player)
  264. minetest.log("verbose", "node_metadata_inventory_offer_allow_all")
  265. local meta = minetest.env:get_meta(pos)
  266. local inv = meta:get_inventory()
  267. local the_stack = inv:get_stack(listname, index)
  268. the_stack:add_item(stack)
  269. inv:set_stack(listname, index, the_stack)
  270. return ItemStack("")
  271. end
  272. function minetest.node_metadata_inventory_take_allow_all(pos, listname, index, count, player)
  273. minetest.log("verbose", "node_metadata_inventory_take_allow_all")
  274. local meta = minetest.env:get_meta(pos)
  275. local inv = meta:get_inventory()
  276. local the_stack = inv:get_stack(listname, index)
  277. local taken_items = the_stack:take_item(count)
  278. inv:set_stack(listname, index, the_stack)
  279. return taken_items
  280. end
  281. -- This is used to allow mods to redefine minetest.item_place and so on
  282. -- NOTE: This is not the preferred way. Preferred way is to provide enough
  283. -- callbacks to not require redefining global functions. -celeron55
  284. local function redef_wrapper(table, name)
  285. return function(...)
  286. return table[name](...)
  287. end
  288. end
  289. --
  290. -- Item definition defaults
  291. --
  292. minetest.nodedef_default = {
  293. -- Item properties
  294. type="node",
  295. -- name intentionally not defined here
  296. description = "",
  297. groups = {},
  298. inventory_image = "",
  299. wield_image = "",
  300. wield_scale = {x=1,y=1,z=1},
  301. stack_max = 99,
  302. usable = false,
  303. liquids_pointable = false,
  304. tool_capabilities = nil,
  305. -- Interaction callbacks
  306. on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
  307. on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
  308. on_use = nil,
  309. can_dig = nil,
  310. on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
  311. on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
  312. on_receive_fields = nil,
  313. on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
  314. on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
  315. on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
  316. -- Node properties
  317. drawtype = "normal",
  318. visual_scale = 1.0,
  319. tile_images = {""},
  320. special_materials = {
  321. {image="", backface_culling=true},
  322. {image="", backface_culling=true},
  323. },
  324. alpha = 255,
  325. post_effect_color = {a=0, r=0, g=0, b=0},
  326. paramtype = "none",
  327. paramtype2 = "none",
  328. is_ground_content = false,
  329. sunlight_propagates = false,
  330. walkable = true,
  331. pointable = true,
  332. diggable = true,
  333. climbable = false,
  334. buildable_to = false,
  335. liquidtype = "none",
  336. liquid_alternative_flowing = "",
  337. liquid_alternative_source = "",
  338. liquid_viscosity = 0,
  339. light_source = 0,
  340. damage_per_second = 0,
  341. selection_box = {type="regular"},
  342. legacy_facedir_simple = false,
  343. legacy_wallmounted = false,
  344. }
  345. minetest.craftitemdef_default = {
  346. type="craft",
  347. -- name intentionally not defined here
  348. description = "",
  349. groups = {},
  350. inventory_image = "",
  351. wield_image = "",
  352. wield_scale = {x=1,y=1,z=1},
  353. stack_max = 99,
  354. liquids_pointable = false,
  355. tool_capabilities = nil,
  356. -- Interaction callbacks
  357. on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
  358. on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
  359. on_use = nil,
  360. }
  361. minetest.tooldef_default = {
  362. type="tool",
  363. -- name intentionally not defined here
  364. description = "",
  365. groups = {},
  366. inventory_image = "",
  367. wield_image = "",
  368. wield_scale = {x=1,y=1,z=1},
  369. stack_max = 1,
  370. liquids_pointable = false,
  371. tool_capabilities = nil,
  372. -- Interaction callbacks
  373. on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
  374. on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
  375. on_use = nil,
  376. }
  377. minetest.noneitemdef_default = { -- This is used for the hand and unknown items
  378. type="none",
  379. -- name intentionally not defined here
  380. description = "",
  381. groups = {},
  382. inventory_image = "",
  383. wield_image = "",
  384. wield_scale = {x=1,y=1,z=1},
  385. stack_max = 99,
  386. liquids_pointable = false,
  387. tool_capabilities = nil,
  388. -- Interaction callbacks
  389. on_place = nil,
  390. on_drop = nil,
  391. on_use = nil,
  392. }