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