hbsprint/init.lua

258 lines
7.8 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 stamina_heal = tonumber(setting_get("sprint_stamina_heal", "2"))
2019-10-17 16:09:23 +02:00
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("hudbars_autohide_stamina", "true"))
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
2019-10-17 17:00:35 +02:00
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")
2020-04-02 00:13:55 +02:00
local ctrl = player:get_player_control()
if player_stamina < 20 and not ctrl.jump then
player_stamina = math.min(20, player_stamina + stamina_heal)
2019-10-17 16:43:56 +02:00
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()
2019-10-17 17:00:35 +02:00
if player_breath < player:get_properties().breath_max 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
2019-10-17 17:00:35 +02:00
local function is_walkable(ground)
local ground_def = minetest.registered_nodes[ground.name]
return ground_def and (ground_def.walkable and ground_def.liquidtype == "none")
end
local function create_particles(player, name, ground)
2019-10-17 16:43:56 +02:00
local def = minetest.registered_nodes[ground.name]
local tile = def.tiles[1] or def.inventory_image
2019-10-17 17:00:35 +02:00
if type(tile) == "table" then
tile = tile.name
end
if not tile then
2019-10-17 16:43:56 +02:00
return
end
2019-10-17 17:00:35 +02:00
local pos = player:get_pos()
2019-10-17 16:43:56 +02:00
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()
2019-10-17 17:00:35 +02:00
if minetest.get_player_privs(name).fast then
return
end
2019-10-17 16:43:56 +02:00
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
2019-10-17 17:00:35 +02:00
local ground_pos = player:get_pos()
ground_pos.y = math.floor(ground_pos.y)
-- check if player is reasonably near a walkable node
local ground
for _, y_off in ipairs({0, -1, -2}) do
local testpos = vector.add(ground_pos, {x=0, y=y_off, z=0})
local testnode = minetest.get_node_or_nil(testpos)
if testnode ~= nil and is_walkable(testnode) then
ground = testnode
break
end
2019-10-17 16:43:56 +02:00
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
2019-10-17 17:00:35 +02:00
if player_stamina > 0 and hunger > starve_limit and ground then
2019-10-17 16:43:56 +02:00
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
2019-10-17 17:00:35 +02:00
create_particles(player, name, ground)
2019-10-17 16:43:56 +02:00
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)