/*********************************************************************************************/
/*                                                                                           */
/* Option/inventory stuff                                                                    */
/*                                                                                           */
/*********************************************************************************************/

#include <sys\types.h>
#include <stdio.h>
#include "typedefs.h"
#include "memory.h"
#include "gpu.h"
#include "gen_draw.h"
#include "psxinput.h"
#include "picture.h"
#include "specific.h"
#include "cd.h"
#include "memcard.h"
#include "..\game\objects.h"
#include "..\game\camera.h"
#include "..\game\control.h"
#include "..\game\input.h"
#include "..\game\text.h"
#include "..\game\sound.h"
#include "..\game\invfunc.h"
#include "..\game\savegame.h"
#include "..\game\inventry.h"
#include "..\game\gameflow.h"
#include "..\game\title.h"

static char *generateControlLR( void );
static void S_ShowControls(void);
void	do_pickup_option(INVENTORY_ITEM *inv_item);
extern uchar NoInventoryItemDraw;

//#define SFX_TEST
#define NUM_B_TEXT	8
#define NUM_C_TEXT	16

/* -------- this fucker should be INCLUDEd from MEMCARD.H or summat */

//enum	{
//	MCF_NONE,
//	MCF_LOADED,
//	MCF_CAN_SAVE,
//	MCF_SAVED,
//	MCF_FAILED,
//};

typedef struct pconfig {
	uint16	pL2;
	uint16	pR2;
	uint16	pL2b;
	uint16	pR2b;
	uint16	pL1;
	uint16	pR1;
	uint16	pL1b;
	uint16	pR1b;
	uint16	pTRI;
	uint16	pTRIb;
	uint16	pSQU;
	uint16	pCIR;
	uint16	pSQUb;
	uint16	pCIRb;
	uint16	pX;
	uint16	pXb;
} PCONFIG;

/*---------------------------------------------------------------------------
 * Externals
\*--------------------------------------------------------------------------*/

extern UBYTE DualShock;
UBYTE ControlVibrate = 0;	// Changing control or vibrate.

extern sint16 NextAdventure;

extern void CreateMonoScreen();
extern void RemoveMonoScreen();
extern void DrawMonoScreen();
extern void T_RightJustify(TEXTSTRING *);

extern sint16 	GlobeLevelAngles[7][4];
extern sint16	GlobeXRot,GlobeYRot,GlobeZRot,GlobeLevel;
extern sint32	GlobeButton;
extern uchar 	GlobeLevelComplete[6];

extern int Level2World(int level);
extern int World2Level(int world);
extern void SortOutAdventureSave(int world);

/*---------------------------------------------------------------------------
 * Locals
\*--------------------------------------------------------------------------*/

static int passport_mode = 0;

static char passport_LR_text[128];

static TEXTSTRING *seltext=NULL;
static TEXTSTRING *deseltext=NULL;
static TEXTSTRING *continuetext=NULL;
static TEXTSTRING *passportLR = NULL;
static TEXTSTRING *sfxvoltxt=NULL;
static TEXTSTRING *cdvoltxt=NULL;
static TEXTSTRING *control_text[1]={NULL};
static TEXTSTRING *btext[NUM_B_TEXT]={NULL};
static TEXTSTRING *ctext[NUM_C_TEXT]={NULL};
static TEXTSTRING *AdventureText=NULL;
static TEXTSTRING *LeftArrow=NULL;
static TEXTSTRING *RightArrow=NULL;

#ifdef SFX_TEST
static int sfx_num=0;
static int pan_qty=0;
static TEXTSTRING *sndtext=NULL;
static TEXTSTRING *pantext=NULL;
#endif

static GouraudBarColourSet loBarColourSet = {
	{  0x10, 0x18, 0x20, 0x18, 0x10 },
	{  0x10, 0x18, 0x20, 0x18, 0x10 },
	{  0x10, 0x18, 0x20, 0x18, 0x10 },

	{  0x30, 0x40, 0x50, 0x40, 0x30 },
	{  0x30, 0x40, 0x50, 0x40, 0x30 },
	{  0x30, 0x40, 0x50, 0x40, 0x30 }
};

static GouraudBarColourSet hiBarColourSet =	{
	{ 57+40, 73+40, 57+40, 46+40, 37+40},	// left green
	{ 62+40, 76+40, 61+40, 54+40, 45+40},	// left blue
	{  0,  0,  0,  0,  0},	// left red
	{113,146,113, 93, 74},	// right green
	{  0,  0,  0,  0,  0},	// right blue
	{  0,  0,  0,  0,  0}	// right red
};

#define GT_DUCK GT_USEFLARE
#define GT_DASH GT_STEPSHIFT
#define GT_DRAW GT_DRAWWEAPON1

static PCONFIG	padfigs[]={
	{	GT_DUCK,GT_DASH, 	GT_SPACE,GT_SPACE,
		GT_LOOK,GT_WALK, 	GT_SPACE,GT_SPACE,
		GT_DRAW,			GT_DRAWWEAPON2,
		GT_JUMP,GT_ROLL,	GT_SPACE,GT_SPACE,
		GT_ACTION,		GT_SPACE
	},

	{
		GT_LOOK,GT_WALK, 	GT_SPACE,GT_SPACE,
		GT_DUCK,GT_DASH, 	GT_SPACE,GT_SPACE,
		GT_ROLL,			GT_DRAWWEAPON2,
		GT_DRAW,GT_ACTION,	GT_SPACE,GT_SPACE,
		GT_JUMP,			GT_SPACE
	},

	{
		GT_DASH,GT_DUCK, 	GT_SPACE,GT_SPACE,
		GT_WALK,GT_LOOK, 	GT_SPACE,GT_SPACE,
		GT_JUMP,			GT_DRAWWEAPON2,
		GT_ROLL,GT_ACTION,	GT_SPACE,GT_SPACE,
		GT_DRAW,			GT_SPACE
	},

	{
		GT_DUCK,GT_DASH, 	GT_SPACE,GT_SPACE,
		GT_DRAW,GT_WALK, 	GT_SPACE,GT_SPACE,
		GT_LOOK,			GT_DRAWWEAPON2,
		GT_ACTION,GT_JUMP,	GT_SPACE,GT_SPACE,
		GT_ROLL,			GT_SPACE
	},

	{
    		GT_ROLL,GT_DRAW, 	GT_SPACE,GT_SPACE,
		GT_JUMP,GT_ACTION, 	GT_SPACE,GT_SPACE,
		GT_DASH,			GT_DRAWWEAPON2,
		GT_LOOK,GT_DUCK,	GT_SPACE,GT_SPACE,
		GT_WALK,			GT_SPACE
	}
};

/*---------------------------------------------------------------------------
 * Prototypes
\*--------------------------------------------------------------------------*/

void DisplayControlArrows();
void	RemoveArrowText();
void	RemoveDeselectText();

/*---------------------------------------------------------------------------
 * Local Functions
\*--------------------------------------------------------------------------*/

extern unsigned char LTab[];

static void CreateLTab()
{
	int lp1, lp2, lp3, off, mul;

#ifdef GAMEDEBUG
	printf("Creating LTab\n");
#endif

	off = 0;

	for (lp1=1;lp1<33;lp1++)
	{
		for (lp2=lp1;lp2>0;lp2--)
		{

			mul = (65536*lp2)/(lp1);

			for (lp3=0;lp3<32;lp3++)
			{
				LTab[off++] = (lp3*mul)>>16;
			}
		}
	}

}


