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