From b5fc27c5450845f724c394524fc88a918bceb420 Mon Sep 17 00:00:00 2001
From: mazmazz <mar.marcoz@outlook.com>
Date: Fri, 15 Mar 2019 01:00:50 -0400
Subject: [PATCH] Implement MUSICINTERFADEOUT level header

---
 src/dehacked.c   | 2 ++
 src/doomstat.h   | 1 +
 src/lua_maplib.c | 4 +++-
 src/p_setup.c    | 2 ++
 src/y_inter.c    | 8 ++++++--
 5 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/dehacked.c b/src/dehacked.c
index 6978dd16a..a5fddfea2 100644
--- a/src/dehacked.c
+++ b/src/dehacked.c
@@ -1198,6 +1198,8 @@ static void readlevelheader(MYFILE *f, INT32 num)
 				mapheaderinfo[num-1]->mustrack = ((UINT16)i - 1);
 			else if (fastcmp(word, "MUSICPOS"))
 				mapheaderinfo[num-1]->muspos = (UINT32)get_number(word2);
+			else if (fastcmp(word, "MUSICINTERFADEOUT"))
+				mapheaderinfo[num-1]->musicinterfadeout = (UINT32)get_number(word2);
 			else if (fastcmp(word, "MUSICINTER"))
 				deh_strlcpy(mapheaderinfo[num-1]->musintername, word2,
 					sizeof(mapheaderinfo[num-1]->musintername), va("Level header %d: intermission music", num));
diff --git a/src/doomstat.h b/src/doomstat.h
index 716c4d654..82654ba1a 100644
--- a/src/doomstat.h
+++ b/src/doomstat.h
@@ -248,6 +248,7 @@ typedef struct
 	nightsgrades_t *grades; ///< NiGHTS grades. Allocated dynamically for space reasons. Be careful.
 
 	// Music stuff.
+	UINT32 musicinterfadeout;  ///< Fade out level music on intermission screen in milliseconds
 	char musintername[7];    ///< Intermission screen music.
 
 	// Lua stuff.
diff --git a/src/lua_maplib.c b/src/lua_maplib.c
index d77e636b3..2715f807f 100644
--- a/src/lua_maplib.c
+++ b/src/lua_maplib.c
@@ -1489,7 +1489,9 @@ static int mapheaderinfo_get(lua_State *L)
 			if (!header->interscreen[i])
 				break;
 		lua_pushlstring(L, header->interscreen, i);
-	} else if (fastcmp(field,"runsoc"))
+	} else if (fastcmp(field,"musicinterfadeout"))
+		lua_pushinteger(L, header->musicinterfadeout);
+	else if (fastcmp(field,"runsoc"))
 		lua_pushstring(L, header->runsoc);
 	else if (fastcmp(field,"scriptname"))
 		lua_pushstring(L, header->scriptname);
diff --git a/src/p_setup.c b/src/p_setup.c
index 4409ec356..e6aa635e5 100644
--- a/src/p_setup.c
+++ b/src/p_setup.c
@@ -189,6 +189,8 @@ static void P_ClearSingleMapHeaderInfo(INT16 i)
 	mapheaderinfo[num]->mustrack = 0;
 	DEH_WriteUndoline("MUSICPOS", va("%d", mapheaderinfo[num]->muspos), UNDO_NONE);
 	mapheaderinfo[num]->muspos = 0;
+	DEH_WriteUndoline("MUSICINTERFADEOUT", va("%d", mapheaderinfo[num]->musicinterfadeout), UNDO_NONE);
+	mapheaderinfo[num]->musicinterfadeout = 0;
 	DEH_WriteUndoline("MUSICINTER", mapheaderinfo[num]->musintername, UNDO_NONE);
 	mapheaderinfo[num]->musintername[0] = '\0';
 	DEH_WriteUndoline("FORCECHARACTER", va("%d", mapheaderinfo[num]->forcecharacter), UNDO_NONE);
diff --git a/src/y_inter.c b/src/y_inter.c
index e6e5a1d22..ab4c21692 100644
--- a/src/y_inter.c
+++ b/src/y_inter.c
@@ -697,7 +697,9 @@ void Y_Ticker(void)
 
 		if (!intertic) // first time only
 		{
-			if (mapheaderinfo[gamemap-1]->musintername[0] && S_MusicExists(mapheaderinfo[gamemap-1]->musintername, !midi_disabled, !digital_disabled))
+			if (mapheaderinfo[gamemap-1]->musicinterfadeout)
+				S_FadeOutStopMusic(mapheaderinfo[gamemap-1]->musicinterfadeout);
+			else if (mapheaderinfo[gamemap-1]->musintername[0] && S_MusicExists(mapheaderinfo[gamemap-1]->musintername, !midi_disabled, !digital_disabled))
 				S_ChangeMusicInternal(mapheaderinfo[gamemap-1]->musintername, false); // don't loop it
 			else
 				S_ChangeMusicInternal("lclear", false); // don't loop it
@@ -762,7 +764,9 @@ void Y_Ticker(void)
 
 		if (!intertic) // first time only
 		{
-			if (mapheaderinfo[gamemap-1]->musintername[0] && S_MusicExists(mapheaderinfo[gamemap-1]->musintername, !midi_disabled, !digital_disabled))
+			if (mapheaderinfo[gamemap-1]->musicinterfadeout)
+				S_FadeOutStopMusic(mapheaderinfo[gamemap-1]->musicinterfadeout);
+			else if (mapheaderinfo[gamemap-1]->musintername[0] && S_MusicExists(mapheaderinfo[gamemap-1]->musintername, !midi_disabled, !digital_disabled))
 				S_ChangeMusicInternal(mapheaderinfo[gamemap-1]->musintername, false); // don't loop it
 			else
 				S_ChangeMusicInternal("lclear", false); // don't loop it
-- 
GitLab