From 9b50eec56a3451bbdf0c51e23842ddb33c4fb9eb Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Sun, 19 Aug 2018 16:20:14 -0400
Subject: [PATCH] S_FadeMusic[FromLevel] and S_StopFadingMusic Lua

* Fixed some arg position weirdness with other music lua funcs
---
 src/lua_baselib.c | 59 +++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 4 deletions(-)

diff --git a/src/lua_baselib.c b/src/lua_baselib.c
index 3855f07adb..580c53c1e9 100644
--- a/src/lua_baselib.c
+++ b/src/lua_baselib.c
@@ -2305,9 +2305,9 @@ static int lib_sGetMusicLoopPoint(lua_State *L)
 {
 	player_t *player = NULL;
 	NOHUD
-	if (!lua_isnone(L, 2) && lua_isuserdata(L, 2))
+	if (!lua_isnone(L, 1) && lua_isuserdata(L, 1))
 	{
-		player = *((player_t **)luaL_checkudata(L, 2, META_PLAYER));
+		player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
 		if (!player)
 			return LUA_ErrInvalid(L, "player_t");
 	}
@@ -2340,9 +2340,9 @@ static int lib_sGetMusicPosition(lua_State *L)
 {
 	player_t *player = NULL;
 	NOHUD
-	if (!lua_isnone(L, 2) && lua_isuserdata(L, 2))
+	if (!lua_isnone(L, 1) && lua_isuserdata(L, 1))
 	{
-		player = *((player_t **)luaL_checkudata(L, 2, META_PLAYER));
+		player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
 		if (!player)
 			return LUA_ErrInvalid(L, "player_t");
 	}
@@ -2538,6 +2538,55 @@ static int lib_sSetInternalMusicVolume(lua_State *L)
 	return 1;
 }
 
+static int lib_sStopFadingMusic(lua_State *L)
+{
+	player_t *player = NULL;
+	NOHUD
+	if (!lua_isnone(L, 1) && lua_isuserdata(L, 1))
+	{
+		player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
+		if (!player)
+			return LUA_ErrInvalid(L, "player_t");
+	}
+	if (!player || P_IsLocalPlayer(player))
+	{
+		S_StopFadingMusic();
+		lua_pushboolean(L, true);
+	}
+	else
+		lua_pushnil(L);
+	return 1;
+}
+
+static int lib_sFadeMusic(lua_State *L)
+{
+	UINT32 target_volume = (UINT32)luaL_checkinteger(L, 2);
+	UINT32 ms = (UINT32)luaL_optinteger(L, 4, UINT32_MAX);
+	INT32 source_volume;
+
+	if (ms == UINT32_MAX)
+	{
+		ms = (UINT32)luaL_checkinteger(L, 3);
+		source_volume = -1;
+	}
+	else
+		source_volume = (INT32)luaL_checkinteger(L, 3);
+
+	player_t *player = NULL;
+	NOHUD
+	if (!lua_isnone(L, 1) && lua_isuserdata(L, 1))
+	{
+		player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
+		if (!player)
+			return LUA_ErrInvalid(L, "player_t");
+	}
+	if (!player || P_IsLocalPlayer(player))
+		lua_pushboolean(L, S_FadeMusicFromLevel(target_volume, source_volume, ms));
+	else
+		lua_pushnil(L);
+	return 1;
+}
+
 #endif
 static int lib_sOriginPlaying(lua_State *L)
 {
@@ -2931,6 +2980,8 @@ static luaL_Reg lib[] = {
 	{"S_MusicName",lib_sMusicName},
 	{"S_MusicExists",lib_sMusicExists},
 	{"S_SetInternalMusicVolume", lib_sSetInternalMusicVolume},
+	{"S_StopFadingMusic",lib_sStopFadingMusic},
+	{"S_FadeMusic",lib_sFadeMusic},
 #endif
 	{"S_OriginPlaying",lib_sOriginPlaying},
 	{"S_IdPlaying",lib_sIdPlaying},
-- 
GitLab