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