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