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.
19 * $Id: curses.c,v 1.33 1999/05/16 06:56:25 mhw Exp $
23 #include <sys/types.h>
29 #ifdef NCURSES_VERSION
33 ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type);
35 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
36 static EventGenRec keyGen =
37 { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
39 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
40 static int statusYPos, statusXPos;
41 static int messageYPos, messageXPos, messageHeight, messageWidth;
44 static char *term_vi; /* String to make cursor invisible */
45 static char *term_ve; /* String to make cursor visible */
47 ExtFunc void InitScreens(void)
54 * Block signals while initializing curses. Otherwise a badly timed
55 * Ctrl-C during initialization might leave the terminal in a bad state.
57 BlockSignals(&oldMask, SIGINT, 0);
58 initscr(); //start curses
69 haveColor = Game.color && has_colors();
75 { BT_white, COLOR_WHITE },
76 { BT_blue, COLOR_BLUE },
77 { BT_magenta, COLOR_MAGENTA },
78 { BT_cyan, COLOR_CYAN },
79 { BT_yellow, COLOR_YELLOW },
80 { BT_green, COLOR_GREEN },
81 { BT_red, COLOR_RED },
87 if (can_change_color()) {
88 init_color (COLOR_YELLOW, 1000, 1000, 0);
89 } //I've never worked on a color-changable terminal, so no idea..
90 for (i = 0; myColorTable[i].type != BT_none; ++i)
91 init_pair(myColorTable[i].type, COLOR_BLACK,
92 myColorTable[i].color);
98 AtExit(CleanupScreens); //restore everything when done
99 RestoreSignals(NULL, &oldMask);
101 cbreak(); //no line buffering
103 // keypad(stdscr, TRUE); //get arrow/functionkeys 'n stuff
104 OutputTermStr(term_vi, 0);
105 AddEventGen(&keyGen); //key handler
106 standend(); //normal text
109 ExtFunc void CleanupScreens(void)
111 RemoveEventGen(&keyGen);
112 endwin(); //end curses
113 OutputTermStr(term_ve, 1);
116 ExtFunc void GetTermcapInfo(void)
118 char *term, *buf, *data;
121 if (!(term = getenv("TERM")))
123 if (tgetent(scratch, term) == 1) {
125 * Make the buffer HUGE, since tgetstr is unsafe.
126 * Allocate it on the heap too.
128 data = buf = malloc(bufSize);
131 * There is no standard include file for tgetstr, no prototype
132 * definitions. I like casting better than using my own prototypes
133 * because if I guess the prototype, I might be wrong, especially
134 * with regards to "const".
136 term_vi = (char *)tgetstr("vi", &data);
137 term_ve = (char *)tgetstr("ve", &data);
139 /* Okay, so I'm paranoid; I just don't like unsafe routines */
140 if (data > buf + bufSize)
141 fatal("tgetstr overflow, you must have a very sick termcap");
143 /* Trim off the unused portion of buffer */
144 buf = realloc(buf, data - buf);
148 * If that fails, use hardcoded vt220 codes.
149 * They don't seem to do anything bad on vt100's, so
150 * we'll try them just in case they work.
152 if (!term_vi || !term_ve) {
153 static char *vts[] = {
154 "vt100", "vt101", "vt102",
155 "vt200", "vt220", "vt300",
156 "vt320", "vt400", "vt420",
157 "screen", "xterm", NULL };
160 for (i = 0; vts[i]; i++)
161 if (!strcmp(term, vts[i]))
163 term_vi = "\033[?25l";
164 term_ve = "\033[?25h";
168 if (!term_vi || !term_ve)
169 term_vi = term_ve = NULL;
172 ExtFunc void OutputTermStr(char *str, int flush)
176 if (flush) fflush(stdout);
180 ExtFunc void DrawTitle(void)
190 getmaxyx(stdscr, rows, cols);
191 sprintf(s, " NETRIS %s", version_string);
192 memset(&s[strlen(s)], ' ', 254 - strlen(s));
193 if (cols > 56 + strlen(version_string))
194 memcpy(&s[cols - 48],
195 "(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar \0", 49);
196 else memcpy(&s[cols], "\0", 1);
198 standend(); //normal text
201 ExtFunc void DrawBox(int x1, int y1, int x2, int y2)
205 for (y = y1 + 1; y < y2; y++) {
206 mvaddch(y, x1, Game.ascii ? '|' : ACS_VLINE); //left
207 mvaddch(y, x2, Game.ascii ? '|' : ACS_VLINE); //right
210 addch(Game.ascii ? '+' : ACS_ULCORNER);
211 for (x = x1 + 1; x < x2; x++)
212 addch(Game.ascii ? '-' : ACS_HLINE);
213 addch(Game.ascii ? '+' : ACS_URCORNER);
214 move(y2, x1); //bottom
215 addch(Game.ascii ? '+' : ACS_LLCORNER);
216 for (x = x1 + 1; x < x2; x++)
217 addch(Game.ascii ? '-' : ACS_HLINE);
218 addch(Game.ascii ? '+' : ACS_LRCORNER);
221 ExtFunc void DrawField(int scr)
222 { //draw field for player scr
223 if (!Players[scr].spy) return;
224 DrawBox(boardXPos[scr] - 1, boardYPos[scr] - Players[scr].boardVisible,
225 boardXPos[scr] + 2 * Players[scr].boardWidth, boardYPos[scr] + 1);
227 char s[2*Players[scr].boardWidth];
229 memset(s, ' ', sizeof(s));
230 if (Players[scr].host && Players[scr].host[0])
231 snprintf(s, sizeof(s), "%s <%s>",
232 Players[scr].name, Players[scr].host);
233 else snprintf(s, sizeof(s), "%s", Players[scr].name);
235 s[sizeof(s) - 7*((Players[scr].flags & SCF_usingRobot) != 0)
236 - 5*((Players[scr].flags & SCF_fairRobot) != 0)] = 0;
237 mvaddstr(1, boardXPos[scr], s);
239 if (Players[scr].flags & SCF_usingRobot) {
240 addstr((Players[scr].flags & SCF_fairRobot)
241 ? "(fair robot)" : "(robot)");
242 } //add robot indicator
243 } //display playername/host
247 for (y = 0; y <= Players[scr].boardVisible; y++)
248 for (x = 0; x <= Players[scr].boardWidth; x++)
249 PlotBlock(scr, y, x, GetBlock(scr, y, x));
255 ExtFunc void InitFields(void)
256 { //calculate positions of all fields
260 getmaxyx(stdscr, y, x);
263 statusXPos = 2 * Players[me].boardWidth + 3;
267 messageWidth = x - messageXPos - 2;
268 if ((messageHeight = y - messageYPos - 1) < 0) messageHeight = 0;
269 else DrawBox(messageXPos - 2, messageYPos - 1,
270 messageXPos + messageWidth + 1, messageYPos + messageHeight);
272 for (scr = 1; scr < MAX_SCREENS; scr++) if (scr != me) {
274 boardXPos[prevscr] + 2 * Players[prevscr].boardWidth + 3;
276 boardXPos[scr] += 14; //scorebar
278 if (x < boardXPos[scr] + 2 * Players[scr].boardWidth + 1)
279 Players[scr].spy = 0; //field doesn't fit on screen
282 for (scr = 1; scr <= Game.maxplayers; scr++)
286 ExtFunc void CleanupScreen(int scr)
290 ExtFunc void Messagef(char *fmt, ...)
291 { //print game/bot message
298 if (!messageHeight) return;
300 move(messageYPos + line, messageXPos);
301 // vwprintw(stdscr, fmt, args); //doesn't seem to be a vprintw
302 vsprintf(s, fmt, args);
304 while (psearch = strchr(s, '\\')) {
308 attrset(A_REVERSE | COLOR_PAIR(atoi(psearch + 1) + 1));
310 } //search for color escapes (\)
312 if (messageHeight > 1) {
313 char s[messageWidth + 1];
314 line = (line + 1) % messageHeight;
315 memset(s, ' ', messageWidth);
317 mvaddstr(messageYPos + line, messageXPos, s);
319 if (haveColor) standend();
323 ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type)
325 int colorIndex = abs(type);
328 if (type == BT_none) addstr(" ");
329 else if (type == BT_shadow) addstr("::");
333 if (haveColor) attrset(COLOR_PAIR(colorIndex));
334 else attrset(A_REVERSE);
337 addstr(type ? "[]" : "$$");
339 if (Game.standout) standend();
341 } //display one brick
343 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
345 if (y >= 0 && y < Players[scr].boardVisible &&
346 x >= 0 && x < Players[scr].boardWidth)
347 PlotBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
350 ExtFunc void PlotBlockS1(int scr, int y, int x, BlockType type)
351 { //DOESN"T WORK YET...
353 if (type == BT_none) addstr(" ");
355 addstr(type ? "O" : "$");
357 } //display one brick
359 ExtFunc void PlotBlockS(int scr, int y, int x, BlockType type)
361 if (y >= 0 && y < Players[scr].boardVisible &&
362 x >= 0 && x < Players[scr].boardWidth)
363 PlotBlockS1(scr, boardYPos[scr] - y, boardXPos[scr] + x, type);
366 ExtFunc void PlotUnderline(int scr, int x, int flag)
367 { //display piece of bottom fieldgrid
368 move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
370 addstr(flag ? "==" : "--");
372 addch(flag ? ACS_BTEE : ACS_HLINE);
373 addch(flag ? ACS_BTEE : ACS_HLINE);
377 ExtFunc void ShowScore(int scr, struct _Score score)
381 mvaddstr(13, statusXPos, "next ");
382 mvaddstr(14, statusXPos + 5, " ");
383 ShapeIterate(Players[scr].nextShape, scr,
384 ShapeToNetNum(Players[scr].nextShape) == 15 ? 6 : 7,
385 statusXPos / 2 + 4, 1, GlanceFunc, NULL);
386 mvprintw(3, statusXPos, "level %5d", score.level);
387 mvprintw(5, statusXPos, "score%8d", score.score);
388 mvprintw(6, statusXPos, "lines%8d", score.lines);
389 timer = CurTimeval() / 1e6;
391 mvprintw(9, statusXPos, "ppm %9.1f", score.drops * 60 / timer);
392 if (score.lines > 0) {
393 mvprintw(7, statusXPos,
394 "yield %3d%%", 100 * score.adds / score.lines);
395 mvprintw(10, statusXPos, "apm %9.1f", score.adds * 60 / timer);
400 for (i = 7; i <= 10; i++)
401 mvaddstr(i, statusXPos, " ");
402 } //too early to display stats, remove old..
405 ExtFunc void FieldMessage(int playa, char *message)
406 { //put a message over playa's field
407 if (!Players[playa].spy) return;
409 char s[MAX_BOARD_WIDTH+1];
410 memset(s, ' ', MAX_BOARD_WIDTH);
411 memcpy(&s[Players[playa].boardWidth - strlen(message) / 2],
412 message, strlen(message));
413 s[Players[playa].boardWidth * 2] = 0;
419 mvprintw(boardYPos[playa] - Players[playa].boardVisible / 2,
420 boardXPos[playa], "%s", s);
425 y = Players[playa].boardVisible / 2;
426 for (x = 0; x <= Players[playa].boardWidth; x++)
427 PlotBlock(playa, y, x, GetBlock(playa, y, x));
431 ExtFunc void ShowPause(int playa)
432 { //put paused over player's field
433 if (Players[playa].alive)
434 if (Players[playa].flags & SCF_paused)
435 FieldMessage(playa, Game.started > 1
436 ? "P A U S E D" : "N O T R E A D Y");
437 else FieldMessage(playa, Game.started > 1 ? NULL : "R E A D Y");
438 else FieldMessage(playa, playa > maxPlayer
439 ? "E M P T Y" : "G A M E O V E R");
443 ExtFunc void ShowTime(void)
445 mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6);
446 // move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1);
450 ExtFunc void ScheduleFullRedraw(void)
453 } //ScheduleFullRedraw
455 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
457 if (MyRead(gen->fd, &event->u.key, 1))