From 183608546f8c0154642f87c40ff4e16d61ff4fe3 Mon Sep 17 00:00:00 2001 From: tacotexmex Date: Wed, 12 Jul 2017 01:07:32 +0200 Subject: [PATCH] Initial commit --- README.md | 28 +++++ depends.txt | 3 + description.txt | 1 + init.lua | 160 +++++++++++++++++++++++++++++ mod.conf | 1 + settingtypes.txt | 29 ++++++ textures/sprint_stamina_bar.png | Bin 0 -> 71 bytes textures/sprint_stamina_bgicon.png | Bin 0 -> 91 bytes textures/sprint_stamina_icon.png | Bin 0 -> 180 bytes 9 files changed, 222 insertions(+) create mode 100644 README.md create mode 100644 depends.txt create mode 100644 description.txt create mode 100644 init.lua create mode 100644 mod.conf create mode 100644 settingtypes.txt create mode 100644 textures/sprint_stamina_bar.png create mode 100644 textures/sprint_stamina_bgicon.png create mode 100644 textures/sprint_stamina_icon.png diff --git a/README.md b/README.md new file mode 100644 index 0000000..eebff24 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# hbSprint +License: LGPLv3/CC BY-SA 3.0. Particle code Copyright (c) 2017 Elijah Duffy, licensed MIT. + +## Description +A flexible sprint mod supporting stamina, hunger and coexistance with other physics altering mods. + +## Notes +hbSprint can be played with Minetest 0.4.16 or above. +It supports on [hudbars](http://repo.or.cz/w/minetest_hudbars.git), [hbhunger](http://repo.or.cz/w/minetest_hbhunger.git) and [player_monoids](https://github.com/minetest-mods/player_monoids). + +Thanks to [octacian](https://github.com/octacian/sprint/) for particle code. + +## List of features + +- Displays and drains stamina (by default, if hudbars is present). Hides stamina bar if full. +- Displays and drains satiation (by default, if hbhunger is present) +- Requires only forward key to be pressed, not left and right (by default) +- Requires walkable ground (no water sprinting) +- All variables customizable in Advanced settings or directly in minetest.conf + +## Known issues +- Forward double tap support not implemented + +## Bug reports and suggestions +You can report bugs or suggest ideas by [filing an issue](http://github.com/tacotexmex/hbsprint/issues/new). + +## Links +* [GitHub](http://github.com/tacotexmex/hbsprint/) diff --git a/depends.txt b/depends.txt new file mode 100644 index 0000000..fddd574 --- /dev/null +++ b/depends.txt @@ -0,0 +1,3 @@ +player_monoids? +hudbars? +hbhunger? diff --git a/description.txt b/description.txt new file mode 100644 index 0000000..4de59ff --- /dev/null +++ b/description.txt @@ -0,0 +1 @@ +A flexible sprint mod supporting stamina, hunger and monoids. diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..92e93f4 --- /dev/null +++ b/init.lua @@ -0,0 +1,160 @@ +-- Vars + +local speed = tonumber(minetest.settings:get("sprint_speed")) or 1.3 +local jump = tonumber(minetest.settings:get("sprint_jump")) or 1.1 +local key = minetest.settings:get("sprint_key") or "Use" +local dir = minetest.settings:get_bool("sprint_forward_only") +local particles = tonumber(minetest.settings:get("sprint_particles")) or 2 +local stamina = minetest.settings:get_bool("sprint_stamina") or true +local stamina_drain = tonumber(minetest.settings:get("sprint_stamina_drain")) or 2 +local replenish = tonumber(minetest.settings:get("sprint_stamina_replenish")) or 2 +local starve = minetest.settings:get_bool("sprint_starve") or true +local starve_drain = minetest.settings:get("sprint_starve_drain") or 0.5 + +local sprint_timer_step = 0.5 +local sprint_timer = 0 +local player_stamina = 20 +local stamina_timer = 0 + +if dir == nil then dir = true end +if stamina ~= false then stamina = true end +if starve == nil then starve = true end +if not minetest.get_modpath("hudbars") then hudbars = false end +if not minetest.get_modpath("hbhunger") then starve = false end +if not minetest.get_modpath("player_monoids") then monoids = false end + +-- Functions + +local function start_sprint(player) + if monoids then + player_monoids.speed:add_change(player, speed, "sprint:sprint") + player_monoids.jump:add_change(player, jump, "sprint:jump") + else + player:set_physics_override({speed = speed, jump = jump}) + end +end + +local function stop_sprint(player) + if monoids then + player_monoids.speed:del_change(player, "sprint:sprint") + player_monoids.jump:del_change(player, "sprint:jump") + else + player:set_physics_override({speed = 1, jump = 1}) + end +end + +local function drain_stamina(player) + player_stamina = tonumber(player:get_attribute("stamina")) + if player_stamina > 0 then + player:set_attribute("stamina", player_stamina - stamina_drain) + end + if hudbars then + if player_stamina < 20 then hb.unhide_hudbar(player, "stamina") end + hb.change_hudbar(player, "stamina", player_stamina) + end +end + +local function replenish_stamina(player) + player_stamina = tonumber(player:get_attribute("stamina")) + if player_stamina < 20 then + player:set_attribute("stamina", player_stamina + stamina_drain) + end + if hudbars then + hb.change_hudbar(player, "stamina", player_stamina) + if player_stamina == 20 then hb.hide_hudbar(player, "stamina") end + end +end + +local function drain_hunger(player, hunger, name) + if hunger > 0 then + hbhunger.hunger[name] = hunger - starve_drain + hbhunger.set_hunger_raw(player) + end +end + +local function create_particles(player, name, pos, ground) + 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 +end + +-- Registrations + +if minetest.get_modpath("hudbars") ~= nil and stamina then + hb.register_hudbar("stamina", + 0xFFFFFF, + "Stamina", + { bar = "sprint_stamina_bar.png", icon = "sprint_stamina_icon.png" }, + player_stamina, player_stamina, + false, "%s: %.1f/%.1f") + hudbars = true + hb.hide_hudbar(player, "stamina") +end + +minetest.register_on_joinplayer(function(player) + if hudbars and stamina then hb.init_hudbar(player, "stamina") end + player:set_attribute("stamina", 20) +end) + +minetest.register_globalstep(function(dtime) + sprint_timer = sprint_timer + dtime + stamina_timer = stamina_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 + if key == "Use" and dir then + key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right + elseif key == "Use" and not dir then + key_press = ctrl.aux1 + end + + -- if key == "W" and dir then + -- key_press = ctrl.aux1 and ctrl.up or key_press and ctrl.up + -- elseif key == "W" then + -- key_press = ctrl.aux1 or key_press and key_tap + -- 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 + if starve then + hunger = tonumber(hbhunger.hunger[name]) + end + if ground ~= nil then + walkable = minetest.registered_nodes[ground.name].walkable + end + if player_stamina > 0 and hunger > 9 and walkable then --AND IF NOT WATER! + start_sprint(player) + if stamina then drain_stamina(player) end + if starve then drain_hunger(player, hunger, name) end + if particles then create_particles(player, name, pos, ground) end + end + else + stop_sprint(player) + if stamina_timer >= replenish then + if stamina then replenish_stamina(player) end + stamina_timer = 0 + end + end + end + sprint_timer = 0 + end +end) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..4433f1a --- /dev/null +++ b/mod.conf @@ -0,0 +1 @@ +name = hbsprint diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..1eaf0ad --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,29 @@ +#Sprint speed multiplier +sprint_speed (Sprint speed multiplier) float 1.3 + +#Sprint jump multiplier +sprint_jump (Sprint jump multiplier) float 1.1 + +#Use a key to sprint +sprint_key (Sprint key) enum Use Use + +#Require player to move forward only to be able to sprint +sprint_forward_only (Sprint forward only) bool true + +#The amount of particles to spawn behind a sprinting player +sprint_particles (Particles) float 2 + +#Drain stamina while sprinting +sprint_stamina (Stamina) bool true + +#The amount of stamina to drain while sprinting +sprint_stamina_drain (Stamina drain) float 2 + +#The amount of seconds before starting to replenish stamina +sprint_stamina_replenish (Stamina replenish) float 2 + +#Drain satiation while sprinting +sprint_starve (Starve) bool true + +#The amount of satiation to drain while sprinting +sprint_starve_drain (Starve drain) float 0.5 diff --git a/textures/sprint_stamina_bar.png b/textures/sprint_stamina_bar.png new file mode 100644 index 0000000000000000000000000000000000000000..55e1462c531d641cf99a51d2274d4d94e1411f2a GIT binary patch literal 71 zcmeAS@N?(olHy`uVBq!ia0vp^Oh7Ea0U|e_vV07r_&i-4Ln;`P1!l85l`Lj>pCi`T TRH;$`lwt66^>bP0l+XkK-v5Os%ftNG+r*>WbxkavgLN|KV|pL`l5YaV!|(n+NBMjY#q3muTEvq z>g(u;ty8#N<8bnn=Z=UY^L-!rvK1ab{{O)3$F|H*wpa=7{?lM>;83<$c;jmB4}FY6 f|Gzi9+0FQoyZ)m6)ElWlXE1oW`njxgN@xNA8ih&2 literal 0 HcmV?d00001