mcl_util player globalstep API rewrite

This commit is contained in:
AFCMS 2021-11-15 19:48:41 +01:00
parent 79e1e2fa8f
commit 19ded9650b
5 changed files with 89 additions and 31 deletions

View File

@ -0,0 +1,9 @@
# mcl_util
This mod provide several helper functions
## Player Globalsteps
This API aims to simplificate and speedup the creation of globalsteps itterating over online players.
## `mcl_util.register_player_globalstep(def)`

View File

@ -0,0 +1,42 @@
local pairs = pairs
local table = table
mcl_util.registered_player_globalsteps = {}
local player_globalsteps = mcl_util.registered_player_globalsteps
local function do_nothing() return true end
function mcl_util.register_player_globalstep(def)
if not def.before then
def.before = do_nothing
end
if not def.after then
def.after = do_nothing
end
table.insert(mcl_util.registered_player_globalsteps, {before=def.before, func=def.func, after=def.after})
end
minetest.register_globalstep(function(dtime)
local players_ref = minetest.get_connected_players()
local players = {}
for i = 1, #players_ref do
local player = players_ref[i]
table.insert(players, {
object = player,
inventory = player:get_inventory(),
name = player:get_player_name(),
controls = player:get_player_control(),
})
end
for i = 1, #player_globalsteps do
if player_globalsteps[i].before(dtime) then
for p = 1, #players do
player_globalsteps[i].func(players[p], dtime)
end
player_globalsteps[i].after(dtime)
end
end
end)

View File

@ -1,3 +1,5 @@
local modpath = minetest.get_modpath("mcl_util")
mcl_util = {}
-- Updates all values in t using values from to*.
@ -569,3 +571,5 @@ function mcl_util.replace_mob(obj, mob)
obj:set_yaw(rot)
return obj
end
dofile(modpath.."/globalstep.lua")

View File

@ -51,37 +51,40 @@ minetest.register_on_mods_loaded(function()
end
end)
-- Hurt players or teleport them back to spawn if they are too deep in the void
minetest.register_globalstep(function(dtime)
voidtimer = voidtimer + dtime
if voidtimer > VOID_DAMAGE_FREQ then
voidtimer = 0
mcl_util.register_player_globalstep({
before = function(dtime)
voidtimer = voidtimer + dtime
if voidtimer > VOID_DAMAGE_FREQ then
voidtimer = 0
return true
else
return false
end
end,
func = function(player, dtime)
local enable_damage = minetest.settings:get_bool("enable_damage")
local players = get_connected()
for p=1, #players do
local player = players[p]
local pos = player:get_pos()
local _, void_deadly = is_in_void(pos)
if void_deadly then
local immortal_val = player:get_armor_groups().immortal
local is_immortal = false
if immortal_val and immortal_val > 0 then
is_immortal = true
end
if is_immortal or not enable_damage then
-- If damage is disabled, we can't kill players.
-- So we just teleport the player back to spawn.
local spawn = get_spawn_pos(player)
player:set_pos(spawn)
dim_change(player, pos_to_dim(spawn))
send_chat(player:get_player_name(), S("The void is off-limits to you!"))
elseif enable_damage and not is_immortal then
-- Damage enabled, not immortal: Deal void damage (4 HP / 0.5 seconds)
if player:get_hp() > 0 then
mcl_util.deal_damage(player, VOID_DAMAGE, {type = "out_of_world"})
end
local _, void_deadly = is_in_void(player.object:get_pos())
if void_deadly then
local immortal_val = player.object:get_armor_groups().immortal
local is_immortal = false
if immortal_val and immortal_val > 0 then
is_immortal = true
end
if is_immortal or not enable_damage then
-- If damage is disabled, we can't kill players.
-- So we just teleport the player back to spawn.
local spawn = get_spawn_pos(player.object)
player.object:set_pos(spawn)
dim_change(player.object, pos_to_dim(spawn))
send_chat(player.name, S("The void is off-limits to you!"))
elseif enable_damage and not is_immortal then
-- Damage enabled, not immortal: Deal void damage (4 HP / 0.5 seconds)
if player.object:get_hp() > 0 then
mcl_util.deal_damage(player.object, VOID_DAMAGE, {type = "out_of_world"})
end
end
end
end
end)
end,
--after = function(dtime) return true end,
})

View File

@ -1,4 +1,4 @@
name = mcl_void_damage
author = Wuzzy
description = Deal damage to entities stuck in the deep void
depends = mcl_worlds
depends = mcl_worlds, mcl_util