2 * Netris -- A free networked version of T*tris
3 * Copyright (C) 1994-1996,1999 Mark H. Weaver <mhw@netris.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <sys/types.h>
34 #ifdef NCURSES_VERSION
38 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
39 static EventGenRec keyGen = {
40 // NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key
41 NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key
44 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
45 static int boardSize[MAX_SCREENS];
47 static int statusYPos, statusXPos;
48 static int messageYPos, messageXPos, messageHeight, messageWidth;
51 int PlayerDisp[MAX_SCREENS];
53 #define MSG_HEIGHT 64 //max history
54 char *message[MSG_HEIGHT];
55 char messages[MSG_HEIGHT][MSG_WIDTH];
57 static char *term_vi; /* String to make cursor invisible */
58 static char *term_ve; /* String to make cursor visible */
60 void InitScreens(void)
67 * Block signals while initializing curses. Otherwise a badly timed
68 * Ctrl-C during initialization might leave the terminal in a bad state.
70 BlockSignals(&oldMask, SIGINT, 0);
71 initscr(); //start curses
82 haveColor = Sets.color && has_colors();
88 { BT_white, COLOR_WHITE },
89 { BT_blue, COLOR_BLUE },
90 { BT_magenta, COLOR_MAGENTA },
91 { BT_cyan, COLOR_CYAN },
92 { BT_yellow, COLOR_YELLOW },
93 { BT_green, COLOR_GREEN },
94 { BT_red, COLOR_RED },
100 if (can_change_color()) {
101 init_color (COLOR_YELLOW, 1000, 1000, 0);
102 } //I've never worked on a color-changable terminal, so no idea..
103 for (i = 0; myColorTable[i].type != BT_none; ++i)
104 init_pair(myColorTable[i].type, COLOR_BLACK,
105 myColorTable[i].color);
111 AtExit(CleanupScreens); //restore everything when done
112 RestoreSignals(NULL, &oldMask);
114 cbreak(); //no line buffering
116 // keypad(stdscr, TRUE); //get arrow/functionkeys 'n stuff
117 OutputTermStr(term_vi, 0);
118 AddEventGen(&keyGen); //key handler
119 signal(SIGWINCH, CatchWinCh); //handle window resize
120 // ioctl(STDIN_FILENO, KDSKBMODE, K_MEDIUMRAW);
121 standend(); //normal text
123 memset(messages, 0, sizeof(messages)); //empty messages
126 for (i = 0; i<MSG_HEIGHT; i++)
127 message[i] = messages[i]; //set pointers
131 void CleanupScreens(void)
133 RemoveEventGen(&keyGen);
134 endwin(); //end curses
135 OutputTermStr(term_ve, 1);
138 void GetTermcapInfo(void)
140 char *term, *buf, *data;
144 if (!(term = getenv("TERM")))
146 if (tgetent(scratch, term) == 1) {
148 * Make the buffer HUGE, since tgetstr is unsafe.
149 * Allocate it on the heap too.
151 data = buf = malloc(bufSize);
154 * There is no standard include file for tgetstr, no prototype
155 * definitions. I like casting better than using my own prototypes
156 * because if I guess the prototype, I might be wrong, especially
157 * with regards to "const".
159 term_vi = (char *)tgetstr("vi", &data);
160 term_ve = (char *)tgetstr("ve", &data);
162 /* Okay, so I'm paranoid; I just don't like unsafe routines */
163 if (data > buf + bufSize)
164 fatal("tgetstr overflow, you must have a very sick termcap");
166 /* Trim off the unused portion of buffer */
167 buf = realloc(buf, data - buf);
171 * If that fails, use hardcoded vt220 codes.
172 * They don't seem to do anything bad on vt100's, so
173 * we'll try them just in case they work.
175 if (!term_vi || !term_ve) {
176 static char *vts[] = {
177 "vt100", "vt101", "vt102",
178 "vt200", "vt220", "vt300",
179 "vt320", "vt400", "vt420",
180 "screen", "xterm", NULL
184 for (i = 0; vts[i]; i++)
185 if (!strcmp(term, vts[i])) {
186 term_vi = "\033[?25l";
187 term_ve = "\033[?25h";
191 if (!term_vi || !term_ve)
192 term_vi = term_ve = NULL;
195 void OutputTermStr(char *str, int flush)
199 if (flush) fflush(stdout);
213 getmaxyx(stdscr, rows, cols);
214 sprintf(s, " NETRIS %s", version_string);
215 memset(&s[strlen(s)], ' ', 254 - strlen(s));
216 if (cols > 56 + strlen(version_string))
217 memcpy(&s[cols - 48],
218 "(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar \0", 49);
219 else memcpy(&s[cols], "\0", 1);
221 standend(); //normal text
224 void DrawBox(int x1, int y1, int x2, int y2)
228 for (y = y1 + 1; y < y2; y++) {
229 mvaddch(y, x1, Sets.ascii ? '|' : ACS_VLINE); //left
230 mvaddch(y, x2, Sets.ascii ? '|' : ACS_VLINE); //right
231 } //draw vertical lines
233 addch(Sets.ascii ? '+' : ACS_ULCORNER);
234 for (x = x1 + 1; x < x2; x++)
235 addch(Sets.ascii ? '-' : ACS_HLINE);
236 addch(Sets.ascii ? '+' : ACS_URCORNER);
237 move(y2, x1); //bottom
238 addch(Sets.ascii ? '+' : ACS_LLCORNER);
239 for (x = x1 + 1; x < x2; x++)
240 addch(Sets.ascii ? '-' : ACS_HLINE);
241 addch(Sets.ascii ? '+' : ACS_LRCORNER);
244 void DrawField(int scr)
245 { //draw field for player scr
246 if (!PlayerDisp[scr]) return;
247 DrawBox(boardXPos[scr] - 1, boardYPos[scr] - Players[scr].boardVisible,
248 boardXPos[scr] + boardSize[scr] * Players[scr].boardWidth, boardYPos[scr] + 1);
250 char s[boardSize[scr]*Players[scr].boardWidth+1];
252 if (Players[scr].host && Players[scr].host[0])
253 snprintf(s, sizeof(s), " %s <%s> ",
254 Players[scr].name, Players[scr].host);
255 else snprintf(s, sizeof(s), " %s ", Players[scr].name);
257 if (haveColor && Players[scr].team > 0 && Players[scr].team <= 7)
258 attrset(A_REVERSE | COLOR_PAIR(Players[scr].team + 1));
259 mvaddstr(1, boardXPos[scr], s);
260 if (haveColor) standend();
261 } //display playername/host
265 for (y = 0; y <= Players[scr].boardVisible; y++)
266 for (x = 0; x <= Players[scr].boardWidth; x++)
267 PlotBlock(scr, y, x, GetBlock(scr, y, x));
273 void InitFields(void)
274 { //calculate positions of all fields
281 getmaxyx(stdscr, y, x);
286 statusXPos = boardSize[me] * Players[me].boardWidth + 3;
288 ShowScore(me, Players[me].score);
292 if ((messageWidth = x - messageXPos - 2) > MSG_WIDTH) messageWidth = MSG_WIDTH;
293 if ((messageHeight = y - messageYPos - 1) > MSG_HEIGHT) messageHeight = MSG_HEIGHT;
294 if (messageHeight <= 0) {
296 messageHeight = y - 3;
297 messageXPos = statusXPos + 16;
299 } //messagebox doesn't fit below
300 DrawBox(messageXPos - 2, messageYPos - 1,
301 messageXPos + messageWidth + 1, messageYPos+messageHeight);
302 if (msgwin = subwin(stdscr, messageHeight, messageWidth,
303 messageYPos, messageXPos))
304 scrollok(msgwin, 1); //allow scrolling
305 wmove(msgwin, messageHeight - 2, 0);
306 for (scr = messageHeight - 2; scr >= 0; scr--) //display message history
307 DisplayMessage(message[scr]);
310 for (scr = 1; scr <= maxPlayer; scr++)
311 spaceavail -= Players[scr].boardWidth+2;
313 for (scr = 1; scr < MAX_SCREENS; scr++) if (scr != me) {
316 boardXPos[prevscr] + 2 + boardSize[prevscr] * Players[prevscr].boardWidth;
318 boardXPos[scr] += 15; //scorebar
319 if (messageYPos < 24)
320 boardXPos[scr] += messageWidth + 4; //messagebox
321 spaceavail -= boardXPos[scr] - 3;
322 } //stuff before second player
323 if (spaceavail >= 0) {
325 spaceavail -= Players[scr].boardWidth;
326 } //not enough space, half width
329 if (x < boardXPos[scr] + 1 + boardSize[scr] * Players[scr].boardWidth)
330 PlayerDisp[scr] = 0; //field doesn't fit on screen
335 for (scr = 1; scr <= maxPlayer; scr++)
339 void CleanupScreen(int scr)
343 void DisplayMessage(char *p)
349 memcpy(s, p, sizeof(s)-1);
352 while (psearch = strchr(p, '\\')) {
355 c = atoi(++psearch)+1;
356 if (haveColor) wattrset(msgwin, A_REVERSE | COLOR_PAIR(c));
358 } //search for color escapes (\)
360 if (haveColor) wstandend(msgwin);
361 waddch(msgwin, '\n');
364 void Message(char *fmt, ...)
365 { //print game/bot message
371 if (!messageHeight) return;
373 vsnprintf(s, sizeof(s), fmt, args);
375 p = message[MSG_HEIGHT - 1]; //save last pointer
376 for (i = MSG_HEIGHT - 1; i > 0; i--)
377 message[i] = message[i - 1]; //scroll history
381 wmove(msgwin, messageHeight - 1, 0);
387 void Messagetype(char c, int x, char *s)
388 { //show single typed character
390 mvwaddch(msgwin, messageHeight-1, (x+1) % (messageWidth-1), ' ');
393 if (c == 13 || c == 127) //enter/backspace
394 mvwaddch(msgwin, messageHeight - 1, (x+2) % (messageWidth-1),
395 x >= messageWidth-3 ? s[x - messageWidth + 3] : ' ');
397 mvwaddch(msgwin, messageHeight - 1, x % (messageWidth-1), c);
398 mvwaddch(msgwin, messageHeight - 1, (x+1) % (messageWidth-1), '_');
403 void PlotBlock1(int y, int x, unsigned char type)
404 { //display block on screen
406 if (type == BT_none) addstr(" ");
407 else if (type == BT_shadow) addstr("::");
411 if (haveColor) attrset(COLOR_PAIR(type & 15));
412 else attrset(A_REVERSE);
415 switch (Sets.drawstyle) {
417 switch (type & 192) {
418 case 64: //right neighbour
427 } //horizontal stickiness
428 break; //ascii horizontally grouped
430 switch (type & 240) {
432 addstr("||"); break; //middle
433 case 64: case 80: case 96:
434 addstr("[="); break; //left end
437 case 128: case 144: case 160:
438 addstr("=]"); break; //right end
441 case 192: case 208: case 224:
444 addstr("[]"); break; //top/bottom/mid
446 break; //ascii semi-grouped
448 switch (type & 240) {
449 case 16: addch(ACS_ULCORNER); addch(ACS_URCORNER); break;//top end
450 case 32: addch(ACS_LLCORNER); addch(ACS_LRCORNER); break;//bottom end
451 case 48: addch(ACS_VLINE); addch(ACS_VLINE); break; //vertical middle
452 case 64: addch(' '); addch(ACS_HLINE); break; //left end
453 case 80: addch(ACS_ULCORNER); addch(ACS_TTEE); break; //top left corner
454 case 96: addch(ACS_LLCORNER); addch(ACS_BTEE); break; //bottom left corner
455 case 112: addch(ACS_LTEE); addch(ACS_PLUS); break; //vertical+right
456 case 128: addch(ACS_HLINE); addch(' '); break; //right end
457 case 144: addch(ACS_TTEE); addch(ACS_URCORNER); break; //top right corner
458 case 160: addch(ACS_BTEE); addch(ACS_LRCORNER); break; //bottom right corner
459 case 176: addch(ACS_PLUS); addch(ACS_RTEE); break; //vertical+left
460 case 192: addch(ACS_HLINE); addch(ACS_HLINE); break; //horizontal middle
461 case 208: addch(ACS_TTEE); addch(ACS_TTEE); break; //horizontal+down
462 case 224: addch(ACS_BTEE); addch(ACS_BTEE); break; //horizontal+up
463 default: addstr("[]"); break;
465 break; //curses grouped
468 break; //ascii non-grouped
471 if (Sets.standout) standend();
473 } //display one brick
475 void PlotBlock1S(int y, int x, unsigned char type)
476 { //display block small
478 if (type == BT_none) addch(' ');
479 else if (type == BT_shadow) addch(':');
484 attrset(COLOR_PAIR(type & 15));
485 else attrset(A_REVERSE);
488 if ((type & 192) == 64)
490 else if ((type & 192) == 128)
495 if (Sets.standout) standend();
497 } //display one brick
499 void PlotBlock(int scr, int y, int x, unsigned char type)
501 if (y >= 0 && y < Players[scr].boardVisible
502 && x >= 0 && x < Players[scr].boardWidth) {
503 if (boardSize[scr] > 1)
504 PlotBlock1(boardYPos[scr] - y, boardXPos[scr] + 2*x, type);
506 PlotBlock1S(boardYPos[scr] - y, boardXPos[scr] + x, type);
509 void PlotBlockXY(int y, int x, unsigned char type)
510 { //Draw block at specified position on screen (not on field)
511 PlotBlock1(20 - y, 2 * x, type);
514 void ShowScore(int scr, struct _Score score)
518 mvaddstr(13, statusXPos, MSG_NEXT " ");
519 mvaddstr(14, statusXPos + 5, " ");
520 ShapeIterate(Players[scr].nextShape, scr,
521 8, statusXPos/2 + (Players[scr].nextShape/4 == 5 ? 3 : 4),
522 GlanceFunc); //draw; stick one more to the left
523 mvprintw(3, statusXPos, MSG_LEVEL, score.level);
524 mvprintw(5, statusXPos, MSG_SCORE, score.score);
525 mvprintw(6, statusXPos, MSG_LINES, score.lines);
526 timer = CurTimeval() / 1e6;
528 mvprintw(9, statusXPos, MSG_PPM, score.pieces * 60 / timer);
529 if (score.lines > 0) {
530 mvprintw(7, statusXPos, MSG_YIELD, 100 * score.adds / score.lines);
531 mvprintw(10, statusXPos, MSG_APM, score.adds * 60 / timer);
536 for (i = 7; i <= 10; i++)
537 mvaddstr(i, statusXPos, " ");
538 } //too early to display stats, remove old..
541 void FieldMessage(int playa, char *message)
542 { //put a message over playa's field
543 if (!PlayerDisp[playa]) return;
545 char s[MAX_BOARD_WIDTH+1];
546 memset(s, ' ', MAX_BOARD_WIDTH);
547 memcpy(&s[(boardSize[playa] * Players[playa].boardWidth / 2) - (strlen(message) / 2)],
548 message, strlen(message));
549 s[boardSize[playa] * Players[playa].boardWidth] = 0;
555 mvprintw(boardYPos[playa] - Players[playa].boardVisible / 2,
556 boardXPos[playa], "%s", s);
561 y = Players[playa].boardVisible / 2;
562 for (x = 0; x <= Players[playa].boardWidth; x++)
563 PlotBlock(playa, y, x, GetBlock(playa, y, x));
567 void ShowPause(int playa)
568 { //put paused over player's field
569 if (Players[playa].alive > 0)
570 if (Players[playa].flags & SCF_paused)
571 if (Game.started > 1)
572 FieldMessage(playa, boardSize[playa] > 1 ? "P A U S E D" : "PAUSED");
575 boardSize[playa] > 1 ? "N O T R E A D Y" : "NOT READY");
577 if (Game.started > 1)
578 FieldMessage(playa, NULL);
580 FieldMessage(playa, boardSize[playa] > 1 ? "R E A D Y" : "READY");
581 else if (!Players[playa].alive)
583 boardSize[playa] > 1 ? "G A M E O V E R" : "GAME OVER");
585 FieldMessage(playa, boardSize[playa] > 1 ? "E M P T Y" : "EMPTY");
591 mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6);
594 void ScheduleFullRedraw(void)
599 void CatchWinCh(int sig)
600 { //handle window resize
601 endwin(); //exit curses
602 refresh(); //and reinit display (with different sizes)
603 InitFields(); //manually redraw everything
607 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
609 if (MyRead(gen->fd, &event->u.key, 1))