]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
Don't reduce mission op area when range shrinks
[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         if (dist > gp->radius)
325             continue;
326
327         /* Ok, it is within the operations range. */
328         /* Now check from where the object actually is */
329         dist = mapdist(x, y, gp->x, gp->y);
330         radius = oprange(gp);
331         if (dist > radius)
332             continue;
333         /* Ok, the object can get to where the x,y is */
334
335         if (opt_SLOW_WAR) {
336             if (mission != MI_AIR_DEFENSE) {
337                 getsect(x, y, &sect);
338                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
339
340                     /*
341                      * If the owner of the unit isn't at war
342                      * with the victim, and doesn't own the
343                      * sect being acted upon, and isn't the
344                      * old owner of that sect, bounce them.
345                      */
346                     if (sect.sct_type != SCT_WATER &&
347                         sect.sct_own != gp->own &&
348                         sect.sct_oldown != gp->own)
349                         continue;
350                 }
351             }
352         }
353
354         glp = malloc(sizeof(struct genlist));
355         memset(glp, 0, sizeof(struct genlist));
356         glp->cp = get_empobj_chr(gp);
357         glp->thing = malloc(sizeof(item));
358         memcpy(glp->thing, &item, sizeof(item));
359         emp_insque(&glp->queue, &mi[gp->own].queue);
360     }
361 }
362
363 static void
364 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
365 {
366     struct nstr_item ni;
367     struct plist *plp;
368     struct plnstr plane;
369     int dist;
370
371     snxtitem_all(&ni, EF_PLANE);
372     while (nxtitem(&ni, &plane)) {
373         if (plane.pln_own != cn)
374             continue;
375
376         if (plane.pln_mission != MI_ESCORT)
377             continue;
378
379         dist = mapdist(x, y, plane.pln_x, plane.pln_y);
380         if (dist > plane.pln_range / 2)
381             continue;
382
383         plp = malloc(sizeof(struct plist));
384         memset(plp, 0, sizeof(struct plist));
385         plp->pcp = &plchr[(int)plane.pln_type];
386         plp->plane = plane;
387         emp_insque(&plp->queue, escorts);
388     }
389 }
390
391 static int
392 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
393                 int mission, char *s, int hardtarget)
394 {
395     struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
396     struct emp_qelem *newqp;
397     struct genlist *glp;
398     struct plist *plp;
399     struct empobj *gp;
400     struct lndstr *lp;
401     struct shpstr *sp;
402     struct sctstr sect;
403     struct mchrstr *mcp;
404     struct plchrstr *pcp;
405     int dam = 0, dam2, mission_flags;
406     natid plane_owner = 0;
407     int md, range, air_dam = 0;
408     double hitchance, vrange;
409     int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
410
411     getsect(x, y, &sect);
412
413     emp_initque(&missiles);
414     emp_initque(&bombers);
415     emp_initque(&escorts);
416     emp_initque(&airp);
417
418     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
419         glp = (struct genlist *)qp;
420         gp = glp->thing;
421
422         md = mapdist(x, y, gp->x, gp->y);
423
424         if (glp->thing->ef_type == EF_LAND) {
425             lp = (struct lndstr *)glp->thing;
426
427             if (mission == MI_SINTERDICT)
428                 continue;
429
430             if ((mission == MI_INTERDICT) &&
431                 (md > land_max_interdiction_range))
432                 continue;
433
434             range = roundrange(lnd_fire_range(lp));
435             if (md > range)
436                 continue;
437
438             dam2 = lnd_fire(lp);
439             putland(lp->lnd_uid, lp);
440             if (dam2 < 0)
441                 continue;
442
443             if (targeting_ships) {
444                 if (chance(lnd_acc(lp) / 100.0))
445                     dam2 = ldround(dam2 / 2.0, 1);
446             }
447             dam += dam2;
448             if (targeting_ships)
449                 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
450             else
451                 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
452             wu(0, lp->lnd_own,
453                "%s fires at %s %s at %s\n",
454                prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
455
456             mpr(victim, "%s %s fires at you at %s\n",
457                 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
458         } else if (glp->thing->ef_type == EF_SHIP) {
459             sp = (struct shpstr *)glp->thing;
460             mcp = glp->cp;
461
462             if (((mission == MI_INTERDICT) ||
463                  (mission == MI_SINTERDICT)) &&
464                 (md > ship_max_interdiction_range))
465                 continue;
466             if (mission == MI_SINTERDICT) {
467                 if (!(mcp->m_flags & M_SONAR))
468                     continue;
469                 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
470                     continue;
471                 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
472                 vrange *= sp->shp_effic / 200.0;
473                 if (md > vrange)
474                     continue;
475                 /* can't look all the time */
476                 if (chance(0.5))
477                     continue;
478             }
479             if (mcp->m_flags & M_SUB) {
480                 if (!targeting_ships)
481                     continue;   /* subs interdict only ships */
482                 range = roundrange(torprange(sp));
483                 if (md > range)
484                     continue;
485                 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
486                     continue;
487                 dam2 = shp_torp(sp, 1);
488                 putship(sp->shp_uid, sp);
489                 if (dam2 < 0)
490                     continue;
491                 hitchance = shp_torp_hitchance(sp, md);
492
493                 wu(0, sp->shp_own,
494                    "%s locking on %s %s in %s\n",
495                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
496                 wu(0, sp->shp_own,
497                    "\tEffective torpedo range is %d.0\n", range);
498                 wu(0, sp->shp_own,
499                    "\tWhooosh... Hitchance = %d%%\n",
500                    (int)(hitchance * 100));
501
502                 if (!chance(hitchance)) {
503                     wu(0, sp->shp_own, "\tMissed\n");
504                     mpr(victim,
505                         "Incoming torpedo sighted @ %s missed (whew)!\n",
506                         xyas(x, y, victim));
507                     continue;
508                 }
509                 wu(0, sp->shp_own, "\tBOOM!...\n");
510                 dam += dam2;
511                 nreport(victim, N_TORP_SHIP, 0, 1);
512                 wu(0, sp->shp_own,
513                    "\tTorpedo hit %s %s for %d damage\n",
514                    cname(victim), s, dam2);
515
516                 mpr(victim,
517                     "Incoming torpedo sighted @ %s hits and does %d damage!\n",
518                     xyas(x, y, victim), dam2);
519             } else {
520                 range = roundrange(shp_fire_range(sp));
521                 if (md > range)
522                     continue;
523                 if (mission == MI_SINTERDICT)
524                     dam2 = shp_dchrg(sp);
525                 else
526                     dam2 = shp_fire(sp);
527                 putship(sp->shp_uid, sp);
528                 if (dam2 < 0)
529                     continue;
530                 dam += dam2;
531                 if (targeting_ships)
532                     nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
533                 else
534                     nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
535                 wu(0, sp->shp_own,
536                    "%s fires at %s %s at %s\n",
537                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
538
539                 mpr(victim, "%s %s fires at you at %s\n",
540                     cname(sp->shp_own), prship(sp), xyas(x, y, victim));
541             }
542         } else if (glp->thing->ef_type == EF_PLANE) {
543             pcp = glp->cp;
544             if (pcp->pl_flags & P_M)
545                 /* units have their own missile interdiction */
546                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
547                     continue;
548
549             /* save planes for later */
550             plp = malloc(sizeof(struct plist));
551
552             memset(plp, 0, sizeof(struct plist));
553             plp->pcp = pcp;
554             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
555             if (plp->pcp->pl_flags & P_M)
556                 emp_insque(&plp->queue, &missiles);
557             else
558                 emp_insque(&plp->queue, &bombers);
559             plane_owner = plp->plane.pln_own;
560         } else {
561             CANT_REACH();
562             break;
563         }
564     }
565     if (!QEMPTY(&missiles)) {
566         /* I arbitrarily chose 100 mindam -KHS */
567         dam +=
568             msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
569                               "sector", victim, mission);
570         qp = missiles.q_forw;
571         while (qp != (&missiles)) {
572             newqp = qp->q_forw;
573             emp_remque(qp);
574             free(qp);
575             qp = newqp;
576         }
577     }
578
579     if (QEMPTY(&bombers)) {
580         qp = list->q_forw;
581         while (qp != list) {
582             glp = (struct genlist *)qp;
583             qp = qp->q_forw;
584
585             free(glp->thing);
586             free(glp);
587         }
588         return dam;
589     }
590     /*
591      * If there are planes performing an
592      * interdict or support mission, find
593      * some escorts for them, if possible.
594      * Up to 2 per bomber, if possible.
595      */
596     find_escorts(x, y, plane_owner, &escorts);
597
598     if (mission == MI_SINTERDICT)
599         mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
600     else
601         mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
602
603     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
604
605     for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
606         plp = (struct plist *)qp;
607         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
608             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
609     }
610
611     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
612         struct airport *air;
613         char buf[512];
614         char *pp;
615
616         air = (struct airport *)qp;
617         md = mapdist(x, y, air->x, air->y);
618
619         emp_initque(&b);
620         emp_initque(&e);
621
622         /* Split off the bombers at this base into b */
623         divide(&bombers, &b, air->x, air->y);
624
625         /* Split off the escorts at this base into e */
626         divide(&escorts, &e, air->x, air->y);
627
628         mission_flags = 0;
629         mission_flags |= P_X;   /* stealth (shhh) */
630         mission_flags |= P_H;   /* gets turned off if not all choppers */
631
632         mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
633                                         0, mission_flags);
634
635         if (QEMPTY(&b))
636             continue;
637
638         mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
639                                         P_F | P_ESC, mission_flags);
640
641         pp = BestAirPath(buf, air->x, air->y, x, y);
642         if (CANT_HAPPEN(!pp))
643             continue;
644         wu(0, plane_owner, "Flying %s mission from %s to %s\n",
645            mission_name(mission),
646            xyas(air->x, air->y, plane_owner),
647            xyas(x, y, plane_owner));
648         if (air->own && (air->own != plane_owner)) {
649             wu(0, air->own, "%s is flying %s mission from %s to %s\n",
650                cname(plane_owner), mission_name(mission),
651                xyas(air->x, air->y, air->own),
652                xyas(x, y, air->own));
653         }
654
655         ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0);
656
657         if (!QEMPTY(&b))
658             air_dam +=
659                 air_damage(&b, x, y, mission, victim, s, hardtarget);
660
661         pln_put(&b);
662         pln_put(&e);
663     }
664
665     if (air_dam > 0) {
666         dam += air_dam;
667         if (targeting_ships)
668             nreport(plane_owner, N_SHP_BOMB, victim, 1);
669         else
670             nreport(plane_owner, N_SCT_BOMB, victim, 1);
671     }
672
673     /* free up all this memory */
674     qp = list->q_forw;
675     while (qp != list) {
676         glp = (struct genlist *)qp;
677         qp = qp->q_forw;
678
679         free(glp->thing);
680         free(glp);
681     }
682
683     qp = escorts.q_forw;
684     while (qp != (&escorts)) {
685         newqp = qp->q_forw;
686         emp_remque(qp);
687         free(qp);
688         qp = newqp;
689     }
690
691     qp = bombers.q_forw;
692     while (qp != (&bombers)) {
693         newqp = qp->q_forw;
694         emp_remque(qp);
695         free(qp);
696         qp = newqp;
697     }
698
699     return dam;
700 }
701
702 int
703 cando(int mission, int type)
704 {
705     switch (mission) {
706     case MI_ESCORT:
707         if (type == EF_PLANE)
708             return 1;
709         return 0;
710     case MI_AIR_DEFENSE:
711         if (type == EF_PLANE)
712             return 1;
713         return 0;
714     case MI_SINTERDICT:
715         if ((type == EF_PLANE) || (type == EF_SHIP))
716             return 1;
717         return 0;
718     case MI_INTERDICT:
719         return 1;
720     case MI_SUPPORT:
721     case MI_OSUPPORT:
722     case MI_DSUPPORT:
723         if (type == EF_PLANE)
724             return 1;
725         return 0;
726     case MI_RESERVE:
727         if (type == EF_LAND)
728             return 1;
729         return 0;
730     }
731
732     return 0;
733 }
734
735 char *
736 mission_name(short mission)
737 {
738     switch (mission) {
739     case MI_INTERDICT:
740         return "an interdiction";
741     case MI_SUPPORT:
742         return "a support";
743     case MI_OSUPPORT:
744         return "an offensive support";
745     case MI_DSUPPORT:
746         return "a defensive support";
747     case MI_RESERVE:
748         return "a reserve";
749     case MI_ESCORT:
750         return "an escort";
751     case MI_SINTERDICT:
752         return "a sub interdiction";
753     case MI_AIR_DEFENSE:
754         return "an air defense";
755     }
756     CANT_REACH();
757     return "a mysterious";
758 }
759
760 void
761 show_mission(int type, struct nstr_item *np)
762 {
763     int first = 1, radius;
764     union empobj_storage item;
765     struct empobj *gp;
766     struct sctstr sect;
767
768     while (nxtitem(np, &item)) {
769         gp = (struct empobj *)&item;
770         if (!player->owner || gp->own == 0)
771             continue;
772
773         if (first) {
774             pr("Thing                         x,y   op-sect rad mission\n");
775             first = 0;
776         }
777         pr("%-25s", obj_nameof(gp));
778         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
779         switch (gp->mission) {
780         case MI_INTERDICT:
781         case MI_SUPPORT:
782         case MI_AIR_DEFENSE:
783         case MI_DSUPPORT:
784         case MI_OSUPPORT:
785             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
786             pr("  %4d", gp->radius);
787             break;
788         case MI_RESERVE:
789             radius = item.land.lnd_rad_max;
790
791             if (radius) {
792                 getsect(gp->x, gp->y, &sect);
793                 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
794                     radius++;
795             }
796             prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
797             pr("  %4d", radius);
798             break;
799         case MI_ESCORT:
800             pr("        ");
801             pr("  %4d", item.plane.pln_range / 2);
802             break;
803         default:
804             CANT_REACH();
805             /* fall through */
806         case MI_NONE:
807             pr("              ");
808         }
809         if (gp->mission)
810             pr(" is on %s mission\n", mission_name(gp->mission));
811         else
812             pr(" has no mission.\n");
813     }
814 }
815
816 int
817 oprange(struct empobj *gp)
818 {
819     switch (gp->ef_type) {
820     case EF_SHIP:
821         return ldround(shp_fire_range((struct shpstr *)gp), 1);
822     case EF_LAND:
823         return ldround(lnd_fire_range((struct lndstr *)gp), 1);
824     case EF_PLANE:
825         /* missiles go one way, so we can use all the range */
826         if (plchr[(int)gp->type].pl_flags & P_M)
827             return ((struct plnstr *)gp)->pln_range;
828         return ((struct plnstr *)gp)->pln_range / 2;
829     }
830     CANT_REACH();
831     return -1;
832 }
833
834 /*
835  *  Remove all planes who cannot go on
836  *  the mission from the plane list.
837  */
838 static void
839 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
840                 int hardtarget)
841 {
842     struct emp_qelem *qp, *next;
843     struct plnstr *pp;
844     struct plchrstr *pcp;
845     struct plist *plp;
846
847     for (qp = list->q_forw; qp != list; qp = next) {
848         next = qp->q_forw;
849         plp = (struct plist *)qp;
850         pp = &plp->plane;
851         pcp = plp->pcp;
852
853         if (pp->pln_effic < 40) {
854             emp_remque(qp);
855             free(qp);
856             continue;
857         }
858
859         if (pp->pln_mobil < 1) {
860             emp_remque(qp);
861             free(qp);
862             continue;
863         }
864
865         if (opt_MARKET) {
866             if (ontradingblock(EF_PLANE, pp)) {
867                 emp_remque(qp);
868                 free(qp);
869                 continue;
870             }
871         }
872
873         if (!pln_capable(pp, wantflags, nowantflags)) {
874             emp_remque(qp);
875             free(qp);
876             continue;
877         }
878
879         if (!pln_airbase_ok(pp, 0, 0)) {
880             emp_remque(qp);
881             free(qp);
882             continue;
883         }
884
885         if (pcp->pl_flags & P_A) {
886             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
887                 emp_remque(qp);
888                 free(qp);
889                 continue;
890             }
891         }
892
893         putplane(pp->pln_uid, pp);
894     }
895 }
896
897 /*
898  * Arm only the planes at x,y
899  */
900 static int
901 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
902                 int mission, struct ichrstr *ip, int flags,
903                 int mission_flags)
904 {
905     struct emp_qelem *qp;
906     struct emp_qelem *next;
907     struct plist *plp;
908     struct plnstr *pp;
909
910     for (qp = list->q_forw; qp != list; qp = next) {
911         next = qp->q_forw;
912         plp = (struct plist *)qp;
913         pp = &plp->plane;
914
915         if (pp->pln_x != x)
916             continue;
917         if (pp->pln_y != y)
918             continue;
919
920         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
921             || mission_pln_equip(plp, ip, flags, mission) < 0) {
922             emp_remque(qp);
923             free(qp);
924             continue;
925         }
926         if (flags & (P_S | P_I)) {
927             if (plp->pcp->pl_flags & P_S)
928                 mission_flags |= P_S;
929             if (plp->pcp->pl_flags & P_I)
930                 mission_flags |= P_I;
931         }
932         if (!(plp->pcp->pl_flags & P_H))
933             /* no stealth on this mission */
934             mission_flags &= ~P_H;
935         if (!(plp->pcp->pl_flags & P_X))
936             /* no stealth on this mission */
937             mission_flags &= ~P_X;
938         if (!(plp->pcp->pl_flags & P_A)) {
939             /* no asw on this mission */
940             mission_flags &= ~P_A;
941         }
942         if (!(plp->pcp->pl_flags & P_MINE)) {
943             /* no asw on this mission */
944             mission_flags &= ~P_MINE;
945         }
946
947         pp->pln_flags |= PLN_LAUNCHED;
948         pp->pln_mobil -= pln_mobcost(dist, pp, flags);
949         putplane(pp->pln_uid, pp);
950     }
951     return mission_flags;
952 }
953
954 int
955 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
956                   char mission)
957 {
958     struct plchrstr *pcp;
959     struct plnstr *pp;
960     int load, needed;
961     struct lndstr land;
962     struct shpstr ship;
963     struct sctstr sect;
964     i_type itype;
965     short *item;
966
967     pp = &plp->plane;
968     pcp = plp->pcp;
969     if (pp->pln_ship >= 0) {
970         getship(pp->pln_ship, &ship);
971         item = ship.shp_item;
972     } else if (pp->pln_land >= 0) {
973         getland(pp->pln_land, &land);
974         item = land.lnd_item;
975     } else {
976         getsect(pp->pln_x, pp->pln_y, &sect);
977         item = sect.sct_item;
978     }
979     if (pcp->pl_fuel > item[I_PETROL]) {
980         return -1;
981     }
982     item[I_PETROL] -= pcp->pl_fuel;
983     if (!(flags & P_F)) {
984         load = pln_load(pp);
985         itype = I_NONE;
986         needed = 0;
987         switch (mission) {
988         case 's':               /* strategic bomb */
989         case 'p':               /* pinpoint bomb */
990             if (nuk_on_plane(pp) < 0) {
991                 itype = I_SHELL;
992                 needed = load;
993             }
994             break;
995         case 't':               /* transport */
996         case 'd':               /* drop */
997             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
998                 break;
999             itype = ip->i_uid;
1000             needed = (load * 2) / ip->i_lbs;
1001             break;
1002         case 'a':               /* paradrop */
1003             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1004                 break;
1005             itype = I_MILIT;
1006             needed = load / ip->i_lbs;
1007             break;
1008         case 'i':               /* missile interception */
1009             if (load) {
1010                 itype = I_SHELL;
1011                 needed = load;
1012             }
1013             break;
1014         case 'r':               /* reconnaissance */
1015         case 0:                 /* plane interception */
1016             break;
1017         default:
1018             CANT_REACH();
1019             break;
1020         }
1021         if (itype != I_NONE && needed <= 0)
1022             return -1;
1023         if (itype != I_NONE) {
1024             if (itype == I_SHELL && item[itype] < needed)
1025                 item[itype] += supply_commod(plp->plane.pln_own,
1026                                              plp->plane.pln_x,
1027                                              plp->plane.pln_y,
1028                                              I_SHELL, needed);
1029             if (item[itype] < needed)
1030                 return -1;
1031             item[itype] -= needed;
1032         }
1033         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1034             plp->bombs = needed;
1035         else
1036             plp->misc = needed;
1037     }
1038     if (pp->pln_ship >= 0)
1039         putship(ship.shp_uid, &ship);
1040     else if (pp->pln_land >= 0)
1041         putland(land.lnd_uid, &land);
1042     else
1043         putsect(&sect);
1044     return 0;
1045 }
1046
1047 /*
1048  *  Return 1 if this x,y pair is in the list
1049  */
1050 static int
1051 find_airport(struct emp_qelem *airp, coord x, coord y)
1052 {
1053     struct emp_qelem *qp;
1054     struct airport *a;
1055
1056     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1057         a = (struct airport *)qp;
1058         if ((a->x == x) && (a->y == y))
1059             return 1;
1060     }
1061
1062     return 0;
1063 }
1064
1065 /* #*# This needs to be changed to include acc's -KHS */
1066 static void
1067 add_airport(struct emp_qelem *airp, coord x, coord y)
1068 {
1069     struct airport *a;
1070     struct sctstr sect;
1071
1072     a = malloc(sizeof(struct airport));
1073
1074     a->x = x;
1075     a->y = y;
1076     getsect(x, y, &sect);
1077     a->own = sect.sct_own;
1078
1079     emp_insque((struct emp_qelem *)a, airp);
1080 }
1081
1082 /*
1083  *  Take all the planes in list 1 that
1084  *  are at x,y, and put them into list 2.
1085  */
1086 static void
1087 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1088 {
1089     struct emp_qelem *qp, *next;
1090     struct plist *plp;
1091
1092     for (qp = l1->q_forw; qp != l1; qp = next) {
1093         next = qp->q_forw;
1094         plp = (struct plist *)qp;
1095
1096         if (plp->plane.pln_x != x)
1097             continue;
1098         if (plp->plane.pln_y != y)
1099             continue;
1100
1101         emp_remque(qp);
1102         emp_insque(qp, l2);
1103     }
1104 }
1105
1106 static int
1107 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1108            natid victim, char *s, int hardtarget)
1109 {
1110     struct emp_qelem *qp;
1111     struct plist *plp;
1112     struct plnstr *pp;
1113     int newdam, dam = 0;
1114     int hitchance;
1115     int nukedam;
1116
1117     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1118         plp = (struct plist *)qp;
1119         pp = &plp->plane;
1120
1121         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1122             continue;
1123
1124         if (!plp->bombs)
1125             continue;
1126
1127         newdam = 0;
1128         if (plp->pcp->pl_flags & P_A) {
1129             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1130                 wu(0, pp->pln_own,
1131                    "\t%s detects sub movement in %s\n",
1132                    prplane(pp), xyas(x, y, pp->pln_own));
1133                 continue;
1134             }
1135             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1136                 wu(0, pp->pln_own,
1137                    "\t%s tracks %s %s at %s\n",
1138                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1139                 continue;
1140             }
1141             wu(0, pp->pln_own,
1142                "\t%s depth-charging %s %s in %s\n",
1143                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1144         } else {
1145             wu(0, pp->pln_own,
1146                "\t%s pinbombing %s %s in %s\n",
1147                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1148         }
1149         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1150         if (nuk_on_plane(&plp->plane) >= 0)
1151             hitchance = 100;
1152         else if (hardtarget != SECT_HARDTARGET)
1153             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1154         /* Always calculate damage */
1155         if (roll(100) <= hitchance) {
1156             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1157             if (nukedam) {
1158                 if (mission == MI_INTERDICT) {
1159                     wu(0, pp->pln_own,
1160                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1161                        prplane(pp), nukedam, cname(victim), s);
1162                     dam += nukedam;
1163                 }
1164             } else {
1165                 wu(0, pp->pln_own,
1166                    "\t\thit %s %s for %d damage\n",
1167                    cname(victim), s, newdam);
1168                 dam += newdam;
1169             }
1170         } else {
1171             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1172             wu(0, pp->pln_own, "missed\n");
1173             if (mission == MI_SINTERDICT) {
1174                 mpr(victim,
1175                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1176                     xyas(x, y, victim));
1177             } else if (*s == 's') {
1178                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1179                     s, xyas(x, y, victim));
1180             } else {
1181                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1182                     s, xyas(x, y, victim));
1183             }
1184             /* Now, even though we missed, the bombs
1185                land somewhere. */
1186             collateral_damage(x, y, newdam);
1187         }
1188
1189         /* use up missiles */
1190         if (plp->pcp->pl_flags & P_M)
1191             pp->pln_effic = 0;
1192     }
1193
1194     return dam;
1195 }
1196
1197 /*
1198  * Check to see if anyone hostile to the victim
1199  * is running an air defense mission on this
1200  * sector. If so, do air combat
1201  */
1202 int
1203 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1204             struct emp_qelem *esc_list)
1205 {
1206     int dam = 0, cn;
1207     int mission_flags, combat = 0, rel, dist, z;
1208     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1209     struct plist *plp;
1210     struct genlist *glp;
1211     struct empobj *gp;
1212     struct genlist mi[MAXNOC];
1213     char buf[512];
1214     char *path;
1215     int count;
1216     int tcount;
1217
1218     count = 0;
1219     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1220         count++;
1221     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1222         count++;
1223
1224     memset(mi, 0, sizeof(mi));
1225     for (z = 1; z < MAXNOC; z++)
1226         emp_initque((struct emp_qelem *)&mi[z]);
1227
1228     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1229
1230     for (cn = 1; cn < MAXNOC; cn++) {
1231         /* Check our relations */
1232         rel = getrel(getnatp(cn), victim);
1233
1234         if (rel > HOSTILE)
1235             continue;
1236
1237         if (QEMPTY(&mi[cn].queue))
1238             continue;
1239
1240         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1241          * list from the mission creation.  This list must be deleted later. */
1242         emp_initque(&interceptors);
1243         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1244             next = qp->q_forw;
1245             glp = (struct genlist *)qp;
1246             gp = glp->thing;
1247             if (CANT_HAPPEN(gp->ef_type != EF_PLANE))
1248                 break;
1249
1250             dist = mapdist(x, y, gp->x, gp->y);
1251
1252             plp = malloc(sizeof(struct plist));
1253             memset(plp, 0, sizeof(struct plist));
1254             plp->pcp = glp->cp;
1255             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1256
1257             /* missiles go one way, so we can use all the range */
1258             if (!(plp->pcp->pl_flags & P_M))
1259                 dist *= 2;
1260             /* If it's out of range, free it and continue on */
1261             if (dist > plp->plane.pln_range) {
1262                 free(plp);
1263                 continue;
1264             }
1265             emp_insque(&plp->queue, &interceptors);
1266         }
1267
1268         /* Remove those who cannot go */
1269         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1270
1271         if (QEMPTY(&interceptors))
1272             continue;
1273
1274         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1275          * that the higher numbered planes go into battle (they should be the better ones
1276          * at fighting, if all went well.) */
1277         tcount = 0;
1278         for (qp = interceptors.q_forw; qp != (&interceptors);
1279              qp = qp->q_forw)
1280             tcount++;
1281         tcount -= count * 2;
1282         /* Just in case there are more incoming than we have */
1283         if (tcount < 0)
1284             tcount = 0;
1285         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1286             next = qp->q_forw;
1287             if (tcount) {
1288                 tcount--;
1289                 /* Free it up and continue */
1290                 emp_remque(qp);
1291                 glp = (struct genlist *)qp;
1292                 free(glp);
1293             }
1294         }
1295
1296         /* Now, make a list of all the airports these planes are coming from */
1297         emp_initque(&airp);
1298         for (qp = interceptors.q_forw; qp != (&interceptors);
1299              qp = qp->q_forw) {
1300             plp = (struct plist *)qp;
1301             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1302                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1303         }
1304
1305         /* Now, fly them out one airport at a time */
1306         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1307             struct airport *air;
1308
1309             air = (struct airport *)qp;
1310             dist = mapdist(x, y, air->x, air->y);
1311
1312             emp_initque(&i);
1313
1314             /* Split off the interceptors at this base into i */
1315             divide(&interceptors, &i, air->x, air->y);
1316
1317             mission_flags = 0;
1318             mission_flags |= P_X;       /* stealth (shhh) */
1319             /* gets turned off if not all choppers */
1320             mission_flags |= P_H;
1321             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1322             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1323
1324             /* Did we run out of interceptors? */
1325             if (QEMPTY(&i))
1326                 continue;
1327             /* Did we run out of bombers? */
1328             if (QEMPTY(bomb_list)) {
1329                 /* Yes, so we have to put the rest of the interceptors back, and
1330                    then continue, or we leak memory */
1331                 pln_put(&i);
1332                 continue;
1333             }
1334             mission_flags =
1335                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 0, 0, P_F,
1336                                 mission_flags);
1337
1338             /* Did we run out of interceptors? */
1339             if (QEMPTY(&i))
1340                 continue;
1341             /* Did we run out of bombers? */
1342             if (QEMPTY(bomb_list)) {
1343                 /* Yes, so we have to put the rest of the interceptors back, and
1344                    then continue, or we leak memory */
1345                 pln_put(&i);
1346                 continue;
1347             }
1348
1349             path = BestAirPath(buf, air->x, air->y, x, y);
1350             if (CANT_HAPPEN(!path)) {
1351                 pln_put(&i);
1352                 continue;
1353             }
1354             wu(0, cn, "Flying %s mission from %s to %s\n",
1355                mission_name(MI_AIR_DEFENSE),
1356                xyas(air->x, air->y, cn),
1357                xyas(x, y, cn));
1358             if (air->own && (air->own != cn)) {
1359                 wu(0, air->own, "%s is flying %s mission from %s to %s\n",
1360                    cname(cn), mission_name(MI_AIR_DEFENSE),
1361                    xyas(air->x, air->y, air->own),
1362                    xyas(x, y, air->own));
1363             }
1364
1365             /* Now, fly the planes to the sector */
1366             emp_initque(&empty);
1367             ac_encounter(&i, &empty, air->x, air->y,
1368                          path, mission_flags, 1);
1369
1370             /* If none made it, continue */
1371             if (QEMPTY(&i))
1372                 continue;
1373
1374             /* Some made it, so now they get to try to fight. */
1375             /* Intercept the escorts first */
1376             combat = 0;
1377             if (!QEMPTY(esc_list)) {
1378                 mpr(victim, "%s air defense planes intercept!\n",
1379                     cname(cn));
1380                 ac_combat_headers(victim, cn);
1381                 ac_airtoair(esc_list, &i);
1382                 combat = 1;
1383             }
1384             /* Now intercept the bombers */
1385             if (!QEMPTY(bomb_list)) {
1386                 if (!combat) {
1387                     mpr(victim, "%s air defense planes intercept!\n",
1388                         cname(cn));
1389                     ac_combat_headers(victim, cn);
1390                 }
1391                 ac_airtoair(bomb_list, &i);
1392                 PR(cn, "\n");
1393                 PR(victim, "\n");
1394             }
1395
1396             pln_put(&i);
1397         }
1398         if (CANT_HAPPEN(!QEMPTY(&interceptors)))
1399             pln_put(&interceptors);
1400     }
1401
1402     /* We have to free all of these, if they are still there, otherwise they get
1403        lost and we leak memory all over the place. */
1404     for (cn = 1; cn < MAXNOC; cn++) {
1405         /* free up all this memory if it's still there */
1406         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1407             next = qp->q_forw;
1408             glp = (struct genlist *)qp;
1409             free(glp->thing);
1410             free(glp);
1411         }
1412     }
1413
1414     return dam;
1415 }