/*********************************************************************************************/
/*                                                                                           */
/* Main Titles, Option Code                                                                  */
/* & Inventory stuff                                                                    	 */
/*                                                                                           */
/*********************************************************************************************/

#include "typedefs.h"
#include "malloc.h"
#include "memory.h"
#include "picture.h"
#include "specific.h"
#include "memcard.h"
#include "..\game\objects.h"
#include "..\game\control.h"
#include "..\game\savegame.h"
#include "..\game\inventry.h"
#include "..\game\title.h"
#include "..\game\text.h"
#include "..\game\setup.h"

extern sint16 NextAdventure;

/********************************* GLOBAL VARIABLES ******************************************/
#define TITLE_DEMO_WAIT		20*30
static	int memcard_examined = 0;
int	title_loaded = 0;
int	save_toggle = 0;


sint32 TitleSequence()
{
	int level;
	int rety;

	T_InitPrint();

	noinput_count = 0;

	if (!title_loaded)
	{
#ifdef GAMEDEBUG
		printf("Initialising Title...\n");
#endif
		if (!InitialiseLevel(0, IL_TITLE))
			return(END_ACTION);

		title_loaded = 1;
	}

	if ((!memcard_examined)
	&&  (!gameflow.loadsave_disabled))
	{
		char tmp[128];
//##		SAVEGAME_INFO 	*tmp;

//##		tmp = (SAVEGAME_INFO *)game_malloc(sizeof(SAVEGAME_INFO)+127, TEMP_ALLOC);

//##		if (tmp)
		{
			if ((S_FrontEndCheck(tmp, 128)) == MCF_LOADED)
			{
				S_MemCpy(&savegame, tmp, 128);
				S_control_screen_position();
			}

			memcard_examined = 1;
		}
	}

#ifdef GAMEDEBUG
	printf("Display_Inventory...\n");
#endif
	rety = Display_Inventory(INV_TITLE_MODE);

	if ((rety == STARTDEMO)
	||  (Inventory_Chosen != PASSPORT_OPTION)
	||  (Inventory_ExtraData[0] != 0)
	||  (Inventory_ExtraData[1] != -2000))
	{
		DrawSync(0);
		XAFade(0);
		S_FadeToBlack();
		XAWaitFade();
		S_CDStop();
	}

	if (rety == STARTDEMO)
		return(STARTDEMO);

	if (Inventory_Chosen == PASSPORT_OPTION)
	{
		if (Inventory_ExtraData[0] == 0)	// First Page (LOAD)
		{
			level = Inventory_ExtraData[1]; // Selected level or currentlevel

			if (level == -1000)
				return(STARTSAVEDGAME|savegame.current_level);

		/* -------- adventure select */

			else if (level == -2000)
			{
				char Adventure[] = {
					LV_JUNGLE,
					LV_SHORE,
					LV_DESERT,
					LV_JUNGLE,	// ## PERU
					LV_ROOFTOPS,
					LV_ANTARC
				};

				Display_Inventory(INV_LEVELSELECT_MODE);
				DrawSync(0);
				S_FadeToBlack();
				S_CDStop();
				return STARTGAME|Adventure[NextAdventure];
			}

			else if (level < 0)
				return(DOSTORYSOFAR | -level);

			else
				return(STARTGAME|level);
		}

		else if ( Inventory_ExtraData[0]==1)            // Middle page (START)
		{
			if (gameflow.play_any_level)
			{
				level = Inventory_ExtraData[1]; // Selected level or currentlevel
				return(STARTGAME|level);
			}
			else
	            return(STARTGAME|LV_FIRSTLEVEL);
		}

		else
			return(EXITGAME);
	}

	if (Inventory_Chosen == PHOTO_OPTION)
		return(STARTGAME|LV_GYM);

	return(EXITGAME);
}

