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