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
54 #include "prototypes.h"
58 struct emp_qelem queue;
63 static void add_airport(struct emp_qelem *, coord, coord);
64 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
66 static void build_mission_list(struct genlist *, coord, coord, int, natid);
67 static void build_mission_list_type(struct genlist *, coord, coord, int,
69 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
70 static int dosupport(struct genlist *, coord, coord, natid, natid);
71 static int find_airport(struct emp_qelem *, coord, coord);
72 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
73 int, struct ichrstr *, int, int, int *);
74 static void mission_pln_sel(struct emp_qelem *, int, int, int);
75 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
79 * Interdict commodities & transported planes
82 ground_interdict(coord x, coord y, natid victim, s_char *s)
85 int dam = 0, newdam, rel;
86 struct genlist mi[MAXNOC];
89 memset(mi, 0, sizeof(mi));
90 for (z = 1; z < MAXNOC; z++)
91 emp_initque((struct emp_qelem *)&mi[z]);
93 build_mission_list(mi, x, y, MI_INTERDICT, victim);
95 for (cn = 1; cn < MAXNOC; cn++) {
96 rel = getrel(getnatp(cn), victim);
100 if (QEMPTY(&mi[cn].queue))
103 newdam = perform_mission(x, y, victim, &mi[cn].queue,
104 MI_INTERDICT, s, SECT_HARDTARGET);
107 mpr(victim, "%s interdiction mission does %d damage!\n",
111 collateral_damage(x, y, dam, 0);
117 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
125 getsect(x, y, §);
127 coll = ldround((double)dam * collateral_dam, 1);
130 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
131 xyas(x, y, sect.sct_own), coll);
132 sectdamage(§, coll, list);
140 only_subs(struct emp_qelem *list)
142 struct emp_qelem *qp;
146 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
147 glp = (struct genlist *)qp;
149 if (glp->type != EF_SHIP)
151 mcp = (struct mchrstr *)glp->cp;
152 if (!(mcp->m_flags & M_SUB))
156 /* They were all subs! */
162 * Interdict ships & land units
166 unit_interdict(coord x, coord y, natid victim, s_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
202 "Enemy interdiction mission does %d damage!\n",
206 "%s interdiction mission does %d damage!\n",
212 collateral_damage(x, y, dam, 0);
218 * Perform a mission against victim, on behalf of actee
221 off_support(coord x, coord y, natid victim, natid actee)
224 struct genlist mi[MAXNOC];
227 memset(mi, 0, sizeof(mi));
228 for (z = 1; z < MAXNOC; z++)
229 emp_initque((struct emp_qelem *)&mi[z]);
231 build_mission_list(mi, x, y, MI_SUPPORT, victim);
232 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
234 dam = dosupport(mi, x, y, victim, actee);
239 * Perform a mission against victim, on behalf of actee
242 def_support(coord x, coord y, natid victim, natid actee)
245 struct genlist mi[MAXNOC];
248 memset(mi, 0, sizeof(mi));
249 for (z = 1; z < MAXNOC; z++)
250 emp_initque((struct emp_qelem *)&mi[z]);
252 build_mission_list(mi, x, y, MI_SUPPORT, victim);
253 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
255 dam = dosupport(mi, x, y, victim, actee);
260 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
266 for (cn = 1; cn < MAXNOC; cn++) {
267 rel = getrel(getnatp(cn), actee);
268 if ((cn != actee) && (rel != ALLIED))
270 rel = getrel(getnatp(cn), victim);
271 if ((cn != actee) && (rel != AT_WAR))
274 if (QEMPTY(&mi[cn].queue))
277 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
278 "", SECT_HARDTARGET);
284 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
287 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
288 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
289 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
293 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
294 int type, natid victim)
311 size = MAX(sizeof(struct shpstr),sizeof(struct lndstr));
312 size = MAX(size,sizeof(struct plnstr));
313 block = malloc(size);
315 size = sizeof(u_block);
316 block = (s_char *)&u_block;
318 snxtitem_all(&ni, type);
319 while (nxtitem(&ni, block)) {
320 gp = (struct genitem *)block;
328 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
331 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
332 (gp->mission != MI_INTERDICT))
335 relat = getrel(getnatp(gp->own), victim);
336 if (mission == MI_SINTERDICT) {
337 if (relat >= FRIENDLY)
339 else if (type != EF_PLANE && relat > HOSTILE)
341 } else if (relat > HOSTILE)
344 dist = mapdist(x, y, gp->opx, gp->opy);
347 if (mission != MI_RESERVE) /* XXX */
348 oprange(gp, type, &radius);
353 /* Ok, it is within the operations range. */
354 /* Now check from where the object actually is */
355 dist = mapdist(x, y, gp->x, gp->y);
357 oprange(gp, type, &radius);
360 /* Ok, the object can get to where the x,y is */
363 if (mission != MI_AIR_DEFENSE) {
364 getsect(x, y, §);
365 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
368 * If the player->owner of the unit isn't at war
369 * with the victim, and doesn't own the
370 * sect being acted upon, and isn't the
371 * old player->owner of that sect, bounce them.
373 if (sect.sct_type != SCT_WATER &&
374 sect.sct_own != gp->own &&
375 sect.sct_oldown != gp->own)
381 glp = malloc(sizeof(struct genlist));
382 memset(glp, 0, sizeof(struct genlist));
388 glp->cp = (s_char *)&lchr[(int)gp->type];
391 glp->cp = (s_char *)&mchr[(int)gp->type];
394 glp->cp = (s_char *)&plchr[(int)gp->type];
397 glp->thing = malloc(size);
398 memcpy(glp->thing, block, size);
399 emp_insque(&glp->queue, &mi[gp->own].queue);
404 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
411 snxtitem_all(&ni, EF_PLANE);
412 while (nxtitem(&ni, &plane)) {
413 if (plane.pln_own != cn)
416 if (plane.pln_mission != MI_ESCORT)
419 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
421 if (dist > ((int)((float)plane.pln_range / 2.0)))
424 plp = malloc(sizeof(struct plist));
425 memset(plp, 0, sizeof(struct plist));
426 plp->pcp = &plchr[(int)plane.pln_type];
428 emp_insque(&plp->queue, escorts);
433 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
434 int mission, s_char *s, int hardtarget)
436 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
437 struct emp_qelem *newqp;
445 struct plchrstr *pcp;
446 int dam = 0, dam2, mission_flags, tech;
447 natid plane_owner = 0;
448 int gun, shell, md, air_dam = 0;
449 double range2, prb, range, mobcost, hitchance;
451 getsect(x, y, §);
453 emp_initque(&missiles);
454 emp_initque(&bombers);
455 emp_initque(&escorts);
458 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
459 glp = (struct genlist *)qp;
460 gp = (struct genitem *)glp->thing;
462 md = mapdist(x, y, gp->x, gp->y);
464 if (glp->type == EF_LAND) {
465 lp = (struct lndstr *)glp->thing;
467 if (lp->lnd_effic < LAND_MINFIREEFF)
470 if (mission == MI_SINTERDICT)
473 if ((mission == MI_INTERDICT) &&
474 (md > land_max_interdiction_range))
477 if (md > (lp->lnd_frg / 2))
480 if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
483 if (lnd_getmil(lp) < 1)
486 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
487 range2 = (double)roundrange(range);
492 shell = lp->lnd_item[I_SHELL];
493 gun = lp->lnd_item[I_GUN];
494 if (shell == 0 || gun == 0)
497 if (has_supply(lp)) {
499 putland(lp->lnd_uid, lp);
500 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
501 lp->lnd_ammo, shell), 1);
502 if (sect.sct_type == SCT_WATER) {
503 double dam3 = (double)dam2;
504 if (chance(((double)lp->lnd_acc) / 100.0))
505 dam2 = ldround((dam3 / 2.0), 1);
508 if (sect.sct_type == SCT_WATER)
509 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
511 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
513 "%s fires at %s %s at %s\n",
514 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
516 mpr(victim, "%s %s fires at you at %s\n",
517 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
519 } else if (glp->type == EF_SHIP) {
520 sp = (struct shpstr *)glp->thing;
521 mcp = (struct mchrstr *)glp->cp;
523 if (sp->shp_effic < 60)
525 if (sp->shp_frnge == 0)
527 if (((mission == MI_INTERDICT) ||
528 (mission == MI_SINTERDICT)) &&
529 (md > ship_max_interdiction_range))
531 if (sp->shp_item[I_MILIT] < 1)
534 if ((mcp->m_flags & M_SUB) &&
535 (sect.sct_type != SCT_WATER))
538 if (mission == MI_SINTERDICT) {
539 if (!(mcp->m_flags & M_SONAR))
541 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
543 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
544 range2 *= (double)sp->shp_effic / 200.0;
547 /* can't look all the time */
551 if (mcp->m_flags & M_SUB) {
552 /* If we aren't shooting at "subs" or "ships" don't fire at all from
556 if (sp->shp_mobil < 0)
558 gun = sp->shp_item[I_GUN];
561 shell = sp->shp_item[I_SHELL];
562 if (shell < SHP_TORP_SHELLS)
563 shell += supply_commod(sp->shp_own,
564 sp->shp_x, sp->shp_y, I_SHELL,
565 SHP_TORP_SHELLS - shell);
566 if (shell < SHP_TORP_SHELLS)
569 range = sp->shp_effic * techfact(sp->shp_tech,
570 ((double)sp->shp_frnge)) /
573 range2 = (double)roundrange(range);
577 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
579 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
580 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
581 mobcost = (480.0 / (mobcost +
582 techfact(sp->shp_tech, mobcost)));
583 sp->shp_mobil -= mobcost;
584 putship(sp->shp_uid, sp);
585 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
588 "%s locking on %s %s in %s\n",
589 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
591 "\tEffective torpedo range is %.1f\n", range);
593 "\tWhooosh... Hitchance = %d%%\n",
594 (int)(hitchance * 100));
596 if (hitchance < 1.0 && !chance(hitchance)) {
597 wu(0, sp->shp_own, "\tMissed\n");
599 "Incoming torpedo sighted @ %s missed (whew)!\n",
603 wu(0, sp->shp_own, "\tBOOM!...\n");
604 dam2 = TORP_DAMAGE();
607 nreport(victim, N_TORP_SHIP, 0, 1);
609 "\tTorpedo hit %s %s for %d damage\n",
610 cname(victim), s, dam2);
613 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
614 xyas(x, y, victim), dam2);
616 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
617 range2 = (double)roundrange(range);
620 gun = sp->shp_item[I_GUN];
621 gun = MIN(gun, sp->shp_glim);
622 shell = sp->shp_item[I_SHELL];
624 shell += supply_commod(sp->shp_own,
625 sp->shp_x, sp->shp_y, I_SHELL,
627 gun = MIN(gun, shell);
628 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
632 dam2 = seagun(sp->shp_effic, gun);
636 prb = ((double)md) / range2;
639 dam2 = (int)((float)dam2 / 2.0);
641 if (sect.sct_type == SCT_WATER)
642 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
644 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
646 "%s fires at %s %s at %s\n",
647 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
649 mpr(victim, "%s %s fires at you at %s\n",
650 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
652 sp->shp_item[I_SHELL] = shell - gun;
653 putship(sp->shp_uid, sp);
655 } else if (glp->type == EF_PLANE) {
656 pcp = (struct plchrstr *)glp->cp;
657 if (pcp->pl_flags & P_M)
658 /* units have their own missile interdiction */
659 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
662 /* save planes for later */
663 plp = malloc(sizeof(struct plist));
665 memset(plp, 0, sizeof(struct plist));
667 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
668 if (plp->pcp->pl_flags & P_M)
669 emp_insque(&plp->queue, &missiles);
671 emp_insque(&plp->queue, &bombers);
672 plane_owner = plp->plane.pln_own;
675 if (!QEMPTY(&missiles)) {
676 /* I arbitrarily chose 100 mindam -KHS */
678 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
679 "sector", victim, mission);
680 qp = missiles.q_forw;
681 while (qp != (&missiles)) {
689 if (QEMPTY(&bombers)) {
692 glp = (struct genlist *)qp;
701 * If there are planes performing an
702 * interdict or support mission, find
703 * some escorts for them, if possible.
704 * Up to 2 per bomber, if possible.
706 find_escorts(x, y, plane_owner, &escorts);
708 if (mission == MI_SINTERDICT)
709 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
711 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
713 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
715 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
716 plp = (struct plist *)qp;
717 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
718 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
721 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
725 air = (struct airport *)qp;
726 md = mapdist(x, y, air->x, air->y);
731 /* Split off the bombers at this base into b */
732 divide(&bombers, &b, air->x, air->y);
734 /* Split off the escorts at this base into e */
735 divide(&escorts, &e, air->x, air->y);
739 mission_flags |= P_X; /* stealth (shhh) */
740 mission_flags |= P_H; /* gets turned off if not all choppers */
742 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
743 0, mission_flags, &tech);
749 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
750 P_F | P_ESC, mission_flags, &tech);
752 BestAirPath(pp, air->x, air->y, x, y);
753 wu(0, plane_owner, "Flying %s mission from %s\n",
754 mission_name(mission), xyas(air->x, air->y, plane_owner));
755 if (air->own && (air->own != plane_owner)) {
756 wu(0, air->own, "%s is flying %s mission from %s\n",
757 cname(plane_owner), mission_name(mission),
758 xyas(air->x, air->y, air->own));
761 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
765 air_damage(&b, x, y, mission, victim, s, hardtarget);
773 if (sect.sct_type == SCT_WATER)
774 nreport(plane_owner, N_SHP_BOMB, victim, 1);
776 nreport(plane_owner, N_SCT_BOMB, victim, 1);
779 /* free up all this memory */
782 glp = (struct genlist *)qp;
790 while (qp != (&escorts)) {
798 while (qp != (&bombers)) {
809 cando(int mission, int type)
813 if (type == EF_PLANE)
817 if (type == EF_PLANE)
821 if ((type == EF_PLANE) || (type == EF_SHIP))
829 if (type == EF_PLANE)
842 nameofitem(struct genitem *gp, int type)
846 return prship((struct shpstr *)gp);
848 return prplane((struct plnstr *)gp);
850 return prland((struct lndstr *)gp);
856 mission_name(short int mission)
860 return "an interdiction";
864 return "a offensive support";
866 return "a defensive support";
872 return "a sub interdiction";
874 return "an air defense";
876 return "a mysterious";
880 show_mission(int type, struct nstr_item *np)
883 int first = 1, radius;
887 size = MAX(sizeof(struct lndstr), sizeof(struct plnstr));
888 size = MAX(size, sizeof(struct shpstr));
889 block = malloc(size);
891 while (nxtitem(np, block)) {
892 gp = (struct genitem *)block;
893 if (!player->owner || gp->own == 0)
897 pr("Thing x,y op-sect rad mission\n");
900 pr("%-25s", nameofitem(gp, type));
901 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
902 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
903 gp->mission == MI_OSUPPORT ||
904 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
906 oprange(gp, type, &radius);
907 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
908 if (radius < gp->radius)
911 pr(" %4d", gp->radius);
912 } else if (gp->mission == MI_RESERVE) {
916 getsect(gp->x, gp->y, §);
917 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
920 if (((struct lndstr *)block)->lnd_rad_max == 0)
923 plus += ((struct lndstr *)block)->lnd_rad_max;
924 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
926 } else if (gp->mission == MI_ESCORT) {
929 ((float)((struct plnstr *)block)->pln_range / 2.0)
934 pr(" is on %s mission\n", mission_name(gp->mission));
936 pr(" has no mission.\n");
941 oprange(struct genitem *gp, int type, int *radius)
950 getship(gp->uid, &ship);
951 range = ldround(techfact(gp->tech,
952 (double)ship.shp_frnge / 2.0), 1);
955 getland(gp->uid, &land);
956 range = ldround(techfact((int)land.lnd_tech,
957 (double)land.lnd_frg / 2.0), 1);
960 getplane(gp->uid, &plane);
961 /* missiles go one way, so we can use all the range */
962 if (plchr[(int)plane.pln_type].pl_flags & P_M)
963 range = plane.pln_range;
965 range = ldround((double)plane.pln_range / 2.0, 1);;
969 if ((*radius) > range)
976 * Remove all planes who cannot go on
977 * the mission from the plane list.
980 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
983 struct emp_qelem *qp, *next;
988 struct plchrstr *pcp;
993 for (qp = list->q_forw; qp != list; qp = next) {
995 plp = (struct plist *)qp;
999 if (pp->pln_effic < 40) {
1005 if (pp->pln_mobil < 1) {
1012 if (ontradingblock(EF_PLANE, pp)) {
1022 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1024 if ((wantflags & y) == y)
1025 if ((pcp->pl_flags & y) != y) {
1047 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1051 if ((pcp->pl_flags & P_E) ||
1052 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1064 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1066 if ((nowantflags & y) == y)
1067 if ((pcp->pl_flags & y) == y)
1076 if (pp->pln_ship >= 0) {
1077 if (!getship(pp->pln_ship, &ship)) {
1080 putplane(pp->pln_uid, pp);
1085 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1088 if (ship.shp_effic < SHIP_MINEFF) {
1091 /* Can't fly off of inefficient or non-owned, non-allied ships */
1092 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1093 ((ship.shp_own != pp->pln_own) &&
1094 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1100 if (pp->pln_land >= 0) {
1101 if (!getland(pp->pln_land, &land)) {
1104 putplane(pp->pln_uid, pp);
1109 if (!(pcp->pl_flags & P_E))
1111 if (land.lnd_effic < LAND_MINEFF)
1114 /* Can't fly off of inefficient or non-owned, non-allied units */
1115 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1116 ((land.lnd_own != pp->pln_own) &&
1117 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1123 /* Can't fly off units in ships or other units */
1124 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1130 /* Now, check the sector status if not on a plane or unit */
1131 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1132 /* If we can't get the sector, we can't check it, and can't fly */
1133 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1138 /* First, check allied status */
1139 /* Can't fly from non-owned sectors or non-allied sectors */
1140 if ((sect.sct_own != pp->pln_own) &&
1141 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1146 /* non-vtol plane */
1147 if ((pcp->pl_flags & P_V) == 0) {
1148 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1155 if (pcp->pl_flags & P_A) {
1156 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1163 putplane(pp->pln_uid, pp);
1168 * Arm only the planes at x,y
1172 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1173 int mission, struct ichrstr *ip, int flags,
1174 int mission_flags, int *tech)
1176 struct emp_qelem *qp;
1177 struct emp_qelem *next;
1182 for (qp = list->q_forw; qp != list; qp = next) {
1184 plp = (struct plist *)qp;
1186 if (plp->plane.pln_x != x)
1188 if (plp->plane.pln_y != y)
1191 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1196 if (flags & (P_S | P_I)) {
1197 if (plp->pcp->pl_flags & P_S)
1198 mission_flags |= P_S;
1199 if (plp->pcp->pl_flags & P_I)
1200 mission_flags |= P_I;
1202 if (*tech > plp->plane.pln_tech)
1203 *tech = plp->plane.pln_tech;
1204 if (!(plp->pcp->pl_flags & P_H))
1205 /* no stealth on this mission */
1206 mission_flags &= ~P_H;
1207 if (!(plp->pcp->pl_flags & P_X))
1208 /* no stealth on this mission */
1209 mission_flags &= ~P_X;
1210 if (!(plp->pcp->pl_flags & P_A)) {
1211 /* no asw on this mission */
1212 mission_flags &= ~P_A;
1214 if (!(plp->pcp->pl_flags & P_MINE)) {
1215 /* no asw on this mission */
1216 mission_flags &= ~P_MINE;
1220 * Mob costs for missions are 1/2 normal
1223 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1224 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1227 return mission_flags;
1231 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1234 struct plchrstr *pcp;
1246 if (pp->pln_ship >= 0) {
1247 getship(pp->pln_ship, &ship);
1248 item = ship.shp_item;
1249 } else if (pp->pln_land >= 0) {
1250 getland(pp->pln_land, &land);
1251 item = land.lnd_item;
1253 getsect(pp->pln_x, pp->pln_y, §);
1254 item = sect.sct_item;
1256 if (pcp->pl_fuel > item[I_PETROL]) {
1259 item[I_PETROL] -= pcp->pl_fuel;
1261 if (!(flags & P_F)) {
1267 if (pp->pln_nuketype == -1) {
1269 needed = pp->pln_load;
1273 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1276 needed = (pp->pln_load * 2) / ip->i_lbs;
1279 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1282 needed = (pp->pln_load * 2) / ip->i_lbs;
1285 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1288 needed = pp->pln_load / ip->i_lbs;
1291 if (pp->pln_nuketype == -1)
1294 case 'i': /* missile interception */
1297 needed = pp->pln_load;
1303 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1306 if (itype != I_NONE) {
1307 if (itype == I_SHELL && item[itype] < needed)
1308 item[itype] += supply_commod(plp->plane.pln_own,
1312 if (item[itype] < needed)
1314 item[itype] -= needed;
1316 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1317 plp->bombs = needed;
1321 if (pp->pln_ship >= 0)
1322 putship(ship.shp_uid, &ship);
1323 else if (pp->pln_land >= 0)
1324 putland(land.lnd_uid, &land);
1331 * Return 1 if this x,y pair is in the list
1334 find_airport(struct emp_qelem *airp, coord x, coord y)
1336 struct emp_qelem *qp;
1339 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1340 a = (struct airport *)qp;
1341 if ((a->x == x) && (a->y == y))
1348 /* #*# This needs to be changed to include acc's -KHS */
1350 add_airport(struct emp_qelem *airp, coord x, coord y)
1355 a = malloc(sizeof(struct airport));
1359 getsect(x, y, §);
1360 a->own = sect.sct_own;
1362 emp_insque((struct emp_qelem *)a, airp);
1366 * Take all the planes in list 1 that
1367 * are at x,y, and put them into list 2.
1370 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1372 struct emp_qelem *qp, *next;
1375 for (qp = l1->q_forw; qp != l1; qp = next) {
1377 plp = (struct plist *)qp;
1379 if (plp->plane.pln_x != x)
1381 if (plp->plane.pln_y != y)
1390 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1391 natid victim, s_char *s, int hardtarget)
1393 struct emp_qelem *qp;
1396 int newdam, dam = 0;
1400 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1401 plp = (struct plist *)qp;
1404 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1411 if (plp->pcp->pl_flags & P_A) {
1412 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1414 "\t%s detects sub movement in %s\n",
1415 prplane(pp), xyas(x, y, pp->pln_own));
1418 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1420 "\t%s tracks %s %s at %s\n",
1421 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1425 "\t%s depth-charging %s %s in %s\n",
1426 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1429 "\t%s pinbombing %s %s in %s\n",
1430 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1432 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1433 if (plp->plane.pln_nuketype != -1)
1435 else if (hardtarget != SECT_HARDTARGET)
1436 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1437 /* Always calculate damage */
1438 if (roll(100) <= hitchance) {
1439 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1441 if (mission == MI_INTERDICT) {
1443 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1444 prplane(pp), nukedam, cname(victim), s);
1449 "\t\thit %s %s for %d damage\n",
1450 cname(victim), s, newdam);
1454 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1455 wu(0, pp->pln_own, "missed\n");
1456 if (mission == MI_SINTERDICT) {
1458 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1459 xyas(x, y, victim));
1460 } else if (*s == 's') {
1462 "SPLASH! Bombs miss your %s in %s\n",
1463 s, xyas(x, y, victim));
1465 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1466 s, xyas(x, y, victim));
1468 /* Now, even though we missed, the bombs
1470 collateral_damage(x, y, newdam, bombers);
1473 /* use up missiles */
1474 if (plp->pcp->pl_flags & P_M) {
1475 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1485 * Check to see if anyone hostile to the victim
1486 * is running an air defense mission on this
1487 * sector. If so, do air combat
1490 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1491 struct emp_qelem *esc_list)
1494 int mission_flags, tech, combat = 0, rel, dist, z;
1495 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1497 struct genlist *glp;
1499 struct genlist mi[MAXNOC];
1505 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1507 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1510 memset(mi, 0, sizeof(mi));
1511 for (z = 1; z < MAXNOC; z++)
1512 emp_initque((struct emp_qelem *)&mi[z]);
1514 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1516 for (cn = 1; cn < MAXNOC; cn++) {
1517 /* Check our relations */
1518 rel = getrel(getnatp(cn), victim);
1523 if (QEMPTY(&mi[cn].queue))
1526 /* Ok, make a list of all the interceptors. Note that this *copies* the
1527 * list from the mission creation. This list must be deleted later. */
1528 emp_initque(&interceptors);
1529 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1532 glp = (struct genlist *)qp;
1533 gp = (struct genitem *)glp->thing;
1534 plp = (struct plist *)qp;
1536 dist = mapdist(x, y, gp->x, gp->y);
1538 plp = malloc(sizeof(struct plist));
1539 memset(plp, 0, sizeof(struct plist));
1540 plp->pcp = (struct plchrstr *)glp->cp;
1541 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1543 /* missiles go one way, so we can use all the range */
1544 if (!(plp->pcp->pl_flags & P_M))
1546 /* If it's out of range, free it and continue on */
1547 if (dist > plp->plane.pln_range) {
1551 emp_insque(&plp->queue, &interceptors);
1554 /* Remove those who cannot go */
1555 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1557 if (QEMPTY(&interceptors))
1560 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1561 * that the higher numbered planes go into battle (they should be the better ones
1562 * at fighting, if all went well.) */
1564 for (qp = interceptors.q_forw; qp != (&interceptors);
1567 tcount -= (count * 2);
1568 /* Just in case there are more incoming than we have */
1571 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1575 /* Free it up and continue */
1577 glp = (struct genlist *)qp;
1582 /* Now, make a list of all the airports these planes are coming from */
1584 for (qp = interceptors.q_forw; qp != (&interceptors);
1586 plp = (struct plist *)qp;
1587 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1588 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1591 /* Now, fly them out one airport at a time */
1592 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1593 struct airport *air;
1595 air = (struct airport *)qp;
1596 dist = mapdist(x, y, air->x, air->y);
1600 /* Split off the interceptors at this base into i */
1601 divide(&interceptors, &i, air->x, air->y);
1605 mission_flags |= P_X; /* stealth (shhh) */
1606 /* gets turned off if not all choppers */
1607 mission_flags |= P_H;
1608 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1609 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1611 /* Did we run out of interceptors? */
1614 /* Did we run out of bombers? */
1615 if (QEMPTY(bomb_list)) {
1616 /* Yes, so we have to put the rest of the interceptors back, and
1617 then continue, or we leak memory */
1622 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1623 mission_flags, &tech);
1625 /* Did we run out of interceptors? */
1628 /* Did we run out of bombers? */
1629 if (QEMPTY(bomb_list)) {
1630 /* Yes, so we have to put the rest of the interceptors back, and
1631 then continue, or we leak memory */
1636 BestAirPath(path, air->x, air->y, x, y);
1637 wu(0, cn, "Flying %s mission from %s\n",
1638 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1639 if (air->own && (air->own != cn)) {
1640 wu(0, air->own, "%s is flying %s mission from %s\n",
1641 cname(cn), mission_name(MI_AIR_DEFENSE),
1642 xyas(air->x, air->y, air->own));
1645 /* Now, fly the planes to the sector */
1646 emp_initque(&empty);
1647 ac_encounter(&i, &empty, air->x, air->y,
1648 path, mission_flags, 1, bomb_list, esc_list);
1650 /* If none made it, continue */
1654 /* Some made it, so now they get to try to fight. */
1655 /* Intercept the escorts first */
1657 if (!QEMPTY(esc_list)) {
1658 mpr(victim, "%s air defense planes intercept!\n",
1660 ac_combat_headers(victim, cn);
1661 ac_airtoair(esc_list, &i);
1664 /* Now intercept the bombers */
1665 if (!QEMPTY(bomb_list)) {
1667 mpr(victim, "%s air defense planes intercept!\n",
1669 ac_combat_headers(victim, cn);
1671 ac_airtoair(bomb_list, &i);
1680 /* We have to free all of these, if they are still there, otherwise they get
1681 lost and we leak memory all over the place. */
1682 for (cn = 1; cn < MAXNOC; cn++) {
1683 /* free up all this memory if it's still there */
1684 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1686 glp = (struct genlist *)qp;