]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
Fix ship interdiction targeting ships not at sea
[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 "empobj.h"
39 #include "file.h"
40 #include "item.h"
41 #include "misc.h"
42 #include "mission.h"
43 #include "nsc.h"
44 #include "optlist.h"
45 #include "path.h"
46 #include "player.h"
47 #include "prototypes.h"
48 #include "queue.h"
49 #include "xy.h"
50
51 struct genlist {
52     struct emp_qelem queue;     /* list of units */
53     void *cp;                   /* pointer to desc of thing */
54     struct empobj *thing;       /* thing's struct */
55 };
56
57 struct airport {
58     struct emp_qelem queue;
59     coord x, y;
60     natid own;
61 };
62
63 static void add_airport(struct emp_qelem *, coord, coord);
64 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
65                       char *, int);
66 static void build_mission_list(struct genlist *, coord, coord, int, natid);
67 static void build_mission_list_type(struct genlist *, coord, coord, int,
68                                     int, natid);
69 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
70 static int dosupport(struct genlist *, coord, coord, natid, natid);
71 static int find_airport(struct emp_qelem *, coord, coord);
72 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
73                            int, struct ichrstr *, int, int);
74 static void mission_pln_sel(struct emp_qelem *, int, int, int);
75 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
76                            char *, int);
77
78 /*
79  * Interdict commodities & transported planes
80  */
81 int
82 ground_interdict(coord x, coord y, natid victim, char *s)
83 {
84     int cn;
85     int dam = 0, newdam, rel;
86     struct genlist mi[MAXNOC];
87     int z;
88
89     memset(mi, 0, sizeof(mi));
90     for (z = 1; z < MAXNOC; z++)
91         emp_initque((struct emp_qelem *)&mi[z]);
92
93     build_mission_list(mi, x, y, MI_INTERDICT, victim);
94
95     for (cn = 1; cn < MAXNOC; cn++) {
96         rel = getrel(getnatp(cn), victim);
97         if (rel > HOSTILE)
98             continue;
99
100         if (QEMPTY(&mi[cn].queue))
101             continue;
102
103         newdam = perform_mission(x, y, victim, &mi[cn].queue,
104                                  MI_INTERDICT, s, SECT_HARDTARGET);
105         dam += newdam;
106         if (newdam)
107             mpr(victim, "%s interdiction mission does %d damage!\n",
108                 cname(cn), newdam);
109     }
110     if (dam) {
111         collateral_damage(x, y, dam);
112     }
113     return dam;
114 }
115
116 int
117 collateral_damage(coord x, coord y, int dam)
118 {
119     int coll;
120     struct sctstr sect;
121
122     if (!dam)
123         return 0;
124
125     getsect(x, y, &sect);
126     if (sect.sct_own) {
127         coll = ldround((double)dam * collateral_dam, 1);
128         if (coll == 0)
129             return 0;
130         mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
131             xyas(x, y, sect.sct_own), coll);
132         sectdamage(&sect, coll);
133         putsect(&sect);
134         return coll;
135     }
136     return 0;
137 }
138
139 static int
140 only_subs(struct emp_qelem *list)
141 {
142     struct emp_qelem *qp;
143     struct genlist *glp;
144     struct mchrstr *mcp;
145
146     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
147         glp = (struct genlist *)qp;
148
149         if (glp->thing->ef_type != EF_SHIP)
150             return 0;
151         mcp = glp->cp;
152         if (!(mcp->m_flags & M_SUB))
153             return 0;
154         /* It's a sub! */
155     }
156     /* They were all subs! */
157     return 1;
158 }
159
160
161 /*
162  *  Interdict ships & land units
163  */
164 int
165 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
166                int mission)
167 {
168     int cn;
169     int dam = 0, newdam;
170     struct genlist mi[MAXNOC];
171     int z;
172     int osubs;
173
174     memset(mi, 0, sizeof(mi));
175     for (z = 1; z < MAXNOC; z++)
176         emp_initque((struct emp_qelem *)&mi[z]);
177
178     build_mission_list(mi, x, y, mission, victim);
179
180     for (cn = 1; cn < MAXNOC; cn++) {
181         if (cn == victim)
182             continue;
183         if (mission == MI_SINTERDICT) {
184             if (getrel(getnatp(cn), victim) >= FRIENDLY)
185                 continue;
186         } else if (getrel(getnatp(cn), victim) > HOSTILE)
187             continue;
188
189         if (QEMPTY(&mi[cn].queue))
190             continue;
191
192         osubs = only_subs(&mi[cn].queue);
193         newdam = perform_mission(x, y, victim, &mi[cn].queue,
194                                  mission, s, hardtarget);
195         dam += newdam;
196         if (newdam) {
197             /* If only subs responded, then we don't know who's
198                subs they are */
199             mpr(victim, "%s interdiction mission does %d damage!\n",
200                 osubs ? "Enemy" : cname(cn), newdam);
201         }
202     }
203     if (dam) {
204         collateral_damage(x, y, dam);
205     }
206     return dam;
207 }
208
209 /*
210  *  Perform a mission against victim, on behalf of actee
211  */
212 int
213 off_support(coord x, coord y, natid victim, natid actee)
214 {
215     int dam = 0;
216     struct genlist mi[MAXNOC];
217     int z;
218
219     memset(mi, 0, sizeof(mi));
220     for (z = 1; z < MAXNOC; z++)
221         emp_initque((struct emp_qelem *)&mi[z]);
222
223     build_mission_list(mi, x, y, MI_SUPPORT, victim);
224     build_mission_list(mi, x, y, MI_OSUPPORT, victim);
225
226     dam = dosupport(mi, x, y, victim, actee);
227     return dam;
228 }
229
230 /*
231  *  Perform a mission against victim, on behalf of actee
232  */
233 int
234 def_support(coord x, coord y, natid victim, natid actee)
235 {
236     int dam = 0;
237     struct genlist mi[MAXNOC];
238     int z;
239
240     memset(mi, 0, sizeof(mi));
241     for (z = 1; z < MAXNOC; z++)
242         emp_initque((struct emp_qelem *)&mi[z]);
243
244     build_mission_list(mi, x, y, MI_SUPPORT, victim);
245     build_mission_list(mi, x, y, MI_DSUPPORT, victim);
246
247     dam = dosupport(mi, x, y, victim, actee);
248     return dam;
249 }
250
251 static int
252 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
253 {
254     int cn;
255     int rel;
256     int dam = 0;
257
258     for (cn = 1; cn < MAXNOC; cn++) {
259         rel = getrel(getnatp(cn), actee);
260         if ((cn != actee) && (rel != ALLIED))
261             continue;
262         rel = getrel(getnatp(cn), victim);
263         if ((cn != actee) && (rel != AT_WAR))
264             continue;
265
266         if (QEMPTY(&mi[cn].queue))
267             continue;
268
269         dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
270                                "", SECT_HARDTARGET);
271     }
272     return dam;
273 }
274
275 static void
276 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
277                    natid victim)
278 {
279     build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
280     build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
281     build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
282 }
283
284 static void
285 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
286                         int type, natid victim)
287 {
288     struct nstr_item ni;
289     struct genlist *glp;
290     struct empobj *gp;
291     union empobj_storage item;
292     int dist;
293     int radius;
294     int relat;
295     struct sctstr sect;
296
297     snxtitem_all(&ni, type);
298     while (nxtitem(&ni, &item)) {
299         gp = (struct empobj *)&item;
300
301         if (gp->own == 0)
302             continue;
303
304         if (gp->mobil < 1)
305             continue;
306
307         if ((gp->mission != mission) && (mission != MI_SINTERDICT))
308             continue;
309
310         if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
311             (gp->mission != MI_INTERDICT))
312             continue;
313
314         relat = getrel(getnatp(gp->own), victim);
315         if (mission == MI_SINTERDICT) {
316             if (relat >= FRIENDLY)
317                 continue;
318             else if (type != EF_PLANE && relat > HOSTILE)
319                 continue;
320         } else if (relat > HOSTILE)
321             continue;
322
323         dist = mapdist(x, y, gp->opx, gp->opy);
324
325         radius = gp->radius;
326         if (mission != MI_RESERVE)      /* XXX */
327             oprange(gp, &radius);
328
329         if (dist > radius)
330             continue;
331
332         /* Ok, it is within the operations range. */
333         /* Now check from where the object actually is */
334         dist = mapdist(x, y, gp->x, gp->y);
335         radius = 999;
336         oprange(gp, &radius);
337         if (dist > radius)
338             continue;
339         /* Ok, the object can get to where the x,y is */
340
341         if (opt_SLOW_WAR) {
342             if (mission != MI_AIR_DEFENSE) {
343                 getsect(x, y, &sect);
344                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
345
346                     /*
347                      * If the owner of the unit isn't at war
348                      * with the victim, and doesn't own the
349                      * sect being acted upon, and isn't the
350                      * old owner of that sect, bounce them.
351                      */
352                     if (sect.sct_type != SCT_WATER &&
353                         sect.sct_own != gp->own &&
354                         sect.sct_oldown != gp->own)
355                         continue;
356                 }
357             }
358         }
359
360         glp = malloc(sizeof(struct genlist));
361         memset(glp, 0, sizeof(struct genlist));
362         glp->cp = get_empobj_chr(gp);
363         glp->thing = malloc(sizeof(item));
364         memcpy(glp->thing, &item, sizeof(item));
365         emp_insque(&glp->queue, &mi[gp->own].queue);
366     }
367 }
368
369 static void
370 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
371 {
372     struct nstr_item ni;
373     struct plist *plp;
374     struct plnstr plane;
375     int dist;
376
377     snxtitem_all(&ni, EF_PLANE);
378     while (nxtitem(&ni, &plane)) {
379         if (plane.pln_own != cn)
380             continue;
381
382         if (plane.pln_mission != MI_ESCORT)
383             continue;
384
385         dist = mapdist(x, y, plane.pln_x, plane.pln_y);
386         if (dist > plane.pln_range / 2)
387             continue;
388
389         plp = malloc(sizeof(struct plist));
390         memset(plp, 0, sizeof(struct plist));
391         plp->pcp = &plchr[(int)plane.pln_type];
392         plp->plane = plane;
393         emp_insque(&plp->queue, escorts);
394     }
395 }
396
397 static int
398 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
399                 int mission, char *s, int hardtarget)
400 {
401     struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
402     struct emp_qelem *newqp;
403     struct genlist *glp;
404     struct plist *plp;
405     struct empobj *gp;
406     struct lndstr *lp;
407     struct shpstr *sp;
408     struct sctstr sect;
409     struct mchrstr *mcp;
410     struct plchrstr *pcp;
411     int dam = 0, dam2, mission_flags;
412     natid plane_owner = 0;
413     int md, range, air_dam = 0;
414     double prb, hitchance, vrange;
415     int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
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             /* Too ponderous for interdiction fire */
441             if (lchr[(int)lp->lnd_type].l_flags & L_HEAVY)
442                 continue;
443
444             range = roundrange(lnd_fire_range(lp));
445             if (md > range)
446                 continue;
447
448             lnd_unlimber(lp);
449             dam2 = lnd_fire(lp);
450             putland(lp->lnd_uid, lp);
451             if (dam2 < 0)
452                 continue;
453
454             if (targeting_ships) {
455                 if (chance(lnd_acc(lp) / 100.0))
456                     dam2 = ldround(dam2 / 2.0, 1);
457             }
458             dam += dam2;
459             if (targeting_ships)
460                 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
461             else
462                 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
463             wu(0, lp->lnd_own,
464                "%s fires at %s %s at %s\n",
465                prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
466
467             mpr(victim, "%s %s fires at you at %s\n",
468                 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
469         } else if (glp->thing->ef_type == EF_SHIP) {
470             sp = (struct shpstr *)glp->thing;
471             mcp = glp->cp;
472
473             if (((mission == MI_INTERDICT) ||
474                  (mission == MI_SINTERDICT)) &&
475                 (md > ship_max_interdiction_range))
476                 continue;
477 /*
478   if ((mcp->m_flags & M_SUB) &&
479   (sect.sct_type != SCT_WATER))
480   continue;
481 */
482             if (mission == MI_SINTERDICT) {
483                 if (!(mcp->m_flags & M_SONAR))
484                     continue;
485                 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
486                     continue;
487                 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
488                 vrange *= sp->shp_effic / 200.0;
489                 if (md > vrange)
490                     continue;
491                 /* can't look all the time */
492                 if (chance(0.5))
493                     continue;
494             }
495             if (mcp->m_flags & M_SUB) {
496                 if (!targeting_ships)
497                     continue;   /* subs interdict only ships */
498                 range = roundrange(torprange(sp));
499                 if (md > range)
500                     continue;
501                 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
502                     continue;
503                 dam2 = shp_torp(sp, 1);
504                 putship(sp->shp_uid, sp);
505                 if (dam2 < 0)
506                     continue;
507                 hitchance = shp_torp_hitchance(sp, md);
508
509                 wu(0, sp->shp_own,
510                    "%s locking on %s %s in %s\n",
511                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
512                 wu(0, sp->shp_own,
513                    "\tEffective torpedo range is %d.0\n", range);
514                 wu(0, sp->shp_own,
515                    "\tWhooosh... Hitchance = %d%%\n",
516                    (int)(hitchance * 100));
517
518                 if (!chance(hitchance)) {
519                     wu(0, sp->shp_own, "\tMissed\n");
520                     mpr(victim,
521                         "Incoming torpedo sighted @ %s missed (whew)!\n",
522                         xyas(x, y, victim));
523                     continue;
524                 }
525                 wu(0, sp->shp_own, "\tBOOM!...\n");
526                 dam += dam2;
527                 nreport(victim, N_TORP_SHIP, 0, 1);
528                 wu(0, sp->shp_own,
529                    "\tTorpedo hit %s %s for %d damage\n",
530                    cname(victim), s, dam2);
531
532                 mpr(victim,
533                     "Incoming torpedo sighted @ %s hits and does %d damage!\n",
534                     xyas(x, y, victim), dam2);
535             } else {
536                 range = roundrange(shp_fire_range(sp));
537                 if (md > range)
538                     continue;
539                 if (mission == MI_SINTERDICT)
540                     dam2 = shp_dchrg(sp);
541                 else
542                     dam2 = shp_fire(sp);
543                 putship(sp->shp_uid, sp);
544                 if (dam2 < 0)
545                     continue;
546                 if (range == 0.0)
547                     prb = 1.0;
548                 else
549                     prb = (double)md / range;
550                 prb *= prb;
551                 if (chance(prb))
552                     dam2 /= 2;
553                 dam += dam2;
554                 if (targeting_ships)
555                     nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
556                 else
557                     nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
558                 wu(0, sp->shp_own,
559                    "%s fires at %s %s at %s\n",
560                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
561
562                 mpr(victim, "%s %s fires at you at %s\n",
563                     cname(sp->shp_own), prship(sp), xyas(x, y, victim));
564             }
565         } else if (glp->thing->ef_type == EF_PLANE) {
566             pcp = glp->cp;
567             if (pcp->pl_flags & P_M)
568                 /* units have their own missile interdiction */
569                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
570                     continue;
571
572             /* save planes for later */
573             plp = malloc(sizeof(struct plist));
574
575             memset(plp, 0, sizeof(struct plist));
576             plp->pcp = pcp;
577             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
578             if (plp->pcp->pl_flags & P_M)
579                 emp_insque(&plp->queue, &missiles);
580             else
581                 emp_insque(&plp->queue, &bombers);
582             plane_owner = plp->plane.pln_own;
583         } else {
584             CANT_REACH();
585             break;
586         }
587     }
588     if (!QEMPTY(&missiles)) {
589         /* I arbitrarily chose 100 mindam -KHS */
590         dam +=
591             msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
592                               "sector", victim, mission);
593         qp = missiles.q_forw;
594         while (qp != (&missiles)) {
595             newqp = qp->q_forw;
596             emp_remque(qp);
597             free(qp);
598             qp = newqp;
599         }
600     }
601
602     if (QEMPTY(&bombers)) {
603         qp = list->q_forw;
604         while (qp != list) {
605             glp = (struct genlist *)qp;
606             qp = qp->q_forw;
607
608             free(glp->thing);
609             free(glp);
610         }
611         return dam;
612     }
613     /*
614      * If there are planes performing an
615      * interdict or support mission, find
616      * some escorts for them, if possible.
617      * Up to 2 per bomber, if possible.
618      */
619     find_escorts(x, y, plane_owner, &escorts);
620
621     if (mission == MI_SINTERDICT)
622         mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
623     else
624         mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
625
626     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
627
628     for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
629         plp = (struct plist *)qp;
630         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
631             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
632     }
633
634     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
635         struct airport *air;
636         char buf[512];
637         char *pp;
638
639         air = (struct airport *)qp;
640         md = mapdist(x, y, air->x, air->y);
641
642         emp_initque(&b);
643         emp_initque(&e);
644
645         /* Split off the bombers at this base into b */
646         divide(&bombers, &b, air->x, air->y);
647
648         /* Split off the escorts at this base into e */
649         divide(&escorts, &e, air->x, air->y);
650
651         mission_flags = 0;
652         mission_flags |= P_X;   /* stealth (shhh) */
653         mission_flags |= P_H;   /* gets turned off if not all choppers */
654
655         mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
656                                         0, mission_flags);
657
658         if (QEMPTY(&b))
659             continue;
660
661         mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
662                                         P_F | P_ESC, mission_flags);
663
664         pp = BestAirPath(buf, air->x, air->y, x, y);
665         if (CANT_HAPPEN(!pp))
666             continue;
667         wu(0, plane_owner, "Flying %s mission from %s\n",
668            mission_name(mission), xyas(air->x, air->y, plane_owner));
669         if (air->own && (air->own != plane_owner)) {
670             wu(0, air->own, "%s is flying %s mission from %s\n",
671                cname(plane_owner), mission_name(mission),
672                xyas(air->x, air->y, air->own));
673         }
674
675         ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0);
676
677         if (!QEMPTY(&b))
678             air_dam +=
679                 air_damage(&b, x, y, mission, victim, s, hardtarget);
680
681         pln_put(&b);
682         pln_put(&e);
683     }
684
685     if (air_dam > 0) {
686         dam += air_dam;
687         if (targeting_ships)
688             nreport(plane_owner, N_SHP_BOMB, victim, 1);
689         else
690             nreport(plane_owner, N_SCT_BOMB, victim, 1);
691     }
692
693     /* free up all this memory */
694     qp = list->q_forw;
695     while (qp != list) {
696         glp = (struct genlist *)qp;
697         qp = qp->q_forw;
698
699         free(glp->thing);
700         free(glp);
701     }
702
703     qp = escorts.q_forw;
704     while (qp != (&escorts)) {
705         newqp = qp->q_forw;
706         emp_remque(qp);
707         free(qp);
708         qp = newqp;
709     }
710
711     qp = bombers.q_forw;
712     while (qp != (&bombers)) {
713         newqp = qp->q_forw;
714         emp_remque(qp);
715         free(qp);
716         qp = newqp;
717     }
718
719     return dam;
720 }
721
722 int
723 cando(int mission, int type)
724 {
725     switch (mission) {
726     case MI_ESCORT:
727         if (type == EF_PLANE)
728             return 1;
729         return 0;
730     case MI_AIR_DEFENSE:
731         if (type == EF_PLANE)
732             return 1;
733         return 0;
734     case MI_SINTERDICT:
735         if ((type == EF_PLANE) || (type == EF_SHIP))
736             return 1;
737         return 0;
738     case MI_INTERDICT:
739         return 1;
740     case MI_SUPPORT:
741     case MI_OSUPPORT:
742     case MI_DSUPPORT:
743         if (type == EF_PLANE)
744             return 1;
745         return 0;
746     case MI_RESERVE:
747         if (type == EF_LAND)
748             return 1;
749         return 0;
750     }
751
752     return 0;
753 }
754
755 char *
756 mission_name(short mission)
757 {
758     switch (mission) {
759     case MI_INTERDICT:
760         return "an interdiction";
761     case MI_SUPPORT:
762         return "a support";
763     case MI_OSUPPORT:
764         return "a offensive support";
765     case MI_DSUPPORT:
766         return "a defensive support";
767     case MI_RESERVE:
768         return "a reserve";
769     case MI_ESCORT:
770         return "an escort";
771     case MI_SINTERDICT:
772         return "a sub interdiction";
773     case MI_AIR_DEFENSE:
774         return "an air defense";
775     }
776     return "a mysterious";
777 }
778
779 void
780 show_mission(int type, struct nstr_item *np)
781 {
782     int first = 1, radius;
783     union empobj_storage item;
784     struct empobj *gp;
785
786     while (nxtitem(np, &item)) {
787         gp = (struct empobj *)&item;
788         if (!player->owner || gp->own == 0)
789             continue;
790
791         if (first) {
792             pr("Thing                         x,y   op-sect rad mission\n");
793             first = 0;
794         }
795         pr("%-25s", obj_nameof(gp));
796         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
797         if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
798             gp->mission == MI_OSUPPORT ||
799             gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
800             radius = 999;
801             oprange(gp, &radius);
802             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
803             if (radius < gp->radius)
804                 pr("  %4d", radius);
805             else
806                 pr("  %4d", gp->radius);
807         } else if (gp->mission == MI_RESERVE) {
808             struct sctstr sect;
809             int plus = 2;
810
811             getsect(gp->x, gp->y, &sect);
812             if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
813                 plus++;
814
815             if (item.land.lnd_rad_max == 0)
816                 plus = 0;
817             else
818                 plus += item.land.lnd_rad_max;
819             prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
820             pr("  %4d", plus);
821         } else if (gp->mission == MI_ESCORT) {
822             pr("        ");
823             pr("  %4d", item.plane.pln_range / 2);
824         } else
825             pr("              ");
826         if (gp->mission)
827             pr(" is on %s mission\n", mission_name(gp->mission));
828         else
829             pr(" has no mission.\n");
830     }
831 }
832
833 int
834 oprange(struct empobj *gp, int *radius)
835 {
836     int range;
837
838     switch (gp->ef_type) {
839     case EF_SHIP:
840         range = ldround(shp_fire_range((struct shpstr *)gp), 1);
841         break;
842     case EF_LAND:
843         range = ldround(lnd_fire_range((struct lndstr *)gp), 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)
933 {
934     struct emp_qelem *qp;
935     struct emp_qelem *next;
936     struct plist *plp;
937     struct plnstr *pp;
938
939     for (qp = list->q_forw; qp != list; qp = next) {
940         next = qp->q_forw;
941         plp = (struct plist *)qp;
942         pp = &plp->plane;
943
944         if (pp->pln_x != x)
945             continue;
946         if (pp->pln_y != y)
947             continue;
948
949         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
950             || mission_pln_equip(plp, ip, flags, mission) < 0) {
951             emp_remque(qp);
952             free(qp);
953             continue;
954         }
955         if (flags & (P_S | P_I)) {
956             if (plp->pcp->pl_flags & P_S)
957                 mission_flags |= P_S;
958             if (plp->pcp->pl_flags & P_I)
959                 mission_flags |= P_I;
960         }
961         if (!(plp->pcp->pl_flags & P_H))
962             /* no stealth on this mission */
963             mission_flags &= ~P_H;
964         if (!(plp->pcp->pl_flags & P_X))
965             /* no stealth on this mission */
966             mission_flags &= ~P_X;
967         if (!(plp->pcp->pl_flags & P_A)) {
968             /* no asw on this mission */
969             mission_flags &= ~P_A;
970         }
971         if (!(plp->pcp->pl_flags & P_MINE)) {
972             /* no asw on this mission */
973             mission_flags &= ~P_MINE;
974         }
975
976         pp->pln_flags |= PLN_LAUNCHED;
977         pp->pln_mobil -= pln_mobcost(dist, pp, flags);
978         putplane(pp->pln_uid, pp);
979     }
980     return mission_flags;
981 }
982
983 int
984 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
985                   char mission)
986 {
987     struct plchrstr *pcp;
988     struct plnstr *pp;
989     int load, needed;
990     struct lndstr land;
991     struct shpstr ship;
992     struct sctstr sect;
993     i_type itype;
994     short *item;
995
996     pp = &plp->plane;
997     pcp = plp->pcp;
998     if (pp->pln_ship >= 0) {
999         getship(pp->pln_ship, &ship);
1000         item = ship.shp_item;
1001     } else if (pp->pln_land >= 0) {
1002         getland(pp->pln_land, &land);
1003         item = land.lnd_item;
1004     } else {
1005         getsect(pp->pln_x, pp->pln_y, &sect);
1006         item = sect.sct_item;
1007     }
1008     if (pcp->pl_fuel > item[I_PETROL]) {
1009         return -1;
1010     }
1011     item[I_PETROL] -= pcp->pl_fuel;
1012     if (!(flags & P_F)) {
1013         load = pln_load(pp);
1014         itype = I_NONE;
1015         needed = 0;
1016         switch (mission) {
1017         case 's':               /* strategic bomb */
1018         case 'p':               /* pinpoint bomb */
1019             if (pp->pln_nuketype == -1) {
1020                 itype = I_SHELL;
1021                 needed = load;
1022             }
1023             break;
1024         case 't':               /* transport */
1025         case 'd':               /* drop */
1026             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1027                 break;
1028             itype = ip->i_uid;
1029             needed = (load * 2) / ip->i_lbs;
1030             break;
1031         case 'a':               /* paradrop */
1032             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1033                 break;
1034             itype = I_MILIT;
1035             needed = load / ip->i_lbs;
1036             break;
1037         case 'i':               /* missile interception */
1038             if (load) {
1039                 itype = I_SHELL;
1040                 needed = load;
1041             }
1042             break;
1043         case 'r':               /* reconnaissance */
1044         case 0:                 /* plane interception */
1045             break;
1046         default:
1047             CANT_REACH();
1048             break;
1049         }
1050         if (itype != I_NONE && needed <= 0)
1051             return -1;
1052         if (itype != I_NONE) {
1053             if (itype == I_SHELL && item[itype] < needed)
1054                 item[itype] += supply_commod(plp->plane.pln_own,
1055                                              plp->plane.pln_x,
1056                                              plp->plane.pln_y,
1057                                              I_SHELL, needed);
1058             if (item[itype] < needed)
1059                 return -1;
1060             item[itype] -= needed;
1061         }
1062         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1063             plp->bombs = needed;
1064         else
1065             plp->misc = needed;
1066     }
1067     if (pp->pln_ship >= 0)
1068         putship(ship.shp_uid, &ship);
1069     else if (pp->pln_land >= 0)
1070         putland(land.lnd_uid, &land);
1071     else
1072         putsect(&sect);
1073     return 0;
1074 }
1075
1076 /*
1077  *  Return 1 if this x,y pair is in the list
1078  */
1079 static int
1080 find_airport(struct emp_qelem *airp, coord x, coord y)
1081 {
1082     struct emp_qelem *qp;
1083     struct airport *a;
1084
1085     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1086         a = (struct airport *)qp;
1087         if ((a->x == x) && (a->y == y))
1088             return 1;
1089     }
1090
1091     return 0;
1092 }
1093
1094 /* #*# This needs to be changed to include acc's -KHS */
1095 static void
1096 add_airport(struct emp_qelem *airp, coord x, coord y)
1097 {
1098     struct airport *a;
1099     struct sctstr sect;
1100
1101     a = malloc(sizeof(struct airport));
1102
1103     a->x = x;
1104     a->y = y;
1105     getsect(x, y, &sect);
1106     a->own = sect.sct_own;
1107
1108     emp_insque((struct emp_qelem *)a, airp);
1109 }
1110
1111 /*
1112  *  Take all the planes in list 1 that
1113  *  are at x,y, and put them into list 2.
1114  */
1115 static void
1116 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1117 {
1118     struct emp_qelem *qp, *next;
1119     struct plist *plp;
1120
1121     for (qp = l1->q_forw; qp != l1; qp = next) {
1122         next = qp->q_forw;
1123         plp = (struct plist *)qp;
1124
1125         if (plp->plane.pln_x != x)
1126             continue;
1127         if (plp->plane.pln_y != y)
1128             continue;
1129
1130         emp_remque(qp);
1131         emp_insque(qp, l2);
1132     }
1133 }
1134
1135 static int
1136 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1137            natid victim, char *s, int hardtarget)
1138 {
1139     struct emp_qelem *qp;
1140     struct plist *plp;
1141     struct plnstr *pp;
1142     int newdam, dam = 0;
1143     int hitchance;
1144     int nukedam;
1145
1146     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1147         plp = (struct plist *)qp;
1148         pp = &plp->plane;
1149
1150         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1151             continue;
1152
1153         if (!plp->bombs)
1154             continue;
1155
1156         newdam = 0;
1157         if (plp->pcp->pl_flags & P_A) {
1158             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1159                 wu(0, pp->pln_own,
1160                    "\t%s detects sub movement in %s\n",
1161                    prplane(pp), xyas(x, y, pp->pln_own));
1162                 continue;
1163             }
1164             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1165                 wu(0, pp->pln_own,
1166                    "\t%s tracks %s %s at %s\n",
1167                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1168                 continue;
1169             }
1170             wu(0, pp->pln_own,
1171                "\t%s depth-charging %s %s in %s\n",
1172                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1173         } else {
1174             wu(0, pp->pln_own,
1175                "\t%s pinbombing %s %s in %s\n",
1176                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1177         }
1178         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1179         if (plp->plane.pln_nuketype != -1)
1180             hitchance = 100;
1181         else if (hardtarget != SECT_HARDTARGET)
1182             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1183         /* Always calculate damage */
1184         if (roll(100) <= hitchance) {
1185             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1186             if (nukedam) {
1187                 if (mission == MI_INTERDICT) {
1188                     wu(0, pp->pln_own,
1189                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1190                        prplane(pp), nukedam, cname(victim), s);
1191                     dam += nukedam;
1192                 }
1193             } else {
1194                 wu(0, pp->pln_own,
1195                    "\t\thit %s %s for %d damage\n",
1196                    cname(victim), s, newdam);
1197                 dam += newdam;
1198             }
1199         } else {
1200             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1201             wu(0, pp->pln_own, "missed\n");
1202             if (mission == MI_SINTERDICT) {
1203                 mpr(victim,
1204                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1205                     xyas(x, y, victim));
1206             } else if (*s == 's') {
1207                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1208                     s, xyas(x, y, victim));
1209             } else {
1210                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1211                     s, xyas(x, y, victim));
1212             }
1213             /* Now, even though we missed, the bombs
1214                land somewhere. */
1215             collateral_damage(x, y, newdam);
1216         }
1217
1218         /* use up missiles */
1219         if (plp->pcp->pl_flags & P_M)
1220             pp->pln_effic = 0;
1221     }
1222
1223     return dam;
1224 }
1225
1226 /*
1227  * Check to see if anyone hostile to the victim
1228  * is running an air defense mission on this
1229  * sector. If so, do air combat
1230  */
1231 int
1232 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1233             struct emp_qelem *esc_list)
1234 {
1235     int dam = 0, cn;
1236     int mission_flags, combat = 0, rel, dist, z;
1237     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1238     struct plist *plp;
1239     struct genlist *glp;
1240     struct empobj *gp;
1241     struct genlist mi[MAXNOC];
1242     char buf[512];
1243     char *path;
1244     int count;
1245     int tcount;
1246
1247     count = 0;
1248     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1249         count++;
1250     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1251         count++;
1252
1253     memset(mi, 0, sizeof(mi));
1254     for (z = 1; z < MAXNOC; z++)
1255         emp_initque((struct emp_qelem *)&mi[z]);
1256
1257     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1258
1259     for (cn = 1; cn < MAXNOC; cn++) {
1260         /* Check our relations */
1261         rel = getrel(getnatp(cn), victim);
1262
1263         if (rel > HOSTILE)
1264             continue;
1265
1266         if (QEMPTY(&mi[cn].queue))
1267             continue;
1268
1269         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1270          * list from the mission creation.  This list must be deleted later. */
1271         emp_initque(&interceptors);
1272         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1273             next = qp->q_forw;
1274             glp = (struct genlist *)qp;
1275             gp = glp->thing;
1276             if (CANT_HAPPEN(gp->ef_type != EF_PLANE))
1277                 break;
1278
1279             dist = mapdist(x, y, gp->x, gp->y);
1280
1281             plp = malloc(sizeof(struct plist));
1282             memset(plp, 0, sizeof(struct plist));
1283             plp->pcp = glp->cp;
1284             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1285
1286             /* missiles go one way, so we can use all the range */
1287             if (!(plp->pcp->pl_flags & P_M))
1288                 dist *= 2;
1289             /* If it's out of range, free it and continue on */
1290             if (dist > plp->plane.pln_range) {
1291                 free(plp);
1292                 continue;
1293             }
1294             emp_insque(&plp->queue, &interceptors);
1295         }
1296
1297         /* Remove those who cannot go */
1298         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1299
1300         if (QEMPTY(&interceptors))
1301             continue;
1302
1303         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1304          * that the higher numbered planes go into battle (they should be the better ones
1305          * at fighting, if all went well.) */
1306         tcount = 0;
1307         for (qp = interceptors.q_forw; qp != (&interceptors);
1308              qp = qp->q_forw)
1309             tcount++;
1310         tcount -= count * 2;
1311         /* Just in case there are more incoming than we have */
1312         if (tcount < 0)
1313             tcount = 0;
1314         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1315             next = qp->q_forw;
1316             if (tcount) {
1317                 tcount--;
1318                 /* Free it up and continue */
1319                 emp_remque(qp);
1320                 glp = (struct genlist *)qp;
1321                 free(glp);
1322             }
1323         }
1324
1325         /* Now, make a list of all the airports these planes are coming from */
1326         emp_initque(&airp);
1327         for (qp = interceptors.q_forw; qp != (&interceptors);
1328              qp = qp->q_forw) {
1329             plp = (struct plist *)qp;
1330             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1331                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1332         }
1333
1334         /* Now, fly them out one airport at a time */
1335         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1336             struct airport *air;
1337
1338             air = (struct airport *)qp;
1339             dist = mapdist(x, y, air->x, air->y);
1340
1341             emp_initque(&i);
1342
1343             /* Split off the interceptors at this base into i */
1344             divide(&interceptors, &i, air->x, air->y);
1345
1346             mission_flags = 0;
1347             mission_flags |= P_X;       /* stealth (shhh) */
1348             /* gets turned off if not all choppers */
1349             mission_flags |= P_H;
1350             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1351             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1352
1353             /* Did we run out of interceptors? */
1354             if (QEMPTY(&i))
1355                 continue;
1356             /* Did we run out of bombers? */
1357             if (QEMPTY(bomb_list)) {
1358                 /* Yes, so we have to put the rest of the interceptors back, and
1359                    then continue, or we leak memory */
1360                 pln_put(&i);
1361                 continue;
1362             }
1363             mission_flags =
1364                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 0, 0, P_F,
1365                                 mission_flags);
1366
1367             /* Did we run out of interceptors? */
1368             if (QEMPTY(&i))
1369                 continue;
1370             /* Did we run out of bombers? */
1371             if (QEMPTY(bomb_list)) {
1372                 /* Yes, so we have to put the rest of the interceptors back, and
1373                    then continue, or we leak memory */
1374                 pln_put(&i);
1375                 continue;
1376             }
1377
1378             path = BestAirPath(buf, air->x, air->y, x, y);
1379             if (CANT_HAPPEN(!path)) {
1380                 pln_put(&i);
1381                 continue;
1382             }
1383             wu(0, cn, "Flying %s mission from %s\n",
1384                mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1385             if (air->own && (air->own != cn)) {
1386                 wu(0, air->own, "%s is flying %s mission from %s\n",
1387                    cname(cn), mission_name(MI_AIR_DEFENSE),
1388                    xyas(air->x, air->y, air->own));
1389             }
1390
1391             /* Now, fly the planes to the sector */
1392             emp_initque(&empty);
1393             ac_encounter(&i, &empty, air->x, air->y,
1394                          path, mission_flags, 1);
1395
1396             /* If none made it, continue */
1397             if (QEMPTY(&i))
1398                 continue;
1399
1400             /* Some made it, so now they get to try to fight. */
1401             /* Intercept the escorts first */
1402             combat = 0;
1403             if (!QEMPTY(esc_list)) {
1404                 mpr(victim, "%s air defense planes intercept!\n",
1405                     cname(cn));
1406                 ac_combat_headers(victim, cn);
1407                 ac_airtoair(esc_list, &i);
1408                 combat = 1;
1409             }
1410             /* Now intercept the bombers */
1411             if (!QEMPTY(bomb_list)) {
1412                 if (!combat) {
1413                     mpr(victim, "%s air defense planes intercept!\n",
1414                         cname(cn));
1415                     ac_combat_headers(victim, cn);
1416                 }
1417                 ac_airtoair(bomb_list, &i);
1418                 PR(cn, "\n");
1419                 PR(victim, "\n");
1420             }
1421
1422             pln_put(&i);
1423         }
1424         if (CANT_HAPPEN(!QEMPTY(&interceptors)))
1425             pln_put(&interceptors);
1426     }
1427
1428     /* We have to free all of these, if they are still there, otherwise they get
1429        lost and we leak memory all over the place. */
1430     for (cn = 1; cn < MAXNOC; cn++) {
1431         /* free up all this memory if it's still there */
1432         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1433             next = qp->q_forw;
1434             glp = (struct genlist *)qp;
1435             free(glp->thing);
1436             free(glp);
1437         }
1438     }
1439
1440     return dam;
1441 }