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