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