#include "global.h"
#include "main.h"
#include "debris.h"
#include "lights.h"
#include "timer.h"
#include "maths.h"
#include "overlays.h"
#include "fx.h"
#include "powerbar.h"
#include "catapult.h"
#include "seesaw.h"
#include "demo.h"
#include "map_view.h"
#include "ranges.h"
//#include "text_ids.h"
#include "gametext.h"
#include "charactr.h"
#include "deth.h"
#include "enemies.h"
#include "dualshock.h"
#include <psitypes.h>
#include "construct.h"
#include "inventory.h"
#include "menu.h"
#include "maths.h"
#include "loadsnd.h"
#include "sound.h"
#include "overlays.h"
#include "card.h"
#include "sound.h"

#include "map_play.h"
#include "subgame.h"
#include "startup.h"
#include "loadsnd.h"


//#define CON_VIEW_POS
//#define CON_VIEW_STATES


#define CON_SAWER_XPOS	76
#define CON_SAWER_YPOS	-50
#define CON_SAWER_ZPOS	861
#define CON_SAWER_YROT  1080

#define CON_HAMMER_XPOS	 -342
#define CON_HAMMER_YPOS	 240
#define CON_HAMMER_ZPOS	 -2486
#define CON_HAMMER_YROT  1110

#define CON_SPANNER_XPOS  942
#define CON_SPANNER_YPOS  120
#define CON_SPANNER_ZPOS  -1998
#define CON_SPANNER_YROT  3060


#define CON_NUM_RHYTHMS 12
#define CON_MAX_TIMES_PLAYED 4

		
char rhythms[CON_NUM_RHYTHMS][8] = 
{
	{0,1,0,2,0,3,0,1},		//0
	{0,2,0,1,0,3,2,0},		//1
	{0,3,1,0,2,0,1,0},		//2
	{0,2,0,2,2,0,1,1},		//3
	{0,1,2,0,0,2,3,0},		//4
	{0,2,2,0,0,1,2,3},		//5
	{1,1,0,3,0,2,0,3},		//6
	{2,0,1,0,0,3,2,1},		//7
	{0,1,2,3,0,3,2,1},		//8
	{0,3,1,0,2,1,0,2},		//9
	{1,2,0,3,2,0,1,3},		//10
	{3,1,2,1,3,2,1,3},		//11
};




VECTOR	conCamPos[CON_NUM_TOOLS]=
{

	{402, -1765, -1649},		//CON_SAWER 
	{600, -1476, -827},			//CON_HAMMER
	{-860, -1534, -523},		//CON_SPANNER

};

VECTOR	conGaribPos[CON_NUM_TOOLS]=
{
	{130, -213, 1372},			//CON_SAWER 
	{-1378, 222, -3020},		//CON_HAMMER
	{1326, -17, -2936},		//CON_SPANNER

};

int conCamAngle[CON_NUM_TOOLS];



CONSTRUCT_DATA *con;

#define CONSTRUCT_FOV  700

#define CON_CAM_XSTART  0
#define CON_CAM_YSTART  0
#define CON_CAM_ZSTART  0

#define CON_GAR_XSTART  0
#define CON_GAR_YSTART  0
#define CON_GAR_ZSTART  1000

#define CON_PROGRESS_BRONZE_SCORE 140
#define CON_PROGRESS_SILVER_SCORE 188
#define CON_PROGRESS_GOLD_SCORE   234



//ACTOR *myactor = 0;


short conBackRythm[16]=
{
	SFX_CONSTRUCT_HAT1, SFX_CONSTRUCT_HAT2, SFX_CONSTRUCT_HAT2, SFX_CONSTRUCT_HAT3, 
	SFX_CONSTRUCT_KICK, SFX_CONSTRUCT_HAT2, SFX_CONSTRUCT_HAT1, SFX_CONSTRUCT_HAT2,
	SFX_CONSTRUCT_KICK, SFX_CONSTRUCT_HAT2, SFX_CONSTRUCT_HAT3, SFX_CONSTRUCT_KICK,
	SFX_CONSTRUCT_KICK, SFX_CONSTRUCT_HAT3, SFX_CONSTRUCT_HAT2, SFX_CONSTRUCT_HAT2
};



//-----------------------------------------------------------------------------------------------------------------------
void constructStart()
{
	int i, dist, height;
	char string[32];

	subGameRestart = FALSE;


	con = (CONSTRUCT_DATA*)MALLOC(sizeof(CONSTRUCT_DATA));
	memset(con, 0, sizeof(CONSTRUCT_DATA));

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



	draw_routine = constructDraw;
	cycle_routine = constructCycle;

//	fw->countdown = FW_COUNTDOWN_TIME * FRAMESEC;

	pReadPad->shockEnable=1;

	dsEvent_Init();

	// ============
	// Load the world map that we'll be using
	// ============

// RFW	EXLIGHT_Initialise();	// called inside mapviewLoadMap

	loadsndLoadBank("A4_CONSTRUCT");
	mapviewLoadMap("A4_CONSTRUCT");			// Load the map we're going to use
	setupCollision("A4_CONSTRUCT");			// Setup the collision for the level map
	EXLIGHT_PostBFF();


	initFowler(CONSTRUCT_FOV);
	loadsndSubGames(LEVNUMBER_A4_CONSTRUCT, con->pSamples, NUM_CONSTRUCT_SAMPLES);



	con->tools[CON_HAMMER].model = BFF_IsModelLoaded("HAMMER");
	ASSERT(con->tools[CON_HAMMER].model);
	con->tools[CON_HAMMER].actor = actorCreate(con->tools[CON_HAMMER].model);
	ASSERT(con->tools[CON_HAMMER].actor);
	actorInitAnim(con->tools[CON_HAMMER].actor);	

	con->tools[CON_HAMMER].actor->position.vx = CON_HAMMER_XPOS;
	con->tools[CON_HAMMER].actor->position.vy = CON_HAMMER_YPOS;
	con->tools[CON_HAMMER].actor->position.vz = CON_HAMMER_ZPOS;
	con->tools[CON_HAMMER].actor->psiData.object->rotate.vy = CON_HAMMER_YROT;



	con->tools[CON_SPANNER].model = BFF_IsModelLoaded("SPANNER");
	ASSERT(con->tools[CON_SPANNER].model);
	con->tools[CON_SPANNER].actor = actorCreate(con->tools[CON_SPANNER].model);
	ASSERT(con->tools[CON_SPANNER].actor);
	actorInitAnim(con->tools[CON_SPANNER].actor);
	

	con->tools[CON_SPANNER].actor->position.vx =  CON_SPANNER_XPOS;
	con->tools[CON_SPANNER].actor->position.vy =  CON_SPANNER_YPOS;
	con->tools[CON_SPANNER].actor->position.vz =  CON_SPANNER_ZPOS;
	con->tools[CON_SPANNER].actor->psiData.object->rotate.vy = CON_SPANNER_YROT;


	con->tools[CON_SAWER].model = BFF_IsModelLoaded("SAWER");
	ASSERT(con->tools[CON_SAWER].model);
	con->tools[CON_SAWER].actor = actorCreate(con->tools[CON_SAWER].model);
	ASSERT(con->tools[CON_SAWER].actor);
	actorInitAnim(con->tools[CON_SAWER].actor);
	
	con->tools[CON_SAWER].actor->position.vx = CON_SAWER_XPOS;
	con->tools[CON_SAWER].actor->position.vy =	CON_SAWER_YPOS;
	con->tools[CON_SAWER].actor->position.vz =	CON_SAWER_ZPOS;
	con->tools[CON_SAWER].actor->psiData.object->rotate.vy = CON_SAWER_YROT;

	actorAnimate(con->tools[CON_SAWER].actor,CON_BREATH, TRUE, FALSE, 300, 0, 900);
	actorAnimate(con->tools[CON_SPANNER].actor,CON_BREATH, TRUE, FALSE, 300, 0, 900);
	actorAnimate(con->tools[CON_HAMMER].actor,CON_BREATH, TRUE, FALSE, 300, 0, 900);

	con->tools[CON_SAWER].state = CON_TOOL_PREPARE;


	con->tweedy.model = BFF_IsModelLoaded("MRTWEEDYC");
	ASSERT(con->tweedy.model);
	con->tweedy.actor = actorCreate(con->tweedy.model);
	ASSERT(con->tweedy.actor);
	actorInitAnim(con->tweedy.actor);

	actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_SIGH, FALSE, FALSE, 300, 0, 0);
	actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_BREATHE, FALSE, TRUE, 300, 0, 0);
	actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_SCRATCH_HEAD, FALSE, TRUE, 300, 0, 0);

	con->tweedy.state = CON_TWEEDY_STOP_WORKING;

	con->rhythm.state = CON_RHYTHM_STATE_STARTNEW;


	PSImodelctrl.farclip =  1023;
	max_map_ot_depth = 1023;

	// init camera stuff
	con->cam.state = CON_CAMSTATE_ARRIVED;
	con->cam.targetTool = 0;
	con->cam.lastTool = 0;
	con->cam.distBetweenCamPos = 0;

	con->cam.pos = conCamPos[0];
	con->cam.garibPos = conGaribPos[0];


	mycamera.vpx = con->cam.pos.vx;
	mycamera.vpy = con->cam.pos.vy;
	mycamera.vpz = con->cam.pos.vz;

	mycamera.vrx = con->cam.garibPos.vx;
	mycamera.vry = con->cam.garibPos.vy;
	mycamera.vrz = con->cam.garibPos.vz;



	con->tweedy.overlay = textureFindInAllBanks("TELESC");
	ASSERT(con->tweedy.overlay);



	for(i=0; i<CON_NUM_TOOLS; i++)
	{
		dist = Magnitude2D( (conCamPos[i].vx - conGaribPos[i].vx), (conCamPos[i].vz - conGaribPos[i].vz) );
		height =  (conGaribPos[i].vy - conCamPos[i].vy);

		conCamAngle[i] = (4096*dist)/height;
	}


