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