2023-04-19 19:32:21 +02:00
|
|
|
local S = minetest.get_translator(minetest.get_current_modname())
|
|
|
|
|
2022-01-21 02:30:16 +01:00
|
|
|
local modname = minetest.get_current_modname()
|
|
|
|
local modpath = minetest.get_modpath(modname)
|
|
|
|
|
2023-01-13 00:33:39 +01:00
|
|
|
local music_enabled = minetest.settings:get_bool("mcl_game_music", true)
|
2023-01-12 01:33:26 +01:00
|
|
|
|
2023-03-19 20:19:32 +01:00
|
|
|
local pianowtune = "diminixed-pianowtune02"
|
|
|
|
local end_tune = "diminixed-ambientwip02"
|
|
|
|
local never_grow_up = "diminixed-nevergrowup04"
|
2022-01-21 16:32:27 +01:00
|
|
|
local nether_tune = "horizonchris96-traitor"
|
2023-02-20 03:03:03 +01:00
|
|
|
local odd_block = "Jester-0dd-BL0ck"
|
|
|
|
local flock_of_one = "Jester-Flock-of-One"
|
|
|
|
local gift = "Jester-Gift"
|
|
|
|
local hailing_forest = "Jester-Hailing_Forest"
|
2023-03-05 20:15:50 +01:00
|
|
|
local lonely_blossom = "exhale_and_tim_unwin-lonely_blossom"
|
|
|
|
local valley_of_ghosts = "exhale_and_tim_unwin-valley_of_ghosts"
|
2023-03-19 20:19:32 +01:00
|
|
|
local farmer = "exhale_and_tim_unwin-farmer"
|
2022-01-21 16:32:27 +01:00
|
|
|
|
|
|
|
local dimension_to_base_track = {
|
2023-03-19 20:19:32 +01:00
|
|
|
["overworld"] = {pianowtune, never_grow_up, flock_of_one, gift, hailing_forest, lonely_blossom, farmer},
|
2023-03-05 20:15:50 +01:00
|
|
|
["nether"] = {nether_tune, valley_of_ghosts},
|
2023-02-20 03:03:03 +01:00
|
|
|
["end"] = {end_tune},
|
|
|
|
["mining"] = {odd_block},
|
2022-01-21 16:32:27 +01:00
|
|
|
}
|
2022-01-21 02:30:16 +01:00
|
|
|
|
|
|
|
local listeners = {}
|
|
|
|
|
2023-02-20 03:03:03 +01:00
|
|
|
local function pick_track(dimension, underground)
|
|
|
|
local track_key
|
|
|
|
|
|
|
|
if dimension == "overworld" and underground then
|
|
|
|
track_key = "mining"
|
2023-02-18 22:58:50 +01:00
|
|
|
else
|
|
|
|
-- Pick random dimension song
|
2023-02-20 03:03:03 +01:00
|
|
|
track_key = dimension
|
2023-02-18 22:58:50 +01:00
|
|
|
end
|
|
|
|
|
2023-02-20 03:03:03 +01:00
|
|
|
local dimension_tracks = dimension_to_base_track[track_key]
|
|
|
|
|
|
|
|
if dimension_tracks and #dimension_tracks >= 1 then
|
|
|
|
local index = 1
|
|
|
|
if #dimension_tracks > 1 then
|
|
|
|
index = math.random(1, #dimension_tracks)
|
|
|
|
end
|
|
|
|
local chosen_track = dimension_tracks[index]
|
|
|
|
--minetest.log("chosen_track: " .. chosen_track)
|
2023-02-20 03:58:40 +01:00
|
|
|
minetest.log("action", "[mcl_music] Playing track: " .. chosen_track .. ", for context: " .. track_key)
|
2023-02-20 03:03:03 +01:00
|
|
|
return chosen_track
|
|
|
|
else
|
2023-04-19 19:32:21 +02:00
|
|
|
-- ?
|
2023-02-20 03:03:03 +01:00
|
|
|
end
|
2023-02-20 03:58:40 +01:00
|
|
|
|
2023-02-20 03:03:03 +01:00
|
|
|
return nil
|
2023-02-18 22:58:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-01-21 02:30:16 +01:00
|
|
|
local function stop_music_for_listener_name(listener_name)
|
|
|
|
if not listener_name then return end
|
|
|
|
local listener = listeners[listener_name]
|
|
|
|
if not listener then return end
|
|
|
|
local handle = listener.handle
|
|
|
|
if not handle then return end
|
2023-03-05 20:16:53 +01:00
|
|
|
|
2023-03-05 20:15:50 +01:00
|
|
|
minetest.log("action", "[mcl_music] Stopping music")
|
2022-01-21 02:30:16 +01:00
|
|
|
minetest.sound_stop(handle)
|
|
|
|
listeners[listener_name].handle = nil
|
|
|
|
end
|
|
|
|
|
2023-02-18 22:58:50 +01:00
|
|
|
local function stop_music_for_all()
|
2022-01-21 02:30:16 +01:00
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
stop_music_for_listener_name(player_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-11 23:16:55 +01:00
|
|
|
local function play_song(track, player_name, dimension, day_count)
|
2023-02-18 22:58:50 +01:00
|
|
|
local spec = {
|
|
|
|
name = track,
|
|
|
|
gain = 0.3,
|
|
|
|
pitch = 1.0,
|
|
|
|
}
|
|
|
|
local parameters = {
|
|
|
|
to_player = player_name,
|
|
|
|
gain = 1.0,
|
|
|
|
fade = 0.0,
|
|
|
|
pitch = 1.0,
|
|
|
|
}
|
|
|
|
local handle = minetest.sound_play(spec, parameters, false)
|
|
|
|
listeners[player_name] = {
|
|
|
|
handle = handle,
|
|
|
|
dimension = dimension,
|
|
|
|
day_count = day_count,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-01-21 02:30:16 +01:00
|
|
|
local function play()
|
|
|
|
local time = minetest.get_timeofday()
|
2022-01-21 15:49:16 +01:00
|
|
|
if time < 0.25 or time >= 0.75 then
|
2023-02-18 22:58:50 +01:00
|
|
|
stop_music_for_all()
|
2022-01-21 15:49:16 +01:00
|
|
|
minetest.after(10, play)
|
2022-01-21 02:30:16 +01:00
|
|
|
return
|
|
|
|
end
|
2023-02-20 03:03:03 +01:00
|
|
|
|
2022-01-21 16:32:27 +01:00
|
|
|
local day_count = minetest.get_day_count()
|
2022-01-21 02:30:16 +01:00
|
|
|
for _, player in pairs(minetest.get_connected_players()) do
|
2023-04-19 19:32:21 +02:00
|
|
|
if not player:get_meta():get("mcl_music:disable") then
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
local hp = player:get_hp()
|
|
|
|
local pos = player:get_pos()
|
|
|
|
local dimension = mcl_worlds.pos_to_dimension(pos)
|
|
|
|
|
|
|
|
local listener = listeners[player_name]
|
|
|
|
local handle = listener and listener.handle
|
|
|
|
|
|
|
|
--local old_hp = listener and listener.hp
|
|
|
|
--local is_hp_changed = old_hp and (math.abs(old_hp - hp) > 0.00001) or false
|
|
|
|
|
|
|
|
local old_dimension = listener and listener.dimension
|
|
|
|
local is_dimension_changed = old_dimension and (old_dimension ~= dimension) or false
|
|
|
|
|
|
|
|
-- minetest.log("handle: " .. dump (handle))
|
|
|
|
if is_dimension_changed then
|
|
|
|
stop_music_for_listener_name(player_name)
|
|
|
|
if not listeners[player_name] then
|
|
|
|
listeners[player_name] = {}
|
|
|
|
end
|
|
|
|
listeners[player_name].hp = hp
|
|
|
|
listeners[player_name].dimension = dimension
|
|
|
|
elseif not handle and (not listener or (listener.day_count ~= day_count)) then
|
|
|
|
local underground = dimension == "overworld" and pos and pos.y < 0
|
|
|
|
local track = pick_track(dimension, underground)
|
|
|
|
if track then
|
|
|
|
play_song(track, player_name, dimension, day_count)
|
|
|
|
else
|
|
|
|
--minetest.log("no track found. weird")
|
|
|
|
end
|
2023-02-20 03:03:03 +01:00
|
|
|
else
|
2023-04-19 19:32:21 +02:00
|
|
|
--minetest.log("else")
|
2023-02-20 03:03:03 +01:00
|
|
|
end
|
2022-01-21 02:30:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.after(7, play)
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:33:39 +01:00
|
|
|
if music_enabled then
|
2023-01-14 11:21:35 +01:00
|
|
|
minetest.log("action", "[mcl_music] In-game music is activated")
|
2023-01-12 01:33:26 +01:00
|
|
|
minetest.after(15, play)
|
2022-01-21 02:30:16 +01:00
|
|
|
|
2023-01-12 01:33:26 +01:00
|
|
|
minetest.register_on_joinplayer(function(player, last_login)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
stop_music_for_listener_name(player_name)
|
|
|
|
end)
|
2022-01-21 02:30:16 +01:00
|
|
|
|
2023-04-19 19:32:21 +02:00
|
|
|
minetest.register_on_leaveplayer(function(player, timed_out)
|
|
|
|
listeners[player:get_player_name()] = nil
|
|
|
|
end)
|
|
|
|
|
2023-01-12 01:33:26 +01:00
|
|
|
minetest.register_on_respawnplayer(function(player)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
stop_music_for_listener_name(player_name)
|
|
|
|
end)
|
|
|
|
else
|
2023-01-14 11:21:35 +01:00
|
|
|
minetest.log("action", "[mcl_music] In-game music is deactivated")
|
|
|
|
end
|
2023-04-19 19:32:21 +02:00
|
|
|
|
|
|
|
minetest.register_chatcommand("music", {
|
|
|
|
params = "[on|off|invert [<player name>]]",
|
2024-12-03 21:26:41 +01:00
|
|
|
description = S("Turn music for yourself or another player on or off."),
|
2023-04-19 19:32:21 +02:00
|
|
|
func = function(sender_name, params)
|
|
|
|
local argtable = {}
|
|
|
|
for str in string.gmatch(params, "([^%s]+)") do
|
|
|
|
table.insert(argtable, str)
|
|
|
|
end
|
|
|
|
|
|
|
|
local action = argtable[1]
|
|
|
|
local playername = argtable[2]
|
|
|
|
|
|
|
|
local sender = minetest.get_player_by_name(sender_name)
|
|
|
|
local target_player = nil
|
|
|
|
|
|
|
|
if not action or action == "" then action = "invert" end
|
|
|
|
|
|
|
|
if not playername or playername == "" or sender_name == playername then
|
|
|
|
target_player = sender
|
|
|
|
playername =sender_name
|
|
|
|
elseif not minetest.check_player_privs(sender, "debug") then -- Self-use handled above
|
2024-12-03 21:26:41 +01:00
|
|
|
minetest.chat_send_player(sender_name, S("You need the debug privilege in order to turn in-game music on or off for somebody else!"))
|
2023-04-19 19:32:21 +02:00
|
|
|
return
|
|
|
|
else -- Admin
|
|
|
|
target_player = minetest.get_player_by_name(playername)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not target_player then
|
2024-12-03 21:26:41 +01:00
|
|
|
minetest.chat_send_player(sender_name, S("Could not find player @1!", playername))
|
2023-04-19 19:32:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local meta = target_player:get_meta()
|
|
|
|
local display_new_state = "unknown" -- Should never be displayed -> no translation
|
|
|
|
|
|
|
|
if action == "invert" then
|
|
|
|
if not meta:get("mcl_music:disable") then
|
|
|
|
meta:set_int("mcl_music:disable", 1)
|
|
|
|
display_new_state = S("off")
|
|
|
|
else
|
|
|
|
meta:set_string("mcl_music:disable", "") -- This deletes the meta value!
|
|
|
|
display_new_state = S("on")
|
|
|
|
end
|
|
|
|
elseif action == "on" then
|
|
|
|
meta:set_string("mcl_music:disable", "") -- Delete
|
|
|
|
display_new_state = S("on")
|
|
|
|
else
|
|
|
|
meta:set_int("mcl_music:disable", 1)
|
|
|
|
display_new_state = S("off")
|
|
|
|
end
|
|
|
|
|
|
|
|
stop_music_for_listener_name(playername)
|
|
|
|
minetest.chat_send_player(sender_name, S("Set music for @1 to: @2", playername, display_new_state))
|
|
|
|
end,
|
2024-12-03 21:26:41 +01:00
|
|
|
})
|