]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - trek/phaser.c
robots: Use arc4random_uniform for better uniform distribution
[bsdgames-darwin.git] / trek / phaser.c
index 7ac3b8b6341f2d71f01361b166d4515a8dcc0bce..6328ee5a265385043bfe65a1eab28885b4c0b8cb 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: phaser.c,v 1.11 2009/05/24 19:18:44 dholland Exp $     */
+/*     $NetBSD: phaser.c,v 1.15 2009/08/12 08:54:54 dholland Exp $     */
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)phaser.c   8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: phaser.c,v 1.11 2009/05/24 19:18:44 dholland Exp $");
+__RCSID("$NetBSD: phaser.c,v 1.15 2009/08/12 08:54:54 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -45,11 +45,11 @@ __RCSID("$NetBSD: phaser.c,v 1.11 2009/05/24 19:18:44 dholland Exp $");
 
 /* factors for phaser hits; see description below */
 
-# define       ALPHA           3.0             /* spread */
-# define       BETA            3.0             /* franf() */
-# define       GAMMA           0.30            /* cos(angle) */
-# define       EPSILON         150.0           /* dist ** 2 */
-# define       OMEGA           10.596          /* overall scaling factor */
+#define ALPHA          3.0             /* spread */
+#define BETA           3.0             /* franf() */
+#define GAMMA          0.30            /* cos(angle) */
+#define EPSILON                150.0           /* dist ** 2 */
+#define OMEGA          10.596          /* overall scaling factor */
 
 /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
 
@@ -76,15 +76,13 @@ __RCSID("$NetBSD: phaser.c,v 1.11 2009/05/24 19:18:44 dholland Exp $");
 **     Uses trace flag 30
 */
 
-struct cvntab  Matab[] =
-{
+static struct cvntab Matab[] = {
        { "m",          "anual",        (cmdfun) 1,     0 },
        { "a",          "utomatic",     (cmdfun) 0,     0 },
        { NULL,         NULL,           NULL,           0 }
 };
 
-struct banks
-{
+struct banks {
        int     units;
        double  angle;
        double  spread;
@@ -122,39 +120,32 @@ phaser(int v __unused)
                printf("Sulu: Captain, we cannot fire through shields.\n");
                return;
        }
-       if (Ship.cloaked)
-       {
-               printf("Sulu: Captain, surely you must realize that we cannot fire\n");
+       if (Ship.cloaked) {
+               printf("Sulu: Captain, surely you must realize that we cannot "
+                      "fire\n");
                printf("  phasers with the cloaking device up.\n");
                return;
        }
 
        /* decide if we want manual or automatic mode */
        manual = 0;
-       if (testnl())
-       {
-               if (damaged(COMPUTER))
-               {
+       if (testnl()) {
+               if (damaged(COMPUTER)) {
                        printf("%s", Device[COMPUTER].name);
                        manual++;
+               } else if (damaged(SRSCAN)) {
+                       printf("%s", Device[SRSCAN].name);
+                       manual++;
                }
-               else
-                       if (damaged(SRSCAN))
-                       {
-                               printf("%s", Device[SRSCAN].name);
-                               manual++;
-                       }
                if (manual)
                        printf(" damaged, manual mode selected\n");
        }
 
-       if (!manual)
-       {
+       if (!manual) {
                ptr = getcodpar("Manual or automatic", Matab);
                manual = (long) ptr->value;
        }
-       if (!manual && damaged(COMPUTER))
-       {
+       if (!manual && damaged(COMPUTER)) {
                printf("Computer damaged, manual selected\n");
                skiptonl(0);
                manual++;
@@ -164,16 +155,13 @@ phaser(int v __unused)
        flag = 1;
        for (i = 0; i < NBANKS; i++)
                bank[i].units = 0;
-       if (manual)
-       {
+       if (manual) {
                /* collect manual mode statistics */
-               while (flag)
-               {
+               while (flag) {
                        printf("%d units available\n", Ship.energy);
                        extra = 0;
                        flag = 0;
-                       for (i = 0; i < NBANKS; i++)
-                       {
+                       for (i = 0; i < NBANKS; i++) {
                                b = &bank[i];
                                printf("\nBank %d:\n", i);
                                hit = getintpar("units");
@@ -182,8 +170,7 @@ phaser(int v __unused)
                                if (hit == 0)
                                        break;
                                extra += hit;
-                               if (extra > Ship.energy)
-                               {
+                               if (extra > Ship.energy) {
                                        printf("available energy exceeded.  ");
                                        skiptonl(0);
                                        flag++;
@@ -201,23 +188,20 @@ phaser(int v __unused)
                        Ship.energy -= extra;
                }
                extra = 0;
-       }
-       else
-       {
+       } else {
                /* automatic distribution of power */
                if (Etc.nkling <= 0) {
-                       printf("Sulu: But there are no Klingons in this quadrant\n");
+                       printf("Sulu: But there are no Klingons in this "
+                              "quadrant\n");
                        return;
                }
                printf("Phasers locked on target.  ");
-               while (flag)
-               {
+               while (flag) {
                        printf("%d units available\n", Ship.energy);
                        hit = getintpar("Units to fire");
                        if (hit <= 0)
                                return;
-                       if (hit > Ship.energy)
-                       {
+                       if (hit > Ship.energy) {
                                printf("available energy exceeded.  ");
                                skiptonl(0);
                                continue;
@@ -229,12 +213,12 @@ phaser(int v __unused)
                        if (n > NBANKS)
                                n = NBANKS;
                        tot = n * (n + 1) / 2;
-                       for (i = 0; i < n; i++)
-                       {
+                       for (i = 0; i < n; i++) {
                                k = &Etc.klingon[i];
                                b = &bank[i];
                                distfactor = k->dist;
-                               anglefactor = ALPHA * BETA * OMEGA / (distfactor * distfactor + EPSILON);
+                               anglefactor = ALPHA * BETA * OMEGA /
+                                       (distfactor * distfactor + EPSILON);
                                anglefactor *= GAMMA;
                                distfactor = k->power;
                                distfactor /= anglefactor;
@@ -244,34 +228,29 @@ phaser(int v __unused)
                                b->angle = atan2(dy, dx);
                                b->spread = 0.0;
                                b->units = ((n - i) / tot) * extra;
-#                              ifdef xTRACE
-                               if (Trace)
-                               {
+#ifdef xTRACE
+                               if (Trace) {
                                        printf("b%d hr%d u%d df%.2f af%.2f\n",
                                                i, hitreqd[i], b->units,
                                                distfactor, anglefactor);
                                }
-#                              endif
+#endif
                                extra -= b->units;
                                hit = b->units - hitreqd[i];
-                               if (hit > 0)
-                               {
+                               if (hit > 0) {
                                        extra += hit;
                                        b->units -= hit;
                                }
                        }
 
                        /* give out any extra energy we might have around */
-                       if (extra > 0)
-                       {
-                               for (i = 0; i < n; i++)
-                               {
+                       if (extra > 0) {
+                               for (i = 0; i < n; i++) {
                                        b = &bank[i];
                                        hit = hitreqd[i] - b->units;
                                        if (hit <= 0)
                                                continue;
-                                       if (hit >= extra)
-                                       {
+                                       if (hit >= extra) {
                                                b->units += extra;
                                                extra = 0;
                                                break;
@@ -285,11 +264,9 @@ phaser(int v __unused)
                }
        }
 
-#      ifdef xTRACE
-       if (Trace)
-       {
-               for (i = 0; i < NBANKS; i++)
-               {
+#ifdef xTRACE
+       if (Trace) {
+               for (i = 0; i < NBANKS; i++) {
                        b = &bank[i];
                        printf("b%d u%d", i, b->units);
                        if (b->units > 0)
@@ -298,22 +275,19 @@ phaser(int v __unused)
                                printf("\n");
                }
        }
-#      endif
+#endif
 
        /* actually fire the shots */
        Move.free = 0;
-       for (i = 0; i < NBANKS; i++)
-       {
+       for (i = 0; i < NBANKS; i++) {
                b = &bank[i];
-               if (b->units <= 0)
-               {
+               if (b->units <= 0) {
                        continue;
                }
                printf("\nPhaser bank %d fires:\n", i);
                n = Etc.nkling;
                k = Etc.klingon;
-               for (j = 0; j < n; j++)
-               {
+               for (j = 0; j < n; j++) {
                        if (b->units <= 0)
                                break;
                        /*
@@ -355,8 +329,7 @@ phaser(int v __unused)
                        dy = k->y - Ship.secty;
                        anglefactor = atan2(dy, dx) - b->angle;
                        anglefactor = cos((anglefactor * b->spread) + GAMMA);
-                       if (anglefactor < 0.0)
-                       {
+                       if (anglefactor < 0.0) {
                                k++;
                                continue;
                        }
@@ -367,8 +340,7 @@ phaser(int v __unused)
                                printf(" at %d,%d", k->x, k->y);
                        printf("\n");
                        b->units -= hit;
-                       if (k->power <= 0)
-                       {
+                       if (k->power <= 0) {
                                killk(k->x, k->y);
                                continue;
                        }