]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/aircombat.c
Update copyright notice.
[empserver] / src / lib / subs / aircombat.c
index b888b97c33405425c53f604ac0b12edad9d170e8..b513e5cad1c2721016f62516feaac22196674749 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *     Dave Pare, 1986
  *     Thomas Ruschak, 1992
  *     Steve McClure, 1996
+ *     Markus Armbruster, 2006
  */
 
-#include <string.h>
+#include <config.h>
 
-#include "misc.h"
-#include "player.h"
-#include "var.h"
-#include "news.h"
+#include "file.h"
 #include "land.h"
-#include "sect.h"
-#include "nuke.h"
-#include "plane.h"
-#include "ship.h"
+#include "map.h"
+#include "misc.h"
 #include "nat.h"
-#include "file.h"
-#include "xy.h"
+#include "news.h"
 #include "nsc.h"
+#include "optlist.h"
 #include "path.h"
+#include "plane.h"
+#include "player.h"
 #include "prototypes.h"
-#include "optlist.h"
+#include "sect.h"
 #include "server.h"
+#include "ship.h"
+#include "xy.h"
 
-
-#ifndef MAX
-#define MAX(x,y) ((x) < (y) ? (y) : (x))
-#endif
-
-static void getilist(struct emp_qelem *list, natid own,
-                    struct emp_qelem *a, struct emp_qelem *b,
-                    struct emp_qelem *c, struct emp_qelem *d);
-static void ac_dog(register struct plist *ap, register struct plist *dp);
-
-#define FLAK_MAX        15
-
-               /*       -7    -6    -5    -4    -3    -2    -1    0 */
-float flaktable[16] = { 0.20, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50,
-    0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85,
-};
+#define FLAK_GUN_MAX 14
+
+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 *);
+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 getilist(struct emp_qelem *, natid,
+                    struct emp_qelem *, struct emp_qelem *,
+                    struct emp_qelem *, struct emp_qelem *);
+static int can_fly(int);
+static int do_evade(struct emp_qelem *, struct emp_qelem *);
 
 void
 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
-            coord x, coord y, s_char *path, int mission_flags,
+            coord x, coord y, char *path, int mission_flags,
             int no_air_defense, struct emp_qelem *obomb,
             struct emp_qelem *oesc)
 {
@@ -85,7 +86,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     int unfriendly[MAXNOC];
     int overfly[MAXNOC];
     struct emp_qelem ilist[MAXNOC], *qp;
-    s_char mypath[1024];
+    char mypath[1024];
     int myp;
     int civ, mil;
     natid plane_owner;
@@ -97,18 +98,13 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     struct natstr *over, *mynatp;
     struct plist *plp;
     int evaded;
-    struct shiplook head;
-    struct shiplook *s, *s2;
+    struct shiplist *head = NULL;
     int changed = 0;
-    int intx, inty;            /* Last sector planes intercepted over */
     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. */
 
-    memset(&head, 0, sizeof(struct shiplook));
-    head.uid = -1;
-
     plp = (struct plist *)bomb_list->q_forw;
     plane_owner = plp->plane.pln_own;
 
@@ -143,18 +139,17 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
 
     pln_removedupes(bomb_list, esc_list);
     while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
-       if ((val = chkdir(dir, DIR_STOP, DIR_LAST)) == 0)
+       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 (opt_HIDDEN) {
-           setcont(plane_owner, sect.sct_own, FOUND_FLY);
-       }
 
        if (mission_flags & PM_R) {
+           if (opt_HIDDEN)
+               setcont(plane_owner, sect.sct_own, FOUND_FLY);
            if (sect.sct_type == SCT_WATER) {
                PR(plane_owner, "flying over %s at %s\n",
                   dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
@@ -167,15 +162,15 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                                   sect.sct_x, sect.sct_y,
                                   dchr[sect.sct_type].d_mnem, 0);
            } else if (mission_flags & P_S) {
-               satdisp(&sect, (mission_flags & P_I) ? 10 : 50, 1);
+               satdisp_sect(&sect, (mission_flags & P_I) ? 10 : 50);
            } else {
                /* This is borrowed from lookout */
                if (sect.sct_own == plane_owner)
                    PR(plane_owner, "Your ");
                else
-                   PR(plane_owner,
-                      "%s (#%d) ", cname(sect.sct_own), sect.sct_own);
-               PR(plane_owner, dchr[sect.sct_type].d_name);
+                   PR(plane_owner, "%s (#%d) ",
+                      cname(sect.sct_own), sect.sct_own);
+               PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
                changed += map_set(plane_owner,
                                   sect.sct_x, sect.sct_y,
                                   dchr[sect.sct_type].d_mnem, 0);
@@ -198,23 +193,27 @@ 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)
+               satdisp_units(sect.sct_x, sect.sct_y);
        } 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);
