#library "MSTRLVLL"
#include "zcommon.acs"

//
//  THESE LIBRARIES SCRIPTS ARE PURELY FOR MANAGING PLAY RESULT RECORDINGS AND
//  DON'T HAVE ANYTHING TO DO WITH GETPLAYERINPUT.  IF YOU ARE INTERESTING IN
//  THE DEMONSTRATION OF GETPLAYERINPUT, SEE MenuLevelScripts.txt OR LOOK AT THE
//  SCRIPTS FOR MENULEVL IN Menu.wad (THEY ARE IDENTICAL).
//

// See the copy of the map scripts for definitions of the global variables
global int 51:returningToMenu;
global int 52:option_music;
global int 53:option_inv;

global int 54:recentPlayResults[];

global int 55:playResults[]; // 0 if not beaten yet, 1 for beaten before
global int 56:killsResults[];
global int 57:killsMaxResults[];
global int 58:itemsResults[];
global int 59:itemsMaxResults[];
global int 60:secretsResults[];
global int 61:secretsMaxResults[];
global int 62:timeResults[];  // int tics
global int 63:timeParResults[]; // in seconds

// recentPlayResults format
// levelnum, kills, killmax, items, itemmax, secrets, secretmax, time, par

#DEFINE R_LEVELNUM	0
#DEFINE R_KILLS		1
#DEFINE R_KILLS_MAX	2
#DEFINE R_ITEMS		3
#DEFINE R_ITEMS_MAX	4
#DEFINE R_SECRETS	5
#DEFINE R_SECRETS_MAX	6
#DEFINE R_TIME		7	// in tics
#DEFINE R_TIME_PAR	8	// in seconds

#DEFINE MAP_BADDREAM 21
#DEFINE MAP_TITLEMAP 98
#DEFINE MAP_MENU 99


// When using the "Varied music" option, play these songs for the levels
int newMusic[21] = {	    // Map    - Default music
		"D_RUNNIN", // Attack - D_RUNNIN
		"D_ADRIAN", // Black Tower - D_ADRIAN
		"D_SHAWN",  // Bloodsea - D_SHAWN
		"D_THE_DA", // Canyon - D_RUNNIN
		"D_BETWEE", // Catwalk - D_RUNNIN
		"D_STALKS", // Combine - D_RUNNIN
		"D_AMPIE",  // Fistula - D_RUNNIN
		"D_MESSAG", // Garrison - D_RUNNIN
		"D_DDTBLU", // Geryon - D_DDTBLU
		"D_DEAD",   // Titan Manor - D_RUNNIN
		"D_ROMERO", // Mephisto - D_SHAWN
		"D_DOOM",   // Minos - D_DOOM
		"D_SHAWN2", // Nessus - D_SHAWN
		"D_THEDA2", // Paradox - D_RUNNIN
		"D_COUNT2", // Subspace - D_RUNNIN
		"D_TENSE",  // Subterra - D_RUNNIN
		"D_EVIL",   // TEETH - D_EVIL
		"D_DDTBL2", // Trapped - D_RUNNIN
		"D_IN_CIT", // Vesperas - D_IN_CIT
		"D_COUNTD", // Virgil - D_COUNTD
		"D_ULTIMA", // TEETH2 - D_ULTIMA
	};

// Music level start script
script 100 OPEN
{
	int levelnum = GetLevelInfo(LEVELINFO_LEVELNUM);
	
	// Safety check.  Make sure the level is inside the array
	// range, to prevent the script from bombing if the user
	// goes to a different level.  This also prevents the script
	// from changing music on the menu level
	if( levelnum > 21 )
	{
		terminate;
	}

	if( option_music )
	{
		SetMusic( newMusic[levelnum - 1] );
	}
}

// Beat a level
script 102 UNLOADING
{
	// Special behavior: Leaving Menu level, titlemap, or "Bad Dream"
	// means don't do anything.  Not worth the special coding to record
	// the results for "Bad Dream".  Plus, it's a joke level, I mean come on.
	int levelnum = GetLevelInfo(LEVELINFO_LEVELNUM);
	if( levelnum == MAP_MENU || levelnum == MAP_BADDREAM )
	{
		terminate;
	}

	returningToMenu = 1;
	
	// Store the results of this map session
	recentPlayResults[R_LEVELNUM]	 = levelnum;
	recentPlayResults[R_KILLS]	 = GetLevelInfo(LEVELINFO_KILLED_MONSTERS);
	recentPlayResults[R_KILLS_MAX]	 = GetLevelInfo(LEVELINFO_TOTAL_MONSTERS);
	recentPlayResults[R_ITEMS]	 = GetLevelInfo(LEVELINFO_FOUND_ITEMS);
	recentPlayResults[R_ITEMS_MAX]	 = GetLevelInfo(LEVELINFO_TOTAL_ITEMS);
	recentPlayResults[R_SECRETS]	 = GetLevelInfo(LEVELINFO_FOUND_SECRETS);
	recentPlayResults[R_SECRETS_MAX] = GetLevelInfo(LEVELINFO_TOTAL_SECRETS);
	recentPlayResults[R_TIME]	 = Timer();
	recentPlayResults[R_TIME_PAR]	 = GetLevelInfo(LEVELINFO_PAR_TIME);
}
