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