/***************************************************************************************************
						Ultra 64 MARIO Brothers

							  star select module

			Copyright 1996 Nintendo co., ltd.  All rights reserved

				  This module was programmed by Iwamoto Daiki

							Junuary 19, 1996
 ***************************************************************************************************/
#include "headers.h"
#include "include/iwa_select.h"

#define state_flag stw_work0
#define counter    stw_work1
#define str_scale  stw_work5
#define STARMAX		6

static StrategyRecord* CourseStar[8];

static char goTogame;


static unsigned char get_starNum;
static char star_num;
static unsigned char next_level;

extern Gfx RCP_selectpict[];
extern unsigned long e_select_polystar[];

static char cursor = 0;
static char cursor_flag = 0;
static int select_counter = 0;

/*================================================================================================*/
/*================================================================================================*/

/**************************************************************************************************/
/*	Select Star Strategy Polygon.																		  */
/**************************************************************************************************/
extern void s_select_polystar(void)
{

	switch (execstp->s[state_flag].d) {
		case 0: execstp->s[str_scale].f -= 0.1;
				if (execstp->s[str_scale].f <1.0 ) execstp->s[str_scale].f = 1.0;
				execstp->s[stw_animeangleY].d = 0; 
				break;

		case 1: execstp->s[str_scale].f += 0.1; 
				if (execstp->s[str_scale].f > 1.3) execstp->s[str_scale].f = 1.3;
				execstp->s[stw_animeangleY].d += 0x800;
				break;

		case 2: execstp->s[stw_animeangleY].d += 0x800;
				break;
	}

	s_set_scale(execstp->s[str_scale].f);
	execstp->s[counter].d++;


}

/**************************************************************************************************/
/*	Select Star Main Init.																		  */
/**************************************************************************************************/
static void HiscoreStarSet(unsigned char get_starFlag)
{
	
	if (get_starFlag & (0x01 << 6)) {
		CourseStar[6] = s_makeobj_absolute(execstp,0,S_polystar,e_select_polystar, 370, 24,-300,0,0,0);
		CourseStar[6]->s[str_scale].f = 0.8; 
		CourseStar[6]->s[state_flag].d = 2;
	}
}
/**************************************************************************************************/
/*	Select Star Main Init.																		  */
/**************************************************************************************************/
extern void s_CoStarmain_init(void)
{
	short i=0;
	int shapeNo[10];
	unsigned char get_starFlag = BuGetStarFlag(activePlayerNo-1,activeCourseNo-1);

	star_num = 0;

	while (i != get_starNum) {
		if (get_starFlag & (0x01 << star_num) ) {
			shapeNo[star_num] = S_polystar;  
			i++;
		}
		else							 		{
			shapeNo[star_num] = S_shadestar; 
			if (next_level == 0) {
				next_level = star_num+1;
				cursor_flag = star_num;
			}
		}
		star_num++;
	}

	if (star_num == get_starNum && star_num != STARMAX) {
			shapeNo[star_num] = S_shadestar; 
			next_level = star_num+1;
			cursor_flag = star_num;
			star_num++;
	}

	if ( get_starNum == STARMAX) next_level = star_num;
	if ( get_starNum == 0) next_level = 1;

	for (i=0; i< star_num; i++) {
		CourseStar[i] = s_makeobj_absolute(execstp,0,shapeNo[i],e_select_polystar,(-75)*(star_num-1) + 152*i,   248, -300,  0,0,0);
		CourseStar[i]->s[str_scale].f = 1.0;
	}


	HiscoreStarSet(get_starFlag);


}
/**************************************************************************************************/
/*	Select Star Main Init.																		  */
/**************************************************************************************************/
extern void s_CoStar_main(void)
{
	char i;
	unsigned char flag;
	unsigned char get_starFlag = BuGetStarFlag(activePlayerNo-1,activeCourseNo-1);


	if (get_starNum != STARMAX) {

		cursor = 0;
		ContCursorEvent(2,&cursor_flag,0,(get_starNum+1)-1);

		flag = cursor_flag;
		for (i=0; i<star_num; i++) {
			if (get_starFlag & (0x01 << i) || i == (next_level-1) ) {
				if (flag == 0) {
					cursor = i;
					break;
				}
				flag--;
			}
		}
	}
	else {

		ContCursorEvent(2,&cursor_flag,0,(star_num)-1);

		cursor = cursor_flag;
	}
//rmonpf(("flag   %d\n",cursor_flag));
//rmonpf(("cursor %d\n",cursor));



	for (i=0; i<star_num; i++) {
		if (cursor == i) CourseStar[i]->s[state_flag].d = 1;
		else			 CourseStar[i]->s[state_flag].d = 0;
	}


}








