init.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. ambience = {}
  2. -- settings
  3. local SOUNDVOLUME = 1.0
  4. local MUSICVOLUME = 0.6
  5. local play_music = minetest.settings:get_bool("ambience_music") ~= false
  6. local pplus = minetest.get_modpath("playerplus")
  7. local radius = 6
  8. local playing = {}
  9. local sound_sets = {} -- all the sounds and their settings
  10. local sound_set_order = {} -- needed because pairs loops randomly through tables
  11. local set_nodes = {} -- all the nodes needed for sets
  12. local is_50 = minetest.has_feature("object_use_texture_alpha")
  13. -- add set to list
  14. ambience.add_set = function(set_name, def)
  15. if not set_name or not def then
  16. return
  17. end
  18. sound_sets[set_name] = {
  19. frequency = def.frequency or 50,
  20. sounds = def.sounds,
  21. sound_check = def.sound_check,
  22. nodes = def.nodes
  23. }
  24. -- add set name to the sound_set_order table
  25. local can_add = true
  26. for i = 1, #sound_set_order do
  27. if sound_set_order[i] == set_name then
  28. can_add = false
  29. end
  30. end
  31. if can_add then
  32. table.insert(sound_set_order, set_name)
  33. end
  34. -- add any missing nodes to the set_nodes table
  35. if def.nodes then
  36. for i = 1, #def.nodes do
  37. can_add = def.nodes[i]
  38. for j = 1, #set_nodes do
  39. if def.nodes[i] == set_nodes[j] then
  40. can_add = false
  41. end
  42. end
  43. if can_add then
  44. table.insert(set_nodes, can_add)
  45. end
  46. end
  47. end
  48. end
  49. -- return set from list using name
  50. ambience.get_set = function(set_name)
  51. return sound_sets[set_name]
  52. end
  53. -- remove set from list
  54. ambience.del_set = function(set_name)
  55. sound_sets[set_name] = nil
  56. local can_del = false
  57. for i = 1, #sound_set_order do
  58. if sound_set_order[i] == set_name then
  59. can_del = i
  60. end
  61. end
  62. if can_del then
  63. table.remove(sound_set_order, can_del)
  64. end
  65. end
  66. -- setup table when player joins
  67. minetest.register_on_joinplayer(function(player)
  68. if player then
  69. local name = player:get_player_name()
  70. playing[name] = {music = -1}
  71. local mvol, svol
  72. if is_50 then
  73. local meta = player:get_meta()
  74. mvol = meta:get_string("ambience.mvol")
  75. svol = meta:get_string("ambience.svol")
  76. else
  77. mvol = player:get_attribute("ambience.mvol")
  78. svol = player:get_attribute("ambience.svol")
  79. end
  80. mvol = tonumber(mvol) or MUSICVOLUME
  81. svol = tonumber(svol) or SOUNDVOLUME
  82. playing[name].mvol = mvol
  83. playing[name].svol = svol
  84. end
  85. end)
  86. -- remove table when player leaves
  87. minetest.register_on_leaveplayer(function(player)
  88. if player then
  89. playing[player:get_player_name()] = nil
  90. end
  91. end)
  92. -- plays music and selects sound set
  93. local get_ambience = function(player, tod, name)
  94. -- play server or local music if music enabled and music not already playing
  95. if play_music
  96. and playing[name] and playing[name].music < 0
  97. and playing[name].mvol > 0 then
  98. -- count backwards
  99. playing[name].music = playing[name].music -1
  100. -- play music every 20 minutes
  101. if playing[name].music < -(60 * 20) then
  102. playing[name].music = minetest.sound_play("ambience_music", {
  103. to_player = name,
  104. gain = playing[name].mvol
  105. })
  106. -- reset music timer after 10 minutes
  107. minetest.after(60 * 10, function(name)
  108. if playing[name] then
  109. playing[name].music = -1
  110. end
  111. end, name)
  112. end
  113. --print("-- music count", playing[name].music)
  114. end
  115. -- get foot and head level nodes at player position
  116. local pos = player:get_pos() ; if not pos then return end
  117. local prop = player:get_properties()
  118. local eyeh = prop.eye_height or 1.47 -- eye level with fallback
  119. pos.y = pos.y + eyeh
  120. local nod_head = pplus and name and playerplus[name]
  121. and playerplus[name].nod_head or minetest.get_node(pos).name
  122. pos.y = (pos.y - eyeh) + 0.2 -- foot level
  123. local nod_feet = pplus and name and playerplus[name]
  124. and playerplus[name].nod_feet or minetest.get_node(pos).name
  125. pos.y = pos.y - 0.2 -- reset pos
  126. -- get all set nodes around player
  127. local ps, cn = minetest.find_nodes_in_area(
  128. {x = pos.x - radius, y = pos.y - radius, z = pos.z - radius},
  129. {x = pos.x + radius, y = pos.y + radius, z = pos.z + radius}, set_nodes)
  130. -- loop through sets in order and choose first that meets it's conditions
  131. for n = 1, #sound_set_order do
  132. local set = sound_sets[ sound_set_order[n] ]
  133. if set and set.sound_check then
  134. -- pass settings to function for condition check
  135. local set_name, gain = set.sound_check({
  136. player = player,
  137. pos = pos,
  138. tod = tod,
  139. totals = cn,
  140. positions = ps,
  141. head_node = nod_head,
  142. feet_node = nod_feet
  143. })
  144. -- if conditions met return set name and gain value
  145. if set_name then
  146. return set_name, gain
  147. end
  148. end
  149. end
  150. return nil, nil -- ADDED
  151. end
  152. local timer = 0
  153. local random = math.random
  154. -- players routine
  155. minetest.register_globalstep(function(dtime)
  156. -- one second timer
  157. timer = timer + dtime
  158. if timer < 1 then return end
  159. timer = 0
  160. local player_name, number, chance, ambience, handler, ok
  161. local tod = minetest.get_timeofday()
  162. -- loop through players
  163. for _, player in pairs(minetest.get_connected_players()) do
  164. player_name = player:get_player_name()
  165. --local t1 = os.clock()
  166. local set_name, MORE_GAIN = get_ambience(player, tod, player_name)
  167. --print(string.format("elapsed time: %.4f\n", os.clock() - t1))
  168. ok = playing[player_name] -- everything starts off ok if player found
  169. -- are we playing something already?
  170. if ok and playing[player_name].handler then
  171. -- stop current sound if another set active or gain changed
  172. if playing[player_name].set ~= set_name
  173. or playing[player_name].gain ~= MORE_GAIN then
  174. --print ("-- change stop", set_name, playing[player_name].handler)
  175. minetest.sound_stop(playing[player_name].handler)
  176. playing[player_name].set = nil
  177. playing[player_name].gain = nil
  178. playing[player_name].handler = nil
  179. else
  180. ok = false -- sound set still playing, skip new sound
  181. end
  182. end
  183. -- set random chance
  184. chance = random(1000)
  185. -- if chance is lower than set frequency then select set
  186. if ok and set_name and chance < sound_sets[set_name].frequency then
  187. -- choose random sound from set
  188. number = random(#sound_sets[set_name].sounds)
  189. ambience = sound_sets[set_name].sounds[number]
  190. -- play sound
  191. handler = minetest.sound_play(ambience.name, {
  192. to_player = player_name,
  193. gain = ((ambience.gain or 0.3) + (MORE_GAIN or 0)) * playing[player_name].svol,
  194. pitch = ambience.pitch or 1.0
  195. }, ambience.ephemeral)
  196. --print ("playing... " .. ambience.name .. " (" .. chance .. " < "
  197. -- .. sound_sets[set_name].frequency .. ") @ ", MORE_GAIN, handler)
  198. -- only continue if sound playing returns handler
  199. if handler then
  200. --print("-- current handler", handler)
  201. -- set what player is currently listening to
  202. playing[player_name].set = set_name
  203. playing[player_name].gain = MORE_GAIN
  204. playing[player_name].handler = handler
  205. -- set timer to stop sound
  206. minetest.after(ambience.length, function(handler, player_name)
  207. --print("-- timed stop", set_name, handler)
  208. if handler then
  209. minetest.sound_stop(handler)
  210. end
  211. -- reset variables if handlers match
  212. if playing[player_name]
  213. and playing[player_name].handler == handler then
  214. --print("-- timed reset", handler, player_name)
  215. playing[player_name].set = nil
  216. playing[player_name].gain = nil
  217. playing[player_name].handler = nil
  218. end
  219. end, handler, player_name)
  220. end
  221. end
  222. end
  223. end)
  224. -- sound volume command
  225. minetest.register_chatcommand("svol", {
  226. params = "<svol>",
  227. description = "set sound volume (0.1 to 1.0)",
  228. privs = {},
  229. func = function(name, param)
  230. local svol = tonumber(param) or playing[name].svol
  231. if svol < 0.1 then svol = 0.1 end
  232. if svol > 1.0 then svol = 1.0 end
  233. if is_50 then
  234. local player = minetest.get_player_by_name(name)
  235. local meta = player:get_meta()
  236. meta:set_string("ambience.svol", svol)
  237. else
  238. player:set_attribute("ambience.svol", svol)
  239. end
  240. playing[name].svol = svol
  241. return true, "Sound volume set to " .. svol
  242. end
  243. })
  244. -- music volume command (0 stops music)
  245. minetest.register_chatcommand("mvol", {
  246. params = "<mvol>",
  247. description = "set music volume (0.1 to 1.0, 0 to stop music)",
  248. privs = {},
  249. func = function(name, param)
  250. local mvol = tonumber(param) or playing[name].mvol
  251. -- ability to stop music by setting volume to 0
  252. if mvol == 0 and playing[name].music
  253. and playing[name].music >= 0 then
  254. minetest.sound_stop(playing[name].music)
  255. playing[name].music = -1
  256. end
  257. if mvol < 0 then mvol = 0 end
  258. if mvol > 1.0 then mvol = 1.0 end
  259. if is_50 then
  260. local player = minetest.get_player_by_name(name)
  261. local meta = player:get_meta()
  262. meta:set_string("ambience.mvol", mvol)
  263. else
  264. player:set_attribute("ambience.mvol", mvol)
  265. end
  266. playing[name].mvol = mvol
  267. return true, "Music volume set to " .. mvol
  268. end
  269. })
  270. -- load default sound sets
  271. dofile(minetest.get_modpath("ambience") .. "/soundsets.lua")
  272. print("[MOD] Ambience Lite loaded")