/*
	ネームエントリー
*/
#include "ot_header.h"
#include "ot_newton.h"
#include "hjm_camera.h"
#include "ot_sea.h"
#include "ot_sub.h"

#include "endefine.h"
#include "enchkcrs.h"
#include "enrecord.h"

#include "sot_world.h"

#include "kn_def.h"
#include "kn_main.h"
#include "kn_backup.h"
#include "kn_dynamic.h"
#include "kn_font.h"
#include "kn_mask.h"
#include "kn_gword.h"
#include "kn_letter.h"
#include "kn_name.h"
#include "kn_title.h"
#include "kn_ctitle.h"
#include "kn_record.h"
#include "kn_screc.h"
#include "kn_message.h"
#include "kn_cfgrec.h"
#include "kn_machine.h"

/*******************************************************
	FUNCTION
*******************************************************/
Gfx 		*makeMainGlistName( Gfx *gp );
static Gfx 	*makeKnLetterGlist( Gfx *gp );
static Gfx 	*makeNameGlist( Gfx *gp );
static void	initMoveMainName( void );
static void	changeGameModeName( void );
static void	copyPlayerName( void );
static void	initMainGlistName2Next( int next_game_mode );
static float calcVelMag( void );
/*******************************************************
	VARIABLE
*******************************************************/
/* --- 外部参照 --- */
/* Display List */
extern Gfx 		dlInitLetterModel[];
extern Gfx		dlNameDispLine1[], dlNameDispLine2[], dlNameDispLine3[];
/* コントローラー */
extern sKnPad	mKnPad[];
/* ネームエントリー １回目フラグ */
extern short 	nameEntryFirstFlag;
/* ネームエントリーで前回入力したのを保存 */
extern char 	strNameEntryBak[PLAYER_NAME_MAX_SIZE+1];

/* --- 内部 --- */
/* ディスプレイリスト ポインタ */
static Gfx		*pLetterGL[MAX_NUM_LETTER_SHAPE] = {
	a_GL, b_GL, c_GL, d_GL,	e_GL, f_GL, g_GL, h_GL, i_GL, j_GL,
	k_GL, l_GL, m_GL, n_GL,	o_GL, p_GL, q_GL, r_GL, s_GL, t_GL,
	u_GL, v_GL, w_GL, x_GL,	y_GL, z_GL, hyphen_GL, period_GL, sp_GL, bs_GL, end_GL
};
static char		*strInputLetter[MAX_NUM_LETTER_SHAPE-2] = {
	"A", "B", "C", "D",	"E", "F", "G", "H", "I", "J",
	"K", "L", "M", "N",	"O", "P", "Q", "R", "S", "T",
	"U", "V", "W", "X",	"Y", "Z", "-", ".", " "
};
static char		tblInputLetter[MAX_NUM_LETTER_SHAPE-2] = {
	'A', 'B', 'C', 'D',	'E', 'F', 'G', 'H', 'I', 'J',
	'K', 'L', 'M', 'N',	'O', 'P', 'Q', 'R', 'S', 'T',
	'U', 'V', 'W', 'X',	'Y', 'Z', '-', '.', ' '
};
static int		dxInputLetter[MAX_NUM_LETTER_SHAPE-2] = {
	2, 5, 5, 4,	6, 5, 4, 3, 7, 6,
	4, 6, 0, 3,	3, 5, 3, 4, 5, 4,
	3, 3, 0, 3,	3, 4, 5, 8, 0
};

/* 文字盤 */
static int		numSelLetter = 0;		/* 選択文字番号 */
static int		numDispLetterMin, numDispLetterMax;		/* しぇいぷ表示範囲 */
static int		nameLetterStat = NAME_LETTER_STAT_APPR;
static float	nameLetterR = 0.0f;
static float	nameLetterRad = 0.0f;
static float	nameLetterNextRad = 0.0f;
static int		nameLetterBuf[MAX_NUM_NAME_DISP_LETTER];
static float	nameLetterOldPx[MAX_NUM_NAME_DISP_LETTER];
static float	nameLetterOldPy[MAX_NUM_NAME_DISP_LETTER];
static float	nameLetterOldPz[MAX_NUM_NAME_DISP_LETTER];
static float	nameLetterRotVelMag = 1.0f;
static short	flagNameLetterMag = 0;

/* カウンタ */
static int		countNameEntry = 0;
/* 名前バッファ */
static int		idxNameEntryBuffer = 0;
static char		strNameEntry[PLAYER_NAME_MAX_SIZE+1];

/* タイトル表示フラグ */
static int		flagDispNameEntryTitle = 0;

/* 終了処理 */
static ushort	flagNameEnd;

