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