performances improvement in mcl_void_damage

cache global functions
This commit is contained in:
AFCMS 2021-03-14 23:52:04 +01:00
parent e069962c48
commit bb9df764c4

View file

@ -1,6 +1,12 @@
local S = minetest.get_translator("mcl_void_damage") local S = minetest.get_translator("mcl_void_damage")
local enable_damage = minetest.settings:get_bool("enable_damage") local enable_damage = minetest.settings:get_bool("enable_damage")
local pos_to_dim = mcl_worlds.pos_to_dimension
local dim_change = mcl_worlds.dimension_change
local is_in_void = mcl_worlds.is_in_void
local get_spawn_pos = mcl_spawn.get_player_spawn_pos
local death_msg = mcl_death_messages.player_damage
local voidtimer = 0 local voidtimer = 0
local VOID_DAMAGE_FREQ = 0.5 local VOID_DAMAGE_FREQ = 0.5
local VOID_DAMAGE = 4 local VOID_DAMAGE = 4
@ -33,7 +39,7 @@ minetest.register_on_mods_loaded(function()
self._void_timer = 0 self._void_timer = 0
local pos = obj:get_pos() local pos = obj:get_pos()
local void, void_deadly = mcl_worlds.is_in_void(pos) local void, void_deadly = is_in_void(pos)
if void_deadly then if void_deadly then
local ent = obj:get_luaentity() local ent = obj:get_luaentity()
obj:remove() obj:remove()
@ -55,7 +61,7 @@ minetest.register_globalstep(function(dtime)
for p=1, #players do for p=1, #players do
local player = players[p] local player = players[p]
local pos = player:get_pos() local pos = player:get_pos()
local void, void_deadly = mcl_worlds.is_in_void(pos) local void, void_deadly = is_in_void(pos)
if void_deadly then if void_deadly then
local immortal_val = player:get_armor_groups().immortal local immortal_val = player:get_armor_groups().immortal
local is_immortal = false local is_immortal = false
@ -65,14 +71,14 @@ minetest.register_globalstep(function(dtime)
if is_immortal or not enable_damage then if is_immortal or not enable_damage then
-- If damage is disabled, we can't kill players. -- If damage is disabled, we can't kill players.
-- So we just teleport the player back to spawn. -- So we just teleport the player back to spawn.
local spawn = mcl_spawn.get_player_spawn_pos(player) local spawn = get_spawn_pos(player)
player:set_pos(spawn) player:set_pos(spawn)
mcl_worlds.dimension_change(player, mcl_worlds.pos_to_dimension(spawn)) dim_change(player, pos_to_dim(spawn))
minetest.chat_send_player(player:get_player_name(), S("The void is off-limits to you!")) minetest.chat_send_player(player:get_player_name(), S("The void is off-limits to you!"))
elseif enable_damage and not is_immortal then elseif enable_damage and not is_immortal then
-- Damage enabled, not immortal: Deal void damage (4 HP / 0.5 seconds) -- Damage enabled, not immortal: Deal void damage (4 HP / 0.5 seconds)
if player:get_hp() > 0 then if player:get_hp() > 0 then
mcl_death_messages.player_damage(player, S("@1 fell into the endless void.", player:get_player_name())) death_msg(player, S("@1 fell into the endless void.", player:get_player_name()))
player:set_hp(player:get_hp() - VOID_DAMAGE) player:set_hp(player:get_hp() - VOID_DAMAGE)
end end
end end