/*******************************************************************************
 *			remove Display X to Select text
 ******************************************************************************/
static void	RemoveSelectText( void )
{
	if (seltext)
	{
		T_RemovePrint( seltext );
		seltext = NULL;
	}
}

/*****************************************************************************
 *		Modified By Paul so Didnt OverWrite Array
 ****************************************************************************/
static void S_RemoveCtrlText(void)
{
	int i;

	for ( i=0 ; i<NUM_B_TEXT ; i++ )
	{
		T_RemovePrint(btext[i]);
		btext[i] = NULL;
	}

	for ( i=0 ; i<NUM_C_TEXT ; i++ )
	{
		T_RemovePrint(ctext[i]);
		ctext[i] = NULL;
	}
}

/****************************************************************************
 *			Change Control Text
 ***************************************************************************/
static void S_ChangeCtrlText(void)
{
	if (!ControlVibrate)
	{
		T_ChangeText(ctext[0], GF_GameStrings[padfigs[iconfig].pL2] );	// L2
		T_ChangeText(ctext[1], GF_GameStrings[padfigs[iconfig].pR2] );	// R2
		T_ChangeText(ctext[2], GF_GameStrings[padfigs[iconfig].pL1] );	// L1
		T_ChangeText(ctext[3], GF_GameStrings[padfigs[iconfig].pR1] );	// R1
		T_ChangeText(ctext[4], GF_GameStrings[padfigs[iconfig].pTRI] );	// TRIANGLE
		T_ChangeText(ctext[5], GF_GameStrings[padfigs[iconfig].pSQU] );	// SQUARE
		T_ChangeText(ctext[6], GF_GameStrings[padfigs[iconfig].pCIR] );	// CIRCLE
		T_ChangeText(ctext[7], GF_GameStrings[padfigs[iconfig].pX] );	// CROSS
	}
	else
	{
		if (DualShock)
			T_ChangeText(ctext[0], GF_PSXStrings[PSSTR_VIBOFF + VibrateOn]);
		else
		{
			ControlVibrate = 0;
			VibrateOn = 0;
			S_RemoveCtrlText();
			S_ShowControls();
			T_ChangeText(control_text[0], generateControlLR() );
		}
	}
}

/*********************************************************************************
 *				Build LR Strings
 ********************************************************************************/
static void generateLR( char *string, int left, int right )
{
	char 	*ptr;

	ptr = passport_LR_text;
	if ( left )
	{
		*(ptr++) = 17;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
	}
	else
	{
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
	}

	while ( *(string) )
	{
		*(ptr++) = *(string++);
	}

	if ( right )
	{
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 18;
	}
	else
	{
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
	}
	*(ptr++) = 0;
	passportLR = T_Print(0, -24, 5, passport_LR_text);
//	printf("%s %d\n", passport_LR_text, passportLR);
    	T_BottomAlign(passportLR, 1);
    	T_CentreH(passportLR, 1);
}
/******************************************************************************
 *			Generate Control Method %d text with Left/Right Arrows
 *****************************************************************************/
static char *generateControlLR( void )
{
	char 	*ptr,*src;

	ptr = passport_LR_text;

	if (!ControlVibrate)
	{
		src = GF_PSXStrings[PSSTR_USERKEYS];
		*(ptr++) = 17;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;

		while ( *(src) )
			*(ptr++) = *(src++);

		*(ptr++) = 32;
		*(ptr++) = (char)('1' + iconfig);
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 18;
		*(ptr++) = 0;
	}
	else
	{
		src = GF_PSXStrings[PSSTR_VIBOFF+2];	// Control Vibrator text?
		*(ptr++) = 17;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;

		while ( *(src) )
			*(ptr++) = *(src++);

		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 32;
		*(ptr++) = 18;
		*(ptr++) = 0;
	}

	return( passport_LR_text );
}


/******************************************************************************
 *		Control Configuration
 *****************************************************************************/
static void S_ShowControls(void)
{
	int	i;

	DisplayControlArrows();

	if (!ControlVibrate)
	{
		if (!btext[0])
		{
			btext[0] = T_Print(256-32, 	-60+1-9+8, 	2, 	"`");	// L2
			btext[1] = T_Print(256+32, 	-60+1-9+8, 	2, 	"{");	// R2
			btext[2] = T_Print(256-32, 	-35+1-9-4+16, 	2, 	"^");	// L1
			btext[3] = T_Print(256+32, 	-35+1-9-4+16, 	2, 	"_");	// R1
			btext[4] = T_Print(256, 		5-9-4+24, 		2, 	";");		// TRIANGLE
			btext[5] = T_Print(256-32, 	20-9-4+32, 		2, 	"\\");		// SQUARE
			btext[6] = T_Print(256+32, 	20-9-4+32, 		2, 	"<");		// CIRCLE
			btext[7] = T_Print(256, 	  	35-9-4+40, 		2, 	">");		// CROSS

			for ( i=0 ; i<NUM_B_TEXT ; i++ )
				T_CentreV(btext[i], 1);
		}

		ctext[0] = T_Print(256-48, -60-9+8, 6, GF_GameStrings[padfigs[iconfig].pL2]);
		ctext[1] = T_Print(256+48, -60-9+8, 6, GF_GameStrings[padfigs[iconfig].pR2]);
		ctext[2] = T_Print(256-48, -35-9-4+16, 6, GF_GameStrings[padfigs[iconfig].pL1]);
		ctext[3] = T_Print(256+48, -35-9-4+16, 6, GF_GameStrings[padfigs[iconfig].pR1]);
		ctext[4] = T_Print(     0, -12-9-4+24, 6, GF_GameStrings[padfigs[iconfig].pTRI]);
		ctext[5] = T_Print(256-48,  20-9-4+32, 6, GF_GameStrings[padfigs[iconfig].pSQU]);
		ctext[6] = T_Print(256+48,  20-9-4+32, 6, GF_GameStrings[padfigs[iconfig].pCIR]);
		ctext[7] = T_Print(     0,  51-9-4+40, 6, GF_GameStrings[padfigs[iconfig].pX]);

		T_RightJustify(ctext[0]);
		T_RightJustify(ctext[2]);
		T_RightJustify(ctext[5]);
		T_CentreH(ctext[4], 1);
		T_CentreH(ctext[7], 1);
		T_CentreH(ctext[8], 1);	// Rumble on/off;

		for ( i=0 ; i<NUM_C_TEXT ; i++ )
			T_CentreV( ctext[i],1 );
	}
	else
	{
		ctext[0] = T_Print(     0,  0, 6, GF_PSXStrings[PSSTR_VIBOFF + VibrateOn]);	// Rumble on/off.
		T_CentreH(ctext[0], 1);	// Rumble on/off;
		T_CentreV(ctext[0], 1);
	}
}

