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
40 { BT_white, COLOR_WHITE },
41 { BT_blue, COLOR_BLUE },
42 { BT_magenta, COLOR_MAGENTA },
43 { BT_cyan, COLOR_CYAN },
44 { BT_yellow, COLOR_YELLOW },
45 { BT_green, COLOR_GREEN },
46 { BT_red, COLOR_RED },
51 ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type);
52 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
54 static EventGenRec keyGen =
55 { NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
57 static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
58 static int statusYPos, statusXPos;
61 static char *term_vi; /* String to make cursor invisible */
62 static char *term_ve; /* String to make cursor visible */
64 ExtFunc void InitScreens(void)
71 * Block signals while initializing curses. Otherwise a badly timed
72 * Ctrl-C during initialization might leave the terminal in a bad state.
74 BlockSignals(&oldMask, SIGINT, 0);
86 haveColor = Game.color && has_colors();
91 for (i = 0; myColorTable[i].type != BT_none; ++i)
92 init_pair(myColorTable[i].type, COLOR_BLACK,
93 myColorTable[i].color);
99 AtExit(CleanupScreens);
100 RestoreSignals(NULL, &oldMask);
104 OutputTermStr(term_vi, 0);
105 AddEventGen(&keyGen);
109 addstr(version_string);
113 getmaxyx(stdscr, rows, cols);
115 addstr("(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar");
116 // addstr(" \"netris -h\" for more info");
122 ExtFunc void CleanupScreens(void)
124 RemoveEventGen(&keyGen);
126 OutputTermStr(term_ve, 1);
129 ExtFunc void GetTermcapInfo(void)
131 char *term, *buf, *data;
134 if (!(term = getenv("TERM")))
136 if (tgetent(scratch, term) == 1) {
138 * Make the buffer HUGE, since tgetstr is unsafe.
139 * Allocate it on the heap too.
141 data = buf = malloc(bufSize);
144 * There is no standard include file for tgetstr, no prototype
145 * definitions. I like casting better than using my own prototypes
146 * because if I guess the prototype, I might be wrong, especially
147 * with regards to "const".
149 term_vi = (char *)tgetstr("vi", &data);
150 term_ve = (char *)tgetstr("ve", &data);
152 /* Okay, so I'm paranoid; I just don't like unsafe routines */
153 if (data > buf + bufSize)
154 fatal("tgetstr overflow, you must have a very sick termcap");
156 /* Trim off the unused portion of buffer */
157 buf = realloc(buf, data - buf);
161 * If that fails, use hardcoded vt220 codes.
162 * They don't seem to do anything bad on vt100's, so
163 * we'll try them just in case they work.
165 if (!term_vi || !term_ve) {
166 static char *vts[] = {
167 "vt100", "vt101", "vt102",
168 "vt200", "vt220", "vt300",
169 "vt320", "vt400", "vt420",
170 "screen", "xterm", NULL };
173 for (i = 0; vts[i]; i++)
174 if (!strcmp(term, vts[i]))
176 term_vi = "\033[?25l";
177 term_ve = "\033[?25h";
181 if (!term_vi || !term_ve)
182 term_vi = term_ve = NULL;
185 ExtFunc void OutputTermStr(char *str, int flush)
194 ExtFunc void DrawField(int scr)
199 for (y = Players[scr].boardVisible - 1; y >= 0; --y) {
200 mvaddch(boardYPos[scr] - y, boardXPos[scr] - 1,
201 Game.ascii ? '|' : ACS_VLINE); //left
202 mvaddch(boardYPos[scr] - y,
203 boardXPos[scr] + 2 * Players[scr].boardWidth,
204 Game.ascii ? '|' : ACS_VLINE); //right
206 move(2, boardXPos[scr] - 1); //top
207 addch(Game.ascii ? '+' : ACS_ULCORNER);
208 for (x = Players[scr].boardWidth * 2 - 1; x >= 0; --x)
209 addch(Game.ascii ? '-' : ACS_HLINE);
210 addch(Game.ascii ? '+' : ACS_URCORNER);
211 move(boardYPos[scr] + 1, boardXPos[scr] - 1); //bottom
212 addch(Game.ascii ? '+' : ACS_LLCORNER);
213 for (x = Players[scr].boardWidth * 2 - 1; x >= 0; --x)
214 addch(Game.ascii ? '-' : ACS_HLINE);
215 addch(Game.ascii ? '+' : ACS_LRCORNER);
221 sprintf(userstr, "%s <%s>", Players[scr].name, Players[scr].host);
222 userstr[20 - 7*((Players[scr].flags & SCF_usingRobot) != 0)
223 - 5*((Players[scr].flags & SCF_fairRobot) != 0)] = 0;
224 mvaddstr(1, boardXPos[scr], userstr);
226 if (Players[scr].flags & SCF_usingRobot) {
227 addstr((Players[scr].flags & SCF_fairRobot)
228 ? "(fair robot)" : "(robot)");
230 } //display playername/host
233 ExtFunc void InitFields()
240 for (scr = 1; scr <= totalPlayers + 1; scr++) if (scr != me) {
242 boardXPos[prevscr] + 2 * Players[prevscr].boardWidth + 3;
244 boardXPos[scr] += 24; //scorebar
248 statusXPos = 2 * Players[me].boardWidth + 3;
251 ExtFunc void CleanupScreen(int scr)
255 ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type)
257 int colorIndex = abs(type);
269 attrset(COLOR_PAIR(colorIndex));
275 addstr(type ? "[]" : "$$");
280 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
282 if (y >= 0 && y < Players[scr].boardVisible &&
283 x >= 0 && x < Players[scr].boardWidth)
284 PlotBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
287 ExtFunc void PlotUnderline(int scr, int x, int flag)
289 move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
291 addstr(flag ? "==" : "--");
293 addch(flag ? ACS_BTEE : ACS_HLINE);
294 addch(flag ? ACS_BTEE : ACS_HLINE);
298 ExtFunc void ShowDisplayInfo(void)
300 move(statusYPos - 9, statusXPos);
301 printw("Seed: %010d", Game.seed);
302 // move(statusYPos - 8, statusXPos);
303 // printw("Speed: %dms ", speed / 1000);
305 move(statusYPos - 6, statusXPos);
307 addstr("Controlled by a fair robot");
309 addstr("Controlled by a robot");
312 if (Players[1].flags & SCF_usingRobot) {
313 move(statusYPos - 5, statusXPos);
314 if (Players[1].flags & SCF_fairRobot)
315 addstr("The opponent is a fair robot");
317 addstr("The opponent is a robot");
322 ExtFunc void ShowScore(int scr, struct _Score score)
326 move(6, statusXPos); addstr("Next: ");
327 move(7, statusXPos + 7); addstr(" ");
328 ShapeIterate(Players[scr].nextShape, scr,
329 ShapeToNetNum(Players[scr].nextShape) == 15 ? 13 : 14,
330 statusXPos / 2 + 5, 1, GlanceFunc, NULL);
331 move(statusYPos - 21 + 1, statusXPos);
332 printw("Score:%6d level: %2d", score.score, score.level);
333 move(statusYPos - 20 + 1, statusXPos);
334 timer = CurTimeval() / 1e6;
335 printw("Lines:%6d", score.lines);
337 printw(" ppm:%5.1f", score.drops * 60 / timer);
338 move(statusYPos - 18, statusXPos);
340 printw("yield: %3d%%", 100 * score.adds / score.lines);
342 printw(" apm:%5.1f", score.adds * 60 / timer);
346 ExtFunc void ShowPause(int pausedByMe, int pausedByThem)
348 move(statusYPos - 3, statusXPos);
350 addstr("Game paused by opponent");
353 move(statusYPos - 2, statusXPos);
355 addstr("Game paused by you");
360 ExtFunc void Message(char *s)
364 // move(statusYPos - 20 + line, statusXPos);
365 move(statusYPos + 2 + line, 1);
366 addstr(s); /* XXX Should truncate long lines */
368 line = (line + 1) % 10;
369 // move(statusYPos - 20 + line, statusXPos);
370 move(statusYPos + 2 + line, 1);
374 ExtFunc void ShowTime(void)
376 move(statusYPos, statusXPos);
377 printw("Timer: %.0f ", CurTimeval() / 1e6);
378 move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1);
382 ExtFunc void ScheduleFullRedraw(void)
387 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
389 if (MyRead(gen->fd, &event->u.key, 1))