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