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