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

#include "standard.h"
#include "global.h"
#include "input.h"


#include "..\game\invdata.h"
#include "..\game\objects.h"
#include "..\game\inventry.h"
#include "..\game\gameflow.h"
#include "..\game\title.h"
#include "..\game\sound.h"
#include "..\game\savegame.h"


int keychange = 0, select_key = 0;
int iconfig = 0;

int detail_level = 1;

#define NUM_CONTROLS	15


TEXTSTRING *ctext[NUM_CONTROLS]={NULL};							   
TEXTSTRING *btext[NUM_CONTROLS]={NULL};

#define JOY_KEY 256

/*********************************** PROTOTYPES *******************************************/

void	do_control_option(INVENTORY_ITEM *inv_item);

void S_ShowControls(void);
void S_ChangeCtrlText(void);
void S_RemoveCtrlText(void);
void S_DrawCtrlLines(void);


/*********************************** FUNCTION CODE *******************************************/

/********************************************************************/
/* Which Invenrtory Option */
/********************************************************************/
sint32	m_status = 0;

void	do_inventory_options(INVENTORY_ITEM *inv_item)
{
#if 0
#if CODER==JASON && defined(GAMEDEBUG)
	if (input&IN_JUMP && input&IN_BACK)
	{
		inv_item->x_rot-=0x200;
		inv_item->x_rot_sel-=0x200;
	}
	if (input&IN_JUMP && input&IN_FORWARD)
	{
		inv_item->x_rot+=0x200;
		inv_item->x_rot_sel+=0x200;
	}
	if (input&IN_JUMP && input&IN_LEFT)
	{
		inv_item->y_rot+=0x200;
		inv_item->y_rot_sel+=0x200;
	}
	if (input&IN_JUMP && input&IN_RIGHT)
	{
		inv_item->y_rot-=0x200;
		inv_item->y_rot_sel-=0x200;
	}
	sprintf(exit_message,"xrot:%d yrot:%d",inv_item->x_rot,inv_item->y_rot);
	PrintDbug( 10,8, exit_message );
	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,"ytrans:%d ztrans:%d",inv_item->ytrans,inv_item->ztrans);
	PrintDbug( 10,9, exit_message );
#endif
#endif
	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:
			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:
			break;

		case MEDI_OPTION:
		case BIGMEDI_OPTION:
		case PICKUP_OPTION1:
		case PICKUP_OPTION2:
		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;

		/* OPTIONS */
		case PASSPORT_OPTION:
			do_passport_option(inv_item);
			break;
		case GAMMA_OPTION:
			do_gamma_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;
		default:
			if (inputDB&SELECT_ITEM || inputDB&DESELECT_ITEM)
			{
				inv_item->goal_frame = 0;
				inv_item->anim_direction = -1;
			}
			break;
	}
}


/********************************************************************/
/* PASSPORT OPTION */
/********************************************************************/

TEXTSTRING *passport_text1=NULL;


int	passport_mode = 0;

sint16	sam_num = 0;
sint16	normal_pitch = 0x1700;

