2 * Netris -- A free networked version of T*tris
3 * Copyright (C) 1994,1995,1996 Mark H. Weaver <mhw@netris.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/types.h>
28 #include <netinet/in.h>
37 static struct option options[] = {
38 { "ascii", 2, 0, 'a' },
39 { "connect", 1, 0, 'c' },
40 { "port", 1, 0, 'p' },
41 { "level", 1, 0, 'l' },
42 { "nick", 1, 0, 'n' },
43 { "team", 1, 0, 't' },
44 { "color", 2, 0, 'C' },
45 { "slowterm", 2, 0, 'S' },
46 { "keys", 1, 0, 'k' },
47 { "info", 0, 0, 'H' },
48 { "help", 0, 0, 'h' },
53 KT_left, KT_right, KT_rotright, KT_rotleft, KT_drop, KT_dropsoft, KT_down,
54 KT_faster, KT_pause, KT_redraw, KT_say, KT_quit, KT_numKeys
57 static char *keyNames[KT_numKeys+1] = {
58 "Left", "Right", "RotRight", "RotLeft", "Drop", "DropSoft", "Down",
59 "Faster", "Pause", "Redraw", "Say", "Quit", NULL
62 sets_t Sets = {3, 0, 1, 1, 1};
64 static char keyTable[KT_numKeys+1];
70 static char *cmds[] = {
75 static bool paused = 0;
79 static void handle_setkeys(char *newKeys)
86 /* XXX assumptions about ASCII encoding here */
87 for (i = k = 0; newKeys[i] && k < KT_numKeys; i++,k++) {
88 if (newKeys[i] == '^' && newKeys[i+1])
89 keyTable[k] = toupper(newKeys[++i]) - ('A' - 1);
91 keyTable[k] = newKeys[i];
93 memset(used, 0, sizeof(used));
94 for (k = 0; k < KT_numKeys; k++) {
95 ch = (unsigned char) keyTable[k];
97 if (iscntrl(ch) && ch < ' ')
98 sprintf(scratch, "Ctrl-%c", ch + ('A' - 1));
100 sprintf(scratch, "\"%c\"", ch);
102 sprintf(scratch, "0x%X", ch);
104 fprintf(stderr, "Duplicate key mappings:\n");
106 fprintf(stderr, " %s mapped to both %s and %s\n",
107 scratch, keyNames[used[ch]-1], keyNames[k]);
115 static void Usage(void)
119 "Usage: netris <options>\n"
121 " -h, --help\t\tPrint this usage information\n"
122 " -H, --info\t\tShow distribution and warranty information\n"
124 " -S, --slowterm\tDisable inverse/bold/color for slow terminals\n"
125 " -a, --ascii\t\tUse ascii characters\n"
126 " -C, --color=0\t\tDisable color\n"
128 " -c, --connect <host>\tInitiate connection\n"
129 " -p, --port <port>\tSet port number (default is %d)\n"
131 " -t, --team <team>\tJoin a team (don't receive lines from your teammates)\n"
132 " -l, --level <lvl>\tBegin at a higher level (can be used as handicap)\n"
133 " -k, --keys <keys>\tRemap keys (default is \"%s\" for cursors)\n"
135 DEFAULT_PORT, DEFAULT_KEYS
139 static void handle_arg(char tag, char *value)
143 Sets.ascii = value && !strcasecmp(value, "0") ? 0 : 1;
144 Sets.drawstyle &= ~Sets.ascii;
147 Game.type = GT_classicTwo;
153 case 'i': //speed (of level 1)
154 Game.initspeed = atof(value) * 1e6;
157 Players[0].score.level = MIN(MAX(atof(value), 1), 15);
160 memcpy(Players[0].name, value, strlen(value) + 1);
163 Players[0].team = atoi(value);
166 Sets.color = value && strcasecmp(value, "0") ? 1 : 0;
169 Sets.standout = value && !strcasecmp(value, "0") ? 1 : 0;
172 handle_setkeys(value);
187 static void handle_conffile(char *filename)
193 char tag[81], value[81];
195 file_in = fopen(filename, "r");
197 while (fgets(buf, 512, file_in) != NULL) {
198 if ((ch = strchr(buf, '#')))
199 *ch = '\0'; // truncate string from # char
200 for (i = strlen(buf) - 1; i >= 0; i--)
201 if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\t' || buf[i] == 13)
205 sscanf(buf, "%80[^= \t] = %80[^\n]", tag, value);
206 for (i = 0; options[i].name; i++){
207 if (!strcasecmp(options[i].name, tag)) {
208 handle_arg(options[i].val, value);
216 fprintf(stderr, "Unable to open config file %s.\n", filename);
220 static int player_piece(int scr, char shape)
222 Players[scr].score.pieces++;
224 Players[scr].curShape = Players[scr].nextShape;
225 Players[scr].nextShape = shape;
227 Players[scr].curY = Players[scr].boardVisible + 4;
228 Players[scr].curX = Players[scr].boardWidth / 2 - 2;
229 while (!shape_visible(Players[scr].curShape, scr,
230 Players[scr].curY, Players[scr].curX))
232 if (!shape_get(Players[scr].curShape, scr,
233 Players[scr].curY, Players[scr].curX))
235 shape_draw(Players[scr].curShape, scr,
236 Players[scr].curY, Players[scr].curX, scr == me);
240 static void game_setpaused(void)
241 { //check whether anyone paused the game
244 paused = Game.started < 1;
245 for (i = 1; i < MAX_SCREENS; i++) if (Players[i].alive > 0)
246 paused |= (Players[i].flags & SCF_paused) != 0;
249 static void game_reset(void)
256 // speed = MAX(initspeed / SPEEDINC ** (level - 1), SPEEDMINIMUM)
257 Game.speed = Game.initspeed;
258 for (i = 1; i < Players[me].score.level; i++)
259 Game.speed /= SPEEDINC;
260 if (Game.speed < SPEEDMINIMUM)
261 Game.speed = SPEEDMINIMUM;
263 ResetBaseTime(); //reset timer
264 SetITimer(Game.speed, Game.speed);
265 Players[me].nextShape = ChooseOption(Game.shapes);
266 for (i = 1; i <= maxPlayer; i++) {
267 Players[i].score.score = Players[i].score.lines
268 = Players[i].score.adds = 0;
269 Players[i].score.pieces = -1;
271 } //reset all players
275 static void game_clear(int scr)
276 { //check for full lines
278 int linevalues[] = { 40, 100, 400, 1200, }; //= 50*lines! - 10*(lines==1)
279 // int linevaluesq[] = { 25, 50, 100, 200, 500, 720, 980, 1280, 1620, 2000,
280 // 2420, 2880, 3380, 3920, 4500, 5120, 5780, 6480 };
281 int linevaluesq[] = { 20, 50, 100, 200, 500, 750, 1000, 1250, 1500, 2000,
282 2500, 3000, 3500, 4000, 4500, 5000, 6000, 7500 };
284 if ((linesCleared = player_lineclear(scr)) > 0) {
285 if (Game.type == GT_onePlayer)
286 if ((Players[scr].score.lines / 10) <
287 ((Players[scr].score.lines+linesCleared)/10)) {
288 if ((Game.speed /= SPEEDINC) < SPEEDMINIMUM)
289 Game.speed = SPEEDMINIMUM;
290 SetITimer(Game.speed, SetITimer(0, 0));
291 Players[scr].score.level++;
293 Players[scr].score.score += Game.gravity
294 ? linevaluesq[linesCleared - 1] : linevalues[linesCleared - 1];
295 Players[scr].score.lines += linesCleared;
296 Players[scr].score.adds += linesCleared - (linesCleared < 4); //XXX match handicap
298 if (Game.type == GT_classicTwo) {
299 SendPacket(scr, NP_clear, 0, NULL);
300 if (linesCleared > 1) {
304 junkLines = linesCleared - (Game.gravity ? 1 : linesCleared < 4);
306 SendPacket(me, NP_giveJunk, sizeof(data), data);
307 msg_add("\\%d" MSG_GAME_LINES_SELF,
308 Players[me].team > 7 ? 7 : Players[me].team, junkLines);
309 } //send junk to others
312 msg_add("\\%d" MSG_GAME_LINE_SELF,
313 Players[me].team > 7 ? 7 : Players[me].team, linesCleared);
319 static void game_loop(void)
322 short gameStatus = 2; //2=loop; 1=new piece; 0=quit
324 char chatText[MSG_WIDTH] = "\0";
326 void handle_cmd(char cmd, char *arg)
330 window_msg_status(me);
335 if (Players[me].alive <= 0) return;
336 Players[me].flags ^= SCF_paused;
337 if (Game.started > 1)
338 msg_add(Players[me].flags & SCF_paused
339 ? MSG_GAME_PAUSE_SELF : MSG_GAME_UNPAUSE_SELF);
341 msg_add(Players[me].flags & SCF_paused
342 ? MSG_GAME_UNREADY_SELF : MSG_GAME_READY_SELF);
344 if (Game.type == GT_classicTwo)
345 SendPacket(me, NP_pause, 0, NULL);
346 window_msg_status(me);
352 void handle_cmdstr(char *cmd)
354 char tag[17], value[81];
358 if ((cmdend = strchr(cmd, ' '))) {
361 cmdend = cmd + strlen(cmd); // whole string
363 for (i = 0; i < CT_MAX; i++){
364 if (!strcasecmp(cmds[i], cmd)) {
365 return handle_cmd(i, cmdend + 1);
368 msg_add(MSG_CMD_EUNKNOWN, cmd);
371 void handle_str(char *str)
373 if (chatText[0] == '/') {
374 if (chatText[1] != '/') {
375 handle_cmdstr(chatText + 1);
378 memmove(chatText, chatText + 1, strlen(chatText));
381 msg_add("<\\%d%s\\7> %s",
382 Players[me].team > 7 ? 7 : Players[me].team,
383 Players[me].name, chatText);
384 if (Game.type == GT_classicTwo)
385 SendPacket(me, NP_msg, strlen(chatText) + 1, chatText);
388 void handle_key(char key)
397 handle_str(chatText);
398 memset(chatText, 0, sizeof(chatText));
400 else msg_add_char(27, -1, NULL); //escape
403 else if (key == 27) //escape
405 else if (key == 127 && chatText) //backspace
406 chatText[strlen(chatText) - 1] = 0;
407 else if (strlen(chatText) < MSG_WIDTH-1) //text
408 chatText[strlen(chatText)] = key;
409 msg_add_char(key, strlen(chatText) - 1, chatText);
413 if (!(p = strchr(keyTable, tolower(key)))) return;
416 void handle_keycmd(char key)
422 // ScheduleFullRedraw();
427 msg_add_char(key, strlen(chatText) - 1, chatText);
430 handle_cmd(CT_quit, NULL);
433 handle_cmd(CT_pause, NULL);
437 if (Players[me].alive <= 0 || paused) return;
438 // actions only available while actually playing
441 if (player_move(me, 0, -1) && spied) SendPacket(me, NP_left, 0, NULL);
444 if (player_move(me, 0, 1) && spied) SendPacket(me, NP_right, 0, NULL);
447 if (player_rotate(me, -1) && spied) SendPacket(me, NP_rotleft, 0, NULL);
450 if (player_rotate(me, 1) && spied) SendPacket(me, NP_rotright, 0, NULL);
453 SetITimer(Game.speed, Game.speed);
454 if (player_move(me, -1, 0)) {
455 if (spied) SendPacket(me, NP_down, 0, NULL);
458 gameStatus = 1; //completely dropped
461 SetITimer(Game.speed, Game.speed);
462 if (player_drop(me)) {
463 if (spied) SendPacket(me, NP_drop, 0, NULL);
465 else gameStatus = 1; //dropped
468 SetITimer(Game.speed, Game.speed);
469 if (player_drop(me)) {
470 if (spied) SendPacket(me, NP_drop, 0, NULL);
472 gameStatus = 1; // drop
475 if (Game.type != GT_onePlayer) break;
476 if ((Game.speed /= SPEEDINC) < SPEEDMINIMUM)
477 Game.speed = SPEEDMINIMUM;
478 SetITimer(Game.speed, SetITimer(0, 0));
479 Players[me].score.level++;
480 status_draw(me, Players[me].score);
490 void handle_net(_netEvent net)
495 memcpy(&Players[net.uid].nextShape, net.data,
496 sizeof(Players[0].nextShape));
497 player_piece(net.uid, Players[net.uid].curShape);
501 player_move(net.uid, -1, 0);
504 player_move(net.uid, 0, -1);
507 player_move(net.uid, 0, 1);
510 player_rotate(net.uid, -1);
513 player_rotate(net.uid, 1);
516 player_drop(net.uid);
525 memcpy(data, net.data, sizeof(data));
526 player_lineadd(net.uid, Players[data[2]].team, data[0], data[1]);
528 } //player added junklines
534 if (Players[me].alive <= 0) break;
535 memcpy(data, net.data, sizeof(data[0]));
536 column = Random(0, Players[me].boardWidth);
537 msg_add("\\%d" MSG_GAME_LINES,
538 Players[net.uid].team > 7 ? 7 : Players[net.uid].team,
539 Players[net.uid].name, data[0]);
541 player_lineadd(me, Players[net.uid].team, data[0], column);
545 SendPacket(me, NP_insertJunk, sizeof(data), data);
546 } //show changes to others
548 } //receive junklines
551 msg_add("<\\%d%s\\7> %s",
552 Players[net.uid].team > 7 ? 7 : Players[net.uid].team,
553 Players[net.uid].name, net.data, net.type);
562 msg_add(MSG_GAME_START);
563 for (i = 1; i < MAX_SCREENS; i++) if (Players[i].alive > 0)
564 window_msg_status(i);
569 if (Game.started > 1) {
574 msg_add(MSG_GAME_STOP);
575 timer = CurTimeval() / 1e6;
577 for (i = MAX_SCREENS-1; i > 0; i--) if (Players[i].alive >= 0) {
578 msg_add("\\%d%10s%6.1fp%5.1fa",
579 Players[i].team > 7 ? 7 : Players[i].team, Players[i].name,
580 Players[i].score.pieces / timer * 60,
581 Players[i].score.adds / timer * 60);
582 if (Players[i].alive > 0) winner = i;
583 } //show player stats
585 msg_add(MSG_GAME_WINNER,
586 Players[winner].name, timer / 60, (int)timer % 60);
591 memcpy(&Game.seed, net.data, net.size);
595 for (i = 1; i < MAX_SCREENS; i++) if (Players[i].alive >= 0) {
596 Players[i].alive = 1;
597 Players[i].flags |= SCF_paused;
600 game_reset(); //reset everything
601 status_tick(); //redraw timer while unpaused
602 game_setpaused(); //pause
603 oldPaused = 0; //reset pause
610 if (net.uid>maxPlayer) maxPlayer = net.uid;
611 memcpy(&Players[net.uid], net.data, net.size);
612 player_empty(net.uid);
614 if (Players[net.uid].team > 7)
615 msg_add(MSG_GAME_JOIN, Players[net.uid].name);
617 msg_add(MSG_GAME_JOIN_TEAM, Players[net.uid].name,
618 teamname[Players[net.uid].team]);
619 if (Players[net.uid].flags & SCF_paused) {
621 } //player has paused
622 // window_draw(net.uid);
623 // window_msg_status(net.uid);
631 Players[net.uid].flags ^= SCF_paused;
632 if (Game.started > 1)
633 strcpy(s, Players[net.uid].flags&SCF_paused
634 ? MSG_GAME_PAUSE : MSG_GAME_UNPAUSE);
636 strcpy(s, Players[net.uid].flags&SCF_paused
637 ? MSG_GAME_UNREADY : MSG_GAME_READY);
638 msg_add(s, Players[net.uid].name);
640 window_msg_status(net.uid);
648 Players[net.uid].alive = -1;
649 msg_add(MSG_GAME_PART, Players[net.uid].name);
651 window_msg_status(net.uid);
657 memcpy(&i, net.data, sizeof(i));
658 Players[net.uid].alive = 0;
660 msg_add("\\%d" MSG_GAME_KILL_1_2,
661 Players[me].team > 7 ? 7 : Players[me].team, Players[net.uid].name);
662 else if (i == net.uid)
663 msg_add("\\%d" MSG_GAME_KILL_2_2,
664 Players[i].team > 7 ? 7 : Players[i].team, Players[i].name);
666 msg_add("\\%d" MSG_GAME_KILL_2_3,
667 Players[i].team > 7 ? 7 : Players[i].team, Players[i].name,
668 Players[net.uid].name);
670 window_msg_status(net.uid);
686 if (Players[me].alive > 0) {
687 if (!player_piece(me, ChooseOption(Game.shapes))) {
690 Players[me].alive = 0;
691 if (lastadd == me) msg_add("\\%d" MSG_GAME_KILL_1_1,
692 Players[me].team > 7 ? 7 : Players[me].team);
693 else msg_add("\\%d" MSG_GAME_KILL_2_1,
694 Players[lastadd].team > 7 ? 7 : Players[lastadd].team,
695 Players[lastadd].name);
696 if (Game.type == GT_classicTwo)
697 SendPacket(me, NP_argghhh, sizeof(lastadd), &lastadd);
698 window_msg_status(me);
702 status_draw(me, Players[me].score);
704 SendPacket(me, NP_newPiece, sizeof(Players[me].curShape), &Players[me].curShape);
708 while (gameStatus == 2) {
709 for (i = 1; i < MAX_SCREENS; i++)
710 if (Players[i].alive > 0 && window[i].shown)
711 changed |= player_draw(i);
713 if (!paused) status_tick();
718 short playercount = 0;
719 for (i = 1; i < MAX_SCREENS; i++)
720 if (Players[i].alive >= 0) playercount++;
721 if (playercount < 1) gameStatus = 0;
723 switch (WaitMyEvent(&event, EM_any)) {
725 if (!paused && Players[me].alive > 0)
726 if (!player_move(me, -1, 0)) //move down
727 gameStatus = 1; //new piece
729 if (spied) SendPacket(me, NP_down, 0, NULL);
732 handle_key(event.u.key);
735 handle_net(event.u.net);
740 if (paused != oldPaused) {
743 pauseTimeLeft = SetITimer(0, 0);
746 SetITimer(Game.speed, pauseTimeLeft);
752 Players[me].score.score++;
759 int main(int argc, char **argv)
763 Game.type = GT_onePlayer;
766 Game.initspeed = DEFAULT_INTERVAL;
768 handle_setkeys(DEFAULT_KEYS);
773 for (i = 0; i < MAX_SCREENS; i++) {
774 Players[i].alive = -1;
775 Players[i].score.level = 1;
776 Players[i].boardWidth = 10;
777 Players[i].boardHeight = MAX_BOARD_HEIGHT;
778 Players[i].boardVisible = 20;
779 strcpy(Players[i].name, "???");
782 if (!(userName = getenv("LOGNAME")) || !userName[0])
783 if (!(userName = getenv("USER")) || !userName[0])
784 userName = "Anonymous";
785 strncpy(Players[0].name, userName, 16); //sizeof(Player.name)
786 Players[0].name[16] = 0;
787 Players[0].alive = 1;
790 // if (getopt(argc, argv, "f:") == 'f')
791 // handle_conffile(optarg);
793 handle_conffile(CONFIG_FILE);
794 while ((ch = getopt_long(
795 argc, argv, "hHk:c:n:oSCap:i:l:t:", options, NULL
797 handle_arg(ch, optarg);
804 InitScreens(); //setup screen
806 if (Game.type == GT_classicTwo) {
808 InitiateConnection(hostStr, port);
817 for (i = 0; i < sizeof(Game.shapes) / sizeof(Game.shapes[0]); i++)
821 memcpy(&Players[me], &Players[0], sizeof(player_t));
822 Players[me].team = 7;