/*
 * patrol.c
 * 
 * 950424 by Rastlin
 */

static int gPathpos = -1, gMovetime = 15;
static mixed gPath = ({ });
static int gAlarm_patrol;

static varargs mixed 
check_command(int add = 0)
{
    object cmdres;
    int tpos = gPathpos + add;

    if (tpos >= sizeof(gPath))
	tpos = 0;

    cmdres = gPath[tpos];

    if (stringp(cmdres))
	return this_object()->check_call(cmdres);
    else
	return cmdres;
}


int
atak()
{
  int i, ok, pom;
  object *p= this_object()->query_team();
  
  pom = sizeof(p);
  ok =0;
  for (i=0; (i<pom)&&!ok; i++)
    if (p[i]->query_attack())
      ok =1;
  return ok;
}


static void
patrol_heartbeat()
{
    int time;
    mixed cmdres;

    if (!sizeof(gPath))
	return;

    if ((this_object()->query_attack())||
       (atak()))
    {
	time = gMovetime + random(10);
	gAlarm_patrol = set_alarm(itof(time), 0.0, "patrol_heartbeat");
	return;
    }

    if (++gPathpos >= sizeof(gPath))
	gPathpos = 0;

    cmdres = check_command();

    if (intp(cmdres))
    {
	gAlarm_patrol = set_alarm(itof(cmdres), 0.0, "patrol_heartbeat");
	return;
    }
    else
	if (stringp(cmdres))
	{
	    command(cmdres);
	
	    cmdres = check_command(1);

	    if (intp(cmdres))
	    {
		gAlarm_patrol = set_alarm(itof(cmdres), 0.0, "patrol_heartbeat");
		return;
	    }

	    time = gMovetime + random(10);
	    gAlarm_patrol = set_alarm(itof(time), 0.0, "patrol_heartbeat");
	    return;
	}
}

/*
 * Function:	set_patrol_path
 * Arguments:	path - An array of string and integers.
 * Description:	Set the command path the NPC should follow.
 *		The elements in the array should be a string or an
 *		array containing a string and an integer. The integer
 *		will work as an delat value.
 *		The strings may be VBFC.
 * Example:	set_patrol_path(({"n", "unlock door", 3, "open door", "n",
 *			"close door", 4, "lock door", "n"}));
 * 		This means that it will take 3 seconds from that the door
 *		has been unlocked until the NPC opens the door and that it will
 *		take 4 seconds until the NPC locks it after that it has been closed.
 */
public void
set_patrol_path(mixed path)
{
    gPath = path;
}

/*
 * Function:	query_patrol_path
 * Description:	Return the command sequence
 * Returns:	mixed
 */
public mixed
query_patrol_path() { return gPath; }

/*
 * Function:	set_patrol_time
 * Arguments:	i - The delay in seconds
 * Description: Set the number of seconds it will delay between two moves.
 *		The real delay number will be i + random(10) seconds.
 */
public void
set_patrol_time(int i)
{
    gMovetime = i;
}

/*
 * Function:	query_patrol_time
 * Description: Return the delay time in seconds.
 * Returns:	An integer.
 */
public int
query_patrol_time() { return gMovetime; }

/*
 * Function:	start_patrol
 * Description: This will start the patroling
 */
public void
start_patrol()
{
    if (gAlarm_patrol)
	remove_alarm(gAlarm_patrol);

    gAlarm_patrol = set_alarm(itof(gMovetime + random(10)), 0.0, "patrol_heartbeat");
}

/*
 * Function:	stop_patrol
 * Description:	This will stop the patrol until it is started again.
 */
public void
stop_patrol()
{
    if (gAlarm_patrol)
	remove_alarm(gAlarm_patrol);
}




