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