From 8cd114a9e407f89149d56ce23d3d366bd2db0405 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Fri, 26 May 2017 23:10:18 +0200 Subject: [PATCH] Fix arrow damaging multiple objects at once --- mods/ITEMS/mcl_throwing/arrow.lua | 33 +++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/mods/ITEMS/mcl_throwing/arrow.lua b/mods/ITEMS/mcl_throwing/arrow.lua index f82dcba451..dc1e474296 100644 --- a/mods/ITEMS/mcl_throwing/arrow.lua +++ b/mods/ITEMS/mcl_throwing/arrow.lua @@ -53,11 +53,40 @@ THROWING_ARROW_ENTITY.on_step = function(self, dtime) local node = minetest.get_node(pos) if self._timer>0.2 then - local objs = minetest.get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2) + local objs = minetest.get_objects_inside_radius(pos, 2) + local closest_object + local closest_distance + local ok = false + + -- Iterate through all objects and remember the closest attackable object for k, obj in pairs(objs) do if obj:get_luaentity() ~= nil then local entity_name = obj:get_luaentity().name - if obj ~= self._shooter and entity_name ~= "mcl_throwing:arrow_entity" and entity_name ~= "__builtin:item" then + if obj ~= self._shooter and entity_name ~= "mcl_throwing:arrow_entity" and entity_name ~= "__builtin:item" and entity_name ~= "__builtin:falling_node" then + ok = true + end + elseif obj ~= self._shooter then + ok = true + end + + if ok then + local dist = vector.distance(pos, obj:getpos()) + if not closest_object or not closest_distance then + closest_object = obj + closest_distance = dist + elseif dist < closest_distance then + closest_object = obj + closest_distance = dist + end + end + end + + -- If an attackable object was found, we will damage the closest one only + if closest_object ~= nil then + local obj = closest_object + if obj:get_luaentity() ~= nil then + local entity_name = obj:get_luaentity().name + if obj ~= self._shooter and entity_name ~= "mcl_throwing:arrow_entity" and entity_name ~= "__builtin:item" and entity_name ~= "__builtin:falling_node" then obj:punch(self.object, 1.0, { full_punch_interval=1.0, damage_groups={fleshy=self._damage},