void	do_passport_option(INVENTORY_ITEM *inv_item)
{
	sint16	page;
	sint32	select;

	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 (Inventory_Mode == INV_LOAD_MODE || Inventory_Mode == INV_SAVE_MODE || gameflow.loadsave_disabled)
		inputDB &= ~(PASSPORT_2FRONT|PASSPORT_2BACK);

	// Do Passport requesters
	switch (page)
	{
		case 0:		// LOAD GAME
			if (gameflow.loadsave_disabled)
			{
				inputDB = PASSPORT_2BACK;
				break;
			}
	   		if (passport_mode==1)
			{
				SetPCRequesterSize(&Load_Game_Requester,10,-32);

//				height = GetRenderHeight();
//				lines = (height/2) / (TEXT_HEIGHT+7);
//				if (lines>10)
//					lines = 10;
//				Load_Game_Requester.ypos = -32;
//				Load_Game_Requester.vis_lines = lines;
/*				if (height>=400)
				{
					Load_Game_Requester.ypos = -80;
					Load_Game_Requester.vis_lines = 10;
				}
				else
				{
					Load_Game_Requester.ypos = -20;
					Load_Game_Requester.vis_lines = 5;
				}
*/
				select = Display_Requester(&Load_Game_Requester,1,1);
				if (select)
				{
					if (select > 0)
						Inventory_ExtraData[1] = select-1;
//					else if (Inventory_Mode != INV_SAVE_MODE && Inventory_Mode != INV_LOAD_MODE)
//						input = inputDB = 0;
					passport_mode = 0;
				}
				else if (inputDB&PASSPORT_2BACK)
				{
					Remove_Requester(&Load_Game_Requester);
					passport_mode=0;
				}
				else
					input = inputDB = 0;
			}
			else if (passport_mode==0)
			{
				if (!SavedGames || Inventory_Mode==INV_SAVE_MODE)
					inputDB = PASSPORT_2BACK;
				else
				{
	            // TEXT FOR FIRST PAGE DEPENDING ON MODE
		        	if (!passport_text1)
   		     		{
        				passport_text1 = T_Print(0,-16,IT_BRONZE, GF_GameStrings[GT_LOADGAME]);
	        			T_BottomAlign(passport_text1, 1);
   	     				T_CentreH(passport_text1, 1);
		        	}

//					if (inputDB&SELECT_ITEM || Inventory_Mode==INV_LOAD_MODE)
//					{
						T_RemovePrint(Inv_levelText);	Inv_levelText=NULL;
						T_RemovePrint(Inv_tagText);	Inv_tagText=NULL;
						T_RemovePrint(Inv_ringText);	Inv_ringText=NULL;
						T_RemovePrint(Inv_itemText[IT_NAME]);	Inv_itemText[IT_NAME]=NULL;
//						Init_Requester(&Load_Game_Requester);
						GetSavedGamesList(&Load_Game_Requester);
						SetRequesterHeading( &Load_Game_Requester, GF_GameStrings[GT_LOADGAME], R_CENTRE, NULL,0);
	    				passport_mode=1;
						input = 0;
						inputDB = 0;
//					}
				}
			}
			break;
		case 1:		// START / SAVE
   		// SAVE GAME REQUESTER
			if (gameflow.loadsave_disabled)
			{
					inputDB = PASSPORT_2BACK;
					break;
			}
			
			if (passport_mode==1 || passport_mode==2)
			{
/*				if (GetRenderHeight()>=400)
  				{
  					Load_Game_Requester.ypos = -80;
  					Load_Game_Requester.vis_lines = 10;
  				}
  				else
  				{
  					Load_Game_Requester.ypos = -20;
  					Load_Game_Requester.vis_lines = 5;
  				}
*/
				if (passport_mode==1)
				{
					
					SetPCRequesterSize(&Load_Game_Requester,10,-32);
					select = Display_Requester(&Load_Game_Requester,1,1);
				}
				else
				{
					SetPCRequesterSize(&Level_Select_Requester,10,-32);
					select = Display_Requester(&Level_Select_Requester,1,1);
				}

				if (select)
				{
					if (select >0)
       					Inventory_ExtraData[1] = select-1;
//					else if (Inventory_Mode != INV_SAVE_MODE && Inventory_Mode != INV_LOAD_MODE)
//						input = inputDB = 0;
  					passport_mode = 0;
				}
				else if (inputDB&(PASSPORT_2FRONT|PASSPORT_2BACK))
				{
					if (passport_mode==1)
						Remove_Requester(&Load_Game_Requester);
					else
						Remove_Requester(&Level_Select_Requester);
					passport_mode = 0;
				}
				else
					input = inputDB = 0;
			}
			else if (passport_mode==0)
			{
				if (Inventory_Mode == INV_DEATH_MODE)
				{
					if (inv_item->anim_direction == -1)
						inputDB = PASSPORT_2FRONT;
					else
						inputDB = PASSPORT_2BACK;
				}
				else
				{
		        	if (!passport_text1)
	   	     		{
						if (Inventory_Mode==INV_TITLE_MODE || CurrentLevel==LV_GYM)
		        			passport_text1 = T_Print(0,-16,IT_BRONZE,GF_GameStrings[GT_STARTGAME]);
						else
		        			passport_text1 = T_Print(0,-16,IT_BRONZE, GF_GameStrings[GT_SAVEGAME]);
		        		T_BottomAlign(passport_text1, 1);
	   	     			T_CentreH(passport_text1, 1);
		        	}

//					if (inputDB&SELECT_ITEM || Inventory_Mode==INV_SAVE_MODE)
//					{
						/* Is this save game? Or is it start new game? */
						if (Inventory_Mode==INV_TITLE_MODE || CurrentLevel==LV_GYM)
						{
							if (gameflow.play_any_level)
							{
								// Init SELECT-A-LEVEL Requester
								T_RemovePrint(Inv_levelText);	Inv_levelText=NULL;
								T_RemovePrint(Inv_tagText);	Inv_tagText=NULL;
								T_RemovePrint(Inv_itemText[IT_NAME]);	Inv_itemText[IT_NAME]=NULL;
								Init_Requester(&Level_Select_Requester);
								GetValidLevelsList(&Level_Select_Requester);
								SetRequesterHeading( &Level_Select_Requester, GF_GameStrings[GT_SELECTLEVEL], R_CENTRE, NULL,0);
	    						passport_mode=2;
								input = 0;
								inputDB = 0;
							}
							else if (inputDB&SELECT_ITEM)
							{
						  		Inventory_ExtraData[1]=LV_FIRSTLEVEL;
							}
						}
						else
						{
							// Init SAVEGAME Requester
							T_RemovePrint(Inv_levelText);	Inv_levelText=NULL;
							T_RemovePrint(Inv_tagText);	Inv_tagText=NULL;
							T_RemovePrint(Inv_ringText);	Inv_ringText=NULL;
							T_RemovePrint(Inv_itemText[IT_NAME]);	Inv_itemText[IT_NAME]=NULL;
//							Init_Requester(&Load_Game_Requester);
							GetSavedGamesList(&Load_Game_Requester); // Savegame in this case
							SetRequesterHeading( &Load_Game_Requester, GF_GameStrings[GT_SAVEGAME], R_CENTRE, NULL,0);
	    					passport_mode=1;
							input = 0;
							inputDB = 0;
						}
//					}
				}
			}
			break;
		case 2:		// EXIT
            // Text for last pages
        	if (!passport_text1)
        	{
        		if (Inventory_Mode==INV_TITLE_MODE)
        			passport_text1 = T_Print(0,-16,IT_BRONZE, GF_GameStrings[GT_EXITGAME]);
        		else if (!gameflow.demoversion)
        			passport_text1 = T_Print(0,-16,IT_BRONZE, GF_GameStrings[GT_EXIT2TITLE]);
				else
        			passport_text1 = T_Print(0,-16,IT_BRONZE, GF_GameStrings[GT_EXITDEMO]);
        		T_BottomAlign(passport_text1, 1);
        		T_CentreH(passport_text1, 1);
        	}
			break;
	}

	if (inputDB&PASSPORT_2FRONT)
	{
		if (Inventory_Mode != INV_DEATH_MODE || SavedGames)
		{
			inv_item->anim_direction = -1;
  			inv_item->goal_frame -= 5;

			if ( !SavedGames )
			{
				if (inv_item->goal_frame < (inv_item->open_frame+5))
					inv_item->goal_frame = (inv_item->open_frame+5);
				else 
				{
      				T_RemovePrint(passport_text1);
					passport_text1=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);
   	  				T_RemovePrint(passport_text1);
					passport_text1=NULL;
   				}
			}
			input = 0;
			inputDB = 0;
		}
	}

	if (inputDB&PASSPORT_2BACK)
	{
  		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);
			T_RemovePrint(passport_text1);
			passport_text1=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;
			}
			T_RemovePrint(passport_text1);
			passport_text1=NULL;
		}
	}

	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;
		}
		T_RemovePrint(passport_text1);
		passport_text1=NULL;
	}
}

