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