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
55 #include "prototypes.h"
59 struct emp_qelem queue; /* list of units */
60 int type; /* unit type: EF_SHIP, EF_PLANE, EF_LAND */
61 void *cp; /* pointer to desc of thing */
62 void *thing; /* thing's struct */
66 struct emp_qelem queue;
77 static void add_airport(struct emp_qelem *, coord, coord);
78 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
80 static void build_mission_list(struct genlist *, coord, coord, int, natid);
81 static void build_mission_list_type(struct genlist *, coord, coord, int,
83 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
84 static int dosupport(struct genlist *, coord, coord, natid, natid);
85 static int find_airport(struct emp_qelem *, coord, coord);
86 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
87 int, struct ichrstr *, int, int, int *);
88 static void mission_pln_sel(struct emp_qelem *, int, int, int);
89 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
93 * Interdict commodities & transported planes
96 ground_interdict(coord x, coord y, natid victim, char *s)
99 int dam = 0, newdam, rel;
100 struct genlist mi[MAXNOC];
103 memset(mi, 0, sizeof(mi));
104 for (z = 1; z < MAXNOC; z++)
105 emp_initque((struct emp_qelem *)&mi[z]);
107 build_mission_list(mi, x, y, MI_INTERDICT, victim);
109 for (cn = 1; cn < MAXNOC; cn++) {
110 rel = getrel(getnatp(cn), victim);
114 if (QEMPTY(&mi[cn].queue))
117 newdam = perform_mission(x, y, victim, &mi[cn].queue,
118 MI_INTERDICT, s, SECT_HARDTARGET);
121 mpr(victim, "%s interdiction mission does %d damage!\n",
125 collateral_damage(x, y, dam, 0);
131 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
139 getsect(x, y, §);
141 coll = ldround((double)dam * collateral_dam, 1);
144 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
145 xyas(x, y, sect.sct_own), coll);
146 sectdamage(§, coll, list);
154 only_subs(struct emp_qelem *list)
156 struct emp_qelem *qp;
160 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
161 glp = (struct genlist *)qp;
163 if (glp->type != EF_SHIP)
166 if (!(mcp->m_flags & M_SUB))
170 /* They were all subs! */
176 * Interdict ships & land units
179 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
184 struct genlist mi[MAXNOC];
188 memset(mi, 0, sizeof(mi));
189 for (z = 1; z < MAXNOC; z++)
190 emp_initque((struct emp_qelem *)&mi[z]);
192 build_mission_list(mi, x, y, mission, victim);
194 for (cn = 1; cn < MAXNOC; cn++) {
197 if (mission == MI_SINTERDICT) {
198 if (getrel(getnatp(cn), victim) >= FRIENDLY)
200 } else if (getrel(getnatp(cn), victim) > HOSTILE)
203 if (QEMPTY(&mi[cn].queue))
206 osubs = only_subs(&mi[cn].queue);
207 newdam = perform_mission(x, y, victim, &mi[cn].queue,
208 mission, s, hardtarget);
211 /* If only subs responded, then we don't know who's
213 mpr(victim, "%s interdiction mission does %d damage!\n",
214 osubs ? "Enemy" : cname(cn), newdam);
218 collateral_damage(x, y, dam, 0);
224 * Perform a mission against victim, on behalf of actee
227 off_support(coord x, coord y, natid victim, natid actee)
230 struct genlist mi[MAXNOC];
233 memset(mi, 0, sizeof(mi));
234 for (z = 1; z < MAXNOC; z++)
235 emp_initque((struct emp_qelem *)&mi[z]);
237 build_mission_list(mi, x, y, MI_SUPPORT, victim);
238 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
240 dam = dosupport(mi, x, y, victim, actee);
245 * Perform a mission against victim, on behalf of actee
248 def_support(coord x, coord y, natid victim, natid actee)
251 struct genlist mi[MAXNOC];
254 memset(mi, 0, sizeof(mi));
255 for (z = 1; z < MAXNOC; z++)
256 emp_initque((struct emp_qelem *)&mi[z]);
258 build_mission_list(mi, x, y, MI_SUPPORT, victim);
259 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
261 dam = dosupport(mi, x, y, victim, actee);
266 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
272 for (cn = 1; cn < MAXNOC; cn++) {
273 rel = getrel(getnatp(cn), actee);
274 if ((cn != actee) && (rel != ALLIED))
276 rel = getrel(getnatp(cn), victim);
277 if ((cn != actee) && (rel != AT_WAR))
280 if (QEMPTY(&mi[cn].queue))
283 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
284 "", SECT_HARDTARGET);
290 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
293 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
294 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
295 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
299 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
300 int type, natid victim)
311 snxtitem_all(&ni, type);
312 while (nxtitem(&ni, &item)) {
313 gp = (struct genitem *)&item;
321 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
324 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
325 (gp->mission != MI_INTERDICT))
328 relat = getrel(getnatp(gp->own), victim);
329 if (mission == MI_SINTERDICT) {
330 if (relat >= FRIENDLY)
332 else if (type != EF_PLANE && relat > HOSTILE)
334 } else if (relat > HOSTILE)
337 dist = mapdist(x, y, gp->opx, gp->opy);
340 if (mission != MI_RESERVE) /* XXX */
341 oprange(gp, type, &radius);
346 /* Ok, it is within the operations range. */
347 /* Now check from where the object actually is */
348 dist = mapdist(x, y, gp->x, gp->y);
350 oprange(gp, type, &radius);
353 /* Ok, the object can get to where the x,y is */
356 if (mission != MI_AIR_DEFENSE) {
357 getsect(x, y, §);
358 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
361 * If the player->owner of the unit isn't at war
362 * with the victim, and doesn't own the
363 * sect being acted upon, and isn't the
364 * old player->owner of that sect, bounce them.
366 if (sect.sct_type != SCT_WATER &&
367 sect.sct_own != gp->own &&
368 sect.sct_oldown != gp->own)
374 glp = malloc(sizeof(struct genlist));
375 memset(glp, 0, sizeof(struct genlist));
379 glp->cp = &lchr[(int)gp->type];
382 glp->cp = &mchr[(int)gp->type];
385 glp->cp = &plchr[(int)gp->type];
388 glp->thing = malloc(sizeof(item));
389 memcpy(glp->thing, &item, sizeof(item));
390 emp_insque(&glp->queue, &mi[gp->own].queue);
395 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
402 snxtitem_all(&ni, EF_PLANE);
403 while (nxtitem(&ni, &plane)) {
404 if (plane.pln_own != cn)
407 if (plane.pln_mission != MI_ESCORT)
410 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
411 if (dist > plane.pln_range / 2)
414 plp = malloc(sizeof(struct plist));
415 memset(plp, 0, sizeof(struct plist));
416 plp->pcp = &plchr[(int)plane.pln_type];
418 emp_insque(&plp->queue, escorts);
423 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
424 int mission, char *s, int hardtarget)
426 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
427 struct emp_qelem *newqp;
435 struct plchrstr *pcp;
436 int dam = 0, dam2, mission_flags, tech;
437 natid plane_owner = 0;
438 int gun, shell, md, range, air_dam = 0;
439 double prb, hitchance, vrange;
441 getsect(x, y, §);
443 emp_initque(&missiles);
444 emp_initque(&bombers);
445 emp_initque(&escorts);
448 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
449 glp = (struct genlist *)qp;
452 md = mapdist(x, y, gp->x, gp->y);
454 if (glp->type == EF_LAND) {
457 if (lp->lnd_effic < LAND_MINFIREEFF)
460 if (mission == MI_SINTERDICT)
463 if ((mission == MI_INTERDICT) &&
464 (md > land_max_interdiction_range))
467 if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
470 if (lp->lnd_item[I_MILIT] < 1)
473 range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
477 shell = lp->lnd_item[I_SHELL];
478 gun = lp->lnd_item[I_GUN];
479 if (shell == 0 || gun == 0)
482 if (has_supply(lp)) {
484 putland(lp->lnd_uid, lp);
485 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
486 lp->lnd_ammo, shell), 1);
487 if (sect.sct_type == SCT_WATER) {
488 if (chance(lp->lnd_acc / 100.0))
489 dam2 = ldround(dam2 / 2.0, 1);
492 if (sect.sct_type == SCT_WATER)
493 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
495 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
497 "%s fires at %s %s at %s\n",
498 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
500 mpr(victim, "%s %s fires at you at %s\n",
501 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
503 } else if (glp->type == EF_SHIP) {
507 if (sp->shp_effic < 60)
509 if (sp->shp_frnge == 0)
511 if (((mission == MI_INTERDICT) ||
512 (mission == MI_SINTERDICT)) &&
513 (md > ship_max_interdiction_range))
515 if (sp->shp_item[I_MILIT] < 1)
518 if ((mcp->m_flags & M_SUB) &&
519 (sect.sct_type != SCT_WATER))
522 if (mission == MI_SINTERDICT) {
523 if (!(mcp->m_flags & M_SONAR))
525 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
527 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
528 vrange *= sp->shp_effic / 200.0;
531 /* can't look all the time */
535 if (mcp->m_flags & M_SUB) {
536 /* If we aren't shooting at "subs" or "ships" don't fire at all from
540 if (sp->shp_mobil < 0)
542 gun = sp->shp_item[I_GUN];
545 shell = sp->shp_item[I_SHELL];
546 if (shell < SHP_TORP_SHELLS)
547 shell += supply_commod(sp->shp_own,
548 sp->shp_x, sp->shp_y, I_SHELL,
549 SHP_TORP_SHELLS - shell);
550 if (shell < SHP_TORP_SHELLS)
553 range = roundrange(torprange(sp));
557 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
559 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
560 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
561 putship(sp->shp_uid, sp);
562 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
565 "%s locking on %s %s in %s\n",
566 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
568 "\tEffective torpedo range is %d.0\n", range);
570 "\tWhooosh... Hitchance = %d%%\n",
571 (int)(hitchance * 100));
573 if (hitchance < 1.0 && !chance(hitchance)) {
574 wu(0, sp->shp_own, "\tMissed\n");
576 "Incoming torpedo sighted @ %s missed (whew)!\n",
580 wu(0, sp->shp_own, "\tBOOM!...\n");
581 dam2 = TORP_DAMAGE();
584 nreport(victim, N_TORP_SHIP, 0, 1);
586 "\tTorpedo hit %s %s for %d damage\n",
587 cname(victim), s, dam2);
590 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
591 xyas(x, y, victim), dam2);
593 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
596 gun = sp->shp_item[I_GUN];
597 gun = MIN(gun, sp->shp_glim);
598 shell = sp->shp_item[I_SHELL];
600 shell += supply_commod(sp->shp_own,
601 sp->shp_x, sp->shp_y, I_SHELL,
603 gun = MIN(gun, shell);
604 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
608 dam2 = seagun(sp->shp_effic, gun);
612 prb = (double)md / range;
617 if (sect.sct_type == SCT_WATER)
618 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
620 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
622 "%s fires at %s %s at %s\n",
623 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
625 mpr(victim, "%s %s fires at you at %s\n",
626 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
628 sp->shp_item[I_SHELL] = shell - gun;
629 putship(sp->shp_uid, sp);
631 } else if (glp->type == EF_PLANE) {
633 if (pcp->pl_flags & P_M)
634 /* units have their own missile interdiction */
635 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
638 /* save planes for later */
639 plp = malloc(sizeof(struct plist));
641 memset(plp, 0, sizeof(struct plist));
643 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
644 if (plp->pcp->pl_flags & P_M)
645 emp_insque(&plp->queue, &missiles);
647 emp_insque(&plp->queue, &bombers);
648 plane_owner = plp->plane.pln_own;
651 if (!QEMPTY(&missiles)) {
652 /* I arbitrarily chose 100 mindam -KHS */
654 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
655 "sector", victim, mission);
656 qp = missiles.q_forw;
657 while (qp != (&missiles)) {
665 if (QEMPTY(&bombers)) {
668 glp = (struct genlist *)qp;
677 * If there are planes performing an
678 * interdict or support mission, find
679 * some escorts for them, if possible.
680 * Up to 2 per bomber, if possible.
682 find_escorts(x, y, plane_owner, &escorts);
684 if (mission == MI_SINTERDICT)
685 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
687 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
689 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
691 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
692 plp = (struct plist *)qp;
693 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
694 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
697 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
702 air = (struct airport *)qp;
703 md = mapdist(x, y, air->x, air->y);
708 /* Split off the bombers at this base into b */
709 divide(&bombers, &b, air->x, air->y);
711 /* Split off the escorts at this base into e */
712 divide(&escorts, &e, air->x, air->y);
716 mission_flags |= P_X; /* stealth (shhh) */
717 mission_flags |= P_H; /* gets turned off if not all choppers */
719 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
720 0, mission_flags, &tech);
725 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
726 P_F | P_ESC, mission_flags, &tech);
728 pp = BestAirPath(buf, air->x, air->y, x, y);
729 if (CANT_HAPPEN(!pp))
731 wu(0, plane_owner, "Flying %s mission from %s\n",
732 mission_name(mission), xyas(air->x, air->y, plane_owner));
733 if (air->own && (air->own != plane_owner)) {
734 wu(0, air->own, "%s is flying %s mission from %s\n",
735 cname(plane_owner), mission_name(mission),
736 xyas(air->x, air->y, air->own));
739 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
743 air_damage(&b, x, y, mission, victim, s, hardtarget);
751 if (sect.sct_type == SCT_WATER)
752 nreport(plane_owner, N_SHP_BOMB, victim, 1);
754 nreport(plane_owner, N_SCT_BOMB, victim, 1);
757 /* free up all this memory */
760 glp = (struct genlist *)qp;
768 while (qp != (&escorts)) {
776 while (qp != (&bombers)) {
787 cando(int mission, int type)
791 if (type == EF_PLANE)
795 if (type == EF_PLANE)
799 if ((type == EF_PLANE) || (type == EF_SHIP))
807 if (type == EF_PLANE)
820 nameofitem(struct genitem *gp, int type)
824 return prship((struct shpstr *)gp);
826 return prplane((struct plnstr *)gp);
828 return prland((struct lndstr *)gp);
834 mission_name(short mission)
838 return "an interdiction";
842 return "a offensive support";
844 return "a defensive support";
850 return "a sub interdiction";
852 return "an air defense";
854 return "a mysterious";
858 show_mission(int type, struct nstr_item *np)
860 int first = 1, radius;
864 while (nxtitem(np, &item)) {
865 gp = (struct genitem *)&item;
866 if (!player->owner || gp->own == 0)
870 pr("Thing x,y op-sect rad mission\n");
873 pr("%-25s", nameofitem(gp, type));
874 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
875 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
876 gp->mission == MI_OSUPPORT ||
877 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
879 oprange(gp, type, &radius);
880 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
881 if (radius < gp->radius)
884 pr(" %4d", gp->radius);
885 } else if (gp->mission == MI_RESERVE) {
889 getsect(gp->x, gp->y, §);
890 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
893 if (item.land.lnd_rad_max == 0)
896 plus += item.land.lnd_rad_max;
897 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
899 } else if (gp->mission == MI_ESCORT) {
901 pr(" %4d", item.plane.pln_range / 2);
905 pr(" is on %s mission\n", mission_name(gp->mission));
907 pr(" has no mission.\n");
912 oprange(struct genitem *gp, int type, int *radius)
921 getship(gp->uid, &ship);
922 range = ldround(effrange(ship.shp_frnge, ship.shp_tech), 1);
925 getland(gp->uid, &land);
926 range = ldround(effrange(land.lnd_frg, land.lnd_tech), 1);
929 getplane(gp->uid, &plane);
930 /* missiles go one way, so we can use all the range */
931 if (plchr[(int)plane.pln_type].pl_flags & P_M)
932 range = plane.pln_range;
934 range = plane.pln_range / 2;
937 CANT_HAPPEN("bad TYPE");
948 * Remove all planes who cannot go on
949 * the mission from the plane list.
952 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
955 struct emp_qelem *qp, *next;
960 struct plchrstr *pcp;
965 for (qp = list->q_forw; qp != list; qp = next) {
967 plp = (struct plist *)qp;
971 if (pp->pln_effic < 40) {
977 if (pp->pln_mobil < 1) {
984 if (ontradingblock(EF_PLANE, pp)) {
994 for (x = 0; x < sizeof(wantflags) * 8; x++) {
996 if ((wantflags & y) == y)
997 if ((pcp->pl_flags & y) != y) {
1019 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1023 if ((pcp->pl_flags & P_E) ||
1024 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1036 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1038 if ((nowantflags & y) == y)
1039 if ((pcp->pl_flags & y) == y)
1048 if (pp->pln_ship >= 0) {
1049 if (!getship(pp->pln_ship, &ship)) {
1052 putplane(pp->pln_uid, pp);
1057 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1060 if (ship.shp_effic < SHIP_MINEFF) {
1063 /* Can't fly off of inefficient or non-owned, non-allied ships */
1064 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1065 ((ship.shp_own != pp->pln_own) &&
1066 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1072 if (pp->pln_land >= 0) {
1073 if (!getland(pp->pln_land, &land)) {
1076 putplane(pp->pln_uid, pp);
1081 if (!(pcp->pl_flags & P_E))
1083 if (land.lnd_effic < LAND_MINEFF)
1086 /* Can't fly off of inefficient or non-owned, non-allied units */
1087 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1088 ((land.lnd_own != pp->pln_own) &&
1089 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1095 /* Can't fly off units in ships or other units */
1096 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1102 /* Now, check the sector status if not on a plane or unit */
1103 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1104 /* If we can't get the sector, we can't check it, and can't fly */
1105 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1110 /* First, check allied status */
1111 /* Can't fly from non-owned sectors or non-allied sectors */
1112 if ((sect.sct_own != pp->pln_own) &&
1113 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1118 /* non-vtol plane */
1119 if ((pcp->pl_flags & P_V) == 0) {
1120 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1127 if (pcp->pl_flags & P_A) {
1128 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1135 putplane(pp->pln_uid, pp);
1140 * Arm only the planes at x,y
1143 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1144 int mission, struct ichrstr *ip, int flags,
1145 int mission_flags, int *tech)
1147 struct emp_qelem *qp;
1148 struct emp_qelem *next;
1153 for (qp = list->q_forw; qp != list; qp = next) {
1155 plp = (struct plist *)qp;
1157 if (plp->plane.pln_x != x)
1159 if (plp->plane.pln_y != y)
1162 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1167 if (flags & (P_S | P_I)) {
1168 if (plp->pcp->pl_flags & P_S)
1169 mission_flags |= P_S;
1170 if (plp->pcp->pl_flags & P_I)
1171 mission_flags |= P_I;
1173 if (*tech > plp->plane.pln_tech)
1174 *tech = plp->plane.pln_tech;
1175 if (!(plp->pcp->pl_flags & P_H))
1176 /* no stealth on this mission */
1177 mission_flags &= ~P_H;
1178 if (!(plp->pcp->pl_flags & P_X))
1179 /* no stealth on this mission */
1180 mission_flags &= ~P_X;
1181 if (!(plp->pcp->pl_flags & P_A)) {
1182 /* no asw on this mission */
1183 mission_flags &= ~P_A;
1185 if (!(plp->pcp->pl_flags & P_MINE)) {
1186 /* no asw on this mission */
1187 mission_flags &= ~P_MINE;
1191 * Mob costs for missions are 1/2 normal
1194 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1195 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1198 return mission_flags;
1202 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1205 struct plchrstr *pcp;
1217 if (pp->pln_ship >= 0) {
1218 getship(pp->pln_ship, &ship);
1219 item = ship.shp_item;
1220 } else if (pp->pln_land >= 0) {
1221 getland(pp->pln_land, &land);
1222 item = land.lnd_item;
1224 getsect(pp->pln_x, pp->pln_y, §);
1225 item = sect.sct_item;
1227 if (pcp->pl_fuel > item[I_PETROL]) {
1230 item[I_PETROL] -= pcp->pl_fuel;
1232 if (!(flags & P_F)) {
1238 if (pp->pln_nuketype == -1) {
1240 needed = pp->pln_load;
1244 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1247 needed = (pp->pln_load * 2) / ip->i_lbs;
1250 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1253 needed = (pp->pln_load * 2) / ip->i_lbs;
1256 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1259 needed = pp->pln_load / ip->i_lbs;
1262 if (pp->pln_nuketype == -1)
1265 case 'i': /* missile interception */
1268 needed = pp->pln_load;
1274 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1277 if (itype != I_NONE) {
1278 if (itype == I_SHELL && item[itype] < needed)
1279 item[itype] += supply_commod(plp->plane.pln_own,
1283 if (item[itype] < needed)
1285 item[itype] -= needed;
1287 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1288 plp->bombs = needed;
1292 if (pp->pln_ship >= 0)
1293 putship(ship.shp_uid, &ship);
1294 else if (pp->pln_land >= 0)
1295 putland(land.lnd_uid, &land);
1302 * Return 1 if this x,y pair is in the list
1305 find_airport(struct emp_qelem *airp, coord x, coord y)
1307 struct emp_qelem *qp;
1310 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1311 a = (struct airport *)qp;
1312 if ((a->x == x) && (a->y == y))
1319 /* #*# This needs to be changed to include acc's -KHS */
1321 add_airport(struct emp_qelem *airp, coord x, coord y)
1326 a = malloc(sizeof(struct airport));
1330 getsect(x, y, §);
1331 a->own = sect.sct_own;
1333 emp_insque((struct emp_qelem *)a, airp);
1337 * Take all the planes in list 1 that
1338 * are at x,y, and put them into list 2.
1341 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1343 struct emp_qelem *qp, *next;
1346 for (qp = l1->q_forw; qp != l1; qp = next) {
1348 plp = (struct plist *)qp;
1350 if (plp->plane.pln_x != x)
1352 if (plp->plane.pln_y != y)
1361 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1362 natid victim, char *s, int hardtarget)
1364 struct emp_qelem *qp;
1367 int newdam, dam = 0;
1371 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1372 plp = (struct plist *)qp;
1375 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1382 if (plp->pcp->pl_flags & P_A) {
1383 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1385 "\t%s detects sub movement in %s\n",
1386 prplane(pp), xyas(x, y, pp->pln_own));
1389 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1391 "\t%s tracks %s %s at %s\n",
1392 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1396 "\t%s depth-charging %s %s in %s\n",
1397 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1400 "\t%s pinbombing %s %s in %s\n",
1401 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1403 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1404 if (plp->plane.pln_nuketype != -1)
1406 else if (hardtarget != SECT_HARDTARGET)
1407 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1408 /* Always calculate damage */
1409 if (roll(100) <= hitchance) {
1410 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1412 if (mission == MI_INTERDICT) {
1414 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1415 prplane(pp), nukedam, cname(victim), s);
1420 "\t\thit %s %s for %d damage\n",
1421 cname(victim), s, newdam);
1425 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1426 wu(0, pp->pln_own, "missed\n");
1427 if (mission == MI_SINTERDICT) {
1429 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1430 xyas(x, y, victim));
1431 } else if (*s == 's') {
1432 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1433 s, xyas(x, y, victim));
1435 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1436 s, xyas(x, y, victim));
1438 /* Now, even though we missed, the bombs
1440 collateral_damage(x, y, newdam, bombers);
1443 /* use up missiles */
1444 if (plp->pcp->pl_flags & P_M)
1452 * Check to see if anyone hostile to the victim
1453 * is running an air defense mission on this
1454 * sector. If so, do air combat
1457 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1458 struct emp_qelem *esc_list)
1461 int mission_flags, tech, combat = 0, rel, dist, z;
1462 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1464 struct genlist *glp;
1466 struct genlist mi[MAXNOC];
1473 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1475 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1478 memset(mi, 0, sizeof(mi));
1479 for (z = 1; z < MAXNOC; z++)
1480 emp_initque((struct emp_qelem *)&mi[z]);
1482 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1484 for (cn = 1; cn < MAXNOC; cn++) {
1485 /* Check our relations */
1486 rel = getrel(getnatp(cn), victim);
1491 if (QEMPTY(&mi[cn].queue))
1494 /* Ok, make a list of all the interceptors. Note that this *copies* the
1495 * list from the mission creation. This list must be deleted later. */
1496 emp_initque(&interceptors);
1497 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1499 glp = (struct genlist *)qp;
1502 dist = mapdist(x, y, gp->x, gp->y);
1504 plp = malloc(sizeof(struct plist));
1505 memset(plp, 0, sizeof(struct plist));
1507 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1509 /* missiles go one way, so we can use all the range */
1510 if (!(plp->pcp->pl_flags & P_M))
1512 /* If it's out of range, free it and continue on */
1513 if (dist > plp->plane.pln_range) {
1517 emp_insque(&plp->queue, &interceptors);
1520 /* Remove those who cannot go */
1521 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1523 if (QEMPTY(&interceptors))
1526 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1527 * that the higher numbered planes go into battle (they should be the better ones
1528 * at fighting, if all went well.) */
1530 for (qp = interceptors.q_forw; qp != (&interceptors);
1533 tcount -= count * 2;
1534 /* Just in case there are more incoming than we have */
1537 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1541 /* Free it up and continue */
1543 glp = (struct genlist *)qp;
1548 /* Now, make a list of all the airports these planes are coming from */
1550 for (qp = interceptors.q_forw; qp != (&interceptors);
1552 plp = (struct plist *)qp;
1553 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1554 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1557 /* Now, fly them out one airport at a time */
1558 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1559 struct airport *air;
1561 air = (struct airport *)qp;
1562 dist = mapdist(x, y, air->x, air->y);
1566 /* Split off the interceptors at this base into i */
1567 divide(&interceptors, &i, air->x, air->y);
1571 mission_flags |= P_X; /* stealth (shhh) */
1572 /* gets turned off if not all choppers */
1573 mission_flags |= P_H;
1574 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1575 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1577 /* Did we run out of interceptors? */
1580 /* Did we run out of bombers? */
1581 if (QEMPTY(bomb_list)) {
1582 /* Yes, so we have to put the rest of the interceptors back, and
1583 then continue, or we leak memory */
1588 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1589 mission_flags, &tech);
1591 /* Did we run out of interceptors? */
1594 /* Did we run out of bombers? */
1595 if (QEMPTY(bomb_list)) {
1596 /* Yes, so we have to put the rest of the interceptors back, and
1597 then continue, or we leak memory */
1602 path = BestAirPath(buf, air->x, air->y, x, y);
1603 if (CANT_HAPPEN(!path))
1605 wu(0, cn, "Flying %s mission from %s\n",
1606 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1607 if (air->own && (air->own != cn)) {
1608 wu(0, air->own, "%s is flying %s mission from %s\n",
1609 cname(cn), mission_name(MI_AIR_DEFENSE),
1610 xyas(air->x, air->y, air->own));
1613 /* Now, fly the planes to the sector */
1614 emp_initque(&empty);
1615 ac_encounter(&i, &empty, air->x, air->y,
1616 path, mission_flags, 1, bomb_list, esc_list);
1618 /* If none made it, continue */
1622 /* Some made it, so now they get to try to fight. */
1623 /* Intercept the escorts first */
1625 if (!QEMPTY(esc_list)) {
1626 mpr(victim, "%s air defense planes intercept!\n",
1628 ac_combat_headers(victim, cn);
1629 ac_airtoair(esc_list, &i);
1632 /* Now intercept the bombers */
1633 if (!QEMPTY(bomb_list)) {
1635 mpr(victim, "%s air defense planes intercept!\n",
1637 ac_combat_headers(victim, cn);
1639 ac_airtoair(bomb_list, &i);
1648 /* We have to free all of these, if they are still there, otherwise they get
1649 lost and we leak memory all over the place. */
1650 for (cn = 1; cn < MAXNOC; cn++) {
1651 /* free up all this memory if it's still there */
1652 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1654 glp = (struct genlist *)qp;