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