]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/aircombat.c
Remove useless strncpy() in ac_encounter()
[empserver] / src / lib / subs / aircombat.c
index bcac4395973b877b83ff973b19b97035a4e892cd..2c6c0f7611d5f52e0f38ac70ffd3b96ce0a90ab5 100644 (file)
@@ -54,9 +54,9 @@
 
 #define FLAK_GUN_MAX 14
 
+static int plane_caps(struct emp_qelem *);
 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
                         struct emp_qelem *, natid, coord, coord);
-static int all_missiles(struct emp_qelem *);
 static void ac_dog(struct plist *, struct plist *);
 static void ac_planedamage(struct plist *, natid, int, natid, int,
                           int, char *);
@@ -72,7 +72,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
             coord x, coord y, char *path, int mission_flags,
             int no_air_defense)
 {
-    int val, non_missiles;
+    int val;
     int rel;
     int dir;
     int nats[MAXNOC];
@@ -80,9 +80,8 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     int gotilist[MAXNOC];
     int unfriendly[MAXNOC];
     int overfly[MAXNOC];
-    struct emp_qelem ilist[MAXNOC], *qp;
-    char mypath[1024];
-    int myp;
+    int flags;
+    struct emp_qelem ilist[MAXNOC];
     int civ, mil;
     natid plane_owner;
     struct sctstr sect;
@@ -90,12 +89,11 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     struct lndstr land;
     struct nstr_item ni;
     natid cn;
-    struct natstr *over, *mynatp;
+    struct natstr *mynatp;
     struct plist *plp;
     int evaded;
     struct shiplist *head = NULL;
     int changed = 0;
-    int intown = 0;            /* Last owner to intercept */
 /* We want to only intercept once per sector per owner.  So, if we overfly
    a sector, and then overfly some land units or ships, we don't want to
    potentially intercept 3 times. */
@@ -103,9 +101,6 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     plp = (struct plist *)bomb_list->q_forw;
     plane_owner = plp->plane.pln_own;
 
-    strncpy(mypath, path, sizeof(mypath));
-    myp = 0;
-
     memset(overfly, 0, sizeof(overfly));
     memset(gotilist, 0, sizeof(gotilist));
     memset(unfriendly, 0, sizeof(unfriendly));
@@ -120,11 +115,12 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
        unfriendly[cn]++;
     }
     if (mission_flags & PM_R) {
-       if (mission_flags & P_S) {
+       flags = plane_caps(bomb_list);
+       if (flags & P_S) {
            PR(plane_owner, "\nSPY Plane report\n");
            PRdate(plane_owner);
            sathead();
-       } else if (mission_flags & P_A) {
+       } else if (flags & P_A) {
            PR(plane_owner, "\nAnti-Sub Patrol report\n");
        } else {
            PR(plane_owner, "\nReconnaissance report\n");
@@ -132,16 +128,16 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
        }
     }
 
-    while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
+    while ((dir = *path++) && !QEMPTY(bomb_list)) {
        if ((val = diridx(dir)) == DIR_STOP)
            break;
        /* XXX using xnorm is probably bad */
        x = xnorm(x + diroff[val][0]);
        y = ynorm(y + diroff[val][1]);
        getsect(x, y, &sect);
-       over = getnatp(sect.sct_own);
 
        if (mission_flags & PM_R) {
+           flags = plane_caps(bomb_list);
            if (opt_HIDDEN)
                setcont(plane_owner, sect.sct_own, FOUND_FLY);
            if (sect.sct_type == SCT_WATER) {
@@ -149,14 +145,13 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                   dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
                if (mission_flags & PM_S)
                    plane_sweep(bomb_list, x, y);
-               if (mission_flags & P_A) {
+               if (flags & P_A)
                    plane_sona(bomb_list, x, y, &head);
-               }
                changed += map_set(plane_owner,
                                   sect.sct_x, sect.sct_y,
                                   dchr[sect.sct_type].d_mnem, 0);
-           } else if (mission_flags & P_S) {
-               satdisp_sect(&sect, (mission_flags & P_I) ? 10 : 50);
+           } else if (flags & P_S) {
+               satdisp_sect(&sect, flags & P_I ? 10 : 50);
            } else {
                /* This is borrowed from lookout */
                if (sect.sct_own == plane_owner)
@@ -187,7 +182,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                       mil : roundintby(mil, 25));
                PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
            }
-           if (mission_flags & P_S)
+           if (flags & P_S)
                satdisp_units(sect.sct_x, sect.sct_y);
        } else {
            PR(plane_owner, "flying over %s at %s\n",
@@ -195,62 +190,46 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
            changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
                               dchr[sect.sct_type].d_mnem, 0);
        }
-       if ((rel = getrel(over, plane_owner)) == ALLIED)
-           continue;
 
        evaded = do_evade(bomb_list, esc_list);
+       if (evaded)
+           continue;
 
-       if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
-           /* We only show planes overhead if they didn't
-            * evade radar */
+       if (sect.sct_own != 0 && sect.sct_own != plane_owner
+           && getrel(getnatp(sect.sct_own), plane_owner) != ALLIED) {
            overfly[sect.sct_own]++;
            PR(sect.sct_own, "%s planes spotted over %s\n",
               cname(plane_owner), xyas(x, y, sect.sct_own));
            if (opt_HIDDEN)
-               setcont(cn, plane_owner, FOUND_FLY);
+               setcont(sect.sct_own, plane_owner, FOUND_FLY);
        }
 
-       if (!evaded) {
-           /* Fire flak */
-           if (unfriendly[sect.sct_own])
-               ac_doflak(bomb_list, &sect);
-           /* If bombers left, fire flak from units and ships */
-           if (!QEMPTY(bomb_list))
-               ac_landflak(bomb_list, x, y);
-           if (!QEMPTY(bomb_list))
-               ac_shipflak(bomb_list, x, y);
-       }
+       /* Fire flak */
+       if (unfriendly[sect.sct_own])
+           ac_doflak(bomb_list, &sect);
+       /* If bombers left, fire flak from units and ships */
+       if (!QEMPTY(bomb_list))
+           ac_landflak(bomb_list, x, y);
+       if (!QEMPTY(bomb_list))
+           ac_shipflak(bomb_list, x, y);
        /* mission planes aborted due to flak -- don't send escorts */
        if (QEMPTY(bomb_list))
            break;
-       if (!no_air_defense && !evaded)
+
+       if (!no_air_defense)
            air_defense(x, y, plane_owner, bomb_list, esc_list);
 
        if (sect.sct_own == 0 || sect.sct_own == plane_owner)
            continue;
 
-       if (evaded)
-           continue;
-
-       non_missiles = 0;
-       for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
-           struct plist *ip = (struct plist *)qp;
-           if (!(plchr[(int)ip->plane.pln_type].pl_flags & P_M))
-               non_missiles = 1;
-       }
-
-       if (!non_missiles)
-           continue;
-
-       if (unfriendly[sect.sct_own] && !gotilist[sect.sct_own]) {
-           getilist(&ilist[sect.sct_own], sect.sct_own);
-           gotilist[sect.sct_own]++;
+       if (unfriendly[sect.sct_own]) {
+           if (!gotilist[sect.sct_own]) {
+               getilist(&ilist[sect.sct_own], sect.sct_own);
+               gotilist[sect.sct_own]++;
+           }
+           ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
+                        sect.sct_own, x, y);
        }
