#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 "overlays.h"
#include "fx.h"
#include "deth.h"
#include "demo.h"
#include "enemies.h"
#include "dualshock.h"
#include "map_view.h"
#include "card.h"
#include "map_play.h"
#include "construct.h"
#include "subgame.h"
//#include "text_ids.h"
#include "egglaying.h"
#include "inventory.h"
#include "sound.h"
#include "loadsnd.h"
#include "startup.h"



#define EGGLAY_TIMER (40*FRAMESEC)

//#define EL_DEBUG_CAMERA
//#define EL_VIEW_CAMERA
//#define EL_VIEW_POS
//#define EL_VIEW_STATES
//#define EL_VIEW_EGGS

EGGLAYING_CTRL *el;


#define EL_CAMERA_X	 -60
#define EL_CAMERA_Y	 -476
#define EL_CAMERA_Z	 -420

#define EL_GARIB_X	 -1054
#define EL_GARIB_Y	 116
#define EL_GARIB_Z	 3909



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

int ELCHUTE_X		=   -220;
int ELCHUTE_XSEP	=   20;
int ELCHUTE_W		=   10;
int ELCHUTE_Y		=   42;
int ELCHUTE_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 eggAnimSpeeds[4] = {86, 66, 140, 106};


//int el_eggpos[EGGLAY_CHUTES];		// countdowns till egg reaches bottom (4096'd)
int el_eggpos[EGGLAY_EGGS];		// countdowns till egg reaches bottom (4096'd)
unsigned int el_eggtex[EGGLAY_EGGS];  // used for animated egg textures
int el_chute[EGGLAY_EGGS];

//int el_eggspeeds[EGGLAY_CHUTES] = {2000, 1400,3000,2300};	// 4096'd speeds of the chutes
// slow em down as their going faster at end, so total time is same
int el_eggspeeds[EGGLAY_CHUTES] = {1800, 1260,2740,2030};	// 4096'd speeds of the chutes

int el_eggfallspeed = 9000;
int el_chickens[EGGLAY_CHUTES] = {0,1,1,2};
int el_chickLastTimeFed[EGGLAY_CHICKENS];

int el_currentchute;
int el_gotcount = 0;
int el_lostcount = 0;
char el_message[100];
char el_message_timer;
int el_doing;

int el_chicken_doing[EGGLAY_CHICKENS];
int el_ready[EGGLAY_CHICKENS];

int el_readyspeed[EGGLAY_CHICKENS] = {30000,28000,30000};
int el_starvespeed[EGGLAY_CHICKENS] = {1000,1000,1000};
int el_strainspeed[EGGLAY_CHICKENS] = {10000,10000,10000};
int el_recoverspeed[EGGLAY_CHICKENS] = {3000,4000,3000};
int el_splat[EGGLAY_CHUTES];

int el_chick_x[EGGLAY_CHICKENS];// = {ELCHUTE_X,ELCHUTE_X + ELCHUTE_XSEP * 3/2, ELCHUTE_X + ELCHUTE_XSEP * 3};





int el_time_remaining;


#define NUM_EGGS_BRONZE  6
#define NUM_EGGS_SILVER  8
#define NUM_EGGS_GOLD    10




//which chicks belong to which station
short elChicks[4]= {EL_LAYER1, EL_BUNTY, EL_BUNTY, EL_LAYER2};

VECTOR elChickPos[NUM_EL_CHICKS] = 
{
	{-786, -610, -1337},
	{9, -580, 1307},
	{659, -630, -1287}
};

int elChickRot[NUM_EL_CHICKS] = {2520, 30, 1560};


VECTOR	elCamPos[4]=
{
	{ 300, -488, 56},							
	{-228, -596, -740},							
	{100, -636, -484},
	{-664, -652, -196},							
												
};

int elCamXRot[4] = {-104, 108, 16, 56};         
int elCamYRot[4] = {1576, 20, 3904, 2612};      


// ============================================
int EL_FOV = 380;	//400
#define STATION_RANGE	1000

int EL_CRICKET_X = -228;
int EL_CRICKET_Y = -110;
int elLineCount;
//int elDrawLine;


int elActFrom = 0;
// also need to set up different intro texts
// ============================================
void elStart4()
{
	elActFrom = 4;
	elStart();
}

void elStart2()
{
	elActFrom = 2;
	elStart();
}

// ============================================
void elStart1()
{
	elActFrom = 1;
	elStart();
}


/*
	SFX_EGGLAYING_SPLAT,
	SFX_EGGLAYING_LAY,
	SFX_EGGLAYING_LAY_SQUELCH,
	SFX_EGGLAYING_LAY_FART,
	SFX_EGGLAYING_TIRED,
	NUM_EGGLAYING_SAMPLES,
*/

