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