/*================================================================================================*/
/*			Star Select Message 																  */
/*================================================================================================*/
/********************************************************************************/
/*	Star Select Picture.														*/
/********************************************************************************/
static void DrawCoursePicture(void)
{
	unsigned char Number[8];

	iTranslate(1,158,81,0);
	gSPDisplayList(graphPtr++, RCP_selectpict);	
	gSPPopMatrix(graphPtr++,G_MTX_MODELVIEW);

	gSPDisplayList(graphPtr++, RCP_tfont2_on);	
	gDPSetEnvColor(graphPtr++,255,255,255,255); 
#if CHINA
	I_itochar_16((activeCourseNo-1)+1,Number);
#else
	I_itochar((activeCourseNo-1)+1,Number);
#endif

	if (activeCourseNo< 10){ Draw16bitFont(2,152,158, Number); }
	else				   { Draw16bitFont(2,143,158, Number); } 

	gSPDisplayList(graphPtr++, RCP_tfont2_off);	

}
/********************************************************************************/
/*	Star Select message.															*/
/********************************************************************************/

static void StarSelectScene(void)
{
#if ENGLISH 
	unsigned char se8_hiscore[]= 	{f8_M, f8_Y, f8_S, f8_C, f8_O, f8_R, f8_E, 0xff,};	//maiscore
#elif CHINA
	unsigned char se8_hiscore[]= 	{0x1, 0x0, 0x1, 0x1, 0x1, 0x2, 0x1, 0x3, 0xff, 0xff,};	//maiscore
#else
	unsigned char se8_hiscore[]= 	{0x8e, 0x71, 0x7c, 0x79, 0x70, 0xff,};	//maiscore
#endif
	unsigned char se88_num[]= 	{0x00, 0xff,};
	unsigned char 	 **crs  = (unsigned char**)(SegmentToVirtual(crsNamePtr));
	unsigned char *crsname  = (unsigned char* )(SegmentToVirtual(crs[activeCourseNo-1]));
	unsigned char **starnamep = (unsigned char**)(SegmentToVirtual(starnamePtr));
	unsigned char  *starname;
 
	short ce_posx,ce_posx2;
	char i;

	MakeSelectProjection();

/*------------- Draw 16 Bit ----------------------------------*/

	gSPDisplayList(graphPtr++, RCP_tfont2_on);	
	gDPSetEnvColor(graphPtr++,255,255,255,255); 
	DrawMyScore(activePlayerNo-1,activeCourseNo-1, 155,  106);
	gSPDisplayList(graphPtr++, RCP_tfont2_off);	

/*------------- Draw 8 Bit ----------------------------------*/

	gSPDisplayList(graphPtr++, RCP_mess_font_on);
	gDPSetEnvColor(graphPtr++,0,0,0,255); 

	if (BuGetNumCoins(activePlayerNo-1,activeCourseNo-1) != 0) 
#if CHINA
            Draw8bitFont(89,118,se8_hiscore);
#else
            Draw8bitFont(102,118,se8_hiscore);
#endif

#if CHINA
	ce_posx = CharCentering(160,(crsname+6),16.);
	Draw8bitFont(ce_posx,30,(crsname+6));
#else
	ce_posx = CharCentering(160,(crsname+3),10.);
	Draw8bitFont(ce_posx,33,(crsname+3));
#endif

	gSPDisplayList(graphPtr++, RCP_mess_font_off);	


/*------------- Draw Picture ----------------------------------*/
	DrawCoursePicture();

/*------------- Draw 88 Bit ----------------------------------*/

#if CHINA
	gSPDisplayList(graphPtr++, RCP_mess_font_on);
        gDPSetEnvColor(graphPtr++,0,0,0,255);
        if (star_num != 0) {
                starname = (unsigned char*)(SegmentToVirtual(starnamep[(activeCourseNo-1)*6
+cursor]));
                ce_posx2 = CharCentering(163,starname,16.);
                Draw8bitFont(ce_posx2,141,starname );
        }
        gSPDisplayList(graphPtr++, RCP_mess_font_off);

	gSPDisplayList(graphPtr++, RCP_s88font_on);	
	gDPSetEnvColor(graphPtr++,0,0,0,255); 
        for (i=1; i<=star_num; i++) {
                se88_num[0] = i;
                DrawS88Font((156-17*(star_num-1)) + 34*(i-1),38 ,se88_num);
        }
	gSPDisplayList(graphPtr++, RCP_s88font_off);	
#else
	gSPDisplayList(graphPtr++, RCP_s88font_on);	
	gDPSetEnvColor(graphPtr++,0,0,0,255); 
	if (star_num != 0) {
		starname = (unsigned char*)(SegmentToVirtual(starnamep[(activeCourseNo-1)*6+cursor]));
		ce_posx2 = CharCentering(163,starname,8.);
		DrawS88Font(ce_posx2,81,starname );
	}

	for (i=1; i<=star_num; i++) {
		se88_num[0] = i;
		DrawS88Font((156-17*(star_num-1)) + 34*(i-1),38 ,se88_num);   
	}
	gSPDisplayList(graphPtr++, RCP_s88font_off);	
#endif

}



