item.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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, is6d)
  34. --account for y if requested
  35. if is6d and math.abs(dir.y) > math.abs(dir.x) and math.abs(dir.y) > math.abs(dir.z) then
  36. --from above
  37. if dir.y < 0 then
  38. if math.abs(dir.x) > math.abs(dir.z) then
  39. if dir.x < 0 then
  40. return 19
  41. else
  42. return 13
  43. end
  44. else
  45. if dir.z < 0 then
  46. return 10
  47. else
  48. return 4
  49. end
  50. end
  51. --from below
  52. else
  53. if math.abs(dir.x) > math.abs(dir.z) then
  54. if dir.x < 0 then
  55. return 15
  56. else
  57. return 17
  58. end
  59. else
  60. if dir.z < 0 then
  61. return 6
  62. else
  63. return 8
  64. end
  65. end
  66. end
  67. --otherwise, place horizontally
  68. elseif math.abs(dir.x) > math.abs(dir.z) then
  69. if dir.x < 0 then
  70. return 3
  71. else
  72. return 1
  73. end
  74. else
  75. if dir.z < 0 then
  76. return 2
  77. else
  78. return 0
  79. end
  80. end
  81. end
  82. function minetest.facedir_to_dir(facedir)
  83. --a table of possible dirs
  84. return ({{x=0, y=0, z=1},
  85. {x=1, y=0, z=0},
  86. {x=0, y=0, z=-1},
  87. {x=-1, y=0, z=0},
  88. {x=0, y=-1, z=0},
  89. {x=0, y=1, z=0}})
  90. --indexed into by a table of correlating facedirs
  91. [({[0]=1, 2, 3, 4,
  92. 5, 4, 6, 2,
  93. 6, 2, 5, 4,
  94. 1, 5, 3, 6,
  95. 1, 6, 3, 5,
  96. 1, 4, 3, 2})
  97. --indexed into by the facedir in question
  98. [facedir]]
  99. end
  100. function minetest.dir_to_wallmounted(dir)
  101. if math.abs(dir.y) > math.max(math.abs(dir.x), math.abs(dir.z)) then
  102. if dir.y < 0 then
  103. return 1
  104. else
  105. return 0
  106. end
  107. elseif math.abs(dir.x) > math.abs(dir.z) then
  108. if dir.x < 0 then
  109. return 3
  110. else
  111. return 2
  112. end
  113. else
  114. if dir.z < 0 then
  115. return 5
  116. else
  117. return 4
  118. end
  119. end
  120. end
  121. function minetest.get_node_drops(nodename, toolname)
  122. local drop = ItemStack({name=nodename}):get_definition().drop
  123. if drop == nil then
  124. -- default drop
  125. return {nodename}
  126. elseif type(drop) == "string" then
  127. -- itemstring drop
  128. return {drop}
  129. elseif drop.items == nil then
  130. -- drop = {} to disable default drop
  131. return {}
  132. end
  133. -- Extended drop table
  134. local got_items = {}
  135. local got_count = 0
  136. local _, item, tool
  137. for _, item in ipairs(drop.items) do
  138. local good_rarity = true
  139. local good_tool = true
  140. if item.rarity ~= nil then
  141. good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
  142. end
  143. if item.tools ~= nil then
  144. good_tool = false
  145. for _, tool in ipairs(item.tools) do
  146. if tool:sub(1, 1) == '~' then
  147. good_tool = toolname:find(tool:sub(2)) ~= nil
  148. else
  149. good_tool = toolname == tool
  150. end
  151. if good_tool then
  152. break
  153. end
  154. end
  155. end
  156. if good_rarity and good_tool then
  157. got_count = got_count + 1
  158. for _, add_item in ipairs(item.items) do
  159. got_items[#got_items+1] = add_item
  160. end
  161. if drop.max_items ~= nil and got_count == drop.max_items then
  162. break
  163. end
  164. end
  165. end
  166. return got_items
  167. end
  168. function minetest.item_place_node(itemstack, placer, pointed_thing, param2)
  169. local item = itemstack:peek_item()
  170. local def = itemstack:get_definition()
  171. if def.type ~= "node" or pointed_thing.type ~= "node" then
  172. return itemstack, false
  173. end
  174. local under = pointed_thing.under
  175. local oldnode_under = minetest.get_node_or_nil(under)
  176. local above = pointed_thing.above
  177. local oldnode_above = minetest.get_node_or_nil(above)
  178. if not oldnode_under or not oldnode_above then
  179. minetest.log("info", placer:get_player_name() .. " tried to place"
  180. .. " node in unloaded position " .. minetest.pos_to_string(above))
  181. return itemstack, false
  182. end
  183. local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
  184. olddef_under = olddef_under or minetest.nodedef_default
  185. local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
  186. olddef_above = olddef_above or minetest.nodedef_default
  187. if not olddef_above.buildable_to and not olddef_under.buildable_to then
  188. minetest.log("info", placer:get_player_name() .. " tried to place"
  189. .. " node in invalid position " .. minetest.pos_to_string(above)
  190. .. ", replacing " .. oldnode_above.name)
  191. return itemstack, false
  192. end
  193. -- Place above pointed node
  194. local place_to = {x = above.x, y = above.y, z = above.z}
  195. -- If node under is buildable_to, place into it instead (eg. snow)
  196. if olddef_under.buildable_to then
  197. minetest.log("info", "node under is buildable to")
  198. place_to = {x = under.x, y = under.y, z = under.z}
  199. end
  200. minetest.log("action", placer:get_player_name() .. " places node "
  201. .. def.name .. " at " .. minetest.pos_to_string(place_to))
  202. local oldnode = minetest.get_node(place_to)
  203. local newnode = {name = def.name, param1 = 0, param2 = param2}
  204. -- Calculate direction for wall mounted stuff like torches and signs
  205. if def.paramtype2 == 'wallmounted' and not param2 then
  206. local dir = {
  207. x = under.x - above.x,
  208. y = under.y - above.y,
  209. z = under.z - above.z
  210. }
  211. newnode.param2 = minetest.dir_to_wallmounted(dir)
  212. -- Calculate the direction for furnaces and chests and stuff
  213. elseif def.paramtype2 == 'facedir' and not param2 then
  214. local placer_pos = placer:getpos()
  215. if placer_pos then
  216. local dir = {
  217. x = above.x - placer_pos.x,
  218. y = above.y - placer_pos.y,
  219. z = above.z - placer_pos.z
  220. }
  221. newnode.param2 = minetest.dir_to_facedir(dir)
  222. minetest.log("action", "facedir: " .. newnode.param2)
  223. end
  224. end
  225. -- Check if the node is attached and if it can be placed there
  226. if minetest.get_item_group(def.name, "attached_node") ~= 0 and
  227. not check_attached_node(place_to, newnode) then
  228. minetest.log("action", "attached node " .. def.name ..
  229. " can not be placed at " .. minetest.pos_to_string(place_to))
  230. return itemstack, false
  231. end
  232. -- Add node and update
  233. minetest.add_node(place_to, newnode)
  234. local take_item = true
  235. -- Run callback
  236. if def.after_place_node then
  237. -- Copy place_to because callback can modify it
  238. local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
  239. if def.after_place_node(place_to_copy, placer, itemstack) then
  240. take_item = false
  241. end
  242. end
  243. -- Run script hook
  244. local _, callback
  245. for _, callback in ipairs(minetest.registered_on_placenodes) do
  246. -- Copy pos and node because callback can modify them
  247. local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
  248. local newnode_copy = {name=newnode.name, param1=newnode.param1, param2=newnode.param2}
  249. local oldnode_copy = {name=oldnode.name, param1=oldnode.param1, param2=oldnode.param2}
  250. if callback(place_to_copy, newnode_copy, placer, oldnode_copy, itemstack) then
  251. take_item = false
  252. end
  253. end
  254. if take_item then
  255. itemstack:take_item()
  256. end
  257. return itemstack, true
  258. end
  259. function minetest.item_place_object(itemstack, placer, pointed_thing)
  260. local pos = minetest.get_pointed_thing_position(pointed_thing, true)
  261. if pos ~= nil then
  262. local item = itemstack:take_item()
  263. minetest.add_item(pos, item)
  264. end
  265. return itemstack
  266. end
  267. function minetest.item_place(itemstack, placer, pointed_thing, param2)
  268. -- Call on_rightclick if the pointed node defines it
  269. if pointed_thing.type == "node" and placer and
  270. not placer:get_player_control().sneak then
  271. local n = minetest.get_node(pointed_thing.under)
  272. local nn = n.name
  273. if minetest.registered_nodes[nn] and minetest.registered_nodes[nn].on_rightclick then
  274. return minetest.registered_nodes[nn].on_rightclick(pointed_thing.under, n, placer, itemstack) or itemstack, false
  275. end
  276. end
  277. if itemstack:get_definition().type == "node" then
  278. return minetest.item_place_node(itemstack, placer, pointed_thing, param2)
  279. end
  280. return itemstack
  281. end
  282. function minetest.item_drop(itemstack, dropper, pos)
  283. if dropper.get_player_name then
  284. local v = dropper:get_look_dir()
  285. local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
  286. local obj = minetest.add_item(p, itemstack)
  287. if obj then
  288. v.x = v.x*2
  289. v.y = v.y*2 + 1
  290. v.z = v.z*2
  291. obj:setvelocity(v)
  292. end
  293. else
  294. minetest.add_item(pos, itemstack)
  295. end
  296. return ItemStack("")
  297. end
  298. function minetest.item_eat(hp_change, replace_with_item)
  299. return function(itemstack, user, pointed_thing) -- closure
  300. if itemstack:take_item() ~= nil then
  301. user:set_hp(user:get_hp() + hp_change)
  302. itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
  303. end
  304. return itemstack
  305. end
  306. end
  307. function minetest.node_punch(pos, node, puncher)
  308. -- Run script hook
  309. local _, callback
  310. for _, callback in ipairs(minetest.registered_on_punchnodes) do
  311. -- Copy pos and node because callback can modify them
  312. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  313. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  314. callback(pos_copy, node_copy, puncher)
  315. end
  316. end
  317. function minetest.handle_node_drops(pos, drops, digger)
  318. -- Add dropped items to object's inventory
  319. if digger:get_inventory() then
  320. local _, dropped_item
  321. for _, dropped_item in ipairs(drops) do
  322. local left = digger:get_inventory():add_item("main", dropped_item)
  323. if not left:is_empty() then
  324. local p = {
  325. x = pos.x + math.random()/2-0.25,
  326. y = pos.y + math.random()/2-0.25,
  327. z = pos.z + math.random()/2-0.25,
  328. }
  329. minetest.add_item(p, left)
  330. end
  331. end
  332. end
  333. end
  334. function minetest.node_dig(pos, node, digger)
  335. local def = ItemStack({name=node.name}):get_definition()
  336. -- Check if def ~= 0 because we always want to be able to remove unknown nodes
  337. if #def ~= 0 and not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
  338. minetest.log("info", digger:get_player_name() .. " tried to dig "
  339. .. node.name .. " which is not diggable "
  340. .. minetest.pos_to_string(pos))
  341. return
  342. end
  343. minetest.log('action', digger:get_player_name() .. " digs "
  344. .. node.name .. " at " .. minetest.pos_to_string(pos))
  345. local wielded = digger:get_wielded_item()
  346. local drops = minetest.get_node_drops(node.name, wielded:get_name())
  347. local wdef = wielded:get_definition()
  348. local tp = wielded:get_tool_capabilities()
  349. local dp = minetest.get_dig_params(def.groups, tp)
  350. if wdef and wdef.after_use then
  351. wielded = wdef.after_use(wielded, digger, node, dp) or wielded
  352. else
  353. -- Wear out tool
  354. if not minetest.setting_getbool("creative_mode") then
  355. wielded:add_wear(dp.wear)
  356. end
  357. end
  358. digger:set_wielded_item(wielded)
  359. -- Handle drops
  360. minetest.handle_node_drops(pos, drops, digger)
  361. local oldmetadata = nil
  362. if def.after_dig_node then
  363. oldmetadata = minetest.get_meta(pos):to_table()
  364. end
  365. -- Remove node and update
  366. minetest.remove_node(pos)
  367. -- Run callback
  368. if def.after_dig_node then
  369. -- Copy pos and node because callback can modify them
  370. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  371. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  372. def.after_dig_node(pos_copy, node_copy, oldmetadata, digger)
  373. end
  374. -- Run script hook
  375. local _, callback
  376. for _, callback in ipairs(minetest.registered_on_dignodes) do
  377. -- Copy pos and node because callback can modify them
  378. local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
  379. local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
  380. callback(pos_copy, node_copy, digger)
  381. end
  382. end
  383. -- This is used to allow mods to redefine minetest.item_place and so on
  384. -- NOTE: This is not the preferred way. Preferred way is to provide enough
  385. -- callbacks to not require redefining global functions. -celeron55
  386. local function redef_wrapper(table, name)
  387. return function(...)
  388. return table[name](...)
  389. end
  390. end
  391. --
  392. -- Item definition defaults
  393. --
  394. minetest.nodedef_default = {
  395. -- Item properties
  396. type="node",
  397. -- name intentionally not defined here
  398. description = "",
  399. groups = {},
  400. inventory_image = "",
  401. wield_image = "",
  402. wield_scale = {x=1,y=1,z=1},
  403. stack_max = 99,
  404. usable = false,
  405. liquids_pointable = false,
  406. tool_capabilities = nil,
  407. node_placement_prediction = nil,
  408. -- Interaction callbacks
  409. on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
  410. on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
  411. on_use = nil,
  412. can_dig = nil,
  413. on_punch = redef_wrapper(minetest, 'node_punch'), -- minetest.node_punch
  414. on_rightclick = nil,
  415. on_dig = redef_wrapper(minetest, 'node_dig'), -- minetest.node_dig
  416. on_receive_fields = nil,
  417. on_metadata_inventory_move = minetest.node_metadata_inventory_move_allow_all,
  418. on_metadata_inventory_offer = minetest.node_metadata_inventory_offer_allow_all,
  419. on_metadata_inventory_take = minetest.node_metadata_inventory_take_allow_all,
  420. -- Node properties
  421. drawtype = "normal",
  422. visual_scale = 1.0,
  423. -- Don't define these because otherwise the old tile_images and
  424. -- special_materials wouldn't be read
  425. --tiles ={""},
  426. --special_tiles = {
  427. -- {name="", backface_culling=true},
  428. -- {name="", backface_culling=true},
  429. --},
  430. alpha = 255,
  431. post_effect_color = {a=0, r=0, g=0, b=0},
  432. paramtype = "none",
  433. paramtype2 = "none",
  434. is_ground_content = false,
  435. sunlight_propagates = false,
  436. walkable = true,
  437. pointable = true,
  438. diggable = true,
  439. climbable = false,
  440. buildable_to = false,
  441. liquidtype = "none",
  442. liquid_alternative_flowing = "",
  443. liquid_alternative_source = "",
  444. liquid_viscosity = 0,
  445. drowning = 0,
  446. light_source = 0,
  447. damage_per_second = 0,
  448. selection_box = {type="regular"},
  449. legacy_facedir_simple = false,
  450. legacy_wallmounted = false,
  451. }
  452. minetest.craftitemdef_default = {
  453. type="craft",
  454. -- name intentionally not defined here
  455. description = "",
  456. groups = {},
  457. inventory_image = "",
  458. wield_image = "",
  459. wield_scale = {x=1,y=1,z=1},
  460. stack_max = 99,
  461. liquids_pointable = false,
  462. tool_capabilities = nil,
  463. -- Interaction callbacks
  464. on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
  465. on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
  466. on_use = nil,
  467. }
  468. minetest.tooldef_default = {
  469. type="tool",
  470. -- name intentionally not defined here
  471. description = "",
  472. groups = {},
  473. inventory_image = "",
  474. wield_image = "",
  475. wield_scale = {x=1,y=1,z=1},
  476. stack_max = 1,
  477. liquids_pointable = false,
  478. tool_capabilities = nil,
  479. -- Interaction callbacks
  480. on_place = redef_wrapper(minetest, 'item_place'), -- minetest.item_place
  481. on_drop = redef_wrapper(minetest, 'item_drop'), -- minetest.item_drop
  482. on_use = nil,
  483. }
  484. minetest.noneitemdef_default = { -- This is used for the hand and unknown items
  485. type="none",
  486. -- name intentionally not defined here
  487. description = "",
  488. groups = {},
  489. inventory_image = "",
  490. wield_image = "",
  491. wield_scale = {x=1,y=1,z=1},
  492. stack_max = 99,
  493. liquids_pointable = false,
  494. tool_capabilities = nil,
  495. -- Interaction callbacks
  496. on_place = redef_wrapper(minetest, 'item_place'),
  497. on_drop = nil,
  498. on_use = nil,
  499. }