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