init.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local player_pos = {}
  2. local player_pos_previous = {}
  3. minetest.register_on_joinplayer(function(player)
  4. local player_name = player:get_player_name()
  5. player_pos_previous[player_name] = { x=0, y=0, z=0 }
  6. end)
  7. minetest.register_on_leaveplayer(function(player)
  8. local player_name = player:get_player_name()
  9. player_pos_previous[player_name] = nil
  10. end)
  11. minetest.override_item("default:dirt_with_grass_footsteps", {
  12. on_timer = function(pos)
  13. local node = minetest.get_node(pos)
  14. node.name = "default:dirt_with_grass"
  15. minetest.add_node(pos, node)
  16. end,
  17. })
  18. -- Chance to change is odd of 100
  19. local odd = 30
  20. local timer = 0
  21. minetest.register_globalstep(function(dtime)
  22. -- Don't check all the time.
  23. timer = timer + dtime
  24. if timer < 5 then return end
  25. timer = 0
  26. for _,player in ipairs(minetest.get_connected_players()) do
  27. local pos = player:get_pos()
  28. local player_name = player:get_player_name()
  29. player_pos[player_name] = { x=math.floor(pos.x+0.5), y=math.floor(pos.y+0.5), z=math.floor(pos.z+0.5) }
  30. local p_ground = { x=math.floor(pos.x+0.5), y=math.floor(pos.y), z=math.floor(pos.z+0.5) }
  31. local n_ground = minetest.get_node(p_ground)
  32. -- Check if position is the previous position
  33. if player_pos_previous[player_name] == nil then
  34. return
  35. end
  36. if player:get_player_control().sneak then
  37. return
  38. end
  39. if player_pos[player_name].x == player_pos_previous[player_name].x and
  40. player_pos[player_name].y == player_pos_previous[player_name].y and
  41. player_pos[player_name].z == player_pos_previous[player_name].z
  42. then
  43. return
  44. end
  45. -- Make footsteps
  46. if math.random(1, 100) <= odd then
  47. if n_ground.name == "default:dirt_with_grass"
  48. or n_ground.name == "default:dirt_with_grass_footsteps" then
  49. minetest.add_node(p_ground,{type="node",name="default:dirt_with_grass_footsteps"})
  50. minetest.get_node_timer(p_ground):start(math.random(3, 48))
  51. end
  52. if n_ground.name == "farming:soil" or n_ground.name == "farming:soil_wet" then
  53. minetest.add_node(p_ground,{type="node",name="default:dirt"})
  54. end
  55. end
  56. -- Make position to previous position
  57. player_pos_previous[player_name] = player_pos[player_name]
  58. end
  59. end)
  60. minetest.register_lbm({
  61. name = "footsteps:convert_footsteps_to_node_timer",
  62. nodenames = {"default:dirt_with_grass_footsteps"},
  63. action = function(pos)
  64. minetest.get_node_timer(pos):start(math.random(3, 48))
  65. end
  66. })