]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
Clarify how perform_mission() uses missiles
[empserver] / src / lib / subs / mission.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *     Markus Armbruster, 2003-2009
34  */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39 #include "empobj.h"
40 #include "file.h"
41 #include "item.h"
42 #include "misc.h"
43 #include "mission.h"
44 #include "nsc.h"
45 #include "optlist.h"
46 #include "path.h"
47 #include "player.h"
48 #include "prototypes.h"
49 #include "queue.h"
50 #include "xy.h"
51
52 struct genlist {
53     struct emp_qelem queue;     /* list of units */
54     void *cp;                   /* pointer to desc of thing */
55     struct empobj *thing;       /* thing's struct */
56 };
57
58 struct airport {
59     struct emp_qelem queue;
60     coord x, y;
61     natid own;
62 };
63
64 static void add_airport(struct emp_qelem *, coord, coord);
65 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
66                       char *, int);
67 static void build_mission_list(struct genlist *, coord, coord, int, natid);
68 static void build_mission_list_type(struct genlist *, coord, coord, int,
69                                     int, natid);
70 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
71 static int dosupport(struct genlist *, coord, coord, natid, natid);
72 static int find_airport(struct emp_qelem *, coord, coord);
73 static void mission_pln_arm(struct emp_qelem *, coord, coord, int,
74                             int, struct ichrstr *);
75 static void mission_pln_sel(struct emp_qelem *, int, int, int);
76 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
77                            char *, int);
78
79 /*
80  * Interdict commodities & transported planes
81  */
82 int
83 ground_interdict(coord x, coord y, natid victim, char *s)
84 {
85     int cn;
86     int dam = 0, newdam, rel;
87     struct genlist mi[MAXNOC];
88     int z;
89
90     memset(mi, 0, sizeof(mi));
91     for (z = 1; z < MAXNOC; z++)
92         emp_initque((struct emp_qelem *)&mi[z]);
93
94     build_mission_list(mi, x, y, MI_INTERDICT, victim);
95
96     for (cn = 1; cn < MAXNOC; cn++) {
97         rel = getrel(getnatp(cn), victim);
98         if (rel > HOSTILE)
99             continue;
100
101         if (QEMPTY(&mi[cn].queue))
102             continue;
103
104         newdam = perform_mission(x, y, victim, &mi[cn].queue,
105                                  MI_INTERDICT, s, SECT_HARDTARGET);
106         dam += newdam;
107         if (newdam)
108             mpr(victim, "%s interdiction mission does %d damage!\n",
109                 cname(cn), newdam);
110     }
111     if (dam) {
112         collateral_damage(x, y, dam);
113     }
114     return dam;
115 }
116
117 int
118 collateral_damage(coord x, coord y, int dam)
119 {
120     int coll;
121     struct sctstr sect;
122
123     if (!dam)
124         return 0;
125
126     getsect(x, y, &sect);
127     if (sect.sct_own) {
128         coll = ldround((double)dam * collateral_dam, 1);
129         if (coll == 0)
130             return 0;
131         mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
132             xyas(x, y, sect.sct_own), coll);
133         sectdamage(&sect, coll);
134         putsect(&sect);
135         return coll;
136     }
137     return 0;
138 }
139
140 static int
141 only_subs(struct emp_qelem *list)
142 {
143     struct emp_qelem *qp;
144     struct genlist *glp;
145     struct mchrstr *mcp;
146
147     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
148         glp = (struct genlist *)qp;
149
150         if (glp->thing->ef_type != EF_SHIP)
151             return 0;
152         mcp = glp->cp;
153         if (!(mcp->m_flags & M_SUB))
154             return 0;
155         /* It's a sub! */
156     }
157     /* They were all subs! */
158     return 1;
159 }
160
161
162 /*
163  *  Interdict ships & land units
164  */
165 int
166 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
167                int mission)
168 {
169     int cn;
170     int dam = 0, newdam;
171     struct genlist mi[MAXNOC];
172     int z;
173     int osubs;
174
175     memset(mi, 0, sizeof(mi));
176     for (z = 1; z < MAXNOC; z++)
177         emp_initque((struct emp_qelem *)&mi[z]);
178
179     build_mission_list(mi, x, y, mission, victim);
180
181     for (cn = 1; cn < MAXNOC; cn++) {
182         if (cn == victim)
183             continue;
184         if (mission == MI_SINTERDICT) {
185             if (getrel(getnatp(cn), victim) >= FRIENDLY)
186                 continue;
187         } else if (getrel(getnatp(cn), victim) > HOSTILE)
188             continue;
189
190         if (QEMPTY(&mi[cn].queue))
191             continue;
192
193         osubs = only_subs(&mi[cn].queue);
194         newdam = perform_mission(x, y, victim, &mi[cn].queue,
195                                  mission, s, hardtarget);
196         dam += newdam;
197         if (newdam) {
198             /* If only subs responded, then we don't know who's
199                subs they are */
200             mpr(victim, "%s interdiction mission does %d damage!\n",
201                 osubs ? "Enemy" : cname(cn), newdam);
202         }
203     }
204     if (dam) {
205         collateral_damage(x, y, dam);
206     }
207     return dam;
208 }
209
210 /*
211  *  Perform a mission against victim, on behalf of actee
212  */
213 int
214 off_support(coord x, coord y, natid victim, natid actee)
215 {
216     int dam = 0;
217     struct genlist mi[MAXNOC];
218     int z;
219
220     memset(mi, 0, sizeof(mi));
221     for (z = 1; z < MAXNOC; z++)
222         emp_initque((struct emp_qelem *)&mi[z]);
223
224     build_mission_list(mi, x, y, MI_SUPPORT, victim);
225     build_mission_list(mi, x, y, MI_OSUPPORT, victim);
226
227     dam = dosupport(mi, x, y, victim, actee);
228     return dam;
229 }
230
231 /*
232  *  Perform a mission against victim, on behalf of actee
233  */
234 int
235 def_support(coord x, coord y, natid victim, natid actee)
236 {
237     int dam = 0;
238     struct genlist mi[MAXNOC];
239     int z;
240
241     memset(mi, 0, sizeof(mi));
242     for (z = 1; z < MAXNOC; z++)
243         emp_initque((struct emp_qelem *)&mi[z]);
244
245     build_mission_list(mi, x, y, MI_SUPPORT, victim);
246     build_mission_list(mi, x, y, MI_DSUPPORT, victim);
247
248     dam = dosupport(mi, x, y, victim, actee);
249     return dam;
250 }
251
252 static int
253 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
254 {
255     int cn;
256     int rel;
257     int dam = 0;
258
259     for (cn = 1; cn < MAXNOC; cn++) {
260         rel = getrel(getnatp(cn), actee);
261         if ((cn != actee) && (rel != ALLIED))
262             continue;
263         rel = getrel(getnatp(cn), victim);
264         if ((cn != actee) && (rel != AT_WAR))
265             continue;
266
267         if (QEMPTY(&mi[cn].queue))
268             continue;
269
270         dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
271                                "", SECT_HARDTARGET);
272     }
273     return dam;
274 }
275
276 static void
277 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
278                    natid victim)
279 {
280     build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
281     build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
282     build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
283 }
284
285 static void
286 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
287                         int type, natid victim)
288 {
289     struct nstr_item ni;
290     struct genlist *glp;
291     struct empobj *gp;
292     union empobj_storage item;
293     int relat;
294     struct sctstr sect;
295
296     snxtitem_all(&ni, type);
297     while (nxtitem(&ni, &item)) {
298         gp = (struct empobj *)&item;
299
300         if (gp->own == 0)
301             continue;
302
303         if (gp->mobil < 1)
304             continue;
305
306         if ((gp->mission != mission) && (mission != MI_SINTERDICT))
307             continue;
308
309         if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
310             (gp->mission != MI_INTERDICT))
311             continue;
312
313         relat = getrel(getnatp(gp->own), victim);
314         if (mission == MI_SINTERDICT) {
315             if (relat >= FRIENDLY)
316                 continue;
317             else if (type != EF_PLANE && relat > HOSTILE)
318                 continue;
319         } else if (relat > HOSTILE)
320             continue;
321
322         if (!in_oparea(gp, x, y))
323             continue;
324
325         if (opt_SLOW_WAR) {
326             if (mission != MI_AIR_DEFENSE) {
327                 getsect(x, y, &sect);
328                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
329
330                     /*
331                      * If the owner of the unit isn't at war
332                      * with the victim, and doesn't own the
333                      * sect being acted upon, and isn't the
334                      * old owner of that sect, bounce them.
335                      */
336                     if (sect.sct_type != SCT_WATER &&
337                         sect.sct_own != gp->own &&
338                         sect.sct_oldown != gp->own)
339                         continue;
340                 }
341             }
342         }
343
344         glp = malloc(sizeof(struct genlist));
345         memset(glp, 0, sizeof(struct genlist));
346         glp->cp = get_empobj_chr(gp);
347         glp->thing = malloc(sizeof(item));
348         memcpy(glp->thing, &item, sizeof(item));
349         emp_insque(&glp->queue, &mi[gp->own].queue);
350     }
351 }
352
353 static void
354 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
355 {
356     struct nstr_item ni;
357     struct plist *plp;
358     struct plnstr plane;
359
360     snxtitem_all(&ni, EF_PLANE);
361     while (nxtitem(&ni, &plane)) {
362         if (plane.pln_own != cn)
363             continue;
364         if (plane.pln_mission != MI_ESCORT)
365             continue;
366         if (!in_oparea((struct empobj *)&plane, x, y))
367             continue;
368         plp = malloc(sizeof(struct plist));
369         memset(plp, 0, sizeof(struct plist));
370         plp->pcp = &plchr[(int)plane.pln_type];
371         plp->plane = plane;
372         emp_insque(&plp->queue, escorts);
373     }
374 }
375
376 static int
377 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
378                 int mission, char *s, int hardtarget)
379 {
380     struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
381     struct emp_qelem *newqp;
382     struct genlist *glp;
383     struct plist *plp;
384     struct empobj *gp;
385     struct lndstr *lp;
386     struct shpstr *sp;
387     struct sctstr sect;
388     struct mchrstr *mcp;
389     struct plchrstr *pcp;
390     int dam = 0, dam2;
391     natid plane_owner = 0;
392     int md, range, air_dam;
393     double hitchance, vrange;
394     int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
395
396     getsect(x, y, &sect);
397
398     emp_initque(&missiles);
399     emp_initque(&bombers);
400     emp_initque(&escorts);
401     emp_initque(&airp);
402
403     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
404         glp = (struct genlist *)qp;
405         gp = glp->thing;
406
407         md = mapdist(x, y, gp->x, gp->y);
408
409         if (glp->thing->ef_type == EF_LAND) {
410             lp = (struct lndstr *)glp->thing;
411
412             if (mission == MI_SINTERDICT)
413                 continue;
414
415             if ((mission == MI_INTERDICT) &&
416                 (md > land_max_interdiction_range))
417                 continue;
418
419             range = roundrange(lnd_fire_range(lp));
420             if (md > range)
421                 continue;
422
423             dam2 = lnd_fire(lp);
424             putland(lp->lnd_uid, lp);
425             if (dam2 < 0)
426                 continue;
427
428             if (targeting_ships) {
429                 if (chance(lnd_acc(lp) / 100.0))
430                     dam2 = ldround(dam2 / 2.0, 1);
431             }
432             dam += dam2;
433             if (targeting_ships)
434                 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
435             else
436                 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
437             wu(0, lp->lnd_own,
438                "%s fires at %s %s at %s\n",
439                prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
440
441             mpr(victim, "%s %s fires at you at %s\n",
442                 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
443         } else if (glp->thing->ef_type == EF_SHIP) {
444             sp = (struct shpstr *)glp->thing;
445             mcp = glp->cp;
446
447             if (((mission == MI_INTERDICT) ||
448                  (mission == MI_SINTERDICT)) &&
449                 (md > ship_max_interdiction_range))
450                 continue;
451             if (mission == MI_SINTERDICT) {
452                 if (!(mcp->m_flags & M_SONAR))
453                     continue;
454                 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
455                     continue;
456                 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
457                 vrange *= sp->shp_effic / 200.0;
458                 if (md > vrange)
459                     continue;
460                 /* can't look all the time */
461                 if (chance(0.5))
462                     continue;
463             }
464             if (mcp->m_flags & M_SUB) {
465                 if (!targeting_ships)
466                     continue;   /* subs interdict only ships */
467                 range = roundrange(torprange(sp));
468                 if (md > range)
469                     continue;
470                 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
471                     continue;
472                 dam2 = shp_torp(sp, 1);
473                 putship(sp->shp_uid, sp);
474                 if (dam2 < 0)
475                     continue;
476                 hitchance = shp_torp_hitchance(sp, md);
477
478                 wu(0, sp->shp_own,
479                    "%s locking on %s %s in %s\n",
480                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
481                 wu(0, sp->shp_own,
482                    "\tEffective torpedo range is %d.0\n", range);
483                 wu(0, sp->shp_own,
484                    "\tWhooosh... Hitchance = %d%%\n",
485                    (int)(hitchance * 100));
486
487                 if (!chance(hitchance)) {
488                     wu(0, sp->shp_own, "\tMissed\n");
489                     mpr(victim,
490                         "Incoming torpedo sighted @ %s missed (whew)!\n",
491                         xyas(x, y, victim));
492                     continue;
493                 }
494                 wu(0, sp->shp_own, "\tBOOM!...\n");
495                 dam += dam2;
496                 nreport(victim, N_TORP_SHIP, 0, 1);
497                 wu(0, sp->shp_own,
498                    "\tTorpedo hit %s %s for %d damage\n",
499                    cname(victim), s, dam2);
500
501                 mpr(victim,
502                     "Incoming torpedo sighted @ %s hits and does %d damage!\n",
503                     xyas(x, y, victim), dam2);
504             } else {
505                 range = roundrange(shp_fire_range(sp));
506                 if (md > range)
507                     continue;
508                 if (mission == MI_SINTERDICT)
509                     dam2 = shp_dchrg(sp);
510                 else
511                     dam2 = shp_fire(sp);
512                 putship(sp->shp_uid, sp);
513                 if (dam2 < 0)
514                     continue;
515                 dam += dam2;
516                 if (targeting_ships)
517                     nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
518                 else
519                     nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
520                 wu(0, sp->shp_own,
521                    "%s fires at %s %s at %s\n",
522                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
523
524                 mpr(victim, "%s %s fires at you at %s\n",
525                     cname(sp->shp_own), prship(sp), xyas(x, y, victim));
526             }
527         } else if (glp->thing->ef_type == EF_PLANE) {
528             pcp = glp->cp;
529             if (pcp->pl_flags & P_M)
530                 /* units have their own missile interdiction */
531                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
532                     continue;
533
534             /* save planes for later */
535             plp = malloc(sizeof(struct plist));
536
537             memset(plp, 0, sizeof(struct plist));
538             plp->pcp = pcp;
539             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
540             if (plp->pcp->pl_flags & P_M)
541                 emp_insque(&plp->queue, &missiles);
542             else
543                 emp_insque(&plp->queue, &bombers);
544             plane_owner = plp->plane.pln_own;
545         } else {
546             CANT_REACH();
547             break;
548         }
549     }
550
551     /*
552      * Missiles, except for interdiction of ships or land units,
553      * because that happens elsewhere, in shp_missile_interdiction()
554      * and lnd_missile_interdiction().
555      */
556     air_dam = 0;
557     for (qp = missiles.q_back; qp != &missiles; qp = newqp) {
558         newqp = qp->q_back;
559         plp = (struct plist *)qp;
560
561         if (air_dam < 100
562             && !CANT_HAPPEN(hardtarget != SECT_HARDTARGET
563                             || (plp->pcp->pl_flags & P_MAR))
564             && mission_pln_equip(plp, NULL, 'p') >= 0) {
565             if (msl_hit(&plp->plane, SECT_HARDTARGET, EF_SECTOR,
566                         N_SCT_MISS, N_SCT_SMISS,
567                         "sector", x, y, victim)) {
568                 dam2 = pln_damage(&plp->plane, 'p', 1);
569                 air_dam += dam2;
570 #if 0
571             /*
572              * FIXME want collateral damage on miss, but we get here
573              * too when launch fails or missile is intercepted
574              */
575             } else {
576                 /* Missiles that miss have to hit somewhere! */
577                 dam2 = pln_damage(&plp->plane, 'p', 0);
578                 collateral_damage(x, y, dam2);
579 #endif
580             }
581             plp->plane.pln_effic = 0;
582             putplane(plp->plane.pln_uid, &plp->plane);
583         }
584         emp_remque(qp);
585         free(qp);
586     }
587     dam += air_dam;
588
589     if (QEMPTY(&bombers)) {
590         qp = list->q_forw;
591         while (qp != list) {
592             glp = (struct genlist *)qp;
593             qp = qp->q_forw;
594
595             free(glp->thing);
596             free(glp);
597         }
598         return dam;
599     }
600     /*
601      * If there are planes performing an
602      * interdict or support mission, find
603      * some escorts for them, if possible.
604      * Up to 2 per bomber, if possible.
605      */
606     find_escorts(x, y, plane_owner, &escorts);
607
608     if (mission == MI_SINTERDICT)
609         mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
610     else
611         mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
612
613     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
614
615     for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
616         plp = (struct plist *)qp;
617         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
618             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
619     }
620
621     air_dam = 0;
622     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
623         struct airport *air;
624         char buf[512];
625         char *pp;
626
627         air = (struct airport *)qp;
628         md = mapdist(x, y, air->x, air->y);
629
630         emp_initque(&b);
631         emp_initque(&e);
632
633         /* Split off the bombers at this base into b */
634         divide(&bombers, &b, air->x, air->y);
635
636         /* Split off the escorts at this base into e */
637         divide(&escorts, &e, air->x, air->y);
638
639         mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', NULL);
640
641         if (QEMPTY(&b))
642             continue;
643
644         mission_pln_arm(&e, air->x, air->y, 2 * md, 'e', NULL);
645
646         pp = BestAirPath(buf, air->x, air->y, x, y);
647         if (CANT_HAPPEN(!pp))
648             continue;
649         wu(0, plane_owner, "Flying %s mission from %s to %s\n",
650            mission_name(mission),
651            xyas(air->x, air->y, plane_owner),
652            xyas(x, y, plane_owner));
653         if (air->own && (air->own != plane_owner)) {
654             wu(0, air->own, "%s is flying %s mission from %s to %s\n",
655                cname(plane_owner), mission_name(mission),
656                xyas(air->x, air->y, air->own),
657                xyas(x, y, air->own));
658         }
659
660         ac_encounter(&b, &e, air->x, air->y, pp, 0);
661
662         if (!QEMPTY(&b))
663             air_dam +=
664                 air_damage(&b, x, y, mission, victim, s, hardtarget);
665
666         pln_put(&b);
667         pln_put(&e);
668     }
669
670     if (air_dam > 0) {
671         dam += air_dam;
672         if (targeting_ships)
673             nreport(plane_owner, N_SHP_BOMB, victim, 1);
674         else
675             nreport(plane_owner, N_SCT_BOMB, victim, 1);
676     }
677
678     /* free up all this memory */
679     qp = list->q_forw;
680     while (qp != list) {
681         glp = (struct genlist *)qp;
682         qp = qp->q_forw;
683
684         free(glp->thing);
685         free(glp);
686     }
687
688     qp = escorts.q_forw;
689     while (qp != (&escorts)) {
690         newqp = qp->q_forw;
691         emp_remque(qp);
692         free(qp);
693         qp = newqp;
694     }
695
696     qp = bombers.q_forw;
697     while (qp != (&bombers)) {
698         newqp = qp->q_forw;
699         emp_remque(qp);
700         free(qp);
701         qp = newqp;
702     }
703
704     return dam;
705 }
706
707 int
708 cando(int mission, int type)
709 {
710     switch (mission) {
711     case MI_ESCORT:
712         if (type == EF_PLANE)
713             return 1;
714         return 0;
715     case MI_AIR_DEFENSE:
716         if (type == EF_PLANE)
717             return 1;
718         return 0;
719     case MI_SINTERDICT:
720         if ((type == EF_PLANE) || (type == EF_SHIP))
721             return 1;
722         return 0;
723     case MI_INTERDICT:
724         return 1;
725     case MI_SUPPORT:
726     case MI_OSUPPORT:
727     case MI_DSUPPORT:
728         if (type == EF_PLANE)
729             return 1;
730         return 0;
731     case MI_RESERVE:
732         if (type == EF_LAND)
733             return 1;
734         return 0;
735     }
736
737     return 0;
738 }
739
740 char *
741 mission_name(short mission)
742 {
743     switch (mission) {
744     case MI_INTERDICT:
745         return "an interdiction";
746     case MI_SUPPORT:
747         return "a support";
748     case MI_OSUPPORT:
749         return "an offensive support";
750     case MI_DSUPPORT:
751         return "a defensive support";
752     case MI_RESERVE:
753         return "a reserve";
754     case MI_ESCORT:
755         return "an escort";
756     case MI_SINTERDICT:
757         return "a sub interdiction";
758     case MI_AIR_DEFENSE:
759         return "an air defense";
760     }
761     CANT_REACH();
762     return "a mysterious";
763 }
764
765 /*
766  * Maximum distance GP can perform its mission.
767  * Note: this has nothing to do with the radius of the op-area.
768  * oprange() governs where the unit *can* strike, the op-area governs
769  * where the player wants it to strike.
770  */
771 int
772 oprange(struct empobj *gp)
773 {
774     switch (gp->ef_type) {
775     case EF_SHIP:
776         return ldround(shp_fire_range((struct shpstr *)gp), 1);
777     case EF_LAND:
778         if (gp->mission == MI_RESERVE)
779             return lnd_reaction_range((struct lndstr *)gp);
780         return ldround(lnd_fire_range((struct lndstr *)gp), 1);
781     case EF_PLANE:
782         /* missiles go one way, so we can use all the range */
783         if (plchr[(int)gp->type].pl_flags & P_M)
784             return ((struct plnstr *)gp)->pln_range;
785         return ((struct plnstr *)gp)->pln_range / 2;
786     }
787     CANT_REACH();
788     return -1;
789 }
790
791 /*
792  * Does GP's mission op area cover X,Y?
793  */
794 int
795 in_oparea(struct empobj *gp, coord x, coord y)
796 {
797     return mapdist(x, y, gp->opx, gp->opy) <= gp->radius
798         && mapdist(x, y, gp->x, gp->y) <= oprange(gp);
799 }
800
801 /*
802  *  Remove all planes who cannot go on
803  *  the mission from the plane list.
804  */
805 static void
806 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
807                 int hardtarget)
808 {
809     struct emp_qelem *qp, *next;
810     struct plnstr *pp;
811     struct plchrstr *pcp;
812     struct plist *plp;
813
814     for (qp = list->q_forw; qp != list; qp = next) {
815         next = qp->q_forw;
816         plp = (struct plist *)qp;
817         pp = &plp->plane;
818         pcp = plp->pcp;
819
820         if (pp->pln_effic < 40) {
821             emp_remque(qp);
822             free(qp);
823             continue;
824         }
825
826         if (pp->pln_mobil < 1) {
827             emp_remque(qp);
828             free(qp);
829             continue;
830         }
831
832         if (opt_MARKET) {
833             if (ontradingblock(EF_PLANE, pp)) {
834                 emp_remque(qp);
835                 free(qp);
836                 continue;
837             }
838         }
839
840         if (!pln_capable(pp, wantflags, nowantflags)) {
841             emp_remque(qp);
842             free(qp);
843             continue;
844         }
845
846         if (!pln_airbase_ok(pp, 0, 0)) {
847             emp_remque(qp);
848             free(qp);
849             continue;
850         }
851
852         if (pcp->pl_flags & P_A) {
853             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
854                 emp_remque(qp);
855                 free(qp);
856                 continue;
857             }
858         }
859
860         putplane(pp->pln_uid, pp);
861     }
862 }
863
864 /*
865  * Arm only the planes at x,y
866  */
867 static void
868 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
869                 int mission, struct ichrstr *ip)
870 {
871     struct emp_qelem *qp;
872     struct emp_qelem *next;
873     struct plist *plp;
874     struct plnstr *pp;
875
876     for (qp = list->q_forw; qp != list; qp = next) {
877         next = qp->q_forw;
878         plp = (struct plist *)qp;
879         pp = &plp->plane;
880
881         if (pp->pln_x != x)
882             continue;
883         if (pp->pln_y != y)
884             continue;
885
886         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
887             || mission_pln_equip(plp, ip, mission) < 0) {
888             emp_remque(qp);
889             free(qp);
890             continue;
891         }
892
893         pp->pln_flags |= PLN_LAUNCHED;
894         pp->pln_mobil -= pln_mobcost(dist, pp, mission);
895         putplane(pp->pln_uid, pp);
896     }
897 }
898
899 int
900 mission_pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
901 {
902     struct plchrstr *pcp;
903     struct plnstr *pp;
904     int load, needed;
905     struct lndstr land;
906     struct shpstr ship;
907     struct sctstr sect;
908     i_type itype;
909     short *item;
910
911     pp = &plp->plane;
912     pcp = plp->pcp;
913     if (pp->pln_ship >= 0) {
914         getship(pp->pln_ship, &ship);
915         item = ship.shp_item;
916     } else if (pp->pln_land >= 0) {
917         getland(pp->pln_land, &land);
918         item = land.lnd_item;
919     } else {
920         getsect(pp->pln_x, pp->pln_y, &sect);
921         item = sect.sct_item;
922     }
923     if (pcp->pl_fuel > item[I_PETROL]) {
924         return -1;
925     }
926     item[I_PETROL] -= pcp->pl_fuel;
927     load = pln_load(pp);
928     itype = I_NONE;
929     switch (mission) {
930     case 'p':           /* pinpoint bomb */
931         itype = I_SHELL;
932         break;
933     case 'i':           /* missile interception */
934         if (load)
935             itype = I_SHELL;
936         break;
937     case 'e':           /* escort */
938     case 0:             /* plane interception */
939         load = 0;
940         break;
941     default:
942         CANT_REACH();
943         load = 0;
944     }
945
946     if (itype != I_NONE) {
947         needed = load / ichr[itype].i_lbs;
948         if (needed <= 0)
949             return -1;
950         if (CANT_HAPPEN(nuk_on_plane(pp) >= 0))
951             return -1;
952         if (itype == I_SHELL && item[itype] < needed) {
953             if (pp->pln_ship >= 0)
954                 shp_supply(&ship, I_SHELL, needed);
955             else if (pp->pln_land >= 0)
956                 lnd_supply(&land, I_SHELL, needed);
957             else
958                 sct_supply(&sect, I_SHELL, needed);
959         }
960         if (item[itype] < needed)
961             return -1;
962         item[itype] -= needed;
963         plp->load = needed;
964     }
965
966     if (pp->pln_ship >= 0)
967         putship(ship.shp_uid, &ship);
968     else if (pp->pln_land >= 0)
969         putland(land.lnd_uid, &land);
970     else
971         putsect(&sect);
972     return 0;
973 }
974
975 /*
976  *  Return 1 if this x,y pair is in the list
977  */
978 static int
979 find_airport(struct emp_qelem *airp, coord x, coord y)
980 {
981     struct emp_qelem *qp;
982     struct airport *a;
983
984     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
985         a = (struct airport *)qp;
986         if ((a->x == x) && (a->y == y))
987             return 1;
988     }
989
990     return 0;
991 }
992
993 /* #*# This needs to be changed to include acc's -KHS */
994 static void
995 add_airport(struct emp_qelem *airp, coord x, coord y)
996 {
997     struct airport *a;
998     struct sctstr sect;
999
1000     a = malloc(sizeof(struct airport));
1001
1002     a->x = x;
1003     a->y = y;
1004     getsect(x, y, &sect);
1005     a->own = sect.sct_own;
1006
1007     emp_insque((struct emp_qelem *)a, airp);
1008 }
1009
1010 /*
1011  *  Take all the planes in list 1 that
1012  *  are at x,y, and put them into list 2.
1013  */
1014 static void
1015 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1016 {
1017     struct emp_qelem *qp, *next;
1018     struct plist *plp;
1019
1020     for (qp = l1->q_forw; qp != l1; qp = next) {
1021         next = qp->q_forw;
1022         plp = (struct plist *)qp;
1023
1024         if (plp->plane.pln_x != x)
1025             continue;
1026         if (plp->plane.pln_y != y)
1027             continue;
1028
1029         emp_remque(qp);
1030         emp_insque(qp, l2);
1031     }
1032 }
1033
1034 static int
1035 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1036            natid victim, char *s, int hardtarget)
1037 {
1038     struct emp_qelem *qp;
1039     struct plist *plp;
1040     struct plnstr *pp;
1041     int newdam, dam = 0;
1042     int hitchance;
1043
1044     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1045         plp = (struct plist *)qp;
1046         pp = &plp->plane;
1047
1048         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1049             continue;
1050
1051         if (!plp->load)
1052             continue;
1053
1054         newdam = 0;
1055         if (plp->pcp->pl_flags & P_A) {
1056             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1057                 wu(0, pp->pln_own,
1058                    "\t%s detects sub movement in %s\n",
1059                    prplane(pp), xyas(x, y, pp->pln_own));
1060                 continue;
1061             }
1062             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1063                 wu(0, pp->pln_own,
1064                    "\t%s tracks %s %s at %s\n",
1065                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1066                 continue;
1067             }
1068             wu(0, pp->pln_own,
1069                "\t%s depth-charging %s %s in %s\n",
1070                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1071         } else {
1072             wu(0, pp->pln_own,
1073                "\t%s pinbombing %s %s in %s\n",
1074                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1075         }
1076         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1077         if (nuk_on_plane(&plp->plane) >= 0)
1078             hitchance = 100;
1079         else if (hardtarget != SECT_HARDTARGET)
1080             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1081         if (roll(100) <= hitchance) {
1082             newdam = pln_damage(&plp->plane, 'p', 1);
1083             wu(0, pp->pln_own,
1084                "\t\thit %s %s for %d damage\n",
1085                cname(victim), s, newdam);
1086             dam += newdam;
1087         } else {
1088             newdam = pln_damage(&plp->plane, 'p', 0);
1089             wu(0, pp->pln_own, "missed\n");
1090             if (mission == MI_SINTERDICT) {
1091                 mpr(victim,
1092                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1093                     xyas(x, y, victim));
1094             } else if (*s == 's') {
1095                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1096                     s, xyas(x, y, victim));
1097             } else {
1098                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1099                     s, xyas(x, y, victim));
1100             }
1101             /* Now, even though we missed, the bombs
1102                land somewhere. */
1103             collateral_damage(x, y, newdam);
1104         }
1105
1106         /* use up missiles */
1107         if (plp->pcp->pl_flags & P_M)
1108             pp->pln_effic = 0;
1109     }
1110
1111     return dam;
1112 }