#include "global.h"
#include "main.h"
#include "collide.h"
#include "enemies.h"
#include "map_draw.h"
#include "gametext.h"
#include "puzzles.h"
#include "inventory.h"
#include "charactr.h"
#include "map.h"
#include "maths.h"
#include "sound.h"
#include "dualshock.h"
#include "deth.h"

#define MAP_DEPTHOFFS	2

int mapoverlaySpriteRotate(TextureType *spr, int x, int y, int xoff, int yoff, int depth, int rot,unsigned long colour);

MAPDATA mapData;
int *mapRot;
int pRadarData[LEVNUMBER_NUM_LEVELS];
int radarrotate;
int radardelay;		// Delay between blips
int flashdelay;		// Delay between flashes

int radarcolour = 0x1f801f;	// grey radar, green colourisation
//int radarcolour = 0x1f1fff;		// grey radar, red overcolourisation

void mapSetPos(VECTOR *pPos)
{
	ASSERT(pPos);
	mapData.pPos = pPos;
}

void mapSetRot(int *pDir)
{
	ASSERT(pDir);
	mapData.pDir = pDir;
}

void mapGetSizeOfWorld(FMA_WORLD2 *pW)
{
/*	WORLD_MESH_INSTANCE *pMesh;
	int i;
	int minx, maxx, miny, maxy;


	ASSERT(pW);
	ASSERT(pW->n_meshes);

	radarrotate = 0;

	pMesh = ADD2POINTER(pW, sizeof(FMA_WORLD));

	mapData.levelXMin = pMesh[0].posx + pMesh[0].mesh->minx;
	mapData.levelXMax = pMesh[0].posx + pMesh[0].mesh->maxx;

	mapData.levelYMin = pMesh[0].posz + pMesh[0].mesh->minz;
	mapData.levelYMax = pMesh[0].posz + pMesh[0].mesh->maxz;


	for(i=0; i<pW->n_meshes; i++)
	{
		minx = pMesh[i].posx + pMesh[i].mesh->minx;
		maxx = pMesh[i].posx + pMesh[i].mesh->maxx;

		miny = pMesh[i].posz + pMesh[i].mesh->minz;
		maxy = pMesh[i].posz + pMesh[i].mesh->maxz;

		if(minx < mapData.levelXMin)
			mapData.levelXMin = minx;

		if(maxx > mapData.levelXMax)
			mapData.levelXMax = maxx;

		if(miny < mapData.levelYMin)
			mapData.levelYMin = miny;

		if(maxy > mapData.levelYMax)
			mapData.levelYMax = maxy;
	}

	mapData.levelXSize = mapData.levelXMax - mapData.levelXMin;
	mapData.levelYSize = mapData.levelYMax - mapData.levelYMin;*/
}

