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