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