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