init.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. -- Parameters
  2. local radius = 8 -- Water node search radius around player
  3. -- End of parameters
  4. local river_source_sounds = minetest.settings:get_bool("river_source_sounds")
  5. -- Update sound for player
  6. local function update_sound(player)
  7. local player_name = player:get_player_name()
  8. local ppos = player:get_pos()
  9. local areamin = vector.subtract(ppos, radius)
  10. local areamax = vector.add(ppos, radius)
  11. local water_nodes = {"default:water_flowing", "default:river_water_flowing"}
  12. if river_source_sounds then
  13. table.insert(water_nodes, "default:river_water_source")
  14. end
  15. local wpos, _ = minetest.find_nodes_in_area(areamin, areamax, water_nodes)
  16. local waters = #wpos
  17. if waters == 0 then
  18. return
  19. end
  20. -- Find average position of water positions
  21. local wposav = vector.new()
  22. for _, pos in ipairs(wpos) do
  23. wposav.x = wposav.x + pos.x
  24. wposav.y = wposav.y + pos.y
  25. wposav.z = wposav.z + pos.z
  26. end
  27. wposav = vector.divide(wposav, waters)
  28. minetest.sound_play(
  29. "env_sounds_water",
  30. {
  31. pos = wposav,
  32. to_player = player_name,
  33. gain = math.min(0.04 + waters * 0.004, 0.4),
  34. }
  35. )
  36. end
  37. -- Update sound 'on joinplayer'
  38. minetest.register_on_joinplayer(function(player)
  39. update_sound(player)
  40. end)
  41. -- Cyclic sound update
  42. local function cyclic_update()
  43. for _, player in pairs(minetest.get_connected_players()) do
  44. update_sound(player)
  45. end
  46. minetest.after(3.5, cyclic_update)
  47. end
  48. minetest.after(0, cyclic_update)