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