+           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 (sect.sct_own != 0 && sect.sct_own != plane_owner && (!evaded)) {
+       if (sect.sct_own != 0 && sect.sct_own != plane_owner && !evaded) {
            /* We only show planes overhead if they didn't
             * evade radar */
            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);
        }
 
        if (!evaded) {
@@ -230,7 +229,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
        /* mission planes aborted due to flak -- don't send escorts */
        if (QEMPTY(bomb_list))
            break;
-       if ((!no_air_defense) && (!evaded))
+       if (!no_air_defense && !evaded)
            air_defense(x, y, plane_owner, bomb_list, esc_list);
 
        if (sect.sct_own == 0 || sect.sct_own == plane_owner)
@@ -258,8 +257,6 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
            continue;
        ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
                     sect.sct_own, x, y);
-       intx = x;
-       inty = y;
        intown = sect.sct_own;
     }
 
@@ -276,14 +273,8 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
            writemap(player->cnum);
     /* Now, if the bomber and escort lists are empty, we are done */
     if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
-       if (mission_flags & P_A) {
-           s = head.next;
-           while (s != (struct shiplook *)0) {
-               s2 = s;
-               s = s->next;
-               free(s2);
-           }
-       }
+       if (mission_flags & P_A)
+           free_shiplist(&head);
        return;
     }
 
@@ -291,7 +282,7 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     /* Go figure out if there are ships in this sector, and who's they are */
     memset(nats, 0, sizeof(nats));
     snxtitem_xy(&ni, EF_SHIP, x, y);
-    while (nxtitem(&ni, (s_char *)&ship)) {
+    while (nxtitem(&ni, &ship)) {
        if (mchr[(int)ship.shp_type].m_flags & M_SUB)
            continue;
        nats[ship.shp_own]++;
@@ -299,7 +290,9 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
     /* Go figure out if there are units in this sector, and who's they are */
     memset(lnats, 0, sizeof(lnats));
     snxtitem_xy(&ni, EF_LAND, x, y);
-    while (nxtitem(&ni, (s_char *)&land)) {
+    while (nxtitem(&ni, &land)) {
+       if (land.lnd_ship >= 0 || land.lnd_land >= 0)
+           continue;
        lnats[land.lnd_own]++;
     }
 
@@ -311,9 +304,12 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
        /* Are there ships owned by this country? */
        if (nats[cn] != 0) {
            /* Yes. */
-           if (cn != 0)
+           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]) {
@@ -321,25 +317,28 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                             oesc);
                    gotilist[cn]++;
                }
-               PR(plane_owner, "Flying over %s ships in %s\n", cname(cn),
-                  xyas(x, y, plane_owner));
+               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);
+                   ac_intercept(bomb_list, esc_list, &ilist[cn],
+                                cn, x, y);
                }
            }
        }
        /* Are there units owned by this country? */
        if (lnats[cn] != 0) {
            /* Yes. */
-           if (cn != 0)
+           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 */
                if (!gotilist[cn]) {
@@ -352,22 +351,15 @@ ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
                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);
                    }
                }
            }
        }
     }
