Next step towards structure merge.
[super-star-trek.git] / ai.c
diff --git a/ai.c b/ai.c
index 8f4bb988e56eeece0d3c964b7902b66d8deb1094..e34eabcfd58a0e6cb242f61fbae5ef7314deeade 100644 (file)
--- a/ai.c
+++ b/ai.c
@@ -6,14 +6,14 @@ static int tryexit(int lookx, int looky, int ienm, int loccom, int irun) {
        iqx = quadx+(lookx+9)/10 - 1;\r
        iqy = quady+(looky+9)/10 - 1;\r
        if (iqx < 1 || iqx > 8 || iqy < 1 || iqy > 8 ||\r
-               d.galaxy[iqx][iqy] > 899)\r
+               state.galaxy[iqx][iqy] > 899)\r
                return 0; /* no can do -- neg energy, supernovae, or >8 Klingons */\r
        if (ienm == IHR) return 0; /* Romulans cannot escape! */\r
        if (irun == 0) {\r
                /* avoid intruding on another commander's territory */\r
                if (ienm == IHC) {\r
-                       for (l = 1; l <= d.remcom; l++)\r
-                               if (d.cx[l]==iqx && d.cy[l]==iqy) return 0;\r
+                       for (l = 1; l <= state.remcom; l++)\r
+                               if (state.cx[l]==iqx && state.cy[l]==iqy) return 0;\r
                        /* refuse to leave if currently attacking starbase */\r
                        if (batx==quadx && baty==quady) return 0;\r
                }\r
@@ -40,23 +40,23 @@ static int tryexit(int lookx, int looky, int ienm, int loccom, int irun) {
        nenhere--;\r
        if (condit != IHDOCKED) newcnd();\r
        /* Handle global matters related to escape */\r
-       d.galaxy[quadx][quady] -= 100;\r
-       d.galaxy[iqx][iqy] += 100;\r
+       state.galaxy[quadx][quady] -= 100;\r
+       state.galaxy[iqx][iqy] += 100;\r
        if (ienm==IHS) {\r
                ishere=0;\r
                iscate=0;\r
                ientesc=0;\r
                isatb=0;\r
-               future[FSCMOVE]=0.2777+d.date;\r
+               future[FSCMOVE]=0.2777+state.date;\r
                future[FSCDBAS]=1e30;\r
-               d.isx=iqx;\r
-               d.isy=iqy;\r
+               state.isx=iqx;\r
+               state.isy=iqy;\r
        }\r
        else {\r
-               for (l=1; l<=d.remcom; l++) {\r
-                       if (d.cx[l]==quadx && d.cy[l]==quady) {\r
-                               d.cx[l]=iqx;\r
-                               d.cy[l]=iqy;\r
+               for (l=1; l<=state.remcom; l++) {\r
+                       if (state.cx[l]==quadx && state.cy[l]==quady) {\r
+                               state.cx[l]=iqx;\r
+                               state.cy[l]=iqy;\r
                                break;\r
                        }\r
                }\r
@@ -311,18 +311,18 @@ static int checkdest(int iqx, int iqy, int flag, int *ipage) {
 \r
        if ((iqx==quadx && iqy==quady) ||\r
                iqx < 1 || iqx > 8 || iqy < 1 || iqy > 8 ||\r
-               d.galaxy[iqx][iqy] > 899) return 1;\r
+               state.galaxy[iqx][iqy] > 899) return 1;\r
        if (flag) {\r
                /* Avoid quadrants with bases if we want to avoid Enterprise */\r
-               for (i = 1; i <= d.rembase; i++)\r
-                       if (d.baseqx[i]==iqx && d.baseqy[i]==iqy) return 1;\r
+               for (i = 1; i <= state.rembase; i++)\r
+                       if (state.baseqx[i]==iqx && state.baseqy[i]==iqy) return 1;\r
        }\r
 \r
        /* do the move */\r
-       d.galaxy[d.isx][d.isy] -= 100;\r
-       d.isx = iqx;\r
-       d.isy = iqy;\r
-       d.galaxy[d.isx][d.isy] += 100;\r
+       state.galaxy[state.isx][state.isy] -= 100;\r
+       state.isx = iqx;\r
+       state.isy = iqy;\r
+       state.galaxy[state.isx][state.isy] += 100;\r
        if (iscate) {\r
                /* SC has scooted, Remove him from current quadrant */\r
                iscate=0;\r
@@ -345,17 +345,17 @@ static int checkdest(int iqx, int iqy, int flag, int *ipage) {
        }\r
        /* check for a helpful planet */\r
        for (i = 1; i <= inplan; i++) {\r
-               if (d.plnets[i].x==d.isx && d.plnets[i].y==d.isy &&\r
-                       d.plnets[i].crystals == 1) {\r
+               if (state.plnets[i].x==state.isx && state.plnets[i].y==state.isy &&\r
+                       state.plnets[i].crystals == 1) {\r
                        /* destroy the planet */\r
-                       d.plnets[i] = nulplanet;\r
-                       d.newstuf[d.isx][d.isy] -= 1;\r
+                       state.plnets[i] = nulplanet;\r
+                       state.newstuf[state.isx][state.isy] -= 1;\r
                        if (damage[DRADIO] == 0.0 || condit == IHDOCKED) {\r
                                if (*ipage==0) pause(1);\r
                                *ipage = 1;\r
                                prout("Lt. Uhura-  \"Captain, Starfleet Intelligence reports");\r
                                proutn("   a planet in");\r
-                               cramlc(1, d.isx, d.isy);\r
+                               cramlc(1, state.isx, state.isy);\r
                                prout(" has been destroyed");\r
                                prout("   by the Super-commander.\"");\r
                        }\r
@@ -380,39 +380,39 @@ void scom(int *ipage) {
 #endif\r
 \r
        /* Decide on being active or passive */\r
-       flag = ((d.killc+d.killk)/(d.date+0.01-indate) < 0.1*skill*(skill+1.0) ||\r
-                       (d.date-indate) < 3.0);\r
+       flag = ((state.killc+state.killk)/(state.date+0.01-indate) < 0.1*skill*(skill+1.0) ||\r
+                       (state.date-indate) < 3.0);\r
        if (iscate==0 && flag) {\r
                /* compute move away from Enterprise */\r
-               ideltax = d.isx-quadx;\r
-               ideltay = d.isy-quady;\r
+               ideltax = state.isx-quadx;\r
+               ideltay = state.isy-quady;\r
                if (sqrt(ideltax*(double)ideltax+ideltay*(double)ideltay) > 2.0) {\r
                        /* circulate in space */\r
-                       ideltax = d.isy-quady;\r
-                       ideltay = quadx-d.isx;\r
+                       ideltax = state.isy-quady;\r
+                       ideltay = quadx-state.isx;\r
                }\r
        }\r
        else {\r
                /* compute distances to starbases */\r
-               if (d.rembase <= 0) {\r
+               if (state.rembase <= 0) {\r
                        /* nothing left to do */\r
                        future[FSCMOVE] = 1e30;\r
                        return;\r
                }\r
-               sx = d.isx;\r
-               sy = d.isy;\r
-               for (i = 1; i <= d.rembase; i++) {\r
+               sx = state.isx;\r
+               sy = state.isy;\r
+               for (i = 1; i <= state.rembase; i++) {\r
                        basetbl[i] = i;\r
-                       ibqx = d.baseqx[i];\r
-                       ibqy = d.baseqy[i];\r
+                       ibqx = state.baseqx[i];\r
+                       ibqy = state.baseqy[i];\r
                        bdist[i] = sqrt(square(ibqx-sx) + square(ibqy-sy));\r
                }\r
-               if (d.rembase > 1) {\r
+               if (state.rembase > 1) {\r
                        /* sort into nearest first order */\r
                        int iswitch;\r
                        do {\r
                                iswitch = 0;\r
-                               for (i=1; i < d.rembase-1; i++) {\r
+                               for (i=1; i < state.rembase-1; i++) {\r
                                        if (bdist[i] > bdist[i+1]) {\r
                                                int ti = basetbl[i];\r
                                                double t = bdist[i];\r
@@ -429,34 +429,34 @@ void scom(int *ipage) {
                   without too many Klingons, and not already under attack. */\r
                ifindit = iwhichb = 0;\r
 \r
-               for (i2 = 1; i2 <= d.rembase; i2++) {\r
+               for (i2 = 1; i2 <= state.rembase; i2++) {\r
                        i = basetbl[i2];        /* bug in original had it not finding nearest*/\r
-                       ibqx = d.baseqx[i];\r
-                       ibqy = d.baseqy[i];\r
+                       ibqx = state.baseqx[i];\r
+                       ibqy = state.baseqy[i];\r
                        if ((ibqx == quadx && ibqy == quady) ||\r
                                (ibqx == batx && ibqy == baty) ||\r
-                               d.galaxy[ibqx][ibqy] > 899) continue;\r
+                               state.galaxy[ibqx][ibqy] > 899) continue;\r
                        /* if there is a commander, an no other base is appropriate,\r
                           we will take the one with the commander */\r
-                       for (j = 1; j <= d.remcom; j++) {\r
-                               if (ibqx==d.cx[j] && ibqy==d.cy[j] && ifindit!= 2) {\r
+                       for (j = 1; j <= state.remcom; j++) {\r
+                               if (ibqx==state.cx[j] && ibqy==state.cy[j] && ifindit!= 2) {\r
                                                ifindit = 2;\r
                                                iwhichb = i;\r
                                                break;\r
                                }\r
                        }\r
-                       if (j > d.remcom) { /* no commander -- use this one */\r
+                       if (j > state.remcom) { /* no commander -- use this one */\r
                                ifindit = 1;\r
                                iwhichb = i;\r
                                break;\r
                        }\r
                }\r
                if (ifindit==0) return; /* Nothing suitable -- wait until next time*/\r
-               ibqx = d.baseqx[iwhichb];\r
-               ibqy = d.baseqy[iwhichb];\r
+               ibqx = state.baseqx[iwhichb];\r
+               ibqy = state.baseqy[iwhichb];\r
                /* decide how to move toward base */\r
-               ideltax = ibqx - d.isx;\r
-               ideltay = ibqy - d.isy;\r
+               ideltax = ibqx - state.isx;\r
+               ideltay = ibqy - state.isy;\r
        }\r
        /* Maximum movement is 1 quadrant in either or both axis */\r
        if (ideltax > 1) ideltax = 1;\r
@@ -465,58 +465,58 @@ void scom(int *ipage) {
        if (ideltay < -1) ideltay = -1;\r
 \r
        /* try moving in both x and y directions */\r
-       iqx = d.isx + ideltax;\r
-       iqy = d.isy + ideltax;\r
+       iqx = state.isx + ideltax;\r
+       iqy = state.isy + ideltax;\r
        if (checkdest(iqx, iqy, flag, ipage)) {\r
                /* failed -- try some other maneuvers */\r
                if (ideltax==0 || ideltay==0) {\r
                        /* attempt angle move */\r
                        if (ideltax != 0) {\r
-                               iqy = d.isy + 1;\r
+                               iqy = state.isy + 1;\r
                                if (checkdest(iqx, iqy, flag, ipage)) {\r
-                                       iqy = d.isy - 1;\r
+                                       iqy = state.isy - 1;\r
                                        checkdest(iqx, iqy, flag, ipage);\r
                                }\r
                        }\r
                        else {\r
-                               iqx = d.isx + 1;\r
+                               iqx = state.isx + 1;\r
                                if (checkdest(iqx, iqy, flag, ipage)) {\r
-                                       iqx = d.isx - 1;\r
+                                       iqx = state.isx - 1;\r
                                        checkdest(iqx, iqy, flag, ipage);\r
                                }\r
                        }\r
                }\r
                else {\r
                        /* try moving just in x or y */\r
-                       iqy = d.isy;\r
+                       iqy = state.isy;\r
                        if (checkdest(iqx, iqy, flag, ipage)) {\r
-                               iqy = d.isy + ideltay;\r
-                               iqx = d.isx;\r
+                               iqy = state.isy + ideltay;\r
+                               iqx = state.isx;\r
                                checkdest(iqx, iqy, flag, ipage);\r
                        }\r
                }\r
        }\r
        /* check for a base */\r
-       if (d.rembase == 0) {\r
+       if (state.rembase == 0) {\r
                future[FSCMOVE] = 1e30;\r
        }\r
-       else for (i=1; i<=d.rembase; i++) {\r
-               ibqx = d.baseqx[i];\r
-               ibqy = d.baseqy[i];\r
-               if (ibqx==d.isx && ibqy == d.isy && d.isx != batx && d.isy != baty) {\r
+       else for (i=1; i<=state.rembase; i++) {\r
+               ibqx = state.baseqx[i];\r
+               ibqy = state.baseqy[i];\r
+               if (ibqx==state.isx && ibqy == state.isy && state.isx != batx && state.isy != baty) {\r
                        /* attack the base */\r
                        if (flag) return; /* no, don't attack base! */\r
                        iseenit = 0;\r
                        isatb=1;\r
-                       future[FSCDBAS] = d.date + 1.0 +2.0*Rand();\r
-                       if (batx != 0) future[FSCDBAS] += future[FCDBAS]-d.date;\r
+                       future[FSCDBAS] = state.date + 1.0 +2.0*Rand();\r
+                       if (batx != 0) future[FSCDBAS] += future[FCDBAS]-state.date;\r
                        if (damage[DRADIO] > 0 && condit != IHDOCKED)\r
                                return; /* no warning */\r
                        iseenit = 1;\r
                        if (*ipage == 0)  pause(1);\r
                        *ipage=1;\r
                        proutn("Lt. Uhura-  \"Captain, the starbase in");\r
-                       cramlc(1, d.isx, d.isy);\r
+                       cramlc(1, state.isx, state.isy);\r
                        skip(1);\r
                        prout("   reports that it is under attack from the Klingon Super-commander.");\r
                        proutn("   It can survive until stardate ");\r
@@ -537,13 +537,13 @@ void scom(int *ipage) {
 #endif\r
                (Rand() > 0.2 ||\r
                 (damage[DRADIO] > 0.0 && condit != IHDOCKED) ||\r
-                starch[d.isx][d.isy] > 0))\r
+                starch[state.isx][state.isy] > 0))\r
                return;\r
        if (*ipage==0) pause(1);\r
        *ipage = 1;\r
        prout("Lt. Uhura-  \"Captain, Starfleet Intelligence reports");\r
        proutn("   the Super-commander is in");\r
-       cramlc(1, d.isx, d.isy);\r
+       cramlc(1, state.isx, state. isy);\r
        prout(".\"");\r
        return;\r
 }\r