//	con->rhythm.chick[0] = textureFindInAllBanks("CHICK1");
//	ASSERT(con->rhythm.chick[0]);
//	con->rhythm.chick[1] = textureFindInAllBanks("CHICK2");
//	ASSERT(con->rhythm.chick[1]);


// Rhythm things

	con->rhythm.model =	BFF_IsModelLoaded("CHICK");
	ASSERT(con->rhythm.model);
	con->rhythm.actor = actorCreate(con->rhythm.model);
	ASSERT(con->rhythm.actor);
	actorInitAnim(con->rhythm.actor);

	actorAnimate(con->rhythm.actor, 0, TRUE, FALSE, 400, 0, 0);


	con->rhythm.buttons[CON_SQUARE] = textureFindInAllBanks("SQUARE");
	ASSERT(con->rhythm.buttons[CON_SQUARE]);
	con->rhythm.buttons[CON_CROSS] = textureFindInAllBanks("CROSS");
	ASSERT(con->rhythm.buttons[CON_CROSS]);
	con->rhythm.buttons[CON_CIRCLE]	= textureFindInAllBanks("CIRCLE");
	ASSERT(con->rhythm.buttons[CON_CIRCLE]);

	con->rhythm.wrongButtons[CON_SQUARE] = textureFindInAllBanks("SQUAREX");
	ASSERT(con->rhythm.wrongButtons[CON_SQUARE]);
	con->rhythm.wrongButtons[CON_CROSS] = textureFindInAllBanks("CROSSX");
	ASSERT(con->rhythm.wrongButtons[CON_CROSS]);
	con->rhythm.wrongButtons[CON_CIRCLE]	= textureFindInAllBanks("CIRCLEX");
	ASSERT(con->rhythm.wrongButtons[CON_CIRCLE]);

	con->rhythm.back = textureFindInAllBanks("COMBO");
	ASSERT(con->rhythm.back);

//	con->progBar = textureFindInAllBanks("GRADER");
//	ASSERT(con->progBar);

	con->goldBar = textureFindInAllBanks("GRADERG");
	ASSERT(con->goldBar);
	con->silverBar = textureFindInAllBanks("GRADERS");
	ASSERT(con->silverBar);
	con->bronzeBar = textureFindInAllBanks("GRADERB");
	ASSERT(con->bronzeBar);

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


	con->tweedycEyesAnim[0] = textureFindInAllBanks("MRTWEEDYC_EYEOPEN");
	ASSERT(con->tweedycEyesAnim[0]);

	con->tweedycEyesAnim[1] = textureFindInAllBanks("MRTWEEDYC_EYECLOSED");
	ASSERT(con->tweedycEyesAnim[1]);

	con->tweedycEyesDummy = textureFindInAllBanks("MRTWEEDYC_EYES");
	ASSERT(con->tweedycEyesDummy);

	con->tweedycEyesBlinking = textureCreateAnimation(con->tweedycEyesDummy,con->tweedycEyesAnim,2);
	ASSERT(con->tweedycEyesBlinking);

	con->eyesTimer = 0;

	myactor = con->tools[CON_SAWER].actor;
}


//-----------------------------------------------------------------------------------------------------------------------
void constructEnd()
{
	mapviewReleaseMap();
	dsEvent_Init();
	

	textureDestroyAnimation(con->tweedycEyesBlinking);


	actorFree(con->tools[CON_HAMMER].actor);
	actorFree(con->tools[CON_SPANNER].actor);
	actorFree(con->tools[CON_SAWER].actor);
	actorFree(con->tweedy.actor);
	actorFree(con->rhythm.actor);
	freeFowler();
	
	FREE(con);
	//menuStart();

	mapviewReleaseMap();

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


	if(subGameRestart == TRUE)
	{
		constructStart();
	}
	else
	{
		act4SubgameComplete("A4_WORKHUT4");
	}
}

#define CON_TELESCOPE_X  34
#define CON_TELESCOPE_Y  -110
#define CON_INRO_TEXT_XPOS  -170

int CON_ARROW_Y = -110;

//-----------------------------------------------------------------------------------------------------------------------
void constructDraw()
{
	int i;

	GsSetProjection(CONSTRUCT_FOV);

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


	actorMaintainAnimation(con->tools[CON_SAWER].actor);
	EXLIGHT_SetupActorLights(con->tools[CON_SAWER].actor);
	actorDraw(con->tools[CON_SAWER].actor);

	actorMaintainAnimation(con->tools[CON_SPANNER].actor);
	EXLIGHT_SetupActorLights(con->tools[CON_SPANNER].actor);
	actorDraw(con->tools[CON_SPANNER].actor);

	actorMaintainAnimation(con->tools[CON_HAMMER].actor);
	EXLIGHT_SetupActorLights(con->tools[CON_HAMMER].actor);
	actorDraw(con->tools[CON_HAMMER].actor);


	if(!con->eyesTimer)
	{
		if(utilRandomInt(100)>93)
		{
			textureSetAnimation(con->tweedycEyesBlinking,1);
			con->eyesTimer = 2;		// Hold the blink for 3! frames 
		}
		else
		{
			textureSetAnimation(con->tweedycEyesBlinking,0);
		}
	}
	else
		con->eyesTimer--;


	if(con->gameState != CON_GAMESTATE_OUTRO)
	{
		overlaySprite(con->tweedy.overlay, CON_TELESCOPE_X, CON_TELESCOPE_Y, 10,0xff808080);
		conDrawTweedy();
		conDrawRhythm();
		conDrawProgressBar();
	}



	
	constructPrintDebugText();



	switch(con->gameState)
	{
		case CON_GAMESTATE_FADEIN:
			i= 256 - (con->frameCount)*10;
			if(i <= 0)
				con->gameState = CON_GAMESTATE_INTRO;
			else
				deathScreenAdd(i, i, i, SEMITRANS_SUB, 2);
			break;

		case CON_GAMESTATE_INTRO:
			conDrawIntro();
			break;

		case CON_GAMESTATE_RUNNING:
		case CON_GAMESTATE_ENDING:

			 DrawDirectionArrows(con->arrow, CON_ARROW_Y);

//			alpha = ((rsin(con->frameCount*SG_ARROW_FLASH_FREQ) + 4096) / 128) + 64;
//			overlaySprite(con->arrow, CON_ARROW_RX, CON_ARROW_Y, 4, alpha*(0x10101));
//			overlaySpriteFlipped(con->arrow, CON_ARROW_LX, CON_ARROW_Y, 4, alpha*(0x10101));
			break;

		case CON_GAMESTATE_OUTRO:

			if(con->progress < CON_PROGRESS_BRONZE_SCORE)
			{
				//fail
				fowlerDraw();
				conDrawFail();
			}
			else if(con->progress < CON_PROGRESS_SILVER_SCORE)
			{
				//bronze
				fowlerDraw();
				conDrawBronzeSuccess();
			}
			else if(con->progress < CON_PROGRESS_GOLD_SCORE)
			{
				//silver
				fowlerDraw();
				conDrawSilverSuccess();

			}
			else
			{
				//gold
				fowlerDraw();
				conDrawGoldSuccess();

			}
			break;

		case CON_GAMESTATE_FADEOUT:

			i= (con->frameCount)*20;
			if(i > 220)
				deathScreenAdd(255, 255, 255, SEMITRANS_SUB, 0);

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

			break;
	}

}








#define CON_PROGRESS_XPOS  6
#if PALMODE==1
#define CON_PROGRESS_YPOS  90
#else
#define CON_PROGRESS_YPOS  80
#endif

#define CON_PROG_BAR_YOFFSET  -1

#define CON_PROG_BRONZE_XOFFSET  122
#define CON_PROG_SILVER_XOFFSET  155
#define CON_PROG_GOLD_XOFFSET  187


#define CON_PROGRESS_BACK_DEPTH   5
#define CON_PROGRESS_FRONT_DEPTH  1
#define CON_PROGRESS_BAR_DEPTH    2

#define CON_PROGRESS_BAR_LEFT   28
#define CON_PROGRESS_BAR_TOP    6
#define CON_PROGRESS_BAR_WIDTH  168
#define CON_PROGRESS_BAR_HEIGHT 9

#define CON_PROGRESS_MAX   244


