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