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