//-----------------------------------------------------------------------------------------------------------------------
void conDrawProgressBar()
{
	POLY_G4 *bar;
	unsigned short r1,b1,g1;
	unsigned long gold, silver, bronze;


	gold = silver = bronze = 0xff808080;

//	overlaySprite(con->progBar, CON_PROGRESS_XPOS, CON_PROGRESS_YPOS, CON_PROGRESS_BACK_DEPTH,0xff808080);


// bar

	BEGINPRIM(bar, POLY_G4);
	setPolyG4(bar);				
	bar->x0 = CON_PROGRESS_XPOS + CON_PROGRESS_BAR_LEFT;				
	bar->y0 = CON_PROGRESS_YPOS + CON_PROGRESS_BAR_TOP;	
	

	bar->x1 = bar->x0 + (con->progress * CON_PROGRESS_BAR_WIDTH)/CON_PROGRESS_MAX;
	bar->y1 = bar->y0;

	bar->x2 = bar->x0;
	bar->y2 = bar->y0 + CON_PROGRESS_BAR_HEIGHT;
	
	// on power
	bar->x3 = bar->x1;
	bar->y3 = bar->y2;


	if(con->progress < CON_PROGRESS_BRONZE_SCORE)
	{
		// nothing
	 	bar->r0 = 110;
		bar->g0 = 110;
		bar->b0 = 110;
		bar->r1 = 120;
		bar->g1 = 120;
		bar->b1 = 120;
	}
	else if(con->progress < CON_PROGRESS_SILVER_SCORE)
	{
		//bronze

		bronze = 0xffcccccc;
	 	bar->r0 = 180;
		bar->g0 = 140;
		bar->b0 = 120;
		bar->r1 = 180;
		bar->g1 = 160;
		bar->b1 = 140;
	}
	else if(con->progress < CON_PROGRESS_GOLD_SCORE)
	{
		//silver
		bronze = silver = 0xffcccccc;

		bar->r0 = 160;
		bar->g0 = 160;
		bar->b0 = 210;
		bar->r1 = 190;
		bar->g1 = 190;
		bar->b1 = 210;
	}
	else
	{
		//gold
		bronze = silver = gold = 0xffcccccc;

		bar->r0 = 240;
		bar->g0 = 230;
		bar->b0 = 160;
		bar->r1 = 240;
		bar->g1 = 230;
		bar->b1 = 190;
	}

	bar->r2 = bar->r0;
	bar->g2 = bar->g0;
	bar->b2 = bar->b0;
	bar->r3 = bar->r1;
	bar->g3 = bar->g1;
	bar->b3 = bar->b1;
		
	ENDPRIM(bar, CON_PROGRESS_BAR_DEPTH, POLY_G4);

	overlaySprite(con->goldBar, CON_PROGRESS_XPOS + CON_PROG_GOLD_XOFFSET, CON_PROGRESS_YPOS + CON_PROG_BAR_YOFFSET, CON_PROGRESS_FRONT_DEPTH,0xff999999);
	overlaySprite(con->silverBar, CON_PROGRESS_XPOS + CON_PROG_SILVER_XOFFSET, CON_PROGRESS_YPOS + CON_PROG_BAR_YOFFSET, CON_PROGRESS_FRONT_DEPTH,0xff999999);
	overlaySprite(con->bronzeBar, CON_PROGRESS_XPOS + CON_PROG_BRONZE_XOFFSET, CON_PROGRESS_YPOS + CON_PROG_BAR_YOFFSET, CON_PROGRESS_FRONT_DEPTH,0xff999999);

//	fontPrintCentred(font,(x - 4 + SUB_BAR_BAR_LEFT + SUB_BAR_BAR_WIDTH/2), y + SUB_BAR_YTEXT,TEXTSTR(TEXTSTR_SUB_POWER), 110, 110, 110);

}






#define CON_TWEEDY_CAM_LOOKATX  0
#define CON_TWEEDY_CAM_LOOKATY  0
#define CON_TWEEDY_CAM_LOOKATZ  0

#define CON_TWEEDY_CAM_X  0
#define CON_TWEEDY_CAM_Y  0
#define CON_TWEEDY_CAM_Z  -10000

#define MY_DEPTH_OVERRIDE  8
#define MY_OFFSET_DEPTH    1



#define CON_CLIP_XSIZE  140
#define CON_CLIP_YSIZE  70

#define CONTWEEDY_XOFFSET  136
#define CONTWEEDY_YOFFSET  100

#define CONTWEEDY_CLIP_XPOS  52
#define CONTWEEDY_CLIP_YPOS  -97


//-----------------------------------------------------------------------------------------------------------------------
void conDrawTweedy()
{
	PSIMODELCTRL		*modctrl = &PSImodelctrl;

	int depth = 1;
	VECTOR oldCamPos;
	VECTOR oldGaribPos;



	{	// CPW: put it back
		DR_AREA *da;
		DR_OFFSET *dof;
		u_short ofs[2];

	
 /*	dof = BEGINPRIM(dof, DR_OFFSET);
	ofs[0] = currentDisplayPage->drawenv.ofs[0];
	ofs[1] = currentDisplayPage->drawenv.ofs[1];
	SetDrawOffset(dof, ofs);
	ENDPRIM(dof, MY_OFFSET_DEPTH, DR_OFFSET);


		da = BEGINPRIM(da, DR_AREA);
		SetDrawArea(da, &currentDisplayPage->drawenv.clip);
		ENDPRIM(da, depth, DR_AREA);
 */
	}



// store cam pos

	oldCamPos.vx = mycamera.vpx;
	oldCamPos.vy = mycamera.vpy;
	oldCamPos.vz = mycamera.vpz;

	oldGaribPos.vx = mycamera.vrx;
	oldGaribPos.vy = mycamera.vry;
	oldGaribPos.vz = mycamera.vrz;


	mycamera.vrx = CON_TWEEDY_CAM_LOOKATX;
	mycamera.vry = CON_TWEEDY_CAM_LOOKATY;
	mycamera.vrz = CON_TWEEDY_CAM_LOOKATZ;

	mycamera.vpx = CON_TWEEDY_CAM_X;
	mycamera.vpy = CON_TWEEDY_CAM_Y;
	mycamera.vpz = CON_TWEEDY_CAM_Z;


	mapCamSetRefView2L(&mycamera);


	PSImodelctrl.depthoverride = MY_DEPTH_OVERRIDE;

	actorMaintainAnimation(con->tweedy.actor);
	EXLIGHT_SetupActorLights(con->tweedy.actor);
	actorDraw(con->tweedy.actor);


	PSImodelctrl.depthoverride = 0;

	
	{// set clip window
		
#ifndef _WINDOWS		
		RECT clip;
#else
		RECTPS clip;
#endif
		DR_AREA *da;
		DR_OFFSET *dof;

		u_short ofs[2];

		clip.x = currentDisplayPage->drawenv.clip.x + 256 + CONTWEEDY_CLIP_XPOS;
		clip.y = currentDisplayPage->drawenv.clip.y + 128 + CONTWEEDY_CLIP_YPOS;
		clip.w = CON_CLIP_XSIZE;
   		clip.h = CON_CLIP_YSIZE;
	/*
   		da = BEGINPRIM(da, DR_AREA);
   		SetDrawArea(da, &clip);
   		ENDPRIM(da, depth, DR_AREA);

	dof = BEGINPRIM(dof, DR_OFFSET);
   	ofs[0] = currentDisplayPage->drawenv.ofs[0]+CONTWEEDY_CLIP_XPOS-CON_CLIP_XSIZE/2 + CONTWEEDY_XOFFSET;
   	ofs[1] = currentDisplayPage->drawenv.ofs[1]+CONTWEEDY_CLIP_YPOS-CON_CLIP_YSIZE/2 + CONTWEEDY_YOFFSET;
   	SetDrawOffset(dof, ofs);
   	ENDPRIM(dof, MY_OFFSET_DEPTH, DR_OFFSET);
	  */

	}


	//restore cam pos
	mycamera.vpx = oldCamPos.vx;
	mycamera.vpy = oldCamPos.vy;
	mycamera.vpz = oldCamPos.vz;

	mycamera.vrx = oldGaribPos.vx;
	mycamera.vry = oldGaribPos.vy;
	mycamera.vrz = oldGaribPos.vz;

	mapCamSetRefView2L(&mycamera);

}



#define CON_BACK_XPOS   16
#if PALMODE==1
  #define CON_BACK_YPOS   36
#else
  #define CON_BACK_YPOS   26
#endif
#define BUTTON_START_X   2
#define BUTTON_START_Y   7
#define HALF_BEAT_DELTA  25
#define FULL_BEAT_DELTA  50
#define CHICK_START_Y    -22
#define CON_CHICK_MOVE_FACTOR 100
#define CON_PRESSED_Y  25


