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