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