From c7c47c1ca76ef94f78e513414a842e949e607256 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sat, 17 Apr 2021 13:49:40 +0200 Subject: [PATCH] Increase mob_view_range_factor performance --- mods/ENTITIES/mcl_mobs/api.lua | 3 +- mods/ITEMS/mcl_armor/api.lua | 56 +++++++++------------------------ mods/ITEMS/mcl_armor/init.lua | 3 +- mods/ITEMS/mcl_armor/player.lua | 9 +++++- 4 files changed, 27 insertions(+), 44 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 87aff0d76..d5caee66f 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -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 diff --git a/mods/ITEMS/mcl_armor/api.lua b/mods/ITEMS/mcl_armor/api.lua index a5d1ac0e5..9f9384c6d 100644 --- a/mods/ITEMS/mcl_armor/api.lua +++ b/mods/ITEMS/mcl_armor/api.lua @@ -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 diff --git a/mods/ITEMS/mcl_armor/init.lua b/mods/ITEMS/mcl_armor/init.lua index cc8fb2d32..0f7725010 100644 --- a/mods/ITEMS/mcl_armor/init.lua +++ b/mods/ITEMS/mcl_armor/init.lua @@ -55,7 +55,8 @@ mcl_armor = { } end, } - } + }, + player_view_range_factors = {}, } local modpath = minetest.get_modpath("mcl_armor") diff --git a/mods/ITEMS/mcl_armor/player.lua b/mods/ITEMS/mcl_armor/player.lua index 651f8f776..50828fcea 100644 --- a/mods/ITEMS/mcl_armor/player.lua +++ b/mods/ITEMS/mcl_armor/player.lua @@ -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)