//-----------------------------------------------------------------------------------------------------------------------
void conDrawRhythm()
{
	int i;
	TextureType *texture;
	
	overlaySprite(con->rhythm.back, CON_BACK_XPOS, CON_BACK_YPOS, 10,0xff808080);

	for(i=0; i<8; i++)
	{
		if(con->rhythm.currentButtons[i] > 0)
		{
			if(con->tweedy.state != CON_TWEEDY_WORKING)
				overlaySprite(con->rhythm.buttons[con->rhythm.currentButtons[i]-1], CON_BACK_XPOS + BUTTON_START_X + i*FULL_BEAT_DELTA/2, CON_BACK_YPOS + BUTTON_START_Y, 5,0xff404080);
			else if((i == con->rhythm.currentBeat) && (con->rhythm.state ==CON_RHYTHM_STATE_TESTING) && ((con->rhythm.numTimesPlayed < (CON_MAX_TIMES_PLAYED-1))||(con->rhythm.currentBeat != 0 )) && (con->tweedy.state == CON_TWEEDY_WORKING))
				overlaySprite(con->rhythm.buttons[con->rhythm.currentButtons[i]-1], CON_BACK_XPOS + BUTTON_START_X + i*FULL_BEAT_DELTA/2, CON_BACK_YPOS + BUTTON_START_Y, 5,0xffeeeeee);
			else
				overlaySprite(con->rhythm.buttons[con->rhythm.currentButtons[i]-1], CON_BACK_XPOS + BUTTON_START_X + i*FULL_BEAT_DELTA/2, CON_BACK_YPOS + BUTTON_START_Y, 5,0xff808080);
		}
		
		if(con->rhythm.pressedButtons[i] > 0)
			overlaySprite(con->rhythm.buttons[con->rhythm.pressedButtons[i]-1], CON_BACK_XPOS + BUTTON_START_X + i*FULL_BEAT_DELTA/2, CON_BACK_YPOS + BUTTON_START_Y + CON_PRESSED_Y, 5,0xff808080);
		else if(con->rhythm.pressedButtons[i] < 0)
			overlaySprite(con->rhythm.wrongButtons[-1 - con->rhythm.pressedButtons[i]], CON_BACK_XPOS + BUTTON_START_X + i*FULL_BEAT_DELTA/2, CON_BACK_YPOS + BUTTON_START_Y + CON_PRESSED_Y, 5,0xff808080);

	}
	

	if((con->rhythm.state == CON_RHYTHM_STATE_TESTING) || (con->rhythm.state == CON_RHYTHM_STATE_CHANGING))//(con->tweedy.state == CON_TWEEDY_WORKING)||(con->tweedy.state == CON_TWEEDY_SUSPICIOUS))//
	{

		conDrawChick();


//		if((con->frameCount % 4) < 2)
//			texture = con->rhythm.chick[0];
//		else
//			texture = con->rhythm.chick[1];

//		overlaySprite(texture, CON_BACK_XPOS + BUTTON_START_X + (con->rhythm.posOnWood * CON_CHICK_MOVE_FACTOR)/4096 , CON_BACK_YPOS + CHICK_START_Y, 5,0xff808080);
	}


}





#define CHICK_TWEEDY_CAM_LOOKATX  0
#define CHICK_TWEEDY_CAM_LOOKATY  0
#define CHICK_TWEEDY_CAM_LOOKATZ  0

#define CHICK_TWEEDY_CAM_X  0
#define CHICK_TWEEDY_CAM_Y  0
#define CHICK_TWEEDY_CAM_Z  -8000

//int MY_DEPTH_OVERRIDE = 8;
//int MY_OFFSET_DEPTH = 1;



int CHICK_CLIP_XSIZE = 52;
int CHICK_CLIP_YSIZE = 64;

int CHICKTWEEDY_XOFFSET = 50;
int CHICKTWEEDY_YOFFSET = 98;


int CHICK_CHICK_CLIP_XPOS = 6;
#if PALMODE==1
int CHICK_CHICK_CLIP_YPOS = -28;
#else
int CHICK_CHICK_CLIP_YPOS = -38;
#endif
//int CON_CHICK_ROTATE = 0;


//-----------------------------------------------------------------------------------------------------------------------
void conDrawChick()
{
	PSIMODELCTRL		*modctrl = &PSImodelctrl;

	int depth = 1;
	VECTOR oldCamPos;
	VECTOR oldGaribPos;


	int clipXPos = CHICK_CHICK_CLIP_XPOS + (con->rhythm.posOnWood * CON_CHICK_MOVE_FACTOR)/4096;

//	con->rhythm.actor->psiData.object->rotate.vy = CON_CHICK_ROTATE;



	{	// CPW: put it back
		DR_AREA *da;
		DR_OFFSET *dof;
		u_short ofs[2];

	
  /*	dof = BEGINPRIM(dof, DR_OFFSET);
	ofs[0] = currentDisplayPage->drawenv.ofs[0];
	ofs[1] = currentDisplayPage->drawenv.ofs[1];
	SetDrawOffset(dof, ofs);
	ENDPRIM(dof, MY_OFFSET_DEPTH, DR_OFFSET);


		da = BEGINPRIM(da, DR_AREA);
		SetDrawArea(da, &currentDisplayPage->drawenv.clip);
		ENDPRIM(da, depth, DR_AREA);
	  */
	  }



// store cam pos

	oldCamPos.vx = mycamera.vpx;
	oldCamPos.vy = mycamera.vpy;
	oldCamPos.vz = mycamera.vpz;

	oldGaribPos.vx = mycamera.vrx;
	oldGaribPos.vy = mycamera.vry;
	oldGaribPos.vz = mycamera.vrz;


	mycamera.vrx = CHICK_TWEEDY_CAM_LOOKATX;
	mycamera.vry = CHICK_TWEEDY_CAM_LOOKATY;
	mycamera.vrz = CHICK_TWEEDY_CAM_LOOKATZ;

	mycamera.vpx = CHICK_TWEEDY_CAM_X;
	mycamera.vpy = CHICK_TWEEDY_CAM_Y;
	mycamera.vpz = CHICK_TWEEDY_CAM_Z;


	mapCamSetRefView2L(&mycamera);


	PSImodelctrl.depthoverride = MY_DEPTH_OVERRIDE;

	actorMaintainAnimation(con->rhythm.actor);
	EXLIGHT_SetupActorLights(con->rhythm.actor);
	actorDraw(con->rhythm.actor);

	PSImodelctrl.depthoverride = 0;

	
	{// set clip window
		
#ifndef _WINDOWS		
		RECT clip;
#else
		RECTPS clip;
#endif

		DR_AREA *da;
		DR_OFFSET *dof;

		u_short ofs[2];

		clip.x = currentDisplayPage->drawenv.clip.x + 256 + clipXPos;
		clip.y = currentDisplayPage->drawenv.clip.y + 128 + CHICK_CHICK_CLIP_YPOS;
		clip.w = CHICK_CLIP_XSIZE;
   		clip.h = CHICK_CLIP_YSIZE;
	
 /*  		da = BEGINPRIM(da, DR_AREA);
   		SetDrawArea(da, &clip);
   		ENDPRIM(da, depth, DR_AREA);

	dof = BEGINPRIM(dof, DR_OFFSET);
   	ofs[0] = currentDisplayPage->drawenv.ofs[0]+clipXPos-CHICK_CLIP_XSIZE/2 + CHICKTWEEDY_XOFFSET;
   	ofs[1] = currentDisplayPage->drawenv.ofs[1]+CHICK_CHICK_CLIP_YPOS-CHICK_CLIP_YSIZE/2 + CHICKTWEEDY_YOFFSET;
   	SetDrawOffset(dof, ofs);
   	ENDPRIM(dof, MY_OFFSET_DEPTH, DR_OFFSET);
   */

	}


	//restore cam pos
	mycamera.vpx = oldCamPos.vx;
	mycamera.vpy = oldCamPos.vy;
	mycamera.vpz = oldCamPos.vz;

	mycamera.vrx = oldGaribPos.vx;
	mycamera.vry = oldGaribPos.vy;
	mycamera.vrz = oldGaribPos.vz;

	mapCamSetRefView2L(&mycamera);




}




void conDrawIntro()
{
	char string[256];
	
	sprintf(string, "%s\n\n%s\n%s",
		(char*)TEXTSTR(TEXTSTR_SUB_CON_INTRO1),
		(char*)TEXTSTR(TEXTSTR_SUB_CON_BUTTONS), 
		(char*)TEXTSTR(TEXTSTR_SUB_XSTART)
		);

	fontPrintMultiLineBox(font, 0, 0, 340, string, 100, 100, 100);
}






#define CON_OUTRO_WIDTH       380
#define CON_OUTRO_TEXT_XPOS   -196
#define CON_OUTRO_MARGIN      34

#define CON2_OUTRO_WIDTH      420
#define CON2_OUTRO_TEXT_XPOS  -220
#define CON2_OUTRO_MARGIN     26



//------------------------------------------------
void conDrawBronzeSuccess()
{
	char string[256];
	
	sprintf(string, "%s\n%s", (char*)TEXTSTR(TEXTSTR_SUB_CON_BRONZE), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, string, 120, 100, 0,2);
}

//------------------------------------------------
void conDrawSilverSuccess()
{
	char string[256];
	
	sprintf(string, "%s\n%s", (char*)TEXTSTR(TEXTSTR_SUB_CON_SILVER), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, string, 120, 100, 0,2);
}

//------------------------------------------------
void conDrawGoldSuccess()
{
	char string[256];
	
	sprintf(string, "%s\n%s", (char*)TEXTSTR(TEXTSTR_SUB_CON_GOLD), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, string, 120, 100, 0,2);
}