/*==============================================================================*/
/*==============================================================================*/

/*==============================================================================*/
/*	Star Select Message Main.													*/
/*==============================================================================*/
/********************************************************************************/
/*	Star Select message.															*/
/********************************************************************************/
extern ulong StarSelectSceneProc(short code, long param)
{

	if (code == MAP_CBACK_EXEC) {
		StarSelectScene();
	}

	return(0);
}
/*==============================================================================*/
/*	Star Select Init.															*/
/*==============================================================================*/
/********************************************************************************/
/*	Star Select main Init.															*/
/********************************************************************************/
extern ulong SeStarSelectInitProc(short code, long param)
{
	unsigned char get_starFlag = BuGetStarFlag(activePlayerNo-1,activeCourseNo-1);

	goTogame = 0;
	next_level = 0;
	star_num = 0;
	select_counter = 0;

	get_starNum = BuGetCourseStars(activePlayerNo-1,activeCourseNo-1);

	if (get_starFlag & (0x01<<6)) 	get_starNum--;

}

/*==============================================================================*/
/*	Star Select Main.															*/
/*==============================================================================*/
/********************************************************************************/
/*	Star Select main process.															*/
/********************************************************************************/
extern ulong SeStarSelectProcess(short code, long param)
{

	if (select_counter>10 && ((contOr->trigger & CONT_A)  || (contOr->trigger & CONT_START)|| (contOr->trigger & CONT_B))) {
		Na_FixSeFlagEntry(NA_SYS_LETS_GO);
		SendMotorEvent(60,70);		/* MOTOR 1997.6.5 */
		SendMotorDecay(1);			/* MOTOR 1997.6.5 */

		if (next_level >= (cursor+1)) {
			goTogame = cursor+1;
		}
		else {
			goTogame = next_level;
		}

		select_starNo = cursor+1;

//rmonpf(("hh %d\n",goTogame));
	}


	SnExecuteStrategy();
	select_counter++;

	return((ulong)goTogame);
}