/* 下線 */
static Gfx	*pDlNameLine[] = {
	dlNameDispLine1,
	dlNameDispLine2,
	dlNameDispLine3,
};

static flagMetalLetter = 0;

/*******************************************************
	DEFINE
*******************************************************/

#if (SW_KN_LANGUAGE == KN_JP || SW_KN_LANGUAGE == KN_ZH)

#define	DX_NAME18	68

#elif (SW_KN_LANGUAGE == KN_US)

#define	DX_NAME18	64

#elif (SW_KN_LANGUAGE == KN_EU)

#define	DX_NAME18	64

#define	DX_NAME18_GE	120

#endif
/* ====================================================================== */
/*
	ムーブ
*/
void moveMainName( void )
{
	register int	i, f, letter_kind;
	register int	wave_pat, wave_id;
	register float	rad, input_letter_r, input_letter_rad, vel;
	Mtx				mtx_dummy, mtx_scale;
	Mtxf			mtxf0, mtxf1, mtxf2, mtxf_scale, mtxf_ans;

	static float amp = 50.0f;
	static float ampc = -50.0f;

	wave_pat = 0;

	/* 初期化 */
	if( gameModeChangeFlag ) {
		if( game_mode == GM_NAME ) initMoveMainName();
	}

	/* 計算 */
	switch( nameLetterStat ) {
		case NAME_LETTER_STAT_APPR:
			nameLetterR = NAME_LETTER_INIT_R * (float)countNameEntry / (float)NAME_LETTER_APPR_COUNT;
			countNameEntry ++;
			if( countNameEntry > NAME_LETTER_APPR_COUNT ) {
				countNameEntry = 0;
				nameLetterStat = NAME_LETTER_STAT_STOP;
				nameLetterR = NAME_LETTER_INIT_R;
				flagDispNameEntryTitle = 1;
			}
			break;
		case NAME_LETTER_STAT_STOP:
			wave_pat = 1;
			break;
		case NAME_LETTER_STAT_LEFT_END:
		case NAME_LETTER_STAT_LEFT:
			wave_pat = 1;
			if( nameLetterStat == NAME_LETTER_STAT_LEFT_END ) vel = NAME_LETTER_ROT_V_END;
			else {
				if( flagNameLetterMag ) nameLetterRotVelMag = calcVelMag();
				vel = NAME_LETTER_ROT_V * nameLetterRotVelMag;
			}
			nameLetterRad -= vel;
			if( nameLetterRad < 0.0f ) {
				if( nameLetterNextRad != 0.0f ) nameLetterRad += ( 2.0f * PAI );
			}
			if( nameLetterRad <= nameLetterNextRad ) {
				nameLetterRad = nameLetterNextRad;
				if( nameLetterStat == NAME_LETTER_STAT_LEFT_END && (flagNameEnd & 1) ) {
					nameLetterStat = NAME_LETTER_STAT_STOP_END;
				}
				else nameLetterStat = NAME_LETTER_STAT_STOP;
			}
			break;
		case NAME_LETTER_STAT_RIGHT_END:
		case NAME_LETTER_STAT_RIGHT:
			wave_pat = 1;
			if( nameLetterStat == NAME_LETTER_STAT_RIGHT_END ) vel = NAME_LETTER_ROT_V_END;
			else {
				if( flagNameLetterMag ) nameLetterRotVelMag = calcVelMag();
				vel = NAME_LETTER_ROT_V * nameLetterRotVelMag;
			}
			nameLetterRad += vel;
			if( nameLetterRad >= nameLetterNextRad ) {
				nameLetterRad = nameLetterNextRad;
				if( nameLetterStat == NAME_LETTER_STAT_RIGHT_END && (flagNameEnd & 2) ) {
					nameLetterStat = NAME_LETTER_STAT_STOP_END;
				}
				else nameLetterStat = NAME_LETTER_STAT_STOP;
			}
			if( nameLetterRad >= ( 2.0f * PAI ) ) nameLetterRad -= ( 2.0f * PAI );
			break;
		case NAME_LETTER_STAT_ENTRY:
			wave_pat = 1;
			countNameEntry ++;
			input_letter_rad = 2.0 * PAI * (float)countNameEntry / (float)NAME_LETTER_FALL_COUNT;
			if( countNameEntry >= NAME_LETTER_FALL_COUNT ) {
				input_letter_r = NAME_LETTER_INIT_R;
				letter_kind = nameLetterBuf[numSelLetter];
				if( letter_kind == NAME_LETTER_NUMBER_END ) {
					/* END */
					if( mSelGameInfo.gameKind == GAME_KIND_TOUR ) nameLetterStat = NAME_LETTER_STAT_EXIT;
					else {
						nameLetterStat = NAME_LETTER_STAT_DISAP;
						flagDispNameEntryTitle = 0;
						countNameEntry = 0;
					}
				}
				else if( letter_kind == NAME_LETTER_NUMBER_BS ) {
					nameLetterStat = NAME_LETTER_STAT_STOP;
					if( idxNameEntryBuffer > 0 ) idxNameEntryBuffer --;
					strNameEntry[idxNameEntryBuffer] = 0x00;
				}
				else {
					strNameEntry[idxNameEntryBuffer] = *(strInputLetter[letter_kind]);
					idxNameEntryBuffer ++;
					if( idxNameEntryBuffer == PLAYER_NAME_MAX_SIZE ) {
						nameLetterStat = NAME_LETTER_STAT_STOP_END;
						if( nameLetterBuf[numSelLetter] > 15 ) flagNameEnd = 2;
						else flagNameEnd = 1;
					}
					else nameLetterStat = NAME_LETTER_STAT_STOP;
				}
			}
			else if( countNameEntry >= NAME_LETTER_FALL_COUNT2 ) {
				input_letter_r = NAME_LETTER_INIT_R - NAME_LETTER_FALL_DR + NAME_LETTER_FALL_DR
					* (float)( countNameEntry - NAME_LETTER_FALL_COUNT2 ) / (float)NAME_LETTER_FALL_COUNT2;
			}
			else {
				input_letter_r = NAME_LETTER_INIT_R - NAME_LETTER_FALL_DR
					* (float)countNameEntry / (float)NAME_LETTER_FALL_COUNT2;
			}
			break;
		case NAME_LETTER_STAT_DISAP_WAIT:
			countNameEntry ++;
			if( countNameEntry > 10 ) {
				nameLetterStat = NAME_LETTER_STAT_DISAP;
				flagDispNameEntryTitle = 0;
				countNameEntry = 0;
			}
			break;
		case NAME_LETTER_STAT_DISAP:
			nameLetterR = NAME_LETTER_INIT_R * (float)(NAME_LETTER_APPR_COUNT - countNameEntry)
				/ (float)NAME_LETTER_APPR_COUNT;
			countNameEntry ++;
			if( countNameEntry > NAME_LETTER_APPR_COUNT ) {
				countNameEntry = 0;
				nameLetterStat = NAME_LETTER_STAT_EXIT;
				nameLetterR = 0;
			}
			break;
	}
	
	numDispLetterMin = numSelLetter - 3;
	if( numDispLetterMin < 0 ) numDispLetterMin += MAX_NUM_NAME_DISP_LETTER;
	numDispLetterMax = numSelLetter + 3;
	if( numDispLetterMax >= MAX_NUM_NAME_DISP_LETTER ) numDispLetterMax -= MAX_NUM_NAME_DISP_LETTER;

	for( i = 0 ; i < MAX_NUM_NAME_DISP_LETTER ; i ++ ) {
		make_ZXYTmatrix( &mtx_dummy, &mtxf0,
						 0.0f, -0.2f, nameLetterRad
						 - 2.0f * PAI * (float)i / (float)MAX_NUM_NAME_DISP_LETTER,
						 0.0f, NAME_LETTER_INIT_CY, 0.0f );
		if( nameLetterStat == NAME_LETTER_STAT_ENTRY && i == numSelLetter ) {
			make_ZXYTmatrix( &mtx_dummy, &mtxf1,
							 0.0f + input_letter_rad, 0.5f * PAI + 0.2f, -0.5f * PAI,
							 0.0f, input_letter_r, 0.0f );
		}
		else {
			make_ZXYTmatrix( &mtx_dummy, &mtxf1,
							 0.0f, 0.5f * PAI + 0.2f, -0.5f * PAI,
							 0.0f, nameLetterR, 0.0f );
		}
		mult_mtxf( &mtxf0, &mtxf1, &mtxf2 );
		make_ROTSmatrix( &mtx_scale,
				0.0f, 0.0f, 1.0f,
				0.0f, 1.0f, 0.0f,
				0.0f, 0.0f, 0.0f, 0.1f ) ;
		mtx2mtxf( &mtx_scale, &mtxf_scale );
		mult_mtxf( &mtxf2, &mtxf_scale, &mtxf_ans );
		mtxf2mtx( &mtxf_ans, &(pKnDynamic->mtxLetterModel[i]) );
		f = 0;
		if( !gameModeChangeFlag ) {
			/* 最初の１フレーム以外 */
			if( nameLetterOldPy[i] < 0.0f && mtxf2[3][1] >= 0.0f ) f = 1;
			else if( nameLetterOldPy[i] >= 0.0f && mtxf2[3][1]  < 0.0f ) f = 2;
			if( wave_pat == 0 ) {
				if( f == 1 ) push_sea( mtxf2[3][0], mtxf2[3][2], amp );		
				else if( f == 2 ) push_sea( mtxf2[3][0], mtxf2[3][2], 0.0f-amp );
			}
			else {
				if( i == numSelLetter ) {
					if( mtxf2[3][1] < 0.0f ) push_sea( mtxf2[3][0], mtxf2[3][2], -50.0f );		
				}
				else {
					if( mtxf2[3][0] >= 0.0f ) wave_id = 1;
					else wave_id = 0;
					if( f == 1 ) push_sea_trig( nameLetterOldPx[i], nameLetterOldPz[i], amp, wave_id );		
					else if( f == 2 ) push_sea_trig( nameLetterOldPx[i], nameLetterOldPz[i],
													 0.0f-amp, wave_id );
				}
			}
		}
		nameLetterOldPx[i] = mtxf2[3][0];
		nameLetterOldPy[i] = mtxf2[3][1];
		nameLetterOldPz[i] = mtxf2[3][2];
		
	}
}

