
// Sprouts.
// Throwable objects that can be used to distract dogs

#include "global.h"
#include "maths.h"
#include "sprouts.h"
#include "enemies.h"
#include "card.h"
#include "charactr.h"
#include "map_play.h"
#include "debris.h"
#include "scenics.h"
#include "sound.h"

/*
typedef struct tagCOLLDATA
{
	int nBoxes;
	int bounce;
	int radius;
	COLLBOX *pCollBox;
	VECTOR *pVel;
	VECTOR *pPos;
	VECTOR *pRot;	// changed from FVECTOR
	int nHits;

	// internal use only
	VECTOR	oldPos;	Y
	VECTOR	oldRot;
	VECTOR	oldVel;
	VECTOR	hitPos;	// changed from FVECTOR
	int lastHit;

	ULONG	flags;	// OR of the flags of the collboxes we've hit. NB - Unlike Glover, I'm *not* shifting this down 8
}COLLDATA;
*/


SPROUTMANAGER *sproutman = NULL;
TextureType *sprout_panel_spr;
TextureType *sprout_flying_spr;

SfxSampleType *sprout_sample = NULL;



// Malloc some sprout-space on levels containing Ginger or Rocky
void sproutsInit()
{
	sproutman = MALLOC(sizeof(SPROUTMANAGER));
	FMEMZERO(sproutman,sizeof(SPROUTMANAGER));

	sprout_panel_spr = textureFindInAllBanks("SPROUT02");
	sprout_flying_spr = textureFindInAllBanks("SPROUT01");
	if((sproutsTimer<0) ||(sproutsTimer>SPROUT_TIME))
		sproutsTimer=0;
	sprout_sample = NULL;
}

// Free the sprout-space
void sproutsShutdown()
{
	FREE(sproutman);
	sproutman = NULL;
}


void sproutsSplat(SPROUTSTR *sprout)
{
	DEBRISSTR *deb;

	if(!sprout_sample)	// it's zeroed at level-init
		sprout_sample = sfxFindSampleInAllBanks("oilblob");
	if(sprout_sample)
	{
		soundPlaySamplePanFade(sprout_sample, 255, 0, &sprout->pos);
	}

	deb = debrisDust(&sprout->pos, 10, 512);
	if(deb)
	{
//		deb->colour = 0x8010ff10;
		deb->colour = 0x80485A1A;

	}

	sprout->flags &= ~SPROUT_ACTIVE;

// tbd - Start a splat debris at that point

}

// Move the sprouts, taking note of collision
// Set off a "splat" debris if we hit stuff
void sproutsCycle()
{
	int i;
	SPROUTSTR *sprout;

	if(sproutsTimer<SPROUT_TIME) sproutsTimer++;

	sprout = &sproutman->sprouts[0];
	for(i = 0; i < MAX_SPROUTS; i++,sprout++)
	{
		if(sprout->flags & SPROUT_ACTIVE)
		{

			if(enemyCheckCollision(sprout, COLLIDE_WITH_SPROUT, &sprout->pos, SPROUT_RADIUS))
			{
				sproutsSplat(sprout);
				continue;
			}

			sprout->collvel.vy += (SPROUT_GRAVITY << 9);

			sprout->collpos.vx += sprout->collvel.vx;
			sprout->collpos.vy += sprout->collvel.vy;
			sprout->collpos.vz += sprout->collvel.vz;


			if(collboxCheckSphere(&sprout->colldata))
			{
// Kill the sprout.
// I would bounce it, but I'm fed up of trying to get the collision to bounce the velocity.
				sproutsSplat(sprout);
				continue;
			}

			
			if((sprout->pos.vy) >= (WorldBFFcoll->endY>>9) + 100 *8)
			{
				// If you manage to throw it out of the level, kill it.
				sprout->flags &= ~SPROUT_ACTIVE;
			}

			sprout->pos.vx = sprout->collpos.vx>>9;
			sprout->pos.vy = sprout->collpos.vy>>9;
			sprout->pos.vz = sprout->collpos.vz>>9;

			sprout->age++;


		}
	}
}

// plot the sprouts
// tbd - they need shadows
// Actually, they need sprites too.

//#define SPROUT_PANEL_X -180	// (invent) (sprouts)
//#define SPROUT_PANEL_Y 67
//#define SPROUT_TEXT_DX	33
//#define SPROUT_TEXT_DY	19

#define SPROUT_PANEL_X -230	// (sprouts) (invent)
#define SPROUT_PANEL_Y 60
#define SPROUT_TEXT_DX	12
#define SPROUT_TEXT_DY	32

