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
48 #include "prototypes.h"
53 struct emp_qelem queue; /* list of units */
54 void *cp; /* pointer to desc of thing */
55 struct empobj *thing; /* thing's struct */
59 struct emp_qelem queue;
64 static void add_airport(struct emp_qelem *, coord, coord);
65 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
67 static void build_mission_list(struct genlist *, coord, coord, int, natid);
68 static void build_mission_list_type(struct genlist *, coord, coord, int,
70 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
71 static int dosupport(struct genlist *, coord, coord, natid, natid);
72 static int find_airport(struct emp_qelem *, coord, coord);
73 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
74 int, struct ichrstr *, int, int, int *);
75 static void mission_pln_sel(struct emp_qelem *, int, int, int);
76 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
80 * Interdict commodities & transported planes
83 ground_interdict(coord x, coord y, natid victim, char *s)
86 int dam = 0, newdam, rel;
87 struct genlist mi[MAXNOC];
90 memset(mi, 0, sizeof(mi));
91 for (z = 1; z < MAXNOC; z++)
92 emp_initque((struct emp_qelem *)&mi[z]);
94 build_mission_list(mi, x, y, MI_INTERDICT, victim);
96 for (cn = 1; cn < MAXNOC; cn++) {
97 rel = getrel(getnatp(cn), victim);
101 if (QEMPTY(&mi[cn].queue))
104 newdam = perform_mission(x, y, victim, &mi[cn].queue,
105 MI_INTERDICT, s, SECT_HARDTARGET);
108 mpr(victim, "%s interdiction mission does %d damage!\n",
112 collateral_damage(x, y, dam, 0);
118 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
126 getsect(x, y, §);
128 coll = ldround((double)dam * collateral_dam, 1);
131 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
132 xyas(x, y, sect.sct_own), coll);
133 sectdamage(§, coll, list);
141 only_subs(struct emp_qelem *list)
143 struct emp_qelem *qp;
147 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
148 glp = (struct genlist *)qp;
150 if (glp->thing->ef_type != EF_SHIP)
153 if (!(mcp->m_flags & M_SUB))
157 /* They were all subs! */
163 * Interdict ships & land units
166 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
171 struct genlist mi[MAXNOC];
175 memset(mi, 0, sizeof(mi));
176 for (z = 1; z < MAXNOC; z++)
177 emp_initque((struct emp_qelem *)&mi[z]);
179 build_mission_list(mi, x, y, mission, victim);
181 for (cn = 1; cn < MAXNOC; cn++) {
184 if (mission == MI_SINTERDICT) {
185 if (getrel(getnatp(cn), victim) >= FRIENDLY)
187 } else if (getrel(getnatp(cn), victim) > HOSTILE)
190 if (QEMPTY(&mi[cn].queue))
193 osubs = only_subs(&mi[cn].queue);
194 newdam = perform_mission(x, y, victim, &mi[cn].queue,
195 mission, s, hardtarget);
198 /* If only subs responded, then we don't know who's
200 mpr(victim, "%s interdiction mission does %d damage!\n",
201 osubs ? "Enemy" : cname(cn), newdam);
205 collateral_damage(x, y, dam, 0);
211 * Perform a mission against victim, on behalf of actee
214 off_support(coord x, coord y, natid victim, natid actee)
217 struct genlist mi[MAXNOC];
220 memset(mi, 0, sizeof(mi));
221 for (z = 1; z < MAXNOC; z++)
222 emp_initque((struct emp_qelem *)&mi[z]);
224 build_mission_list(mi, x, y, MI_SUPPORT, victim);
225 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
227 dam = dosupport(mi, x, y, victim, actee);
232 * Perform a mission against victim, on behalf of actee
235 def_support(coord x, coord y, natid victim, natid actee)
238 struct genlist mi[MAXNOC];
241 memset(mi, 0, sizeof(mi));
242 for (z = 1; z < MAXNOC; z++)
243 emp_initque((struct emp_qelem *)&mi[z]);
245 build_mission_list(mi, x, y, MI_SUPPORT, victim);
246 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
248 dam = dosupport(mi, x, y, victim, actee);
253 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
259 for (cn = 1; cn < MAXNOC; cn++) {
260 rel = getrel(getnatp(cn), actee);
261 if ((cn != actee) && (rel != ALLIED))
263 rel = getrel(getnatp(cn), victim);
264 if ((cn != actee) && (rel != AT_WAR))
267 if (QEMPTY(&mi[cn].queue))
270 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
271 "", SECT_HARDTARGET);
277 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
280 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
281 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
282 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
286 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
287 int type, natid victim)
292 union empobj_storage item;
298 snxtitem_all(&ni, type);
299 while (nxtitem(&ni, &item)) {
300 gp = (struct empobj *)&item;
308 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
311 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
312 (gp->mission != MI_INTERDICT))
315 relat = getrel(getnatp(gp->own), victim);
316 if (mission == MI_SINTERDICT) {
317 if (relat >= FRIENDLY)
319 else if (type != EF_PLANE && relat > HOSTILE)
321 } else if (relat > HOSTILE)
324 dist = mapdist(x, y, gp->opx, gp->opy);
327 if (mission != MI_RESERVE) /* XXX */
328 oprange(gp, &radius);
333 /* Ok, it is within the operations range. */
334 /* Now check from where the object actually is */
335 dist = mapdist(x, y, gp->x, gp->y);
337 oprange(gp, &radius);
340 /* Ok, the object can get to where the x,y is */
343 if (mission != MI_AIR_DEFENSE) {
344 getsect(x, y, §);
345 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
348 * If the player->owner of the unit isn't at war
349 * with the victim, and doesn't own the
350 * sect being acted upon, and isn't the
351 * old player->owner of that sect, bounce them.
353 if (sect.sct_type != SCT_WATER &&
354 sect.sct_own != gp->own &&
355 sect.sct_oldown != gp->own)
361 glp = malloc(sizeof(struct genlist));
362 memset(glp, 0, sizeof(struct genlist));
363 glp->cp = get_empobj_chr(gp);
364 glp->thing = malloc(sizeof(item));
365 memcpy(glp->thing, &item, sizeof(item));
366 emp_insque(&glp->queue, &mi[gp->own].queue);
371 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
378 snxtitem_all(&ni, EF_PLANE);
379 while (nxtitem(&ni, &plane)) {
380 if (plane.pln_own != cn)
383 if (plane.pln_mission != MI_ESCORT)
386 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
387 if (dist > plane.pln_range / 2)
390 plp = malloc(sizeof(struct plist));
391 memset(plp, 0, sizeof(struct plist));
392 plp->pcp = &plchr[(int)plane.pln_type];
394 emp_insque(&plp->queue, escorts);
399 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
400 int mission, char *s, int hardtarget)
402 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
403 struct emp_qelem *newqp;
411 struct plchrstr *pcp;
412 int dam = 0, dam2, mission_flags, tech;
413 natid plane_owner = 0;
414 int md, range, air_dam = 0;
415 double prb, hitchance, vrange;
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 range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
445 putland(lp->lnd_uid, lp);
449 if (sect.sct_type == SCT_WATER) {
450 if (chance(lp->lnd_acc / 100.0))
451 dam2 = ldround(dam2 / 2.0, 1);
454 if (sect.sct_type == SCT_WATER)
455 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
457 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
459 "%s fires at %s %s at %s\n",
460 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
462 mpr(victim, "%s %s fires at you at %s\n",
463 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
464 } else if (glp->thing->ef_type == EF_SHIP) {
465 sp = (struct shpstr *)glp->thing;
468 if (((mission == MI_INTERDICT) ||
469 (mission == MI_SINTERDICT)) &&
470 (md > ship_max_interdiction_range))
473 if ((mcp->m_flags & M_SUB) &&
474 (sect.sct_type != SCT_WATER))
477 if (mission == MI_SINTERDICT) {
478 if (!(mcp->m_flags & M_SONAR))
480 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
482 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
483 vrange *= sp->shp_effic / 200.0;
486 /* can't look all the time */
490 if (mcp->m_flags & M_SUB) {
491 /* If we aren't shooting at "subs" or "ships" don't fire at all from
495 range = roundrange(torprange(sp));
498 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
500 dam2 = shp_torp(sp, 1);
501 putship(sp->shp_uid, sp);
504 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
507 "%s locking on %s %s in %s\n",
508 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
510 "\tEffective torpedo range is %d.0\n", range);
512 "\tWhooosh... Hitchance = %d%%\n",
513 (int)(hitchance * 100));
515 if (!chance(hitchance)) {
516 wu(0, sp->shp_own, "\tMissed\n");
518 "Incoming torpedo sighted @ %s missed (whew)!\n",
522 wu(0, sp->shp_own, "\tBOOM!...\n");
524 nreport(victim, N_TORP_SHIP, 0, 1);
526 "\tTorpedo hit %s %s for %d damage\n",
527 cname(victim), s, dam2);
530 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
531 xyas(x, y, victim), dam2);
533 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
536 if (mission == MI_SINTERDICT)
537 dam2 = shp_dchrg(sp);
540 putship(sp->shp_uid, sp);
546 prb = (double)md / range;
551 if (sect.sct_type == SCT_WATER)
552 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
554 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
556 "%s fires at %s %s at %s\n",
557 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
559 mpr(victim, "%s %s fires at you at %s\n",
560 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
562 } else if (glp->thing->ef_type == EF_PLANE) {
564 if (pcp->pl_flags & P_M)
565 /* units have their own missile interdiction */
566 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
569 /* save planes for later */
570 plp = malloc(sizeof(struct plist));
572 memset(plp, 0, sizeof(struct plist));
574 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
575 if (plp->pcp->pl_flags & P_M)
576 emp_insque(&plp->queue, &missiles);
578 emp_insque(&plp->queue, &bombers);
579 plane_owner = plp->plane.pln_own;
585 if (!QEMPTY(&missiles)) {
586 /* I arbitrarily chose 100 mindam -KHS */
588 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
589 "sector", victim, mission);
590 qp = missiles.q_forw;
591 while (qp != (&missiles)) {
599 if (QEMPTY(&bombers)) {
602 glp = (struct genlist *)qp;
611 * If there are planes performing an
612 * interdict or support mission, find
613 * some escorts for them, if possible.
614 * Up to 2 per bomber, if possible.
616 find_escorts(x, y, plane_owner, &escorts);
618 if (mission == MI_SINTERDICT)
619 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
621 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
623 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
625 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
626 plp = (struct plist *)qp;
627 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
628 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
631 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
636 air = (struct airport *)qp;
637 md = mapdist(x, y, air->x, air->y);
642 /* Split off the bombers at this base into b */
643 divide(&bombers, &b, air->x, air->y);
645 /* Split off the escorts at this base into e */
646 divide(&escorts, &e, air->x, air->y);
650 mission_flags |= P_X; /* stealth (shhh) */
651 mission_flags |= P_H; /* gets turned off if not all choppers */
653 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
654 0, mission_flags, &tech);
659 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
660 P_F | P_ESC, mission_flags, &tech);
662 pp = BestAirPath(buf, air->x, air->y, x, y);
663 if (CANT_HAPPEN(!pp))
665 wu(0, plane_owner, "Flying %s mission from %s\n",
666 mission_name(mission), xyas(air->x, air->y, plane_owner));
667 if (air->own && (air->own != plane_owner)) {
668 wu(0, air->own, "%s is flying %s mission from %s\n",
669 cname(plane_owner), mission_name(mission),
670 xyas(air->x, air->y, air->own));
673 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
677 air_damage(&b, x, y, mission, victim, s, hardtarget);
685 if (sect.sct_type == SCT_WATER)
686 nreport(plane_owner, N_SHP_BOMB, victim, 1);
688 nreport(plane_owner, N_SCT_BOMB, victim, 1);
691 /* free up all this memory */
694 glp = (struct genlist *)qp;
702 while (qp != (&escorts)) {
710 while (qp != (&bombers)) {
721 cando(int mission, int type)
725 if (type == EF_PLANE)
729 if (type == EF_PLANE)
733 if ((type == EF_PLANE) || (type == EF_SHIP))
741 if (type == EF_PLANE)
754 mission_name(short mission)
758 return "an interdiction";
762 return "a offensive support";
764 return "a defensive support";
770 return "a sub interdiction";
772 return "an air defense";
774 return "a mysterious";
778 show_mission(int type, struct nstr_item *np)
780 int first = 1, radius;
781 union empobj_storage item;
784 while (nxtitem(np, &item)) {
785 gp = (struct empobj *)&item;
786 if (!player->owner || gp->own == 0)
790 pr("Thing x,y op-sect rad mission\n");
793 pr("%-25s", obj_nameof(gp));
794 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
795 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
796 gp->mission == MI_OSUPPORT ||
797 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
799 oprange(gp, &radius);
800 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
801 if (radius < gp->radius)
804 pr(" %4d", gp->radius);
805 } else if (gp->mission == MI_RESERVE) {
809 getsect(gp->x, gp->y, §);
810 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
813 if (item.land.lnd_rad_max == 0)
816 plus += item.land.lnd_rad_max;
817 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
819 } else if (gp->mission == MI_ESCORT) {
821 pr(" %4d", item.plane.pln_range / 2);
825 pr(" is on %s mission\n", mission_name(gp->mission));
827 pr(" has no mission.\n");
832 oprange(struct empobj *gp, int *radius)
836 switch (gp->ef_type) {
838 range = ldround(effrange(((struct shpstr *)gp)->shp_frnge,
839 ((struct shpstr *)gp)->shp_tech), 1);
842 range = ldround(effrange(((struct lndstr *)gp)->lnd_frg,
843 ((struct lndstr *)gp)->lnd_tech), 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,
932 int mission_flags, int *tech)
934 struct emp_qelem *qp;
935 struct emp_qelem *next;
940 for (qp = list->q_forw; qp != list; qp = next) {
942 plp = (struct plist *)qp;
944 if (plp->plane.pln_x != x)
946 if (plp->plane.pln_y != y)
949 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
954 if (flags & (P_S | P_I)) {
955 if (plp->pcp->pl_flags & P_S)
956 mission_flags |= P_S;
957 if (plp->pcp->pl_flags & P_I)
958 mission_flags |= P_I;
960 if (*tech > plp->plane.pln_tech)
961 *tech = plp->plane.pln_tech;
962 if (!(plp->pcp->pl_flags & P_H))
963 /* no stealth on this mission */
964 mission_flags &= ~P_H;
965 if (!(plp->pcp->pl_flags & P_X))
966 /* no stealth on this mission */
967 mission_flags &= ~P_X;
968 if (!(plp->pcp->pl_flags & P_A)) {
969 /* no asw on this mission */
970 mission_flags &= ~P_A;
972 if (!(plp->pcp->pl_flags & P_MINE)) {
973 /* no asw on this mission */
974 mission_flags &= ~P_MINE;
978 * Mob costs for missions are 1/2 normal
981 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
982 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
985 return mission_flags;
989 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
992 struct plchrstr *pcp;
1004 if (pp->pln_ship >= 0) {
1005 getship(pp->pln_ship, &ship);
1006 item = ship.shp_item;
1007 } else if (pp->pln_land >= 0) {
1008 getland(pp->pln_land, &land);
1009 item = land.lnd_item;
1011 getsect(pp->pln_x, pp->pln_y, §);
1012 item = sect.sct_item;
1014 if (pcp->pl_fuel > item[I_PETROL]) {
1017 item[I_PETROL] -= pcp->pl_fuel;
1019 if (!(flags & P_F)) {
1025 if (pp->pln_nuketype == -1) {
1027 needed = pp->pln_load;
1031 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1034 needed = (pp->pln_load * 2) / ip->i_lbs;
1037 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1040 needed = (pp->pln_load * 2) / ip->i_lbs;
1043 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1046 needed = pp->pln_load / ip->i_lbs;
1049 if (pp->pln_nuketype == -1)
1052 case 'i': /* missile interception */
1055 needed = pp->pln_load;
1061 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1064 if (itype != I_NONE) {
1065 if (itype == I_SHELL && item[itype] < needed)
1066 item[itype] += supply_commod(plp->plane.pln_own,
1070 if (item[itype] < needed)
1072 item[itype] -= needed;
1074 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1075 plp->bombs = needed;
1079 if (pp->pln_ship >= 0)
1080 putship(ship.shp_uid, &ship);
1081 else if (pp->pln_land >= 0)
1082 putland(land.lnd_uid, &land);
1089 * Return 1 if this x,y pair is in the list
1092 find_airport(struct emp_qelem *airp, coord x, coord y)
1094 struct emp_qelem *qp;
1097 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1098 a = (struct airport *)qp;
1099 if ((a->x == x) && (a->y == y))
1106 /* #*# This needs to be changed to include acc's -KHS */
1108 add_airport(struct emp_qelem *airp, coord x, coord y)
1113 a = malloc(sizeof(struct airport));
1117 getsect(x, y, §);
1118 a->own = sect.sct_own;
1120 emp_insque((struct emp_qelem *)a, airp);
1124 * Take all the planes in list 1 that
1125 * are at x,y, and put them into list 2.
1128 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1130 struct emp_qelem *qp, *next;
1133 for (qp = l1->q_forw; qp != l1; qp = next) {
1135 plp = (struct plist *)qp;
1137 if (plp->plane.pln_x != x)
1139 if (plp->plane.pln_y != y)
1148 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1149 natid victim, char *s, int hardtarget)
1151 struct emp_qelem *qp;
1154 int newdam, dam = 0;
1158 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1159 plp = (struct plist *)qp;
1162 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1169 if (plp->pcp->pl_flags & P_A) {
1170 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1172 "\t%s detects sub movement in %s\n",
1173 prplane(pp), xyas(x, y, pp->pln_own));
1176 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1178 "\t%s tracks %s %s at %s\n",
1179 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1183 "\t%s depth-charging %s %s in %s\n",
1184 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1187 "\t%s pinbombing %s %s in %s\n",
1188 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1190 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1191 if (plp->plane.pln_nuketype != -1)
1193 else if (hardtarget != SECT_HARDTARGET)
1194 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1195 /* Always calculate damage */
1196 if (roll(100) <= hitchance) {
1197 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1199 if (mission == MI_INTERDICT) {
1201 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1202 prplane(pp), nukedam, cname(victim), s);
1207 "\t\thit %s %s for %d damage\n",
1208 cname(victim), s, newdam);
1212 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1213 wu(0, pp->pln_own, "missed\n");
1214 if (mission == MI_SINTERDICT) {
1216 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1217 xyas(x, y, victim));
1218 } else if (*s == 's') {
1219 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1220 s, xyas(x, y, victim));
1222 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1223 s, xyas(x, y, victim));
1225 /* Now, even though we missed, the bombs
1227 collateral_damage(x, y, newdam, bombers);
1230 /* use up missiles */
1231 if (plp->pcp->pl_flags & P_M)
1239 * Check to see if anyone hostile to the victim
1240 * is running an air defense mission on this
1241 * sector. If so, do air combat
1244 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1245 struct emp_qelem *esc_list)
1248 int mission_flags, tech, combat = 0, rel, dist, z;
1249 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1251 struct genlist *glp;
1253 struct genlist mi[MAXNOC];
1260 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1262 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1265 memset(mi, 0, sizeof(mi));
1266 for (z = 1; z < MAXNOC; z++)
1267 emp_initque((struct emp_qelem *)&mi[z]);
1269 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1271 for (cn = 1; cn < MAXNOC; cn++) {
1272 /* Check our relations */
1273 rel = getrel(getnatp(cn), victim);
1278 if (QEMPTY(&mi[cn].queue))
1281 /* Ok, make a list of all the interceptors. Note that this *copies* the
1282 * list from the mission creation. This list must be deleted later. */
1283 emp_initque(&interceptors);
1284 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1286 glp = (struct genlist *)qp;
1288 if (CANT_HAPPEN(gp->ef_type != EF_PLANE))
1291 dist = mapdist(x, y, gp->x, gp->y);
1293 plp = malloc(sizeof(struct plist));
1294 memset(plp, 0, sizeof(struct plist));
1296 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1298 /* missiles go one way, so we can use all the range */
1299 if (!(plp->pcp->pl_flags & P_M))
1301 /* If it's out of range, free it and continue on */
1302 if (dist > plp->plane.pln_range) {
1306 emp_insque(&plp->queue, &interceptors);
1309 /* Remove those who cannot go */
1310 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1312 if (QEMPTY(&interceptors))
1315 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1316 * that the higher numbered planes go into battle (they should be the better ones
1317 * at fighting, if all went well.) */
1319 for (qp = interceptors.q_forw; qp != (&interceptors);
1322 tcount -= count * 2;
1323 /* Just in case there are more incoming than we have */
1326 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1330 /* Free it up and continue */
1332 glp = (struct genlist *)qp;
1337 /* Now, make a list of all the airports these planes are coming from */
1339 for (qp = interceptors.q_forw; qp != (&interceptors);
1341 plp = (struct plist *)qp;
1342 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1343 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1346 /* Now, fly them out one airport at a time */
1347 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1348 struct airport *air;
1350 air = (struct airport *)qp;
1351 dist = mapdist(x, y, air->x, air->y);
1355 /* Split off the interceptors at this base into i */
1356 divide(&interceptors, &i, air->x, air->y);
1360 mission_flags |= P_X; /* stealth (shhh) */
1361 /* gets turned off if not all choppers */
1362 mission_flags |= P_H;
1363 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1364 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1366 /* Did we run out of interceptors? */
1369 /* Did we run out of bombers? */
1370 if (QEMPTY(bomb_list)) {
1371 /* Yes, so we have to put the rest of the interceptors back, and
1372 then continue, or we leak memory */
1377 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1378 mission_flags, &tech);
1380 /* Did we run out of interceptors? */
1383 /* Did we run out of bombers? */
1384 if (QEMPTY(bomb_list)) {
1385 /* Yes, so we have to put the rest of the interceptors back, and
1386 then continue, or we leak memory */
1391 path = BestAirPath(buf, air->x, air->y, x, y);
1392 if (CANT_HAPPEN(!path))
1394 wu(0, cn, "Flying %s mission from %s\n",
1395 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1396 if (air->own && (air->own != cn)) {
1397 wu(0, air->own, "%s is flying %s mission from %s\n",
1398 cname(cn), mission_name(MI_AIR_DEFENSE),
1399 xyas(air->x, air->y, air->own));
1402 /* Now, fly the planes to the sector */
1403 emp_initque(&empty);
1404 ac_encounter(&i, &empty, air->x, air->y,
1405 path, mission_flags, 1, bomb_list, esc_list);
1407 /* If none made it, continue */
1411 /* Some made it, so now they get to try to fight. */
1412 /* Intercept the escorts first */
1414 if (!QEMPTY(esc_list)) {
1415 mpr(victim, "%s air defense planes intercept!\n",
1417 ac_combat_headers(victim, cn);
1418 ac_airtoair(esc_list, &i);
1421 /* Now intercept the bombers */
1422 if (!QEMPTY(bomb_list)) {
1424 mpr(victim, "%s air defense planes intercept!\n",
1426 ac_combat_headers(victim, cn);
1428 ac_airtoair(bomb_list, &i);
1437 /* We have to free all of these, if they are still there, otherwise they get
1438 lost and we leak memory all over the place. */
1439 for (cn = 1; cn < MAXNOC; cn++) {
1440 /* free up all this memory if it's still there */
1441 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1443 glp = (struct genlist *)qp;