static int do_control_option2(INVENTORY_ITEM *inv_item)
{
	DisplayControlArrows();

	if (!control_text[0])
	{
#ifdef	NTSC
		control_text[0] = T_Print(0,26,5, generateControlLR() );
#else
		control_text[0] = T_Print(0,26+8,5, generateControlLR() );
#endif
		T_CentreH(control_text[0], 1);
		S_ShowControls();
	}

	if (inputDB&IN_RIGHT)
	{
		if (!ControlVibrate)	// Selecting control option?
		{
			iconfig++;
			if (iconfig >= 5)
				iconfig=0;
			input=0;
			inputDB=0;
		}
		else
		{
			input=0;
			inputDB=0;
			VibrateOn ^= 1;
		}
	}

	if (inputDB&IN_LEFT)
	{
		if (!ControlVibrate)	// Selecting control option?
		{
			iconfig--;
			if (iconfig <0)
				iconfig=4;
			input=0;
			inputDB=0;
		}
		else
		{
			input=0;
			inputDB=0;
			VibrateOn ^= 1;
		}
	}

	T_ChangeText(control_text[0], generateControlLR() );

	if ((inputDB & IN_FORWARD) && DualShock)
	{
		if (ControlVibrate)
		{
			ControlVibrate ^= 1;
			S_RemoveCtrlText();
			S_ShowControls();
			T_ChangeText(control_text[0], generateControlLR() );
		}
	}

	if ((inputDB & IN_BACK) && DualShock)
	{
		if (!ControlVibrate)
		{
			ControlVibrate ^= 1;
			S_RemoveCtrlText();
			S_ShowControls();
			T_ChangeText(control_text[0], generateControlLR() );
		}
	}

	if (inputDB&DESELECT_ITEM)
	{
		T_RemovePrint(control_text[0]);
		control_text[0] = NULL;
		iconfig = old_iconfig;
		VibrateOn = old_VibrateOn;
		S_RemoveCtrlText();
		ControlVibrate = 0;
		return(1);
	}

	if (inputDB&SELECT_ITEM)
	{
		old_iconfig = iconfig;
		old_VibrateOn = iconfig;
		T_RemovePrint(control_text[0]);
		control_text[0] = NULL;
		S_RemoveCtrlText();
		ControlVibrate = 0;
		return(1);
	}

	S_ChangeCtrlText();
	input = 0;
	inputDB = 0;
	return(0);
}

/******************************************************************************
 *				Show Different Control Options
 *****************************************************************************/
static void do_control_option( INVENTORY_ITEM *inv_item )
{
	int	quitme;
	TEXTSTRING *nojoytext=NULL;

	if ( Inv_ringText )                         // remove Option text
	{
		T_RemovePrint( Inv_ringText );
		Inv_ringText = NULL;
	}

	if (Inventory_Mode == INV_TITLE_MODE)
		S_InitFade(FADE_MAX, FADE_MID, 0);

	old_iconfig = iconfig;
	old_VibrateOn = VibrateOn;

	do {
		S_InitialisePolyList();
		S_ClearScreen();
		S_UpdateInput();

		if (Inventory_Mode == INV_TITLE_MODE)
		{
			if (input & IN_PAUSE)
				input |= IN_SELECT;
		}

		if ( !PadConnected )                    // Some Buffoon has removed their Controller
		{
			if (nojoytext == NULL)
			{
  				nojoytext = T_Print(0, 62, 9, GF_PSXStrings[PSSTR_NOJOY_TEXT] );
   				T_CentreH( nojoytext, 1 );
				T_FlashText(nojoytext, 1, 20);

				if (control_text[0])
				{
					T_RemovePrint(control_text[0]);
					control_text[0] = NULL;
					S_RemoveCtrlText();
					RemoveArrowText();
				}
			}

			quitme = 0;
		}
		else
		{
			if (nojoytext)
			{
				T_RemovePrint(nojoytext);
				nojoytext = NULL;
			}

			inputDB = GetDebouncedInput(input);
			quitme = do_control_option2(inv_item);
		}

		if (fades_enabled)
			S_DrawPic();
		else
			DrawMonoScreen(0x50, 0x50, 0x70);

		T_DrawText();
		S_OutputPolyList();
		S_DumpScreen();

	} while (!quitme);

	RemoveArrowText();

	if ( nojoytext )
	{
		T_RemovePrint( nojoytext ); nojoytext = NULL;
	}
	if ( Inventory_Mode==INV_TITLE_MODE )
		S_InitFade( FADE_MID,FADE_MAX,0 );
}

/*********************************************************************************
 *				Remove All possible PSX Prompts
 ********************************************************************************/
void	RemovePromptText( void )
{
	RemoveSelectText();						// Remove Button Prompts
	RemoveDeselectText();
	RemoveArrowText();
}

/******************************************************************************
 *				Do Level Select option in Inventory
 *****************************************************************************/
static void do_levelselect_option(INVENTORY_ITEM *inv_item)
{
	sint16 diff,set;
	char	la[2] = {	17,0	};
	char	ra[2] = {	18,0	};

	T_RemovePrint(AdventureText);
	AdventureText = NULL;
	T_RemovePrint(LeftArrow);
	LeftArrow = NULL;
	T_RemovePrint(RightArrow);
	RightArrow = NULL;

	set = 0;
	diff = GlobeLevelAngles[GlobeLevel][0] - GlobeXRot;
	if (diff < 128 && diff > -128)
	{
		GlobeXRot = GlobeLevelAngles[GlobeLevel][0];
		set++;
	}
	else
		GlobeXRot += diff>>3;

	diff = GlobeLevelAngles[GlobeLevel][1] - GlobeYRot;
	if (diff < 128 && diff > -128)
	{
		GlobeYRot = GlobeLevelAngles[GlobeLevel][1];
		set++;
	}
	else
		GlobeYRot += diff>>3;

	diff = GlobeLevelAngles[GlobeLevel][2] - GlobeZRot;
	if (diff < 128 && diff > -128)
	{
		GlobeZRot = GlobeLevelAngles[GlobeLevel][2];
		set++;
	}
	else
		GlobeZRot += diff>>3;

	if (set == 3 && GlobeLevel != 6)	// Reached destination and not deselected?
	{
		AdventureText = T_Print(0, -16, IT_BRONZE, GF_GameStrings[GlobeLevelAngles[GlobeLevel][3]]);
		T_CentreH(AdventureText, 1);
		T_BottomAlign(AdventureText, 1);

		if (input&IN_LEFT)
		{
			for (;;)
			{
				if (GlobeLevel == 0)
					GlobeLevel = 5;
				else
					GlobeLevel--;
				if (!GlobeLevelComplete[GlobeLevel])
					break;
			}
		}
		else	if (input&IN_RIGHT)
		{
			for (;;)
			{
				if (GlobeLevel == 5)
					GlobeLevel = 0;
				else
					GlobeLevel++;
				if (!GlobeLevelComplete[GlobeLevel])
					break;
			}
		}
	}

	if (GlobeLevel == 6)
		inv_item->drawn_meshes = 0xf81;
	if (set == 3 && GlobeLevel == 6)
	{
		GlobeLevel = 0;
		while(GlobeLevelComplete[GlobeLevel])
			GlobeLevel++;

		inputDB |= GlobeButton;
		inv_item->drawn_meshes = 0xf81;
		return;
	}
	else if (GlobeLevel != 6)
	{
		long advs,lp;

		advs = 0;
		for (lp=0;lp<6;lp++)
		{
			if (GlobeLevelComplete[lp] == 0)
				advs++;
		}

		if (set == 3 && advs > 1)
		{
			LeftArrow = T_Print(140, -16, 2, la);
			T_BottomAlign(LeftArrow, 1);
			RightArrow = T_Print(370, -16, 2, ra);
			T_BottomAlign(RightArrow, 1);
		}

		inv_item->drawn_meshes = 0xfff;

		if (GlobeLevelComplete[3] == 255)	// Draw peru?
			inv_item->drawn_meshes &= ~8;

		if (GlobeLevelComplete[5] == 255)	// Draw Antarctica?
			inv_item->drawn_meshes &= ~0x40;
	}

	inputDB &= ~DESELECT_ITEM;

	if ((inputDB & (DESELECT_ITEM|SELECT_ITEM)) && GlobeLevel != 6)
	{
		if (inputDB & SELECT_ITEM)
			NextAdventure = GlobeLevel;	// Here you go you miserable cunt.
		// Set your desired level
		GlobeLevel = 6;	// Reset X Y Z rotations of globe.
		GlobeButton = inputDB&(DESELECT_ITEM|SELECT_ITEM);
		inputDB &= (~(DESELECT_ITEM|SELECT_ITEM));
	}

	if ((inputDB & (DESELECT_ITEM|SELECT_ITEM)) && GlobeLevel == 6)	// Stop button presses while being deselected.
		inputDB &= (~(DESELECT_ITEM|SELECT_ITEM));

}