//------------------------------------------------
void conDrawFail()
{
	char string[256];
	
	sprintf(string, "%s\n%s", (char*)TEXTSTR(TEXTSTR_SUB_CON_FAIL), (char*)TEXTSTR(TEXTSTR_SUB_GAME_EXIT_RESTART));
	fontPrintMultiLineBoxSetDepth(font, 0, 0, 340, string, 120, 100, 0,2);
}




//-----------------------------------------------------------------------------------------------------------------------
void constructCycle()
{

	con->frameCount++;

	EXLIGHT_Update();
	dsEvent_Update();


	switch(con->gameState)
	{
		case CON_GAMESTATE_FADEIN:
			break;
		case CON_GAMESTATE_INTRO:
			if(pReadPad->debounce[0] & PAD_CROSS)
				con->gameState = CON_GAMESTATE_RUNNING;

			else if(pReadPad->debounce[0] & PAD_TRIANGLE)
			{
				con->frameCount = 0;
				con->gameState = CON_GAMESTATE_FADEOUT;
				subGameRestart = FALSE;
				return;
			}
			break;

		case CON_GAMESTATE_RUNNING:


#ifdef DEBUG
			if(SwimCheatCycle())
			{
				con->gameState = CON_GAMESTATE_ENDING;
				con->progress = CON_PROGRESS_BRONZE_SCORE;
				actorAnimate(con->tweedy.actor, CON_MRTWEEDY_OI_BUGGERS, FALSE, TRUE, 200, 0, 900);

			}
#endif


			constructControl();
			conUpdateCamera();
			conUpdateRhythm();
			conUpdateTools();
			conUpdateMrTweedy();
			break;

		case CON_GAMESTATE_ENDING:

			// CON_MRTWEEDY_OI_BUGGERS


			if((con->tweedy.actor->animation.animData[0].reachedEnd)&&(con->tweedy.actor->animation.animData[0].scaledFrameOffset > 0) && (con->tweedy.actor->animation.animData[0].segment == CON_MRTWEEDY_OI_BUGGERS))
			{
				con->gameState = CON_GAMESTATE_OUTRO;



				if(con->progress < CON_PROGRESS_BRONZE_SCORE)
				{
					sound_XA_Start(TUNE_4_FAILED + SOUND_XA_NOLOOP);
					startFowler(FOWLER_FAIL_OFFSET, 100);
				}
				else
				{
					if(gameCalledFrom != GAMECALLEDFROM_DEBUG)
					{
						gamePlayData->SubGameStates[SUBGAME_CONSTRUCT] = REPLAY_BEATEN;
						gameSaveData.levels_available[REPLAY_AVAILABLE_BUILDING] = REPLAY_BEATEN;
					}

					if(con->progress < CON_PROGRESS_SILVER_SCORE)
					{
						//bronze
						if(gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION] < 1) 
							gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION] = 1; 

						sound_XA_Start(TUNE_3_COMPLETE + SOUND_XA_NOLOOP);
						startFowler(FOWLER_BRONZE_OFFSET, 100);

					}
					else if(con->progress < CON_PROGRESS_GOLD_SCORE)
					{
						//silver
						if(gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION] < 2) 
							gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION] = 2; 
						sound_XA_Start(TUNE_3_COMPLETE + SOUND_XA_NOLOOP);
						startFowler(FOWLER_SILVER_OFFSET, 100);
					}
					else
					{
						//gold
						gameSaveData.medals_won[MEDAL_WON_CONSTRUCTION] = 3; 
						sound_XA_Start(TUNE_3_COMPLETE + SOUND_XA_NOLOOP);
						startFowler(FOWLER_GOLD_OFFSET, 100);
					}
				}
			}



			// check for end of Tweedy anim then bring on outr screen
			break;

		case CON_GAMESTATE_OUTRO:

			if(con->frameCount > 10)
			{
				if(pReadPad->debounce[0] & PAD_CROSS)
				{
					con->frameCount = 0;
					con->gameState = CON_GAMESTATE_FADEOUT;
					subGameRestart = TRUE;
				
				}
				else if(pReadPad->debounce[0] & PAD_TRIANGLE)
				{
					con->frameCount = 0;
					con->gameState = CON_GAMESTATE_FADEOUT;
					subGameRestart = FALSE;
				}
			}
			break;

		case CON_GAMESTATE_FADEOUT:

			break;
	}
}



#define CON_RYTHM_TIME_PER_FRAME  100

#define CON_MYPITCH			 1600
#define CON_TOOL_ANIM_SPEED  800


