]> thyrsus.com Git - bs.git/commitdiff
This matches the 2.1 that went out with ncurses-1.9.5.
authorEric S. Raymond <esr@thyrsus.com>
Mon, 18 Sep 1995 03:01:30 +0000 (03:01 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 18 Sep 1995 03:01:30 +0000 (03:01 +0000)
bs.c

diff --git a/bs.c b/bs.c
index 9448e6bf13c6303101b62c0bce9932ffb4e8e901..d5247654ff75779d3764d0295851b314f5c09d8c 100644 (file)
--- a/bs.c
+++ b/bs.c
@@ -5,8 +5,7 @@
  *             by Eric S. Raymond <esr@snark.thyrsus.com>
  * v1.2 with color support and minor portability fixes, November 1990
  * v2.0 featuring strict ANSI/POSIX conformance, November 1993.
- * v2.1 for Linux, October 1994.
- * Slipstreamed in fixes to make it gcc -Wall clean, May '95.
+ * v2.1 with ncurses mouse support, September 1995
  */
 #define _POSIX_SOURCE
 
@@ -17,6 +16,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <assert.h>
+#include <time.h>
 
 #ifndef SIGIOT
 #define SIGIOT SIGABRT
 #define strchr index
 #endif /* !A_UNDERLINE */
 
-#ifdef isxdigit                /* aha, must be an AT&T system... */
-
-#if !defined(__386BSD__) && !defined(__bsdi__)
-#define srand(n)       srand48(n)
-#define rand()         lrand48()
-extern long lrand48();
-extern void srand48();
-#endif
-
-#define bzero(s, n)    (void)memset((char *)(s), '\0', n)
-extern char *memset();
-/*
- * Try this if ungetch() fails to resolve.
- *
- * #define ungetch ungetc
- */
-#endif /* isxdigit */
-
-extern unsigned sleep();
-extern char *strchr(), *strcpy();
-extern long time();
-extern void exit();
-
-static bool checkplace();
+static int getcoord(int);
 
 /*
  * Constants for tuning the random-fire algorithm. It prefers moves that
@@ -94,6 +71,8 @@ static bool checkplace();
 #define CXBASE 48
 #define CY(y)  (CYBASE + (y))
 #define CX(x)  (CXBASE + (x)*3)
+#define CYINV(y)       ((y) - CYBASE)
+#define CXINV(x)       (((x) - CXBASE) / 3)
 #define cgoto(y, x)    (void)move(CY(y), CX(x))
 
 #define ONBOARD(x, y)  (x >= 0 && x < BWIDTH && y >= 0 && y < BDEPTH)
@@ -148,6 +127,8 @@ typedef struct
 }
 ship_t;
 
+static bool checkplace(int b, ship_t *ss, int vis);
+
 ship_t plyship[SHIPTYPES] =
 {
     { carrier, 0, 'A', 5},
@@ -176,19 +157,18 @@ static int salvo, blitz, closepack;
 
 #define        PR      (void)addstr
 
-static void uninitgame(sig)
+static void uninitgame(int sig)
 /* end the game, either normally or due to signal */
-int    sig;
 {
     clear();
     (void)refresh();
     (void)resetterm();
     (void)echo();
     (void)endwin();
-    exit(0);
+    exit(sig);
 }
 
-static void announceopts()
+static void announceopts(void)
 /* announce which game options are enabled */
 {
     if (salvo || blitz || closepack)
@@ -212,12 +192,12 @@ static void announceopts()
        "Playing standard game (noblitz, nosalvo, noclosepack)");
 }
 
-static void intro()
+static void intro(void)
 {
-    extern char *getlogin();
+    extern char *getlogin(void);
     char *tmpname;
 
-    srand(time(0L)+getpid());  /* Kick the random number generator */
+    srand((unsigned)(time(0L)+getpid()));      /* Kick the random number generator */
 
     (void) signal(SIGINT,uninitgame);
     (void) signal(SIGINT,uninitgame);
@@ -274,13 +254,14 @@ static void intro()
     init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
 #endif /* A_COLOR */
 
+#ifdef NCURSES_MOUSE_VERSION
+    (void) mousemask(BUTTON1_CLICKED, (mmask_t *)NULL);
+#endif /* NCURSES_MOUSE_VERSION*/
 }                  
 
 /* VARARGS1 */