/*---------------------------------------------------------------------------
 * Public Functions
\*--------------------------------------------------------------------------*/

/********************************************************************/
/* Which Inventory Option */
/********************************************************************/
void do_inventory_options(INVENTORY_ITEM *inv_item)
{
	int mem;
	uint32 copy_timer;
	uint32 copy_ammo_used;
	uint32 copy_ammo_hit;
	uint32 copy_distance_travelled;
	uint16 copy_kills;
	unsigned char copy_secrets;
	unsigned char copy_health_used;


#if 0
	char	buf[80];

	if (!(input&IN_ROLL))
	{
	if (input&IN_LOOK && input&IN_BACK)
	{
		GlobeXRot-=0x100;
//		inv_item->x_rot-=0x200;
//		inv_item->x_rot_sel-=0x200;
//		inv_item->x_rot_nosel-=0x200;
	}
	if (input&IN_LOOK && input&IN_FORWARD)
	{
		GlobeXRot+=0x100;
//		inv_item->x_rot+=0x200;
//		inv_item->x_rot_nosel+=0x200;
	}
	if (input&IN_LOOK && input&IN_LEFT)
	{
		GlobeYRot+=0x100;
//		inv_item->y_rot+=0x200;
//		inv_item->y_rot_sel+=0x200;
	}
	if (input&IN_LOOK && input&IN_RIGHT)
	{
		GlobeYRot-=0x100;
 //		inv_item->y_rot-=0x200;
//		inv_item->y_rot_sel-=0x200;
	}
	if (input&IN_SLOW && input&IN_BACK)
	{
		GlobeZRot-=0x100;
	}
	if (input&IN_SLOW && input&IN_FORWARD)
	{
		GlobeZRot+=0x100;
	}
//	sprintf(buf,"ptxr:%d xr:%d  xrn:%d yr:%d",inv_item->pt_xrot,inv_item->x_rot, inv_item->x_rot_nosel, inv_item->y_rot);
	sprintf(buf,"xr:%d yr:%d zr:%d",GlobeXRot,GlobeYRot,GlobeZRot);
	PrintDbug( 6,1, buf );
/*	if (input&IN_SLOW && input&IN_BACK)
	{
		inv_item->ytrans-=2;
		inv_item->ytrans_sel-=2;
	}
	if (input&IN_SLOW && input&IN_FORWARD)
	{
		inv_item->ytrans+=2;
		inv_item->ytrans_sel+=2;
	}
	if (input&IN_SLOW && input&IN_LEFT)
	{
		inv_item->ztrans+=2;
		inv_item->ztrans_sel+=2;
	}
	if (input&IN_SLOW && input&IN_RIGHT)
	{
		inv_item->ztrans-=2;
		inv_item->ztrans_sel-=2;
	}
	sprintf(exit_message,"yt:%d zt:%d",inv_item->ytrans,inv_item->ztrans);
	PrintDbug( 10,9, exit_message );*/
	}
#endif

	if (reset_flag)
		inputDB |= DESELECT_ITEM;

	switch (inv_item->object_number)
	{
		case MAP_OPTION:
			do_compass_option(inv_item);
			break;

		case GUN_OPTION:
		case SHOTGUN_OPTION:
		case MAGNUM_OPTION:
		case UZI_OPTION:
		case HARPOON_OPTION:
		case M16_OPTION:
		case ROCKET_OPTION:
		case GRENADE_OPTION:
			inputDB|=SELECT_ITEM;
			break;

		case GUN_AMMO_OPTION:
		case SG_AMMO_OPTION:
		case MAG_AMMO_OPTION:
		case UZI_AMMO_OPTION:
		case HARPOON_AMMO_OPTION:
		case M16_AMMO_OPTION:
		case ROCKET_AMMO_OPTION:
		case GRENADE_AMMO_OPTION:
			break;

		case MEDI_OPTION:
		case BIGMEDI_OPTION:
		case PUZZLE_OPTION1:
		case PUZZLE_OPTION2:
		case PUZZLE_OPTION3:
		case PUZZLE_OPTION4:
		case KEY_OPTION1:
		case KEY_OPTION2:
		case KEY_OPTION3:
		case KEY_OPTION4:
			inputDB |= SELECT_ITEM;
			break;

		case PICKUP_OPTION1:
		case PICKUP_OPTION2:
			do_pickup_option(inv_item);
			break;

		/* OPTIONS */
		case PASSPORT_OPTION:
			do_passport_option(inv_item);
			break;

		case DETAIL_OPTION:
			do_detail_option(inv_item);
			break;

		case SOUND_OPTION:
			do_sound_option(inv_item);
			break;

		case CONTROL_OPTION:
			do_control_option(inv_item);
			break;

		case GAMMA_OPTION:
			do_levelselect_option(inv_item);
			break;

		case SAVEGAME_CRYSTAL_OPTION:
			if (CurrentLevel != LV_GYM)
			{
				if (savegame.WorldRequired)
					SortOutAdventureSave(Level2World(CurrentLevel));

				savegame.mid_level_save = 1;
			    	Inv_RemoveItem(SAVEGAME_CRYSTAL_OPTION);

				savegame.cp_secrets = savegame.secrets;
				savegame.cp_timer = savegame.timer;
				savegame.cp_kills = savegame.kills;
				savegame.cp_distance_travelled = savegame.distance_travelled;
				savegame.cp_ammo_used = savegame.ammo_used;
				savegame.cp_ammo_hit = savegame.ammo_hit;
				savegame.cp_health_used = savegame.health_used;

				CreateSaveGameInfo();
			}
			else
			{
				copy_secrets = savegame.secrets;
				copy_timer = savegame.timer;
				copy_kills = savegame.kills;
				copy_distance_travelled = savegame.distance_travelled;
				copy_ammo_used = savegame.ammo_used;
				copy_ammo_hit = savegame.ammo_hit;
				copy_health_used = savegame.health_used;

				savegame.secrets = savegame.cp_secrets;
				savegame.timer = savegame.cp_timer;
				savegame.kills = savegame.cp_kills;
				savegame.distance_travelled = savegame.cp_distance_travelled;
				savegame.ammo_used = savegame.cp_ammo_used;
				savegame.ammo_hit = savegame.cp_ammo_hit;
				savegame.health_used = savegame.cp_health_used;
			}

			mem = S_SaveGame(&savegame, sizeof(SAVEGAME_INFO), 1);

			if (CurrentLevel == LV_GYM)
			{
				savegame.secrets = copy_secrets;
				savegame.timer = copy_timer;
				savegame.kills = copy_kills;
				savegame.distance_travelled = copy_distance_travelled;
				savegame.ammo_used = copy_ammo_used;
				savegame.ammo_hit = copy_ammo_hit;
				savegame.health_used = copy_health_used;
			}

			if ((CurrentLevel != LV_GYM)
			&& ((mem == MCF_NONE)
			||  (mem == MCF_FAILED)))
				Inv_AddItem(SAVEGAME_CRYSTAL_OPTION);

			inputDB |= SELECT_ITEM;
			break;

		default:
	    		if (inputDB&(DESELECT_ITEM|SELECT_ITEM))
			{
				inv_item->goal_frame = 0;
				inv_item->anim_direction = -1;
			}
			return;
	}
}