/* ネームエントリーの初期化 */
static void initMoveMainName( void )
{
	register int	i, course_num, start_race_num;
	register int	num_letter_back, num_letter_back_next;

	numSelLetter = 0;
	nameLetterStat = NAME_LETTER_STAT_APPR;
	nameLetterR = 0.0f;
	nameLetterRad = 0.0f;
	nameLetterNextRad = 0.0f;
	
	countNameEntry = 0;
	idxNameEntryBuffer = 0;
	flagDispNameEntryTitle = 0;
	flagNameEnd = 0;
	
	for( i = 0 ; i < MAX_NUM_NAME_DISP_LETTER ; i ++ ) {
		if( i < 8 ) nameLetterBuf[i] = i;
		else nameLetterBuf[i] = i + 15;
	}

	idxNameEntryBuffer = 0;
	if( nameEntryFirstFlag ) {
		/* 最初の１回目*/
		nameEntryFirstFlag = 0;
		for( i = 0 ; i < PLAYER_NAME_MAX_SIZE+1 ; i ++ ) strNameEntry[i] = 0x00;
	}
	else {
		for( i = 0 ; i < PLAYER_NAME_MAX_SIZE+1 ; i ++ ) {
			strNameEntry[i] = strNameEntryBak[i];
		}
		for( i = 0 ; i < PLAYER_NAME_MAX_SIZE ; i ++ ) {
			if( strNameEntry[i] == 0 ) break;
		}
		idxNameEntryBuffer = i;
		num_letter_back = (numSelLetter + 8) % MAX_NUM_NAME_DISP_LETTER;
		num_letter_back_next = (num_letter_back + 1) % MAX_NUM_NAME_DISP_LETTER;
		nameLetterBuf[num_letter_back] = nameLetterBuf[num_letter_back_next] - 1;
		if( nameLetterBuf[num_letter_back] < 0 ) {
			nameLetterBuf[num_letter_back] += MAX_NUM_LETTER_SHAPE;
		}
		numSelLetter --;
		if( numSelLetter < 0 ) numSelLetter = MAX_NUM_NAME_DISP_LETTER - 1;
		nameLetterRad = nameLetterNextRad = 2.0f * PAI * (float)numSelLetter
			/ (float)MAX_NUM_NAME_DISP_LETTER;
	}

	flagMetalLetter = 0;
	switch( mSelGameInfo.gameKind ) {
		case GAME_KIND_TOUR:
			if( tourMyDif == TOUR_DIF_NORMAL ) start_race_num = 1;
			else start_race_num = 0;
			for( i = start_race_num ; i < tourRaceNum + 1 ; i ++ ) {
				course_num = tourUseCourseNum[tourMyDif][i];
				if( frank_total_rec[course_num] == 1 || frank_lap_rec[course_num] ) {
					flagMetalLetter = 1;
				}
			}
			break;
		case GAME_KIND_ATTACK:
			if( frank_total == 1 || frank_lap ) flagMetalLetter = 1;
			break;
		case GAME_KIND_SCORE:
			if( frank_sca == 1 ) flagMetalLetter = 1;
			break;
	}

	
}

