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 /* Parse a line from the server. Destroys the buffer it's given as a side
59 cmd
= strtok(buf
, " ");
64 } else if (strcmp(cmd
, "noconnecting") == 0) {
68 /* XXX not to stderr, please! -- we need to stay running w/o server */
69 fprintf(stderr
, "Server error: %s\n", s
);
72 } else if (strcmp(cmd
, "winlist") == 0) {
75 while (i
< MAXWINLIST
&& (s
= strtok(NULL
, " "))) {
85 strncpy(winlist
[i
].name
, s
, sizeof(winlist
[i
].name
)-1);
86 winlist
[i
].name
[sizeof(winlist
[i
].name
)] = 0;
87 winlist
[i
].points
= atoi(t
);
88 if ((t
= strchr(t
, ';')) != NULL
)
89 winlist
[i
].games
= atoi(t
+1);
93 winlist
[i
].name
[0] = 0;
94 if (dispmode
== MODE_WINLIST
)
97 } else if (strcmp(cmd
, tetrifast
? ")#)(!@(*3" : "playernum") == 0) {
98 if ((s
= strtok(NULL
, " ")))
99 my_playernum
= atoi(s
);
100 /* Note: players[my_playernum-1] is set in init() */
101 /* But that doesn't work when joining other channel. */
102 players
[my_playernum
-1] = strdup(my_nick
);
104 } else if (strcmp(cmd
, "playerjoin") == 0) {
108 s
= strtok(NULL
, " ");
109 t
= strtok(NULL
, "");
113 if (player
< 0 || player
> 5)
115 players
[player
] = strdup(t
);
118 teams
[player
] = NULL
;
120 snprintf(buf
, sizeof(buf
), "*** %s is Now Playing", t
);
121 io
->draw_text(BUFFER_PLINE
, buf
);
122 if (dispmode
== MODE_FIELDS
)
125 } else if (strcmp(cmd
, "playerleave") == 0) {
129 s
= strtok(NULL
, " ");
133 if (player
< 0 || player
> 5 || !players
[player
])
135 snprintf(buf
, sizeof(buf
), "*** %s has Left", players
[player
]);
136 io
->draw_text(BUFFER_PLINE
, buf
);
137 free(players
[player
]);
138 players
[player
] = NULL
;
139 if (dispmode
== MODE_FIELDS
)
142 } else if (strcmp(cmd
, "team") == 0) {
146 s
= strtok(NULL
, " ");
147 t
= strtok(NULL
, "");
151 if (player
< 0 || player
> 5 || !players
[player
])
156 teams
[player
] = strdup(t
);
158 teams
[player
] = NULL
;
160 snprintf(buf
, sizeof(buf
), "*** %s is Now on Team %s", players
[player
], t
);
162 snprintf(buf
, sizeof(buf
), "*** %s is Now Alone", players
[player
]);
163 io
->draw_text(BUFFER_PLINE
, buf
);
165 } else if (strcmp(cmd
, "pline") == 0) {
167 char buf
[1024], *name
;
169 s
= strtok(NULL
, " ");
170 t
= strtok(NULL
, "");
175 playernum
= atoi(s
)-1;
176 if (playernum
== -1) {
179 if (playernum
< 0 || playernum
> 5 || !players
[playernum
])
181 name
= players
[playernum
];
183 snprintf(buf
, sizeof(buf
), "<%s> %s", name
, t
);
184 io
->draw_text(BUFFER_PLINE
, buf
);
186 } else if (strcmp(cmd
, "plineact") == 0) {
188 char buf
[1024], *name
;
190 s
= strtok(NULL
, " ");
191 t
= strtok(NULL
, "");
196 playernum
= atoi(s
)-1;
197 if (playernum
== -1) {
200 if (playernum
< 0 || playernum
> 5 || !players
[playernum
])
202 name
= players
[playernum
];
204 snprintf(buf
, sizeof(buf
), "* %s %s", name
, t
);
205 io
->draw_text(BUFFER_PLINE
, buf
);
207 } else if (strcmp(cmd
, tetrifast
? "*******" : "newgame") == 0) {
210 if ((s
= strtok(NULL
, " ")))
212 if ((s
= strtok(NULL
, " ")))
213 initial_level
= atoi(s
);
214 if ((s
= strtok(NULL
, " ")))
215 lines_per_level
= atoi(s
);
216 if ((s
= strtok(NULL
, " ")))
218 if ((s
= strtok(NULL
, " ")))
219 special_lines
= atoi(s
);
220 if ((s
= strtok(NULL
, " ")))
221 special_count
= atoi(s
);
222 if ((s
= strtok(NULL
, " "))) {
223 special_capacity
= atoi(s
);
224 if (special_capacity
> MAX_SPECIALS
)
225 special_capacity
= MAX_SPECIALS
;
227 if ((s
= strtok(NULL
, " "))) {
228 memset(piecefreq
, 0, sizeof(piecefreq
));
236 if ((s
= strtok(NULL
, " "))) {
237 memset(specialfreq
, 0, sizeof(specialfreq
));
245 if ((s
= strtok(NULL
, " ")))
246 level_average
= atoi(s
);
247 if ((s
= strtok(NULL
, " ")))
250 for (i
= 0; i
< 6; i
++)
251 levels
[i
] = initial_level
;
252 memset(&fields
[my_playernum
-1], 0, sizeof(Field
));
254 io
->clear_text(BUFFER_GMSG
);
255 io
->clear_text(BUFFER_ATTDEF
);
259 io
->draw_text(BUFFER_PLINE
, "*** The Game Has Started");
261 } else if (strcmp(cmd
, "ingame") == 0) {
262 /* Sent when a player connects in the middle of a game */
266 s
= buf
+ sprintf(buf
, "f %d ", my_playernum
);
267 for (y
= 0; y
< FIELD_HEIGHT
; y
++) {
268 for (x
= 0; x
< FIELD_WIDTH
; x
++) {
269 fields
[my_playernum
-1][y
][x
] = rand()%5 + 1;
270 *s
++ = '0' + fields
[my_playernum
-1][y
][x
];
274 sputs(buf
, server_sock
);
276 not_playing_game
= 1;
278 } else if (strcmp(cmd
, "pause") == 0) {
279 if ((s
= strtok(NULL
, " ")))
280 game_paused
= atoi(s
);
282 io
->draw_text(BUFFER_PLINE
, "*** The Game Has Been Paused");
283 io
->draw_text(BUFFER_GMSG
, "*** The Game Has Been Paused");
285 io
->draw_text(BUFFER_PLINE
, "*** The Game Has Been Unpaused");
286 io
->draw_text(BUFFER_GMSG
, "*** The Game Has Been Unpaused");
289 } else if (strcmp(cmd
, "endgame") == 0) {
291 not_playing_game
= 0;
292 memset(fields
, 0, sizeof(fields
));
294 io
->clear_text(BUFFER_ATTDEF
);
295 io
->draw_text(BUFFER_PLINE
, "*** The Game Has Ended");
296 if (dispmode
== MODE_FIELDS
) {
298 io
->draw_own_field();
299 for (i
= 1; i
<= 6; i
++) {
300 if (i
!= my_playernum
)
301 io
->draw_other_field(i
);
305 } else if (strcmp(cmd
, "playerwon") == 0) {
306 /* Syntax: playerwon # -- sent when all but one player lose */
308 } else if (strcmp(cmd
, "playerlost") == 0) {
309 /* Syntax: playerlost # -- sent after playerleave on disconnect
310 * during a game, or when a player loses (sent by the losing
311 * player and from the server to all other players */
313 } else if (strcmp(cmd
, "f") == 0) { /* field */
314 int player
, x
, y
, tile
;
316 /* This looks confusing, but what it means is, ignore this message
317 * if a game isn't going on. */
318 if (!playing_game
&& !not_playing_game
)
320 if (!(s
= strtok(NULL
, " ")))
324 if (!(s
= strtok(NULL
, "")))
327 /* Set field directly */
328 char *ptr
= (char *) fields
[player
];
331 *ptr
++ = (*s
++) - '0';
333 case 'a': *ptr
++ = 6 + SPECIAL_A
; break;
334 case 'b': *ptr
++ = 6 + SPECIAL_B
; break;
335 case 'c': *ptr
++ = 6 + SPECIAL_C
; break;
336 case 'g': *ptr
++ = 6 + SPECIAL_G
; break;
337 case 'n': *ptr
++ = 6 + SPECIAL_N
; break;
338 case 'o': *ptr
++ = 6 + SPECIAL_O
; break;
339 case 'q': *ptr
++ = 6 + SPECIAL_Q
; break;
340 case 'r': *ptr
++ = 6 + SPECIAL_R
; break;
341 case 's': *ptr
++ = 6 + SPECIAL_S
; break;
345 /* Set specific locations on field */
353 fields
[player
][y
][x
] = tile
;
358 if (player
== my_playernum
-1)
359 io
->draw_own_field();
361 io
->draw_other_field(player
+1);
362 } else if (strcmp(cmd
, "lvl") == 0) {
365 if (!(s
= strtok(NULL
, " ")))
368 if (!(s
= strtok(NULL
, "")))
370 levels
[player
] = atoi(s
);
372 } else if (strcmp(cmd
, "sb") == 0) {
376 if (!(s
= strtok(NULL
, " ")))
379 if (!(type
= strtok(NULL
, " ")))
381 if (!(s
= strtok(NULL
, " ")))
384 do_special(type
, from
, to
);
386 } else if (strcmp(cmd
, "gmsg") == 0) {
387 if (!(s
= strtok(NULL
, "")))
389 io
->draw_text(BUFFER_GMSG
, s
);
394 /*************************************************************************/
395 /*************************************************************************/
397 static char partyline_buffer
[512];
398 static int partyline_pos
= 0;
400 #define curpos (partyline_buffer+partyline_pos)
402 /*************************************************************************/
404 /* Add a character to the partyline buffer. */
406 void partyline_input(int c
)
408 if (partyline_pos
< sizeof(partyline_buffer
) - 1) {
409 memmove(curpos
+1, curpos
, strlen(curpos
)+1);
410 partyline_buffer
[partyline_pos
++] = c
;
411 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
415 /*************************************************************************/
417 /* Delete the current character from the partyline buffer. */
419 void partyline_delete(void)
421 if (partyline_buffer
[partyline_pos
]) {
422 memmove(curpos
, curpos
+1, strlen(curpos
)-1+1);
423 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
427 /*************************************************************************/
429 /* Backspace a character from the partyline buffer. */
431 void partyline_backspace(void)
433 if (partyline_pos
> 0) {
439 /*************************************************************************/
441 /* Kill the entire partyline input buffer. */
443 void partyline_kill(void)
446 *partyline_buffer
= 0;
447 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
450 /*************************************************************************/
452 /* Move around the input buffer. Sign indicates direction; absolute value
453 * of 1 means one character, 2 means the whole line.
456 void partyline_move(int how
)
460 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
461 } else if (how
== -1 && partyline_pos
> 0) {
463 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
464 } else if (how
== 1 && partyline_buffer
[partyline_pos
]) {
466 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
467 } else if (how
== 2) {
468 partyline_pos
= strlen(partyline_buffer
);
469 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
473 /*************************************************************************/
475 /* Send the input line to the server. */
477 void partyline_enter(void)
481 if (*partyline_buffer
) {
482 if (strncasecmp(partyline_buffer
, "/me ", 4) == 0) {
483 sockprintf(server_sock
, "plineact %d %s", my_playernum
, partyline_buffer
+4);
484 snprintf(buf
, sizeof(buf
), "* %s %s", players
[my_playernum
-1], partyline_buffer
+4);
485 io
->draw_text(BUFFER_PLINE
, buf
);
486 } else if (strcasecmp(partyline_buffer
, "/start") == 0) {
487 sockprintf(server_sock
, "startgame 1 %d", my_playernum
);
488 } else if (strcasecmp(partyline_buffer
, "/end") == 0) {
489 sockprintf(server_sock
, "startgame 0 %d", my_playernum
);
490 } else if (strcasecmp(partyline_buffer
, "/pause") == 0) {
491 sockprintf(server_sock
, "pause 1 %d", my_playernum
);
492 } else if (strcasecmp(partyline_buffer
, "/unpause") == 0) {
493 sockprintf(server_sock
, "pause 0 %d", my_playernum
);
494 } else if (strncasecmp(partyline_buffer
, "/team", 5) == 0) {
495 if (strlen(partyline_buffer
) == 5)
496 strcpy(partyline_buffer
+5, " "); /* make it "/team " */
497 sockprintf(server_sock
, "team %d %s", my_playernum
, partyline_buffer
+6);
498 if (partyline_buffer
[6]) {
499 if (teams
[my_playernum
-1])
500 free(teams
[my_playernum
-1]);
501 teams
[my_playernum
-1] = strdup(partyline_buffer
+6);
502 snprintf(buf
, sizeof(buf
), "*** %s is Now on Team %s", players
[my_playernum
-1], partyline_buffer
+6);
503 io
->draw_text(BUFFER_PLINE
, buf
);
505 if (teams
[my_playernum
-1])
506 free(teams
[my_playernum
-1]);
507 teams
[my_playernum
-1] = NULL
;
508 snprintf(buf
, sizeof(buf
), "*** %s is Now Alone", players
[my_playernum
-1]);
509 io
->draw_text(BUFFER_PLINE
, buf
);
512 sockprintf(server_sock
, "pline %d %s", my_playernum
, partyline_buffer
);
513 if (*partyline_buffer
!= '/'
514 || partyline_buffer
[1] == 0 || partyline_buffer
[1] == ' ') {
515 /* We do not show server-side commands. */
516 snprintf(buf
, sizeof(buf
), "<%s> %s", players
[my_playernum
-1], partyline_buffer
);
517 io
->draw_text(BUFFER_PLINE
, buf
);
521 *partyline_buffer
= 0;
522 io
->draw_partyline_input(partyline_buffer
, partyline_pos
);
528 /*************************************************************************/
529 /*************************************************************************/
534 "Tetrinet " VERSION
" - Text-mode tetrinet client\n"
536 "Usage: tetrinet [OPTION]... NICK SERVER\n"
538 "Options (see README for details):\n"
539 " -fancy Use \"fancy\" TTY graphics.\n"
540 " -fast Connect to the server in the tetrifast mode.\n"
541 " -log <file> Log network traffic to the given file.\n"
542 " -noshadow Do not make the pieces cast shadow.\n"
543 " -noslide Do not allow pieces to \"slide\" after being dropped\n"
544 " with the spacebar.\n"
545 " -server Start the server instead of the client.\n"
546 " -shadow Make the pieces cast shadow. Can speed up gameplay\n"
547 " considerably, but it can be considered as cheating by\n"
548 " some people since some other tetrinet clients lack this.\n"
549 " -slide Opposite of -noslide; allows pieces to \"slide\" after\n"
550 " being dropped. If both -slide and -noslide are given,\n"
551 " -slide takes precedence.\n"
552 " -windows Behave as much like the Windows version of Tetrinet as\n"
553 " possible. Implies -noslide and -noshadow.\n"
557 int init(int ac
, char **av
)
560 char *nick
= NULL
, *server
= NULL
;
566 #ifdef BUILTIN_SERVER
567 int start_server
= 0; /* Start the server? (-server) */
569 int slide
= 0; /* Do we definitely want to slide? (-slide) */
572 /* If there's a DISPLAY variable set in the environment, default to
573 * Xwindows I/O, else default to terminal I/O. */
574 if (getenv("DISPLAY"))
575 io
= &xwin_interface
;
578 io
=&tty_interface
; /* because Xwin isn't done yet */
583 for (i
= 1; i
< ac
; i
++) {
585 #ifdef BUILTIN_SERVER
586 if (strcmp(av
[i
], "-server") == 0) {
590 if (strcmp(av
[i
], "-fancy") == 0) {
592 } else if (strcmp(av
[i
], "-log") == 0) {
596 fprintf(stderr
, "Option -log requires an argument\n");
600 } else if (strcmp(av
[i
], "-noslide") == 0) {
602 } else if (strcmp(av
[i
], "-noshadow") == 0) {
604 } else if (strcmp(av
[i
], "-shadow") == 0) {
606 } else if (strcmp(av
[i
], "-slide") == 0) {
608 } else if (strcmp(av
[i
], "-windows") == 0) {
612 } else if (strcmp(av
[i
], "-fast") == 0) {
615 fprintf(stderr
, "Unknown option %s\n", av
[i
]);
620 my_nick
= nick
= av
[i
];
621 } else if (!server
) {
630 #ifdef BUILTIN_SERVER
638 if (strlen(nick
) > 63) /* put a reasonable limit on nick length */
641 if ((server_sock
= conn(server
, 31457, ip
)) < 0) {
642 fprintf(stderr
, "Couldn't connect to server %s: %s\n",
643 server
, strerror(errno
));
646 sprintf(nickmsg
, "tetri%s %s 1.13", tetrifast
? "faster" : "sstart", nick
);
647 sprintf(iphashbuf
, "%d", ip
[0]*54 + ip
[1]*41 + ip
[2]*29 + ip
[3]*17);
648 /* buf[0] does not need to be initialized for this algorithm */
649 len
= strlen(nickmsg
);
650 for (i
= 0; i
< len
; i
++)
651 buf
[i
+1] = (((buf
[i
]&0xFF) + (nickmsg
[i
]&0xFF)) % 255) ^ iphashbuf
[i
% strlen(iphashbuf
)];
653 for (i
= 0; i
< len
; i
++)
654 sprintf(nickmsg
+i
*2, "%02X", buf
[i
] & 0xFF);
655 sputs(nickmsg
, server_sock
);
658 if (!sgets(buf
, sizeof(buf
), server_sock
)) {
659 fprintf(stderr
, "Server %s closed connection\n", server
);
660 disconn(server_sock
);
664 } while (my_playernum
< 0);
665 sockprintf(server_sock
, "team %d ", my_playernum
);
667 players
[my_playernum
-1] = strdup(nick
);
668 dispmode
= MODE_PARTYLINE
;
670 io
->setup_partyline();
675 /*************************************************************************/
677 int main(int ac
, char **av
)
681 if ((i
= init(ac
, av
)) != 0)
686 if (playing_game
&& !game_paused
)
687 timeout
= tetris_timeout();
690 i
= io
->wait_for_input(timeout
);
693 if (sgets(buf
, sizeof(buf
), server_sock
))
696 io
->draw_text(BUFFER_PLINE
, "*** Disconnected from Server");
699 } else if (i
== -2) {
700 tetris_timeout_action();
701 } else if (i
== 12) { /* Ctrl-L */
703 } else if (i
== K_F10
) {
704 break; /* out of main loop */
705 } else if (i
== K_F1
) {
706 if (dispmode
!= MODE_FIELDS
) {
707 dispmode
= MODE_FIELDS
;
710 } else if (i
== K_F2
) {
711 if (dispmode
!= MODE_PARTYLINE
) {
712 dispmode
= MODE_PARTYLINE
;
713 io
->setup_partyline();
715 } else if (i
== K_F3
) {
716 if (dispmode
!= MODE_WINLIST
) {
717 dispmode
= MODE_WINLIST
;
720 } else if (dispmode
== MODE_FIELDS
) {
722 } else if (dispmode
== MODE_PARTYLINE
) {
723 if (i
== 8 || i
== 127) /* Backspace or Delete */
724 partyline_backspace();
725 else if (i
== 4) /* Ctrl-D */
727 else if (i
== 21) /* Ctrl-U */
729 else if (i
== '\r' || i
== '\n')
731 else if (i
== K_LEFT
)
733 else if (i
== K_RIGHT
)
735 else if (i
== 1) /* Ctrl-A */
737 else if (i
== 5) /* Ctrl-E */
739 else if (i
>= 1 && i
<= 0xFF)
744 disconn(server_sock
);
748 /*************************************************************************/
750 #endif /* !SERVER_ONLY */
752 /*************************************************************************/