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