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