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