#include <stdio.h>
#include <string.h>
#include "struct.h"

/*
 *	Routines for finding items. These do simple linear searches on
 *	big dbases keep list of children of items, and hashed lists
 *	by noun.
 */

 
/*
 *	Locate an item in a given item.
 */
 
tag FindIn(i,a,n)
tag i;
int a,n;
{
	int ct=OBJECT(i)->ob_Child;
	while(ct!= -1)
	{
		if(ISITEM(ct)&&(LOC(ct)==i)&&((ADJ(ct)==a||ADJ(ct)==-1||a==-1)&&NOUN(ct)==n)&&(GCANSEE(Me(),ct)))
			return(ct);
		ct=OBJECT(ct)->ob_Next;
	}
	return(-1);
}

/*
 *	Find first matching item in game, very inefficient should be sorted or
 *	hashed some way
 */
 
tag FindAny(a,n)
int a,n;
{
	int ct=ISOBJ(0);
	while(ct<ISOBJ(NObs()))
	{
		if(ISITEM(ct)&&(ADJ(ct)==a||ADJ(ct)==-1||a==-1)&&NOUN(ct)==n&&GCANSEE(Me(),ct))
			return(ct);
		ct++;
	}
	return(-1);
}
	
tag NextIn(i,a,n,ct)
tag i;
int a,n;
tag ct;
{
	if(ct==-1)
		ct=OBJECT(i)->ob_Child;
	else
		ct=OBJECT(ct)->ob_Next;
	while(ct != -1)
	{
		if(ISITEM(ct)&&(LOC(ct)==i)&&((ADJ(ct)==a||ADJ(ct)==-1||a==-1)&&NOUN(ct)==n)&&GCANSEE(Me(),ct))
			return(ct);
		ct=OBJECT(ct)->ob_Next;
	}
	return(-1);
}

tag NextAny(a,n,ct)
int a,n;
tag ct;
{
	ct++;
	while(ct<ISOBJ(NObs()))
	{
		if(ISITEM(ct)&&(ADJ(ct)==a||a==-1)&&NOUN(ct)==n&&GCANSEE(Me(),ct))
			return(ct);
		ct++;
	}
	return(-1);
}