void elStart()
{
	int i;
	PSIMODEL *model;


	subGameRestart = FALSE;



	loadsndLoadBank("A4_EGGHUT");
	mapviewLoadMap("A4_EGGHUT");

	el = (EGGLAYING_CTRL*)MALLOC(sizeof(EGGLAYING_CTRL));


			   

	if(gameCalledFrom != GAMECALLEDFROM_DEBUG)
	{
		if(!gameSaveData.levels_available[REPLAY_AVAILABLE_EGGLAYING])
		{
			gameSaveData.levels_available[REPLAY_AVAILABLE_EGGLAYING] = REPLAY_NOTBEATEN;
		}
	}


	ASSERT(el);
	memset(el, 0, sizeof(EGGLAYING_CTRL));

	draw_routine = elDraw;
	cycle_routine = elCycle;

	pReadPad->shockEnable=1;
	dsEvent_Init();

	loadsndSubGames(LEVNUMBER_A4_EGGHUT, el->pSamples, NUM_EGGLAYING_SAMPLES);



	GsSetProjection(EL_FOV);

	model = BFF_IsModelLoaded("EGGLAYER1");
	ASSERT(model);
	el->chicks[0].actor = actorCreate(model);
	ASSERT(el->chicks[0].actor);
	actorInitAnim(el->chicks[0].actor);
	
	el->chicks[0].actor->position = elChickPos[0];
	el->chicks[0].actor->psiData.object->rotate.vy = elChickRot[0];

	actorAnimate(el->chicks[0].actor, EGGLAYER_BREATH, TRUE, FALSE, 256, 0, 0);

	model = BFF_IsModelLoaded("BUNTY");
	ASSERT(model);
	el->chicks[1].actor = actorCreate(model);
	ASSERT(el->chicks[1].actor);
	actorInitAnim(el->chicks[1].actor);
	el->chicks[1].actor->position = elChickPos[1];
	el->chicks[1].actor->psiData.object->rotate.vy = elChickRot[1];

	actorAnimate(el->chicks[1].actor, EGGLAYER_BREATH, TRUE, FALSE, 256, 0, 0);


	model = BFF_IsModelLoaded("EGGLAYER2");
	ASSERT(model);
	el->chicks[2].actor = actorCreate(model);
	ASSERT(el->chicks[2].actor);
	actorInitAnim(el->chicks[2].actor);
	el->chicks[2].actor->position = elChickPos[2];
	el->chicks[2].actor->psiData.object->rotate.vy = elChickRot[2];

	actorAnimate(el->chicks[2].actor, EGGLAYER_BREATH, TRUE, FALSE, 256, 0, 0);
													 

	model = BFF_IsModelLoaded("EGG04");
	ASSERT(model);
	el->eggs[0][0].actor = actorCreate(model);
	ASSERT(el->eggs[0][0].actor);
	actorInitAnim(el->eggs[0][0].actor);
//	actorAnimate(el->eggs[0][0].actor, 0, 1, 0, 200, 0, 0);



	model = BFF_IsModelLoaded("EGG03");
	ASSERT(model);
	el->eggs[1][0].actor = actorCreate(model);
	ASSERT(el->eggs[1][0].actor);
	actorInitAnim(el->eggs[1][0].actor);

	model = BFF_IsModelLoaded("EGG02");
	ASSERT(model);
	el->eggs[2][0].actor = actorCreate(model);
	ASSERT(el->eggs[2][0].actor);
	actorInitAnim(el->eggs[2][0].actor);

	model = BFF_IsModelLoaded("EGG01");
	ASSERT(model);
	el->eggs[3][0].actor = actorCreate(model);
	ASSERT(el->eggs[3][0].actor);
	actorInitAnim(el->eggs[3][0].actor);
//	actorAnimate(el->eggs[3][0].actor, 0, 1, 0, 200, 0, 0);


	model = BFF_IsModelLoaded("EGG04");
	ASSERT(model);
	el->eggs[0][1].actor = actorCreate(model);
	ASSERT(el->eggs[0][1].actor);
	actorInitAnim(el->eggs[0][1].actor);


	model = BFF_IsModelLoaded("EGG03");
	ASSERT(model);
	el->eggs[1][1].actor = actorCreate(model);
	ASSERT(el->eggs[1][1].actor);
	actorInitAnim(el->eggs[1][1].actor);

	model = BFF_IsModelLoaded("EGG02");
	ASSERT(model);
	el->eggs[2][1].actor = actorCreate(model);
	ASSERT(el->eggs[2][1].actor);
	actorInitAnim(el->eggs[2][1].actor);

	model = BFF_IsModelLoaded("EGG01");
	ASSERT(model);
	el->eggs[3][1].actor = actorCreate(model);
	ASSERT(el->eggs[3][1].actor);
	actorInitAnim(el->eggs[3][1].actor);


	// Load up the Panel textures . . . .. .
	el->panel.chick[0] = textureFindInAllBanks("DCHICK");
	el->panel.chick[1] = textureFindInAllBanks("BUNTY");
	el->panel.chick[2] = textureFindInAllBanks("LCHICK");


	ASSERT(el->panel.chick[0]);
	ASSERT(el->panel.chick[1]);
	ASSERT(el->panel.chick[2]);

	el->panel.fillChick[0] = textureFindInAllBanks("FILLDCHICK");
	el->panel.fillChick[1] = textureFindInAllBanks("FILLBUNTY");
	el->panel.fillChick[2] = textureFindInAllBanks("FILLLCHICK");

	ASSERT(el->panel.fillChick[0]);
	ASSERT(el->panel.fillChick[1]);
	ASSERT(el->panel.fillChick[2]);


	el->panel.layChick[0] = textureFindInAllBanks("LAYDCHICK");
	el->panel.layChick[1] = textureFindInAllBanks("LAYBUNTY");
	el->panel.layChick[2] = textureFindInAllBanks("LAYLCHICK");

	ASSERT(el->panel.layChick[0]);
	ASSERT(el->panel.layChick[1]);
	ASSERT(el->panel.layChick[2]);


	el->panel.pipes = textureFindInAllBanks("PIPES2");

	ASSERT(el->panel.pipes);
	
	el->panel.egg[0] = textureFindInAllBanks("EGGR1A");
	el->panel.egg[1] = textureFindInAllBanks("EGGR2A");
	el->panel.egg[2] = textureFindInAllBanks("EGGR3A");
	el->panel.egg[3] = textureFindInAllBanks("EGGR4A");
	el->panel.egg[4] = textureFindInAllBanks("EGGR5A");


	ASSERT(el->panel.egg[0]);
	ASSERT(el->panel.egg[1]);
	ASSERT(el->panel.egg[2]);
	ASSERT(el->panel.egg[3]);
	ASSERT(el->panel.egg[4]);

	el->panel.baskOn = textureFindInAllBanks("BASKATCH");
	ASSERT(el->panel.baskOn);
	el->panel.baskOff = textureFindInAllBanks("BASKNORM");
	ASSERT(el->panel.baskOff);


	el->panel.splat = textureFindInAllBanks("SPLAT");
	ASSERT(el->panel.splat);

	el->panel.chickSel[0] = textureFindInAllBanks("DCHICKSEL");
	ASSERT(el->panel.chickSel[0]);
	el->panel.chickSel[1] = textureFindInAllBanks("BUNTYSEL");
	ASSERT(el->panel.chickSel[1]);
	el->panel.chickSel[2] = textureFindInAllBanks("LCHICKSEL");
	ASSERT(el->panel.chickSel[2]);


	el->panel.pipeSel = textureFindInAllBanks("SELPIPE");
	ASSERT(el->panel.pipeSel);

	el->grain = textureFindInAllBanks("SEED");
	ASSERT(el->grain);

	initCricketScores(EL_CRICKET_X, EL_CRICKET_Y, NUM_EGGS_BRONZE, NUM_EGGS_SILVER, NUM_EGGS_GOLD, 2);

	el->arrow = textureFindInAllBanks("RARROW");
	ASSERT(el->arrow);

	mycamera.vpx = EL_CAMERA_X;
	mycamera.vpy = EL_CAMERA_Y;
	mycamera.vpz = EL_CAMERA_Z;
	mycamera.vrx = EL_GARIB_X;
	mycamera.vry = EL_GARIB_Y;
	mycamera.vrz = EL_GARIB_Z;

	
	elInit();
//	nmeDogEnsureHourglass();


	el->state = EL_FADEIN;
	el->station = 1;

//	elLineCount = 0;
//	elDrawLine = 0; 

	myactor = el->chicks[0].actor;


	initStopWatch(160, -64, EL_FOV);
	initFowler(EL_FOV);

	//DB("elActFrom=%d\n", elActFrom);


}

void elEnd()
{
	if(mapview_level_bff)
	{
		mapviewReleaseMap();
	}
	actorFree(el->chicks[0].actor);
	actorFree(el->chicks[1].actor);
	actorFree(el->chicks[2].actor);
	actorFree(el->eggs[0][0].actor);
	actorFree(el->eggs[1][0].actor);
	actorFree(el->eggs[2][0].actor);
	actorFree(el->eggs[3][0].actor);
	actorFree(el->eggs[0][1].actor);
	actorFree(el->eggs[1][1].actor);
	actorFree(el->eggs[2][1].actor);
	actorFree(el->eggs[3][1].actor);
	freeStopWatch();
	freeFowler();


	FREE(el);
	el = NULL;

	GsSetProjection(950);
	dsEvent_Init();

//	if(gameCalledFrom == GAMECALLEDFROM_GAME)
//	{

 //	memoryShowStats();
 //	memoryShow();


	if(subGameRestart == TRUE)
	{
		// go in through the same route as you went in last time..
		switch(elActFrom)
		{
			case 4:
				if(el_gotcount >=6)
					gamePlayData->eggs_carried = 6;

				elStart4();
				break;
			case 2:
				elStart2();
				break;
			case 1:
				elStart1();
				break;

			default:
				elStart();
				break;
		}
	}
	else
	{

		switch(elActFrom)
		{
			case 4:
				if(el_gotcount >=6)
					gamePlayData->eggs_carried = 6;
				mapplayStart("A4_COOP1_N", 2);
				break;
			case 2:
				mapplayStart("A2_COOP1_D", 2);

				break;
			case 1:
				mapplayStart("A1_COOP1_N", 2);
				break;
			default:
				menuStart();

		}
	}

//	}
//	else
//		menuStart();
}




#if PALMODE==1
	#define EL_HGLASS_X  140
	#define EL_HGLASS_Y	 46
#else
	#define EL_HGLASS_X  140
	#define EL_HGLASS_Y	 36
#endif

			  

int el_seedHeight[NUM_EL_CHICKS]={525, 380, 525};
int el_seedStartHeight[NUM_EL_CHICKS]={675, 530, 675};
int el_seedEndHeight[NUM_EL_CHICKS]={375, 200, 375};

int EL_SEED_SPEED = 40;

int EL_SEED_SCALE = 400;
int EL_GRAIN_MOVE = 140;

int EL_SEED_CLIP_X = 206;
int EL_SEED_CLIP_Y = 0;
int EL_CLIP_XSIZE = 100;
int EL_CLIP_YSIZE = 5;

int EL_CLIP_DEPTH = 8;

int el_clip_size[EGGLAY_CHUTES] = {28, 22, 36, 35};

