/*
 * util.c
 */
#include "copyright.h"
#include "../config.h"

#include <stdio.h>
#include INC_MATH
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include "defs.h"
#include INC_STRINGS
#include INC_UNISTD
#include "struct.h"
#include "data.h"



/*
** Provide the angular distance between two angles.
*/
angdist(x, y)
u_char x, y;
{
    register u_char res;

    res = (u_char)ABS(x - y);
    if (res > 128)
	return(256 - (int) res);
    return((int) res);
}

#ifdef DEFINE_NINT
int
nint(x)
   double          x;
{
   return (int) x;              /* xx */
}

#endif /* DEFINE_NINT */


#ifndef HAVE_USLEEP
microsleep(microSeconds)
U_LONG microSeconds;
{
        u_int            Seconds, uSec;
        int                     nfds, readfds, writefds, exceptfds;
        struct  timeval         Timer;

        nfds = readfds = writefds = exceptfds = 0;

        if( (microSeconds == (U_LONG) 0)
                || microSeconds > (U_LONG) 4000000 )
        {
                errno = ERANGE;         /* value out of range */
                perror( "usleep time out of range ( 0 -> 4000000 ) " );
                return -1;
        }

        Seconds = microSeconds / (U_LONG) 1000000;
        uSec    = microSeconds % (U_LONG) 1000000;

        Timer.tv_sec            = Seconds;
        Timer.tv_usec           = uSec;

        if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
        {
		if (errno != EINTR) {
                	perror( "usleep (select) failed" );
                	return -1;
		}
        }

        return 0;
}
#endif

realNumShips(owner)
{
    int         i, num = 0;
    struct player       *p;

    for (i = 0, p = players; i < MAXPLAYER; i++, p++)
        if (p->p_status != PFREE && 
#ifdef OBSERVERS
            p->p_status != POBSERV &&
#endif
            !(p->p_flags & PFROBOT) &&
            p->p_team == owner)
                num++;
    return (num);
}
