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