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