]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/aircombat.c
Eliminate a few pointless relations variables
[empserver] / src / lib / subs / aircombat.c
index 2b32bfd0215dabc2b352433fc6f786358219d011..2645eb69e507cc192a6e89e1d5ac48730fec8be9 100644 (file)
@@ -62,8 +62,7 @@ static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
 static void ac_combat_headers(natid, natid);
 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
 static void ac_dog(struct plist *, struct plist *);
-static void ac_planedamage(struct plist *, natid, int, natid, int,
-                          int, char *);
+static void ac_planedamage(struct plist *, natid, int, int, char *);
 static void ac_doflak(struct emp_qelem *, struct sctstr *);
 static void ac_landflak(struct emp_qelem *, coord, coord);
 static void ac_shipflak(struct emp_qelem *, coord, coord);
@@ -83,7 +82,6 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     int overfly[MAXNOC];
     int flags;
     struct emp_qelem ilist[MAXNOC];
-    int civ, mil;
     natid plane_owner;
     struct sctstr sect;
     struct shpstr ship;
@@ -142,48 +140,14 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
            flags = pln_caps(bomb_list);
            if (opt_HIDDEN)
                setcont(player->cnum, sect.sct_own, FOUND_FLY);
-           if (sect.sct_type == SCT_WATER) {
+           if (sect.sct_type == SCT_WATER)
                pr("flying over %s at %s\n",
                   dchr[sect.sct_type].d_name, xyas(x, y, player->cnum));
-               if (mission_flags & PM_S)
-                   plane_sweep(bomb_list, x, y);
-               if (flags & P_A)
-                   plane_sona(bomb_list, x, y, &head);
-               changed += map_set(player->cnum,
-                                  sect.sct_x, sect.sct_y,
-                                  dchr[sect.sct_type].d_mnem, 0);
-           } else if (flags & P_S) {
+           else if (flags & P_S)
                satdisp_sect(&sect, flags & P_I ? 10 : 50);
-           } else {
-               /* This is borrowed from lookout */
-               if (sect.sct_own == player->cnum)
-                   pr("Your ");
-               else
-                   pr("%s (#%d) ",
-                      cname(sect.sct_own), sect.sct_own);
-               pr("%s", dchr[sect.sct_type].d_name);
-               changed += map_set(player->cnum,
-                                  sect.sct_x, sect.sct_y,
-                                  dchr[sect.sct_type].d_mnem, 0);
-               pr(" %d%% efficient ",
-                  (sect.sct_own == player->cnum) ?
-                  sect.sct_effic : roundintby((int)sect.sct_effic, 25));
-               civ = sect.sct_item[I_CIVIL];
-               mil = sect.sct_item[I_MILIT];
-               if (civ)
-                   pr("with %s%d civ ",
-                      (sect.sct_own == player->cnum) ?
-                      "" : "approx ",
-                      (sect.sct_own == player->cnum) ?
-                      civ : roundintby(civ, 25));
-               if (mil)
-                   pr("with %s%d mil ",
-                      (sect.sct_own == player->cnum) ?
-                      "" : "approx ",
-                      (sect.sct_own == player->cnum) ?
-                      mil : roundintby(mil, 25));
-               pr("@ %s\n", xyas(x, y, player->cnum));
-           }
+           else
+               look_at_sect(&sect, 25);
+
            if (flags & P_S)
                satdisp_units(sect.sct_x, sect.sct_y);
            else {
@@ -198,12 +162,11 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                           cname(cn), xyas(x, y, player->cnum));
                }
            }
-       } else {
-           PR(plane_owner, "flying over %s at %s\n",
-              dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
-           changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
-                              dchr[sect.sct_type].d_mnem, 0);
-       }
+       } else
+           mpr(plane_owner, "flying over %s at %s\n",
+               dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
+       changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
+                          dchr[sect.sct_type].d_mnem, 0);
 
        evaded = do_evade(bomb_list, esc_list);
        if (!evaded) {
@@ -213,8 +176,8 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                    continue;
                if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
                    continue;
-               PR(cn, "%s planes spotted over %s\n",
-                  cname(plane_owner), xyas(x, y, cn));
+               mpr(cn, "%s planes spotted over %s\n",
+                   cname(plane_owner), xyas(x, y, cn));
                if (opt_HIDDEN)
                    setcont(cn, plane_owner, FOUND_FLY);
            }
@@ -240,6 +203,14 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
            }
        }
 
