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