/********************************************************************/
/* GAMMA OPTION */
/********************************************************************/
void	do_gamma_option(INVENTORY_ITEM *inv_item)
{
/*
	sint16		gamma;

	if (input&DEC_HBAR)
	{
		idelay = 1;
		idcount = 10;
		Option_Gamma_Level-=GAMMA_MODIFIER;
		if (Option_Gamma_Level<MIN_GAMMA_LEVEL)
			Option_Gamma_Level=MIN_GAMMA_LEVEL;
	}
	if (input&INC_HBAR)
	{
		idelay = 1;
		idcount = 10;
		Option_Gamma_Level+=GAMMA_MODIFIER;
		if (Option_Gamma_Level>MAX_GAMMA_LEVEL)
			Option_Gamma_Level=MAX_GAMMA_LEVEL;
	}
	inv_item->sprlist = is_gamma_list;
	is_gamma_level[6].param1 = Option_Gamma_Level/2+63;
	S_SetGamma(Option_Gamma_Level);

	if ((inputDB&SELECT_ITEM))
	{
		inv_item->goal_frame = 0;
		inv_item->anim_direction = -1;
	}
	if ((inputDB&DESELECT_ITEM))
	{
		gamma = Option_Gamma_Level-64;
		if (gamma < -255) gamma = -255;
		if (Inventory_Mode!=INV_TITLE_MODE)
			S_SetBackgroundGamma(gamma);
	}
*/
}

/********************************************************************/
/* DETAIL OPTION */
/********************************************************************/
TEXTSTRING *dtext[5] = {NULL};

#if LANGUAGE == 2
	#define DWIDTH 210
