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

	Title :			Building Lights Database Header File
	Filename :		lights.h
	Author :		Thomas Szirtes
	Revision :		1.1

	Notes :			Handles the flashing lights on the buildings

	Copyright :		(C) SEGA Europe 1998

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

/*
 *		Includes
 */

#include	<ninja.h>
#include	"ftypes.h"
#include	"future.h"
#ifndef USING_SET4
#include	<malloc.h>
#endif

/*
 *		Module Variables
 */

static	int				bldlightdb_count	=	0;
static	LIGHTINSTANCE	*bldlightdb			=	NULL;	/* light instance database - indexed */

static 	NJS_SPRITE		lightsp;

static	NJS_TEXANIM		bldlight_texanim[] = 
{
	{ 64,64,32,32,0,0,255, 255,0,0 },
};

static	NJS_SPRITE		lightsp;


/*---------------------------------- PUBLIC FUNCTIONS -----------------------------------------// */

/*--------------------------------------------------------------------------

  void BLDLIGHTS_Draw( LIGHTTEMPLATE *lt, Uint16 li )

  Parameters:
		lt	-	pointer to predefined lights template database
		li	-	index into database
  Returns:
 
  Notes:	Draws lights at the specified index in database on
				the current building for matrix
  --------------------------------------------------------------------------*/

void BLDLIGHTS_Draw( LIGHTTEMPLATE *lt, Uint16 li )
{
	int n;

	/* change addition mode */


	/* do each light - assume that related lights follow existing one */
	for (n=0;n<lt->count;n++)
	{
		/* check on/off status */
		if (bldlightdb[li].status&0x80)
		{
			lightsp.p.x		=	bldlightdb[li].pos.x; 
			lightsp.p.y		=	bldlightdb[li].pos.y; 
			lightsp.p.z		=	bldlightdb[li].pos.z; 
			njDrawSprite3D	( &lightsp, 0, NJD_SPRITE_ANGLE | NJD_SPRITE_SCALE | NJD_SPRITE_ALPHA );

		}
		li++;
	}
}



/*--------------------------------------------------------------------------

  void BLDLIGHTS_Init()

  Parameters:
 
  Returns:
 
  Notes:	Initialization function for light

  --------------------------------------------------------------------------*/

void BLDLIGHTS_Init()
{
	lightsp.tlist	=	&misc_texlist;
	lightsp.tanim	=	bldlight_texanim;
	lightsp.ang		=	0;
	lightsp.sx		=	lightsp.sy	=	0.8f;
}



/*--------------------------------------------------------------------------

  void BLDLIGHTS_AnalysisMap()
 
  Parameters:
 
  Returns:
 
  Notes:	Performs an analysis of map and builds database

  --------------------------------------------------------------------------*/

void BLDLIGHTS_AnalysisMap()
{
	int	i;
	int	count	=	0;
	int	id;
	int	n;
	LIGHTPOINT		*lp;

	/* First count number of lights in map */
	for	(i=0;i<instance_count;i++)
	{
		if(build_inst[i].id	< MAX_BUILDINGS)
		{
			if (buildings[build_inst[i].id].lighttemplate!=NULL)
			{
				count	+=	buildings[build_inst[i].id].lighttemplate->count;
			}
		}
	}

	/* allocate block - so we don't fragment memory too much */
	if(count==0) return;

#ifdef USING_SET4
	if (bldlightdb!=NULL)	syFree(bldlightdb);
#else
	if (bldlightdb!=NULL)	free(bldlightdb);
#endif
	
	/* Allocate correct memory for database */
#ifdef USING_SET4
	bldlightdb			=	syMalloc(sizeof(LIGHTINSTANCE)*count);
#else
	bldlightdb			=	malloc(sizeof(LIGHTINSTANCE)*count);
#endif
	bldlightdb_count	=	count;

	/* Initialize database */
	count	=	0;				/* reset count */
	
	for(i=0;i<instance_count;i++)
	{
		id	=	build_inst[i].id;
		if(id	< MAX_BUILDINGS)
		{
			if (buildings[id].lighttemplate != NULL)
			{
				lp							=	buildings[id].lighttemplate->pos;
				build_inst[i].lbdlightinst	=	count;

				for(n=0;n<buildings[id].lighttemplate->count;n++)
				{
					bldlightdb[count].status	=	lp->status;
					bldlightdb[count].timer		=	lp->counter;
					bldlightdb[count].pos.x		=	lp->pos.x;
					bldlightdb[count].pos.z		=	lp->pos.z;
					bldlightdb[count].pos.y		=	lp->pos.y;
					lp++;
					count++;
				}
			}
		}
	}
	
	BLDLIGHTS_Init();
}


/*--------------------------------------------------------------------------

  void BLDLIGHTS_Kill()
 
  Parameters:
 
  Returns:
 
  Notes:	Kill light database

  --------------------------------------------------------------------------*/

void BLDLIGHTS_Kill()
{
	/* check then free */
#ifdef USING_SET4
	if (bldlightdb!=NULL)	syFree(bldlightdb);
#else
	if (bldlightdb!=NULL)	free(bldlightdb);
#endif
}



/*--------------------------------------------------------------------------

  void BLDLIGHTS_Update(void)
 
  Parameters:
 
  Returns:
 
  Notes:	Perform one cycle of light - update on/off status
 
  --------------------------------------------------------------------------*/

void BLDLIGHTS_Update(void)
{
	int		i;

	for(i=0;i<bldlightdb_count;i++)
	{	
		if ( --bldlightdb[i].timer == 0 )
		{
			bldlightdb[i].timer		=	bldlightdb[i].status&0x7f;		/* restore timer (up to 127) */
			bldlightdb[i].status	^=	0x80;				/* change state */
		}
	}
}