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