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