don't write past the end of the array
[tetrinet.git] / tetrinet.c
blobaa687a28a6819f82707c26f70ce8c6b3a1cb4459
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 <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <errno.h>
14 #include "tetrinet.h"
15 #include "io.h"
16 #include "server.h"
17 #include "sockets.h"
18 #include "tetris.h"
19 #include "version.h"
21 /*************************************************************************/
23 int fancy = 0; /* Fancy TTY graphics? */
24 int log = 0; /* Log network traffic to file? */
25 char *logname; /* Log filename */
26 int windows_mode = 0; /* Try to be just like the Windows version? */
27 int noslide = 0; /* Disallow piece sliding? */
28 int tetrifast = 0; /* TetriFast mode? */
29 int cast_shadow = 1; /* Make pieces cast shadow? */
31 int my_playernum = -1; /* What player number are we? */
32 char *my_nick; /* And what is our nick? */
33 WinInfo winlist[MAXWINLIST]; /* Winners' list from server */
34 int server_sock; /* Socket for server communication */
35 int dispmode; /* Current display mode */
36 char *players[6]; /* Player names (NULL for no such player) */
37 char *teams[6]; /* Team names (NULL for not on a team) */
38 int playing_game; /* Are we currently playing a game? */
39 int not_playing_game; /* Are we currently watching people play a game? */
40 int game_paused; /* Is the game currently paused? */
42 Interface *io; /* Input/output routines */
44 /*************************************************************************/
45 /*************************************************************************/
47 #ifndef SERVER_ONLY
49 /*************************************************************************/
51 /* Output message to a message buffer, possibly decoding the text attributes
52 * tetrinet code. */
54 void msg_text(int bufnum, const char *s)
56 /* Stolen from gtetrinet: (leading space <=> undefined) */
57 static enum tattr map[32] = {
58 0, /* N/A */
59 TATTR_CBLACK,
60 TATTR_BOLD,
61 TATTR_CCYAN | TATTR_CXBRIGHT,
62 TATTR_CBLACK,
63 TATTR_CBLUE | TATTR_CXBRIGHT,
64 TATTR_CGREY,
65 TATTR_CBLACK,
66 TATTR_CMAGENTA,
67 TATTR_CBLACK,
68 TATTR_CBLACK,
69 TATTR_CBLACK | TATTR_CXBRIGHT,
70 TATTR_CGREEN,
71 TATTR_CBLACK,
72 TATTR_CGREEN | TATTR_CXBRIGHT,
73 TATTR_CGREY,
74 TATTR_CRED,
75 TATTR_CBLUE,
76 TATTR_CBROWN,
77 TATTR_CMAGENTA | TATTR_CXBRIGHT,
78 TATTR_CRED | TATTR_CXBRIGHT,
79 TATTR_CGREY,
80 TATTR_ITALIC,
81 TATTR_CCYAN,
82 TATTR_CGREY | TATTR_CXBRIGHT,
83 TATTR_CBROWN | TATTR_CXBRIGHT,
84 TATTR_CBLACK,
85 TATTR_CBLACK,
86 TATTR_CBLACK,
87 TATTR_CBLACK,
88 TATTR_CBLACK,
89 TATTR_UNDERLINE,
91 char tb[1024], *t;
93 for (t = tb; *s && t - tb < 1024; t++, s++) {
94 if (*s == 0xFF) {
95 *t = TATTR_RESET + 1;
96 } else if (*s < 32) {
97 *t = map[(int) *s] + 1;
98 } else {
99 *t = *s;
102 if (t - tb >= 1024) t = &tb[1023];
103 *t = 0;
105 io->draw_text(bufnum, tb);
109 /* Parse a line from the server. Destroys the buffer it's given as a side
110 * effect.
113 void parse(char *buf)
115 char *cmd, *s, *t;
117 cmd = strtok(buf, " ");
119 if (!cmd) {
120 return;
122 } else if (strcmp(cmd, "noconnecting") == 0) {
123 s = strtok(NULL, "");
124 if (!s)
125 s = "Unknown";
126 /* XXX not to stderr, please! -- we need to stay running w/o server */
127 fprintf(stderr, "Server error: %s\n", s);
128 exit(1);
130 } else if (strcmp(cmd, "winlist") == 0) {
131 int i = 0;
133 while (i < MAXWINLIST && (s = strtok(NULL, " "))) {
134 t = strchr(s, ';');
135 if (!t)
136 break;
137 *t++ = 0;
138 if (*s == 't')
139 winlist[i].team = 1;
140 else
141 winlist[i].team = 0;
142 s++;
143 strncpy(winlist[i].name, s, sizeof(winlist[i].name)-1);
144 winlist[i].points = atoi(t);
145 if ((t = strchr(t, ';')) != NULL)
146 winlist[i].games = atoi(t+1);
147 i++;
149 if (i < MAXWINLIST)
150 winlist[i].name[0] = 0;
151 if (dispmode == MODE_WINLIST)
152 io->setup_winlist();
154 } else if (strcmp(cmd, tetrifast ? ")#)(!@(*3" : "playernum") == 0) {
155 if ((s = strtok(NULL, " ")))
156 my_playernum = atoi(s);
157 /* Note: players[my_playernum-1] is set in init() */
158 /* But that doesn't work when joining other channel. */
159 players[my_playernum-1] = strdup(my_nick);
161 } else if (strcmp(cmd, "playerjoin") == 0) {
162 int player;
163 char buf[1024];
165 s = strtok(NULL, " ");
166 t = strtok(NULL, "");
167 if (!s || !t)
168 return;
169 player = atoi(s)-1;
170 if (player < 0 || player > 5)
171 return;
172 players[player] = strdup(t);
173 if (teams[player]) {
174 free(teams[player]);
175 teams[player] = NULL;
177 snprintf(buf, sizeof(buf), "*** %s is Now Playing", t);
178 msg_text(BUFFER_PLINE, buf);
179 if (dispmode == MODE_FIELDS)
180 io->setup_fields();
182 } else if (strcmp(cmd, "playerleave") == 0) {
183 int player;
184 char buf[1024];
186 s = strtok(NULL, " ");
187 if (!s)
188 return;
189 player = atoi(s)-1;
190 if (player < 0 || player > 5 || !players[player])
191 return;
192 snprintf(buf, sizeof(buf), "*** %s has Left", players[player]);
193 msg_text(BUFFER_PLINE, buf);
194 free(players[player]);
195 players[player] = NULL;
196 if (dispmode == MODE_FIELDS)
197 io->setup_fields();
199 } else if (strcmp(cmd, "team") == 0) {
200 int player;
201 char buf[1024];
203 s = strtok(NULL, " ");
204 t = strtok(NULL, "");
205 if (!s)
206 return;
207 player = atoi(s)-1;
208 if (player < 0 || player > 5 || !players[player])
209 return;
210 if (teams[player])
211 free(teams[player]);
212 if (t)
213 teams[player] = strdup(t);
214 else
215 teams[player] = NULL;
216 if (t)
217 snprintf(buf, sizeof(buf), "*** %s is Now on Team %s", players[player], t);
218 else
219 snprintf(buf, sizeof(buf), "*** %s is Now Alone", players[player]);
220 msg_text(BUFFER_PLINE, buf);
222 } else if (strcmp(cmd, "pline") == 0) {
223 int playernum;
224 char buf[1024], *name;
226 s = strtok(NULL, " ");
227 t = strtok(NULL, "");
228 if (!s)
229 return;
230 if (!t)
231 t = "";
232 playernum = atoi(s)-1;
233 if (playernum == -1) {
234 name = "Server";
235 } else {
236 if (playernum < 0 || playernum > 5 || !players[playernum])
237 return;
238 name = players[playernum];
240 snprintf(buf, sizeof(buf), "<%s> %s", name, t);
241 msg_text(BUFFER_PLINE, buf);
243 } else if (strcmp(cmd, "plineact") == 0) {
244 int playernum;
245 char buf[1024], *name;
247 s = strtok(NULL, " ");
248 t = strtok(NULL, "");
249 if (!s)
250 return;
251 if (!t)
252 t = "";
253 playernum = atoi(s)-1;
254 if (playernum == -1) {
255 name = "Server";
256 } else {
257 if (playernum < 0 || playernum > 5 || !players[playernum])
258 return;
259 name = players[playernum];
261 snprintf(buf, sizeof(buf), "* %s %s", name, t);
262 msg_text(BUFFER_PLINE, buf);
264 } else if (strcmp(cmd, tetrifast ? "*******" : "newgame") == 0) {
265 int i;
267 if ((s = strtok(NULL, " ")))
268 /* stack height */;
269 if ((s = strtok(NULL, " ")))
270 initial_level = atoi(s);
271 if ((s = strtok(NULL, " ")))
272 lines_per_level = atoi(s);
273 if ((s = strtok(NULL, " ")))
274 level_inc = atoi(s);
275 if ((s = strtok(NULL, " ")))
276 special_lines = atoi(s);
277 if ((s = strtok(NULL, " ")))
278 special_count = atoi(s);
279 if ((s = strtok(NULL, " "))) {
280 special_capacity = atoi(s);
281 if (special_capacity > MAX_SPECIALS)
282 special_capacity = MAX_SPECIALS;
284 if ((s = strtok(NULL, " "))) {
285 memset(piecefreq, 0, sizeof(piecefreq));
286 while (*s) {
287 i = *s - '1';
288 if (i >= 0 && i < 7)
289 piecefreq[i]++;
290 s++;
293 if ((s = strtok(NULL, " "))) {
294 memset(specialfreq, 0, sizeof(specialfreq));
295 while (*s) {
296 i = *s - '1';
297 if (i >= 0 && i < 9)
298 specialfreq[i]++;
299 s++;
302 if ((s = strtok(NULL, " ")))
303 level_average = atoi(s);
304 if ((s = strtok(NULL, " ")))
305 old_mode = atoi(s);
306 lines = 0;
307 for (i = 0; i < 6; i++)
308 levels[i] = initial_level;
309 memset(&fields[my_playernum-1], 0, sizeof(Field));
310 specials[0] = -1;
311 io->clear_text(BUFFER_GMSG);
312 io->clear_text(BUFFER_ATTDEF);
313 new_game();
314 playing_game = 1;
315 game_paused = 0;
316 msg_text(BUFFER_PLINE, "*** The Game Has Started");
317 if (dispmode != MODE_FIELDS) {
318 dispmode = MODE_FIELDS;
319 io->setup_fields();
322 } else if (strcmp(cmd, "ingame") == 0) {
323 /* Sent when a player connects in the middle of a game */
324 int x, y;
325 char buf[1024], *s;
327 s = buf + sprintf(buf, "f %d ", my_playernum);
328 for (y = 0; y < FIELD_HEIGHT; y++) {
329 for (x = 0; x < FIELD_WIDTH; x++) {
330 fields[my_playernum-1][y][x] = rand()%5 + 1;
331 *s++ = '0' + fields[my_playernum-1][y][x];
334 *s = 0;
335 sputs(buf, server_sock);
336 playing_game = 0;
337 not_playing_game = 1;
339 } else if (strcmp(cmd, "pause") == 0) {
340 if ((s = strtok(NULL, " ")))
341 game_paused = atoi(s);
342 if (game_paused) {
343 msg_text(BUFFER_PLINE, "*** The Game Has Been Paused");
344 msg_text(BUFFER_GMSG, "*** The Game Has Been Paused");
345 } else {
346 msg_text(BUFFER_PLINE, "*** The Game Has Been Unpaused");
347 msg_text(BUFFER_GMSG, "*** The Game Has Been Unpaused");
350 } else if (strcmp(cmd, "endgame") == 0) {
351 playing_game = 0;
352 not_playing_game = 0;
353 memset(fields, 0, sizeof(fields));
354 specials[0] = -1;
355 io->clear_text(BUFFER_ATTDEF);
356 msg_text(BUFFER_PLINE, "*** The Game Has Ended");
357 if (dispmode == MODE_FIELDS) {
358 int i;
359 io->draw_own_field();
360 for (i = 1; i <= 6; i++) {
361 if (i != my_playernum)
362 io->draw_other_field(i);
365 if (dispmode != MODE_PARTYLINE) {
366 dispmode = MODE_PARTYLINE;
367 io->setup_partyline();
370 } else if (strcmp(cmd, "playerwon") == 0) {
371 /* Syntax: playerwon # -- sent when all but one player lose */
373 } else if (strcmp(cmd, "playerlost") == 0) {
374 /* Syntax: playerlost # -- sent after playerleave on disconnect
375 * during a game, or when a player loses (sent by the losing
376 * player and from the server to all other players */
378 } else if (strcmp(cmd, "f") == 0) { /* field */
379 int player, x, y, tile;
381 /* This looks confusing, but what it means is, ignore this message
382 * if a game isn't going on. */
383 if (!playing_game && !not_playing_game)
384 return;
385 if (!(s = strtok(NULL, " ")))
386 return;
387 player = atoi(s);
388 player--;
389 if (!(s = strtok(NULL, "")))
390 return;
391 if (*s >= '0') {
392 /* Set field directly */
393 char *ptr = (char *) fields[player];
394 while (*s) {
395 if (*s <= '5')
396 *ptr++ = (*s++) - '0';
397 else switch (*s++) {
398 case 'a': *ptr++ = 6 + SPECIAL_A; break;
399 case 'b': *ptr++ = 6 + SPECIAL_B; break;
400 case 'c': *ptr++ = 6 + SPECIAL_C; break;
401 case 'g': *ptr++ = 6 + SPECIAL_G; break;
402 case 'n': *ptr++ = 6 + SPECIAL_N; break;
403 case 'o': *ptr++ = 6 + SPECIAL_O; break;
404 case 'q': *ptr++ = 6 + SPECIAL_Q; break;
405 case 'r': *ptr++ = 6 + SPECIAL_R; break;
406 case 's': *ptr++ = 6 + SPECIAL_S; break;
409 } else {
410 /* Set specific locations on field */
411 tile = 0;
412 while (*s) {
413 if (*s < '0') {
414 tile = *s - '!';
415 } else {
416 x = *s - '3';
417 y = (*++s) - '3';
418 fields[player][y][x] = tile;
420 s++;
423 if (player == my_playernum-1)
424 io->draw_own_field();
425 else
426 io->draw_other_field(player+1);
427 } else if (strcmp(cmd, "lvl") == 0) {
428 int player;
430 if (!(s = strtok(NULL, " ")))
431 return;
432 player = atoi(s)-1;
433 if (!(s = strtok(NULL, "")))
434 return;
435 levels[player] = atoi(s);
437 } else if (strcmp(cmd, "sb") == 0) {
438 int from, to;
439 char *type;
441 if (!(s = strtok(NULL, " ")))
442 return;
443 to = atoi(s);
444 if (!(type = strtok(NULL, " ")))
445 return;
446 if (!(s = strtok(NULL, " ")))
447 return;
448 from = atoi(s);
449 do_special(type, from, to);
451 } else if (strcmp(cmd, "gmsg") == 0) {
452 if (!(s = strtok(NULL, "")))
453 return;
454 msg_text(BUFFER_GMSG, s);
459 /*************************************************************************/
460 /*************************************************************************/
462 static char partyline_buffer[512];
463 static int partyline_pos = 0;
465 #define curpos (partyline_buffer+partyline_pos)
467 /*************************************************************************/
469 /* Add a character to the partyline buffer. */
471 void partyline_input(int c)
473 if (partyline_pos < sizeof(partyline_buffer) - 1) {
474 memmove(curpos+1, curpos, strlen(curpos)+1);
475 partyline_buffer[partyline_pos++] = c;
476 io->draw_partyline_input(partyline_buffer, partyline_pos);
480 /*************************************************************************/
482 /* Delete the current character from the partyline buffer. */
484 void partyline_delete(void)
486 if (partyline_buffer[partyline_pos]) {
487 memmove(curpos, curpos+1, strlen(curpos)-1+1);
488 io->draw_partyline_input(partyline_buffer, partyline_pos);
492 /*************************************************************************/
494 /* Backspace a character from the partyline buffer. */
496 void partyline_backspace(void)
498 if (partyline_pos > 0) {
499 partyline_pos--;
500 partyline_delete();
504 /*************************************************************************/
506 /* Kill the entire partyline input buffer. */
508 void partyline_kill(void)
510 partyline_pos = 0;
511 *partyline_buffer = 0;
512 io->draw_partyline_input(partyline_buffer, partyline_pos);
515 /*************************************************************************/
517 /* Move around the input buffer. Sign indicates direction; absolute value
518 * of 1 means one character, 2 means the whole line.
521 void partyline_move(int how)
523 if (how == -2) {
524 partyline_pos = 0;
525 io->draw_partyline_input(partyline_buffer, partyline_pos);
526 } else if (how == -1 && partyline_pos > 0) {
527 partyline_pos--;
528 io->draw_partyline_input(partyline_buffer, partyline_pos);
529 } else if (how == 1 && partyline_buffer[partyline_pos]) {
530 partyline_pos++;
531 io->draw_partyline_input(partyline_buffer, partyline_pos);
532 } else if (how == 2) {
533 partyline_pos = strlen(partyline_buffer);
534 io->draw_partyline_input(partyline_buffer, partyline_pos);
538 /*************************************************************************/
540 /* Send the input line to the server. */
542 void partyline_enter(void)
544 char buf[1024];
546 if (*partyline_buffer) {
547 if (strncasecmp(partyline_buffer, "/me ", 4) == 0) {
548 sockprintf(server_sock, "plineact %d %s", my_playernum, partyline_buffer+4);
549 snprintf(buf, sizeof(buf), "* %s %s", players[my_playernum-1], partyline_buffer+4);
550 msg_text(BUFFER_PLINE, buf);
551 } else if (strcasecmp(partyline_buffer, "/start") == 0) {
552 sockprintf(server_sock, "startgame 1 %d", my_playernum);
553 } else if (strcasecmp(partyline_buffer, "/end") == 0) {
554 sockprintf(server_sock, "startgame 0 %d", my_playernum);
555 } else if (strcasecmp(partyline_buffer, "/pause") == 0) {
556 sockprintf(server_sock, "pause 1 %d", my_playernum);
557 } else if (strcasecmp(partyline_buffer, "/unpause") == 0) {
558 sockprintf(server_sock, "pause 0 %d", my_playernum);
559 } else if (strncasecmp(partyline_buffer, "/team", 5) == 0) {
560 if (strlen(partyline_buffer) == 5)
561 strcpy(partyline_buffer+5, " "); /* make it "/team " */
562 sockprintf(server_sock, "team %d %s", my_playernum, partyline_buffer+6);
563 if (partyline_buffer[6]) {
564 if (teams[my_playernum-1])
565 free(teams[my_playernum-1]);
566 teams[my_playernum-1] = strdup(partyline_buffer+6);
567 snprintf(buf, sizeof(buf), "*** %s is Now on Team %s", players[my_playernum-1], partyline_buffer+6);
568 msg_text(BUFFER_PLINE, buf);
569 } else {
570 if (teams[my_playernum-1])
571 free(teams[my_playernum-1]);
572 teams[my_playernum-1] = NULL;
573 snprintf(buf, sizeof(buf), "*** %s is Now Alone", players[my_playernum-1]);
574 msg_text(BUFFER_PLINE, buf);
576 } else {
577 sockprintf(server_sock, "pline %d %s", my_playernum, partyline_buffer);
578 if (*partyline_buffer != '/'
579 || partyline_buffer[1] == 0 || partyline_buffer[1] == ' ') {
580 /* We do not show server-side commands. */
581 snprintf(buf, sizeof(buf), "<%s> %s", players[my_playernum-1], partyline_buffer);
582 msg_text(BUFFER_PLINE, buf);
585 partyline_pos = 0;
586 *partyline_buffer = 0;
587 io->draw_partyline_input(partyline_buffer, partyline_pos);
591 #undef curpos
593 /*************************************************************************/
594 /*************************************************************************/
596 void help()
598 fprintf(stderr,
599 "Tetrinet " VERSION " - Text-mode tetrinet client\n"
600 "\n"
601 "Usage: tetrinet [OPTION]... NICK SERVER\n"
602 "\n"
603 "Options (see README for details):\n"
604 " -fancy Use \"fancy\" TTY graphics.\n"
605 " -fast Connect to the server in the tetrifast mode.\n"
606 " -log <file> Log network traffic to the given file.\n"
607 " -noshadow Do not make the pieces cast shadow.\n"
608 " -noslide Do not allow pieces to \"slide\" after being dropped\n"
609 " with the spacebar.\n"
610 " -server Start the server instead of the client.\n"
611 " -shadow Make the pieces cast shadow. Can speed up gameplay\n"
612 " considerably, but it can be considered as cheating by\n"
613 " some people since some other tetrinet clients lack this.\n"
614 " -slide Opposite of -noslide; allows pieces to \"slide\" after\n"
615 " being dropped. If both -slide and -noslide are given,\n"
616 " -slide takes precedence.\n"
617 " -windows Behave as much like the Windows version of Tetrinet as\n"
618 " possible. Implies -noslide and -noshadow.\n"
622 int init(int ac, char **av)
624 int i;
625 char *nick = NULL, *server = NULL;
626 char buf[1024];
627 char nickmsg[1024];
628 char ip[4];
629 char iphashbuf[32];
630 int len;
631 #ifdef BUILTIN_SERVER
632 int start_server = 0; /* Start the server? (-server) */
633 #endif
634 int slide = 0; /* Do we definitely want to slide? (-slide) */
637 /* If there's a DISPLAY variable set in the environment, default to
638 * Xwindows I/O, else default to terminal I/O. */
639 /* if (getenv("DISPLAY"))
640 io = &xwin_interface;
641 else
642 io = &tty_interface; */
643 io=&tty_interface; /* because Xwin isn't done yet */
645 srand(time(NULL));
646 init_shapes();
648 for (i = 1; i < ac; i++) {
649 if (*av[i] == '-') {
650 #ifdef BUILTIN_SERVER
651 if (strcmp(av[i], "-server") == 0) {
652 start_server = 1;
653 } else
654 #endif
655 if (strcmp(av[i], "-fancy") == 0) {
656 fancy = 1;
657 } else if (strcmp(av[i], "-log") == 0) {
658 log = 1;
659 i++;
660 if (i >= ac) {
661 fprintf(stderr, "Option -log requires an argument\n");
662 return 1;
664 logname = av[i];
665 } else if (strcmp(av[i], "-noslide") == 0) {
666 noslide = 1;
667 } else if (strcmp(av[i], "-noshadow") == 0) {
668 cast_shadow = 0;
669 } else if (strcmp(av[i], "-shadow") == 0) {
670 cast_shadow = 1;
671 } else if (strcmp(av[i], "-slide") == 0) {
672 slide = 1;
673 } else if (strcmp(av[i], "-windows") == 0) {
674 windows_mode = 1;
675 noslide = 1;
676 cast_shadow = 0;
677 } else if (strcmp(av[i], "-fast") == 0) {
678 tetrifast = 1;
679 } else {
680 fprintf(stderr, "Unknown option %s\n", av[i]);
681 help();
682 return 1;
684 } else if (!nick) {
685 my_nick = nick = av[i];
686 } else if (!server) {
687 server = av[i];
688 } else {
689 help();
690 return 1;
693 if (slide)
694 noslide = 0;
695 #ifdef BUILTIN_SERVER
696 if (start_server)
697 exit(server_main());
698 #endif
699 if (!server) {
700 help();
701 return 1;
703 if (strlen(nick) > 63) /* put a reasonable limit on nick length */
704 nick[63] = 0;
706 if ((server_sock = conn(server, 31457, ip)) < 0) {
707 fprintf(stderr, "Couldn't connect to server %s: %s\n",
708 server, strerror(errno));
709 return 1;
711 sprintf(nickmsg, "tetri%s %s 1.13", tetrifast ? "faster" : "sstart", nick);
712 sprintf(iphashbuf, "%d", ip[0]*54 + ip[1]*41 + ip[2]*29 + ip[3]*17);
713 /* buf[0] does not need to be initialized for this algorithm */
714 len = strlen(nickmsg);
715 for (i = 0; i < len; i++)
716 buf[i+1] = (((buf[i]&0xFF) + (nickmsg[i]&0xFF)) % 255) ^ iphashbuf[i % strlen(iphashbuf)];
717 len++;
718 for (i = 0; i < len; i++)
719 sprintf(nickmsg+i*2, "%02X", buf[i] & 0xFF);
720 sputs(nickmsg, server_sock);
722 do {
723 if (!sgets(buf, sizeof(buf), server_sock)) {
724 fprintf(stderr, "Server %s closed connection\n", server);
725 disconn(server_sock);
726 return 1;
728 parse(buf);
729 } while (my_playernum < 0);
730 sockprintf(server_sock, "team %d ", my_playernum);
732 players[my_playernum-1] = strdup(nick);
733 dispmode = MODE_PARTYLINE;
734 io->screen_setup();
735 io->setup_partyline();
737 return 0;
740 /*************************************************************************/
742 int main(int ac, char **av)
744 int i;
746 if ((i = init(ac, av)) != 0)
747 return i;
749 for (;;) {
750 int timeout;
751 if (playing_game && !game_paused)
752 timeout = tetris_timeout();
753 else
754 timeout = -1;
755 i = io->wait_for_input(timeout);
756 if (i == -1) {
757 char buf[1024];
758 if (sgets(buf, sizeof(buf), server_sock))
759 parse(buf);
760 else {
761 msg_text(BUFFER_PLINE, "*** Disconnected from Server");
762 break;
764 } else if (i == -2) {
765 tetris_timeout_action();
766 } else if (i == 12) { /* Ctrl-L */
767 io->screen_redraw();
768 } else if (i == K_F10) {
769 break; /* out of main loop */
770 } else if (i == K_F1) {
771 if (dispmode != MODE_FIELDS) {
772 dispmode = MODE_FIELDS;
773 io->setup_fields();
775 } else if (i == K_F2) {
776 if (dispmode != MODE_PARTYLINE) {
777 dispmode = MODE_PARTYLINE;
778 io->setup_partyline();
780 } else if (i == K_F3) {
781 if (dispmode != MODE_WINLIST) {
782 dispmode = MODE_WINLIST;
783 io->setup_winlist();
785 } else if (dispmode == MODE_FIELDS) {
786 tetris_input(i);
787 } else if (dispmode == MODE_PARTYLINE) {
788 if (i == 8 || i == 127) /* Backspace or Delete */
789 partyline_backspace();
790 else if (i == 4) /* Ctrl-D */
791 partyline_delete();
792 else if (i == 21) /* Ctrl-U */
793 partyline_kill();
794 else if (i == '\r' || i == '\n')
795 partyline_enter();
796 else if (i == K_LEFT)
797 partyline_move(-1);
798 else if (i == K_RIGHT)
799 partyline_move(1);
800 else if (i == 1) /* Ctrl-A */
801 partyline_move(-2);
802 else if (i == 5) /* Ctrl-E */
803 partyline_move(2);
804 else if (i >= 1 && i <= 0xFF)
805 partyline_input(i);
809 disconn(server_sock);
810 return 0;
813 /*************************************************************************/
815 #endif /* !SERVER_ONLY */
817 /*************************************************************************/