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