int EL_ARROW_Y = 110;
// ====================================================================================================
void elDraw()
{
	int i, j;

	GsSetProjection(EL_FOV);

	mapCamSetRefView2L(&mycamera);
	MapDraw_DrawFMA_World((FMA_WORLD2 *)mapview_fma_world);
	elPrintDebug();


	if(mapdrawIsSphereVisible(&el->chicks[0].actor->position, el->chicks[0].actor->radius))
	{
//		actorMaintainAnimation(el->chicks[0].actor);
		EXLIGHT_SetupActorLights(el->chicks[0].actor);
		actorDraw(el->chicks[0].actor);
	}

	if(mapdrawIsSphereVisible(&el->chicks[1].actor->position, el->chicks[1].actor->radius))
	{
//		actorMaintainAnimation(el->chicks[1].actor);
		EXLIGHT_SetupActorLights(el->chicks[1].actor);
		actorDraw(el->chicks[1].actor);
	}

	if(mapdrawIsSphereVisible(&el->chicks[2].actor->position, el->chicks[2].actor->radius))
	{
//		actorMaintainAnimation(el->chicks[2].actor);
		EXLIGHT_SetupActorLights(el->chicks[2].actor);
		actorDraw(el->chicks[2].actor);
	}


//	for(i=0; i<NUM_EL_STATIONS; i++)
//	{
//		for(j=0; j<2; j++)
//		{
//			if(el->eggs[i][j].active)
//			{
//				actorMaintainAnimation(el->eggs[i][j].actor);
//				EXLIGHT_SetupActorLights(el->eggs[i][j].actor);
//				actorDraw(el->eggs[i][j].actor);
//			}
//		}
//	}



	if(el->eggs[0][0].active)
	{
//		actorMaintainAnimation(el->eggs[0][0].actor);
		EXLIGHT_SetupActorLights(el->eggs[0][0].actor);
		actorDraw(el->eggs[0][0].actor);
	}

	if(el->eggs[1][0].active)
	{
//		actorMaintainAnimation(el->eggs[1][0].actor);
		EXLIGHT_SetupActorLights(el->eggs[1][0].actor);
		actorDraw(el->eggs[1][0].actor);
	}

	if(el->eggs[2][0].active)
	{
//		actorMaintainAnimation(el->eggs[2][0].actor);
		if(el->station != 0)
		{
			EXLIGHT_SetupActorLights(el->eggs[2][0].actor);
			actorDraw(el->eggs[2][0].actor);
		}
	}

	if(el->eggs[3][0].active)
	{
//		actorMaintainAnimation(el->eggs[3][0].actor);
		EXLIGHT_SetupActorLights(el->eggs[3][0].actor);
		actorDraw(el->eggs[3][0].actor);
	}


	if(el->eggs[0][1].active)
	{
//		actorMaintainAnimation(el->eggs[0][1].actor);
		EXLIGHT_SetupActorLights(el->eggs[0][1].actor);
		actorDraw(el->eggs[0][1].actor);
	}

	if(el->eggs[1][1].active)
	{
//		actorMaintainAnimation(el->eggs[1][1].actor);
		EXLIGHT_SetupActorLights(el->eggs[1][1].actor);
		actorDraw(el->eggs[1][1].actor);
	}

	if(el->eggs[2][1].active)
	{
//		actorMaintainAnimation(el->eggs[2][1].actor);

		if(el->station != 0)
		{
			EXLIGHT_SetupActorLights(el->eggs[2][1].actor);
			actorDraw(el->eggs[2][1].actor);
		}
	}

	if(el->eggs[3][1].active)
	{
//		actorMaintainAnimation(el->eggs[3][1].actor);
		EXLIGHT_SetupActorLights(el->eggs[3][1].actor);
		actorDraw(el->eggs[3][1].actor);
	}


	if(el->state == EL_RUNNING)
		drawStopWatch(4096 - ((4096*el_time_remaining)/EGGLAY_TIMER));
	else
		drawStopWatch(0);

	
	switch(el->state)
	{
		case EL_FADEIN:
			i= 256 - (el->frameCount)*10;
			if(i <= 0)
			{
				if((elActFrom == 4)&&(currentInv->empty_slots != 6))
				{
					el->state = EL_INTRO_BUGGER_OFF;
				}
				else
					el->state = EL_INTRO;
			}
			else
				deathScreenAdd(i, i, i, SEMITRANS_SUB, 2);

			break;

		case EL_INTRO_BUGGER_OFF:
			elDrawIntroBuggerOff();
			break;

		case EL_INTRO:
			elDrawIntro();
			break;

		case EL_RUNNING:

	  		gte_SetRotMatrix(&GsWSMATRIX);
	  		gte_SetTransMatrix(&GsWSMATRIX);
			for(i=0; i<NUM_EL_CHICKS; i++)
			{
				
				if((el->chicks[i].grainState == EL_GRAIN_ACTIVE) || (el->chicks[i].grainState == EL_GRAIN_STOPPING))
				{
					#ifndef _WINDOWS		
							RECT clip;
					#else
							RECTPS clip;
					#endif


					// CPW: put it back
					DR_AREA *da;

			//		da = BEGINPRIM(da, DR_AREA);
			 //		SetDrawArea(da, &currentDisplayPage->drawenv.clip);
			  //		ENDPRIM(da, EL_CLIP_DEPTH, DR_AREA);
	
					
					for(j=0; j<2; j++)
					{
						if(el->chicks[i].grainActive[j])
						{	
							VERT vect;
							VECTOR v;
							long m;

							// move grain towards camera
							v.vx =  mycamera.vpx - el->chicks[i].actor->position.vx;
							v.vy =  mycamera.vpy - el->chicks[i].actor->position.vy;
							v.vz =  mycamera.vpz - el->chicks[i].actor->position.vz;

							m = Magnitude(&v);
							SCALEVECTOR(&v, (4096*EL_GRAIN_MOVE)/m);
							ADDTOVECTOR(&v, &el->chicks[i].actor->position);

							vect.vx = v.vx - mycamera.vpx;
							vect.vy = v.vy - mycamera.vpy - el->chicks[i].grainPos[j];//el_seedHeight[i];
							vect.vz = v.vz - mycamera.vpz;

							PrintFixedDepth3DSprite(el->grain, &vect, EL_SEED_SCALE, 0x808080, EL_CLIP_DEPTH);

						}
					}

					clip.x = currentDisplayPage->drawenv.clip.x;// + 256 + CONTWEEDY_CLIP_XPOS;
					clip.y = currentDisplayPage->drawenv.clip.y;// + 128 + CONTWEEDY_CLIP_YPOS;
					clip.w = currentDisplayPage->drawenv.clip.w;
   					clip.h = el_clip_size[el_currentchute];
				
					// Set clip area
   			  //		da = BEGINPRIM(da, DR_AREA);
   			  //		SetDrawArea(da, &clip);
   			   //		ENDPRIM(da, EL_CLIP_DEPTH, DR_AREA);


				}
			}



//			if(el_time_remaining > 0)
//				nmeDrawHourglass(EL_HGLASS_X, EL_HGLASS_Y, 20 - 20*el_time_remaining/EGGLAY_TIMER, 1);
//			else
//				nmeDrawHourglass(EL_HGLASS_X, EL_HGLASS_Y, 20 , 0);

			elDrawPanel();
			
			DrawDirectionArrows(el->arrow, EL_ARROW_Y);

			if(el_message_timer > 0)
				fontPrintCentred(font, 0, 0, el_message, 128, 128, 128);
			break;

		case EL_BRONZE_SUCCESS:
			fowlerDraw();
			elDrawBronzeSuccess();
			break;
		case EL_SILVER_SUCCESS:
			fowlerDraw();
			elDrawSilverSuccess();
			break;
		case EL_GOLD_SUCCESS:
			fowlerDraw();
			elDrawGoldSuccess();
			break;
		case EL_FAILED:
			fowlerDraw();
			elDrawFail();
			break;
		case EL_FADEOUT:
			i= (el->frameCount)*20;
			if(i > 220)
				deathScreenAdd(255, 255, 255, SEMITRANS_SUB, 0);

			if(i >=255)
			{
				elEnd();
				return;
			}
			else
				deathScreenAdd(i, i, i, SEMITRANS_SUB, 0);

			break;



	}

}


