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