+       if (mission_flags & PM_R) {
+           flags = pln_caps(bomb_list);
+           if (sect.sct_type == SCT_WATER && mission_flags & PM_S)
+               plane_sweep(bomb_list, x, y);
+           if (sect.sct_type == SCT_WATER && flags & P_A)
+               plane_sona(bomb_list, x, y, &head);
+       }
+
        dir = *path++;
        if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
            break;
@@ -256,7 +227,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     }
     /* If the map changed, update it */
     if (changed)
-       writemap(player->cnum);
+       writemap(plane_owner);
 
     free_shiplist(&head);
     for (cn = 1; cn < MAXNOC; cn++)
@@ -307,9 +278,9 @@ sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
            putplane(pp->pln_uid, pp);
            if (first) {
                first = 0;
-               PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
-               PR(def_own, "Launching SAMs at %s planes over %s!\n",
-                  cname(plane_owner), xyas(x, y, def_own));
+               mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
+               mpr(def_own, "Launching SAMs at %s planes over %s!\n",
+                   cname(plane_owner), xyas(x, y, def_own));
                ac_combat_headers(plane_owner, def_own);
            }
            ac_dog(aplp, dplp);
@@ -318,8 +289,8 @@ sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
        }
     }
     if (!first) {
-       PR(plane_owner, "\n");
-       PR(def_own, "\n");
+       mpr(plane_owner, "\n");
+       mpr(def_own, "\n");
     }
 }
 
@@ -389,28 +360,28 @@ ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     }
     if (icount == 0)
        return;
-    PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
-       icount, cname(def_own), icount == 1 ? " is" : "s are");
-    PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
-       icount, icount == 1 ? " is" : "s are", cname(plane_owner),
-       xyas(x, y, def_own));
+    mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
+       icount, cname(def_own), icount == 1 ? " is" : "s are");
+    mpr(def_own, "%d fighter%s intercepting %s planes over %s!\n",
+       icount, icount == 1 ? " is" : "s are", cname(plane_owner),
+       xyas(x, y, def_own));
     ac_combat_headers(plane_owner, def_own);
     ac_airtoair(esc_list, &int_list);
     ac_airtoair(bomb_list, &int_list);
-    PR(plane_owner, "\n");
-    PR(def_own, "\n");
+    mpr(plane_owner, "\n");
+    mpr(def_own, "\n");
     pln_put(&int_list);
 }
 
 static void
 ac_combat_headers(natid plane_owner, natid def_own)
 {
-    PR(plane_owner,
-       " %-10.10s %-10.10s  strength int odds  damage           results\n",
-       cname(plane_owner), cname(def_own));
-    PR(def_own,
-       " %-10.10s %-10.10s  strength int odds  damage           results\n",
-       cname(def_own), cname(plane_owner));
+    mpr(plane_owner,
+       " %-10.10s %-10.10s  strength int odds  damage           results\n",
+       cname(plane_owner), cname(def_own));
+    mpr(def_own,
+       " %-10.10s %-10.10s  strength int odds  damage           results\n",
+       cname(def_own), cname(plane_owner));
 }
 
 /*
@@ -461,27 +432,32 @@ ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
     }
 }
 
+static void
+ac_dog_report(natid to, int intensity, double odds,
+             struct plist *p1, int val1, int dam1, char *dam_mesg1,
+             struct plist *p2, int val2, int dam2, char *dam_mesg2)
+{
+    mpr(to, " %3.3s #%-4d  %3.3s #%-4d   %3d/%-3d %3d  %3.2f  %3d/%-3d"
+       "%-13.13s %-13.13s\n",
+       p1->pcp->pl_name, p1->plane.pln_uid,
+       p2->pcp->pl_name, p2->plane.pln_uid,
+       val1, val2, intensity, odds, dam1, dam2,
+       dam_mesg1, dam_mesg2);
+}
+
 static void
 ac_dog(struct plist *ap, struct plist *dp)
 {
     int att, def;
     double odds;
-    int intensity;
+    int intensity, i;
     natid att_own, def_own;
     int adam, ddam;
-    char mesg[1024];
-    char temp[14];
+    char adam_mesg[14], ddam_mesg[14];
 
     att_own = ap->plane.pln_own;
     def_own = dp->plane.pln_own;
 
-    PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
-       ap->pcp->pl_name,
-       ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
-    if (def_own)
-       PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
-          dp->pcp->pl_name,
-          dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
     att = pln_att(&ap->plane);
     if (att == 0)
        att = pln_def(&ap->plane);
@@ -510,34 +486,31 @@ ac_dog(struct plist *ap, struct plist *dp)
        odds = 0.05;
     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
 
-    PR(att_own, "   %3d/%-3d %3d  %3.2f  ", att, def, intensity, odds);
-    PR(def_own, "   %3d/%-3d %3d  %3.2f  ", def, att, intensity, odds);
-
     adam = 0;
     ddam = 0;
-    while ((intensity--) > 0) {
-
+    for (i = 0; i < intensity; i++) {
        if (chance(odds)) {
-           ddam += 1;
-           if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
-               intensity = 0;
+           ddam++;
+           if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
+               break;
        } else {
-           adam += 1;
-           if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
-               intensity = 0;
+           adam++;
+           if (ap->plane.pln_effic - adam < PLANE_MINEFF)
+               break;
        }
     }
 
     if (dp->pcp->pl_flags & P_M)
        ddam = 100;
 
-    PR(att_own, "%3d/%-3d", adam, ddam);
-    PR(def_own, "%3d/%-3d", ddam, adam);
-    ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
-    strncpy(temp, mesg, 14);
-    ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
-    PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
-    PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
+    ac_planedamage(ap, def_own, adam, 0, adam_mesg);
+    ac_planedamage(dp, att_own, ddam, 0, ddam_mesg);
+    ac_dog_report(att_own, intensity, odds,
+                 ap, att, adam, adam_mesg,
+                 dp, def, ddam, ddam_mesg);
+    ac_dog_report(def_own, intensity, odds,
+                 dp, def, ddam, ddam_mesg,
+                 ap, att, adam, adam_mesg);
 
     if (opt_HIDDEN) {
        setcont(att_own, def_own, FOUND_FLY);
@@ -548,9 +521,6 @@ ac_dog(struct plist *ap, struct plist *dp)
 /*
  * zap plane associated with plp.
  * Damaging country is "from", damage is "dam".
- * def_own is the country on the other side of the conflict from the plane
- * owner. The only time def_own != from is when the interceptor is getting
- * damaged.
  *
  * NOTE: This routine removes the appropriate plane element from the
  * queue if it gets destroyed.  That means that the caller must assume
@@ -558,62 +528,50 @@ ac_dog(struct plist *ap, struct plist *dp)
  * call.  (this has caused bugs in the past)
  */
 static void