-
-    if ((mission_flags & P_A) && (head.uid != -1)) {
-       s = head.next;
-       while (s != (struct shiplook *)0) {
-           s2 = s;
-           s = s->next;
-           free(s2);
-       }
-    }
+    if (mission_flags & P_A)
+       free_shiplist(&head);
 }
 
 static int
@@ -417,12 +409,12 @@ sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
            if (dplp->plane.pln_range <
                mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
                emp_remque(dqp);
-               free((s_char *)dqp);
+               free(dqp);
                continue;
            }
            if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
                emp_remque(dqp);
-               free((s_char *)dqp);
+               free(dqp);
                continue;
            }
            if (first) {
@@ -448,17 +440,17 @@ sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
            if (!(dplp->pcp->pl_flags & P_M))
                continue;
            emp_remque(dqp);
-           free((s_char *)dqp);
+           free(dqp);
            continue;
        }
     }
 }
 
-void
+static void
 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
             struct emp_qelem *def_list, natid def_own, coord x, coord y)
 {
-    register struct plnstr *pp;
+    struct plnstr *pp;
     struct plist *plp;
     int icount;
     struct emp_qelem *next;
@@ -492,7 +484,7 @@ ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
            continue;
        if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
            emp_remque(qp);
-           free((s_char *)qp);
+           free(qp);
            continue;
        }
        /* got one; delete from def_list, add to int_list */
@@ -506,13 +498,13 @@ 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!\n", icount,
-       icount == 1 ? " is" : "s are", cname(plane_owner));
+    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!\n",
+       icount, icount == 1 ? " is" : "s are", cname(plane_owner));
     ac_combat_headers(plane_owner, def_own);
-    ac_airtoair(esc_list, &int_list, def_own);
-    ac_airtoair(bomb_list, &int_list, def_own);
+    ac_airtoair(esc_list, &int_list);
+    ac_airtoair(bomb_list, &int_list);
     PR(plane_owner, "\n");
     PR(def_own, "\n");
 }
@@ -532,11 +524,10 @@ ac_combat_headers(natid plane_owner, natid def_own)
  * air-to-air combat.
  */
 void
-ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list,
-           natid def_own)
+ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
 {
-    register struct plist *attacker;
-    register struct plist *interceptor;
+    struct plist *attacker;
+    struct plist *interceptor;
     struct emp_qelem *att;
     struct emp_qelem *in;
     int nplanes;
@@ -544,7 +535,6 @@ ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list,
     int more_int;
     struct emp_qelem *att_next;
     struct emp_qelem *in_next;
-    natid att_own;
 
     att = att_list->q_forw;
     in = int_list->q_forw;
@@ -574,8 +564,6 @@ ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list,
            continue;
        }
        interceptor = (struct plist *)in;
-       att_own = attacker->plane.pln_own;
-       def_own = interceptor->plane.pln_own;
        nplanes = attacker->plane.pln_effic;
        if (nplanes > interceptor->plane.pln_effic)
            nplanes = interceptor->plane.pln_effic;
@@ -599,7 +587,7 @@ ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list,
     }
 }
 
-int
+static int
 all_missiles(struct emp_qelem *att_list)
 {
     struct emp_qelem *qp;
@@ -617,15 +605,15 @@ all_missiles(struct emp_qelem *att_list)
 }
 
 static void