int mapInitialise(void)
{
	int i;


	// CPW: fill whole array with most common map type
	for(i=0; i<LEVNUMBER_NUM_LEVELS; i++)
		pRadarData[i] = MAPTYPE_SCENIC_ONLY;


	pRadarData[LEVNUMBER_A1_HUT17] =		TEXTSTR_HUT_17;
	pRadarData[LEVNUMBER_A1_FOWLER] =		MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A1_WORKHUT1] =		TEXTSTR_HUT_16;
	pRadarData[LEVNUMBER_A1_EGGHUT] =		TEXTSTR_HUT_2;


	pRadarData[LEVNUMBER_A2_FOWLER] =		MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A2_EGGHUT] =		TEXTSTR_HUT_2;
	pRadarData[LEVNUMBER_A2_HUT17] =		TEXTSTR_HUT_17;
	pRadarData[LEVNUMBER_A2_WORKHUT1] =		TEXTSTR_HUT_16;

	pRadarData[LEVNUMBER_A2_SCENIC1] =		TEXTSTR_HUT_3;
	pRadarData[LEVNUMBER_A2_SCENIC2] =		TEXTSTR_HUT_4;
	pRadarData[LEVNUMBER_A2_ENGINEHUT] =	TEXTSTR_HUT_14;
	pRadarData[LEVNUMBER_A2_WORKHUT2] =		TEXTSTR_HUT_15;

	pRadarData[LEVNUMBER_A2_WINGSHUT] =		TEXTSTR_HUT_5;
	pRadarData[LEVNUMBER_A2_SCENIC4] =		TEXTSTR_HUT_6;
	pRadarData[LEVNUMBER_A2_WORKHUT3] =		TEXTSTR_HUT_11;
	pRadarData[LEVNUMBER_A2_SCENIC3] =		TEXTSTR_HUT_12;


	pRadarData[LEVNUMBER_A4_FOWLER] =		MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A4_EGGHUT] =		MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A4_HUT17] =		TEXTSTR_HUT_17;
	pRadarData[LEVNUMBER_A4_WORKHUT1] =		TEXTSTR_HUT_16;

	pRadarData[LEVNUMBER_A4_SCENIC1] =		TEXTSTR_HUT_3;
	pRadarData[LEVNUMBER_A4_SCENIC2] =		TEXTSTR_HUT_4;
	pRadarData[LEVNUMBER_A4_ENGINEHUT] =	MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A4_WORKHUT2] =		TEXTSTR_HUT_15;

	pRadarData[LEVNUMBER_A4_WINGSHUT] =		MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A4_SCENIC4] =		TEXTSTR_HUT_6;
	pRadarData[LEVNUMBER_A4_WORKHUT3] =		TEXTSTR_HUT_11;
	pRadarData[LEVNUMBER_A4_SCENIC3] =		TEXTSTR_HUT_12;

	pRadarData[LEVNUMBER_A4_CONSTRUCT] =	MAPTYPE_NO_MAP;
	pRadarData[LEVNUMBER_A4_SCENIC6] =		TEXTSTR_HUT_8;
	pRadarData[LEVNUMBER_A4_SCENIC5] =		TEXTSTR_HUT_9;
	pRadarData[LEVNUMBER_A4_WORKHUT4] =		TEXTSTR_HUT_10;

	mapData.pBackG = textureFindInAllBanks("RADAR");
	ASSERT(mapData.pBackG);

	mapData.pFrame = textureFindInAllBanks("SURROUND3X16");
	ASSERT(mapData.pFrame);

	mapData.pBlob = textureFindInAllBanks("MAPNME");
	ASSERT(mapData.pBlob);

	mapData.pGrScale = textureFindInAllBanks("GRSCALE");
	ASSERT(mapData.pGrScale);

	mapData.pRotateything = textureFindInAllBanks("RADARTHINGY");
	ASSERT(mapData.pRotateything);

	mapData.pHut = textureFindInAllBanks("RADHUT");
	ASSERT(mapData.pHut);

	mapData.sample = sfxFindSampleInAllBanks("heartbeat1");
	if(!mapData.sample)
		debugPrintf("Cant find bleep sample for the radar thingy!\n");

	mapData.sample2 = sfxFindSampleInAllBanks("bleep2");
	if(!mapData.sample2)
		debugPrintf("Cant find bleep2 sample for the radar thingy!\n");

	mapData.pLight = textureFindInAllBanks("WARNALPHA2");
	ASSERT(mapData.pLight);

	mapData.pArrow = textureFindInAllBanks("MAPARROW");
	ASSERT(mapData.pArrow);

	radardelay = 0;
	flashdelay = 0;

	return 1;
}


#define RADAR_XPOS	(144)
#define RADAR_YPOS	(64)

#define RADAR_XSIZE	(64)
#define RADAR_YSIZE	(43)

#define RADAR_XHALFSIZE	(RADAR_XSIZE>>1)
#define RADAR_YHALFSIZE	(RADAR_YSIZE>>1)

#define RADAR_XCENTRE	(RADAR_XPOS + RADAR_XHALFSIZE)
#define RADAR_YCENTRE	(RADAR_YPOS + RADAR_YHALFSIZE)

#define RADAR_RANGE	(7000)


void mapColourCycle()
{
	int tr,tg,tb;
	int r,g,b,c;

	if(hourglass_game_is_alert)
	{
		c = (rcos((loop_ticker<<8) & 4095) + 4096);	// 0...8192
		c = (c + 3000) * 256 / (8192 + 3000); 

		tr = (0xff * c) >> 8;
		tg = (0x1f * c) >> 8;
		tb = (0x1f * c) >> 8;
	}
	else
	{
		tr = 0x1f;
		tg = 0x80;
		tb = 0x1f;
	}

	r = radarcolour & 0xff;
	g = (radarcolour>>8) & 0xff;
	b = (radarcolour>>16) & 0xff;

	r = r + (tr - r)/8;	// homer
	g = g + (tg - g)/8;
	b = b + (tb - b)/8;

	radarcolour = r | (g<<8) | (b<<16);
}

