init.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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", "2"))
  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 breath = minetest.is_yes(setting_get("sprint_breath", "true"))
  10. local breath_drain = tonumber(setting_get("sprint_breath_drain", "1"))
  11. local autohide = minetest.is_yes(setting_get("hudbars_autohide_stamina", "true"))
  12. local sprint_timer_step = 0.5
  13. local sprint_timer = 0
  14. local sprinting = {}
  15. local breath_timer = {}
  16. local food_drain = 0.01
  17. -- Functions
  18. local function start_sprint(player)
  19. local name = player:get_player_name()
  20. if not sprinting[name] then
  21. player:set_physics_override({speed = speed, jump = jump})
  22. sprinting[name] = true
  23. end
  24. end
  25. local function stop_sprint(player)
  26. local name = player:get_player_name()
  27. if sprinting[name] then
  28. player:set_physics_override({speed = 1, jump = 1})
  29. sprinting[name] = false
  30. end
  31. end
  32. local function drain_hunger(name)
  33. hbhunger.players[name].hunger = hbhunger.players[name].hunger - food_drain
  34. end
  35. local function drain_breath(player)
  36. local player_breath = player:get_breath()
  37. if player_breath < player:get_properties().breath_max then
  38. player_breath = math.max(0, player_breath - breath_drain)
  39. player:set_breath(player_breath)
  40. end
  41. end
  42. local function is_walkable(ground)
  43. local ground_def = minetest.registered_nodes[ground.name]
  44. return ground_def and (ground_def.walkable and ground_def.liquidtype == "none")
  45. end
  46. local function create_particles(player, name, ground)
  47. local def = minetest.registered_nodes[ground.name]
  48. local tile = def.tiles[1] or def.inventory_image
  49. if type(tile) == "table" then
  50. tile = tile.name
  51. end
  52. if not tile then
  53. return
  54. end
  55. local pos = player:get_pos()
  56. local rand = function() return math.random(-1,1) * math.random() / 2 end
  57. for i = 1, particles do
  58. minetest.add_particle({
  59. pos = {x = pos.x + rand(), y = pos.y + 0.1, z = pos.z + rand()},
  60. velocity = {x = 0, y = 5, z = 0},
  61. acceleration = {x = 0, y = -13, z = 0},
  62. expirationtime = math.random(),
  63. size = math.random() + 0.5,
  64. vertical = false,
  65. texture = tile,
  66. })
  67. end
  68. end
  69. -- Registrations
  70. local function sprint_step(player, dtime)
  71. local name = player:get_player_name()
  72. if breath then
  73. breath_timer[name] = (breath_timer[name] or 0) + dtime
  74. end
  75. local ctrl = player:get_player_control()
  76. local key_press
  77. if dir then
  78. key_press = ctrl.aux1 and ctrl.up and not ctrl.left and not ctrl.right
  79. else
  80. key_press = ctrl.aux1 and (ctrl.up or ctrl.left or ctrl.right or ctrl.down)
  81. end
  82. if not key_press then
  83. stop_sprint(player)
  84. return
  85. end
  86. local ground_pos = player:get_pos()
  87. ground_pos.y = math.floor(ground_pos.y)
  88. -- check if player is reasonably near a walkable node
  89. local ground
  90. for _, y_off in ipairs({0, -1, -2}) do
  91. local testpos = vector.add(ground_pos, {x=0, y=y_off, z=0})
  92. local testnode = minetest.get_node_or_nil(testpos)
  93. if testnode ~= nil and is_walkable(testnode) then
  94. ground = testnode
  95. break
  96. end
  97. end
  98. local starving = false
  99. if minetest.is_yes(minetest.settings:get('enable_damage')) then
  100. starving = hbhunger.players[name].hunger < 10
  101. end
  102. if not starving and ground then
  103. start_sprint(player)
  104. if minetest.settings:get_bool("enable_damage") then
  105. drain_hunger(name)
  106. end
  107. if breath and breath_timer[name] >= 2 then
  108. drain_breath(player)
  109. breath_timer[name] = 0
  110. end
  111. if particles then
  112. create_particles(player, name, ground)
  113. end
  114. else
  115. stop_sprint(player)
  116. end
  117. end
  118. minetest.register_globalstep(function(dtime)
  119. sprint_timer = sprint_timer + dtime
  120. if sprint_timer >= sprint_timer_step then
  121. for _, player in ipairs(minetest.get_connected_players()) do
  122. sprint_step(player, sprint_timer)
  123. end
  124. sprint_timer = 0
  125. end
  126. end)