/* ====================================================================== */
/*
	メイン
*/
Gfx *makeMainGlistName( Gfx *gp )
{
	register int	i, course_num;
	
	/* はじめの１回 */
	if( gameModeChangeFlag ) {
		gameModeChangeFlag = 0;
//		if( game_mode == GM_NAME && gameModeOld == GM_TOUREND2NEXT ) {
//			gp = clearAllFBufferColor( gp, 255, 255, 255 );
//			return( gp );
//		}
	}

	/* ディスプレイリストの作成 */
	player_index = 0;
	gp = clearFBuffer( gp );
	gp = make_world_glist( gp );
	gp = makeKnLetterGlist( gp );
	gSPMatrix( gp++, &(automatic_BF.e_MT), G_MTX_MODELVIEW | G_MTX_LOAD | G_MTX_NOPUSH );
	gp = makeKnBlindBoardGlist( gp, ncourse );
	gp = make_sea_glist( gp );
	gp = makeNameGlist( gp );

	/* ゲームモードの変更 */
	if( game_mode == GM_NAME ) changeGameModeName();
	else {
		if( mMask.flag == MASK_FLAG_END ) {
			/* タイトルへ */
			/* セーブ */
			if( mSelGameInfo.gameKind == GAME_KIND_TOUR ) {
				for( i = 0 ; i < tourRaceNum + 1 ; i ++ ) {
					course_num = tourUseCourseNum[tourMyDif][i];
					if( frank_total_rec[course_num] || frank_lap_rec[course_num] ) {
						if( reverseRunFlag ) EepWriteDataReverse( course_num );
						else EepWriteDataTime( course_num );
					}
				}
			}
			/* ツアーモードの時 */
			initMainGlistCfgRec();
		}
	}

	return( gp );
}

