X-Git-Url: http://git.shiar.net/netris.git/blobdiff_plain/e3d58186949bfcdb149cc2a545ce6c14a8689268..b5a9cdfd671a10cd5b5e9f1f53105e9993414247:/util.c diff --git a/util.c b/util.c index f93b8c8..791a007 100644 --- a/util.c +++ b/util.c @@ -30,19 +30,19 @@ #include #include -static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event); +#include "util.h" +static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event); static EventGenRec alarmGen = { &alarmGen, 0, FT_read, -1, AlarmGenFunc, EM_alarm }; static EventGenRec *nextGen = &alarmGen; -static sigjmp_buf close_env; - static int myRandSeed = 1; static long baseTimeval; -ExtFunc void AtExit(void (*handler)(void)) + +void AtExit(void (*handler)(void)) { #ifdef HAS_ON_EXIT on_exit((void *)handler, NULL); @@ -53,7 +53,7 @@ ExtFunc void AtExit(void (*handler)(void)) ///////////// HELP MESSAGES ///////////// -ExtFunc void Header(void) +void Header(void) { fprintf(stderr, "NETRIS %s\t(c) 1994-1996,1999 Mark H. Weaver \n" @@ -61,7 +61,7 @@ ExtFunc void Header(void) version_string); } //Header -ExtFunc void Usage(void) +void Usage(void) { Header(); fprintf(stderr, @@ -75,12 +75,10 @@ ExtFunc void Usage(void) " -a, --ascii\t\tUse ascii characters\n" " -C, --color=0\t\tDisable color\n" "\n" - " -w, --wait\t\tWait for connection\n" " -c, --connect \tInitiate connection\n" " -p, --port \tSet port number (default is %d)\n" "\n" - " -s, --seed \tStart with given random seed\n" - " -i, --speed \tSet the initial step-down interval, in seconds\n" + " -t, --team \tJoin a team (don't receive lines from your teammates)\n" " -l, --level \tBegin at a higher level (can be used as handicap)\n" " -k, --keys \tRemap keys (default is \"%s\" for cursors)\n" " -d, --dropmode\tDrops go into drop mode\n" @@ -91,9 +89,8 @@ ExtFunc void Usage(void) "\n", DEFAULT_PORT, DEFAULT_KEYS); } -ExtFunc void DistInfo(void) +void DistInfo(void) { - Header(); fprintf(stderr, "This program is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" @@ -111,7 +108,7 @@ ExtFunc void DistInfo(void) "\n"); } //DistInfo -ExtFunc void Rules(void) +void Rules(void) { Header(); fprintf(stderr, @@ -137,36 +134,25 @@ ExtFunc void Rules(void) ///////////// RANDOM ///////////// -ExtFunc void InitUtil(void) -{ - if (Game.seed) - SRandom(Game.seed); - else - SRandom(time(0)); - if (sigsetjmp(close_env, 1)) exit(0); - signal(SIGINT, CatchInt); - ResetBaseTime(); -} //InitUtil - /* * My really crappy random number generator follows * Should be more than sufficient for our purposes though */ -ExtFunc void SRandom(int seed) +void SRandom(int seed) { Game.seed = seed; myRandSeed = seed % 31751 + 1; } //SRandom -ExtFunc int Random(int min, int max1) -{ +int Random(int min, int max1) +{ //return a random value myRandSeed = (myRandSeed * 31751 + 15437) % 32767; return myRandSeed % (max1 - min) + min; } //Random ///////////// I/O ///////////// -ExtFunc int MyRead(int fd, void *data, int len) +int MyRead(int fd, void *data, int len) { int result, left; @@ -183,7 +169,7 @@ ExtFunc int MyRead(int fd, void *data, int len) return len; } //MyRead -ExtFunc int MyWrite(int fd, void *data, int len) +int MyWrite(int fd, void *data, int len) { int result, left; @@ -202,7 +188,7 @@ ExtFunc int MyWrite(int fd, void *data, int len) ///////////// TIME ///////////// -ExtFunc void NormalizeTime(struct timeval *tv) +void NormalizeTime(struct timeval *tv) { tv->tv_sec += tv->tv_usec / 1000000; tv->tv_usec %= 1000000; @@ -212,12 +198,7 @@ ExtFunc void NormalizeTime(struct timeval *tv) } } -ExtFunc void CatchInt(int sig) -{ - siglongjmp(close_env, 1); -} - -ExtFunc void CatchAlarm(int sig) +void CatchAlarm(int sig) { alarmGen.ready = 1; signal(SIGALRM, CatchAlarm); @@ -228,18 +209,18 @@ static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event) return E_alarm; } -ExtFunc void SetTimeval(struct timeval *tv, long usec) +void SetTimeval(struct timeval *tv, long usec) { tv->tv_sec = usec / 1000000; tv->tv_usec = usec % 1000000; } //SetTimeval -ExtFunc long GetTimeval(struct timeval *tv) +long GetTimeval(struct timeval *tv) { return tv->tv_sec * 1000000 + tv->tv_usec; } //GetTimeval -ExtFunc long AbsTimeval(void) +long AbsTimeval(void) { struct timeval tv; @@ -247,22 +228,22 @@ ExtFunc long AbsTimeval(void) return GetTimeval(&tv); } //CurTimeval -ExtFunc void ResetBaseTime(void) -{ +void ResetBaseTime(void) +{ //Reset the timer baseTimeval = AbsTimeval(); } //ResetBaseTime -ExtFunc void PauseTime(void) -{ +void PauseTime(void) +{ //Pause the timer baseTimeval -= AbsTimeval(); } //PauseTime -ExtFunc void ResumeTime(void) -{ +void ResumeTime(void) +{ //Unpause timer baseTimeval += AbsTimeval(); } //ResumeTime -ExtFunc long CurTimeval(void) +long CurTimeval(void) { struct timeval tv; @@ -282,7 +263,7 @@ static long SetITimer1(long interval, long value) return GetTimeval(&old.it_value); } -ExtFunc long SetITimer(long interval, long value) +long SetITimer(long interval, long value) { long old; @@ -294,19 +275,19 @@ ExtFunc long SetITimer(long interval, long value) ///////////// ... ///////////// -ExtFunc volatile void die(char *msg) +volatile void die(char *msg) { perror(msg); exit(1); } -ExtFunc volatile void fatal(char *msg) +volatile void fatal(char *msg) { fprintf(stderr, "%s\n", msg); exit(1); -} +} //fatal -ExtFunc void BlockSignals(MySigSet *saved, ...) +void BlockSignals(MySigSet *saved, ...) { MySigSet set; va_list args; @@ -331,9 +312,9 @@ ExtFunc void BlockSignals(MySigSet *saved, ...) *saved = sigblock(set); #endif va_end(args); -} +} //BlockSignals -ExtFunc void RestoreSignals(MySigSet *saved, MySigSet *set) +void RestoreSignals(MySigSet *saved, MySigSet *set) { #ifdef HAS_SIGPROCMASK sigprocmask(SIG_SETMASK, set, saved); @@ -343,18 +324,18 @@ ExtFunc void RestoreSignals(MySigSet *saved, MySigSet *set) else sigsetmask(*set); #endif -} +} //RestoreSignals ///////////// EVENTS ///////////// -ExtFunc void AddEventGen(EventGenRec *gen) +void AddEventGen(EventGenRec *gen) { assert(gen->next == NULL); gen->next = nextGen->next; nextGen->next = gen; } //AddEventGen -ExtFunc void RemoveEventGen(EventGenRec *gen) +void RemoveEventGen(EventGenRec *gen) { // assert(gen->next != NULL); /* Be more forgiving, for SIGINTs */ if (gen->next) { @@ -365,7 +346,7 @@ ExtFunc void RemoveEventGen(EventGenRec *gen) } } //RemoveEventGen -ExtFunc MyEventType WaitMyEvent(MyEvent *event, int mask) +MyEventType WaitMyEvent(MyEvent *event, int mask) { //poll int i, retry = 0; fd_set fds[FT_len];