/*
* Netris -- A free networked version of T*tris
- * Copyright (C) 1994,1995,1996 Mark H. Weaver <mhw@netris.org>
+ * Copyright (C) 1994-1996,1999 Mark H. Weaver <mhw@netris.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
- * $Id: curses.c,v 1.32 1996/02/09 08:47:25 mhw Exp $
+ * $Id: curses.c,v 1.33 1999/05/16 06:56:25 mhw Exp $
*/
#include "netris.h"
#include <string.h>
#include <stdlib.h>
-static void PlotBlock1(int scr, int y, int x, BlockType type);
-static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
+#ifdef NCURSES_VERSION
+# define HAVE_NCURSES
+#endif
+
+ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type);
+static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
static EventGenRec keyGen =
{ NULL, 0, FT_read, STDIN_FILENO, KeyGenFunc, EM_key };
static int boardYPos[MAX_SCREENS], boardXPos[MAX_SCREENS];
static int statusYPos, statusXPos;
+static int messageYPos, messageXPos, messageHeight, messageWidth;
+static int haveColor;
static char *term_vi; /* String to make cursor invisible */
static char *term_ve; /* String to make cursor visible */
GetTermcapInfo();
/*
- * Do this atomically. Otherwise a badly timed Ctrl-C during
- * initialization will leave your terminal in a bad state.
+ * Block signals while initializing curses. Otherwise a badly timed
+ * Ctrl-C during initialization might leave the terminal in a bad state.
*/
BlockSignals(&oldMask, SIGINT, 0);
- initscr();
- AtExit(CleanupScreens);
+ initscr(); //start curses
+
+#ifdef CURSES_HACK
+ {
+ extern char *CS;
+
+ CS = 0;
+ }
+#endif
+
+#ifdef HAVE_NCURSES
+ haveColor = Game.color && has_colors();
+ if (haveColor) {
+ static struct {
+ BlockType type;
+ short color;
+ } myColorTable[] = {
+ { BT_white, COLOR_WHITE },
+ { BT_blue, COLOR_BLUE },
+ { BT_magenta, COLOR_MAGENTA },
+ { BT_cyan, COLOR_CYAN },
+ { BT_yellow, COLOR_YELLOW },
+ { BT_green, COLOR_GREEN },
+ { BT_red, COLOR_RED },
+ { BT_none, 0 }
+ }; //myColorTable
+ int i = 0;
+
+ start_color();
+ if (can_change_color()) {
+ init_color (COLOR_YELLOW, 1000, 1000, 0);
+ } //I've never worked on a color-changable terminal, so no idea..
+ for (i = 0; myColorTable[i].type != BT_none; ++i)
+ init_pair(myColorTable[i].type, COLOR_BLACK,
+ myColorTable[i].color);
+ } //haveColor
+#else
+ haveColor = 0;
+#endif
+
+ AtExit(CleanupScreens); //restore everything when done
RestoreSignals(NULL, &oldMask);
- cbreak();
+ cbreak(); //no line buffering
noecho();
+// keypad(stdscr, TRUE); //get arrow/functionkeys 'n stuff
OutputTermStr(term_vi, 0);
- AddEventGen(&keyGen);
-
- move(0, 0);
- addstr("Netris ");
- addstr(version_string);
- addstr(" (C) 1994,1995,1996 Mark H. Weaver "
- "\"netris -h\" for more info");
- statusYPos = 22;
- statusXPos = 0;
-}
+ AddEventGen(&keyGen); //key handler
+ standend(); //normal text
+} //InitScreens
ExtFunc void CleanupScreens(void)
{
RemoveEventGen(&keyGen);
- endwin();
+ endwin(); //end curses
OutputTermStr(term_ve, 1);
-}
+} //CleanupScreens
ExtFunc void GetTermcapInfo(void)
{
}
if (!term_vi || !term_ve)
term_vi = term_ve = NULL;
-}
+} //GetTermcapInfo
ExtFunc void OutputTermStr(char *str, int flush)
{
if (str) {
fputs(str, stdout);
- if (flush)
- fflush(stdout);
+ if (flush) fflush(stdout);
}
-}
+} //OutputTermStr
-ExtFunc void InitScreen(int scr)
+ExtFunc void DrawTitle(void)
{
+ int rows, cols;
+ char s[255];
+
+#ifdef HAVE_NCURSES
+ attrset(A_REVERSE);
+#else
+ standout();
+#endif
+ getmaxyx(stdscr, rows, cols);
+ sprintf(s, " NETRIS %s", version_string);
+ memset(&s[strlen(s)], ' ', 254 - strlen(s));
+ if (cols > 56 + strlen(version_string))
+ memcpy(&s[cols - 48],
+ "(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar \0", 49);
+ else memcpy(&s[cols], "\0", 1);
+ mvaddstr(0, 0, s);
+ standend(); //normal text
+} //DrawTitle
+
+ExtFunc void DrawBox(int x1, int y1, int x2, int y2)
+{ //draw grid
int y, x;
- if (scr == 0)
- boardXPos[scr] = 1;
- else
- boardXPos[scr] = boardXPos[scr - 1] +
- 2 * boardWidth[scr - 1] + 3;
- boardYPos[scr] = 22;
- if (statusXPos < boardXPos[scr] + 2 * boardWidth[scr] + 3)
- statusXPos = boardXPos[scr] + 2 * boardWidth[scr] + 3;
- for (y = boardVisible[scr] - 1; y >= 0; --y) {
- move(boardYPos[scr] - y, boardXPos[scr] - 1);
- addch('|');
- move(boardYPos[scr] - y, boardXPos[scr] + 2 * boardWidth[scr]);
- addch('|');
+ for (y = y1 + 1; y < y2; y++) {
+ mvaddch(y, x1, Game.ascii ? '|' : ACS_VLINE); //left
+ mvaddch(y, x2, Game.ascii ? '|' : ACS_VLINE); //right
}
- for (y = boardVisible[scr]; y >= -1; y -= boardVisible[scr] + 1) {
- move(boardYPos[scr] - y, boardXPos[scr] - 1);
- addch('+');
- for (x = boardWidth[scr] - 1; x >= 0; --x)
- addstr("--");
- addch('+');
+ move(y1, x1); //top
+ addch(Game.ascii ? '+' : ACS_ULCORNER);
+ for (x = x1 + 1; x < x2; x++)
+ addch(Game.ascii ? '-' : ACS_HLINE);
+ addch(Game.ascii ? '+' : ACS_URCORNER);
+ move(y2, x1); //bottom
+ addch(Game.ascii ? '+' : ACS_LLCORNER);
+ for (x = x1 + 1; x < x2; x++)
+ addch(Game.ascii ? '-' : ACS_HLINE);
+ addch(Game.ascii ? '+' : ACS_LRCORNER);
+} //DrawBox
+
+ExtFunc void DrawField(int scr)
+{ //draw field for player scr
+ if (!Players[scr].spy) return;
+ DrawBox(boardXPos[scr] - 1, boardYPos[scr] - Players[scr].boardVisible,
+ boardXPos[scr] + 2 * Players[scr].boardWidth, boardYPos[scr] + 1);
+ {
+ char s[2*Players[scr].boardWidth];
+
+ memset(s, ' ', sizeof(s));
+ if (Players[scr].host && Players[scr].host[0])
+ snprintf(s, sizeof(s), "%s <%s>",
+ Players[scr].name, Players[scr].host);
+ else snprintf(s, sizeof(s), "%s", Players[scr].name);
+ s[strlen(s)] = ' ';
+ s[sizeof(s) - 7*((Players[scr].flags & SCF_usingRobot) != 0)
+ - 5*((Players[scr].flags & SCF_fairRobot) != 0)] = 0;
+ mvaddstr(1, boardXPos[scr], s);
+
+ if (Players[scr].flags & SCF_usingRobot) {
+ addstr((Players[scr].flags & SCF_fairRobot)
+ ? "(fair robot)" : "(robot)");
+ } //add robot indicator
+ } //display playername/host
+
+ {
+ int x, y;
+ for (y = 0; y <= Players[scr].boardVisible; y++)
+ for (x = 0; x <= Players[scr].boardWidth; x++)
+ PlotBlock(scr, y, x, GetBlock(scr, y, x));
+ } //draw field
+
+ ShowPause(scr);
+} //DrawField
+
+ExtFunc void InitFields(void)
+{ //calculate positions of all fields
+ int scr, prevscr;
+ int y, x;
+
+ getmaxyx(stdscr, y, x);
+ boardXPos[me] = 1;
+ boardYPos[me] = 22;
+ statusXPos = 2 * Players[me].boardWidth + 3;
+ statusYPos = 22;
+ messageXPos = 2;
+ messageYPos = 25;
+ messageWidth = x - messageXPos - 2;
+ if ((messageHeight = y - messageYPos - 1) < 0) messageHeight = 0;
+ else DrawBox(messageXPos - 2, messageYPos - 1,
+ messageXPos + messageWidth + 1, messageYPos + messageHeight);
+ prevscr = me;
+ for (scr = 1; scr < MAX_SCREENS; scr++) if (scr != me) {
+ boardXPos[scr] =
+ boardXPos[prevscr] + 2 * Players[prevscr].boardWidth + 3;
+ if (prevscr == me)
+ boardXPos[scr] += 14; //scorebar
+ boardYPos[scr] = 22;
+ if (x < boardXPos[scr] + 2 * Players[scr].boardWidth + 1)
+ Players[scr].spy = 0; //field doesn't fit on screen
+ prevscr = scr;
}
-}
+ for (scr = 1; scr <= Game.maxplayers; scr++)
+ DrawField(scr);
+} //InitFields
ExtFunc void CleanupScreen(int scr)
{
}
-static void PlotBlock1(int scr, int y, int x, BlockType type)
+ExtFunc void Messagef(char *fmt, ...)
+{ //print game/bot message
+ static int line = 0;
+ va_list args;
+ char s[255];
+ char *p, *psearch;
+ int i;
+
+ if (!messageHeight) return;
+ va_start(args, fmt);
+ move(messageYPos + line, messageXPos);
+// vwprintw(stdscr, fmt, args); //doesn't seem to be a vprintw
+ vsprintf(s, fmt, args);
+ p = s;
+ while (psearch = strchr(s, '\\')) {
+ *psearch = '\0';
+ addstr(p);
+ if (haveColor)
+ attrset(A_REVERSE | COLOR_PAIR(atoi(psearch + 1) + 1));
+ p = psearch + 2;
+ } //search for color escapes (\)
+ addstr(p);
+ if (messageHeight > 1) {
+ char s[messageWidth + 1];
+ line = (line + 1) % messageHeight;
+ memset(s, ' ', messageWidth);
+ s[messageWidth] = 0;
+ mvaddstr(messageYPos + line, messageXPos, s);
+ } //multiple lines
+ if (haveColor) standend();
+ va_end(args);
+} //Message
+
+ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type)
{
- move(boardYPos[scr] - y, boardXPos[scr] + 2 * x);
- switch (type) {
- case BT_none:
- addstr(" ");
- break;
- case -BT_piece1:
- if (standoutEnable)
- standout();
- addstr("$$");
- if (standoutEnable)
- standend();
- break;
- case BT_piece1:
- if (standoutEnable)
- standout();
- addstr("[]");
- if (standoutEnable)
- standend();
- break;
- default:
- assert(0);
- }
-}
-
+ int colorIndex = abs(type);
+
+ move(y, x);
+ if (type == BT_none) addstr(" ");
+ else if (type == BT_shadow) addstr("::");
+ else {
+ if (Game.standout) {
+#ifdef HAVE_NCURSES
+ if (haveColor) attrset(COLOR_PAIR(colorIndex));
+ else attrset(A_REVERSE);
+#endif
+ }
+ addstr(type ? "[]" : "$$");
+#ifdef HAVE_NCURSES
+ if (Game.standout) standend();
+#endif
+ } //display one brick
+} //PlotBlock1
ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
{
- if (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr])
- PlotBlock1(scr, y, x, type);
-}
+ if (y >= 0 && y < Players[scr].boardVisible &&
+ x >= 0 && x < Players[scr].boardWidth)
+ PlotBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
+} //PlotBlock
+
+ExtFunc void PlotBlockS1(int scr, int y, int x, BlockType type)
+{ //DOESN"T WORK YET...
+ move(y, x);
+ if (type == BT_none) addstr(" ");
+ else {
+ addstr(type ? "O" : "$");
+ standend();
+ } //display one brick
+} //PlotBlock1
+ExtFunc void PlotBlockS(int scr, int y, int x, BlockType type)
+{
+ if (y >= 0 && y < Players[scr].boardVisible &&
+ x >= 0 && x < Players[scr].boardWidth)
+ PlotBlockS1(scr, boardYPos[scr] - y, boardXPos[scr] + x, type);
+} //PlotBlock
ExtFunc void PlotUnderline(int scr, int x, int flag)
-{
- move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
+{ //display piece of bottom fieldgrid
+ move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
+ if (Game.ascii)
addstr(flag ? "==" : "--");
-}
-
-ExtFunc void ShowDisplayInfo(void)
-{
- move(statusYPos - 9, statusXPos);
- printw("Seed: %d", initSeed);
- clrtoeol();
- move(statusYPos - 8, statusXPos);
- printw("Speed: %dms", speed / 1000);
- clrtoeol();
- if (robotEnable) {
- move(statusYPos - 6, statusXPos);
- if (fairRobot)
- addstr("Controlled by a fair robot");
- else
- addstr("Controlled by a robot");
- clrtoeol();
- }
- if (opponentFlags & SCF_usingRobot) {
- move(statusYPos - 5, statusXPos);
- if (opponentFlags & SCF_fairRobot)
- addstr("The opponent is a fair robot");
- else
- addstr("The opponent is a robot");
- clrtoeol();
- }
-}
-
-ExtFunc void UpdateOpponentDisplay(void)
-{
- move(1, 0);
- printw("Playing %s@%s", opponentName, opponentHost);
- clrtoeol();
-}
-
-ExtFunc void ShowPause(int pausedByMe, int pausedByThem)
-{
- move(statusYPos - 3, statusXPos);
- if (pausedByThem)
- addstr("Game paused by opponent");
- else
- clrtoeol();
- move(statusYPos - 2, statusXPos);
- if (pausedByMe)
- addstr("Game paused by you");
- else
- clrtoeol();
-}
-
-ExtFunc void Message(char *s)
-{
- static int line = 0;
-
- move(statusYPos - 20 + line, statusXPos);
- addstr(s); /* XXX Should truncate long lines */
- clrtoeol();
- line = (line + 1) % 10;
- move(statusYPos - 20 + line, statusXPos);
- clrtoeol();
-}
-
-ExtFunc void RefreshScreen(void)
-{
- static char timeStr[2][32];
- time_t theTime;
-
- time(&theTime);
- strftime(timeStr[0], 30, "%I:%M %p", localtime(&theTime));
- /* Just in case the local curses library sucks */
- if (strcmp(timeStr[0], timeStr[1]))
- {
- move(statusYPos, statusXPos);
- addstr(timeStr[0]);
- strcpy(timeStr[1], timeStr[0]);
- }
- move(boardYPos[0] + 1, boardXPos[0] + 2 * boardWidth[0] + 1);
- refresh();
-}
+ else {
+ addch(flag ? ACS_BTEE : ACS_HLINE);
+ addch(flag ? ACS_BTEE : ACS_HLINE);
+ } //ncurses graphics
+} //PlotUnderline
+
+ExtFunc void ShowScore(int scr, struct _Score score)
+{ //show score stuff
+ float timer;
+
+ mvaddstr(13, statusXPos, "next ");
+ mvaddstr(14, statusXPos + 5, " ");
+ ShapeIterate(Players[scr].nextShape, scr,
+ ShapeToNetNum(Players[scr].nextShape) == 15 ? 6 : 7,
+ statusXPos / 2 + 4, 1, GlanceFunc, NULL);
+ mvprintw(3, statusXPos, "level %5d", score.level);
+ mvprintw(5, statusXPos, "score%8d", score.score);
+ mvprintw(6, statusXPos, "lines%8d", score.lines);
+ timer = CurTimeval() / 1e6;
+ if (timer > 4) {
+ mvprintw(9, statusXPos, "ppm %9.1f", score.drops * 60 / timer);
+ if (score.lines > 0) {
+ mvprintw(7, statusXPos,
+ "yield %3d%%", 100 * score.adds / score.lines);
+ mvprintw(10, statusXPos, "apm %9.1f", score.adds * 60 / timer);
+ }
+ } //display [ap]pm
+ else {
+ int i;
+ for (i = 7; i <= 10; i++)
+ mvaddstr(i, statusXPos, " ");
+ } //too early to display stats, remove old..
+} //ShowScore
+
+ExtFunc void FieldMessage(int playa, char *message)
+{ //put a message over playa's field
+ if (!Players[playa].spy) return;
+ if (message) {
+ char s[MAX_BOARD_WIDTH+1];
+ memset(s, ' ', MAX_BOARD_WIDTH);
+ memcpy(&s[Players[playa].boardWidth - strlen(message) / 2],
+ message, strlen(message));
+ s[Players[playa].boardWidth * 2] = 0;
+#ifdef HAVE_NCURSES
+ attrset(A_REVERSE);
+#else
+ standout();
+#endif
+ mvprintw(boardYPos[playa] - Players[playa].boardVisible / 2,
+ boardXPos[playa], "%s", s);
+ standend();
+ } //display
+ else {
+ int x, y;
+ y = Players[playa].boardVisible / 2;
+ for (x = 0; x <= Players[playa].boardWidth; x++)
+ PlotBlock(playa, y, x, GetBlock(playa, y, x));
+ } //restore field
+} //FieldMessage
+
+ExtFunc void ShowPause(int playa)
+{ //put paused over player's field
+ if (Players[playa].alive)
+ if (Players[playa].flags & SCF_paused)
+ FieldMessage(playa, Game.started > 1
+ ? "P A U S E D" : "N O T R E A D Y");
+ else FieldMessage(playa, Game.started > 1 ? NULL : "R E A D Y");
+ else FieldMessage(playa, playa > maxPlayer
+ ? "E M P T Y" : "G A M E O V E R");
+} //ShowPause
+
+
+ExtFunc void ShowTime(void)
+{ //display timer
+ mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6);
+// move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1);
+// refresh();
+} //ShowTime
ExtFunc void ScheduleFullRedraw(void)
{
touchwin(stdscr);
-}
+} //ScheduleFullRedraw
static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event)
-{
+{ //read keypresses
if (MyRead(gen->fd, &event->u.key, 1))
return E_key;
else
return E_none;
-}
+} //KeyGenFunc
/*
* vi: ts=4 ai