]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - builtin/game/item.lua
Optimize item.lua
[dragonfireclient.git] / builtin / game / item.lua
index 11a36bdc3e8653b0842315d5177910b92dcfd7c3..7048dded17402278994dce74f98760c74827cd29 100644 (file)
@@ -1,5 +1,7 @@
 -- Minetest: builtin/item.lua
 
+local builtin_shared = ...
+
 local function copy_pointed_thing(pointed_thing)
        return {
                type  = pointed_thing.type,
@@ -27,19 +29,11 @@ function core.get_pointed_thing_position(pointed_thing, above)
                if above then
                        -- The position where a node would be placed
                        return pointed_thing.above
-               else
-                       -- The position where a node would be dug
-                       return pointed_thing.under
                end
+               -- The position where a node would be dug
+               return pointed_thing.under
        elseif pointed_thing.type == "object" then
-               obj = pointed_thing.ref
-               if obj ~= nil then
-                       return obj:getpos()
-               else
-                       return nil
-               end
-       else
-               return nil
+               return pointed_thing.ref and pointed_thing.ref:getpos()
        end
 end
 
@@ -96,25 +90,26 @@ function core.dir_to_facedir(dir, is6d)
        end
 end
 
+-- Table of possible dirs
+local facedir_to_dir = {
+       {x= 0, y=0,  z= 1},
+       {x= 1, y=0,  z= 0},
+       {x= 0, y=0,  z=-1},
+       {x=-1, y=0,  z= 0},
+       {x= 0, y=-1, z= 0},
+       {x= 0, y=1,  z= 0},
+}
+-- Mapping from facedir value to index in facedir_to_dir.
+local facedir_to_dir_map = {
+       [0]=1, 2, 3, 4,
+       5, 2, 6, 4,
+       6, 2, 5, 4,
+       1, 5, 3, 6,
+       1, 6, 3, 5,
+       1, 4, 3, 2,
+}
 function core.facedir_to_dir(facedir)
-       --a table of possible dirs
-       return ({{x=0, y=0, z=1},
-                                       {x=1, y=0, z=0},
-                                       {x=0, y=0, z=-1},
-                                       {x=-1, y=0, z=0},
-                                       {x=0, y=-1, z=0},
-                                       {x=0, y=1, z=0}})
-
-                                       --indexed into by a table of correlating facedirs
-                                       [({[0]=1, 2, 3, 4, 
-                                               5, 2, 6, 4,
-                                               6, 2, 5, 4,
-                                               1, 5, 3, 6,
-                                               1, 6, 3, 5,
-                                               1, 4, 3, 2})
-
-                                               --indexed into by the facedir in question
-                                               [facedir]]
+       return facedir_to_dir[facedir_to_dir_map[facedir % 32]]
 end
 
 function core.dir_to_wallmounted(dir)
@@ -139,8 +134,30 @@ function core.dir_to_wallmounted(dir)
        end
 end
 
+-- table of dirs in wallmounted order
+local wallmounted_to_dir = {
+       [0] = {x = 0, y = 1, z = 0},
+       {x =  0, y = -1, z =  0},
+       {x =  1, y =  0, z =  0},
+       {x = -1, y =  0, z =  0},
+       {x =  0, y =  0, z =  1},
+       {x =  0, y =  0, z = -1},
+}
+function core.wallmounted_to_dir(wallmounted)
+       return wallmounted_to_dir[wallmounted % 8]
+end
+
+function core.dir_to_yaw(dir)
+       return -math.atan2(dir.x, dir.z)
+end
+
+function core.yaw_to_dir(yaw)
+       return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)}
+end
+
 function core.get_node_drops(nodename, toolname)
-       local drop = ItemStack({name=nodename}):get_definition().drop
+       local def = core.registered_nodes[nodename]
+       local drop = def and def.drop
        if drop == nil then
                -- default drop
                return {nodename}
@@ -189,7 +206,6 @@ function core.get_node_drops(nodename, toolname)
 end
 
 function core.item_place_node(itemstack, placer, pointed_thing, param2)
-       local item = itemstack:peek_item()
        local def = itemstack:get_definition()
        if def.type ~= "node" or pointed_thing.type ~= "node" then
                return itemstack, false
@@ -199,20 +215,21 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
        local oldnode_under = core.get_node_or_nil(under)
        local above = pointed_thing.above
        local oldnode_above = core.get_node_or_nil(above)
+       local playername = placer:get_player_name()
 
        if not oldnode_under or not oldnode_above then