-       if (rel > HOSTILE)
-           continue;
-       ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
-                    sect.sct_own, x, y);
-       intown = sect.sct_own;
     }
 
     /* Let's report all of the overflights even if aborted */
@@ -289,65 +268,32 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
        if (plane_owner == cn)
            continue;
-       intown = -1;
-       /* Are there ships owned by this country? */
        if (nats[cn] != 0) {
-           /* Yes. */
-           if (cn != 0) {
-               PR(cn, "%s planes spotted over ships in %s\n",
-                  cname(plane_owner), xyas(x, y, cn));
-               if (opt_HIDDEN)
-                   setcont(cn, plane_owner, FOUND_FLY);
-           }
-           if (unfriendly[cn]) {
-               /* They are unfriendly too */
-               if (!gotilist[cn]) {
-                   getilist(&ilist[cn], cn);
-                   gotilist[cn]++;
-               }
-               PR(plane_owner, "Flying over %s ships in %s\n",
-                  cname(cn), xyas(x, y, plane_owner));
-               /* This makes going for ships in harbors tough */
-               if (!evaded) {
-                   /* We already fired flak up above.  Now we intercept again if we haven't already */
-                   /* Flag that we intercepted */
-                   intown = 1;
-                   /* And now intercept again */
-                   ac_intercept(bomb_list, esc_list, &ilist[cn],
-                                cn, x, y);
-               }
-           }
+           PR(plane_owner, "Flying over %s ships in %s\n",
+              cname(cn), xyas(x, y, plane_owner));
+           PR(cn, "%s planes spotted over ships in %s\n",
+              cname(plane_owner), xyas(x, y, cn));
        }
