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