BSD 4_3_Tahoe release
[unix-history] / usr / src / games / sail / dr_1.c
index 339d662..04884aa 100644 (file)
@@ -1,94 +1,44 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)dr_1.c      1.9 83/10/31";
-#endif
+static char sccsid[] = "@(#)dr_1.c     5.3 (Berkeley) 6/18/88";
+#endif /* not lint */
 
 #include "driver.h"
 
 
 #include "driver.h"
 
-main(argc, argv)
-int argc;
-char **argv;
-{
-       register int n;
-       register struct ship *sp;
-       int nat[NNATION];
-
-       if (argc != 2)
-               exit(1);
-       (void) signal(SIGINT, SIG_IGN);
-       (void) signal(SIGQUIT, SIG_IGN);
-       (void) srand(getpid());
-       /* ;;; add code here to check the game number. */
-       game = atoi(argv[1]);
-       cc = &scene[game];
-       ls = &cc->ship[cc->vessels];
-       if (sync_open() < 0) {
-               perror("driver: syncfile");
-               exit(1);
-       }
-       for (n = 0; n < NNATION; n++)
-               nat[n] = 0;
-       foreachship(sp) {
-               sp->file = (struct File *) calloc(1, sizeof (struct File));
-               if (sp == NULL) {
-                       (void) printf("driver: OUT OF MEMORY\n");
-                       exit(0);
-               }
-               sp->file->loadL = L_ROUND;
-               sp->file->loadR = L_ROUND;
-               sp->file->readyR = R_LOADED|R_INITIAL;
-               sp->file->readyL = R_LOADED|R_INITIAL;
-               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;
-       for (;;) {
-               Sync();
-               next();
-               unfoul();
-               checkup();
-               prizecheck();
-               moveall();
-               /*
-               readpos();
-               */
-               thinkofgrapples();
-               boardcomp();
-               compcombat();
-               /*
-               readpos();
-               */
-               resolve();
-               reload();
-               checksails();
-               Sync();
-               sleep(7);
-       }
-}
-
 unfoul()
 {
 unfoul()
 {
-       register int k;
        register struct ship *sp;
        struct ship *to;
        register int nat;
        register struct ship *sp;
        struct ship *to;
        register int nat;
+       register i;
 
        foreachship(sp) {
                if (sp->file->captain[0])
                        continue;
                nat = capship(sp)->nationality;
 
        foreachship(sp) {
                if (sp->file->captain[0])
                        continue;
                nat = capship(sp)->nationality;
-               for (k = 0; k < NSHIP; k++) {
-                       if (sp->file->fouls[k].turnfoul == 0)
+               foreachship(to) {
+                       if (nat != capship(to)->nationality &&
+                           !toughmelee(sp, to, 0, 0))
                                continue;
                                continue;
-                       to = sp->file->fouls[k].toship;
-                       if (nat != capship(to)->nationality)
-                               continue;
-                       if (!toughmelee(sp, to, 0, 0))
-                               continue;
-                       if (die() <= 2)
-                               cleanfoul(sp, to, k);
+                       for (i = fouled2(sp, to); --i >= 0;)
+                               if (die() <= 2)
+                                       cleanfoul(sp, to, 0);
                }
        }
 }
                }
        }
 }
@@ -101,12 +51,12 @@ boardcomp()
        foreachship(sp) {
                if (*sp->file->captain)
                        continue;
        foreachship(sp) {
                if (*sp->file->captain)
                        continue;
-               if (!fouled(sp) && !grappled(sp))
-                       continue;
                if (sp->file->dir == 0)
                        continue;
                if (sp->file->struck || sp->file->captured != 0)
                        continue;
                if (sp->file->dir == 0)
                        continue;
                if (sp->file->struck || sp->file->captured != 0)
                        continue;
+               if (!snagged(sp))
+                       continue;
                crew[0] = sp->specs->crew1 != 0;
                crew[1] = sp->specs->crew2 != 0;
                crew[2] = sp->specs->crew3 != 0;
                crew[0] = sp->specs->crew1 != 0;
                crew[1] = sp->specs->crew2 != 0;
                crew[2] = sp->specs->crew3 != 0;
@@ -175,7 +125,7 @@ int key;
                fromcap = from;
        if (tocap == 0)
                tocap = to;
                fromcap = from;
        if (tocap == 0)
                tocap = to;
-       if (key) { 
+       if (key) {
                if (!menfrom) {          /* if crew surprised */
                        if (fromcap == from)
                                menfrom = from->specs->crew1
                if (!menfrom) {          /* if crew surprised */
                        if (fromcap == from)
                                menfrom = from->specs->crew1
@@ -235,7 +185,7 @@ int key;
    I guess that what is going on here is that the pointer is multiplied
    or something. */
 
    I guess that what is going on here is that the pointer is multiplied
    or something. */
 
-                       Write(W_CAPTURED, from, 0, to-SHIP(0), 0, 0, 0);
+                       Write(W_CAPTURED, from, 0, to->file->index, 0, 0, 0);
                        topoints = 2 * from->specs->pts + to->file->points;
                        if (from->file->struck)
                                topoints -= from->specs->pts;
                        topoints = 2 * from->specs->pts + to->file->points;
                        if (from->file->struck)
                                topoints -= from->specs->pts;
@@ -266,23 +216,24 @@ resolve()
        foreachship(sp) {
                if (sp->file->dir == 0)
                        continue;
        foreachship(sp) {
                if (sp->file->dir == 0)
                        continue;
-               thwart = 2;
                for (sq = sp + 1; sq < ls; sq++)
                        if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
                                (void) fightitout(sp, sq, 0);
                for (sq = sp + 1; sq < ls; sq++)
                        if (sq->file->dir && meleeing(sp, sq) && meleeing(sq, sp))
                                (void) fightitout(sp, sq, 0);
+               thwart = 2;
                foreachship(sq) {
                        if (sq->file->dir && meleeing(sq, sp))
                                thwart = fightitout(sp, sq, 1);
                        if (!thwart)
                                break;
                }
                foreachship(sq) {
                        if (sq->file->dir && meleeing(sq, sp))
                                thwart = fightitout(sp, sq, 1);
                        if (!thwart)
                                break;
                }
-               foreachship(sq) {
-                       if (sq->file->dir && meleeing(sq, sp))
-                               unboard(sq, sp, 0);
-                       unboard(sp, sq, 0);
-               }
-               unboard(sp, sp, 1);
-               if (thwart == 2)
+               if (!thwart) {
+                       foreachship(sq) {
+                               if (sq->file->dir && meleeing(sq, sp))
+                                       unboard(sq, sp, 0);
+                               unboard(sp, sq, 0);
+                       }
+                       unboard(sp, sp, 1);
+               } else if (thwart == 2)
                        unboard(sp, sp, 1);
        }
 }
                        unboard(sp, sp, 1);
        }
 }
@@ -424,56 +375,71 @@ next()
                else
                        people = 0;
        if (people <= 0 || windspeed == 7) {
                else
                        people = 0;
        if (people <= 0 || windspeed == 7) {
-               sync_close(1);
-               exit(0);
+               register struct ship *s;
+               struct ship *bestship;
+               float net, best = 0.0;
+               foreachship(s) {
+                       if (*s->file->captain)
+                               continue;
+                       net = (float)s->file->points / s->specs->pts;
+                       if (net > best) {
+                               best = net;
+                               bestship = s;
+                       }
+               }
+               if (best > 0.0) {
+                       char *p = getenv("WOTD");
+                       if (p == 0)
+                               p = "Driver";
+                       if (islower(*p))
+                               *p = toupper(*p);
+                       (void) strncpy(bestship->file->captain, p,
+                               sizeof bestship->file->captain);
+                       bestship->file->captain
+                               [sizeof bestship->file->captain - 1] = 0;
+                       log(bestship);
+               }
+               return -1;
        }
        Write(W_TURN, SHIP(0), 0, turn, 0, 0, 0);
        }
        Write(W_TURN, SHIP(0), 0, turn, 0, 0, 0);
-       if (turn % 7 == 0) {
-               if (die() >= cc->windchange || !windspeed) {
+       if (turn % 7 == 0 && (die() >= cc->windchange || !windspeed)) {
+               switch (die()) {
+               case 1:
+                       winddir = 1;
+                       break;
+               case 2:
+                       break;
+               case 3:
+                       winddir++;
+                       break;
+               case 4:
+                       winddir--;
+                       break;
+               case 5:
+                       winddir += 2;
+                       break;
+               case 6:
+                       winddir -= 2;
+                       break;
+               }
+               if (winddir > 8)
+                       winddir -= 8;
+               if (winddir < 1)
+                       winddir += 8;
+               if (windspeed)
                        switch (die()) {
                        case 1:
                        switch (die()) {
                        case 1:
-                               winddir = 1;
-                               break;
                        case 2:
                        case 2:
-                               break;
-                       case 3:
-                               winddir++;
-                               break;
-                       case 4:
-                               winddir--;
+                               windspeed--;
                                break;
                        case 5:
                                break;
                        case 5:
-                               winddir += 2;
-                               break;
                        case 6:
                        case 6:
-                               winddir -= 2;
+                               windspeed++;
                                break;
                        }
                                break;
                        }
-                       if (winddir > 8)
-                               winddir -= 8;
-                       if (winddir < 1)
-                               winddir += 8;
-                       if (windspeed)
-                               switch (die()) {
-                               case 1:
-                               case 2:
-                                       windspeed--;
-                                       break;
-                               case 5:
-                               case 6:
-                                       windspeed++;
-                                       break;
-                               }
-                       else
-                               windspeed++;
-                       Write(W_WIND, SHIP(0), 0, winddir, windspeed, 0, 0);
-               }
+               else
+                       windspeed++;
+               Write(W_WIND, SHIP(0), 0, winddir, windspeed, 0, 0);
        }
        }
-}
-
-/*VARARGS2*/
-Signal(fmt, ship, a, b, c)
-char *fmt;
-struct ship *ship;
-{
+       return 0;
 }
 }