#else
	#define DWIDTH 160
#endif

void do_detail_option(INVENTORY_ITEM *inv_item)
{
	int i;

	if (dtext[0] == NULL)
	{
		dtext[2] = T_Print(0,0,0, GF_GameStrings[GT_HIGH_DETAIL]);
		dtext[1] = T_Print(0,25,0, GF_GameStrings[GT_MEDIUM_DETAIL]);
		dtext[0] = T_Print(0,50,0, GF_GameStrings[GT_LOW_DETAIL]);
		dtext[3] = T_Print(0,-32,0, " ");
		dtext[4] = T_Print(0,-30,0, GF_GameStrings[GT_DETAIL]);

		T_AddBackground(dtext[4], DWIDTH-4,0, 0,0,8, C_BLACK, NULL, 0);
		T_AddOutline(dtext[4], 1, C_ORANGE, NULL, 0);

		T_AddBackground(dtext[detail_level], DWIDTH-12,0, 0,0,8, C_BLACK, NULL, 0);
		T_AddOutline(dtext[detail_level], 1, C_ORANGE, NULL, 0);

		T_AddBackground(dtext[3], DWIDTH, 107, 0,0,16, C_BLACK, NULL, 0);
		T_AddOutline(dtext[3], 1, C_BLUE, NULL, 0);

		for (i=0; i<5; i++)
		{
			T_CentreH(dtext[i], 1);
			T_CentreV(dtext[i], 1);
		}
	}

	if ((inputDB & IN_BACK) && detail_level > 0)
	{
		T_RemoveOutline(dtext[detail_level]);
		T_RemoveBackground(dtext[detail_level]);

		detail_level--;

		T_AddOutline(dtext[detail_level], 1, C_ORANGE, NULL, 0);
		T_AddBackground(dtext[detail_level], DWIDTH-12,0, 0,0,8, C_BLACK, NULL, 0);
	}
	if ((inputDB & IN_FORWARD) && detail_level < 2)
	{
		T_RemoveOutline(dtext[detail_level]);
		T_RemoveBackground(dtext[detail_level]);

		detail_level++;

		T_AddOutline(dtext[detail_level], 1, C_ORANGE, NULL, 0);
		T_AddBackground(dtext[detail_level], DWIDTH-12,0, 0,0,8, C_BLACK, NULL, 0);
	}

   switch ( detail_level )                 // Note if you change this! then change similar in INPUT.C
   {
   	case 0: perspective_distance = 0*1024*W2V_SCALE; break;
      case 1: perspective_distance = 3*1024*W2V_SCALE; break;
      //case 2: perspective_distance = 6*1024*W2V_SCALE; break;
		case 2: perspective_distance = 20*1024*W2V_SCALE;
   }

	if (inputDB & (SELECT_ITEM|DESELECT_ITEM))
	{
		for (i=0; i<5; i++)
		{
			T_RemovePrint(dtext[i]);
			dtext[i]=NULL;
		}
	}
}

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

TEXTSTRING *sa_text[4] = {NULL};

#define SAWIDTH 140

