]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
(mission_pln_wanted): New, factored out of mission_pln_sel(). No
[empserver] / src / lib / subs / mission.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  *  mission.c: Mission subroutines for planes/ships/units
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include "damage.h"
39 #include "empobj.h"
40 #include "file.h"
41 #include "item.h"
42 #include "misc.h"
43 #include "mission.h"
44 #include "nsc.h"
45 #include "optlist.h"
46 #include "path.h"
47 #include "player.h"
48 #include "prototypes.h"
49 #include "queue.h"
50 #include "xy.h"
51
52 struct genlist {
53     struct emp_qelem queue;     /* list of units */
54     void *cp;                   /* pointer to desc of thing */
55     struct empobj *thing;       /* thing's struct */
56 };
57
58 struct airport {
59     struct emp_qelem queue;
60     coord x, y;
61     natid own;
62 };
63
64 static void add_airport(struct emp_qelem *, coord, coord);
65 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
66                       char *, int);
67 static void build_mission_list(struct genlist *, coord, coord, int, natid);
68 static void build_mission_list_type(struct genlist *, coord, coord, int,
69                                     int, natid);
70 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
71 static int dosupport(struct genlist *, coord, coord, natid, natid);
72 static int find_airport(struct emp_qelem *, coord, coord);
73 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
74                            int, struct ichrstr *, int, int, int *);
75 static void mission_pln_sel(struct emp_qelem *, int, int, int);
76 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
77                            char *, int);
78
79 /*
80  * Interdict commodities & transported planes
81  */
82 int
83 ground_interdict(coord x, coord y, natid victim, char *s)
84 {
85     int cn;
86     int dam = 0, newdam, rel;
87     struct genlist mi[MAXNOC];
88     int z;
89
90     memset(mi, 0, sizeof(mi));
91     for (z = 1; z < MAXNOC; z++)
92         emp_initque((struct emp_qelem *)&mi[z]);
93
94     build_mission_list(mi, x, y, MI_INTERDICT, victim);
95
96     for (cn = 1; cn < MAXNOC; cn++) {
97         rel = getrel(getnatp(cn), victim);
98         if (rel > HOSTILE)
99             continue;
100
101         if (QEMPTY(&mi[cn].queue))
102             continue;
103
104         newdam = perform_mission(x, y, victim, &mi[cn].queue,
105                                  MI_INTERDICT, s, SECT_HARDTARGET);
106         dam += newdam;
107         if (newdam)
108             mpr(victim, "%s interdiction mission does %d damage!\n",
109                 cname(cn), newdam);
110     }
111     if (dam) {
112         collateral_damage(x, y, dam, 0);
113     }
114     return dam;
115 }
116
117 int
118 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
119 {
120     int coll;
121     struct sctstr sect;
122
123     if (!dam)
124         return 0;
125
126     getsect(x, y, &sect);
127     if (sect.sct_own) {
128         coll = ldround((double)dam * collateral_dam, 1);
129         if (coll == 0)
130             return 0;
131         mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
132             xyas(x, y, sect.sct_own), coll);
133         sectdamage(&sect, coll, list);
134         putsect(&sect);
135         return coll;
136     }
137     return 0;
138 }
139
140 static int
141 only_subs(struct emp_qelem *list)
142 {
143     struct emp_qelem *qp;
144     struct genlist *glp;
145     struct mchrstr *mcp;
146
147     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
148         glp = (struct genlist *)qp;
149
150         if (glp->thing->ef_type != EF_SHIP)
151             return 0;
152         mcp = glp->cp;
153         if (!(mcp->m_flags & M_SUB))
154             return 0;
155         /* It's a sub! */
156     }
157     /* They were all subs! */
158     return 1;
159 }
160
161
162 /*
163  *  Interdict ships & land units
164  */
165 int
166 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
167                int mission)
168 {
169     int cn;
170     int dam = 0, newdam;
171     struct genlist mi[MAXNOC];
172     int z;
173     int osubs;
174
175     memset(mi, 0, sizeof(mi));
176     for (z = 1; z < MAXNOC; z++)
177         emp_initque((struct emp_qelem *)&mi[z]);
178
179     build_mission_list(mi, x, y, mission, victim);
180
181     for (cn = 1; cn < MAXNOC; cn++) {
182         if (cn == victim)
183             continue;
184         if (mission == MI_SINTERDICT) {
185             if (getrel(getnatp(cn), victim) >= FRIENDLY)
186                 continue;
187         } else if (getrel(getnatp(cn), victim) > HOSTILE)
188             continue;
189
190         if (QEMPTY(&mi[cn].queue))
191             continue;
192
193         osubs = only_subs(&mi[cn].queue);
194         newdam = perform_mission(x, y, victim, &mi[cn].queue,
195                                  mission, s, hardtarget);
196         dam += newdam;
197         if (newdam) {
198             /* If only subs responded, then we don't know who's
199                subs they are */
200             mpr(victim, "%s interdiction mission does %d damage!\n",
201                 osubs ? "Enemy" : cname(cn), newdam);
202         }
203     }
204     if (dam) {
205         collateral_damage(x, y, dam, 0);
206     }
207     return dam;
208 }
209
210 /*
211  *  Perform a mission against victim, on behalf of actee
212  */
213 int
214 off_support(coord x, coord y, natid victim, natid actee)
215 {
216     int dam = 0;
217     struct genlist mi[MAXNOC];
218     int z;
219
220     memset(mi, 0, sizeof(mi));
221     for (z = 1; z < MAXNOC; z++)
222         emp_initque((struct emp_qelem *)&mi[z]);
223
224     build_mission_list(mi, x, y, MI_SUPPORT, victim);
225     build_mission_list(mi, x, y, MI_OSUPPORT, victim);
226
227     dam = dosupport(mi, x, y, victim, actee);
228     return dam;
229 }
230
231 /*
232  *  Perform a mission against victim, on behalf of actee
233  */
234 int
235 def_support(coord x, coord y, natid victim, natid actee)
236 {
237     int dam = 0;
238     struct genlist mi[MAXNOC];
239     int z;
240
241     memset(mi, 0, sizeof(mi));
242     for (z = 1; z < MAXNOC; z++)
243         emp_initque((struct emp_qelem *)&mi[z]);
244
245     build_mission_list(mi, x, y, MI_SUPPORT, victim);
246     build_mission_list(mi, x, y, MI_DSUPPORT, victim);
247
248     dam = dosupport(mi, x, y, victim, actee);
249     return dam;
250 }
251
252 static int
253 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
254 {
255     int cn;
256     int rel;
257     int dam = 0;
258
259     for (cn = 1; cn < MAXNOC; cn++) {
260         rel = getrel(getnatp(cn), actee);
261         if ((cn != actee) && (rel != ALLIED))
262             continue;
263         rel = getrel(getnatp(cn), victim);
264         if ((cn != actee) && (rel != AT_WAR))
265             continue;
266
267         if (QEMPTY(&mi[cn].queue))
268             continue;
269
270         dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
271                                "", SECT_HARDTARGET);
272     }
273     return dam;
274 }
275
276 static void
277 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
278                    natid victim)
279 {
280     build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
281     build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
282     build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
283 }
284
285 static void
286 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
287                         int type, natid victim)
288 {
289     struct nstr_item ni;
290     struct genlist *glp;
291     struct empobj *gp;
292     union empobj_storage item;
293     int dist;
294     int radius;
295     int relat;
296     struct sctstr sect;
297
298     snxtitem_all(&ni, type);
299     while (nxtitem(&ni, &item)) {
300         gp = (struct empobj *)&item;
301
302         if (gp->own == 0)
303             continue;
304
305         if (gp->mobil < 1)
306             continue;
307
308         if ((gp->mission != mission) && (mission != MI_SINTERDICT))
309             continue;
310
311         if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
312             (gp->mission != MI_INTERDICT))
313             continue;
314
315         relat = getrel(getnatp(gp->own), victim);
316         if (mission == MI_SINTERDICT) {
317             if (relat >= FRIENDLY)
318                 continue;
319             else if (type != EF_PLANE && relat > HOSTILE)
320                 continue;
321         } else if (relat > HOSTILE)
322             continue;
323
324         dist = mapdist(x, y, gp->opx, gp->opy);
325
326         radius = gp->radius;
327         if (mission != MI_RESERVE)      /* XXX */
328             oprange(gp, &radius);
329
330         if (dist > radius)
331             continue;
332
333         /* Ok, it is within the operations range. */
334         /* Now check from where the object actually is */
335         dist = mapdist(x, y, gp->x, gp->y);
336         radius = 999;
337         oprange(gp, &radius);
338         if (dist > radius)
339             continue;
340         /* Ok, the object can get to where the x,y is */
341
342         if (opt_SLOW_WAR) {
343             if (mission != MI_AIR_DEFENSE) {
344                 getsect(x, y, &sect);
345                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
346
347                     /*
348                      * If the player->owner of the unit isn't at war
349                      * with the victim, and doesn't own the
350                      * sect being acted upon, and isn't the
351                      * old player->owner of that sect, bounce them.
352                      */
353                     if (sect.sct_type != SCT_WATER &&
354                         sect.sct_own != gp->own &&
355                         sect.sct_oldown != gp->own)
356                         continue;
357                 }
358             }
359         }
360
361         glp = malloc(sizeof(struct genlist));
362         memset(glp, 0, sizeof(struct genlist));
363         glp->cp = get_empobj_chr(gp);
364         glp->thing = malloc(sizeof(item));
365         memcpy(glp->thing, &item, sizeof(item));
366         emp_insque(&glp->queue, &mi[gp->own].queue);
367     }
368 }
369
370 static void
371 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
372 {
373     struct nstr_item ni;
374     struct plist *plp;
375     struct plnstr plane;
376     int dist;
377
378     snxtitem_all(&ni, EF_PLANE);
379     while (nxtitem(&ni, &plane)) {
380         if (plane.pln_own != cn)
381             continue;
382
383         if (plane.pln_mission != MI_ESCORT)
384             continue;
385
386         dist = mapdist(x, y, plane.pln_x, plane.pln_y);
387         if (dist > plane.pln_range / 2)
388             continue;
389
390         plp = malloc(sizeof(struct plist));
391         memset(plp, 0, sizeof(struct plist));
392         plp->pcp = &plchr[(int)plane.pln_type];
393         plp->plane = plane;
394         emp_insque(&plp->queue, escorts);
395     }
396 }
397
398 static int
399 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
400                 int mission, char *s, int hardtarget)
401 {
402     struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
403     struct emp_qelem *newqp;
404     struct genlist *glp;
405     struct plist *plp;
406     struct empobj *gp;
407     struct lndstr *lp;
408     struct shpstr *sp;
409     struct sctstr sect;
410     struct mchrstr *mcp;
411     struct plchrstr *pcp;
412     int dam = 0, dam2, mission_flags, tech;
413     natid plane_owner = 0;
414     int gun, shell, md, range, air_dam = 0;
415     double prb, hitchance, vrange;
416
417     getsect(x, y, &sect);
418
419     emp_initque(&missiles);
420     emp_initque(&bombers);
421     emp_initque(&escorts);
422     emp_initque(&airp);
423
424     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
425         glp = (struct genlist *)qp;
426         gp = glp->thing;
427
428         md = mapdist(x, y, gp->x, gp->y);
429
430         if (glp->thing->ef_type == EF_LAND) {
431             lp = (struct lndstr *)glp->thing;
432
433             if (lp->lnd_effic < LAND_MINFIREEFF)
434                 continue;
435
436             if (mission == MI_SINTERDICT)
437                 continue;
438
439             if ((mission == MI_INTERDICT) &&
440                 (md > land_max_interdiction_range))
441                 continue;
442
443             if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
444                 continue;
445
446             if (lp->lnd_item[I_MILIT] < 1)
447                 continue;
448
449             range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
450             if (md > range)
451                 continue;
452
453             shell = lp->lnd_item[I_SHELL];
454             gun = lp->lnd_item[I_GUN];
455             if (shell == 0 || gun == 0)
456                 continue;
457
458             if (has_supply(lp)) {
459                 use_supply(lp);
460                 putland(lp->lnd_uid, lp);
461                 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
462                                            lp->lnd_ammo, shell), 1);
463                 if (sect.sct_type == SCT_WATER) {
464                     if (chance(lp->lnd_acc / 100.0))
465                         dam2 = ldround(dam2 / 2.0, 1);
466                 }
467                 dam += dam2;
468                 if (sect.sct_type == SCT_WATER)
469                     nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
470                 else
471                     nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
472                 wu(0, lp->lnd_own,
473                    "%s fires at %s %s at %s\n",
474                    prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
475
476                 mpr(victim, "%s %s fires at you at %s\n",
477                     cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
478             }
479         } else if (glp->thing->ef_type == EF_SHIP) {
480             sp = (struct shpstr *)glp->thing;
481             mcp = glp->cp;
482
483             if (sp->shp_effic < 60)
484                 continue;
485             if (sp->shp_frnge == 0)
486                 continue;
487             if (((mission == MI_INTERDICT) ||
488                  (mission == MI_SINTERDICT)) &&
489                 (md > ship_max_interdiction_range))
490                 continue;
491             if (sp->shp_item[I_MILIT] < 1)
492                 continue;
493 /*
494   if ((mcp->m_flags & M_SUB) &&
495   (sect.sct_type != SCT_WATER))
496   continue;
497 */
498             if (mission == MI_SINTERDICT) {
499                 if (!(mcp->m_flags & M_SONAR))
500                     continue;
501                 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
502                     continue;
503                 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
504                 vrange *= sp->shp_effic / 200.0;
505                 if (md > vrange)
506                     continue;
507                 /* can't look all the time */
508                 if (chance(0.5))
509                     continue;
510             }
511             if (mcp->m_flags & M_SUB) {
512 /* If we aren't shooting at "subs" or "ships" don't fire at all from
513    a sub. */
514                 if (*s != 's')
515                     continue;
516                 if (sp->shp_mobil < 0)
517                     continue;
518                 gun = sp->shp_item[I_GUN];
519                 if (gun < 1)
520                     continue;
521                 shell = sp->shp_item[I_SHELL];
522                 if (shell < SHP_TORP_SHELLS)
523                     shell += supply_commod(sp->shp_own,
524                                            sp->shp_x, sp->shp_y, I_SHELL,
525                                            SHP_TORP_SHELLS - shell);
526                 if (shell < SHP_TORP_SHELLS)
527                     continue;
528
529                 range = roundrange(torprange(sp));
530                 if (md > range)
531                     continue;
532
533                 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
534                     continue;
535                 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
536                 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
537                 putship(sp->shp_uid, sp);
538                 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
539
540                 wu(0, sp->shp_own,
541                    "%s locking on %s %s in %s\n",
542                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
543                 wu(0, sp->shp_own,
544                    "\tEffective torpedo range is %d.0\n", range);
545                 wu(0, sp->shp_own,
546                    "\tWhooosh... Hitchance = %d%%\n",
547                    (int)(hitchance * 100));
548
549                 if (hitchance < 1.0 && !chance(hitchance)) {
550                     wu(0, sp->shp_own, "\tMissed\n");
551                     mpr(victim,
552                         "Incoming torpedo sighted @ %s missed (whew)!\n",
553                         xyas(x, y, victim));
554                     continue;
555                 }
556                 wu(0, sp->shp_own, "\tBOOM!...\n");
557                 dam2 = TORP_DAMAGE();
558
559                 dam += dam2;
560                 nreport(victim, N_TORP_SHIP, 0, 1);
561                 wu(0, sp->shp_own,
562                    "\tTorpedo hit %s %s for %d damage\n",
563                    cname(victim), s, dam2);
564
565                 mpr(victim,
566                     "Incoming torpedo sighted @ %s hits and does %d damage!\n",
567                     xyas(x, y, victim), dam2);
568             } else {
569                 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
570                 if (md > range)
571                     continue;
572                 gun = sp->shp_item[I_GUN];
573                 gun = MIN(gun, sp->shp_glim);
574                 shell = sp->shp_item[I_SHELL];
575                 if (shell < gun)
576                     shell += supply_commod(sp->shp_own,
577                                            sp->shp_x, sp->shp_y, I_SHELL,
578                                            gun - shell);
579                 gun = MIN(gun, shell);
580                 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
581                 if (gun == 0)
582                     continue;
583                 gun = MAX(gun, 1);
584                 dam2 = seagun(sp->shp_effic, gun);
585                 if (range == 0.0)
586                     prb = 1.0;
587                 else
588                     prb = (double)md / range;
589                 prb *= prb;
590                 if (chance(prb))
591                     dam2 /= 2;
592                 dam += dam2;
593                 if (sect.sct_type == SCT_WATER)
594                     nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
595                 else
596                     nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
597                 wu(0, sp->shp_own,
598                    "%s fires at %s %s at %s\n",
599                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
600
601                 mpr(victim, "%s %s fires at you at %s\n",
602                     cname(sp->shp_own), prship(sp), xyas(x, y, victim));
603
604                 sp->shp_item[I_SHELL] = shell - gun;
605                 putship(sp->shp_uid, sp);
606             }
607         } else if (glp->thing->ef_type == EF_PLANE) {
608             pcp = glp->cp;
609             if (pcp->pl_flags & P_M)
610                 /* units have their own missile interdiction */
611                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
612                     continue;
613
614             /* save planes for later */
615             plp = malloc(sizeof(struct plist));
616
617             memset(plp, 0, sizeof(struct plist));
618             plp->pcp = pcp;
619             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
620             if (plp->pcp->pl_flags & P_M)
621                 emp_insque(&plp->queue, &missiles);
622             else
623                 emp_insque(&plp->queue, &bombers);
624             plane_owner = plp->plane.pln_own;
625         }
626     }
627     if (!QEMPTY(&missiles)) {
628         /* I arbitrarily chose 100 mindam -KHS */
629         dam +=
630             msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
631                               "sector", victim, mission);
632         qp = missiles.q_forw;
633         while (qp != (&missiles)) {
634             newqp = qp->q_forw;
635             emp_remque(qp);
636             free(qp);
637             qp = newqp;
638         }
639     }
640
641     if (QEMPTY(&bombers)) {
642         qp = list->q_forw;
643         while (qp != list) {
644             glp = (struct genlist *)qp;
645             qp = qp->q_forw;
646
647             free(glp->thing);
648             free(glp);
649         }
650         return dam;
651     }
652     /*
653      * If there are planes performing an
654      * interdict or support mission, find
655      * some escorts for them, if possible.
656      * Up to 2 per bomber, if possible.
657      */
658     find_escorts(x, y, plane_owner, &escorts);
659
660     if (mission == MI_SINTERDICT)
661         mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
662     else
663         mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
664
665     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
666
667     for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
668         plp = (struct plist *)qp;
669         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
670             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
671     }
672
673     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
674         struct airport *air;
675         char buf[512];
676         char *pp;
677
678         air = (struct airport *)qp;
679         md = mapdist(x, y, air->x, air->y);
680
681         emp_initque(&b);
682         emp_initque(&e);
683
684         /* Split off the bombers at this base into b */
685         divide(&bombers, &b, air->x, air->y);
686
687         /* Split off the escorts at this base into e */
688         divide(&escorts, &e, air->x, air->y);
689
690         tech = 0;
691         mission_flags = 0;
692         mission_flags |= P_X;   /* stealth (shhh) */
693         mission_flags |= P_H;   /* gets turned off if not all choppers */
694
695         mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
696                                         0, mission_flags, &tech);
697
698         if (QEMPTY(&b))
699             continue;
700
701         mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
702                                         P_F | P_ESC, mission_flags, &tech);
703
704         pp = BestAirPath(buf, air->x, air->y, x, y);
705         if (CANT_HAPPEN(!pp))
706             continue;
707         wu(0, plane_owner, "Flying %s mission from %s\n",
708            mission_name(mission), xyas(air->x, air->y, plane_owner));
709         if (air->own && (air->own != plane_owner)) {
710             wu(0, air->own, "%s is flying %s mission from %s\n",
711                cname(plane_owner), mission_name(mission),
712                xyas(air->x, air->y, air->own));
713         }
714
715         ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
716
717         if (!QEMPTY(&b))
718             air_dam +=
719                 air_damage(&b, x, y, mission, victim, s, hardtarget);
720
721         pln_put(&b);
722         pln_put(&e);
723     }
724
725     if (air_dam > 0) {
726         dam += air_dam;
727         if (sect.sct_type == SCT_WATER)
728             nreport(plane_owner, N_SHP_BOMB, victim, 1);
729         else
730             nreport(plane_owner, N_SCT_BOMB, victim, 1);
731     }
732
733     /* free up all this memory */
734     qp = list->q_forw;
735     while (qp != list) {
736         glp = (struct genlist *)qp;
737         qp = qp->q_forw;
738
739         free(glp->thing);
740         free(glp);
741     }
742
743     qp = escorts.q_forw;
744     while (qp != (&escorts)) {
745         newqp = qp->q_forw;
746         emp_remque(qp);
747         free(qp);
748         qp = newqp;
749     }
750
751     qp = bombers.q_forw;
752     while (qp != (&bombers)) {
753         newqp = qp->q_forw;
754         emp_remque(qp);
755         free(qp);
756         qp = newqp;
757     }
758
759     return dam;
760 }
761
762 int
763 cando(int mission, int type)
764 {
765     switch (mission) {
766     case MI_ESCORT:
767         if (type == EF_PLANE)
768             return 1;
769         return 0;
770     case MI_AIR_DEFENSE:
771         if (type == EF_PLANE)
772             return 1;
773         return 0;
774     case MI_SINTERDICT:
775         if ((type == EF_PLANE) || (type == EF_SHIP))
776             return 1;
777         return 0;
778     case MI_INTERDICT:
779         return 1;
780     case MI_SUPPORT:
781     case MI_OSUPPORT:
782     case MI_DSUPPORT:
783         if (type == EF_PLANE)
784             return 1;
785         return 0;
786     case MI_RESERVE:
787         if (type == EF_LAND)
788             return 1;
789         return 0;
790     }
791
792     return 0;
793 }
794
795 char *
796 mission_name(short mission)
797 {
798     switch (mission) {
799     case MI_INTERDICT:
800         return "an interdiction";
801     case MI_SUPPORT:
802         return "a support";
803     case MI_OSUPPORT:
804         return "a offensive support";
805     case MI_DSUPPORT:
806         return "a defensive support";
807     case MI_RESERVE:
808         return "a reserve";
809     case MI_ESCORT:
810         return "an escort";
811     case MI_SINTERDICT:
812         return "a sub interdiction";
813     case MI_AIR_DEFENSE:
814         return "an air defense";
815     }
816     return "a mysterious";
817 }
818
819 void
820 show_mission(int type, struct nstr_item *np)
821 {
822     int first = 1, radius;
823     union empobj_storage item;
824     struct empobj *gp;
825
826     while (nxtitem(np, &item)) {
827         gp = (struct empobj *)&item;
828         if (!player->owner || gp->own == 0)
829             continue;
830
831         if (first) {
832             pr("Thing                         x,y   op-sect rad mission\n");
833             first = 0;
834         }
835         pr("%-25s", obj_nameof(gp));
836         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
837         if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
838             gp->mission == MI_OSUPPORT ||
839             gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
840             radius = 999;
841             oprange(gp, &radius);
842             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
843             if (radius < gp->radius)
844                 pr("  %4d", radius);
845             else
846                 pr("  %4d", gp->radius);
847         } else if (gp->mission == MI_RESERVE) {
848             struct sctstr sect;
849             int plus = 2;
850
851             getsect(gp->x, gp->y, &sect);
852             if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
853                 plus++;
854
855             if (item.land.lnd_rad_max == 0)
856                 plus = 0;
857             else
858                 plus += item.land.lnd_rad_max;
859             prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
860             pr("  %4d", plus);
861         } else if (gp->mission == MI_ESCORT) {
862             pr("        ");
863             pr("  %4d", item.plane.pln_range / 2);
864         } else
865             pr("              ");
866         if (gp->mission)
867             pr(" is on %s mission\n", mission_name(gp->mission));
868         else
869             pr(" has no mission.\n");
870     }
871 }
872
873 int
874 oprange(struct empobj *gp, int *radius)
875 {
876     int range;
877
878     switch (gp->ef_type) {
879     case EF_SHIP:
880         range = ldround(effrange(((struct shpstr *)gp)->shp_frnge,
881                                  ((struct shpstr *)gp)->shp_tech), 1);
882         break;
883     case EF_LAND:
884         range = ldround(effrange(((struct lndstr *)gp)->lnd_frg,
885                                  ((struct lndstr *)gp)->lnd_tech), 1);
886         break;
887     case EF_PLANE:
888         /* missiles go one way, so we can use all the range */
889         if (plchr[(int)gp->type].pl_flags & P_M)
890             range = ((struct plnstr *)gp)->pln_range;
891         else
892             range = ((struct plnstr *)gp)->pln_range / 2;
893         break;
894     default:
895         CANT_HAPPEN("bad TYPE");
896         range = -1;
897     }
898
899     if (*radius > range)
900         *radius = range;
901
902     return range;
903 }
904
905 static int
906 mission_pln_wanted(struct plnstr *pp, int wantflags, int nowantflags)
907 {
908     int y, bad, bad1;
909     unsigned x;
910     struct plchrstr *pcp = plchr + pp->pln_type;
911
912     bad = 0;
913     bad1 = 0;
914     if (wantflags) {
915         for (x = 0; x < sizeof(wantflags) * 8; x++) {
916             y = (1 << x);
917             if ((wantflags & y) == y)
918                 if ((pcp->pl_flags & y) != y) {
919                     switch (y) {
920                     case P_F:
921                     case P_ESC:
922                         bad1 = 2;
923                         break;
924                     case P_E:
925                     case P_L:
926                     case P_K:
927                         bad1 = 1;
928                         break;
929                     default:
930                         bad = 1;
931                     }
932                 }
933         }
934         if (bad) {
935             return 0;
936         }
937         if (bad1 == 2) {
938             if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
939                 bad1 = 0;
940         }
941         if (bad1 == 1) {
942             if ((pcp->pl_flags & P_E) ||
943                 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
944                 bad1 = 0;
945         }
946         if (bad1) {
947             return 0;
948         }
949     }
950     bad = 0;
951     bad1 = 0;
952     if (nowantflags) {
953         for (x = 0; x < sizeof(nowantflags) * 8; x++) {
954             y = (1 << x);
955             if ((nowantflags & y) == y)
956                 if ((pcp->pl_flags & y) == y)
957                     bad = 1;
958         }
959         if (bad) {
960             return 0;
961         }
962     }
963     return 1;
964 }
965
966 /*
967  *  Remove all planes who cannot go on
968  *  the mission from the plane list.
969  */
970 static void
971 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
972                 int hardtarget)
973 {
974     struct emp_qelem *qp, *next;
975     struct plnstr *pp;
976     struct shpstr ship;
977     struct lndstr land;
978     struct sctstr sect;
979     struct plchrstr *pcp;
980     struct plist *plp;
981     int y, bad, bad1;
982     unsigned x;
983
984     for (qp = list->q_forw; qp != list; qp = next) {
985         next = qp->q_forw;
986         plp = (struct plist *)qp;
987         pp = &plp->plane;
988         pcp = plp->pcp;
989
990         if (pp->pln_effic < 40) {
991             emp_remque(qp);
992             free(qp);
993             continue;
994         }
995
996         if (pp->pln_mobil < 1) {
997             emp_remque(qp);
998             free(qp);
999             continue;
1000         }
1001
1002         if (opt_MARKET) {
1003             if (ontradingblock(EF_PLANE, pp)) {
1004                 emp_remque(qp);
1005                 free(qp);
1006                 continue;
1007             }
1008         }
1009
1010         if (!mission_pln_wanted(pp, wantflags, nowantflags)) {
1011             emp_remque(qp);
1012             free(qp);
1013             continue;
1014         }
1015
1016         if (pp->pln_ship >= 0) {
1017             if (!getship(pp->pln_ship, &ship)) {
1018               shipsunk:
1019                 pp->pln_effic = 0;
1020                 putplane(pp->pln_uid, pp);
1021                 emp_remque(qp);
1022                 free(qp);
1023                 continue;
1024             }
1025             if (!could_be_on_ship(pp, &ship)) {
1026                 goto shipsunk;
1027             }
1028             if (ship.shp_effic < SHIP_MINEFF) {
1029                 goto shipsunk;
1030             }
1031             /* Can't fly off of inefficient or non-owned, non-allied ships */
1032             if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1033                 ((ship.shp_own != pp->pln_own) &&
1034                  (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1035                 emp_remque(qp);
1036                 free(qp);
1037                 continue;
1038             }
1039         }
1040         if (pp->pln_land >= 0) {
1041             if (!getland(pp->pln_land, &land)) {
1042               landdead:
1043                 pp->pln_effic = 0;
1044                 putplane(pp->pln_uid, pp);
1045                 emp_remque(qp);
1046                 free(qp);
1047                 continue;
1048             }
1049             if (!(pcp->pl_flags & P_E))
1050                 goto landdead;
1051             if (land.lnd_effic < LAND_MINEFF)
1052                 goto landdead;
1053
1054             /* Can't fly off of inefficient or non-owned, non-allied units */
1055             if ((land.lnd_effic < LND_AIROPS_EFF) ||
1056                 ((land.lnd_own != pp->pln_own) &&
1057                  (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1058                 emp_remque(qp);
1059                 free(qp);
1060                 continue;
1061             }
1062
1063             /* Can't fly off units in ships or other units */
1064             if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1065                 emp_remque(qp);
1066                 free(qp);
1067                 continue;
1068             }
1069         }
1070         /* Now, check the sector status if not on a plane or unit */
1071         if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1072             /* If we can't get the sector, we can't check it, and can't fly */
1073             if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
1074                 emp_remque(qp);
1075                 free(qp);
1076                 continue;
1077             }
1078             /* First, check allied status */
1079             /* Can't fly from non-owned sectors or non-allied sectors */
1080             if ((sect.sct_own != pp->pln_own) &&
1081                 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1082                 emp_remque(qp);
1083                 free(qp);
1084                 continue;
1085             }
1086             /* non-vtol plane */
1087             if ((pcp->pl_flags & P_V) == 0) {
1088                 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1089                     emp_remque(qp);
1090                     free(qp);
1091                     continue;
1092                 }
1093             }
1094         }
1095         if (pcp->pl_flags & P_A) {
1096             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1097                 emp_remque(qp);
1098                 free(qp);
1099                 continue;
1100             }
1101         }
1102
1103         putplane(pp->pln_uid, pp);
1104     }
1105 }
1106
1107 /*
1108  * Arm only the planes at x,y
1109  */
1110 static int
1111 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1112                 int mission, struct ichrstr *ip, int flags,
1113                 int mission_flags, int *tech)
1114 {
1115     struct emp_qelem *qp;
1116     struct emp_qelem *next;
1117     struct plist *plp;
1118
1119     if (*tech == 0)
1120         *tech = 9999;
1121     for (qp = list->q_forw; qp != list; qp = next) {
1122         next = qp->q_forw;
1123         plp = (struct plist *)qp;
1124
1125         if (plp->plane.pln_x != x)
1126             continue;
1127         if (plp->plane.pln_y != y)
1128             continue;
1129
1130         if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1131             emp_remque(qp);
1132             free(qp);
1133             continue;
1134         }
1135         if (flags & (P_S | P_I)) {
1136             if (plp->pcp->pl_flags & P_S)
1137                 mission_flags |= P_S;
1138             if (plp->pcp->pl_flags & P_I)
1139                 mission_flags |= P_I;
1140         }
1141         if (*tech > plp->plane.pln_tech)
1142             *tech = plp->plane.pln_tech;
1143         if (!(plp->pcp->pl_flags & P_H))
1144             /* no stealth on this mission */
1145             mission_flags &= ~P_H;
1146         if (!(plp->pcp->pl_flags & P_X))
1147             /* no stealth on this mission */
1148             mission_flags &= ~P_X;
1149         if (!(plp->pcp->pl_flags & P_A)) {
1150             /* no asw on this mission */
1151             mission_flags &= ~P_A;
1152         }
1153         if (!(plp->pcp->pl_flags & P_MINE)) {
1154             /* no asw on this mission */
1155             mission_flags &= ~P_MINE;
1156         }
1157
1158         /*
1159          *      Mob costs for missions are 1/2 normal
1160          *       Not anymore. :)
1161          */
1162 /*      plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1163         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1164
1165     }
1166     return mission_flags;
1167 }
1168
1169 int
1170 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1171                   char mission)
1172 {
1173     struct plchrstr *pcp;
1174     struct plnstr *pp;
1175     int needed;
1176     struct lndstr land;
1177     struct shpstr ship;
1178     struct sctstr sect;
1179     i_type itype;
1180     int rval;
1181     short *item;
1182
1183     pp = &plp->plane;
1184     pcp = plp->pcp;
1185     if (pp->pln_ship >= 0) {
1186         getship(pp->pln_ship, &ship);
1187         item = ship.shp_item;
1188     } else if (pp->pln_land >= 0) {
1189         getland(pp->pln_land, &land);
1190         item = land.lnd_item;
1191     } else {
1192         getsect(pp->pln_x, pp->pln_y, &sect);
1193         item = sect.sct_item;
1194     }
1195     if (pcp->pl_fuel > item[I_PETROL]) {
1196         return -1;
1197     }
1198     item[I_PETROL] -= pcp->pl_fuel;
1199     rval = 0;
1200     if (!(flags & P_F)) {
1201         itype = I_NONE;
1202         needed = 0;
1203         switch (mission) {
1204         case 's':
1205         case 'p':
1206             if (pp->pln_nuketype == -1) {
1207                 itype = I_SHELL;
1208                 needed = pp->pln_load;
1209             }
1210             break;
1211         case 't':
1212             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1213                 break;
1214             itype = ip->i_uid;
1215             needed = (pp->pln_load * 2) / ip->i_lbs;
1216             break;
1217         case 'd':
1218             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1219                 break;
1220             itype = ip->i_uid;
1221             needed = (pp->pln_load * 2) / ip->i_lbs;
1222             break;
1223         case 'a':
1224             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1225                 break;
1226             itype = I_MILIT;
1227             needed = pp->pln_load / ip->i_lbs;
1228             break;
1229         case 'n':
1230             if (pp->pln_nuketype == -1)
1231                 rval = -1;
1232             break;
1233         case 'i':               /* missile interception */
1234             if (pp->pln_load) {
1235                 itype = I_SHELL;
1236                 needed = pp->pln_load;
1237             }
1238             break;
1239         default:
1240             break;
1241         }
1242         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1243             return -1;
1244         }
1245         if (itype != I_NONE) {
1246             if (itype == I_SHELL && item[itype] < needed)
1247                 item[itype] += supply_commod(plp->plane.pln_own,
1248                                              plp->plane.pln_x,
1249                                              plp->plane.pln_y,
1250                                              I_SHELL, needed);
1251             if (item[itype] < needed)
1252                 return -1;
1253             item[itype] -= needed;
1254         }
1255         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1256             plp->bombs = needed;
1257         else
1258             plp->misc = needed;
1259     }
1260     if (pp->pln_ship >= 0)
1261         putship(ship.shp_uid, &ship);
1262     else if (pp->pln_land >= 0)
1263         putland(land.lnd_uid, &land);
1264     else
1265         putsect(&sect);
1266     return rval;
1267 }
1268
1269 /*
1270  *  Return 1 if this x,y pair is in the list
1271  */
1272 static int
1273 find_airport(struct emp_qelem *airp, coord x, coord y)
1274 {
1275     struct emp_qelem *qp;
1276     struct airport *a;
1277
1278     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1279         a = (struct airport *)qp;
1280         if ((a->x == x) && (a->y == y))
1281             return 1;
1282     }
1283
1284     return 0;
1285 }
1286
1287 /* #*# This needs to be changed to include acc's -KHS */
1288 static void
1289 add_airport(struct emp_qelem *airp, coord x, coord y)
1290 {
1291     struct airport *a;
1292     struct sctstr sect;
1293
1294     a = malloc(sizeof(struct airport));
1295
1296     a->x = x;
1297     a->y = y;
1298     getsect(x, y, &sect);
1299     a->own = sect.sct_own;
1300
1301     emp_insque((struct emp_qelem *)a, airp);
1302 }
1303
1304 /*
1305  *  Take all the planes in list 1 that
1306  *  are at x,y, and put them into list 2.
1307  */
1308 static void
1309 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1310 {
1311     struct emp_qelem *qp, *next;
1312     struct plist *plp;
1313
1314     for (qp = l1->q_forw; qp != l1; qp = next) {
1315         next = qp->q_forw;
1316         plp = (struct plist *)qp;
1317
1318         if (plp->plane.pln_x != x)
1319             continue;
1320         if (plp->plane.pln_y != y)
1321             continue;
1322
1323         emp_remque(qp);
1324         emp_insque(qp, l2);
1325     }
1326 }
1327
1328 static int
1329 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1330            natid victim, char *s, int hardtarget)
1331 {
1332     struct emp_qelem *qp;
1333     struct plist *plp;
1334     struct plnstr *pp;
1335     int newdam, dam = 0;
1336     int hitchance;
1337     int nukedam;
1338
1339     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1340         plp = (struct plist *)qp;
1341         pp = &plp->plane;
1342
1343         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1344             continue;
1345
1346         if (!plp->bombs)
1347             continue;
1348
1349         newdam = 0;
1350         if (plp->pcp->pl_flags & P_A) {
1351             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1352                 wu(0, pp->pln_own,
1353                    "\t%s detects sub movement in %s\n",
1354                    prplane(pp), xyas(x, y, pp->pln_own));
1355                 continue;
1356             }
1357             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1358                 wu(0, pp->pln_own,
1359                    "\t%s tracks %s %s at %s\n",
1360                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1361                 continue;
1362             }
1363             wu(0, pp->pln_own,
1364                "\t%s depth-charging %s %s in %s\n",
1365                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1366         } else {
1367             wu(0, pp->pln_own,
1368                "\t%s pinbombing %s %s in %s\n",
1369                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1370         }
1371         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1372         if (plp->plane.pln_nuketype != -1)
1373             hitchance = 100;
1374         else if (hardtarget != SECT_HARDTARGET)
1375             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1376         /* Always calculate damage */
1377         if (roll(100) <= hitchance) {
1378             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1379             if (nukedam) {
1380                 if (mission == MI_INTERDICT) {
1381                     wu(0, pp->pln_own,
1382                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1383                        prplane(pp), nukedam, cname(victim), s);
1384                     dam += nukedam;
1385                 }
1386             } else {
1387                 wu(0, pp->pln_own,
1388                    "\t\thit %s %s for %d damage\n",
1389                    cname(victim), s, newdam);
1390                 dam += newdam;
1391             }
1392         } else {
1393             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1394             wu(0, pp->pln_own, "missed\n");
1395             if (mission == MI_SINTERDICT) {
1396                 mpr(victim,
1397                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1398                     xyas(x, y, victim));
1399             } else if (*s == 's') {
1400                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1401                     s, xyas(x, y, victim));
1402             } else {
1403                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1404                     s, xyas(x, y, victim));
1405             }
1406             /* Now, even though we missed, the bombs
1407                land somewhere. */
1408             collateral_damage(x, y, newdam, bombers);
1409         }
1410
1411         /* use up missiles */
1412         if (plp->pcp->pl_flags & P_M)
1413             pp->pln_effic = 0;
1414     }
1415
1416     return dam;
1417 }
1418
1419 /*
1420  * Check to see if anyone hostile to the victim
1421  * is running an air defense mission on this
1422  * sector. If so, do air combat
1423  */
1424 int
1425 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1426             struct emp_qelem *esc_list)
1427 {
1428     int dam = 0, cn;
1429     int mission_flags, tech, combat = 0, rel, dist, z;
1430     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1431     struct plist *plp;
1432     struct genlist *glp;
1433     struct empobj *gp;
1434     struct genlist mi[MAXNOC];
1435     char buf[512];
1436     char *path;
1437     int count;
1438     int tcount;
1439
1440     count = 0;
1441     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1442         count++;
1443     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1444         count++;
1445
1446     memset(mi, 0, sizeof(mi));
1447     for (z = 1; z < MAXNOC; z++)
1448         emp_initque((struct emp_qelem *)&mi[z]);
1449
1450     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1451
1452     for (cn = 1; cn < MAXNOC; cn++) {
1453         /* Check our relations */
1454         rel = getrel(getnatp(cn), victim);
1455
1456         if (rel > HOSTILE)
1457             continue;
1458
1459         if (QEMPTY(&mi[cn].queue))
1460             continue;
1461
1462         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1463          * list from the mission creation.  This list must be deleted later. */
1464         emp_initque(&interceptors);
1465         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1466             next = qp->q_forw;
1467             glp = (struct genlist *)qp;
1468             gp = glp->thing;
1469
1470             dist = mapdist(x, y, gp->x, gp->y);
1471
1472             plp = malloc(sizeof(struct plist));
1473             memset(plp, 0, sizeof(struct plist));
1474             plp->pcp = glp->cp;
1475             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1476
1477             /* missiles go one way, so we can use all the range */
1478             if (!(plp->pcp->pl_flags & P_M))
1479                 dist *= 2;
1480             /* If it's out of range, free it and continue on */
1481             if (dist > plp->plane.pln_range) {
1482                 free(plp);
1483                 continue;
1484             }
1485             emp_insque(&plp->queue, &interceptors);
1486         }
1487
1488         /* Remove those who cannot go */
1489         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1490
1491         if (QEMPTY(&interceptors))
1492             continue;
1493
1494         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1495          * that the higher numbered planes go into battle (they should be the better ones
1496          * at fighting, if all went well.) */
1497         tcount = 0;
1498         for (qp = interceptors.q_forw; qp != (&interceptors);
1499              qp = qp->q_forw)
1500             tcount++;
1501         tcount -= count * 2;
1502         /* Just in case there are more incoming than we have */
1503         if (tcount < 0)
1504             tcount = 0;
1505         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1506             next = qp->q_forw;
1507             if (tcount) {
1508                 tcount--;
1509                 /* Free it up and continue */
1510                 emp_remque(qp);
1511                 glp = (struct genlist *)qp;
1512                 free(glp);
1513             }
1514         }
1515
1516         /* Now, make a list of all the airports these planes are coming from */
1517         emp_initque(&airp);
1518         for (qp = interceptors.q_forw; qp != (&interceptors);
1519              qp = qp->q_forw) {
1520             plp = (struct plist *)qp;
1521             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1522                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1523         }
1524
1525         /* Now, fly them out one airport at a time */
1526         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1527             struct airport *air;
1528
1529             air = (struct airport *)qp;
1530             dist = mapdist(x, y, air->x, air->y);
1531
1532             emp_initque(&i);
1533
1534             /* Split off the interceptors at this base into i */
1535             divide(&interceptors, &i, air->x, air->y);
1536
1537             tech = 0;
1538             mission_flags = 0;
1539             mission_flags |= P_X;       /* stealth (shhh) */
1540             /* gets turned off if not all choppers */
1541             mission_flags |= P_H;
1542             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1543             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1544
1545             /* Did we run out of interceptors? */
1546             if (QEMPTY(&i))
1547                 continue;
1548             /* Did we run out of bombers? */
1549             if (QEMPTY(bomb_list)) {
1550                 /* Yes, so we have to put the rest of the interceptors back, and
1551                    then continue, or we leak memory */
1552                 pln_put(&i);
1553                 continue;
1554             }
1555             mission_flags =
1556                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1557                                 mission_flags, &tech);
1558
1559             /* Did we run out of interceptors? */
1560             if (QEMPTY(&i))
1561                 continue;
1562             /* Did we run out of bombers? */
1563             if (QEMPTY(bomb_list)) {
1564                 /* Yes, so we have to put the rest of the interceptors back, and
1565                    then continue, or we leak memory */
1566                 pln_put(&i);
1567                 continue;
1568             }
1569
1570             path = BestAirPath(buf, air->x, air->y, x, y);
1571             if (CANT_HAPPEN(!path))
1572                 continue;
1573             wu(0, cn, "Flying %s mission from %s\n",
1574                mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1575             if (air->own && (air->own != cn)) {
1576                 wu(0, air->own, "%s is flying %s mission from %s\n",
1577                    cname(cn), mission_name(MI_AIR_DEFENSE),
1578                    xyas(air->x, air->y, air->own));
1579             }
1580
1581             /* Now, fly the planes to the sector */
1582             emp_initque(&empty);
1583             ac_encounter(&i, &empty, air->x, air->y,
1584                          path, mission_flags, 1, bomb_list, esc_list);
1585
1586             /* If none made it, continue */
1587             if (QEMPTY(&i))
1588                 continue;
1589
1590             /* Some made it, so now they get to try to fight. */
1591             /* Intercept the escorts first */
1592             combat = 0;
1593             if (!QEMPTY(esc_list)) {
1594                 mpr(victim, "%s air defense planes intercept!\n",
1595                     cname(cn));
1596                 ac_combat_headers(victim, cn);
1597                 ac_airtoair(esc_list, &i);
1598                 combat = 1;
1599             }
1600             /* Now intercept the bombers */
1601             if (!QEMPTY(bomb_list)) {
1602                 if (!combat) {
1603                     mpr(victim, "%s air defense planes intercept!\n",
1604                         cname(cn));
1605                     ac_combat_headers(victim, cn);
1606                 }
1607                 ac_airtoair(bomb_list, &i);
1608                 PR(cn, "\n");
1609                 PR(victim, "\n");
1610             }
1611
1612             pln_put(&i);
1613         }
1614     }
1615
1616     /* We have to free all of these, if they are still there, otherwise they get
1617        lost and we leak memory all over the place. */
1618     for (cn = 1; cn < MAXNOC; cn++) {
1619         /* free up all this memory if it's still there */
1620         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1621             next = qp->q_forw;
1622             glp = (struct genlist *)qp;
1623             free(glp->thing);
1624             free(glp);
1625         }
1626     }
1627
1628     return dam;
1629 }