]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mission.c
(cuto, show_mission): Fix justification of coordinate output.
[empserver] / src / lib / subs / mission.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
38 #include "player.h"
39 #include "xy.h"
40 #include "sect.h"
41 #include "ship.h"
42 #include "land.h"
43 #include "plane.h"
44 #include "nat.h"
45 #include "nsc.h"
46 #include "file.h"
47 #include "path.h"
48 #include "mission.h"
49 #include "genitem.h"
50 #include "news.h"
51 #include "item.h"
52 #include <fcntl.h>
53 #include "damage.h"
54 #include "prototypes.h"
55 #include "optlist.h"
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                       s_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, 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                            s_char *, int);
77
78 /*
79  * Interdict commodities & transported planes
80  */
81 int
82 ground_interdict(coord x, coord y, natid victim, s_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, 0);
112     }
113     return dam;
114 }
115
116 int
117 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
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, list);
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->type != EF_SHIP)
150             return 0;
151         mcp = (struct mchrstr *)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  */
165 int
166 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
167                int mission)
168 {
169     int cn;
170     int dam = 0, newdam;
171     struct genlist mi[MAXNOC];
172     int z;
173     int osubs;
174
175     memset(mi, 0, sizeof(mi));
176     for (z = 1; z < MAXNOC; z++)
177         emp_initque((struct emp_qelem *)&mi[z]);
178
179     build_mission_list(mi, x, y, mission, victim);
180
181     for (cn = 1; cn < MAXNOC; cn++) {
182         if (cn == victim)
183             continue;
184         if (mission == MI_SINTERDICT) {
185             if (getrel(getnatp(cn), victim) >= FRIENDLY)
186                 continue;
187         } else if (getrel(getnatp(cn), victim) > HOSTILE)
188             continue;
189
190         if (QEMPTY(&mi[cn].queue))
191             continue;
192
193         osubs = only_subs(&mi[cn].queue);
194         newdam = perform_mission(x, y, victim, &mi[cn].queue,
195                                  mission, s, hardtarget);
196         dam += newdam;
197         if (newdam) {
198             /* If only subs responded, then we don't know who's
199                subs they are */
200             if (osubs) {
201                 mpr(victim,
202                     "Enemy interdiction mission does %d damage!\n",
203                     newdam);
204             } else {
205                 mpr(victim,
206                     "%s interdiction mission does %d damage!\n",
207                     cname(cn), newdam);
208             }
209         }
210     }
211     if (dam) {
212         collateral_damage(x, y, dam, 0);
213     }
214     return dam;
215 }
216
217 /*
218  *  Perform a mission against victim, on behalf of actee
219  */
220 int
221 off_support(coord x, coord y, natid victim, natid actee)
222 {
223     int dam = 0;
224     struct genlist mi[MAXNOC];
225     int z;
226
227     memset(mi, 0, sizeof(mi));
228     for (z = 1; z < MAXNOC; z++)
229         emp_initque((struct emp_qelem *)&mi[z]);
230
231     build_mission_list(mi, x, y, MI_SUPPORT, victim);
232     build_mission_list(mi, x, y, MI_OSUPPORT, victim);
233
234     dam = dosupport(mi, x, y, victim, actee);
235     return dam;
236 }
237
238 /*
239  *  Perform a mission against victim, on behalf of actee
240  */
241 int
242 def_support(coord x, coord y, natid victim, natid actee)
243 {
244     int dam = 0;
245     struct genlist mi[MAXNOC];
246     int z;
247
248     memset(mi, 0, sizeof(mi));
249     for (z = 1; z < MAXNOC; z++)
250         emp_initque((struct emp_qelem *)&mi[z]);
251
252     build_mission_list(mi, x, y, MI_SUPPORT, victim);
253     build_mission_list(mi, x, y, MI_DSUPPORT, victim);
254
255     dam = dosupport(mi, x, y, victim, actee);
256     return dam;
257 }
258
259 static int
260 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
261 {
262     int cn;
263     int rel;
264     int dam = 0;
265
266     for (cn = 1; cn < MAXNOC; cn++) {
267         rel = getrel(getnatp(cn), actee);
268         if ((cn != actee) && (rel != ALLIED))
269             continue;
270         rel = getrel(getnatp(cn), victim);
271         if ((cn != actee) && (rel != AT_WAR))
272             continue;
273
274         if (QEMPTY(&mi[cn].queue))
275             continue;
276
277         dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
278                                "", SECT_HARDTARGET);
279     }
280     return dam;
281 }
282
283 static void
284 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
285                    natid victim)
286 {
287     build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
288     build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
289     build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
290 }
291
292 static void
293 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
294                         int type, natid victim)
295 {
296     struct nstr_item ni;
297     struct genlist *glp;
298     struct genitem *gp;
299     union {
300         struct shpstr u_sp;
301         struct lndstr u_lp;
302         struct plnstr u_pp;
303     } u_block;
304     s_char *block;
305     int dist, size;
306     int radius;
307     int relat;
308     struct sctstr sect;
309
310 /*
311         size = max(sizeof(struct shpstr),sizeof(struct lndstr));
312         size = max(size,sizeof(struct plnstr));
313         block = malloc(size);
314  */
315     size = sizeof(u_block);
316     block = (s_char *)&u_block;
317
318     snxtitem_all(&ni, type);
319     while (nxtitem(&ni, block)) {
320         gp = (struct genitem *)block;
321
322         if (gp->own == 0)
323             continue;
324
325         if (gp->mobil < 1)
326             continue;
327
328         if ((gp->mission != mission) && (mission != MI_SINTERDICT))
329             continue;
330
331         if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
332             (gp->mission != MI_INTERDICT))
333             continue;
334
335         relat = getrel(getnatp(gp->own), victim);
336         if (mission == MI_SINTERDICT) {
337             if (relat >= FRIENDLY)
338                 continue;
339             else if (type != EF_PLANE && relat > HOSTILE)
340                 continue;
341         } else if (relat > HOSTILE)
342             continue;
343
344         dist = mapdist(x, y, gp->opx, gp->opy);
345
346         radius = gp->radius;
347         if (mission != MI_RESERVE)      /* XXX */
348             oprange(gp, type, &radius);
349
350         if (dist > radius)
351             continue;
352
353         /* Ok, it is within the operations range. */
354         /* Now check from where the object actually is */
355         dist = mapdist(x, y, gp->x, gp->y);
356         radius = 999;
357         oprange(gp, type, &radius);
358         if (dist > radius)
359             continue;
360         /* Ok, the object can get to where the x,y is */
361
362         if (opt_SLOW_WAR) {
363             if (mission != MI_AIR_DEFENSE) {
364                 getsect(x, y, &sect);
365                 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
366
367                     /*
368                      * If the player->owner of the unit isn't at war
369                      * with the victim, and doesn't own the
370                      * sect being acted upon, and isn't the
371                      * old player->owner of that sect, bounce them.
372                      */
373                     if (sect.sct_type != SCT_WATER &&
374                         sect.sct_own != gp->own &&
375                         sect.sct_oldown != gp->own)
376                         continue;
377                 }
378             }
379         }
380
381         glp = malloc(sizeof(struct genlist));
382         memset(glp, 0, sizeof(struct genlist));
383         glp->x = gp->x;
384         glp->y = gp->y;
385         glp->type = type;
386         switch (type) {
387         case EF_LAND:
388             glp->cp = (s_char *)&lchr[(int)gp->type];
389             break;
390         case EF_SHIP:
391             glp->cp = (s_char *)&mchr[(int)gp->type];
392             break;
393         case EF_PLANE:
394             glp->cp = (s_char *)&plchr[(int)gp->type];
395             break;
396         }
397         glp->thing = malloc(size);
398         memcpy(glp->thing, block, size);
399         emp_insque(&glp->queue, &mi[gp->own].queue);
400     }
401 }
402
403 static void
404 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
405 {
406     struct nstr_item ni;
407     struct plist *plp;
408     struct plnstr plane;
409     int dist;
410
411     snxtitem_all(&ni, EF_PLANE);
412     while (nxtitem(&ni, &plane)) {
413         if (plane.pln_own != cn)
414             continue;
415
416         if (plane.pln_mission != MI_ESCORT)
417             continue;
418
419         dist = mapdist(x, y, plane.pln_x, plane.pln_y);
420
421         if (dist > ((int)((float)plane.pln_range / 2.0)))
422             continue;
423
424         plp = malloc(sizeof(struct plist));
425         memset(plp, 0, sizeof(struct plist));
426         plp->pcp = &plchr[(int)plane.pln_type];
427         plp->plane = plane;
428         emp_insque(&plp->queue, escorts);
429     }
430 }
431
432 static int
433 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
434                 int mission, s_char *s, int hardtarget)
435 {
436     struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
437     struct emp_qelem *newqp;
438     struct genlist *glp;
439     struct plist *plp;
440     struct genitem *gp;
441     struct lndstr *lp;
442     struct shpstr *sp;
443     struct sctstr sect;
444     struct mchrstr *mcp;
445     struct plchrstr *pcp;
446     int dam = 0, dam2, mission_flags, tech;
447     natid plane_owner = 0;
448     int gun, shell, md, air_dam = 0;
449     double range2, prb, range, mobcost, hitchance;
450
451     getsect(x, y, &sect);
452
453     emp_initque(&missiles);
454     emp_initque(&bombers);
455     emp_initque(&escorts);
456     emp_initque(&airp);
457
458     for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
459         glp = (struct genlist *)qp;
460         gp = (struct genitem *)glp->thing;
461
462         md = mapdist(x, y, gp->x, gp->y);
463
464         if (glp->type == EF_LAND) {
465             lp = (struct lndstr *)glp->thing;
466
467             if (lp->lnd_effic < LAND_MINFIREEFF)
468                 continue;
469
470             if (mission == MI_SINTERDICT)
471                 continue;
472
473             if ((mission == MI_INTERDICT) &&
474                 (md > land_max_interdiction_range))
475                 continue;
476
477             if (md > (lp->lnd_frg / 2))
478                 continue;
479
480             if (lnd_getmil(lp) < 1)
481                 continue;
482
483             range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
484             range2 = (double)roundrange(range);
485
486             if (md > range2)
487                 continue;
488
489             shell = lp->lnd_item[I_SHELL];
490             gun = lp->lnd_item[I_GUN];
491             if (shell == 0 || gun == 0)
492                 continue;
493
494             if (has_supply(lp)) {
495                 use_supply(lp);
496                 putland(lp->lnd_uid, lp);
497                 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
498                                            lp->lnd_ammo, shell), 1);
499                 if (sect.sct_type == SCT_WATER) {
500                     double dam3 = (double)dam2;
501                     if (chance(((double)lp->lnd_acc) / 100.0))
502                         dam2 = ldround((dam3 / 2.0), 1);
503                 }
504                 dam += dam2;
505                 if (sect.sct_type == SCT_WATER)
506                     nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
507                 else
508                     nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
509                 wu(0, lp->lnd_own,
510                    "%s fires at %s %s at %s\n",
511                    prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
512
513                 mpr(victim, "%s %s fires at you at %s\n",
514                     cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
515             }
516         } else if (glp->type == EF_SHIP) {
517             sp = (struct shpstr *)glp->thing;
518             mcp = (struct mchrstr *)glp->cp;
519
520             if (sp->shp_effic < 60)
521                 continue;
522             if (sp->shp_frnge == 0)
523                 continue;
524             if (((mission == MI_INTERDICT) ||
525                  (mission == MI_SINTERDICT)) &&
526                 (md > ship_max_interdiction_range))
527                 continue;
528             if (sp->shp_item[I_MILIT] < 1)
529                 continue;
530 /*
531   if ((mcp->m_flags & M_SUB) &&
532   (sect.sct_type != SCT_WATER))
533   continue;
534 */
535             if (mission == MI_SINTERDICT) {
536                 if (!(mcp->m_flags & M_SONAR))
537                     continue;
538                 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
539                     continue;
540                 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
541                 range2 *= (double)sp->shp_effic / 200.0;
542                 if (md > range2)
543                     continue;
544                 /* can't look all the time */
545                 if (chance(0.5))
546                     continue;
547             }
548             if (mcp->m_flags & M_SUB) {
549 /* If we aren't shooting at "subs" or "ships" don't fire at all from
550    a sub. */
551                 if (*s != 's')
552                     continue;
553                 if (sp->shp_mobil < 0)
554                     continue;
555                 gun = sp->shp_item[I_GUN];
556                 if (gun < 1)
557                     continue;
558                 shell = sp->shp_item[I_SHELL];
559                 if (shell < SHP_TORP_SHELLS)
560                     shell += supply_commod(sp->shp_own,
561                                            sp->shp_x, sp->shp_y, I_SHELL,
562                                            SHP_TORP_SHELLS - shell);
563                 if (shell < SHP_TORP_SHELLS)
564                     continue;
565
566                 range = sp->shp_effic * techfact(sp->shp_tech,
567                                                  ((double)sp->shp_frnge)) /
568                     100.0;
569
570                 range2 = (double)roundrange(range);
571                 if (md > range)
572                     continue;
573
574                 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
575                     continue;
576                 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
577                 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
578                 mobcost = (480.0 / (mobcost +
579                                     techfact(sp->shp_tech, mobcost)));
580                 sp->shp_mobil -= mobcost;
581                 putship(sp->shp_uid, sp);
582                 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
583
584                 wu(0, sp->shp_own,
585                    "%s locking on %s %s in %s\n",
586                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
587                 wu(0, sp->shp_own,
588                    "\tEffective torpedo range is %.1f\n", range);
589                 wu(0, sp->shp_own,
590                    "\tWhooosh... Hitchance = %d%%\n",
591                    (int)(hitchance * 100));
592
593                 if (hitchance < 1.0 && !chance(hitchance)) {
594                     wu(0, sp->shp_own, "\tMissed\n");
595                     mpr(victim,
596                         "Incoming torpedo sighted @ %s missed (whew)!\n",
597                         xyas(x, y, victim));
598                     continue;
599                 }
600                 wu(0, sp->shp_own, "\tBOOM!...\n");
601                 dam2 = TORP_DAMAGE();
602
603                 dam += dam2;
604                 nreport(victim, N_TORP_SHIP, 0, 1);
605                 wu(0, sp->shp_own,
606                    "\tTorpedo hit %s %s for %d damage\n",
607                    cname(victim), s, dam2);
608
609                 mpr(victim,
610                     "Incoming torpedo sighted @ %s hits and does %d damage!\n",
611                     xyas(x, y, victim), dam2);
612             } else {
613                 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
614                 range2 = (double)roundrange(range);
615                 if (md > range2)
616                     continue;
617                 gun = sp->shp_item[I_GUN];
618                 gun = min(gun, sp->shp_glim);
619                 shell = sp->shp_item[I_SHELL];
620                 if (shell < gun)
621                     shell += supply_commod(sp->shp_own,
622                                            sp->shp_x, sp->shp_y, I_SHELL,
623                                            gun - shell);
624                 gun = min(gun, shell);
625                 gun = min(gun, sp->shp_item[I_MILIT] / 2.0);
626                 if (gun == 0)
627                     continue;
628                 gun = max(gun, 1);
629                 dam2 = seagun(sp->shp_effic, gun);
630                 if (range2 == 0.0)
631                     prb = 1.0;
632                 else
633                     prb = ((double)md) / range2;
634                 prb *= prb;
635                 if (chance(prb))
636                     dam2 = (int)((float)dam2 / 2.0);
637                 dam += dam2;
638                 if (sect.sct_type == SCT_WATER)
639                     nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
640                 else
641                     nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
642                 wu(0, sp->shp_own,
643                    "%s fires at %s %s at %s\n",
644                    prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
645
646                 mpr(victim, "%s %s fires at you at %s\n",
647                     cname(sp->shp_own), prship(sp), xyas(x, y, victim));
648
649                 sp->shp_item[I_SHELL] = shell - gun;
650                 putship(sp->shp_uid, sp);
651             }
652         } else if (glp->type == EF_PLANE) {
653             pcp = (struct plchrstr *)glp->cp;
654             if (pcp->pl_flags & P_M)
655                 /* units have their own missile interdiction */
656                 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
657                     continue;
658
659             /* save planes for later */
660             plp = malloc(sizeof(struct plist));
661
662             memset(plp, 0, sizeof(struct plist));
663             plp->pcp = pcp;
664             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
665             if (plp->pcp->pl_flags & P_M)
666                 emp_insque(&plp->queue, &missiles);
667             else
668                 emp_insque(&plp->queue, &bombers);
669             plane_owner = plp->plane.pln_own;
670         }
671     }
672     if (!QEMPTY(&missiles)) {
673         /* I arbitrarily chose 100 mindam -KHS */
674         dam +=
675             msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
676                               "sector", victim, mission);
677         qp = missiles.q_forw;
678         while (qp != (&missiles)) {
679             newqp = qp->q_forw;
680             emp_remque(qp);
681             free(qp);
682             qp = newqp;
683         }
684     }
685
686     if (QEMPTY(&bombers)) {
687         qp = list->q_forw;
688         while (qp != list) {
689             glp = (struct genlist *)qp;
690             qp = qp->q_forw;
691
692             free(glp->thing);
693             free(glp);
694         }
695         return dam;
696     }
697     /*
698      * If there are planes performing an
699      * interdict or support mission, find
700      * some escorts for them, if possible.
701      * Up to 2 per bomber, if possible.
702      */
703     find_escorts(x, y, plane_owner, &escorts);
704
705     if (mission == MI_SINTERDICT)
706         mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
707     else
708         mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
709
710     mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
711
712     for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
713         plp = (struct plist *)qp;
714         if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
715             add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
716     }
717
718     for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
719         struct airport *air;
720         s_char pp[512];
721
722         air = (struct airport *)qp;
723         md = mapdist(x, y, air->x, air->y);
724
725         emp_initque(&b);
726         emp_initque(&e);
727
728         /* Split off the bombers at this base into b */
729         divide(&bombers, &b, air->x, air->y);
730
731         /* Split off the escorts at this base into e */
732         divide(&escorts, &e, air->x, air->y);
733
734         tech = 0;
735         mission_flags = 0;
736         mission_flags |= P_X;   /* stealth (shhh) */
737         mission_flags |= P_H;   /* gets turned off if not all choppers */
738
739         mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
740                                         0, mission_flags, &tech);
741
742         if (QEMPTY(&b)) {
743             continue;
744         }
745
746         mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
747                                         P_F | P_ESC, mission_flags, &tech);
748
749         BestAirPath(pp, air->x, air->y, x, y);
750         wu(0, plane_owner, "Flying %s mission from %s\n",
751            mission_name(mission), xyas(air->x, air->y, plane_owner));
752         if (air->own && (air->own != plane_owner)) {
753             wu(0, air->own, "%s is flying %s mission from %s\n",
754                cname(plane_owner), mission_name(mission),
755                xyas(air->x, air->y, air->own));
756         }
757
758         ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
759
760         if (!QEMPTY(&b))
761             air_dam +=
762                 air_damage(&b, x, y, mission, victim, s, hardtarget);
763
764         pln_put(&b);
765         pln_put(&e);
766     }
767
768     if (air_dam > 0) {
769         dam += air_dam;
770         if (sect.sct_type == SCT_WATER)
771             nreport(plane_owner, N_SHP_BOMB, victim, 1);
772         else
773             nreport(plane_owner, N_SCT_BOMB, victim, 1);
774     }
775
776     /* free up all this memory */
777     qp = list->q_forw;
778     while (qp != list) {
779         glp = (struct genlist *)qp;
780         qp = qp->q_forw;
781
782         free(glp->thing);
783         free(glp);
784     }
785
786     qp = escorts.q_forw;
787     while (qp != (&escorts)) {
788         newqp = qp->q_forw;
789         emp_remque(qp);
790         free(qp);
791         qp = newqp;
792     }
793
794     qp = bombers.q_forw;
795     while (qp != (&bombers)) {
796         newqp = qp->q_forw;
797         emp_remque(qp);
798         free(qp);
799         qp = newqp;
800     }
801
802     return dam;
803 }
804
805 int
806 cando(int mission, int type)
807 {
808     switch (mission) {
809     case MI_ESCORT:
810         if (type == EF_PLANE)
811             return 1;
812         return 0;
813     case MI_AIR_DEFENSE:
814         if (type == EF_PLANE)
815             return 1;
816         return 0;
817     case MI_SINTERDICT:
818         if ((type == EF_PLANE) || (type == EF_SHIP))
819             return 1;
820         return 0;
821     case MI_INTERDICT:
822         return 1;
823     case MI_SUPPORT:
824     case MI_OSUPPORT:
825     case MI_DSUPPORT:
826         if (type == EF_PLANE)
827             return 1;
828         return 0;
829     case MI_RESERVE:
830         if (type == EF_LAND)
831             return 1;
832         return 0;
833     }
834
835     return 0;
836 }
837
838 s_char *
839 nameofitem(struct genitem *gp, int type)
840 {
841     switch (type) {
842     case EF_SHIP:
843         return prship((struct shpstr *)gp);
844     case EF_PLANE:
845         return prplane((struct plnstr *)gp);
846     case EF_LAND:
847         return prland((struct lndstr *)gp);
848     }
849     return NULL;
850 }
851
852 s_char *
853 mission_name(short int mission)
854 {
855     switch (mission) {
856     case MI_INTERDICT:
857         return "an interdiction";
858     case MI_SUPPORT:
859         return "a support";
860     case MI_OSUPPORT:
861         return "a offensive support";
862     case MI_DSUPPORT:
863         return "a defensive support";
864     case MI_RESERVE:
865         return "a reserve";
866     case MI_ESCORT:
867         return "an escort";
868     case MI_SINTERDICT:
869         return "a sub interdiction";
870     case MI_AIR_DEFENSE:
871         return "an air defense";
872     }
873     return "a mysterious";
874 }
875
876 void
877 show_mission(int type, struct nstr_item *np)
878 {
879     int size, first = 1, radius;
880     s_char *block;
881     struct genitem *gp;
882
883     size = max(sizeof(struct lndstr), sizeof(struct plnstr));
884     size = max(size, sizeof(struct shpstr));
885     block = malloc(size);
886
887     while (nxtitem(np, block)) {
888         gp = (struct genitem *)block;
889         if (!player->owner || gp->own == 0)
890             continue;
891
892         if (first) {
893             pr("Thing                         x,y   op-sect rad mission\n");
894             first = 0;
895         }
896         pr("%-25s", nameofitem(gp, type));
897         prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
898         if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
899             gp->mission == MI_OSUPPORT ||
900             gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
901             radius = 999;
902             oprange(gp, type, &radius);
903             prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
904             if (radius < gp->radius)
905                 pr("  %4d", radius);
906             else
907                 pr("  %4d", gp->radius);
908         } else if (gp->mission == MI_RESERVE) {
909             struct sctstr sect;
910             int plus = 2;
911
912             getsect(gp->x, gp->y, &sect);
913             if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
914                 plus++;
915
916             if (((struct lndstr *)block)->lnd_rad_max == 0)
917                 plus = 0;
918             else
919                 plus += ((struct lndstr *)block)->lnd_rad_max;
920             prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
921             pr("  %4d", plus);
922         } else if (gp->mission == MI_ESCORT) {
923             pr("        ");
924             pr("  %4d", (int)
925                ((float)((struct plnstr *)block)->pln_range / 2.0)
926                 );
927         } else
928             pr("              ");
929         if (gp->mission)
930             pr(" is on %s mission\n", mission_name(gp->mission));
931         else
932             pr(" has no mission.\n");
933     }
934 }
935
936 int
937 oprange(struct genitem *gp, int type, int *radius)
938 {
939     int range;
940     struct shpstr ship;
941     struct lndstr land;
942     struct plnstr plane;
943
944     switch (type) {
945     case EF_SHIP:
946         getship(gp->uid, &ship);
947         range = ldround(techfact(gp->tech,
948                                  (double)ship.shp_frnge / 2.0), 1);
949         break;
950     case EF_LAND:
951         getland(gp->uid, &land);
952         range = ldround(techfact((int)land.lnd_tech,
953                                  (double)land.lnd_frg / 2.0), 1);
954         break;
955     case EF_PLANE:
956         getplane(gp->uid, &plane);
957         /* missiles go one way, so we can use all the range */
958         if (plchr[(int)plane.pln_type].pl_flags & P_M)
959             range = plane.pln_range;
960         else
961             range = ldround((double)plane.pln_range / 2.0, 1);;
962         break;
963     }
964
965     if ((*radius) > range)
966         *radius = range;
967
968     return range;
969 }
970
971 /*
972  *  Remove all planes who cannot go on
973  *  the mission from the plane list.
974  */
975 static void
976 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
977                 int hardtarget)
978 {
979     struct emp_qelem *qp, *next;
980     struct plnstr *pp;
981     struct shpstr ship;
982     struct lndstr land;
983     struct sctstr sect;
984     struct plchrstr *pcp;
985     struct plist *plp;
986     int y, bad, bad1;
987     unsigned int x;
988
989     for (qp = list->q_forw; qp != list; qp = next) {
990         next = qp->q_forw;
991         plp = (struct plist *)qp;
992         pp = &plp->plane;
993         pcp = plp->pcp;
994
995         if (pp->pln_effic < 40) {
996             emp_remque(qp);
997             free(qp);
998             continue;
999         }
1000
1001         if (pp->pln_mobil < 1) {
1002             emp_remque(qp);
1003             free(qp);
1004             continue;
1005         }
1006
1007         if (opt_MARKET) {
1008             if (ontradingblock(EF_PLANE, (int *)pp)) {
1009                 emp_remque(qp);
1010                 free(qp);
1011                 continue;
1012             }
1013         }
1014
1015         bad = 0;
1016         bad1 = 0;
1017         if (wantflags) {
1018             for (x = 0; x < sizeof(wantflags) * 8; x++) {
1019                 y = (1 << x);
1020                 if ((wantflags & y) == y)
1021                     if ((pcp->pl_flags & y) != y) {
1022                         switch (y) {
1023                         case P_F:
1024                         case P_ESC:
1025                             bad1 = 2;
1026                             break;
1027                         case P_E:
1028                         case P_L:
1029                         case P_K:
1030                             bad1 = 1;
1031                             break;
1032                         default:
1033                             bad = 1;
1034                         }
1035                     }
1036             }
1037             if (bad) {
1038                 emp_remque(qp);
1039                 free(qp);
1040                 continue;
1041             }
1042             if (bad1 == 2) {
1043                 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1044                     bad1 = 0;
1045             }
1046             if (bad1 == 1) {
1047                 if ((pcp->pl_flags & P_E) ||
1048                     (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1049                     bad1 = 0;
1050             }
1051             if (bad1) {
1052                 emp_remque(qp);
1053                 free(qp);
1054                 continue;
1055             }
1056         }
1057         bad = 0;
1058         bad1 = 0;
1059         if (nowantflags) {
1060             for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1061                 y = (1 << x);
1062                 if ((nowantflags & y) == y)
1063                     if ((pcp->pl_flags & y) == y)
1064                         bad = 1;
1065             }
1066             if (bad) {
1067                 emp_remque(qp);
1068                 free(qp);
1069                 continue;
1070             }
1071         }
1072         if (pp->pln_ship >= 0) {
1073             if (!getship(pp->pln_ship, &ship)) {
1074               shipsunk:
1075                 pp->pln_effic = 0;
1076                 putplane(pp->pln_uid, pp);
1077                 emp_remque(qp);
1078                 free(qp);
1079                 continue;
1080             }
1081             if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1082                 goto shipsunk;
1083             }
1084             if (ship.shp_effic < SHIP_MINEFF) {
1085                 goto shipsunk;
1086             }
1087             /* Can't fly off of inefficient or non-owned, non-allied ships */
1088             if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1089                 ((ship.shp_own != pp->pln_own) &&
1090                  (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1091                 emp_remque(qp);
1092                 free(qp);
1093                 continue;
1094             }
1095         }
1096         if (pp->pln_land >= 0) {
1097             if (!getland(pp->pln_land, &land)) {
1098               landdead:
1099                 pp->pln_effic = 0;
1100                 putplane(pp->pln_uid, pp);
1101                 emp_remque(qp);
1102                 free(qp);
1103                 continue;
1104             }
1105             if (!(pcp->pl_flags & P_E))
1106                 goto landdead;
1107             if (land.lnd_effic < LAND_MINEFF)
1108                 goto landdead;
1109
1110             /* Can't fly off of inefficient or non-owned, non-allied units */
1111             if ((land.lnd_effic < LND_AIROPS_EFF) ||
1112                 ((land.lnd_own != pp->pln_own) &&
1113                  (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1114                 emp_remque(qp);
1115                 free(qp);
1116                 continue;
1117             }
1118
1119             /* Can't fly off units in ships or other units */
1120             if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1121                 emp_remque(qp);
1122                 free(qp);
1123                 continue;
1124             }
1125         }
1126         /* Now, check the sector status if not on a plane or unit */
1127         if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1128             /* If we can't get the sector, we can't check it, and can't fly */
1129             if (!getsect(pp->pln_x, pp->pln_y, &sect)) {
1130                 emp_remque(qp);
1131                 free(qp);
1132                 continue;
1133             }
1134             /* First, check allied status */
1135             /* Can't fly from non-owned sectors or non-allied sectors */
1136             if ((sect.sct_own != pp->pln_own) &&
1137                 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1138                 emp_remque(qp);
1139                 free(qp);
1140                 continue;
1141             }
1142             /* non-vtol plane */
1143             if ((pcp->pl_flags & P_V) == 0) {
1144                 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1145                     emp_remque(qp);
1146                     free(qp);
1147                     continue;
1148                 }
1149             }
1150         }
1151         if (pcp->pl_flags & P_A) {
1152             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1153                 emp_remque(qp);
1154                 free(qp);
1155                 continue;
1156             }
1157         }
1158
1159         putplane(pp->pln_uid, pp);
1160     }
1161 }
1162
1163 /*
1164  * Arm only the planes at x,y
1165  *
1166  */
1167 static int
1168 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1169                 int mission, struct ichrstr *ip, int flags,
1170                 int mission_flags, int *tech)
1171 {
1172     struct emp_qelem *qp;
1173     struct emp_qelem *next;
1174     struct plist *plp;
1175
1176     if (*tech == 0)
1177         *tech = 9999;
1178     for (qp = list->q_forw; qp != list; qp = next) {
1179         next = qp->q_forw;
1180         plp = (struct plist *)qp;
1181
1182         if (plp->plane.pln_x != x)
1183             continue;
1184         if (plp->plane.pln_y != y)
1185             continue;
1186
1187         if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1188             emp_remque(qp);
1189             free(qp);
1190             continue;
1191         }
1192         if (flags & (P_S | P_I)) {
1193             if (plp->pcp->pl_flags & P_S)
1194                 mission_flags |= P_S;
1195             if (plp->pcp->pl_flags & P_I)
1196                 mission_flags |= P_I;
1197         }
1198         if (*tech > plp->plane.pln_tech)
1199             *tech = plp->plane.pln_tech;
1200         if (!(plp->pcp->pl_flags & P_H))
1201             /* no stealth on this mission */
1202             mission_flags &= ~P_H;
1203         if (!(plp->pcp->pl_flags & P_X))
1204             /* no stealth on this mission */
1205             mission_flags &= ~P_X;
1206         if (!(plp->pcp->pl_flags & P_A)) {
1207             /* no asw on this mission */
1208             mission_flags &= ~P_A;
1209         }
1210         if (!(plp->pcp->pl_flags & P_MINE)) {
1211             /* no asw on this mission */
1212             mission_flags &= ~P_MINE;
1213         }
1214
1215         /*
1216          *      Mob costs for missions are 1/2 normal
1217          *       Not anymore. :)
1218          */
1219 /*      plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1220         plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1221
1222     }
1223     return mission_flags;
1224 }
1225
1226 int
1227 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1228                   s_char mission)
1229 {
1230     struct plchrstr *pcp;
1231     struct plnstr *pp;
1232     int needed;
1233     struct lndstr land;
1234     struct shpstr ship;
1235     struct sctstr sect;
1236     i_type itype;
1237     int rval;
1238     short *item;
1239
1240     pp = &plp->plane;
1241     pcp = plp->pcp;
1242     if (pp->pln_ship >= 0) {
1243         getship(pp->pln_ship, &ship);
1244         item = ship.shp_item;
1245     } else if (pp->pln_land >= 0) {
1246         getland(pp->pln_land, &land);
1247         item = land.lnd_item;
1248     } else {
1249         getsect(pp->pln_x, pp->pln_y, &sect);
1250         item = sect.sct_item;
1251     }
1252     if (pcp->pl_fuel > item[I_PETROL]) {
1253         return -1;
1254     }
1255     item[I_PETROL] -= pcp->pl_fuel;
1256     rval = 0;
1257     if (!(flags & P_F)) {
1258         itype = I_NONE;
1259         needed = 0;
1260         switch (mission) {
1261         case 's':
1262         case 'p':
1263             if (pp->pln_nuketype == -1) {
1264                 itype = I_SHELL;
1265                 needed = pp->pln_load;
1266             }
1267             break;
1268         case 't':
1269             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1270                 break;
1271             itype = ip->i_vtype;
1272             needed = (pp->pln_load * 2) / ip->i_lbs;
1273             break;
1274         case 'd':
1275             if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1276                 break;
1277             itype = ip->i_vtype;
1278             needed = (pp->pln_load * 2) / ip->i_lbs;
1279             break;
1280         case 'a':
1281             if ((pcp->pl_flags & (P_V | P_C)) == 0)
1282                 break;
1283             itype = I_MILIT;
1284             needed = pp->pln_load / ip->i_lbs;
1285             break;
1286         case 'n':
1287             if (pp->pln_nuketype == -1)
1288                 rval = -1;
1289             break;
1290         case 'i':               /* missile interception */
1291             if (pp->pln_load) {
1292                 itype = I_SHELL;
1293                 needed = pp->pln_load;
1294             }
1295             break;
1296         default:
1297             break;
1298         }
1299         if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1300             return -1;
1301         }
1302         if (itype != I_NONE) {
1303             if (itype == I_SHELL && item[itype] < needed)
1304                 item[itype] += supply_commod(plp->plane.pln_own,
1305                                              plp->plane.pln_x,
1306                                              plp->plane.pln_y,
1307                                              I_SHELL, needed);
1308             if (item[itype] < needed)
1309                 return -1;
1310             item[itype] -= needed;
1311         }
1312         if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1313             plp->bombs = needed;
1314         else
1315             plp->misc = needed;
1316     }
1317     if (pp->pln_ship >= 0)
1318         putship(ship.shp_uid, &ship);
1319     else if (pp->pln_land >= 0)
1320         putland(land.lnd_uid, &land);
1321     else
1322         putsect(&sect);
1323     return rval;
1324 }
1325
1326 /*
1327  *  Return 1 if this x,y pair is in the list
1328  */
1329 static int
1330 find_airport(struct emp_qelem *airp, coord x, coord y)
1331 {
1332     struct emp_qelem *qp;
1333     struct airport *a;
1334
1335     for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1336         a = (struct airport *)qp;
1337         if ((a->x == x) && (a->y == y))
1338             return 1;
1339     }
1340
1341     return 0;
1342 }
1343
1344 /* #*# This needs to be changed to include acc's -KHS */
1345 static void
1346 add_airport(struct emp_qelem *airp, coord x, coord y)
1347 {
1348     struct airport *a;
1349     struct sctstr sect;
1350
1351     a = malloc(sizeof(struct airport));
1352
1353     a->x = x;
1354     a->y = y;
1355     getsect(x, y, &sect);
1356     a->own = sect.sct_own;
1357
1358     emp_insque((struct emp_qelem *)a, airp);
1359 }
1360
1361 /*
1362  *  Take all the planes in list 1 that
1363  *  are at x,y, and put them into list 2.
1364  */
1365 static void
1366 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1367 {
1368     struct emp_qelem *qp, *next;
1369     struct plist *plp;
1370
1371     for (qp = l1->q_forw; qp != l1; qp = next) {
1372         next = qp->q_forw;
1373         plp = (struct plist *)qp;
1374
1375         if (plp->plane.pln_x != x)
1376             continue;
1377         if (plp->plane.pln_y != y)
1378             continue;
1379
1380         emp_remque(qp);
1381         emp_insque(qp, l2);
1382     }
1383 }
1384
1385 static int
1386 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1387            natid victim, s_char *s, int hardtarget)
1388 {
1389     struct emp_qelem *qp;
1390     struct plist *plp;
1391     struct plnstr *pp;
1392     int newdam, dam = 0;
1393     int hitchance;
1394     int nukedam;
1395
1396     for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1397         plp = (struct plist *)qp;
1398         pp = &plp->plane;
1399
1400         if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1401             continue;
1402
1403         if (!plp->bombs)
1404             continue;
1405
1406         newdam = 0;
1407         if (plp->pcp->pl_flags & P_A) {
1408             if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1409                 wu(0, pp->pln_own,
1410                    "\t%s detects sub movement in %s\n",
1411                    prplane(pp), xyas(x, y, pp->pln_own));
1412                 continue;
1413             }
1414             if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1415                 wu(0, pp->pln_own,
1416                    "\t%s tracks %s %s at %s\n",
1417                    prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1418                 continue;
1419             }
1420             wu(0, pp->pln_own,
1421                "\t%s depth-charging %s %s in %s\n",
1422                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1423         } else {
1424             wu(0, pp->pln_own,
1425                "\t%s pinbombing %s %s in %s\n",
1426                prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1427         }
1428         hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1429         if (plp->plane.pln_nuketype != -1)
1430             hitchance = 100;
1431         else if (hardtarget != SECT_HARDTARGET)
1432             wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1433         /* Always calculate damage */
1434         if (roll(100) <= hitchance) {
1435             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1436             if (nukedam) {
1437                 if (mission == MI_INTERDICT) {
1438                     wu(0, pp->pln_own,
1439                        "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1440                        prplane(pp), nukedam, cname(victim), s);
1441                     dam += nukedam;
1442                 }
1443             } else {
1444                 wu(0, pp->pln_own,
1445                    "\t\thit %s %s for %d damage\n",
1446                    cname(victim), s, newdam);
1447                 dam += newdam;
1448             }
1449         } else {
1450             newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1451             wu(0, pp->pln_own, "missed\n");
1452             if (mission == MI_SINTERDICT) {
1453                 mpr(victim,
1454                     "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1455                     xyas(x, y, victim));
1456             } else if (*s == 's') {
1457                 mpr(victim,
1458                     "SPLASH!  Bombs miss your %s in %s\n",
1459                     s, xyas(x, y, victim));
1460             } else {
1461                 mpr(victim, "SPLAT!  Bombs miss your %s in %s\n",
1462                     s, xyas(x, y, victim));
1463             }
1464             /* Now, even though we missed, the bombs
1465                land somewhere. */
1466             collateral_damage(x, y, newdam, bombers);
1467         }
1468
1469         /* use up missiles */
1470         if (plp->pcp->pl_flags & P_M) {
1471             makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1472                      pp->pln_y);
1473             pp->pln_own = 0;
1474         }
1475     }
1476
1477     return dam;
1478 }
1479
1480 /*
1481  * Check to see if anyone hostile to the victim
1482  * is running an air defense mission on this
1483  * sector. If so, do air combat
1484  */
1485 int
1486 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1487             struct emp_qelem *esc_list)
1488 {
1489     int dam = 0, cn;
1490     int mission_flags, tech, combat = 0, rel, dist, z;
1491     struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1492     struct plist *plp;
1493     struct genlist *glp;
1494     struct genitem *gp;
1495     struct genlist mi[MAXNOC];
1496     s_char path[512];
1497     int count;
1498     int tcount;
1499
1500     count = 0;
1501     for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1502         count++;
1503     for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1504         count++;
1505
1506     memset(mi, 0, sizeof(mi));
1507     for (z = 1; z < MAXNOC; z++)
1508         emp_initque((struct emp_qelem *)&mi[z]);
1509
1510     build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1511
1512     for (cn = 1; cn < MAXNOC; cn++) {
1513         /* Check our relations */
1514         rel = getrel(getnatp(cn), victim);
1515
1516         if (rel > HOSTILE)
1517             continue;
1518
1519         if (QEMPTY(&mi[cn].queue))
1520             continue;
1521
1522         /* Ok, make a list of all the interceptors.  Note that this *copies* the
1523          * list from the mission creation.  This list must be deleted later. */
1524         emp_initque(&interceptors);
1525         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1526             next = qp->q_forw;
1527
1528             glp = (struct genlist *)qp;
1529             gp = (struct genitem *)glp->thing;
1530             plp = (struct plist *)qp;
1531
1532             dist = mapdist(x, y, gp->x, gp->y);
1533
1534             plp = malloc(sizeof(struct plist));
1535             memset(plp, 0, sizeof(struct plist));
1536             plp->pcp = (struct plchrstr *)glp->cp;
1537             memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1538
1539             /* missiles go one way, so we can use all the range */
1540             if (!(plp->pcp->pl_flags & P_M))
1541                 dist *= 2;
1542             /* If it's out of range, free it and continue on */
1543             if (dist > plp->plane.pln_range) {
1544                 free(plp);
1545                 continue;
1546             }
1547             emp_insque(&plp->queue, &interceptors);
1548         }
1549
1550         /* Remove those who cannot go */
1551         mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1552
1553         if (QEMPTY(&interceptors))
1554             continue;
1555
1556         /* Now, delete all the extras, but delete the first ones, not the last ones, so
1557          * that the higher numbered planes go into battle (they should be the better ones
1558          * at fighting, if all went well.) */
1559         tcount = 0;
1560         for (qp = interceptors.q_forw; qp != (&interceptors);
1561              qp = qp->q_forw)
1562             tcount++;
1563         tcount -= (count * 2);
1564         /* Just in case there are more incoming than we have */
1565         if (tcount < 0)
1566             tcount = 0;
1567         for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1568             next = qp->q_forw;
1569             if (tcount) {
1570                 tcount--;
1571                 /* Free it up and continue */
1572                 emp_remque(qp);
1573                 glp = (struct genlist *)qp;
1574                 free(glp);
1575             }
1576         }
1577
1578         /* Now, make a list of all the airports these planes are coming from */
1579         emp_initque(&airp);
1580         for (qp = interceptors.q_forw; qp != (&interceptors);
1581              qp = qp->q_forw) {
1582             plp = (struct plist *)qp;
1583             if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1584                 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1585         }
1586
1587         /* Now, fly them out one airport at a time */
1588         for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1589             struct airport *air;
1590
1591             air = (struct airport *)qp;
1592             dist = mapdist(x, y, air->x, air->y);
1593
1594             emp_initque(&i);
1595
1596             /* Split off the interceptors at this base into i */
1597             divide(&interceptors, &i, air->x, air->y);
1598
1599             tech = 0;
1600             mission_flags = 0;
1601             mission_flags |= P_X;       /* stealth (shhh) */
1602             /* gets turned off if not all choppers */
1603             mission_flags |= P_H;
1604             sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1605             sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1606
1607             /* Did we run out of interceptors? */
1608             if (QEMPTY(&i))
1609                 continue;
1610             /* Did we run out of bombers? */
1611             if (QEMPTY(bomb_list)) {
1612                 /* Yes, so we have to put the rest of the interceptors back, and
1613                    then continue, or we leak memory */
1614                 pln_put(&i);
1615                 continue;
1616             }
1617             mission_flags =
1618                 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1619                                 mission_flags, &tech);
1620
1621             /* Did we run out of interceptors? */
1622             if (QEMPTY(&i))
1623                 continue;
1624             /* Did we run out of bombers? */
1625             if (QEMPTY(bomb_list)) {
1626                 /* Yes, so we have to put the rest of the interceptors back, and
1627                    then continue, or we leak memory */
1628                 pln_put(&i);
1629                 continue;
1630             }
1631
1632             BestAirPath(path, air->x, air->y, x, y);
1633             wu(0, cn, "Flying %s mission from %s\n",
1634                mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1635             if (air->own && (air->own != cn)) {
1636                 wu(0, air->own, "%s is flying %s mission from %s\n",
1637                    cname(cn), mission_name(MI_AIR_DEFENSE),
1638                    xyas(air->x, air->y, air->own));
1639             }
1640
1641             /* Now, fly the planes to the sector */
1642             emp_initque(&empty);
1643             ac_encounter(&i, &empty, air->x, air->y,
1644                          path, mission_flags, 1, bomb_list, esc_list);
1645
1646             /* If none made it, continue */
1647             if (QEMPTY(&i))
1648                 continue;
1649
1650             /* Some made it, so now they get to try to fight. */
1651             /* Intercept the escorts first */
1652             combat = 0;
1653             if (!QEMPTY(esc_list)) {
1654                 mpr(victim, "%s air defense planes intercept!\n",
1655                     cname(cn));
1656                 ac_combat_headers(victim, cn);
1657                 ac_airtoair(esc_list, &i);
1658                 combat = 1;
1659             }
1660             /* Now intercept the bombers */
1661             if (!QEMPTY(bomb_list)) {
1662                 if (!combat) {
1663                     mpr(victim, "%s air defense planes intercept!\n",
1664                         cname(cn));
1665                     ac_combat_headers(victim, cn);
1666                 }
1667                 ac_airtoair(bomb_list, &i);
1668                 PR(cn, "\n");
1669                 PR(victim, "\n");
1670             }
1671
1672             pln_put(&i);
1673         }
1674     }
1675
1676     /* We have to free all of these, if they are still there, otherwise they get
1677        lost and we leak memory all over the place. */
1678     for (cn = 1; cn < MAXNOC; cn++) {
1679         /* free up all this memory if it's still there */
1680         for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1681             next = qp->q_forw;
1682             glp = (struct genlist *)qp;
1683             free(glp->thing);
1684             free(glp);
1685         }
1686     }
1687
1688     return dam;
1689 }