void	do_sound_option(INVENTORY_ITEM *inv_item)
{
	int i;
	char string[20];

	if (!sa_text[0])
	{
		if (Option_Music_Volume > 10)
			Option_Music_Volume = 10;
		wsprintf(string, "| %2d", Option_Music_Volume);
		sa_text[0] = T_Print(0,0,0, string);
		T_AddBackground(sa_text[0], SAWIDTH-12,0, 0,0,8, C_BLACK, NULL, 0);
		T_AddOutline(sa_text[0], 1, C_ORANGE, NULL, 0);

		if (Option_SoundFX_Volume > 10)
			Option_SoundFX_Volume = 10;
		wsprintf(string, "} %2d", Option_SoundFX_Volume);
		sa_text[1] = T_Print(0,25,0, string);

		// the box
		sa_text[2] = T_Print(0,-32,0, " ");
		T_AddBackground(sa_text[2], SAWIDTH,85, 0,0,48, C_BLACK, NULL, 0);
		T_AddOutline(sa_text[2], 1, C_BLUE, NULL, 0);

		sa_text[3] = T_Print(0,-30,0, GF_PCStrings[PCSTR_SETVOLUME]);
		T_AddBackground(sa_text[3], SAWIDTH-4,0, 0,0,8, C_BLACK, NULL, 0);
		T_AddOutline(sa_text[3], 1, C_BLUE, NULL, 0);

		for (i=0; i<4; i++)
		{
			T_CentreH(sa_text[i],1);
			T_CentreV(sa_text[i],1);
		}
	}

	if ((inputDB & PREV_HBAR) && item_data > 0)
	{
		T_RemoveOutline(sa_text[item_data]);
		T_RemoveBackground(sa_text[item_data]);

		item_data--;

		T_AddBackground(sa_text[item_data], SAWIDTH-12,0, 0,0,8, C_BLACK, NULL, 0);
		T_AddOutline(sa_text[item_data], 1, C_ORANGE, NULL, 0);
	}
	if ((inputDB & NEXT_HBAR) && item_data < 1)
	{
		T_RemoveOutline(sa_text[item_data]);
		T_RemoveBackground(sa_text[item_data]);

		item_data++;

		T_AddBackground(sa_text[item_data], SAWIDTH-12,0, 0,0,8, C_BLACK, NULL, 0);
		T_AddOutline(sa_text[item_data], 1, C_ORANGE, NULL, 0);
	}

	if (!item_data)
	{
		if ((input & DEC_HBAR) && Option_Music_Volume > 0)
		{
			idelay = 1;
			idcount = 10;
			Option_Music_Volume--;
			wsprintf(string, "| %2d", Option_Music_Volume);
			T_ChangeText(sa_text[0], string);
		
			if (Option_Music_Volume)
				S_CDVolume(Option_Music_Volume * 25 + 5);
			else
				S_CDVolume(0);
			SoundEffect(115, NULL, SFX_ALWAYS);
		}
		else if ((input & INC_HBAR) && Option_Music_Volume < 10)
		{
			idelay = 1;
			idcount = 10;
			Option_Music_Volume++;
			wsprintf(string, "| %2d", Option_Music_Volume);
			T_ChangeText(sa_text[0], string);

			if (Option_Music_Volume)
				S_CDVolume(Option_Music_Volume * 25 + 5);
			else
				S_CDVolume(0);
			SoundEffect(115, NULL, SFX_ALWAYS);
		}
	}
	else
	{
		if ((input & DEC_HBAR) && Option_SoundFX_Volume > 0)
		{
			idelay = 1;
			idcount = 10;
			Option_SoundFX_Volume--;
			wsprintf(string, "} %2d", Option_SoundFX_Volume);
			T_ChangeText(sa_text[1], string);
		
			if (Option_SoundFX_Volume)
				S_SoundSetMasterVolume(Option_SoundFX_Volume*6 + 4);
			else
				S_SoundSetMasterVolume(0);
			SoundEffect(115, NULL, SFX_ALWAYS);
		}
		else if ((input & INC_HBAR) && Option_SoundFX_Volume < 10)
		{
			idelay = 1;
			idcount = 10;
			Option_SoundFX_Volume++;
			wsprintf(string, "} %2d", Option_SoundFX_Volume);
			T_ChangeText(sa_text[1], string);

			if (Option_SoundFX_Volume)
				S_SoundSetMasterVolume(Option_SoundFX_Volume*6 + 4);
			else
				S_SoundSetMasterVolume(0);
			SoundEffect(115, NULL, SFX_ALWAYS);
		}
	}

	is_sound_sfxlevel[6].param1 = Option_SoundFX_Volume;
	is_sound_sfxlevellow[6].param1 = Option_SoundFX_Volume;

	is_sound_muslevel[6].param1 = Option_Music_Volume;
	is_sound_muslevellow[6].param1 = Option_Music_Volume;

	if (inputDB & (DESELECT_ITEM|SELECT_ITEM))
	{
		T_RemovePrint(sa_text[0]); sa_text[0]=NULL;
		T_RemovePrint(sa_text[1]); sa_text[1]=NULL;
		T_RemovePrint(sa_text[2]); sa_text[2]=NULL;
		T_RemovePrint(sa_text[3]); sa_text[3]=NULL;
	}
}


void	do_compass_option(INVENTORY_ITEM *inv_item)
{
	int	h=0,m=0,s=0;
	char time_str[32];

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

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

#if 0
#if CODER==JASON
#ifdef GAMEDEBUG
	if (input&IN_RIGHT)
		savegame.timer+=30*10;
	if (input&IN_LEFT)
		savegame.timer-=30*10;
	if (input&IN_FORWARD)
		savegame.timer+=30*10*60;
	if (input&IN_BACK)
		savegame.timer-=30*10*60;
#endif
#endif
#endif

	if (CurrentLevel==LV_GYM)
		ShowGymStatsText(time_str,1);
	else
		ShowStatsText(time_str,1);

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

//	if ( inv_item->misc_data[0] != inv_item->misc_data[1] )
	SoundEffect(113, NULL, SFX_ALWAYS);
}


TEXTSTRING *control_text[2]={NULL,NULL};

