]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
f1aefe2c30bb8dc8c36e9a3124fac59f1fea4e66
[empserver] / src / lib / subs / mission.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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  *     Markus Armbruster, 2003-2009
34  */
35
36 #include <config.h>
37
38 #include <stdlib.h>
39 #include "empobj.h"
40 #include "file.h"
41 #include "item.h"
42 #include "misc.h"
43 #include "mission.h"
44 #include "nsc.h"
45 #include "optlist.h"
46 #include "path.h"
47 #include "player.h"
48 #include "prototypes.h"
49 #include "queue.h"
50 #include "xy.h"
51
52 struct genlist {
53     struct emp_qelem queue;     /* list of units */
54     struct empobj *thing;       /* thing's struct */
55 };
56
57 struct airport {
58     struct emp_qelem queue;
59     coord x, y;
60     natid own;
61 };
62
63 static void add_airport(struct emp_qelem *, coord, coord);
64 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
65                       char *, int);
66 static void build_mission_list(struct genlist *, coord, coord, int, natid);
67 static void build_mission_list_type(struct genlist *, coord, coord, int,
68                                     int, natid);
69 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
70 static int dosupport(struct genlist *, coord, coord, natid, natid);
71 static int find_airport(struct emp_qelem *, coord, coord);
72 static void mission_pln_arm(struct emp_qelem *, coord, coord, int,
73                             int, struct ichrstr *);
74 static void mission_pln_sel(struct emp_qelem *, int, int, int);
75 static int perform_mission_land(int, struct lndstr *, coord, coord,
76                                 natid, int, char *, int);
77 static int perform_mission_ship(int, struct shpstr *, coord, coord,
78                                 natid, int, char *, int);
79 static int perform_mission_msl(int, struct emp_qelem *, coord, coord,
80                                natid, int);
81 static int perform_mission_bomb(int, struct emp_qelem *, coord, coord,
82                                 natid, int, char *, int, int);
83 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
84                            char *, int);
85
86 /*
87  * Interdict commodities & transported planes
88  */
89 int
90 ground_interdict(coord x, coord y, natid victim, char *s)
91 {
92     int cn;
93     int dam = 0, newdam, rel;
94     struct genlist mi[MAXNOC];
95     int z;
96
97     memset(mi, 0, sizeof(mi));
98     for (z = 1; z < MAXNOC; z++)
99         emp_initque((struct emp_qelem *)&mi[z]);
100
101     build_mission_list(mi, x, y, MI_INTERDICT, victim);
102
103     for (cn = 1; cn < MAXNOC; cn++) {
104         rel = getrel(getnatp(cn), victim);
105         if (rel > HOSTILE)
106             continue;
107
108         if (QEMPTY(&mi[cn].queue))
109             continue;
110
111         newdam = perform_mission(x, y, victim, &mi[cn].queue,
112                                  MI_INTERDICT, s, SECT_HARDTARGET);
113         dam += newdam;
114         if (newdam)
115             mpr(victim, "%s interdiction mission does %d damage!\n",
116                 cname(cn), newdam);
117     }
118     if (dam) {
119         collateral_damage(x, y, dam);
120     }
121     return dam;
122 }
123
124 int
125 collateral_damage(coord x, coord y, int dam)
126 {
127     int coll;
128     struct sctstr sect;
129
130     if (!dam)
131         return 0;
132
133     getsect(x, y, &sect);
134     if (sect.sct_own) {
135         coll = ldround((double)dam * collateral_dam, 1);
136         if (coll == 0)
137             return 0;
138         mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
139             xyas(x, y, sect.sct_own), coll);
140         sectdamage(&sect, coll);
141         putsect(&sect);
142         return coll;
143     }
144     return 0;
145 }
146
147 static int
148 only_subs(struct emp_qelem *list)
149 {
150     struct emp_qelem *qp;
151     struct genlist *glp;
152
153     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
154         glp = (struct genlist *)qp;
155
156         if (glp->thing->ef_type != EF_SHIP)
157             return 0;
158         if (!(mchr[glp->thing->type].m_flags & M_SUB))
159             return 0;
160         /* It's a sub! */
161     }
162     /* They were all subs! */
163     return 1;
164 }
165
166
167 /*
168  *  Interdict ships & land units
169  */
170 int
171 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
172                int mission)
173 {
174     int cn;
175     int dam = 0, newdam;
176     struct genlist mi[MAXNOC];
177     int z;
178     int osubs;
179
180     memset(mi, 0, sizeof(mi));
181     for (z = 1; z < MAXNOC; z++)
182         emp_initque((struct emp_qelem *)&mi[z]);
183
184     build_mission_list(mi, x, y, mission, victim);
185
186     for (cn = 1; cn < MAXNOC; cn++) {
187         if (cn == victim)
188             continue;
189         if (mission == MI_SINTERDICT) {
190             if (getrel(getnatp(cn), victim) >= FRIENDLY)
191                 continue;
192         } else if (getrel(getnatp(cn), victim) > HOSTILE)
193             continue;
194
195         if (QEMPTY(&mi[cn].queue))
196             continue;
197
198         osubs = only_subs(&mi[cn].queue);
199         newdam = perform_mission(x, y, victim, &mi[cn].queue,
200                                  mission, s, hardtarget);
201         dam += newdam;
202         if (newdam) {
203             /* If only subs responded, then we don't know who's
204                subs they are */
205             mpr(victim, "%s interdiction mission does %d damage!\n",
206                 osubs ? "Enemy" : cname(cn), newdam);
207         }
208     }
209     if (dam) {
210         collateral_damage(x, y, dam);
211     }
212     return dam;
213 }
214
215 /*
216  *  Perform a mission against victim, on behalf of actee
217  */
218 int
219 off_support(coord x, coord y, natid victim, natid actee)
220 {
221     int dam = 0;
222     struct genlist mi[MAXNOC];
223     int z;
224
225     memset(mi, 0, sizeof(mi));
226     for (z = 1; z < MAXNOC; z++)
227         emp_initque((struct emp_qelem *)&mi[z]);
228
229     build_mission_list(mi, x, y, MI_SUPPORT, victim);
230     build_mission_list(mi, x, y, MI_OSUPPORT, victim);
231
232     dam = dosupport(mi, x, y, victim, actee);
233     return dam;
234 }
235
236 /*
237  *  Perform a mission against victim, on behalf of actee
238  */
239 int
240 def_support(coord x, coord y, natid victim, natid actee)
241 {
242     int dam = 0;
243     struct genlist mi[MAXNOC];
244     int z;
245
246     memset(mi, 0, sizeof(mi));
247     for (z = 1; z < MAXNOC; z++)
248         emp_initque((struct emp_qelem *)&mi[z]);
249
250     build_mission_list(mi, x, y, MI_SUPPORT, victim);
251     build_mission_list(mi, x, y, MI_DSUPPORT, victim);
252
253     dam = dosupport(mi, x, y, victim, actee);
254     return dam;
255 }
256
257 static int
258 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
259 {
260     int cn;
261     int rel;
262     int dam = 0;
263
264     for (cn = 1; cn < MAXNOC; cn++) {
265         rel = getrel(getnatp(cn), actee);
266         if ((cn != actee) && (rel != ALLIED))
267             continue;
268         rel = getrel(getnatp(cn), victim);
269         if ((cn != actee) && (rel != AT_WAR))
270             continue;
271
272         if (QEMPTY(&mi[cn].queue))
273             continue;
274
275         dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
276                                "", SECT_HARDTARGET);
277     }
278     return dam;
279 }
280
281 static void
282 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
283                    natid victim)
284 {
285     build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
286     build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
287     build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
288 }
289
290 static void
291 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
292                         int type, natid victim)
293 {
294     struct nstr_item ni;
295     struct genlist *glp;
296     struct empobj *gp;
297     union empobj_storage item;
298     int relat;
299     struct sctstr sect;
300
301     snxtitem_all(&ni, type);
302     while (nxtitem(&ni, &item)) {
303         gp = (struct empobj *)&item;
304
305         if (gp->own == 0)
306             continue;
307
308         if (gp->mobil < 1)
309             continue;
310
311         if ((gp->mission != mission) && (mission != MI_SINTERDICT))
312             continue;
313
314         if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
315             (gp->mission != MI_INTERDICT))
316             continue;
317
318         relat = getrel(getnatp(gp->own), victim);
319         if (mission == MI_SINTERDICT) {
320             if (relat >= FRIENDLY)
321                 continue;
322             else if (type != EF_PLANE && relat > HOSTILE)
323                 continue;
324         } else if (relat > HOSTILE)
325             continue;
326
327         if (!in_oparea(gp, x, y))
328             continue;
329
330         if (opt_SLOW_WAR) {
331             if (mission != MI_AIR_DEFENSE) {
332                 getsect(x, y, &sect);
333                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
334
335                     /*
336                      * If the owner of the unit isn't at war
337                      * with the victim, and doesn't own the
338                      * sect being acted upon, and isn't the
339                      * old owner of that sect, bounce them.
340                      */
341                     if (sect.sct_type != SCT_WATER &&
342                         sect.sct_own != gp->own &&
343                         sect.sct_oldown != gp->own)
344                         continue;
345                 }
346             }
347         }
348
349         glp = malloc(sizeof(struct genlist));
350         memset(glp, 0, sizeof(struct genlist));
351         glp->thing = malloc(sizeof(item));
352         memcpy(glp->thing, &item, sizeof(item));
353         emp_insque(&glp->queue, &mi[gp->own].queue);
354     }
355 }
356
357 static void
358 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
359 {
360     struct nstr_item ni;
361     struct plist *plp;
362     struct plnstr plane;
363
364     snxtitem_all(&ni, EF_PLANE);
365     while (nxtitem(&ni, &plane)) {
366         if (plane.pln_own != cn)
367             continue;
368         if (plane.pln_mission != MI_ESCORT)
369             continue;
370         if (!in_oparea((struct empobj *)&plane, x, y))
371             continue;
372         plp = malloc(sizeof(struct plist));
373         memset(plp, 0, sizeof(struct plist));
374         plp->pcp = &plchr[(int)plane.pln_type];
375         plp->plane = plane;
376         emp_insque(&plp->queue, escorts);
377     }
378 }
379
380 static int
381 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
382                 int mission, char *s, int hardtarget)
383 {
384     struct emp_qelem *qp, missiles, bombers;
385     struct genlist *glp;
386     struct plist *plp;
387     struct sctstr sect;
388     struct plchrstr *pcp;
389     int dam = 0;
390     int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
391
392     getsect(x, y, &sect);
393
394     emp_initque(&missiles);
395     emp_initque(&bombers);
396
397     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
398         glp = (struct genlist *)qp;
399
400         if (glp->thing->ef_type == EF_LAND) {
401             dam = perform_mission_land(dam, (struct lndstr *)glp->thing,
402                                        x, y, victim, mission, s,
403                                        targeting_ships);
404         } else if (glp->thing->ef_type == EF_SHIP) {
405             dam = perform_mission_ship(dam, (struct shpstr *)glp->thing,
406                                        x, y, victim, mission, s,
407                                        targeting_ships);
408         } else if (glp->thing->ef_type == EF_PLANE) {
409             pcp = &plchr[glp->thing->type];
410             if (pcp->pl_flags & P_M)
411                 /* units have their own missile interdiction */
412                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
413                     continue;
414
415             /* save planes for later */
416             plp = malloc(sizeof(struct plist));
417
418             memset(plp, 0, sizeof(struct plist));
419             plp->pcp = pcp;
420             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
421             if (plp->pcp->pl_flags & P_M)
422                 emp_insque(&plp->queue, &missiles);
423             else
424                 emp_insque(&plp->queue, &bombers);
425         } else {
426             CANT_REACH();
427             break;
428         }
429     }
430
431     dam = perform_mission_msl(dam, &missiles, x, y, victim, hardtarget);
432     dam = perform_mission_bomb(dam, &bombers, x, y, victim, mission, s,
433                                hardtarget, targeting_ships);
434
435     qp = list->q_forw;
436     while (qp != list) {
437         glp = (struct genlist *)qp;
438         qp = qp->q_forw;
439
440         free(glp->thing);
441         free(glp);
442     }
443
444     return dam;
445 }
446
447 static int
448 perform_mission_land(int dam, struct lndstr *lp, coord x, coord y,
449                      natid victim, int mission, char *s,
450                      int targeting_ships)
451 {
452     int md, range, dam2;
453
454     if (mission == MI_SINTERDICT)
455         return dam;
456
457     md = mapdist(x, y, lp->lnd_x, lp->lnd_y);
458
459     if (mission == MI_INTERDICT && md > land_max_interdiction_range)
460         return dam;
461
462     range = roundrange(lnd_fire_range(lp));
463     if (md > range)
464         return dam;
465
466     dam2 = lnd_fire(lp);
467     putland(lp->lnd_uid, lp);
468     if (dam2 < 0)
469         return dam;
470
471     if (targeting_ships) {
472         if (chance(lnd_acc(lp) / 100.0))
473             dam2 = ldround(dam2 / 2.0, 1);
474     }
475     if (targeting_ships)
476         nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
477     else
478         nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
479     wu(0, lp->lnd_own,
480        "%s fires at %s %s at %s\n",
481        prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
482
483     mpr(victim, "%s %s fires at you at %s\n",
484         cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
485
486     return dam + dam2;
487 }
488
489 static int
490 perform_mission_ship(int dam, struct shpstr *sp, coord x, coord y,
491                      natid victim, int mission, char *s,
492                      int targeting_ships)
493 {
494     struct mchrstr *mcp = &mchr[sp->shp_type];
495     double vrange, hitchance;
496     int md, range, dam2;
497
498     md = mapdist(x, y, sp->shp_x, sp->shp_y);
499
500     if ((mission == MI_INTERDICT || mission == MI_SINTERDICT)
501         && md > ship_max_interdiction_range)
502         return dam;
503
504     if (mission == MI_SINTERDICT) {
505         if (!(mcp->m_flags & M_SONAR))
506             return dam;
507         if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
508             return dam;
509         vrange = techfact(sp->shp_tech, mcp->m_vrnge);
510         vrange *= sp->shp_effic / 200.0;
511         if (md > vrange)
512             return dam;
513         /* can't look all the time */
514         if (chance(0.5))
515             return dam;
516     }
517     if (mcp->m_flags & M_SUB) {
518         if (!targeting_ships)
519             return dam; /* subs interdict only ships */
520         range = roundrange(torprange(sp));
521         if (md > range)
522             return dam;
523         if (!line_of_sight(NULL, x, y, sp->shp_x, sp->shp_y))
524             return dam;
525         dam2 = shp_torp(sp, 1);
526         putship(sp->shp_uid, sp);
527         if (dam2 < 0)
528             return dam;
529         hitchance = shp_torp_hitchance(sp, md);
530
531         wu(0, sp->shp_own,
532            "%s locking on %s %s in %s\n",
533            prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
534         wu(0, sp->shp_own,
535            "\tEffective torpedo range is %d.0\n", range);
536         wu(0, sp->shp_own,
537            "\tWhooosh... Hitchance = %d%%\n",
538            (int)(hitchance * 100));
539
540         if (!chance(hitchance)) {
541             wu(0, sp->shp_own, "\tMissed\n");
542             mpr(victim,
543                 "Incoming torpedo sighted @ %s missed (whew)!\n",
544                 xyas(x, y, victim));
545             return dam;
546         }
547         wu(0, sp->shp_own, "\tBOOM!...\n");
548         nreport(victim, N_TORP_SHIP, 0, 1);
549         wu(0, sp->shp_own,
550            "\tTorpedo hit %s %s for %d damage\n",
551            cname(victim), s, dam2);
552
553         mpr(victim,
554             "Incoming torpedo sighted @ %s hits and does %d damage!\n",
555             xyas(x, y, victim), dam2);
556     } else {
557         range = roundrange(shp_fire_range(sp));
558         if (md > range)
559             return dam;
560         if (mission == MI_SINTERDICT)
561             dam2 = shp_dchrg(sp);
562         else
563             dam2 = shp_fire(sp);
564         putship(sp->shp_uid, sp);
565         if (dam2 < 0)
566             return dam;
567         if (targeting_ships)
568             nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
569         else
570             nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
571         wu(0, sp->shp_own,
572            "%s fires at %s %s at %s\n",
573            prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
574
575         mpr(victim, "%s %s fires at you at %s\n",
576             cname(sp->shp_own), prship(sp), xyas(x, y, victim));
577     }
578
579     return dam + dam2;
580 }
581
582 static int
583 perform_mission_msl(int dam, struct emp_qelem *missiles, coord x, coord y,
584                     natid victim, int hardtarget)
585 {
586     int air_dam;
587     struct emp_qelem *qp, *newqp;
588     struct plist *plp;
589     int sublaunch, dam2;
590
591     /*
592      * Missiles, except for interdiction of ships or land units,
593      * because that happens elsewhere, in shp_missile_interdiction()
594      * and lnd_missile_interdiction().
595      */
596     air_dam = 0;
597     for (qp = missiles->q_back; qp != missiles; qp = newqp) {
598         newqp = qp->q_back;
599         plp = (struct plist *)qp;
600
601         if (air_dam < 100
602             && !CANT_HAPPEN(hardtarget != SECT_HARDTARGET
603                             || (plp->pcp->pl_flags & P_MAR))
604             && mission_pln_equip(plp, NULL, 'p') >= 0) {
605             if (msl_launch(&plp->plane, EF_SECTOR, "sector", x, y, victim,
606                            &sublaunch) < 0)
607                 goto use_up_msl;
608             if (!msl_hit(&plp->plane, SECT_HARDTARGET, EF_SECTOR,
609                          N_SCT_MISS, N_SCT_SMISS, sublaunch, victim))
610                 CANT_REACH();
611             dam2 = pln_damage(&plp->plane, 'p', 1);
612             air_dam += dam2;
613         use_up_msl:
614             plp->plane.pln_effic = 0;
615             putplane(plp->plane.pln_uid, &plp->plane);
616         }
617         emp_remque(qp);
618         free(qp);
619     }
620     return dam + air_dam;
621 }
622
623 static int
624 perform_mission_bomb(int dam, struct emp_qelem *bombers, coord x, coord y,
625                      natid victim, int mission, char *s, int hardtarget,
626                      int targeting_ships)
627 {
628     struct emp_qelem *qp, *newqp, escorts, airp, b, e;
629     struct plist *plp;
630     int plane_owner, air_dam, md;
631
632     emp_initque(&escorts);
633     emp_initque(&airp);
634
635     if (QEMPTY(bombers))
636         return dam;
637
638     plp = (struct plist *)bombers->q_forw;
639     plane_owner = plp->plane.pln_own;
640
641     /*
642      * If there are planes performing an
643      * interdict or support mission, find
644      * some escorts for them, if possible.
645      * Up to 2 per bomber, if possible.
646      */
647     find_escorts(x, y, plane_owner, &escorts);
648
649     if (mission == MI_SINTERDICT)
650         mission_pln_sel(bombers, P_T | P_A, 0, hardtarget);
651     else
652         mission_pln_sel(bombers, P_T, P_A, SECT_HARDTARGET);
653
654     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
655
656     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
657         plp = (struct plist *)qp;
658         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
659             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
660     }
661
662     air_dam = 0;
663     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
664         struct airport *air;
665         char buf[512];
666         char *pp;
667
668         air = (struct airport *)qp;
669         md = mapdist(x, y, air->x, air->y);
670
671         emp_initque(&b);
672         emp_initque(&e);
673
674         /* Split off the bombers at this base into b */
675         divide(bombers, &b, air->x, air->y);
676
677         /* Split off the escorts at this base into e */
678         divide(&escorts, &e, air->x, air->y);
679
680         mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', NULL);
681
682         if (QEMPTY(&b))
683             continue;
684
685         mission_pln_arm(&e, air->x, air->y, 2 * md, 'e', NULL);
686
687         pp = BestAirPath(buf, air->x, air->y, x, y);
688         if (CANT_HAPPEN(!pp))
689             continue;
690         wu(0, plane_owner, "Flying %s mission from %s to %s\n",
691            mission_name(mission),
692            xyas(air->x, air->y, plane_owner),
693            xyas(x, y, plane_owner));
694         if (air->own && (air->own != plane_owner)) {
695             wu(0, air->own, "%s is flying %s mission from %s to %s\n",
696                cname(plane_owner), mission_name(mission),
697                xyas(air->x, air->y, air->own),
698                xyas(x, y, air->own));
699         }
700
701         ac_encounter(&b, &e, air->x, air->y, pp, 0);
702
703         if (!QEMPTY(&b))
704             air_dam +=
705                 air_damage(&b, x, y, mission, victim, s, hardtarget);
706
707         pln_put(&b);
708         pln_put(&e);
709     }
710
711     if (air_dam > 0) {
712         if (targeting_ships)
713             nreport(plane_owner, N_SHP_BOMB, victim, 1);
714         else
715             nreport(plane_owner, N_SCT_BOMB, victim, 1);
716     }
717
718     qp = escorts.q_forw;
719     while (qp != (&escorts)) {
720         newqp = qp->q_forw;
721         emp_remque(qp);
722         free(qp);
723         qp = newqp;
724     }
725
726     qp = bombers->q_forw;
727     while (qp != bombers) {
728         newqp = qp->q_forw;
729         emp_remque(qp);
730         free(qp);
731         qp = newqp;
732     }
733
734     return dam + air_dam;
735 }
736
737 int
738 cando(int mission, int type)
739 {
740     switch (mission) {
741     case MI_ESCORT:
742         if (type == EF_PLANE)
743             return 1;
744         return 0;
745     case MI_AIR_DEFENSE:
746         if (type == EF_PLANE)
747             return 1;
748         return 0;
749     case MI_SINTERDICT:
750         if ((type == EF_PLANE) || (type == EF_SHIP))
751             return 1;
752         return 0;
753     case MI_INTERDICT:
754         return 1;
755     case MI_SUPPORT:
756     case MI_OSUPPORT:
757     case MI_DSUPPORT:
758         if (type == EF_PLANE)
759             return 1;
760         return 0;
761     case MI_RESERVE:
762         if (type == EF_LAND)
763             return 1;
764         return 0;
765     }
766
767     return 0;
768 }
769
770 char *
771 mission_name(int mission)
772 {
773     switch (mission) {
774     case MI_INTERDICT:
775         return "an interdiction";
776     case MI_SUPPORT:
777         return "a support";
778     case MI_OSUPPORT:
779         return "an offensive support";
780     case MI_DSUPPORT:
781         return "a defensive support";
782     case MI_RESERVE:
783         return "a reserve";
784     case MI_ESCORT:
785         return "an escort";
786     case MI_SINTERDICT:
787         return "a sub interdiction";
788     case MI_AIR_DEFENSE:
789         return "an air defense";
790     }
791     CANT_REACH();
792     return "a mysterious";
793 }
794
795 /*
796  * Maximum distance GP can perform its mission.
797  * Note: this has nothing to do with the radius of the op-area.
798  * oprange() governs where the unit *can* strike, the op-area governs
799  * where the player wants it to strike.
800  */
801 int
802 oprange(struct empobj *gp)
803 {
804     switch (gp->ef_type) {
805     case EF_SHIP:
806         return ldround(shp_fire_range((struct shpstr *)gp), 1);
807     case EF_LAND:
808         if (gp->mission == MI_RESERVE)
809             return lnd_reaction_range((struct lndstr *)gp);
810         return ldround(lnd_fire_range((struct lndstr *)gp), 1);
811     case EF_PLANE:
812         /* missiles go one way, so we can use all the range */
813         if (plchr[(int)gp->type].pl_flags & P_M)
814             return ((struct plnstr *)gp)->pln_range;
815         return ((struct plnstr *)gp)->pln_range / 2;
816     }
817     CANT_REACH();
818     return -1;
819 }
820
821 /*
822  * Does GP's mission op area cover X,Y?
823  */
824 int
825 in_oparea(struct empobj *gp, coord x, coord y)
826 {
827     return mapdist(x, y, gp->opx, gp->opy) <= gp->radius
828         && mapdist(x, y, gp->x, gp->y) <= oprange(gp);
829 }
830
831 /*
832  *  Remove all planes who cannot go on
833  *  the mission from the plane list.
834  */
835 static void
836 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
837                 int hardtarget)
838 {
839     struct emp_qelem *qp, *next;
840     struct plnstr *pp;
841     struct plchrstr *pcp;
842     struct plist *plp;
843
844     for (qp = list->q_forw; qp != list; qp = next) {
845         next = qp->q_forw;
846         plp = (struct plist *)qp;
847         pp = &plp->plane;
848         pcp = plp->pcp;
849
850         if (pp->pln_effic < 40) {
851             emp_remque(qp);
852             free(qp);
853             continue;
854         }
855
856         if (pp->pln_mobil < 1) {
857             emp_remque(qp);
858             free(qp);
859             continue;
860         }
861
862         if (opt_MARKET) {
863             if (ontradingblock(EF_PLANE, pp)) {
864                 emp_remque(qp);
865                 free(qp);
866                 continue;
867             }
868         }
869
870         if (!pln_capable(pp, wantflags, nowantflags)) {
871             emp_remque(qp);
872             free(qp);
873             continue;
874         }
875
876         if (!pln_airbase_ok(pp, 0, 0)) {
877             emp_remque(qp);
878             free(qp);
879             continue;
880         }
881
882         if (pcp->pl_flags & P_A) {
883             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
884                 emp_remque(qp);
885                 free(qp);
886                 continue;
887             }
888         }
889
890         putplane(pp->pln_uid, pp);
891     }
892 }
893
894 /*
895  * Arm only the planes at x,y
896  */
897 static void
898 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
899                 int mission, struct ichrstr *ip)
900 {
901     struct emp_qelem *qp;
902     struct emp_qelem *next;
903     struct plist *plp;
904     struct plnstr *pp;
905
906     for (qp = list->q_forw; qp != list; qp = next) {
907         next = qp->q_forw;
908         plp = (struct plist *)qp;
909         pp = &plp->plane;
910
911         if (pp->pln_x != x)
912             continue;
913         if (pp->pln_y != y)
914             continue;
915
916         if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
917             || mission_pln_equip(plp, ip, mission) < 0) {
918             emp_remque(qp);
919             free(qp);
920             continue;
921         }
922
923         pp->pln_flags |= PLN_LAUNCHED;
924         pp->pln_mobil -= pln_mobcost(dist, pp, mission);
925         putplane(pp->pln_uid, pp);
926     }
927 }
928
929 int
930 mission_pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
931 {
932     struct plchrstr *pcp;
933     struct plnstr *pp;
934     int load, needed;
935     struct lndstr land;
936     struct shpstr ship;
937     struct sctstr sect;
938     i_type itype;
939     short *item;
940
941     pp = &plp->plane;
942     pcp = plp->pcp;
943     if (pp->pln_ship >= 0) {
944         getship(pp->pln_ship, &ship);
945         item = ship.shp_item;
946     } else if (pp->pln_land >= 0) {
947         getland(pp->pln_land, &land);
948         item = land.lnd_item;
949     } else {
950         getsect(pp->pln_x, pp->pln_y, &sect);
951         item = sect.sct_item;
952     }
953     if (pcp->pl_fuel > item[I_PETROL]) {
954         return -1;
955     }
956     item[I_PETROL] -= pcp->pl_fuel;
957     load = pln_load(pp);
958     itype = I_NONE;
959     switch (mission) {
960     case 'p':           /* pinpoint bomb */
961         itype = I_SHELL;
962         break;
963     case 'i':           /* missile interception */
964         if (load)
965             itype = I_SHELL;
966         break;
967     case 'e':           /* escort */
968     case 0:             /* plane interception */
969         load = 0;
970         break;
971     default:
972         CANT_REACH();
973         load = 0;
974     }
975
976     if (itype != I_NONE) {
977         needed = load / ichr[itype].i_lbs;
978         if (needed <= 0)
979             return -1;
980         if (CANT_HAPPEN(nuk_on_plane(pp) >= 0))
981             return -1;
982         if (itype == I_SHELL && item[itype] < needed) {
983             if (pp->pln_ship >= 0)
984                 shp_supply(&ship, I_SHELL, needed);
985             else if (pp->pln_land >= 0)
986                 lnd_supply(&land, I_SHELL, needed);
987             else
988                 sct_supply(&sect, I_SHELL, needed);
989         }
990         if (item[itype] < needed)
991             return -1;
992         item[itype] -= needed;
993         plp->load = needed;
994     }
995
996     if (pp->pln_ship >= 0)
997         putship(ship.shp_uid, &ship);
998     else if (pp->pln_land >= 0)
999         putland(land.lnd_uid, &land);
1000     else
1001         putsect(&sect);
1002     return 0;
1003 }
1004
1005 /*
1006  *  Return 1 if this x,y pair is in the list
1007  */
1008 static int
1009 find_airport(struct emp_qelem *airp, coord x, coord y)
1010 {
1011     struct emp_qelem *qp;
1012     struct airport *a;
1013
1014     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1015         a = (struct airport *)qp;
1016         if ((a->x == x) && (a->y == y))
1017             return 1;
1018     }
1019
1020     return 0;
1021 }
1022
1023 /* #*# This needs to be changed to include acc's -KHS */
1024 static void
1025 add_airport(struct emp_qelem *airp, coord x, coord y)
1026 {
1027     struct airport *a;
1028     struct sctstr sect;
1029
1030     a = malloc(sizeof(struct airport));
1031
1032     a->x = x;
1033     a->y = y;
1034     getsect(x, y, &sect);
1035     a->own = sect.sct_own;
1036
1037     emp_insque((struct emp_qelem *)a, airp);
1038 }
1039
1040 /*
1041  *  Take all the planes in list 1 that
1042  *  are at x,y, and put them into list 2.
1043  */
1044 static void
1045 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1046 {
1047     struct emp_qelem *qp, *next;
1048     struct plist *plp;
1049
1050     for (qp = l1->q_forw; qp != l1; qp = next) {
1051         next = qp->q_forw;
1052         plp = (struct plist *)qp;
1053
1054         if (plp->plane.pln_x != x)
1055             continue;
1056         if (plp->plane.pln_y != y)
1057             continue;
1058
1059         emp_remque(qp);
1060         emp_insque(qp, l2);
1061     }
1062 }
1063
1064 static int
1065 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1066            natid victim, char *s, int hardtarget)
1067 {
1068     struct emp_qelem *qp;
1069     struct plist *plp;
1070     struct plnstr *pp;
1071     int newdam, dam = 0;
1072     int hitchance;
1073
1074     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1075         plp = (struct plist *)qp;
1076         pp = &plp->plane;
1077
1078         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1079             continue;
1080
1081         if (!plp->load)
1082             continue;
1083
1084         newdam = 0;
1085         if (plp->pcp->pl_flags & P_A) {
1086             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1087                 wu(0, pp->pln_own,
1088                    "\t%s detects sub movement in %s\n",
1089                    prplane(pp), xyas(x, y, pp->pln_own));
1090                 continue;
1091             }
1092             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1093                 wu(0, pp->pln_own,
1094                    "\t%s tracks %s %s at %s\n",
1095                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1096                 continue;
1097             }
1098             wu(0, pp->pln_own,
1099                "\t%s depth-charging %s %s in %s\n",
1100                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1101         } else {
1102             wu(0, pp->pln_own,
1103                "\t%s pinbombing %s %s in %s\n",
1104                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1105         }
1106         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1107         if (nuk_on_plane(&plp->plane) >= 0)
1108             hitchance = 100;
1109         else if (hardtarget != SECT_HARDTARGET)
1110             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1111         if (roll(100) <= hitchance) {
1112             newdam = pln_damage(&plp->plane, 'p', 1);
1113             wu(0, pp->pln_own,
1114                "\t\thit %s %s for %d damage\n",
1115                cname(victim), s, newdam);
1116             dam += newdam;
1117         } else {
1118             newdam = pln_damage(&plp->plane, 'p', 0);
1119             wu(0, pp->pln_own, "missed\n");
1120             if (mission == MI_SINTERDICT) {
1121                 mpr(victim,
1122                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1123                     xyas(x, y, victim));
1124             } else if (*s == 's') {
1125                 mpr(victim, "SPLASH!  Bombs miss your %s in %s\n",
1126                     s, xyas(x, y, victim));
1127             } else {
1128                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1129                     s, xyas(x, y, victim));
1130             }
1131             /* Now, even though we missed, the bombs
1132                land somewhere. */
1133             collateral_damage(x, y, newdam);
1134         }
1135
1136         /* use up missiles */
1137         if (plp->pcp->pl_flags & P_M)
1138             pp->pln_effic = 0;
1139     }
1140
1141     return dam;
1142 }