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
245 // draw blocks (which is usually just clear field)
250 ExtFunc void InitFields(void)
251 { //calculate positions of all fields
255 getmaxyx(stdscr, y, x);
258 statusXPos = 2 * Players[me].boardWidth + 3;
262 messageWidth = x - messageXPos - 2;
263 if ((messageHeight = y - messageYPos - 1) < 0) messageHeight = 0;
264 else DrawBox(messageXPos - 2, messageYPos - 1,
265 messageXPos + messageWidth + 1, messageYPos + messageHeight);
267 for (scr = 1; scr < MAX_SCREENS; scr++) if (scr != me) {
269 boardXPos[prevscr] + 2 * Players[prevscr].boardWidth + 3;
271 boardXPos[scr] += 14; //scorebar
273 if (x < boardXPos[scr] + 2 * Players[scr].boardWidth + 1)
274 Players[scr].spy = 0; //field doesn't fit on screen
277 for (scr = 1; scr <= Game.maxplayers; scr++)
281 ExtFunc void CleanupScreen(int scr)
285 ExtFunc void Messagef(char *fmt, ...)
286 { //print game/bot message
293 if (!messageHeight) return;
295 move(messageYPos + line, messageXPos);
296 // vwprintw(stdscr, fmt, args); //doesn't seem to be a vprintw
297 vsprintf(s, fmt, args);
299 while (psearch = strchr(s, '\\')) {
303 attrset(A_REVERSE | COLOR_PAIR(atoi(psearch + 1) + 1));
305 } //search for color escapes (\)
307 if (messageHeight > 1) {
308 char s[messageWidth + 1];
309 line = (line + 1) % messageHeight;
310 memset(s, ' ', messageWidth);
312 mvaddstr(messageYPos + line, messageXPos, s);
314 if (haveColor) standend();
318 ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type)
320 int colorIndex = abs(type);
323 if (type == BT_none) addstr(" ");
324 else if (type == BT_shadow) addstr("::");
328 if (haveColor) attrset(COLOR_PAIR(colorIndex));
329 else attrset(A_REVERSE);
332 addstr(type ? "[]" : "$$");
334 if (Game.standout) standend();
336 } //display one brick
338 ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
340 if (y >= 0 && y < Players[scr].boardVisible &&
341 x >= 0 && x < Players[scr].boardWidth)
342 PlotBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
345 ExtFunc void PlotShadowBlock1(int scr, int y, int x, BlockType type)
348 if (type == BT_none) addstr(" ");
351 ExtFunc void PlotShadowBlock(int scr, int y, int x, BlockType type)
353 if (y >= 0 && y < Players[scr].boardVisible &&
354 x >= 0 && x < Players[scr].boardWidth)
355 PlotShadowBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
358 ExtFunc void PlotBlockS1(int scr, int y, int x, BlockType type)
359 { //DOESN"T WORK YET...
361 if (type == BT_none) addstr(" ");
363 addstr(type ? "O" : "$");
365 } //display one brick
367 ExtFunc void PlotBlockS(int scr, int y, int x, BlockType type)
369 if (y >= 0 && y < Players[scr].boardVisible &&
370 x >= 0 && x < Players[scr].boardWidth)
371 PlotBlockS1(scr, boardYPos[scr] - y, boardXPos[scr] + x, type);
374 ExtFunc void PlotUnderline(int scr, int x, int flag)
375 { //display piece of bottom fieldgrid
376 move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
378 addstr(flag ? "==" : "--");
380 addch(flag ? ACS_BTEE : ACS_HLINE);
381 addch(flag ? ACS_BTEE : ACS_HLINE);
385 ExtFunc void ShowScore(int scr, struct _Score score)
389 mvaddstr(13, statusXPos, "next ");
390 mvaddstr(14, statusXPos + 5, " ");
391 ShapeIterate(Players[scr].nextShape, scr,
392 ShapeToNetNum(Players[scr].nextShape) == 15 ? 6 : 7,
393 statusXPos / 2 + 4, 1, GlanceFunc, NULL);
394 mvprintw(3, statusXPos, "level %5d", score.level);
395 mvprintw(5, statusXPos, "score%8d", score.score);
396 mvprintw(6, statusXPos, "lines%8d", score.lines);
397 timer = CurTimeval() / 1e6;
399 mvprintw(9, statusXPos, "ppm %9.1f", score.drops * 60 / timer);
400 if (score.lines > 0) {
401 mvprintw(7, statusXPos,
402 "yield %3d%%", 100 * score.adds / score.lines);
403 mvprintw(10, statusXPos, "apm %9.1f", score.adds * 60 / timer);
408 ExtFunc void FieldMessage(int playa, char *message)
409 { //put a message over playa's field
410 if (!Players[playa].spy) return;
412 char s[MAX_BOARD_WIDTH+1];
413 memset(s, ' ', MAX_BOARD_WIDTH);
414 memcpy(&s[Players[playa].boardWidth - strlen(message) / 2],
415 message, strlen(message));
416 s[Players[playa].boardWidth * 2] = 0;
422 mvprintw(boardYPos[playa] - Players[playa].boardVisible / 2,
423 boardXPos[playa], "%s", s);
428 y = Players[playa].boardVisible / 2;
429 for (x = 0; x <= Players[playa].boardWidth; x++)
430 PlotBlock(playa, y, x, GetBlock(playa, y, x));
434 ExtFunc void ShowPause(int playa)
435 { //put paused over player's field
436 if (Players[playa].alive)
437 if (Players[playa].flags & SCF_paused)
438 FieldMessage(playa, Game.started > 1
439 ? "P A U S E D" : "N O T R E A D Y");
440 else FieldMessage(playa, Game.started > 1 ? NULL : "R E A D Y");
441 else FieldMessage(playa, playa > maxPlayer
442 ? "E M P T Y" : "G A M E O V E R");
446 ExtFunc void ShowTime(void)
448 mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6);
449 // move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1);
453 ExtFunc void ScheduleFullRedraw(void)
456 } //ScheduleFullRedraw
458 static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
460 if (MyRead(gen->fd, &event->u.key, 1))