-ac_dog(register struct plist *ap, register struct plist *dp)
+ac_dog(struct plist *ap, struct plist *dp)
 {
     int att, def;
     double odds;
     int intensity;
     natid att_own, def_own;
     int adam, ddam;
-    s_char mesg[1024];
-    s_char temp[14];
+    char mesg[1024];
+    char temp[14];
 
     att_own = ap->plane.pln_own;
     def_own = dp->plane.pln_own;
@@ -652,8 +640,8 @@ ac_dog(register struct plist *ap, register struct plist *dp)
        att -= 2;
     if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
        def -= 2;
-    att += ((float)ap->pcp->pl_stealth / 25.0);
-    def += ((float)dp->pcp->pl_stealth / 25.0);
+    att += ap->pcp->pl_stealth / 25.0;
+    def += dp->pcp->pl_stealth / 25.0;
     if (att < 1) {
        def += 1 - att;
        att = 1;
@@ -690,11 +678,16 @@ ac_dog(register struct plist *ap, register struct plist *dp)
 
     PR(att_own, "%3d/%-3d", adam, ddam);
     PR(def_own, "%3d/%-3d", ddam, adam);
-    ac_planedamage(ap, dp->plane.pln_own, adam, def_own, 1, 0, mesg);
+    ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
     strncpy(temp, mesg, 14);
-    ac_planedamage(dp, ap->plane.pln_own, ddam, att_own, 1, 0, mesg);
+    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);
+
+    if (opt_HIDDEN) {
+       setcont(att_own, def_own, FOUND_FLY);
+       setcont(def_own, att_own, FOUND_FLY);
+    }
 }
 
 /*
@@ -709,17 +702,16 @@ ac_dog(register struct plist *ap, register struct plist *dp)
  * that the current queue pointer is invalid on return from the ac_planedamage
  * call.  (this has caused bugs in the past)
  */
-void
+static void
 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
-              int checkabort, int show, s_char *mesg)
+              int checkabort, int show, char *mesg)
 {
-    register struct plnstr *pp;
+    struct plnstr *pp;
     int disp;
-    s_char dmess[255];
+    char dmess[255];
     int eff;
     struct shpstr ship;
     struct lndstr land;
-    /* s_char *sprintf();           already in misc.h [JFW] */
     natid plane_owner;
 
     disp = 0;
@@ -738,7 +730,7 @@ ac_planedamage(struct plist *plp, natid from, int dam, natid other,
     if (eff < PLANE_MINEFF) {
        sprintf(dmess, " shot down");
        disp = 1;
-    } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
+    } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
        sprintf(dmess, " aborted @%2d%%", eff);
        disp = 2;
     } else if (show == 0) {
@@ -761,7 +753,7 @@ ac_planedamage(struct plist *plp, natid from, int dam, natid other,
     }
 
     pp->pln_effic = eff;
-    pp->pln_mobil -= min(32 + pp->pln_mobil, dam / 2);
+    pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
     if (disp == 1) {
        if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
            nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
@@ -773,21 +765,20 @@ ac_planedamage(struct plist *plp, natid from, int dam, natid other,
            getland(pp->pln_land, &land);
            take_plane_off_land(pp, &land);
        }
-       makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
-       pp->pln_own = 0;
+       pp->pln_effic = 0;
        putplane(pp->pln_uid, pp);
        emp_remque(&plp->queue);
-       free((s_char *)plp);
+       free(plp);
     } else if (disp == 2) {
        putplane(pp->pln_uid, pp);
        emp_remque(&plp->queue);
-       free((s_char *)plp);
+       free(plp);
     } else
        putplane(pp->pln_uid, pp);
     strcpy(mesg, dmess);
 }
 
-void
+static void
 ac_doflak(struct emp_qelem *list, struct sctstr *from)
 {
     int shell;
@@ -798,7 +789,7 @@ ac_doflak(struct emp_qelem *list, struct sctstr *from)
     plp = (struct plist *)list->q_forw;
     plane_owner = plp->plane.pln_own;
 
-    gun = from->sct_item[I_GUN];
+    gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
     shell = from->sct_item[I_SHELL];
     if (gun > shell * 2) {
        shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
@@ -808,30 +799,26 @@ ac_doflak(struct emp_qelem *list, struct sctstr *from)
     }
     if (gun > shell * 2)
        gun = shell * 2;
-    if (gun > 14)
-       gun = 14;
 
-    gun = 2.0 * tfact(from->sct_own, 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));
-       ac_fireflak(list, from->sct_own, 0, gun);
+       ac_fireflak(list, from->sct_own, gun);
     }
 }
 
