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