]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
(mission_short_name): Remove mission_short_name, not used.
[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 = 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, &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 = 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 = 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, &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 = 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 < 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, 0, 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 = 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(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(glp);
783     }
784
785     qp = escorts.q_forw;
786     while (qp != (&escorts)) {
787         newqp = qp->q_forw;
788         emp_remque(qp);
789         free(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(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_name(short int mission)
853 {
854     switch (mission) {
855     case MI_INTERDICT:
856         return "an interdiction";
857     case MI_SUPPORT:
858         return "a support";
859     case MI_OSUPPORT:
860         return "a offensive support";
861     case MI_DSUPPORT:
862         return "a defensive support";
863     case MI_RESERVE:
864         return "a reserve";
865     case MI_ESCORT:
866         return "an escort";
867     case MI_SINTERDICT:
868         return "a sub interdiction";
869     case MI_AIR_DEFENSE:
870         return "an air defense";
871     }
872     return "a mysterious";
873 }
874
875 void
876 show_mission(int type, struct nstr_item *np)
877 {
878     int size, first = 1, radius;
879     s_char *block;
880     struct genitem *gp;
881
882     size = max(sizeof(struct lndstr), sizeof(struct plnstr));
883     size = max(size, sizeof(struct shpstr));
884     block = malloc(size);
885
886     while (nxtitem(np, block)) {
887         gp = (struct genitem *)block;
888         if (!player->owner || gp->own == 0)
889             continue;
890
891         if (first) {
892             pr("Thing                         x,y   op-sect rad mission\n");
893             first = 0;
894         }
895         pr("%-25s", nameofitem(gp, type));
896         pr(" %7s", xyas(gp->x, gp->y, player->cnum));
897         if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
898             gp->mission == MI_OSUPPORT ||
899             gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
900             radius = 999;
901             oprange(gp, type, &radius);
902             pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
903             if (radius < gp->radius)
904                 pr("  %4d", radius);
905             else
906                 pr("  %4d", gp->radius);
907         } else if (gp->mission == MI_RESERVE) {
908             struct sctstr sect;
909             int plus = 2;
910
911             getsect(gp->x, gp->y, &sect);
912             if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
913                 plus++;
914
915             if (((struct lndstr *)block)->lnd_rad_max == 0)
916                 plus = 0;
917             else
918                 plus += ((struct lndstr *)block)->lnd_rad_max;
919             pr(" %7s", xyas(gp->x, gp->y, player->cnum));
920             pr("  %4d", plus);
921         } else if (gp->mission == MI_ESCORT) {
922             pr("        ");
923             pr("  %4d", (int)
924                ((float)((struct plnstr *)block)->pln_range / 2.0)
925                 );
926         } else
927             pr("              ");
928         if (gp->mission)
929             pr(" is on %s mission\n", mission_name(gp->mission));
930         else
931             pr(" has no mission.\n");
932     }
933 }
934
935 int
936 oprange(struct genitem *gp, int type, int *radius)
937 {
938     int range;
939     struct shpstr ship;
940     struct lndstr land;
941     struct plnstr plane;
942
943     switch (type) {
944     case EF_SHIP:
945         getship(gp->uid, &ship);
946         range = ldround(techfact(gp->tech,
947                                  (double)ship.shp_frnge / 2.0), 1);
948         break;
949     case EF_LAND:
950         getland(gp->uid, &land);
951         range = ldround(techfact((int)land.lnd_tech,
952                                  (double)land.lnd_frg / 2.0), 1);
953         break;
954     case EF_PLANE:
955         getplane(gp->uid, &plane);
956         /* missiles go one way, so we can use all the range */
957         if (plchr[(int)plane.pln_type].pl_flags & P_M)
958             range = plane.pln_range;
959         else
960             range = ldround((double)plane.pln_range / 2.0, 1);;
961         break;
962     }
963
964     if ((*radius) > range)
965         *radius = range;
966
967     return range;
968 }
969
970 /*
971  *  Remove all planes who cannot go on
972  *  the mission from the plane list.
973  */
974 static void
975 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
976                 int hardtarget)
977 {
978     struct emp_qelem *qp, *next;
979     struct plnstr *pp;
980     struct shpstr ship;
981     struct lndstr land;
982     struct sctstr sect;
983     struct plchrstr *pcp;
984     struct plist *plp;
985     int y, bad, bad1;
986     unsigned int x;
987
988     for (qp = list->q_forw; qp != list; qp = next) {
989         next = qp->q_forw;
990         plp = (struct plist *)qp;
991         pp = &plp->plane;
992         pcp = plp->pcp;
993
994         if (pp->pln_effic < 40) {
995             emp_remque(qp);
996             free(qp);
997             continue;
998         }
999
1000         if (pp->pln_mobil < 1) {
1001             emp_remque(qp);
1002             free(qp);
1003             continue;
1004         }
1005
1006         if (opt_MARKET) {
1007             if (ontradingblock(EF_PLANE, (int *)pp)) {
1008                 emp_remque(qp);
1009                 free(qp);
1010                 continue;
1011             }
1012         }
1013
1014         bad = 0;
1015         bad1 = 0;
1016         if (wantflags) {
1017             for (x = 0; x < sizeof(wantflags) * 8; x++) {
1018                 y = (1 << x);
1019                 if ((wantflags & y) == y)
1020                     if ((pcp->pl_flags & y) != y) {
1021                         switch (y) {
1022                         case P_F:
1023                         case P_ESC:
1024                             bad1 = 2;
1025                             break;
1026                         case P_E:
1027                         case P_L:
1028                         case P_K:
1029                             bad1 = 1;
1030                             break;
1031                         default:
1032                             bad = 1;
1033                         }
1034                     }
1035             }
1036             if (bad) {
1037                 emp_remque(qp);
1038                 free(qp);
1039                 continue;
1040             }
1041             if (bad1 == 2) {
1042                 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1043                     bad1 = 0;
1044             }
1045             if (bad1 == 1) {
1046                 if ((pcp->pl_flags & P_E) ||
1047                     (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1048                     bad1 = 0;
1049             }
1050             if (bad1) {
1051                 emp_remque(qp);
1052                 free(qp);
1053                 continue;
1054             }
1055         }
1056         bad = 0;
1057         bad1 = 0;
1058         if (nowantflags) {
1059             for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1060                 y = (1 << x);
1061                 if ((nowantflags & y) == y)
1062                     if ((pcp->pl_flags & y) == y)
1063                         bad = 1;
1064             }
1065             if (bad) {
1066                 emp_remque(qp);
1067                 free(qp);
1068                 continue;
1069             }
1070         }
1071         if (pp->pln_ship >= 0) {
1072             if (!getship(pp->pln_ship, &ship)) {
1073               shipsunk:
1074                 pp->pln_effic = 0;
1075                 putplane(pp->pln_uid, pp);
1076                 emp_remque(qp);
1077                 free(qp);
1078                 continue;
1079             }
1080             if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1081                 goto shipsunk;
1082             }
1083             if (ship.shp_effic < SHIP_MINEFF) {
1084                 goto shipsunk;
1085             }
1086             /* Can't fly off of inefficient or non-owned, non-allied ships */
1087             if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1088                 ((ship.shp_own != pp->pln_own) &&
1089                  (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1090                 emp_remque(qp);
1091                 free(qp);
1092                 continue;
1093             }
1094         }
1095         if (pp->pln_land >= 0) {
1096             if (!getland(pp->pln_land, &land)) {
1097               landdead:
1098                 pp->pln_effic = 0;
1099                 putplane(pp->pln_uid, pp);
1100                 emp_remque(qp);
1101                 free(qp);
1102                 continue;
1103             }
1104             if (!(pcp->pl_flags & P_E))
1105                 goto landdead;
1106             if (land.lnd_effic < LAND_MINEFF)
1107                 goto landdead;
1108
1109             /* Can't fly off of inefficient or non-owned, non-allied units */
1110             if ((land.lnd_effic < LND_AIROPS_EFF) ||
1111                 ((land.lnd_own != pp->pln_own) &&
1112                  (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1113                 emp_remque(qp);
1114                 free(qp);
1115                 continue;
1116             }
1117
1118             /* Can't fly off units in ships or other units */
1119             if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1120                 emp_remque(qp);
1121                 free(qp);
1122                 continue;
1123             }
1124         }
1125         /* Now, check the sector status if not on a plane or unit */
1126         if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1127             /* If we can't get the sector, we can't check it, and can't fly */
1128             if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
1129                 emp_remque(qp);
1130                 free(qp);
1131                 continue;
1132             }
1133             /* First, check allied status */
1134             /* Can't fly from non-owned sectors or non-allied sectors */
1135             if ((sect.sct_own != pp->pln_own) &&
1136                 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1137                 emp_remque(qp);
1138                 free(qp);
1139                 continue;
1140             }
1141             /* non-vtol plane */
1142             if ((pcp->pl_flags & P_V) == 0) {
1143                 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1144                     emp_remque(qp);
1145                     free(qp);
1146                     continue;
1147                 }
1148             }
1149         }
1150         if (pcp->pl_flags & P_A) {
1151             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1152                 emp_remque(qp);
1153                 free(qp);
1154                 continue;
1155             }
1156         }
1157
1158         putplane(pp->pln_uid, pp);
1159     }
1160 }
1161
1162 /*
1163  * Arm only the planes at x,y
1164  *
1165  */
1166 static int
1167 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1168                 int mission, struct ichrstr *ip, int flags,
1169                 int mission_flags, int *tech)
1170 {
1171     struct emp_qelem *qp;
1172     struct emp_qelem *next;
1173     struct plist *plp;
1174
1175     if (*tech == 0)
1176         *tech = 9999;
1177     for (qp = list->q_forw; qp != list; qp = next) {
1178         next = qp->q_forw;
1179         plp = (struct plist *)qp;
1180
1181         if (plp->plane.pln_x != x)
1182             continue;
1183         if (plp->plane.pln_y != y)
1184             continue;
1185
1186         if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1187             emp_remque(qp);
1188             free(qp);
1189             continue;
1190         }
1191         if (flags & (P_S | P_I)) {
1192             if (plp->pcp->pl_flags & P_S)
1193                 mission_flags |= P_S;
1194             if (plp->pcp->pl_flags & P_I)
1195                 mission_flags |= P_I;
1196         }
1197         if (*tech > plp->plane.pln_tech)
1198             *tech = plp->plane.pln_tech;
1199         if (!(plp->pcp->pl_flags & P_H))
1200             /* no stealth on this mission */
1201             mission_flags &= ~P_H;
1202         if (!(plp->pcp->pl_flags & P_X))
1203             /* no stealth on this mission */
1204             mission_flags &= ~P_X;
1205         if (!(plp->pcp->pl_flags & P_A)) {
1206             /* no asw on this mission */
1207             mission_flags &= ~P_A;
1208         }
1209         if (!(plp->pcp->pl_flags & P_MINE)) {
1210             /* no asw on this mission */
1211             mission_flags &= ~P_MINE;
1212         }
1213
1214         /*
1215          *      Mob costs for missions are 1/2 normal
1216          *       Not anymore. :)
1217          */
1218 /*      plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1219         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1220
1221     }
1222     return mission_flags;
1223 }
1224
1225 int
1226 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1227                   s_char mission)
1228 {
1229     struct plchrstr *pcp;
1230     struct plnstr *pp;
1231     int needed;
1232     struct lndstr land;
1233     struct shpstr ship;
1234     struct sctstr sect;
1235     i_type itype;
1236     int rval;
1237     short *item;
1238
1239     pp = &plp->plane;
1240     pcp = plp->pcp;
1241     if (pp->pln_ship >= 0) {
1242         getship(pp->pln_ship, &ship);
1243         item = ship.shp_item;
1244     } else if (pp->pln_land >= 0) {
1245         getland(pp->pln_land, &land);
1246         item = land.lnd_item;
1247     } else {
1248         getsect(pp->pln_x, pp->pln_y, &sect);
1249         item = sect.sct_item;
1250     }
1251     if (pcp->pl_fuel > item[I_PETROL]) {
1252         return -1;
1253     }
1254     item[I_PETROL] -= pcp->pl_fuel;
1255     rval = 0;
1256     if (!(flags & P_F)) {
1257         itype = I_NONE;
1258         needed = 0;
1259         switch (mission) {
1260         case 's':
1261         case 'p':
1262             if (pp->pln_nuketype == -1) {
1263                 itype = I_SHELL;
1264                 needed = pp->pln_load;
1265             }
1266             break;
1267         case 't':
1268             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1269                 break;
1270             itype = ip->i_vtype;
1271             needed = (pp->pln_load * 2) / ip->i_lbs;
1272             break;
1273         case 'd':
1274             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1275                 break;
1276             itype = ip->i_vtype;
1277             needed = (pp->pln_load * 2) / ip->i_lbs;
1278             break;
1279         case 'a':
1280             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1281                 break;
1282             itype = I_MILIT;
1283             needed = pp->pln_load / ip->i_lbs;
1284             break;
1285         case 'n':
1286             if (pp->pln_nuketype == -1)
1287                 rval = -1;
1288             break;
1289         case 'i':               /* missile interception */
1290             if (pp->pln_load) {
1291                 itype = I_SHELL;
1292                 needed = pp->pln_load;
1293             }
1294             break;
1295         default:
1296             break;
1297         }
1298         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1299             return -1;
1300         }
1301         if (itype != I_NONE) {
1302             if (itype == I_SHELL && item[itype] < needed)
1303                 item[itype] += supply_commod(plp->plane.pln_own,
1304                                              plp->plane.pln_x,
1305                                              plp->plane.pln_y,
1306                                              I_SHELL, needed);
1307             if (item[itype] < needed)
1308                 return -1;
1309             item[itype] -= needed;
1310         }
1311         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1312             plp->bombs = needed;
1313         else
1314             plp->misc = needed;
1315     }
1316     if (pp->pln_ship >= 0)
1317         putship(ship.shp_uid, &ship);
1318     else if (pp->pln_land >= 0)
1319         putland(land.lnd_uid, &land);
1320     else
1321         putsect(&sect);
1322     return rval;
1323 }
1324
1325 /*
1326  *  Return 1 if this x,y pair is in the list
1327  */
1328 static int
1329 find_airport(struct emp_qelem *airp, coord x, coord y)
1330 {
1331     struct emp_qelem *qp;
1332     struct airport *a;
1333
1334     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1335         a = (struct airport *)qp;
1336         if ((a->x == x) && (a->y == y))
1337             return 1;
1338     }
1339
1340     return 0;
1341 }
1342
1343 /* #*# This needs to be changed to include acc's -KHS */
1344 static void
1345 add_airport(struct emp_qelem *airp, coord x, coord y)
1346 {
1347     struct airport *a;
1348     struct sctstr sect;
1349
1350     a = malloc(sizeof(struct airport));
1351
1352     a->x = x;
1353     a->y = y;
1354     getsect(x, y, &sect);
1355     a->own = sect.sct_own;
1356
1357     emp_insque((struct emp_qelem *)a, airp);
1358 }
1359
1360 /*
1361  *  Take all the planes in list 1 that
1362  *  are at x,y, and put them into list 2.
1363  */
1364 static void
1365 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1366 {
1367     struct emp_qelem *qp, *next;
1368     struct plist *plp;
1369
1370     for (qp = l1->q_forw; qp != l1; qp = next) {
1371         next = qp->q_forw;
1372         plp = (struct plist *)qp;
1373
1374         if (plp->plane.pln_x != x)
1375             continue;
1376         if (plp->plane.pln_y != y)
1377             continue;
1378
1379         emp_remque(qp);
1380         emp_insque(qp, l2);
1381     }
1382 }
1383
1384 static int
1385 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1386            natid victim, s_char *s, int hardtarget)
1387 {
1388     struct emp_qelem *qp;
1389     struct plist *plp;
1390     struct plnstr *pp;
1391     int newdam, dam = 0;
1392     int hitchance;
1393     int nukedam;
1394
1395     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1396         plp = (struct plist *)qp;
1397         pp = &plp->plane;
1398
1399         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1400             continue;
1401
1402         if (!plp->bombs)
1403             continue;
1404
1405         newdam = 0;
1406         if (plp->pcp->pl_flags & P_A) {
1407             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1408                 wu(0, pp->pln_own,
1409                    "\t%s detects sub movement in %s\n",
1410                    prplane(pp), xyas(x, y, pp->pln_own));
1411                 continue;
1412             }
1413             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1414                 wu(0, pp->pln_own,
1415                    "\t%s tracks %s %s at %s\n",
1416                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1417                 continue;
1418             }
1419             wu(0, pp->pln_own,
1420                "\t%s depth-charging %s %s in %s\n",
1421                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1422         } else {
1423             wu(0, pp->pln_own,
1424                "\t%s pinbombing %s %s in %s\n",
1425                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1426         }
1427         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1428         if (plp->plane.pln_nuketype != -1)
1429             hitchance = 100;
1430         else if (hardtarget != SECT_HARDTARGET)
1431             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1432         /* Always calculate damage */
1433         if (roll(100) <= hitchance) {
1434             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1435             if (nukedam) {
1436                 if (mission == MI_INTERDICT) {
1437                     wu(0, pp->pln_own,
1438                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1439                        prplane(pp), nukedam, cname(victim), s);
1440                     dam += nukedam;
1441                 }
1442             } else {
1443                 wu(0, pp->pln_own,
1444                    "\t\thit %s %s for %d damage\n",
1445                    cname(victim), s, newdam);
1446                 dam += newdam;
1447             }
1448         } else {
1449             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1450             wu(0, pp->pln_own, "missed\n");
1451             if (mission == MI_SINTERDICT) {
1452                 mpr(victim,
1453                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1454                     xyas(x, y, victim));
1455             } else if (*s == 's') {
1456                 mpr(victim,
1457                     "SPLASH!  Bombs miss your %s in %s\n",
1458                     s, xyas(x, y, victim));
1459             } else {
1460                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1461                     s, xyas(x, y, victim));
1462             }
1463             /* Now, even though we missed, the bombs
1464                land somewhere. */
1465             collateral_damage(x, y, newdam, bombers);
1466         }
1467
1468         /* use up missiles */
1469         if (plp->pcp->pl_flags & P_M) {
1470             makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1471                      pp->pln_y);
1472             pp->pln_own = 0;
1473         }
1474     }
1475
1476     return dam;
1477 }
1478
1479 /*
1480  * Check to see if anyone hostile to the victim
1481  * is running an air defense mission on this
1482  * sector. If so, do air combat
1483  */
1484 int
1485 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1486             struct emp_qelem *esc_list)
1487 {
1488     int dam = 0, cn;
1489     int mission_flags, tech, combat = 0, rel, dist, z;
1490     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1491     struct plist *plp;
1492     struct genlist *glp;
1493     struct genitem *gp;
1494     struct genlist mi[MAXNOC];
1495     s_char path[512];
1496     int count;
1497     int tcount;
1498
1499     count = 0;
1500     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1501         count++;
1502     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1503         count++;
1504
1505     memset(mi, 0, sizeof(mi));
1506     for (z = 1; z < MAXNOC; z++)
1507         emp_initque((struct emp_qelem *)&mi[z]);
1508
1509     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1510
1511     for (cn = 1; cn < MAXNOC; cn++) {
1512         /* Check our relations */
1513         rel = getrel(getnatp(cn), victim);
1514
1515         if (rel > HOSTILE)
1516             continue;
1517
1518         if (QEMPTY(&mi[cn].queue))
1519             continue;
1520
1521         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1522          * list from the mission creation.  This list must be deleted later. */
1523         emp_initque(&interceptors);
1524         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1525             next = qp->q_forw;
1526
1527             glp = (struct genlist *)qp;
1528             gp = (struct genitem *)glp->thing;
1529             plp = (struct plist *)qp;
1530
1531             dist = mapdist(x, y, gp->x, gp->y);
1532
1533             plp = malloc(sizeof(struct plist));
1534             memset(plp, 0, sizeof(struct plist));
1535             plp->pcp = (struct plchrstr *)glp->cp;
1536             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1537
1538             /* missiles go one way, so we can use all the range */
1539             if (!(plp->pcp->pl_flags & P_M))
1540                 dist *= 2;
1541             /* If it's out of range, free it and continue on */
1542             if (dist > plp->plane.pln_range) {
1543                 free(plp);
1544                 continue;
1545             }
1546             emp_insque(&plp->queue, &interceptors);
1547         }
1548
1549         /* Remove those who cannot go */
1550         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1551
1552         if (QEMPTY(&interceptors))
1553             continue;
1554
1555         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1556          * that the higher numbered planes go into battle (they should be the better ones
1557          * at fighting, if all went well.) */
1558         tcount = 0;
1559         for (qp = interceptors.q_forw; qp != (&interceptors);
1560              qp = qp->q_forw)
1561             tcount++;
1562         tcount -= (count * 2);
1563         /* Just in case there are more incoming than we have */
1564         if (tcount < 0)
1565             tcount = 0;
1566         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1567             next = qp->q_forw;
1568             if (tcount) {
1569                 tcount--;
1570                 /* Free it up and continue */
1571                 emp_remque(qp);
1572                 glp = (struct genlist *)qp;
1573                 free(glp);
1574             }
1575         }
1576
1577         /* Now, make a list of all the airports these planes are coming from */
1578         emp_initque(&airp);
1579         for (qp = interceptors.q_forw; qp != (&interceptors);
1580              qp = qp->q_forw) {
1581             plp = (struct plist *)qp;
1582             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1583                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1584         }
1585
1586         /* Now, fly them out one airport at a time */
1587         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1588             struct airport *air;
1589
1590             air = (struct airport *)qp;
1591             dist = mapdist(x, y, air->x, air->y);
1592
1593             emp_initque(&i);
1594
1595             /* Split off the interceptors at this base into i */
1596             divide(&interceptors, &i, air->x, air->y);
1597
1598             tech = 0;
1599             mission_flags = 0;
1600             mission_flags |= P_X;       /* stealth (shhh) */
1601             /* gets turned off if not all choppers */
1602             mission_flags |= P_H;
1603             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1604             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1605
1606             /* Did we run out of interceptors? */
1607             if (QEMPTY(&i))
1608                 continue;
1609             /* Did we run out of bombers? */
1610             if (QEMPTY(bomb_list)) {
1611                 /* Yes, so we have to put the rest of the interceptors back, and
1612                    then continue, or we leak memory */
1613                 pln_put(&i);
1614                 continue;
1615             }
1616             mission_flags =
1617                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1618                                 mission_flags, &tech);
1619
1620             /* Did we run out of interceptors? */
1621             if (QEMPTY(&i))
1622                 continue;
1623             /* Did we run out of bombers? */
1624             if (QEMPTY(bomb_list)) {
1625                 /* Yes, so we have to put the rest of the interceptors back, and
1626                    then continue, or we leak memory */
1627                 pln_put(&i);
1628                 continue;
1629             }
1630
1631             BestAirPath(path, air->x, air->y, x, y);
1632             wu(0, cn, "Flying %s mission from %s\n",
1633                mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1634             if (air->own && (air->own != cn)) {
1635                 wu(0, air->own, "%s is flying %s mission from %s\n",
1636                    cname(cn), mission_name(MI_AIR_DEFENSE),
1637                    xyas(air->x, air->y, air->own));
1638             }
1639
1640             /* Now, fly the planes to the sector */
1641             emp_initque(&empty);
1642             ac_encounter(&i, &empty, air->x, air->y,
1643                          path, mission_flags, 1, bomb_list, esc_list);
1644
1645             /* If none made it, continue */
1646             if (QEMPTY(&i))
1647                 continue;
1648
1649             /* Some made it, so now they get to try to fight. */
1650             /* Intercept the escorts first */
1651             combat = 0;
1652             if (!QEMPTY(esc_list)) {
1653                 mpr(victim, "%s air defense planes intercept!\n",
1654                     cname(cn));
1655                 ac_combat_headers(victim, cn);
1656                 ac_airtoair(esc_list, &i);
1657                 combat = 1;
1658             }
1659             /* Now intercept the bombers */
1660             if (!QEMPTY(bomb_list)) {
1661                 if (!combat) {
1662                     mpr(victim, "%s air defense planes intercept!\n",
1663                         cname(cn));
1664                     ac_combat_headers(victim, cn);
1665                 }
1666                 ac_airtoair(bomb_list, &i);
1667                 PR(cn, "\n");
1668                 PR(victim, "\n");
1669             }
1670
1671             pln_put(&i);
1672         }
1673     }
1674
1675     /* We have to free all of these, if they are still there, otherwise they get
1676        lost and we leak memory all over the place. */
1677     for (cn = 1; cn < MAXNOC; cn++) {
1678         /* free up all this memory if it's still there */
1679         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1680             next = qp->q_forw;
1681             glp = (struct genlist *)qp;
1682             free(glp->thing);
1683             free(glp);
1684         }
1685     }
1686
1687     return dam;
1688 }