// ====================================================================================================
void elCycle()
{
	int i, j;

	el->frameCount++;

	constructDebugMoveActor();

	EXLIGHT_Update();
	dsEvent_Update();


	switch(el->state)
	{
		case EL_FADEIN:
			break;
		case EL_INTRO:
			if(el->frameCount > 10)
			{
				if(pReadPad->debounce[0] & PAD_CROSS)
					el->state = EL_RUNNING;
				else if(pReadPad->debounce[0] & PAD_TRIANGLE)
				{
					el->state = EL_FADEOUT;
					el->frameCount = 1;
					subGameRestart = FALSE;
				}
			}
			break;


		case EL_INTRO_BUGGER_OFF:
			if((el->frameCount > 10) && (pReadPad->debounce[0] & PAD_CROSS))
			{
				el->state = EL_FADEOUT;
				el->frameCount = 1;
				subGameRestart = FALSE;
			}
			break;


		case EL_RUNNING:
			elEggUpdate();

			elCamera();
			//if(el_time_remaining && (!el->ending))
			elPanelCycle();
			if(el->ending)
			{
				//DB("here %d\n",	el_message_timer);

				
				// wait for eggs to finish before counting down end text
				if(el->eggsFinished == FALSE)
				{
					int egg;

					el->eggsFinished = TRUE;

					for(egg = 0; egg < EGGLAY_EGGS; egg++)
					{
						// if any of the eggs haven't finished don't do it
						if(el_eggpos[egg] != -99999)
						{
							//DB("el_eggpos[%d] = %d", egg, el_eggpos[egg]);
							el->eggsFinished = FALSE;
							break;
						}
					}
				}
				else
					el_message_timer--;

				if((el_message_timer <= 0))
				{

					if(el_gotcount < NUM_EGGS_BRONZE)
					{
						el->state = EL_FAILED;
						startFowler(FOWLER_FAIL_OFFSET, 100);
					}
					else
					{
						if(gameCalledFrom != GAMECALLEDFROM_DEBUG)
						{
							// Doesn't matter from a level-progression point of view, but anyway...
							gameSaveData.levels_available[REPLAY_AVAILABLE_EGGLAYING] = REPLAY_BEATEN;
						}

						if(el_gotcount >= NUM_EGGS_GOLD)
						{
							el->state = EL_GOLD_SUCCESS;
							startFowler(FOWLER_GOLD_OFFSET, 100);
						}
						else if(el_gotcount >= NUM_EGGS_SILVER)
						{
							el->state = EL_SILVER_SUCCESS;
							startFowler(FOWLER_SILVER_OFFSET, 100);
						}
						else if(el_gotcount >= NUM_EGGS_BRONZE)
						{
							el->state = EL_BRONZE_SUCCESS;
							startFowler(FOWLER_BRONZE_OFFSET, 100);
						}
					}
					el->frameCount = 1;
				}
			}
			else if(el_message_timer > 0)
				el_message_timer--;

			break;

		case EL_BRONZE_SUCCESS:
		case EL_SILVER_SUCCESS:
		case EL_GOLD_SUCCESS:
		case EL_FAILED:

			if(el->frameCount > 10)
			{
				if(pReadPad->debounce[0] & PAD_CROSS)
				{
					el->frameCount = 1;
					el->state = EL_FADEOUT;
					subGameRestart = TRUE;
				}
				else if(pReadPad->debounce[0] & PAD_TRIANGLE)
				{
					el->frameCount = 1;
					el->state = EL_FADEOUT;
					subGameRestart = FALSE;
				}
			}
			break;
		case EL_FADEOUT:
			break;
	}

	actorMaintainAnimation(el->chicks[0].actor);
	actorMaintainAnimation(el->chicks[1].actor);
	actorMaintainAnimation(el->chicks[2].actor);

	for(i=0; i<NUM_EL_STATIONS; i++)
	{
		for(j=0; j<2; j++)
		{
			if(el->eggs[i][j].active)
			{
				actorMaintainAnimation(el->eggs[i][j].actor);
			}
		}
	}


}



// ====================================================================================================
/*
void elEggDraw()
{
	int i;

	for(i=0; i<NUM_EL_EGGS; i++)
	{
		if(el->eggs[i][0].active)
		{
			actorMaintainAnimation(el->eggs[i][0].actor);
			EXLIGHT_SetupActorLights(el->eggs[i][0].actor);
			actorDraw(el->eggs[i][0].actor);
		}
		if(el->eggs[i][1].active)
		{
			actorMaintainAnimation(el->eggs[i][1].actor);
			EXLIGHT_SetupActorLights(el->eggs[i][1].actor);
			actorDraw(el->eggs[i][1].actor);
		}
	}
}
*/

// allows the basket to catch an egg before it reaches the bottom
int EGG_CATCH_MARGIN = 4000;
int EGG_FALL_MARGIN  = 60000;

void elEggUpdate()
{
	int i;

	for(i=0; i<EGGLAY_CHUTES; i++)
	{
		if((el->eggs[i][0].active) && (el->eggs[i][0].actor->animation.endOfCurrentAnim))
		{
			el->eggs[i][0].active = FALSE;
			//DB("Making el->eggs[%d][0].active = 0\n", i);
		}
		if((el->eggs[i][1].active) && (el->eggs[i][1].actor->animation.endOfCurrentAnim))
		{
			el->eggs[i][1].active = FALSE;
			//DB("Making el->eggs[%d][1].active = 0\n", i);
		}

	}
}





int EL_SPLAT_TIME = 16;
int EL_TIME_TO_DRAW_LINE = 5;
int EGG_FALL_DIST  = 100000;
int EGG_CATCH_DIST = 32000;


