2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * mission.c: Mission subroutines for planes/ships/units
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
47 #include "prototypes.h"
52 struct emp_qelem queue; /* list of units */
53 void *cp; /* pointer to desc of thing */
54 struct empobj *thing; /* thing's struct */
58 struct emp_qelem queue;
63 static void add_airport(struct emp_qelem *, coord, coord);
64 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
66 static void build_mission_list(struct genlist *, coord, coord, int, natid);
67 static void build_mission_list_type(struct genlist *, coord, coord, int,
69 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
70 static int dosupport(struct genlist *, coord, coord, natid, natid);
71 static int find_airport(struct emp_qelem *, coord, coord);
72 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
73 int, struct ichrstr *, int, int);
74 static void mission_pln_sel(struct emp_qelem *, int, int, int);
75 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
79 * Interdict commodities & transported planes
82 ground_interdict(coord x, coord y, natid victim, char *s)
85 int dam = 0, newdam, rel;
86 struct genlist mi[MAXNOC];
89 memset(mi, 0, sizeof(mi));
90 for (z = 1; z < MAXNOC; z++)
91 emp_initque((struct emp_qelem *)&mi[z]);
93 build_mission_list(mi, x, y, MI_INTERDICT, victim);
95 for (cn = 1; cn < MAXNOC; cn++) {
96 rel = getrel(getnatp(cn), victim);
100 if (QEMPTY(&mi[cn].queue))
103 newdam = perform_mission(x, y, victim, &mi[cn].queue,
104 MI_INTERDICT, s, SECT_HARDTARGET);
107 mpr(victim, "%s interdiction mission does %d damage!\n",
111 collateral_damage(x, y, dam);
117 collateral_damage(coord x, coord y, int dam)
125 getsect(x, y, §);
127 coll = ldround((double)dam * collateral_dam, 1);
130 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
131 xyas(x, y, sect.sct_own), coll);
132 sectdamage(§, coll);
140 only_subs(struct emp_qelem *list)
142 struct emp_qelem *qp;
146 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
147 glp = (struct genlist *)qp;
149 if (glp->thing->ef_type != EF_SHIP)
152 if (!(mcp->m_flags & M_SUB))
156 /* They were all subs! */
162 * Interdict ships & land units
165 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
170 struct genlist mi[MAXNOC];
174 memset(mi, 0, sizeof(mi));
175 for (z = 1; z < MAXNOC; z++)
176 emp_initque((struct emp_qelem *)&mi[z]);
178 build_mission_list(mi, x, y, mission, victim);
180 for (cn = 1; cn < MAXNOC; cn++) {
183 if (mission == MI_SINTERDICT) {
184 if (getrel(getnatp(cn), victim) >= FRIENDLY)
186 } else if (getrel(getnatp(cn), victim) > HOSTILE)
189 if (QEMPTY(&mi[cn].queue))
192 osubs = only_subs(&mi[cn].queue);
193 newdam = perform_mission(x, y, victim, &mi[cn].queue,
194 mission, s, hardtarget);
197 /* If only subs responded, then we don't know who's
199 mpr(victim, "%s interdiction mission does %d damage!\n",
200 osubs ? "Enemy" : cname(cn), newdam);
204 collateral_damage(x, y, dam);
210 * Perform a mission against victim, on behalf of actee
213 off_support(coord x, coord y, natid victim, natid actee)
216 struct genlist mi[MAXNOC];
219 memset(mi, 0, sizeof(mi));
220 for (z = 1; z < MAXNOC; z++)
221 emp_initque((struct emp_qelem *)&mi[z]);
223 build_mission_list(mi, x, y, MI_SUPPORT, victim);
224 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
226 dam = dosupport(mi, x, y, victim, actee);
231 * Perform a mission against victim, on behalf of actee
234 def_support(coord x, coord y, natid victim, natid actee)
237 struct genlist mi[MAXNOC];
240 memset(mi, 0, sizeof(mi));
241 for (z = 1; z < MAXNOC; z++)
242 emp_initque((struct emp_qelem *)&mi[z]);
244 build_mission_list(mi, x, y, MI_SUPPORT, victim);
245 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
247 dam = dosupport(mi, x, y, victim, actee);
252 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
258 for (cn = 1; cn < MAXNOC; cn++) {
259 rel = getrel(getnatp(cn), actee);
260 if ((cn != actee) && (rel != ALLIED))
262 rel = getrel(getnatp(cn), victim);
263 if ((cn != actee) && (rel != AT_WAR))
266 if (QEMPTY(&mi[cn].queue))
269 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
270 "", SECT_HARDTARGET);
276 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
279 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
280 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
281 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
285 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
286 int type, natid victim)
291 union empobj_storage item;
297 snxtitem_all(&ni, type);
298 while (nxtitem(&ni, &item)) {
299 gp = (struct empobj *)&item;
307 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
310 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
311 (gp->mission != MI_INTERDICT))
314 relat = getrel(getnatp(gp->own), victim);
315 if (mission == MI_SINTERDICT) {
316 if (relat >= FRIENDLY)
318 else if (type != EF_PLANE && relat > HOSTILE)
320 } else if (relat > HOSTILE)
323 dist = mapdist(x, y, gp->opx, gp->opy);
326 if (mission != MI_RESERVE) /* XXX */
327 oprange(gp, &radius);
332 /* Ok, it is within the operations range. */
333 /* Now check from where the object actually is */
334 dist = mapdist(x, y, gp->x, gp->y);
336 oprange(gp, &radius);
339 /* Ok, the object can get to where the x,y is */
342 if (mission != MI_AIR_DEFENSE) {
343 getsect(x, y, §);
344 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
347 * If the owner of the unit isn't at war
348 * with the victim, and doesn't own the
349 * sect being acted upon, and isn't the
350 * old owner of that sect, bounce them.
352 if (sect.sct_type != SCT_WATER &&
353 sect.sct_own != gp->own &&
354 sect.sct_oldown != gp->own)
360 glp = malloc(sizeof(struct genlist));
361 memset(glp, 0, sizeof(struct genlist));
362 glp->cp = get_empobj_chr(gp);
363 glp->thing = malloc(sizeof(item));
364 memcpy(glp->thing, &item, sizeof(item));
365 emp_insque(&glp->queue, &mi[gp->own].queue);
370 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
377 snxtitem_all(&ni, EF_PLANE);
378 while (nxtitem(&ni, &plane)) {
379 if (plane.pln_own != cn)
382 if (plane.pln_mission != MI_ESCORT)
385 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
386 if (dist > plane.pln_range / 2)
389 plp = malloc(sizeof(struct plist));
390 memset(plp, 0, sizeof(struct plist));
391 plp->pcp = &plchr[(int)plane.pln_type];
393 emp_insque(&plp->queue, escorts);
398 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
399 int mission, char *s, int hardtarget)
401 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
402 struct emp_qelem *newqp;
410 struct plchrstr *pcp;
411 int dam = 0, dam2, mission_flags;
412 natid plane_owner = 0;
413 int md, range, air_dam = 0;
414 double prb, hitchance, vrange;
415 int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
417 getsect(x, y, §);
419 emp_initque(&missiles);
420 emp_initque(&bombers);
421 emp_initque(&escorts);
424 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
425 glp = (struct genlist *)qp;
428 md = mapdist(x, y, gp->x, gp->y);
430 if (glp->thing->ef_type == EF_LAND) {
431 lp = (struct lndstr *)glp->thing;
433 if (mission == MI_SINTERDICT)
436 if ((mission == MI_INTERDICT) &&
437 (md > land_max_interdiction_range))
440 /* Too ponderous for interdiction fire */
441 if (lchr[(int)lp->lnd_type].l_flags & L_HEAVY)
444 range = roundrange(lnd_fire_range(lp));
450 putland(lp->lnd_uid, lp);
454 if (targeting_ships) {
455 if (chance(lnd_acc(lp) / 100.0))
456 dam2 = ldround(dam2 / 2.0, 1);
460 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
462 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
464 "%s fires at %s %s at %s\n",
465 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
467 mpr(victim, "%s %s fires at you at %s\n",
468 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
469 } else if (glp->thing->ef_type == EF_SHIP) {
470 sp = (struct shpstr *)glp->thing;
473 if (((mission == MI_INTERDICT) ||
474 (mission == MI_SINTERDICT)) &&
475 (md > ship_max_interdiction_range))
478 if ((mcp->m_flags & M_SUB) &&
479 (sect.sct_type != SCT_WATER))
482 if (mission == MI_SINTERDICT) {
483 if (!(mcp->m_flags & M_SONAR))
485 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
487 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
488 vrange *= sp->shp_effic / 200.0;
491 /* can't look all the time */
495 if (mcp->m_flags & M_SUB) {
496 if (!targeting_ships)
497 continue; /* subs interdict only ships */
498 range = roundrange(torprange(sp));
501 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
503 dam2 = shp_torp(sp, 1);
504 putship(sp->shp_uid, sp);
507 hitchance = shp_torp_hitchance(sp, md);
510 "%s locking on %s %s in %s\n",
511 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
513 "\tEffective torpedo range is %d.0\n", range);
515 "\tWhooosh... Hitchance = %d%%\n",
516 (int)(hitchance * 100));
518 if (!chance(hitchance)) {
519 wu(0, sp->shp_own, "\tMissed\n");
521 "Incoming torpedo sighted @ %s missed (whew)!\n",
525 wu(0, sp->shp_own, "\tBOOM!...\n");
527 nreport(victim, N_TORP_SHIP, 0, 1);
529 "\tTorpedo hit %s %s for %d damage\n",
530 cname(victim), s, dam2);
533 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
534 xyas(x, y, victim), dam2);
536 range = roundrange(shp_fire_range(sp));
539 if (mission == MI_SINTERDICT)
540 dam2 = shp_dchrg(sp);
543 putship(sp->shp_uid, sp);
549 prb = (double)md / range;
555 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
557 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
559 "%s fires at %s %s at %s\n",
560 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
562 mpr(victim, "%s %s fires at you at %s\n",
563 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
565 } else if (glp->thing->ef_type == EF_PLANE) {
567 if (pcp->pl_flags & P_M)
568 /* units have their own missile interdiction */
569 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
572 /* save planes for later */
573 plp = malloc(sizeof(struct plist));
575 memset(plp, 0, sizeof(struct plist));
577 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
578 if (plp->pcp->pl_flags & P_M)
579 emp_insque(&plp->queue, &missiles);
581 emp_insque(&plp->queue, &bombers);
582 plane_owner = plp->plane.pln_own;
588 if (!QEMPTY(&missiles)) {
589 /* I arbitrarily chose 100 mindam -KHS */
591 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
592 "sector", victim, mission);
593 qp = missiles.q_forw;
594 while (qp != (&missiles)) {
602 if (QEMPTY(&bombers)) {
605 glp = (struct genlist *)qp;
614 * If there are planes performing an
615 * interdict or support mission, find
616 * some escorts for them, if possible.
617 * Up to 2 per bomber, if possible.
619 find_escorts(x, y, plane_owner, &escorts);
621 if (mission == MI_SINTERDICT)
622 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
624 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
626 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
628 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
629 plp = (struct plist *)qp;
630 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
631 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
634 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
639 air = (struct airport *)qp;
640 md = mapdist(x, y, air->x, air->y);
645 /* Split off the bombers at this base into b */
646 divide(&bombers, &b, air->x, air->y);
648 /* Split off the escorts at this base into e */
649 divide(&escorts, &e, air->x, air->y);
652 mission_flags |= P_X; /* stealth (shhh) */
653 mission_flags |= P_H; /* gets turned off if not all choppers */
655 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
661 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
662 P_F | P_ESC, mission_flags);
664 pp = BestAirPath(buf, air->x, air->y, x, y);
665 if (CANT_HAPPEN(!pp))
667 wu(0, plane_owner, "Flying %s mission from %s\n",
668 mission_name(mission), xyas(air->x, air->y, plane_owner));
669 if (air->own && (air->own != plane_owner)) {
670 wu(0, air->own, "%s is flying %s mission from %s\n",
671 cname(plane_owner), mission_name(mission),
672 xyas(air->x, air->y, air->own));
675 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0);
679 air_damage(&b, x, y, mission, victim, s, hardtarget);
688 nreport(plane_owner, N_SHP_BOMB, victim, 1);
690 nreport(plane_owner, N_SCT_BOMB, victim, 1);
693 /* free up all this memory */
696 glp = (struct genlist *)qp;
704 while (qp != (&escorts)) {
712 while (qp != (&bombers)) {
723 cando(int mission, int type)
727 if (type == EF_PLANE)
731 if (type == EF_PLANE)
735 if ((type == EF_PLANE) || (type == EF_SHIP))
743 if (type == EF_PLANE)
756 mission_name(short mission)
760 return "an interdiction";
764 return "a offensive support";
766 return "a defensive support";
772 return "a sub interdiction";
774 return "an air defense";
776 return "a mysterious";
780 show_mission(int type, struct nstr_item *np)
782 int first = 1, radius;
783 union empobj_storage item;
786 while (nxtitem(np, &item)) {
787 gp = (struct empobj *)&item;
788 if (!player->owner || gp->own == 0)
792 pr("Thing x,y op-sect rad mission\n");
795 pr("%-25s", obj_nameof(gp));
796 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
797 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
798 gp->mission == MI_OSUPPORT ||
799 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
801 oprange(gp, &radius);
802 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
803 if (radius < gp->radius)
806 pr(" %4d", gp->radius);
807 } else if (gp->mission == MI_RESERVE) {
811 getsect(gp->x, gp->y, §);
812 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
815 if (item.land.lnd_rad_max == 0)
818 plus += item.land.lnd_rad_max;
819 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
821 } else if (gp->mission == MI_ESCORT) {
823 pr(" %4d", item.plane.pln_range / 2);
827 pr(" is on %s mission\n", mission_name(gp->mission));
829 pr(" has no mission.\n");
834 oprange(struct empobj *gp, int *radius)
838 switch (gp->ef_type) {
840 range = ldround(shp_fire_range((struct shpstr *)gp), 1);
843 range = ldround(lnd_fire_range((struct lndstr *)gp), 1);
846 /* missiles go one way, so we can use all the range */
847 if (plchr[(int)gp->type].pl_flags & P_M)
848 range = ((struct plnstr *)gp)->pln_range;
850 range = ((struct plnstr *)gp)->pln_range / 2;
864 * Remove all planes who cannot go on
865 * the mission from the plane list.
868 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
871 struct emp_qelem *qp, *next;
873 struct plchrstr *pcp;
876 for (qp = list->q_forw; qp != list; qp = next) {
878 plp = (struct plist *)qp;
882 if (pp->pln_effic < 40) {
888 if (pp->pln_mobil < 1) {
895 if (ontradingblock(EF_PLANE, pp)) {
902 if (!pln_capable(pp, wantflags, nowantflags)) {
908 if (!pln_airbase_ok(pp, 0, 0)) {
914 if (pcp->pl_flags & P_A) {
915 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
922 putplane(pp->pln_uid, pp);
927 * Arm only the planes at x,y
930 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
931 int mission, struct ichrstr *ip, int flags,
934 struct emp_qelem *qp;
935 struct emp_qelem *next;
939 for (qp = list->q_forw; qp != list; qp = next) {
941 plp = (struct plist *)qp;
949 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
950 || mission_pln_equip(plp, ip, flags, mission) < 0) {
955 if (flags & (P_S | P_I)) {
956 if (plp->pcp->pl_flags & P_S)
957 mission_flags |= P_S;
958 if (plp->pcp->pl_flags & P_I)
959 mission_flags |= P_I;
961 if (!(plp->pcp->pl_flags & P_H))
962 /* no stealth on this mission */
963 mission_flags &= ~P_H;
964 if (!(plp->pcp->pl_flags & P_X))
965 /* no stealth on this mission */
966 mission_flags &= ~P_X;
967 if (!(plp->pcp->pl_flags & P_A)) {
968 /* no asw on this mission */
969 mission_flags &= ~P_A;
971 if (!(plp->pcp->pl_flags & P_MINE)) {
972 /* no asw on this mission */
973 mission_flags &= ~P_MINE;
976 pp->pln_flags |= PLN_LAUNCHED;
977 pp->pln_mobil -= pln_mobcost(dist, pp, flags);
978 putplane(pp->pln_uid, pp);
980 return mission_flags;
984 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
987 struct plchrstr *pcp;
998 if (pp->pln_ship >= 0) {
999 getship(pp->pln_ship, &ship);
1000 item = ship.shp_item;
1001 } else if (pp->pln_land >= 0) {
1002 getland(pp->pln_land, &land);
1003 item = land.lnd_item;
1005 getsect(pp->pln_x, pp->pln_y, §);
1006 item = sect.sct_item;
1008 if (pcp->pl_fuel > item[I_PETROL]) {
1011 item[I_PETROL] -= pcp->pl_fuel;
1012 if (!(flags & P_F)) {
1013 load = pln_load(pp);
1017 case 's': /* strategic bomb */
1018 case 'p': /* pinpoint bomb */
1019 if (pp->pln_nuketype == -1) {
1024 case 't': /* transport */
1025 case 'd': /* drop */
1026 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1029 needed = (load * 2) / ip->i_lbs;
1031 case 'a': /* paradrop */
1032 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1035 needed = load / ip->i_lbs;
1037 case 'i': /* missile interception */
1043 case 'r': /* reconnaissance */
1044 case 0: /* plane interception */
1050 if (itype != I_NONE && needed <= 0)
1052 if (itype != I_NONE) {
1053 if (itype == I_SHELL && item[itype] < needed)
1054 item[itype] += supply_commod(plp->plane.pln_own,
1058 if (item[itype] < needed)
1060 item[itype] -= needed;
1062 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1063 plp->bombs = needed;
1067 if (pp->pln_ship >= 0)
1068 putship(ship.shp_uid, &ship);
1069 else if (pp->pln_land >= 0)
1070 putland(land.lnd_uid, &land);
1077 * Return 1 if this x,y pair is in the list
1080 find_airport(struct emp_qelem *airp, coord x, coord y)
1082 struct emp_qelem *qp;
1085 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1086 a = (struct airport *)qp;
1087 if ((a->x == x) && (a->y == y))
1094 /* #*# This needs to be changed to include acc's -KHS */
1096 add_airport(struct emp_qelem *airp, coord x, coord y)
1101 a = malloc(sizeof(struct airport));
1105 getsect(x, y, §);
1106 a->own = sect.sct_own;
1108 emp_insque((struct emp_qelem *)a, airp);
1112 * Take all the planes in list 1 that
1113 * are at x,y, and put them into list 2.
1116 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1118 struct emp_qelem *qp, *next;
1121 for (qp = l1->q_forw; qp != l1; qp = next) {
1123 plp = (struct plist *)qp;
1125 if (plp->plane.pln_x != x)
1127 if (plp->plane.pln_y != y)
1136 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1137 natid victim, char *s, int hardtarget)
1139 struct emp_qelem *qp;
1142 int newdam, dam = 0;
1146 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1147 plp = (struct plist *)qp;
1150 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1157 if (plp->pcp->pl_flags & P_A) {
1158 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1160 "\t%s detects sub movement in %s\n",
1161 prplane(pp), xyas(x, y, pp->pln_own));
1164 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1166 "\t%s tracks %s %s at %s\n",
1167 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1171 "\t%s depth-charging %s %s in %s\n",
1172 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1175 "\t%s pinbombing %s %s in %s\n",
1176 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1178 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1179 if (plp->plane.pln_nuketype != -1)
1181 else if (hardtarget != SECT_HARDTARGET)
1182 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1183 /* Always calculate damage */
1184 if (roll(100) <= hitchance) {
1185 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1187 if (mission == MI_INTERDICT) {
1189 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1190 prplane(pp), nukedam, cname(victim), s);
1195 "\t\thit %s %s for %d damage\n",
1196 cname(victim), s, newdam);
1200 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1201 wu(0, pp->pln_own, "missed\n");
1202 if (mission == MI_SINTERDICT) {
1204 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1205 xyas(x, y, victim));
1206 } else if (*s == 's') {
1207 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1208 s, xyas(x, y, victim));
1210 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1211 s, xyas(x, y, victim));
1213 /* Now, even though we missed, the bombs
1215 collateral_damage(x, y, newdam);
1218 /* use up missiles */
1219 if (plp->pcp->pl_flags & P_M)
1227 * Check to see if anyone hostile to the victim
1228 * is running an air defense mission on this
1229 * sector. If so, do air combat
1232 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1233 struct emp_qelem *esc_list)
1236 int mission_flags, combat = 0, rel, dist, z;
1237 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1239 struct genlist *glp;
1241 struct genlist mi[MAXNOC];
1248 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1250 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1253 memset(mi, 0, sizeof(mi));
1254 for (z = 1; z < MAXNOC; z++)
1255 emp_initque((struct emp_qelem *)&mi[z]);
1257 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1259 for (cn = 1; cn < MAXNOC; cn++) {
1260 /* Check our relations */
1261 rel = getrel(getnatp(cn), victim);
1266 if (QEMPTY(&mi[cn].queue))
1269 /* Ok, make a list of all the interceptors. Note that this *copies* the
1270 * list from the mission creation. This list must be deleted later. */
1271 emp_initque(&interceptors);
1272 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1274 glp = (struct genlist *)qp;
1276 if (CANT_HAPPEN(gp->ef_type != EF_PLANE))
1279 dist = mapdist(x, y, gp->x, gp->y);
1281 plp = malloc(sizeof(struct plist));
1282 memset(plp, 0, sizeof(struct plist));
1284 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1286 /* missiles go one way, so we can use all the range */
1287 if (!(plp->pcp->pl_flags & P_M))
1289 /* If it's out of range, free it and continue on */
1290 if (dist > plp->plane.pln_range) {
1294 emp_insque(&plp->queue, &interceptors);
1297 /* Remove those who cannot go */
1298 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1300 if (QEMPTY(&interceptors))
1303 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1304 * that the higher numbered planes go into battle (they should be the better ones
1305 * at fighting, if all went well.) */
1307 for (qp = interceptors.q_forw; qp != (&interceptors);
1310 tcount -= count * 2;
1311 /* Just in case there are more incoming than we have */
1314 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1318 /* Free it up and continue */
1320 glp = (struct genlist *)qp;
1325 /* Now, make a list of all the airports these planes are coming from */
1327 for (qp = interceptors.q_forw; qp != (&interceptors);
1329 plp = (struct plist *)qp;
1330 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1331 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1334 /* Now, fly them out one airport at a time */
1335 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1336 struct airport *air;
1338 air = (struct airport *)qp;
1339 dist = mapdist(x, y, air->x, air->y);
1343 /* Split off the interceptors at this base into i */
1344 divide(&interceptors, &i, air->x, air->y);
1347 mission_flags |= P_X; /* stealth (shhh) */
1348 /* gets turned off if not all choppers */
1349 mission_flags |= P_H;
1350 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1351 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1353 /* Did we run out of interceptors? */
1356 /* Did we run out of bombers? */
1357 if (QEMPTY(bomb_list)) {
1358 /* Yes, so we have to put the rest of the interceptors back, and
1359 then continue, or we leak memory */
1364 mission_pln_arm(&i, air->x, air->y, 2 * dist, 0, 0, P_F,
1367 /* Did we run out of interceptors? */
1370 /* Did we run out of bombers? */
1371 if (QEMPTY(bomb_list)) {
1372 /* Yes, so we have to put the rest of the interceptors back, and
1373 then continue, or we leak memory */
1378 path = BestAirPath(buf, air->x, air->y, x, y);
1379 if (CANT_HAPPEN(!path)) {
1383 wu(0, cn, "Flying %s mission from %s\n",
1384 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1385 if (air->own && (air->own != cn)) {
1386 wu(0, air->own, "%s is flying %s mission from %s\n",
1387 cname(cn), mission_name(MI_AIR_DEFENSE),
1388 xyas(air->x, air->y, air->own));
1391 /* Now, fly the planes to the sector */
1392 emp_initque(&empty);
1393 ac_encounter(&i, &empty, air->x, air->y,
1394 path, mission_flags, 1);
1396 /* If none made it, continue */
1400 /* Some made it, so now they get to try to fight. */
1401 /* Intercept the escorts first */
1403 if (!QEMPTY(esc_list)) {
1404 mpr(victim, "%s air defense planes intercept!\n",
1406 ac_combat_headers(victim, cn);
1407 ac_airtoair(esc_list, &i);
1410 /* Now intercept the bombers */
1411 if (!QEMPTY(bomb_list)) {
1413 mpr(victim, "%s air defense planes intercept!\n",
1415 ac_combat_headers(victim, cn);
1417 ac_airtoair(bomb_list, &i);
1424 if (CANT_HAPPEN(!QEMPTY(&interceptors)))
1425 pln_put(&interceptors);
1428 /* We have to free all of these, if they are still there, otherwise they get
1429 lost and we leak memory all over the place. */
1430 for (cn = 1; cn < MAXNOC; cn++) {
1431 /* free up all this memory if it's still there */
1432 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1434 glp = (struct genlist *)qp;