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