// ====================================================================================================
void elPanelCycle()
{
	int i,j;
	int chicken;
	int chute;
	int egg;


//	if(elDrawLine)
//	{
//		elDrawLine += 4100/EL_TIME_TO_DRAW_LINE;
// 
//		if(elDrawLine >= 4096)
//		{
//			elDrawLine = 0;
//			elLineCount++;
//		}
//	}

	el_currentchute = el->station;


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


	for(chicken = 0; chicken < EGGLAY_CHICKENS; chicken++)
	{

		
		if(el_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
			el_ready[chicken] -= el_strainspeed[chicken];
			if(el_ready[chicken]<= 0)
			{

//				soundPlaySample(el->pSamples[SFX_EGGLAYING_LAY_SQUELCH], 255, 0);

				soundPlaySamplePanFade(el->pSamples[SFX_EGGLAYING_LAY_SQUELCH], 255, 0, &el->chicks[chicken].actor->position);
				for(chute = 0; chute < EGGLAY_CHUTES; chute++)
				{
					if(el_chickens[chute] == chicken)
					{
						int egg;
						for(egg = 0; egg < EGGLAY_EGGS; egg++)
						{
							if(el_eggpos[egg] == -99999)
							{
								el_eggpos[egg] = EGGLAY_MAXIMUM;
								el_chute[egg] = chute;

//								elEggStartRolling(chute);
								break;
							}

						}

						if(!el->eggs[chute][0].active)
						{
							dsEvent_Add(0, 0, 1, 120,0,9, 0);

							actorAnimate(el->eggs[chute][0].actor, 0, FALSE, FALSE, eggAnimSpeeds[chute], 0, 0);
							el->eggs[chute][0].active = TRUE;
							//DB("animating & making el->eggs[%d][0].active = TRUE\n", chute);
						}
						else if(!el->eggs[chute][1].active)
						{
							dsEvent_Add(0, 0, 1, 120,0,9, 0);

							actorAnimate(el->eggs[chute][1].actor, 0, FALSE, FALSE, eggAnimSpeeds[chute], 0, 0);
							el->eggs[chute][1].active = TRUE;
							//DB("animating & making el->eggs[%d][1].active = TRUE\n", chute);
						}
						//DB("calling animation\n");
					}
				}
				el_chicken_doing[chicken] = EGGLAY_CHICKEN_RECOVERING;

				actorAnimate(el->chicks[chicken].actor, EGGLAYER_RELIEF, FALSE, FALSE, 256, 0, 1024);
				actorAnimate(el->chicks[chicken].actor, EGGLAYER_IDLE, TRUE, TRUE, 256, 0, 0);

				el_ready[chicken] = 0;
			}
			else if( (el_ready[chicken] < (EGGLAY_MAXIMUM/3)) && (el_ready[chicken] > ((EGGLAY_MAXIMUM/3) - el_strainspeed[chicken])) ) 
				soundPlaySamplePanFade(el->pSamples[SFX_EGGLAYING_LAY_FART], 255, 0, &el->chicks[chicken].actor->position);


		}
		else if(el_chicken_doing[chicken] == EGGLAY_CHICKEN_RECOVERING)
		{

			if(el_ready[chicken] == 0)
				soundPlaySamplePanFade(el->pSamples[SFX_EGGLAYING_TIRED], 200, 0, &el->chicks[chicken].actor->position);


			el_ready[chicken] += el_recoverspeed[chicken];
			if(el_ready[chicken] >= EGGLAY_MAXIMUM)
			{
				el_chicken_doing[chicken] = EGGLAY_CHICKEN_HUNGRY;
				el_ready[chicken] = 0;
				actorAnimate(el->chicks[chicken].actor, EGGLAYER_FEEDME, TRUE, FALSE, 200, 0, 1024);
			}

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

// Is this the chicken we've got selected?
			if(chicken == el_chickens[el_currentchute]
				&& (padData.debounce[0] & (PAD_CIRCLE))
				&& (el_doing != EGGLAY_BOWL_READY)
				&& (el_time_remaining>0)
				)
			{
				el_ready[chicken] += el_readyspeed[chicken];

				el_chickLastTimeFed[chicken] = 6;

				if(el->chicks[chicken].actor->animation.animData->segment != EGGLAYER_EATING)
				{
					actorAnimate(el->chicks[chicken].actor, EGGLAYER_EATING, TRUE, FALSE, 256, 0, 0);
					//DB("anim eating\n");
				}

				if(el->chicks[chicken].grainState != EL_GRAIN_ACTIVE)
					el->chicks[chicken].grainState = EL_GRAIN_STARTING;


	// If we've fed a chicken up to the top, it starts straining to lay,
	// during which time, it can't be fed
				if(el_ready[chicken] >= EGGLAY_MAXIMUM)
				{
					el_chicken_doing[chicken] = EGGLAY_CHICKEN_STRAINING;

					soundPlaySamplePanFade(el->pSamples[SFX_EGGLAYING_LAY], 255, 0, &el->chicks[chicken].actor->position);
	
					el_ready[chicken] = EGGLAY_MAXIMUM;	// ...and start to count down

					actorAnimate(el->chicks[chicken].actor, EGGLAYER_SWALLOW, FALSE, FALSE, 500, 0, 1024);
					actorAnimate(el->chicks[chicken].actor, EGGLAYER_LAY, FALSE, TRUE, 500, 0, 0);
					actorAnimate(el->chicks[chicken].actor, EGGLAYER_IDLE, TRUE, TRUE, 256, 0, 0);
					//DB("anim swal lay idle\n");

				}
			}
			else
			{
	// the chicken gets hungrier if you leave it alone
				
				// basically if you feed a chicken it doesn't go back to hungry anim for a number of frames (5)
				if(el_chickLastTimeFed[chicken] > 0)
					el_chickLastTimeFed[chicken]--;
				else if((el->chicks[chicken].actor->animation.animData->segment != EGGLAYER_FEEDME)&&(!el->ending))	//this to stop feedme animation being called at end - seems to work;
				{
					actorAnimate(el->chicks[chicken].actor, EGGLAYER_FEEDME, TRUE, FALSE, 256, 0, 1024);
					el->chicks[chicken].grainState = EL_GRAIN_STOPPING;//winding down
					//DB("anim feedme\n");
				}



				el_ready[chicken] -= el_starvespeed[chicken];
				if(el_ready[chicken] < 0)
				{
					el_ready[chicken] = 0;
				}
			}

		}



		// Update the grain..
		switch(el->chicks[chicken].grainState)
		{
			case EL_GRAIN_STARTING:

				el->chicks[chicken].grainPos[0] = el->chicks[chicken].grainPos[1] = el_seedStartHeight[chicken];
				el->chicks[chicken].grainActive[0] = TRUE;
				el->chicks[chicken].grainActive[1] = FALSE;
				el->chicks[chicken].grainState = EL_GRAIN_ACTIVE;
				//pass on through to next state
		

			case EL_GRAIN_ACTIVE:

				


				if(el_chicken_doing[chicken] != EGGLAY_CHICKEN_HUNGRY)
					el->chicks[chicken].grainState = EL_GRAIN_STOPPING;


				if(el->chicks[chicken].grainActive[0])
				{
					el->chicks[chicken].grainPos[0] -= EL_SEED_SPEED;
					if((el->chicks[chicken].grainPos[0] <= el_seedHeight[chicken]) && (el->chicks[chicken].grainActive[1] == FALSE))
					{
						el->chicks[chicken].grainPos[1] = el_seedStartHeight[chicken];
						el->chicks[chicken].grainActive[1] = TRUE;
					}
					if(el->chicks[chicken].grainPos[0] <= el_seedEndHeight[chicken])
						el->chicks[chicken].grainActive[0] = FALSE;
						
				}
				if(el->chicks[chicken].grainActive[1])
				{
					el->chicks[chicken].grainPos[1] -= EL_SEED_SPEED;
					if((el->chicks[chicken].grainPos[1] <= el_seedHeight[chicken])&&(el->chicks[chicken].grainActive[0] == FALSE))
					{
						el->chicks[chicken].grainPos[0] = el_seedStartHeight[chicken];
						el->chicks[chicken].grainActive[0] = TRUE;
					}
					if(el->chicks[chicken].grainPos[1] <= el_seedEndHeight[chicken])
						el->chicks[chicken].grainActive[1] = FALSE;

				}
				break;

			case EL_GRAIN_STOPPING:
				el->chicks[chicken].grainPos[0] -= EL_SEED_SPEED;
				el->chicks[chicken].grainPos[1] -= EL_SEED_SPEED;

				if(el->chicks[chicken].grainPos[0] <= el_seedEndHeight[chicken])
					el->chicks[chicken].grainActive[0] = FALSE;
				if(el->chicks[chicken].grainPos[1] <= el_seedEndHeight[chicken])
					el->chicks[chicken].grainActive[1] = FALSE;

				if((el->chicks[chicken].grainActive[0] == FALSE) && (el->chicks[chicken].grainActive[1] == FALSE))
					el->chicks[chicken].state = EL_GRAIN_STOPPED;
				break;

			case EL_GRAIN_STOPPED:
				break;
		}

	}

	for(egg = 0; egg < EGGLAY_EGGS; egg++)
	{
		if(el_eggpos[egg] != -99999)
		{
			if(el_eggpos[egg] < EGG_FALL_MARGIN)
				el_eggpos[egg] -= el_eggfallspeed;
			else
				el_eggpos[egg] -= el_eggspeeds[el_chute[egg]];

			// goes faster at end
			if(el_time_remaining <= 0)
			{
				el_eggpos[egg] -= el_eggfallspeed;
				// find out what chute and speed up animation also...
				// speed already doubled (done below)
			}




			
			if(el_eggpos[egg] < EGG_CATCH_MARGIN)
			{

//				if(el_time_remaining )
//				{
	// Egg falls off the bottom of the chute
	// ? Is the bowl there ?
					if((el_currentchute == el_chute[egg]) && (el_doing == EGGLAY_BOWL_READY)&& (el_eggpos[egg] > -EGG_CATCH_DIST))
					{
	// we get an egg.
						dsEvent_Add(0, 0, 1, 170,0,4, 0);
						el_gotcount++;
	
						if(el_time_remaining > 0)
						{									 
							sprintf(el_message,(char*)TEXTSTR(TEXTSTR_SUB_EGGLAY_GOT));
							el_message_timer = 30;
						}

						el_eggpos[egg] = -99999;
						
//						if(elDrawLine > 0)
//						{
//							elLineCount++;
//						}
//						elDrawLine = 4100/EL_TIME_TO_DRAW_LINE;

					}
					else if(el_eggpos[egg] < -EGG_FALL_DIST)
					{
	// the egg splats
						soundPlaySample(el->pSamples[SFX_EGGLAYING_SPLAT], 200, 0);
						el_lostcount++;
						if(el_time_remaining > 0)
						{							  
							sprintf(el_message,(char*)TEXTSTR(TEXTSTR_SUB_EGGLAY_SPLAT));
							el_message_timer = 30;
						}
						el_splat[el_chute[egg]] = EL_SPLAT_TIME;
						el_eggpos[egg] = -99999;
						dsEvent_Add(1, 1, 1, 180,0,6, 0);


					}
//				}
			}
		}
	}

	el_time_remaining--;

	if(el_time_remaining == 0)
	{
						
		sprintf(el_message,(char*)TEXTSTR(TEXTSTR_SUB_SUBGAME_TIMEUP));
		el_message_timer = 90;
		//DB("el->ending = TRUE;\n");
		el->ending = TRUE;
		el->eggsFinished = FALSE;

		for(i=0; i<NUM_EL_CHICKS; i++)
		{
			actorAnimate(el->chicks[i].actor, EGGLAYER_RELIEF, FALSE, FALSE, 256, 0, 1024);
			actorAnimate(el->chicks[i].actor, EGGLAYER_BREATH, TRUE, TRUE, 256, 0, 0);
			el_chicken_doing[i] = EGGLAY_CHICKEN_HUNGRY;
			el_ready[i] = 0;

			if(el->chicks[i].grainState != EL_GRAIN_STOPPED)
				el->chicks[i].grainState = EL_GRAIN_STOPPING;

		}

		//here double all the animspeeds of active 3d eggs
		for(i=0; i<NUM_EL_EGGS; i++)
		{
			for(j=0; j<2; j++)
			{
				if(el->eggs[i][j].active)
				{
					el->eggs[i][j].actor->animation.animData->speed = (el->eggs[i][j].actor->animation.animData->speed * 4) ;
				}
			}
		}
	}

}





