-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
156 lines (133 loc) · 4.64 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
print("Ambience 0.0.0 loaded! Wubba lubba dub dub!")
local MUSICVOLUME = 1
local SOUNDVOLUME = 1
local storage = minetest.get_mod_storage()
local volume = {}
local music = {}
local music_config = {}
local sounds = {}
local world_path = minetest.get_modpath("ambience")
local function load_volumes()
local volumes = storage:get_string("volumes")
if volumes == "" then
return
end
local playerVolumes = string.split(volumes, ";")
for _,line in pairs(playerVolumes) do
local config_line = string.split(line, ":")
volume[config_line[1]] = {music=config_line[2],sound=config_line[3]}
end
end
local function load_config()
-- Load /ambience_music/config and /ambience_sounds/config
for v,i in pairs(minetest.get_dir_list(world_path.."/sounds/config/", false)) do
local f = io.open(world_path.."/sounds/config/"..i, "r")
local c = f:read("*a")
music_config[i] = minetest.parse_json(c)
end
end
local function load_music()
music = minetest.get_dir_list(world_path.."/sounds", false)
for i,v in pairs(music) do
music[i] = string.split(v,".")[1] -- Strip the extension
end
end
local function load_sounds()
--sounds = minetest.get_dir_list(world_path.."/ambience_sounds", false)
end
local function can_play(name, player)
if music_config[name] == nil then
return false
end
local foo = music_config[name].conditions
for _,i in pairs(foo) do
if i.type == "biome" then
local b = minetest.get_biome_name(minetest.get_biome_data(player:getpos()).biome)
if i.val == "ocean" then
end
if i.val ~= b then
return false
end
end
end
return true
end
load_volumes()
load_config()
load_music()
load_sounds()
local function play_music(player,filename)
local db = volume[player:get_player_name()]
if db.music_handle ~= nil then
minetest.sound_stop(db.music_handle)
end
db.music_handle = minetest.sound_play(filename, {
to_player = player:get_player_name(),
gain = 1.0,
})
end
minetest.register_on_joinplayer(function(player)
if volume[player:get_player_name()] == nil then
volume[player:get_player_name()] = {music=MUSICVOLUME, sound=SOUNDVOLUME}
end
volume[player:get_player_name()].music_handle = nil
volume[player:get_player_name()].sound_handle = nil
end)
minetest.register_chatcommand("volume", {
description = "View sliders to set sound a music volume",
func = function(name,param)
minetest.show_formspec(name, "ambience:volume",
"size[6,3.5]" ..
"label[0,0.5;Music]" ..
"scrollbar[0,1;5.8,0.4;horizontal;music;" .. volume[name].music * 1000 .. "]" ..
"label[0,1.5;Sound]" ..
"scrollbar[0,2;5.8,0.4;horizontal;sound;" .. volume[name].sound * 1000 .. "]" ..
"button_exit[2,2.8;2,0.8;quit;Done]"
)
end,
})
minetest.register_chatcommand("poop", {
func = function(name,param)
print(get_biome(minetest.get_player_by_name(name):getpos()))
end
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "ambience:volume" then
return false
end
local playerName = player:get_player_name()
minetest.log(dump(fields))
if fields.quit ~= "true" then
volume[playerName].music = tonumber(string.split(fields.music,":")[2]) / 1000
volume[playerName].sound = tonumber(string.split(fields.sound,":")[2]) / 1000
end
if fields.quit then
local volumes = ""
for item in pairs(volume) do
volumes = volumes .. item .. ":" .. volume[item].music .. ":" .. volume[item].sound .. ";"
end
storage:set_string("volumes",volumes)
end
return true
end)
local delay = 0
minetest.register_globalstep(function(dtime)
delay = delay + dtime
if delay < 5 then
return
end
delay = 0
for _,player in ipairs(minetest.get_connected_players()) do -- do I want to determine playable songs now or just validate random ones?
if volume[player:get_player_name()].music_handle == nil then -- only play one song at a time
-- Pick a song, roll the dice
for _,i in ipairs(music) do -- Let's determine availability, then apply randomness
if can_play(i,player) and math.random(2) == 1 then
print(i)
local p_name = player:get_player_name()
volume[p_name].music_handle = minetest.sound_play(i,{gain= volume[p_name].music, to_player=p_name})
end
end
end
-- Somehow decide on ambient sounds (these can overlap)
end
end)