unofficial version 0.7.1: ui improvements
[netris.git] / curses.c
index f28a6a36a2ec81cf6c846f87599e187a31713c05..030ed25ceb1d7958e1033782ddc0b9fc54cb8181 100644 (file)
--- a/curses.c
+++ b/curses.c
 # define HAVE_NCURSES
 #endif
 
-#ifdef HAVE_NCURSES
-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 }
-};
-#endif
-
 ExtFunc void PlotBlock1(int scr, int y, int x, BlockType type);
-static MyEventType KeyGenFunc(EventGenRec *gen, MyEvent *event);
 
+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 */
@@ -70,7 +55,7 @@ ExtFunc void InitScreens(void)
         * Ctrl-C during initialization might leave the terminal in a bad state.
         */
        BlockSignals(&oldMask, SIGINT, 0);
-       initscr();
+       initscr();              //start curses
 
 #ifdef CURSES_HACK
        {
@@ -83,44 +68,48 @@ ExtFunc void InitScreens(void)
 #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);
+       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, 1);
-       addstr("NETRIS ");
-       addstr(version_string);
-       {
-               int rows, cols;
-
-               getmaxyx(stdscr, rows, cols);
-               move(0, cols - 48);
-               addstr("(C)1994-1996,1999 Mark H. Weaver, (C)2002 Shiar");
-//             addstr("    \"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
 
@@ -188,102 +177,164 @@ ExtFunc void OutputTermStr(char *str, int flush)
        }
 } //OutputTermStr
 
-ExtFunc void DrawField(int scr)
-{ //drow field for player scr
-       {
-               int y, x;
-
-               getmaxyx(stdscr, y, x);
-               if (x < boardXPos[scr] + 2 * Players[scr].boardWidth + 1) {
-                       Players[scr].spy = 0;
-                       return;
-               }
+ExtFunc void DrawTitle(void)
+{
+       int rows, cols;
+       char s[255];
 
-               for (y = Players[scr].boardVisible - 1; y >= 0; --y) {
-                       mvaddch(boardYPos[scr] - y, boardXPos[scr] - 1,
-                               Game.ascii ? '|' : ACS_VLINE); //left
-                       mvaddch(boardYPos[scr] - y,
-                               boardXPos[scr] + 2 * Players[scr].boardWidth,
-                               Game.ascii ? '|' : ACS_VLINE); //right
-               }
-               move(2, boardXPos[scr] - 1); //top
-               addch(Game.ascii ? '+' : ACS_ULCORNER);
-               for (x = Players[scr].boardWidth * 2 - 1; x >= 0; --x)
-                       addch(Game.ascii ? '-' : ACS_HLINE);
-               addch(Game.ascii ? '+' : ACS_URCORNER);
-               move(boardYPos[scr] + 1, boardXPos[scr] - 1); //bottom
-               addch(Game.ascii ? '+' : ACS_LLCORNER);
-               for (x = Players[scr].boardWidth * 2 - 1; x >= 0; --x)
-                       addch(Game.ascii ? '-' : ACS_HLINE);
-               addch(Game.ascii ? '+' : ACS_LRCORNER);
-       } //draw field grid
+#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;
+
+       for (y = y1 + 1; y < y2; y++) {
+               mvaddch(y, x1, Game.ascii ? '|' : ACS_VLINE); //left
+               mvaddch(y, x2, Game.ascii ? '|' : ACS_VLINE); //right
+       }
+       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 userstr[300];
+               char s[2*Players[scr].boardWidth];
 
+               memset(s, ' ', sizeof(s));
                if (Players[scr].host && Players[scr].host[0])
-                        sprintf(userstr, "%s <%s>", Players[scr].name, Players[scr].host);
-               else sprintf(userstr, "%s", Players[scr].name);
-               userstr[20 - 7*((Players[scr].flags & SCF_usingRobot) != 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], userstr);
+               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
 
+       // draw blocks (which is usually just clear field)
+
        ShowPause(scr);
-} //DrawScreen
+} //DrawField
 
-ExtFunc void InitFields()
-{ //place fields for all players
+ExtFunc void InitFields(void)
+{ //calculate positions of all fields
        int scr, prevscr;
+       int y, x;
 
-       statusXPos = 2 * Players[me].boardWidth + 3;
+       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 <= totalPlayers + 1; scr++) if (scr != 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] += 24; //scorebar
+                       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;
        }
-} //InitScreen
+       for (scr = 1; scr <= Game.maxplayers; scr++)
+               DrawField(scr);
+} //InitFields
 
 ExtFunc void CleanupScreen(int scr)
 {
 }
 
