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