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