void sproutsDraw()
{
	int i;
	SPROUTSTR *sprout;

	sprout = &sproutman->sprouts[0];

	gte_SetRotMatrix(&GsWSMATRIX);
  	gte_SetTransMatrix(&GsWSMATRIX);


	for(i = 0; i < MAX_SPROUTS; i++,sprout++)
	{
		if(sprout->flags & SPROUT_ACTIVE)
		{
			VERT vect;

			vect.vx = sprout->pos.vx-mapCamData.gameCam.vpx;
			vect.vy = sprout->pos.vy-mapCamData.gameCam.vpy;
			vect.vz = sprout->pos.vz-mapCamData.gameCam.vpz;
// We'll assume that since it's the lead character that throws sprouts, they won't be 32768 away from the camera

//			Print3DSprite(sprout_flying_spr, &vect, 512, 0x0010ff10);
			Print3DSprite(sprout_flying_spr, &vect, 512, 0x00808080);

//			overlayShowSphere(sprout->pos.vx, sprout->pos.vy, sprout->pos.vz, SPROUT_RADIUS, 0x00ff00);
		}
	}

	if(gamePlayData->sprouts_carried > 0 && (currentCharacter == CHAR_NAME_GINGER || currentCharacter == CHAR_NAME_ROCKY))
	{
// Sprouts overlay - bottom left
		char temp[20];
		sprintf(temp,"%d",(int)gamePlayData->sprouts_carried);
		fontSetPrintDepth(2);

		fontPrint(font, SPROUT_PANEL_X + SPROUT_TEXT_DX,SPROUT_PANEL_Y+SPROUT_TEXT_DY, temp, 96*3/4,120*3/4,35*3/4);	//10,128,10);
		fontSetPrintDepth(0);

		overlaySprite(sprout_panel_spr, SPROUT_PANEL_X, SPROUT_PANEL_Y, SPROUT_OVERLAY_OT_DEPTH, 0xff808080);	//0xff10ff10);
	}
}

SPROUTSTR *sproutsFindFree()
{
	SPROUTSTR *oldest;
	SPROUTSTR *sprout;
	int i;

	sprout = &sproutman->sprouts[0];
	for(i = 0; i < MAX_SPROUTS; i++,sprout++)
	{
		if(!(sprout->flags & SPROUT_ACTIVE))
		{
			return sprout;
		}
	}

	oldest = sprout = &sproutman->sprouts[0];
	for(i = 0; i < MAX_SPROUTS; i++,sprout++)
	{
		if(sprout->age > oldest->age)
			oldest = sprout;
	}
	return oldest;
}


// dirn & speed rather than vel?
// position, dirn, and offset, rather than pos?
// Have a separate rou for that...
SPROUTSTR *sproutsThrow(VECTOR *pos, VECTOR *vel)
{
	int i;
	SPROUTSTR *sprout;

	if(gamePlayData->sprouts_carried <= 0)
		return NULL;

	sprout = sproutsFindFree();

	sprout->flags |= SPROUT_ACTIVE;
	sprout->age = 0;
	sprout->pos.vx = pos->vx;
	sprout->pos.vy = pos->vy;
	sprout->pos.vz = pos->vz;


//	sprout->vel.vx = vel->vx;
//	sprout->vel.vy = vel->vy;
//	sprout->vel.vz = vel->vz;


	sprout->collpos.vx = pos->vx<<9;
	sprout->collpos.vy = pos->vy<<9;
	sprout->collpos.vz = pos->vz<<9;

	sprout->collvel.vx = vel->vx<<9;
	sprout->collvel.vy = vel->vy<<9;
	sprout->collvel.vz = vel->vz<<9;

	sprout->colldata.oldPos = sprout->collpos;
	sprout->colldata.oldVel = sprout->collvel;

	sprout->colldata.nBoxes = enemyGlobals.numCollBoxes;
	sprout->colldata.pCollBox = enemyGlobals.pCollBoxes;

	sprout->colldata.radius = SPROUT_RADIUS << 9;
	sprout->colldata.bounce = 4096;

	sprout->colldata.pVel = &sprout->collvel;
	sprout->colldata.pPos = &sprout->collpos;

// TBD - stop player from throwing sprouts when they ain't got any ;)

	gamePlayData->sprouts_carried--;
//	{
//		char temp[20];
//		sprintf(temp,"Sprouts: %d",gamePlayData->sprouts_carried);
//		overlayDebugMessage(temp, 25 * 2);
//	}


	return sprout;
}


