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