use -10 for sterncolour
[unix-history] / usr / src / games / sail / pl_1.c
index 6dff455..c36d3e0 100644 (file)
@@ -1,26 +1,25 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)pl_1.c      1.9 83/07/20";
+static char *sccsid = "@(#)pl_1.c      2.3 83/11/02";
 #endif
 
 #include "player.h"
 #include <sys/types.h>
 #endif
 
 #include "player.h"
 #include <sys/types.h>
-#include <wait.h>
+#include <sys/wait.h>
 
 int choke(), child();
 
 
 int choke(), child();
 
+static char hasdriver;
+
 /*ARGSUSED*/
 main(argc, argv)
 int argc;
 char **argv;
 {
 /*ARGSUSED*/
 main(argc, argv)
 int argc;
 char **argv;
 {
-       register struct ship *sp;
-       int aheadfirst, ma;
-       int ta;
        char nodrive = 0, randomize = 0, debug = 0;
        char nodrive = 0, randomize = 0, debug = 0;
-       char *badstring();
        extern char _sobuf[];
 
        setbuf(stdout, _sobuf);
        extern char _sobuf[];
 
        setbuf(stdout, _sobuf);
+       isplayer = 1;
 
        while (*++argv && **argv == '-')
                switch (*++*argv) {
 
        while (*++argv && **argv == '-')
                switch (*++*argv) {
@@ -43,108 +42,7 @@ char **argv;
                game = -1;
        initialize(nodrive, randomize, debug);
        Signal("Aye aye, Sir", (struct ship *)0);
                game = -1;
        initialize(nodrive, randomize, debug);
        Signal("Aye aye, Sir", (struct ship *)0);
-       for (;;) {
-               prompt();
-               switch (sgetch(0)) {
-               case 'm':
-                       if (mc->crew3 && !snagged(ms)
-                           && windspeed != 0) {
-                               ta = maxturns(ms);
-                               aheadfirst = ta & 0100000;
-                               ma = maxmove(ms, mf->dir, 0);
-                               ta &= 077777;
-                               acceptmove(ma, ta, aheadfirst);
-                       } else
-                               Signal("Unable to move", (struct ship *)0);
-                       break;
-               case 's':
-                       acceptsignal();
-                       break;
-               case 'g':
-                       grapungrap();
-                       break;
-               case 'u':
-                       unfoulplayer();
-                       break;
-               case 'v':
-                       Signal("%s", (struct ship *)0, version);
-                       break;
-               case 'b':
-                       doboarding();
-                       break;
-               case 'f':
-                       acceptcombat();
-                       break;
-               case 'l':
-                       loadplayer();
-                       break;
-               case 'c':
-                       changesail();
-                       break;
-               case 'r':
-                       repair();
-                       break;
-               case 'B':
-                       Signal("'Hands to stations!'", (struct ship *)0);
-                       unboard(ms, ms, 1);     /* cancel DBP's */
-                       unboard(ms, ms, 0);     /* cancel offense */
-                       break;
-               case '\f':
-                       centerview();
-                       board();
-                       screen();
-                       break;
-               case 'L':
-                       mf->loadL = L_EMPTY;
-                       mf->loadR = L_EMPTY;
-                       mf->readyL = R_EMPTY;
-                       mf->readyR = R_EMPTY;
-                       Signal("Broadsides unloaded", (struct ship *)0);
-                       break;
-               case 'q':
-                       Signal("Type 'Q' to quit", (struct ship *)0);
-                       break;
-               case 'Q':
-                       leave(LEAVE_QUIT);
-                       break;
-               case 'I':
-                       foreachship(sp)
-                               eyeball(sp);
-                       break;
-               case 'i':
-                       eyeball(closestenemy(ms, 0, 1));
-                       break;
-               case 'C':
-                       centerview();
-                       draw_view();
-                       break;
-               case 'U':
-                       upview();
-                       draw_view();
-                       break;
-               case 'D':
-               case 'N':
-                       downview();
-                       draw_view();
-                       break;
-               case 'H':
-                       leftview();
-                       draw_view();
-                       break;
-               case 'J':
-                       rightview();
-                       draw_view();
-                       break;
-               case 'F':
-                       lookout();
-                       break;
-               case 'S':
-                       dont_adjust = !dont_adjust;
-                       break;
-               }
-               signalflags();
-               lost();
-       }
+       play();
 }
 
 initialize(nodriver, randomize, debug)
 }
 
 initialize(nodriver, randomize, debug)
@@ -152,10 +50,9 @@ char randomize, nodriver, debug;
 {
        register struct File *fp;
        register struct ship *sp;
 {
        register struct File *fp;
        register struct ship *sp;
-       char captain[80], file[25];
+       char captain[80];
        char message[60];
        int load;
        char message[60];
        int load;
-       int people = 0;
        register int n;
        char *nameptr;
        int nat[NNATION];
        register int n;
        char *nameptr;
        int nat[NNATION];
@@ -166,13 +63,10 @@ char randomize, nodriver, debug;
                (void) puts("Choose a scenario:\n");
                (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
                for (n = 0; n < NSCENE; n++) {
                (void) puts("Choose a scenario:\n");
                (void) puts("\n\tNUMBER\tSHIPS\tIN PLAY\tTITLE");
                for (n = 0; n < NSCENE; n++) {
-                       printf("\t%d):\t%d", n, scene[n].vessels);
-                       (void) sprintf(file, "/tmp/.%d", n);
-                       if (access(file, 0) >= 0)
-                               printf("\tYES");
-                       else
-                               printf("\tno");
-                       printf("\t%s\n", scene[n].name);
+                       /* ( */
+                       printf("\t%d):\t%d\t%s\t%s\n", n, scene[n].vessels,
+                               sync_exists(n) ? "YES" : "no",
+                               scene[n].name);
                }
 reprint:
                printf("\nScenario number? ");
                }
 reprint:
                printf("\nScenario number? ");
@@ -188,22 +82,6 @@ reprint:
        cc = &scene[game];
        ls = cc->ship + cc->vessels;
 
        cc = &scene[game];
        ls = cc->ship + cc->vessels;
 
-       (void) sprintf(file, "/tmp/.%d", game);
-       if (access(file, 0) < 0) {
-               int omask;
-#ifdef SETUID
-               omask = umask(077);
-#else
-               omask = umask(011);
-#endif
-               syncfile = fopen(file, "w+");
-               (void) umask(omask);
-       } else {
-               syncfile = fopen(file, "r+");
-               people = 1;
-       }
-       lastsync = 0;
-
        for (n = 0; n < NNATION; n++)
                nat[n] = 0;
        foreachship(sp) {
        for (n = 0; n < NNATION; n++)
                nat[n] = 0;
        foreachship(sp) {
@@ -213,70 +91,102 @@ reprint:
                        exit(0);
                }
                sp->file->stern = nat[sp->nationality]++;
                        exit(0);
                }
                sp->file->stern = nat[sp->nationality]++;
+               sp->file->dir = sp->shipdir;
+               sp->file->row = sp->shiprow;
+               sp->file->col = sp->shipcol;
+       }
+       windspeed = cc->windspeed;
+       winddir = cc->winddir;
+
+       (void) signal(SIGHUP, choke);
+       (void) signal(SIGINT, choke);
+       (void) signal(SIGQUIT, choke);
+
+       hasdriver = sync_exists(game);
+       if (sync_open() < 0) {
+               perror("sail: syncfile");
+               exit(1);
        }
        }
-       if (people > 0) {
+
+       if (hasdriver) {
                (void) puts("Synchronizing with the other players...");
                (void) fflush(stdout);
                Sync();
                (void) puts("Synchronizing with the other players...");
                (void) fflush(stdout);
                Sync();
-               foreachship(sp) {
-                       if (sp->file->captain[0]
-                           || sp->file->struck || sp->file->captured != 0)
+       }
+       for (;;) {
+               foreachship(sp)
+                       if (sp->file->captain[0] == 0 && !sp->file->struck
+                           && sp->file->captured == 0)
                                break;
                                break;
-               }
                if (sp >= ls) {
                        (void) puts("All ships taken in that scenario.");
                        foreachship(sp)
                                free((char *)sp->file);
                if (sp >= ls) {
                        (void) puts("All ships taken in that scenario.");
                        foreachship(sp)
                                free((char *)sp->file);
+                       sync_close(0);
                        people = 0;
                        people = 0;
-                       (void) fclose(syncfile);
                        goto reprint;
                }
                        goto reprint;
                }
-               player = sp - cc->ship;
-       } else
-               player = 0;
-
-       while (randomize) {
-               printf("%s\n\n", cc->name);
-               foreachship(sp) {
-                       printf("  %2d:  %-10s %-15s  (%-2d pts)   %s\n",
-                               sp - SHIP(0),
-                               countryname[sp->nationality],
-                               sp->shipname,
-                               sp->specs->pts,
-                               saywhat(sp, 1));
-               }
-               printf("\nWhich ship do you want (0-%d)? ", cc->vessels-1);
-               (void) fflush(stdout);
-               if (scanf("%d", &player) != 1 || player < 0
-                   || player >= cc->vessels) {
-                       while (getchar() != '\n')
-                               ;
-                       (void) puts("Say what?");
+               if (randomize) {
+                       player = sp - cc->ship;
                } else {
                } else {
-                       while (getchar() != '\n')
-                               ;
-                       Sync();
-                       fp = SHIP(player)->file;
-                       if (fp->captain[0] || fp->struck || fp->captured != 0)
-                               (void) puts("Sorry, that ship is taken.");
-                       else
-                               break;
+                       printf("%s\n\n", cc->name);
+                       foreachship(sp)
+                               printf("  %2d:  %-10s %-15s  (%-2d pts)   %s\n",
+                                       sp - SHIP(0),
+                                       countryname[sp->nationality],
+                                       sp->shipname,
+                                       sp->specs->pts,
+                                       saywhat(sp, 1));
+                       printf("\nWhich ship (0-%d)? ", cc->vessels-1);
+                       (void) fflush(stdout);
+                       if (scanf("%d", &player) != 1 || player < 0
+                           || player >= cc->vessels) {
+                               while (getchar() != '\n')
+                                       ;
+                               (void) puts("Say what?");
+                               player = -1;
+                       } else
+                               while (getchar() != '\n')
+                                       ;
                }
                }
+               if (player < 0)
+                       continue;
+               Sync();
+               fp = SHIP(player)->file;
+               if (fp->captain[0] || fp->struck || fp->captured != 0)
+                       (void) puts("That ship is taken.");
+               else
+                       break;
        }
 
        ms = SHIP(player);
        mf = ms->file;
        mc = ms->specs;
 
        }
 
        ms = SHIP(player);
        mf = ms->file;
        mc = ms->specs;
 
-       (void) signal(SIGHUP, choke);
-       (void) signal(SIGINT, choke);
-       (void) signal(SIGQUIT, choke);
+       Write(W_BEGIN, ms, 0, 0, 0, 0, 0);
+       Sync();
+
        (void) signal(SIGCHLD, child);
        (void) signal(SIGCHLD, child);
+       if (!hasdriver && !nodriver) {
+               char num[10];
+               (void) sprintf(num, "%d", game);
+               switch (fork()) {
+               case 0:
+                       execl(DRIVER1, DRIVERNAME, num, 0);
+                       execl(DRIVER2, DRIVERNAME, num, 0);
+                       execl(DRIVER3, DRIVERNAME, num, 0);
+                       perror(DRIVERNAME);
+                       exit(1);
+                       break;
+               case -1:
+                       perror("fork");
+                       leave(LEAVE_QUIT);
+                       break;
+               default:
+                       hasdriver++;
+               }
+       }
 
 
-       Write(W_CAPTAIN, ms, 1, (int) "begin", 0, 0, 0);
-       if (people)
-               Write(W_PEOPLE, SHIP(0), 0, cc->people + 1, 0, 0, 0);
-       Sync();
        printf("Your ship is the %s, a %d gun %s (%s crew).\n",
                ms->shipname, mc->guns, classname[mc->class],
                qualname[mc->qual]);
        printf("Your ship is the %s, a %d gun %s (%s crew).\n",
                ms->shipname, mc->guns, classname[mc->class],
                qualname[mc->qual]);
@@ -290,12 +200,15 @@ reprint:
                        (void) strcpy(captain, "no name");
        }
        captain[sizeof captain - 1] = '\0';
                        (void) strcpy(captain, "no name");
        }
        captain[sizeof captain - 1] = '\0';
+       Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
        for (n = 0; n < 2; n++) {
        for (n = 0; n < 2; n++) {
+               char buf[10];
+
                printf("\nInitial broadside %s (grape, chain, round, double): ",
                        n ? "right" : "left");
                (void) fflush(stdout);
                printf("\nInitial broadside %s (grape, chain, round, double): ",
                        n ? "right" : "left");
                (void) fflush(stdout);
-               (void) scanf("%s", file);
-               switch (*file) {
+               (void) scanf("%s", buf);
+               switch (*buf) {
                case 'g':
                        load = L_GRAPE;
                        break;
                case 'g':
                        load = L_GRAPE;
                        break;
@@ -319,36 +232,26 @@ reprint:
                        mf->readyL = R_LOADED|R_INITIAL;
                }
        }
                        mf->readyL = R_LOADED|R_INITIAL;
                }
        }
-       Write(W_CAPTAIN, ms, 1, (int)captain, 0, 0, 0);
-       if (!people && !nodriver) {
-               char num[10];
-               (void) sprintf(num, "%d", game);
-               if (!fork()) {
-                       if (debug)
-                               execl(DEBUGDRIVER, DRIVERNAME, num, 0);
-                       else
-                               execl(DRIVER, DRIVERNAME, num, 0);
-                       perror(DRIVER);
-                       exit(1);
-               }
-       }
 
        initscreen();
 
        initscreen();
-
-       board();
+       draw_board();
        (void) sprintf(message, "Captain %s assuming command", captain);
        Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
        (void) sprintf(message, "Captain %s assuming command", captain);
        Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
-
        newturn();
 }
 
        newturn();
 }
 
