Move timing functions out to globalstep

This commit is contained in:
Brandon 2020-06-20 15:50:58 -04:00
parent 3dbaac7549
commit 12d0c3019e
2 changed files with 212 additions and 55 deletions

View File

@ -3,19 +3,35 @@ local is_poisoned = {}
local is_regenerating = {} local is_regenerating = {}
local is_strong = {} local is_strong = {}
local is_weak = {} local is_weak = {}
local is_water_breathing = {}
local is_leaping = {}
local is_swift = {}
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
-- Check for invisible players -- Check for invisible players
for player, bool in pairs(is_invisible) do for player, vals in pairs(is_invisible) do
if is_invisible[player] then if is_invisible[player] then
mcl_potions._add_spawner(player, "#B0B0B0")
player = player or player:get_luaentity()
is_invisible[player].timer = is_invisible[player].timer + dtime
if player:get_pos() then mcl_potions._add_spawner(player, "#B0B0B0") end
if is_invisible[player].timer >= is_invisible[player].dur then
mcl_potions.make_invisible(player, false)
is_invisible[player] = nil
end
end end
end end
-- Check for poisoned players -- Check for poisoned players
for player, bool in pairs(is_poisoned) do for player, vals in pairs(is_poisoned) do
if is_poisoned[player] then if is_poisoned[player] then
@ -24,7 +40,7 @@ minetest.register_globalstep(function(dtime)
is_poisoned[player].timer = is_poisoned[player].timer + dtime is_poisoned[player].timer = is_poisoned[player].timer + dtime
is_poisoned[player].hit_timer = (is_poisoned[player].hit_timer or 0) + dtime is_poisoned[player].hit_timer = (is_poisoned[player].hit_timer or 0) + dtime
mcl_potions._add_spawner(player, "#225533") if player:get_pos() then mcl_potions._add_spawner(player, "#225533") end
if is_poisoned[player].hit_timer >= is_poisoned[player].step then if is_poisoned[player].hit_timer >= is_poisoned[player].step then
player:set_hp( math.max(player:get_hp() - 1, 1) ) player:set_hp( math.max(player:get_hp() - 1, 1) )
@ -36,6 +52,95 @@ minetest.register_globalstep(function(dtime)
end end
end end
end
-- Check for regnerating players
for player, vals in pairs(is_regenerating) do
if is_regenerating[player] then
player = player or player:get_luaentity()
is_regenerating[player].timer = is_regenerating[player].timer + dtime
is_regenerating[player].heal_timer = (is_regenerating[player].heal_timer or 0) + dtime
if player:get_pos() then mcl_potions._add_spawner(player, "#A52BB2") end
if is_regenerating[player].heal_timer >= is_regenerating[player].step then
player:set_hp(math.min(player:get_properties().hp_max or 20, player:get_hp() + 1))
is_regenerating[player].heal_timer = 0
end
if is_regenerating[player].timer >= is_regenerating[player].dur then
is_regenerating[player] = nil
end
end
end
-- Check for water breathing players
for player, vals in pairs(is_water_breathing) do
if is_water_breathing[player] then
player = player or player:get_luaentity()
is_water_breathing[player].timer = is_water_breathing[player].timer + dtime
if player:get_pos() then mcl_potions._add_spawner(player, "#0000AA") end
if player:get_breath() then
if player:get_breath() < 10 then player:set_breath(10) end
end
if is_water_breathing[player].timer >= is_water_breathing[player].dur then
is_water_breathing[player] = nil
end
end
end
-- Check for leaping players
for player, vals in pairs(is_leaping) do
if is_leaping[player] then
player = player or player:get_luaentity()
is_leaping[player].timer = is_leaping[player].timer + dtime
if player:get_pos() then mcl_potions._add_spawner(player, "#00CC33") end
if is_leaping[player].timer >= is_leaping[player].dur then
playerphysics.remove_physics_factor(player, "jump", "leaping")
is_leaping[player] = nil
end
end
end
-- Check for swift players
for player, vals in pairs(is_swift) do
if is_swift[player] then
player = player or player:get_luaentity()
is_swift[player].timer = is_swift[player].timer + dtime
if player:get_pos() then mcl_potions._add_spawner(player, "#009999") end
if is_swift[player].timer >= is_swift[player].dur then
playerphysics.remove_physics_factor(player, "speed", "swiftness")
is_swift[player] = nil
end
end
end end
end ) end )
@ -43,41 +148,52 @@ end )
-- reset player is_invisible/poison if they go offline -- reset player is_invisible/poison if they go offline
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name() player = player or player:get_luaentity()
if is_invisible[name] then if is_invisible[player] then
is_invisible[name] = nil is_invisible[player] = nil
end end
if is_poisoned[name] then if is_poisoned[player] then
is_poisoned[name] = nil is_poisoned[player] = nil
end end
if is_regenerating[name] then if is_regenerating[player] then
is_regenerating[name] = nil is_regenerating[player] = nil
end end
if is_strong[name] then if is_strong[player] then
is_strong[name] = nil is_strong[player] = nil
end end
if is_weak[name] then if is_weak[player] then
is_weak[name] = nil is_weak[player] = nil
end
if is_water_breathing[player] then
is_water_breathing[player] = nil
end
if is_leaping[player] then
is_leaping[player] = nil
end
if is_swift[player] then
is_swift[player] = nil
end end
end) end)
function mcl_potions.invisible(player, toggle) function mcl_potions.make_invisible(player, toggle)
if not player then return false end if not player then return false end
is_invisible[player:get_player_name()] = toggle
if toggle then -- hide player if toggle then -- hide player
is_invisible[player].old_size = player:get_properties().visual_size
player:set_properties({visual_size = {x = 0, y = 0}}) player:set_properties({visual_size = {x = 0, y = 0}})
player:set_nametag_attributes({color = {a = 0}}) player:set_nametag_attributes({color = {a = 0}})
else -- show player else -- show player
player:set_properties({visual_size = {x = 1, y = 1}}) player:set_properties({visual_size = is_invisible[player].old_size})
player:set_nametag_attributes({color = {a = 255}}) player:set_nametag_attributes({color = {a = 255}})
end end
@ -165,23 +281,48 @@ function mcl_potions.healing_func(player, hp)
end end
function mcl_potions.swiftness_func(player, factor, duration) function mcl_potions.swiftness_func(player, factor, duration)
if not player:get_meta() then return false end if not player:get_meta() then return false end
playerphysics.add_physics_factor(player, "speed", "swiftness", factor)
minetest.after(duration, function() playerphysics.remove_physics_factor(player, "speed", "swiftness") end ) if not is_swift[player] then
for i=1,math.floor(duration) do
minetest.after(i, function() mcl_potions._add_spawner(player, "#009999") end) is_swift[player] = {dur = duration, timer = 0}
playerphysics.add_physics_factor(player, "speed", "swiftness", factor)
else
local victim = is_swift[player]
playerphysics.add_physics_factor(player, "speed", "swiftness", factor)
victim.dur = math.max(duration, victim.dur - victim.timer)
victim.timer = 0
end end
end end
function mcl_potions.leaping_func(player, factor, duration) function mcl_potions.leaping_func(player, factor, duration)
if player:get_meta() then return false end
playerphysics.add_physics_factor(player, "jump", "leaping", factor) if not player:get_meta() then return false end
minetest.after(duration, function() playerphysics.remove_physics_factor(player, "jump", "leaping") end )
for i=1,math.floor(duration) do if not is_leaping[player] then
minetest.after(i, function() mcl_potions._add_spawner(player, "#00CC33") end)
is_leaping[player] = {dur = duration, timer = 0}
playerphysics.add_physics_factor(player, "jump", "leaping", factor)
else
local victim = is_leaping[player]
playerphysics.add_physics_factor(player, "jump", "leaping", factor)
victim.dur = math.max(duration, victim.dur - victim.timer)
victim.timer = 0
end end
end end
function mcl_potions.weakness_func(player, factor, duration) function mcl_potions.weakness_func(player, factor, duration)
player:set_attribute("weakness", tostring(factor)) player:set_attribute("weakness", tostring(factor))
-- print(player:get_player_name().." ".."weakness = "..player:get_attribute("weakness")) -- print(player:get_player_name().." ".."weakness = "..player:get_attribute("weakness"))
@ -191,6 +332,7 @@ function mcl_potions.weakness_func(player, factor, duration)
end end
end end
function mcl_potions.poison_func(player, factor, duration) function mcl_potions.poison_func(player, factor, duration)
if not is_poisoned[player] then if not is_poisoned[player] then
@ -208,40 +350,55 @@ function mcl_potions.poison_func(player, factor, duration)
end end
end end
function mcl_potions.regeneration_func(player, factor, duration) function mcl_potions.regeneration_func(player, factor, duration)
if not is_regenerating[player:get_player_name()] then
mcl_potions.regenerate(player, true) if not is_regenerating[player] then
for i=1,math.floor(duration/factor) do
minetest.after(i*factor, function() is_regenerating[player] = {step = factor, dur = duration, timer = 0}
if player:get_hp() < 20 then
player:set_hp(player:get_hp() + 1) else
end
end ) local victim = is_regenerating[player]
end
for i=1,math.floor(duration) do victim.step = math.min(victim.step, factor)
minetest.after(i, function() mcl_potions._add_spawner(player, "#A52BB2") end) victim.dur = math.max(duration, victim.dur - victim.timer)
end victim.timer = 0
minetest.after(duration, function() mcl_potions.regenerate(player, false) end)
end end
end end
function mcl_potions.invisiblility_func(player, duration) function mcl_potions.invisiblility_func(player, duration)
mcl_potions.invisible(player, true)
minetest.after(duration, function() mcl_potions.invisible(player, false) end )
end
function mcl_potions.water_breathing_func(player, duration) if not is_invisible[player] then
if minetest.is_player(player) then
for i=1,math.floor(duration) do is_invisible[player] = {dur = duration, timer = 0}
minetest.after(i, function() mcl_potions.make_invisible(player, true)
if player:get_breath() < 10 then
player:set_breath(10) else
end
mcl_potions._add_spawner(player, "#0000AA") local victim = is_invisible[player]
end )
end victim.dur = math.max(duration, victim.dur - victim.timer)
victim.timer = 0
end
end
function mcl_potions.water_breathing_func(player, duration)
if not is_water_breathing[player] then
is_water_breathing[player] = {dur = duration, timer = 0}
else
local victim = is_water_breathing[player]
victim.dur = math.max(duration, victim.dur - victim.timer)
victim.timer = 0
end end

View File

@ -61,7 +61,7 @@ local function register_splash(name, descr, color, def)
if minetest.is_player(obj) or obj:get_entity_name() then if minetest.is_player(obj) or obj:get_entity_name() then
pos2 = obj:get_pos() local pos2 = obj:get_pos()
local rad = math.floor(math.sqrt((pos2.x-pos.x)^2 + (pos2.y-pos.y)^2 + (pos2.z-pos.z)^2)) local rad = math.floor(math.sqrt((pos2.x-pos.x)^2 + (pos2.y-pos.y)^2 + (pos2.z-pos.z)^2))
if rad > 0 then def.potion_fun(obj, redux_map[rad]) else def.potion_fun(obj, 1) end if rad > 0 then def.potion_fun(obj, redux_map[rad]) else def.potion_fun(obj, 1) end