int mapDrawSprite(void)
{
	int x=0, y=0,w,h;
	int colour = 0x7f7f7f;
	int depth = 1;


	int minx, miny, maxx, maxy, xs, ys, u0, v0, u1, v1;
	int xp0, xp1, yp0, yp1;
	int xoffs, yoffs, xcent, ycent;
	int enemydistance;					// Distance of the nearest enemy
	int objectdistance;					// Distance of the nearest interesting object
	char nearest_enemy_is_evil;			// Whether or not the nearest enemy makes our heart beat faster

	VECTOR mapdist;

	int a;

	ENEMY *pE;


	if((levinfo->level_number==LEVNUMBER_A2_COGCHASE) ||(deathDying))
		return 0;

	mapColourCycle();

	// Draw the rotating lines
	if(!globalPause)
		radarrotate+=32;
	
	if(radarrotate>4095) 
	{
		radarrotate=0;
	}
	
	if(pRadarData[(int)(levinfo->level_number)] == MAPTYPE_NO_MAP)
	{
		return 0;
	}

	else if(pRadarData[(int)(levinfo->level_number)]!=MAPTYPE_SCENIC_ONLY)
	{
		overlayTranspSprite(mapData.pBackG, RADAR_XPOS, RADAR_YPOS, 2, radarcolour);
		overlaySprite(mapData.pFrame, RADAR_XPOS-13,RADAR_YPOS-7,3,0x808080);
		overlaySprite(mapData.pHut, RADAR_XCENTRE-24,RADAR_YCENTRE-16,3,0x808080);

		fontPrintCentred(font,RADAR_XCENTRE,RADAR_YCENTRE,"16",32,96,32);
		return 0;
	}

	{	// CPW: put it back
   //		DR_AREA *da;
   //		da = BEGINPRIM(da, DR_AREA);
   //		SetDrawArea(da, &currentDisplayPage->drawenv.clip);
   //		ENDPRIM(da, 4, DR_AREA);
	}

	overlayTranspSprite(mapData.pBackG, RADAR_XPOS, RADAR_YPOS, 2, radarcolour);
	overlaySprite(mapData.pFrame, RADAR_XPOS-13,RADAR_YPOS-7,3,0x808080);

	// Plot interesting scenics (eg - inventory items) as flashing green things
	{
		SCENIC *pS;
		POLY_FT4 *si;

		int xp, yp;
		int tempdist;
			
		colour = (loop_ticker <<4) & 0x7f;

		if (colour >= 64)
			colour = 127 - colour;
		
		colour = (colour+64) << 16;
		colour+=0x406060;

		objectdistance = 10000;

		for(pS = scenicList; pS; pS=pS->next)
		{
			if((pS->flags & SCENICFLAG_IS_INTERESTING) && (pS->flags & SCENICFLAG_ACTIVE))
			{
				// Get the distance between the chicken and this object
	
				mapdist.vy = 0;
				mapdist.vx = pS->pActor->position.vx - mapData.pPos->vx;
				mapdist.vz = pS->pActor->position.vz - mapData.pPos->vz;

				tempdist = Magnitude2D(mapdist.vx,mapdist.vz);

				if(tempdist<objectdistance)
					objectdistance = tempdist;

				if(tempdist<RADAR_RANGE)
				{
					VECTOR myvec;
					int chickenangle;

					xp = RADAR_XCENTRE;
					yp = RADAR_YCENTRE;

					myvec.vx = xp;
					myvec.vy = 0;
					myvec.vz = 0;

					xp = (Magnitude2D(mapdist.vx,mapdist.vz)/(RADAR_RANGE/RADAR_XHALFSIZE));
					myvec.vx = xp;
					myvec.vy = 0;
					myvec.vz = 0;

					// Get the angle between the chicken and the object
					chickenangle = (utilCalcAngle(mapdist.vx,mapdist.vz)) & 4095;

					RotateVector2DXYZ(&myvec, &myvec, (*mapData.pDir-chickenangle)-3172,2);

					xp = myvec.vx;
					yp = myvec.vy;

					// Ok, now scale the y position down by (Radar height/Radar width) to take account of the aspect ratio
					
					yp = (yp*3012)/4096;

					overlaySprite(mapData.pBlob, RADAR_XCENTRE + (xp - (mapData.pBlob->w>>1)), RADAR_YCENTRE+(yp - (mapData.pBlob->h>>1)), 6, colour);
				}
			}

			if(flashdelay>0 && !globalPause)
				flashdelay--;

			if(objectdistance<RADAR_RANGE/4)
			{
				if(!flashdelay)
				{
					overlayAlphaSprite(mapData.pLight, RADAR_XCENTRE+29, RADAR_YCENTRE-24, 2, 0x0000ff00);
					soundPlaySample(mapData.sample2, (abs(RADAR_RANGE-objectdistance))/56, RADAR_RANGE-objectdistance);
					flashdelay = (objectdistance/25);
					if(flashdelay<10)
						flashdelay=10;
				}
			}
		}

		{
#ifndef _WINDOWS
			RECT clip;
#else
			RECTPS clip;
#endif
			DR_AREA *da;
			clip.x = currentDisplayPage->drawenv.clip.x + 256 + RADAR_XPOS;
#if PALMODE == 1
			clip.y = currentDisplayPage->drawenv.clip.y + 128 + RADAR_YPOS;
#else
			clip.y = currentDisplayPage->drawenv.clip.y + 120 + RADAR_YPOS;
#endif
			clip.w = RADAR_XSIZE+1;
			clip.h = RADAR_YSIZE+1;
	 //		da = BEGINPRIM(da, DR_AREA);
	 //		SetDrawArea(da, &clip);
	 //		ENDPRIM(da, 6, DR_AREA);
		}


		// Plot the enemies on the radar

		enemydistance = 10000;				// Distance of the *nearest* enemy to the chicken, for the heartbeat thing on the radar
											// Make it a big value to start with

		nearest_enemy_is_evil = 0;

		for(pE=enemyList.head.next; pE != &enemyList.head; pE = pE->next)
		{
			// CPW: search for enemies that are real enemies, then plot them on the 'radar'
		
			if((!(pE->flags & ENEMYFLAGS_MAINCHARACTER)) && (pE->runFlags & RUNFLAGS_VISIBLE))
			{
				int xp, yp;
				char this_enemy_is_evil;


				// Get the distance between this enemy and the chicken
				
				mapdist.vy = 0;
				mapdist.vx = pE->pos.vx - mapData.pPos->vx;
				mapdist.vz = pE->pos.vz - mapData.pPos->vz;

				tempdist = Magnitude2D(mapdist.vx,mapdist.vz);

				if((pE->readData.pInfo->nme_type!=ENEMY_CHICKEN) && (pE->readData.pInfo->nme_type!=ENEMY_SCENIC_CHICKEN) && (pE->readData.pInfo->nme_type!=ENEMY_UNDEFINED))
				{
					this_enemy_is_evil = 1;
				}
				else
				{
					this_enemy_is_evil = 0;
				}

				if(tempdist<enemydistance)		// If this is the closest enemy we've found so far
				{
					enemydistance = tempdist;
					nearest_enemy_is_evil = this_enemy_is_evil;
				}
				
				if(tempdist<RADAR_RANGE)
				{
					VECTOR myvec;
					int chickenangle;
				
					xp = RADAR_XCENTRE;
					yp = RADAR_YCENTRE;

					myvec.vx = xp;
					myvec.vy = 0;
					myvec.vz = 0;

					xp = (Magnitude2D(mapdist.vx,mapdist.vz)/(RADAR_RANGE/RADAR_XHALFSIZE));
					myvec.vx = xp;
					myvec.vy = 0;
					myvec.vz = 0;

					// Get the angle between the chicken and the object

					chickenangle = (utilCalcAngle(mapdist.vx,mapdist.vz)) & 4095;

					RotateVector2DXYZ(&myvec, &myvec, (*mapData.pDir-chickenangle)-3072,2);

					xp = myvec.vx;
					yp = myvec.vy;

					// Ok, now scale the y position down by (Radar height/Radar width) to take account of the aspect ratio
					
					yp = (yp*3012)/4096;

					if(this_enemy_is_evil)
					{
						overlaySprite(mapData.pBlob, RADAR_XCENTRE+(xp - (mapData.pBlob->w>>1)), RADAR_YCENTRE+(yp - (mapData.pBlob->h>>1)), 6 , 0x404080);
					}
					else
					{
						overlaySprite(mapData.pBlob, RADAR_XCENTRE+(xp - (mapData.pBlob->w>>1)), RADAR_YCENTRE+(yp - (mapData.pBlob->h>>1)), 6 , 0x804040);
					}

					// That's the blob drawn, now we need to draw the FOV

					// BP: Re-written now so that the level size is ignored and now takes notice of the enemy's FOV and view distance
					{
						int coneAngle = pE->dir+ pE->head.actualAngle - *mapData.pDir-1024;
						int fov; 
				
						fov = pE->view_fov_current / 2;
						
						BEGINPRIM(si, POLY_FT4);
		
						si->x2 = 0;		//xp,yp = blob position
						si->y2 = 0;
						
						si->x1 = 0;
					
						// Gives us a line straight up the screen (= - (enemy sight range/(RADAR_RANGE/radar width in pixels)))
						si->y1 = -(pE->view_range_current/(RADAR_RANGE/64));

						myvec.vx = si->x1;		// x0,y0
						myvec.vy = si->y1;
						myvec.vz = 0;
						RotateVector2DXYZ(&myvec, &myvec, 4096-fov,2);
						si->x0 = myvec.vx;
						si->y0 = myvec.vy;
					
						myvec.vx = si->x1;		// x3,y3
						myvec.vy = si->y1;
						myvec.vz = 0;
						RotateVector2DXYZ(&myvec, &myvec, fov,2);
						si->x3 = myvec.vx;
						si->y3 = myvec.vy;

						// Ok that gives us a straight-up-the screen cone, now rotate it...

						myvec.vx = si->x0;
						myvec.vy = si->y0;
						myvec.vz = 0;
						RotateVector2DXYZ(&myvec, &myvec, (3072-coneAngle)+2048,2);
						si->x0 = myvec.vx;
						si->y0 = myvec.vy;
					
						myvec.vx = si->x1;
						myvec.vy = si->y1;
						myvec.vz = 0;
						RotateVector2DXYZ(&myvec, &myvec, (3072-coneAngle)+2048,2);
						si->x1 = myvec.vx;
						si->y1 = myvec.vy;

						myvec.vx = si->x3;
						myvec.vy = si->y3;
						myvec.vz = 0;
						RotateVector2DXYZ(&myvec, &myvec, (3072-coneAngle)+2048,2);
						si->x3 = myvec.vx;
						si->y3 = myvec.vy;

						// and add xp,yp and do aspect ratio conversion

						si->x0 += xp;
						si->y0 = (si->y0*43/64) + yp;

						si->x1 += xp;
						si->y1 = (si->y1*43/64) + yp;
						
						si->x2 += xp;
						si->y2 += yp;
						
						si->x3 += xp;
						si->y3 = (si->y3*43/64) + yp;


						si->x0+=RADAR_XCENTRE;
						si->x1+=RADAR_XCENTRE;
						si->x2+=RADAR_XCENTRE;
						si->x3+=RADAR_XCENTRE;
						si->y0+=RADAR_YCENTRE;
						si->y1+=RADAR_YCENTRE;
						si->y2+=RADAR_YCENTRE;
						si->y3+=RADAR_YCENTRE;
					}
	
					si->u2 = mapData.pGrScale->u0 + 7;
					si->v2 = mapData.pGrScale->v0 + 15;	// CPW: assume 16 x 16 texture
	
					si->u0 = mapData.pGrScale->u0 + 0;
					si->v0 = mapData.pGrScale->v0 + 0;
	
					si->u3 = mapData.pGrScale->u0 + 15;
					si->v3 = mapData.pGrScale->v0 + 0;
	
					si->u1 = mapData.pGrScale->u0 + 7;
					si->v1 = mapData.pGrScale->v0 + 0;
	
					si->clut = mapData.pGrScale->clut;
					si->tpage = mapData.pGrScale->tpage | ((SEMITRANS_ADD-1) << 5);
	
					setPolyFT4(si);
	
					*(ULONG*)&si->r0 = 0x1f7f7f | ((GPU_COM_TF4+2)<<24);
	
					ENDPRIM(si, 5, POLY_FT4);
				}
			}
		}
	}

	// Play the sample thing according to how near the nearest enemy is
	if((enemydistance<7000) && (nearest_enemy_is_evil))
	{
		if(radardelay>0)
			radardelay--;
		else
		{
			if(!deathDying)		
			{
				soundPlaySample(mapData.sample, 16+((RADAR_RANGE-enemydistance)/64), 2000);//(abs(RADAR_RANGE-enemydistance))/56
				// shock the pad
				dsEvent_Add(1, 1, 0, 0, 0, 0, 0);		// Heartbeat full effect.. omg
			}
			radardelay = (enemydistance/200);
			if(radardelay<8)
				radardelay = 8;
		}
	}

	w = mapData.pRotateything->u1 - mapData.pRotateything->u0;
	h = mapData.pRotateything->v2 - mapData.pRotateything->v0;
	mapoverlaySpriteRotate(mapData.pRotateything, RADAR_XCENTRE,RADAR_YCENTRE,w,h, 5, radarrotate,radarcolour);	
	w = mapData.pArrow->u1 - mapData.pArrow->u0;
	h = mapData.pArrow->v2 - mapData.pArrow->v0;
	mapoverlaySpriteRotate(mapData.pArrow, RADAR_XCENTRE,RADAR_YCENTRE,w/2,h/2, 1, 2048-(characterVars.pMainChar->pEnemy->dir - *mapData.pDir) , 0x102070);

	return 1;
}

