/////////////////////////////////////////////////////////////////////////////////
/// treasure list functions
///
/// the treasure list is a linked list of items a player/bot has seen on a map
/// each node has an item name and a pointer to the node it is located in
/////////////////////////////////////////////////////////////////////////////////

// user interface file between katana and dlls

#include	"world.h"
#include	"nodelist.h"

/*
===============
skat_createlist
===============
*/

skatNode_t	*skat_createlist (skatNode_t *skat_list)
{
	return	(skatNode_t *) NULL;
}

/*
===============
skat_add_item
===============
*/

skatNode_t	*skat_add_item (skatNode_t *skat_list, mapNode_t *cur_node, userEntity_t *item, float dist)
{
	skatNode_t	*head = skat_list;
	skatNode_t	*new_node, *last_node;
	float		dist_from_node;
	Vector		temp;

	if (head == NULL)
	{
		// set up the first node in the skat_list
		head = (skatNode_t *) com->malloc (sizeof (skatNode_t), MEM_MALLOC);
		head->map_node = cur_node;
		head->item = item;
		head->next_node = NULL;

		gstate->Con_Printf ("skat_add_item: adding first item ");
		if (item->className)
			gstate->Con_Dprintf ("%s\n", item->className);
		
		return	head;
	}
	else
	{
		while (head)
		{
			// determine if this item is already in the skat_list
			if (head->item == item)
			{
				if (head->item->className)
					gstate->Con_Dprintf ("skat_item: %s\n", head->item->className);

				temp = Vector (head->map_node->origin) - Vector (item->s.origin);
				dist_from_node = temp.vlen ();

				// is the old node for this item further away?
				// if so, replace this skat_list with a pointer to cur_node
				if (dist_from_node > dist)
				// if so, replace this skat_list with a pointer to cur_node
				{
					gstate->Con_Dprintf ("skat_add_item: replacing item ");
					if (item->className)
						gstate->Con_Dprintf ("%s\n", item->className);

					head->map_node = cur_node;
				}
				else
				{
					gstate->Con_Dprintf ("skat_add_item: item ");
					if (item->className)
						gstate->Con_Dprintf ("%s already logged\n", item->className);
				}

				return	skat_list;
			}

			last_node = head;
			head = head->next_node;
		}
	}

	// append a new node to the end of the list
	gstate->Con_Dprintf ("skat_add_item: appending new node ");
	if (item->className)
		gstate->Con_Dprintf ("%s\n", item->className);

	new_node = (skatNode_t *) com->malloc (sizeof (skatNode_t), MEM_MALLOC);
	last_node->next_node = new_node;
	new_node->map_node = cur_node;
	new_node->item = item;
	new_node->next_node = NULL;

	return	skat_list;
}	

/*
============
skat_show_list
============
*/

void	skat_show_list	(userEntity_t *self)
{
	skatNode_t		*head;
	playerHook_t	*hook = (playerHook_t *) self->userHook;
	int			i;

//	gstate->Con_Dprintf ("skat_list --\n");

	head = hook->skat_list;
	i = 1;
	while (head)
	{
//		if (head->item->className)		
//			gstate->Con_Dprintf ("item #%i, %s\n", i, head->item->className);

		head = head->next_node;
		i++;
	}
}

/*
===========
skat_find_item

returns the map node of the matching item in self's skat_list, or returns NULL if no
matching item was found in the skat_list
===========
*/

mapNode_t	*skat_find_item	(userEntity_t *self, char *className)
{
	skatNode_t		*head;
	playerHook_t	*hook = (playerHook_t *) self->userHook;

	head = hook->skat_list;

	while (head)
	{
		if (head->item->className [0] == className [0])		
			if (strcmp (head->item->className, className) == 0)
				return	head->map_node;

		head = head->next_node;
	}

	return	NULL;
}