/********************************************************************/
/* DETAIL OPTION */
/********************************************************************/
void	do_detail_option(INVENTORY_ITEM *inv_item)
{
	static int x, y;
	static TEXTSTRING *detailtext1=NULL;
	static TEXTSTRING *detailtext2=NULL;

	if (detailtext1 == NULL)
	{
		x = savegame.screen_x;
		y = savegame.screen_y;
		S_InitFade(FADE_MAX, FADE_MID, 0);
		detailtext1 = T_Print( 0,-10,0, GF_PSXStrings[PSSTR_MOVESTR0] );
		detailtext2 = T_Print( 0,+10,0, GF_PSXStrings[PSSTR_MOVESTR1] );
		T_CentreH( detailtext1,1 );
		T_CentreV( detailtext1,1 );
		T_CentreH( detailtext2,1 );
		T_CentreV( detailtext2,1 );
	}

	S_control_screen_position();

	if ( inputDB&(DESELECT_ITEM|SELECT_ITEM) )
	{
		if (inputDB & DESELECT_ITEM)
		{
			savegame.screen_x = x;
			savegame.screen_y = y;
			GPU_SetScreenPosition(savegame.screen_x,savegame.screen_y);
		}

		T_RemovePrint(detailtext1);
		T_RemovePrint(detailtext2);
		detailtext1 = NULL;
		detailtext2 = NULL;
		S_InitFade(FADE_MID, FADE_MAX, 0);
	}
}