#define EL_NUM_FRAMES_FOR_CAM_MOVE 10

// ====================================================================================================
void elCamera()
{
	VECTOR look = {0, 0, 4096};
	int xRot, yRot, i, j;
	
//	replaced EL_NUM_FRAMES_FOR_CAM_MOVE with a el member value so that the number of frames can be varied between different stations
	
	if(el->cameraMoving)
	{
		el->cameraMovingCount++;

///		el->cameraXRot = ((el->cameraMovingCount*elCamXRot[el->station]) + ((EL_NUM_FRAMES_FOR_CAM_MOVE -el->cameraMovingCount)*elCamXRot[el->lastStation]))/EL_NUM_FRAMES_FOR_CAM_MOVE;
///		el->cameraYRot = ((el->cameraMovingCount*el->cameraTargetYRot) + ((EL_NUM_FRAMES_FOR_CAM_MOVE -el->cameraMovingCount)*elCamYRot[el->lastStation]))/EL_NUM_FRAMES_FOR_CAM_MOVE;

		el->cameraXRot = ((el->cameraMovingCount*elCamXRot[el->station]) + ((el->framesForCamMove -el->cameraMovingCount)*el->startCameraXRot))/el->framesForCamMove;
		el->cameraYRot = ((el->cameraMovingCount*el->cameraTargetYRot) + ((el->framesForCamMove -el->cameraMovingCount)*el->startCameraYRot))/el->framesForCamMove;


		//DB("xRot = %d, yRot = %d\n",xRot, yRot );

		RotateVector2DXYZ(&look, &look, el->cameraXRot, 0);
		RotateVector2DXYZ(&look, &look, el->cameraYRot, 1);


		mycamera.vpx = ((el->cameraMovingCount*elCamPos[el->station].vx) + ((el->framesForCamMove -el->cameraMovingCount)*elCamPos[el->lastStation].vx))/el->framesForCamMove;
 		mycamera.vpy = ((el->cameraMovingCount*elCamPos[el->station].vy) + ((el->framesForCamMove -el->cameraMovingCount)*elCamPos[el->lastStation].vy))/el->framesForCamMove;
		mycamera.vpz = ((el->cameraMovingCount*elCamPos[el->station].vz) + ((el->framesForCamMove -el->cameraMovingCount)*elCamPos[el->lastStation].vz))/el->framesForCamMove;

		mycamera.vrx = mycamera.vpx + look.vx;
		mycamera.vry = mycamera.vpx + look.vy;
		mycamera.vrz = mycamera.vpx + look.vz;


		// update camera
		if (el->cameraMovingCount >= el->framesForCamMove) 
			el->cameraMoving = FALSE;

	}
	//else
	{
		//debug

#ifdef EL_DEBUG_CAMERA

		if((pReadPad->digital[0] & PAD_R1) && (!(pReadPad->digital[0] & PAD_L1)))

		{

			if(pReadPad->digital[0] & PAD_LEFT)
				elCamYRot[el->station] += 4;
			if(pReadPad->digital[0] & PAD_RIGHT)
				elCamYRot[el->station] -= 4;

			if(pReadPad->digital[0] & PAD_UP)
				elCamXRot[el->station] -= 4;
			if(pReadPad->digital[0] & PAD_DOWN)
				elCamXRot[el->station] += 4;



			RotateVector2DXYZ(&look, &look, elCamXRot[el->station], 0);
			RotateVector2DXYZ(&look, &look, elCamYRot[el->station], 1);

			mycamera.vrx = mycamera.vpx + look.vx;
			mycamera.vry = mycamera.vpy + look.vy;
			mycamera.vrz = mycamera.vpz + look.vz;
		}

		else if((pReadPad->digital[0] & PAD_R2)&& (!(pReadPad->digital[0] & PAD_L1)))
		{
			int dx=0, dy=0, dz=0;



			if(pReadPad->digital[0] & PAD_LEFT)
				dx = 4;
			if(pReadPad->digital[0] & PAD_RIGHT)
				dx = -4;

			if(pReadPad->digital[0] & PAD_UP)
				dz = -4;
			if(pReadPad->digital[0] & PAD_DOWN)
				dz = 4;
		
			if(pReadPad->digital[0] & PAD_TRIANGLE)
				dy = -4;

			if(pReadPad->digital[0] & PAD_SQUARE)
				dy = 4;

			mycamera.vrx += dx;
			mycamera.vry += dy;
			mycamera.vrz += dz;

			mycamera.vpx +=	dx;
			mycamera.vpy +=	dy;
			mycamera.vpz +=	dz;

			elCamPos[el->station].vx += dx;
			elCamPos[el->station].vy += dy;
			elCamPos[el->station].vz += dz;

		}
		else if((!(pReadPad->digital[0] & PAD_L1)))// && (!(pReadPad->digital[0] & PAD_R1)))
		{
		
#endif

		if(((pReadPad->analogXS[0] > 400)&&((pReadPad->digital[0] & PAD_RIGHT)==0)&&((!el->cameraMoving)||(el->cameraMovingCount >4) )) || (pReadPad->debounce[0] & (PAD_R1 | PAD_R2 | PAD_RIGHT)))
		{
			
			// this is to enable switching stations before the camera has even arrived
			if(el->cameraMoving)
			{
				el->startCameraXRot = el->cameraXRot;
				el->startCameraYRot = el->cameraYRot;
				el->framesForCamMove = EL_NUM_FRAMES_FOR_CAM_MOVE;
			}
			else
			{
				el->startCameraXRot = elCamXRot[el->station];
				el->startCameraYRot = elCamYRot[el->station];

				if(el->station == 1)
					el->framesForCamMove = EL_NUM_FRAMES_FOR_CAM_MOVE/2;
				else
					el->framesForCamMove = EL_NUM_FRAMES_FOR_CAM_MOVE;
			}
			
			
			el->lastStation = el->station;
			el->station++;


			if(el->station >= NUM_EL_STATIONS)
				el->station = 0;


			// find out which direction we're going in!
			// this change is to enable switching stations before the camera has even arrived
			if(el->startCameraYRot > elCamYRot[el->station])
			{
				i = el->startCameraYRot - elCamYRot[el->station];
				j = 4096 + elCamYRot[el->station] - elCamYRot[el->lastStation];

				if(j < i)
					el->cameraTargetYRot = elCamYRot[el->station] + 4096;
				else
					el->cameraTargetYRot = elCamYRot[el->station];
			}
			else
			{
				i =  elCamYRot[el->station] - el->startCameraYRot;
				j = 4096 + el->startCameraYRot - elCamYRot[el->station];

				if(j < i)
					el->cameraTargetYRot = elCamYRot[el->station] - 4096;
				else
					el->cameraTargetYRot = elCamYRot[el->station];
			}



		//	DB("targetYrot = %d\n", el->cameraTargetYRot);
		//	DB("lastRot = %d\n\n", elCamYRot[el->lastStation]);
			el->cameraMovingCount = 0;
			el->cameraMoving = TRUE;
		}
		else if(((pReadPad->analogXS[0] < -400)&&((pReadPad->digital[0] & PAD_LEFT)==0)&&((!el->cameraMoving)||(el->cameraMovingCount >4) )) || (pReadPad->debounce[0] & (PAD_L1 | PAD_L2 | PAD_LEFT)))
		{

			// this change is to enable switching stations before the camera has even arrived
			if(el->cameraMoving)
			{
				el->startCameraXRot = el->cameraXRot;
				el->startCameraYRot = el->cameraYRot;
				el->framesForCamMove = EL_NUM_FRAMES_FOR_CAM_MOVE;
			}
			else
			{
				el->startCameraXRot = elCamXRot[el->station];
				el->startCameraYRot = elCamYRot[el->station];

				if(el->station == 2)
					el->framesForCamMove = EL_NUM_FRAMES_FOR_CAM_MOVE/2;
				else
					el->framesForCamMove = EL_NUM_FRAMES_FOR_CAM_MOVE;

			}

			
			el->lastStation = el->station;
			el->station--;

			if(el->station < 0)
				el->station = NUM_EL_STATIONS-1;


			// find out which direction we're going in!
			// (this change is to enable switching stations before the camera has even arrived)
			if(el->startCameraYRot > elCamYRot[el->station])
			{
				i = el->startCameraYRot - elCamYRot[el->station];
				j = 4096 + elCamYRot[el->station] - el->startCameraYRot;

				if(j < i)
					el->cameraTargetYRot = elCamYRot[el->station] + 4096;
				else
					el->cameraTargetYRot = elCamYRot[el->station];

//				DB("\n1: el->cameraTargetYRot=%d\n", el->cameraTargetYRot);
//				DB("1: el->startCameraYRot=%d\n", el->startCameraYRot);
			}
			else
			{
				i =  elCamYRot[el->station] - el->startCameraYRot;
				j = 4096 + el->startCameraYRot - elCamYRot[el->station];

				if(j < i)
					el->cameraTargetYRot = elCamYRot[el->station] - 4096;
				else
					el->cameraTargetYRot = elCamYRot[el->station];
//				DB("\n2: el->cameraTargetYRot=%d\n", el->cameraTargetYRot);
//				DB("2: el->startCameraYRot=%d\n", el->startCameraYRot);


			}


			el->cameraMovingCount = 0;
			el->cameraMoving = TRUE;
		}

		myactor = el->chicks[elChicks[el->station]].actor;//el->workers[el->station].actor;


#ifdef EL_DEBUG_CAMERA
		}
#endif

	
	}
}



