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