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