void elInit()
{
	int i;

	for(i = 0; i < EGGLAY_EGGS; i++)
	{
		el_eggpos[i] = -99999;
		el_eggtex[i] = 0;
	}
	for(i = 0; i < EGGLAY_CHICKENS; i++)
	{
		el_ready[i] = 0;
		el_chicken_doing[i] = EGGLAY_CHICKEN_HUNGRY;
	}

	for(i = 0; i < EGGLAY_CHUTES; i++)
	{
		el_splat[i] = 0;
	}


	el_currentchute = 0;
	el_gotcount = 0;
	el_lostcount = 0;
	el_message_timer = 0;
	el_doing = EGGLAY_NOTHING;

	el_time_remaining = EGGLAY_TIMER;

}

#define EL_PANEL_X  -220
#define EL_PANEL_Y  0
#define EL_PANEL_CH1_DX  -5
#define EL_PANEL_CH1_DY  -38
#define EL_PANEL_CH2_DX  23
#define EL_PANEL_CH2_DY  -43
#define EL_PANEL_CH3_DX  66
#define EL_PANEL_CH3_DY  -38

#define EL_PANEL_EGG_DX		4
#define EL_PANEL_EGG_XSEP	24
#define EL_PANEL_EGG_DY 	0
#define EL_PANEL_EGG_H		80

#define EL_PANEL_CLIP_CHICK1_TOP	1
#define EL_PANEL_CLIP_CHICK2_TOP	2
#define EL_PANEL_CLIP_CHICK3_TOP	1
#define EL_PANEL_CLIP_CHICK_BOTTOM  4

#define EL_PANEL_CLIP_CHICK_LEFT	0
#define EL_PANEL_CLIP_CHICK_RIGHT	50


#define EL_PANEL_BASK_X		 1
#define EL_PANEL_BASK_Y		 82
#define EL_PANEL_BASK_XSEP	 24
#define EL_PANEL_BASKON_X	 1
#define EL_PANEL_BASKON_Y	 82

#define EL_PANEL_PIPE_X  1
#define EL_PANEL_PIPE_Y  0

#define EL_SPLAT_X		 -4
#define EL_SPLAT_XSEP	 24
#define EL_SPLAT_Y		 108




// ====================================================================================================
void elDrawPanel()
{
	int i, t, b;


	drawCricketScores(EL_CRICKET_X, EL_CRICKET_Y, el_gotcount);

	overlaySprite(el->panel.pipes, EL_PANEL_X, EL_PANEL_Y, 6, 0x808080);
	
	if(elChicks[el_currentchute] == 0)
		overlaySprite(el->panel.chickSel[0], EL_PANEL_CH1_DX + EL_PANEL_X, EL_PANEL_CH1_DY + EL_PANEL_Y, 4, 0x808080);
	else
		overlaySprite(el->panel.chick[0], EL_PANEL_CH1_DX + EL_PANEL_X, EL_PANEL_CH1_DY + EL_PANEL_Y, 4, 0x808080);

	if(elChicks[el_currentchute] == 1)
		overlaySprite(el->panel.chickSel[1], EL_PANEL_CH2_DX + EL_PANEL_X, EL_PANEL_CH2_DY + EL_PANEL_Y, 3, 0x808080);
	else
		overlaySprite(el->panel.chick[1], EL_PANEL_CH2_DX + EL_PANEL_X, EL_PANEL_CH2_DY + EL_PANEL_Y, 3, 0x808080);

	if(elChicks[el_currentchute] == 2)
		overlaySprite(el->panel.chickSel[2], EL_PANEL_CH3_DX + EL_PANEL_X, EL_PANEL_CH3_DY + EL_PANEL_Y, 4, 0x808080);
	else
		overlaySprite(el->panel.chick[2], EL_PANEL_CH3_DX + EL_PANEL_X, EL_PANEL_CH3_DY + EL_PANEL_Y, 4, 0x808080);


	for(i = 0; i < EGGLAY_EGGS; i++)
	{
		if(el_eggpos[i] != -99999)
		{

			if((el->frameCount % 3) == 0)
				el_eggtex[i]++;
			if(el_eggtex[i] >= EL_NUM_EGG_TEXTURES)
				el_eggtex[i] = 0;
			

			if(el_eggpos[i] < -EGG_CATCH_DIST)
				//if it's gonna smash draw itt closer so it doesn't go through the basket
				overlaySprite(el->panel.egg[el_eggtex[i]], EL_PANEL_X + EL_PANEL_EGG_DX + EL_PANEL_EGG_XSEP * el_chute[i],
						   EL_PANEL_Y + EL_PANEL_EGG_DY + EL_PANEL_EGG_H - (el_eggpos[i] * EL_PANEL_EGG_H)/EGGLAY_MAXIMUM, 1, 0x808080);
			else
				overlaySprite(el->panel.egg[el_eggtex[i]], EL_PANEL_X + EL_PANEL_EGG_DX + EL_PANEL_EGG_XSEP * el_chute[i],
						   EL_PANEL_Y + EL_PANEL_EGG_DY + EL_PANEL_EGG_H - (el_eggpos[i] * EL_PANEL_EGG_H)/EGGLAY_MAXIMUM, 5, 0x808080);

		}
	}


	b = EL_PANEL_Y + EL_PANEL_CLIP_CHICK_BOTTOM;
	t = b -( (EL_PANEL_CLIP_CHICK_BOTTOM - (EL_PANEL_CH1_DY  + EL_PANEL_CLIP_CHICK1_TOP)) * el_ready[0] / EGGLAY_MAXIMUM);

//	if(pReadPad->digital[0] & PAD_R2)
//		DB("b=%d t=%d el_ready[0]=%d\n", b, t, el_ready[0]);


	if(el_chicken_doing[0] == EGGLAY_CHICKEN_HUNGRY)
	{
		drawClippedSpriteOverlay(el->panel.fillChick[0], EL_PANEL_CH1_DX + EL_PANEL_X, EL_PANEL_CH1_DY + EL_PANEL_Y, 2, 
			EL_PANEL_CH1_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT, 
			EL_PANEL_CH1_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT + EL_PANEL_CLIP_CHICK_RIGHT,
			t, b);
	}
	else
	{
		drawClippedSpriteOverlay(el->panel.layChick[0], EL_PANEL_CH1_DX + EL_PANEL_X, EL_PANEL_CH1_DY + EL_PANEL_Y, 2, 
			EL_PANEL_CH1_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT, 
			EL_PANEL_CH1_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT + EL_PANEL_CLIP_CHICK_RIGHT,
			t, b);
	}

	t = b -( (EL_PANEL_CLIP_CHICK_BOTTOM - (EL_PANEL_CH2_DY  + EL_PANEL_CLIP_CHICK2_TOP)) * el_ready[1] / EGGLAY_MAXIMUM);
	if(el_chicken_doing[1] == EGGLAY_CHICKEN_HUNGRY)
	{
		drawClippedSpriteOverlay(el->panel.fillChick[1], EL_PANEL_CH2_DX + EL_PANEL_X, EL_PANEL_CH2_DY + EL_PANEL_Y, 2, 
			EL_PANEL_CH2_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT, 
			EL_PANEL_CH2_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT + EL_PANEL_CLIP_CHICK_RIGHT,
			t, b);
	}
	else
	{
		drawClippedSpriteOverlay(el->panel.layChick[1], EL_PANEL_CH2_DX + EL_PANEL_X, EL_PANEL_CH2_DY + EL_PANEL_Y, 2, 
			EL_PANEL_CH2_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT, 
			EL_PANEL_CH2_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT + EL_PANEL_CLIP_CHICK_RIGHT,
			t, b);
	}


	t = b -( (EL_PANEL_CLIP_CHICK_BOTTOM - (EL_PANEL_CH3_DY  + EL_PANEL_CLIP_CHICK3_TOP)) * el_ready[2] / EGGLAY_MAXIMUM);
	if(el_chicken_doing[2] == EGGLAY_CHICKEN_HUNGRY)
	{
		drawClippedSpriteOverlay(el->panel.fillChick[2], EL_PANEL_CH3_DX + EL_PANEL_X, EL_PANEL_CH3_DY + EL_PANEL_Y, 2, 
			EL_PANEL_CH3_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT, 
			EL_PANEL_CH3_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT + EL_PANEL_CLIP_CHICK_RIGHT,
			t, b);
	}
	else
	{
		drawClippedSpriteOverlay(el->panel.layChick[2], EL_PANEL_CH3_DX + EL_PANEL_X, EL_PANEL_CH1_DY + EL_PANEL_Y, 2, 
			EL_PANEL_CH3_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT, 
			EL_PANEL_CH3_DX + EL_PANEL_X + EL_PANEL_CLIP_CHICK_LEFT + EL_PANEL_CLIP_CHICK_RIGHT,
			t, b);
	}


	overlaySprite(el->panel.baskOff, EL_PANEL_X + EL_PANEL_BASK_X + EL_PANEL_BASK_XSEP*el_currentchute, EL_PANEL_BASK_Y + EL_PANEL_Y, 6, 0x808080);
	
	
	if(el_doing == EGGLAY_BOWL_READY)
		overlaySprite(el->panel.baskOn, EL_PANEL_X + EL_PANEL_BASKON_X + EL_PANEL_BASK_XSEP*el_currentchute, EL_PANEL_BASKON_Y + EL_PANEL_Y, 2, 0x808080);

	//selected pipe
	overlaySprite(el->panel.pipeSel, EL_PANEL_X + EL_PANEL_PIPE_X + EL_PANEL_BASK_XSEP*el_currentchute, EL_PANEL_PIPE_Y + EL_PANEL_Y, 5, 0x707070);



	for(i=0; i<EGGLAY_CHUTES; i++)
	{
		if(el_splat[i] > 2)
		{
			ULONG colour;

			el_splat[i]--;
			if(el_splat[i] < 8)
			{
				
				// lokk at this again - it aint quite right
				colour = 0xffffff &( (el_splat[i]<<4) | (el_splat[i]<<12) | (el_splat[i]<<20));
				overlayTranspSprite(el->panel.splat, EL_PANEL_X + EL_SPLAT_X + EL_SPLAT_XSEP*i, EL_PANEL_Y + EL_SPLAT_Y,1, colour | OV_TRANSSPR_SEMI);
				
			}
			else
			{
				overlaySprite(el->panel.splat, EL_PANEL_X + EL_SPLAT_X + EL_SPLAT_XSEP*i, EL_PANEL_Y + EL_SPLAT_Y, 1, 0x808080);
			}
		}
	}
}


