hbsprint/init.lua

219 lines
7.6 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:17:44 +02:00
if not sprinting[player:get_player_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
end
2017-07-12 01:07:32 +02:00
end
local function stop_sprint(player)
2019-10-17 16:17:44 +02:00
if sprinting[player:get_player_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
end
2017-07-12 01:07:32 +02:00
end
local function drain_stamina(player)
local player_stamina = tonumber(player:get_meta():get("hbsprint:stamina"))
2017-08-24 08:21:20 +02:00
if player_stamina > 0 then
player:get_meta():set_float("hbsprint:stamina", player_stamina - stamina_drain)
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)
local player_stamina = tonumber(player:get_meta():get("hbsprint:stamina"))
2017-08-24 08:21:20 +02:00
if player_stamina < 20 then
player:get_meta():set_float("hbsprint:stamina", player_stamina + stamina_drain)
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)
if autohide and player_stamina == 20 then hb.hide_hudbar(player, "stamina") end
end
2017-07-12 01:07:32 +02:00
end
local function drain_hunger(player, hunger, name)
2017-08-24 08:21:20 +02:00
if hunger > 0 then
2018-02-25 22:15:18 +01:00
local newhunger = hunger - starve_drain
if starve == "hbhunger" then
hbhunger.hunger[name] = newhunger
hbhunger.set_hunger_raw(player)
elseif starve == "hunger_ng" then
2019-02-10 09:26:50 +01:00
hunger_ng.alter_hunger(name, - starve_drain, "Sprinting")
2018-02-25 22:15:18 +01:00
end
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
player_breath = player_breath - breath_drain
if player_breath > 0 then
player:set_breath(player_breath)
end
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)
2017-08-24 08:21:20 +02:00
if ground and ground.name ~= "air" and ground.name ~= "ignore" then
local def = minetest.registered_nodes[ground.name]
local tile = def.tiles[1] or def.inventory_image or ""
if type(tile) == "string" then
for i = 1, particles do
minetest.add_particle({
pos = {x = pos.x + math.random(-1,1) * math.random() / 2, y = pos.y + 0.1, z = pos.z + math.random(-1,1) * math.random() / 2},
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,
})
end
end
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)
if mod_hudbars and stamina then hb.init_hudbar(player, "stamina", 20, 20, autohide) end
player:get_meta():set_float("hbsprint:stamina", 20)
2017-07-12 01:07:32 +02:00
end)
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
for _,player in ipairs(minetest.get_connected_players()) do
local ctrl = player:get_player_control()
local key_press = false
2019-10-17 16:17:44 +02:00
stamina_timer[player:get_player_name()] = (stamina_timer[player:get_player_name()] or 0) + sprint_timer
breath_timer[player:get_player_name()] = (breath_timer[player:get_player_name()] or 0) + sprint_timer
2019-10-17 16:01:38 +02:00
if dir then
2017-08-24 08:21:20 +02:00
key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right
2019-10-17 16:01:38 +02:00
else
2017-08-24 08:21:20 +02:00
key_press = ctrl.aux1
end
if key_press then
local name = player:get_player_name()
local hunger = 30
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
local player_stamina = tonumber(player:get_meta():get("hbsprint:stamina"))
2018-02-25 22:15:18 +01:00
if starve == "hbhunger" then
2017-08-24 08:21:20 +02:00
hunger = tonumber(hbhunger.hunger[name])
2018-02-25 22:15:18 +01:00
elseif starve == "hunger_ng" then
2019-02-10 09:26:50 +01:00
hunger = hunger_ng.get_hunger_information(name).hunger.exact
2017-08-24 08:21:20 +02:00
end
if ground ~= nil then
local ground_def = minetest.registered_nodes[ground.name]
if ground_def and (minetest.registered_nodes[ground.name].walkable or minetest.registered_nodes[ground.name].liquidtype ~= "none") then
walkable = true
end
2017-08-24 08:21:20 +02:00
end
2018-11-03 10:12:09 +01:00
if player_stamina > 0 and hunger > starve_limit and walkable then
2017-08-24 08:21:20 +02:00
start_sprint(player)
2019-10-17 16:17:44 +02:00
sprinting[name] = true
2017-08-24 08:21:20 +02:00
if stamina then drain_stamina(player) end
if starve then drain_hunger(player, hunger, name) end
if breath then
2019-10-17 16:17:44 +02:00
if breath_timer[name] >= 2 then
2017-08-24 08:21:20 +02:00
drain_breath(player)
2019-10-17 16:17:44 +02:00
breath_timer[name] = 0
2017-08-24 08:21:20 +02:00
end
end
if particles then create_particles(player, name, pos, ground) end
else
stop_sprint(player)
2019-10-17 16:17:44 +02:00
sprinting[name] = false
2017-08-24 08:21:20 +02:00
end
else
stop_sprint(player)
2019-10-17 16:17:44 +02:00
sprinting[player:get_player_name()] = false
if stamina_timer[player:get_player_name()] >= replenish then
2017-08-24 08:21:20 +02:00
if stamina then replenish_stamina(player) end
2019-10-17 16:17:44 +02:00
stamina_timer[player:get_player_name()] = 0
2017-08-24 08:21:20 +02:00
end
end
end
sprint_timer = 0
end
2017-07-12 01:07:32 +02:00
end)