From 3b453bab5c990f862a4915ae4e5e4a0e90b167f2 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Tue, 27 Feb 2007 15:34:29 +0100 Subject: [PATCH] code cosmetics --- board.c | 19 +++++++-------- curses.c | 33 +++++++++++++------------- game.c | 70 +++++++++++++++++++++++++++++++------------------------- inet.c | 5 ++-- server.c | 19 +++++++++------ util.c | 17 +++++++++----- 6 files changed, 92 insertions(+), 71 deletions(-) diff --git a/board.c b/board.c index c6c4837..9044f78 100644 --- a/board.c +++ b/board.c @@ -94,10 +94,10 @@ int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func) int i, j, result; char type, rotation; - type = s/4; - rotation = s&3; - for (i = 0; i<4; i++) - for (j = 0; j<4; j++) + type = s / 4; + rotation = s & 3; + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) if (shapes[type][rotation][i][j]) if (result = func(scr, y-i, x+j, shapes[type][rotation][i][j])) return result; @@ -112,9 +112,10 @@ char ChooseOption(float options[7]) int i; float total = 0, val; - for (i = 0; i<7; i++) total += options[i]; - val = Random(0, 32767)/32768.0*total; - for (i = 0; i<7; i++) if ((val -= options[i])<0) return i<<2; + for (i = 0; i < 7; i++) total += options[i]; + val = Random(0, 32767) / 32768.0 * total; + for (i = 0; i < 7; i++) if ((val -= options[i]) < 0) + return i << 2; return 0; } @@ -319,7 +320,7 @@ int CheckFall(int scr) unsigned char z; if (!Game.gravity) return 0; - for (y = Players[scr].boardHeight-1; y > 0; y--) + for (y = Players[scr].boardHeight - 1; y > 0; y--) for (x = 0; x < Players[scr].boardWidth; x++) { if (((z = GetBlock(scr, y, x)) > BT_none) && ((z & 160) == 0)) { //doesn't stick left/up => topleft block @@ -362,7 +363,7 @@ int ClearFullLines(int scr) from++; //skip for (x = 0; x1) + if (from > 1) SetBlock(scr, from-2, x, GetBlock(scr, from-2, x)&223); } //don't stick blocks to line which we'll remove } //full lines diff --git a/curses.c b/curses.c index bece839..a8af702 100644 --- a/curses.c +++ b/curses.c @@ -38,9 +38,10 @@ #endif static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event); -static EventGenRec keyGen = -// { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key }; - { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key }; +static EventGenRec keyGen = { +// NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key + NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key +}; static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS]; static int boardSize[MAX_SCREENS]; @@ -51,12 +52,12 @@ WINDOW *msgwin; static int haveColor; int PlayerDisp[MAX_SCREENS]; -#define MSG_HEIGHT 64 //max history +#define MSG_HEIGHT 64 //max history char *message[MSG_HEIGHT]; char messages[MSG_HEIGHT][MSG_WIDTH]; -static char *term_vi; /* String to make cursor invisible */ -static char *term_ve; /* String to make cursor visible */ +static char *term_vi; /* String to make cursor invisible */ +static char *term_ve; /* String to make cursor visible */ void InitScreens(void) { @@ -178,12 +179,12 @@ void GetTermcapInfo(void) "vt100", "vt101", "vt102", "vt200", "vt220", "vt300", "vt320", "vt400", "vt420", - "screen", "xterm", NULL }; + "screen", "xterm", NULL + }; int i; for (i = 0; vts[i]; i++) - if (!strcmp(term, vts[i])) - { + if (!strcmp(term, vts[i])) { term_vi = "\033[?25l"; term_ve = "\033[?25h"; break; @@ -391,9 +392,9 @@ void Messagetype(char c, int x, char *s) mvwaddch(msgwin, messageHeight-1, (x+1) % (messageWidth-1), ' '); } //escape else { - if (c == 13 || c==127) //enter/backspace + if (c == 13 || c == 127) //enter/backspace mvwaddch(msgwin, messageHeight - 1, (x+2) % (messageWidth-1), - x>=messageWidth-3 ? s[x - messageWidth + 3] : ' '); + x >= messageWidth-3 ? s[x - messageWidth + 3] : ' '); else //any character mvwaddch(msgwin, messageHeight - 1, x % (messageWidth-1), c); mvwaddch(msgwin, messageHeight - 1, (x+1) % (messageWidth-1), '_'); @@ -499,8 +500,8 @@ void PlotBlock1S(int y, int x, unsigned char type) } void PlotBlock(int scr, int y, int x, unsigned char type) { - if (y >= 0 && y < Players[scr].boardVisible && - x >= 0 && x < Players[scr].boardWidth) { + if (y >= 0 && y < Players[scr].boardVisible + && x >= 0 && x < Players[scr].boardWidth) { if (boardSize[scr] > 1) PlotBlock1(boardYPos[scr] - y, boardXPos[scr] + 2*x, type); else @@ -599,10 +600,10 @@ void ScheduleFullRedraw(void) void CatchWinCh(int sig) { //handle window resize - endwin(); //exit curses - refresh(); //and reinit display (with different sizes) + endwin(); //exit curses + refresh(); //and reinit display (with different sizes) InitFields(); //manually redraw everything - refresh(); //refresh + refresh(); //refresh } static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event) diff --git a/game.c b/game.c index f26e07a..d7214dc 100644 --- a/game.c +++ b/game.c @@ -52,14 +52,17 @@ static struct option options[] = { { 0, 0, 0, 0 } }; -enum { KT_left, KT_right, KT_rotright, KT_rotleft, KT_drop, KT_down, - KT_faster, KT_pause, KT_redraw, KT_quit, KT_numKeys }; +enum { + KT_left, KT_right, KT_rotright, KT_rotleft, KT_drop, KT_down, + KT_faster, KT_pause, KT_redraw, KT_quit, KT_numKeys +}; static char *keyNames[KT_numKeys+1] = { "Left", "Right", "RotRight", "RotLeft", "Drop", "Down", - "Faster", "Pause", "Redraw", "Quit", NULL }; + "Faster", "Pause", "Redraw", "Quit", NULL +}; -_Sets Sets = {7, 0, 1, 1, 1}; //Sets +_Sets Sets = {7, 0, 1, 1, 1}; static char keyTable[KT_numKeys+1]; @@ -159,16 +162,21 @@ void HandleOption(char tag, char *value) else Sets.standout = 0; break; case 'k': //keys - MapKeys(value); break; + MapKeys(value); + break; case 'H': //info Header(); - DistInfo(); exit(0); + DistInfo(); + exit(0); case 'R': //rules - Rules(); exit(0); + Rules(); + exit(0); case 'h': //help - Usage(); exit(0); + Usage(); + exit(0); default: - Usage(); exit(1); + Usage(); + exit(1); } } @@ -185,7 +193,7 @@ void ReadConf(char *filename) while (fgets(buf, 512, file_in) != NULL) { if ((ch = strchr(buf, '#'))) *ch = '\0'; // truncate string from # char - for (i = strlen(buf)-1; i >= 0; i--) + for (i = strlen(buf) - 1; i >= 0; i--) if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\t' || buf[i] == 13) buf[i] = '\0'; else break; @@ -372,7 +380,7 @@ void OneGame(void) if (!Sets.dropmode) gameStatus = 1; //instadrop } else gameStatus = 1; //dropped - dropMode = Sets.dropmode>1; + dropMode = Sets.dropmode > 1; break; case KT_faster: if (game != GT_onePlayer) break; @@ -463,11 +471,11 @@ void OneGame(void) netint4 data[3]; short column; - if (Players[me].alive<=0) break; + if (Players[me].alive <= 0) break; memcpy(data, net.data, sizeof(data[0])); column = Random(0, Players[me].boardWidth); Message("\\%d%s sends %d lines", - Players[net.uid].team>7 ? 7 : Players[net.uid].team, + Players[net.uid].team > 7 ? 7 : Players[net.uid].team, Players[net.uid].name, data[0]); lastadd = net.uid; InsertJunk(me, Players[net.uid].team, data[0], column); @@ -481,7 +489,7 @@ void OneGame(void) case NP_msg: { Message("<\\%d%s\\7> %s", - Players[net.uid].team>7 ? 7 : Players[net.uid].team, + Players[net.uid].team > 7 ? 7 : Players[net.uid].team, Players[net.uid].name, net.data, net.type); break; } //chat @@ -492,30 +500,30 @@ void OneGame(void) Game.started = 2; paused = 0; Message("The game has started"); - for (i = 1; i0) + for (i = 1; i < MAX_SCREENS; i++) if (Players[i].alive > 0) ShowPause(i); break; } //start game case NP_stop: { - if (Game.started>1) { + if (Game.started > 1) { int winner; float timer; int i; Message("The game has ended"); - timer = CurTimeval()/1e6; - if (timer>5) { - for (i = MAX_SCREENS-1; i>0; i--) if (Players[i].alive>=0) { + timer = CurTimeval() / 1e6; + if (timer > 5) { + for (i = MAX_SCREENS-1; i > 0; i--) if (Players[i].alive >= 0) { Message("\\%d%10s%6.1fp%5.1fa", - Players[i].team>7 ? 7 : Players[i].team, Players[i].name, - Players[i].score.pieces/timer*60, - Players[i].score.adds/timer*60); - if (Players[i].alive>0) winner = i; + Players[i].team > 7 ? 7 : Players[i].team, Players[i].name, + Players[i].score.pieces / timer * 60, + Players[i].score.adds / timer * 60); + if (Players[i].alive > 0) winner = i; } //show player stats if (winner) Message("%s won after %0.0f'%02d\"", - Players[winner].name, timer/60, (int)timer%60); + Players[winner].name, timer / 60, (int)timer % 60); } //show game stats Message(NULL); } //game was playing @@ -524,7 +532,7 @@ void OneGame(void) { int i; - for (i = 1; i=0) { + for (i = 1; i < MAX_SCREENS; i++) if (Players[i].alive >= 0) { Players[i].alive = 1; Players[i].flags |= SCF_paused; } //reset players @@ -546,12 +554,12 @@ void OneGame(void) memcpy(&Players[net.uid], net.data, net.size); ClearField(net.uid); InitFields(); - if (Players[net.uid].team>7) + if (Players[net.uid].team > 7) Message("%s joined the game", Players[net.uid].name); else Message("%s joined %s team", Players[net.uid].name, teams[Players[net.uid].team]); - if (Players[net.uid].flags&SCF_paused) { + if (Players[net.uid].flags & SCF_paused) { checkPaused(); } //player has paused // DrawField(net.uid); @@ -564,7 +572,7 @@ void OneGame(void) char s[20]; Players[net.uid].flags ^= SCF_paused; - if (Game.started>1) + if (Game.started > 1) strcpy(s, Players[net.uid].flags&SCF_paused ? "paused the game" : "unpaused the game"); else @@ -593,13 +601,13 @@ void OneGame(void) memcpy(&i, net.data, sizeof(i)); Players[net.uid].alive = 0; if (i == me) Message("\\%dYou fragged %s", - Players[me].team>7 ? 7 : Players[me].team, Players[net.uid].name); + Players[me].team > 7 ? 7 : Players[me].team, Players[net.uid].name); else if (i==net.uid) Message("\\%d%s died", - Players[i].team>7 ? 7 : Players[i].team, Players[i].name); + Players[i].team > 7 ? 7 : Players[i].team, Players[i].name); else Message("\\%d%s fragged %s", - Players[i].team>7 ? 7 : Players[i].team, Players[i].name, + Players[i].team > 7 ? 7 : Players[i].team, Players[i].name, Players[net.uid].name); checkPaused(); ShowPause(net.uid); diff --git a/inet.c b/inet.c index ca3230c..6dcb0aa 100644 --- a/inet.c +++ b/inet.c @@ -37,8 +37,9 @@ #define HEADER_SIZE3 sizeof(netint4[3]) MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event); -EventGenRec netGen = - { NULL, 0, FT_read, -1, NetGenFunc, EM_net, 0, "\0", 0, HEADER_SIZE3 }; +EventGenRec netGen = { + NULL, 0, FT_read, -1, NetGenFunc, EM_net, 0, "\0", 0, HEADER_SIZE3 +}; static sigjmp_buf close_env; diff --git a/server.c b/server.c index 3d6453c..4c6582b 100644 --- a/server.c +++ b/server.c @@ -61,15 +61,18 @@ struct sockaddr_in addr; MyEventType NetGenFunc(EventGenRec *gen, MyEvent *event); static EventGenRec netGen[MAX_SCREENS] = { - { NULL, 0, FT_read, -1, NetGenFunc, EM_net, 0, "\0", 0, HEADER_SIZE } }; + { NULL, 0, FT_read, -1, NetGenFunc, EM_net, 0, "\0", 0, HEADER_SIZE } +}; 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 MyEventType ConnGenFunc(EventGenRec *gen, MyEvent *event); -static EventGenRec connGen = - { NULL, 0, FT_read, -1, ConnGenFunc, EM_connect }; +static EventGenRec connGen = { + NULL, 0, FT_read, -1, ConnGenFunc, EM_connect +}; static EventGenRec *nextGen = &alarmGen; @@ -220,8 +223,10 @@ int StartServer(void) int playersReady = 0; int paused = 1; int i; - char teams[10][7] = { "", "Green", "Cyan", "Blue", "Purple", - "Red", "Grey", "White", "*Orange" }; + char teams[10][7] = { + "", "Green", "Cyan", "Blue", "Purple", + "Red", "Grey", "White", "*Orange" + }; do { switch (WaitMyEvent(&event, EM_any)) { diff --git a/util.c b/util.c index fc70d57..4f702e6 100644 --- a/util.c +++ b/util.c @@ -33,8 +33,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; @@ -58,7 +59,8 @@ void Header(void) fprintf(stderr, "NETRIS %s\t(c) 1994-1996,1999 Mark H. Weaver \n" " \t(c) 2002 Shiar \n\n", - version_string); + version_string + ); } void Usage(void) @@ -86,7 +88,8 @@ void Usage(void) "\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); + "\n", DEFAULT_PORT, DEFAULT_KEYS + ); } void DistInfo(void) @@ -105,7 +108,8 @@ void DistInfo(void) "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"); + "\n" + ); } void Rules(void) @@ -129,7 +133,8 @@ void Rules(void) "multiple rows.\n" "\n" "The longest surviving player wins the game.\n" - "\n"); + "\n" + ); } ///////////// RANDOM ///////////// -- 2.30.0