//-----------------------------------------------------------------------------------------------------------------------
void conUpdateRhythm()
{
	int tempInt;
	int sampleNum;
	int sampleBeat;
	int soundPlayed=0;


	switch(con->rhythm.state)
	{
		case CON_RHYTHM_STATE_OFF:
			break;

		case CON_RHYTHM_STATE_SHOWING:
			break;

		case CON_RHYTHM_STATE_COUNTING:
			break;
		
		case CON_RHYTHM_STATE_TESTING:


			if(con->rhythm.posInBar == CON_TIME_FOR_BAR)
			{
				con->rhythm.posInBar = 0;
				con->rhythm.numTimesPlayed++;

			
				// start tweedy anims etc here
				if(con->rhythm.numTimesPlayed == CON_MAX_TIMES_PLAYED)
				{
					con->rhythm.currentRhythm++;// = random(CON_NUM_RHYTHMS);

					con->rhythm.state = CON_RHYTHM_STATE_STARTNEW;
					con->tweedy.state = CON_TWEEDY_STOP_WORKING;

					con->rhythm.numTimesPlayed = 0;

					//clear buttons
					for(tempInt=0; tempInt<8; tempInt++)
					{
						con->rhythm.currentButtons[tempInt] = 0;
//						con->rhythm.pressedButtons[tempInt] = 0;
					}
					break;
				}
			}
			
			con->rhythm.currentBeat = (con->rhythm.posInBar + (CON_TIME_FOR_BAR/16)) / (CON_TIME_FOR_BAR/8);

			if(con->rhythm.currentBeat == 8)
				con->rhythm.currentBeat = 0;

			if((con->rhythm.lastBeat == 7) && (con->rhythm.currentBeat == 0))
			{
				for(tempInt=0; tempInt<8; tempInt++)
				{
					con->rhythm.pressedButtons[tempInt] = 0;
				}
			}


			if((pReadPad->debounce[0] & (PAD_CROSS|PAD_CIRCLE|PAD_SQUARE)) && (con->rhythm.pressedButtons[con->rhythm.currentBeat] == 0) && !((con->rhythm.numTimesPlayed == CON_MAX_TIMES_PLAYED-1) && (con->rhythm.currentBeat == 0) && (con->rhythm.posInBar > 1000) ) && (con->cam.state == CON_CAMSTATE_ARRIVED) && (con->tweedy.state == CON_TWEEDY_WORKING))
			{
				if(pReadPad->debounce[0] & PAD_CROSS)
				{
					if((rhythms[con->rhythm.currentRhythm][con->rhythm.currentBeat] == CON_CROSS+1)&&(con->activeTool == con->tweedy.currentTool) )
						con->rhythm.pressedButtons[con->rhythm.currentBeat] = CON_CROSS+1;
					else
						con->rhythm.pressedButtons[con->rhythm.currentBeat] = -(CON_CROSS+1);

					conPlaySound(FALSE, CON_CROSS+1);
					soundPlayed = 1;


				}

				if(pReadPad->debounce[0] & PAD_SQUARE)
				{
					if((rhythms[con->rhythm.currentRhythm][con->rhythm.currentBeat] == CON_SQUARE+1)&&(con->activeTool == con->tweedy.currentTool) )
						con->rhythm.pressedButtons[con->rhythm.currentBeat] = CON_SQUARE+1;
					else
						con->rhythm.pressedButtons[con->rhythm.currentBeat] = -(CON_SQUARE+1);

					conPlaySound(FALSE, CON_SQUARE+1);
					soundPlayed = 1;
				}

				if(pReadPad->debounce[0] & PAD_CIRCLE)
				{
					if((rhythms[con->rhythm.currentRhythm][con->rhythm.currentBeat] == CON_CIRCLE+1)&&(con->activeTool == con->tweedy.currentTool) )
						con->rhythm.pressedButtons[con->rhythm.currentBeat] = CON_CIRCLE+1;
					else
						con->rhythm.pressedButtons[con->rhythm.currentBeat] = -(CON_CIRCLE+1);
		
					conPlaySound(FALSE, CON_CIRCLE+1);
					soundPlayed = 1;
				}

				if(con->rhythm.pressedButtons[con->rhythm.currentBeat] < 0)
				{
					con->tweedy.state = CON_TWEEDY_SUSPICIOUS;
				}
				else
				{
					con->progress++;
					if(con->progress > CON_PROGRESS_MAX)
						con->progress = CON_PROGRESS_MAX;
				}

				actorAnimate(con->tools[con->activeTool].actor, CON_WORK, FALSE, FALSE, CON_TOOL_ANIM_SPEED, 0, 0);
				actorAnimate(con->tools[con->activeTool].actor, CON_READY, TRUE, TRUE, 300, 0, 0);

			}
			

			if((con->rhythm.posInBar % (CON_TIME_FOR_BAR/8)) == 0)
			{
				if((con->tweedy.state == CON_TWEEDY_WORKING) && (con->rhythm.currentButtons[con->rhythm.currentBeat] > 0))
				{

					if(!soundPlayed)
					{
						soundPlayed = 1;
						conPlaySound(TRUE, con->rhythm.currentButtons[con->rhythm.currentBeat]);// need to make this tweedy?, tool??, button
					}
					// try animhere may have to move it to previous frame!
					switch(con->tweedy.currentTool)
					{
						case CON_SAWER:
							actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SAW, FALSE, FALSE, 600, 0, 0);
							break;
						case CON_HAMMER:
							actorAnimate(con->tweedy.actor, CON_MRTWEEDY_HAMMER, FALSE, FALSE, 600, 0, 0);
							break;
						case CON_SPANNER:
							actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SPANNER, FALSE, FALSE, 600, 0, 0);
							break;
					}
				}
//				else //if((con->rhythm.posInBar % (CON_TIME_FOR_BAR/4)) == 0)
				{

					sampleBeat = con->rhythm.currentBeat*2;
					sampleNum = conBackRythm[sampleBeat];

#ifdef PRINT_SOUNDS
				DB("beat = %d  sn = %d\n",sampleBeat, sampleNum);
#endif
					if(!soundPlayed)
					{
						soundPlayed = TRUE;
						soundPlaySample(con->pSamples[sampleNum], 0x8080, CON_MYPITCH);
					}




					// play half beat
					//soundPlaySample(pSamples[SFX_GINGERLAND], 0xffff, CON_MYPITCH);
	//				if((con->rhythm.posInBar % (CON_TIME_FOR_BARc/4)) == 0)
	//				{						   
	//					soundPlaySample(pSamples[SFX_GINGER_LAND], 0xffff, CON_MYPITCH);
	//				}
				}
			}
			else if((!soundPlayed)&&(con->rhythm.posInBar % (CON_TIME_FOR_BAR/16)) == 0)
			{
				//play quarter beat
				
				if(con->rhythm.currentBeat == 0)
					sampleBeat = 15;
				else
					sampleBeat = (con->rhythm.currentBeat*2)-1;


				sampleNum = conBackRythm[sampleBeat];

#ifdef PRINT_SOUNDS
				DB("beat = %d  sn = %d\n",sampleBeat, sampleNum);
#endif


				soundPlayed = 1;
				soundPlaySample(con->pSamples[sampleNum], 0x8080, CON_MYPITCH);


			}


			con->rhythm.posInBar += CON_RYTHM_TIME_PER_FRAME;

			
			if((con->rhythm.posInBar > ((CON_TIME_FOR_BAR * 15)/16)) && (con->rhythm.numTimesPlayed < (CON_MAX_TIMES_PLAYED-1)))
				con->rhythm.posOnWood = con->rhythm.posInBar - CON_TIME_FOR_BAR;
			else
				con->rhythm.posOnWood = con->rhythm.posInBar;
			break;
		
		case CON_RHYTHM_STATE_CHANGING:

			con->rhythm.currentBeat = (con->rhythm.posInBar + (CON_TIME_FOR_BAR/16)) / (CON_TIME_FOR_BAR/8);
			if(con->rhythm.currentBeat == 8)
				con->rhythm.currentBeat = 0;


			if((con->rhythm.posInBar % (CON_TIME_FOR_BAR/8)) == 0)
			{
				tempInt = (con->rhythm.posInBar / (CON_TIME_FOR_BAR/8));
				con->rhythm.currentButtons[tempInt] = rhythms[con->rhythm.currentRhythm][tempInt];

				if((!soundPlayed)&&(con->tweedy.state == CON_TWEEDY_WORKING) && (con->rhythm.currentButtons[tempInt] > 0))
				{
					conPlaySound(TRUE, con->rhythm.currentButtons[tempInt]);// need to make this tweedy?, tool??, button
					soundPlayed = 1;
				// try animhere may have to move it to previous frame!
					switch(con->tweedy.currentTool)
					{
						case CON_SAWER:
							actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SAW, FALSE, FALSE, 900, 0, 0);
							break;
						case CON_HAMMER:
							actorAnimate(con->tweedy.actor, CON_MRTWEEDY_HAMMER, FALSE, FALSE, 900, 0, 0);
							break;
						case CON_SPANNER:
							actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SPANNER, FALSE, FALSE, 900, 0, 0);
							break;
					}
				}
				// here play background sample

				//else //if((con->rhythm.posInBar % (CON_TIME_FOR_BAR/8)) == 0)
				if(!soundPlayed)
				{

					sampleBeat = con->rhythm.currentBeat*2;

					sampleNum = conBackRythm[sampleBeat];

#ifdef PRINT_SOUNDS
					DB("beat = %d  sn = %d\n",sampleBeat, sampleNum);
#endif
					
					
					soundPlaySample(con->pSamples[sampleNum], 0x8080, CON_MYPITCH);
					soundPlayed = 1;

					

					// half beat
	//				soundPlaySamplePanFade(pSamples[SFX_GINGER_FOOTSTEP], 255, 0, &con->tools[0].actor->position);
					//soundPlaySample(pSamples[SFX_GINGERLAND], 0xffff, CON_MYPITCH);
  				}
			}
			else if((!soundPlayed)&&(con->rhythm.posInBar % (CON_TIME_FOR_BAR/16)) == 0)
			{
				//quarter beat
				if(con->rhythm.currentBeat == 0)
					sampleBeat = 15;
				else
					sampleBeat = (con->rhythm.currentBeat*2)-1;
	
				
				sampleNum = conBackRythm[sampleBeat];

#ifdef PRINT_SOUNDS
				DB("beat = %d  sn = %d\n",sampleBeat, sampleNum);
#endif

				soundPlaySample(con->pSamples[sampleNum], 0x8080, CON_MYPITCH);

			}

			con->rhythm.posInBar += CON_RYTHM_TIME_PER_FRAME;

			if(con->rhythm.posInBar == CON_TIME_FOR_BAR)
			{
				con->rhythm.posInBar = 0;
				con->rhythm.state = CON_RHYTHM_STATE_TESTING;
			}

			if(con->rhythm.posInBar > ((CON_TIME_FOR_BAR * 15)/16))
				con->rhythm.posOnWood = con->rhythm.posInBar - CON_RYTHM_TIME_PER_FRAME;
			else
				con->rhythm.posOnWood = con->rhythm.posInBar;

			break;

		case CON_RHYTHM_STATE_STARTNEW:

			// if tweedy ready then...

			if(con->tweedy.state == CON_TWEEDY_WORKING)
			{
				if(con->rhythm.currentRhythm >= CON_NUM_RHYTHMS)
				{
					// Game Over . . .
					//con->gameState = CON_GAMESTATE_ENDING;
					con->rhythm.state = CON_RHYTHM_STATE_OFF;
//					con->tweedy.state = CON_TWEEDY_STOP_WORKING;
//					break;

					// animate Tweedy

					// game state = ending


//					con->rhythm.currentRhythm = 0;
				}

///				con->rhythm.posInBar = 0;
				con->rhythm.state = CON_RHYTHM_STATE_CHANGING;
			}
			break;
		
		case CON_RHYTHM_STATE_PLACINGBUTTONS:
			break;
	}


	// test for and call chick anims here
	if(con->rhythm.currentBeat != con->rhythm.lastBeat)
	{

		if((rhythms[con->rhythm.currentRhythm][con->rhythm.currentBeat] > 0) && (con->tweedy.state == CON_TWEEDY_WORKING))
		{
			switch((rhythms[con->rhythm.currentRhythm][con->rhythm.currentBeat]-1))
			{
				case CON_SQUARE:
					actorAnimate(con->rhythm.actor, CON_CHICK_SQUARE, FALSE, FALSE,500, 0, 0);
					actorAnimate(con->rhythm.actor, CON_CHICK_WALK, TRUE, TRUE,500, 0, 0);
					//DB("square\n");
					break;

				case CON_CROSS:
					actorAnimate(con->rhythm.actor, CON_CHICK_CROSS, FALSE, FALSE,500, 0, 0);
					actorAnimate(con->rhythm.actor, CON_CHICK_WALK, TRUE, TRUE,500, 0, 0);
					//DB("cross\n");
					break;

				case CON_CIRCLE:
					actorAnimate(con->rhythm.actor, CON_CHICK_CIRCLE, FALSE, FALSE,500, 0, 0);
					actorAnimate(con->rhythm.actor, CON_CHICK_WALK, TRUE, TRUE,500, 0, 0);
					//DB("circle\n");
					break;

				default:
				//	DB("poo\n");
					break;
			}
		}


		con->rhythm.lastBeat = con->rhythm.currentBeat;
	}
}


