]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
Factor ac_dog_report() out of ac_dog()
[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_report(natid to, int intensity, double odds,
435               struct plist *p1, int val1, int dam1, char *dam_mesg1,
436               struct plist *p2, int val2, int dam2, char *dam_mesg2)
437 {
438     mpr(to, " %3.3s #%-4d  %3.3s #%-4d   %3d/%-3d %3d  %3.2f  %3d/%-3d"
439         "%-13.13s %-13.13s\n",
440         p1->pcp->pl_name, p1->plane.pln_uid,
441         p2->pcp->pl_name, p2->plane.pln_uid,
442         val1, val2, intensity, odds, dam1, dam2,
443         dam_mesg1, dam_mesg2);
444 }
445
446 static void
447 ac_dog(struct plist *ap, struct plist *dp)
448 {
449     int att, def;
450     double odds;
451     int intensity, i;
452     natid att_own, def_own;
453     int adam, ddam;
454     char adam_mesg[14], ddam_mesg[14];
455
456     att_own = ap->plane.pln_own;
457     def_own = dp->plane.pln_own;
458
459     att = pln_att(&ap->plane);
460     if (att == 0)
461         att = pln_def(&ap->plane);
462     att = att * ap->plane.pln_effic / 100;
463     att = MAX(att, ap->pcp->pl_def / 2);
464
465     def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
466     def = MAX(def, dp->pcp->pl_def / 2);
467
468     if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
469         att -= 2;
470     if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
471         def -= 2;
472     att += ap->pcp->pl_stealth / 25.0;
473     def += dp->pcp->pl_stealth / 25.0;
474     if (att < 1) {
475         def += 1 - att;
476         att = 1;
477     }
478     if (def < 1) {
479         att += 1 - def;
480         def = 1;
481     }
482     odds = ((double)att / ((double)def + (double)att));
483     if (odds <= 0.05)
484         odds = 0.05;
485     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
486
487     adam = 0;
488     ddam = 0;
489     for (i = 0; i < intensity; i++) {
490         if (chance(odds)) {
491             ddam++;
492             if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
493                 break;
494         } else {
495             adam++;
496             if (ap->plane.pln_effic - adam < PLANE_MINEFF)
497                 break;
498         }
499     }
500
501     if (dp->pcp->pl_flags & P_M)
502         ddam = 100;
503
504     ac_planedamage(ap, def_own, adam, 0, adam_mesg);
505     ac_planedamage(dp, att_own, ddam, 0, ddam_mesg);
506     ac_dog_report(att_own, intensity, odds,
507                   ap, att, adam, adam_mesg,
508                   dp, def, ddam, ddam_mesg);
509     ac_dog_report(def_own, intensity, odds,
510                   dp, def, ddam, ddam_mesg,
511                   ap, att, adam, adam_mesg);
512
513     if (opt_HIDDEN) {
514         setcont(att_own, def_own, FOUND_FLY);
515         setcont(def_own, att_own, FOUND_FLY);
516     }
517 }
518
519 /*
520  * zap plane associated with plp.
521  * Damaging country is "from", damage is "dam".
522  *
523  * NOTE: This routine removes the appropriate plane element from the
524  * queue if it gets destroyed.  That means that the caller must assume
525  * that the current queue pointer is invalid on return from the ac_planedamage
526  * call.  (this has caused bugs in the past)
527  */
528 static void
529 ac_planedamage(struct plist *plp, natid from, int dam, int flak,
530                char *mesg)
531 {
532     int disp = ac_damage_plane(&plp->plane, from, dam, flak, mesg);
533
534     if (disp) {
535         pln_put1(plp);
536     } else
537         putplane(plp->plane.pln_uid, &plp->plane);
538 }
539
540 int
541 ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
542                 char *mesg)
543 {
544     int eff, disp;
545
546     *mesg = 0;
547     if (dam <= 0) {
548         if (!flak)
549             snprintf(mesg, 14, " no damage");
550         return 0;
551     }
552
553     eff = pp->pln_effic - dam;
554     if (eff < 0)
555         eff = 0;
556
557     disp = 0;
558     if (eff < PLANE_MINEFF) {
559         snprintf(mesg, 14, " shot down");
560         disp = 1;
561     } else if (eff < 80 && chance((80 - eff) / 100.0)) {
562         snprintf(mesg, 14, " aborted @%2d%%", eff);
563         disp = 2;
564     } else if (!flak)
565         snprintf(mesg, 14, " cleared");
566
567     pp->pln_effic = eff;
568     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
569
570     if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
571         nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
572     return disp;
573 }
574
575 static void
576 ac_doflak(struct emp_qelem *list, struct sctstr *from)
577 {
578     int gun;
579     natid plane_owner;
580     struct plist *plp;
581
582     plp = (struct plist *)list->q_forw;
583     plane_owner = plp->plane.pln_own;
584
585     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
586     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
587     if (gun > 0) {
588         PR(plane_owner, "firing %d flak guns in %s...\n",
589            gun, xyas(from->sct_x, from->sct_y, plane_owner));
590         if (from->sct_own != 0)
591             PR(from->sct_own, "firing %d flak guns in %s...\n",
592                gun, xyas(from->sct_x, from->sct_y, from->sct_own));
593         ac_fireflak(list, from->sct_own, gun);
594     }
595 }
596
597 static void
598 ac_shipflak(struct emp_qelem *list, coord x, coord y)
599 {
600     struct nstr_item ni;
601     struct shpstr ship;
602     struct mchrstr *mcp;
603     double flak, total, ngun;
604     int gun;
605     int rel;
606     struct plist *plp;
607     natid plane_owner;
608     natid from;
609
610     plp = (struct plist *)list->q_forw;
611     plane_owner = plp->plane.pln_own;
612
613     total = ngun = 0;
614     snxtitem_xy(&ni, EF_SHIP, x, y);
615     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
616         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
617             continue;
618         mcp = &mchr[(int)ship.shp_type];
619         if (mcp->m_flags & M_SUB)
620             continue;
621         rel = getrel(getnatp(ship.shp_own), plane_owner);
622         if (rel > HOSTILE)
623             continue;
624         gun = shp_usable_guns(&ship);
625         if (gun == 0)
626             continue;
627         flak = gun * (ship.shp_effic / 100.0);
628         ngun += flak;
629         total += techfact(ship.shp_tech, flak * 2.0);
630
631         PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
632            flak, prship(&ship));
633         from = ship.shp_own;
634     }
635
636     /* Limit to FLAK_GUN_MAX guns of average tech factor */
637     if (ngun > FLAK_GUN_MAX)
638         total *= FLAK_GUN_MAX / ngun;
639
640     gun = roundavg(total);
641     if (gun > 0) {
642         PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
643         ac_fireflak(list, from, gun);
644     }
645 }
646
647 static void
648 ac_landflak(struct emp_qelem *list, coord x, coord y)
649 {
650     struct nstr_item ni;
651     struct lndstr land;
652     struct lchrstr *lcp;
653     double flak, total, ngun;
654     int aaf, gun;
655     int rel;
656     struct plist *plp;
657     natid plane_owner;
658     natid from;
659
660     plp = (struct plist *)list->q_forw;
661     plane_owner = plp->plane.pln_own;
662
663     total = ngun = 0;
664     snxtitem_xy(&ni, EF_LAND, x, y);
665     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
666         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
667             continue;
668         lcp = &lchr[(int)land.lnd_type];
669         aaf = lnd_aaf(&land);
670         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
671             continue;
672         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
673             continue;
674         rel = getrel(getnatp(land.lnd_own), plane_owner);
675         if (rel > HOSTILE)
676             continue;
677         flak = aaf * 1.5 * land.lnd_effic / 100.0;
678         ngun += flak;
679         total += techfact(land.lnd_tech, flak * 2.0);
680
681         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
682            prland(&land), aaf);
683         from = land.lnd_own;
684     }
685
686     /* Limit to FLAK_GUN_MAX guns of average tech factor */
687     if (ngun > FLAK_GUN_MAX)
688         total *= FLAK_GUN_MAX / ngun;
689
690     gun = roundavg(total);
691     if (gun > 0) {
692         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
693         ac_fireflak(list, from, gun);
694     }
695 }
696
697 /*
698  * Called from shipflak, landflak, and doflak.
699  */
700 static void
701 ac_fireflak(struct emp_qelem *list, natid from, int guns)
702 {
703     struct plist *plp;
704     int n;
705     struct emp_qelem *qp;
706     struct emp_qelem *next;
707     char msg[14];
708
709     for (qp = list->q_forw; qp != list; qp = next) {
710         next = qp->q_forw;
711         plp = (struct plist *)qp;
712         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
713         ac_planedamage(plp, from, n, 1, msg);
714         PR(plp->plane.pln_own, "    %s takes %d%s%s.\n",
715            prplane(&plp->plane), n, *msg ? " --" : "", msg);
716     }
717 }
718
719 /*
720  * Calculate flak damage
721  */
722 int
723 ac_flak_dam(int guns, int def, int pl_flags)
724 {
725     int flak, dam;
726     float mult;
727     /*                             <-7      -7     -6     -5     -4 */
728     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
729     /*    -3     -2     -1      0     +1     +2     +3     +4 */
730          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
731     /*    +5    +6     +7     +8    >+8 */
732          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
733     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
734
735     flak = guns - def;
736     if ((pl_flags & P_T) == 0)
737         flak--;
738
739     if (flak > 8)
740         mult = flaktable[FLAK_MAX];
741     else if (flak < -7)
742         mult = flaktable[0];
743     else {
744         flak += 8;
745         mult = flaktable[flak];
746     }
747     mult *= flakscale;
748     dam = (int)((roll(8) + 2) * mult);
749     if (dam > 100)
750         dam = 100;
751     return dam;
752 }
753
754 /*
755  * Get planes available for interception duties.
756  */
757 static void
758 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
759 {
760     natid cn;
761     struct plchrstr *pcp;
762     struct plnstr plane;
763     struct nstr_item ni;
764     struct plist *ip;
765
766     rel[0] = NEUTRAL;
767     for (cn = 1; cn < MAXNOC; cn++) {
768         rel[cn] = getrel(getnatp(cn), intruder);
769         emp_initque(&list[cn]);
770     }
771
772     snxtitem_all(&ni, EF_PLANE);
773     while (nxtitem(&ni, &plane)) {
774         if (rel[plane.pln_own] > HOSTILE)
775             continue;
776         pcp = &plchr[(int)plane.pln_type];
777         if ((pcp->pl_flags & P_F) == 0)
778             continue;
779         if (plane.pln_flags & PLN_LAUNCHED)
780             continue;
781         if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
782             continue;
783         if (plane.pln_mobil <= 0)
784             continue;
785         if (plane.pln_effic < 40)
786             continue;
787         if (!pln_airbase_ok(&plane, 0, 0))
788             continue;
789         /* got one! */
790         ip = malloc(sizeof(*ip));
791         ip->load = 0;
792         ip->pcp = &plchr[(int)plane.pln_type];
793         ip->plane = plane;
794         emp_insque(&ip->queue, &list[plane.pln_own]);
795     }
796 }
797
798 static int
799 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
800 {
801     struct emp_qelem *qp;
802     double evade;
803     struct plist *plp;
804
805     evade = 100.0;
806     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
807         plp = (struct plist *)qp;
808         if (evade > plp->pcp->pl_stealth / 100.0)
809             evade = plp->pcp->pl_stealth / 100.0;
810     }
811     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
812         plp = (struct plist *)qp;
813         if (evade > plp->pcp->pl_stealth / 100.0)
814             evade = plp->pcp->pl_stealth / 100.0;
815     }
816
817     if (chance(evade))
818         return 1;
819
820     return 0;
821 }