init.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ambience = {}
  2. -- override default water sounds
  3. minetest.override_item("default:water_source", { sounds = {} })
  4. minetest.override_item("default:water_flowing", { sounds = {} })
  5. minetest.override_item("default:river_water_source", { sounds = {} })
  6. minetest.override_item("default:river_water_flowing", { sounds = {} })
  7. -- settings
  8. local SOUNDVOLUME = 1.0
  9. local MUSICVOLUME = 1.0
  10. local play_music = minetest.settings:get_bool("ambience_music") ~= false
  11. local pplus = minetest.get_modpath("playerplus")
  12. local radius = 6
  13. local playing = {}
  14. local sound_sets = {} -- all the sounds and their settings
  15. local sound_set_order = {} -- needed because pairs loops randomly through tables
  16. local set_nodes = {} -- all the nodes needed for sets
  17. -- global functions
  18. -- add set to list
  19. ambience.add_set = function(set_name, def)
  20. if not set_name or not def then
  21. return
  22. end
  23. sound_sets[set_name] = {
  24. frequency = def.frequency or 50,
  25. sounds = def.sounds,
  26. sound_check = def.sound_check,
  27. nodes = def.nodes
  28. }
  29. -- add set name to the sound_set_order table
  30. local can_add = true
  31. for i = 1, #sound_set_order do
  32. if sound_set_order[i] == set_name then
  33. can_add = false
  34. end
  35. end
  36. if can_add then
  37. table.insert(sound_set_order, set_name)
  38. end
  39. -- add any missing nodes to the set_nodes table
  40. if def.nodes then
  41. for i = 1, #def.nodes do
  42. can_add = def.nodes[i]
  43. for j = 1, #set_nodes do
  44. if def.nodes[i] == set_nodes[j] then
  45. can_add = false
  46. end
  47. end
  48. if can_add then
  49. table.insert(set_nodes, can_add)
  50. end
  51. end
  52. end
  53. end
  54. -- return set from list using name
  55. ambience.get_set = function(set_name)
  56. if sound_sets[set_name] then
  57. return sound_sets[set_name]
  58. end
  59. end
  60. -- remove set from list
  61. ambience.del_set = function(set_name)
  62. sound_sets[set_name] = nil
  63. local can_del = false
  64. for i = 1, #sound_set_order do
  65. if sound_set_order[i] == set_name then
  66. can_del = i
  67. end
  68. end
  69. if can_del then
  70. table.remove(sound_set_order, can_del)
  71. end
  72. end
  73. -- plays music and selects sound set
  74. local get_ambience = function(player, tod, name)
  75. -- play server or local music if available
  76. if play_music and playing[name] then
  77. -- play at midnight
  78. if tod >= 0.0 and tod <= 0.01 then
  79. if not playing[name].music then
  80. playing[name].music = minetest.sound_play("ambience_music", {
  81. to_player = player:get_player_name(),
  82. gain = MUSICVOLUME
  83. })
  84. end
  85. elseif tod > 0.1 and playing[name].music then
  86. playing[name].music = nil
  87. end
  88. end
  89. -- get foot and head level nodes at player position
  90. local pos = player:get_pos()
  91. pos.y = pos.y + 1.4 -- head level
  92. local nod_head = pplus and playerplus[name].nod_head or minetest.get_node(pos).name
  93. pos.y = pos.y - 1.2 -- foot level
  94. local nod_feet = pplus and playerplus[name].nod_feet or minetest.get_node(pos).name
  95. pos.y = pos.y - 0.2 -- reset pos
  96. -- get all set nodes around player
  97. local ps, cn = minetest.find_nodes_in_area(
  98. {x = pos.x - radius, y = pos.y - radius, z = pos.z - radius},
  99. {x = pos.x + radius, y = pos.y + radius, z = pos.z + radius}, set_nodes)
  100. -- loop through sets in order and choose first that meets it's conditions
  101. for n = 1, #sound_set_order do
  102. local set = sound_sets[ sound_set_order[n] ]
  103. if set and set.sound_check then
  104. -- pass settings to function for condition check
  105. local set_name, gain = set.sound_check({
  106. player = player,
  107. pos = pos,
  108. tod = tod,
  109. totals = cn,
  110. positions = ps,
  111. head_node = nod_head,
  112. feet_node = nod_feet
  113. })
  114. -- if conditions met return set name and gain value
  115. if set_name then
  116. return set_name, gain
  117. end
  118. end
  119. end
  120. end
  121. local timer = 0
  122. local random = math.random
  123. -- players routine
  124. minetest.register_globalstep(function(dtime)
  125. -- one second timer
  126. timer = timer + dtime
  127. if timer < 1 then return end
  128. timer = 0
  129. -- get list of players and set some variables
  130. local players = minetest.get_connected_players()
  131. local player_name, number, chance, ambience, handler, ok
  132. local tod = minetest.get_timeofday()
  133. -- loop through players
  134. for n = 1, #players do
  135. player_name = players[n]:get_player_name()
  136. --local t1 = os.clock()
  137. local set_name, MORE_GAIN = get_ambience(players[n], tod, player_name)
  138. --print(string.format("elapsed time: %.4f\n", os.clock() - t1))
  139. ok = true -- everything starts off ok
  140. -- stop current sound if another set active or gain changed
  141. if playing[player_name]
  142. and playing[player_name].handler then
  143. if playing[player_name].set ~= set_name
  144. or (playing[player_name].set == set_name
  145. and playing[player_name].gain ~= MORE_GAIN) then
  146. --print ("-- change stop", set_name, playing[player_name].old_handler)
  147. minetest.sound_stop(playing[player_name].old_handler)
  148. playing[player_name].set = nil
  149. playing[player_name].handler = nil
  150. playing[player_name].gain = nil
  151. else
  152. ok = false -- sound set still playing, skip new sound
  153. end
  154. end
  155. -- set random chance and reset seed
  156. chance = random(1, 1000)
  157. math.randomseed(tod + chance)
  158. -- if chance is lower than set frequency then select set
  159. if ok and set_name and chance < sound_sets[set_name].frequency then
  160. -- choose random sound from set
  161. number = random(#sound_sets[set_name].sounds)
  162. ambience = sound_sets[set_name].sounds[number]
  163. -- play sound
  164. handler = minetest.sound_play(ambience.name, {
  165. to_player = player_name,
  166. gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * SOUNDVOLUME,
  167. pitch = ambience.pitch or 1.0
  168. }, ambience.ephemeral)
  169. --print ("playing... " .. ambience.name .. " (" .. chance .. " < "
  170. -- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN, handler)
  171. -- only continue if sound playing returns handler
  172. if handler then
  173. --print("-- current handler", handler)
  174. -- set what player is currently listening to
  175. playing[player_name] = {
  176. set = set_name, gain = MORE_GAIN,
  177. handler = handler, old_handler = handler
  178. }
  179. -- set timer to stop sound
  180. minetest.after(ambience.length, function()
  181. --print("-- after", set_name, handler)
  182. -- make sure we are stopping same sound we started
  183. if playing[player_name]
  184. and playing[player_name].handler
  185. and playing[player_name].old_handler == handler then
  186. --print("-- timed stop", set_name, handler)
  187. --minetest.sound_stop(playing[player_name].handler)
  188. minetest.sound_stop(handler)
  189. -- reset player variables and backup handler
  190. playing[player_name] = {
  191. set = nil, gain = nil,
  192. handler = nil, old_handler = nil
  193. }
  194. end
  195. end)
  196. end
  197. end
  198. end
  199. end)
  200. -- sound volume command
  201. minetest.register_chatcommand("svol", {
  202. params = "<svol>",
  203. description = "set sound volume (0.1 to 1.0)",
  204. privs = {server = true},
  205. func = function(name, param)
  206. SOUNDVOLUME = tonumber(param) or SOUNDVOLUME
  207. if SOUNDVOLUME < 0.1 then SOUNDVOLUME = 0.1 end
  208. if SOUNDVOLUME > 1.0 then SOUNDVOLUME = 1.0 end
  209. return true, "Sound volume set to " .. SOUNDVOLUME
  210. end,
  211. })
  212. -- music volume command (0 stops music)
  213. minetest.register_chatcommand("mvol", {
  214. params = "<mvol>",
  215. description = "set music volume (0.1 to 1.0)",
  216. privs = {server = true},
  217. func = function(name, param)
  218. MUSICVOLUME = tonumber(param) or MUSICVOLUME
  219. -- ability to stop music just as it begins
  220. if MUSICVOLUME == 0 and playing[name].music then
  221. minetest.sound_stop(playing[name].music)
  222. end
  223. if MUSICVOLUME < 0.1 then MUSICVOLUME = 0.1 end
  224. if MUSICVOLUME > 1.0 then MUSICVOLUME = 1.0 end
  225. return true, "Music volume set to " .. MUSICVOLUME
  226. end,
  227. })
  228. -- load default sound sets
  229. dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
  230. print("[MOD] Ambience Lite loaded")