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, &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, &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, &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 *radius)
904 switch (gp->ef_type) {
906 range = ldround(effrange(((struct shpstr *)gp)->shp_frnge,
907 ((struct shpstr *)gp)->shp_tech), 1);
910 range = ldround(effrange(((struct lndstr *)gp)->lnd_frg,
911 ((struct lndstr *)gp)->lnd_tech), 1);
914 /* missiles go one way, so we can use all the range */
915 if (plchr[(int)gp->type].pl_flags & P_M)
916 range = ((struct plnstr *)gp)->pln_range;
918 range = ((struct plnstr *)gp)->pln_range / 2;
921 CANT_HAPPEN("bad TYPE");
932 * Remove all planes who cannot go on
933 * the mission from the plane list.
936 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
939 struct emp_qelem *qp, *next;
944 struct plchrstr *pcp;
949 for (qp = list->q_forw; qp != list; qp = next) {
951 plp = (struct plist *)qp;
955 if (pp->pln_effic < 40) {
961 if (pp->pln_mobil < 1) {
968 if (ontradingblock(EF_PLANE, pp)) {
978 for (x = 0; x < sizeof(wantflags) * 8; x++) {
980 if ((wantflags & y) == y)
981 if ((pcp->pl_flags & y) != y) {
1003 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1007 if ((pcp->pl_flags & P_E) ||
1008 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1020 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1022 if ((nowantflags & y) == y)
1023 if ((pcp->pl_flags & y) == y)
1032 if (pp->pln_ship >= 0) {
1033 if (!getship(pp->pln_ship, &ship)) {
1036 putplane(pp->pln_uid, pp);
1041 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1044 if (ship.shp_effic < SHIP_MINEFF) {
1047 /* Can't fly off of inefficient or non-owned, non-allied ships */
1048 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1049 ((ship.shp_own != pp->pln_own) &&
1050 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1056 if (pp->pln_land >= 0) {
1057 if (!getland(pp->pln_land, &land)) {
1060 putplane(pp->pln_uid, pp);
1065 if (!(pcp->pl_flags & P_E))
1067 if (land.lnd_effic < LAND_MINEFF)
1070 /* Can't fly off of inefficient or non-owned, non-allied units */
1071 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1072 ((land.lnd_own != pp->pln_own) &&
1073 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1079 /* Can't fly off units in ships or other units */
1080 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1086 /* Now, check the sector status if not on a plane or unit */
1087 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1088 /* If we can't get the sector, we can't check it, and can't fly */
1089 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1094 /* First, check allied status */
1095 /* Can't fly from non-owned sectors or non-allied sectors */
1096 if ((sect.sct_own != pp->pln_own) &&
1097 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1102 /* non-vtol plane */
1103 if ((pcp->pl_flags & P_V) == 0) {
1104 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1111 if (pcp->pl_flags & P_A) {
1112 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1119 putplane(pp->pln_uid, pp);
1124 * Arm only the planes at x,y
1127 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1128 int mission, struct ichrstr *ip, int flags,
1129 int mission_flags, int *tech)
1131 struct emp_qelem *qp;
1132 struct emp_qelem *next;
1137 for (qp = list->q_forw; qp != list; qp = next) {
1139 plp = (struct plist *)qp;
1141 if (plp->plane.pln_x != x)
1143 if (plp->plane.pln_y != y)
1146 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1151 if (flags & (P_S | P_I)) {
1152 if (plp->pcp->pl_flags & P_S)
1153 mission_flags |= P_S;
1154 if (plp->pcp->pl_flags & P_I)
1155 mission_flags |= P_I;
1157 if (*tech > plp->plane.pln_tech)
1158 *tech = plp->plane.pln_tech;
1159 if (!(plp->pcp->pl_flags & P_H))
1160 /* no stealth on this mission */
1161 mission_flags &= ~P_H;
1162 if (!(plp->pcp->pl_flags & P_X))
1163 /* no stealth on this mission */
1164 mission_flags &= ~P_X;
1165 if (!(plp->pcp->pl_flags & P_A)) {
1166 /* no asw on this mission */
1167 mission_flags &= ~P_A;
1169 if (!(plp->pcp->pl_flags & P_MINE)) {
1170 /* no asw on this mission */
1171 mission_flags &= ~P_MINE;
1175 * Mob costs for missions are 1/2 normal
1178 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1179 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1182 return mission_flags;
1186 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1189 struct plchrstr *pcp;
1201 if (pp->pln_ship >= 0) {
1202 getship(pp->pln_ship, &ship);
1203 item = ship.shp_item;
1204 } else if (pp->pln_land >= 0) {
1205 getland(pp->pln_land, &land);
1206 item = land.lnd_item;
1208 getsect(pp->pln_x, pp->pln_y, §);
1209 item = sect.sct_item;
1211 if (pcp->pl_fuel > item[I_PETROL]) {
1214 item[I_PETROL] -= pcp->pl_fuel;
1216 if (!(flags & P_F)) {
1222 if (pp->pln_nuketype == -1) {
1224 needed = pp->pln_load;
1228 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1231 needed = (pp->pln_load * 2) / ip->i_lbs;
1234 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1237 needed = (pp->pln_load * 2) / ip->i_lbs;
1240 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1243 needed = pp->pln_load / ip->i_lbs;
1246 if (pp->pln_nuketype == -1)
1249 case 'i': /* missile interception */
1252 needed = pp->pln_load;
1258 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1261 if (itype != I_NONE) {
1262 if (itype == I_SHELL && item[itype] < needed)
1263 item[itype] += supply_commod(plp->plane.pln_own,
1267 if (item[itype] < needed)
1269 item[itype] -= needed;
1271 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1272 plp->bombs = needed;
1276 if (pp->pln_ship >= 0)
1277 putship(ship.shp_uid, &ship);
1278 else if (pp->pln_land >= 0)
1279 putland(land.lnd_uid, &land);
1286 * Return 1 if this x,y pair is in the list
1289 find_airport(struct emp_qelem *airp, coord x, coord y)
1291 struct emp_qelem *qp;
1294 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1295 a = (struct airport *)qp;
1296 if ((a->x == x) && (a->y == y))
1303 /* #*# This needs to be changed to include acc's -KHS */
1305 add_airport(struct emp_qelem *airp, coord x, coord y)
1310 a = malloc(sizeof(struct airport));
1314 getsect(x, y, §);
1315 a->own = sect.sct_own;
1317 emp_insque((struct emp_qelem *)a, airp);
1321 * Take all the planes in list 1 that
1322 * are at x,y, and put them into list 2.
1325 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1327 struct emp_qelem *qp, *next;
1330 for (qp = l1->q_forw; qp != l1; qp = next) {
1332 plp = (struct plist *)qp;
1334 if (plp->plane.pln_x != x)
1336 if (plp->plane.pln_y != y)
1345 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1346 natid victim, char *s, int hardtarget)
1348 struct emp_qelem *qp;
1351 int newdam, dam = 0;
1355 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1356 plp = (struct plist *)qp;
1359 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1366 if (plp->pcp->pl_flags & P_A) {
1367 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1369 "\t%s detects sub movement in %s\n",
1370 prplane(pp), xyas(x, y, pp->pln_own));
1373 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1375 "\t%s tracks %s %s at %s\n",
1376 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1380 "\t%s depth-charging %s %s in %s\n",
1381 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1384 "\t%s pinbombing %s %s in %s\n",
1385 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1387 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1388 if (plp->plane.pln_nuketype != -1)
1390 else if (hardtarget != SECT_HARDTARGET)
1391 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1392 /* Always calculate damage */
1393 if (roll(100) <= hitchance) {
1394 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1396 if (mission == MI_INTERDICT) {
1398 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1399 prplane(pp), nukedam, cname(victim), s);
1404 "\t\thit %s %s for %d damage\n",
1405 cname(victim), s, newdam);
1409 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1410 wu(0, pp->pln_own, "missed\n");
1411 if (mission == MI_SINTERDICT) {
1413 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1414 xyas(x, y, victim));
1415 } else if (*s == 's') {
1416 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1417 s, xyas(x, y, victim));
1419 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1420 s, xyas(x, y, victim));
1422 /* Now, even though we missed, the bombs
1424 collateral_damage(x, y, newdam, bombers);
1427 /* use up missiles */
1428 if (plp->pcp->pl_flags & P_M)
1436 * Check to see if anyone hostile to the victim
1437 * is running an air defense mission on this
1438 * sector. If so, do air combat
1441 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1442 struct emp_qelem *esc_list)
1445 int mission_flags, tech, combat = 0, rel, dist, z;
1446 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1448 struct genlist *glp;
1450 struct genlist mi[MAXNOC];
1457 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1459 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1462 memset(mi, 0, sizeof(mi));
1463 for (z = 1; z < MAXNOC; z++)
1464 emp_initque((struct emp_qelem *)&mi[z]);
1466 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1468 for (cn = 1; cn < MAXNOC; cn++) {
1469 /* Check our relations */
1470 rel = getrel(getnatp(cn), victim);
1475 if (QEMPTY(&mi[cn].queue))
1478 /* Ok, make a list of all the interceptors. Note that this *copies* the
1479 * list from the mission creation. This list must be deleted later. */
1480 emp_initque(&interceptors);
1481 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1483 glp = (struct genlist *)qp;
1486 dist = mapdist(x, y, gp->x, gp->y);
1488 plp = malloc(sizeof(struct plist));
1489 memset(plp, 0, sizeof(struct plist));
1491 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1493 /* missiles go one way, so we can use all the range */
1494 if (!(plp->pcp->pl_flags & P_M))
1496 /* If it's out of range, free it and continue on */
1497 if (dist > plp->plane.pln_range) {
1501 emp_insque(&plp->queue, &interceptors);
1504 /* Remove those who cannot go */
1505 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1507 if (QEMPTY(&interceptors))
1510 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1511 * that the higher numbered planes go into battle (they should be the better ones
1512 * at fighting, if all went well.) */
1514 for (qp = interceptors.q_forw; qp != (&interceptors);
1517 tcount -= count * 2;
1518 /* Just in case there are more incoming than we have */
1521 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1525 /* Free it up and continue */
1527 glp = (struct genlist *)qp;
1532 /* Now, make a list of all the airports these planes are coming from */
1534 for (qp = interceptors.q_forw; qp != (&interceptors);
1536 plp = (struct plist *)qp;
1537 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1538 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1541 /* Now, fly them out one airport at a time */
1542 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1543 struct airport *air;
1545 air = (struct airport *)qp;
1546 dist = mapdist(x, y, air->x, air->y);
1550 /* Split off the interceptors at this base into i */
1551 divide(&interceptors, &i, air->x, air->y);
1555 mission_flags |= P_X; /* stealth (shhh) */
1556 /* gets turned off if not all choppers */
1557 mission_flags |= P_H;
1558 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1559 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1561 /* Did we run out of interceptors? */
1564 /* Did we run out of bombers? */
1565 if (QEMPTY(bomb_list)) {
1566 /* Yes, so we have to put the rest of the interceptors back, and
1567 then continue, or we leak memory */
1572 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1573 mission_flags, &tech);
1575 /* Did we run out of interceptors? */
1578 /* Did we run out of bombers? */
1579 if (QEMPTY(bomb_list)) {
1580 /* Yes, so we have to put the rest of the interceptors back, and
1581 then continue, or we leak memory */
1586 path = BestAirPath(buf, air->x, air->y, x, y);
1587 if (CANT_HAPPEN(!path))
1589 wu(0, cn, "Flying %s mission from %s\n",
1590 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1591 if (air->own && (air->own != cn)) {
1592 wu(0, air->own, "%s is flying %s mission from %s\n",
1593 cname(cn), mission_name(MI_AIR_DEFENSE),
1594 xyas(air->x, air->y, air->own));
1597 /* Now, fly the planes to the sector */
1598 emp_initque(&empty);
1599 ac_encounter(&i, &empty, air->x, air->y,
1600 path, mission_flags, 1, bomb_list, esc_list);
1602 /* If none made it, continue */
1606 /* Some made it, so now they get to try to fight. */
1607 /* Intercept the escorts first */
1609 if (!QEMPTY(esc_list)) {
1610 mpr(victim, "%s air defense planes intercept!\n",
1612 ac_combat_headers(victim, cn);
1613 ac_airtoair(esc_list, &i);
1616 /* Now intercept the bombers */
1617 if (!QEMPTY(bomb_list)) {
1619 mpr(victim, "%s air defense planes intercept!\n",
1621 ac_combat_headers(victim, cn);
1623 ac_airtoair(bomb_list, &i);
1632 /* We have to free all of these, if they are still there, otherwise they get
1633 lost and we leak memory all over the place. */
1634 for (cn = 1; cn < MAXNOC; cn++) {
1635 /* free up all this memory if it's still there */
1636 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1638 glp = (struct genlist *)qp;