/********************************************************************/
/* PASSPORT OPTION */
/********************************************************************/
void	do_passport_option(INVENTORY_ITEM *inv_item)
{
	sint16 page;
	sint32 select;
	int level, memcard_status;

	T_RemovePrint(Inv_itemText[IT_NAME]);
	Inv_itemText[IT_NAME] = NULL;

	page = (inv_item->goal_frame-inv_item->open_frame)/5;
	if ((inv_item->goal_frame-inv_item->open_frame)%5)
		page = -1;

/* Cancel any attempts to change page if SAVE/LOAD quick keys */
	if (gameflow.loadsave_disabled)
		inputDB &= ~(PASSPORT_2FRONT|PASSPORT_2BACK);

// Do Passport requesters
	switch (page)
	{
	/* ------------------------------ */
		case 0:	// LOAD GAME
	/* ------------------------------ */
			if ((!MemCardAvailable)
			||  (!SavedGames)
			||  (gameflow.loadsave_disabled))
				inputDB = PASSPORT_2BACK;

		// TEXT FOR FIRST PAGE DEPENDING ON MODE
			if (!passportLR)
				generateLR(GF_GameStrings[GT_LOADGAME], 0, 1);

		// LOAD GAME REQUESTER
			if (passport_mode == 1)
			{
				T_RemovePrint(passportLR);
				passportLR = NULL;

				if (Inventory_Mode == INV_DEATH_MODE)
					DisplayDeselectText();

				if ((select = Display_Requester(&Load_Game_Requester, 1, 1)))
				{
					if (select > 0)
					{
						level = saved_levels[select-1];

					// Load current mid-level position
						if (level < 0)
						{
							Inventory_ExtraData[1] = level;
		    					passport_mode = 0;
							CreateLTab();
						}

						else if ((level > 0) && (level<(gameflow.num_levels-gameflow.num_demos)))
						{
   							Inventory_ExtraData[1] = level;
							passport_mode = 0;
							CreateLTab();
						}

						else
							input = inputDB = 0;
					}
				// User cancelled so revert to previous setting
					else
					{
						S_MemCpy(&savegame, &LTab, sizeof(SAVEGAME_INFO));
						CreateLTab();

//## Twats				S_MemCpy(&savegame, savegame_backup, sizeof(SAVEGAME_INFO));
						passport_mode = 0;
						input =0;
						inputDB = 0;

						if (Inventory_Mode == INV_DEATH_MODE)
							RemoveDeselectText();
					}
				}

				else
				{
					input = 0;
					inputDB = 0;
				}
			}

			else if (passport_mode == 0)
			{
				if (inputDB & SELECT_ITEM)
				{
#ifdef GAMEDEBUG
					printf("Written over LTab\n");
#endif
					S_MemCpy(&LTab, &savegame, sizeof(SAVEGAME_INFO));

					T_RemovePrint(passportLR);
					passportLR = NULL;

					T_RemovePrint(Inv_ringText);
					Inv_ringText = NULL;

					T_RemovePrint(Inv_itemText[IT_NAME]);
					Inv_itemText[IT_NAME] = NULL;

					RemovePromptText();
					memcard_status = LoadSaveGameInfo();

					if (memcard_status==MCF_LOADED)
					{
#ifdef GAMEDEBUG
				    		printf("MCF_LOADED.\n");
#endif
						//Load OK
						Load_Game_Requester.ypos = -44;
						Init_Requester(&Load_Game_Requester);             // Initialise Requester
						SetRequesterHeading( &Load_Game_Requester, GF_GameStrings[GT_LOADGAME], R_CENTRE, NULL,0);
						GetSavedGamesList(&Load_Game_Requester);
						passport_mode=1;                                  // flag we need to bring up savegames
					}
					else
					{
#ifdef GAMEDEBUG
				    		printf("Not MCF_LOADED!\n");
#endif
						CreateLTab();
						//Error loading savegame or chose to quit.
					}
					input = 0;
					inputDB = 0;
				}
			}

			else if (passport_mode==2)
			{
//				if (loadok_count>0)
//					loadok_count++;
//				else
//				{
//					T_RemovePrint(loadok);
//					loadok = NULL;
//					passport_mode = 0;
//				}
//				input = 0;
//				inputDB = 0;
			}
			break;

	/* ------------------------------ */
		case 1:		//	 START/SAVE GAME
	/* ------------------------------ */
			if (((gameflow.loadsave_disabled)
			&&  (Inventory_Mode != INV_DEATH_MODE)
			&&  (Inventory_Mode != INV_TITLE_MODE)
			&&  (Inventory_Mode != INV_LEVELSELECT_MODE))
			||  (Inventory_Mode == INV_GAME_MODE))
				inputDB = PASSPORT_2BACK;

		// Print Text for centre pages
        		if (!passportLR)
        		{
	        		if ((Inventory_Mode == INV_TITLE_MODE || CurrentLevel == LV_GYM) && Inventory_Mode != INV_LEVELSELECT_MODE)
				{
					if (gameflow.loadsave_disabled)
						generateLR(GF_GameStrings[GT_STARTGAME], 0, 0);
					else
						generateLR(GF_GameStrings[GT_STARTGAME], 1, 0);
				}

				else if (Inventory_Mode==INV_DEATH_MODE)
					generateLR(GF_GameStrings[GT_RESTARTLEVEL], 1, 1);
				else
#if defined(FULL_SAVE)
					generateLR(GF_GameStrings[GT_SAVEGAME], 1, 1);
#else
					generateLR(GF_GameStrings[GT_RESTARTLEVEL], 1, 1);
#endif
			}

#if defined(FULL_SAVE)
			if (((Inventory_Mode == INV_TITLE_MODE)
			||   (CurrentLevel == LV_GYM))
			&&   (Inventory_Mode != INV_LEVELSELECT_MODE))
			{
#endif
	   			if (gameflow.play_any_level)
   				{
   					if (passport_mode == 1)
   					{
	   					if ((select = Display_Requester(&Level_Select_Requester,1,1)))
   						{
   							if (select > 0)
   							{
			  					InitialiseStartInfo();
								CurrentLevel = Inventory_ExtraData[1] = select;
   								passport_mode = 0;
	   						}
   							else
   							{
   								passport_mode = 0;
   								input = 0;
   								inputDB = 0;
   							}
	   					}
   						else
   						{
   							input = 0;
   							inputDB = 0;
   						}
	   				}
   					else if (passport_mode == 0)
   					{
   						if (inputDB & SELECT_ITEM)
   						{
							Level_Select_Requester.ypos = -44;
	   						Init_Requester(&Level_Select_Requester);
   							SetRequesterHeading( &Level_Select_Requester, GF_GameStrings[GT_SELECTLEVEL], R_CENTRE, NULL,0);
   							GetValidLevelsList(&Level_Select_Requester);
   							passport_mode=1;
   							input =0;
   							inputDB = 0;
	   					}
   					}
   				}
   				else
	   			{
#ifdef FULL_SAVE
	   				// START GAME
  					InitialiseStartInfo();
  		  			Inventory_ExtraData[1]=LV_FIRSTLEVEL;
#else
   					if ( Inventory_Mode!=INV_TITLE_MODE || (CurrentLevel>=0 && CurrentLevel<=gameflow.num_levels) )
	   					// RESTART LEVEL
   		  				Inventory_ExtraData[1]=CurrentLevel;
   					else
   					{
		   				// START GAME
   						InitialiseStartInfo();
   		  				Inventory_ExtraData[1]=LV_FIRSTLEVEL;
   					}
#endif
				}
#if defined(FULL_SAVE)
			}
			else if (Inventory_Mode != INV_DEATH_MODE)
			{
			/* -------- save game */

				if (inputDB & IN_SELECT)
				{
					RemovePromptText();		//##
					savegame.mid_level_save = (Inventory_Mode == INV_LEVELSELECT_MODE);
					savegame.AfterAdventureSave = (Inventory_Mode == INV_LEVELSELECT_MODE);
					CreateSaveGameInfo();
					memcard_status = S_SaveGame(&savegame, sizeof(SAVEGAME_INFO), 1);
				}
			}
#endif
			break;

		case 2:		// EXIT / MISC
            // Text for last pages
        	if (!passportLR)
        	{
        		if (Inventory_Mode==INV_TITLE_MODE)
					generateLR( GF_GameStrings[GT_EXITGAME],1,0 );
        		else if (gameflow.demoversion)
				{
					if (gameflow.loadsave_disabled)
						generateLR( GF_GameStrings[GT_EXITDEMO],0,0 );
					else
						generateLR( GF_GameStrings[GT_EXITDEMO],1,0 );
				}
				else
					generateLR( GF_GameStrings[GT_EXIT2TITLE],1,0 );
        	}
			break;
	}

	if (inputDB&PASSPORT_2FRONT)
	{
		inv_item->anim_direction = -1;

		inv_item->goal_frame-=5 + ((Inventory_Mode == INV_GAME_MODE) ? 5 : 0);


		if ( !MemCardAvailable || !SavedGames )
		{
			if (inv_item->goal_frame < (inv_item->open_frame+5))
				inv_item->goal_frame = (inv_item->open_frame+5);
			else if (passportLR)
    			{
	//				printf("Rem3");
                	T_RemovePrint(passportLR);
                	passportLR = NULL;
    			}
		}
		else
		{
			if (inv_item->goal_frame < inv_item->open_frame)
				inv_item->goal_frame = inv_item->open_frame;
            else
            {
                SoundEffect(115, NULL, SFX_ALWAYS);
	            if (passportLR)
    			{
//					printf("Rem4");
        	        T_RemovePrint(passportLR);
    	            passportLR = NULL;
	    		}
            }
		}
		input = 0;
		inputDB = 0;
	}
	if (inputDB&PASSPORT_2BACK)
	{
        // IF CONSOLE VERSION DON'T DO LAST PAGE ON TITLE SCREEN
		if (!(Inventory_Mode==INV_TITLE_MODE && page==1))
		{
			inv_item->anim_direction = 1;
			inv_item->goal_frame+=5;
			if (inv_item->goal_frame > inv_item->frames_total-6)
				inv_item->goal_frame = inv_item->frames_total-6;
			else
			{
				SoundEffect(115, NULL, SFX_ALWAYS);
				if (passportLR)
				{
//					printf("Rem5");
					T_RemovePrint(passportLR);
					passportLR = NULL;
				}
			}
			input = 0;
			inputDB = 0;
		}
	}
	if (inputDB&DESELECT_ITEM)
	{
		if (Inventory_Mode==INV_DEATH_MODE)
		{
			input = 0;
			inputDB = 0;
		}
		else
		{
			if (page == 2)
			{
				inv_item->goal_frame = inv_item->frames_total-1;
				inv_item->anim_direction = 1;
			}
			else
			{
				inv_item->goal_frame = 0;
				inv_item->anim_direction = -1;
			}
			if (passportLR)
			{
//					printf("Rem6");
				T_RemovePrint(passportLR);
				passportLR = NULL;
			}
		}
	}


	if (inputDB&SELECT_ITEM)
	{
		if (inputDB&SELECT_ITEM)
		{
			// SELECTED
			Inventory_ExtraData[0] = page; // indicate page number
		}
		if (page == 2)
		{
			inv_item->goal_frame = inv_item->frames_total-1;
			inv_item->anim_direction = 1;
		}
		else
		{
			inv_item->goal_frame = 0;
			inv_item->anim_direction = -1;
		}
		if (passportLR)
		{
			T_RemovePrint(passportLR);
			passportLR = NULL;
		}
	}

}

