X-Git-Url: http://git.shiar.nl/netris.git/blobdiff_plain/0e779d807aa1830dde2f4a75117fd16f5627dc76..HEAD:/util.c diff --git a/util.c b/util.c index a55aeb0..92a8908 100644 --- a/util.c +++ b/util.c @@ -15,8 +15,6 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * $Id: util.c,v 1.29 1999/05/16 06:56:33 mhw Exp $ */ #include "netris.h" @@ -33,8 +31,9 @@ #include "util.h" static MyEventType AlarmGenFunc(EventGenRec *gen, MyEvent *event); -static EventGenRec alarmGen = - { &alarmGen, 0, FT_read, -1, AlarmGenFunc, EM_alarm }; +static EventGenRec alarmGen = { + &alarmGen, 0, FT_read, -1, AlarmGenFunc, EM_alarm +}; static EventGenRec *nextGen = &alarmGen; static int myRandSeed = 1; @@ -56,82 +55,31 @@ void AtExit(void (*handler)(void)) void Header(void) { fprintf(stderr, - "NETRIS %s\t(c) 1994-1996,1999 Mark H. Weaver \n" - " \t(c) 2002 Shiar \n\n", - version_string); -} //Header - -void Usage(void) -{ - Header(); - fprintf(stderr, - "Usage: netris \n" - "\n" - " -h, --help\t\tPrint this usage information\n" - " -H, --info\t\tShow distribution and warranty information\n" - " -R, --rules\t\tShow game rules\n" - "\n" - " -S, --slowterm\tDisable inverse/bold/color for slow terminals\n" - " -a, --ascii\t\tUse ascii characters\n" - " -C, --color=0\t\tDisable color\n" - "\n" - " -c, --connect \tInitiate connection\n" - " -p, --port \tSet port number (default is %d)\n" - "\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" - " -D, --instadrop\tInstant drop\n" - "\n" - " -r, --robot \tExecute program to control the game instead of keyboard\n" - " -F, --fair-robot\tUse fair robot interface\n" - "\n", DEFAULT_PORT, DEFAULT_KEYS); + "NETRIS %s\t(c) 1994-1996,1999 Mark H. Weaver \n" + " \t(c) 2002 Shiar \n\n", + version_string + ); } 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" - "the Free Software Foundation; either version 2 of the License, or\n" - "(at your option) any later version.\n" - "\n" - "This program is distributed in the hope that it will be useful,\n" - "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" - "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" - "GNU General Public License for more details.\n" - "\n" - "You should have received a copy of the GNU General Public License\n" - "along with this program; if not, write to the Free Software\n" - "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n" - "\n"); -} //DistInfo - -void Rules(void) -{ - Header(); fprintf(stderr, - "One player mode\n" - "---------------\n" - "Good old Tetris. Scoring is like on the GameBoy version (so try to\n" - "remove as many lines at once as you can). After removing ten lines\n" - "you go to the next level, which will be faster thus making the game\n" - "harder to play.\n" - "\n" - "Two player mode\n" - "---------------\n" - "It's just like normal T*tris except that when you clear more than\n" - "one row with a single piece, the other player receives penalty lines\n" - "For clearing 2, 3 or 4 rows, respectively 1, 2 or 4 junk rows will\n" - "be added to the bottom of your opponent's board respectively.\n" - "The junk rows have exactly one empty column, which will line up for\n" - "multiple rows.\n" - "\n" - "The longest surviving player wins the game.\n" - "\n"); -} //Rules + "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" + "the Free Software Foundation; either version 2 of the License, or\n" + "(at your option) any later version.\n" + "\n" + "This program is distributed in the hope that it will be useful,\n" + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License\n" + "along with this program; if not, write to the Free Software\n" + "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n" + "\n" + ); +} ///////////// RANDOM ///////////// @@ -143,13 +91,13 @@ void SRandom(int seed) { Game.seed = seed; myRandSeed = seed % 31751 + 1; -} //SRandom +} int Random(int min, int max1) { //return a random value myRandSeed = (myRandSeed * 31751 + 15437) % 32767; return myRandSeed % (max1 - min) + min; -} //Random +} ///////////// I/O ///////////// @@ -168,7 +116,7 @@ int MyRead(int fd, void *data, int len) return result; } return len; -} //MyRead +} int MyWrite(int fd, void *data, int len) { @@ -185,7 +133,25 @@ int MyWrite(int fd, void *data, int len) return result; } return len; -} //MyWrite +} + +///////////// CONFIG ///////////// + +void WriteConf(void) +{ + FILE *file_out; + + file_out = fopen(CONFIG_FILE, "w"); + if (file_out == NULL) { + perror("Error writing config file"); + exit(1); + } + + fprintf(file_out, "### NETRIS %s Config file ###\n\n", version_string); + + fclose(file_out); + fprintf(stderr, "Wrote new game configuration to %s\n", CONFIG_FILE); +} ///////////// TIME ///////////// @@ -214,12 +180,12 @@ void SetTimeval(struct timeval *tv, long usec) { tv->tv_sec = usec / 1000000; tv->tv_usec = usec % 1000000; -} //SetTimeval +} long GetTimeval(struct timeval *tv) { return tv->tv_sec * 1000000 + tv->tv_usec; -} //GetTimeval +} long AbsTimeval(void) { @@ -227,30 +193,27 @@ long AbsTimeval(void) gettimeofday(&tv, NULL); return GetTimeval(&tv); -} //CurTimeval +} void ResetBaseTime(void) { //Reset the timer baseTimeval = AbsTimeval(); -} //ResetBaseTime +} void PauseTime(void) { //Pause the timer baseTimeval -= AbsTimeval(); -} //PauseTime +} void ResumeTime(void) { //Unpause timer baseTimeval += AbsTimeval(); -} //ResumeTime +} long CurTimeval(void) { - struct timeval tv; - - gettimeofday(&tv, NULL); - return GetTimeval(&tv) - baseTimeval; -} //CurTimeval + return AbsTimeval() - baseTimeval; +} static long SetITimer1(long interval, long value) { @@ -286,7 +249,7 @@ volatile void fatal(char *msg) { fprintf(stderr, "%s\n", msg); exit(1); -} //fatal +} void BlockSignals(MySigSet *saved, ...) { @@ -313,7 +276,7 @@ void BlockSignals(MySigSet *saved, ...) *saved = sigblock(set); #endif va_end(args); -} //BlockSignals +} void RestoreSignals(MySigSet *saved, MySigSet *set) { @@ -325,7 +288,7 @@ void RestoreSignals(MySigSet *saved, MySigSet *set) else sigsetmask(*set); #endif -} //RestoreSignals +} ///////////// EVENTS ///////////// @@ -334,7 +297,7 @@ void AddEventGen(EventGenRec *gen) assert(gen->next == NULL); gen->next = nextGen->next; nextGen->next = gen; -} //AddEventGen +} void RemoveEventGen(EventGenRec *gen) { @@ -345,7 +308,7 @@ void RemoveEventGen(EventGenRec *gen) nextGen->next = gen->next; gen->next = NULL; } -} //RemoveEventGen +} MyEventType WaitMyEvent(MyEvent *event, int mask) { //poll @@ -375,7 +338,7 @@ MyEventType WaitMyEvent(MyEvent *event, int mask) tv.tv_sec = 0; tv.tv_usec = (retry && !anyReady) ? 500000 : 0; result = select(FD_SETSIZE, &fds[FT_read], &fds[FT_write], - &fds[FT_except], anyReady ? &tv : NULL); + &fds[FT_except], anyReady ? &tv : NULL); } else { if (retry && !anyReady) @@ -384,9 +347,10 @@ MyEventType WaitMyEvent(MyEvent *event, int mask) } gen = nextGen; do { - if ((gen->mask & mask) - && (gen->ready || (result > 0 && gen->fd >= 0 - && FD_ISSET(gen->fd, &fds[gen->fdType])))) { + if ((gen->mask & mask) && (gen->ready || ( + result > 0 && gen->fd >= 0 + && FD_ISSET(gen->fd, &fds[gen->fdType]) + ))) { gen->ready = 0; event->type = gen->func(gen, event); if (event->type != E_none) { @@ -398,9 +362,5 @@ MyEventType WaitMyEvent(MyEvent *event, int mask) } while (gen != nextGen); retry = 1; } -} //WaitMyEvent +} -/* - * vi: ts=4 ai - * vim: noai si - */