/******************************************************************************************
	CRTest PS   (c) 1999-2001 ISL

	station.c:			Spinny camera code for the Level 4 "multi-station" games
******************************************************************************************/

// The Prototype Egg-Laying Game...
// 3 eggs is a beginner
// 4 eggs is a nice leisurely lay
// 6 eggs is the player who's got egg-catching & chicken-feeding sussed
// 7 eggs is doable.
// Current record with the settings in this file (24-Feb-2000) is Flynnie with 8 eggs, 1 dropped

// (note - I've moved the catapult prototype out to it's own file. Luv, Fred)

///#define MAP_SCALE_ADJUST 8


#include "global.h"
#include "main.h"
#include "menu.h"
#include "map_draw.h"
#include "bff_load.h"
#include "collide.h"
#include "debris.h"
#include "lights.h"
#include "timer.h"
#include "maths.h"

//#include "puzzles.h"
#include "map_view.h"

#include "global.h"
#include <psitypes.h>

// ============================================


/*
void stationDraw();
void stationCycle();


typedef struct stationVarStr
{
	int rqd_ya;
	int rqd_xa;
	int ya;
	int xa;
} stationVarStr;

stationVarStr stationVars;




// ============================================

// Prototype stuff for the egg laying game...


// rqd :-
// Three chicken indicators with a "hungry / fed / munching" graph
// Four rolling-egg tube bars, with different-speed eggs.
// One button (O) to add feed in at the top of the currently selected chicken
// One button (X) to shove the bowl in at the bottom of the currently selected chicken



#define EGGLAY_CHUTES 4
#define EGGLAY_CHICKENS 3
#define EGGLAY_EGGS 20

int CHUTE_X		=   -220;
int CHUTE_XSEP	=   20;
int CHUTE_W		=   10;
int CHUTE_Y		=   -90;
int CHUTE_H		=   60;

#define EGGLAY_NOTHING 0
#define EGGLAY_BOWL_READY 1

#define EGGLAY_CHICKEN_HUNGRY 0
#define EGGLAY_CHICKEN_STRAINING 1
#define EGGLAY_CHICKEN_RECOVERING 2

#define EGGLAY_MAXIMUM (4096 * 100)

//int egglay_eggpos[EGGLAY_CHUTES];		// countdowns till egg reaches bottom (4096'd)
int egglay_eggpos[EGGLAY_EGGS];		// countdowns till egg reaches bottom (4096'd)
int egglay_chute[EGGLAY_EGGS];

int egglay_eggspeeds[EGGLAY_CHUTES] = {2000, 1400,3000,2300};	// 4096'd speeds of the chutes
int egglay_chickens[EGGLAY_CHUTES] = {0,1,1,2};

int egglay_currentchute;
int egglay_gotcount = 0;
int egglay_lostcount = 0;
char egglay_message[100];
char egglay_message_timer;
int egglay_doing;

int egglay_chicken_doing[EGGLAY_CHICKENS];
int egglay_ready[EGGLAY_CHICKENS];

int egglay_readyspeed[EGGLAY_CHICKENS] = {30000,28000,30000};
int egglay_starvespeed[EGGLAY_CHICKENS] = {1000,1000,1000};
int egglay_strainspeed[EGGLAY_CHICKENS] = {10000,10000,10000};
int egglay_recoverspeed[EGGLAY_CHICKENS] = {3000,4000,3000};

int egglay_chick_x[EGGLAY_CHICKENS];// = {CHUTE_X,CHUTE_X + CHUTE_XSEP * 3/2, CHUTE_X + CHUTE_XSEP * 3};

#define EGGLAY_TIMER (60*FRAMESEC)


int egglay_time_remaining;


void egglayInit()
{
	int i;

	for(i = 0; i < EGGLAY_EGGS; i++)
	{
		egglay_eggpos[i] = -1;
	}
	for(i = 0; i < EGGLAY_CHICKENS; i++)
	{
		egglay_ready[i] = 0;
		egglay_chicken_doing[i] = EGGLAY_CHICKEN_HUNGRY;
	}

	egglay_currentchute = 0;
	egglay_gotcount = 0;
	egglay_lostcount = 0;
	egglay_message_timer = 0;
	egglay_doing = EGGLAY_NOTHING;

	egglay_time_remaining = EGGLAY_TIMER;

}

void panel_number(int x, int y, int number, int hilite)
{
	int counter;
	int digit;
	int fract;
	int opened = 0;
	char string[4];

	digit = 1000000;
	counter = 6;

	while(counter != -1)
	{
		fract = number / digit;

		if (fract != 0 || counter == 0 || hilite >= counter)
			opened = 1;

		number = number - fract * digit;

		sprintf(string,"%d",fract);
		if(opened)
		{
			if(counter == hilite)
			{
				fontPrint(font, x,y, string, 128,128,128);
			}
			else
			{
				fontPrint(font, x,y, string, 64,64,64);
			}
		}
		x += 12;
		digit = digit / 10;
		counter--;
	}

}


void egglayPanel()
{
	int i;
	char message[100];

	for(i = 0; i < EGGLAY_CHUTES; i++)
	{
// chute
		overlayOutlineRectangle(CHUTE_X + CHUTE_XSEP * i, CHUTE_Y,CHUTE_W,CHUTE_H, 0x00ffff);
		if(egglay_currentchute == i)
		{
// selected indicator
			overlayOutlineRectangle(CHUTE_X + CHUTE_XSEP * i - 2,
				CHUTE_Y - 2,
				CHUTE_W+4,CHUTE_H + 4, 0xffffff);

// bowl
			if(egglay_doing == EGGLAY_BOWL_READY)
			{
				overlayFilledRectangle(CHUTE_X + CHUTE_XSEP * i - 2,
					CHUTE_Y + CHUTE_H + CHUTE_W,
					CHUTE_W+4,CHUTE_W, 0x11ccff);
			}
		}
	}


	for(i = 0; i < EGGLAY_EGGS; i++)
	{
// egg
		if(egglay_eggpos[i] != -1)
		{
			overlayFilledRectangle(CHUTE_X + CHUTE_XSEP * egglay_chute[i],
				CHUTE_Y + CHUTE_H - egglay_eggpos[i] * CHUTE_H / EGGLAY_MAXIMUM,
				CHUTE_W,CHUTE_W, 0xffffff);
		}
	}



	//temp debug
	egglay_chick_x[0] = CHUTE_X;// = {CHUTE_X,CHUTE_X + CHUTE_XSEP * 3/2, CHUTE_X + CHUTE_XSEP * 3};
	egglay_chick_x[1] =	CHUTE_X + CHUTE_XSEP * 3/2;
	egglay_chick_x[2] =	CHUTE_X + CHUTE_XSEP * 3;

	for(i = 0; i < EGGLAY_CHICKENS; i++)
	{
		int x = egglay_chick_x[i];
		int h;


		if(egglay_chicken_doing[i] == EGGLAY_CHICKEN_HUNGRY)
		{
// Display the chicken's hunger meter
			h = CHUTE_W * egglay_ready[i] / EGGLAY_MAXIMUM;
			overlayOutlineRectangle(x, CHUTE_Y - CHUTE_W*2, CHUTE_W,CHUTE_W, 0x00ff00);
			overlayFilledRectangle(x,  CHUTE_Y - CHUTE_W - h, CHUTE_W,h, 0x00ff00);
		}
		else
		{
// Display a little countdown graph to laying time
			h = CHUTE_W * egglay_ready[i] / EGGLAY_MAXIMUM;
			overlayOutlineRectangle(x + CHUTE_W/4, CHUTE_Y - CHUTE_W*2, CHUTE_W/2,CHUTE_W, 0xFF40FF);
			overlayFilledRectangle(x + CHUTE_W/4, CHUTE_Y - CHUTE_W - h, CHUTE_W/2,h, 0xFF40FF);
		}
	}

// panel info
//	fontPrint(font, -60,95, "The Laying Game", 64,64,128);
	sprintf(message,"Eggs Collected %d",egglay_gotcount);
	fontPrint(font, -200,105, message, 128,128,0);
	sprintf(message,"Eggs Lost %d",egglay_lostcount);
	fontPrint(font,  100,105, message, 128,128,0);
	sprintf(message,"%d",egglay_time_remaining/25);
	fontPrint(font, -30,0, message, 128,0,128);

// temporary message
//	if(egglay_message_timer)
//	{
//		fontPrint(font, -180,0, egglay_message, 128,64,128);
//	}




}

void egglayCycle()
{
//	int i;
	int chicken;
	int chute;
	int egg;



	if(padData.debounce[0] & (PAD_L1 | PAD_L2 | PAD_LEFT))
	{
		egglay_currentchute = egglay_currentchute -1;
		if(egglay_currentchute < 0)
			egglay_currentchute = EGGLAY_CHUTES-1;
		egglay_doing = EGGLAY_NOTHING;
	}
	if(padData.debounce[0] & (PAD_R1 | PAD_R2 | PAD_RIGHT))
	{
		egglay_currentchute = egglay_currentchute +1;
		if(egglay_currentchute >= EGGLAY_CHUTES)
			egglay_currentchute = 0;
		egglay_doing = EGGLAY_NOTHING;
	}

	if(padData.digital[0] & (PAD_CROSS))
	{
		egglay_doing = EGGLAY_BOWL_READY;
	}
	else
	{
		egglay_doing = EGGLAY_NOTHING;
	}


//	chicken = egglay_chickens[egglay_currentchute];



	for(chicken = 0; chicken < EGGLAY_CHICKENS; chicken++)
	{
		if(egglay_chicken_doing[chicken] == EGGLAY_CHICKEN_STRAINING)
		{
// It's straining to lay...
// When it's ready, an egg begins to roll down each chute belonging to that chicken
			egglay_ready[chicken] -= egglay_strainspeed[chicken];
			if(egglay_ready[chicken]<= 0)
			{
				for(chute = 0; chute < EGGLAY_CHUTES; chute++)
				{
					if(egglay_chickens[chute] == chicken)
					{
						int egg;
						for(egg = 0; egg < EGGLAY_EGGS; egg++)
						{
							if(egglay_eggpos[egg] == -1)
							{
								egglay_eggpos[egg] = EGGLAY_MAXIMUM;
								egglay_chute[egg] = chute;
								break;
							}
						}
						
					}
				}
//				egglay_chicken_doing[chicken] = EGGLAY_CHICKEN_HUNGRY;
//				egglay_ready[chicken] = 0;
				egglay_chicken_doing[chicken] = EGGLAY_CHICKEN_RECOVERING;
				egglay_ready[chicken] = 0;
			}
		}
		else if(egglay_chicken_doing[chicken] == EGGLAY_CHICKEN_RECOVERING)
		{
			egglay_ready[chicken] += egglay_recoverspeed[chicken];
			if(egglay_ready[chicken] >= EGGLAY_MAXIMUM)
			{
				egglay_chicken_doing[chicken] = EGGLAY_CHICKEN_HUNGRY;
				egglay_ready[chicken] = 0;
			}

		}
		else if(egglay_chicken_doing[chicken] == EGGLAY_CHICKEN_HUNGRY)
		{

// Is this the chicken we've got selected?
			if(chicken == egglay_chickens[egglay_currentchute]
				&& (padData.debounce[0] & (PAD_CIRCLE))
				&& (egglay_doing != EGGLAY_BOWL_READY)
				)
			{
				egglay_ready[chicken] += egglay_readyspeed[chicken];

	// If we've fed a chicken up to the top, it starts straining to lay,
	// during which time, it can't be fed
				if(egglay_ready[chicken] >= EGGLAY_MAXIMUM)
				{
					egglay_chicken_doing[chicken] = EGGLAY_CHICKEN_STRAINING;
					egglay_ready[chicken] = EGGLAY_MAXIMUM;	// ...and start to count down
				}
			}
			else
			{
	// the chicken gets hungrier if you leave it alone
				egglay_ready[chicken] -= egglay_starvespeed[chicken];
				if(egglay_ready[chicken] < 0)
				{
					egglay_ready[chicken] = 0;
				}
			}

		}
	}

	for(egg = 0; egg < EGGLAY_EGGS; egg++)
	{
		if(egglay_eggpos[egg] != -1)
		{
			egglay_eggpos[egg] -= egglay_eggspeeds[egglay_chute[egg]];
			if(egglay_eggpos[egg] < 0)
			{

				if(egglay_time_remaining)
				{
	// Egg falls off the bottom of the chute
	// ? Is the bowl there ?
					if(egglay_currentchute == egglay_chute[egg] && egglay_doing == EGGLAY_BOWL_READY)
					{
	// we get an egg.
						egglay_gotcount++;
						sprintf(egglay_message,"You got an egg!");
						egglay_message_timer = 20;
					}
					else
					{
	// the egg splats
						egglay_lostcount++;
						sprintf(egglay_message,"SPLAT!");
						egglay_message_timer = 20;
					}
				}
				egglay_eggpos[egg] = -1;
			}
		}
	}

	if(!egglay_time_remaining)
	{
		sprintf(egglay_message,"Time Up!");
		egglay_message_timer = 20;
	}
	else
	{
		egglay_time_remaining--;
	}

	if(egglay_message_timer)
	{
		egglay_message_timer--;
	}
}

// ============================================


// ============================================
#define STATION_PROJECTION	380	//400
#define STATION_RANGE	1000



void stationStart(char *fname)
{
	int i;

	mapviewLoadMap(fname);

	draw_routine = stationDraw;
	cycle_routine = stationCycle;

	memset(&stationVars,0,sizeof(stationVarStr));

	GsSetProjection(STATION_PROJECTION);

	egglayInit();

}

void stationEnd()
{
	if(mapview_level_bff)
	{
		mapviewReleaseMap();
	}
	GsSetProjection(950);
	menuStart();
}

void stationDraw()
{
	stationVarStr *vars = &stationVars;
	char temp[80];

	TIMER_START(TIMER_MAPREND);
	MapDraw_DrawFMA_World(mapview_fma_world);
	TIMER_STOP(TIMER_MAPREND);


#if PREVIEW_VERSION==1
	overlayPreviewOverlay();
#else
//	sprintf(temp,"ang %d %d\n",vars->xa,vars->ya);
//	fontPrint(font, -70,64+20, temp, 128,128,0);
#endif

	egglayPanel();
}



void vectorRotateByXYAngles(VECTOR *in, VECTOR *out, int xa, int ya)
{
	int x,y,z;
	x = in->vx;
	z = ( in->vz * rcos(xa) + in->vy * rsin(xa))>>12;
	y = (-in->vz * rsin(xa) + in->vy * rcos(xa))>>12;

	out->vy = y;
	out->vx = ( x * rcos(ya) + z * rsin(ya))>>12;
	out->vz = (-x * rsin(ya) + z * rcos(ya))>>12;
}

int stationAngleHomer(int angle, int target)
{
	int dangle;
	dangle = (target - angle) & 4095;
	if(dangle >= 2048) dangle -= 4096;
	angle = angle + ((dangle * 500)>>12);
	return (angle & 4095);
}



// Actually, what we want here, rather than "angles" is for the editor to include a collection of
// (camera pos) (camera target) points, possibly plus a centrepoint, and we need to move between 'em

// Oh, and we need to fisheye a bit more that this for enough of the interiors to be visible

void stationCycle()
{
	static VECTOR ref = {0,0,STATION_RANGE};
	VECTOR vec;
	stationVarStr *vars = &stationVars;

	vars->rqd_xa = -0x180;
	vars->ya = stationAngleHomer(vars->ya,vars->rqd_ya);
	vars->xa = stationAngleHomer(vars->xa,vars->rqd_xa);

	if(padData.debounce[0] & (PAD_L1 | PAD_L2 | PAD_LEFT))
	{
		vars->rqd_ya = (vars->rqd_ya - 0x400) & 4095;
	}
	if(padData.debounce[0] & (PAD_R1 | PAD_R2 | PAD_RIGHT))
	{
		vars->rqd_ya = (vars->rqd_ya + 0x400) & 4095;
	}

	vectorRotateByXYAngles(&ref, &vec, vars->xa, vars->ya);



// "Egglaying room"
	mycamera.vpx =  -1107 - vec.vx;
	mycamera.vpy = -750;
	mycamera.vpz = -1501 - vec.vz;

	mycamera.vrx = mycamera.vpx + vec.vx;
	mycamera.vry = mycamera.vpy + vec.vy /2;
	mycamera.vrz = mycamera.vpz + vec.vz;




	mapCamSetRefView2L(&mycamera);

	egglayCycle();

	if(padData.debounce[0] & PAD_START)
	{
		stationEnd();
	}
}
*/