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