-void
+static void
 ac_shipflak(struct emp_qelem *list, coord x, coord y)
 {
     struct nstr_item ni;
     struct shpstr ship;
     struct mchrstr *mcp;
-    int firing;
-    int guns;
-    int gun;
-    int shell;
+    double flak, total, ngun;
+    int gun, shell;
     int rel;
     struct plist *plp;
     natid plane_owner;
@@ -842,13 +829,11 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
     plane_owner = plp->plane.pln_own;
 
     memset(nats, 0, sizeof(nats));
-    guns = 0;
+    total = ngun = 0;
     snxtitem_xy(&ni, EF_SHIP, x, y);
-    while (!QEMPTY(list) && nxtitem(&ni, (s_char *)&ship)) {
+    while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
        if (ship.shp_own == 0 || ship.shp_own == plane_owner)
            continue;
-       if (guns >= 14)
-           break;
        mcp = &mchr[(int)ship.shp_type];
        if (mcp->m_flags & M_SUB)
            continue;
@@ -856,20 +841,21 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
        if (rel > HOSTILE)
            continue;
        shell = gun = 0;
-       gun = min(ship.shp_item[I_GUN], ship.shp_glim);
+       gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
        if (gun) {
            shell = ship.shp_item[I_SHELL];
            if (shell <= 0) {
-               shell = supply_commod(ship.shp_own, ship.shp_x,
-                                     ship.shp_y, I_SHELL, 1);
+               shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
+                                     I_SHELL, 1);
                ship.shp_item[I_SHELL] = shell;
                putship(ship.shp_uid, &ship);
            }
        }
        if (gun == 0 || shell == 0)
            continue;
-       firing = (int)(techfact(ship.shp_tech, (double)gun) * 2.0);
-       guns += firing;
+       flak = gun * (ship.shp_effic / 100.0);
+       ngun += flak;
+       total += techfact(ship.shp_tech, flak * 2.0);
 
        if (!nats[ship.shp_own]) {
            /* First time here, print the message */
@@ -879,58 +865,56 @@ ac_shipflak(struct emp_qelem *list, coord x, coord y)
               cname(ship.shp_own), xyas(x, y, plane_owner));
            nats[ship.shp_own] = 1;
        }
-       PR(ship.shp_own, "firing %d flak guns from %s...\n",
-          firing, prship(&ship));
+       PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
+          flak, prship(&ship));
        from = ship.shp_own;
     }
-    if (guns > 0) {
-       if (guns > 14)
-           guns = 14;
-       guns = 2.0 * tfact(from, (double)guns);
-       PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", guns);
-       ac_fireflak(list, from, 0, guns);
+
+    /* Limit to FLAK_GUN_MAX guns of average tech factor */
+    if (ngun > FLAK_GUN_MAX)
+       total *= FLAK_GUN_MAX / ngun;
+
+    gun = roundavg(total);
+    if (gun > 0) {
+       PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
+       ac_fireflak(list, from, gun);
     }
 }
 
