]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
Factor out common land unit fire code into lnd_fire()
[empserver] / src / lib / subs / mission.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  */
34
35 #include <config.h>
36
37 #include <stdlib.h>
38 #include "damage.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 int mission_pln_arm(struct emp_qelem *, coord, coord, int,
74                            int, struct ichrstr *, int, int, 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, 0);
113     }
114     return dam;
115 }
116
117 int
118 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
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, list);
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, 0);
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 dist;
294     int radius;
295     int relat;
296     struct sctstr sect;
297
298     snxtitem_all(&ni, type);
299     while (nxtitem(&ni, &item)) {
300         gp = (struct empobj *)&item;
301
302         if (gp->own == 0)
303             continue;
304
305         if (gp->mobil < 1)
306             continue;
307
308         if ((gp->mission != mission) && (mission != MI_SINTERDICT))
309             continue;
310
311         if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
312             (gp->mission != MI_INTERDICT))
313             continue;
314
315         relat = getrel(getnatp(gp->own), victim);
316         if (mission == MI_SINTERDICT) {
317             if (relat >= FRIENDLY)
318                 continue;
319             else if (type != EF_PLANE && relat > HOSTILE)
320                 continue;
321         } else if (relat > HOSTILE)
322             continue;
323
324         dist = mapdist(x, y, gp->opx, gp->opy);
325
326         radius = gp->radius;
327         if (mission != MI_RESERVE)      /* XXX */
328             oprange(gp, &radius);
329
330         if (dist > radius)
331             continue;
332
333         /* Ok, it is within the operations range. */
334         /* Now check from where the object actually is */
335         dist = mapdist(x, y, gp->x, gp->y);
336         radius = 999;
337         oprange(gp, &radius);
338         if (dist > radius)
339             continue;
340         /* Ok, the object can get to where the x,y is */
341
342         if (opt_SLOW_WAR) {
343             if (mission != MI_AIR_DEFENSE) {
344                 getsect(x, y, &sect);
345                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
346
347                     /*
348                      * If the player->owner of the unit isn't at war
349                      * with the victim, and doesn't own the
350                      * sect being acted upon, and isn't the
351                      * old player->owner of that sect, bounce them.
352                      */
353                     if (sect.sct_type != SCT_WATER &&
354                         sect.sct_own != gp->own &&
355                         sect.sct_oldown != gp->own)
356                         continue;
357                 }
358             }
359         }
360
361         glp = malloc(sizeof(struct genlist));
362         memset(glp, 0, sizeof(struct genlist));
363         glp->cp = get_empobj_chr(gp);
364         glp->thing = malloc(sizeof(item));
365         memcpy(glp->thing, &item, sizeof(item));
366         emp_insque(&glp->queue, &mi[gp->own].queue);
367     }
368 }
369
370 static void
371 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
372 {
373     struct nstr_item ni;
374     struct plist *plp;
375     struct plnstr plane;
376     int dist;
377
378     snxtitem_all(&ni, EF_PLANE);
379     while (nxtitem(&ni, &plane)) {
380         if (plane.pln_own != cn)
381             continue;
382
383         if (plane.pln_mission != MI_ESCORT)
384             continue;
385
386         dist = mapdist(x, y, plane.pln_x, plane.pln_y);
387         if (dist > plane.pln_range / 2)
388             continue;
389
390         plp = malloc(sizeof(struct plist));
391         memset(plp, 0, sizeof(struct plist));
392         plp->pcp = &plchr[(int)plane.pln_type];
393         plp->plane = plane;
394         emp_insque(&plp->queue, escorts);
395     }
396 }
397
398 static int
399 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
400                 int mission, char *s, int hardtarget)
401 {
402     struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
403     struct emp_qelem *newqp;
404     struct genlist *glp;
405     struct plist *plp;
406     struct empobj *gp;
407     struct lndstr *lp;
408     struct shpstr *sp;
409     struct sctstr sect;
410     struct mchrstr *mcp;
411     struct plchrstr *pcp;
412     int dam = 0, dam2, mission_flags, tech;
413     natid plane_owner = 0;
414     int md, range, air_dam = 0;
415     double prb, hitchance, vrange;
416
417     getsect(x, y, &sect);
418
419     emp_initque(&missiles);
420     emp_initque(&bombers);
421     emp_initque(&escorts);
422     emp_initque(&airp);
423
424     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
425         glp = (struct genlist *)qp;
426         gp = glp->thing;
427
428         md = mapdist(x, y, gp->x, gp->y);
429
430         if (glp->thing->ef_type == EF_LAND) {
431             lp = (struct lndstr *)glp->thing;
432
433             if (mission == MI_SINTERDICT)
434                 continue;
435
436             if ((mission == MI_INTERDICT) &&
437                 (md > land_max_interdiction_range))
438                 continue;
439
440             range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
441             if (md > range)
442                 continue;
443
444             dam2 = lnd_fire(lp);
445             putland(lp->lnd_uid, lp);
446             if (dam2 < 0)
447                 continue;
448
449             if (sect.sct_type == SCT_WATER) {
450                 if (chance(lp->lnd_acc / 100.0))
451                     dam2 = ldround(dam2 / 2.0, 1);
452             }
453             dam += dam2;
454             if (sect.sct_type == SCT_WATER)
455                 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
456             else
457                 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
458             wu(0, lp->lnd_own,
459                "%s fires at %s %s at %s\n",
460                prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
461
462             mpr(victim, "%s %s fires at you at %s\n",
463                 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
464         } else if (glp->thing->ef_type == EF_SHIP) {
465             sp = (struct shpstr *)glp->thing;
466             mcp = glp->cp;
467
468             if (((mission == MI_INTERDICT) ||
469                  (mission == MI_SINTERDICT)) &&
470                 (md > ship_max_interdiction_range))
471                 continue;
472 /*
473   if ((mcp->m_flags & M_SUB) &&
474   (sect.sct_type != SCT_WATER))
475   continue;
476 */
477             if (mission == MI_SINTERDICT) {
478                 if (!(mcp->m_flags & M_SONAR))
479                     continue;
480                 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
481                     continue;
482                 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
483                 vrange *= sp->shp_effic / 200.0;
484                 if (md > vrange)
485                     continue;
486                 /* can't look all the time */
487                 if (chance(0.5))
488                     continue;
489             }
490             if (mcp->m_flags & M_SUB) {
491 /* If we aren't shooting at "subs" or "ships" don't fire at all from
492    a sub. */
493                 if (*s != 's')
494                     continue;
495                 range = roundrange(torprange(sp));
496                 if (md > range)
497                     continue;
498                 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
499                     continue;
500                 dam2 = shp_torp(sp, 1);
501                 putship(sp->shp_uid, sp);
502                 if (dam2 < 0)
503                     continue;
504                 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
505
506                 wu(0, sp->shp_own,
507                    "%s locking on %s %s in %s\n",
508                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
509                 wu(0, sp->shp_own,
510                    "\tEffective torpedo range is %d.0\n", range);
511                 wu(0, sp->shp_own,
512                    "\tWhooosh... Hitchance = %d%%\n",
513                    (int)(hitchance * 100));
514
515                 if (!chance(hitchance)) {
516                     wu(0, sp->shp_own, "\tMissed\n");
517                     mpr(victim,
518                         "Incoming torpedo sighted @ %s missed (whew)!\n",
519                         xyas(x, y, victim));
520                     continue;
521                 }
522                 wu(0, sp->shp_own, "\tBOOM!...\n");
523                 dam += dam2;
524                 nreport(victim, N_TORP_SHIP, 0, 1);
525                 wu(0, sp->shp_own,
526                    "\tTorpedo hit %s %s for %d damage\n",
527                    cname(victim), s, dam2);
528
529                 mpr(victim,
530                     "Incoming torpedo sighted @ %s hits and does %d damage!\n",
531                     xyas(x, y, victim), dam2);
532             } else {
533                 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
534                 if (md > range)
535                     continue;
536                 if (mission == MI_SINTERDICT)
537                     dam2 = shp_dchrg(sp);
538                 else
539                     dam2 = shp_fire(sp);
540                 putship(sp->shp_uid, sp);
541                 if (dam2 < 0)
542                     continue;
543                 if (range == 0.0)
544                     prb = 1.0;
545                 else
546                     prb = (double)md / range;
547                 prb *= prb;
548                 if (chance(prb))
549                     dam2 /= 2;
550                 dam += dam2;
551                 if (sect.sct_type == SCT_WATER)
552                     nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
553                 else
554                     nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
555                 wu(0, sp->shp_own,
556                    "%s fires at %s %s at %s\n",
557                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
558
559                 mpr(victim, "%s %s fires at you at %s\n",
560                     cname(sp->shp_own), prship(sp), xyas(x, y, victim));
561             }
562         } else if (glp->thing->ef_type == EF_PLANE) {
563             pcp = glp->cp;
564             if (pcp->pl_flags & P_M)
565                 /* units have their own missile interdiction */
566                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
567                     continue;
568
569             /* save planes for later */
570             plp = malloc(sizeof(struct plist));
571
572             memset(plp, 0, sizeof(struct plist));
573             plp->pcp = pcp;
574             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
575             if (plp->pcp->pl_flags & P_M)
576                 emp_insque(&plp->queue, &missiles);
577             else
578                 emp_insque(&plp->queue, &bombers);
579             plane_owner = plp->plane.pln_own;
580         } else {
581             CANT_REACH();
582             break;
583         }
584     }
585     if (!QEMPTY(&missiles)) {
586         /* I arbitrarily chose 100 mindam -KHS */
587         dam +=
588             msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
589                               "sector", victim, mission);
590         qp = missiles.q_forw;
591         while (qp != (&missiles)) {
592             newqp = qp->q_forw;
593             emp_remque(qp);
594             free(qp);
595             qp = newqp;
596         }
597     }
598
599     if (QEMPTY(&bombers)) {
600         qp = list->q_forw;
601         while (qp != list) {
602             glp = (struct genlist *)qp;
603             qp = qp->q_forw;
604
605             free(glp->thing);
606             free(glp);
607         }
608         return dam;
609     }
610     /*
611      * If there are planes performing an
612      * interdict or support mission, find
613      * some escorts for them, if possible.
614      * Up to 2 per bomber, if possible.
615      */
616     find_escorts(x, y, plane_owner, &escorts);
617
618     if (mission == MI_SINTERDICT)
619         mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
620     else
621         mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
622
623     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
624
625     for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
626         plp = (struct plist *)qp;
627         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
628             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
629     }
630
631     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
632         struct airport *air;
633         char buf[512];
634         char *pp;
635
636         air = (struct airport *)qp;
637         md = mapdist(x, y, air->x, air->y);
638
639         emp_initque(&b);
640         emp_initque(&e);
641
642         /* Split off the bombers at this base into b */
643         divide(&bombers, &b, air->x, air->y);
644
645         /* Split off the escorts at this base into e */
646         divide(&escorts, &e, air->x, air->y);
647
648         tech = 0;
649         mission_flags = 0;
650         mission_flags |= P_X;   /* stealth (shhh) */
651         mission_flags |= P_H;   /* gets turned off if not all choppers */
652
653         mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
654                                         0, mission_flags, &tech);
655
656         if (QEMPTY(&b))
657             continue;
658
659         mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
660                                         P_F | P_ESC, mission_flags, &tech);
661
662         pp = BestAirPath(buf, air->x, air->y, x, y);
663         if (CANT_HAPPEN(!pp))
664             continue;
665         wu(0, plane_owner, "Flying %s mission from %s\n",
666            mission_name(mission), xyas(air->x, air->y, plane_owner));
667         if (air->own && (air->own != plane_owner)) {
668             wu(0, air->own, "%s is flying %s mission from %s\n",
669                cname(plane_owner), mission_name(mission),
670                xyas(air->x, air->y, air->own));
671         }
672
673         ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
674
675         if (!QEMPTY(&b))
676             air_dam +=
677                 air_damage(&b, x, y, mission, victim, s, hardtarget);
678
679         pln_put(&b);
680         pln_put(&e);
681     }
682
683     if (air_dam > 0) {
684         dam += air_dam;
685         if (sect.sct_type == SCT_WATER)
686             nreport(plane_owner, N_SHP_BOMB, victim, 1);
687         else
688             nreport(plane_owner, N_SCT_BOMB, victim, 1);
689     }
690
691     /* free up all this memory */
692     qp = list->q_forw;
693     while (qp != list) {
694         glp = (struct genlist *)qp;
695         qp = qp->q_forw;
696
697         free(glp->thing);
698         free(glp);
699     }
700
701     qp = escorts.q_forw;
702     while (qp != (&escorts)) {
703         newqp = qp->q_forw;
704         emp_remque(qp);
705         free(qp);
706         qp = newqp;
707     }
708
709     qp = bombers.q_forw;
710     while (qp != (&bombers)) {
711         newqp = qp->q_forw;
712         emp_remque(qp);
713         free(qp);
714         qp = newqp;
715     }
716
717     return dam;
718 }
719
720 int
721 cando(int mission, int type)
722 {
723     switch (mission) {
724     case MI_ESCORT:
725         if (type == EF_PLANE)
726             return 1;
727         return 0;
728     case MI_AIR_DEFENSE:
729         if (type == EF_PLANE)
730             return 1;
731         return 0;
732     case MI_SINTERDICT:
733         if ((type == EF_PLANE) || (type == EF_SHIP))
734             return 1;
735         return 0;
736     case MI_INTERDICT:
737         return 1;
738     case MI_SUPPORT:
739     case MI_OSUPPORT:
740     case MI_DSUPPORT:
741         if (type == EF_PLANE)
742             return 1;
743         return 0;
744     case MI_RESERVE:
745         if (type == EF_LAND)
746             return 1;
747         return 0;
748     }
749
750     return 0;
751 }
752
753 char *
754 mission_name(short mission)
755 {
756     switch (mission) {
757     case MI_INTERDICT:
758         return "an interdiction";
759     case MI_SUPPORT:
760         return "a support";
761     case MI_OSUPPORT:
762         return "a offensive support";
763     case MI_DSUPPORT:
764         return "a defensive support";
765     case MI_RESERVE:
766         return "a reserve";
767     case MI_ESCORT:
768         return "an escort";
769     case MI_SINTERDICT:
770         return "a sub interdiction";
771     case MI_AIR_DEFENSE:
772         return "an air defense";
773     }
774     return "a mysterious";
775 }
776
777 void
778 show_mission(int type, struct nstr_item *np)
779 {
780     int first = 1, radius;
781     union empobj_storage item;
782     struct empobj *gp;
783
784     while (nxtitem(np, &item)) {
785         gp = (struct empobj *)&item;
786         if (!player->owner || gp->own == 0)
787             continue;
788
789         if (first) {
790             pr("Thing                         x,y   op-sect rad mission\n");
791             first = 0;
792         }
793         pr("%-25s", obj_nameof(gp));
794         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
795         if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
796             gp->mission == MI_OSUPPORT ||
797             gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
798             radius = 999;
799             oprange(gp, &radius);
800             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
801             if (radius < gp->radius)
802                 pr("  %4d", radius);
803             else
804                 pr("  %4d", gp->radius);
805         } else if (gp->mission == MI_RESERVE) {
806             struct sctstr sect;
807             int plus = 2;
808
809             getsect(gp->x, gp->y, &sect);
810             if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
811                 plus++;
812
813             if (item.land.lnd_rad_max == 0)
814                 plus = 0;
815             else
816                 plus += item.land.lnd_rad_max;
817             prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
818             pr("  %4d", plus);
819         } else if (gp->mission == MI_ESCORT) {
820             pr("        ");
821             pr("  %4d", item.plane.pln_range / 2);
822         } else
823             pr("              ");
824         if (gp->mission)
825             pr(" is on %s mission\n", mission_name(gp->mission));
826         else
827             pr(" has no mission.\n");
828     }
829 }
830
831 int
832 oprange(struct empobj *gp, int *radius)
833 {
834     int range;
835
836     switch (gp->ef_type) {
837     case EF_SHIP:
838         range = ldround(effrange(((struct shpstr *)gp)->shp_frnge,
839                                  ((struct shpstr *)gp)->shp_tech), 1);
840         break;
841     case EF_LAND:
842         range = ldround(effrange(((struct lndstr *)gp)->lnd_frg,
843                                  ((struct lndstr *)gp)->lnd_tech), 1);
844         break;
845     case EF_PLANE:
846         /* missiles go one way, so we can use all the range */
847         if (plchr[(int)gp->type].pl_flags & P_M)
848             range = ((struct plnstr *)gp)->pln_range;
849         else
850             range = ((struct plnstr *)gp)->pln_range / 2;
851         break;
852     default:
853         CANT_REACH();
854         range = -1;
855     }
856
857     if (*radius > range)
858         *radius = range;
859
860     return range;
861 }
862
863 /*
864  *  Remove all planes who cannot go on
865  *  the mission from the plane list.
866  */
867 static void
868 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
869                 int hardtarget)
870 {
871     struct emp_qelem *qp, *next;
872     struct plnstr *pp;
873     struct plchrstr *pcp;
874     struct plist *plp;
875
876     for (qp = list->q_forw; qp != list; qp = next) {
877         next = qp->q_forw;
878         plp = (struct plist *)qp;
879         pp = &plp->plane;
880         pcp = plp->pcp;
881
882         if (pp->pln_effic < 40) {
883             emp_remque(qp);
884             free(qp);
885             continue;
886         }
887
888         if (pp->pln_mobil < 1) {
889             emp_remque(qp);
890             free(qp);
891             continue;
892         }
893
894         if (opt_MARKET) {
895             if (ontradingblock(EF_PLANE, pp)) {
896                 emp_remque(qp);
897                 free(qp);
898                 continue;
899             }
900         }
901
902         if (!pln_capable(pp, wantflags, nowantflags)) {
903             emp_remque(qp);
904             free(qp);
905             continue;
906         }
907
908         if (!pln_airbase_ok(pp, 0, 0)) {
909             emp_remque(qp);
910             free(qp);
911             continue;
912         }
913             
914         if (pcp->pl_flags & P_A) {
915             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
916                 emp_remque(qp);
917                 free(qp);
918                 continue;
919             }
920         }
921
922         putplane(pp->pln_uid, pp);
923     }
924 }
925
926 /*
927  * Arm only the planes at x,y
928  */
929 static int
930 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
931                 int mission, struct ichrstr *ip, int flags,
932                 int mission_flags, int *tech)
933 {
934     struct emp_qelem *qp;
935     struct emp_qelem *next;
936     struct plist *plp;
937
938     if (*tech == 0)
939         *tech = 9999;
940     for (qp = list->q_forw; qp != list; qp = next) {
941         next = qp->q_forw;
942         plp = (struct plist *)qp;
943
944         if (plp->plane.pln_x != x)
945             continue;
946         if (plp->plane.pln_y != y)
947             continue;
948
949         if (mission_pln_equip(plp, ip, flags, mission) < 0) {
950             emp_remque(qp);
951             free(qp);
952             continue;
953         }
954         if (flags & (P_S | P_I)) {
955             if (plp->pcp->pl_flags & P_S)
956                 mission_flags |= P_S;
957             if (plp->pcp->pl_flags & P_I)
958                 mission_flags |= P_I;
959         }
960         if (*tech > plp->plane.pln_tech)
961             *tech = plp->plane.pln_tech;
962         if (!(plp->pcp->pl_flags & P_H))
963             /* no stealth on this mission */
964             mission_flags &= ~P_H;
965         if (!(plp->pcp->pl_flags & P_X))
966             /* no stealth on this mission */
967             mission_flags &= ~P_X;
968         if (!(plp->pcp->pl_flags & P_A)) {
969             /* no asw on this mission */
970             mission_flags &= ~P_A;
971         }
972         if (!(plp->pcp->pl_flags & P_MINE)) {
973             /* no asw on this mission */
974             mission_flags &= ~P_MINE;
975         }
976
977         /*
978          *      Mob costs for missions are 1/2 normal
979          *       Not anymore. :)
980          */
981 /*      plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
982         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
983
984     }
985     return mission_flags;
986 }
987
988 int
989 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
990                   char mission)
991 {
992     struct plchrstr *pcp;
993     struct plnstr *pp;
994     int needed;
995     struct lndstr land;
996     struct shpstr ship;
997     struct sctstr sect;
998     i_type itype;
999     int rval;
1000     short *item;
1001
1002     pp = &plp->plane;
1003     pcp = plp->pcp;
1004     if (pp->pln_ship >= 0) {
1005         getship(pp->pln_ship, &ship);
1006         item = ship.shp_item;
1007     } else if (pp->pln_land >= 0) {
1008         getland(pp->pln_land, &land);
1009         item = land.lnd_item;
1010     } else {
1011         getsect(pp->pln_x, pp->pln_y, &sect);
1012         item = sect.sct_item;
1013     }
1014     if (pcp->pl_fuel > item[I_PETROL]) {
1015         return -1;
1016     }
1017     item[I_PETROL] -= pcp->pl_fuel;
1018     rval = 0;
1019     if (!(flags & P_F)) {
1020         itype = I_NONE;
1021         needed = 0;
1022         switch (mission) {
1023         case 's':
1024         case 'p':
1025             if (pp->pln_nuketype == -1) {
1026                 itype = I_SHELL;
1027                 needed = pp->pln_load;
1028             }
1029             break;
1030         case 't':
1031             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1032                 break;
1033             itype = ip->i_uid;
1034             needed = (pp->pln_load * 2) / ip->i_lbs;
1035             break;
1036         case 'd':
1037             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1038                 break;
1039             itype = ip->i_uid;
1040             needed = (pp->pln_load * 2) / ip->i_lbs;
1041             break;
1042         case 'a':
1043             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1044                 break;
1045             itype = I_MILIT;
1046             needed = pp->pln_load / ip->i_lbs;
1047             break;
1048         case 'n':
1049             if (pp->pln_nuketype == -1)
1050                 rval = -1;
1051             break;
1052         case 'i':               /* missile interception */
1053             if (pp->pln_load) {
1054                 itype = I_SHELL;
1055                 needed = pp->pln_load;
1056             }
1057             break;
1058         default:
1059             break;
1060         }
1061         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1062             return -1;
1063         }
1064         if (itype != I_NONE) {
1065             if (itype == I_SHELL && item[itype] < needed)
1066                 item[itype] += supply_commod(plp->plane.pln_own,
1067                                              plp->plane.pln_x,
1068                                              plp->plane.pln_y,
1069                                              I_SHELL, needed);
1070             if (item[itype] < needed)
1071                 return -1;
1072             item[itype] -= needed;
1073         }
1074         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1075             plp->bombs = needed;
1076         else
1077             plp->misc = needed;
1078     }
1079     if (pp->pln_ship >= 0)
1080         putship(ship.shp_uid, &ship);
1081     else if (pp->pln_land >= 0)
1082         putland(land.lnd_uid, &land);
1083     else
1084         putsect(&sect);
1085     return rval;
1086 }
1087
1088 /*
1089  *  Return 1 if this x,y pair is in the list
1090  */
1091 static int
1092 find_airport(struct emp_qelem *airp, coord x, coord y)
1093 {
1094     struct emp_qelem *qp;
1095     struct airport *a;
1096
1097     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1098         a = (struct airport *)qp;
1099         if ((a->x == x) && (a->y == y))
1100             return 1;
1101     }
1102
1103     return 0;
1104 }
1105
1106 /* #*# This needs to be changed to include acc's -KHS */
1107 static void
1108 add_airport(struct emp_qelem *airp, coord x, coord y)
1109 {
1110     struct airport *a;
1111     struct sctstr sect;
1112
1113     a = malloc(sizeof(struct airport));
1114
1115     a->x = x;
1116     a->y = y;
1117     getsect(x, y, &sect);
1118     a->own = sect.sct_own;
1119
1120     emp_insque((struct emp_qelem *)a, airp);
1121 }
1122
1123 /*
1124  *  Take all the planes in list 1 that
1125  *  are at x,y, and put them into list 2.
1126  */
1127 static void
1128 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1129 {
1130     struct emp_qelem *qp, *next;
1131     struct plist *plp;
1132
1133     for (qp = l1->q_forw; qp != l1; qp = next) {
1134         next = qp->q_forw;
1135         plp = (struct plist *)qp;
1136
1137         if (plp->plane.pln_x != x)
1138             continue;
1139         if (plp->plane.pln_y != y)
1140             continue;
1141
1142         emp_remque(qp);
1143         emp_insque(qp, l2);
1144     }
1145 }
1146
1147 static int
1148 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1149            natid victim, char *s, int hardtarget)
1150 {
1151     struct emp_qelem *qp;
1152     struct plist *plp;
1153     struct plnstr *pp;
1154     int newdam, dam = 0;
1155     int hitchance;
1156     int nukedam;
1157
1158     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1159         plp = (struct plist *)qp;
1160         pp = &plp->plane;
1161
1162         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1163             continue;
1164
1165         if (!plp->bombs)
1166             continue;
1167
1168         newdam = 0;
1169         if (plp->pcp->pl_flags & P_A) {
1170             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1171                 wu(0, pp->pln_own,
1172                    "\t%s detects sub movement in %s\n",
1173                    prplane(pp), xyas(x, y, pp->pln_own));
1174                 continue;
1175             }
1176             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1177                 wu(0, pp->pln_own,
1178                    "\t%s tracks %s %s at %s\n",
1179                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1180                 continue;
1181             }
1182             wu(0, pp->pln_own,
1183                "\t%s depth-charging %s %s in %s\n",
1184                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1185         } else {
1186             wu(0, pp->pln_own,
1187                "\t%s pinbombing %s %s in %s\n",
1188                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1189         }
1190         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1191         if (plp->plane.pln_nuketype != -1)
1192             hitchance = 100;
1193         else if (hardtarget != SECT_HARDTARGET)
1194             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1195         /* Always calculate damage */
1196         if (roll(100) <= hitchance) {
1197             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1198             if (nukedam) {
1199                 if (mission == MI_INTERDICT) {
1200                     wu(0, pp->pln_own,
1201                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1202                        prplane(pp), nukedam, cname(victim), s);
1203                     dam += nukedam;
1204                 }
1205             } else {
1206                 wu(0, pp->pln_own,
1207                    "\t\thit %s %s for %d damage\n",
1208                    cname(victim), s, newdam);
1209                 dam += newdam;
1210             }
1211         } else {
1212             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1213             wu(0, pp->pln_own, "missed\n");
1214             if (mission == MI_SINTERDICT) {
1215                 mpr(victim,
1216                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1217                     xyas(x, y, victim));
1218             } else if (*s == 's') {
1219                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1220                     s, xyas(x, y, victim));
1221             } else {
1222                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1223                     s, xyas(x, y, victim));
1224             }
1225             /* Now, even though we missed, the bombs
1226                land somewhere. */
1227             collateral_damage(x, y, newdam, bombers);
1228         }
1229
1230         /* use up missiles */
1231         if (plp->pcp->pl_flags & P_M)
1232             pp->pln_effic = 0;
1233     }
1234
1235     return dam;
1236 }
1237
1238 /*
1239  * Check to see if anyone hostile to the victim
1240  * is running an air defense mission on this
1241  * sector. If so, do air combat
1242  */
1243 int
1244 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1245             struct emp_qelem *esc_list)
1246 {
1247     int dam = 0, cn;
1248     int mission_flags, tech, combat = 0, rel, dist, z;
1249     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1250     struct plist *plp;
1251     struct genlist *glp;
1252     struct empobj *gp;
1253     struct genlist mi[MAXNOC];
1254     char buf[512];
1255     char *path;
1256     int count;
1257     int tcount;
1258
1259     count = 0;
1260     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1261         count++;
1262     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1263         count++;
1264
1265     memset(mi, 0, sizeof(mi));
1266     for (z = 1; z < MAXNOC; z++)
1267         emp_initque((struct emp_qelem *)&mi[z]);
1268
1269     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1270
1271     for (cn = 1; cn < MAXNOC; cn++) {
1272         /* Check our relations */
1273         rel = getrel(getnatp(cn), victim);
1274
1275         if (rel > HOSTILE)
1276             continue;
1277
1278         if (QEMPTY(&mi[cn].queue))
1279             continue;
1280
1281         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1282          * list from the mission creation.  This list must be deleted later. */
1283         emp_initque(&interceptors);
1284         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1285             next = qp->q_forw;
1286             glp = (struct genlist *)qp;
1287             gp = glp->thing;
1288             if (CANT_HAPPEN(gp->ef_type != EF_PLANE))
1289                 break;
1290
1291             dist = mapdist(x, y, gp->x, gp->y);
1292
1293             plp = malloc(sizeof(struct plist));
1294             memset(plp, 0, sizeof(struct plist));
1295             plp->pcp = glp->cp;
1296             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1297
1298             /* missiles go one way, so we can use all the range */
1299             if (!(plp->pcp->pl_flags & P_M))
1300                 dist *= 2;
1301             /* If it's out of range, free it and continue on */
1302             if (dist > plp->plane.pln_range) {
1303                 free(plp);
1304                 continue;
1305             }
1306             emp_insque(&plp->queue, &interceptors);
1307         }
1308
1309         /* Remove those who cannot go */
1310         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1311
1312         if (QEMPTY(&interceptors))
1313             continue;
1314
1315         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1316          * that the higher numbered planes go into battle (they should be the better ones
1317          * at fighting, if all went well.) */
1318         tcount = 0;
1319         for (qp = interceptors.q_forw; qp != (&interceptors);
1320              qp = qp->q_forw)
1321             tcount++;
1322         tcount -= count * 2;
1323         /* Just in case there are more incoming than we have */
1324         if (tcount < 0)
1325             tcount = 0;
1326         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1327             next = qp->q_forw;
1328             if (tcount) {
1329                 tcount--;
1330                 /* Free it up and continue */
1331                 emp_remque(qp);
1332                 glp = (struct genlist *)qp;
1333                 free(glp);
1334             }
1335         }
1336
1337         /* Now, make a list of all the airports these planes are coming from */
1338         emp_initque(&airp);
1339         for (qp = interceptors.q_forw; qp != (&interceptors);
1340              qp = qp->q_forw) {
1341             plp = (struct plist *)qp;
1342             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1343                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1344         }
1345
1346         /* Now, fly them out one airport at a time */
1347         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1348             struct airport *air;
1349
1350             air = (struct airport *)qp;
1351             dist = mapdist(x, y, air->x, air->y);
1352
1353             emp_initque(&i);
1354
1355             /* Split off the interceptors at this base into i */
1356             divide(&interceptors, &i, air->x, air->y);
1357
1358             tech = 0;
1359             mission_flags = 0;
1360             mission_flags |= P_X;       /* stealth (shhh) */
1361             /* gets turned off if not all choppers */
1362             mission_flags |= P_H;
1363             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1364             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1365
1366             /* Did we run out of interceptors? */
1367             if (QEMPTY(&i))
1368                 continue;
1369             /* Did we run out of bombers? */
1370             if (QEMPTY(bomb_list)) {
1371                 /* Yes, so we have to put the rest of the interceptors back, and
1372                    then continue, or we leak memory */
1373                 pln_put(&i);
1374                 continue;
1375             }
1376             mission_flags =
1377                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1378                                 mission_flags, &tech);
1379
1380             /* Did we run out of interceptors? */
1381             if (QEMPTY(&i))
1382                 continue;
1383             /* Did we run out of bombers? */
1384             if (QEMPTY(bomb_list)) {
1385                 /* Yes, so we have to put the rest of the interceptors back, and
1386                    then continue, or we leak memory */
1387                 pln_put(&i);
1388                 continue;
1389             }
1390
1391             path = BestAirPath(buf, air->x, air->y, x, y);
1392             if (CANT_HAPPEN(!path))
1393                 continue;
1394             wu(0, cn, "Flying %s mission from %s\n",
1395                mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1396             if (air->own && (air->own != cn)) {
1397                 wu(0, air->own, "%s is flying %s mission from %s\n",
1398                    cname(cn), mission_name(MI_AIR_DEFENSE),
1399                    xyas(air->x, air->y, air->own));
1400             }
1401
1402             /* Now, fly the planes to the sector */
1403             emp_initque(&empty);
1404             ac_encounter(&i, &empty, air->x, air->y,
1405                          path, mission_flags, 1, bomb_list, esc_list);
1406
1407             /* If none made it, continue */
1408             if (QEMPTY(&i))
1409                 continue;
1410
1411             /* Some made it, so now they get to try to fight. */
1412             /* Intercept the escorts first */
1413             combat = 0;
1414             if (!QEMPTY(esc_list)) {
1415                 mpr(victim, "%s air defense planes intercept!\n",
1416                     cname(cn));
1417                 ac_combat_headers(victim, cn);
1418                 ac_airtoair(esc_list, &i);
1419                 combat = 1;
1420             }
1421             /* Now intercept the bombers */
1422             if (!QEMPTY(bomb_list)) {
1423                 if (!combat) {
1424                     mpr(victim, "%s air defense planes intercept!\n",
1425                         cname(cn));
1426                     ac_combat_headers(victim, cn);
1427                 }
1428                 ac_airtoair(bomb_list, &i);
1429                 PR(cn, "\n");
1430                 PR(victim, "\n");
1431             }
1432
1433             pln_put(&i);
1434         }
1435     }
1436
1437     /* We have to free all of these, if they are still there, otherwise they get
1438        lost and we leak memory all over the place. */
1439     for (cn = 1; cn < MAXNOC; cn++) {
1440         /* free up all this memory if it's still there */
1441         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1442             next = qp->q_forw;
1443             glp = (struct genlist *)qp;
1444             free(glp->thing);
1445             free(glp);
1446         }
1447     }
1448
1449     return dam;
1450 }