]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/aircombat.c
Update copyright notice
[empserver] / src / lib / subs / aircombat.c
index cfe767b7b2772cf6bb15d5d8f932cabd4ebf680a..3d5009c58c2e841f7953c1fcfbe2d105dfed9dc1 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *     Dave Pare, 1986
  *     Thomas Ruschak, 1992
  *     Steve McClure, 1996
- *     Markus Armbruster, 2006-2009
+ *     Markus Armbruster, 2006-2020
  */
 
 #include <config.h>
 
-#include "file.h"
+#include "chance.h"
 #include "land.h"
 #include "map.h"
 #include "misc.h"
@@ -46,6 +45,7 @@
 #include "nsc.h"
 #include "optlist.h"
 #include "path.h"
+#include "plague.h"
 #include "plane.h"
 #include "player.h"
 #include "prototypes.h"
 
 #define FLAK_GUN_MAX 14
 
-static void sam_intercept(struct emp_qelem *, struct emp_qelem *,
-                         natid, natid, coord, coord, int);
 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
                         struct emp_qelem *, natid, coord, coord, int);
 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, int, char *);
+static void ac_putplane(struct plist *, int);
 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);
 static void ac_fireflak(struct emp_qelem *, natid, int);
-static void getilists(struct emp_qelem *, unsigned char *, natid);
+static void getilists(struct emp_qelem *, enum relations[], natid);
 static int do_evade(struct emp_qelem *, struct emp_qelem *);
 
 void
@@ -78,7 +76,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     int dir;
     unsigned char gotships[MAXNOC];
     unsigned char gotlands[MAXNOC];
-    unsigned char rel[MAXNOC];
+    enum relations rel[MAXNOC];
     int overfly[MAXNOC];
     int flags;
     struct emp_qelem ilist[MAXNOC];
@@ -140,19 +138,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);
-           } else if (flags & P_S)
+           else if (flags & P_S)
                satdisp_sect(&sect, flags & P_I ? 10 : 50);
            else
                look_at_sect(&sect, 25);
-           changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
-                              dchr[sect.sct_type].d_mnem, 0);
+
            if (flags & P_S)
                satdisp_units(sect.sct_x, sect.sct_y);
            else {
@@ -167,23 +160,22 @@ 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) {
            overfly[sect.sct_own]++;
            for (cn = 1; cn < MAXNOC; cn++) {
-               if (cn == plane_owner || rel[cn] == ALLIED)
+               if (rel[cn] == ALLIED)
                    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);
            }
@@ -209,6 +201,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;
@@ -218,8 +218,6 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
 
     /* Let's report all of the overflights even if aborted */
     for (cn = 1; cn < MAXNOC; cn++) {
-       if (plane_owner == cn)
-           continue;
        if (overfly[cn] > 0 && rel[cn] != ALLIED)
            nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
     }
@@ -276,9 +274,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);
@@ -287,8 +285,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");
     }
 }
 
@@ -315,11 +313,7 @@ ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
                  only_mission);
 
-    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++;
+    att_count = emp_quelen(bomb_list) + emp_quelen(esc_list);
     if (!att_count)
        return;
 
@@ -358,28 +352,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));
 }
 
 /*
@@ -450,7 +444,7 @@ ac_dog(struct plist *ap, struct plist *dp)
     double odds;
     int intensity, i;
     natid att_own, def_own;
-    int adam, ddam;
+    int adam, ddam, adisp, ddisp;
     char adam_mesg[14], ddam_mesg[14];
 
     att_own = ap->plane.pln_own;
@@ -501,14 +495,16 @@ ac_dog(struct plist *ap, struct plist *dp)
     if (dp->pcp->pl_flags & P_M)
        ddam = 100;
 
-    ac_planedamage(ap, def_own, adam, 0, adam_mesg);
-    ac_planedamage(dp, att_own, ddam, 0, ddam_mesg);
+    adisp = ac_damage_plane(&ap->plane, def_own, adam, 0, adam_mesg);
+    ddisp = ac_damage_plane(&dp->plane, 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);
+    ac_putplane(ap, adisp);
+    ac_putplane(dp, ddisp);
 
     if (opt_HIDDEN) {
        setcont(att_own, def_own, FOUND_FLY);
@@ -516,27 +512,6 @@ ac_dog(struct plist *ap, struct plist *dp)
     }
 }
 
-/*
- * zap plane associated with plp.
- * Damaging country is "from", damage is "dam".
- *
- * NOTE: This routine removes the appropriate plane element from the
- * queue if it gets destroyed.  That means that the caller must assume
- * that the current queue pointer is invalid on return from the ac_planedamage
- * call.  (this has caused bugs in the past)
- */
-static void
-ac_planedamage(struct plist *plp, natid from, int dam, int flak,
-              char *mesg)
-{
-    int disp = ac_damage_plane(&plp->plane, from, dam, flak, mesg);
-
-    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)
@@ -572,6 +547,21 @@ ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
     return disp;
 }
 
