tidy and tweak code, add some nil checks
This commit is contained in:
parent
d597cf488c
commit
aacc11d64c
215
init.lua
215
init.lua
@ -31,7 +31,7 @@ SPRINT_DRAIN = 0.35 -- how fast to drain satation while sprinting (0-1)
|
|||||||
local function get_int_attribute(player)
|
local function get_int_attribute(player)
|
||||||
|
|
||||||
-- pipeworks fake player check
|
-- pipeworks fake player check
|
||||||
if not player.get_attribute then
|
if not player or not player.get_attribute then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -204,27 +204,14 @@ local function set_sprinting(name, sprinting)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Time based stamina functions
|
local function drunk_tick()
|
||||||
local stamina_timer, health_timer, action_timer, poison_timer, drunk_timer = 0,0,0,0,0
|
|
||||||
|
|
||||||
local function stamina_globaltimer(dtime)
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
stamina_timer = stamina_timer + dtime
|
|
||||||
health_timer = health_timer + dtime
|
|
||||||
action_timer = action_timer + dtime
|
|
||||||
poison_timer = poison_timer + dtime
|
|
||||||
drunk_timer = drunk_timer + dtime
|
|
||||||
|
|
||||||
local players = minetest.get_connected_players()
|
|
||||||
|
|
||||||
-- simulate drunk walking (thanks LumberJ)
|
|
||||||
if drunk_timer > 1.0 then
|
|
||||||
|
|
||||||
for _,player in pairs(players) do
|
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
if stamina.players[name]
|
if name
|
||||||
|
and stamina.players[name]
|
||||||
and stamina.players[name].drunk then
|
and stamina.players[name].drunk then
|
||||||
|
|
||||||
-- play burp sound every 20 seconds when drunk
|
-- play burp sound every 20 seconds when drunk
|
||||||
@ -242,66 +229,63 @@ local function stamina_globaltimer(dtime)
|
|||||||
stamina.players[name].drunk = nil
|
stamina.players[name].drunk = nil
|
||||||
stamina.players[name].units = 0
|
stamina.players[name].units = 0
|
||||||
|
|
||||||
|
if not stamina.players[name].poisoned then
|
||||||
|
|
||||||
player:hud_change(stamina.players[name].hud_id,
|
player:hud_change(stamina.players[name].hud_id,
|
||||||
"text", "stamina_hud_fg.png")
|
"text", "stamina_hud_fg.png")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- effect only works when not riding boat/cart/horse etc.
|
-- effect only works when not riding boat/cart/horse etc.
|
||||||
if not player:get_attach() then
|
if not player:get_attach() then
|
||||||
|
|
||||||
local yaw = player:get_look_horizontal()
|
local yaw = player:get_look_horizontal() + math.random(-0.5, 0.5)
|
||||||
|
|
||||||
yaw = yaw + math.random(-0.5, 0.5)
|
|
||||||
|
|
||||||
player:set_look_horizontal(yaw)
|
player:set_look_horizontal(yaw)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
drunk_timer = 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- hurt player when poisoned
|
local function health_tick()
|
||||||
if poison_timer > STAMINA_POISON_TICK then
|
|
||||||
|
|
||||||
for _,player in pairs(players) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
|
local air = player:get_breath() or 0
|
||||||
|
local hp = player:get_hp()
|
||||||
|
local h = get_int_attribute(player)
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
if stamina.players[name]
|
if name then
|
||||||
and stamina.players[name].poisoned
|
|
||||||
and stamina.players[name].poisoned > 0 then
|
|
||||||
|
|
||||||
stamina.players[name].poisoned =
|
-- damage player by 1 hp if saturation is < 2
|
||||||
stamina.players[name].poisoned - 1
|
if h and h < STAMINA_STARVE_LVL
|
||||||
|
and hp > 0 then
|
||||||
local hp = player:get_hp() - 1
|
player:set_hp(hp - STAMINA_STARVE, {hunger = true})
|
||||||
|
|
||||||
if hp > 0 then
|
|
||||||
player:set_hp(hp, {poison = true})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif stamina.players[name]
|
-- don't heal if drowning or dead or poisoned
|
||||||
and stamina.players[name].poisoned then
|
if h and h >= STAMINA_HEAL_LVL
|
||||||
|
and h >= hp
|
||||||
|
and hp > 0
|
||||||
|
and air > 0
|
||||||
|
and not stamina.players[name].poisoned then
|
||||||
|
|
||||||
player:hud_change(stamina.players[name].hud_id,
|
player:set_hp(hp + STAMINA_HEAL)
|
||||||
"text", "stamina_hud_fg.png")
|
|
||||||
|
|
||||||
stamina.players[name].poisoned = nil
|
stamina_update_level(player, h - 1)
|
||||||
end
|
|
||||||
|
|
||||||
poison_timer = 0
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- sprint control and particle animation
|
local function action_tick()
|
||||||
if action_timer > STAMINA_MOVE_TICK then
|
|
||||||
|
|
||||||
for _,player in pairs(players) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
local controls = player:get_player_control()
|
local controls = player and player:get_player_control()
|
||||||
|
|
||||||
-- Determine if the player is walking or jumping
|
-- Determine if the player is walking or jumping
|
||||||
if controls then
|
if controls then
|
||||||
@ -320,10 +304,11 @@ local function stamina_globaltimer(dtime)
|
|||||||
--- START sprint
|
--- START sprint
|
||||||
if enable_sprint then
|
if enable_sprint then
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player and player:get_player_name()
|
||||||
|
|
||||||
-- check if player can sprint (stamina must be over 6 points)
|
-- check if player can sprint (stamina must be over 6 points)
|
||||||
if stamina.players[name]
|
if name
|
||||||
|
and stamina.players[name]
|
||||||
and not stamina.players[name].poisoned
|
and not stamina.players[name].poisoned
|
||||||
and not stamina.players[name].drunk
|
and not stamina.players[name].drunk
|
||||||
and controls and controls.aux1 and controls.up
|
and controls and controls.aux1 and controls.up
|
||||||
@ -337,7 +322,10 @@ local function stamina_globaltimer(dtime)
|
|||||||
|
|
||||||
local pos = player:get_pos()
|
local pos = player:get_pos()
|
||||||
local node = minetest.get_node({
|
local node = minetest.get_node({
|
||||||
x = pos.x, y = pos.y - 1, z = pos.z})
|
x = pos.x,
|
||||||
|
y = pos.y - 1,
|
||||||
|
z = pos.z
|
||||||
|
})
|
||||||
|
|
||||||
if node.name ~= "air" then
|
if node.name ~= "air" then
|
||||||
|
|
||||||
@ -367,65 +355,100 @@ local function stamina_globaltimer(dtime)
|
|||||||
|
|
||||||
stamina_update_level(player,
|
stamina_update_level(player,
|
||||||
level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
|
level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
|
||||||
else
|
|
||||||
|
elseif name then
|
||||||
set_sprinting(name, false)
|
set_sprinting(name, false)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- END sprint
|
-- END sprint
|
||||||
|
|
||||||
action_timer = 0
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function poison_tick()
|
||||||
|
|
||||||
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
|
local name = player and player:get_player_name()
|
||||||
|
|
||||||
|
if name
|
||||||
|
and stamina.players[name]
|
||||||
|
and stamina.players[name].poisoned
|
||||||
|
and stamina.players[name].poisoned > 0 then
|
||||||
|
|
||||||
|
stamina.players[name].poisoned =
|
||||||
|
stamina.players[name].poisoned - 1
|
||||||
|
|
||||||
|
local hp = player:get_hp() - 1
|
||||||
|
|
||||||
|
if hp > 0 then
|
||||||
|
player:set_hp(hp, {poison = true})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
elseif name
|
||||||
|
and stamina.players[name]
|
||||||
|
and stamina.players[name].poisoned then
|
||||||
|
|
||||||
|
if not stamina.players[name].drunk then
|
||||||
|
|
||||||
|
player:hud_change(stamina.players[name].hud_id,
|
||||||
|
"text", "stamina_hud_fg.png")
|
||||||
|
end
|
||||||
|
|
||||||
|
stamina.players[name].poisoned = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function stamina_tick()
|
||||||
|
|
||||||
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
|
local h = get_int_attribute(player)
|
||||||
|
|
||||||
|
if h and h > STAMINA_TICK_MIN then
|
||||||
|
stamina_update_level(player, h - 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Time based stamina functions
|
||||||
|
local stamina_timer, health_timer, action_timer, poison_timer, drunk_timer = 0,0,0,0,0
|
||||||
|
|
||||||
|
local function stamina_globaltimer(dtime)
|
||||||
|
|
||||||
|
stamina_timer = stamina_timer + dtime
|
||||||
|
health_timer = health_timer + dtime
|
||||||
|
action_timer = action_timer + dtime
|
||||||
|
poison_timer = poison_timer + dtime
|
||||||
|
drunk_timer = drunk_timer + dtime
|
||||||
|
|
||||||
|
-- simulate drunk walking (thanks LumberJ)
|
||||||
|
if drunk_timer > 1.0 then
|
||||||
|
drunk_tick() ; drunk_timer = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
-- hurt player when poisoned
|
||||||
|
if poison_timer > STAMINA_POISON_TICK then
|
||||||
|
poison_tick() ; poison_timer = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
-- sprint control and particle animation
|
||||||
|
if action_timer > STAMINA_MOVE_TICK then
|
||||||
|
action_tick() ; action_timer = 0
|
||||||
|
end
|
||||||
|
|
||||||
-- lower saturation by 1 point after STAMINA_TICK
|
-- lower saturation by 1 point after STAMINA_TICK
|
||||||
if stamina_timer > STAMINA_TICK then
|
if stamina_timer > STAMINA_TICK then
|
||||||
|
stamina_tick() ; stamina_timer = 0
|
||||||
for _,player in pairs(players) do
|
|
||||||
|
|
||||||
local h = get_int_attribute(player)
|
|
||||||
|
|
||||||
if h > STAMINA_TICK_MIN then
|
|
||||||
stamina_update_level(player, h - 1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
stamina_timer = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- heal or damage player, depending on saturation
|
-- heal or damage player, depending on saturation
|
||||||
if health_timer > STAMINA_HEALTH_TICK then
|
if health_timer > STAMINA_HEALTH_TICK then
|
||||||
|
health_tick() ; health_timer = 0
|
||||||
for _,player in pairs(players) do
|
|
||||||
|
|
||||||
local air = player:get_breath() or 0
|
|
||||||
local hp = player:get_hp()
|
|
||||||
local h = get_int_attribute(player)
|
|
||||||
local name = player:get_player_name()
|
|
||||||
|
|
||||||
-- damage player by 1 hp if saturation is < 2 (of 30)
|
|
||||||
if h and h < STAMINA_STARVE_LVL
|
|
||||||
and hp > 0 then
|
|
||||||
player:set_hp(hp - STAMINA_STARVE, {hunger = true})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- don't heal if drowning or dead or poisoned
|
|
||||||
if h and h >= STAMINA_HEAL_LVL
|
|
||||||
and h >= hp
|
|
||||||
and hp > 0
|
|
||||||
and air > 0
|
|
||||||
and not stamina.players[name].poisoned then
|
|
||||||
|
|
||||||
player:set_hp(hp + STAMINA_HEAL)
|
|
||||||
|
|
||||||
stamina_update_level(player, h - 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
health_timer = 0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user