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