1 /* Tetrinet for Linux, by Andrew Church <achurch@achurch.org>
2 * This program is public domain.
4 * Tetrinet main program.
7 /*************************************************************************/
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 /*************************************************************************/
45 /*************************************************************************/
47 /* Parse a line from the server. Destroys the buffer it's given as a side
55 cmd
= strtok(buf
, " ");
60 } else if (strcmp(cmd
, "noconnecting") == 0) {
64 /* XXX not to stderr, please! -- we need to stay running w/o server */
65 fprintf(stderr
, "Server error: %s\n", s
);
68 } else if (strcmp(cmd
, "winlist") == 0) {
71 while (i
< MAXWINLIST
&& (s
= strtok(NULL
, " "))) {
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);
89 winlist
[i
].name
[0] = 0;
90 if (dispmode
== MODE_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) {
104 s
= strtok(NULL
, " ");
105 t
= strtok(NULL
, "");
109 if (player
< 0 || player
> 5)
111 players
[player
] = strdup(t
);
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
)
121 } else if (strcmp(cmd
, "playerleave") == 0) {
125 s
= strtok(NULL
, " ");
129 if (player
< 0 || player
> 5 || !players
[player
])
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
)
138 } else if (strcmp(cmd
, "team") == 0) {
142 s
= strtok(NULL
, " ");
143 t
= strtok(NULL
, "");
147 if (player
< 0 || player
> 5 || !players
[player
])
152 teams
[player
] = strdup(t
);
154 teams
[player
] = NULL
;
156 snprintf(buf
, sizeof(buf
), "*** %s is Now on Team %s", players
[player
], t
);
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) {
163 char buf
[1024], *name
;
165 s
= strtok(NULL
, " ");
166 t
= strtok(NULL
, "");
171 playernum
= atoi(s
)-1;
172 if (playernum
== -1) {
175 if (playernum
< 0 || playernum
> 5 || !players
[playernum
])
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) {
184 char buf
[1024], *name
;
186 s
= strtok(NULL
, " ");
187 t
= strtok(NULL
, "");
192 playernum
= atoi(s
)-1;
193 if (playernum
== -1) {
196 if (playernum
< 0 || playernum
> 5 || !players
[playernum
])
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) {
206 if (s
= strtok(NULL
, " "))
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
, " "))
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
));
232 if (s
= strtok(NULL
, " ")) {
233 memset(specialfreq
, 0, sizeof(specialfreq
));
241 if (s
= strtok(NULL
, " "))
242 level_average
= atoi(s
);
243 if (s
= strtok(NULL
, " "))
246 for (i
= 0; i
< 6; i
++)
247 levels
[i
] = initial_level
;
248 memset(&fields
[my_playernum
-1], 0, sizeof(Field
));
250 io
->clear_text(BUFFER_GMSG
);
251 io
->clear_text(BUFFER_ATTDEF
);
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 */
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
];
270 sputs(buf
, server_sock
);
272 not_playing_game
= 1;
274 } else if (strcmp(cmd
, "pause") == 0) {
275 if (s
= strtok(NULL
, " "))
276 game_paused
= atoi(s
);
278 io
->draw_text(BUFFER_PLINE
, "*** The Game Has Been Paused");
279 io
->draw_text(BUFFER_GMSG
, "*** The Game Has Been Paused");
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) {
287 not_playing_game
= 0;
288 memset(fields
, 0, sizeof(fields
));
290 io
->clear_text(BUFFER_ATTDEF
);
291 io
->draw_text(BUFFER_PLINE
, "*** The Game Has Ended");
292 if (dispmode
== MODE_FIELDS
) {
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
)
316 if (!(s
= strtok(NULL
, " ")))
320 if (!(s
= strtok(NULL
, "")))
323 /* Set field directly */
324 char *ptr
= (char *) fields
[player
];
327 *ptr
++ = (*s
++) - '0';
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;
341 /* Set specific locations on field */
349 fields
[player
][y
][x
] = tile
;
354 if (player
== my_playernum
-1)
355 io
->draw_own_field();
357 io
->draw_other_field(player
+1);
358 } else if (strcmp(cmd
, "lvl") == 0) {
361 if (!(s
= strtok(NULL
, " ")))
364 if (!(s
= strtok(NULL
, "")))
366 levels
[player
] = atoi(s
);
368 } else if (strcmp(cmd
, "sb") == 0) {
372 if (!(s
= strtok(NULL
, " ")))
375 if (!(type
= strtok(NULL
, " ")))
377 if (!(s
= strtok(NULL
, " ")))
380 do_special(type
, from
, to
);
382 } else if (strcmp(cmd
, "gmsg") == 0) {
383 if (!(s
= strtok(NULL
, "")))
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) {
435 /*************************************************************************/
437 /* Kill the entire partyline input buffer. */
439 void partyline_kill(void)
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
)
456 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
457 } else if (how
== -1 && partyline_pos
> 0) {
459 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
460 } else if (how
== 1 && partyline_buffer
[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)
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
);
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
);
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
);
517 *partyline_buffer
= 0;
518 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
524 /*************************************************************************/
525 /*************************************************************************/
530 "Usage: tetrinet [OPTION]... NICK SERVER\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
)
549 char *nick
= NULL
, *server
= NULL
;
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
;
565 io
=&tty_interface
; /* because Xwin isn't done yet */
570 for (i
= 1; i
< ac
; i
++) {
572 if (strcmp(av
[i
], "-server") == 0) {
574 } else if (strcmp(av
[i
], "-fancy") == 0) {
576 } else if (strcmp(av
[i
], "-log") == 0) {
580 fprintf(stderr
, "Option -log requires an argument\n");
584 } else if (strcmp(av
[i
], "-noslide") == 0) {
586 } else if (strcmp(av
[i
], "-slide") == 0) {
588 } else if (strcmp(av
[i
], "-windows") == 0) {
591 } else if (strcmp(av
[i
], "-fast") == 0) {
594 fprintf(stderr
, "Unknown option %s\n", av
[i
]);
599 my_nick
= nick
= av
[i
];
600 } else if (!server
) {
615 if (strlen(nick
) > 63) /* put a reasonable limit on nick length */
618 if ((server_sock
= conn(server
, 31457, ip
)) < 0) {
619 fprintf(stderr
, "Couldn't connect to server %s: %s\n",
620 server
, strerror(errno
));
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
)];
630 for (i
= 0; i
< len
; i
++)
631 sprintf(nickmsg
+i
*2, "%02X", buf
[i
] & 0xFF);
632 sputs(nickmsg
, server_sock
);
635 if (!sgets(buf
, sizeof(buf
), server_sock
)) {
636 fprintf(stderr
, "Server %s closed connection\n", server
);
637 disconn(server_sock
);
641 } while (my_playernum
< 0);
642 sockprintf(server_sock
, "team %d ", my_playernum
);
644 players
[my_playernum
-1] = strdup(nick
);
645 dispmode
= MODE_PARTYLINE
;
647 io
->setup_partyline();
652 /*************************************************************************/
654 int main(int ac
, char **av
)
658 if ((i
= init(ac
, av
)) != 0)
663 if (playing_game
&& !game_paused
)
664 timeout
= tetris_timeout();
667 i
= io
->wait_for_input(timeout
);
670 if (sgets(buf
, sizeof(buf
), server_sock
))
673 io
->draw_text(BUFFER_PLINE
, "*** Disconnected from Server");
676 } else if (i
== -2) {
677 tetris_timeout_action();
678 } else if (i
== 12) { /* Ctrl-L */
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
;
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
;
697 } else if (dispmode
== MODE_FIELDS
) {
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 */
704 else if (i
== 21) /* Ctrl-U */
706 else if (i
== '\r' || i
== '\n')
708 else if (i
== K_LEFT
)
710 else if (i
== K_RIGHT
)
712 else if (i
== 1) /* Ctrl-A */
714 else if (i
== 5) /* Ctrl-E */
716 else if (i
>= 1 && i
<= 0xFF)
721 disconn(server_sock
);
725 /*************************************************************************/
727 #endif /* !SERVER_ONLY */
729 /*************************************************************************/