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