]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
8bb544bb4ac797d5507f9753e6e91b3a66d4e20a
[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                 emp_remque(dqp);
353                 free(dqp);
354                 continue;
355             }
356             if (CANT_HAPPEN(dplp->plane.pln_flags & PLN_LAUNCHED)
357                 || mission_pln_equip(dplp, 0, P_F, 0) < 0) {
358                 emp_remque(dqp);
359                 free(dqp);
360                 continue;
361             }
362             dplp->plane.pln_flags |= PLN_LAUNCHED;
363             putplane(dplp->plane.pln_uid, &dplp->plane);
364             if (first) {
365                 first = 0;
366                 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
367                 PR(def_own, "Launching SAMs at %s planes over %s!\n",
368                    cname(plane_owner), xyas(x, y, def_own));
369                 ac_combat_headers(plane_owner, def_own);
370             }
371             ac_dog(aplp, dplp);
372             dqp = dnext;
373             break;
374         }
375     }
376     if (!first) {
377         PR(plane_owner, "\n");
378         PR(def_own, "\n");
379     }
380     if (delete_missiles) {
381         for (; dqp != def_list; dqp = dnext) {
382             dnext = dqp->q_forw;
383             dplp = (struct plist *)dqp;
384             if (!(dplp->pcp->pl_flags & P_M))
385                 continue;
386             emp_remque(dqp);
387             free(dqp);
388             continue;
389         }
390     }
391 }
392
393 static void
394 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
395              struct emp_qelem *def_list, natid def_own, coord x, coord y)
396 {
397     struct plnstr *pp;
398     struct plist *plp;
399     int icount;
400     struct emp_qelem *next;
401     struct emp_qelem *qp;
402     struct emp_qelem int_list;
403     int att_count;
404     natid plane_owner;
405     int dist;
406
407     plp = (struct plist *)bomb_list->q_forw;
408     plane_owner = plp->plane.pln_own;
409
410     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
411     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
412
413     att_count = 0;
414     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
415         att_count++;
416     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
417         att_count++;
418     if (!att_count)
419         return;
420
421     emp_initque(&int_list);
422     icount = 0;
423     for (qp = def_list->q_forw; qp != def_list; qp = next) {
424         next = qp->q_forw;
425         plp = (struct plist *)qp;
426         pp = &plp->plane;
427         /* SAMs interdict separately */
428         if (plp->pcp->pl_flags & P_M)
429             continue;
430         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
431         if (pp->pln_range < dist)
432             continue;
433         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
434             || mission_pln_equip(plp, 0, P_F, 0) < 0) {
435             emp_remque(qp);
436             free(qp);
437             continue;
438         }
439         /* got one; delete from def_list, add to int_list */
440         emp_remque(qp);
441         emp_insque(qp, &int_list);
442         pp->pln_flags |= PLN_LAUNCHED;
443         pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
444         putplane(pp->pln_uid, pp);
445         icount++;
446         if (icount > att_count)
447             break;
448     }
449     if (icount == 0)
450         return;
451     PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
452        icount, cname(def_own), icount == 1 ? " is" : "s are");
453     PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
454        icount, icount == 1 ? " is" : "s are", cname(plane_owner),
455        xyas(x, y, def_own));
456     ac_combat_headers(plane_owner, def_own);
457     ac_airtoair(esc_list, &int_list);
458     ac_airtoair(bomb_list, &int_list);
459     PR(plane_owner, "\n");
460     PR(def_own, "\n");
461     pln_put(&int_list);
462 }
463
464 void
465 ac_combat_headers(natid plane_owner, natid def_own)
466 {
467     PR(plane_owner,
468        " %-10.10s %-10.10s  strength int odds  damage           results\n",
469        cname(plane_owner), cname(def_own));
470     PR(def_own,
471        " %-10.10s %-10.10s  strength int odds  damage           results\n",
472        cname(def_own), cname(plane_owner));
473 }
474
475 /*
476  * air-to-air combat.
477  */
478 void
479 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
480 {
481     struct plist *attacker;
482     struct plist *interceptor;
483     struct emp_qelem *att;
484     struct emp_qelem *in;
485     int nplanes;
486     int more_att;
487     int more_int;
488     struct emp_qelem *att_next;
489     struct emp_qelem *in_next;
490
491     att = att_list->q_forw;
492     in = int_list->q_forw;
493     more_att = 1;
494     more_int = 1;
495     if (QEMPTY(att_list) || QEMPTY(int_list)) {
496         more_att = 0;
497         more_int = 0;
498     }
499     while (more_att || more_int) {
500         in_next = in->q_forw;
501         att_next = att->q_forw;
502         attacker = (struct plist *)att;
503         interceptor = (struct plist *)in;
504         nplanes = attacker->plane.pln_effic;
505         if (nplanes > interceptor->plane.pln_effic)
506             nplanes = interceptor->plane.pln_effic;
507         ac_dog(attacker, interceptor);
508         in = in_next;
509         att = att_next;
510         if (att == att_list) {
511             more_att = 0;
512             if (QEMPTY(att_list))
513                 more_int = 0;
514             else
515                 att = att->q_forw;
516         }
517         if (in == int_list) {
518             more_int = 0;
519             if (QEMPTY(int_list))
520                 more_att = 0;
521             else
522                 in = in->q_forw;
523         }
524     }
525 }
526
527 static void
528 ac_dog(struct plist *ap, struct plist *dp)
529 {
530     int att, def;
531     double odds;
532     int intensity;
533     natid att_own, def_own;
534     int adam, ddam;
535     char mesg[1024];
536     char temp[14];
537
538     att_own = ap->plane.pln_own;
539     def_own = dp->plane.pln_own;
540
541     PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
542        ap->pcp->pl_name,
543        ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
544     if (def_own)
545         PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
546            dp->pcp->pl_name,
547            dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
548     att = pln_att(&ap->plane);
549     if (att == 0)
550         att = pln_def(&ap->plane);
551     att = att * ap->plane.pln_effic / 100;
552     att = MAX(att, ap->pcp->pl_def / 2);
553
554     def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
555     def = MAX(def, dp->pcp->pl_def / 2);
556
557     if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
558         att -= 2;
559     if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
560         def -= 2;
561     att += ap->pcp->pl_stealth / 25.0;
562     def += dp->pcp->pl_stealth / 25.0;
563     if (att < 1) {
564         def += 1 - att;
565         att = 1;
566     }
567     if (def < 1) {
568         att += 1 - def;
569         def = 1;
570     }
571     odds = ((double)att / ((double)def + (double)att));
572     if (odds <= 0.05)
573         odds = 0.05;
574     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
575
576     PR(att_own, "   %3d/%-3d %3d  %3.2f  ", att, def, intensity, odds);
577     PR(def_own, "   %3d/%-3d %3d  %3.2f  ", def, att, intensity, odds);
578
579     adam = 0;
580     ddam = 0;
581     while ((intensity--) > 0) {
582
583         if (chance(odds)) {
584             ddam += 1;
585             if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
586                 intensity = 0;
587         } else {
588             adam += 1;
589             if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
590                 intensity = 0;
591         }
592     }
593
594     if (dp->pcp->pl_flags & P_M)
595         ddam = 100;
596
597     PR(att_own, "%3d/%-3d", adam, ddam);
598     PR(def_own, "%3d/%-3d", ddam, adam);
599     ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
600     strncpy(temp, mesg, 14);
601     ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
602     PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
603     PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
604
605     if (opt_HIDDEN) {
606         setcont(att_own, def_own, FOUND_FLY);
607         setcont(def_own, att_own, FOUND_FLY);
608     }
609 }
610
611 /*
612  * zap plane associated with plp.
613  * Damaging country is "from", damage is "dam".
614  * def_own is the country on the other side of the conflict from the plane
615  * owner. The only time def_own != from is when the interceptor is getting
616  * damaged.
617  *
618  * NOTE: This routine removes the appropriate plane element from the
619  * queue if it gets destroyed.  That means that the caller must assume
620  * that the current queue pointer is invalid on return from the ac_planedamage
621  * call.  (this has caused bugs in the past)
622  */
623 static void
624 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
625                int checkabort, int show, char *mesg)
626 {
627     struct plnstr *pp;
628     int disp;
629     char dmess[255];
630     int eff;
631     natid plane_owner;
632
633     disp = 0;
634     pp = &plp->plane;
635     plane_owner = pp->pln_own;
636     eff = pp->pln_effic;
637     sprintf(dmess, " no damage");
638     if (dam <= 0) {
639         strcpy(mesg, dmess);
640         return;
641     }
642     memset(dmess, 0, sizeof(dmess));
643     eff -= dam;
644     if (eff < 0)
645         eff = 0;
646     if (eff < PLANE_MINEFF) {
647         sprintf(dmess, " shot down");
648         disp = 1;
649     } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
650         sprintf(dmess, " aborted @%2d%%", eff);
651         disp = 2;
652     } else if (show == 0) {
653         sprintf(dmess, " cleared");
654     }
655
656     if ((plp->pcp->pl_flags & P_M) == 0) {
657         if (show) {
658             PR(plane_owner, "    %s %s takes %d%s.\n",
659                cname(pp->pln_own), prplane(pp), dam, dmess);
660             if (other)
661                 PR(other, "    %s %s takes %d%s.\n",
662                    cname(pp->pln_own), prplane(pp), dam, dmess);
663         }
664     }
665     if (show && checkabort == 1) {
666         PR(plane_owner, "\n");
667         if (other)
668             PR(other, "\n");
669     }
670
671     pp->pln_effic = eff;
672     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
673     if (disp) {
674         if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
675             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
676         pln_put1(plp);
677     } else
678         putplane(pp->pln_uid, pp);
679     strcpy(mesg, dmess);
680 }
681
682 static void
683 ac_doflak(struct emp_qelem *list, struct sctstr *from)
684 {
685     int shell;
686     int gun;
687     natid plane_owner;
688     struct plist *plp;
689
690     plp = (struct plist *)list->q_forw;
691     plane_owner = plp->plane.pln_own;
692
693     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
694     shell = from->sct_item[I_SHELL];
695     if (gun > shell * 2) {
696         shell += supply_commod(from->sct_own, from->sct_x, from->sct_y,
697                                I_SHELL, (gun + 1) / 2 - shell);
698         from->sct_item[I_SHELL] = shell;
699         putsect(from);
700     }
701     if (gun > shell * 2)
702         gun = shell * 2;
703
704     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
705     if (gun > 0) {
706         PR(plane_owner, "firing %d flak guns in %s...\n",
707            gun, xyas(from->sct_x, from->sct_y, plane_owner));
708         if (from->sct_own != 0)
709             PR(from->sct_own, "firing %d flak guns in %s...\n",
710                gun, xyas(from->sct_x, from->sct_y, from->sct_own));
711         ac_fireflak(list, from->sct_own, gun);
712     }
713 }
714
715 static void
716 ac_shipflak(struct emp_qelem *list, coord x, coord y)
717 {
718     struct nstr_item ni;
719     struct shpstr ship;
720     struct mchrstr *mcp;
721     double flak, total, ngun;
722     int gun, shell;
723     int rel;
724     struct plist *plp;
725     natid plane_owner;
726     natid from;
727     int nats[MAXNOC];
728
729     plp = (struct plist *)list->q_forw;
730     plane_owner = plp->plane.pln_own;
731
732     memset(nats, 0, sizeof(nats));
733     total = ngun = 0;
734     snxtitem_xy(&ni, EF_SHIP, x, y);
735     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
736         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
737             continue;
738         mcp = &mchr[(int)ship.shp_type];
739         if (mcp->m_flags & M_SUB)
740             continue;
741         rel = getrel(getnatp(ship.shp_own), plane_owner);
742         if (rel > HOSTILE)
743             continue;
744         shell = 0;
745         gun = shp_usable_guns(&ship);
746         if (gun) {
747             shell = ship.shp_item[I_SHELL];
748             if (shell <= 0) {
749                 shell = supply_commod(ship.shp_own, ship.shp_x, ship.shp_y,
750                                       I_SHELL, 1);
751                 ship.shp_item[I_SHELL] = shell;
752                 putship(ship.shp_uid, &ship);
753             }
754         }
755         if (gun == 0 || shell == 0)
756             continue;
757         flak = gun * (ship.shp_effic / 100.0);
758         ngun += flak;
759         total += techfact(ship.shp_tech, flak * 2.0);
760
761         if (!nats[ship.shp_own]) {
762             /* First time here, print the message */
763             PR(ship.shp_own, "%s planes spotted over ships in %s\n",
764                cname(plane_owner), xyas(x, y, ship.shp_own));
765             PR(plane_owner, "Flying over %s ships in %s\n",
766                cname(ship.shp_own), xyas(x, y, plane_owner));
767             nats[ship.shp_own] = 1;
768         }
769         PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
770            flak, prship(&ship));
771         from = ship.shp_own;
772     }
773
774     /* Limit to FLAK_GUN_MAX guns of average tech factor */
775     if (ngun > FLAK_GUN_MAX)
776         total *= FLAK_GUN_MAX / ngun;
777
778     gun = roundavg(total);
779     if (gun > 0) {
780         PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
781         ac_fireflak(list, from, gun);
782     }
783 }
784
785 static void
786 ac_landflak(struct emp_qelem *list, coord x, coord y)
787 {
788     struct nstr_item ni;
789     struct lndstr land;
790     struct lchrstr *lcp;
791     double flak, total, ngun;
792     int aaf, gun;
793     int rel;
794     struct plist *plp;
795     natid plane_owner;
796     natid from;
797     int nats[MAXNOC];
798
799     plp = (struct plist *)list->q_forw;
800     plane_owner = plp->plane.pln_own;
801
802     memset(nats, 0, sizeof(nats));
803     total = ngun = 0;
804     snxtitem_xy(&ni, EF_LAND, x, y);
805     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
806         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
807             continue;
808         lcp = &lchr[(int)land.lnd_type];
809         aaf = lnd_aaf(&land);
810         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
811             continue;
812         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
813             continue;
814         rel = getrel(getnatp(land.lnd_own), plane_owner);
815         if (rel > HOSTILE)
816             continue;
817         flak = aaf * 1.5 * land.lnd_effic / 100.0;
818         ngun += flak;
819         total += techfact(land.lnd_tech, flak * 2.0);
820
821         if (!nats[land.lnd_own]) {
822             /* First time here, print the message */
823             PR(land.lnd_own, "%s planes spotted over land units in %s\n",
824                cname(plane_owner), xyas(x, y, land.lnd_own));
825             PR(plane_owner, "Flying over %s land units in %s\n",
826                cname(land.lnd_own), xyas(x, y, plane_owner));
827             nats[land.lnd_own] = 1;
828         }
829         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
830            prland(&land), aaf);
831         from = land.lnd_own;
832     }
833
834     /* Limit to FLAK_GUN_MAX guns of average tech factor */
835     if (ngun > FLAK_GUN_MAX)
836         total *= FLAK_GUN_MAX / ngun;
837
838     gun = roundavg(total);
839     if (gun > 0) {
840         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
841         ac_fireflak(list, from, gun);
842     }
843 }
844
845 /*
846  * Called from shipflak, landflak, and doflak.
847  */
848 static void
849 ac_fireflak(struct emp_qelem *list, natid from, int guns)
850 {
851     struct plist *plp;
852     int n;
853     struct emp_qelem *qp;
854     struct emp_qelem *next;
855     char msg[255];
856
857     plp = (struct plist *)list->q_forw;
858
859     for (qp = list->q_forw; qp != list; qp = next) {
860         next = qp->q_forw;
861         plp = (struct plist *)qp;
862         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
863         ac_planedamage(plp, from, n, 0, 2, 1, msg);
864     }
865 }
866
867 /*
868  * Calculate flak damage
869  */
870 int
871 ac_flak_dam(int guns, int def, int pl_flags)
872 {
873     int flak, dam;
874     float mult;
875     /*                             <-7      -7     -6     -5     -4 */
876     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
877     /*    -3     -2     -1      0     +1     +2     +3     +4 */
878          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
879     /*    +5    +6     +7     +8    >+8 */
880          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
881     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
882
883     flak = guns - def;
884     if ((pl_flags & P_T) == 0)
885         flak--;
886     if (pl_flags & P_X)
887         flak -= 2;
888     if (pl_flags & P_H)
889         flak -= 1;
890
891     if (flak > 8)
892         mult = flaktable[FLAK_MAX];
893     else if (flak < -7)
894         mult = flaktable[0];
895     else {
896         flak += 8;
897         mult = flaktable[flak];
898     }
899     mult *= flakscale;
900     dam = (int)((roll(8) + 2) * mult);
901     if (dam > 100)
902         dam = 100;
903     return dam;
904 }
905
906 /*
907  * Get a list of planes available for interception duties.
908  */
909 static void
910 getilist(struct emp_qelem *list, natid own)
911 {
912     struct plchrstr *pcp;
913     struct plnstr plane;
914     struct nstr_item ni;
915     struct plist *ip;
916
917     emp_initque(list);
918     snxtitem_all(&ni, EF_PLANE);
919     while (nxtitem(&ni, &plane)) {
920         if (plane.pln_own != own)
921             continue;
922         pcp = &plchr[(int)plane.pln_type];
923         if ((pcp->pl_flags & P_F) == 0)
924             continue;
925         if (plane.pln_flags & PLN_LAUNCHED)
926             continue;
927         if (plane.pln_mission != 0)
928             continue;
929         if (plane.pln_mobil <= 0)
930             continue;
931         if (plane.pln_effic < 40)
932             continue;
933         if (!pln_airbase_ok(&plane, 0, 0))
934             continue;
935         /* got one! */
936         ip = malloc(sizeof(*ip));
937         ip->bombs = 0;
938         ip->misc = 0;
939         ip->pcp = &plchr[(int)plane.pln_type];
940         ip->plane = plane;
941         emp_insque(&ip->queue, list);
942     }
943 }
944
945 static int
946 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
947 {
948     struct emp_qelem *qp;
949     double evade;
950     struct plist *plp;
951
952     evade = 100.0;
953     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
954         plp = (struct plist *)qp;
955         if (evade > plp->pcp->pl_stealth / 100.0)
956             evade = plp->pcp->pl_stealth / 100.0;
957     }
958     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
959         plp = (struct plist *)qp;
960         if (evade > plp->pcp->pl_stealth / 100.0)
961             evade = plp->pcp->pl_stealth / 100.0;
962     }
963
964     if (chance(evade))
965         return 1;
966
967     return 0;
968 }