// ====================================================================================================
void elDrawIntro()													
{
	char message[256];

	sprintf(message, "%s\n\n%s",
	(char*)TEXTSTR(TEXTSTR_SUB_EGGLAY_INTRO1) ,
	(char*)TEXTSTR(TEXTSTR_SUB_XSTART)
	);

	fontPrintMultiLineBox(font, 0, 30, 340, message, 60, 80, 80);

}


// ====================================================================================================
void elDrawIntroBuggerOff()
{
	fontPrintMultiLineBox(font, 0, 30, 340,(char*)TEXTSTR(TEXTSTR_SUB_EGG_GOAWAY), 100, 80, 80);
}


// ====================================================================================================
void elDrawFail()
{
	char message[256];

	if(el_gotcount == 0)
	{																	 
		sprintf(message, "%s\n%s\n%s", (char*)TEXTSTR(TEXTSTR_SUB_EGGLAY_NONE), (char*)TEXTSTR(TEXTSTR_SUB_EGG_FAIL), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
		fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, message, 100, 80, 80,2);
	}
	else if(el_gotcount == 1)
	{
		sprintf(message, "%s %s\n%s", (char*)TEXTSTR(TEXTSTR_SUB_EGGLAY_ONE),(char*)TEXTSTR(TEXTSTR_SUB_EGG_FAIL), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
		fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, message, 100, 80, 80,2);
	}
	else
	{
		sprintf(message, "%d %s %s\n%s", el_gotcount, (char*)TEXTSTR(TEXTSTR_SUB_EGGLAY_EGGS), (char*)TEXTSTR(TEXTSTR_SUB_EGG_FAIL), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
		fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, message, 100, 80, 80,2);
	}
}

// ====================================================================================================
void elDrawGoldSuccess()
{
	char message[256];

	sprintf(message, "%d eggs! %s\n%s", el_gotcount, (char*)TEXTSTR(TEXTSTR_SUB_EGG_GOLD), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));

	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, message, 100, 80, 80,2);
}
																				 
// ====================================================================================================
void elDrawSilverSuccess()
{
	char message[256];

	sprintf(message, "%d eggs! %s\n%s", el_gotcount, (char*)TEXTSTR(TEXTSTR_SUB_EGG_SILVER), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, message, 100, 80, 80,2);
}

// ====================================================================================================
void elDrawBronzeSuccess()
{
	char message[256];

	sprintf(message, "%d eggs! %s\n%s", el_gotcount, (char*)TEXTSTR(TEXTSTR_SUB_EGG_BRONZE), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, message, 100, 80, 80,2);
}




void elPrintDebug()
{
	char strBuff[128];


#ifdef EL_VIEW_POS

	if(myactor)
	{
		sprintf(strBuff, "actorPos: %d %d %d rot %d", (int)myactor->position.vx, (int)myactor->position.vy, (int)myactor->position.vz, (int)myactor->psiData.object->rotate.vy);
		fontPrint(font, -140, 90, strBuff, 128, 128, 0);
	}
#endif

#ifdef EL_VIEW_CAMERA
	sprintf(strBuff, "cam pos: %d  %d  %d", (int)mycamera.vpx, (int)mycamera.vpy, (int)mycamera.vpz);
	fontPrint(font, -240, -110, strBuff, 128, 128, 0);
	sprintf(strBuff, "garib  : %d %d %d", (int)mycamera.vrx, (int)mycamera.vry, (int)mycamera.vrz);
	fontPrint(font, -240, -100, strBuff, 128, 128, 0);

	sprintf(strBuff, "station %d camMove %d", el->station, el->cameraMoving);
	fontPrint(font, -240, -90, strBuff, 128, 128, 0);

	

	sprintf(strBuff, "elCamXRot %d elCamYRot %d", elCamXRot[el->station], elCamYRot[el->station]);
	fontPrint(font, -240, -80, strBuff, 128, 128, 0);


//	sprintf(strBuff, "target:%d, last:%d, state:%d", con->cam.targetTool, con->cam.lastTool, con->cam.state);
//	fontPrint(font, -240, -90, strBuff, 128, 128, 0);


#endif

#ifdef EL_VIEW_STATES

	sprintf(strBuff, "el->state = %d", el->state);
	fontPrint(font, -240, -90, strBuff, 128, 128, 0);

	sprintf(strBuff, "timeleft %d", el_time_remaining);
	fontPrint(font, -240, -80, strBuff, 128, 128, 0);

#endif



#ifdef EL_VIEW_EGGS

	sprintf(strBuff, "0)%d 1)%d 2)%d 3)%d  (%d selected) @ST", eggAnimSpeeds[0], eggAnimSpeeds[1], eggAnimSpeeds[2], eggAnimSpeeds[3], el->station);
	fontPrint(font, -240, -90, strBuff, 128, 128, 0);

	if(padData.debounce[0] & PAD_TRIANGLE)
	{
		eggAnimSpeeds[el->station]++;
	}
	if(padData.debounce[0] & PAD_SQUARE)
	{
		eggAnimSpeeds[el->station]--;
	}


#endif


}

