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