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)
|
||||
|
||||
-- pipeworks fake player check
|
||||
if not player.get_attribute then
|
||||
if not player or not player.get_attribute then
|
||||
return nil
|
||||
end
|
||||
|
||||
@ -204,27 +204,14 @@ local function set_sprinting(name, sprinting)
|
||||
end
|
||||
|
||||
|
||||
-- Time based stamina functions
|
||||
local stamina_timer, health_timer, action_timer, poison_timer, drunk_timer = 0,0,0,0,0
|
||||
local function drunk_tick()
|
||||
|
||||
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
|
||||
|
||||
local players = minetest.get_connected_players()
|
||||
|
||||
-- simulate drunk walking (thanks LumberJ)
|
||||
if drunk_timer > 1.0 then
|
||||
|
||||
for _,player in pairs(players) do
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
if stamina.players[name]
|
||||
if name
|
||||
and stamina.players[name]
|
||||
and stamina.players[name].drunk then
|
||||
|
||||
-- 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].units = 0
|
||||
|
||||
if not stamina.players[name].poisoned then
|
||||
|
||||
player:hud_change(stamina.players[name].hud_id,
|
||||
"text", "stamina_hud_fg.png")
|
||||
end
|
||||
end
|
||||
|
||||
-- effect only works when not riding boat/cart/horse etc.
|
||||
if not player:get_attach() then
|
||||
|
||||
local yaw = player:get_look_horizontal()
|
||||
|
||||
yaw = yaw + math.random(-0.5, 0.5)
|
||||
local yaw = player:get_look_horizontal() + math.random(-0.5, 0.5)
|
||||
|
||||
player:set_look_horizontal(yaw)
|
||||
end
|
||||
end
|
||||
|
||||
drunk_timer = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- hurt player when poisoned
|
||||
if poison_timer > STAMINA_POISON_TICK then
|
||||
local function health_tick()
|
||||
|
||||
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()
|
||||
|
||||
if stamina.players[name]
|
||||
and stamina.players[name].poisoned
|
||||
and stamina.players[name].poisoned > 0 then
|
||||
if name 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})
|
||||
-- damage player by 1 hp if saturation is < 2
|
||||
if h and h < STAMINA_STARVE_LVL
|
||||
and hp > 0 then
|
||||
player:set_hp(hp - STAMINA_STARVE, {hunger = true})
|
||||
end
|
||||
|
||||
elseif stamina.players[name]
|
||||
and stamina.players[name].poisoned then
|
||||
-- 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:hud_change(stamina.players[name].hud_id,
|
||||
"text", "stamina_hud_fg.png")
|
||||
player:set_hp(hp + STAMINA_HEAL)
|
||||
|
||||
stamina.players[name].poisoned = nil
|
||||
end
|
||||
|
||||
poison_timer = 0
|
||||
stamina_update_level(player, h - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- sprint control and particle animation
|
||||
if action_timer > STAMINA_MOVE_TICK then
|
||||
local function action_tick()
|
||||
|
||||
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
|
||||
if controls then
|
||||
@ -320,10 +304,11 @@ local function stamina_globaltimer(dtime)
|
||||
--- START sprint
|
||||
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)
|
||||
if stamina.players[name]
|
||||
if name
|
||||
and stamina.players[name]
|
||||
and not stamina.players[name].poisoned
|
||||
and not stamina.players[name].drunk
|
||||
and controls and controls.aux1 and controls.up
|
||||
@ -337,7 +322,10 @@ local function stamina_globaltimer(dtime)
|
||||
|
||||
local pos = player:get_pos()
|
||||
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
|
||||
|
||||
@ -367,65 +355,100 @@ local function stamina_globaltimer(dtime)
|
||||
|
||||
stamina_update_level(player,
|
||||
level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
|
||||
else
|
||||
|
||||
elseif name then
|
||||
set_sprinting(name, false)
|
||||
end
|
||||
end
|
||||
-- END sprint
|
||||
|
||||
action_timer = 0
|
||||
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
|
||||
|
||||
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
|
||||
if stamina_timer > STAMINA_TICK then
|
||||
|
||||
for _,player in pairs(players) do
|
||||
|
||||
local h = get_int_attribute(player)
|
||||
|
||||
if h > STAMINA_TICK_MIN then
|
||||
stamina_update_level(player, h - 1)
|
||||
stamina_tick() ; stamina_timer = 0
|
||||
end
|
||||
|
||||
stamina_timer = 0
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- heal or damage player, depending on saturation
|
||||
if health_timer > STAMINA_HEALTH_TICK then
|
||||
|
||||
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})
|
||||
health_tick() ; health_timer = 0
|
||||
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
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user