int mapoverlaySpriteRotate(TextureType *spr, int x, int y, int xoff, int yoff, int depth, int rot,unsigned long colour)
{

	POLY_FT4 *si;

	int	x0,y0,x1,y1,x2,y2,x3,y3,tempx,tempy,sin,cos;
	
	BEGINPRIM(si, POLY_FT4);

	y*=1.875;


	x+=256;
	y+=240;
	
	x*=1.25;

													  
	x0=x1= -yoff;									                      
	x2=x3=x0 + ((spr->u1 - spr->u0)*1.875);						  
	y0=y2= -xoff;
	y1=y3=y0 + ( (spr->v2 - spr->v0)*1.25);

	sin=rsin(rot);
	cos=rcos(rot);

	tempx=((sin*x0)+(cos*y0))/4096;
	tempy=((cos*x0)-(sin*y0))/4096;
	x0=tempx;
	y0=tempy;

	tempx=((sin*x1)+(cos*y1))/4096;
	tempy=((cos*x1)-(sin*y1))/4096;
	x1=tempx;
	y1=tempy;

	tempx=((sin*x2)+(cos*y2))/4096;
	tempy=((cos*x2)-(sin*y2))/4096;
	x2=tempx;
	y2=tempy;

	tempx=((sin*x3)+(cos*y3))/4096;
	tempy=((cos*x3)-(sin*y3))/4096;
	x3=tempx;
	y3=tempy;

	si->x0=x + x0;
	si->y0=y + y0 * 320/512;	// RFW - square-aspect correction
	si->x1=x + x1;
	si->y1=y + y1 * 320/512;
	si->x2=x + x2;
	si->y2=y + y2 * 320/512;
	si->x3=x + x3;
	si->y3=y + y3 * 320/512;

	*(ULONG *)(&si->u0) = ((unsigned long)*(USHORT *)&spr->u0);
	*(ULONG *)(&si->u2) = ((unsigned long)*(USHORT *)&spr->u2);
	*(ULONG *)(&si->u3) = ((unsigned long)*(USHORT *)&spr->u3);
	*(ULONG *)(&si->u1) = ((unsigned long)*(USHORT *)&spr->u1);

	setPolyFT4(si);
		
	si->clut = spr->clut;
	si->tpage = spr->tpage;// | ((SEMITRANS_ADD-1) << 5);
	*(ULONG*)&si->r0 = colour;// | ((GPU_COM_TF4+2)<<24);
	si->tag = SEMITRANS_ADD;
	si->code=GPU_COM_TF4;


	ENDPRIM(si, depth, POLY_FT4);
	return 1;
}