/********************************************************************/
/* SOUND OPTION */
/********************************************************************/

void	do_sound_option(INVENTORY_ITEM *inv_item)
{
	char	str[20];
	int muslevel,sfxlevel;
	static int mus, sfx;

//	if (Option_SoundFX_Volume == 0)
//		Option_SoundFX_Volume = 192;
//	if (Option_Music_Volume == 0)
//		Option_Music_Volume = 192;

#ifndef SFX_TEST

	if (!sfxvoltxt)
	{
		S_InitFade(FADE_MAX, FADE_MID, 0);

		sfx = Option_SoundFX_Volume;

		sprintf(str, "|");
#ifdef NTSC
		sfxvoltxt = T_Print(-120, 8, 0, str);
#else
		sfxvoltxt = T_Print(-120, 8-8, 0, str);
#endif
		T_CentreH(sfxvoltxt,1);
		T_CentreV(sfxvoltxt,1);
	}


	if (!cdvoltxt)
	{
		mus = Option_Music_Volume;

		sprintf(str, "}");
#ifdef NTSC
		cdvoltxt = T_Print(-120, 32, 0, str);
#else
		cdvoltxt = T_Print(-120, 32-8, 0, str);
#endif
		T_CentreH(cdvoltxt,1);
		T_CentreV(cdvoltxt,1);
	}

	if (inputDB & PREV_HBAR)
		item_data = 0;

	if (inputDB & NEXT_HBAR)
		item_data = 1;

	muslevel = (Option_Music_Volume*100)/256;
	sfxlevel = (Option_SoundFX_Volume*100)/256;

	if (!item_data)
	{
		cdvoltxt->Colour = 2;
		sfxvoltxt->Colour = 3;

		S_DrawGouraudBar(148, 118, 199, muslevel*2, &hiBarColourSet);
		S_DrawGouraudBar(148, 142, 199, sfxlevel*2, &loBarColourSet);

		if (input & DEC_HBAR)
		{
			Option_Music_Volume -= 4;

			if (Option_Music_Volume < MIN_SOUND_LEVEL)
				Option_Music_Volume = MIN_SOUND_LEVEL;
		}

		else if (input & INC_HBAR)
		{
			Option_Music_Volume += 4;

			if (Option_Music_Volume > MAX_SOUND_LEVEL)
				Option_Music_Volume = MAX_SOUND_LEVEL;
		}
#if defined(CD_LOAD) || defined(PLAY_CDTRACKS)
	CDDA_SetMasterVolume(Option_Music_Volume);
#endif

	}
	else
	{
		cdvoltxt->Colour = 3;
		sfxvoltxt->Colour = 2;

		S_DrawGouraudBar(148, 118, 199, muslevel*2, &loBarColourSet);
		S_DrawGouraudBar(148, 142, 199, sfxlevel*2, &hiBarColourSet);

		if (input & DEC_HBAR)
		{
			Option_SoundFX_Volume -= 4;

			if (Option_SoundFX_Volume < MIN_SOUND_LEVEL)
				Option_SoundFX_Volume = MIN_SOUND_LEVEL;
		}

		else if (input & INC_HBAR)
		{
			Option_SoundFX_Volume += 4;

			if (Option_SoundFX_Volume > MAX_SOUND_LEVEL)
				Option_SoundFX_Volume = MAX_SOUND_LEVEL;
		}

		SoundEffect(337, NULL, SFX_ALWAYS);
	}

	if ((input & (DEC_HBAR|INC_HBAR))
	&&  (inputDB & (IN_SELECT|IN_DESELECT)))
		inputDB &= ~(IN_SELECT|IN_DESELECT);

	if ((!(input & (DEC_HBAR|INC_HBAR)))
	&&  (input & (IN_DESELECT|IN_SELECT)))
	{
		if (input & IN_DESELECT)
		{
			Option_SoundFX_Volume = sfx;
			Option_Music_Volume = mus;
		}

		if (sfxvoltxt)
		{
			S_InitFade(FADE_MID, FADE_MAX, 0);

			T_RemovePrint(sfxvoltxt);
			sfxvoltxt = NULL;
		}

		if (cdvoltxt)
		{
			T_RemovePrint(cdvoltxt);
			cdvoltxt = NULL;
		}
	}

#if defined(PLAY_CDTRACKS) && defined(GAMEDEBUG)

/* -------- Chris's sound test */

	{
		static int track = 2;
		static TEXTSTRING *PosTxt = NULL;
		static TEXTSTRING *TrackTxt = NULL;

	/* -------- initialise display */

		if (TrackTxt == NULL)
		{
			sprintf(str, "Track:%d", track);
			TrackTxt = T_Print(0, -64, 1, str);
			T_CentreH(TrackTxt, 1);
			T_CentreV(TrackTxt, 1);

			sprintf(str, "Cur:%d  End:%d", XACurPos, XAEndPos);
			PosTxt = T_Print(0, -48, 1, str);
			T_CentreH(PosTxt, 1);
			T_CentreV(PosTxt, 1);

			S_CDPlay(track, 1);
		}

	/* -------- change stuff */

		if (inputDB & IN_DASH)
			CdControl(CdlForward, 0, 0);

		if (inputDB & IN_DUCK)
			CdControl(CdlBackward, 0, 0);

		if (inputDB & IN_JUMP)
			CdControl(CdlReadS, 0, 0);

		if (inputDB & IN_SLOW)
			track++;

		if (inputDB & IN_LOOK)
			track--;

		if (inputDB & (IN_SLOW|IN_LOOK))
		{
			sprintf(str, "Track:%d", track);
			T_ChangeText(TrackTxt, str);

			S_CDPlay(track, 1);
		}

		sprintf(str, "Cur:%d  End:%d", XACurPos, XAEndPos);
		T_ChangeText(PosTxt, str);

	/* -------- clear display */

		if (cdvoltxt == NULL)
		{
			T_RemovePrint(TrackTxt);
			TrackTxt = NULL;
			T_RemovePrint(PosTxt);
			PosTxt = NULL;
		}
	}
#endif

#else
	Option_SoundFX_Volume = 255;

	if (sndtext==NULL)
	{
		sfx_num = 0;
		sndtext = T_Print(0,40,0,"SFX: 0");
		T_CentreH(sndtext,1);
		pantext = T_Print(0,60,0,"PAN: 0");
		T_CentreH(pantext,1);
	}
	if (inputDB&IN_FORWARD)
	{
		sfx_num--;
		if (sfx_num<0) sfx_num=175;
		if ( sample_lut[sfx_num]<0 )
			sprintf(str, "SFX: %d NP", sfx_num );
		else
			sprintf(str, "SFX: %d", sfx_num );
		T_ChangeText(sndtext, str);
	}
	if (inputDB&IN_BACK)
	{
		sfx_num++;
		if ( sfx_num>175 ) sfx_num = 0;
		if ( sample_lut[sfx_num]<0 )
			sprintf(str, "SFX: %d NP", sfx_num );
		else
			sprintf(str, "SFX: %d ", sfx_num);
		T_ChangeText(sndtext, str);
	}
	if (inputDB&IN_LEFT)
	{
		pan_qty-=0x100;
		sprintf(str, "PAN: %d   ",pan_qty);
		T_ChangeText(pantext, str);
	}
	if (inputDB&IN_RIGHT)
	{
		pan_qty+=0x100;
		sprintf(str, "PAN: %d   ",pan_qty);
		T_ChangeText(pantext, str);
	}

	if ((inputDB&SELECT_ITEM))
	{
		SoundEffect(sfx_num, NULL, SFX_ALWAYS);
//		S_SoundPlaySample(sfx_num, 0x7fff, 100, pan_qty);
		input = 0;
		inputDB = 0;
//		inv_item->goal_frame = 0;
//		inv_item->anim_direction = -1;
	}
	if (inputDB & DESELECT_ITEM)
	{
		T_RemovePrint(sndtext);
		sndtext = NULL;
		T_RemovePrint(pantext);
		pantext = NULL;
		T_RemovePrint(musvoltxt);
		musvoltxt = NULL;
		T_RemovePrint(cdvoltxt);
		cdvoltxt = NULL;
	}
	if ((inputDB&SELECT_ITEM))
	{
		input = 0;
		inputDB = 0;
	}
#endif
}