//-----------------------------------------------------------------------------------------------------------------------
void conUpdateMrTweedy()
{
	switch(con->tweedy.state)
	{
		case CON_TWEEDY_IDLE:
			if((con->tweedy.actor->animation.queueSize == 0)&&(con->tweedy.actor->animation.animData[0].reachedEnd)&&(con->tweedy.actor->animation.animData[0].scaledFrameOffset > 0))
			{
				actorAnimate(con->rhythm.actor, CON_CHICK_WALK, TRUE, FALSE, 500, 0, 1024);
				con->tweedy.state = CON_TWEEDY_WORKING;
			}
			break;
		case CON_TWEEDY_WORKING:
			break;
		case CON_TWEEDY_STOP_WORKING:
			conTweedyChangeTool();
			break;
		case CON_TWEEDY_SUSPICIOUS:

			if(con->tweedy.currentTool == CON_SAWER)
				actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SAW_HUH, FALSE, FALSE, 400, 0, 900);
			else if(con->tweedy.currentTool == CON_HAMMER)
				actorAnimate(con->tweedy.actor, CON_MRTWEEDY_HAMMER_HUH, FALSE, FALSE, 400, 0, 900);
			else if(con->tweedy.currentTool == CON_SPANNER)
				actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SPANNER_HUH, FALSE, FALSE, 400, 0, 900);

			actorAnimate(con->rhythm.actor, CON_CHICK_HIDE, TRUE, FALSE, 500, 0, 1024);


			con->tweedy.state = CON_TWEEDY_IDLE;
			break;

		case CON_TWEEDY_ENDING:


			break;

	}
}

// alot of stuff to do - take it out of update
//-----------------------------------------------------------------------------------------------------------------------
void conTweedyChangeTool()
{

	// put down animation
	switch(con->tweedy.currentTool)
	{
		case CON_SAWER:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SAW_PUTDOWN, FALSE, FALSE, 400, 0, 300);
			break;
		case CON_SPANNER:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_SPANNER_PUTDOWN, FALSE, FALSE, 400, 0, 300);
			break;
		case CON_HAMMER:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_HAMMER_PUTDOWN, FALSE, FALSE, 400, 0, 300);
			break;
	}

//	DB("cr = %d\n", con->rhythm.currentRhythm);

	// queue idle animation
	switch(con->rhythm.currentRhythm)
	{
		case 0:
		case 6:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_SCRATCH, FALSE, TRUE, 200, 0, 0);
			break;

		case 1:
		case 5:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE, FALSE, TRUE, 200, 0, 0);
			break;

		case 7:
		case 2:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_SCRATCH_HEAD, FALSE, TRUE, 200, 0, 0);
			break;

		case 9:
		case 10:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_BREATHE, FALSE, TRUE, 200, 0, 0);
			break;

		case 8:
		case 4:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_SIGH, FALSE, TRUE, 200, 0, 0);
			break;

		case 11:
		case 3:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_IDLE_SPECIAL, FALSE, TRUE, 200, 0, 0);
			break;

		//case 11:
		case 12:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_OI_BUGGERS, FALSE, TRUE, 200, 0, 900);
			con->tweedy.state = CON_TWEEDY_ENDING;
			con->gameState = CON_GAMESTATE_ENDING;
			con->rhythm.state = CON_RHYTHM_STATE_OFF;
			return;
			break;

		default:
			DB("no idle for rhythm!\n");
	}

	// choose next tool
	con->tweedy.currentTool = random(3);
	switch(con->tweedy.currentTool)
	{
		case CON_SAWER:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_GET_SAW, FALSE, TRUE, 400, 0, 0);
			break;
		case CON_SPANNER:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_GET_SPANNER, FALSE, TRUE, 400, 0, 0);
			break;
		case CON_HAMMER:
			actorAnimate(con->tweedy.actor, CON_MRTWEEDY_GET_HAMMER, FALSE, TRUE, 400, 0, 0);
			break;
	}
	con->tweedy.state = CON_TWEEDY_IDLE;
///
	con->rhythm.posInBar = 0;
	con->rhythm.posOnWood = 0;

}



 int HAM1 = 1;
 int HAM2 = 1;
 int HAM3 = 1;
 int HAM4 =	256;
 int HAM5 = 0;
 int HAM6 = 5;


#define CON_HIT_PITCH  2800

#define CON_HIT_DELTA  400
//-----------------------------------------------------------------------------------------------------------------------
// add tools later
void conPlaySound(short tweedy, short button)
{
	int sample, pitch, res;

	if(tweedy)
	{
		switch(button-1)
		{
			case CON_SQUARE:
//				pitch = CON_HIT_PITCH-CON_HIT_DELTA;
				sample = SFX_CONSTRUCT_KICK2;
				break;

			case CON_CROSS:
//				pitch = CON_HIT_PITCH;
				sample = SFX_CONSTRUCT_CRASH;
				break;

			case CON_CIRCLE:
//				pitch = CON_HIT_PITCH+CON_HIT_DELTA;
				sample = SFX_CONSTRUCT_SNARE;
				break;

			default:
				DB("illegal sound in conPlaySound\n");
				return;
		}
	}
	else
	{
		switch(button-1)
		{
			case CON_SQUARE:
//				pitch = CON_HIT_PITCH-CON_HIT_DELTA;
				sample = SFX_CONSTRUCT_KICK2;
				break;

			case CON_CROSS:
//				pitch = CON_HIT_PITCH;
				sample =  SFX_CONSTRUCT_CRASH;
				break;

			case CON_CIRCLE:
//				pitch = CON_HIT_PITCH+CON_HIT_DELTA;
				sample = SFX_CONSTRUCT_SNARE;
				break;

			default:
				DB("illegal sound in conPlaySound\n");
				return;

		}
		switch(con->activeTool)
		{


			case CON_SAWER:
				dsEvent_Add(0, 0, 1, 140, 0, 12, FALSE);
				break;
			case CON_HAMMER:
				dsEvent_Add(HAM1, HAM2, HAM3, HAM4, HAM5, HAM6, FALSE);
				break;
			case CON_SPANNER:
				dsEvent_Add(1, 1, 1, 112, 0, 16, FALSE);
				break;
		}
	}

	res = soundPlaySample(con->pSamples[sample], 0x8080, 0);

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

//-----------------------------------------------------------------------------------------------------------------------
void conUpdateTools()
{
	int i;

	/*
	CON_BREATH,
	CON_TAP_FOOT,
	CON_LOOKAROUND,
	CON_GOTO_WORK,
	CON_WORK,
	CON_STOP_WORK,
	*/

	for(i=0; i< CON_NUM_TOOLS; i++)
	{
		switch(con->tools[i].state)
		{
			case CON_TOOL_IDLE:

//				if((random(100) == 5) && (con->tools[i].actor->animation.animData[0].segment == CON_BREATH))
//				{
//					actorAnimate(con->tools[i].actor,CON_LOOKAROUND, FALSE, FALSE, 300, 0, 900);
//					actorAnimate(con->tools[i].actor,CON_TAP_FOOT, FALSE, TRUE, 300, 0, 0);
//					actorAnimate(con->tools[i].actor,CON_BREATH, TRUE, TRUE, 300, 0, 0);
//				}
				break;

			case CON_TOOL_PREPARE:
				actorAnimate(con->tools[i].actor,CON_LOOKAROUND, FALSE, FALSE, 300, 0, 900);
				actorAnimate(con->tools[i].actor,CON_TAP_FOOT, FALSE, TRUE, 300, 0, 0);
				actorAnimate(con->tools[i].actor,CON_GOTO_WORK, FALSE, TRUE, 300, 0, 900);
				actorAnimate(con->tools[i].actor, CON_READY, TRUE, TRUE, 300, 0, 0);

				
//				if(con->tools[0].state == CON_TOOL_HITTING)
//					con->tools[0].state = CON_TOOL_STOPWORK;
//				if(con->tools[1].state == CON_TOOL_HITTING)
//					con->tools[1].state = CON_TOOL_STOPWORK;
//				if(con->tools[2].state == CON_TOOL_HITTING)
//					con->tools[2].state = CON_TOOL_STOPWORK;

				//debug stuff
				myactor = con->tools[i].actor;

				con->tools[i].state = CON_TOOL_HITTING;
				con->activeTool = i;
				break;

			case CON_TOOL_HITTING:
				break;

			case CON_TOOL_STOPWORK:
				actorAnimate(con->tools[i].actor,CON_STOP_WORK, FALSE, FALSE, 300, 0, 900);
				actorAnimate(con->tools[i].actor,CON_BREATH, TRUE, TRUE, 300, 0, 0);
				con->tools[i].state = CON_TOOL_IDLE;
				break;

		}
	}
}



//-----------------------------------------------------------------------------------------------------------------------
#define CON_CAM_MIN_SPEED  100
#define CON_CAM_MAX_SPEED  300

#define CON_CAM_NORMAL_MOVEMENT  1600

void conUpdateCamera()
{
	VECTOR v, v2;
	int dist, tempInt, height;

	switch(con->cam.state)
	{
		case CON_CAMSTATE_ARRIVED:
			   
			//for debug really
			if((pReadPad->digital[0] & PAD_R1) || (pReadPad->digital[0] & PAD_R2))
				 return;

			if((pReadPad->analogXS[0] < -2000) ||(pReadPad->debounce[0] & PAD_LEFT))
			{
				con->cam.targetTool = (con->cam.targetTool == CON_NUM_TOOLS-1) ? 0:con->cam.targetTool+1;
			}
			else if((pReadPad->analogXS[0] > 2000)||(pReadPad->debounce[0] & PAD_RIGHT))
			{
				con->cam.targetTool = (con->cam.targetTool == 0) ? CON_NUM_TOOLS-1 : con->cam.targetTool-1;
			}
			else
			{
				break;
			}

			//DB("last  angle = %d\n", conCamAngle[con->cam.lastTool]);
			//DB(" target angle = %d\n", conCamAngle[con->cam.targetTool]);

			con->tools[con->cam.lastTool].state = CON_TOOL_STOPWORK;
			con->cam.state = CON_CAMSTATE_MOVING;
			SUBVECTOR(&v, &conCamPos[con->cam.targetTool], &con->cam.pos);// &conCamPos[con->cam.lastTool]);
			con->cam.distBetweenCamPos = Magnitude(&v);
			break;

		case CON_CAMSTATE_MOVING:

			SUBVECTOR(&v, &conCamPos[con->cam.targetTool], &con->cam.pos);
			
			dist = Magnitude(&v);

			ASSERT(dist);

			if(dist > CON_CAM_MIN_SPEED)
			{
				tempInt	= dist * CON_CAM_NORMAL_MOVEMENT / 4096;

				if(tempInt < CON_CAM_MIN_SPEED)
					tempInt = CON_CAM_MIN_SPEED;

				if(tempInt > CON_CAM_MAX_SPEED)
					tempInt = CON_CAM_MAX_SPEED;

				SCALEVECTOR(&v, ((4096*tempInt)/dist));

				dist -=tempInt;
			}
			else
			{
				con->cam.state = CON_CAMSTATE_ARRIVED;
				con->cam.lastTool = con->cam.targetTool;

				con->tools[con->cam.targetTool].state = CON_TOOL_PREPARE;
				dist = 0;
				break;
			}

			ADDTOVECTOR(&con->cam.pos, &v);

			// Now try moving garib exact same fraction of distance? so they arrive at precisely the same time???
			con->cam.garibPos.vx = (conGaribPos[con->cam.lastTool].vx * dist)/con->cam.distBetweenCamPos + (conGaribPos[con->cam.targetTool].vx * (con->cam.distBetweenCamPos - dist)) / con->cam.distBetweenCamPos;
			con->cam.garibPos.vy = (conGaribPos[con->cam.lastTool].vy * dist)/con->cam.distBetweenCamPos + (conGaribPos[con->cam.targetTool].vy * (con->cam.distBetweenCamPos - dist)) / con->cam.distBetweenCamPos;
			con->cam.garibPos.vz = (conGaribPos[con->cam.lastTool].vz * dist)/con->cam.distBetweenCamPos + (conGaribPos[con->cam.targetTool].vz * (con->cam.distBetweenCamPos - dist)) / con->cam.distBetweenCamPos;


			// find desired garib angle
			tempInt = (conCamAngle[con->cam.lastTool] * dist)/con->cam.distBetweenCamPos + (conCamAngle[con->cam.targetTool] * (con->cam.distBetweenCamPos - dist)) / con->cam.distBetweenCamPos;

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

			dist = Magnitude2D((con->cam.pos.vx - con->cam.garibPos.vx), (con->cam.pos.vz - con->cam.garibPos.vz));
			//height = fast_sqrt(con->cam.pos.vy - con->cam.garibPos.vy);

			height = (dist * 4096) / tempInt;
			
			//dist = dist/height;

			mycamera.vpx = con->cam.pos.vx;
			mycamera.vpy = con->cam.pos.vy;
			mycamera.vpz = con->cam.pos.vz;

			mycamera.vrx = con->cam.garibPos.vx;
			mycamera.vry = (con->cam.garibPos.vy + (2*(con->cam.pos.vy + height)))/3;
			mycamera.vrz = con->cam.garibPos.vz;

			break;

	}
}




//-----------------------------------------------------------------------------------------------------------------------
void constructControl()
{
	//db
	if(pReadPad->digital[0]&PAD_R1 || pReadPad->digital[0]&PAD_R2)
	{
		constructDebugMoveCamera();
		constructDebugMoveActor();
		return;
	}


}



//-----------------------------------------------------------------------------------------------------------------------
void constructPrintDebugText()
{
	char strBuff[128];


#ifdef CON_VIEW_POS

	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, -240, -120, strBuff, 128, 128, 0);

	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, "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 CON_VIEW_STATES

	sprintf(strBuff, "sawer state %d", con->tools[CON_SAWER].state);
	fontPrint(font, -240, -120, strBuff, 128, 128, 0);
	
	sprintf(strBuff, "hammer state %d", con->tools[CON_HAMMER].state);
	fontPrint(font, -240, -110, strBuff, 128, 128, 0);

	sprintf(strBuff, "spanner state %d", con->tools[CON_SPANNER].state);
	fontPrint(font, -240, -100, strBuff, 128, 128, 0);

	sprintf(strBuff, "tweedy state %d", con->tweedy.state);
	fontPrint(font, -240, -90, strBuff, 128, 128, 0);

	sprintf(strBuff, "rhythm state %d", con->rhythm.state);
	fontPrint(font, -240, -80, strBuff, 128, 128, 0);

	sprintf(strBuff, "game state %d", con->gameState);
	fontPrint(font, -240, -70, strBuff, 128, 128, 0);
	

#endif



}