/* ====================================================================== */
/*
	文字シェイプ描画
*/
static Gfx	*makeKnLetterGlist( Gfx *gp )
{
	register int	i, f, chk;

	
	gp = makeKnSeaBottomGlist( gp, ncourse );

	/* --- 文字表示 --- */
	gSPDisplayList( gp++, dlInitLetterModel );
	if( flagMetalLetter ) InitDrawNameLetter( &gp );

	if( numDispLetterMax > numDispLetterMin ) chk = 0;
	else chk = 1;
	
	for( i = 0 ; i < MAX_NUM_NAME_DISP_LETTER ; i ++ ) {
		f = 0;
		if( chk == 0 ) {
			if( i >= numDispLetterMin && i <= numDispLetterMax ) f = 1;
		}
		else {
			if( i >= numDispLetterMin || i <= numDispLetterMax ) f = 1;
		}
		if( f ) {
			gSPMatrix( gp++, &(pKnDynamic->mtxLetterModel[i]), G_MTX_MODELVIEW | G_MTX_LOAD | G_MTX_PUSH );
			gSPDisplayList( gp++, pLetterGL[nameLetterBuf[i]] );
			gSPPopMatrix( gp++, G_MTX_MODELVIEW );
		}
	}

	return( gp );
}
/* ====================================================================== */
/*
	描画
*/
static Gfx	*makeNameGlist( Gfx *gp )
{
	register int	i, j, dx, dx_waku, wd_waku;
	register uint	font_flag;
	char	str_work[2];
	
	if( !flagDispNameEntryTitle ) return( gp );
	
	/* --- 枠表示 --- */
	gSPDisplayList( gp++, dlInitWaku );
#if (SW_KN_LANGUAGE == KN_EU)
	if( euLanguage == EL_ENGLISH ) {
		dx_waku = 46;
		wd_waku = 211;
	}
	else {
		dx_waku = 102;
		wd_waku = 99;
	}
#else
	dx_waku = 46;
	wd_waku = 211;
#endif
	gp = makeWakuGlist( gp, WAKU_KIND_256x32, DISPLAY_X1 + dx_waku, DISPLAY_Y1 +  12, wd_waku, 24 );
	gp = makeWakuGlist( gp, WAKU_KIND_256x32, DISPLAY_X1 + 93, DISPLAY_Y1 + 157, 117, 28 );

	/* --- 下線表示 --- */
	for( i = 0 ; i < 3 ; i ++ ) {
		if( i == idxNameEntryBuffer ) font_flag = FONT_FLAG_FLASH_3;
		else font_flag = FONT_FLAG_NORMAL;
		gp = makeInitStrGlist( gp, font_flag, 255,255,255, 255,255,255,
						   255,255,255, 255,255,255 );
		gSPDisplayList( gp++, pDlNameLine[i] );
	}

	/* --- 文字表示 --- */
	gp = makeInitStrGlist( gp, FONT_FLAG_NORMAL, 255,255,255, 255,255,255,
						   255,255,255, 255,255,255 );
#if (SW_KN_LANGUAGE == KN_EU)
	if( euLanguage == EL_ENGLISH ) dx = DX_NAME18;
	else dx = DX_NAME18_GE;
#else
	dx = DX_NAME18;
#endif
	gp = makeGameMessageGlist( gp, GMESG_KIND_18, GMESG_NAME18, DISPLAY_X1 + dx, DISPLAY_Y1 + 15, 0, 0 );
	/* 名前 */
	for( i = 0 ; i < 3 ; i ++ ) {
		dx = 0;
		str_work[0] = strNameEntry[i];
		str_work[1] = 0x00;
		for( j = 0 ; j < MAX_NUM_LETTER_SHAPE-2 ; j ++ ) {
			if( (int)str_work[0] == (int)tblInputLetter[j] ) break; 
		}
		if( j == MAX_NUM_LETTER_SHAPE-2 ) dx = 0;
		else dx = dxInputLetter[j];
		gp = makeStrGlistSub( gp, FONT_TYPE_MAIN, str_work, DISPLAY_X1 + 114 + 26 * i + dx, DISPLAY_Y1 + 160 );
	}

	return( gp );
}
/* ====================================================================== */
/*
	ゲームモードの変更
*/
static void changeGameModeName( void )
{
	register int	f, letter_kind;
	register int	num_letter_back, num_letter_back_next;

	switch( nameLetterStat ) {
		case NAME_LETTER_STAT_EXIT:
			/* 名前のコピー */
			copyPlayerName();
			/* 前回の名前のコピー */
			sprintf( strNameEntryBak, "%s", strNameEntry );
			if( mSelGameInfo.gameKind == GAME_KIND_TOUR ) {
				initMainGlistName2Next( GM_CFGREC );
			}
			else if( mSelGameInfo.gameKind == GAME_KIND_ATTACK ) initMainGlistRecord();
			else if( mSelGameInfo.gameKind == GAME_KIND_SCORE ) initMainGlistScRec();
		case NAME_LETTER_STAT_APPR:
		case NAME_LETTER_STAT_DISAP_WAIT:
		case NAME_LETTER_STAT_DISAP:
		case NAME_LETTER_STAT_LEFT_END:
		case NAME_LETTER_STAT_RIGHT_END:
			return;
			break;
		case NAME_LETTER_STAT_STOP_END:
			if( flagNameEnd & 1 ) {
				nameLetterStat = NAME_LETTER_STAT_LEFT_END;
				num_letter_back = (numSelLetter + 8) % MAX_NUM_NAME_DISP_LETTER;
				num_letter_back_next = (num_letter_back + 1) % MAX_NUM_NAME_DISP_LETTER;
				nameLetterBuf[num_letter_back] = nameLetterBuf[num_letter_back_next] - 1;
				if( nameLetterBuf[num_letter_back] < 0 ) {
					nameLetterBuf[num_letter_back] += MAX_NUM_LETTER_SHAPE;
				}
				numSelLetter --;
				if( numSelLetter < 0 ) numSelLetter = MAX_NUM_NAME_DISP_LETTER - 1;
				if( nameLetterBuf[numSelLetter] == NAME_LETTER_NUMBER_END ) flagNameEnd = 0;
				nameLetterNextRad = 2.0f * PAI * (float)numSelLetter
					/ (float)MAX_NUM_NAME_DISP_LETTER;
				#if SOUND_SW
				Na_SystemSound( NA_SYS_NAME_LROT, 0 );
				#endif
			}
			else if( flagNameEnd & 2 ) {
				nameLetterStat = NAME_LETTER_STAT_RIGHT_END;
				num_letter_back = (numSelLetter + 8) % MAX_NUM_NAME_DISP_LETTER;
				num_letter_back_next = num_letter_back - 1;
				if( num_letter_back_next < 0 ) {
					num_letter_back_next += MAX_NUM_NAME_DISP_LETTER;
				}
				nameLetterBuf[num_letter_back] = nameLetterBuf[num_letter_back_next] + 1;
				if( nameLetterBuf[num_letter_back] >= MAX_NUM_LETTER_SHAPE ) {
					nameLetterBuf[num_letter_back] -= MAX_NUM_LETTER_SHAPE;
				}
				numSelLetter ++;
				nameLetterNextRad = 2.0f * PAI * (float)numSelLetter
					/ (float)MAX_NUM_NAME_DISP_LETTER;
				if( numSelLetter >= MAX_NUM_NAME_DISP_LETTER ) 	numSelLetter = 0;
				if( nameLetterBuf[numSelLetter] == NAME_LETTER_NUMBER_END ) flagNameEnd = 0;
				#if SOUND_SW
				Na_SystemSound( NA_SYS_NAME_RROT, 0 );
				#endif
			}
			return;
			break;
	}

	/* 決定ボタン */
	if( mKnPad[KN_PAD_1P].pad.trig & BUTTON_ARG_ANY ) {
		switch( nameLetterStat ) {
			case NAME_LETTER_STAT_STOP:
				f = 0;
				letter_kind = nameLetterBuf[numSelLetter];
				if( idxNameEntryBuffer < PLAYER_NAME_MAX_SIZE ) {
					f = 1;
					if( letter_kind == NAME_LETTER_NUMBER_END ) f = 2;
				}
				else {
					if( letter_kind == NAME_LETTER_NUMBER_END ) f = 2;
					else if( letter_kind == NAME_LETTER_NUMBER_BS ) f = 1;
				}
				if( f ) {
					nameLetterStat = NAME_LETTER_STAT_ENTRY;
					/* カウンタの初期化 */
					countNameEntry = 0;
					#if SOUND_SW
					if( f == 1 ) Na_SystemSound( NA_SYS_NAME_TYPE, 0 );
					else if( f == 2 ) Na_SystemSound( NA_SYS_NAME_END, 0 );
					else if( f == 3 ) Na_SystemSound( NA_SYS_NAME_BS, 0 );
					#endif
				}
				break;
		}
	}
	/* キャンセルボタン */
	else if( mKnPad[KN_PAD_1P].pad.trig & BUTTON_ARG_CANCEL ) {
		switch( nameLetterStat ) {
			case NAME_LETTER_STAT_STOP:
				if( idxNameEntryBuffer > 0 ) idxNameEntryBuffer --;
				strNameEntry[idxNameEntryBuffer] = 0x00;
				#if SOUND_SW
				Na_SystemSound( NA_SYS_NAME_BS, 0 );
				#endif
				break;
		}
	}
	else if( pad[pad_no[0]].hold & L_JPAD || pad[pad_no[0]].x < (0-STICK_MIN) ) {
		if( nameLetterStat == NAME_LETTER_STAT_STOP ) {
			if( pad[pad_no[0]].hold & L_JPAD ) {
				nameLetterRotVelMag = 1.0f;
				flagNameLetterMag = 0;
			}
			else {
				nameLetterRotVelMag = calcVelMag();
				flagNameLetterMag = 1;
			}
			nameLetterStat = NAME_LETTER_STAT_LEFT;
			num_letter_back = (numSelLetter + 8) % MAX_NUM_NAME_DISP_LETTER;
			num_letter_back_next = (num_letter_back + 1) % MAX_NUM_NAME_DISP_LETTER;
			nameLetterBuf[num_letter_back] = nameLetterBuf[num_letter_back_next] - 1;
			if( nameLetterBuf[num_letter_back] < 0 ) {
				nameLetterBuf[num_letter_back] += MAX_NUM_LETTER_SHAPE;
			}
			numSelLetter --;
			if( numSelLetter < 0 ) numSelLetter = MAX_NUM_NAME_DISP_LETTER - 1;
			nameLetterNextRad = 2.0f * PAI * (float)numSelLetter
				/ (float)MAX_NUM_NAME_DISP_LETTER;
			#if SOUND_SW
			Na_SystemSound( NA_SYS_NAME_LROT, 0 );
			#endif
		}
	}
	else if( pad[pad_no[0]].hold & R_JPAD || pad[pad_no[0]].x > STICK_MIN ) {
		if( nameLetterStat == NAME_LETTER_STAT_STOP ) {
			if( pad[pad_no[0]].hold & R_JPAD ) {
				nameLetterRotVelMag = 1.0f;
				flagNameLetterMag = 0;
			}
			else {
				nameLetterRotVelMag = calcVelMag();
				flagNameLetterMag = 1;
			}
			nameLetterStat = NAME_LETTER_STAT_RIGHT;
			num_letter_back = (numSelLetter + 8) % MAX_NUM_NAME_DISP_LETTER;
			num_letter_back_next = num_letter_back - 1;
			if( num_letter_back_next < 0 ) {
				num_letter_back_next += MAX_NUM_NAME_DISP_LETTER;
			}
			nameLetterBuf[num_letter_back] = nameLetterBuf[num_letter_back_next] + 1;
			if( nameLetterBuf[num_letter_back] >= MAX_NUM_LETTER_SHAPE ) {
				nameLetterBuf[num_letter_back] -= MAX_NUM_LETTER_SHAPE;
			}
			numSelLetter ++;
			nameLetterNextRad = 2.0f * PAI * (float)numSelLetter
				/ (float)MAX_NUM_NAME_DISP_LETTER;
			if( numSelLetter >= MAX_NUM_NAME_DISP_LETTER ) 	numSelLetter = 0;
			#if SOUND_SW
			Na_SystemSound( NA_SYS_NAME_RROT, 0 );
			#endif
		}
	}

	return;
}

 /* ====================================================================== */