/******************************************************************************
 *				Do Compass option in Inventory
 *****************************************************************************/
void	do_compass_option(INVENTORY_ITEM *inv_item)
{
	int	h, m, s;
	char string[100];

	RemoveSelectText();
	inputDB &= ~SELECT_ITEM;

	s = savegame.timer/30;
	h = s/3600;
	m = (s/60)%60;
	s = s%60;

	sprintf(string, "%02d:%02d:%02d", h, m, s);

	if (CurrentLevel==LV_GYM)
	{
		NoInventoryItemDraw = 1;
		inv_item->drawn_meshes = 0;
		ShowGymStatsText(string,1);
	}
	else
		ShowStatsText(string,1);

	if (inputDB & (DESELECT_ITEM))
	{
		NoInventoryItemDraw = 0;
		inv_item->goal_frame = inv_item->frames_total-1;
		inv_item->anim_direction = 1;
	}
}

void	do_pickup_option(INVENTORY_ITEM *inv_item)
{
	if (inputDB & (DESELECT_ITEM|SELECT_ITEM))
	{
		inv_item->goal_frame = inv_item->frames_total-1;
		inv_item->anim_direction = 1;
	}
}

/******************************************************************************
 *			Display Square to DeSelect
 *****************************************************************************/
void	DisplayDeselectText()
{
	if ( deseltext==NULL )
	{
		deseltext = T_Print(-25, -40, IT_GREY, GF_PSXStrings[PSSTR_DESELECT_TEXT]);
		T_BottomAlign(deseltext, 1);
		T_RightAlign(deseltext, 1);
	}
}
/*******************************************************************************
 *			Remove Display square to DeSelect text
 ******************************************************************************/
void	RemoveDeselectText()
{
	if (deseltext)
	{
		T_RemovePrint(deseltext);
		deseltext = NULL;
	}
}
/********************************************************************************
 *			Display X to Continue prompt
 *******************************************************************************/
void	DisplayContinueText()
{
	if (continuetext == NULL)
	{
		continuetext = T_Print(0, -40, 2, GF_PSXStrings[PSSTR_CONTINUE_TEXT]);
		T_BottomAlign(continuetext, 1);
		T_CentreH(continuetext, 1);
	}
}
/*******************************************************************************
 *			remove Display square to DeSelect text
 ******************************************************************************/
void	RemoveContinueText()
{
	if (continuetext)
	{
		T_RemovePrint(continuetext);
		continuetext = NULL;
	}
}
/******************************************************************************
 *			Display X to Select
 *****************************************************************************/
void	DisplaySelectText( void )
{
	if ( seltext==NULL )
	{
		seltext = T_Print(25, -40, IT_GREY, GF_PSXStrings[PSSTR_SELECT_TEXT]);
		T_BottomAlign(seltext, 1);
	}
}

/*********************************************************************************
 *				Remove All possible PSX Prompts
 ********************************************************************************/
void	RemoveArrowText()
{
	if (Inv_upArrow1)
	{
		T_RemovePrint( Inv_upArrow1 );
		Inv_upArrow1 = NULL;
	}
	if ( Inv_upArrow2 )
	{
		T_RemovePrint( Inv_upArrow2 );
		Inv_upArrow2 = NULL;
	}
	if ( Inv_downArrow1 )
	{
		T_RemovePrint( Inv_downArrow1 );
		Inv_downArrow1 = NULL;
	}
	if ( Inv_downArrow2 )
	{
		T_RemovePrint( Inv_downArrow2 );
		Inv_downArrow2 = NULL;
	}
}

/***********************************************************************************
 *						Display the Arrow Text
 **********************************************************************************/
void	DisplayArrowText( RING_INFO *ring )
{
	if ( Inventory_Mode==INV_TITLE_MODE || Inventory_Mode == INV_KEYS_MODE || Inventory_Mode==INV_DEATH_MODE)
		return;

	if (!Inv_upArrow1)
	{
		if (ring->type == OPTION_RING || (ring->type == MAIN_RING && inv_keys_objects))
		{
#ifdef PSX_VERSION
			Inv_upArrow1 = T_Print(40,28,2,"[");
			Inv_upArrow2 = T_Print(-40,28,2,"[");
#else
			Inv_upArrow1 = T_Print(20,28,2,"[");
			Inv_upArrow2 = T_Print(-20,28,2,"[");
#endif
			T_RightAlign(Inv_upArrow2, 1);
		}
	}

	if (!Inv_downArrow1)
	{
		if (ring->type == MAIN_RING || ring->type == KEYS_RING)
		{
#ifdef PSX_VERSION
			Inv_downArrow1 = T_Print(40,-15,2,"]");
			Inv_downArrow2 = T_Print(-40,-15,2,"]");
#else
			Inv_downArrow1 = T_Print(20,-15,2,"]");
			Inv_downArrow2 = T_Print(-20,-15,2,"]");
#endif
			T_BottomAlign(Inv_downArrow1, 1);
			T_BottomAlign(Inv_downArrow2, 1);
			T_RightAlign(Inv_downArrow2, 1);
		}
	}
}

void DisplayControlArrows()
{
	RemoveArrowText();

	if (!Inv_upArrow1 && ControlVibrate && DualShock)
	{
#ifdef PSX_VERSION
		Inv_upArrow1 = T_Print(40,28,2,"[");
		Inv_upArrow2 = T_Print(-40,28,2,"[");
#else
		Inv_upArrow1 = T_Print(20,28,2,"[");
		Inv_upArrow2 = T_Print(-20,28,2,"[");
#endif
		T_RightAlign(Inv_upArrow2, 1);
	}

	if (!Inv_downArrow1 && !ControlVibrate && DualShock)
	{
#ifdef PSX_VERSION
		Inv_downArrow1 = T_Print(40,-15,2,"]");
		Inv_downArrow2 = T_Print(-40,-15,2,"]");
#else
		Inv_downArrow1 = T_Print(20,-15,2,"]");
		Inv_downArrow2 = T_Print(-20,-15,2,"]");
#endif
		T_BottomAlign(Inv_downArrow1, 1);
		T_BottomAlign(Inv_downArrow2, 1);
		T_RightAlign(Inv_downArrow2, 1);
	}
}