-       /* Are there units owned by this country? */
        if (lnats[cn] != 0) {
-           /* Yes. */
-           if (cn != 0) {
-               PR(cn, "%s planes spotted over land units in %s\n",
-                  cname(plane_owner), xyas(x, y, cn));
-               if (opt_HIDDEN)
-                   setcont(cn, plane_owner, FOUND_FLY);
-           }
-           if (unfriendly[cn]) {
-               /* They are unfriendly too */
+           PR(plane_owner, "Flying over %s land units in %s\n",
+              cname(cn), xyas(x, y, plane_owner));
+           PR(cn, "%s planes spotted over land units in %s\n",
+              cname(plane_owner), xyas(x, y, cn));
+       }
+       if (nats[cn] || lnats[cn]) {
+           if (opt_HIDDEN)
+               setcont(cn, plane_owner, FOUND_FLY);
+           if (unfriendly[cn] && !evaded) {
                if (!gotilist[cn]) {
                    getilist(&ilist[cn], cn);
                    gotilist[cn]++;
                }
-               PR(plane_owner, "Flying over %s land units in %s\n",
-                  cname(cn), xyas(x, y, plane_owner));
-               if (!evaded) {
-                   if (intown == -1) {
-                       /* We haven't intercepted yet, so intercept */
-                       ac_intercept(bomb_list, esc_list, &ilist[cn],
-                                    cn, x, y);
-                   }
-               }
+               ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y);
            }
        }
     }
 out:
-    if (mission_flags & P_A)
-       free_shiplist(&head);
+    free_shiplist(&head);
     for (cn = 1; cn < MAXNOC; cn++) {
        if (gotilist[cn])
            pln_put(&ilist[cn]);
@@ -355,19 +301,19 @@ out:
 }
 
 static int
-count_non_missiles(struct emp_qelem *list)
+plane_caps(struct emp_qelem *list)
 {
     struct emp_qelem *qp;
     struct plist *plp;
-    int att_count = 0;
+    int fl;
 
-    /* don't intercept missiles */
+    fl = 0;
     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
        plp = (struct plist *)qp;
-       if (!(plp->pcp->pl_flags & P_M))
-           att_count++;
+       fl |= plp->pcp->pl_flags;
     }
-    return att_count;
+
+    return fl;
 }
 
 void
@@ -388,8 +334,6 @@ sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
         aqp != att_list && dqp != def_list; aqp = anext) {
        anext = aqp->q_forw;
        aplp = (struct plist *)aqp;
-       if (aplp->pcp->pl_flags & P_M)
-           continue;
        if (aplp->pcp->pl_cost < 1000)
            continue;
        for (; dqp != def_list; dqp = dnext) {
@@ -399,11 +343,8 @@ sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
                continue;
 
            if (dplp->plane.pln_range <
-               mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
-               emp_remque(dqp);
-               free(dqp);
+               mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y))
                continue;
-           }
            if (CANT_HAPPEN(dplp->plane.pln_flags & PLN_LAUNCHED)
                || mission_pln_equip(dplp, 0, P_F, 0) < 0) {
                emp_remque(dqp);
@@ -458,15 +399,19 @@ ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     plp = (struct plist *)bomb_list->q_forw;
     plane_owner = plp->plane.pln_own;
 
-    icount = 0;
-
     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
-    sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
-    if (!(att_count = count_non_missiles(bomb_list) +
-         count_non_missiles(esc_list)))
+    sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 0);
+
+    att_count = 0;
+    for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
+       att_count++;
+    for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
+       att_count++;
+    if (!att_count)
        return;
 
     emp_initque(&int_list);
+    icount = 0;
     for (qp = def_list->q_forw; qp != def_list; qp = next) {
        next = qp->q_forw;
        plp = (struct plist *)qp;
@@ -547,21 +492,6 @@ ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
        in_next = in->q_forw;
        att_next = att->q_forw;
        attacker = (struct plist *)att;
-
-       /* skip missiles. If only missiles left, we're done */
-       if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
-           att = att_next;
-           if (att == att_list) {
-               more_att = 0;
-               if (QEMPTY(att_list))
-                   more_int = 0;
-               else
-                   att = att->q_forw;
-           }
-           if (all_missiles(att_list))
-               more_att = 0;
-           continue;
-       }
        interceptor = (struct plist *)in;
        nplanes = attacker->plane.pln_effic;
        if (nplanes > interceptor->plane.pln_effic)
@@ -586,23 +516,6 @@ ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
     }
 }
 
-static int
-all_missiles(struct emp_qelem *att_list)
-{
-    struct emp_qelem *qp;
-    struct plist *p;
-
-    qp = att_list->q_forw;
-    while (qp != att_list) {
-       p = (struct plist *)qp;
-       if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
-           return 0;
-
-       qp = qp->q_forw;
-    }
-    return 1;
-}
-
 static void
 ac_dog(struct plist *ap, struct plist *dp)
 {