1
0
Fork 0

Merge pull request 'Raytraced_arrows_fix' (#2297) from Raytraced_arrows_fix into master

Reviewed-on: MineClone2/MineClone2#2297
This commit is contained in:
epCode 2022-06-11 02:42:45 +00:00
commit fab6cf8152
2 changed files with 44 additions and 34 deletions

View File

@ -410,28 +410,33 @@ function ARROW_ENTITY.on_step(self, dtime)
end end
-- Iterate through all objects and remember the closest attackable object -- Iterate through all objects and remember the closest attackable object
for k, obj in pairs(objs) do local arrow_dir = self.object:get_velocity()
--create a raycast from the arrow based on the velocity of the arrow to deal with lag
local raycast = minetest.raycast(pos, vector.add(pos, vector.multiply(arrow_dir, 0.1)), true, false)
for hitpoint in raycast do
if hitpoint.type == "object" then
-- find the closest object that is in the way of the arrow
local ok = false local ok = false
-- Arrows can only damage players and mobs if hitpoint.ref:is_player() then
if obj:is_player() then
ok = true ok = true
elseif obj:get_luaentity() then elseif hitpoint.ref:get_luaentity() then
if (obj:get_luaentity().is_mob or obj:get_luaentity()._hittable_by_projectile) then if (hitpoint.ref:get_luaentity().is_mob or hitpoint.ref:get_luaentity()._hittable_by_projectile) then
ok = true ok = true
end end
end end
if ok then
local dist = vector.distance(hitpoint.ref:get_pos(), pos)
if not closest_object or not closest_distance then
closest_object = hitpoint.ref
closest_distance = dist
elseif dist < closest_distance then
closest_object = hitpoint.ref
closest_distance = dist
end
end
end
end
if ok then
local dist = vector.distance(pos, obj:get_pos())
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 an attackable object was found, we will damage the closest one only

View File

@ -210,28 +210,33 @@ function mcl_potions.register_arrow(name, desc, color, def)
end end
-- Iterate through all objects and remember the closest attackable object -- Iterate through all objects and remember the closest attackable object
for k, obj in pairs(objs) do local arrow_dir = self.object:get_velocity()
--create a raycast from the arrow based on the velocity of the arrow to deal with lag
local raycast = minetest.raycast(pos, vector.add(pos, vector.multiply(arrow_dir, 0.1)), true, false)
for hitpoint in raycast do
if hitpoint.type == "object" then
-- find the closest object that is in the way of the arrow
local ok = false local ok = false
-- Arrows can only damage players and mobs if hitpoint.ref:is_player() then
if obj ~= self._shooter and obj:is_player() then
ok = true ok = true
elseif obj:get_luaentity() then elseif hitpoint.ref:get_luaentity() then
if obj ~= self._shooter and obj:get_luaentity().is_mob then if (hitpoint.ref:get_luaentity().is_mob or hitpoint.ref:get_luaentity()._hittable_by_projectile) then
ok = true ok = true
end end
end end
if ok then
local dist = vector.distance(hitpoint.ref:get_pos(), pos)
if not closest_object or not closest_distance then
closest_object = hitpoint.ref
closest_distance = dist
elseif dist < closest_distance then
closest_object = hitpoint.ref
closest_distance = dist
end
end
end
end
if ok then
local dist = vector.distance(pos, obj:get_pos())
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 an attackable object was found, we will damage the closest one only
if closest_object then if closest_object then