]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
Remove mission_pln_arm()'s argument tech
[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, 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;
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(lnd_acc(lp) / 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         mission_flags = 0;
648         mission_flags |= P_X;   /* stealth (shhh) */
649         mission_flags |= P_H;   /* gets turned off if not all choppers */
650
651         mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
652                                         0, mission_flags);
653
654         if (QEMPTY(&b))
655             continue;
656
657         mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
658                                         P_F | P_ESC, mission_flags);
659
660         pp = BestAirPath(buf, air->x, air->y, x, y);
661         if (CANT_HAPPEN(!pp))
662             continue;
663         wu(0, plane_owner, "Flying %s mission from %s\n",
664            mission_name(mission), xyas(air->x, air->y, plane_owner));
665         if (air->own && (air->own != plane_owner)) {
666             wu(0, air->own, "%s is flying %s mission from %s\n",
667                cname(plane_owner), mission_name(mission),
668                xyas(air->x, air->y, air->own));
669         }
670
671         ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
672
673         if (!QEMPTY(&b))
674             air_dam +=
675                 air_damage(&b, x, y, mission, victim, s, hardtarget);
676
677         pln_put(&b);
678         pln_put(&e);
679     }
680
681     if (air_dam > 0) {
682         dam += air_dam;
683         if (sect.sct_type == SCT_WATER)
684             nreport(plane_owner, N_SHP_BOMB, victim, 1);
685         else
686             nreport(plane_owner, N_SCT_BOMB, victim, 1);
687     }
688
689     /* free up all this memory */
690     qp = list->q_forw;
691     while (qp != list) {
692         glp = (struct genlist *)qp;
693         qp = qp->q_forw;
694
695         free(glp->thing);
696         free(glp);
697     }
698
699     qp = escorts.q_forw;
700     while (qp != (&escorts)) {
701         newqp = qp->q_forw;
702         emp_remque(qp);
703         free(qp);
704         qp = newqp;
705     }
706
707     qp = bombers.q_forw;
708     while (qp != (&bombers)) {
709         newqp = qp->q_forw;
710         emp_remque(qp);
711         free(qp);
712         qp = newqp;
713     }
714
715     return dam;
716 }
717
718 int
719 cando(int mission, int type)
720 {
721     switch (mission) {
722     case MI_ESCORT:
723         if (type == EF_PLANE)
724             return 1;
725         return 0;
726     case MI_AIR_DEFENSE:
727         if (type == EF_PLANE)
728             return 1;
729         return 0;
730     case MI_SINTERDICT:
731         if ((type == EF_PLANE) || (type == EF_SHIP))
732             return 1;
733         return 0;
734     case MI_INTERDICT:
735         return 1;
736     case MI_SUPPORT:
737     case MI_OSUPPORT:
738     case MI_DSUPPORT:
739         if (type == EF_PLANE)
740             return 1;
741         return 0;
742     case MI_RESERVE:
743         if (type == EF_LAND)
744             return 1;
745         return 0;
746     }
747
748     return 0;
749 }
750
751 char *
752 mission_name(short mission)
753 {
754     switch (mission) {
755     case MI_INTERDICT:
756         return "an interdiction";
757     case MI_SUPPORT:
758         return "a support";
759     case MI_OSUPPORT:
760         return "a offensive support";
761     case MI_DSUPPORT:
762         return "a defensive support";
763     case MI_RESERVE:
764         return "a reserve";
765     case MI_ESCORT:
766         return "an escort";
767     case MI_SINTERDICT:
768         return "a sub interdiction";
769     case MI_AIR_DEFENSE:
770         return "an air defense";
771     }
772     return "a mysterious";
773 }
774
775 void
776 show_mission(int type, struct nstr_item *np)
777 {
778     int first = 1, radius;
779     union empobj_storage item;
780     struct empobj *gp;
781
782     while (nxtitem(np, &item)) {
783         gp = (struct empobj *)&item;
784         if (!player->owner || gp->own == 0)
785             continue;
786
787         if (first) {
788             pr("Thing                         x,y   op-sect rad mission\n");
789             first = 0;
790         }
791         pr("%-25s", obj_nameof(gp));
792         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
793         if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
794             gp->mission == MI_OSUPPORT ||
795             gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
796             radius = 999;
797             oprange(gp, &radius);
798             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
799             if (radius < gp->radius)
800                 pr("  %4d", radius);
801             else
802                 pr("  %4d", gp->radius);
803         } else if (gp->mission == MI_RESERVE) {
804             struct sctstr sect;
805             int plus = 2;
806
807             getsect(gp->x, gp->y, &sect);
808             if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
809                 plus++;
810
811             if (item.land.lnd_rad_max == 0)
812                 plus = 0;
813             else
814                 plus += item.land.lnd_rad_max;
815             prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
816             pr("  %4d", plus);
817         } else if (gp->mission == MI_ESCORT) {
818             pr("        ");
819             pr("  %4d", item.plane.pln_range / 2);
820         } else
821             pr("              ");
822         if (gp->mission)
823             pr(" is on %s mission\n", mission_name(gp->mission));
824         else
825             pr(" has no mission.\n");
826     }
827 }
828
829 int
830 oprange(struct empobj *gp, int *radius)
831 {
832     int range;
833
834     switch (gp->ef_type) {
835     case EF_SHIP:
836         range = ldround(shp_fire_range((struct shpstr *)gp), 1);
837         break;
838     case EF_LAND:
839         range = ldround(lnd_fire_range((struct lndstr *)gp), 1);
840         break;
841     case EF_PLANE:
842         /* missiles go one way, so we can use all the range */
843         if (plchr[(int)gp->type].pl_flags & P_M)
844             range = ((struct plnstr *)gp)->pln_range;
845         else
846             range = ((struct plnstr *)gp)->pln_range / 2;
847         break;
848     default:
849         CANT_REACH();
850         range = -1;
851     }
852
853     if (*radius > range)
854         *radius = range;
855
856     return range;
857 }
858
859 /*
860  *  Remove all planes who cannot go on
861  *  the mission from the plane list.
862  */
863 static void
864 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
865                 int hardtarget)
866 {
867     struct emp_qelem *qp, *next;
868     struct plnstr *pp;
869     struct plchrstr *pcp;
870     struct plist *plp;
871
872     for (qp = list->q_forw; qp != list; qp = next) {
873         next = qp->q_forw;
874         plp = (struct plist *)qp;
875         pp = &plp->plane;
876         pcp = plp->pcp;
877
878         if (pp->pln_effic < 40) {
879             emp_remque(qp);
880             free(qp);
881             continue;
882         }
883
884         if (pp->pln_mobil < 1) {
885             emp_remque(qp);
886             free(qp);
887             continue;
888         }
889
890         if (opt_MARKET) {
891             if (ontradingblock(EF_PLANE, pp)) {
892                 emp_remque(qp);
893                 free(qp);
894                 continue;
895             }
896         }
897
898         if (!pln_capable(pp, wantflags, nowantflags)) {
899             emp_remque(qp);
900             free(qp);
901             continue;
902         }
903
904         if (!pln_airbase_ok(pp, 0, 0)) {
905             emp_remque(qp);
906             free(qp);
907             continue;
908         }
909             
910         if (pcp->pl_flags & P_A) {
911             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
912                 emp_remque(qp);
913                 free(qp);
914                 continue;
915             }
916         }
917
918         putplane(pp->pln_uid, pp);
919     }
920 }
921
922 /*
923  * Arm only the planes at x,y
924  */
925 static int
926 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
927                 int mission, struct ichrstr *ip, int flags,
928                 int mission_flags)
929 {
930     struct emp_qelem *qp;
931     struct emp_qelem *next;
932     struct plist *plp;
933
934     for (qp = list->q_forw; qp != list; qp = next) {
935         next = qp->q_forw;
936         plp = (struct plist *)qp;
937
938         if (plp->plane.pln_x != x)
939             continue;
940         if (plp->plane.pln_y != y)
941             continue;
942
943         if (mission_pln_equip(plp, ip, flags, mission) < 0) {
944             emp_remque(qp);
945             free(qp);
946             continue;
947         }
948         if (flags & (P_S | P_I)) {
949             if (plp->pcp->pl_flags & P_S)
950                 mission_flags |= P_S;
951             if (plp->pcp->pl_flags & P_I)
952                 mission_flags |= P_I;
953         }
954         if (!(plp->pcp->pl_flags & P_H))
955             /* no stealth on this mission */
956             mission_flags &= ~P_H;
957         if (!(plp->pcp->pl_flags & P_X))
958             /* no stealth on this mission */
959             mission_flags &= ~P_X;
960         if (!(plp->pcp->pl_flags & P_A)) {
961             /* no asw on this mission */
962             mission_flags &= ~P_A;
963         }
964         if (!(plp->pcp->pl_flags & P_MINE)) {
965             /* no asw on this mission */
966             mission_flags &= ~P_MINE;
967         }
968
969         /*
970          *      Mob costs for missions are 1/2 normal
971          *       Not anymore. :)
972          */
973 /*      plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
974         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
975
976     }
977     return mission_flags;
978 }
979
980 int
981 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
982                   char mission)
983 {
984     struct plchrstr *pcp;
985     struct plnstr *pp;
986     int load, needed;
987     struct lndstr land;
988     struct shpstr ship;
989     struct sctstr sect;
990     i_type itype;
991     int rval;
992     short *item;
993
994     pp = &plp->plane;
995     pcp = plp->pcp;
996     if (pp->pln_ship >= 0) {
997         getship(pp->pln_ship, &ship);
998         item = ship.shp_item;
999     } else if (pp->pln_land >= 0) {
1000         getland(pp->pln_land, &land);
1001         item = land.lnd_item;
1002     } else {
1003         getsect(pp->pln_x, pp->pln_y, &sect);
1004         item = sect.sct_item;
1005     }
1006     if (pcp->pl_fuel > item[I_PETROL]) {
1007         return -1;
1008     }
1009     item[I_PETROL] -= pcp->pl_fuel;
1010     rval = 0;
1011     if (!(flags & P_F)) {
1012         load = pln_load(pp);
1013         itype = I_NONE;
1014         needed = 0;
1015         switch (mission) {
1016         case 's':
1017         case 'p':
1018             if (pp->pln_nuketype == -1) {
1019                 itype = I_SHELL;
1020                 needed = load;
1021             }
1022             break;
1023         case 't':
1024             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1025                 break;
1026             itype = ip->i_uid;
1027             needed = (load * 2) / ip->i_lbs;
1028             break;
1029         case 'd':
1030             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1031                 break;
1032             itype = ip->i_uid;
1033             needed = (load * 2) / ip->i_lbs;
1034             break;
1035         case 'a':
1036             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1037                 break;
1038             itype = I_MILIT;
1039             needed = load / ip->i_lbs;
1040             break;
1041         case 'n':
1042             if (pp->pln_nuketype == -1)
1043                 rval = -1;
1044             break;
1045         case 'i':               /* missile interception */
1046             if (load) {
1047                 itype = I_SHELL;
1048                 needed = load;
1049             }
1050             break;
1051         default:
1052             break;
1053         }
1054         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1055             return -1;
1056         }
1057         if (itype != I_NONE) {
1058             if (itype == I_SHELL && item[itype] < needed)
1059                 item[itype] += supply_commod(plp->plane.pln_own,
1060                                              plp->plane.pln_x,
1061                                              plp->plane.pln_y,
1062                                              I_SHELL, needed);
1063             if (item[itype] < needed)
1064                 return -1;
1065             item[itype] -= needed;
1066         }
1067         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1068             plp->bombs = needed;
1069         else
1070             plp->misc = needed;
1071     }
1072     if (pp->pln_ship >= 0)
1073         putship(ship.shp_uid, &ship);
1074     else if (pp->pln_land >= 0)
1075         putland(land.lnd_uid, &land);
1076     else
1077         putsect(&sect);
1078     return rval;
1079 }
1080
1081 /*
1082  *  Return 1 if this x,y pair is in the list
1083  */
1084 static int
1085 find_airport(struct emp_qelem *airp, coord x, coord y)
1086 {
1087     struct emp_qelem *qp;
1088     struct airport *a;
1089
1090     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1091         a = (struct airport *)qp;
1092         if ((a->x == x) && (a->y == y))
1093             return 1;
1094     }
1095
1096     return 0;
1097 }
1098
1099 /* #*# This needs to be changed to include acc's -KHS */
1100 static void
1101 add_airport(struct emp_qelem *airp, coord x, coord y)
1102 {
1103     struct airport *a;
1104     struct sctstr sect;
1105
1106     a = malloc(sizeof(struct airport));
1107
1108     a->x = x;
1109     a->y = y;
1110     getsect(x, y, &sect);
1111     a->own = sect.sct_own;
1112
1113     emp_insque((struct emp_qelem *)a, airp);
1114 }
1115
1116 /*
1117  *  Take all the planes in list 1 that
1118  *  are at x,y, and put them into list 2.
1119  */
1120 static void
1121 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1122 {
1123     struct emp_qelem *qp, *next;
1124     struct plist *plp;
1125
1126     for (qp = l1->q_forw; qp != l1; qp = next) {
1127         next = qp->q_forw;
1128         plp = (struct plist *)qp;
1129
1130         if (plp->plane.pln_x != x)
1131             continue;
1132         if (plp->plane.pln_y != y)
1133             continue;
1134
1135         emp_remque(qp);
1136         emp_insque(qp, l2);
1137     }
1138 }
1139
1140 static int
1141 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1142            natid victim, char *s, int hardtarget)
1143 {
1144     struct emp_qelem *qp;
1145     struct plist *plp;
1146     struct plnstr *pp;
1147     int newdam, dam = 0;
1148     int hitchance;
1149     int nukedam;
1150
1151     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1152         plp = (struct plist *)qp;
1153         pp = &plp->plane;
1154
1155         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1156             continue;
1157
1158         if (!plp->bombs)
1159             continue;
1160
1161         newdam = 0;
1162         if (plp->pcp->pl_flags & P_A) {
1163             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1164                 wu(0, pp->pln_own,
1165                    "\t%s detects sub movement in %s\n",
1166                    prplane(pp), xyas(x, y, pp->pln_own));
1167                 continue;
1168             }
1169             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1170                 wu(0, pp->pln_own,
1171                    "\t%s tracks %s %s at %s\n",
1172                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1173                 continue;
1174             }
1175             wu(0, pp->pln_own,
1176                "\t%s depth-charging %s %s in %s\n",
1177                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1178         } else {
1179             wu(0, pp->pln_own,
1180                "\t%s pinbombing %s %s in %s\n",
1181                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1182         }
1183         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1184         if (plp->plane.pln_nuketype != -1)
1185             hitchance = 100;
1186         else if (hardtarget != SECT_HARDTARGET)
1187             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1188         /* Always calculate damage */
1189         if (roll(100) <= hitchance) {
1190             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1191             if (nukedam) {
1192                 if (mission == MI_INTERDICT) {
1193                     wu(0, pp->pln_own,
1194                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1195                        prplane(pp), nukedam, cname(victim), s);
1196                     dam += nukedam;
1197                 }
1198             } else {
1199                 wu(0, pp->pln_own,
1200                    "\t\thit %s %s for %d damage\n",
1201                    cname(victim), s, newdam);
1202                 dam += newdam;
1203             }
1204         } else {
1205             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1206             wu(0, pp->pln_own, "missed\n");
1207             if (mission == MI_SINTERDICT) {
1208                 mpr(victim,
1209                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1210                     xyas(x, y, victim));
1211             } else if (*s == 's') {
1212                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1213                     s, xyas(x, y, victim));
1214             } else {
1215                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1216                     s, xyas(x, y, victim));
1217             }
1218             /* Now, even though we missed, the bombs
1219                land somewhere. */
1220             collateral_damage(x, y, newdam, bombers);
1221         }
1222
1223         /* use up missiles */
1224         if (plp->pcp->pl_flags & P_M)
1225             pp->pln_effic = 0;
1226     }
1227
1228     return dam;
1229 }
1230
1231 /*
1232  * Check to see if anyone hostile to the victim
1233  * is running an air defense mission on this
1234  * sector. If so, do air combat
1235  */
1236 int
1237 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1238             struct emp_qelem *esc_list)
1239 {
1240     int dam = 0, cn;
1241     int mission_flags, combat = 0, rel, dist, z;
1242     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1243     struct plist *plp;
1244     struct genlist *glp;
1245     struct empobj *gp;
1246     struct genlist mi[MAXNOC];
1247     char buf[512];
1248     char *path;
1249     int count;
1250     int tcount;
1251
1252     count = 0;
1253     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1254         count++;
1255     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1256         count++;
1257
1258     memset(mi, 0, sizeof(mi));
1259     for (z = 1; z < MAXNOC; z++)
1260         emp_initque((struct emp_qelem *)&mi[z]);
1261
1262     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1263
1264     for (cn = 1; cn < MAXNOC; cn++) {
1265         /* Check our relations */
1266         rel = getrel(getnatp(cn), victim);
1267
1268         if (rel > HOSTILE)
1269             continue;
1270
1271         if (QEMPTY(&mi[cn].queue))
1272             continue;
1273
1274         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1275          * list from the mission creation.  This list must be deleted later. */
1276         emp_initque(&interceptors);
1277         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1278             next = qp->q_forw;
1279             glp = (struct genlist *)qp;
1280             gp = glp->thing;
1281             if (CANT_HAPPEN(gp->ef_type != EF_PLANE))
1282                 break;
1283
1284             dist = mapdist(x, y, gp->x, gp->y);
1285
1286             plp = malloc(sizeof(struct plist));
1287             memset(plp, 0, sizeof(struct plist));
1288             plp->pcp = glp->cp;
1289             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1290
1291             /* missiles go one way, so we can use all the range */
1292             if (!(plp->pcp->pl_flags & P_M))
1293                 dist *= 2;
1294             /* If it's out of range, free it and continue on */
1295             if (dist > plp->plane.pln_range) {
1296                 free(plp);
1297                 continue;
1298             }
1299             emp_insque(&plp->queue, &interceptors);
1300         }
1301
1302         /* Remove those who cannot go */
1303         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1304
1305         if (QEMPTY(&interceptors))
1306             continue;
1307
1308         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1309          * that the higher numbered planes go into battle (they should be the better ones
1310          * at fighting, if all went well.) */
1311         tcount = 0;
1312         for (qp = interceptors.q_forw; qp != (&interceptors);
1313              qp = qp->q_forw)
1314             tcount++;
1315         tcount -= count * 2;
1316         /* Just in case there are more incoming than we have */
1317         if (tcount < 0)
1318             tcount = 0;
1319         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1320             next = qp->q_forw;
1321             if (tcount) {
1322                 tcount--;
1323                 /* Free it up and continue */
1324                 emp_remque(qp);
1325                 glp = (struct genlist *)qp;
1326                 free(glp);
1327             }
1328         }
1329
1330         /* Now, make a list of all the airports these planes are coming from */
1331         emp_initque(&airp);
1332         for (qp = interceptors.q_forw; qp != (&interceptors);
1333              qp = qp->q_forw) {
1334             plp = (struct plist *)qp;
1335             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1336                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1337         }
1338
1339         /* Now, fly them out one airport at a time */
1340         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1341             struct airport *air;
1342
1343             air = (struct airport *)qp;
1344             dist = mapdist(x, y, air->x, air->y);
1345
1346             emp_initque(&i);
1347
1348             /* Split off the interceptors at this base into i */
1349             divide(&interceptors, &i, air->x, air->y);
1350
1351             mission_flags = 0;
1352             mission_flags |= P_X;       /* stealth (shhh) */
1353             /* gets turned off if not all choppers */
1354             mission_flags |= P_H;
1355             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1356             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1357
1358             /* Did we run out of interceptors? */
1359             if (QEMPTY(&i))
1360                 continue;
1361             /* Did we run out of bombers? */
1362             if (QEMPTY(bomb_list)) {
1363                 /* Yes, so we have to put the rest of the interceptors back, and
1364                    then continue, or we leak memory */
1365                 pln_put(&i);
1366                 continue;
1367             }
1368             mission_flags =
1369                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1370                                 mission_flags);
1371
1372             /* Did we run out of interceptors? */
1373             if (QEMPTY(&i))
1374                 continue;
1375             /* Did we run out of bombers? */
1376             if (QEMPTY(bomb_list)) {
1377                 /* Yes, so we have to put the rest of the interceptors back, and
1378                    then continue, or we leak memory */
1379                 pln_put(&i);
1380                 continue;
1381             }
1382
1383             path = BestAirPath(buf, air->x, air->y, x, y);
1384             if (CANT_HAPPEN(!path))
1385                 continue;
1386             wu(0, cn, "Flying %s mission from %s\n",
1387                mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1388             if (air->own && (air->own != cn)) {
1389                 wu(0, air->own, "%s is flying %s mission from %s\n",
1390                    cname(cn), mission_name(MI_AIR_DEFENSE),
1391                    xyas(air->x, air->y, air->own));
1392             }
1393
1394             /* Now, fly the planes to the sector */
1395             emp_initque(&empty);
1396             ac_encounter(&i, &empty, air->x, air->y,
1397                          path, mission_flags, 1, bomb_list, esc_list);
1398
1399             /* If none made it, continue */
1400             if (QEMPTY(&i))
1401                 continue;
1402
1403             /* Some made it, so now they get to try to fight. */
1404             /* Intercept the escorts first */
1405             combat = 0;
1406             if (!QEMPTY(esc_list)) {
1407                 mpr(victim, "%s air defense planes intercept!\n",
1408                     cname(cn));
1409                 ac_combat_headers(victim, cn);
1410                 ac_airtoair(esc_list, &i);
1411                 combat = 1;
1412             }
1413             /* Now intercept the bombers */
1414             if (!QEMPTY(bomb_list)) {
1415                 if (!combat) {
1416                     mpr(victim, "%s air defense planes intercept!\n",
1417                         cname(cn));
1418                     ac_combat_headers(victim, cn);
1419                 }
1420                 ac_airtoair(bomb_list, &i);
1421                 PR(cn, "\n");
1422                 PR(victim, "\n");
1423             }
1424
1425             pln_put(&i);
1426         }
1427     }
1428
1429     /* We have to free all of these, if they are still there, otherwise they get
1430        lost and we leak memory all over the place. */
1431     for (cn = 1; cn < MAXNOC; cn++) {
1432         /* free up all this memory if it's still there */
1433         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1434             next = qp->q_forw;
1435             glp = (struct genlist *)qp;
1436             free(glp->thing);
1437             free(glp);
1438         }
1439     }
1440
1441     return dam;
1442 }