//-----------------------------------------------------------------------------------------------------------------------
void conMoveCanAndGarib(VECTOR *v)
{

	mycamera.vpx += v->vx;
	mycamera.vpy += v->vy;
	mycamera.vpz += v->vz;
	            
	mycamera.vrx += v->vx;
	mycamera.vry += v->vy;
	mycamera.vrz += v->vz;
}


//-----------------------------------------------------------------------------------------------------------------------
void constructDebugMoveCamera()
{
	VECTOR v1, v2, v;
	


	if(pReadPad->digital[0] & PAD_R1)
	{
		v1.vx = mycamera.vpx;
		v1.vy = mycamera.vpy;
		v1.vz = mycamera.vpz;

		v2.vx = mycamera.vrx;
		v2.vy = mycamera.vry;
		v2.vz = mycamera.vrz;


		if(pReadPad->digital[0] & PAD_UP)
		{
			SUBVECTOR(&v, &v2, &v1);
			SCALEVECTOR(&v, 40);

			conMoveCanAndGarib(&v);
		}

		if(pReadPad->digital[0] & PAD_DOWN)
		{
			SUBVECTOR(&v, &v2, &v1);
			SCALEVECTOR(&v, -80);

			conMoveCanAndGarib(&v);
		}

		if(pReadPad->digital[0] & PAD_LEFT)
		{
			SUBVECTOR(&v, &v2, &v1);
			RotateVector2D(&v, &v, -16);

			ADDTOVECTOR(&v, &v1);
			
			mycamera.vrx = v.vx;
			mycamera.vry = v.vy;
			mycamera.vrz = v.vz;
		}

		if(pReadPad->digital[0] & PAD_RIGHT)
		{
			SUBVECTOR(&v, &v2, &v1);
			RotateVector2D(&v, &v, 16);

			ADDTOVECTOR(&v, &v1);
			
			mycamera.vrx = v.vx;
			mycamera.vry = v.vy;
			mycamera.vrz = v.vz;
		}

		if(pReadPad->digital[0] & PAD_TRIANGLE)
		{
			mycamera.vpy -= 20;
			mycamera.vry -= 20;
		}

		if(pReadPad->digital[0] & PAD_SQUARE)
		{
			mycamera.vpy += 20;
			mycamera.vry += 20;
		}

		if(pReadPad->digital[0] & PAD_CIRCLE)
		{
			mycamera.vry -= 20;
		}

		if(pReadPad->digital[0] & PAD_CROSS)
		{
			mycamera.vry += 20;
		}

		if(con->cam.state == CON_CAMSTATE_ARRIVED)
		{
			//set conCamPos and conGaribPos here//

			conCamPos[con->cam.targetTool].vx = mycamera.vpx;
			conCamPos[con->cam.targetTool].vy = mycamera.vpy;
			conCamPos[con->cam.targetTool].vz = mycamera.vpz;

			conGaribPos[con->cam.targetTool].vx = mycamera.vrx;
			conGaribPos[con->cam.targetTool].vy = mycamera.vry;
			conGaribPos[con->cam.targetTool].vz = mycamera.vrz;

		}

	}

}

//-----------------------------------------------------------------------------------------------------------------------
void constructDebugMoveActor()
{

	if(!myactor)
		return;


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


		if(pReadPad->analogYS[0] < -100)
		{
			myactor->position.vz -= pReadPad->analogYS[0]/160;
		}

		if(pReadPad->analogYS[0] > 100)
		{
			myactor->position.vz -= pReadPad->analogYS[0]/160;
		}

		if(pReadPad->analogXS[0] > 100)
		{	
			myactor->position.vx += pReadPad->analogXS[0]/160;
		}

		if(pReadPad->analogXS[0] < -100)
		{	
			myactor->position.vx += pReadPad->analogXS[0]/160;
		}

		if(pReadPad->digital[0] & PAD_TRIANGLE)
		{
			myactor->position.vy -= 10;
		}
		
		if(pReadPad->digital[0] & PAD_SQUARE)
		{
			myactor->position.vy += 10;
		}

		if(pReadPad->digital[0] & PAD_CIRCLE)
		{
			myactor->psiData.object->rotate.vy += 30;
		}

		if(pReadPad->digital[0] & PAD_CROSS)
		{
			myactor->psiData.object->rotate.vy -= 30;
		}
	}
}