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