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