1 /* Tetrinet for Linux, by Andrew Church <achurch@achurch.org>
2 * This program is public domain.
4 * Tetrinet main program.
7 /*************************************************************************/
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 /*************************************************************************/
49 /*************************************************************************/
51 /* Output message to a message buffer, possibly decoding the text attributes
54 void msg_text(int bufnum
, const char *s
)
56 /* Stolen from gtetrinet: (leading space <=> undefined) */
57 static enum tattr map
[32] = {
61 TATTR_CCYAN
| TATTR_CXBRIGHT
,
63 TATTR_CBLUE
| TATTR_CXBRIGHT
,
69 TATTR_CBLACK
| TATTR_CXBRIGHT
,
72 TATTR_CGREEN
| TATTR_CXBRIGHT
,
77 TATTR_CMAGENTA
| TATTR_CXBRIGHT
,
78 TATTR_CRED
| TATTR_CXBRIGHT
,
82 TATTR_CGREY
| TATTR_CXBRIGHT
,
83 TATTR_CBROWN
| TATTR_CXBRIGHT
,
93 for (t
= tb
; *s
&& t
- tb
< 1024; t
++, s
++) {
97 *t
= map
[(int) *s
] + 1;
102 if (t
- tb
>= 1024) t
= &tb
[1023];
105 io
->draw_text(bufnum
, tb
);
109 /* Parse a line from the server. Destroys the buffer it's given as a side
113 void parse(char *buf
)
117 cmd
= strtok(buf
, " ");
122 } else if (strcmp(cmd
, "noconnecting") == 0) {
123 s
= strtok(NULL
, "");
126 /* XXX not to stderr, please! -- we need to stay running w/o server */
127 fprintf(stderr
, "Server error: %s\n", s
);
130 } else if (strcmp(cmd
, "winlist") == 0) {
133 while (i
< MAXWINLIST
&& (s
= strtok(NULL
, " "))) {
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);
150 winlist
[i
].name
[0] = 0;
151 if (dispmode
== MODE_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) {
165 s
= strtok(NULL
, " ");
166 t
= strtok(NULL
, "");
170 if (player
< 0 || player
> 5)
172 players
[player
] = strdup(t
);
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
)
182 } else if (strcmp(cmd
, "playerleave") == 0) {
186 s
= strtok(NULL
, " ");
190 if (player
< 0 || player
> 5 || !players
[player
])
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
)
199 } else if (strcmp(cmd
, "team") == 0) {
203 s
= strtok(NULL
, " ");
204 t
= strtok(NULL
, "");
208 if (player
< 0 || player
> 5 || !players
[player
])
213 teams
[player
] = strdup(t
);
215 teams
[player
] = NULL
;
217 snprintf(buf
, sizeof(buf
), "*** %s is Now on Team %s", players
[player
], t
);
219 snprintf(buf
, sizeof(buf
), "*** %s is Now Alone", players
[player
]);
220 msg_text(BUFFER_PLINE
, buf
);
222 } else if (strcmp(cmd
, "pline") == 0) {
224 char buf
[1024], *name
;
226 s
= strtok(NULL
, " ");
227 t
= strtok(NULL
, "");
232 playernum
= atoi(s
)-1;
233 if (playernum
== -1) {
236 if (playernum
< 0 || playernum
> 5 || !players
[playernum
])
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) {
245 char buf
[1024], *name
;
247 s
= strtok(NULL
, " ");
248 t
= strtok(NULL
, "");
253 playernum
= atoi(s
)-1;
254 if (playernum
== -1) {
257 if (playernum
< 0 || playernum
> 5 || !players
[playernum
])
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) {
267 if ((s
= strtok(NULL
, " ")))
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
, " ")))
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
));
293 if ((s
= strtok(NULL
, " "))) {
294 memset(specialfreq
, 0, sizeof(specialfreq
));
302 if ((s
= strtok(NULL
, " ")))
303 level_average
= atoi(s
);
304 if ((s
= strtok(NULL
, " ")))
307 for (i
= 0; i
< 6; i
++)
308 levels
[i
] = initial_level
;
309 memset(&fields
[my_playernum
-1], 0, sizeof(Field
));
311 io
->clear_text(BUFFER_GMSG
);
312 io
->clear_text(BUFFER_ATTDEF
);
316 msg_text(BUFFER_PLINE
, "*** The Game Has Started");
317 if (dispmode
!= MODE_FIELDS
) {
318 dispmode
= MODE_FIELDS
;
322 } else if (strcmp(cmd
, "ingame") == 0) {
323 /* Sent when a player connects in the middle of a game */
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
];
335 sputs(buf
, server_sock
);
337 not_playing_game
= 1;
339 } else if (strcmp(cmd
, "pause") == 0) {
340 if ((s
= strtok(NULL
, " ")))
341 game_paused
= atoi(s
);
343 msg_text(BUFFER_PLINE
, "*** The Game Has Been Paused");
344 msg_text(BUFFER_GMSG
, "*** The Game Has Been Paused");
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) {
352 not_playing_game
= 0;
353 memset(fields
, 0, sizeof(fields
));
355 io
->clear_text(BUFFER_ATTDEF
);
356 msg_text(BUFFER_PLINE
, "*** The Game Has Ended");
357 if (dispmode
== MODE_FIELDS
) {
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
)
385 if (!(s
= strtok(NULL
, " ")))
389 if (!(s
= strtok(NULL
, "")))
392 /* Set field directly */
393 char *ptr
= (char *) fields
[player
];
396 *ptr
++ = (*s
++) - '0';
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;
410 /* Set specific locations on field */
418 fields
[player
][y
][x
] = tile
;
423 if (player
== my_playernum
-1)
424 io
->draw_own_field();
426 io
->draw_other_field(player
+1);
427 } else if (strcmp(cmd
, "lvl") == 0) {
430 if (!(s
= strtok(NULL
, " ")))
433 if (!(s
= strtok(NULL
, "")))
435 levels
[player
] = atoi(s
);
437 } else if (strcmp(cmd
, "sb") == 0) {
441 if (!(s
= strtok(NULL
, " ")))
444 if (!(type
= strtok(NULL
, " ")))
446 if (!(s
= strtok(NULL
, " ")))
449 do_special(type
, from
, to
);
451 } else if (strcmp(cmd
, "gmsg") == 0) {
452 if (!(s
= strtok(NULL
, "")))
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) {
504 /*************************************************************************/
506 /* Kill the entire partyline input buffer. */
508 void partyline_kill(void)
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
)
525 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
526 } else if (how
== -1 && partyline_pos
> 0) {
528 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
529 } else if (how
== 1 && partyline_buffer
[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)
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
);
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
);
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
);
586 *partyline_buffer
= 0;
587 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
593 /*************************************************************************/
594 /*************************************************************************/
599 "Tetrinet " VERSION
" - Text-mode tetrinet client\n"
601 "Usage: tetrinet [OPTION]... NICK SERVER\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
)
625 char *nick
= NULL
, *server
= NULL
;
631 #ifdef BUILTIN_SERVER
632 int start_server
= 0; /* Start the server? (-server) */
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;
642 io = &tty_interface; */
643 io
=&tty_interface
; /* because Xwin isn't done yet */
648 for (i
= 1; i
< ac
; i
++) {
650 #ifdef BUILTIN_SERVER
651 if (strcmp(av
[i
], "-server") == 0) {
655 if (strcmp(av
[i
], "-fancy") == 0) {
657 } else if (strcmp(av
[i
], "-log") == 0) {
661 fprintf(stderr
, "Option -log requires an argument\n");
665 } else if (strcmp(av
[i
], "-noslide") == 0) {
667 } else if (strcmp(av
[i
], "-noshadow") == 0) {
669 } else if (strcmp(av
[i
], "-shadow") == 0) {
671 } else if (strcmp(av
[i
], "-slide") == 0) {
673 } else if (strcmp(av
[i
], "-windows") == 0) {
677 } else if (strcmp(av
[i
], "-fast") == 0) {
680 fprintf(stderr
, "Unknown option %s\n", av
[i
]);
685 my_nick
= nick
= av
[i
];
686 } else if (!server
) {
695 #ifdef BUILTIN_SERVER
703 if (strlen(nick
) > 63) /* put a reasonable limit on nick length */
706 if ((server_sock
= conn(server
, 31457, ip
)) < 0) {
707 fprintf(stderr
, "Couldn't connect to server %s: %s\n",
708 server
, strerror(errno
));
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
)];
718 for (i
= 0; i
< len
; i
++)
719 sprintf(nickmsg
+i
*2, "%02X", buf
[i
] & 0xFF);
720 sputs(nickmsg
, server_sock
);
723 if (!sgets(buf
, sizeof(buf
), server_sock
)) {
724 fprintf(stderr
, "Server %s closed connection\n", server
);
725 disconn(server_sock
);
729 } while (my_playernum
< 0);
730 sockprintf(server_sock
, "team %d ", my_playernum
);
732 players
[my_playernum
-1] = strdup(nick
);
733 dispmode
= MODE_PARTYLINE
;
735 io
->setup_partyline();
740 /*************************************************************************/
742 int main(int ac
, char **av
)
746 if ((i
= init(ac
, av
)) != 0)
751 if (playing_game
&& !game_paused
)
752 timeout
= tetris_timeout();
755 i
= io
->wait_for_input(timeout
);
758 if (sgets(buf
, sizeof(buf
), server_sock
))
761 msg_text(BUFFER_PLINE
, "*** Disconnected from Server");
764 } else if (i
== -2) {
765 tetris_timeout_action();
766 } else if (i
== 12) { /* Ctrl-L */
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
;
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
;
785 } else if (dispmode
== MODE_FIELDS
) {
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 */
792 else if (i
== 21) /* Ctrl-U */
794 else if (i
== '\r' || i
== '\n')
796 else if (i
== K_LEFT
)
798 else if (i
== K_RIGHT
)
800 else if (i
== 1) /* Ctrl-A */
802 else if (i
== 5) /* Ctrl-E */
804 else if (i
>= 1 && i
<= 0xFF)
809 disconn(server_sock
);
813 /*************************************************************************/
815 #endif /* !SERVER_ONLY */
817 /*************************************************************************/