void FlashConflicts(void)
{
	/* Check keymap for multiple keys doing the same things */
	int i, j;
	sint16 key;

	for (i=0; i<NUM_CONTROLS; i++)
	{
		key = layout[iconfig][i];

		/* Turn off flashing to start with */
		T_FlashText(btext[i], 0, 0);

		for (j=0; j<NUM_CONTROLS; j++)
		{
			if (i==j)
				continue;

			if (key == layout[iconfig][j])
			{
				T_FlashText(btext[i], 1, 20);
				break;
			}
		}
	}
}


void DefaultConflict(void)
{
	/* Check user keymap for keys that are defined in default. Resolve conflicts in favour of user settings */
	int i, j;
	sint16 key;

	for (i=0; i<14; i++)
	{
		key = layout[0][i];

		conflict[i] = 0;

		for (j=0; j<14; j++)
		{
			if (key == layout[1][j])
			{
				conflict[i] = 1;
				break;
			}
		}
	}
}


void	do_control_option(INVENTORY_ITEM *inv_item)
{
	sint16 key;

	if (!control_text[0])
	{
		if (iconfig)
			control_text[0] = T_Print(0,-120,0, GF_PCStrings[PCSTR_USERKEYS]);
		else
			control_text[0] = T_Print(0,-120,0, GF_PCStrings[PCSTR_DEFAULTKEYS]);
		T_CentreH(control_text[0], 1);
		T_CentreV(control_text[0], 1);
		S_ShowControls();

		keychange = -1;
		T_AddBackground(control_text[0], 0,0, 0,0,48, C_BLACK,0,0);
		T_AddOutline(control_text[0], 1, C_BLUE, NULL, 0);
	}

	switch (select_key)
	{
	case 0:
		if (inputDB & (IN_RIGHT|IN_LEFT))
		{
			if (keychange == -1)
			{
				iconfig = !iconfig;
				S_ChangeCtrlText();
				FlashConflicts();
			}
			else
			{
				/*
				ctext[keychange]->zpos = 16;
				T_RemoveBackground(ctext[keychange]);
				T_RemoveOutline(ctext[keychange]);
				
				if (keychange <= 6)
					keychange += 7;
				else
				{
					if (keychange == 14)
						keychange = 7;
					else
						keychange -= 7;
				}

				ctext[keychange]->zpos = 0;
				T_AddBackground(ctext[keychange], 0,0, 0,0,0, C_BLACK,0,0);
				T_AddOutline(ctext[keychange], 1, C_BLUE, NULL, 0);
				*/
			}
		}
		else if ((inputDB & IN_DESELECT) || ((inputDB & IN_SELECT) && keychange == -1))
		{
			T_RemovePrint(control_text[0]); control_text[0]=NULL;
			T_RemovePrint(control_text[1]);	control_text[1]=NULL;
			S_RemoveCtrlText();
			DefaultConflict();
			return;
		}

		if (iconfig)
		{
			if (inputDB & IN_SELECT)
			{
				select_key = 1;
				ctext[keychange]->zpos = 16;
				T_RemoveBackground(ctext[keychange]);
				T_RemoveOutline(ctext[keychange]);

				btext[keychange]->zpos = 0;
				T_AddBackground(btext[keychange], 0,0, 0,0,0, C_BLACK,0,0);
				T_AddOutline(btext[keychange], 1, C_BLUE, NULL, 0);
			}
			else if (inputDB & IN_FORWARD)
			{
				if (keychange == -1)
				{
					T_RemoveBackground(control_text[0]);
					T_RemoveOutline(control_text[0]);
				}
				else
				{
					ctext[keychange]->zpos = 16;
					T_RemoveBackground(ctext[keychange]);
					T_RemoveOutline(ctext[keychange]);
				}
	
				keychange--;
				if (keychange < -1)
					keychange = 14;

				if (keychange == -1)
				{
					T_AddBackground(control_text[0], 0,0, 0,0,0, C_BLACK,0,0);
					T_AddOutline(control_text[0], 1, C_BLUE, NULL, 0);
				}
				else
				{
					ctext[keychange]->zpos = 0;
					T_AddBackground(ctext[keychange], 0,0, 0,0,0, C_BLACK,0,0);
					T_AddOutline(ctext[keychange], 1, C_BLUE, NULL, 0);
				}
			}
			else if (inputDB & IN_BACK)
			{
				if (keychange == -1)
				{
					T_RemoveBackground(control_text[0]);
					T_RemoveOutline(control_text[0]);
				}
				else
				{
					ctext[keychange]->zpos = 16;
					T_RemoveBackground(ctext[keychange]);
					T_RemoveOutline(ctext[keychange]);
				}

				keychange++;
				if (keychange > 14)
					keychange = -1;

				if (keychange == -1)
				{
					T_AddBackground(control_text[0], 0,0, 0,0,0, C_BLACK,0,0);
					T_AddOutline(control_text[0], 1, C_BLUE, NULL, 0);
				}
				else
				{
					ctext[keychange]->zpos = 0;
					T_AddBackground(ctext[keychange], 0,0, 0,0,0, C_BLACK,0,0);
					T_AddOutline(ctext[keychange], 1, C_BLUE, NULL, 0);
				}
			}
		}
		break;

	case 1:
		if (!(inputDB & IN_SELECT))
			select_key = 2;
		break;

	case 2:
		if (joy_fire)
		{
			for (key=0; key<32; key++)
				if ((1<<key) & joy_fire)
					break;

			if (key == 32)
				key = 0;
			else 
				key += JOY_KEY;
		}
		else
		{
			// WIN95 replacement of KeyGet() (look for any key press in keymap)
			for (key=0; key<256; key++)
				if (rawkey(key))
					break;

			if (key == 256)
				key = 0;
		}

		if (key && kname[key])
		{
			/* Cannot allow ESC/RET or the CURSOR keys to be redefined else user can make menu rings
				impossible to navigate */
			if (key == DIK_RETURN || key == DIK_LEFT || key == DIK_RIGHT || key == DIK_UP || key == DIK_DOWN)
				break;

			if (key != DIK_ESCAPE)
			{
				layout[iconfig][keychange] = key;
				T_ChangeText(btext[keychange], kname[key]);
			}
			
			btext[keychange]->zpos = 16;
			T_RemoveBackground(btext[keychange]);
			T_RemoveOutline(btext[keychange]);
			
			ctext[keychange]->zpos = 0;
			T_AddBackground(ctext[keychange], 0,0, 0,0,0, C_BLACK,0,0);
			T_AddOutline(ctext[keychange], 1, C_BLUE, NULL, 0);

			select_key = 3;

			FlashConflicts();
		}
		break;

	case 3:
		/* Wait for user to release new key selection */
		if (layout[iconfig][keychange] & JOY_KEY)
		{
			if (  !( (1<<(layout[iconfig][keychange] & 0xff)) & joy_fire )  )
				select_key = 0;
		}
		else if (!rawkey(layout[iconfig][keychange]))
		{
			select_key = 0;

			// Adjust left ALT, SHIFT and CTRL to be the right ones so conflicts work
			if (layout[iconfig][keychange] == DIK_LCONTROL)
				layout[iconfig][keychange] = DIK_RCONTROL;
			if (layout[iconfig][keychange] == DIK_LSHIFT)
				layout[iconfig][keychange] = DIK_RSHIFT;
			if (layout[iconfig][keychange] == DIK_LALT)
				layout[iconfig][keychange] = DIK_RALT;

			// Needed to see if above changes have made a difference
			FlashConflicts();
		}
		break;
	}

  	input = 0;
  	inputDB = 0;
}

