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