2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 int type; /* unit type: EF_SHIP, EF_PLANE, EF_LAND */
55 void *cp; /* pointer to desc of thing */
56 void *thing; /* thing's struct */
60 struct emp_qelem queue;
65 static void add_airport(struct emp_qelem *, coord, coord);
66 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
68 static void build_mission_list(struct genlist *, coord, coord, int, natid);
69 static void build_mission_list_type(struct genlist *, coord, coord, int,
71 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
72 static int dosupport(struct genlist *, coord, coord, natid, natid);
73 static int find_airport(struct emp_qelem *, coord, coord);
74 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
75 int, struct ichrstr *, int, int, int *);
76 static void mission_pln_sel(struct emp_qelem *, int, int, int);
77 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
81 * Interdict commodities & transported planes
84 ground_interdict(coord x, coord y, natid victim, char *s)
87 int dam = 0, newdam, rel;
88 struct genlist mi[MAXNOC];
91 memset(mi, 0, sizeof(mi));
92 for (z = 1; z < MAXNOC; z++)
93 emp_initque((struct emp_qelem *)&mi[z]);
95 build_mission_list(mi, x, y, MI_INTERDICT, victim);
97 for (cn = 1; cn < MAXNOC; cn++) {
98 rel = getrel(getnatp(cn), victim);
102 if (QEMPTY(&mi[cn].queue))
105 newdam = perform_mission(x, y, victim, &mi[cn].queue,
106 MI_INTERDICT, s, SECT_HARDTARGET);
109 mpr(victim, "%s interdiction mission does %d damage!\n",
113 collateral_damage(x, y, dam, 0);
119 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
127 getsect(x, y, §);
129 coll = ldround((double)dam * collateral_dam, 1);
132 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
133 xyas(x, y, sect.sct_own), coll);
134 sectdamage(§, coll, list);
142 only_subs(struct emp_qelem *list)
144 struct emp_qelem *qp;
148 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
149 glp = (struct genlist *)qp;
151 if (glp->type != EF_SHIP)
154 if (!(mcp->m_flags & M_SUB))
158 /* They were all subs! */
164 * Interdict ships & land units
167 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
172 struct genlist mi[MAXNOC];
176 memset(mi, 0, sizeof(mi));
177 for (z = 1; z < MAXNOC; z++)
178 emp_initque((struct emp_qelem *)&mi[z]);
180 build_mission_list(mi, x, y, mission, victim);
182 for (cn = 1; cn < MAXNOC; cn++) {
185 if (mission == MI_SINTERDICT) {
186 if (getrel(getnatp(cn), victim) >= FRIENDLY)
188 } else if (getrel(getnatp(cn), victim) > HOSTILE)
191 if (QEMPTY(&mi[cn].queue))
194 osubs = only_subs(&mi[cn].queue);
195 newdam = perform_mission(x, y, victim, &mi[cn].queue,
196 mission, s, hardtarget);
199 /* If only subs responded, then we don't know who's
201 mpr(victim, "%s interdiction mission does %d damage!\n",
202 osubs ? "Enemy" : cname(cn), newdam);
206 collateral_damage(x, y, dam, 0);
212 * Perform a mission against victim, on behalf of actee
215 off_support(coord x, coord y, natid victim, natid actee)
218 struct genlist mi[MAXNOC];
221 memset(mi, 0, sizeof(mi));
222 for (z = 1; z < MAXNOC; z++)
223 emp_initque((struct emp_qelem *)&mi[z]);
225 build_mission_list(mi, x, y, MI_SUPPORT, victim);
226 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
228 dam = dosupport(mi, x, y, victim, actee);
233 * Perform a mission against victim, on behalf of actee
236 def_support(coord x, coord y, natid victim, natid actee)
239 struct genlist mi[MAXNOC];
242 memset(mi, 0, sizeof(mi));
243 for (z = 1; z < MAXNOC; z++)
244 emp_initque((struct emp_qelem *)&mi[z]);
246 build_mission_list(mi, x, y, MI_SUPPORT, victim);
247 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
249 dam = dosupport(mi, x, y, victim, actee);
254 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
260 for (cn = 1; cn < MAXNOC; cn++) {
261 rel = getrel(getnatp(cn), actee);
262 if ((cn != actee) && (rel != ALLIED))
264 rel = getrel(getnatp(cn), victim);
265 if ((cn != actee) && (rel != AT_WAR))
268 if (QEMPTY(&mi[cn].queue))
271 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
272 "", SECT_HARDTARGET);
278 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
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);
287 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
288 int type, natid victim)
293 union empobj_storage item;
299 snxtitem_all(&ni, type);
300 while (nxtitem(&ni, &item)) {
301 gp = (struct empobj *)&item;
309 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
312 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
313 (gp->mission != MI_INTERDICT))
316 relat = getrel(getnatp(gp->own), victim);
317 if (mission == MI_SINTERDICT) {
318 if (relat >= FRIENDLY)
320 else if (type != EF_PLANE && relat > HOSTILE)
322 } else if (relat > HOSTILE)
325 dist = mapdist(x, y, gp->opx, gp->opy);
328 if (mission != MI_RESERVE) /* XXX */
329 oprange(gp, type, &radius);
334 /* Ok, it is within the operations range. */
335 /* Now check from where the object actually is */
336 dist = mapdist(x, y, gp->x, gp->y);
338 oprange(gp, type, &radius);
341 /* Ok, the object can get to where the x,y is */
344 if (mission != MI_AIR_DEFENSE) {
345 getsect(x, y, §);
346 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
349 * If the player->owner of the unit isn't at war
350 * with the victim, and doesn't own the
351 * sect being acted upon, and isn't the
352 * old player->owner of that sect, bounce them.
354 if (sect.sct_type != SCT_WATER &&
355 sect.sct_own != gp->own &&
356 sect.sct_oldown != gp->own)
362 glp = malloc(sizeof(struct genlist));
363 memset(glp, 0, sizeof(struct genlist));
367 glp->cp = &lchr[(int)gp->type];
370 glp->cp = &mchr[(int)gp->type];
373 glp->cp = &plchr[(int)gp->type];
376 glp->thing = malloc(sizeof(item));
377 memcpy(glp->thing, &item, sizeof(item));
378 emp_insque(&glp->queue, &mi[gp->own].queue);
383 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
390 snxtitem_all(&ni, EF_PLANE);
391 while (nxtitem(&ni, &plane)) {
392 if (plane.pln_own != cn)
395 if (plane.pln_mission != MI_ESCORT)
398 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
399 if (dist > plane.pln_range / 2)
402 plp = malloc(sizeof(struct plist));
403 memset(plp, 0, sizeof(struct plist));
404 plp->pcp = &plchr[(int)plane.pln_type];
406 emp_insque(&plp->queue, escorts);
411 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
412 int mission, char *s, int hardtarget)
414 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
415 struct emp_qelem *newqp;
423 struct plchrstr *pcp;
424 int dam = 0, dam2, mission_flags, tech;
425 natid plane_owner = 0;
426 int gun, shell, md, range, air_dam = 0;
427 double prb, hitchance, vrange;
429 getsect(x, y, §);
431 emp_initque(&missiles);
432 emp_initque(&bombers);
433 emp_initque(&escorts);
436 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
437 glp = (struct genlist *)qp;
440 md = mapdist(x, y, gp->x, gp->y);
442 if (glp->type == EF_LAND) {
445 if (lp->lnd_effic < LAND_MINFIREEFF)
448 if (mission == MI_SINTERDICT)
451 if ((mission == MI_INTERDICT) &&
452 (md > land_max_interdiction_range))
455 if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
458 if (lp->lnd_item[I_MILIT] < 1)
461 range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
465 shell = lp->lnd_item[I_SHELL];
466 gun = lp->lnd_item[I_GUN];
467 if (shell == 0 || gun == 0)
470 if (has_supply(lp)) {
472 putland(lp->lnd_uid, lp);
473 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
474 lp->lnd_ammo, shell), 1);
475 if (sect.sct_type == SCT_WATER) {
476 if (chance(lp->lnd_acc / 100.0))
477 dam2 = ldround(dam2 / 2.0, 1);
480 if (sect.sct_type == SCT_WATER)
481 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
483 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
485 "%s fires at %s %s at %s\n",
486 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
488 mpr(victim, "%s %s fires at you at %s\n",
489 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
491 } else if (glp->type == EF_SHIP) {
495 if (sp->shp_effic < 60)
497 if (sp->shp_frnge == 0)
499 if (((mission == MI_INTERDICT) ||
500 (mission == MI_SINTERDICT)) &&
501 (md > ship_max_interdiction_range))
503 if (sp->shp_item[I_MILIT] < 1)
506 if ((mcp->m_flags & M_SUB) &&
507 (sect.sct_type != SCT_WATER))
510 if (mission == MI_SINTERDICT) {
511 if (!(mcp->m_flags & M_SONAR))
513 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
515 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
516 vrange *= sp->shp_effic / 200.0;
519 /* can't look all the time */
523 if (mcp->m_flags & M_SUB) {
524 /* If we aren't shooting at "subs" or "ships" don't fire at all from
528 if (sp->shp_mobil < 0)
530 gun = sp->shp_item[I_GUN];
533 shell = sp->shp_item[I_SHELL];
534 if (shell < SHP_TORP_SHELLS)
535 shell += supply_commod(sp->shp_own,
536 sp->shp_x, sp->shp_y, I_SHELL,
537 SHP_TORP_SHELLS - shell);
538 if (shell < SHP_TORP_SHELLS)
541 range = roundrange(torprange(sp));
545 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
547 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
548 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
549 putship(sp->shp_uid, sp);
550 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
553 "%s locking on %s %s in %s\n",
554 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
556 "\tEffective torpedo range is %d.0\n", range);
558 "\tWhooosh... Hitchance = %d%%\n",
559 (int)(hitchance * 100));
561 if (hitchance < 1.0 && !chance(hitchance)) {
562 wu(0, sp->shp_own, "\tMissed\n");
564 "Incoming torpedo sighted @ %s missed (whew)!\n",
568 wu(0, sp->shp_own, "\tBOOM!...\n");
569 dam2 = TORP_DAMAGE();
572 nreport(victim, N_TORP_SHIP, 0, 1);
574 "\tTorpedo hit %s %s for %d damage\n",
575 cname(victim), s, dam2);
578 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
579 xyas(x, y, victim), dam2);
581 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
584 gun = sp->shp_item[I_GUN];
585 gun = MIN(gun, sp->shp_glim);
586 shell = sp->shp_item[I_SHELL];
588 shell += supply_commod(sp->shp_own,
589 sp->shp_x, sp->shp_y, I_SHELL,
591 gun = MIN(gun, shell);
592 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
596 dam2 = seagun(sp->shp_effic, gun);
600 prb = (double)md / range;
605 if (sect.sct_type == SCT_WATER)
606 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
608 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
610 "%s fires at %s %s at %s\n",
611 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
613 mpr(victim, "%s %s fires at you at %s\n",
614 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
616 sp->shp_item[I_SHELL] = shell - gun;
617 putship(sp->shp_uid, sp);
619 } else if (glp->type == EF_PLANE) {
621 if (pcp->pl_flags & P_M)
622 /* units have their own missile interdiction */
623 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
626 /* save planes for later */
627 plp = malloc(sizeof(struct plist));
629 memset(plp, 0, sizeof(struct plist));
631 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
632 if (plp->pcp->pl_flags & P_M)
633 emp_insque(&plp->queue, &missiles);
635 emp_insque(&plp->queue, &bombers);
636 plane_owner = plp->plane.pln_own;
639 if (!QEMPTY(&missiles)) {
640 /* I arbitrarily chose 100 mindam -KHS */
642 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
643 "sector", victim, mission);
644 qp = missiles.q_forw;
645 while (qp != (&missiles)) {
653 if (QEMPTY(&bombers)) {
656 glp = (struct genlist *)qp;
665 * If there are planes performing an
666 * interdict or support mission, find
667 * some escorts for them, if possible.
668 * Up to 2 per bomber, if possible.
670 find_escorts(x, y, plane_owner, &escorts);
672 if (mission == MI_SINTERDICT)
673 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
675 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
677 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
679 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
680 plp = (struct plist *)qp;
681 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
682 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
685 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
690 air = (struct airport *)qp;
691 md = mapdist(x, y, air->x, air->y);
696 /* Split off the bombers at this base into b */
697 divide(&bombers, &b, air->x, air->y);
699 /* Split off the escorts at this base into e */
700 divide(&escorts, &e, air->x, air->y);
704 mission_flags |= P_X; /* stealth (shhh) */
705 mission_flags |= P_H; /* gets turned off if not all choppers */
707 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
708 0, mission_flags, &tech);
713 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
714 P_F | P_ESC, mission_flags, &tech);
716 pp = BestAirPath(buf, air->x, air->y, x, y);
717 if (CANT_HAPPEN(!pp))
719 wu(0, plane_owner, "Flying %s mission from %s\n",
720 mission_name(mission), xyas(air->x, air->y, plane_owner));
721 if (air->own && (air->own != plane_owner)) {
722 wu(0, air->own, "%s is flying %s mission from %s\n",
723 cname(plane_owner), mission_name(mission),
724 xyas(air->x, air->y, air->own));
727 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
731 air_damage(&b, x, y, mission, victim, s, hardtarget);
739 if (sect.sct_type == SCT_WATER)
740 nreport(plane_owner, N_SHP_BOMB, victim, 1);
742 nreport(plane_owner, N_SCT_BOMB, victim, 1);
745 /* free up all this memory */
748 glp = (struct genlist *)qp;
756 while (qp != (&escorts)) {
764 while (qp != (&bombers)) {
775 cando(int mission, int type)
779 if (type == EF_PLANE)
783 if (type == EF_PLANE)
787 if ((type == EF_PLANE) || (type == EF_SHIP))
795 if (type == EF_PLANE)
808 nameofitem(struct empobj *gp, int type)
812 return prship((struct shpstr *)gp);
814 return prplane((struct plnstr *)gp);
816 return prland((struct lndstr *)gp);
822 mission_name(short mission)
826 return "an interdiction";
830 return "a offensive support";
832 return "a defensive support";
838 return "a sub interdiction";
840 return "an air defense";
842 return "a mysterious";
846 show_mission(int type, struct nstr_item *np)
848 int first = 1, radius;
849 union empobj_storage item;
852 while (nxtitem(np, &item)) {
853 gp = (struct empobj *)&item;
854 if (!player->owner || gp->own == 0)
858 pr("Thing x,y op-sect rad mission\n");
861 pr("%-25s", nameofitem(gp, type));
862 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
863 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
864 gp->mission == MI_OSUPPORT ||
865 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
867 oprange(gp, type, &radius);
868 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
869 if (radius < gp->radius)
872 pr(" %4d", gp->radius);
873 } else if (gp->mission == MI_RESERVE) {
877 getsect(gp->x, gp->y, §);
878 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
881 if (item.land.lnd_rad_max == 0)
884 plus += item.land.lnd_rad_max;
885 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
887 } else if (gp->mission == MI_ESCORT) {
889 pr(" %4d", item.plane.pln_range / 2);
893 pr(" is on %s mission\n", mission_name(gp->mission));
895 pr(" has no mission.\n");
900 oprange(struct empobj *gp, int type, int *radius)
909 getship(gp->uid, &ship);
910 range = ldround(effrange(ship.shp_frnge, ship.shp_tech), 1);
913 getland(gp->uid, &land);
914 range = ldround(effrange(land.lnd_frg, land.lnd_tech), 1);
917 getplane(gp->uid, &plane);
918 /* missiles go one way, so we can use all the range */
919 if (plchr[(int)plane.pln_type].pl_flags & P_M)
920 range = plane.pln_range;
922 range = plane.pln_range / 2;
925 CANT_HAPPEN("bad TYPE");
936 * Remove all planes who cannot go on
937 * the mission from the plane list.
940 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
943 struct emp_qelem *qp, *next;
948 struct plchrstr *pcp;
953 for (qp = list->q_forw; qp != list; qp = next) {
955 plp = (struct plist *)qp;
959 if (pp->pln_effic < 40) {
965 if (pp->pln_mobil < 1) {
972 if (ontradingblock(EF_PLANE, pp)) {
982 for (x = 0; x < sizeof(wantflags) * 8; x++) {
984 if ((wantflags & y) == y)
985 if ((pcp->pl_flags & y) != y) {
1007 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1011 if ((pcp->pl_flags & P_E) ||
1012 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1024 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1026 if ((nowantflags & y) == y)
1027 if ((pcp->pl_flags & y) == y)
1036 if (pp->pln_ship >= 0) {
1037 if (!getship(pp->pln_ship, &ship)) {
1040 putplane(pp->pln_uid, pp);
1045 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1048 if (ship.shp_effic < SHIP_MINEFF) {
1051 /* Can't fly off of inefficient or non-owned, non-allied ships */
1052 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1053 ((ship.shp_own != pp->pln_own) &&
1054 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1060 if (pp->pln_land >= 0) {
1061 if (!getland(pp->pln_land, &land)) {
1064 putplane(pp->pln_uid, pp);
1069 if (!(pcp->pl_flags & P_E))
1071 if (land.lnd_effic < LAND_MINEFF)
1074 /* Can't fly off of inefficient or non-owned, non-allied units */
1075 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1076 ((land.lnd_own != pp->pln_own) &&
1077 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1083 /* Can't fly off units in ships or other units */
1084 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1090 /* Now, check the sector status if not on a plane or unit */
1091 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1092 /* If we can't get the sector, we can't check it, and can't fly */
1093 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1098 /* First, check allied status */
1099 /* Can't fly from non-owned sectors or non-allied sectors */
1100 if ((sect.sct_own != pp->pln_own) &&
1101 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1106 /* non-vtol plane */
1107 if ((pcp->pl_flags & P_V) == 0) {
1108 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1115 if (pcp->pl_flags & P_A) {
1116 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1123 putplane(pp->pln_uid, pp);
1128 * Arm only the planes at x,y
1131 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1132 int mission, struct ichrstr *ip, int flags,
1133 int mission_flags, int *tech)
1135 struct emp_qelem *qp;
1136 struct emp_qelem *next;
1141 for (qp = list->q_forw; qp != list; qp = next) {
1143 plp = (struct plist *)qp;
1145 if (plp->plane.pln_x != x)
1147 if (plp->plane.pln_y != y)
1150 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1155 if (flags & (P_S | P_I)) {
1156 if (plp->pcp->pl_flags & P_S)
1157 mission_flags |= P_S;
1158 if (plp->pcp->pl_flags & P_I)
1159 mission_flags |= P_I;
1161 if (*tech > plp->plane.pln_tech)
1162 *tech = plp->plane.pln_tech;
1163 if (!(plp->pcp->pl_flags & P_H))
1164 /* no stealth on this mission */
1165 mission_flags &= ~P_H;
1166 if (!(plp->pcp->pl_flags & P_X))
1167 /* no stealth on this mission */
1168 mission_flags &= ~P_X;
1169 if (!(plp->pcp->pl_flags & P_A)) {
1170 /* no asw on this mission */
1171 mission_flags &= ~P_A;
1173 if (!(plp->pcp->pl_flags & P_MINE)) {
1174 /* no asw on this mission */
1175 mission_flags &= ~P_MINE;
1179 * Mob costs for missions are 1/2 normal
1182 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1183 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1186 return mission_flags;
1190 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1193 struct plchrstr *pcp;
1205 if (pp->pln_ship >= 0) {
1206 getship(pp->pln_ship, &ship);
1207 item = ship.shp_item;
1208 } else if (pp->pln_land >= 0) {
1209 getland(pp->pln_land, &land);
1210 item = land.lnd_item;
1212 getsect(pp->pln_x, pp->pln_y, §);
1213 item = sect.sct_item;
1215 if (pcp->pl_fuel > item[I_PETROL]) {
1218 item[I_PETROL] -= pcp->pl_fuel;
1220 if (!(flags & P_F)) {
1226 if (pp->pln_nuketype == -1) {
1228 needed = pp->pln_load;
1232 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1235 needed = (pp->pln_load * 2) / ip->i_lbs;
1238 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1241 needed = (pp->pln_load * 2) / ip->i_lbs;
1244 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1247 needed = pp->pln_load / ip->i_lbs;
1250 if (pp->pln_nuketype == -1)
1253 case 'i': /* missile interception */
1256 needed = pp->pln_load;
1262 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1265 if (itype != I_NONE) {
1266 if (itype == I_SHELL && item[itype] < needed)
1267 item[itype] += supply_commod(plp->plane.pln_own,
1271 if (item[itype] < needed)
1273 item[itype] -= needed;
1275 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1276 plp->bombs = needed;
1280 if (pp->pln_ship >= 0)
1281 putship(ship.shp_uid, &ship);
1282 else if (pp->pln_land >= 0)
1283 putland(land.lnd_uid, &land);
1290 * Return 1 if this x,y pair is in the list
1293 find_airport(struct emp_qelem *airp, coord x, coord y)
1295 struct emp_qelem *qp;
1298 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1299 a = (struct airport *)qp;
1300 if ((a->x == x) && (a->y == y))
1307 /* #*# This needs to be changed to include acc's -KHS */
1309 add_airport(struct emp_qelem *airp, coord x, coord y)
1314 a = malloc(sizeof(struct airport));
1318 getsect(x, y, §);
1319 a->own = sect.sct_own;
1321 emp_insque((struct emp_qelem *)a, airp);
1325 * Take all the planes in list 1 that
1326 * are at x,y, and put them into list 2.
1329 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1331 struct emp_qelem *qp, *next;
1334 for (qp = l1->q_forw; qp != l1; qp = next) {
1336 plp = (struct plist *)qp;
1338 if (plp->plane.pln_x != x)
1340 if (plp->plane.pln_y != y)
1349 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1350 natid victim, char *s, int hardtarget)
1352 struct emp_qelem *qp;
1355 int newdam, dam = 0;
1359 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1360 plp = (struct plist *)qp;
1363 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1370 if (plp->pcp->pl_flags & P_A) {
1371 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1373 "\t%s detects sub movement in %s\n",
1374 prplane(pp), xyas(x, y, pp->pln_own));
1377 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1379 "\t%s tracks %s %s at %s\n",
1380 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1384 "\t%s depth-charging %s %s in %s\n",
1385 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1388 "\t%s pinbombing %s %s in %s\n",
1389 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1391 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1392 if (plp->plane.pln_nuketype != -1)
1394 else if (hardtarget != SECT_HARDTARGET)
1395 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1396 /* Always calculate damage */
1397 if (roll(100) <= hitchance) {
1398 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1400 if (mission == MI_INTERDICT) {
1402 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1403 prplane(pp), nukedam, cname(victim), s);
1408 "\t\thit %s %s for %d damage\n",
1409 cname(victim), s, newdam);
1413 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1414 wu(0, pp->pln_own, "missed\n");
1415 if (mission == MI_SINTERDICT) {
1417 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1418 xyas(x, y, victim));
1419 } else if (*s == 's') {
1420 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1421 s, xyas(x, y, victim));
1423 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1424 s, xyas(x, y, victim));
1426 /* Now, even though we missed, the bombs
1428 collateral_damage(x, y, newdam, bombers);
1431 /* use up missiles */
1432 if (plp->pcp->pl_flags & P_M)
1440 * Check to see if anyone hostile to the victim
1441 * is running an air defense mission on this
1442 * sector. If so, do air combat
1445 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1446 struct emp_qelem *esc_list)
1449 int mission_flags, tech, combat = 0, rel, dist, z;
1450 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1452 struct genlist *glp;
1454 struct genlist mi[MAXNOC];
1461 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1463 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1466 memset(mi, 0, sizeof(mi));
1467 for (z = 1; z < MAXNOC; z++)
1468 emp_initque((struct emp_qelem *)&mi[z]);
1470 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1472 for (cn = 1; cn < MAXNOC; cn++) {
1473 /* Check our relations */
1474 rel = getrel(getnatp(cn), victim);
1479 if (QEMPTY(&mi[cn].queue))
1482 /* Ok, make a list of all the interceptors. Note that this *copies* the
1483 * list from the mission creation. This list must be deleted later. */
1484 emp_initque(&interceptors);
1485 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1487 glp = (struct genlist *)qp;
1490 dist = mapdist(x, y, gp->x, gp->y);
1492 plp = malloc(sizeof(struct plist));
1493 memset(plp, 0, sizeof(struct plist));
1495 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1497 /* missiles go one way, so we can use all the range */
1498 if (!(plp->pcp->pl_flags & P_M))
1500 /* If it's out of range, free it and continue on */
1501 if (dist > plp->plane.pln_range) {
1505 emp_insque(&plp->queue, &interceptors);
1508 /* Remove those who cannot go */
1509 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1511 if (QEMPTY(&interceptors))
1514 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1515 * that the higher numbered planes go into battle (they should be the better ones
1516 * at fighting, if all went well.) */
1518 for (qp = interceptors.q_forw; qp != (&interceptors);
1521 tcount -= count * 2;
1522 /* Just in case there are more incoming than we have */
1525 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1529 /* Free it up and continue */
1531 glp = (struct genlist *)qp;
1536 /* Now, make a list of all the airports these planes are coming from */
1538 for (qp = interceptors.q_forw; qp != (&interceptors);
1540 plp = (struct plist *)qp;
1541 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1542 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1545 /* Now, fly them out one airport at a time */
1546 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1547 struct airport *air;
1549 air = (struct airport *)qp;
1550 dist = mapdist(x, y, air->x, air->y);
1554 /* Split off the interceptors at this base into i */
1555 divide(&interceptors, &i, air->x, air->y);
1559 mission_flags |= P_X; /* stealth (shhh) */
1560 /* gets turned off if not all choppers */
1561 mission_flags |= P_H;
1562 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1563 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1565 /* Did we run out of interceptors? */
1568 /* Did we run out of bombers? */
1569 if (QEMPTY(bomb_list)) {
1570 /* Yes, so we have to put the rest of the interceptors back, and
1571 then continue, or we leak memory */
1576 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1577 mission_flags, &tech);
1579 /* Did we run out of interceptors? */
1582 /* Did we run out of bombers? */
1583 if (QEMPTY(bomb_list)) {
1584 /* Yes, so we have to put the rest of the interceptors back, and
1585 then continue, or we leak memory */
1590 path = BestAirPath(buf, air->x, air->y, x, y);
1591 if (CANT_HAPPEN(!path))
1593 wu(0, cn, "Flying %s mission from %s\n",
1594 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1595 if (air->own && (air->own != cn)) {
1596 wu(0, air->own, "%s is flying %s mission from %s\n",
1597 cname(cn), mission_name(MI_AIR_DEFENSE),
1598 xyas(air->x, air->y, air->own));
1601 /* Now, fly the planes to the sector */
1602 emp_initque(&empty);
1603 ac_encounter(&i, &empty, air->x, air->y,
1604 path, mission_flags, 1, bomb_list, esc_list);
1606 /* If none made it, continue */
1610 /* Some made it, so now they get to try to fight. */
1611 /* Intercept the escorts first */
1613 if (!QEMPTY(esc_list)) {
1614 mpr(victim, "%s air defense planes intercept!\n",
1616 ac_combat_headers(victim, cn);
1617 ac_airtoair(esc_list, &i);
1620 /* Now intercept the bombers */
1621 if (!QEMPTY(bomb_list)) {
1623 mpr(victim, "%s air defense planes intercept!\n",
1625 ac_combat_headers(victim, cn);
1627 ac_airtoair(bomb_list, &i);
1636 /* We have to free all of these, if they are still there, otherwise they get
1637 lost and we leak memory all over the place. */
1638 for (cn = 1; cn < MAXNOC; cn++) {
1639 /* free up all this memory if it's still there */
1640 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1642 glp = (struct genlist *)qp;