-ac_planedamage(struct plist *plp, natid from, int dam, natid other,
-              int checkabort, int show, char *mesg)
+ac_planedamage(struct plist *plp, natid from, int dam, int flak,
+              char *mesg)
 {
-    struct plnstr *pp;
-    int disp;
-    char dmess[255];
-    int eff;
-    natid plane_owner;
+    int disp = ac_damage_plane(&plp->plane, from, dam, flak, mesg);
 
-    disp = 0;
-    pp = &plp->plane;
-    plane_owner = pp->pln_own;
-    eff = pp->pln_effic;
-    sprintf(dmess, " no damage");
+    if (disp) {
+       pln_put1(plp);
+    } else
+       putplane(plp->plane.pln_uid, &plp->plane);
+}
+
+int
+ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
+               char *mesg)
+{
+    int eff, disp;
+
+    *mesg = 0;
     if (dam <= 0) {
-       strcpy(mesg, dmess);
-       return;
+       if (!flak)
+           snprintf(mesg, 14, " no damage");
+       return 0;
     }
-    memset(dmess, 0, sizeof(dmess));
-    eff -= dam;
+
+    eff = pp->pln_effic - dam;
     if (eff < 0)
        eff = 0;
+
+    disp = 0;
     if (eff < PLANE_MINEFF) {
-       sprintf(dmess, " shot down");
+       snprintf(mesg, 14, " shot down");
        disp = 1;
-    } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
-       sprintf(dmess, " aborted @%2d%%", eff);
+    } else if (eff < 80 && chance((80 - eff) / 100.0)) {
+       snprintf(mesg, 14, " aborted @%2d%%", eff);
        disp = 2;
-    } else if (show == 0) {
-       sprintf(dmess, " cleared");
-    }
-
-    if ((plp->pcp->pl_flags & P_M) == 0) {
-       if (show) {
-           PR(plane_owner, "    %s %s takes %d%s.\n",
-              cname(pp->pln_own), prplane(pp), dam, dmess);
-           if (other)
-               PR(other, "    %s %s takes %d%s.\n",
-                  cname(pp->pln_own), prplane(pp), dam, dmess);
-       }
-    }
-    if (show && checkabort == 1) {
-       PR(plane_owner, "\n");
-       if (other)
-           PR(other, "\n");
-    }
+    } else if (!flak)
+       snprintf(mesg, 14, " cleared");
 
     pp->pln_effic = eff;
     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
