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