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