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