]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
2645eb69e507cc192a6e89e1d5ac48730fec8be9
[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             else if (flags & P_S)
147                 satdisp_sect(&sect, flags & P_I ? 10 : 50);
148             else
149                 look_at_sect(&sect, 25);
150
151             if (flags & P_S)
152                 satdisp_units(sect.sct_x, sect.sct_y);
153             else {
154                 for (cn = 1; cn < MAXNOC; cn++) {
155                     if (cn == player->cnum)
156                         continue;
157                     if (gotships[cn])
158                         pr("Flying over %s ships in %s\n",
159                            cname(cn), xyas(x, y, player->cnum));
160                     if (gotlands[cn])
161                         pr("Flying over %s land units in %s\n",
162                            cname(cn), xyas(x, y, player->cnum));
163                 }
164             }
165         } else
166             mpr(plane_owner, "flying over %s at %s\n",
167                 dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
168         changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
169                            dchr[sect.sct_type].d_mnem, 0);
170
171         evaded = do_evade(bomb_list, esc_list);
172         if (!evaded) {
173             overfly[sect.sct_own]++;
174             for (cn = 1; cn < MAXNOC; cn++) {
175                 if (cn == plane_owner || rel[cn] == ALLIED)
176                     continue;
177                 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
178                     continue;
179                 mpr(cn, "%s planes spotted over %s\n",
180                     cname(plane_owner), xyas(x, y, cn));
181                 if (opt_HIDDEN)
182                     setcont(cn, plane_owner, FOUND_FLY);
183             }
184
185             /* Fire flak */
186             if (rel[sect.sct_own] <= HOSTILE)
187                 ac_doflak(bomb_list, &sect);
188             /* If bombers left, fire flak from units and ships */
189             if (!QEMPTY(bomb_list))
190                 ac_landflak(bomb_list, x, y);
191             if (!QEMPTY(bomb_list))
192                 ac_shipflak(bomb_list, x, y);
193             /* mission planes aborted due to flak -- don't send escorts */
194             if (QEMPTY(bomb_list))
195                 break;
196
197             for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
198                 if (rel[cn] > HOSTILE)
199                     continue;
200                 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
201                              !(cn == sect.sct_own
202                                || gotships[cn] || gotlands[cn]));
203             }
204         }
205
206         if (mission_flags & PM_R) {
207             flags = pln_caps(bomb_list);
208             if (sect.sct_type == SCT_WATER && mission_flags & PM_S)
209                 plane_sweep(bomb_list, x, y);
210             if (sect.sct_type == SCT_WATER && flags & P_A)
211                 plane_sona(bomb_list, x, y, &head);
212         }
213
214         dir = *path++;
215         if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
216             break;
217         x = xnorm(x + diroff[val][0]);
218         y = ynorm(y + diroff[val][1]);
219     }
220
221     /* Let's report all of the overflights even if aborted */
222     for (cn = 1; cn < MAXNOC; cn++) {
223         if (plane_owner == cn)
224             continue;
225         if (overfly[cn] > 0 && rel[cn] != ALLIED)
226             nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
227     }
228     /* If the map changed, update it */
229     if (changed)
230         writemap(plane_owner);
231
232     free_shiplist(&head);
233     for (cn = 1; cn < MAXNOC; cn++)
234         pln_put(&ilist[cn]);
235 }
236
237 static void
238 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
239               natid def_own, natid plane_owner, coord x, coord y,
240               int only_mission)
241 {
242     struct emp_qelem *aqp;
243     struct emp_qelem *anext;
244     struct emp_qelem *dqp;
245     struct emp_qelem *dnext;
246     struct plist *aplp;
247     struct plist *dplp;
248     struct plnstr *pp;
249     int first = 1;
250
251     for (aqp = att_list->q_forw,
252          dqp = def_list->q_forw;
253          aqp != att_list && dqp != def_list; aqp = anext) {
254         anext = aqp->q_forw;
255         aplp = (struct plist *)aqp;
256         if (aplp->pcp->pl_cost < 1000)
257             continue;
258         for (; dqp != def_list; dqp = dnext) {
259             dnext = dqp->q_forw;
260             dplp = (struct plist *)dqp;
261             pp = &dplp->plane;
262             if (!(dplp->pcp->pl_flags & P_M))
263                 continue;
264             if (only_mission && !pp->pln_mission)
265                 continue;
266             if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
267                 continue;
268             if (pp->pln_mission
269                 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
270                 continue;
271             if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
272                 || mission_pln_equip(dplp, NULL, 0) < 0) {
273                 emp_remque(dqp);
274                 free(dqp);
275                 continue;
276             }
277             pp->pln_flags |= PLN_LAUNCHED;
278             putplane(pp->pln_uid, pp);
279             if (first) {
280                 first = 0;
281                 mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
282                 mpr(def_own, "Launching SAMs at %s planes over %s!\n",
283                     cname(plane_owner), xyas(x, y, def_own));
284                 ac_combat_headers(plane_owner, def_own);
285             }
286             ac_dog(aplp, dplp);
287             dqp = dnext;
288             break;
289         }
290     }
291     if (!first) {
292         mpr(plane_owner, "\n");
293         mpr(def_own, "\n");
294     }
295 }
296
297 static void
298 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
299              struct emp_qelem *def_list, natid def_own, coord x, coord y,
300              int only_mission)
301 {
302     struct plnstr *pp;
303     struct plist *plp;
304     int icount;
305     struct emp_qelem *next;
306     struct emp_qelem *qp;
307     struct emp_qelem int_list;
308     int att_count;
309     natid plane_owner;
310     int dist;
311
312     plp = (struct plist *)bomb_list->q_forw;
313     plane_owner = plp->plane.pln_own;
314
315     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
316                   only_mission);
317     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
318                   only_mission);
319
320     att_count = 0;
321     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
322         att_count++;
323     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
324         att_count++;
325     if (!att_count)
326         return;
327
328     emp_initque(&int_list);
329     icount = 0;
330     for (qp = def_list->q_forw; qp != def_list; qp = next) {
331         next = qp->q_forw;
332         plp = (struct plist *)qp;
333         pp = &plp->plane;
334         /* SAMs interdict separately */
335         if (plp->pcp->pl_flags & P_M)
336             continue;
337         if (only_mission && !pp->pln_mission)
338             continue;
339         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
340         if (pp->pln_range < dist)
341             continue;
342         if (pp->pln_mission
343             && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
344             continue;
345         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
346             || mission_pln_equip(plp, NULL, 0) < 0) {
347             emp_remque(qp);
348             free(qp);
349             continue;
350         }
351         /* got one; delete from def_list, add to int_list */
352         emp_remque(qp);
353         emp_insque(qp, &int_list);
354         pp->pln_flags |= PLN_LAUNCHED;
355         pp->pln_mobil -= pln_mobcost(dist, pp, 0);
356         putplane(pp->pln_uid, pp);
357         icount++;
358         if (icount > att_count)
359             break;
360     }
361     if (icount == 0)
362         return;
363     mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
364         icount, cname(def_own), icount == 1 ? " is" : "s are");
365     mpr(def_own, "%d fighter%s intercepting %s planes over %s!\n",
366         icount, icount == 1 ? " is" : "s are", cname(plane_owner),
367         xyas(x, y, def_own));
368     ac_combat_headers(plane_owner, def_own);
369     ac_airtoair(esc_list, &int_list);
370     ac_airtoair(bomb_list, &int_list);
371     mpr(plane_owner, "\n");
372     mpr(def_own, "\n");
373     pln_put(&int_list);
374 }
375
376 static void
377 ac_combat_headers(natid plane_owner, natid def_own)
378 {
379     mpr(plane_owner,
380         " %-10.10s %-10.10s  strength int odds  damage           results\n",
381         cname(plane_owner), cname(def_own));
382     mpr(def_own,
383         " %-10.10s %-10.10s  strength int odds  damage           results\n",
384         cname(def_own), cname(plane_owner));
385 }
386
387 /*
388  * air-to-air combat.
389  */
390 static void
391 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
392 {
393     struct plist *attacker;
394     struct plist *interceptor;
395     struct emp_qelem *att;
396     struct emp_qelem *in;
397     int more_att;
398     int more_int;
399     struct emp_qelem *att_next;
400     struct emp_qelem *in_next;
401
402     att = att_list->q_forw;
403     in = int_list->q_forw;
404     more_att = 1;
405     more_int = 1;
406     if (QEMPTY(att_list) || QEMPTY(int_list)) {
407         more_att = 0;
408         more_int = 0;
409     }
410     while (more_att || more_int) {
411         in_next = in->q_forw;
412         att_next = att->q_forw;
413         attacker = (struct plist *)att;
414         interceptor = (struct plist *)in;
415         ac_dog(attacker, interceptor);
416         in = in_next;
417         att = att_next;
418         if (att == att_list) {
419             more_att = 0;
420             if (QEMPTY(att_list))
421                 more_int = 0;
422             else
423                 att = att->q_forw;
424         }
425         if (in == int_list) {
426             more_int = 0;
427             if (QEMPTY(int_list))
428                 more_att = 0;
429             else
430                 in = in->q_forw;
431         }
432     }
433 }
434
435 static void
436 ac_dog_report(natid to, int intensity, double odds,
437               struct plist *p1, int val1, int dam1, char *dam_mesg1,
438               struct plist *p2, int val2, int dam2, char *dam_mesg2)
439 {
440     mpr(to, " %3.3s #%-4d  %3.3s #%-4d   %3d/%-3d %3d  %3.2f  %3d/%-3d"
441         "%-13.13s %-13.13s\n",
442         p1->pcp->pl_name, p1->plane.pln_uid,
443         p2->pcp->pl_name, p2->plane.pln_uid,
444         val1, val2, intensity, odds, dam1, dam2,
445         dam_mesg1, dam_mesg2);
446 }
447
448 static void
449 ac_dog(struct plist *ap, struct plist *dp)
450 {
451     int att, def;
452     double odds;
453     int intensity, i;
454     natid att_own, def_own;
455     int adam, ddam;
456     char adam_mesg[14], ddam_mesg[14];
457
458     att_own = ap->plane.pln_own;
459     def_own = dp->plane.pln_own;
460
461     att = pln_att(&ap->plane);
462     if (att == 0)
463         att = pln_def(&ap->plane);
464     att = att * ap->plane.pln_effic / 100;
465     att = MAX(att, ap->pcp->pl_def / 2);
466
467     def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
468     def = MAX(def, dp->pcp->pl_def / 2);
469
470     if ((ap->pcp->pl_flags & P_F) && ap->load != 0)
471         att -= 2;
472     if ((dp->pcp->pl_flags & P_F) && dp->load != 0)
473         def -= 2;
474     att += ap->pcp->pl_stealth / 25.0;
475     def += dp->pcp->pl_stealth / 25.0;
476     if (att < 1) {
477         def += 1 - att;
478         att = 1;
479     }
480     if (def < 1) {
481         att += 1 - def;
482         def = 1;
483     }
484     odds = ((double)att / ((double)def + (double)att));
485     if (odds <= 0.05)
486         odds = 0.05;
487     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
488
489     adam = 0;
490     ddam = 0;
491     for (i = 0; i < intensity; i++) {
492         if (chance(odds)) {
493             ddam++;
494             if (dp->plane.pln_effic - ddam < PLANE_MINEFF)
495                 break;
496         } else {
497             adam++;
498             if (ap->plane.pln_effic - adam < PLANE_MINEFF)
499                 break;
500         }
501     }
502
503     if (dp->pcp->pl_flags & P_M)
504         ddam = 100;
505
506     ac_planedamage(ap, def_own, adam, 0, adam_mesg);
507     ac_planedamage(dp, att_own, ddam, 0, ddam_mesg);
508     ac_dog_report(att_own, intensity, odds,
509                   ap, att, adam, adam_mesg,
510                   dp, def, ddam, ddam_mesg);
511     ac_dog_report(def_own, intensity, odds,
512                   dp, def, ddam, ddam_mesg,
513                   ap, att, adam, adam_mesg);
514
515     if (opt_HIDDEN) {
516         setcont(att_own, def_own, FOUND_FLY);
517         setcont(def_own, att_own, FOUND_FLY);
518     }
519 }
520
521 /*
522  * zap plane associated with plp.
523  * Damaging country is "from", damage is "dam".
524  *
525  * NOTE: This routine removes the appropriate plane element from the
526  * queue if it gets destroyed.  That means that the caller must assume
527  * that the current queue pointer is invalid on return from the ac_planedamage
528  * call.  (this has caused bugs in the past)
529  */
530 static void
531 ac_planedamage(struct plist *plp, natid from, int dam, int flak,
532                char *mesg)
533 {
534     int disp = ac_damage_plane(&plp->plane, from, dam, flak, mesg);
535
536     if (disp) {
537         pln_put1(plp);
538     } else
539         putplane(plp->plane.pln_uid, &plp->plane);
540 }
541
542 int
543 ac_damage_plane(struct plnstr *pp, natid from, int dam, int flak,
544                 char *mesg)
545 {
546     int eff, disp;
547
548     *mesg = 0;
549     if (dam <= 0) {
550         if (!flak)
551             snprintf(mesg, 14, " no damage");
552         return 0;
553     }
554
555     eff = pp->pln_effic - dam;
556     if (eff < 0)
557         eff = 0;
558
559     disp = 0;
560     if (eff < PLANE_MINEFF) {
561         snprintf(mesg, 14, " shot down");
562         disp = 1;
563     } else if (eff < 80 && chance((80 - eff) / 100.0)) {
564         snprintf(mesg, 14, " aborted @%2d%%", eff);
565         disp = 2;
566     } else if (!flak)
567         snprintf(mesg, 14, " cleared");
568
569     pp->pln_effic = eff;
570     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
571
572     if (disp == 1 && from != 0 && !(plchr[pp->pln_type].pl_flags & P_M))
573         nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
574     return disp;
575 }
576
577 static void
578 ac_doflak(struct emp_qelem *list, struct sctstr *from)
579 {
580     int gun;
581     natid plane_owner;
582     struct plist *plp;
583
584     plp = (struct plist *)list->q_forw;
585     plane_owner = plp->plane.pln_own;
586
587     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
588     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
589     if (gun > 0) {
590         mpr(plane_owner, "firing %d flak guns in %s...\n",
591             gun, xyas(from->sct_x, from->sct_y, plane_owner));
592         mpr(from->sct_own, "firing %d flak guns in %s...\n",
593             gun, xyas(from->sct_x, from->sct_y, from->sct_own));
594         ac_fireflak(list, from->sct_own, gun);
595     }
596 }
597
598 static void
599 ac_shipflak(struct emp_qelem *list, coord x, coord y)
600 {
601     struct nstr_item ni;
602     struct shpstr ship;
603     struct mchrstr *mcp;
604     double flak, total, ngun;
605     int gun;
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         if (getrel(getnatp(ship.shp_own), plane_owner) > HOSTILE)
622             continue;
623         gun = shp_usable_guns(&ship);
624         if (gun == 0)
625             continue;
626         flak = gun * (ship.shp_effic / 100.0);
627         ngun += flak;
628         total += techfact(ship.shp_tech, flak * 2.0);
629
630         mpr(ship.shp_own, "firing %.0f flak guns from %s...\n",
631             flak, prship(&ship));
632         from = ship.shp_own;
633     }
634
635     /* Limit to FLAK_GUN_MAX guns of average tech factor */
636     if (ngun > FLAK_GUN_MAX)
637         total *= FLAK_GUN_MAX / ngun;
638
639     gun = roundavg(total);
640     if (gun > 0) {
641         mpr(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
642         ac_fireflak(list, from, gun);
643     }
644 }
645
646 static void
647 ac_landflak(struct emp_qelem *list, coord x, coord y)
648 {
649     struct nstr_item ni;
650     struct lndstr land;
651     struct lchrstr *lcp;
652     double flak, total, ngun;
653     int aaf, gun;
654     struct plist *plp;
655     natid plane_owner;
656     natid from;
657
658     plp = (struct plist *)list->q_forw;
659     plane_owner = plp->plane.pln_own;
660
661     total = ngun = 0;
662     snxtitem_xy(&ni, EF_LAND, x, y);
663     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
664         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
665             continue;
666         lcp = &lchr[(int)land.lnd_type];
667         aaf = lnd_aaf(&land);
668         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
669             continue;
670         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
671             continue;
672         if (getrel(getnatp(land.lnd_own), plane_owner) > HOSTILE)
673             continue;
674         flak = aaf * 1.5 * land.lnd_effic / 100.0;
675         ngun += flak;
676         total += techfact(land.lnd_tech, flak * 2.0);
677
678         mpr(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
679             prland(&land), aaf);
680         from = land.lnd_own;
681     }
682
683     /* Limit to FLAK_GUN_MAX guns of average tech factor */
684     if (ngun > FLAK_GUN_MAX)
685         total *= FLAK_GUN_MAX / ngun;
686
687     gun = roundavg(total);
688     if (gun > 0) {
689         mpr(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
690         ac_fireflak(list, from, gun);
691     }
692 }
693
694 /*
695  * Called from shipflak, landflak, and doflak.
696  */
697 static void
698 ac_fireflak(struct emp_qelem *list, natid from, int guns)
699 {
700     struct plist *plp;
701     int n;
702     struct emp_qelem *qp;
703     struct emp_qelem *next;
704     char msg[14];
705
706     for (qp = list->q_forw; qp != list; qp = next) {
707         next = qp->q_forw;
708         plp = (struct plist *)qp;
709         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
710         ac_planedamage(plp, from, n, 1, msg);
711         mpr(plp->plane.pln_own, "    %s takes %d%s%s.\n",
712             prplane(&plp->plane), n, *msg ? " --" : "", msg);
713     }
714 }
715
716 /*
717  * Calculate flak damage
718  */
719 int
720 ac_flak_dam(int guns, int def, int pl_flags)
721 {
722     int flak, dam;
723     float mult;
724     /*                             <-7      -7     -6     -5     -4 */
725     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
726     /*    -3     -2     -1      0     +1     +2     +3     +4 */
727          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
728     /*    +5    +6     +7     +8    >+8 */
729          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
730     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
731
732     flak = guns - def;
733     if ((pl_flags & P_T) == 0)
734         flak--;
735
736     if (flak > 8)
737         mult = flaktable[FLAK_MAX];
738     else if (flak < -7)
739         mult = flaktable[0];
740     else {
741         flak += 8;
742         mult = flaktable[flak];
743     }
744     mult *= flakscale;
745     dam = (int)((roll(8) + 2) * mult);
746     if (dam > 100)
747         dam = 100;
748     return dam;
749 }
750
751 /*
752  * Get planes available for interception duties.
753  */
754 static void
755 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
756 {
757     natid cn;
758     struct plchrstr *pcp;
759     struct plnstr plane;
760     struct nstr_item ni;
761     struct plist *ip;
762
763     rel[0] = NEUTRAL;
764     for (cn = 1; cn < MAXNOC; cn++) {
765         rel[cn] = getrel(getnatp(cn), intruder);
766         emp_initque(&list[cn]);
767     }
768
769     snxtitem_all(&ni, EF_PLANE);
770     while (nxtitem(&ni, &plane)) {
771         if (rel[plane.pln_own] > HOSTILE)
772             continue;
773         pcp = &plchr[(int)plane.pln_type];
774         if ((pcp->pl_flags & P_F) == 0)
775             continue;
776         if (plane.pln_flags & PLN_LAUNCHED)
777             continue;
778         if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
779             continue;
780         if (plane.pln_mobil <= 0)
781             continue;
782         if (plane.pln_effic < 40)
783             continue;
784         if (!pln_airbase_ok(&plane, 0, 0))
785             continue;
786         /* got one! */
787         ip = malloc(sizeof(*ip));
788         ip->load = 0;
789         ip->pcp = &plchr[(int)plane.pln_type];
790         ip->plane = plane;
791         emp_insque(&ip->queue, &list[plane.pln_own]);
792     }
793 }
794
795 static int
796 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
797 {
798     struct emp_qelem *qp;
799     double evade;
800     struct plist *plp;
801
802     evade = 100.0;
803     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
804         plp = (struct plist *)qp;
805         if (evade > plp->pcp->pl_stealth / 100.0)
806             evade = plp->pcp->pl_stealth / 100.0;
807     }
808     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
809         plp = (struct plist *)qp;
810         if (evade > plp->pcp->pl_stealth / 100.0)
811             evade = plp->pcp->pl_stealth / 100.0;
812     }
813
814     if (chance(evade))
815         return 1;
816
817     return 0;
818 }