Increase mob_view_range_factor performance

This commit is contained in:
Lizzy Fleckenstein 2021-04-17 13:49:40 +02:00
parent d952423dda
commit c7c47c1ca7
4 changed files with 27 additions and 44 deletions

View File

@ -177,7 +177,8 @@ local function object_in_range(self, object)
local factor
-- Apply view range reduction for special player armor
if object:is_player() and mod_armor then
factor = mcl_armor.get_mob_view_range_factor(object, self.name)
local factors = mcl_armor.player_view_range_factors[object]
factor = factors and factors[self.name]
end
-- Distance check
local dist

View File

@ -160,48 +160,8 @@ function mcl_armor.register_protection_enchantment(def)
}
end
function mcl_armor.get_armor_points(obj)
local points = 0
local inv = mcl_util.get_inventory(obj)
if inv then
for i = 2, 5 do
local itemstack = inv:get_stack("armor", i)
if not itemstack:is_empty() then
points = points + minetest.get_item_group(itemstack:get_name(), "mcl_armor_points")
end
end
end
return points
end
-- Returns a change factor for a mob's view_range for the given object
-- or nil, if there's no change. Certain armors (like mob heads) can
-- affect the view range of mobs.
function mcl_armor.get_mob_view_range_factor(obj, mob)
local inv = mcl_util.get_inventory(obj)
local factor
if inv then
for i = 2, 5 do
local itemstack = inv:get_stack("armor", i)
if not itemstack:is_empty() then
local def = itemstack:get_definition()
if def._mcl_armor_mob_range_mob == mob then
if not factor then
factor = def._mcl_armor_mob_range_factor
elseif factor == 0 then
return 0
else
factor = factor * def._mcl_armor_mob_range_factor
end
end
end
end
end
return factor
end
function mcl_armor.update(obj)
local info = {points = 0}
local info = {points = 0, view_range_factors = {}}
local inv = mcl_util.get_inventory(obj)
@ -226,6 +186,20 @@ function mcl_armor.update(obj)
end
info.points = info.points + minetest.get_item_group(itemname, "mcl_armor_points")
local mob_range_mob = def._mcl_armor_mob_range_mob
if mob_range_mob then
local factor = info.view_range_factors[mob_range_mob]
if factor then
if factor > 0 then
info.view_range_factors[mob_range_mob] = factor * def._mcl_armor_mob_range_factor
end
else
info.view_range_factors[mob_range_mob] = def._mcl_armor_mob_range_factor
end
end
end
end
end

View File

@ -55,7 +55,8 @@ mcl_armor = {
}
end,
}
}
},
player_view_range_factors = {},
}
local modpath = minetest.get_modpath("mcl_armor")

View File

@ -61,7 +61,10 @@ mcl_player.player_register_model("mcl_armor_character_female.b3d", {
function mcl_armor.update_player(player, info)
mcl_player.player_set_armor(player, info.texture, info.preview)
player:get_meta():set_int("mcl_armor:armor_points", info.points)
local meta = player:get_meta()
meta:set_int("mcl_armor:armor_points", info.points)
mcl_armor.player_view_range_factors[player] = view_range_factors
end
local function is_armor_action(inventory_info)
@ -149,6 +152,10 @@ minetest.register_on_joinplayer(function(player)
end)
end)
minetest.register_on_leaveplayer(function(player)
mcl_armor.player_view_range_factors[player] = nil
end)
mcl_damage.register_modifier(function(player, hp_change, _, reason)
return mcl_armor.damage_modifier(player, hp_change, reason)
end)