]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
Fix SAM interception for intercepts other than the first
[empserver] / src / lib / subs / aircombat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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-2008
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 "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 int plane_caps(struct emp_qelem *);
58 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
59                          struct emp_qelem *, natid, coord, coord);
60 static void ac_dog(struct plist *, struct plist *);
61 static void ac_planedamage(struct plist *, natid, int, natid, int,
62                            int, char *);
63 static void ac_doflak(struct emp_qelem *, struct sctstr *);
64 static void ac_landflak(struct emp_qelem *, coord, coord);
65 static void ac_shipflak(struct emp_qelem *, coord, coord);
66 static void ac_fireflak(struct emp_qelem *, natid, int);
67 static void getilist(struct emp_qelem *, natid);
68 static int do_evade(struct emp_qelem *, struct emp_qelem *);
69
70 void
71 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
72              coord x, coord y, char *path, int mission_flags,
73              int no_air_defense)
74 {
75     int val;
76     int rel;
77     int dir;
78     int nats[MAXNOC];
79     int lnats[MAXNOC];
80     int gotilist[MAXNOC];
81     int unfriendly[MAXNOC];
82     int overfly[MAXNOC];
83     int flags;
84     struct emp_qelem ilist[MAXNOC];
85     char mypath[1024];
86     int myp;
87     int civ, mil;
88     natid plane_owner;
89     struct sctstr sect;
90     struct shpstr ship;
91     struct lndstr land;
92     struct nstr_item ni;
93     natid cn;
94     struct natstr *mynatp;
95     struct plist *plp;
96     int evaded;
97     struct shiplist *head = NULL;
98     int changed = 0;
99 /* We want to only intercept once per sector per owner.  So, if we overfly
100    a sector, and then overfly some land units or ships, we don't want to
101    potentially intercept 3 times. */
102
103     plp = (struct plist *)bomb_list->q_forw;
104     plane_owner = plp->plane.pln_own;
105
106     strncpy(mypath, path, sizeof(mypath));
107     myp = 0;
108
109     memset(overfly, 0, sizeof(overfly));
110     memset(gotilist, 0, sizeof(gotilist));
111     memset(unfriendly, 0, sizeof(unfriendly));
112     for (cn = 1; cn < MAXNOC; cn++) {
113         if ((mynatp = getnatp(cn)) == 0)
114             continue;
115         rel = getrel(mynatp, plane_owner);
116         if (rel > HOSTILE)
117             continue;
118         if (plane_owner == cn)
119             continue;
120         unfriendly[cn]++;
121     }
122     if (mission_flags & PM_R) {
123         flags = plane_caps(bomb_list);
124         if (flags & P_S) {
125             PR(plane_owner, "\nSPY Plane report\n");
126             PRdate(plane_owner);
127             sathead();
128         } else if (flags & P_A) {
129             PR(plane_owner, "\nAnti-Sub Patrol report\n");
130         } else {
131             PR(plane_owner, "\nReconnaissance report\n");
132             PRdate(plane_owner);
133         }
134     }
135
136     while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
137         if ((val = diridx(dir)) == DIR_STOP)
138             break;
139         /* XXX using xnorm is probably bad */
140         x = xnorm(x + diroff[val][0]);
141         y = ynorm(y + diroff[val][1]);
142         getsect(x, y, &sect);
143
144         if (mission_flags & PM_R) {
145             flags = plane_caps(bomb_list);
146             if (opt_HIDDEN)
147                 setcont(plane_owner, sect.sct_own, FOUND_FLY);
148             if (sect.sct_type == SCT_WATER) {
149                 PR(plane_owner, "flying over %s at %s\n",
150                    dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
151                 if (mission_flags & PM_S)
152                     plane_sweep(bomb_list, x, y);
153                 if (flags & P_A)
154                     plane_sona(bomb_list, x, y, &head);
155                 changed += map_set(plane_owner,
156                                    sect.sct_x, sect.sct_y,
157                                    dchr[sect.sct_type].d_mnem, 0);
158             } else if (flags & P_S) {
159                 satdisp_sect(&sect, flags & P_I ? 10 : 50);
160             } else {
161                 /* This is borrowed from lookout */
162                 if (sect.sct_own == plane_owner)
163                     PR(plane_owner, "Your ");
164                 else
165                     PR(plane_owner, "%s (#%d) ",
166                        cname(sect.sct_own), sect.sct_own);
167                 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
168                 changed += map_set(plane_owner,
169                                    sect.sct_x, sect.sct_y,
170                                    dchr[sect.sct_type].d_mnem, 0);
171                 PR(plane_owner, " %d%% efficient ",
172                    (sect.sct_own == plane_owner) ?
173                    sect.sct_effic : roundintby((int)sect.sct_effic, 25));
174                 civ = sect.sct_item[I_CIVIL];
175                 mil = sect.sct_item[I_MILIT];
176                 if (civ)
177                     PR(plane_owner, "with %s%d civ ",
178                        (sect.sct_own == plane_owner) ?
179                        "" : "approx ",
180                        (sect.sct_own == plane_owner) ?
181                        civ : roundintby(civ, 25));
182                 if (mil)
183                     PR(plane_owner, "with %s%d mil ",
184                        (sect.sct_own == plane_owner) ?
185                        "" : "approx ",
186                        (sect.sct_own == plane_owner) ?
187                        mil : roundintby(mil, 25));
188                 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
189             }
190             if (flags & P_S)
191                 satdisp_units(sect.sct_x, sect.sct_y);
192         } else {
193             PR(plane_owner, "flying over %s at %s\n",
194                dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
195             changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
196                                dchr[sect.sct_type].d_mnem, 0);
197         }
198
199         evaded = do_evade(bomb_list, esc_list);
200         if (evaded)
201             continue;
202
203         if (sect.sct_own != 0 && sect.sct_own != plane_owner
204             && getrel(getnatp(sect.sct_own), plane_owner) != ALLIED) {
205             overfly[sect.sct_own]++;
206             PR(sect.sct_own, "%s planes spotted over %s\n",
207                cname(plane_owner), xyas(x, y, sect.sct_own));
208             if (opt_HIDDEN)
209                 setcont(sect.sct_own, plane_owner, FOUND_FLY);
210         }
211
212         /* Fire flak */
213         if (unfriendly[sect.sct_own])
214             ac_doflak(bomb_list, &sect);
215         /* If bombers left, fire flak from units and ships */
216         if (!QEMPTY(bomb_list))
217             ac_landflak(bomb_list, x, y);
218         if (!QEMPTY(bomb_list))
219             ac_shipflak(bomb_list, x, y);
220         /* mission planes aborted due to flak -- don't send escorts */
221         if (QEMPTY(bomb_list))
222             break;
223
224         if (!no_air_defense)
225             air_defense(x, y, plane_owner, bomb_list, esc_list);
226
227         if (sect.sct_own == 0 || sect.sct_own == plane_owner)
228             continue;
229
230         if (unfriendly[sect.sct_own]) {
231             if (!gotilist[sect.sct_own]) {
232                 getilist(&ilist[sect.sct_own], sect.sct_own);
233                 gotilist[sect.sct_own]++;
234             }
235             ac_intercept(bomb_list, esc_list, &ilist[sect.sct_own],
236                          sect.sct_own, x, y);
237         }
238     }
239
240     /* Let's report all of the overflights even if aborted */
241     for (cn = 1; cn < MAXNOC; cn++) {
242         if (plane_owner == cn)
243             continue;
244         if (overfly[cn] > 0)
245             nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
246     }
247     /* If the map changed, update it */
248     if (changed)
249         writemap(player->cnum);
250     /* Now, if the bomber and escort lists are empty, we are done */
251     if (QEMPTY(bomb_list) && QEMPTY(esc_list))
252         goto out;
253
254     /* Something made it through */
255     /* Go figure out if there are ships in this sector, and who's they are */
256     memset(nats, 0, sizeof(nats));
257     snxtitem_xy(&ni, EF_SHIP, x, y);
258     while (nxtitem(&ni, &ship)) {
259         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
260             continue;
261         nats[ship.shp_own]++;
262     }
263     /* Go figure out if there are units in this sector, and who's they are */
264     memset(lnats, 0, sizeof(lnats));
265     snxtitem_xy(&ni, EF_LAND, x, y);
266     while (nxtitem(&ni, &land)) {
267         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
268             continue;
269         lnats[land.lnd_own]++;
270     }
271
272     /* Now, let's make life a little rougher. */
273     for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
274         if (plane_owner == cn)
275             continue;
276         if (nats[cn] != 0) {
277             PR(plane_owner, "Flying over %s ships in %s\n",
278                cname(cn), xyas(x, y, plane_owner));
279             PR(cn, "%s planes spotted over ships in %s\n",
280                cname(plane_owner), xyas(x, y, cn));
281         }
282         if (lnats[cn] != 0) {
283             PR(plane_owner, "Flying over %s land units in %s\n",
284                cname(cn), xyas(x, y, plane_owner));
285             PR(cn, "%s planes spotted over land units in %s\n",
286                cname(plane_owner), xyas(x, y, cn));
287         }
288         if (nats[cn] || lnats[cn]) {
289             if (opt_HIDDEN)
290                 setcont(cn, plane_owner, FOUND_FLY);
291             if (unfriendly[cn] && !evaded) {
292                 if (!gotilist[cn]) {
293                     getilist(&ilist[cn], cn);
294                     gotilist[cn]++;
295                 }
296                 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y);
297             }
298         }
299     }
300 out:
301     free_shiplist(&head);
302     for (cn = 1; cn < MAXNOC; cn++) {
303         if (gotilist[cn])
304             pln_put(&ilist[cn]);
305     }
306 }
307
308 static int
309 plane_caps(struct emp_qelem *list)
310 {
311     struct emp_qelem *qp;
312     struct plist *plp;
313     int fl;
314
315     fl = 0;
316     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
317         plp = (struct plist *)qp;
318         fl |= plp->pcp->pl_flags;
319     }
320
321     return fl;
322 }
323
324 void
325 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
326               natid def_own, natid plane_owner, coord x, coord y,
327               int delete_missiles)
328 {
329     struct emp_qelem *aqp;
330     struct emp_qelem *anext;
331     struct emp_qelem *dqp;
332     struct emp_qelem *dnext;
333     struct plist *aplp;
334     struct plist *dplp;
335     int first = 1;
336
337     for (aqp = att_list->q_forw,
338          dqp = def_list->q_forw;
339          aqp != att_list && dqp != def_list; aqp = anext) {
340         anext = aqp->q_forw;
341         aplp = (struct plist *)aqp;
342         if (aplp->pcp->pl_cost < 1000)
343             continue;
344         for (; dqp != def_list; dqp = dnext) {
345             dnext = dqp->q_forw;
346             dplp = (struct plist *)dqp;
347             if (!(dplp->pcp->pl_flags & P_M))
348                 continue;
349
350             if (dplp->plane.pln_range <
351                 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y))
352                 continue;
353             if (CANT_HAPPEN(dplp->plane.pln_flags & PLN_LAUNCHED)
354                 || mission_pln_equip(dplp, 0, P_F, 0) < 0) {
355                 emp_remque(dqp);
356                 free(dqp);
357                 continue;
358             }
359             dplp->plane.pln_flags |= PLN_LAUNCHED;
360             putplane(dplp->plane.pln_uid, &dplp->plane);
361             if (first) {
362                 first = 0;
363                 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
364                 PR(def_own, "Launching SAMs at %s planes over %s!\n",
365                    cname(plane_owner), xyas(x, y, def_own));
366                 ac_combat_headers(plane_owner, def_own);
367             }
368             ac_dog(aplp, dplp);
369             dqp = dnext;
370             break;
371         }
372     }
373     if (!first) {
374         PR(plane_owner, "\n");
375         PR(def_own, "\n");
376     }
377     if (delete_missiles) {
378         for (; dqp != def_list; dqp = dnext) {
379             dnext = dqp->q_forw;
380             dplp = (struct plist *)dqp;
381             if (!(dplp->pcp->pl_flags & P_M))
382                 continue;
383             emp_remque(dqp);
384             free(dqp);
385             continue;
386         }
387     }
388 }
389
390 static void
391 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
392              struct emp_qelem *def_list, natid def_own, coord x, coord y)
393 {
394     struct plnstr *pp;
395     struct plist *plp;
396     int icount;
397     struct emp_qelem *next;
398     struct emp_qelem *qp;
399     struct emp_qelem int_list;
400     int att_count;
401     natid plane_owner;
402     int dist;
403
404     plp = (struct plist *)bomb_list->q_forw;
405     plane_owner = plp->plane.pln_own;
406
407     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
408     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 0);
409
410     att_count = 0;
411     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
412         att_count++;
413     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
414         att_count++;
415     if (!att_count)
416         return;
417
418     emp_initque(&int_list);
419     icount = 0;
420     for (qp = def_list->q_forw; qp != def_list; qp = next) {
421         next = qp->q_forw;
422         plp = (struct plist *)qp;
423         pp = &plp->plane;
424         /* SAMs interdict separately */
425         if (plp->pcp->pl_flags & P_M)
426             continue;
427         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
428         if (pp->pln_range < dist)
429             continue;
430         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
431             || mission_pln_equip(plp, 0, P_F, 0) < 0) {
432             emp_remque(qp);
433             free(qp);
434             continue;
435         }
436         /* got one; delete from def_list, add to int_list */
437         emp_remque(qp);
438         emp_insque(qp, &int_list);
439         pp->pln_flags |= PLN_LAUNCHED;
440         pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
441         putplane(pp->pln_uid, pp);
442         icount++;
443         if (icount > att_count)
444             break;
445     }
446     if (icount == 0)
447         return;
448     PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
449        icount, cname(def_own), icount == 1 ? " is" : "s are");
450     PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
451        icount, icount == 1 ? " is" : "s are", cname(plane_owner),
452        xyas(x, y, def_own));
453     ac_combat_headers(plane_owner, def_own);
454     ac_airtoair(esc_list, &int_list);
455     ac_airtoair(bomb_list, &int_list);
456     PR(plane_owner, "\n");
457     PR(def_own, "\n");
458     pln_put(&int_list);
459 }
460
461 void
462 ac_combat_headers(natid plane_owner, natid def_own)
463 {
464     PR(plane_owner,
465        " %-10.10s %-10.10s  strength int odds  damage           results\n",
466        cname(plane_owner), cname(def_own));
467     PR(def_own,
468        " %-10.10s %-10.10s  strength int odds  damage           results\n",
469        cname(def_own), cname(plane_owner));
470 }
471
472 /*
473  * air-to-air combat.
474  */
475 void
476 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
477 {
478     struct plist *attacker;
479     struct plist *interceptor;
480     struct emp_qelem *att;
481     struct emp_qelem *in;
482     int nplanes;
483     int more_att;
484     int more_int;
485     struct emp_qelem *att_next;
486     struct emp_qelem *in_next;
487
488     att = att_list->q_forw;
489     in = int_list->q_forw;
490     more_att = 1;
491     more_int = 1;
492     if (QEMPTY(att_list) || QEMPTY(int_list)) {
493         more_att = 0;
494         more_int = 0;
495     }
496     while (more_att || more_int) {
497         in_next = in->q_forw;
498         att_next = att->q_forw;
499         attacker = (struct plist *)att;
500         interceptor = (struct plist *)in;
501         nplanes = attacker->plane.pln_effic;
502         if (nplanes > interceptor->plane.pln_effic)
503             nplanes = interceptor->plane.pln_effic;
504         ac_dog(attacker, interceptor);
505         in = in_next;
506         att = att_next;
507         if (att == att_list) {
508             more_att = 0;
509             if (QEMPTY(att_list))
510                 more_int = 0;
511             else
512                 att = att->q_forw;
513         }
514         if (in == int_list) {
515             more_int = 0;
516             if (QEMPTY(int_list))
517                 more_att = 0;
518             else
519                 in = in->q_forw;
520         }
521     }
522 }
523
524 static void
525 ac_dog(struct plist *ap, struct plist *dp)
526 {
527     int att, def;
528     double odds;
529     int intensity;
530     natid att_own, def_own;
531     int adam, ddam;
532     char mesg[1024];
533     char temp[14];
534
535     att_own = ap->plane.pln_own;
536     def_own = dp->plane.pln_own;
537
538     PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
539        ap->pcp->pl_name,
540        ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
541     if (def_own)
542         PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
543            dp->pcp->pl_name,
544            dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
545     att = pln_att(&ap->plane);
546     if (att == 0)
547         att = pln_def(&ap->plane);
548     att = att * ap->plane.pln_effic / 100;
549     att = MAX(att, ap->pcp->pl_def / 2);
550
551     def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
552     def = MAX(def, dp->pcp->pl_def / 2);
553
554     if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
555         att -= 2;
556     if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
557         def -= 2;
558     att += ap->pcp->pl_stealth / 25.0;
559     def += dp->pcp->pl_stealth / 25.0;
560     if (att < 1) {
561         def += 1 - att;
562         att = 1;
563     }
564     if (def < 1) {
565         att += 1 - def;
566         def = 1;
567     }
568     odds = ((double)att / ((double)def + (double)att));
569     if (odds <= 0.05)
570         odds = 0.05;
571     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
572
573     PR(att_own, "   %3d/%-3d %3d  %3.2f  ", att, def, intensity, odds);
574     PR(def_own, "   %3d/%-3d %3d  %3.2f  ", def, att, intensity, odds);
575
576     adam = 0;
577     ddam = 0;
578     while ((intensity--) > 0) {
579
580         if (chance(odds)) {
581             ddam += 1;
582             if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
583                 intensity = 0;
584         } else {
585             adam += 1;
586             if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
587                 intensity = 0;
588         }
589     }
590
591     if (dp->pcp->pl_flags & P_M)
592         ddam = 100;
593
594     PR(att_own, "%3d/%-3d", adam, ddam);
595     PR(def_own, "%3d/%-3d", ddam, adam);
596     ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
597     strncpy(temp, mesg, 14);
598     ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
599     PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
600     PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
601
602     if (opt_HIDDEN) {
603         setcont(att_own, def_own, FOUND_FLY);
604         setcont(def_own, att_own, FOUND_FLY);
605     }
606 }
607
608 /*
609  * zap plane associated with plp.
610  * Damaging country is "from", damage is "dam".
611  * def_own is the country on the other side of the conflict from the plane
612  * owner. The only time def_own != from is when the interceptor is getting
613  * damaged.
614  *
615  * NOTE: This routine removes the appropriate plane element from the
616  * queue if it gets destroyed.  That means that the caller must assume
617  * that the current queue pointer is invalid on return from the ac_planedamage
618  * call.  (this has caused bugs in the past)
619  */
620 static void
621 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
622                int checkabort, int show, char *mesg)
623 {
624     struct plnstr *pp;
625     int disp;
626     char dmess[255];
627     int eff;
628     natid plane_owner;
629
630     disp = 0;
631     pp = &plp->plane;
632     plane_owner = pp->pln_own;
633     eff = pp->pln_effic;
634     sprintf(dmess, " no damage");
635     if (dam <= 0) {
636         strcpy(mesg, dmess);
637         return;
638     }
639     memset(dmess, 0, sizeof(dmess));
640     eff -= dam;
641     if (eff < 0)
642         eff = 0;
643     if (eff < PLANE_MINEFF) {
644         sprintf(dmess, " shot down");
645         disp = 1;
646     } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
647         sprintf(dmess, " aborted @%2d%%", eff);
648         disp = 2;
649     } else if (show == 0) {
650         sprintf(dmess, " cleared");
651     }
652
653     if ((plp->pcp->pl_flags & P_M) == 0) {
654         if (show) {
655             PR(plane_owner, "    %s %s takes %d%s.\n",
656                cname(pp->pln_own), prplane(pp), dam, dmess);
657             if (other)
658                 PR(other, "    %s %s takes %d%s.\n",
659                    cname(pp->pln_own), prplane(pp), dam, dmess);
660         }
661     }
662     if (show && checkabort == 1) {
663         PR(plane_owner, "\n");
664         if (other)
665             PR(other, "\n");
666     }
667
668     pp->pln_effic = eff;
669     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
670     if (disp) {
671         if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
672             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
673         pln_put1(plp);
674     } else
675         putplane(pp->pln_uid, pp);
676     strcpy(mesg, dmess);
677 }
678
679 static void
680 ac_doflak(struct emp_qelem *list, struct sctstr *from)
681 {
682     int shell;
683     int gun;
684     natid plane_owner;
685     struct plist *plp;
686
687     plp = (struct plist *)list->q_forw;
688     plane_owner = plp->plane.pln_own;
689
690     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
691     shell = from->sct_item[I_SHELL];
692     if (gun > shell * 2) {
693         shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
694                                I_SHELL, (gun + 1) / 2 - shell);
695         from->sct_item[I_SHELL] = shell;
696         putsect(from);
697     }
698     if (gun > shell * 2)
699         gun = shell * 2;
700
701     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
702     if (gun > 0) {
703         PR(plane_owner, "firing %d flak guns in %s...\n",
704            gun, xyas(from->sct_x, from->sct_y, plane_owner));
705         if (from->sct_own != 0)
706             PR(from->sct_own, "firing %d flak guns in %s...\n",
707                gun, xyas(from->sct_x, from->sct_y, from->sct_own));
708         ac_fireflak(list, from->sct_own, gun);
709     }
710 }
711
712 static void
713 ac_shipflak(struct emp_qelem *list, coord x, coord y)
714 {
715     struct nstr_item ni;
716     struct shpstr ship;
717     struct mchrstr *mcp;
718     double flak, total, ngun;
719     int gun, shell;
720     int rel;
721     struct plist *plp;
722     natid plane_owner;
723     natid from;
724     int nats[MAXNOC];
725
726     plp = (struct plist *)list->q_forw;
727     plane_owner = plp->plane.pln_own;
728
729     memset(nats, 0, sizeof(nats));
730     total = ngun = 0;
731     snxtitem_xy(&ni, EF_SHIP, x, y);
732     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
733         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
734             continue;
735         mcp = &mchr[(int)ship.shp_type];
736         if (mcp->m_flags & M_SUB)
737             continue;
738         rel = getrel(getnatp(ship.shp_own), plane_owner);
739         if (rel > HOSTILE)
740             continue;
741         shell = 0;
742         gun = shp_usable_guns(&ship);
743         if (gun) {
744             shell = ship.shp_item[I_SHELL];
745             if (shell <= 0) {
746                 shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
747                                       I_SHELL, 1);
748                 ship.shp_item[I_SHELL] = shell;
749                 putship(ship.shp_uid, &ship);
750             }
751         }
752         if (gun == 0 || shell == 0)
753             continue;
754         flak = gun * (ship.shp_effic / 100.0);
755         ngun += flak;
756         total += techfact(ship.shp_tech, flak * 2.0);
757
758         if (!nats[ship.shp_own]) {
759             /* First time here, print the message */
760             PR(ship.shp_own, "%s planes spotted over ships in %s\n",
761                cname(plane_owner), xyas(x, y, ship.shp_own));
762             PR(plane_owner, "Flying over %s ships in %s\n",
763                cname(ship.shp_own), xyas(x, y, plane_owner));
764             nats[ship.shp_own] = 1;
765         }
766         PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
767            flak, prship(&ship));
768         from = ship.shp_own;
769     }
770
771     /* Limit to FLAK_GUN_MAX guns of average tech factor */
772     if (ngun > FLAK_GUN_MAX)
773         total *= FLAK_GUN_MAX / ngun;
774
775     gun = roundavg(total);
776     if (gun > 0) {
777         PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
778         ac_fireflak(list, from, gun);
779     }
780 }
781
782 static void
783 ac_landflak(struct emp_qelem *list, coord x, coord y)
784 {
785     struct nstr_item ni;
786     struct lndstr land;
787     struct lchrstr *lcp;
788     double flak, total, ngun;
789     int aaf, gun;
790     int rel;
791     struct plist *plp;
792     natid plane_owner;
793     natid from;
794     int nats[MAXNOC];
795
796     plp = (struct plist *)list->q_forw;
797     plane_owner = plp->plane.pln_own;
798
799     memset(nats, 0, sizeof(nats));
800     total = ngun = 0;
801     snxtitem_xy(&ni, EF_LAND, x, y);
802     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
803         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
804             continue;
805         lcp = &lchr[(int)land.lnd_type];
806         aaf = lnd_aaf(&land);
807         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
808             continue;
809         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
810             continue;
811         rel = getrel(getnatp(land.lnd_own), plane_owner);
812         if (rel > HOSTILE)
813             continue;
814         flak = aaf * 1.5 * land.lnd_effic / 100.0;
815         ngun += flak;
816         total += techfact(land.lnd_tech, flak * 2.0);
817
818         if (!nats[land.lnd_own]) {
819             /* First time here, print the message */
820             PR(land.lnd_own, "%s planes spotted over land units in %s\n",
821                cname(plane_owner), xyas(x, y, land.lnd_own));
822             PR(plane_owner, "Flying over %s land units in %s\n",
823                cname(land.lnd_own), xyas(x, y, plane_owner));
824             nats[land.lnd_own] = 1;
825         }
826         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
827            prland(&land), aaf);
828         from = land.lnd_own;
829     }
830
831     /* Limit to FLAK_GUN_MAX guns of average tech factor */
832     if (ngun > FLAK_GUN_MAX)
833         total *= FLAK_GUN_MAX / ngun;
834
835     gun = roundavg(total);
836     if (gun > 0) {
837         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
838         ac_fireflak(list, from, gun);
839     }
840 }
841
842 /*
843  * Called from shipflak, landflak, and doflak.
844  */
845 static void
846 ac_fireflak(struct emp_qelem *list, natid from, int guns)
847 {
848     struct plist *plp;
849     int n;
850     struct emp_qelem *qp;
851     struct emp_qelem *next;
852     char msg[255];
853
854     plp = (struct plist *)list->q_forw;
855
856     for (qp = list->q_forw; qp != list; qp = next) {
857         next = qp->q_forw;
858         plp = (struct plist *)qp;
859         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
860         ac_planedamage(plp, from, n, 0, 2, 1, msg);
861     }
862 }
863
864 /*
865  * Calculate flak damage
866  */
867 int
868 ac_flak_dam(int guns, int def, int pl_flags)
869 {
870     int flak, dam;
871     float mult;
872     /*                             <-7      -7     -6     -5     -4 */
873     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
874     /*    -3     -2     -1      0     +1     +2     +3     +4 */
875          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
876     /*    +5    +6     +7     +8    >+8 */
877          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
878     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
879
880     flak = guns - def;
881     if ((pl_flags & P_T) == 0)
882         flak--;
883     if (pl_flags & P_X)
884         flak -= 2;
885     if (pl_flags & P_H)
886         flak -= 1;
887
888     if (flak > 8)
889         mult = flaktable[FLAK_MAX];
890     else if (flak < -7)
891         mult = flaktable[0];
892     else {
893         flak += 8;
894         mult = flaktable[flak];
895     }
896     mult *= flakscale;
897     dam = (int)((roll(8) + 2) * mult);
898     if (dam > 100)
899         dam = 100;
900     return dam;
901 }
902
903 /*
904  * Get a list of planes available for interception duties.
905  */
906 static void
907 getilist(struct emp_qelem *list, natid own)
908 {
909     struct plchrstr *pcp;
910     struct plnstr plane;
911     struct nstr_item ni;
912     struct plist *ip;
913
914     emp_initque(list);
915     snxtitem_all(&ni, EF_PLANE);
916     while (nxtitem(&ni, &plane)) {
917         if (plane.pln_own != own)
918             continue;
919         pcp = &plchr[(int)plane.pln_type];
920         if ((pcp->pl_flags & P_F) == 0)
921             continue;
922         if (plane.pln_flags & PLN_LAUNCHED)
923             continue;
924         if (plane.pln_mission != 0)
925             continue;
926         if (plane.pln_mobil <= 0)
927             continue;
928         if (plane.pln_effic < 40)
929             continue;
930         if (!pln_airbase_ok(&plane, 0, 0))
931             continue;
932         /* got one! */
933         ip = malloc(sizeof(*ip));
934         ip->bombs = 0;
935         ip->misc = 0;
936         ip->pcp = &plchr[(int)plane.pln_type];
937         ip->plane = plane;
938         emp_insque(&ip->queue, list);
939     }
940 }
941
942 static int
943 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
944 {
945     struct emp_qelem *qp;
946     double evade;
947     struct plist *plp;
948
949     evade = 100.0;
950     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
951         plp = (struct plist *)qp;
952         if (evade > plp->pcp->pl_stealth / 100.0)
953             evade = plp->pcp->pl_stealth / 100.0;
954     }
955     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
956         plp = (struct plist *)qp;
957         if (evade > plp->pcp->pl_stealth / 100.0)
958             evade = plp->pcp->pl_stealth / 100.0;
959     }
960
961     if (chance(evade))
962         return 1;
963
964     return 0;
965 }