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