hbsprint/init.lua

240 lines
7.3 KiB
Lua
Raw Normal View History

2017-07-12 01:07:32 +02:00
-- Vars
2019-10-17 16:09:23 +02:00
local function setting_get(name, default)
return minetest.settings:get(name) or default
end
local speed = tonumber(setting_get("sprint_speed", "1.3"))
local jump = tonumber(setting_get("sprint_jump", "1.1"))
local dir = minetest.is_yes(setting_get("sprint_forward_only", "false"))
local particles = tonumber(setting_get("sprint_particles", "2"))
local stamina = minetest.is_yes(setting_get("sprint_stamina", "true"))
local stamina_drain = tonumber(setting_get("sprint_stamina_drain", "2"))
local replenish = tonumber(setting_get("sprint_stamina_replenish", "2"))
local starve = minetest.is_yes(setting_get("sprint_starve", "true"))
local starve_drain = tonumber(setting_get("sprint_starve_drain", "0.5"))
local starve_limit = tonumber(setting_get("sprint_starve_limit", "6"))
local breath = minetest.is_yes(setting_get("sprint_breath", "true"))
local breath_drain = tonumber(setting_get("sprint_breath_drain", "1"))
local autohide = minetest.is_yes(setting_get("hudbards_autohide_stamina", "false"))
2017-07-12 01:07:32 +02:00
local sprint_timer_step = 0.5
local sprint_timer = 0
2019-10-17 16:17:44 +02:00
local sprinting = {}
local stamina_timer = {}
local breath_timer = {}
2017-07-12 01:07:32 +02:00
2019-10-17 16:09:23 +02:00
local mod_hudbars = minetest.get_modpath("hudbars") ~= nil
local mod_player_monoids = minetest.get_modpath("player_monoids") ~= nil
local mod_playerphysics = minetest.get_modpath("playerphysics") ~= nil
if starve then
if minetest.get_modpath("hbhunger") then
starve = "hbhunger"
elseif minetest.get_modpath("hunger_ng") then
starve = "hunger_ng"
else
starve = false
end
2018-02-25 22:15:18 +01:00
end
if minetest.settings:get_bool("creative_mode") then
starve = false
end
2017-07-12 01:07:32 +02:00
-- Functions
local function start_sprint(player)
2019-10-17 16:43:56 +02:00
local name = player:get_player_name()
if not sprinting[name] then
2019-02-23 05:16:24 +01:00
if mod_player_monoids then
2017-08-24 08:21:20 +02:00
player_monoids.speed:add_change(player, speed, "hbsprint:speed")
player_monoids.jump:add_change(player, jump, "hbsprint:jump")
2019-02-23 05:16:24 +01:00
elseif mod_playerphysics then
playerphysics.add_physics_factor(player, "speed", "hbsprint:speed", speed)
playerphysics.add_physics_factor(player, "jump", "hbsprint:jump", jump)
2017-08-24 08:21:20 +02:00
else
player:set_physics_override({speed = speed, jump = jump})
end
2019-10-17 16:43:56 +02:00
sprinting[name] = true
2017-08-24 08:21:20 +02:00
end
2017-07-12 01:07:32 +02:00
end
local function stop_sprint(player)
2019-10-17 16:43:56 +02:00
local name = player:get_player_name()
if sprinting[name] then
2019-02-23 05:16:24 +01:00
if mod_player_monoids then
2017-08-24 08:21:20 +02:00
player_monoids.speed:del_change(player, "hbsprint:speed")
player_monoids.jump:del_change(player, "hbsprint:jump")
2019-02-23 05:16:24 +01:00
elseif mod_playerphysics then
playerphysics.remove_physics_factor(player, "speed", "hbsprint:speed")
playerphysics.remove_physics_factor(player, "jump", "hbsprint:jump")
2017-08-24 08:21:20 +02:00
else
player:set_physics_override({speed = 1, jump = 1})
end
2019-10-17 16:43:56 +02:00
sprinting[name] = false
2017-08-24 08:21:20 +02:00
end
2017-07-12 01:07:32 +02:00
end
local function drain_stamina(player)
2019-10-17 16:43:56 +02:00
local player_stamina = player:get_meta():get_float("hbsprint:stamina")
2017-08-24 08:21:20 +02:00
if player_stamina > 0 then
2019-10-17 16:43:56 +02:00
player_stamina = math.max(0, player_stamina - stamina_drain)
player:get_meta():set_float("hbsprint:stamina", player_stamina)
2017-08-24 08:21:20 +02:00
end
if mod_hudbars then
2017-08-24 08:21:20 +02:00
if autohide and player_stamina < 20 then hb.unhide_hudbar(player, "stamina") end
hb.change_hudbar(player, "stamina", player_stamina)
end
2017-07-12 01:07:32 +02:00
end
local function replenish_stamina(player)
2019-10-17 16:43:56 +02:00
local player_stamina = player:get_meta():get_float("hbsprint:stamina")
2017-08-24 08:21:20 +02:00
if player_stamina < 20 then
2019-10-17 16:43:56 +02:00
player_stamina = math.min(20, player_stamina + stamina_drain)
player:get_meta():set_float("hbsprint:stamina", player_stamina)
2017-08-24 08:21:20 +02:00
end
if mod_hudbars then
2017-08-24 08:21:20 +02:00
hb.change_hudbar(player, "stamina", player_stamina)
2019-10-17 16:43:56 +02:00
if autohide and player_stamina >= 20 then hb.hide_hudbar(player, "stamina") end
2017-08-24 08:21:20 +02:00
end
2017-07-12 01:07:32 +02:00
end
2019-10-17 16:43:56 +02:00
local function drain_hunger(player, name)
if starve == "hbhunger" then
local hunger = tonumber(hbhunger.hunger[name]) - starve_drain
hbhunger.hunger[name] = math.max(0, hunger)
hbhunger.set_hunger_raw(player)
elseif starve == "hunger_ng" then
hunger_ng.alter_hunger(name, -starve_drain, "Sprinting")
2017-08-24 08:21:20 +02:00
end
2017-07-12 01:07:32 +02:00
end
2017-07-24 21:36:47 +02:00
local function drain_breath(player)
2017-08-24 08:21:20 +02:00
local player_breath = player:get_breath()
if player_breath < 11 then
2019-10-17 16:43:56 +02:00
player_breath = math.max(0, player_breath - breath_drain)
player:set_breath(player_breath)
2017-08-24 08:21:20 +02:00
end
2017-07-24 21:36:47 +02:00
end
2017-07-12 01:07:32 +02:00
local function create_particles(player, name, pos, ground)
2019-10-17 16:43:56 +02:00
if not ground or ground.name == "air" or ground.name == "ignore" then
return
end
local def = minetest.registered_nodes[ground.name]
local tile = def.tiles[1] or def.inventory_image
if type(tile) ~= "string" then
return
end
local rand = function() return math.random(-1,1) * math.random() / 2 end
for i = 1, particles do
minetest.add_particle({
pos = {x = pos.x + rand(), y = pos.y + 0.1, z = pos.z + rand()},
velocity = {x = 0, y = 5, z = 0},
acceleration = {x = 0, y = -13, z = 0},
expirationtime = math.random(),
size = math.random() + 0.5,
vertical = false,
texture = tile,
})
2017-08-24 08:21:20 +02:00
end
2017-07-12 01:07:32 +02:00
end
-- Registrations
if mod_hudbars and stamina then
2018-02-23 13:23:47 +01:00
hb.register_hudbar(
"stamina",
2017-08-24 08:21:20 +02:00
0xFFFFFF,
"Stamina",
2018-02-23 13:23:47 +01:00
{
bar = "sprint_stamina_bar.png",
icon = "sprint_stamina_icon.png",
bgicon = "sprint_stamina_bgicon.png"
},
20,
20,
autohide)
2017-07-12 01:07:32 +02:00
end
minetest.register_on_joinplayer(function(player)
2019-10-17 16:43:56 +02:00
if stamina then
if mod_hudbars then
hb.init_hudbar(player, "stamina", 20, 20, autohide)
end
player:get_meta():set_float("hbsprint:stamina", 20)
end
2017-07-12 01:07:32 +02:00
end)
2019-10-17 16:43:56 +02:00
local function sprint_step(player, dtime)
local name = player:get_player_name()
if stamina then
stamina_timer[name] = (stamina_timer[name] or 0) + dtime
end
if breath then
breath_timer[name] = (breath_timer[name] or 0) + dtime
end
local ctrl = player:get_player_control()
local key_press
if dir then
key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right
else
key_press = ctrl.aux1
end
if not key_press then
stop_sprint(player)
if stamina and stamina_timer[name] >= replenish then
replenish_stamina(player)
stamina_timer[name] = 0
end
return
end
local pos = player:get_pos()
local ground = minetest.get_node_or_nil({x=pos.x, y=pos.y-1, z=pos.z})
local walkable = false
if ground ~= nil then
local ground_def = minetest.registered_nodes[ground.name]
walkable = ground_def and (ground_def.walkable or ground_def.liquidtype ~= "none")
end
local player_stamina = 1
if stamina then
player_stamina = player:get_meta():get_float("hbsprint:stamina")
end
local hunger = 30
if starve == "hbhunger" then
hunger = tonumber(hbhunger.hunger[name])
elseif starve == "hunger_ng" then
hunger = hunger_ng.get_hunger_information(name).hunger.exact
end
if player_stamina > 0 and hunger > starve_limit and walkable then
start_sprint(player)
if stamina then drain_stamina(player) end
if starve then drain_hunger(player, name) end
if breath and breath_timer[name] >= 2 then
drain_breath(player)
breath_timer[name] = 0
end
if particles then
create_particles(player, name, pos, ground)
end
else
stop_sprint(player)
end
end
2017-07-12 01:07:32 +02:00
minetest.register_globalstep(function(dtime)
2017-08-24 08:21:20 +02:00
sprint_timer = sprint_timer + dtime
if sprint_timer >= sprint_timer_step then
2019-10-17 16:43:56 +02:00
for _, player in ipairs(minetest.get_connected_players()) do
sprint_step(player, sprint_timer)
2017-08-24 08:21:20 +02:00
end
sprint_timer = 0
end
2017-07-12 01:07:32 +02:00
end)