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