+/*
+ * If we get here before a ship is chosen, then ms == 0 and
+ * we don't want to update the score file, or do any Write's either.
+ * We can assume the sync file is already created and may need
+ * to be removed.
+ */
 leave(conditions)
 int conditions;
 {
        FILE *fp;
 leave(conditions)
 int conditions;
 {
        FILE *fp;
-       int people;
+       int persons;
        float net;
        float net;
-       char * capn;
        char message[60];
        register int n;
        struct logs log[10], temp;
        char message[60];
        register int n;
        struct logs log[10], temp;
@@ -359,14 +262,49 @@ int conditions;
        (void) signal(SIGALRM, SIG_IGN);
        (void) signal(SIGCHLD, SIG_IGN);
 
        (void) signal(SIGALRM, SIG_IGN);
        (void) signal(SIGCHLD, SIG_IGN);
 
-       if (conditions != -1) {
-               capn = mf->captain;
-               (void) sprintf(message,"Captain %s relinquishing.",capn);
-               Write(W_SIGNAL, ms, 1, (int)message, 0, 0, 0);
+       if (done_curses) {
+               Signal("It looks like you've had it!",
+                       (struct ship *)0);
+               switch (conditions) {
+               case LEAVE_QUIT:
+                       break;
+               case LEAVE_CAPTURED:
+                       Signal("Your ship was captured.",
+                               (struct ship *)0);
+                       break;
+               case LEAVE_HURRICAN:
+                       Signal("Hurricane!  All ships destroyed.",
+                               (struct ship *)0);
+                       break;
+               case LEAVE_DRIVER:
+                       /* don't clear 'hasdriver' here */
+                       Signal("The driver died.", (struct ship *)0);
+                       break;
+               default:
+                       Signal("A funny thing happened (%d).",
+                               (struct ship *)0, conditions);
+               }
+       } else {
+               switch (conditions) {
+               case LEAVE_QUIT:
+                       break;
+               case LEAVE_DRIVER:
+                       printf("The driver died.\n");
+                       break;
+               case LEAVE_FORK:
+                       hasdriver = 0;
+                       printf("Can't fork.\n");
+                       break;
+               default:
+                       printf("A funny thing happened (%d).\n",
+                               conditions);
+               }
+       }
 
 
+       if (ms != 0) {
                if (fp = fopen(LOGFILE, "r+")) {
                        net = (float)mf->points / mc->pts;
                if (fp = fopen(LOGFILE, "r+")) {
                        net = (float)mf->points / mc->pts;
-                       people = getw(fp);
+                       persons = getw(fp);
                        n = fread((char *)log, sizeof(struct logs), 10, fp);
                        for (; n < 10; n++)
                                log[n].l_name[0]
                        n = fread((char *)log, sizeof(struct logs), 10, fp);
                        for (; n < 10; n++)
                                log[n].l_name[0]
@@ -375,15 +313,15 @@ int conditions;
                                        = log[n].l_gamenum
                                        = log[n].l_netpoints = 0;
                        rewind(fp);
                                        = log[n].l_gamenum
                                        = log[n].l_netpoints = 0;
                        rewind(fp);
-                       if (people < 0)
+                       if (persons < 0)
                                (void) putw(1, fp);
                        else
                                (void) putw(1, fp);
                        else
-                               (void) putw(people + 1, fp);
+                               (void) putw(persons + 1, fp);
                        for (n = 0; n < 10; n++)
                                if (net > (float) log[n].l_netpoints / scene[log[n].l_gamenum].ship[log[n].l_shipnum].specs->pts) {
                                        (void) fwrite((char *)log,
                                                sizeof (struct logs), n, fp);
                        for (n = 0; n < 10; n++)
                                if (net > (float) log[n].l_netpoints / scene[log[n].l_gamenum].ship[log[n].l_shipnum].specs->pts) {
                                        (void) fwrite((char *)log,
                                                sizeof (struct logs), n, fp);
-                                       (void) strcpy(temp.l_name, capn);
+                                       (void) strcpy(temp.l_name, mf->captain);
                                        temp.l_uid = getuid();
                                        temp.l_shipnum = player;
                                        temp.l_gamenum = game;
                                        temp.l_uid = getuid();
                                        temp.l_shipnum = player;
                                        temp.l_gamenum = game;
@@ -396,44 +334,13 @@ int conditions;
                                }
                        (void) fclose(fp);
                }
                                }
                        (void) fclose(fp);
                }
-               Write(W_CAPTAIN, ms, 1, (int)" ", 0, 0, 0);
-               Write(W_PEOPLE, SHIP(0), 0, cc->people - 1, 0, 0, 0);
-               if (done_curses) {
-                       screen();
-                       Signal("It looks like you've had it!",
-                               (struct ship *)0);
-                       switch (conditions) {
-                       case LEAVE_QUIT:
-                               break;
-                       case LEAVE_CAPTURED:
-                               Signal("Your ship was captured.",
-                                       (struct ship *)0);
-                               break;
-                       case LEAVE_HURRICAN:
-                               Signal("Hurricane!  All ships destroyed.",
-                                       (struct ship *)0);
-                               break;
-                       case LEAVE_DRIVER:
-                               Signal("The driver died.", (struct ship *)0);
-                               break;
-                       default:
-                               Signal("A funny thing happened (%d).",
-                                       (struct ship *)0, conditions);
-                       }
-               } else {
-                       if (conditions == LEAVE_DRIVER)
-                               printf("The driver died.\n");
-                       else
-                               printf("leave: unknown code %d\n", conditions);
-               }
-               (void) fclose(syncfile);
-       }
-       if (done_curses) {
-               lastline();
-               nocrmode();
-               echo();
-               endwin();
+               makesignal(ms, "Captain %s relinquishing.", (struct ship *)0,
+                       mf->captain);
+               Write(W_END, ms, 0, 0, 0, 0, 0);
+               Sync();
        }
        }
+       sync_close(!hasdriver);
+       cleanupscreen();
        exit(0);
 }
 
        exit(0);
 }