-    if (disp) {
-       if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
-           nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
-       pln_put1(plp);
-    } else
-       putplane(pp->pln_uid, pp);
-    strcpy(mesg, dmess);
+
+    if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
+       nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
+    return disp;
 }
 
 static void
@@ -629,11 +587,10 @@ ac_doflak(struct emp_qelem *list, struct sctstr *from)
     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
     if (gun > 0) {
-       PR(plane_owner, "firing %d flak guns in %s...\n",
-          gun, xyas(from->sct_x, from->sct_y, plane_owner));
-       if (from->sct_own != 0)
-           PR(from->sct_own, "firing %d flak guns in %s...\n",
-              gun, xyas(from->sct_x, from->sct_y, from->sct_own));
+       mpr(plane_owner, "firing %d flak guns in %s...\n",
+           gun, xyas(from->sct_x, from->sct_y, plane_owner));
+       mpr(from->sct_own, "firing %d flak guns in %s...\n",
+           gun, xyas(from->sct_x, from->sct_y, from->sct_own));
        ac_fireflak(list, from->sct_own, gun);
     }
 }
@@ -646,7 +603,6 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
     struct mchrstr *mcp;
     double flak, total, ngun;
     int gun;
-    int rel;
     struct plist *plp;
     natid plane_owner;
     natid from;
@@ -662,8 +618,7 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
        mcp = &mchr[(int)ship.shp_type];
        if (mcp->m_flags & M_SUB)
            continue;
-       rel = getrel(getnatp(ship.shp_own), plane_owner);
-       if (rel > HOSTILE)
+       if (getrel(getnatp(ship.shp_own), plane_owner) > HOSTILE)
            continue;
        gun = shp_usable_guns(&ship);
        if (gun == 0)
@@ -672,8 +627,8 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
        ngun += flak;
        total += techfact(ship.shp_tech, flak * 2.0);
 
-       PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
-          flak, prship(&ship));
+       mpr(ship.shp_own, "firing %.0f flak guns from %s...\n",
+           flak, prship(&ship));
        from = ship.shp_own;
     }
 
@@ -683,7 +638,7 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
 
     gun = roundavg(total);
     if (gun > 0) {
-       PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
+       mpr(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
        ac_fireflak(list, from, gun);
     }
 }
@@ -696,7 +651,6 @@ ac_landflak(struct emp_qelem *list, coord x, coord y)
     struct lchrstr *lcp;
     double flak, total, ngun;
     int aaf, gun;
-    int rel;
     struct plist *plp;
     natid plane_owner;
     natid from;
@@ -715,15 +669,14 @@ ac_landflak(struct emp_qelem *list, coord x, coord y)
            continue;
        if (land.lnd_ship >= 0 || land.lnd_land >= 0)
            continue;
-       rel = getrel(getnatp(land.lnd_own), plane_owner);
-       if (rel > HOSTILE)
+       if (getrel(getnatp(land.lnd_own), plane_owner) > HOSTILE)
            continue;
        flak = aaf * 1.5 * land.lnd_effic / 100.0;
        ngun += flak;
        total += techfact(land.lnd_tech, flak * 2.0);
 
-       PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
-          prland(&land), aaf);
+       mpr(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
+           prland(&land), aaf);
        from = land.lnd_own;
     }
 
@@ -733,7 +686,7 @@ ac_landflak(struct emp_qelem *list, coord x, coord y)
 
     gun = roundavg(total);
     if (gun > 0) {
-       PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
+       mpr(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
        ac_fireflak(list, from, gun);
     }
 }
@@ -748,13 +701,15 @@ ac_fireflak(struct emp_qelem *list, natid from, int guns)
     int n;
     struct emp_qelem *qp;
     struct emp_qelem *next;
-    char msg[255];
+    char msg[14];
 
     for (qp = list->q_forw; qp != list; qp = next) {
        next = qp->q_forw;
        plp = (struct plist *)qp;
        n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
-       ac_planedamage(plp, from, n, 0, 2, 1, msg);
+       ac_planedamage(plp, from, n, 1, msg);
+       mpr(plp->plane.pln_own, "    %s takes %d%s%s.\n",
+           prplane(&plp->plane), n, *msg ? " --" : "", msg);
     }
 }