-static void prompt(n, f, s)
+static void prompt(int n, char *f, char *s)
 /* print a message at the prompt line */
-int n;
-char *f, *s;
 {
     (void) move(PROMPTLINE + n, 0);
     (void) clrtoeol();
@@ -288,8 +269,7 @@ char *f, *s;
     (void) refresh();
 }
 
-static void error(s)
-char *s;
+static void error(char *s)
 {
     (void) move(PROMPTLINE + 2, 0);
     (void) clrtoeol();
@@ -300,10 +280,7 @@ char *s;
     }
 }
 
-static void placeship(b, ss, vis)
-int b;
-ship_t *ss;
-int vis;
+static void placeship(int b, ship_t *ss, int vis)
 {
     int l;
 
@@ -322,16 +299,13 @@ int vis;
     ss->hits = 0;
 }
 
-static int rnd(n)
-int n;
+static int rnd(int n)
 {
     return(((rand() & 0x7FFF) % n));
 }
 
-static void randomplace(b, ss)
+static void randomplace(int b, ship_t *ss)
 /* generate a valid random ship placement into px,py */
-int b;
-ship_t *ss;
 {
     register int bwidth = BWIDTH - ss->length;
     register int bdepth = BDEPTH - ss->length;
@@ -344,7 +318,7 @@ ship_t *ss;
        (!checkplace(b, ss, FALSE));
 }
 
