Allow loading new songs during runtime.

master
James Stevenson 2017-11-24 01:56:31 -05:00
parent 5a915d0c2f
commit 61d4f03b46
5 changed files with 41 additions and 50 deletions

View File

@ -1,11 +0,0 @@
default_dig_metal,default_dig_snappy,default_punch
3
3
0
1
3
2
3
1
1
0

View File

@ -1 +1,2 @@
walkie_blip
default_dig_dig_resolution_immediarary
fire_fire

View File

@ -2,8 +2,9 @@
-- By James Stevenson (C) 2017
-- GPL3+
gen_music = {}
local se = minetest.request_insecure_environment()
local show_st = minetest.settings:get_bool("show_sound_traveler")
local show_st = minetest.settings:get_bool("show_sound_travelers")
gen_music.songs = {}
local mod_name = minetest.get_current_modname()
@ -25,9 +26,9 @@ local function load_songs()
local song_name = songs[i]
local song = io.open(mod_path .. "/" .. song_name, "r")
if not song then
print(song_name .. " not found.")
print("[" .. mod_name .. "] " .. song_name .. " not found.")
else
print("Loading " .. song_name)
print("[" .. mod_name .. "] " .. "Loading song " .. song_name .. ".")
local ins = song:read("*l"):split()
local seq = {}
local seql = song:read("*l")
@ -41,9 +42,9 @@ local function load_songs()
}
end
end
io.close(input)
end
load_songs()
--print(dump(gen_music))
local sounds = {}
for _, nodes in pairs(minetest.registered_nodes) do
@ -61,23 +62,26 @@ for _, nodes in pairs(minetest.registered_nodes) do
end
end
end
--[[
-- mobs_redo entities.
for _, def in pairs(minetest.registered_entities) do
for prop, val in pairs(def) do
if prop == "sounds" then
for sound_type, sound in pairs(val) do
if type(sound) == "string" then
if not sounds[sound] and
sounds[sound] ~= "" then
sounds[#sounds + 1] = {name = sound, gain = 0.5}
if minetest.get_modpath("mobs") then
for _, def in pairs(minetest.registered_entities) do
for prop, val in pairs(def) do
if prop == "sounds" then
for sound_type, sound in pairs(val) do
if type(sound) == "string" then
if not sounds[sound] and
sounds[sound] ~= "" then
sounds[#sounds + 1] = {
name = sound,
gain = 0.5
}
end
end
end
end
end
end
end
--]]
-- TODO Erase local table, generate based on extra_sounds.txt
local extra_sounds = {
"tnt_ignite",
"tnt_explode",
@ -97,7 +101,10 @@ local extra_sounds = {
for i = 1, #extra_sounds do
local n = extra_sounds[i]
if not sounds[n] then
sounds[#sounds + 1] = {name = extra_sounds[i], gain = 0.5}
sounds[#sounds + 1] = {
name = extra_sounds[i],
gain = 0.5
}
end
end
@ -153,7 +160,7 @@ local st_textures = {"empty.png"}
local st_on_activate = function(self, staticdata, dtime_s) end
local st_on_step = function(self, dtime) end
if show_st then
print("Showing Sound Travelers")
print("[" .. mod_name .. "] Showing Sound Travelers")
st_textures = {"default_mese_crystal.png"}
st_on_activate = function(self, staticdata, dtime_s)
self.pos = self.object:get_pos()
@ -184,7 +191,6 @@ gen_music.gs = function(object, song, loop)
if type(song) == "string" then
song = gen_music.songs[song]
end
--print(dump(song))
if not song then
return
end
@ -232,7 +238,7 @@ gen_music.gs = function(object, song, loop)
if not ensemble then
n = ""
else
n = ensemble[math.random(1, #song)]
n = ensemble[math.random(1, #ensemble)]
end
seq[idx] = play(n, {
gain = gain * 0.1,
@ -240,7 +246,7 @@ gen_music.gs = function(object, song, loop)
fade = fade,
object = obj[obj_idx],
})
print("Starting " .. seq[idx])
--print("Starting " .. seq[idx])
if fade == 1 then
minetest.sound_fade(seq[idx], 0.1, gain)
end
@ -273,7 +279,7 @@ gen_music.gs = function(object, song, loop)
minetest.after(1 + math.random() + math.random(), function()
for idx = 1, #seq do
minetest.after(math.random(), function()
print("Stopping " .. seq[idx])
--print("Stopping " .. seq[idx])
minetest.sound_fade(seq[idx], -0.1, 0.0)
minetest.after(math.random(), function()
minetest.sound_stop(seq[idx])
@ -304,18 +310,26 @@ minetest.register_chatcommand("gs", {
param = param:split(" ")
local player = minetest.get_player_by_name(name)
if param[1] == "load" then
--[[ -- TODO Mod security.
-- TODO Mod security.
if param[2] then
local h = io.open(mod_path .. "/songs.txt", "a+")
h:write(param[2])
local h = se.io.open(mod_path .. "/songs.txt", "a+")
h:write(param[2] .. "\n")
print("Loaded song " .. param[2] .. ".")
io.close(h)
end
--]]
load_songs()
return true, "songs.txt loaded."
end
if param[1] == "list" then
for song, _ in pairs(gen_music.songs) do
print(song)
end
end
if param[1] == "stop" then
player:set_attribute("loop", nil)
return true, "This may take a moment."
end
local lc = param[2] or 3
player:set_attribute("loop", tonumber(lc))
gen_music.gs(minetest.get_player_by_name(name),

View File

@ -1,9 +0,0 @@
walkie_blip,phit,xdecor_enchanting,mobs_spell
1 20
1 10
2 10
1 10
3 10
4 30
4 40

View File

@ -1,7 +1,3 @@
mysong1.txt
dark.txt
sombre.txt
example1.txt
example2.txt
example3.txt
example4.txt