/*
	初期化
*/
static void initMainGlistName2Next( int next_game_mode )
{
	/* ゲームモード */
	gameModeOld = game_mode;
	gameModeNext = next_game_mode;
	game_mode = GM_NAME2NEXT;
	game_stat = GAME_STAT_NAME;
	gameModeChangeFlag = 1;
	gameModeSeq = 0;
	gameModeCount = 0;
	vram_mode = VM_NORMAL;
	Graph_timing_req = GRAPH_TIMING_NAME;

	/* プレイヤー制御変数 */
	
	/* ホバー制御フラグ */

	/* 海制御フラグ */

	/* シグナル制御 */

	/* 初期化関数 */
	initMoveMask( MASK_MODE_FADE_OUT, 14, 20 );

	/* 記録画面用カメラ */

	/* sound */
	#if SOUND_SW
//	if( gameModeNext == GM_OPEN ) Nas_SyncFadeOut( NA_SCENE_TITLE );
	#endif
}

/* ====================================================================== */
/*
	ネームバッファから記録バッファへのコピー
*/
static void copyPlayerName( void )
{
	register int	i, j, course_num;
	
	if( strNameEntry[0] == 0x00 ) {
		for( i = 0 ; i < 3 ; i ++ ) {
			strNameEntry[i] = mMachine[0][rideMachineNum[0]].manName[i];
		}
		strNameEntry[i] = 0x00;
		nameEntryFirstFlag = 1;
	}

	if( mSelGameInfo.gameKind == GAME_KIND_TOUR ) {
		/* ツアーモード */
		for( i = 0 ; i < (tourRaceNum + 1) ; i ++ ) {
			course_num = tourUseCourseNum[tourMyDif][i];
			if( frank_total_rec[course_num] ) {
				for( j = 0 ; j < PLAYER_NAME_MAX_SIZE+1 ; j ++ ) {
					if( reverseRunFlag ) rev_time_rec[course_num].name[j] = strNameEntry[j];
					else total_time_rec[course_num][frank_total_rec[course_num]-1].name[j] = strNameEntry[j];
				}
				if( reverseRunFlag ) rev_time_rec[course_num].name[j] = 0x00;
				else total_time_rec[course_num][frank_total_rec[course_num]-1].name[j] = 0x00;
			}
			if( frank_lap_rec[course_num] ) {
				for( j = 0 ; j < PLAYER_NAME_MAX_SIZE+1 ; j ++ ) {
					lap_time_rec[course_num].name[j] = strNameEntry[j];
				}
				lap_time_rec[course_num].name[j] = 0x00;
			}
		}
	}
	else if( mSelGameInfo.gameKind == GAME_KIND_SCORE ) {
		/* SCORE ATTACK */
		if( frank_sca ) {
			for( j = 0 ; j < PLAYER_NAME_MAX_SIZE+1 ; j ++ ) {
				sca_score_rec[ncourse][frank_sca-1].name[j] = strNameEntry[j];
			}
			sca_score_rec[ncourse][frank_sca-1].name[j] = 0x00;
		}
	}
	else {
		/* TIME ATTACK */
		if( frank_total_rec[ncourse] ) {
			for( j = 0 ; j < PLAYER_NAME_MAX_SIZE+1 ; j ++ ) {
				if( reverseRunFlag ) rev_time_rec[ncourse].name[j] = strNameEntry[j];
				else total_time_rec[ncourse][frank_total_rec[ncourse]-1].name[j] = strNameEntry[j];
			}
			if( reverseRunFlag ) rev_time_rec[ncourse].name[j] = 0x00;
			else total_time_rec[ncourse][frank_total_rec[ncourse]-1].name[j] = 0x00;
		}
		if( frank_lap_rec[ncourse] ) {
			for( j = 0 ; j < PLAYER_NAME_MAX_SIZE+1 ; j ++ ) {
				lap_time_rec[ncourse].name[j] = strNameEntry[j];
			}
			lap_time_rec[ncourse].name[j] = 0x00;
		}
	}
}

/* ====================================================================== */
/*
	倍率計算
*/
static float calcVelMag( void )
{
	register float	mag;
	register  int	x, w;

	x = (int)pad[pad_no[0]].x;

	if( x < 0 ) x = 0 - x;

	w = STICK_MAX - STICK_MIN;
	if( x > STICK_MAX ) x = STICK_MAX;
	else if( x < STICK_MIN ) x = STICK_MIN;

	x -= STICK_MIN;

	mag = (float)x * 2.0f / (float)w;

	if( mag > 2.0f ) mag = 2.0f;
	if( mag < 0.8f ) mag = 0.8f;
	
	return( mag );
}