-               core.log("info", placer:get_player_name() .. " tried to place"
+               core.log("info", playername .. " tried to place"
                        .. " node in unloaded position " .. core.pos_to_string(above))
                return itemstack, false
        end
 
-       local olddef_under = ItemStack({name=oldnode_under.name}):get_definition()
+       local olddef_under = core.registered_nodes[oldnode_under.name]
        olddef_under = olddef_under or core.nodedef_default
-       local olddef_above = ItemStack({name=oldnode_above.name}):get_definition()
+       local olddef_above = core.registered_nodes[oldnode_above.name]
        olddef_above = olddef_above or core.nodedef_default
 
        if not olddef_above.buildable_to and not olddef_under.buildable_to then
-               core.log("info", placer:get_player_name() .. " tried to place"
+               core.log("info", playername .. " tried to place"
                        .. " node in invalid position " .. core.pos_to_string(above)
                        .. ", replacing " .. oldnode_above.name)
                return itemstack, false
@@ -227,23 +244,27 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
                place_to = {x = under.x, y = under.y, z = under.z}
        end
 
-       if core.is_protected(place_to, placer:get_player_name()) then
-               core.log("action", placer:get_player_name()
+       if core.is_protected(place_to, playername) and
+                       not minetest.check_player_privs(placer, "protection_bypass") then
+               core.log("action", playername
                                .. " tried to place " .. def.name
                                .. " at protected position "
                                .. core.pos_to_string(place_to))
-               core.record_protection_violation(place_to, placer:get_player_name())
+               core.record_protection_violation(place_to, playername)
                return itemstack
        end
 
-       core.log("action", placer:get_player_name() .. " places node "
+       core.log("action", playername .. " places node "
                .. def.name .. " at " .. core.pos_to_string(place_to))
-       
+
        local oldnode = core.get_node(place_to)
        local newnode = {name = def.name, param1 = 0, param2 = param2}
 
        -- Calculate direction for wall mounted stuff like torches and signs
-       if def.paramtype2 == 'wallmounted' and not param2 then
+       if def.place_param2 ~= nil then
+               newnode.param2 = def.place_param2
+       elseif (def.paramtype2 == "wallmounted" or
+                       def.paramtype2 == "colorwallmounted") and not param2 then
                local dir = {
                        x = under.x - above.x,
                        y = under.y - above.y,
@@ -251,7 +272,8 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
                }
                newnode.param2 = core.dir_to_wallmounted(dir)
        -- Calculate the direction for furnaces and chests and stuff
-       elseif def.paramtype2 == 'facedir' and not param2 then
+       elseif (def.paramtype2 == "facedir" or
+                       def.paramtype2 == "colorfacedir") and not param2 then
                local placer_pos = placer:getpos()
                if placer_pos then
                        local dir = {
@@ -266,7 +288,7 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
 
        -- Check if the node is attached and if it can be placed there
        if core.get_item_group(def.name, "attached_node") ~= 0 and
-               not check_attached_node(place_to, newnode) then
+               not builtin_shared.check_attached_node(place_to, newnode) then
                core.log("action", "attached node " .. def.name ..
                        " can not be placed at " .. core.pos_to_string(place_to))
                return itemstack, false
@@ -289,7 +311,6 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
        end
 
        -- Run script hook
-       local _, callback
        for _, callback in ipairs(core.registered_on_placenodes) do
                -- Deepcopy pos, node and pointed_thing because callback can modify them
                local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
@@ -334,36 +355,69 @@ function core.item_place(itemstack, placer, pointed_thing, param2)
        return itemstack
 end
 
+function core.item_secondary_use(itemstack, placer)
+       return itemstack
+end
+
 function core.item_drop(itemstack, dropper, pos)
-       if dropper.get_player_name then
+       if dropper and dropper:is_player() then
                local v = dropper:get_look_dir()
-               local p = {x=pos.x+v.x, y=pos.y+1.5+v.y, z=pos.z+v.z}
-               local obj = core.add_item(p, itemstack)
+               local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
+               local cs = itemstack:get_count()
+               if dropper:get_player_control().sneak then
+                       cs = 1
+               end
+               local item = itemstack:take_item(cs)
+               local obj = core.add_item(p, item)
                if obj then
                        v.x = v.x*2
-                       v.y = v.y*2 + 1
+                       v.y = v.y*2 + 2
                        v.z = v.z*2
                        obj:setvelocity(v)
+                       obj:get_luaentity().dropped_by = dropper:get_player_name()
+                       return itemstack
                end
+
        else
-               core.add_item(pos, itemstack)
+               if core.add_item(pos, itemstack) then
+                       return itemstack
+               end
        end
-       return ItemStack("")
+       -- If we reach this, adding the object to the
+       -- environment failed
 end
 
-function core.item_eat(hp_change, replace_with_item)
-       return function(itemstack, user, pointed_thing)  -- closure
-               for _, callback in pairs(core.registered_on_item_eats) do
-                       local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
-                       if result then
-                               return result
-                       end
+function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
+       for _, callback in pairs(core.registered_on_item_eats) do
+               local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
+               if result then
+                       return result
                end
-               if itemstack:take_item() ~= nil then
-                       user:set_hp(user:get_hp() + hp_change)
-                       itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
+       end
+       if itemstack:take_item() ~= nil then
+               user:set_hp(user:get_hp() + hp_change)
+
+               if replace_with_item then
+                       if itemstack:is_empty() then
+                               itemstack:add_item(replace_with_item)
+                       else
+                               local inv = user:get_inventory()
+                               if inv:room_for_item("main", {name=replace_with_item}) then
+                                       inv:add_item("main", replace_with_item)
+                               else
+                                       local pos = user:getpos()
+                                       pos.y = math.floor(pos.y + 0.5)
+                                       core.add_item(pos, replace_with_item)
+                               end
+                       end
                end
-               return itemstack
+       end
+       return itemstack
+end
+
+function core.item_eat(hp_change, replace_with_item)
+       return function(itemstack, user, pointed_thing)  -- closure
+               return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
        end
 end
 
@@ -397,15 +451,17 @@ function core.handle_node_drops(pos, drops, digger)
 end
 
 function core.node_dig(pos, node, digger)
-       local def = ItemStack({name=node.name}):get_definition()
-       if not def.diggable or (def.can_dig and not def.can_dig(pos,digger)) then
+       local def = core.registered_nodes[node.name]
+       if def and (not def.diggable or
+                       (def.can_dig and not def.can_dig(pos, digger))) then
                core.log("info", digger:get_player_name() .. " tried to dig "
                        .. node.name .. " which is not diggable "
                        .. core.pos_to_string(pos))
                return
        end
 
-       if core.is_protected(pos, digger:get_player_name()) then
+       if core.is_protected(pos, digger:get_player_name()) and
+                       not minetest.check_player_privs(digger, "protection_bypass") then
                core.log("action", digger:get_player_name()
                                .. " tried to dig " .. node.name
                                .. " at protected position "
@@ -419,20 +475,23 @@ function core.node_dig(pos, node, digger)
 
        local wielded = digger:get_wielded_item()
        local drops = core.get_node_drops(node.name, wielded:get_name())
-       
+
        local wdef = wielded:get_definition()
        local tp = wielded:get_tool_capabilities()
-       local dp = core.get_dig_params(def.groups, tp)
+       local dp = core.get_dig_params(def and def.groups, tp)
        if wdef and wdef.after_use then
                wielded = wdef.after_use(wielded, digger, node, dp) or wielded
        else
                -- Wear out tool
                if not core.setting_getbool("creative_mode") then
                        wielded:add_wear(dp.wear)
+                       if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
+                               core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
+                       end
                end
        end
        digger:set_wielded_item(wielded)
-       
+
        -- Handle drops
        core.handle_node_drops(pos, drops, digger)
 
@@ -443,7 +502,7 @@ function core.node_dig(pos, node, digger)
 
        -- Remove node and update
        core.remove_node(pos)
-       
+
        -- Run callback
        if def.after_dig_node then
                -- Copy pos and node because callback can modify them
@@ -455,6 +514,15 @@ function core.node_dig(pos, node, digger)
        -- Run script hook
        local _, callback
        for _, callback in ipairs(core.registered_on_dignodes) do
+               local origin = core.callback_origins[callback]
+               if origin then
+                       core.set_last_run_mod(origin.mod)
+                       --print("Running " .. tostring(callback) ..
+                       --      " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
+               else
+                       --print("No data associated with callback")
+               end
+
                -- Copy pos and node because callback can modify them
                local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
                local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
@@ -501,7 +569,7 @@ core.nodedef_default = {
        on_dig = redef_wrapper(core, 'node_dig'), -- core.node_dig
 
        on_receive_fields = nil,
-       
+
        on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all,
        on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all,
        on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all,
@@ -527,6 +595,7 @@ core.nodedef_default = {
        diggable = true,
        climbable = false,
        buildable_to = false,
+       floodable = false,
        liquidtype = "none",
        liquid_alternative_flowing = "",
        liquid_alternative_source = "",
@@ -554,6 +623,7 @@ core.craftitemdef_default = {
        -- Interaction callbacks
        on_place = redef_wrapper(core, 'item_place'), -- core.item_place
        on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
+       on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
        on_use = nil,
 }
 
@@ -571,6 +641,7 @@ core.tooldef_default = {
 
        -- Interaction callbacks
        on_place = redef_wrapper(core, 'item_place'), -- core.item_place
+       on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
        on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
        on_use = nil,
 }
@@ -589,7 +660,7 @@ core.noneitemdef_default = {  -- This is used for the hand and unknown items
 
        -- Interaction callbacks
        on_place = redef_wrapper(core, 'item_place'),
+       on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
        on_drop = nil,
        on_use = nil,
 }
-