]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
e2a2d64134fdfa1e767be3dc896f33db2ee27334
[empserver] / src / lib / subs / aircombat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  aircombat.c: Deal with air to air combat
29  *
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Thomas Ruschak, 1992
33  *     Steve McClure, 1996
34  *     Markus Armbruster, 2006-2009
35  */
36
37 #include <config.h>
38
39 #include "file.h"
40 #include "land.h"
41 #include "map.h"
42 #include "misc.h"
43 #include "mission.h"
44 #include "nat.h"
45 #include "news.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "path.h"
49 #include "plane.h"
50 #include "player.h"
51 #include "prototypes.h"
52 #include "sect.h"
53 #include "ship.h"
54 #include "xy.h"
55
56 #define FLAK_GUN_MAX 14
57
58 static void sam_intercept(struct emp_qelem *, struct emp_qelem *,
59                           natid, natid, coord, coord, int);
60 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
61                          struct emp_qelem *, natid, coord, coord, int);
62 static void ac_combat_headers(natid, natid);
63 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
64 static void ac_dog(struct plist *, struct plist *);
65 static void ac_planedamage(struct plist *, natid, int, int, char *);
66 static void ac_doflak(struct emp_qelem *, struct sctstr *);
67 static void ac_landflak(struct emp_qelem *, coord, coord);
68 static void ac_shipflak(struct emp_qelem *, coord, coord);
69 static void ac_fireflak(struct emp_qelem *, natid, int);
70 static void getilists(struct emp_qelem *, unsigned char *, natid);
71 static int do_evade(struct emp_qelem *, struct emp_qelem *);
72
73 void
74 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
75              coord x, coord y, char *path, int mission_flags)
76 {
77     int val;
78     int dir;
79     unsigned char gotships[MAXNOC];
80     unsigned char gotlands[MAXNOC];
81     unsigned char rel[MAXNOC];
82     int overfly[MAXNOC];
83     int flags;
84     struct emp_qelem ilist[MAXNOC];
85     natid plane_owner;
86     struct sctstr sect;
87     struct shpstr ship;
88     struct lndstr land;
89     struct nstr_item ni;
90     natid cn;
91     struct plist *plp;
92     int evaded;
93     struct shiplist *head = NULL;
94     int changed = 0;
95 /* We want to only intercept once per sector per owner.  So, if we overfly
96    a sector, and then overfly some land units or ships, we don't want to
97    potentially intercept 3 times. */
98
99     plp = (struct plist *)bomb_list->q_forw;
100     plane_owner = plp->plane.pln_own;
101
102     memset(overfly, 0, sizeof(overfly));
103     getilists(ilist, rel, plane_owner);
104
105     if (CANT_HAPPEN(mission_flags && plane_owner != player->cnum))
106         mission_flags = 0;
107
108     if (mission_flags & PM_R) {
109         flags = pln_caps(bomb_list);
110         if (flags & P_S) {
111             pr("\nSPY Plane report\n");
112             prdate();
113             sathead();
114         } else if (flags & P_A) {
115             pr("\nAnti-Sub Patrol report\n");
116         } else {
117             pr("\nReconnaissance report\n");
118             prdate();
119         }
120     }
121
122     for (;;) {
123         getsect(x, y, &sect);
124         memset(gotships, 0, sizeof(gotships));
125         snxtitem_xy(&ni, EF_SHIP, x, y);
126         while (nxtitem(&ni, &ship)) {
127             if (mchr[(int)ship.shp_type].m_flags & M_SUB)
128                 continue;
129             gotships[ship.shp_own] = 1;
130         }
131         memset(gotlands, 0, sizeof(gotlands));
132         snxtitem_xy(&ni, EF_LAND, x, y);
133         while (nxtitem(&ni, &land)) {
134             if (land.lnd_ship >= 0 || land.lnd_land >= 0)
135                 continue;
136             gotlands[land.lnd_own] = 1;
137         }
138
139         if (mission_flags & PM_R) {
140             flags = pln_caps(bomb_list);
141             if (opt_HIDDEN)
142                 setcont(player->cnum, sect.sct_own, FOUND_FLY);
143             if (sect.sct_type == SCT_WATER) {
144                 pr("flying over %s at %s\n",
145                    dchr[sect.sct_type].d_name, xyas(x, y, player->cnum));
146                 if (mission_flags & PM_S)
147                     plane_sweep(bomb_list, x, y);
148                 if (flags & P_A)
149                     plane_sona(bomb_list, x, y, &head);
150             } else if (flags & P_S)
151                 satdisp_sect(&sect, flags & P_I ? 10 : 50);
152             else
153                 look_at_sect(&sect, 25);
154             changed += map_set(player->cnum, sect.sct_x, sect.sct_y,
155                                dchr[sect.sct_type].d_mnem, 0);
156             if (flags & P_S)
157                 satdisp_units(sect.sct_x, sect.sct_y);
158             else {
159                 for (cn = 1; cn < MAXNOC; cn++) {
160                     if (cn == player->cnum)
161                         continue;
162                     if (gotships[cn])
163                         pr("Flying over %s ships in %s\n",
164                            cname(cn), xyas(x, y, player->cnum));
165                     if (gotlands[cn])
166                         pr("Flying over %s land units in %s\n",
167                            cname(cn), xyas(x, y, player->cnum));
168                 }
169             }
170         } else {
171             PR(plane_owner, "flying over %s at %s\n",
172                dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
173             changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
174                                dchr[sect.sct_type].d_mnem, 0);
175         }
176
177         evaded = do_evade(bomb_list, esc_list);
178         if (!evaded) {
179             overfly[sect.sct_own]++;
180             for (cn = 1; cn < MAXNOC; cn++) {
181                 if (cn == plane_owner || rel[cn] == ALLIED)
182                     continue;
183                 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
184                     continue;
185                 PR(cn, "%s planes spotted over %s\n",
186                    cname(plane_owner), xyas(x, y, cn));
187                 if (opt_HIDDEN)
188                     setcont(cn, plane_owner, FOUND_FLY);
189             }
190
191             /* Fire flak */
192             if (rel[sect.sct_own] <= HOSTILE)
193                 ac_doflak(bomb_list, &sect);
194             /* If bombers left, fire flak from units and ships */
195             if (!QEMPTY(bomb_list))
196                 ac_landflak(bomb_list, x, y);
197             if (!QEMPTY(bomb_list))
198                 ac_shipflak(bomb_list, x, y);
199             /* mission planes aborted due to flak -- don't send escorts */
200             if (QEMPTY(bomb_list))
201                 break;
202
203             for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
204                 if (rel[cn] > HOSTILE)
205                     continue;
206                 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
207                              !(cn == sect.sct_own
208                                || gotships[cn] || gotlands[cn]));
209             }
210         }
211
212         dir = *path++;
213         if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
214             break;
215         x = xnorm(x + diroff[val][0]);
216         y = ynorm(y + diroff[val][1]);
217     }
218
219     /* Let's report all of the overflights even if aborted */
220     for (cn = 1; cn < MAXNOC; cn++) {
221         if (plane_owner == cn)
222             continue;
223         if (overfly[cn] > 0 && rel[cn] != ALLIED)
224             nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
225     }
226     /* If the map changed, update it */
227     if (changed)
228         writemap(plane_owner);
229
230     free_shiplist(&head);
231     for (cn = 1; cn < MAXNOC; cn++)
232         pln_put(&ilist[cn]);
233 }
234
235 static void
236 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
237               natid def_own, natid plane_owner, coord x, coord y,
238               int only_mission)
239 {
240     struct emp_qelem *aqp;
241     struct emp_qelem *anext;
242     struct emp_qelem *dqp;
243     struct emp_qelem *dnext;
244     struct plist *aplp;
245     struct plist *dplp;
246     struct plnstr *pp;
247     int first = 1;
248
249     for (aqp = att_list->q_forw,
250          dqp = def_list->q_forw;
251          aqp != att_list && dqp != def_list; aqp = anext) {
252         anext = aqp->q_forw;
253         aplp = (struct plist *)aqp;
254         if (aplp->pcp->pl_cost < 1000)
255             continue;
256         for (; dqp != def_list; dqp = dnext) {
257             dnext = dqp->q_forw;
258             dplp = (struct plist *)dqp;
259             pp = &dplp->plane;
260             if (!(dplp->pcp->pl_flags & P_M))
261                 continue;
262             if (only_mission && !pp->pln_mission)
263                 continue;
264             if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
265                 continue;
266             if (pp->pln_mission
267                 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
268                 continue;
269             if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
270                 || mission_pln_equip(dplp, NULL, 0) < 0) {
271                 emp_remque(dqp);
272                 free(dqp);
273                 continue;
274             }
275             pp->pln_flags |= PLN_LAUNCHED;
276             putplane(pp->pln_uid, pp);
277             if (first) {
278                 first = 0;
279                 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
280                 PR(def_own, "Launching SAMs at %s planes over %s!\n",
281                    cname(plane_owner), xyas(x, y, def_own));
282                 ac_combat_headers(plane_owner, def_own);
283             }
284             ac_dog(aplp, dplp);
285             dqp = dnext;
286             break;
287         }
288     }
289     if (!first) {
290         PR(plane_owner, "\n");
291         PR(def_own, "\n");
292     }
293 }
294
295 static void
296 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
297              struct emp_qelem *def_list, natid def_own, coord x, coord y,
298              int only_mission)
299 {
300     struct plnstr *pp;
301     struct plist *plp;
302     int icount;
303     struct emp_qelem *next;
304     struct emp_qelem *qp;
305     struct emp_qelem int_list;
306     int att_count;
307     natid plane_owner;
308     int dist;
309
310     plp = (struct plist *)bomb_list->q_forw;
311     plane_owner = plp->plane.pln_own;
312
313     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
314                   only_mission);
315     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
316                   only_mission);
317
318     att_count = 0;
319     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
320         att_count++;
321     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
322         att_count++;
323     if (!att_count)
324         return;
325
326     emp_initque(&int_list);
327     icount = 0;
328     for (qp = def_list->q_forw; qp != def_list; qp = next) {
329         next = qp->q_forw;
330         plp = (struct plist *)qp;
331         pp = &plp->plane;
332         /* SAMs interdict separately */
333         if (plp->pcp->pl_flags & P_M)
334             continue;
335         if (only_mission && !pp->pln_mission)
336             continue;
337         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
338         if (pp->pln_range < dist)
339             continue;
340         if (pp->pln_mission
341             && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
342             continue;
343         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
344             || mission_pln_equip(plp, NULL, 0) < 0) {
345             emp_remque(qp);
346             free(qp);
347             continue;
348         }
349         /* got one; delete from def_list, add to int_list */
350         emp_remque(qp);
351         emp_insque(qp, &int_list);
352         pp->pln_flags |= PLN_LAUNCHED;
353         pp->pln_mobil -= pln_mobcost(dist, pp, 0);
354         putplane(pp->pln_uid, pp);
355         icount++;
356         if (icount > att_count)
357             break;
358     }
359     if (icount == 0)
360         return;
361     PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
362        icount, cname(def_own), icount == 1 ? " is" : "s are");
363     PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
364        icount, icount == 1 ? " is" : "s are", cname(plane_owner),
365        xyas(x, y, def_own));
366     ac_combat_headers(plane_owner, def_own);
367     ac_airtoair(esc_list, &int_list);
368     ac_airtoair(bomb_list, &int_list);
369     PR(plane_owner, "\n");
370     PR(def_own, "\n");
371     pln_put(&int_list);
372 }
373
374 static void
375 ac_combat_headers(natid plane_owner, natid def_own)
376 {
377     PR(plane_owner,
378        " %-10.10s %-10.10s  strength int odds  damage           results\n",
379        cname(plane_owner), cname(def_own));
380     PR(def_own,
381        " %-10.10s %-10.10s  strength int odds  damage           results\n",
382        cname(def_own), cname(plane_owner));
383 }
384
385 /*
386  * air-to-air combat.
387  */
388 static void
389 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
390 {
391     struct plist *attacker;
392     struct plist *interceptor;
393     struct emp_qelem *att;
394     struct emp_qelem *in;
395     int more_att;
396     int more_int;
397     struct emp_qelem *att_next;
398     struct emp_qelem *in_next;
399
400     att = att_list->q_forw;
401     in = int_list->q_forw;
402     more_att = 1;
403     more_int = 1;
404     if (QEMPTY(att_list) || QEMPTY(int_list)) {
405         more_att = 0;
406         more_int = 0;
407     }
408     while (more_att || more_int) {
409         in_next = in->q_forw;
410         att_next = att->q_forw;
411         attacker = (struct plist *)att;
412         interceptor = (struct plist *)in;
413         ac_dog(attacker, interceptor);
414         in = in_next;
415         att = att_next;
416         if (att == att_list) {
417             more_att = 0;
418             if (QEMPTY(att_list))
419                 more_int = 0;
420             else
421                 att = att->q_forw;
422         }
423         if (in == int_list) {
424             more_int = 0;
425             if (QEMPTY(int_list))
426                 more_att = 0;
427             else
428                 in = in->q_forw;
429         }
430     }
431 }
432
433 static void
434 ac_dog(struct plist *ap, struct plist *dp)
435 {
436     int att, def;
437     double odds;
438     int intensity;
439     natid att_own, def_own;
440     int adam, ddam;
441     char adam_mesg[14], ddam_mesg[14];
442
443     att_own = ap->plane.pln_own;
444     def_own = dp->plane.pln_own;
445
446     PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
447        ap->pcp->pl_name,
448        ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
449     if (def_own)
450         PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
451            dp->pcp->pl_name,
452            dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
453     att = pln_att(&ap->plane);
454     if (att == 0)
455         att = pln_def(&ap->plane);
456     att = att * ap->plane.pln_effic / 100;
457     att = MAX(att, ap->pcp->pl_def / 2);
458
459     def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
460     def = MAX(def, dp->pcp->pl_def / 2);
461
462     if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
463         att -= 2;
464     if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
465         def -= 2;
466     att += ap->pcp->pl_stealth / 25.0;
467     def += dp->pcp->pl_stealth / 25.0;
468     if (att < 1) {
469         def += 1 - att;
470         att = 1;
471     }
472     if (def < 1) {
473         att += 1 - def;
474         def = 1;
475     }
476     odds = ((double)att / ((double)def + (double)att));
477     if (odds <= 0.05)
478         odds = 0.05;
479     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
480
481     PR(att_own, "   %3d/%-3d %3d  %3.2f  ", att, def, intensity, odds);
482     PR(def_own, "   %3d/%-3d %3d  %3.2f  ", def, att, intensity, odds);
483
484     adam = 0;
485     ddam = 0;
486     while ((intensity--) > 0) {
487
488         if (chance(odds)) {
489             ddam += 1;
490             if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
491                 intensity = 0;
492         } else {
493             adam += 1;
494             if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
495                 intensity = 0;
496         }
497     }
498
499     if (dp->pcp->pl_flags & P_M)
500         ddam = 100;
501
502     PR(att_own, "%3d/%-3d", adam, ddam);
503     PR(def_own, "%3d/%-3d", ddam, adam);
504     ac_planedamage(ap, def_own, adam, 0, adam_mesg);
505     ac_planedamage(dp, att_own, ddam, 0, ddam_mesg);
506     PR(att_own, "%-13.13s %-13.13s\n", adam_mesg, ddam_mesg);
507     PR(def_own, "%-13.13s %-13.13s\n", ddam_mesg, adam_mesg);
508
509     if (opt_HIDDEN) {
510         setcont(att_own, def_own, FOUND_FLY);
511         setcont(def_own, att_own, FOUND_FLY);
512     }
513 }
514
515 /*
516  * zap plane associated with plp.
517  * Damaging country is "from", damage is "dam".
518  * def_own is the country on the other side of the conflict from the plane
519  * owner. The only time def_own != from is when the interceptor is getting
520  * damaged.
521  *
522  * NOTE: This routine removes the appropriate plane element from the
523  * queue if it gets destroyed.  That means that the caller must assume
524  * that the current queue pointer is invalid on return from the ac_planedamage
525  * call.  (this has caused bugs in the past)
526  */
527 static void
528 ac_planedamage(struct plist *plp, natid from, int dam,
529                int show, char *mesg)
530 {
531     struct plnstr *pp;
532     int disp;
533     int eff;
534
535     disp = 0;
536     pp = &plp->plane;
537     eff = pp->pln_effic;
538     *mesg = 0;
539     if (dam <= 0) {
540         snprintf(mesg, 14, " no damage");
541         return;
542     }
543     eff -= dam;
544     if (eff < 0)
545         eff = 0;
546     if (eff < PLANE_MINEFF) {
547         snprintf(mesg, 14, " shot down");
548         disp = 1;
549     } else if (eff < 80 && chance((80 - eff) / 100.0)) {
550         snprintf(mesg, 14, " aborted @%2d%%", eff);
551         disp = 2;
552     } else if (show == 0)
553         snprintf(mesg, 14, " cleared");
554
555     pp->pln_effic = eff;
556     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
557     if (disp) {
558         if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
559             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
560         pln_put1(plp);
561     } else
562         putplane(pp->pln_uid, pp);
563 }
564
565 static void
566 ac_doflak(struct emp_qelem *list, struct sctstr *from)
567 {
568     int gun;
569     natid plane_owner;
570     struct plist *plp;
571
572     plp = (struct plist *)list->q_forw;
573     plane_owner = plp->plane.pln_own;
574
575     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
576     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
577     if (gun > 0) {
578         PR(plane_owner, "firing %d flak guns in %s...\n",
579            gun, xyas(from->sct_x, from->sct_y, plane_owner));
580         if (from->sct_own != 0)
581             PR(from->sct_own, "firing %d flak guns in %s...\n",
582                gun, xyas(from->sct_x, from->sct_y, from->sct_own));
583         ac_fireflak(list, from->sct_own, gun);
584     }
585 }
586
587 static void
588 ac_shipflak(struct emp_qelem *list, coord x, coord y)
589 {
590     struct nstr_item ni;
591     struct shpstr ship;
592     struct mchrstr *mcp;
593     double flak, total, ngun;
594     int gun;
595     int rel;
596     struct plist *plp;
597     natid plane_owner;
598     natid from;
599
600     plp = (struct plist *)list->q_forw;
601     plane_owner = plp->plane.pln_own;
602
603     total = ngun = 0;
604     snxtitem_xy(&ni, EF_SHIP, x, y);
605     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
606         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
607             continue;
608         mcp = &mchr[(int)ship.shp_type];
609         if (mcp->m_flags & M_SUB)
610             continue;
611         rel = getrel(getnatp(ship.shp_own), plane_owner);
612         if (rel > HOSTILE)
613             continue;
614         gun = shp_usable_guns(&ship);
615         if (gun == 0)
616             continue;
617         flak = gun * (ship.shp_effic / 100.0);
618         ngun += flak;
619         total += techfact(ship.shp_tech, flak * 2.0);
620
621         PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
622            flak, prship(&ship));
623         from = ship.shp_own;
624     }
625
626     /* Limit to FLAK_GUN_MAX guns of average tech factor */
627     if (ngun > FLAK_GUN_MAX)
628         total *= FLAK_GUN_MAX / ngun;
629
630     gun = roundavg(total);
631     if (gun > 0) {
632         PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
633         ac_fireflak(list, from, gun);
634     }
635 }
636
637 static void
638 ac_landflak(struct emp_qelem *list, coord x, coord y)
639 {
640     struct nstr_item ni;
641     struct lndstr land;
642     struct lchrstr *lcp;
643     double flak, total, ngun;
644     int aaf, gun;
645     int rel;
646     struct plist *plp;
647     natid plane_owner;
648     natid from;
649
650     plp = (struct plist *)list->q_forw;
651     plane_owner = plp->plane.pln_own;
652
653     total = ngun = 0;
654     snxtitem_xy(&ni, EF_LAND, x, y);
655     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
656         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
657             continue;
658         lcp = &lchr[(int)land.lnd_type];
659         aaf = lnd_aaf(&land);
660         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
661             continue;
662         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
663             continue;
664         rel = getrel(getnatp(land.lnd_own), plane_owner);
665         if (rel > HOSTILE)
666             continue;
667         flak = aaf * 1.5 * land.lnd_effic / 100.0;
668         ngun += flak;
669         total += techfact(land.lnd_tech, flak * 2.0);
670
671         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
672            prland(&land), aaf);
673         from = land.lnd_own;
674     }
675
676     /* Limit to FLAK_GUN_MAX guns of average tech factor */
677     if (ngun > FLAK_GUN_MAX)
678         total *= FLAK_GUN_MAX / ngun;
679
680     gun = roundavg(total);
681     if (gun > 0) {
682         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
683         ac_fireflak(list, from, gun);
684     }
685 }
686
687 /*
688  * Called from shipflak, landflak, and doflak.
689  */
690 static void
691 ac_fireflak(struct emp_qelem *list, natid from, int guns)
692 {
693     struct plist *plp;
694     int n;
695     struct emp_qelem *qp;
696     struct emp_qelem *next;
697     char msg[14];
698
699     for (qp = list->q_forw; qp != list; qp = next) {
700         next = qp->q_forw;
701         plp = (struct plist *)qp;
702         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
703         ac_planedamage(plp, from, n, 1, msg);
704         PR(plp->plane.pln_own, "    %s %s takes %d%s.\n",
705            cname(plp->plane.pln_own), prplane(&plp->plane), n, msg);
706     }
707 }
708
709 /*
710  * Calculate flak damage
711  */
712 int
713 ac_flak_dam(int guns, int def, int pl_flags)
714 {
715     int flak, dam;
716     float mult;
717     /*                             <-7      -7     -6     -5     -4 */
718     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
719     /*    -3     -2     -1      0     +1     +2     +3     +4 */
720          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
721     /*    +5    +6     +7     +8    >+8 */
722          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
723     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
724
725     flak = guns - def;
726     if ((pl_flags & P_T) == 0)
727         flak--;
728
729     if (flak > 8)
730         mult = flaktable[FLAK_MAX];
731     else if (flak < -7)
732         mult = flaktable[0];
733     else {
734         flak += 8;
735         mult = flaktable[flak];
736     }
737     mult *= flakscale;
738     dam = (int)((roll(8) + 2) * mult);
739     if (dam > 100)
740         dam = 100;
741     return dam;
742 }
743
744 /*
745  * Get planes available for interception duties.
746  */
747 static void
748 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
749 {
750     natid cn;
751     struct plchrstr *pcp;
752     struct plnstr plane;
753     struct nstr_item ni;
754     struct plist *ip;
755
756     rel[0] = NEUTRAL;
757     for (cn = 1; cn < MAXNOC; cn++) {
758         rel[cn] = getrel(getnatp(cn), intruder);
759         emp_initque(&list[cn]);
760     }
761
762     snxtitem_all(&ni, EF_PLANE);
763     while (nxtitem(&ni, &plane)) {
764         if (rel[plane.pln_own] > HOSTILE)
765             continue;
766         pcp = &plchr[(int)plane.pln_type];
767         if ((pcp->pl_flags & P_F) == 0)
768             continue;
769         if (plane.pln_flags & PLN_LAUNCHED)
770             continue;
771         if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
772             continue;
773         if (plane.pln_mobil <= 0)
774             continue;
775         if (plane.pln_effic < 40)
776             continue;
777         if (!pln_airbase_ok(&plane, 0, 0))
778             continue;
779         /* got one! */
780         ip = malloc(sizeof(*ip));
781         ip->load = 0;
782         ip->pcp = &plchr[(int)plane.pln_type];
783         ip->plane = plane;
784         emp_insque(&ip->queue, &list[plane.pln_own]);
785     }
786 }
787
788 static int
789 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
790 {
791     struct emp_qelem *qp;
792     double evade;
793     struct plist *plp;
794
795     evade = 100.0;
796     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
797         plp = (struct plist *)qp;
798         if (evade > plp->pcp->pl_stealth / 100.0)
799             evade = plp->pcp->pl_stealth / 100.0;
800     }
801     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
802         plp = (struct plist *)qp;
803         if (evade > plp->pcp->pl_stealth / 100.0)
804             evade = plp->pcp->pl_stealth / 100.0;
805     }
806
807     if (chance(evade))
808         return 1;
809
810     return 0;
811 }