Print verbose usage help, also when an unknown option is passed.
[tetrinet.git] / tetrinet.c
blob0108ad558b9fab2311ca9d6a794beacc99254874
1 /* Tetrinet for Linux, by Andrew Church <achurch@achurch.org>
2 * This program is public domain.
4 * Tetrinet main program.
5 */
7 /*************************************************************************/
9 #include <stdio.h>
10 #include <string.h>
11 #include <errno.h>
12 #include "tetrinet.h"
13 #include "io.h"
14 #include "server.h"
15 #include "sockets.h"
16 #include "tetris.h"
18 /*************************************************************************/
20 int fancy = 0; /* Fancy TTY graphics? */
21 int log = 0; /* Log network traffic to file? */
22 char *logname; /* Log filename */
23 int windows_mode = 0; /* Try to be just like the Windows version? */
24 int noslide = 0; /* Disallow piece sliding? */
25 int tetrifast = 0; /* TetriFast mode? */
27 int my_playernum = -1; /* What player number are we? */
28 char *my_nick; /* And what is our nick? */
29 WinInfo winlist[MAXWINLIST]; /* Winners' list from server */
30 int server_sock; /* Socket for server communication */
31 int dispmode; /* Current display mode */
32 char *players[6]; /* Player names (NULL for no such player) */
33 char *teams[6]; /* Team names (NULL for not on a team) */
34 int playing_game; /* Are we currently playing a game? */
35 int not_playing_game; /* Are we currently watching people play a game? */
36 int game_paused; /* Is the game currently paused? */
38 Interface *io; /* Input/output routines */
40 /*************************************************************************/
41 /*************************************************************************/
43 #ifndef SERVER_ONLY
45 /*************************************************************************/
47 /* Parse a line from the server. Destroys the buffer it's given as a side
48 * effect.
51 void parse(char *buf)
53 char *cmd, *s, *t;
55 cmd = strtok(buf, " ");
57 if (!cmd) {
58 return;
60 } else if (strcmp(cmd, "noconnecting") == 0) {
61 s = strtok(NULL, "");
62 if (!s)
63 s = "Unknown";
64 /* XXX not to stderr, please! -- we need to stay running w/o server */
65 fprintf(stderr, "Server error: %s\n", s);
66 exit(1);
68 } else if (strcmp(cmd, "winlist") == 0) {
69 int i = 0;
71 while (i < MAXWINLIST && (s = strtok(NULL, " "))) {
72 t = strchr(s, ';');
73 if (!t)
74 break;
75 *t++ = 0;
76 if (*s == 't')
77 winlist[i].team = 1;
78 else
79 winlist[i].team = 0;
80 s++;
81 strncpy(winlist[i].name, s, sizeof(winlist[i].name)-1);
82 winlist[i].name[sizeof(winlist[i].name)] = 0;
83 winlist[i].points = atoi(t);
84 if ((t = strchr(t, ';')) != NULL)
85 winlist[i].games = atoi(t+1);
86 i++;
88 if (i < MAXWINLIST)
89 winlist[i].name[0] = 0;
90 if (dispmode == MODE_WINLIST)
91 io->setup_winlist();
93 } else if (strcmp(cmd, tetrifast ? ")#)(!@(*3" : "playernum") == 0) {
94 if (s = strtok(NULL, " "))
95 my_playernum = atoi(s);
96 /* Note: players[my_playernum-1] is set in init() */
97 /* But that doesn't work when joining other channel. */
98 players[my_playernum-1] = strdup(my_nick);
100 } else if (strcmp(cmd, "playerjoin") == 0) {
101 int player;
102 char buf[1024];
104 s = strtok(NULL, " ");
105 t = strtok(NULL, "");
106 if (!s || !t)
107 return;
108 player = atoi(s)-1;
109 if (player < 0 || player > 5)
110 return;
111 players[player] = strdup(t);
112 if (teams[player]) {
113 free(teams[player]);
114 teams[player] = NULL;
116 snprintf(buf, sizeof(buf), "*** %s is Now Playing", t);
117 io->draw_text(BUFFER_PLINE, buf);
118 if (dispmode == MODE_FIELDS)
119 io->setup_fields();
121 } else if (strcmp(cmd, "playerleave") == 0) {
122 int player;
123 char buf[1024];
125 s = strtok(NULL, " ");
126 if (!s)
127 return;
128 player = atoi(s)-1;
129 if (player < 0 || player > 5 || !players[player])
130 return;
131 snprintf(buf, sizeof(buf), "*** %s has Left", players[player]);
132 io->draw_text(BUFFER_PLINE, buf);
133 free(players[player]);
134 players[player] = NULL;
135 if (dispmode == MODE_FIELDS)
136 io->setup_fields();
138 } else if (strcmp(cmd, "team") == 0) {
139 int player;
140 char buf[1024];
142 s = strtok(NULL, " ");
143 t = strtok(NULL, "");
144 if (!s)
145 return;
146 player = atoi(s)-1;
147 if (player < 0 || player > 5 || !players[player])
148 return;
149 if (teams[player])
150 free(teams[player]);
151 if (t)
152 teams[player] = strdup(t);
153 else
154 teams[player] = NULL;
155 if (t)
156 snprintf(buf, sizeof(buf), "*** %s is Now on Team %s", players[player], t);
157 else
158 snprintf(buf, sizeof(buf), "*** %s is Now Alone", players[player]);
159 io->draw_text(BUFFER_PLINE, buf);
161 } else if (strcmp(cmd, "pline") == 0) {
162 int playernum;
163 char buf[1024], *name;
165 s = strtok(NULL, " ");
166 t = strtok(NULL, "");
167 if (!s)
168 return;
169 if (!t)
170 t = "";
171 playernum = atoi(s)-1;
172 if (playernum == -1) {
173 name = "Server";
174 } else {
175 if (playernum < 0 || playernum > 5 || !players[playernum])
176 return;
177 name = players[playernum];
179 snprintf(buf, sizeof(buf), "<%s> %s", name, t);
180 io->draw_text(BUFFER_PLINE, buf);
182 } else if (strcmp(cmd, "plineact") == 0) {
183 int playernum;
184 char buf[1024], *name;
186 s = strtok(NULL, " ");
187 t = strtok(NULL, "");
188 if (!s)
189 return;
190 if (!t)
191 t = "";
192 playernum = atoi(s)-1;
193 if (playernum == -1) {
194 name = "Server";
195 } else {
196 if (playernum < 0 || playernum > 5 || !players[playernum])
197 return;
198 name = players[playernum];
200 snprintf(buf, sizeof(buf), "* %s %s", name, t);
201 io->draw_text(BUFFER_PLINE, buf);
203 } else if (strcmp(cmd, tetrifast ? "*******" : "newgame") == 0) {
204 int i;
206 if (s = strtok(NULL, " "))
207 /* stack height */;
208 if (s = strtok(NULL, " "))
209 initial_level = atoi(s);
210 if (s = strtok(NULL, " "))
211 lines_per_level = atoi(s);
212 if (s = strtok(NULL, " "))
213 level_inc = atoi(s);
214 if (s = strtok(NULL, " "))
215 special_lines = atoi(s);
216 if (s = strtok(NULL, " "))
217 special_count = atoi(s);
218 if (s = strtok(NULL, " ")) {
219 special_capacity = atoi(s);
220 if (special_capacity > MAX_SPECIALS)
221 special_capacity = MAX_SPECIALS;
223 if (s = strtok(NULL, " ")) {
224 memset(piecefreq, 0, sizeof(piecefreq));
225 while (*s) {
226 i = *s - '1';
227 if (i >= 0 && i < 7)
228 piecefreq[i]++;
229 s++;
232 if (s = strtok(NULL, " ")) {
233 memset(specialfreq, 0, sizeof(specialfreq));
234 while (*s) {
235 i = *s - '1';
236 if (i >= 0 && i < 9)
237 specialfreq[i]++;
238 s++;
241 if (s = strtok(NULL, " "))
242 level_average = atoi(s);
243 if (s = strtok(NULL, " "))
244 old_mode = atoi(s);
245 lines = 0;
246 for (i = 0; i < 6; i++)
247 levels[i] = initial_level;
248 memset(&fields[my_playernum-1], 0, sizeof(Field));
249 specials[0] = -1;
250 io->clear_text(BUFFER_GMSG);
251 io->clear_text(BUFFER_ATTDEF);
252 new_game();
253 playing_game = 1;
254 game_paused = 0;
255 io->draw_text(BUFFER_PLINE, "*** The Game Has Started");
257 } else if (strcmp(cmd, "ingame") == 0) {
258 /* Sent when a player connects in the middle of a game */
259 int x, y;
260 char buf[1024], *s;
262 s = buf + sprintf(buf, "f %d ", my_playernum);
263 for (y = 0; y < FIELD_HEIGHT; y++) {
264 for (x = 0; x < FIELD_WIDTH; x++) {
265 fields[my_playernum-1][y][x] = rand()%5 + 1;
266 *s++ = '0' + fields[my_playernum-1][y][x];
269 *s = 0;
270 sputs(buf, server_sock);
271 playing_game = 0;
272 not_playing_game = 1;
274 } else if (strcmp(cmd, "pause") == 0) {
275 if (s = strtok(NULL, " "))
276 game_paused = atoi(s);
277 if (game_paused) {
278 io->draw_text(BUFFER_PLINE, "*** The Game Has Been Paused");
279 io->draw_text(BUFFER_GMSG, "*** The Game Has Been Paused");
280 } else {
281 io->draw_text(BUFFER_PLINE, "*** The Game Has Been Unpaused");
282 io->draw_text(BUFFER_GMSG, "*** The Game Has Been Unpaused");
285 } else if (strcmp(cmd, "endgame") == 0) {
286 playing_game = 0;
287 not_playing_game = 0;
288 memset(fields, 0, sizeof(fields));
289 specials[0] = -1;
290 io->clear_text(BUFFER_ATTDEF);
291 io->draw_text(BUFFER_PLINE, "*** The Game Has Ended");
292 if (dispmode == MODE_FIELDS) {
293 int i;
294 io->draw_own_field();
295 for (i = 1; i <= 6; i++) {
296 if (i != my_playernum)
297 io->draw_other_field(i);
301 } else if (strcmp(cmd, "playerwon") == 0) {
302 /* Syntax: playerwon # -- sent when all but one player lose */
304 } else if (strcmp(cmd, "playerlost") == 0) {
305 /* Syntax: playerlost # -- sent after playerleave on disconnect
306 * during a game, or when a player loses (sent by the losing
307 * player and from the server to all other players */
309 } else if (strcmp(cmd, "f") == 0) { /* field */
310 int player, x, y, tile;
312 /* This looks confusing, but what it means is, ignore this message
313 * if a game isn't going on. */
314 if (!playing_game && !not_playing_game)
315 return;
316 if (!(s = strtok(NULL, " ")))
317 return;
318 player = atoi(s);
319 player--;
320 if (!(s = strtok(NULL, "")))
321 return;
322 if (*s >= '0') {
323 /* Set field directly */
324 char *ptr = (char *) fields[player];
325 while (*s) {
326 if (*s <= '5')
327 *ptr++ = (*s++) - '0';
328 else switch (*s++) {
329 case 'a': *ptr++ = 6 + SPECIAL_A; break;
330 case 'b': *ptr++ = 6 + SPECIAL_B; break;
331 case 'c': *ptr++ = 6 + SPECIAL_C; break;
332 case 'g': *ptr++ = 6 + SPECIAL_G; break;
333 case 'n': *ptr++ = 6 + SPECIAL_N; break;
334 case 'o': *ptr++ = 6 + SPECIAL_O; break;
335 case 'q': *ptr++ = 6 + SPECIAL_Q; break;
336 case 'r': *ptr++ = 6 + SPECIAL_R; break;
337 case 's': *ptr++ = 6 + SPECIAL_S; break;
340 } else {
341 /* Set specific locations on field */
342 tile = 0;
343 while (*s) {
344 if (*s < '0') {
345 tile = *s - '!';
346 } else {
347 x = *s - '3';
348 y = (*++s) - '3';
349 fields[player][y][x] = tile;
351 s++;
354 if (player == my_playernum-1)
355 io->draw_own_field();
356 else
357 io->draw_other_field(player+1);
358 } else if (strcmp(cmd, "lvl") == 0) {
359 int player;
361 if (!(s = strtok(NULL, " ")))
362 return;
363 player = atoi(s)-1;
364 if (!(s = strtok(NULL, "")))
365 return;
366 levels[player] = atoi(s);
368 } else if (strcmp(cmd, "sb") == 0) {
369 int from, to;
370 char *type;
372 if (!(s = strtok(NULL, " ")))
373 return;
374 to = atoi(s);
375 if (!(type = strtok(NULL, " ")))
376 return;
377 if (!(s = strtok(NULL, " ")))
378 return;
379 from = atoi(s);
380 do_special(type, from, to);
382 } else if (strcmp(cmd, "gmsg") == 0) {
383 if (!(s = strtok(NULL, "")))
384 return;
385 io->draw_text(BUFFER_GMSG, s);
390 /*************************************************************************/
391 /*************************************************************************/
393 static char partyline_buffer[512];
394 static int partyline_pos = 0;
396 #define curpos (partyline_buffer+partyline_pos)
398 /*************************************************************************/
400 /* Add a character to the partyline buffer. */
402 void partyline_input(int c)
404 if (partyline_pos < sizeof(partyline_buffer) - 1) {
405 memmove(curpos+1, curpos, strlen(curpos)+1);
406 partyline_buffer[partyline_pos++] = c;
407 io->draw_partyline_input(partyline_buffer, partyline_pos);
411 /*************************************************************************/
413 /* Delete the current character from the partyline buffer. */
415 void partyline_delete(void)
417 if (partyline_buffer[partyline_pos]) {
418 memmove(curpos, curpos+1, strlen(curpos)-1+1);
419 io->draw_partyline_input(partyline_buffer, partyline_pos);
423 /*************************************************************************/
425 /* Backspace a character from the partyline buffer. */
427 void partyline_backspace(void)
429 if (partyline_pos > 0) {
430 partyline_pos--;
431 partyline_delete();
435 /*************************************************************************/
437 /* Kill the entire partyline input buffer. */
439 void partyline_kill(void)
441 partyline_pos = 0;
442 *partyline_buffer = 0;
443 io->draw_partyline_input(partyline_buffer, partyline_pos);
446 /*************************************************************************/
448 /* Move around the input buffer. Sign indicates direction; absolute value
449 * of 1 means one character, 2 means the whole line.
452 void partyline_move(int how)
454 if (how == -2) {
455 partyline_pos = 0;
456 io->draw_partyline_input(partyline_buffer, partyline_pos);
457 } else if (how == -1 && partyline_pos > 0) {
458 partyline_pos--;
459 io->draw_partyline_input(partyline_buffer, partyline_pos);
460 } else if (how == 1 && partyline_buffer[partyline_pos]) {
461 partyline_pos++;
462 io->draw_partyline_input(partyline_buffer, partyline_pos);
463 } else if (how == 2) {
464 partyline_pos = strlen(partyline_buffer);
465 io->draw_partyline_input(partyline_buffer, partyline_pos);
469 /*************************************************************************/
471 /* Send the input line to the server. */
473 void partyline_enter(void)
475 char buf[1024];
477 if (*partyline_buffer) {
478 if (strncasecmp(partyline_buffer, "/me ", 4) == 0) {
479 sockprintf(server_sock, "plineact %d %s", my_playernum, partyline_buffer+4);
480 snprintf(buf, sizeof(buf), "* %s %s", players[my_playernum-1], partyline_buffer+4);
481 io->draw_text(BUFFER_PLINE, buf);
482 } else if (strcasecmp(partyline_buffer, "/start") == 0) {
483 sockprintf(server_sock, "startgame 1 %d", my_playernum);
484 } else if (strcasecmp(partyline_buffer, "/end") == 0) {
485 sockprintf(server_sock, "startgame 0 %d", my_playernum);
486 } else if (strcasecmp(partyline_buffer, "/pause") == 0) {
487 sockprintf(server_sock, "pause 1 %d", my_playernum);
488 } else if (strcasecmp(partyline_buffer, "/unpause") == 0) {
489 sockprintf(server_sock, "pause 0 %d", my_playernum);
490 } else if (strncasecmp(partyline_buffer, "/team", 5) == 0) {
491 if (strlen(partyline_buffer) == 5)
492 strcpy(partyline_buffer+5, " "); /* make it "/team " */
493 sockprintf(server_sock, "team %d %s", my_playernum, partyline_buffer+6);
494 if (partyline_buffer[6]) {
495 if (teams[my_playernum-1])
496 free(teams[my_playernum-1]);
497 teams[my_playernum-1] = strdup(partyline_buffer+6);
498 snprintf(buf, sizeof(buf), "*** %s is Now on Team %s", players[my_playernum-1], partyline_buffer+6);
499 io->draw_text(BUFFER_PLINE, buf);
500 } else {
501 if (teams[my_playernum-1])
502 free(teams[my_playernum-1]);
503 teams[my_playernum-1] = NULL;
504 snprintf(buf, sizeof(buf), "*** %s is Now Alone", players[my_playernum-1]);
505 io->draw_text(BUFFER_PLINE, buf);
507 } else {
508 sockprintf(server_sock, "pline %d %s", my_playernum, partyline_buffer);
509 if (*partyline_buffer != '/'
510 || partyline_buffer[1] == 0 || partyline_buffer[1] == ' ') {
511 /* We do not show server-side commands. */
512 snprintf(buf, sizeof(buf), "<%s> %s", players[my_playernum-1], partyline_buffer);
513 io->draw_text(BUFFER_PLINE, buf);
516 partyline_pos = 0;
517 *partyline_buffer = 0;
518 io->draw_partyline_input(partyline_buffer, partyline_pos);
522 #undef curpos
524 /*************************************************************************/
525 /*************************************************************************/
527 void help()
529 fprintf(stderr,
530 "Usage: tetrinet [OPTION]... NICK SERVER\n"
531 "\n"
532 "Options (see README for details):\n"
533 " -fancy Use \"fancy\" TTY graphics.\n"
534 " -log <file> Log network traffic to the given file.\n"
535 " -noslide Do not allow pieces to \"slide\" after being dropped\n"
536 " with the spacebar.\n"
537 " -server Start the server instead of the client.\n"
538 " -slide Opposite of -noslide; allows pieces to \"slide\" after\n"
539 " being dropped. If both -slide and -noslide are given,\n"
540 " -slide takes precedence.\n"
541 " -windows Behave as much like the Windows version of Tetrinet as\n"
542 " possible. Implies -noslide.\n"
546 int init(int ac, char **av)
548 int i;
549 char *nick = NULL, *server = NULL;
550 char buf[1024];
551 char nickmsg[1024];
552 unsigned char ip[4];
553 char iphashbuf[32];
554 int len;
555 int start_server = 0; /* Start the server? (-server) */
556 int slide = 0; /* Do we definitely want to slide? (-slide) */
559 /* If there's a DISPLAY variable set in the environment, default to
560 * Xwindows I/O, else default to terminal I/O. */
561 if (getenv("DISPLAY"))
562 io = &xwin_interface;
563 else
564 io = &tty_interface;
565 io=&tty_interface; /* because Xwin isn't done yet */
567 srand(time(NULL));
568 init_shapes();
570 for (i = 1; i < ac; i++) {
571 if (*av[i] == '-') {
572 if (strcmp(av[i], "-server") == 0) {
573 start_server = 1;
574 } else if (strcmp(av[i], "-fancy") == 0) {
575 fancy = 1;
576 } else if (strcmp(av[i], "-log") == 0) {
577 log = 1;
578 i++;
579 if (i >= ac) {
580 fprintf(stderr, "Option -log requires an argument\n");
581 return 1;
583 logname = av[i];
584 } else if (strcmp(av[i], "-noslide") == 0) {
585 noslide = 1;
586 } else if (strcmp(av[i], "-slide") == 0) {
587 slide = 1;
588 } else if (strcmp(av[i], "-windows") == 0) {
589 windows_mode = 1;
590 noslide = 1;
591 } else if (strcmp(av[i], "-fast") == 0) {
592 tetrifast = 1;
593 } else {
594 fprintf(stderr, "Unknown option %s\n", av[i]);
595 help();
596 return 1;
598 } else if (!nick) {
599 my_nick = nick = av[i];
600 } else if (!server) {
601 server = av[i];
602 } else {
603 help();
604 return 1;
607 if (slide)
608 noslide = 0;
609 if (start_server)
610 exit(server_main());
611 if (!server) {
612 help();
613 return 1;
615 if (strlen(nick) > 63) /* put a reasonable limit on nick length */
616 nick[63] = 0;
618 if ((server_sock = conn(server, 31457, ip)) < 0) {
619 fprintf(stderr, "Couldn't connect to server %s: %s\n",
620 server, strerror(errno));
621 return 1;
623 sprintf(nickmsg, "tetri%s %s 1.13", tetrifast ? "faster" : "sstart", nick);
624 sprintf(iphashbuf, "%d", ip[0]*54 + ip[1]*41 + ip[2]*29 + ip[3]*17);
625 /* buf[0] does not need to be initialized for this algorithm */
626 len = strlen(nickmsg);
627 for (i = 0; i < len; i++)
628 buf[i+1] = (((buf[i]&0xFF) + (nickmsg[i]&0xFF)) % 255) ^ iphashbuf[i % strlen(iphashbuf)];
629 len++;
630 for (i = 0; i < len; i++)
631 sprintf(nickmsg+i*2, "%02X", buf[i] & 0xFF);
632 sputs(nickmsg, server_sock);
634 do {
635 if (!sgets(buf, sizeof(buf), server_sock)) {
636 fprintf(stderr, "Server %s closed connection\n", server);
637 disconn(server_sock);
638 return 1;
640 parse(buf);
641 } while (my_playernum < 0);
642 sockprintf(server_sock, "team %d ", my_playernum);
644 players[my_playernum-1] = strdup(nick);
645 dispmode = MODE_PARTYLINE;
646 io->screen_setup();
647 io->setup_partyline();
649 return 0;
652 /*************************************************************************/
654 int main(int ac, char **av)
656 int i;
658 if ((i = init(ac, av)) != 0)
659 return i;
661 for (;;) {
662 int timeout;
663 if (playing_game && !game_paused)
664 timeout = tetris_timeout();
665 else
666 timeout = -1;
667 i = io->wait_for_input(timeout);
668 if (i == -1) {
669 char buf[1024];
670 if (sgets(buf, sizeof(buf), server_sock))
671 parse(buf);
672 else {
673 io->draw_text(BUFFER_PLINE, "*** Disconnected from Server");
674 break;
676 } else if (i == -2) {
677 tetris_timeout_action();
678 } else if (i == 12) { /* Ctrl-L */
679 io->screen_redraw();
680 } else if (i == K_F10) {
681 break; /* out of main loop */
682 } else if (i == K_F1) {
683 if (dispmode != MODE_FIELDS) {
684 dispmode = MODE_FIELDS;
685 io->setup_fields();
687 } else if (i == K_F2) {
688 if (dispmode != MODE_PARTYLINE) {
689 dispmode = MODE_PARTYLINE;
690 io->setup_partyline();
692 } else if (i == K_F3) {
693 if (dispmode != MODE_WINLIST) {
694 dispmode = MODE_WINLIST;
695 io->setup_winlist();
697 } else if (dispmode == MODE_FIELDS) {
698 tetris_input(i);
699 } else if (dispmode == MODE_PARTYLINE) {
700 if (i == 8 || i == 127) /* Backspace or Delete */
701 partyline_backspace();
702 else if (i == 4) /* Ctrl-D */
703 partyline_delete();
704 else if (i == 21) /* Ctrl-U */
705 partyline_kill();
706 else if (i == '\r' || i == '\n')
707 partyline_enter();
708 else if (i == K_LEFT)
709 partyline_move(-1);
710 else if (i == K_RIGHT)
711 partyline_move(1);
712 else if (i == 1) /* Ctrl-A */
713 partyline_move(-2);
714 else if (i == 5) /* Ctrl-E */
715 partyline_move(2);
716 else if (i >= 1 && i <= 0xFF)
717 partyline_input(i);
721 disconn(server_sock);
722 return 0;
725 /*************************************************************************/
727 #endif /* !SERVER_ONLY */
729 /*************************************************************************/