-void
+static void
 ac_landflak(struct emp_qelem *list, coord x, coord y)
 {
     struct nstr_item ni;
     struct lndstr land;
     struct lchrstr *lcp;
-    int firing;
-    int guns;
+    double flak, total, ngun;
+    int gun;
     int rel;
-    natid from;
     struct plist *plp;
     natid plane_owner;
+    natid from;
     int nats[MAXNOC];
 
     plp = (struct plist *)list->q_forw;
     plane_owner = plp->plane.pln_own;
 
     memset(nats, 0, sizeof(nats));
-    guns = 0;
+    total = ngun = 0;
     snxtitem_xy(&ni, EF_LAND, x, y);
-    while (!QEMPTY(list) && nxtitem(&ni, (s_char *)&land)) {
+    while (!QEMPTY(list) && nxtitem(&ni, &land)) {
        if (land.lnd_own == 0 || land.lnd_own == plane_owner)
            continue;
-       if (guns >= 14)
-           break;
        lcp = &lchr[(int)land.lnd_type];
-
-       if ((lcp->l_flags & L_FLAK) == 0)
+       if ((lcp->l_flags & L_FLAK) == 0 || land.lnd_aaf == 0)
            continue;
-
-       if (land.lnd_aaf == 0)
+       if (land.lnd_ship >= 0 || land.lnd_land >= 0)
            continue;
-
        rel = getrel(getnatp(land.lnd_own), plane_owner);
        if (rel > HOSTILE)
            continue;
-       firing =
-           (int)(techfact(land.lnd_tech, (double)land.lnd_aaf) * 3.0);
-       guns += firing;
+       flak = land.lnd_aaf * 1.5 * land.lnd_effic / 100.0;
+       ngun += flak;
+       total += techfact(land.lnd_tech, flak * 2.0);
 
        if (!nats[land.lnd_own]) {
            /* First time here, print the message */
@@ -942,68 +926,81 @@ ac_landflak(struct emp_qelem *list, coord x, coord y)
        }
        PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
           prland(&land), land.lnd_aaf);
-
-       from = land.lnd_own;    /* We always use the last owner as the from */
+       from = land.lnd_own;
     }
-    if (guns > 0) {
-       if (guns > 14)
-           guns = 14;
-       guns = 2.0 * tfact(from, (double)guns);
-       PR(plane_owner, "Flak!  Land units firing %d flak guns...\n",
-          guns);
-       ac_fireflak(list, from, 0, guns);
+
+    /* Limit to FLAK_GUN_MAX guns of average tech factor */
+    if (ngun > FLAK_GUN_MAX)
+       total *= FLAK_GUN_MAX / ngun;
+
+    gun = roundavg(total);
+    if (gun > 0) {
+       PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
+       ac_fireflak(list, from, gun);
     }
 }
 
 /*
  * Called from shipflak, landflak, and doflak.
  */
-void
-ac_fireflak(struct emp_qelem *list, natid from, natid other, int guns)
+static void
+ac_fireflak(struct emp_qelem *list, natid from, int guns)
 {
-    register struct plnstr *pp;
     struct plist *plp;
     int n;
-    float mult;
-    int diff;
     struct emp_qelem *qp;
     struct emp_qelem *next;
-    s_char msg[255];
+    char msg[255];
 
     plp = (struct plist *)list->q_forw;
 
     for (qp = list->q_forw; qp != list; qp = next) {
-       /*
-        * fighters don't get shot at by flak
-        * non-tactical bombers are harder to hit with flak.
-        * ('Cause they're not dive-bombing?)
-        */
        next = qp->q_forw;
        plp = (struct plist *)qp;
-       pp = &plp->plane;
-       diff = guns - pp->pln_def;
-       if ((plp->pcp->pl_flags & P_T) == 0)
-           diff--;
-       if (plp->pcp->pl_flags & P_X)
-           diff -= 2;
-       if (plp->pcp->pl_flags & P_H)
-           diff -= 1;
-       if (diff > 8)
-           mult = flaktable[FLAK_MAX] * 1.33;
-       else if (diff < -7)
-           mult = flaktable[0] * 0.66;
-       else {
-           diff += 7;
-           mult = flaktable[diff];
-       }
-       mult *= flakscale;
-       n = (int)((roll(8) + 2) * mult);
-       if (n > 100)
-           n = 100;
-       ac_planedamage(plp, from, n, other, 2, 1, msg);
+       n = ac_flak_dam(guns, plp->plane.pln_def, plp->pcp->pl_flags);
+       ac_planedamage(plp, from, n, 0, 2, 1, msg);
     }
 }
 
+/*
+ * Calculate flak damage
+ */
+int
+ac_flak_dam(int guns, int def, int pl_flags)
+{
+    int flak, dam;
+    float mult;
+    /*                            <-7      -7     -6     -5     -4 */
+    static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
+    /*    -3     -2     -1      0     +1     +2     +3     +4 */
+        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 };
+
+    flak = guns - def;
+    if ((pl_flags & P_T) == 0)
+       flak--;
+    if (pl_flags & P_X)
+       flak -= 2;
+    if (pl_flags & P_H)
+       flak -= 1;
+
+    if (flak > 8)
+       mult = flaktable[FLAK_MAX];
+    else if (flak < -7)
+       mult = flaktable[0];
+    else {
+       flak += 8;
+       mult = flaktable[flak];
+    }
+    mult *= flakscale;
+    dam = (int)((roll(8) + 2) * mult);
+    if (dam > 100)
+       dam = 100;
+    return dam;
+}
+
 /*
  * See if this plane is flying in this list
  */
