A flexible sprint mod supporting stamina, hunger and monoids.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.2 KiB

  1. -- Vars
  2. local function setting_get(name, default)
  3. return minetest.settings:get(name) or default
  4. end
  5. local speed = tonumber(setting_get("sprint_speed", "1.3"))
  6. local jump = tonumber(setting_get("sprint_jump", "1.1"))
  7. local dir = minetest.is_yes(setting_get("sprint_forward_only", "false"))
  8. local particles = tonumber(setting_get("sprint_particles", "2"))
  9. local stamina = minetest.is_yes(setting_get("sprint_stamina", "true"))
  10. local stamina_drain = tonumber(setting_get("sprint_stamina_drain", "2"))
  11. local stamina_heal = tonumber(setting_get("sprint_stamina_heal", "2"))
  12. local standing = tonumber(setting_get("sprint_stamina_standing", "2.5"))
  13. local replenish = tonumber(setting_get("sprint_stamina_replenish", "2"))
  14. local starve = minetest.is_yes(setting_get("sprint_starve", "true"))
  15. local starve_drain = tonumber(setting_get("sprint_starve_drain", "0.5"))
  16. local starve_limit = tonumber(setting_get("sprint_starve_limit", "6"))
  17. local breath = minetest.is_yes(setting_get("sprint_breath", "true"))
  18. local breath_drain = tonumber(setting_get("sprint_breath_drain", "1"))
  19. local autohide = minetest.is_yes(setting_get("hudbars_autohide_stamina", "true"))
  20. local sprint_timer_step = 0.5
  21. local sprint_timer = 0
  22. local sprinting = {}
  23. local stamina_timer = {}
  24. local breath_timer = {}
  25. local mod_hudbars = minetest.get_modpath("hudbars") ~= nil
  26. local mod_player_monoids = minetest.get_modpath("player_monoids") ~= nil
  27. local mod_playerphysics = minetest.get_modpath("playerphysics") ~= nil
  28. if starve then
  29. if minetest.get_modpath("hbhunger") then
  30. starve = "hbhunger"
  31. elseif minetest.get_modpath("hunger_ng") then
  32. starve = "hunger_ng"
  33. else
  34. starve = false
  35. end
  36. end
  37. if minetest.settings:get_bool("creative_mode") then
  38. starve = false
  39. end
  40. -- Functions
  41. local function start_sprint(player)
  42. local name = player:get_player_name()
  43. if not sprinting[name] then
  44. if mod_player_monoids then
  45. player_monoids.speed:add_change(player, speed, "hbsprint:speed")
  46. player_monoids.jump:add_change(player, jump, "hbsprint:jump")
  47. elseif mod_playerphysics then
  48. playerphysics.add_physics_factor(player, "speed", "hbsprint:speed", speed)
  49. playerphysics.add_physics_factor(player, "jump", "hbsprint:jump", jump)
  50. else
  51. player:set_physics_override({speed = speed, jump = jump})
  52. end
  53. sprinting[name] = true
  54. end
  55. end
  56. local function stop_sprint(player)
  57. local name = player:get_player_name()
  58. if sprinting[name] then
  59. if mod_player_monoids then
  60. player_monoids.speed:del_change(player, "hbsprint:speed")
  61. player_monoids.jump:del_change(player, "hbsprint:jump")
  62. elseif mod_playerphysics then
  63. playerphysics.remove_physics_factor(player, "speed", "hbsprint:speed")
  64. playerphysics.remove_physics_factor(player, "jump", "hbsprint:jump")
  65. else
  66. player:set_physics_override({speed = 1, jump = 1})
  67. end
  68. sprinting[name] = false
  69. end
  70. end
  71. local function drain_stamina(player)
  72. local player_stamina = player:get_meta():get_float("hbsprint:stamina")
  73. if player_stamina > 0 then
  74. player_stamina = math.max(0, player_stamina - stamina_drain)
  75. player:get_meta():set_float("hbsprint:stamina", player_stamina)
  76. end
  77. if mod_hudbars then
  78. if autohide and player_stamina < 20 then hb.unhide_hudbar(player, "stamina") end
  79. hb.change_hudbar(player, "stamina", player_stamina)
  80. end
  81. end
  82. local function replenish_stamina(player)
  83. local player_stamina = player:get_meta():get_float("hbsprint:stamina")
  84. local ctrl = player:get_player_control()
  85. if player_stamina < 20 and not ctrl.jump then
  86. if not ctrl.right and not ctrl.left and not ctrl.down and not ctrl.up and not ctrl.LMB and not ctrl.RMB then
  87. player_stamina = math.min(20, player_stamina + standing)
  88. else
  89. player_stamina = math.min(20, player_stamina + stamina_heal)
  90. end
  91. player:get_meta():set_float("hbsprint:stamina", player_stamina)
  92. end
  93. if mod_hudbars then
  94. hb.change_hudbar(player, "stamina", player_stamina)
  95. if autohide and player_stamina >= 20 then hb.hide_hudbar(player, "stamina") end
  96. end
  97. end
  98. local function drain_hunger(player, name)
  99. if starve == "hbhunger" then
  100. local hunger = tonumber(hbhunger.hunger[name]) - starve_drain
  101. hbhunger.hunger[name] = math.max(0, hunger)
  102. hbhunger.set_hunger_raw(player)
  103. elseif starve == "hunger_ng" then
  104. hunger_ng.alter_hunger(name, -starve_drain, "Sprinting")
  105. end
  106. end
  107. local function drain_breath(player)
  108. local player_breath = player:get_breath()
  109. if player_breath < player:get_properties().breath_max then
  110. player_breath = math.max(0, player_breath - breath_drain)
  111. player:set_breath(player_breath)
  112. end
  113. end
  114. local function is_walkable(ground)
  115. local ground_def = minetest.registered_nodes[ground.name]
  116. return ground_def and (ground_def.walkable and ground_def.liquidtype == "none")
  117. end
  118. local function create_particles(player, name, ground)
  119. local def = minetest.registered_nodes[ground.name]
  120. local tile = def.tiles[1] or def.inventory_image
  121. if type(tile) == "table" then
  122. tile = tile.name
  123. end
  124. if not tile then
  125. return
  126. end
  127. local pos = player:get_pos()
  128. local rand = function() return math.random(-1,1) * math.random() / 2 end
  129. for i = 1, particles do
  130. minetest.add_particle({
  131. pos = {x = pos.x + rand(), y = pos.y + 0.1, z = pos.z + rand()},
  132. velocity = {x = 0, y = 5, z = 0},
  133. acceleration = {x = 0, y = -13, z = 0},
  134. expirationtime = math.random(),
  135. size = math.random() + 0.5,
  136. vertical = false,
  137. texture = tile,
  138. })
  139. end
  140. end
  141. -- Registrations
  142. if mod_hudbars and stamina then
  143. hb.register_hudbar(
  144. "stamina",
  145. 0xFFFFFF,
  146. "Stamina",
  147. {
  148. bar = "sprint_stamina_bar.png",
  149. icon = "sprint_stamina_icon.png",
  150. bgicon = "sprint_stamina_bgicon.png"
  151. },
  152. 20,
  153. 20,
  154. autohide)
  155. end
  156. minetest.register_on_joinplayer(function(player)
  157. if stamina then
  158. if mod_hudbars then
  159. hb.init_hudbar(player, "stamina", 20, 20, autohide)
  160. end
  161. player:get_meta():set_float("hbsprint:stamina", 20)
  162. end
  163. end)
  164. local function sprint_step(player, dtime)
  165. local name = player:get_player_name()
  166. local fast = minetest.get_player_privs(name).fast
  167. if not fast then
  168. if stamina then
  169. stamina_timer[name] = (stamina_timer[name] or 0) + dtime
  170. end
  171. if breath then
  172. breath_timer[name] = (breath_timer[name] or 0) + dtime
  173. end
  174. end
  175. local ctrl = player:get_player_control()
  176. local key_press
  177. if dir then
  178. key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right
  179. else
  180. key_press = ctrl.aux1 and (ctrl.up or ctrl.left or ctrl.right or ctrl.down)
  181. end
  182. if not key_press then
  183. stop_sprint(player)
  184. if stamina and not fast and stamina_timer[name] >= replenish then
  185. replenish_stamina(player)
  186. stamina_timer[name] = 0
  187. end
  188. return
  189. end
  190. local ground_pos = player:get_pos()
  191. ground_pos.y = math.floor(ground_pos.y)
  192. -- check if player is reasonably near a walkable node
  193. local ground
  194. for _, y_off in ipairs({0, -1, -2}) do
  195. local testpos = vector.add(ground_pos, {x=0, y=y_off, z=0})
  196. local testnode = minetest.get_node_or_nil(testpos)
  197. if testnode ~= nil and is_walkable(testnode) then
  198. ground = testnode
  199. break
  200. end
  201. end
  202. local player_stamina = 1
  203. if stamina then
  204. player_stamina = player:get_meta():get_float("hbsprint:stamina")
  205. end
  206. local hunger = 30
  207. if starve == "hbhunger" then
  208. hunger = tonumber(hbhunger.hunger[name])
  209. elseif starve == "hunger_ng" then
  210. hunger = hunger_ng.get_hunger_information(name).hunger.exact
  211. end
  212. if (player_stamina > 0 and hunger > starve_limit and ground) or fast then
  213. start_sprint(player)
  214. if stamina and not fast then drain_stamina(player) end
  215. if starve then drain_hunger(player, name) end
  216. if breath and not fast and breath_timer[name] >= 2 then
  217. drain_breath(player)
  218. breath_timer[name] = 0
  219. end
  220. if particles and ground then
  221. create_particles(player, name, ground)
  222. end
  223. else
  224. stop_sprint(player)
  225. end
  226. end
  227. minetest.register_globalstep(function(dtime)
  228. sprint_timer = sprint_timer + dtime
  229. if sprint_timer >= sprint_timer_step then
  230. for _, player in ipairs(minetest.get_connected_players()) do
  231. sprint_step(player, sprint_timer)
  232. end
  233. sprint_timer = 0
  234. end
  235. end)
  236. minetest.log("action", "[hbsprint] loaded.")