]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
2783d7682226874f55fe857e5a9307c35a0dfe82
[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             mpr(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                 mpr(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                 mpr(plane_owner, "%s launches SAMs!\n", cname(def_own));
280                 mpr(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         mpr(plane_owner, "\n");
291         mpr(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     mpr(plane_owner, "%d %s fighter%s rising to intercept!\n",
362         icount, cname(def_own), icount == 1 ? " is" : "s are");
363     mpr(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     mpr(plane_owner, "\n");
370     mpr(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     mpr(plane_owner,
378         " %-10.10s %-10.10s  strength int odds  damage           results\n",
379         cname(plane_owner), cname(def_own));
380     mpr(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         mpr(plane_owner, "firing %d flak guns in %s...\n",
589             gun, xyas(from->sct_x, from->sct_y, plane_owner));
590         mpr(from->sct_own, "firing %d flak guns in %s...\n",
591             gun, xyas(from->sct_x, from->sct_y, from->sct_own));
592         ac_fireflak(list, from->sct_own, gun);
593     }
594 }
595
596 static void
597 ac_shipflak(struct emp_qelem *list, coord x, coord y)
598 {
599     struct nstr_item ni;
600     struct shpstr ship;
601     struct mchrstr *mcp;
602     double flak, total, ngun;
603     int gun;
604     int rel;
605     struct plist *plp;
606     natid plane_owner;
607     natid from;
608
609     plp = (struct plist *)list->q_forw;
610     plane_owner = plp->plane.pln_own;
611
612     total = ngun = 0;
613     snxtitem_xy(&ni, EF_SHIP, x, y);
614     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
615         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
616             continue;
617         mcp = &mchr[(int)ship.shp_type];
618         if (mcp->m_flags & M_SUB)
619             continue;
620         rel = getrel(getnatp(ship.shp_own), plane_owner);
621         if (rel > 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     int rel;
655     struct plist *plp;
656     natid plane_owner;
657     natid from;
658
659     plp = (struct plist *)list->q_forw;
660     plane_owner = plp->plane.pln_own;
661
662     total = ngun = 0;
663     snxtitem_xy(&ni, EF_LAND, x, y);
664     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
665         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
666             continue;
667         lcp = &lchr[(int)land.lnd_type];
668         aaf = lnd_aaf(&land);
669         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
670             continue;
671         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
672             continue;
673         rel = getrel(getnatp(land.lnd_own), plane_owner);
674         if (rel > HOSTILE)
675             continue;
676         flak = aaf * 1.5 * land.lnd_effic / 100.0;
677         ngun += flak;
678         total += techfact(land.lnd_tech, flak * 2.0);
679
680         mpr(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
681             prland(&land), aaf);
682         from = land.lnd_own;
683     }
684
685     /* Limit to FLAK_GUN_MAX guns of average tech factor */
686     if (ngun > FLAK_GUN_MAX)
687         total *= FLAK_GUN_MAX / ngun;
688
689     gun = roundavg(total);
690     if (gun > 0) {
691         mpr(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
692         ac_fireflak(list, from, gun);
693     }
694 }
695
696 /*
697  * Called from shipflak, landflak, and doflak.
698  */
699 static void
700 ac_fireflak(struct emp_qelem *list, natid from, int guns)
701 {
702     struct plist *plp;
703     int n;
704     struct emp_qelem *qp;
705     struct emp_qelem *next;
706     char msg[14];
707
708     for (qp = list->q_forw; qp != list; qp = next) {
709         next = qp->q_forw;
710         plp = (struct plist *)qp;
711         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
712         ac_planedamage(plp, from, n, 1, msg);
713         mpr(plp->plane.pln_own, "    %s takes %d%s%s.\n",
714             prplane(&plp->plane), n, *msg ? " --" : "", msg);
715     }
716 }
717
718 /*
719  * Calculate flak damage
720  */
721 int
722 ac_flak_dam(int guns, int def, int pl_flags)
723 {
724     int flak, dam;
725     float mult;
726     /*                             <-7      -7     -6     -5     -4 */
727     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
728     /*    -3     -2     -1      0     +1     +2     +3     +4 */
729          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
730     /*    +5    +6     +7     +8    >+8 */
731          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
732     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
733
734     flak = guns - def;
735     if ((pl_flags & P_T) == 0)
736         flak--;
737
738     if (flak > 8)
739         mult = flaktable[FLAK_MAX];
740     else if (flak < -7)
741         mult = flaktable[0];
742     else {
743         flak += 8;
744         mult = flaktable[flak];
745     }
746     mult *= flakscale;
747     dam = (int)((roll(8) + 2) * mult);
748     if (dam > 100)
749         dam = 100;
750     return dam;
751 }
752
753 /*
754  * Get planes available for interception duties.
755  */
756 static void
757 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
758 {
759     natid cn;
760     struct plchrstr *pcp;
761     struct plnstr plane;
762     struct nstr_item ni;
763     struct plist *ip;
764
765     rel[0] = NEUTRAL;
766     for (cn = 1; cn < MAXNOC; cn++) {
767         rel[cn] = getrel(getnatp(cn), intruder);
768         emp_initque(&list[cn]);
769     }
770
771     snxtitem_all(&ni, EF_PLANE);
772     while (nxtitem(&ni, &plane)) {
773         if (rel[plane.pln_own] > HOSTILE)
774             continue;
775         pcp = &plchr[(int)plane.pln_type];
776         if ((pcp->pl_flags & P_F) == 0)
777             continue;
778         if (plane.pln_flags & PLN_LAUNCHED)
779             continue;
780         if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
781             continue;
782         if (plane.pln_mobil <= 0)
783             continue;
784         if (plane.pln_effic < 40)
785             continue;
786         if (!pln_airbase_ok(&plane, 0, 0))
787             continue;
788         /* got one! */
789         ip = malloc(sizeof(*ip));
790         ip->load = 0;
791         ip->pcp = &plchr[(int)plane.pln_type];
792         ip->plane = plane;
793         emp_insque(&ip->queue, &list[plane.pln_own]);
794     }
795 }
796
797 static int
798 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
799 {
800     struct emp_qelem *qp;
801     double evade;
802     struct plist *plp;
803
804     evade = 100.0;
805     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
806         plp = (struct plist *)qp;
807         if (evade > plp->pcp->pl_stealth / 100.0)
808             evade = plp->pcp->pl_stealth / 100.0;
809     }
810     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
811         plp = (struct plist *)qp;
812         if (evade > plp->pcp->pl_stealth / 100.0)
813             evade = plp->pcp->pl_stealth / 100.0;
814     }
815
816     if (chance(evade))
817         return 1;
818
819     return 0;
820 }