/*uint32 GetTextScaleH(uint32 scaleH)
{
	uint32	scale;
	int		width;

	width = GetRenderWidth();
	if (width<640)
		width=640;

	scale = (width<<16)/640;
	scaleH = (scaleH>>8) * (scale>>8);

	return(scaleH);
}*/


#define STARTP	-100
#define TSPACE  19


void S_ShowControls(void)
{
	int i, centre;
	sint16 vpos;
	sint16 *key;
	int p,s;

	p = STARTP;
	s = TSPACE;
	
	centre = GetRenderWidth()/2;
	if (centre>320)
		centre = 640/2;


	if (!btext[0])
	{
		key = &layout[iconfig][0];

		if (centre<320)
			vpos = centre - 140;
		else
			vpos = centre - 200;

	
		btext[0] = T_Print(vpos,p,0, kname[*key]);
		p+=s;
		btext[1] = T_Print(vpos,p,0, kname[*(key+1)]);
		p+=s;
		btext[2] = T_Print(vpos,p,0, kname[*(key+2)]);
		p+=s;
		btext[3] = T_Print(vpos,p,0, kname[*(key+3)]);
		p+=s;
		btext[4] = T_Print(vpos,p,0, kname[*(key+4)]);
		p+=s;
		btext[5] = T_Print(vpos,p,0, kname[*(key+5)]);
		p+=s;
		btext[6] = T_Print(vpos,p,0, kname[*(key+6)]);
		p+=s;
		btext[7] = T_Print(vpos,p,0, kname[*(key+7)]);
		p+=s;
		btext[8] = T_Print(vpos,p,0, kname[*(key+8)]);
		p+=s;
		btext[9] = T_Print(vpos,p,0, kname[*(key+9)]);
		p+=s;
		btext[10] = T_Print(vpos,p,0, kname[*(key+10)]);
		p+=s;
		btext[11] = T_Print(vpos,p,0, kname[*(key+11)]);
		p+=s;
		btext[12] = T_Print(vpos,p,0, kname[*(key+12)]);
		p+=s;
		btext[13] = T_Print(vpos,p,0, kname[*(key+13)]);
		p+=s;
		btext[14] = T_Print(vpos,p,0, kname[*(key+14)]);

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

		keychange = 0;
	}

	if (!ctext[0])
	{
		if (centre<320)
			vpos = centre - 70;
		else
			vpos = centre - 120;


		p = STARTP;
		s = TSPACE;
	
		ctext[0] = T_Print(vpos,p,0, GF_GameStrings[GT_RUN]);
		p+=s;
		ctext[1] = T_Print(vpos,p,0, GF_GameStrings[GT_BACK]);
		p+=s;
		ctext[2] = T_Print(vpos,p,0, GF_GameStrings[GT_LEFT]);
		p+=s;
		ctext[3] = T_Print(vpos,p,0, GF_GameStrings[GT_RIGHT]);
		p+=s;
		ctext[4] = T_Print(vpos,p,0, GF_GameStrings[GT_STEPLEFT1]);
		p+=s;
		ctext[5] = T_Print(vpos,p,0, GF_GameStrings[GT_STEPRIGHT1]);
		p+=s;
		ctext[6] = T_Print(vpos,p,0, GF_GameStrings[GT_WALK]);
		p+=s;
		ctext[7] = T_Print(vpos,p,0, GF_GameStrings[GT_JUMP]);
		p+=s;
		ctext[8] = T_Print(vpos,p,0, GF_GameStrings[GT_ACTION]);
		p+=s;
		ctext[9] = T_Print(vpos,p,0, GF_GameStrings[GT_DRAWWEAPON1]);
		p+=s;
		ctext[10] = T_Print(vpos,p,0, GF_GameStrings[GT_USEFLARE]);
		p+=s;
		ctext[11] = T_Print(vpos,p,0, GF_GameStrings[GT_LOOK]);
		p+=s;
		ctext[12] = T_Print(vpos,p,0, GF_GameStrings[GT_ROLL]);
		p+=s;
		ctext[13] = T_Print(vpos,p,0, GF_GameStrings[GT_INVENTORY]);
		p+=s;
		ctext[14] = T_Print(vpos,p,0, GF_GameStrings[GT_DRAWWEAPON2]);

		for (i=0; i<NUM_CONTROLS; i++)
			T_CentreV(ctext[i],1);

	}

//	if (G_RenderInfo.nHeight>=400)
//		control_text[1] = T_Print(0,-60,0, " ");
//	else
		control_text[1] = T_Print(0,-125,0, " ");
	T_CentreV(control_text[1],1);
	T_CentreH(control_text[1],1);
	T_AddOutline(control_text[1], 1, C_BLUE, NULL, 0);

	if (centre<320)
	{
		for (i=0; i<NUM_CONTROLS; i++)
		{
			T_SetScale(btext[i], 0x8000, 0x10000);
			T_SetScale(ctext[i], 0x8000, 0x10000);
		}
		T_AddBackground(control_text[1], 300,140, 0,0,48, C_BLACK, NULL,0);
	}
	else 
		T_AddBackground(control_text[1], 420,320, 0,0,48, C_BLACK, NULL,0);
}


void S_ChangeCtrlText(void)
{
	int i;
	char str[40];

	if (iconfig)
	{
		wsprintf(str, GF_PCStrings[PCSTR_USERKEYS], iconfig);
		T_ChangeText(control_text[0], str);
	}
	else
		T_ChangeText(control_text[0], GF_PCStrings[PCSTR_DEFAULTKEYS]);

	for (i=0; i<NUM_CONTROLS; i++)
	{
		if (kname[layout[iconfig][i]] != NULL)
			T_ChangeText(btext[i], kname[layout[iconfig][i]]);
		else
			T_ChangeText(btext[i], "BAD");
	}
}


void	S_RemoveCtrlText(void)
{
	int i;

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

int		GetRenderHeight( void )
{
//	return(G_RenderInfo.nHeight);
	return phd_winheight;
}

int		GetRenderWidth( void )
{
//	return(G_RenderInfo.nWidth);
	return phd_winwidth;
}


