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