+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)
 {
        int colorIndex = abs(type);
 
        move(y, x);
-
-       if (type == BT_none)
-               addstr("  ");
-       else
-       {
-               if (Game.standout)
-               {
+       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
+                       if (haveColor) attrset(COLOR_PAIR(colorIndex));
+                       else attrset(A_REVERSE);
 #endif
-                               standout();
                }
-
                addstr(type ? "[]" : "$$");
-               standend();
-       }
+#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 < Players[scr].boardVisible &&
@@ -291,38 +342,66 @@ ExtFunc void PlotBlock(int scr, int y, int x, BlockType type)
          PlotBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
 } //PlotBlock
 
-ExtFunc void PlotUnderline(int scr, int x, int flag)
+ExtFunc void PlotShadowBlock1(int scr, int y, int x, BlockType type)
+{
+       move(y, x);
+       if (type == BT_none) addstr("  ");
+       else addstr("::");
+} //PlotShadowBlock1
+ExtFunc void PlotShadowBlock(int scr, int y, int x, BlockType type)
+{
+       if (y >= 0 && y < Players[scr].boardVisible &&
+               x >= 0 && x < Players[scr].boardWidth)
+         PlotShadowBlock1(scr, boardYPos[scr] - y, boardXPos[scr] + 2 * x, type);
+} //PlotShadowBlock
+
+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)
+{ //display piece of bottom fieldgrid
   move(boardYPos[scr] + 1, boardXPos[scr] + 2 * x);
   if (Game.ascii)
        addstr(flag ? "==" : "--");
   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;
 
-       move(6, statusXPos); addstr("Next:          ");
-       move(7, statusXPos + 7);    addstr("        ");
+       mvaddstr(13, statusXPos, "next         ");
+       mvaddstr(14, statusXPos + 5,  "        ");
        ShapeIterate(Players[scr].nextShape, scr,
-         ShapeToNetNum(Players[scr].nextShape) == 15 ? 13 : 14,
-         statusXPos / 2 + 5, 1, GlanceFunc, NULL);
-       move(statusYPos - 21 + 1, statusXPos);
-       printw("Score:%6d  level: %2d", score.score, score.level);
-       move(statusYPos - 20 + 1, statusXPos);
+         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;
-       printw("Lines:%6d", score.lines);
        if (timer > 4) {
-               printw("  ppm:%5.1f", score.drops * 60 / timer);
-               move(statusYPos - 18, statusXPos);
-               if (score.lines > 0)
-                       printw("yield:  %3d%%", 100 * score.adds / score.lines);
-               else addstr("            ");
-               printw("  apm:%5.1f", score.adds * 60 / timer);
+               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);
+               }
        }
 } //ShowScore
 
@@ -335,7 +414,11 @@ ExtFunc void FieldMessage(int playa, char *message)
                memcpy(&s[Players[playa].boardWidth - strlen(message) / 2],
                        message, strlen(message));
                s[Players[playa].boardWidth * 2] = 0;
-               if (Game.standout) standout();
+#ifdef HAVE_NCURSES
+               attrset(A_REVERSE);
+#else
+               standout();
+#endif
                mvprintw(boardYPos[playa] - Players[playa].boardVisible / 2,
                        boardXPos[playa], "%s", s);
                standend();
@@ -350,31 +433,20 @@ ExtFunc void FieldMessage(int playa, char *message)
 
 ExtFunc void ShowPause(int playa)
 { //put paused over player's field
-       if (Players[playa].flags & SCF_paused)
-               FieldMessage(playa, "P A U S E D");
-       else FieldMessage(playa, NULL);
+       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 Message(char *s)
-{
-       static int line = 0;
-
-//     move(statusYPos - 20 + line, statusXPos);
-       move(statusYPos + 2 + line, 1);
-       addstr(s);      /* XXX Should truncate long lines */
-       clrtoeol();
-       line = (line + 1) % 10;
-//     move(statusYPos - 20 + line, statusXPos);
-       move(statusYPos + 2 + line, 1);
-       clrtoeol();
-} //Message
-
 ExtFunc void ShowTime(void)
 { //display timer
-       move(statusYPos, statusXPos);
-       printw("Timer: %.0f ", CurTimeval() / 1e6);
-       move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1);
+       mvprintw(statusYPos, statusXPos, "timer %7.0f ", CurTimeval() / 1e6);
+//     move(boardYPos[0] + 1, boardXPos[0] + 2 * Players[0].boardWidth + 1);
 //     refresh();
 } //ShowTime