-static void initgame()
+static void initgame(void)
 {
     int i, j, unplaced;
     ship_t *ss;
@@ -354,8 +328,8 @@ static void initgame()
     (void) move(PROMPTLINE + 2, 0);
     announceopts();
 
-    bzero(board, sizeof(char) * BWIDTH * BDEPTH * 2);
-    bzero(hits, sizeof(char) * BWIDTH * BDEPTH * 2);
+    memset(board, 0, sizeof(char) * BWIDTH * BDEPTH * 2);
+    memset(hits,  0, sizeof(char) * BWIDTH * BDEPTH * 2);
     for (i = 0; i < SHIPTYPES; i++)
     {
        ss = cpuship + i;
@@ -369,7 +343,7 @@ static void initgame()
     (void) mvaddstr(PYBASE - 1, PXBASE - 3,numbers);
     for(i=0; i < BDEPTH; ++i)
     {
-       (void) mvaddch(PYBASE + i, PXBASE - 3, i + 'A');
+       (void) mvaddch(PYBASE + i, PXBASE - 3, (chtype)(i + 'A'));
 #ifdef A_COLOR
        if (has_colors())
            attron(COLOR_PAIR(COLOR_BLUE));
@@ -381,14 +355,14 @@ static void initgame()
        attrset(0);
 #endif /* A_COLOR */
        (void) addch(' ');
-       (void) addch(i + 'A');
+       (void) addch((chtype)(i + 'A'));
     }
     (void) mvaddstr(PYBASE + BDEPTH, PXBASE - 3,numbers);
     (void) mvaddstr(CYBASE - 2, CXBASE + 7,"Hit/Miss Board");
     (void) mvaddstr(CYBASE - 1, CXBASE - 3, numbers);
     for(i=0; i < BDEPTH; ++i)
     {
-       (void) mvaddch(CYBASE + i, CXBASE - 3, i + 'A');
+       (void) mvaddch(CYBASE + i, CXBASE - 3, (chtype)(i + 'A'));
 #ifdef A_COLOR
        if (has_colors())
            attron(COLOR_PAIR(COLOR_BLUE));
@@ -400,7 +374,7 @@ static void initgame()
        attrset(0);
 #endif /* A_COLOR */
        (void) addch(' ');
-       (void) addch(i + 'A');
+       (void) addch((chtype)(i + 'A'));
     }
 
     (void) mvaddstr(CYBASE + BDEPTH,CXBASE - 3,numbers);
@@ -434,8 +408,6 @@ static void initgame()
 
     ss = (ship_t *)NULL;
     do {
-       extern char *strchr();
-       static char getcoord();
        char c, docked[SHIPTYPES + 2], *cp = docked;
 
        /* figure which ships still wait to be placed */
@@ -485,7 +457,7 @@ static void initgame()
        }           
        else if (c == 'R')
        {
-           prompt(1, "Placing the rest of your fleet at random...");
+           prompt(1, "Placing the rest of your fleet at random...", "");
            for (ss = plyship; ss < plyship + SHIPTYPES; ss++)
                if (!ss->placed)
                {
@@ -536,12 +508,11 @@ static void initgame()
     (void) mvprintw(HYBASE+5,  HXBASE,
                    "                                                       ");
 
-    (void) prompt(0, "Press any key to start...");
+    (void) prompt(0, "Press any key to start...", "");
     (void) getch();
 }
 
-static int getcoord(atcpu)
-int atcpu;
+static int getcoord(int atcpu)
 {
     int ny, nx, c;
 
@@ -618,6 +589,25 @@ int atcpu;
            (void)clearok(stdscr, TRUE);
            (void)refresh();
            break;
+#ifdef NCURSES_MOUSE_VERSION
+       case KEY_MOUSE:
+           {
+               MEVENT  myevent;
+
+               getmouse(&myevent);
+               if (atcpu
+                       && myevent.y >= CY(0) && myevent.y <= CY(BDEPTH)
+                       && myevent.x >= CX(0) && myevent.x <= CX(BDEPTH))
+               {
+                   curx = CXINV(myevent.x);
+                   cury = CYINV(myevent.y);
+                   return(' ');
+               }
+               else
+                   beep();
+           }
+           break;
+#endif /* NCURSES_MOUSE_VERSION */
        default:
            if (atcpu)
                (void) mvaddstr(CYBASE + BDEPTH + 1, CXBASE + 11, "      ");
@@ -631,10 +621,8 @@ int atcpu;
     }
 }
 
-static int collidecheck(b, y, x)
+static int collidecheck(int b, int y, int x)
 /* is this location on the selected zboard adjacent to a ship? */
-int b;
-int y, x;
 {
     int        collide;
 
@@ -660,10 +648,7 @@ int y, x;
     return(collide);
 }
 
-static bool checkplace(b, ss, vis)
-int b;
-ship_t *ss;
-int vis;
+static bool checkplace(int b, ship_t *ss, int vis)
 {
     int l, xend, yend;
 
@@ -711,7 +696,7 @@ int vis;
     return(TRUE);
 }
 
-static int awinna()
+static int awinna(void)
 {
     int i, j;
     ship_t *ss;
@@ -728,9 +713,8 @@ static int awinna()
     return(-1);
 }
 
-static ship_t *hitship(x, y)
+static ship_t *hitship(int x, int y)
 /* register a hit on the targeted ship */
-int x, y;
 {
     ship_t *sb, *ss;
     char sym;
@@ -757,16 +741,16 @@ int x, y;
 
                        for (i = -1; i <= ss->length; ++i)
                        {
-                           int x, y;
+                           int x1, y1;
                            
-                           x = bx + i * xincr[ss->dir];
-                           y = by + i * yincr[ss->dir];
-                           if (ONBOARD(x, y))
+                           x1 = bx + i * xincr[ss->dir];
+                           y1 = by + i * yincr[ss->dir];
+                           if (ONBOARD(x1, y1))
                            {
-                               hits[turn][x][y] = MARK_MISS;
+                               hits[turn][x1][y1] = MARK_MISS;
                                if (turn % 2 == PLAYER)
                                {
-                                   cgoto(y, x);
+                                   cgoto(y1, x1);
 #ifdef A_COLOR
                                    if (has_colors())
                                        attron(COLOR_PAIR(COLOR_GREEN));
@@ -782,14 +766,14 @@ int x, y;
 
                for (i = 0; i < ss->length; ++i)
                {
-                   int x = ss->x + i * xincr[ss->dir];
-                   int y = ss->y + i * yincr[ss->dir];
+                   int x1 = ss->x + i * xincr[ss->dir];
+                   int y1 = ss->y + i * yincr[ss->dir];
 
-                   hits[turn][x][y] = ss->symbol;
+                   hits[turn][x1][y1] = ss->symbol;
                    if (turn % 2 == PLAYER)
                    {
-                       cgoto(y, x);
-                       (void) addch(ss->symbol);
+                       cgoto(y1, x1);
+                       (void) addch((chtype)(ss->symbol));
                    }
                }
 
@@ -801,19 +785,19 @@ int x, y;
     return((ship_t *)NULL);
 }
 
-static int plyturn()
+static int plyturn(void)
 {
     ship_t *ss;
     bool hit;
     char *m = NULL;
 
-    prompt(1, "Where do you want to shoot? ");
+    prompt(1, "Where do you want to shoot? ", "");
     for (;;)
     {
        (void) getcoord(COMPUTER);
        if (hits[PLAYER][curx][cury])
        {
-           prompt(1, "You shelled this spot already! Try again.");
+           prompt(1, "You shelled this spot already! Try again.", "");
            beep();
        }
        else
@@ -862,8 +846,7 @@ static int plyturn()
     return(hit);
 }
 
-static int sgetc(s)
-char *s;
+static int sgetc(char *s)
 {
     char *s1;
     int ch;
@@ -875,7 +858,7 @@ char *s;
        if (islower(ch))
            ch = toupper(ch);
        if (ch == CTRLC)
-           uninitgame();
+           uninitgame(0);
        for (s1=s; *s1 && ch != *s1; ++s1)
            continue;
        if (*s1)
@@ -888,9 +871,8 @@ char *s;
 }
 
 
-static void randomfire(px, py)
+static void randomfire(int *px, int *py)
 /* random-fire routine -- implements simple diagonal-striping strategy */
-int    *px, *py;
 {
     static int turncount = 0;
     static int srchstep = BEGINSTEP;
@@ -948,9 +930,8 @@ int *px, *py;
 #define S_HIT  1
 #define S_SUNK -1
 
-static bool cpufire(x, y)
+static bool cpufire(int x, int y)
 /* fire away at given location */
-int    x, y;
 {
     bool hit, sunk;
     ship_t *ss = NULL;
@@ -983,7 +964,7 @@ int x, y;
  * unstructuredness below. The five labels are states which need to be held
  * between computer turns.
  */
-static bool cputurn()
+static bool cputurn(void)
 {
 #define POSSIBLE(x, y) (ONBOARD(x, y) && !hits[COMPUTER][x][y])
 #define RANDOM_FIRE    0
@@ -1107,7 +1088,8 @@ static bool cputurn()
     return(hit);
 }
 
-int playagain()
+static
+int playagain(void)
 {
     int j;
     ship_t *ss;
@@ -1136,9 +1118,7 @@ int playagain()
     return(sgetc("YN") == 'Y');
 }
 
-static void do_options(c,op)
-int c;
-char *op[];
+static void do_options(int c, char *op[])
 {
     register int i;
 
@@ -1191,8 +1171,7 @@ char *op[];
     }
 }
 
-static int scount(who)
-int who;
+static int scount(int who)
 {
     register int i, shots;
     register ship_t *sp;
@@ -1212,9 +1191,7 @@ int who;
     return(shots);
 }
 
-main(argc, argv)
-int argc;
-char *argv[];
+int main(int argc, char *argv[])
 {
     do_options(argc, argv);
 
@@ -1259,7 +1236,7 @@ char *argv[];
        }
     } while
        (playagain());
-    uninitgame();
+    uninitgame(0);
     /*NOTREACHED*/
 }