@@ -1035,7 +1032,7 @@ static void
 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
         struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
 {
-    register struct plchrstr *pcp;
+    struct plchrstr *pcp;
     struct plnstr plane;
     struct shpstr ship;
     struct lndstr land;
@@ -1046,7 +1043,7 @@ getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
 
     emp_initque(list);
     snxtitem_all(&ni, EF_PLANE);
-    while (nxtitem(&ni, (s_char *)&plane)) {
+    while (nxtitem(&ni, &plane)) {
        if (plane.pln_own != own)
            continue;
        pcp = &plchr[(int)plane.pln_type];
@@ -1075,7 +1072,7 @@ getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
                && (pcp->pl_flags & P_V) == 0)
                continue;
        }
-       if ((float)petrol < (float)pcp->pl_fuel / 2.0)
+       if (petrol <= pcp->pl_fuel / 2)
            continue;
        /* Finally, is it in the list of planes already in
           flight? */
@@ -1088,8 +1085,7 @@ getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
        if (ac_isflying(&plane, d))
            continue;
        /* got one! */
-       ip = (struct plist *)malloc(sizeof(*ip));
-       ip->state = P_OK;
+       ip = malloc(sizeof(*ip));
        ip->bombs = 0;
        ip->misc = 0;
        ip->pcp = &plchr[(int)plane.pln_type];
@@ -1100,7 +1096,7 @@ getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
 
 
 
-int
+static int
 can_fly(int p)
 {                              /* Can this plane fly from the ship or land unit it is on? */
     struct plnstr plane;
@@ -1108,7 +1104,6 @@ can_fly(int p)
     struct lndstr land;
     struct plchrstr *pcp;
     struct mchrstr *scp;
-    struct lchrstr *lcp;
 
     getplane(p, &plane);
     pcp = &plchr[(int)plane.pln_type];
@@ -1145,9 +1140,8 @@ can_fly(int p)
            return 0;
 
        getland(plane.pln_land, &land);
-       lcp = &lchr[(int)land.lnd_type];
 
-       if ((pcp->pl_flags & P_E) && (lcp->l_flags & L_XLIGHT)) {
+       if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
            return 1;
        }
     }
@@ -1155,7 +1149,7 @@ can_fly(int p)
     return 0;
 }
 
-int
+static int
 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
 {
     struct emp_qelem *qp;
@@ -1165,13 +1159,13 @@ do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
     evade = 100.0;
     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
        plp = (struct plist *)qp;
-       if (evade > ((float)plp->pcp->pl_stealth / 100.0))
-           evade = (plp->pcp->pl_stealth / 100.0);
+       if (evade > plp->pcp->pl_stealth / 100.0)
+           evade = plp->pcp->pl_stealth / 100.0;
     }
     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
        plp = (struct plist *)qp;
        if (evade > plp->pcp->pl_stealth / 100.0)
-           evade = (plp->pcp->pl_stealth / 100.0);
+           evade = plp->pcp->pl_stealth / 100.0;
     }
 
     if (chance(evade))