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