init.lua 7.9 KB

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