/*	
	temp.vx=CamVars->oldVR.vx-CamVars->targetmodel->position.vx;
	temp.vy=CamVars->oldVR.vy-(CamVars->targetmodel->position.vy-gloveColl.radius/2048);
	temp.vz=CamVars->oldVR.vz-CamVars->targetmodel->position.vz;

	CamVars->camera.vrx-=temp.vx;
	CamVars->camera.vry=CamVars->targetmodel->position.vy-(gloveColl.radius/4096);
	CamVars->camera.vrz-=temp.vz;
*/


// Puzzle modification by Fred I (Fear bonus, for eg)
/*	if (CamVars->currentHeight > CamVars->requiredHeight+PuzzleVars.camera_additional_height+10)
		CamVars->currentHeight-=5;
	else if (CamVars->currentHeight < CamVars->requiredHeight+PuzzleVars.camera_additional_height-10)
		CamVars->currentHeight+=5;
*/

//	oldHeight=CamVars->requiredHeight;
/*
	temp.vx=CamVars->camera.vpx*4096;
	temp.vy=CamVars->camera.vpy*4096;
	temp.vz=CamVars->camera.vpz*4096;

	temp3[0]=getHeightAtSlow(temp.vx, temp.vy, temp.vz );
	temp3[1]=getHeightAtSlow(temp.vx, temp.vy, temp.vz+(20*4096) );
	temp3[2]=getHeightAtSlow(temp.vx, temp.vy, temp.vz-(20*4096) );
	temp3[3]=getHeightAtSlow(temp.vx+(20*4096), temp.vy, temp.vz );
	temp3[4]=getHeightAtSlow(temp.vx-(20*4096), temp.vy, temp.vz );
	
	CamVars->groundHeight=temp3[0];
	for (temp1=1;temp1<5;temp1++)
	{
		if (CamVars->groundHeight>temp3[temp1]) CamVars->groundHeight=temp3[temp1];
	}

	CamVars->groundHeight = (CamVars->groundHeight/4096)-10;
*/
/*
	if (CamVars->flags&ADDITIONALHEIGHT)
	{
		if (CamVars->currentHeight > CamVars->requiredHeight+CamVars->additionalHeight)
		{
			CamVars->currentHeight-=(CamVars->currentHeight - CamVars->requiredHeight+CamVars->additionalHeight)/2;
		}
		else if (CamVars->currentHeight < CamVars->requiredHeight+CamVars->additionalHeight)
			CamVars->currentHeight-=(CamVars->currentHeight - CamVars->requiredHeight+CamVars->additionalHeight)/2;
	}
	else
	{
		if (CamVars->currentHeight > CamVars->requiredHeight)
			CamVars->currentHeight-=(CamVars->currentHeight - CamVars->requiredHeight)/2;
		else if (CamVars->currentHeight < CamVars->requiredHeight)
			CamVars->currentHeight-=(CamVars->currentHeight - CamVars->requiredHeight)/2;
	}
*/
long getHeightAt(long x,long y,long z)
{
	long	x1,y1,z1,x2,y2,z2,value;//,diff1,diff2;
	COLLBOX	*pB;
	ULONG	collBoxPos;
	ULONG	*collptr,*collptr1;
	//VECTOR	d;
	DYNCOLLBOX *ptr;
	x2=x;
	y2=y;
	z2=z;
	x-=WorldBFFcoll->startX;
	y-=WorldBFFcoll->startY;
	z-=WorldBFFcoll->startZ;		   

	x1=x/WorldBFFcoll->xStepLen;
	y1=y/WorldBFFcoll->yStepLen;
	z1=z/WorldBFFcoll->zStepLen;

	if (x1<0 || x1>=WorldBFFcoll->xSteps) return(-1);	// Out of bounds can not hit anything
//	if (y1<0 || y1>=WorldBFFcoll->ySteps) return(-1);
	if (y1>=WorldBFFcoll->ySteps) return(-1);
	if (z1<0 || z1>=WorldBFFcoll->zSteps) return(-1);


	if (y1 < 0) y1 = 0;

	for(ptr = dyncollList.head.next; ptr != &dyncollList.head; ptr = ptr->next)
	{
		int l;

		if(!(ptr->active))
			continue;

		for(l=0; l<ptr->nCollBoxes; l++)
			getHeightOfOneBox((ptr->pBoxes)+l, &value, x2, y2, z2);
			//getHeightOfOneBox((pPlatsLoaded[ptr->platNumber].pBoxes)+l, &value, x2, y2, z2);

	}


#ifdef TESTALL
	for(collBoxPos = 0; collBoxPos < nMeshColl; collBoxPos++)
	{
		pB=pMeshColl+collBoxPos;
		collptr = (ULONG *)pB;

#else
	while(y1 < WorldBFFcoll->ySteps)
	{

	collBoxPos=( x1+(z1*WorldBFFcoll->xSteps)+(y1*(WorldBFFcoll->xSteps*WorldBFFcoll->zSteps)) );
	collptr=(ULONG *)reducedCollBox[collBoxPos];

	if (collptr==NULL) return(-1);	// No boxes in this voxel to test (this may need to change)

	if (((ULONG)collptr)<0x80000000)	// Check for one or >one
	{
		value=99999999;
		collptr=(ULONG *)(reducedCollBox[collBoxPos]);
		pB=(COLLBOX*)collptr;
#endif
		// Check if point can fall on box
		// Need to add checks for different types

		getHeightOfOneBox(pB, &value, x2, y2, z2);
		if(value != 99999999)
			return value;


/*
		if( (pB->flags&TYPEPART)==BOX )
		{
			if ( (x2>pB->pX) && (x2<(pB->pX+pB->sX)) ) // inside on x
			{
				if ( (z2>pB->pZ) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
				{
					return ( (pB->pY+pB->sY)-y2);
				}
			}
		}
		else if ( (pB->flags&TYPEPART)==RAMP )
		{
			if ( (x2>=pB->pX) && (x2<=(pB->pX+pB->sX)) ) // inside on x
			{
				if ( (z2>=(pB->pZ)) && (z2<=(pB->pZ+pB->sZ)) ) // inside on z
				{
					return(FindRampHeight(pB, x2, z2, y2));
				}
			}
		}
		else if ( (pB->flags&TYPEPART)==CYLINDER_X )
		{
			if ( (x2>pB->pX) && (x2<(pB->pX+pB->sX)) ) // inside on x
			{
				if ( (z2>(pB->pZ-pB->sZ)) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
				{
					diff1=(z2-pB->pZ);
					diff2=FindHeight(pB->sZ , diff1);
					return( (pB->pY-diff2)-y2);
				}
			}
		}
		else if ( (pB->flags&TYPEPART)==CYLINDER_Z )
		{
			if ( (z2>pB->pZ) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
			{
				if ( (x2>(pB->pX-pB->sX)) && (x2<(pB->pX+pB->sX)) ) // inside on x
				{
					diff1=(x2-pB->pX);
					diff2=FindHeight(pB->sX , diff1);
					return( (pB->pY-diff2)-y2);
				}
			}
		}
		else if ( (pB->flags&TYPEPART)==CYLINDER_Y )
		{
			if ( (x2>(pB->pX-pB->sX)) && (x2<(pB->pX+pB->sX)) ) // inside on x
			{
				if ( (z2>(pB->pZ-pB->sZ)) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
				{
					return ( (pB->pY+pB->sY)-y2);
				}
			}
		}
		else if ( (pB->flags&TYPEPART)==SPHERE )
		{
			if ( (x2>(pB->pX-pB->sX)) && (x2<(pB->pX+pB->sX)) ) // inside on x
			{
				if ( (z2>(pB->pZ-pB->sZ)) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
				{
					d.vx=x2-pB->pX;
					d.vz=z2-pB->pZ;
					d.vy=0;
					diff1=Magnitude(&d);
					diff2=FindHeight(pB->sX , diff1);
					return( (pB->pY-diff2)-y2);
				}
			}
		}

		else return(-1);
*/

	}

#ifndef TESTALL

	else
	{	// multiple boxes to deal with
		value=99999999;

		collptr = (ULONG *)(  ((ULONG)(collptr)) & 0x7fffffff);	// mask off collptr's top bit
		collptr1= (ULONG *)(*collptr);

		do	// the "none" case has already been dealt with
		{
			//collptr1=*collptr;
			pB=(COLLBOX*)collptr1;
			// Check if point can fall on box
			// Need to add checks for different types

			getHeightOfOneBox(pB, &value, x2, y2, z2);
/*
			if( (pB->flags&TYPEPART)==BOX )
			{
				if ( (x2>pB->pX) && (x2<(pB->pX+pB->sX)) ) // inside on x
				{
					if ( (z2>pB->pZ) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
					{
						//return ( (pB->pY+pB->sY)-y2);
						if (value > ((pB->pY+pB->sY)-y2) ) value=(pB->pY+pB->sY)-y2;
					}
				}
			}
			else if ( (pB->flags&TYPEPART)==RAMP )
			{
				if ( (x2>=pB->pX) && (x2<=(pB->pX+pB->sX)) ) // inside on x
				{
					if ( (z2>=(pB->pZ)) && (z2<=(pB->pZ+pB->sZ)) ) // inside on z
					{
						int ht=FindRampHeight(pB, x2, z2, y2);
						if(value>ht) value=ht;
						//return(FindRampHeight(pB, x2, z2, y2));
					}
				}
			}
			else if ( (pB->flags&TYPEPART)==CYLINDER_X )
			{
				if ( (x2>=pB->pX) && (x2<=(pB->pX+pB->sX)) ) // inside on x
				{
					if ( (z2>=(pB->pZ-pB->sZ)) && (z2<=(pB->pZ+pB->sZ)) ) // inside on z
					{
						diff1=(z2-pB->pZ);
						diff2=FindHeight(pB->sZ , diff1);
						//return( (pB->pY+diff2)-y2);
						if (value > ( (pB->pY-diff2)-y2) ) value=( (pB->pY-diff2)-y2);
					}
				}
			}
			else if ( (pB->flags&TYPEPART)==CYLINDER_Z )
			{
				if ( (z2>pB->pZ) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
				{
					if ( (x2>(pB->pX-pB->sX)) && (x2<(pB->pX+pB->sX)) ) // inside on x
					{
						diff1=(x2-pB->pX);
						diff2=FindHeight(pB->sX , diff1);
						if (value > ( (pB->pY-diff2)-y2) ) value=( (pB->pY-diff2)-y2);
					}
				}
			}
			else if ( (pB->flags&TYPEPART)==CYLINDER_Y )
			{
				if ( (x2>(pB->pX-pB->sX)) && (x2<(pB->pX+pB->sX)) ) // inside on x
				{
					if ( (z2>(pB->pZ-pB->sZ)) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
					{
						if (value > ( (pB->pY+pB->sY)-y2) ) value=( (pB->pY+pB->sY)-y2);
					}
				}
			}
			else if ( (pB->flags&TYPEPART)==SPHERE )
			{
				if ( (x2>(pB->pX-pB->sX)) && (x2<(pB->pX+pB->sX)) ) // inside on x
				{
					if ( (z2>(pB->pZ-pB->sZ)) && (z2<(pB->pZ+pB->sZ)) ) // inside on z
					{
						d.vx=x2-pB->pX;
						d.vz=z2-pB->pZ;
						d.vy=0;
						diff1=Magnitude(&d);
						diff2=FindHeight(pB->sX , diff1);
						if (value > ( (pB->pY-diff2)-y2) ) value=( (pB->pY-diff2)-y2);
					}
				}
			}
*/

			collptr++;
			collptr1=(ULONG *)(*collptr);
		}
		while (collptr1!=NULL);

//		if (value==99999999) return(-1);
//		else return(value);
		if(value != 99999999)
			return value;
	}
	y1++;
	}
#endif
	return(-1);
}


/*


		SubVector(&v1,&vA,&vS);
		SubVector(&v2,&vB,&vS);

		ScaleVector(&v1, 256);
		ScaleVector(&v2, 256);

		CrossProduct(&n, &v1, &v2);
		MakeUnit(&n);

		for(int i=0; i<3; i++)
			collBox[bCtr].d[i]=n.v[i];

		AddVector(&a, &vS, (FVECTOR *)&(collBox[bCtr].pX));
		collBox[bCtr].d[3]=-DotProduct(&n, &a);



int collboxHasBallHitRamp(COLLDATA *pD, COLLBOX *pB)
{
	long dist;
	VECTOR temp, contact;
	VECTOR pN;
	long j=pB->d[3];
	long pX=pB->pX, pY=pB->pY, pZ=pB->pZ;
	long sX=pB->sX, sY=pB->sY, sZ=pB->sZ;

//	SubVector(&temp, pD->pPos, (FVECTOR *)&(pB->pX));

	dist=-((DotProduct(pN, pD->pPos))+j);
	//PRINTNUM(dist);
	if((dist+(pD->radius))<0)
		return(FALSE);
	// we must have hit the ramp
	// so, we must find out where
	CopyVector(&temp, pN);
	ScaleVector(&temp, dist);
	AddVector(&(pD->hitPos), pD->pPos, &temp);

	if(((pD->hitPos.v[0])>=pX) && ((pD->hitPos.v[0])<(pX+sX)) &&
		((pD->hitPos.v[1])>=pY) && ((pD->hitPos.v[1])<(pY+sY)) &&
		((pD->hitPos.v[2])>=pZ) && ((pD->hitPos.v[2])<(pZ+sZ)))
	{
		// interpolate back up to allow for radius
		CopyVector(&temp, pN);
		ScaleVector(&temp, (pD->radius));
		AddToVector(&(pD->hitPos), &temp);

		return(TRUE);
	}
	else
		return(FALSE);
}
*/

/*
int collboxHasVectorCrossedPoly(COLLDATA *pD, int plane, int planePos, int r, int x1, int y1, int x2, int y2)
{
	long xDist,yDist,zDist;
	long xInt,yInt,zInt;
	long xPos,xNeg,yPos,yNeg,zPos,zNeg, div;
	FVECTOR *oldP=&pD->oldPos;
	FVECTOR *newP=pD->pPos;
	FVECTOR *hitP=&pD->hitPos;
	int bHit=FALSE;

	switch(plane)
	{
	case D_XZPLANE:
	case U_XZPLANE:
		// work out distance from initial point to XZ plane
		yPos=(oldP->v[1])-(planePos+r);
		yNeg=(newP->v[1])-(planePos+r);
		//printf("yAbove=%d, yBelow=%d\n", yPos/4096, yNeg/4096);
		
		if((yPos^yNeg)&0x80000000)
		{	// signs are different, crossing plane
			//printf("Different ...\n");
			xDist=(newP->v[0])-(oldP->v[0]);
			yDist=(newP->v[1])-(oldP->v[1]);
			zDist=(newP->v[2])-(oldP->v[2]);

			div=(yPos*4096)/yDist;
			xInt=((xDist*div)/4096)+(oldP->v[0]);
			zInt=((zDist*div)/4096)+(oldP->v[2]);

			// is it inside our bounds?
			if((xInt>=x1) && (xInt<x2) && (zInt>=y1) && (zInt<y2))
			{
				bHit=TRUE;
				hitP->v[0]=xInt;
				hitP->v[1]=planePos+r;
				hitP->v[2]=zInt;
			}
			//printf("x=%d-%d (%d), y=%d-%d (%d)\n", x1,x2,xInt, y1,y2,zInt);
		}
		break;

	case D_YZPLANE:
	case U_YZPLANE:
		// work out distance from initial point to YZ plane
		xPos=(oldP->v[0])-(planePos+r);
		xNeg=(newP->v[0])-(planePos+r);
		//printf("yAbove=%d, yBelow=%d planePos=%d\n", xPos/4096, xNeg/4096, planePos/4096);

		if((xPos^xNeg)&0x80000000)
		{	// signs are different, crossing plane
			//printf("Different ...\n");
			xDist=(newP->v[0])-(oldP->v[0]);
			yDist=(newP->v[1])-(oldP->v[1]);
			zDist=(newP->v[2])-(oldP->v[2]);

			div=(xPos*4096)/xDist;
			yInt=((yDist*div)/4096)+(oldP->v[1]);
			zInt=((zDist*div)/4096)+(oldP->v[2]);

			// is it inside our bounds?
			if((yInt>=x1) && (yInt<x2) && (zInt>=y1) && (zInt<y2))
			{
				bHit=TRUE;
				hitP->v[0]=planePos+r;
				hitP->v[1]=yInt;
				hitP->v[2]=zInt;
			}
			//printf("x=%d-%d (%d), y=%d-%d (%d)\n", x1,x2,yInt, y1,y2,yInt);
		}
		break;

	case D_XYPLANE:
	case U_XYPLANE:
		// work out distance from initial point to YZ plane
		zPos=(oldP->v[2])-(planePos+r);
		zNeg=(newP->v[2])-(planePos+r);
		//printf("yAbove=%d, yBelow=%d planePos=%d\n", xPos/4096, xNeg/4096, planePos/4096);

		if((zPos^zNeg)&0x80000000)
		{	// signs are different, crossing plane
			//printf("Different ...\n");
			xDist=(newP->v[0])-(oldP->v[0]);
			yDist=(newP->v[1])-(oldP->v[1]);
			zDist=(newP->v[2])-(oldP->v[2]);

			div=(zPos*4096)/zDist;
			yInt=((yDist*div)/4096)+(oldP->v[1]);
			xInt=((xDist*div)/4096)+(oldP->v[0]);

			// is it inside our bounds?
			if((xInt>=x1) && (xInt<x2) && (yInt>=y1) && (yInt<y2))
			{
				bHit=TRUE;
				hitP->v[0]=xInt;
				hitP->v[1]=yInt;
				hitP->v[2]=planePos+r;
			}
		}
	}
	return(bHit);
}

*/

// Allthrought this version has bugs it allows the ball to run round the inside of a sphere
/*
oid	UpdateSphereCollision(COLLDATA *pD , HITDATA *pHitData , VECTOR *collPos , VECTOR *rebound)
{
	long	deltaX,deltaY,deltaZ,mag;
	VECTOR	objectmove,normal;	// changed from FVECTOR

	if (PRINTHITPOS) printf ("Hit sphere\n");
	deltaX=( (pHitData->pos.vx) - (pD->pPos->vx) );
	deltaY=( (pHitData->pos.vy) - (pD->pPos->vy) );
	deltaZ=( (pHitData->pos.vz) - (pD->pPos->vz) );

	// This should give a normal for bounce
	normal.vx=-deltaX;
	normal.vy=deltaY;
	normal.vz=-deltaZ;
	MakeUnit(&normal);

	objectmove.vx=normal.vx;
	objectmove.vy=normal.vy;
	objectmove.vz=normal.vz;

	//normal.vz=-normal.vz;
	//normal.vx=-normal.vx;

	ScaleVector(&objectmove,(pD->radius+1) );

	pD->pPos->vx=pD->oldPos.vx=pHitData->pos.vx+(objectmove.vx);
	pD->pPos->vy=pD->oldPos.vy=pHitData->pos.vy-(objectmove.vy);
	pD->pPos->vz=pD->oldPos.vz=pHitData->pos.vz+(objectmove.vz);

	mag=Magnitude(pD->pVel);
	CopyVector(rebound, pD->pVel);
	CalcBounce(rebound, &normal);
	ScaleVector(rebound,mag);
}
*/

/*
	STARTPOLY;
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz+pD->radius);
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz+pD->radius);
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz+pD->radius);
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz+pD->radius);
	DRAWPOLY(255,255,255);

	STARTPOLY;
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz-pD->radius);
	DRAWPOLY(255,255,255);

	STARTPOLY;
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz+pD->radius);
	DRAWPOLY(255,255,255);

	STARTPOLY;
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx-pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz+pD->radius);
	DRAWPOLY(255,255,255);

	STARTPOLY;
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy-pD->radius, pD->pPos->vz+pD->radius);
	DRAWPOLY(255,255,255);

	STARTPOLY;
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz-pD->radius);
	ADDVERT(pD->pPos->vx+pD->radius, pD->pPos->vy+pD->radius, pD->pPos->vz+pD->radius);
	DRAWPOLY(255,255,255);
*/


























/*
void temp(void)
{
	// now work out direction
	if (temp && GloveCtrl.action!=HAND_LOCATE )//&& GloveCtrl.action!=HAND_BOUNCE)
	{
		temp=calc_angle( leftRight,upDown );
		targetAngle=(temp+CamVars.angle)&4095;

		temp = findShortestAngle(GloveCtrl.direction,targetAngle);
		if ( (abs(temp)>1536 && abs(temp)<2560) && GloveCtrl.speed>SKIDSPEED && GloveCtrl.action==HAND_WALK)
		{
			if (ActionAllowed(GloveCtrl.action,HAND_SKIDDING))
			{
				GloveCtrl.targetSpeed=0;	// stop glove if direction reversed
				GloveCtrl.action=HAND_SKIDDING;
				GloveCtrl.active=FALSE;
				GloveBehaviour.accel=4096*8;
			}
		}
		else
		{
			//GloveCtrl.direction=targetAngle;
			if (temp>0)
			{
				if ( temp>ROTATESPEED )
				{
					GloveCtrl.direction-=ROTATESPEED;
					if (GloveCtrl.action==HAND_WALK) pGlovePSA->world.rotate.vz = (LEANANGLE*GloveCtrl.speed)/TOPSPEED;
				}
				else 
				{
					GloveCtrl.direction=targetAngle;
					if (GloveCtrl.action==HAND_WALK) pGlovePSA->world.rotate.vz =  (((temp*LEANANGLE)/ROTATESPEED)*GloveCtrl.speed)/TOPSPEED;
					//(( (temp*4096)/ROTATESPEED )*LEANANGLE)/4096;
				}
			}
			else if (temp<0)
			{
				if ( temp<-ROTATESPEED )
				{
					GloveCtrl.direction+=ROTATESPEED;
					if (GloveCtrl.action==HAND_WALK) pGlovePSA->world.rotate.vz = -(LEANANGLE*GloveCtrl.speed)/TOPSPEED;
				}
				else 
				{
					GloveCtrl.direction=targetAngle;
					if (GloveCtrl.action==HAND_WALK) pGlovePSA->world.rotate.vz = - (((temp*LEANANGLE)/ROTATESPEED)*GloveCtrl.speed)/TOPSPEED;
				}
			}
		}
	}
	// now work out new speed

/*	if (GloveCtrl.speed<(TOPSPEED/2) && (GloveCtrl.action!=HAND_WALK && GloveCtrl.walkType!=HANDWALK_WALK) )
	{
		AddToQueue(&Glover,HANDANIM_JOG,YES,NO,4096);
		GloveCtrl.action=HAND_WALK;
		GloveCtrl.walkType=HANDWALK_WALK;
		GloveCtrl.lastAction=GloveCtrl.action;
	}
	else if (GloveCtrl.action!=HAND_WALK && GloveCtrl.walkType!=HANDWALK_RUN) 
	{
		AddToQueue(&Glover,HANDANIM_RUN,YES,NO,4096);
		GloveCtrl.action=HAND_WALK;
		GloveCtrl.walkType=HANDWALK_RUN;
		GloveCtrl.lastAction=GloveCtrl.action;
	}
*/
/*
	GloveCtrl.oldSpeed=GloveCtrl.speed;
	if (GloveCtrl.speed<GloveCtrl.targetSpeed)
	{
		if ( GloveCtrl.speed<(GloveCtrl.targetSpeed+(GloveBehaviour.accel/4096)) ) 
			GloveCtrl.speed+=GloveBehaviour.accel/4096;
		else 
			GloveCtrl.speed=GloveCtrl.targetSpeed;
	}
	else if (GloveCtrl.speed>GloveCtrl.targetSpeed)
	{
		if ( GloveCtrl.speed>(GloveCtrl.targetSpeed+(GloveBehaviour.accel/4096)) )
			GloveCtrl.speed-=GloveBehaviour.accel/4096;
		else 
			GloveCtrl.speed=GloveCtrl.targetSpeed;
	}


	GloveCtrl.direction&=4095;
	if (GloveCtrl.action!=HAND_BOUNCE)
	{
		gloveVel.vz=/-(rcos(GloveCtrl.direction)*GloveCtrl.speed)/16;
		gloveVel.vx=/-(rsin(GloveCtrl.direction)*GloveCtrl.speed)/16;
	}
	else
	{
		ballVel.vz=/-(rcos(GloveCtrl.direction)*GloveCtrl.speed)/16;
		ballVel.vx=/-(rsin(GloveCtrl.direction)*GloveCtrl.speed)/16;
	}
	pGlovePSA->world.rotate.vy=GloveCtrl.direction;
}
*/




// stuff from lscape


/*	if (pad[0]&PAD_CIRCLE){
		printf("change cam\n");		
		oldcamx=CamVars.camera.vpx;
		oldcamy=CamVars.camera.vpy;
		oldcamz=CamVars.camera.vpz;

	   	normal.vx=normal.vy=normal.vz=4096*32;
		ScaleMatrix(&matrix, &normal);

		normal.vx=CamVars.camera.vpx-CamVars.camera.vrx;
		normal.vy=CamVars.camera.vpy-CamVars.camera.vry;
		normal.vz=CamVars.camera.vpz-CamVars.camera.vrz;
		MakeUnit(&normal);
		normal.vx/=4;
		normal.vy/=4;
		normal.vz/=4;


		CamVars.camera.vpx+=normal.vx;
		CamVars.camera.vpy+=normal.vy;
		CamVars.camera.vpz+=normal.vz;
		GsSetRefView2(&CamVars.camera);

		CamVars.camera.vpx=oldcamx;
		CamVars.camera.vpy=oldcamy;
		CamVars.camera.vpz=oldcamz;


	}
*/

/**************************************************************/
/*void	StopCards(void)
{
	MemCardStop();
}
*/

/**************************************************************/
/*
long	GetCardInfo(long port,long card)
{
	long	channel,info;

	channel=port*16+card;	// card should always=0 execpt for multi tap
	//info=_card_info(channel);
	info=MemCardAccept(channel);
	return(info);
}
*/

/**************************************************************/

//typedef long MEMCARD(long channel);

//long	channel,info1;
//long	result,cmds,test;

/*
long	TestCardExist(long port,long card)
{
	long	channel,info1;
	long	result,cmds,test;

	channel=(port*16)+card;	// card should always=0 execpt for multi tap

	//channel=0;
	cmds=0;

	test=MemCardAccept( channel );
	//test=MemCardExist( channel );
	if (test==1) printf ("registered ok\n");
	else printf ("register failed\n");

	/* wait for MemCardAccept to finish */
//	test=99;
//	while (test!=1)
//	{
//		test=MemCardSync(1,&cmds,&result);
//	}
/*
	MemCardSync(0,&cmds,&result);

	CardCommand(cmds);

	return(result);

}
*/

/**************************************************************/
/*
void	CardResults(long results)
{

	switch (results)
	{
		case	McErrNone:
			printf ("Card connected\n");
			break;

		case	McErrCardNotExist:
			printf ("No card found\n");
			break;

		case	McErrCardInvalid:
			printf ("Bad card\n");
			break;

		case	McErrNewCard:
			printf ("new card found or card changed\n");
			break;

		case	McErrNotFormat:
			printf ("Card not formated\n");
			break;

		default:
			printf ("Somethings wrong\n");
			break;
	}
}
*/

/**************************************************************/
/*
void	CardCommand(long cmds)
{
	switch (cmds)
	{
		case	McFuncExist:
			printf ("Command MemCardExist\n");
			break;

		case	McFuncAccept:
			printf ("Command MemCardAccept\n");
			break;

		case	McFuncReadFile:
			printf ("Command MemCardReadFile\n");
			break;

		case	McFuncWriteFile:
			printf ("Command MemCardWriteFile\n");
			break;

		case	McFuncReadData:
			printf ("Command MemCardReadData\n");
			break;

		case	McFuncWriteData:
			printf ("Command MemCardWriteData\n");
			break;

		default:
			printf ("Unknown command ERROR\n");
			break;
	}
}
*/

/**************************************************************/
/*
void	CardtestRoutine(void)
{
long	cmds,rslt,chan;


	MemCardInit(1);
	MemCardStart();

	chan = 0x00;

	while(1) {
		switch( MemCardSync( 1, &cmds, &rslt)) {


			case -1:
			MemCardExist( chan );
			break;


			case 0:
			break;


			case 1:
			switch(cmds) {
				case McFuncExist:
				switch(rslt) {
					case	McErrNone:
						printf ("Card connected\n");
						break;

					case	McErrCardNotExist:
						printf ("No card found\n");
						break;

					case	McErrCardInvalid:
						printf ("Bad card\n");
						break;

					case	McErrNewCard:
						printf ("new card found or card changed\n");
						MemCardAccept(chan);
						break;

					case	McErrNotFormat:
						printf ("Card not formated\n");
						break;

					default:
						printf ("Somethings wrong\n");
						break;
				}
				break;
				case McFuncAccept:
				switch(rslt) {
					case McErrNone:
					case McErrNewCard:

					//MemCardGetDirentry( chan, "*", fileList, &files, 0, BLOCK_MAX );
					//for( i=0, n=0; i<files; i++ )
					//	n += fileList[i].size/8192 + (fileList[i].size%8192 ? 1:0);
					//sprintf( mes, "%d file(s) %d block(s)\n", (int)files, (int)n );
					break;
					case McErrNotFormat:
					printf ("NOT FORMAT\n" );
					break;
					default:
					printf ("ERROR(%d)\n", (int)rslt );
					break;
				}
				break;
			}
			break;
		}
	}
	MemCardStop();
	MemCardEnd();
}

*/


	//printf ("setting to digital\n");
	//while (PadGetState(0)!=PadStateStable);

	//PadSetMainMode(0,1,1);	// set to digital ?
	//while (PadGetState(0)!=PadStateStable);

	//PadSetMainMode(0,0,3);	// set to digital ?

	//printf ("digital set\n");
	
	//VSync(50);		// Dos'nt work unless this is done (alway prints an error)	

	//printf ("stopping pad\n");
	//while (PadGetState(0)!=PadStateStable);
	//PadStopCom();	// Stop pad
	//printf ("pad stopped\n");

	//while (PadGetState(0)!=PadStateStable);
	//PadSetMainMode(0,1,2);	// set to digital ?



	//while (PadGetState(0)!=PadStateStable);
	
	//printf ("startting pad\n");
	//VSync(0);		// Dos'nt work unless this is done (alway prints an error)
	//PadInit(0);
	//VSync(0);		// Dos'nt work unless this is done (alway prints an error)
	//printf ("pad started\n");















		//Start_Loop();
		//MemCardDisplay();

		

		//sprctrl.scalex=4096;//2048;
		//sprctrl.scaley=4096;//2048;
		//textSetPos(32,10);
		//sprctrl.r=128;
		//sprctrl.g=64;
		//sprctrl.b=64;

		//textPrintf( "MEMORY CARD CONTROL \n" );

		//textSetPos(32,30);
		//textPrintf( "CARD 1 \n" );

		//textSetPos(256,30);
		//textPrintf( "CARD 0 \n" );

		//sprctrl.scalex=2048;
		//sprctrl.scaley=2048;

		//printf ("get status\n");
		//result0=getCardStatus(0x00);	// test card0
		//printf ("got status\n");
		//textSetPos(256,50);
		//textPrintf( "CARD STATUS %s",cardStatus[result0] );

		//if (result0==McErrNone)
		//{
		//	MemCardGetDirentry( 0x00, "*", fileList[0], &files[0], 0, BLOCK_MAX );
		//	MemCardSync(0,0,&result0);
		//}

		//MemCardUnformat(0); 	// Used to blank card DO NOT USE IN FINISHED GAME
		//MemCardSync( 0, 0, &result0);


		//result1=getCardStatus(0x10);	// test card1
		//textSetPos(32,50);
		//textPrintf( "CARD STATUS %s",cardStatus[result1] );

		//if (result1==McErrNone)
		//{
		//	MemCardGetDirentry( 0x10, "*", fileList[1], &files[1], 0, BLOCK_MAX );
		//	MemCardSync(0,0,&result1);
		//}
/*		getCardStatus(&result0,&result1);
				
		if (result0==McErrNone || result1==McErrNone)
		{
			readCardDir(&result0,&result1);
		}
*/

/*
		if (result0==McErrCardNotExist && result1==McErrCardNotExist) // no cards pluged in
		{
			//MemCardStop();	// Stop memory card file system
			//PadStartCom();	// Reenable pad
			noCardScreen();
			//PadStopCom();	// Stop pad
			//VSync(0);		// Dos'nt work unless this is done (alway prints an error)
			//MemCardStart();	// Start memory card file system
		}
		else
		{
			//MemCardStop();	// Stop memory card file system
			//PadStartCom();	// Reenable pad
			activeCard=selectCardScreen(result0,result1);
			//PadStopCom();	// Stop pad
			//VSync(0);		// Dos'nt work unless this is done (alway prints an error)
			//MemCardStart();	// Start memory card file system
		}
		if (activeCard==CARD0)
		{
			//result0=getCardStatus(0x00);	// test card0
			if (result0==McErrNotFormat)
			{
				//MemCardStop();	// Stop memory card file system
				//PadStartCom();	// Reenable pad
				formatCardScreen(0x00);
				//PadStopCom();	// Stop pad
				//VSync(0);		// Dos'nt work unless this is done (alway prints an error)
				//MemCardStart();	// Start memory card file system
			}
		}
		//End_Loop(framerate);
		*/







long PadRead2( short port )
{
	static long repcnt = 0;
	static long padrec = 0;
	long	pad, trgdata, repdata;

	pad = PadRead( port );
	trgdata = pad &(~padrec);

	repdata = pad&padrec;
	if( pad && pad==padrec ) {
		if( ++repcnt >=15 ) {
			trgdata = pad;
			repcnt -= 2;
		}
	} else	repcnt = 0;

	padrec = pad;
	return( trgdata );
}



/*	--------------------------------------------------------------------------------
	Function 	: selectCardScreen
	Purpose 	: If more than one card selects which card to use
	Parameters 	: results from cards 0 and 1
	Returns 	: card to use
	Info 		: 
	--------------------------------------------------------------------------------*/


/*
long	selectCardScreen(long result0,long result1)
{
long	exitFlag=FALSE,cardNum,selectpos,leftRight,prev,loop,pad;

	selectpos=0;

	if (result0==McErrCardNotExist) return(CARD1);
	if (result1==McErrCardNotExist) return(CARD0);
	printf("select start\n");
	while( !exitFlag )
	{
		Start_Loop();

		MemCardDisplay();
		messctrl.gapx=-10;

		sprctrl.scalex=4096;//2048;
		sprctrl.scaley=4096;//2048;
		//messctrl.gapx=0;
		textSetPos(32,10);
		sprctrl.r=64;
		sprctrl.g=64;
		sprctrl.b=255;
		textPrintf( "SELECT CARD TO USE \n" );

		sprctrl.r=128;
		sprctrl.g=64;
		sprctrl.b=64;

		textSetPos(160,230);
		textPrintf( "PRESS CIRCLE\nTO CONTINUE\n" );

		textSetPos(32,30);
		textPrintf( "CARD 1 \n" );

		textSetPos(256,30);
		textPrintf( "CARD 0 \n" );

		sprctrl.scalex=2048;
		sprctrl.scaley=2048;
		

		textSetPos(256,50);
		textPrintf( "CARD STATUS %s",cardStatus[result0] );

		for (loop=0;loop<15;loop++)
		{
			textSetPos(32,60+(loop*10));
			textPrintf( "File %d name %s",loop,fileList[1][loop].name);
		}

		textSetPos(32,50);
		textPrintf( "CARD STATUS %s",cardStatus[result1] );

		for (loop=0;loop<15;loop++)
		{
			textSetPos(256,60+(loop*10));
			textPrintf( "File %d name %s",loop,fileList[0][loop].name);
		}

		pad=PadRead2(0);
		printf ("pad=%d \n");
		if (pad&PAD_LEFT || pad&PAD_RIGHT) leftRight=TRUE;
		//leftRight=(nleftx[0]);
		if (leftRight && prev==0)
		{
			selectpos++;
			prev=1;
		}
		if (!leftRight) prev=0;

		if (selectpos==2) selectpos=0;

		if (selectpos==0) textSetPos(16,50);
		else textSetPos(240,50);

		textPrintf( "*" );

		End_Loop(framerate);
		if ((pad&PAD_CIRCLE) ) exitFlag=TRUE;
	}
	printf ("select end\n");
	if (selectpos==0) return(CARD1);
	else return (CARD0);
}


*/


/*	--------------------------------------------------------------------------------
	Function 	: formatCardScreen
	Purpose 	: Handles promts for formating
	Parameters 	: card 0 or 1
	Returns 	: 
	Info 		: 
	--------------------------------------------------------------------------------*/
/*
void	formatCardScreen(long channel)
{
long	exitFlag=FALSE,selectpos,leftRight,prev,result;

	selectpos=1;

	while( !exitFlag )
	{
		Start_Loop();

		MemCardDisplay();

		sprctrl.scalex=4096;//2048;
		sprctrl.scaley=4096;//2048;
		textSetPos(32,10);
		sprctrl.r=64;
		sprctrl.g=64;
		sprctrl.b=255;
		textPrintf( "THIS CARD IS NOT FORMATTED\nDO YOU WISH TO FORMAT ?\n" );
		textSetPos(32,200);
		textPrintf( "PRESS TRIANGLE TO SELECT\n" );

		sprctrl.r=128;
		sprctrl.g=64;
		sprctrl.b=64;
		textSetPos(32,50);
		textPrintf( "YES \n" );

		textSetPos(256,50);
		textPrintf( "NO \n" );

		leftRight=(nleftx[0]);
		if (leftRight && prev==0)
		{
			selectpos++;
			prev=1;
		}
		if (!leftRight) prev=0;

		if (selectpos==2) selectpos=0;

		if (selectpos==0) textSetPos(16,50);
		else textSetPos(240,50);

		textPrintf( "*" );


		End_Loop(framerate);
		if ((pad[0]&PAD_TRIANGLE) ) exitFlag=TRUE;
	}
	if (selectpos==0) // yes selected
	{
		PadStopCom();	// Stop pad
		VSync(0);		// Dos'nt work unless this is done (alway prints an error)
		MemCardStart();	// Start memory card file system

		Start_Loop();
		MemCardDisplay();

		sprctrl.scalex=4096;//2048;
		sprctrl.scaley=4096;//2048;
		textSetPos(32,10);
		sprctrl.r=64;
		sprctrl.g=64;
		sprctrl.b=255;
		textPrintf( "FORMATTING CARD" );
		End_Loop(framerate);

		//result=getCardStatus(channel);
		if (result==McErrNotFormat)
		{
			MemCardFormat(channel);
			MemCardSync( 0, 0, &result);


			if (result!=McErrNone)
			{
				Start_Loop();
				MemCardDisplay();

				sprctrl.scalex=4096;//2048;
				sprctrl.scaley=4096;//2048;
				textSetPos(32,10);
				sprctrl.r=64;
				sprctrl.g=64;
				sprctrl.b=255;
				textPrintf( "ERROR WHEN FORMATTING" );
				End_Loop(framerate);
			}
		}
		else
		{
			Start_Loop();
			MemCardDisplay();

			sprctrl.scalex=4096;//2048;
			sprctrl.scaley=4096;//2048;
			textSetPos(32,10);
			sprctrl.r=64;
			sprctrl.g=64;
			sprctrl.b=255;
			textPrintf( "ERROR CANT FORMAT" );
			End_Loop(framerate);
		}
		MemCardStop();	// Stop memory card file system
		PadStartCom();	// Reenable pad
	}
}

*/


/*	--------------------------------------------------------------------------------
	Function 	: getCardStatus
	Purpose 	: Finds current status of card
	Parameters 	: Port for card (0 or 16)
	Returns 	: current stats of card
	Info 		: 
	--------------------------------------------------------------------------------*/
/*
long	getCardStatus(long	*res0,long	*res1)
{
long	cmds,exitFlag,result,exitCount;
long	channel;
UBYTE	card=0;
UBYTE	exitSwitch=0;
	*res0=1;
	*res1=1;
	channel=0;	// card 0

	messctrl.font=fontList[1];
	spriteReset();
	sprctrl.stretch=1;

	cmds=NULL;
	exitFlag=1;
	exitCount=50;

	MemCardExist( channel );	// check for card
	while (exitFlag)
	{
		switch( MemCardSync(1,&cmds,&result) )
		{
			case -1:	// First time no cmds exectuted
			MemCardExist( channel );	// check for card
			break;

			case 0:		// Async command in progress
			break;

			case 1:		// Async command finished
			switch(cmds)
			{
				case McFuncExist:
					//textSetPos(32,50);
					MemCardAccept(channel);
					break;

				case McFuncAccept:
					//if (result!=McErrNewCard) exitFlag=0;
					if (card==0) *res0=result;
					if (card==1) *res1=result;
					if (card==0)
					{
						card = 1;
						channel = 0x10;	// set for card 1
						MemCardExist( channel );	// check for card
					}
					else
					{
						card = 0;
						channel = 0;	// set for card 0
						MemCardExist( channel );	// check for card

						if( (*res0!=McErrNewCard) && (*res1!=McErrNewCard) ) exitSwitch=1;
					}
					//else MemCardAccept(channel);
					break;
			}
			default:
		}
		if (exitSwitch) exitCount--;
		if (exitCount==0) exitFlag=0;

		Start_Loop();
		backDisplay(cardAngle);
		MemCardDisplay();

		sprctrl.scalex=4096;//2048;
		sprctrl.scaley=4096;//2048;
		textSetPos(32,10);
		sprctrl.r=128;
		sprctrl.g=64;
		sprctrl.b=64;
		textPrintf( "MEMORY CARD STATUS \n" );

		textSetPos(32,30);
		textPrintf( "CARD 1 \n" );

		textSetPos(256,30);
		textPrintf( "CARD 0 \n" );

		sprctrl.scalex=2048;
		sprctrl.scaley=2048;

		textSetPos(256,50);
		textPrintf( "CARD STATUS \n");
		textSetPos(256,60);
		textPrintf( "%s \n",cardStatus[*res0] );

		textSetPos(32,50);
		textPrintf( "CARD STATUS \n");
		textSetPos(32,60);
		textPrintf( "%s \n",cardStatus[*res1] );

		//if (*res0==McErrNone

		cardAngle+=16;
		if (cardAngle>4096) cardAngle-=4096;
		End_Loop(framerate);
	}	

	exitFlag=1;

	while (exitFlag)
	{
		switch( MemCardSync(1,&cmds,&result) )
		{
			case -1:	// First time no cmds exectuted
			break;

			case 0:		// Async command in progress
			break;

			case 1:		// Async command finished
			exitFlag=0;
			break;
		}
		Start_Loop();
		backDisplay(cardAngle);
		MemCardDisplay();

		sprctrl.scalex=4096;//2048;
		sprctrl.scaley=4096;//2048;
		textSetPos(32,10);
		sprctrl.r=128;
		sprctrl.g=64;
		sprctrl.b=64;
		textPrintf( "MEMORY CARD STATUS \n" );

		textSetPos(32,30);
		textPrintf( "CARD 1 \n" );

		textSetPos(256,30);
		textPrintf( "CARD 0 \n" );

		sprctrl.scalex=2048;
		sprctrl.scaley=2048;

		textSetPos(256,50);
		textPrintf( "CARD STATUS \n");
		textSetPos(256,60);
		textPrintf( "%s \n",cardStatus[*res0] );

		textSetPos(32,50);
		textPrintf( "CARD STATUS \n");
		textSetPos(32,60);
		textPrintf( "%s \n",cardStatus[*res1] );

		//if (*res0==McErrNone

		cardAngle+=16;
		if (cardAngle>4096) cardAngle-=4096;
		End_Loop(framerate);
	}	





/*	cmds=NULL;

	exitFlag=1;
	MemCardExist( channel );	// check for card
	while (exitFlag)
	{
		switch( MemCardSync(1,&cmds,&result) )
		{
			case -1:	// First time no cmds exectuted
			MemCardExist( channel );	// check for card
			break;

			case 0:		// Async command in progress
			break;

			case 1:		// Async command finished
			switch(cmds)
			{
				case McFuncExist:
					textSetPos(32,50);
					//textPrintf( "CARD STATUS %s",cardStatus[result1] );
					MemCardAccept(channel);
					break;

				case McFuncAccept:
					textSetPos(32,50);
					//textPrintf( "CARD STATUS %s",cardStatus[result1] );
					if (result!=McErrNewCard) exitFlag=0;
					else MemCardAccept(channel);
					break;
			}
			default:
		}
	}




//	return (result);
}
*/
/*	--------------------------------------------------------------------------------
	Function 	: readCard
	Purpose 	: Read files from card if exists
	Parameters 	: none
	Returns 	: none
	Info 		: 
	--------------------------------------------------------------------------------*/

/*

void	readCardDir(long	*res0,long	*res1)
{
long	cmds,exitFlag,result,loop,test;

	exitFlag=1;
	if (*res0==McErrNone)
	{
		//printf ("get card 0 dir\n");
		MemCardGetDirentry( 0x00, "*", fileList[0], &files[0], 0, BLOCK_MAX );

		test=1;
		loop=0;
		while(loop<files[0])
		{
			if (test==1)
			{
				test=0;
				MemCardOpen(0x00,&fileList[0][loop].name[0],O_RDONLY);
				MemCardReadData(&cardData[loop],0,640);
				//loop++;
			}
			switch( MemCardSync(1,&cmds,&result) )
			{
				case -1:	// First time no cmds exectuted
				//MemCardExist( channel );	// check for card
				break;

				case 0:		// Async command in progress
				break;

				case 1:		// Async command finished
				switch(cmds)
				{
					case McFuncReadData:
						MemCardClose();
						test=1;
						loop++;
						break;
					default:
						printf ("error with card\n");
						break;
				}

				break;
				default:
			}

			Start_Loop();
			backDisplay(cardAngle);
			MemCardDisplay();
			End_Loop(framerate);
			cardAngle-=16;
			if (cardAngle<0) cardAngle+=4096;

		}
	}

	if (*res1==McErrNone)
	{
		//printf ("get card 1 dir\n");
		MemCardGetDirentry( 0x10, "*", fileList[1], &files[1], 0, BLOCK_MAX );
		MemCardSync(0,0,res1);
	}

	for (loop=0;loop<15;loop++)
	{
		JIS2Ascii(&filename[loop][0],&cardData[loop][4]);

		for (test=0;test<30;test++)	// temp to convert to upper case
		{
			if (filename[loop][test]>=0x61) filename[loop][test]-=0x20;
		}
	}

	//textureCreate(SPRITEX *pSprX, char *pBitMap, short *pCLUT, int x, int y);

	for (loop=0;loop<files[0];loop++)
	{
		textureCreate(&icons[loop], &cardData[loop][128], (short*)&cardData[loop][96] , 16, 16);
	}
//	textureCreate(&icons[1], &cardData[1][128], (short*)&cardData[1][96] , 16, 16);
//	textureCreate(&icons[2], &cardData[2][128], (short*)&cardData[2][96] , 16, 16);
//	textureCreate(&icons[3], &cardData[3][128], (short*)&cardData[3][96] , 16, 16);

	while (exitFlag)
	{

		Start_Loop();
		backDisplay(cardAngle);
		MemCardDisplay();

		cardAngle-=16;
		if (cardAngle<0) cardAngle+=4096;

		sprctrl.scalex=4096;//2048;
		sprctrl.scaley=4096;//2048;
		textSetPos(32,10);
		sprctrl.r=128;
		sprctrl.g=64;
		sprctrl.b=64;
		textPrintf( "MEMORY CARD FILES \n" );

		sprctrl.scalex=2048;
		sprctrl.scaley=2048;


		textSetPos(256,50);
		textPrintf( "CARD STATUS \n");
		textSetPos(256,60);
		textPrintf( "%s \n",cardStatus[*res0] );

		textSetPos(32,50);
		textPrintf( "CARD STATUS \n");



		textSetPos(32,60);
		textPrintf( "%s \n",cardStatus[*res1] );
	
		sprctrl.r=128;
		sprctrl.g=128;
		sprctrl.b=128;


		sprctrl.scalex=8192;
		sprctrl.scaley=8192;

		cmds=0;
		for (test=0;test<5;test++)
		{
			for (loop=0;loop<3;loop++)
			{
				if (cmds<files[0])spritePrint(&icons[cmds],-96+(loop*33),-96+(test*33),0);
				cmds++;
			}
		}
				



//		spritePrint(&icons[1],-32,64,0);
//		spritePrint(&icons[2],32,64,0);
//		spritePrint(&icons[3],96,64,0);

		sprctrl.scalex=2048;
		sprctrl.scaley=2048;

		//sprctrl.scalex=1024;
		//sprctrl.scaley=1024;

		if (*res1==McErrNone)
		{
			for (loop=0;loop<15;loop++)
			{
				textSetPos(32,70+(loop*10));
				//textPrintf( "File %d name %s",loop,fileList[1][loop].name);
				textPrintf( "%d %s",loop,fileList[1][loop].name);
			}
		}

		if (*res0==McErrNone)
		{
			for (loop=0;loop<15;loop++)
			{
				textSetPos(256,70+(loop*10));
				//textPrintf( "%d %s",loop,fileList[0][loop].name);
				textPrintf( "%d %s",loop,filename[loop]);
			}
		}

		//if (pad[0] ) exitFlag=0;
		//printf ("pad %d \n",pad[0]);
		//printf ("padread2 %d \n",PadRead2(0) );
		End_Loop(framerate);
	}


}

*/
/*	cameraCollProbe[0].pPos->vx=cameraColl.pPos->vx+(20*4096);
	temp1[0]=cameraCollProbe[0].pPos->vy=cameraColl.pPos->vy;
	cameraCollProbe[0].pPos->vz=cameraColl.pPos->vz;

	cameraCollProbe[1].pPos->vx=cameraColl.pPos->vx;
	temp1[1]=cameraCollProbe[1].pPos->vy=cameraColl.pPos->vy;
	cameraCollProbe[1].pPos->vz=cameraColl.pPos->vz+(20*4096);

	cameraCollProbe[2].pPos->vx=cameraColl.pPos->vx-(20*4096);
	temp1[2]=cameraCollProbe[2].pPos->vy=cameraColl.pPos->vy;
	cameraCollProbe[2].pPos->vz=cameraColl.pPos->vz;

	cameraCollProbe[3].pPos->vx=cameraColl.pPos->vx;
	temp1[3]=cameraCollProbe[3].pPos->vy=cameraColl.pPos->vy;
	cameraCollProbe[3].pPos->vz=cameraColl.pPos->vz-(20*4096);

	cameraCollProbe[4].pPos->vx=cameraColl.pPos->vx;
	temp1[4]=cameraCollProbe[4].pPos->vy=cameraColl.pPos->vy;//+(20*4096);
	cameraCollProbe[4].pPos->vz=cameraColl.pPos->vz;
*/
	
/*	for (loop=0;loop<4;loop++)
	{
		CameraProbePosOld[loop].vx=cameraCollProbe[loop].pPos->vx;
		CameraProbePosOld[loop].vy=cameraCollProbe[loop].pPos->vy;
		CameraProbePosOld[loop].vz=cameraCollProbe[loop].pPos->vz;
	}
*/

	//temp[0]=getHeightAt(cameraColl.pPos->vx, cameraColl.pPos->vy, cameraColl.pPos->vz);
	//temp[0]=temp[0]>>12;

	//CamVars->groundHeight = getHeightAt(CamVars->camera.vpx*4096, CamVars->camera.vpy*4096, CamVars->camera.vpz*4096);
	//CamVars->groundHeight = CamVars->groundHeight>>12;

/*
	if ( temp[0] < CamVars->requiredHeight && temp[0]!=-1)
	{
		//temp[0]=abs(temp[0]);
		heightChange=temp[0];
		//CamVars->currentHeight+=(heightChange);
		//CamVars->camera.vpy=CamVars->camera.vry-CamVars->currentHeight;
		temp1[0]=cameraColl.pPos->vy=CamVars->camera.vpy*4096;
	}

*/
//	for (loop=0;loop<5;loop++) cameraHit[loop] = NULL;




	if ( CamVars->mode!=ZOOM )
	{
		if (cameraHit)
		{
//			CamVars->currentHeight+=(heightChange/4096);
			//CamVars->camera.vpy=(cameraColl.pPos->vy/4096);
//			CamVars->camera.vpy=CamVars->camera.vry-CamVars->currentHeight;
		}
	}

/*
	oldR.vx=CamVars->camera.vrx;
	oldR.vy=CamVars->camera.vry;
	oldR.vz=CamVars->camera.vrz;

	oldP.vx=CamVars->camera.vpx;
	oldP.vy=CamVars->camera.vpy;
	oldP.vz=CamVars->camera.vpz;

	CamVars->camera.vrx=(CamVars->camera.vrx*SCALE)/4096;
	CamVars->camera.vry=(CamVars->camera.vry*SCALE)/4096;
	CamVars->camera.vrz=(CamVars->camera.vrz*SCALE)/4096;

	CamVars->camera.vpx=(CamVars->camera.vpx*SCALE)/4096;
	CamVars->camera.vpy=(CamVars->camera.vpy*SCALE)/4096;
 	CamVars->camera.vpz=(CamVars->camera.vpz*SCALE)/4096;

	if (debounce[1]&PAD_SELECT)
	{
		if (switchcam==TRUE) switchcam=FALSE;
		else switchcam=TRUE;
	}
	if (switchcam==TRUE)
	{
		CamVars->camera.vrx=CamVars->targetmodel->position.vx*4;
		CamVars->camera.vry=CamVars->targetmodel->position.vy*4;
		CamVars->camera.vrz=CamVars->targetmodel->position.vz*4;

		CamVars->camera.vpx=CamVars->camera.vrx;
		CamVars->camera.vpy=CamVars->camera.vry-3000;
		CamVars->camera.vpz=CamVars->camera.vrz;
	}


	GsSetRefView2L(&CamVars->camera);

	sprctrl.scalex=2048;
	sprctrl.scaley=2048;

	CamVars->camera.vrx=oldR.vx;
	CamVars->camera.vry=oldR.vy;
	CamVars->camera.vrz=oldR.vz;

	CamVars->camera.vpx=oldP.vx;
	CamVars->camera.vpy=oldP.vy;
	CamVars->camera.vpz=oldP.vz;

	scale.vx=SCALE;
	scale.vy=SCALE;
	scale.vz=SCALE;

	ScaleMatrixL(&GsWSMATRIX,&scale);

	CamVars->oldModelPos.vx=CamVars->targetmodel->position.vx;
	CamVars->oldModelPos.vy=CamVars->targetmodel->position.vy;
	CamVars->oldModelPos.vz=CamVars->targetmodel->position.vz;
*/








/*

	temp.vx=CamVars->camera.vpx*4096;
	temp.vy=CamVars->camera.vpy*4096;
	temp.vz=CamVars->camera.vpz*4096;

	temp3[0]=getHeightAtSlow( temp.vx-(20*4096), temp.vy, temp.vz+(20*4096) );
	temp3[1]=getHeightAtSlow( temp.vx-(20*4096), temp.vy, temp.vz );
	temp3[2]=getHeightAtSlow( temp.vx-(20*4096), temp.vy, temp.vz-(20*4096) );

	temp3[3]=getHeightAtSlow( temp.vx, temp.vy, temp.vz+(20*4096) );
	temp3[4]=getHeightAtSlow( temp.vx, temp.vy, temp.vz );
	temp3[5]=getHeightAtSlow( temp.vx, temp.vy, temp.vz-(20*4096) );

	temp3[6]=getHeightAtSlow( temp.vx+(20*4096), temp.vy, temp.vz+(20*4096) );
	temp3[7]=getHeightAtSlow( temp.vx+(20*4096), temp.vy, temp.vz );
	temp3[8]=getHeightAtSlow( temp.vx+(20*4096), temp.vy, temp.vz-(20*4096) );
	
	CamVars->groundHeight=temp3[0];
	for (temp1=1;temp1<9;temp1++)
	{
		if (CamVars->groundHeight>temp3[temp1]) CamVars->groundHeight=temp3[temp1];
	}
	if (CamVars->groundHeight!=-1) CamVars->groundHeight = (CamVars->groundHeight/4096)-10;
	else CamVars->groundHeight=CamVars->currentHeight;


	if (CamVars->currentHeight>CamVars->groundHeight)
	{
		temp1=(CamVars->currentHeight-CamVars->groundHeight);
	}
	else temp1=0;
	
	temp1=(temp1+lastHeightChange+lastHeightChange)/3;


	CamVars->camera.vpy-=temp1*2;

	lastHeightChange=temp1;

*/

	//CamVars->currentHeight=oldHeight;
	//printf ("ground height = %d\n",CamVars->groundHeight);

/*	CameraCollision(CamVars);
	mag=Magnitude((VECTOR *)&CamVars->cameraVel.vx);
	if (mag>4096)
	{
		if (cameraHit[0] && CamVars->cameraVel.vx>0) CamVars->cameraVel.vx=0;
		if (cameraHit[2] && CamVars->cameraVel.vx<0) CamVars->cameraVel.vx=0;

		if (cameraHit[1] && CamVars->cameraVel.vz>0) CamVars->cameraVel.vz=0;
		if (cameraHit[3] && CamVars->cameraVel.vz<0) CamVars->cameraVel.vz=0;

		CamVars->camera.vpx+=(CamVars->cameraVel.vx/4096);//-CamVars->camera.vrx;
		CamVars->camera.vpy+=(CamVars->cameraVel.vy/4096);//-CamVars->camera.vrx;
		CamVars->camera.vpz+=(CamVars->cameraVel.vz/4096);//-CamVars->camera.vrz;
	}
*/
		//else
		//{
		//	if (CamVars->mode==ZOOM) mag=2;
		//	else mag=10;
		//}
		//if ( (pad[1]&PAD_L2 ) && (CamVars->requiredHeight>=20 || CamVars->mode==ZOOM) ) CamVars->requiredHeight-=10;
		//else if (pad[1]&PAD_R2 ) CamVars->requiredHeight+=10;



//	if ((CamVars->flags&FIXEDANGLE))
//	{
	
//	}
/*
	if (CamVars->mode==ZOOM)
	{	// put move camera here
		if (pad[0] & PAD_UP && CamVars->requiredHeight>0) CamVars->requiredHeight-=2;
		if (pad[0] & PAD_DOWN && CamVars->requiredHeight<60) CamVars->requiredHeight+=2;


		if (pad[0] & PAD_RIGHT)
		{
			RotateVector2D(&temp, &temp, cAngle+512);
			CamVars->cameraVel.vx+=temp.vx;
			CamVars->cameraVel.vy+=temp.vy;
			CamVars->cameraVel.vz+=temp.vz;
		}
		if (pad[0] & PAD_LEFT)
		{
			RotateVector2D(&temp, &temp, cAngle-512);
			CamVars->cameraVel.vx+=temp.vx;
			CamVars->cameraVel.vy+=temp.vy;
			CamVars->cameraVel.vz+=temp.vz;
		}
	}
*/



























				//BallCtrl.preSnowType=BallCtrl.type;
				//BallCtrl.type=BALL_MODE_SNOW;

				//BallChange.fromBall=BallCtrl.preSnowType;
				//BallChange.toBall=BallCtrl.type;
