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

	Title :			Tools Module 
	Filename :		tools.c
	Author :		Thomas Szirtes
	Revision :		1.0

	Notes :			Filled with tools you might want but don't know
					where to put

	Copyright :		(C) SEGA Europe 1998

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

/*
 *		INCLUDE
 */

#include "ninja.h"
#include "ftypes.h"


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

/*--------------------- OVERFLOW ROUTINES ------------------------------*/
#if 0
/* Just simple little database type routines */
/* This one removes an instance from a database */
void overflow_remove(Uint8 n)
{
	int l,m;
	for (m=0;m<overflow.max;m++)
	{
		if (overflow.data[m]==n)
		{
			overflow.max--;
			for(l=m;l<=overflow.max;l++)
				overflow.data[l]=overflow.data[l+1];	
			return;
		}
	}
}

/* This one adds an instance to the end of the database */
void overflow_add(Uint8 n)
{
	if (overflow.max==MAX_OVERFLOW) return;
	overflow.data[overflow.max]=n;
	overflow.max++;
}

#endif

/*--------------------------------------------------------------------------
 
  Uint8 GetBuildingShadow(Uint8 id, Sint8 *io, Sint8 *jo, Uint16 index, Uint8	orient)
 
  Parameters:
	id			identifier of building
	io			pointer to where to write i offset 
	jo			pointer to where to write j offset
	index		index into list
	orient		orientation of building (0-3)

  Returns:
	0			finished list
	1			not finished list

  Notes:
 
 --------------------------------------------------------------------------*/
Uint8 GetBuildingShadow(Uint8 id, Sint8 *io, Sint8 *jo, Uint16 index, Uint8	orient)
{
	Sint8	*sipt;
	Uint16	*uipt;

	uipt	=	(Uint16 *)buildings[id].shadow;
	sipt	=	(Sint8 *)(uipt+1);
	
	if (uipt[0]==0)
	{
		/* rectangle */
		if(orient==0 || orient==2)
		{
			*io		=	(index % sipt[0])-(sipt[0]>>1);
			*jo		=	(index / sipt[0])-(sipt[1]>>1);
		}
		else
		{
			*jo		=	(index % sipt[0])-(sipt[0]>>1);
			*io		=	(index / sipt[0])-(sipt[1]>>1);
		}
		if (index<(sipt[0]*sipt[1])) return 1; else return 0;
	}
	else
	{
		/* irregular shape */
		if (index>=uipt[0]) return 0;		/* out of range */
	
		if(orient==0 || orient==2)
		{
			*io		=	sipt[index<<1];
			*jo		=	sipt[(index<<1)+1];
		}
		else
		{
			*jo		=	sipt[index<<1];
			*io		=	sipt[(index<<1)+1];
		}

	}	
	return 1;	
}


/*--------------------------------------------------------------------------
 
  Float GenerateRandom(void)
 
  Parameters:
 
  Returns:
 
  Notes:
 	Very primitive
 
 --------------------------------------------------------------------------*/
Uint16 GenerateRandom(void)
{
	static Uint16 seed;

	seed	=	seed * 5 + 0x3573;

	return	(Uint16)seed;

}


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

  void SetTextureCache(NJS_TEXLIST *txl)
 
  Parameters:
 	obj				Object to convert textures
 
  Returns:
 
  Notes:
 	Sets the texture to load as cache
 
  --------------------------------------------------------------------------*/
void SetTextureCache(NJS_TEXLIST *txl)
{
	Uint16 n;
	for(n=0;n<txl->nbTexture;n++)
	{
		txl->textures[n].attr|=NJD_TEXATTR_CACHE;
	}
}



/*--------------------------------------------------------------------------
 
  void LocaltoGlobalTex(NJS_OBJECT *obj, NJS_TEXLIST *local, NJS_TEXLIST *global)
 
  Parameters:
 	obj				Object to convert textures
 	local			Old object texture list
 	global			Global texture list
 
  Returns:
 
  Notes:
 	Converts object (and children) so that materials point to global texture
 	list instead of local. ( I don't actually use this anymore )
 
  --------------------------------------------------------------------------*/
#if 0
void LocaltoGlobalTex(NJS_OBJECT *obj, NJS_TEXLIST *local, NJS_TEXLIST *global)
{
	Uint16 i,j;
	Uint16 *convert;

	static void RecurseObject(NJS_OBJECT *, Uint16 *);

	/* allocate conversion table */
	convert=malloc(local->nbTexture*sizeof(Uint16));
	if(convert==NULL) return;
	
	/* generate conversion table */
	for(i=0;i<local->nbTexture;i++)
	{
		convert[i]=1;
		for(j=0;j<global->nbTexture;j++)
		{
			if(local->textures[i].globalIndex==global->textures[j].globalIndex)
			{
				convert[i]=j;
				break;
			}
		}
	}

	RecurseObject(obj,convert);

	free(convert);
}


static void RecurseObject(NJS_OBJECT *obj, Uint16 *convert)
{
	int i;
	Uint16 matcnt;
	NJS_MODEL	*mdl;
	
	
	mdl=obj->model;
	if (mdl!=NULL)
	{
		/* count number of materials by looking at highest value material id */
		if(mdl->nbMeshset!=0)
		{
			matcnt	=	0;
			for(i=0;i<mdl->nbMeshset;i++)
			{
				if (mdl->meshsets[i].type_matId&0x1fff > matcnt) 
				{
					matcnt	=	mdl->meshsets[i].type_matId&0x1fff;		
				}
			}

			for(i=0;i<=matcnt;i++)
			{
				if(mdl->mats[i].attr_texId!=-1) mdl->mats[i].attr_texId=convert[mdl->mats[i].attr_texId];
			}
		}
	}
		/* recurse */
	if(obj->child!=NULL)	RecurseObject(obj->child,convert);
	if(obj->sibling!=NULL)	RecurseObject(obj->sibling,convert);
}
#endif