+/*
+ * NOTE: This routine may remove the appropriate plane element from the
+ * queue if it gets destroyed.  That means that the caller must assume
+ * that the current queue pointer is invalid on return from the
+ * call.  (this has caused bugs in the past)
+ */
+static void
+ac_putplane(struct plist *plp, int disp)
+{
+    if (disp)
+       pln_put1(plp);
+    else
+       putplane(plp->plane.pln_uid, &plp->plane);
+}
+
 static void
 ac_doflak(struct emp_qelem *list, struct sctstr *from)
 {
@@ -585,11 +575,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);
     }
 }
@@ -602,7 +591,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;
@@ -618,8 +606,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 (relations_with(ship.shp_own, plane_owner) > HOSTILE)
            continue;
        gun = shp_usable_guns(&ship);
        if (gun == 0)
@@ -628,8 +615,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;
     }
 
@@ -639,7 +626,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);
     }
 }
@@ -652,7 +639,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;
@@ -671,15 +657,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 (relations_with(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;
     }
 
@@ -689,7 +674,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);
     }
 }
@@ -701,7 +686,7 @@ static void
 ac_fireflak(struct emp_qelem *list, natid from, int guns)
 {
     struct plist *plp;
-    int n;
+    int n, disp;
     struct emp_qelem *qp;
     struct emp_qelem *next;
     char msg[14];
@@ -710,9 +695,10 @@ ac_fireflak(struct emp_qelem *list, natid from, int guns)
        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, 1, msg);
-       PR(plp->plane.pln_own, "    %s takes %d%s%s.\n",
-          prplane(&plp->plane), n, *msg ? " --" : "", msg);
+       disp = ac_damage_plane(&plp->plane, from, n, 1, msg);
+       mpr(plp->plane.pln_own, "    %s takes %d%s%s.\n",
+           prplane(&plp->plane), n, *msg ? " --" : "", msg);
+       ac_putplane(plp, disp);
     }
 }
 
@@ -730,7 +716,7 @@ ac_flak_dam(int guns, int def, int pl_flags)
         0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
     /*    +5    +6     +7     +8    >+8 */
         0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
-    enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
+    enum { FLAK_MAX = ARRAY_SIZE(flaktable) - 1 };
 
     flak = guns - def;
     if ((pl_flags & P_T) == 0)
@@ -755,7 +741,7 @@ ac_flak_dam(int guns, int def, int pl_flags)
  * Get planes available for interception duties.
  */
 static void
-getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
+getilists(struct emp_qelem *list, enum relations rel[], natid intruder)
 {
     natid cn;
     struct plchrstr *pcp;
@@ -765,7 +751,7 @@ getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
 
     rel[0] = NEUTRAL;
     for (cn = 1; cn < MAXNOC; cn++) {
-       rel[cn] = getrel(getnatp(cn), intruder);
+       rel[cn] = relations_with(cn, intruder);
        emp_initque(&list[cn]);
     }
 
@@ -784,11 +770,16 @@ getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
            continue;
        if (plane.pln_effic < 40)
            continue;
+       if (opt_MARKET) {
+           if (ontradingblock(EF_PLANE, &plane))
+               continue;
+       }
        if (!pln_airbase_ok(&plane, 0, 0))
            continue;
        /* got one! */
        ip = malloc(sizeof(*ip));
        ip->load = 0;
+       ip->pstage = PLG_HEALTHY;
        ip->pcp = &plchr[(int)plane.pln_type];
        ip->plane = plane;
        emp_insque(&ip->queue, &list[plane.pln_own]);