]> git.pond.sub.org Git - empserver/blob - src/lib/subs/aircombat.c
5d17bab835cfff4c5dfa852eec7b3136bb156f40
[empserver] / src / lib / subs / aircombat.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *     Markus Armbruster, 2006-2008
35  */
36
37 #include <config.h>
38
39 #include "file.h"
40 #include "land.h"
41 #include "map.h"
42 #include "misc.h"
43 #include "mission.h"
44 #include "nat.h"
45 #include "news.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "path.h"
49 #include "plane.h"
50 #include "player.h"
51 #include "prototypes.h"
52 #include "sect.h"
53 #include "ship.h"
54 #include "xy.h"
55
56 #define FLAK_GUN_MAX 14
57
58 static int plane_caps(struct emp_qelem *);
59 static void sam_intercept(struct emp_qelem *, struct emp_qelem *,
60                           natid, natid, coord, coord, int);
61 static void ac_intercept(struct emp_qelem *, struct emp_qelem *,
62                          struct emp_qelem *, natid, coord, coord, int);
63 static void ac_combat_headers(natid, natid);
64 static void ac_airtoair(struct emp_qelem *, struct emp_qelem *);
65 static void ac_dog(struct plist *, struct plist *);
66 static void ac_planedamage(struct plist *, natid, int, natid, int,
67                            int, char *);
68 static void ac_doflak(struct emp_qelem *, struct sctstr *);
69 static void ac_landflak(struct emp_qelem *, coord, coord);
70 static void ac_shipflak(struct emp_qelem *, coord, coord);
71 static void ac_fireflak(struct emp_qelem *, natid, int);
72 static void getilists(struct emp_qelem *, unsigned char *, natid);
73 static int do_evade(struct emp_qelem *, struct emp_qelem *);
74
75 void
76 ac_encounter(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
77              coord x, coord y, char *path, int mission_flags)
78 {
79     int val;
80     int dir;
81     unsigned char gotships[MAXNOC];
82     unsigned char gotlands[MAXNOC];
83     unsigned char rel[MAXNOC];
84     int overfly[MAXNOC];
85     int flags;
86     struct emp_qelem ilist[MAXNOC];
87     int civ, mil;
88     natid plane_owner;
89     struct sctstr sect;
90     struct shpstr ship;
91     struct lndstr land;
92     struct nstr_item ni;
93     natid cn;
94     struct plist *plp;
95     int evaded;
96     struct shiplist *head = NULL;
97     int changed = 0;
98 /* We want to only intercept once per sector per owner.  So, if we overfly
99    a sector, and then overfly some land units or ships, we don't want to
100    potentially intercept 3 times. */
101
102     plp = (struct plist *)bomb_list->q_forw;
103     plane_owner = plp->plane.pln_own;
104
105     memset(overfly, 0, sizeof(overfly));
106     getilists(ilist, rel, plane_owner);
107
108     if (mission_flags & PM_R) {
109         flags = plane_caps(bomb_list);
110         if (flags & P_S) {
111             PR(plane_owner, "\nSPY Plane report\n");
112             PRdate(plane_owner);
113             sathead();
114         } else if (flags & P_A) {
115             PR(plane_owner, "\nAnti-Sub Patrol report\n");
116         } else {
117             PR(plane_owner, "\nReconnaissance report\n");
118             PRdate(plane_owner);
119         }
120     }
121
122     for (;;) {
123         getsect(x, y, &sect);
124         memset(gotships, 0, sizeof(gotships));
125         snxtitem_xy(&ni, EF_SHIP, x, y);
126         while (nxtitem(&ni, &ship)) {
127             if (mchr[(int)ship.shp_type].m_flags & M_SUB)
128                 continue;
129             gotships[ship.shp_own] = 1;
130         }
131         memset(gotlands, 0, sizeof(gotlands));
132         snxtitem_xy(&ni, EF_LAND, x, y);
133         while (nxtitem(&ni, &land)) {
134             if (land.lnd_ship >= 0 || land.lnd_land >= 0)
135                 continue;
136             gotlands[land.lnd_own] = 1;
137         }
138
139         if (mission_flags & PM_R) {
140             flags = plane_caps(bomb_list);
141             if (opt_HIDDEN)
142                 setcont(plane_owner, sect.sct_own, FOUND_FLY);
143             if (sect.sct_type == SCT_WATER) {
144                 PR(plane_owner, "flying over %s at %s\n",
145                    dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
146                 if (mission_flags & PM_S)
147                     plane_sweep(bomb_list, x, y);
148                 if (flags & P_A)
149                     plane_sona(bomb_list, x, y, &head);
150                 changed += map_set(plane_owner,
151                                    sect.sct_x, sect.sct_y,
152                                    dchr[sect.sct_type].d_mnem, 0);
153             } else if (flags & P_S) {
154                 satdisp_sect(&sect, flags & P_I ? 10 : 50);
155             } else {
156                 /* This is borrowed from lookout */
157                 if (sect.sct_own == plane_owner)
158                     PR(plane_owner, "Your ");
159                 else
160                     PR(plane_owner, "%s (#%d) ",
161                        cname(sect.sct_own), sect.sct_own);
162                 PR(plane_owner, "%s", dchr[sect.sct_type].d_name);
163                 changed += map_set(plane_owner,
164                                    sect.sct_x, sect.sct_y,
165                                    dchr[sect.sct_type].d_mnem, 0);
166                 PR(plane_owner, " %d%% efficient ",
167                    (sect.sct_own == plane_owner) ?
168                    sect.sct_effic : roundintby((int)sect.sct_effic, 25));
169                 civ = sect.sct_item[I_CIVIL];
170                 mil = sect.sct_item[I_MILIT];
171                 if (civ)
172                     PR(plane_owner, "with %s%d civ ",
173                        (sect.sct_own == plane_owner) ?
174                        "" : "approx ",
175                        (sect.sct_own == plane_owner) ?
176                        civ : roundintby(civ, 25));
177                 if (mil)
178                     PR(plane_owner, "with %s%d mil ",
179                        (sect.sct_own == plane_owner) ?
180                        "" : "approx ",
181                        (sect.sct_own == plane_owner) ?
182                        mil : roundintby(mil, 25));
183                 PR(plane_owner, "@ %s\n", xyas(x, y, plane_owner));
184             }
185             if (flags & P_S)
186                 satdisp_units(sect.sct_x, sect.sct_y);
187             else {
188                 for (cn = 1; cn < MAXNOC; cn++) {
189                     if (cn == plane_owner)
190                         continue;
191                     if (gotships[cn])
192                         PR(plane_owner, "Flying over %s ships in %s\n",
193                            cname(cn), xyas(x, y, plane_owner));
194                     if (gotlands[cn])
195                         PR(plane_owner, "Flying over %s land units in %s\n",
196                            cname(cn), xyas(x, y, plane_owner));
197                 }
198             }
199         } else {
200             PR(plane_owner, "flying over %s at %s\n",
201                dchr[sect.sct_type].d_name, xyas(x, y, plane_owner));
202             changed += map_set(plane_owner, sect.sct_x, sect.sct_y,
203                                dchr[sect.sct_type].d_mnem, 0);
204         }
205
206         evaded = do_evade(bomb_list, esc_list);
207         if (!evaded) {
208             overfly[sect.sct_own]++;
209             for (cn = 1; cn < MAXNOC; cn++) {
210                 if (cn == plane_owner || rel[cn] == ALLIED)
211                     continue;
212                 if (cn != sect.sct_own && !gotships[cn] && !gotlands[cn])
213                     continue;
214                 PR(cn, "%s planes spotted over %s\n",
215                    cname(plane_owner), xyas(x, y, cn));
216                 if (opt_HIDDEN)
217                     setcont(cn, plane_owner, FOUND_FLY);
218             }
219
220             /* Fire flak */
221             if (rel[sect.sct_own] <= HOSTILE)
222                 ac_doflak(bomb_list, &sect);
223             /* If bombers left, fire flak from units and ships */
224             if (!QEMPTY(bomb_list))
225                 ac_landflak(bomb_list, x, y);
226             if (!QEMPTY(bomb_list))
227                 ac_shipflak(bomb_list, x, y);
228             /* mission planes aborted due to flak -- don't send escorts */
229             if (QEMPTY(bomb_list))
230                 break;
231
232             for (cn = 1; cn < MAXNOC && !QEMPTY(bomb_list); cn++) {
233                 if (rel[cn] > HOSTILE)
234                     continue;
235                 ac_intercept(bomb_list, esc_list, &ilist[cn], cn, x, y,
236                              !(cn == sect.sct_own
237                                || gotships[cn] || gotlands[cn]));
238             }
239         }
240
241         dir = *path++;
242         if (!dir || QEMPTY(bomb_list) || (val = diridx(dir)) == DIR_STOP)
243             break;
244         x = xnorm(x + diroff[val][0]);
245         y = ynorm(y + diroff[val][1]);
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 && rel[cn] != ALLIED)
253             nreport(plane_owner, N_OVFLY_SECT, cn, overfly[cn]);
254     }
255     /* If the map changed, update it */
256     if (changed)
257         writemap(player->cnum);
258
259     free_shiplist(&head);
260     for (cn = 1; cn < MAXNOC; cn++)
261         pln_put(&ilist[cn]);
262 }
263
264 static int
265 plane_caps(struct emp_qelem *list)
266 {
267     struct emp_qelem *qp;
268     struct plist *plp;
269     int fl;
270
271     fl = 0;
272     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
273         plp = (struct plist *)qp;
274         fl |= plp->pcp->pl_flags;
275     }
276
277     return fl;
278 }
279
280 static void
281 sam_intercept(struct emp_qelem *att_list, struct emp_qelem *def_list,
282               natid def_own, natid plane_owner, coord x, coord y,
283               int only_mission)
284 {
285     struct emp_qelem *aqp;
286     struct emp_qelem *anext;
287     struct emp_qelem *dqp;
288     struct emp_qelem *dnext;
289     struct plist *aplp;
290     struct plist *dplp;
291     struct plnstr *pp;
292     int first = 1;
293
294     for (aqp = att_list->q_forw,
295          dqp = def_list->q_forw;
296          aqp != att_list && dqp != def_list; aqp = anext) {
297         anext = aqp->q_forw;
298         aplp = (struct plist *)aqp;
299         if (aplp->pcp->pl_cost < 1000)
300             continue;
301         for (; dqp != def_list; dqp = dnext) {
302             dnext = dqp->q_forw;
303             dplp = (struct plist *)dqp;
304             pp = &dplp->plane;
305             if (!(dplp->pcp->pl_flags & P_M))
306                 continue;
307             if (only_mission && !pp->pln_mission)
308                 continue;
309             if (pp->pln_range < mapdist(x, y, pp->pln_x, pp->pln_y))
310                 continue;
311             if (pp->pln_mission
312                 && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
313                 continue;
314             if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
315                 || mission_pln_equip(dplp, 0, P_F, 0) < 0) {
316                 emp_remque(dqp);
317                 free(dqp);
318                 continue;
319             }
320             pp->pln_flags |= PLN_LAUNCHED;
321             putplane(pp->pln_uid, pp);
322             if (first) {
323                 first = 0;
324                 PR(plane_owner, "%s launches SAMs!\n", cname(def_own));
325                 PR(def_own, "Launching SAMs at %s planes over %s!\n",
326                    cname(plane_owner), xyas(x, y, def_own));
327                 ac_combat_headers(plane_owner, def_own);
328             }
329             ac_dog(aplp, dplp);
330             dqp = dnext;
331             break;
332         }
333     }
334     if (!first) {
335         PR(plane_owner, "\n");
336         PR(def_own, "\n");
337     }
338 }
339
340 static void
341 ac_intercept(struct emp_qelem *bomb_list, struct emp_qelem *esc_list,
342              struct emp_qelem *def_list, natid def_own, coord x, coord y,
343              int only_mission)
344 {
345     struct plnstr *pp;
346     struct plist *plp;
347     int icount;
348     struct emp_qelem *next;
349     struct emp_qelem *qp;
350     struct emp_qelem int_list;
351     int att_count;
352     natid plane_owner;
353     int dist;
354
355     plp = (struct plist *)bomb_list->q_forw;
356     plane_owner = plp->plane.pln_own;
357
358     sam_intercept(bomb_list, def_list, def_own, plane_owner, x, y,
359                   only_mission);
360     sam_intercept(esc_list, def_list, def_own, plane_owner, x, y,
361                   only_mission);
362
363     att_count = 0;
364     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
365         att_count++;
366     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
367         att_count++;
368     if (!att_count)
369         return;
370
371     emp_initque(&int_list);
372     icount = 0;
373     for (qp = def_list->q_forw; qp != def_list; qp = next) {
374         next = qp->q_forw;
375         plp = (struct plist *)qp;
376         pp = &plp->plane;
377         /* SAMs interdict separately */
378         if (plp->pcp->pl_flags & P_M)
379             continue;
380         if (only_mission && !pp->pln_mission)
381             continue;
382         dist = mapdist(x, y, pp->pln_x, pp->pln_y) * 2;
383         if (pp->pln_range < dist)
384             continue;
385         if (pp->pln_mission
386             && pp->pln_radius < mapdist(x, y, pp->pln_opx, pp->pln_opy))
387             continue;
388         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
389             || mission_pln_equip(plp, 0, P_F, 0) < 0) {
390             emp_remque(qp);
391             free(qp);
392             continue;
393         }
394         /* got one; delete from def_list, add to int_list */
395         emp_remque(qp);
396         emp_insque(qp, &int_list);
397         pp->pln_flags |= PLN_LAUNCHED;
398         pp->pln_mobil -= pln_mobcost(dist, pp, P_F);
399         putplane(pp->pln_uid, pp);
400         icount++;
401         if (icount > att_count)
402             break;
403     }
404     if (icount == 0)
405         return;
406     PR(plane_owner, "%d %s fighter%s rising to intercept!\n",
407        icount, cname(def_own), icount == 1 ? " is" : "s are");
408     PR(def_own, "%d fighter%s intercepting %s planes over %s!\n",
409        icount, icount == 1 ? " is" : "s are", cname(plane_owner),
410        xyas(x, y, def_own));
411     ac_combat_headers(plane_owner, def_own);
412     ac_airtoair(esc_list, &int_list);
413     ac_airtoair(bomb_list, &int_list);
414     PR(plane_owner, "\n");
415     PR(def_own, "\n");
416     pln_put(&int_list);
417 }
418
419 void
420 ac_combat_headers(natid plane_owner, natid def_own)
421 {
422     PR(plane_owner,
423        " %-10.10s %-10.10s  strength int odds  damage           results\n",
424        cname(plane_owner), cname(def_own));
425     PR(def_own,
426        " %-10.10s %-10.10s  strength int odds  damage           results\n",
427        cname(def_own), cname(plane_owner));
428 }
429
430 /*
431  * air-to-air combat.
432  */
433 void
434 ac_airtoair(struct emp_qelem *att_list, struct emp_qelem *int_list)
435 {
436     struct plist *attacker;
437     struct plist *interceptor;
438     struct emp_qelem *att;
439     struct emp_qelem *in;
440     int nplanes;
441     int more_att;
442     int more_int;
443     struct emp_qelem *att_next;
444     struct emp_qelem *in_next;
445
446     att = att_list->q_forw;
447     in = int_list->q_forw;
448     more_att = 1;
449     more_int = 1;
450     if (QEMPTY(att_list) || QEMPTY(int_list)) {
451         more_att = 0;
452         more_int = 0;
453     }
454     while (more_att || more_int) {
455         in_next = in->q_forw;
456         att_next = att->q_forw;
457         attacker = (struct plist *)att;
458         interceptor = (struct plist *)in;
459         nplanes = attacker->plane.pln_effic;
460         if (nplanes > interceptor->plane.pln_effic)
461             nplanes = interceptor->plane.pln_effic;
462         ac_dog(attacker, interceptor);
463         in = in_next;
464         att = att_next;
465         if (att == att_list) {
466             more_att = 0;
467             if (QEMPTY(att_list))
468                 more_int = 0;
469             else
470                 att = att->q_forw;
471         }
472         if (in == int_list) {
473             more_int = 0;
474             if (QEMPTY(int_list))
475                 more_att = 0;
476             else
477                 in = in->q_forw;
478         }
479     }
480 }
481
482 static void
483 ac_dog(struct plist *ap, struct plist *dp)
484 {
485     int att, def;
486     double odds;
487     int intensity;
488     natid att_own, def_own;
489     int adam, ddam;
490     char mesg[1024];
491     char temp[14];
492
493     att_own = ap->plane.pln_own;
494     def_own = dp->plane.pln_own;
495
496     PR(att_own, " %3.3s #%-4d  %3.3s #%-4d",
497        ap->pcp->pl_name,
498        ap->plane.pln_uid, dp->pcp->pl_name, dp->plane.pln_uid);
499     if (def_own)
500         PR(def_own, " %3.3s #%-4d  %3.3s #%-4d",
501            dp->pcp->pl_name,
502            dp->plane.pln_uid, ap->pcp->pl_name, ap->plane.pln_uid);
503     att = pln_att(&ap->plane);
504     if (att == 0)
505         att = pln_def(&ap->plane);
506     att = att * ap->plane.pln_effic / 100;
507     att = MAX(att, ap->pcp->pl_def / 2);
508
509     def = pln_def(&dp->plane) * dp->plane.pln_effic / 100;
510     def = MAX(def, dp->pcp->pl_def / 2);
511
512     if ((ap->pcp->pl_flags & P_F) && ap->bombs != 0)
513         att -= 2;
514     if ((dp->pcp->pl_flags & P_F) && dp->bombs != 0)
515         def -= 2;
516     att += ap->pcp->pl_stealth / 25.0;
517     def += dp->pcp->pl_stealth / 25.0;
518     if (att < 1) {
519         def += 1 - att;
520         att = 1;
521     }
522     if (def < 1) {
523         att += 1 - def;
524         def = 1;
525     }
526     odds = ((double)att / ((double)def + (double)att));
527     if (odds <= 0.05)
528         odds = 0.05;
529     intensity = roll(20) + roll(20) + roll(20) + roll(20) + 1;
530
531     PR(att_own, "   %3d/%-3d %3d  %3.2f  ", att, def, intensity, odds);
532     PR(def_own, "   %3d/%-3d %3d  %3.2f  ", def, att, intensity, odds);
533
534     adam = 0;
535     ddam = 0;
536     while ((intensity--) > 0) {
537
538         if (chance(odds)) {
539             ddam += 1;
540             if ((dp->plane.pln_effic - ddam) < PLANE_MINEFF)
541                 intensity = 0;
542         } else {
543             adam += 1;
544             if ((ap->plane.pln_effic - adam) < PLANE_MINEFF)
545                 intensity = 0;
546         }
547     }
548
549     if (dp->pcp->pl_flags & P_M)
550         ddam = 100;
551
552     PR(att_own, "%3d/%-3d", adam, ddam);
553     PR(def_own, "%3d/%-3d", ddam, adam);
554     ac_planedamage(ap, def_own, adam, def_own, 1, 0, mesg);
555     strncpy(temp, mesg, 14);
556     ac_planedamage(dp, att_own, ddam, att_own, 1, 0, mesg);
557     PR(att_own, "%-13.13s %-13.13s\n", temp, mesg);
558     PR(def_own, "%-13.13s %-13.13s\n", mesg, temp);
559
560     if (opt_HIDDEN) {
561         setcont(att_own, def_own, FOUND_FLY);
562         setcont(def_own, att_own, FOUND_FLY);
563     }
564 }
565
566 /*
567  * zap plane associated with plp.
568  * Damaging country is "from", damage is "dam".
569  * def_own is the country on the other side of the conflict from the plane
570  * owner. The only time def_own != from is when the interceptor is getting
571  * damaged.
572  *
573  * NOTE: This routine removes the appropriate plane element from the
574  * queue if it gets destroyed.  That means that the caller must assume
575  * that the current queue pointer is invalid on return from the ac_planedamage
576  * call.  (this has caused bugs in the past)
577  */
578 static void
579 ac_planedamage(struct plist *plp, natid from, int dam, natid other,
580                int checkabort, int show, char *mesg)
581 {
582     struct plnstr *pp;
583     int disp;
584     char dmess[255];
585     int eff;
586     natid plane_owner;
587
588     disp = 0;
589     pp = &plp->plane;
590     plane_owner = pp->pln_own;
591     eff = pp->pln_effic;
592     sprintf(dmess, " no damage");
593     if (dam <= 0) {
594         strcpy(mesg, dmess);
595         return;
596     }
597     memset(dmess, 0, sizeof(dmess));
598     eff -= dam;
599     if (eff < 0)
600         eff = 0;
601     if (eff < PLANE_MINEFF) {
602         sprintf(dmess, " shot down");
603         disp = 1;
604     } else if (eff < 80 && chance((80 - eff) / 100.0) && checkabort) {
605         sprintf(dmess, " aborted @%2d%%", eff);
606         disp = 2;
607     } else if (show == 0) {
608         sprintf(dmess, " cleared");
609     }
610
611     if ((plp->pcp->pl_flags & P_M) == 0) {
612         if (show) {
613             PR(plane_owner, "    %s %s takes %d%s.\n",
614                cname(pp->pln_own), prplane(pp), dam, dmess);
615             if (other)
616                 PR(other, "    %s %s takes %d%s.\n",
617                    cname(pp->pln_own), prplane(pp), dam, dmess);
618         }
619     }
620     if (show && checkabort == 1) {
621         PR(plane_owner, "\n");
622         if (other)
623             PR(other, "\n");
624     }
625
626     pp->pln_effic = eff;
627     pp->pln_mobil -= MIN(32 + pp->pln_mobil, dam / 2);
628     if (disp) {
629         if (disp == 1 && from != 0 && (plp->pcp->pl_flags & P_M) == 0)
630             nreport(from, N_DOWN_PLANE, pp->pln_own, 1);
631         pln_put1(plp);
632     } else
633         putplane(pp->pln_uid, pp);
634     strcpy(mesg, dmess);
635 }
636
637 static void
638 ac_doflak(struct emp_qelem *list, struct sctstr *from)
639 {
640     int gun;
641     natid plane_owner;
642     struct plist *plp;
643
644     plp = (struct plist *)list->q_forw;
645     plane_owner = plp->plane.pln_own;
646
647     gun = MIN(FLAK_GUN_MAX, from->sct_item[I_GUN]);
648     gun = roundavg(tfact(from->sct_own, 2.0 * gun));
649     if (gun > 0) {
650         PR(plane_owner, "firing %d flak guns in %s...\n",
651            gun, xyas(from->sct_x, from->sct_y, plane_owner));
652         if (from->sct_own != 0)
653             PR(from->sct_own, "firing %d flak guns in %s...\n",
654                gun, xyas(from->sct_x, from->sct_y, from->sct_own));
655         ac_fireflak(list, from->sct_own, gun);
656     }
657 }
658
659 static void
660 ac_shipflak(struct emp_qelem *list, coord x, coord y)
661 {
662     struct nstr_item ni;
663     struct shpstr ship;
664     struct mchrstr *mcp;
665     double flak, total, ngun;
666     int gun;
667     int rel;
668     struct plist *plp;
669     natid plane_owner;
670     natid from;
671
672     plp = (struct plist *)list->q_forw;
673     plane_owner = plp->plane.pln_own;
674
675     total = ngun = 0;
676     snxtitem_xy(&ni, EF_SHIP, x, y);
677     while (!QEMPTY(list) && nxtitem(&ni, &ship)) {
678         if (ship.shp_own == 0 || ship.shp_own == plane_owner)
679             continue;
680         mcp = &mchr[(int)ship.shp_type];
681         if (mcp->m_flags & M_SUB)
682             continue;
683         rel = getrel(getnatp(ship.shp_own), plane_owner);
684         if (rel > HOSTILE)
685             continue;
686         gun = shp_usable_guns(&ship);
687         if (gun == 0)
688             continue;
689         flak = gun * (ship.shp_effic / 100.0);
690         ngun += flak;
691         total += techfact(ship.shp_tech, flak * 2.0);
692
693         PR(ship.shp_own, "firing %.0f flak guns from %s...\n",
694            flak, prship(&ship));
695         from = ship.shp_own;
696     }
697
698     /* Limit to FLAK_GUN_MAX guns of average tech factor */
699     if (ngun > FLAK_GUN_MAX)
700         total *= FLAK_GUN_MAX / ngun;
701
702     gun = roundavg(total);
703     if (gun > 0) {
704         PR(plane_owner, "Flak!  Ships firing %d flak guns...\n", gun);
705         ac_fireflak(list, from, gun);
706     }
707 }
708
709 static void
710 ac_landflak(struct emp_qelem *list, coord x, coord y)
711 {
712     struct nstr_item ni;
713     struct lndstr land;
714     struct lchrstr *lcp;
715     double flak, total, ngun;
716     int aaf, gun;
717     int rel;
718     struct plist *plp;
719     natid plane_owner;
720     natid from;
721
722     plp = (struct plist *)list->q_forw;
723     plane_owner = plp->plane.pln_own;
724
725     total = ngun = 0;
726     snxtitem_xy(&ni, EF_LAND, x, y);
727     while (!QEMPTY(list) && nxtitem(&ni, &land)) {
728         if (land.lnd_own == 0 || land.lnd_own == plane_owner)
729             continue;
730         lcp = &lchr[(int)land.lnd_type];
731         aaf = lnd_aaf(&land);
732         if ((lcp->l_flags & L_FLAK) == 0 || aaf == 0)
733             continue;
734         if (land.lnd_ship >= 0 || land.lnd_land >= 0)
735             continue;
736         rel = getrel(getnatp(land.lnd_own), plane_owner);
737         if (rel > HOSTILE)
738             continue;
739         flak = aaf * 1.5 * land.lnd_effic / 100.0;
740         ngun += flak;
741         total += techfact(land.lnd_tech, flak * 2.0);
742
743         PR(land.lnd_own, "firing flak guns from unit %s (aa rating %d)\n",
744            prland(&land), aaf);
745         from = land.lnd_own;
746     }
747
748     /* Limit to FLAK_GUN_MAX guns of average tech factor */
749     if (ngun > FLAK_GUN_MAX)
750         total *= FLAK_GUN_MAX / ngun;
751
752     gun = roundavg(total);
753     if (gun > 0) {
754         PR(plane_owner, "Flak!  Land units firing %d flak guns...\n", gun);
755         ac_fireflak(list, from, gun);
756     }
757 }
758
759 /*
760  * Called from shipflak, landflak, and doflak.
761  */
762 static void
763 ac_fireflak(struct emp_qelem *list, natid from, int guns)
764 {
765     struct plist *plp;
766     int n;
767     struct emp_qelem *qp;
768     struct emp_qelem *next;
769     char msg[255];
770
771     plp = (struct plist *)list->q_forw;
772
773     for (qp = list->q_forw; qp != list; qp = next) {
774         next = qp->q_forw;
775         plp = (struct plist *)qp;
776         n = ac_flak_dam(guns, pln_def(&plp->plane), plp->pcp->pl_flags);
777         ac_planedamage(plp, from, n, 0, 2, 1, msg);
778     }
779 }
780
781 /*
782  * Calculate flak damage
783  */
784 int
785 ac_flak_dam(int guns, int def, int pl_flags)
786 {
787     int flak, dam;
788     float mult;
789     /*                             <-7      -7     -6     -5     -4 */
790     static float flaktable[18] = { 0.132f, 0.20f, 0.20f, 0.25f, 0.30f,
791     /*    -3     -2     -1      0     +1     +2     +3     +4 */
792          0.35f, 0.40f, 0.45f, 0.50f, 0.50f, 0.55f, 0.60f, 0.65f,
793     /*    +5    +6     +7     +8    >+8 */
794          0.70f,0.75f, 0.80f, 0.85f, 1.1305f };
795     enum { FLAK_MAX = sizeof(flaktable)/sizeof(flaktable[0]) - 1 };
796
797     flak = guns - def;
798     if ((pl_flags & P_T) == 0)
799         flak--;
800     if (pl_flags & P_X)
801         flak -= 2;
802     if (pl_flags & P_H)
803         flak -= 1;
804
805     if (flak > 8)
806         mult = flaktable[FLAK_MAX];
807     else if (flak < -7)
808         mult = flaktable[0];
809     else {
810         flak += 8;
811         mult = flaktable[flak];
812     }
813     mult *= flakscale;
814     dam = (int)((roll(8) + 2) * mult);
815     if (dam > 100)
816         dam = 100;
817     return dam;
818 }
819
820 /*
821  * Get planes available for interception duties.
822  */
823 static void
824 getilists(struct emp_qelem *list, unsigned char *rel, natid intruder)
825 {
826     natid cn;
827     struct plchrstr *pcp;
828     struct plnstr plane;
829     struct nstr_item ni;
830     struct plist *ip;
831
832     rel[0] = NEUTRAL;
833     for (cn = 1; cn < MAXNOC; cn++) {
834         rel[cn] = getrel(getnatp(cn), intruder);
835         emp_initque(&list[cn]);
836     }
837
838     snxtitem_all(&ni, EF_PLANE);
839     while (nxtitem(&ni, &plane)) {
840         if (rel[plane.pln_own] > HOSTILE)
841             continue;
842         pcp = &plchr[(int)plane.pln_type];
843         if ((pcp->pl_flags & P_F) == 0)
844             continue;
845         if (plane.pln_flags & PLN_LAUNCHED)
846             continue;
847         if (plane.pln_mission && plane.pln_mission != MI_AIR_DEFENSE)
848             continue;
849         if (plane.pln_mobil <= 0)
850             continue;
851         if (plane.pln_effic < 40)
852             continue;
853         if (!pln_airbase_ok(&plane, 0, 0))
854             continue;
855         /* got one! */
856         ip = malloc(sizeof(*ip));
857         ip->bombs = 0;
858         ip->misc = 0;
859         ip->pcp = &plchr[(int)plane.pln_type];
860         ip->plane = plane;
861         emp_insque(&ip->queue, &list[plane.pln_own]);
862     }
863 }
864
865 static int
866 do_evade(struct emp_qelem *bomb_list, struct emp_qelem *esc_list)
867 {
868     struct emp_qelem *qp;
869     double evade;
870     struct plist *plp;
871
872     evade = 100.0;
873     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw) {
874         plp = (struct plist *)qp;
875         if (evade > plp->pcp->pl_stealth / 100.0)
876             evade = plp->pcp->pl_stealth / 100.0;
877     }
878     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw) {
879         plp = (struct plist *)qp;
880         if (evade > plp->pcp->pl_stealth / 100.0)
881             evade = plp->pcp->pl_stealth / 100.0;
882     }
883
884     if (chance(evade))
885         return 1;
886
887     return 0;
888 }