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