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