AI-MegaHAL
view release on metacpan or search on metacpan
libmegahal.c view on Meta::CPAN
* Purpose: Display a character to stdout as if it was typed by a human.
*/
void typein(char c)
{
/*
* Standard keyboard delay
*/
usleep(D_KEY+rnd(V_KEY)-rnd(V_KEY));
fprintf(stdout, "%c", c);
fflush(stdout);
/*
* A random thinking delay
*/
if((!isalnum(c))&&((rnd(100))<P_THINK))
usleep(D_THINK+rnd(V_THINK)-rnd(V_THINK));
}
/*---------------------------------------------------------------------------*/
/*
* Function: Ignore
*
* Purpose: Log the occurrence of a signal, but ignore it.
*/
void ignore(int sig)
{
if(sig!=0) warn("ignore", "MegaHAL received signal %d", sig);
#if !defined(DOS)
// signal(SIGINT, saveandexit);
// signal(SIGILL, die);
// signal(SIGSEGV, die);
#endif
// signal(SIGFPE, die);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Die
*
* Purpose: Log the occurrence of a signal, and exit.
*/
void die(int sig)
{
error("die", "MegaHAL received signal %d", sig);
exithal();
}
/*---------------------------------------------------------------------------*/
/*
* Function: Rnd
*
* Purpose: Return a random integer between 0 and range-1.
*/
int rnd(int range)
{
static bool flag=FALSE;
if(flag==FALSE) {
#if defined(DOS) || defined(__mac_os) || defined(_MSC_VER) || defined(__MINGW32_VERSION) || defined(WIN32)
srand(time(NULL));
#else
srand48(time(NULL));
#endif
}
flag=TRUE;
#if defined(DOS) || defined(__mac_os) || defined(_MSC_VER) || defined(__MINGW32_VERSION) || defined(WIN32)
return(rand()%range);
#else
return(floor(drand48()*(double)(range)));
#endif
}
/*---------------------------------------------------------------------------*/
/*
* Function: Usleep
*
* Purpose: Simulate the Un*x function usleep. Necessary because
* Microsoft provide no similar function. Performed via
* a busy loop, which unnecessarily chews up the CPU.
* But Windows '95 isn't properly multitasking anyway, so
* no-one will notice. Modified from a real Microsoft
* example, believe it or not!
*/
#if defined(DOS) || defined(__mac_os)
void usleep(int period)
{
clock_t goal;
goal=(clock_t)(period*CLOCKS_PER_SEC)/(clock_t)1000000+clock();
while(goal>clock());
}
#endif
/*---------------------------------------------------------------------------*/
/*
* Function: Strdup
*
* Purpose: Provide the strdup() function for Macintosh.
*/
#ifdef __mac_os
char *strdup(const char *str)
{
char *rval=(char *)malloc(strlen(str)+1);
if(rval!=NULL) strcpy(rval, str);
return(rval);
}
#endif
/*---------------------------------------------------------------------------*/
/*
* Function: Initialize_Speech
*
* Purpose: Initialize speech output.
*/
#ifdef __mac_os
bool initialize_speech(void)
{
bool speechExists = false;
long response;
OSErr err;
( run in 1.821 second using v1.01-cache-2.11-cpan-39bf76dae61 )