]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
(ac_shipflak, ac_landflak): Reduce flak proportional to efficiency.
[empserver] / src / lib / subs / aircombat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
35
36 #include <config.h>
37
38 #include <string.h>
39
40 #include "misc.h"
41 #include "player.h"
42 #include "news.h"
43 #include "land.h"
44 #include "sect.h"
45 #include "plane.h"
46 #include "ship.h"
47 #include "nat.h"
48 #include "file.h"
49 #include "xy.h"
50 #include "nsc.h"
51 #include "path.h"
52 #include "prototypes.h"
53 #include "optlist.h"
54 #include "server.h"
55
56 #define FLAK_GUN_MAX 14
57
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                      struct emp_qelem *, struct emp_qelem *,
70                      struct emp_qelem *, struct emp_qelem *);
71 static int can_fly(int);
72 static int do_evade(struct emp_qelem *, struct emp_qelem *);
73
74 void
75 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
76              coord x, coord y, char *path, int mission_flags,
77              int no_air_defense, struct emp_qelem *obomb,
78              struct emp_qelem *oesc)
79 {
80     int val, non_missiles;
81     int rel;
82     int dir;
83     int nats[MAXNOC];
84     int lnats[MAXNOC];
85     int gotilist[MAXNOC];
86     int unfriendly[MAXNOC];
87     int overfly[MAXNOC];
88     struct emp_qelem ilist[MAXNOC], *qp;
89     char mypath[1024];
90     int myp;
91     int civ, mil;
92     natid plane_owner;
93     struct sctstr sect;
94     struct shpstr ship;
95     struct lndstr land;
96     struct nstr_item ni;
97     natid cn;
98     struct natstr *over, *mynatp;
99     struct plist *plp;
100     int evaded;
101     struct shiplist *head = NULL;
102     int changed = 0;
103     int intown = 0;             /* Last owner to intercept */
104 /* We want to only intercept once per sector per owner.  So, if we overfly
105    a sector, and then overfly some land units or ships, we don't want to
106    potentially intercept 3 times. */
107
108     plp = (struct plist *)bomb_list->q_forw;
109     plane_owner = plp->plane.pln_own;
110
111     strncpy(mypath, path, sizeof(mypath));
112     myp = 0;
113
114     memset(overfly, 0, sizeof(overfly));
115     memset(gotilist, 0, sizeof(gotilist));
116     memset(unfriendly, 0, sizeof(unfriendly));
117     for (cn = 1; cn < MAXNOC; cn++) {
118         if ((mynatp = getnatp(cn)) == 0)
119             continue;
120         rel = getrel(mynatp, plane_owner);
121         if (rel > HOSTILE)
122             continue;
123         if (plane_owner == cn)
124             continue;
125         unfriendly[cn]++;
126     }
127     if (mission_flags & PM_R) {
128         if (mission_flags & P_S) {
129             PR(plane_owner, "\nSPY Plane report\n");
130             PRdate(plane_owner);
131             sathead();
132         } else if (mission_flags & P_A) {
133             PR(plane_owner, "\nAnti-Sub Patrol report\n");
134         } else {
135             PR(plane_owner, "\nReconnaissance report\n");
136             PRdate(plane_owner);
137         }
138     }
139
140     pln_removedupes(bomb_list, esc_list);
141     while ((dir = mypath[myp++]) && !QEMPTY(bomb_list)) {
142         if ((val = diridx(dir)) == DIR_STOP)
143             break;
144         /* XXX using xnorm is probably bad */
145         x = xnorm(x + diroff[val][0]);
146         y = ynorm(y + diroff[val][1]);
147         getsect(x, y, &sect);
148         over = getnatp(sect.sct_own);
149
150         if (mission_flags & PM_R) {
151             if (opt_HIDDEN)
152                 setcont(plane_owner, sect.sct_own, FOUND_FLY);
153             if (sect.sct_type == SCT_WATER) {
154                 PR(plane_owner, "flying over %s at %s\n",
155                    dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
156                 if (mission_flags & PM_S)
157                     plane_sweep(bomb_list, x, y);
158                 if (mission_flags & P_A) {
159                     plane_sona(bomb_list, x, y, &head);
160                 }
161                 changed += map_set(plane_owner,
162                                    sect.sct_x, sect.sct_y,
163                                    dchr[sect.sct_type].d_mnem, 0);
164             } else if (mission_flags & P_S) {
165                 satdisp(&sect, (mission_flags & P_I) ? 10 : 50, 1);
166             } else {
167                 /* This is borrowed from lookout */
168                 if (sect.sct_own == plane_owner)
169                     PR(plane_owner, "Your ");
170                 else
171                     PR(plane_owner, "%s (#%d) ",
172                        cname(sect.sct_own), sect.sct_own);
173                 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
174                 changed += map_set(plane_owner,
175                                    sect.sct_x, sect.sct_y,
176                                    dchr[sect.sct_type].d_mnem, 0);
177                 PR(plane_owner, " %d%% efficient ",
178                    (sect.sct_own == plane_owner) ?
179                    sect.sct_effic : roundintby((int)sect.sct_effic, 25));
180                 civ = sect.sct_item[I_CIVIL];
181                 mil = sect.sct_item[I_MILIT];
182                 if (civ)
183                     PR(plane_owner, "with %s%d civ ",
184                        (sect.sct_own == plane_owner) ?
185                        "" : "approx ",
186                        (sect.sct_own == plane_owner) ?
187                        civ : roundintby(civ, 25));
188                 if (mil)
189                     PR(plane_owner, "with %s%d mil ",
190                        (sect.sct_own == plane_owner) ?
191                        "" : "approx ",
192                        (sect.sct_own == plane_owner) ?
193                        mil : roundintby(mil, 25));
194                 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
195             }
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         if (!update_pending && plane_owner == player->cnum)
271             writemap(player->cnum);
272     /* Now, if the bomber and escort lists are empty, we are done */
273     if (QEMPTY(bomb_list) && QEMPTY(esc_list)) {
274         if (mission_flags & P_A)
275             free_shiplist(&head);
276         return;
277     }
278
279     /* Something made it through */
280     /* Go figure out if there are ships in this sector, and who's they are */
281     memset(nats, 0, sizeof(nats));
282     snxtitem_xy(&ni, EF_SHIP, x, y);
283     while (nxtitem(&ni, &ship)) {
284         if (mchr[(int)ship.shp_type].m_flags & M_SUB)
285             continue;
286         nats[ship.shp_own]++;
287     }
288     /* Go figure out if there are units in this sector, and who's they are */
289     memset(lnats, 0, sizeof(lnats));
290     snxtitem_xy(&ni, EF_LAND, x, y);
291     while (nxtitem(&ni, &land)) {
292         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
293             continue;
294         lnats[land.lnd_own]++;
295     }
296
297     /* Now, let's make life a little rougher. */
298     for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
299         if (plane_owner == cn)
300             continue;
301         intown = -1;
302         /* Are there ships owned by this country? */
303         if (nats[cn] != 0) {
304             /* Yes. */
305             if (cn != 0) {
306                 PR(cn, "%s planes spotted over ships in %s\n",
307                    cname(plane_owner), xyas(x, y, cn));
308                 if (opt_HIDDEN)
309                     setcont(cn, plane_owner, FOUND_FLY);
310             }
311             if (unfriendly[cn]) {
312                 /* They are unfriendly too */
313                 if (!gotilist[cn]) {
314                     getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
315                              oesc);
316                     gotilist[cn]++;
317                 }
318                 PR(plane_owner, "Flying over %s ships in %s\n",
319                    cname(cn), xyas(x, y, plane_owner));
320                 /* This makes going for ships in harbors tough */
321                 if (!evaded) {
322                     /* We already fired flak up above.  Now we intercept again if we haven't already */
323                     /* Flag that we intercepted */
324                     intown = 1;
325                     /* And now intercept again */
326                     ac_intercept(bomb_list, esc_list, &ilist[cn],
327                                  cn, x, y);
328                 }
329             }
330         }
331         /* Are there units owned by this country? */
332         if (lnats[cn] != 0) {
333             /* Yes. */
334             if (cn != 0) {
335                 PR(cn, "%s planes spotted over land units in %s\n",
336                    cname(plane_owner), xyas(x, y, cn));
337                 if (opt_HIDDEN)
338                     setcont(cn, plane_owner, FOUND_FLY);
339             }
340             if (unfriendly[cn]) {
341                 /* They are unfriendly too */
342                 if (!gotilist[cn]) {
343                     getilist(&ilist[cn], cn, bomb_list, esc_list, obomb,
344                              oesc);
345                     gotilist[cn]++;
346                 }
347                 PR(plane_owner, "Flying over %s land units in %s\n",
348                    cname(cn), xyas(x, y, plane_owner));
349                 if (!evaded) {
350                     if (intown == -1) {
351                         /* We haven't intercepted yet, so intercept */
352                         ac_intercept(bomb_list, esc_list, &ilist[cn],
353                                      cn, x, y);
354                     }
355                 }
356             }
357         }
358     }
359     if (mission_flags & P_A)
360         free_shiplist(&head);
361 }
362
363 static int
364 count_non_missiles(struct emp_qelem *list)
365 {
366     struct emp_qelem *qp;
367     struct plist *plp;
368     int att_count = 0;
369
370     /* don't intercept missiles */
371     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
372         plp = (struct plist *)qp;
373         if (!(plp->pcp->pl_flags & P_M))
374             att_count++;
375     }
376     return att_count;
377 }
378
379 void
380 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
381               natid def_own, natid plane_owner, coord x, coord y,
382               int delete_missiles)
383 {
384     struct emp_qelem *aqp;
385     struct emp_qelem *anext;
386     struct emp_qelem *dqp;
387     struct emp_qelem *dnext;
388     struct plist *aplp;
389     struct plist *dplp;
390     int first = 1;
391
392     for (aqp = att_list->q_forw,
393          dqp = def_list->q_forw;
394          aqp != att_list && dqp != def_list; aqp = anext) {
395         anext = aqp->q_forw;
396         aplp = (struct plist *)aqp;
397         if (aplp->pcp->pl_flags & P_M)
398             continue;
399         if (aplp->pcp->pl_cost < 1000)
400             continue;
401         for (; dqp != def_list; dqp = dnext) {
402             dnext = dqp->q_forw;
403             dplp = (struct plist *)dqp;
404             if (!(dplp->pcp->pl_flags & P_M))
405                 continue;
406
407             if (dplp->plane.pln_range <
408                 mapdist(x, y, dplp->plane.pln_x, dplp->plane.pln_y)) {
409                 emp_remque(dqp);
410                 free(dqp);
411                 continue;
412             }
413             if (mission_pln_equip(dplp, 0, P_F, 0) < 0) {
414                 emp_remque(dqp);
415                 free(dqp);
416                 continue;
417             }
418             if (first) {
419                 first = 0;
420                 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
421                 PR(def_own, "Launching SAMs at %s planes!\n",
422                    cname(plane_owner));
423                 ac_combat_headers(plane_owner, def_own);
424             }
425             ac_dog(aplp, dplp);
426             dqp = dnext;
427             break;
428         }
429     }
430     if (!first) {
431         PR(plane_owner, "\n");
432         PR(def_own, "\n");
433     }
434     if (delete_missiles) {
435         for (; dqp != def_list; dqp = dnext) {
436             dnext = dqp->q_forw;
437             dplp = (struct plist *)dqp;
438             if (!(dplp->pcp->pl_flags & P_M))
439                 continue;
440             emp_remque(dqp);
441             free(dqp);
442             continue;
443         }
444     }
445 }
446
447 static void
448 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
449              struct emp_qelem *def_list, natid def_own, coord x, coord y)
450 {
451     struct plnstr *pp;
452     struct plist *plp;
453     int icount;
454     struct emp_qelem *next;
455     struct emp_qelem *qp;
456     struct emp_qelem int_list;
457     int att_count;
458     natid plane_owner;
459     int dist;
460
461     plp = (struct plist *)bomb_list->q_forw;
462     plane_owner = plp->plane.pln_own;
463
464     icount = 0;
465
466     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y, 0);
467     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y, 1);
468     if (!(att_count = count_non_missiles(bomb_list) +
469           count_non_missiles(esc_list)))
470         return;
471
472     emp_initque(&int_list);
473     for (qp = def_list->q_forw; qp != def_list; qp = next) {
474         next = qp->q_forw;
475         plp = (struct plist *)qp;
476         pp = &plp->plane;
477         /* SAMs interdict separately */
478         if (plp->pcp->pl_flags & P_M)
479             continue;
480         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
481         if (pp->pln_range < dist)
482             continue;
483         if (mission_pln_equip(plp, 0, P_F, 0) < 0) {
484             emp_remque(qp);
485             free(qp);
486             continue;
487         }
488         /* got one; delete from def_list, add to int_list */
489         emp_remque(qp);
490         emp_insque(qp, &int_list);
491         pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
492         putplane(pp->pln_uid, pp);
493         icount++;
494         if (icount > att_count)
495             break;
496     }
497     if (icount == 0)
498         return;
499     PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
500        icount, cname(def_own), icount == 1 ? " is" : "s are");
501     PR(def_own, "%d fighter%s intercepting %s planes!\n",
502        icount, icount == 1 ? " is" : "s are", cname(plane_owner));
503     ac_combat_headers(plane_owner, def_own);
504     ac_airtoair(esc_list, &int_list);
505     ac_airtoair(bomb_list, &int_list);
506     PR(plane_owner, "\n");
507     PR(def_own, "\n");
508 }
509
510 void
511 ac_combat_headers(natid plane_owner, natid def_own)
512 {
513     PR(plane_owner,
514        " %-10.10s %-10.10s  strength int odds  damage           results\n",
515        cname(plane_owner), cname(def_own));
516     PR(def_own,
517        " %-10.10s %-10.10s  strength int odds  damage           results\n",
518        cname(def_own), cname(plane_owner));
519 }
520
521 /*
522  * air-to-air combat.
523  */
524 void
525 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
526 {
527     struct plist *attacker;
528     struct plist *interceptor;
529     struct emp_qelem *att;
530     struct emp_qelem *in;
531     int nplanes;
532     int more_att;
533     int more_int;
534     struct emp_qelem *att_next;
535     struct emp_qelem *in_next;
536
537     att = att_list->q_forw;
538     in = int_list->q_forw;
539     more_att = 1;
540     more_int = 1;
541     if (QEMPTY(att_list) || QEMPTY(int_list)) {
542         more_att = 0;
543         more_int = 0;
544     }
545     while (more_att || more_int) {
546         in_next = in->q_forw;
547         att_next = att->q_forw;
548         attacker = (struct plist *)att;
549
550         /* skip missiles. If only missiles left, we're done */
551         if (plchr[(int)attacker->plane.pln_type].pl_flags & P_M) {
552             att = att_next;
553             if (att == att_list) {
554                 more_att = 0;
555                 if (QEMPTY(att_list))
556                     more_int = 0;
557                 else
558                     att = att->q_forw;
559             }
560             if (all_missiles(att_list))
561                 more_att = 0;
562             continue;
563         }
564         interceptor = (struct plist *)in;
565         nplanes = attacker->plane.pln_effic;
566         if (nplanes > interceptor->plane.pln_effic)
567             nplanes = interceptor->plane.pln_effic;
568         ac_dog(attacker, interceptor);
569         in = in_next;
570         att = att_next;
571         if (att == att_list) {
572             more_att = 0;
573             if (QEMPTY(att_list))
574                 more_int = 0;
575             else
576                 att = att->q_forw;
577         }
578         if (in == int_list) {
579             more_int = 0;
580             if (QEMPTY(int_list))
581                 more_att = 0;
582             else
583                 in = in->q_forw;
584         }
585     }
586 }
587
588 static int
589 all_missiles(struct emp_qelem *att_list)
590 {
591     struct emp_qelem *qp;
592     struct plist *p;
593
594     qp = att_list->q_forw;
595     while (qp != att_list) {
596         p = (struct plist *)qp;
597         if (!(plchr[(int)p->plane.pln_type].pl_flags & P_M))
598             return 0;
599
600         qp = qp->q_forw;
601     }
602     return 1;
603 }
604
605 static void
606 ac_dog(struct plist *ap, struct plist *dp)
607 {
608     int att, def;
609     double odds;
610     int intensity;
611     natid att_own, def_own;
612     int adam, ddam;
613     char mesg[1024];
614     char temp[14];
615
616     att_own = ap->plane.pln_own;
617     def_own = dp->plane.pln_own;
618
619     PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
620        ap->pcp->pl_name,
621        ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
622     if (def_own)
623         PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
624            dp->pcp->pl_name,
625            dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
626     if (ap->plane.pln_att == 0) {
627         att = ap->plane.pln_def * ap->plane.pln_effic / 100;
628         att = MAX(att, ap->pcp->pl_def / 2);
629     } else {
630         att = ap->plane.pln_att * ap->plane.pln_effic / 100;
631         att = MAX(att, ap->pcp->pl_att / 2);
632     }
633
634     def = dp->plane.pln_def * 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     struct shpstr ship;
712     struct lndstr land;
713     natid plane_owner;
714
715     disp = 0;
716     pp = &plp->plane;
717     plane_owner = pp->pln_own;
718     eff = pp->pln_effic;
719     sprintf(dmess, " no damage");
720     if (dam <= 0) {
721         strcpy(mesg, dmess);
722         return;
723     }
724     memset(dmess, 0, sizeof(dmess));
725     eff -= dam;
726     if (eff < 0)
727         eff = 0;
728     if (eff < PLANE_MINEFF) {
729         sprintf(dmess, " shot down");
730         disp = 1;
731     } else if (eff < 80 && chance((100 - eff) / 100.0) && checkabort) {
732         sprintf(dmess, " aborted @%2d%%", eff);
733         disp = 2;
734     } else if (show == 0) {
735         sprintf(dmess, " cleared");
736     }
737
738     if ((plp->pcp->pl_flags & P_M) == 0) {
739         if (show) {
740             PR(plane_owner, "    %s %s takes %d%s.\n",
741                cname(pp->pln_own), prplane(pp), dam, dmess);
742             if (other)
743                 PR(other, "    %s %s takes %d%s.\n",
744                    cname(pp->pln_own), prplane(pp), dam, dmess);
745         }
746     }
747     if (show && checkabort == 1) {
748         PR(plane_owner, "\n");
749         if (other)
750             PR(other, "\n");
751     }
752
753     pp->pln_effic = eff;
754     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
755     if (disp == 1) {
756         if (from != 0 && (plp->pcp->pl_flags & P_M) == 0)
757             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
758         if (pp->pln_ship >= 0) {
759             getship(pp->pln_ship, &ship);
760             take_plane_off_ship(pp, &ship);
761         }
762         if (pp->pln_land >= 0) {
763             getland(pp->pln_land, &land);
764             take_plane_off_land(pp, &land);
765         }
766         pp->pln_effic = 0;
767         putplane(pp->pln_uid, pp);
768         emp_remque(&plp->queue);
769         free(plp);
770     } else if (disp == 2) {
771         putplane(pp->pln_uid, pp);
772         emp_remque(&plp->queue);
773         free(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 = gun = 0;
842         gun = MIN(ship.shp_item[I_GUN], ship.shp_glim);
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 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         if ((lcp->l_flags & L_FLAK) == 0 || land.lnd_aaf == 0)
907             continue;
908         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
909             continue;
910         rel = getrel(getnatp(land.lnd_own), plane_owner);
911         if (rel > HOSTILE)
912             continue;
913         flak = land.lnd_aaf * 1.5 * land.lnd_effic / 100.0;
914         ngun += flak;
915         total += techfact(land.lnd_tech, flak * 2.0);
916
917         if (!nats[land.lnd_own]) {
918             /* First time here, print the message */
919             PR(land.lnd_own, "%s planes spotted over land units in %s\n",
920                cname(plane_owner), xyas(x, y, land.lnd_own));
921             PR(plane_owner, "Flying over %s land units in %s\n",
922                cname(land.lnd_own), xyas(x, y, plane_owner));
923             nats[land.lnd_own] = 1;
924         }
925         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
926            prland(&land), land.lnd_aaf);
927         from = land.lnd_own;
928     }
929
930     /* Limit to FLAK_GUN_MAX guns of average tech factor */
931     if (ngun > FLAK_GUN_MAX)
932         total *= FLAK_GUN_MAX / ngun;
933
934     gun = roundavg(total);
935     if (gun > 0) {
936         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
937         ac_fireflak(list, from, gun);
938     }
939 }
940
941 /*
942  * Called from shipflak, landflak, and doflak.
943  */
944 static void
945 ac_fireflak(struct emp_qelem *list, natid from, int guns)
946 {
947     struct plnstr *pp;
948     struct plist *plp;
949     int n;
950     int diff;
951     struct emp_qelem *qp;
952     struct emp_qelem *next;
953     char msg[255];
954
955     plp = (struct plist *)list->q_forw;
956
957     for (qp = list->q_forw; qp != list; qp = next) {
958         /*
959          * fighters don't get shot at by flak
960          * non-tactical bombers are harder to hit with flak.
961          * ('Cause they're not dive-bombing?)
962          */
963         next = qp->q_forw;
964         plp = (struct plist *)qp;
965         pp = &plp->plane;
966         diff = guns - pp->pln_def;
967         if ((plp->pcp->pl_flags & P_T) == 0)
968             diff--;
969         if (plp->pcp->pl_flags & P_X)
970             diff -= 2;
971         if (plp->pcp->pl_flags & P_H)
972             diff -= 1;
973         n = ac_flak_dam(diff);
974         ac_planedamage(plp, from, n, 0, 2, 1, msg);
975     }
976 }
977
978 /*
979  * Calculate flak damage
980  */
981 int
982 ac_flak_dam(int flak)
983 {
984     int dam;
985     float mult;
986     /*                             <-7      -7     -6     -5     -4 */
987     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
988     /*    -3     -2     -1      0     +1     +2     +3     +4 */
989          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
990     /*    +5    +6     +7     +8    >+8 */
991          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
992     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
993
994     if (flak > 8)
995         mult = flaktable[FLAK_MAX];
996     else if (flak < -7)
997         mult = flaktable[0];
998     else {
999         flak += 8;
1000         mult = flaktable[flak];
1001     }
1002     mult *= flakscale;
1003     dam = (int)((roll(8) + 2) * mult);
1004     if (dam > 100)
1005         dam = 100;
1006     return dam;
1007 }
1008
1009 /*
1010  * See if this plane is flying in this list
1011  */
1012 int
1013 ac_isflying(struct plnstr *plane, struct emp_qelem *list)
1014 {
1015     struct emp_qelem *qp;
1016     struct emp_qelem *next;
1017     struct plnstr *pp;
1018     struct plist *plp;
1019
1020     if (!list)
1021         return 0;
1022     for (qp = list->q_forw; qp != list; qp = next) {
1023         next = qp->q_forw;
1024         plp = (struct plist *)qp;
1025         pp = &plp->plane;
1026         if (plane->pln_uid == pp->pln_uid)
1027             return 1;
1028     }
1029     return 0;
1030 }
1031
1032
1033 /*
1034  * Get a list of planes available for interception duties.
1035  */
1036 static void
1037 getilist(struct emp_qelem *list, natid own, struct emp_qelem *a,
1038          struct emp_qelem *b, struct emp_qelem *c, struct emp_qelem *d)
1039 {
1040     struct plchrstr *pcp;
1041     struct plnstr plane;
1042     struct shpstr ship;
1043     struct lndstr land;
1044     struct sctstr sect;
1045     struct nstr_item ni;
1046     int petrol;
1047     struct plist *ip;
1048
1049     emp_initque(list);
1050     snxtitem_all(&ni, EF_PLANE);
1051     while (nxtitem(&ni, &plane)) {
1052         if (plane.pln_own != own)
1053             continue;
1054         pcp = &plchr[(int)plane.pln_type];
1055         if ((pcp->pl_flags & P_F) == 0)
1056             continue;
1057         if (plane.pln_mission != 0)
1058             continue;
1059         if (plane.pln_mobil <= 0)
1060             continue;
1061         if (plane.pln_effic < 40)
1062             continue;
1063         if (plane.pln_ship >= 0) {
1064             if (!can_fly(plane.pln_uid))
1065                 continue;
1066             getship(plane.pln_ship, &ship);
1067             petrol = ship.shp_item[I_PETROL];
1068         } else if (plane.pln_land >= 0) {
1069             if (!can_fly(plane.pln_uid))
1070                 continue;
1071             getland(plane.pln_land, &land);
1072             petrol = land.lnd_item[I_PETROL];
1073         } else {
1074             getsect(plane.pln_x, plane.pln_y, &sect);
1075             petrol = sect.sct_item[I_PETROL];
1076             if ((sect.sct_effic < 60 || sect.sct_type != SCT_AIRPT)
1077                 && (pcp->pl_flags & P_V) == 0)
1078                 continue;
1079         }
1080         if (petrol <= pcp->pl_fuel / 2)
1081             continue;
1082         /* Finally, is it in the list of planes already in
1083            flight? */
1084         if (ac_isflying(&plane, a))
1085             continue;
1086         if (ac_isflying(&plane, b))
1087             continue;
1088         if (ac_isflying(&plane, c))
1089             continue;
1090         if (ac_isflying(&plane, d))
1091             continue;
1092         /* got one! */
1093         ip = malloc(sizeof(*ip));
1094         ip->bombs = 0;
1095         ip->misc = 0;
1096         ip->pcp = &plchr[(int)plane.pln_type];
1097         ip->plane = plane;
1098         emp_insque(&ip->queue, list);
1099     }
1100 }
1101
1102
1103
1104 static int
1105 can_fly(int p)
1106 {                               /* Can this plane fly from the ship or land unit it is on? */
1107     struct plnstr plane;
1108     struct shpstr ship;
1109     struct lndstr land;
1110     struct plchrstr *pcp;
1111     struct mchrstr *scp;
1112
1113     getplane(p, &plane);
1114     pcp = &plchr[(int)plane.pln_type];
1115
1116     if (plane.pln_ship >= 0) {
1117         if (!(pcp->pl_flags & P_L) && !(pcp->pl_flags & P_M)
1118             && !(pcp->pl_flags & P_K)
1119             && !(pcp->pl_flags & P_E)
1120             )
1121             return 0;
1122
1123         getship(plane.pln_ship, &ship);
1124         scp = &mchr[(int)ship.shp_type];
1125
1126         if ((pcp->pl_flags & P_L) && (scp->m_flags & M_FLY)) {
1127             return 1;
1128         }
1129
1130         if ((pcp->pl_flags & P_M) && (scp->m_flags & M_MSL)) {
1131             return 1;
1132         }
1133
1134         if ((pcp->pl_flags & P_K) && (scp->m_flags & M_CHOPPER)) {
1135             return 1;
1136         }
1137
1138         if ((pcp->pl_flags & P_E) && (scp->m_flags & M_XLIGHT)) {
1139             return 1;
1140         }
1141     }
1142
1143     if (plane.pln_land >= 0) {
1144         if (!(pcp->pl_flags & P_E))
1145             return 0;
1146
1147         getland(plane.pln_land, &land);
1148
1149         if ((pcp->pl_flags & P_E) && land.lnd_maxlight) {
1150             return 1;
1151         }
1152     }
1153
1154     return 0;
1155 }
1156
1157 static int
1158 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
1159 {
1160     struct emp_qelem *qp;
1161     double evade;
1162     struct plist *plp;
1163
1164     evade = 100.0;
1165     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
1166         plp = (struct plist *)qp;
1167         if (evade > plp->pcp->pl_stealth / 100.0)
1168             evade = plp->pcp->pl_stealth / 100.0;
1169     }
1170     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
1171         plp = (struct plist *)qp;
1172         if (evade > plp->pcp->pl_stealth / 100.0)
1173             evade = plp->pcp->pl_stealth / 100.0;
1174     }
1175
1176     if (chance(evade))
1177         return 1;
1178
1179     return 0;
1180 }