2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * 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
56 #include "prototypes.h"
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, s_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;
149 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
150 glp = (struct genlist *)qp;
151 gp = (struct genitem *)glp->thing;
153 if (glp->type != EF_SHIP)
155 mcp = (struct mchrstr *)glp->cp;
156 if (!(mcp->m_flags & M_SUB))
160 /* They were all subs! */
166 * Interdict ships & land units
170 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
175 struct genlist mi[MAXNOC];
179 memset(mi, 0, sizeof(mi));
180 for (z = 1; z < MAXNOC; z++)
181 emp_initque((struct emp_qelem *)&mi[z]);
183 build_mission_list(mi, x, y, mission, victim);
185 for (cn = 1; cn < MAXNOC; cn++) {
188 if (mission == MI_SINTERDICT) {
189 if (getrel(getnatp(cn), victim) >= FRIENDLY)
191 } else if (getrel(getnatp(cn), victim) > HOSTILE)
194 if (QEMPTY(&mi[cn].queue))
197 osubs = only_subs(&mi[cn].queue);
198 newdam = perform_mission(x, y, victim, &mi[cn].queue,
199 mission, s, hardtarget);
202 /* If only subs responded, then we don't know who's
206 "Enemy interdiction mission does %d damage!\n",
210 "%s interdiction mission does %d damage!\n",
216 collateral_damage(x, y, dam, 0);
222 * Perform a mission against victim, on behalf of actee
225 off_support(coord x, coord y, natid victim, natid actee)
228 struct genlist mi[MAXNOC];
231 memset(mi, 0, sizeof(mi));
232 for (z = 1; z < MAXNOC; z++)
233 emp_initque((struct emp_qelem *)&mi[z]);
235 build_mission_list(mi, x, y, MI_SUPPORT, victim);
236 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
238 dam = dosupport(mi, x, y, victim, actee);
243 * Perform a mission against victim, on behalf of actee
246 def_support(coord x, coord y, natid victim, natid actee)
249 struct genlist mi[MAXNOC];
252 memset(mi, 0, sizeof(mi));
253 for (z = 1; z < MAXNOC; z++)
254 emp_initque((struct emp_qelem *)&mi[z]);
256 build_mission_list(mi, x, y, MI_SUPPORT, victim);
257 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
259 dam = dosupport(mi, x, y, victim, actee);
264 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
270 for (cn = 1; cn < MAXNOC; cn++) {
271 rel = getrel(getnatp(cn), actee);
272 if ((cn != actee) && (rel != ALLIED))
274 rel = getrel(getnatp(cn), victim);
275 if ((cn != actee) && (rel != AT_WAR))
278 if (QEMPTY(&mi[cn].queue))
281 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
282 "", SECT_HARDTARGET);
288 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
291 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
292 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
293 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
297 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
298 int type, natid victim)
315 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
316 size = max(size,sizeof(struct plnstr));
317 block = (s_char *)malloc(size);
319 size = sizeof(u_block);
320 block = (s_char *)&u_block;
322 snxtitem_all(&ni, type);
323 while (nxtitem(&ni, block)) {
324 gp = (struct genitem *)block;
332 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
335 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
336 (gp->mission != MI_INTERDICT))
339 relat = getrel(getnatp(gp->own), victim);
340 if (mission == MI_SINTERDICT) {
341 if (relat >= FRIENDLY)
343 else if (type != EF_PLANE && relat > HOSTILE)
345 } else if (relat > HOSTILE)
348 dist = mapdist(x, y, gp->opx, gp->opy);
351 if (mission != MI_RESERVE) /* XXX */
352 oprange(gp, type, &radius);
357 /* Ok, it is within the operations range. */
358 /* Now check from where the object actually is */
359 dist = mapdist(x, y, gp->x, gp->y);
361 oprange(gp, type, &radius);
364 /* Ok, the object can get to where the x,y is */
367 if (mission != MI_AIR_DEFENSE) {
368 getsect(x, y, (s_char *)§);
369 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
372 * If the player->owner of the unit isn't at war
373 * with the victim, and doesn't own the
374 * sect being acted upon, and isn't the
375 * old player->owner of that sect, bounce them.
377 if (sect.sct_type != SCT_WATER &&
378 sect.sct_own != gp->own &&
379 sect.sct_oldown != gp->own)
385 glp = (struct genlist *)malloc(sizeof(struct genlist));
386 memset(glp, 0, sizeof(struct genlist));
392 glp->cp = (s_char *)&lchr[(int)gp->type];
395 glp->cp = (s_char *)&mchr[(int)gp->type];
398 glp->cp = (s_char *)&plchr[(int)gp->type];
401 glp->thing = (s_char *)malloc(size);
402 memcpy(glp->thing, block, size);
403 emp_insque(&glp->queue, &mi[gp->own].queue);
408 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
415 snxtitem_all(&ni, EF_PLANE);
416 while (nxtitem(&ni, (s_char *)&plane)) {
417 if (plane.pln_own != cn)
420 if (plane.pln_mission != MI_ESCORT)
423 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
425 if (dist > ((int)((float)plane.pln_range / 2.0)))
428 plp = (struct plist *)malloc(sizeof(struct plist));
429 memset(plp, 0, sizeof(struct plist));
430 plp->pcp = &plchr[(int)plane.pln_type];
432 emp_insque(&plp->queue, escorts);
437 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
438 int mission, s_char *s, int hardtarget)
440 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
441 struct emp_qelem *newqp;
450 struct plchrstr *pcp;
451 int dam = 0, dam2, mission_flags, tech;
452 natid plane_owner = 0;
453 int gun, shell, md, air_dam = 0;
454 double range2, prb, range, mobcost, hitchance;
456 getsect(x, y, §);
458 emp_initque(&missiles);
459 emp_initque(&bombers);
460 emp_initque(&escorts);
463 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
464 glp = (struct genlist *)qp;
465 gp = (struct genitem *)glp->thing;
467 md = mapdist(x, y, gp->x, gp->y);
469 if (glp->type == EF_LAND) {
470 lp = (struct lndstr *)glp->thing;
471 lcp = (struct lchrstr *)glp->cp;
473 if (lp->lnd_effic < LAND_MINFIREEFF)
476 if (mission == MI_SINTERDICT)
479 if ((mission == MI_INTERDICT) &&
480 (md > land_max_interdiction_range))
483 if (md > (lp->lnd_frg / 2))
486 if (lnd_getmil(lp) < 1)
489 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
490 range2 = (double)roundrange(range);
495 shell = lp->lnd_item[I_SHELL];
496 gun = lp->lnd_item[I_GUN];
497 if (shell == 0 || gun == 0)
500 if (has_supply(lp)) {
502 putland(lp->lnd_uid, lp);
503 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
504 lp->lnd_ammo, shell), 1);
505 if (sect.sct_type == SCT_WATER) {
506 double dam3 = (double)dam2;
507 if (chance(((double)lp->lnd_acc) / 100.0))
508 dam2 = ldround((dam3 / 2.0), 1);
511 if (sect.sct_type == SCT_WATER)
512 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
514 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
516 "%s fires at %s %s at %s\n",
517 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
519 mpr(victim, "%s %s fires at you at %s\n",
520 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
522 } else if (glp->type == EF_SHIP) {
523 sp = (struct shpstr *)glp->thing;
524 mcp = (struct mchrstr *)glp->cp;
526 if (sp->shp_effic < 60)
528 if (sp->shp_frnge == 0)
530 if (((mission == MI_INTERDICT) ||
531 (mission == MI_SINTERDICT)) &&
532 (md > ship_max_interdiction_range))
534 if (sp->shp_item[I_MILIT] < 1)
537 if ((mcp->m_flags & M_SUB) &&
538 (sect.sct_type != SCT_WATER))
541 if (mission == MI_SINTERDICT) {
542 if (!(mcp->m_flags & M_SONAR))
544 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
546 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
547 range2 *= (double)sp->shp_effic / 200.0;
550 /* can't look all the time */
554 if (mcp->m_flags & M_SUB) {
555 /* If we aren't shooting at "subs" or "ships" don't fire at all from
559 if (sp->shp_mobil < (s_char)0)
561 gun = sp->shp_item[I_GUN];
564 shell = sp->shp_item[I_SHELL];
565 if (shell < SHP_TORP_SHELLS)
566 shell += supply_commod(sp->shp_own,
567 sp->shp_x, sp->shp_y, I_SHELL,
568 SHP_TORP_SHELLS - shell);
569 if (shell < SHP_TORP_SHELLS)
572 range = sp->shp_effic * techfact(sp->shp_tech,
573 ((double)sp->shp_frnge)) /
576 range2 = (double)roundrange(range);
580 if (!line_of_sight((s_char **)0, x, y, gp->x, gp->y))
582 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
583 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
584 mobcost = (480.0 / (mobcost +
585 techfact(sp->shp_tech, mobcost)));
586 sp->shp_mobil -= mobcost;
587 putship(sp->shp_uid, sp);
588 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
591 "%s locking on %s %s in %s\n",
592 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
594 "\tEffective torpedo range is %.1f\n", range);
596 "\tWhooosh... Hitchance = %d%%\n",
597 (int)(hitchance * 100));
599 if (hitchance < 1.0 && !chance(hitchance)) {
600 wu(0, sp->shp_own, "\tMissed\n");
602 "Incoming torpedo sighted @ %s missed (whew)!\n",
606 wu(0, sp->shp_own, "\tBOOM!...\n");
607 dam2 = TORP_DAMAGE();
610 nreport(victim, N_TORP_SHIP, sp->shp_own, 1);
612 "\tTorpedo hit %s %s for %d damage\n",
613 cname(victim), s, dam2);
616 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
617 xyas(x, y, victim), dam2);
619 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
620 range2 = (double)roundrange(range);
623 gun = sp->shp_item[I_GUN];
624 gun = min(gun, sp->shp_glim);
625 shell = sp->shp_item[I_SHELL];
627 shell += supply_commod(sp->shp_own,
628 sp->shp_x, sp->shp_y, I_SHELL,
630 gun = min(gun, shell);
631 gun = min(gun, sp->shp_item[I_MILIT] / 2.0);
635 dam2 = seagun(sp->shp_effic, gun);
639 prb = ((double)md) / range2;
642 dam2 = (int)((float)dam2 / 2.0);
644 if (sect.sct_type == SCT_WATER)
645 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
647 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
649 "%s fires at %s %s at %s\n",
650 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
652 mpr(victim, "%s %s fires at you at %s\n",
653 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
655 sp->shp_item[I_SHELL] = shell - gun;
656 putship(sp->shp_uid, sp);
658 } else if (glp->type == EF_PLANE) {
659 pcp = (struct plchrstr *)glp->cp;
660 if (pcp->pl_flags & P_M)
661 /* units have their own missile interdiction */
662 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
665 /* save planes for later */
666 plp = (struct plist *)malloc(sizeof(struct plist));
668 memset(plp, 0, sizeof(struct plist));
670 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
671 if (plp->pcp->pl_flags & P_M)
672 emp_insque(&plp->queue, &missiles);
674 emp_insque(&plp->queue, &bombers);
675 plane_owner = plp->plane.pln_own;
678 if (!QEMPTY(&missiles)) {
679 /* I arbitrarily chose 100 mindam -KHS */
681 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
682 "sector", victim, mission);
683 qp = missiles.q_forw;
684 while (qp != (&missiles)) {
692 if (QEMPTY(&bombers)) {
695 glp = (struct genlist *)qp;
704 * If there are planes performing an
705 * interdict or support mission, find
706 * some escorts for them, if possible.
707 * Up to 2 per bomber, if possible.
709 find_escorts(x, y, plane_owner, &escorts);
711 if (mission == MI_SINTERDICT)
712 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
714 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
716 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
718 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
719 plp = (struct plist *)qp;
720 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
721 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
724 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
728 air = (struct airport *)qp;
729 md = mapdist(x, y, air->x, air->y);
734 /* Split off the bombers at this base into b */
735 divide(&bombers, &b, air->x, air->y);
737 /* Split off the escorts at this base into e */
738 divide(&escorts, &e, air->x, air->y);
742 mission_flags |= P_X; /* stealth (shhh) */
743 mission_flags |= P_H; /* gets turned off if not all choppers */
745 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
746 0, mission_flags, &tech);
752 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
753 P_F | P_ESC, mission_flags, &tech);
755 BestAirPath(pp, air->x, air->y, x, y);
756 wu(0, plane_owner, "Flying %s mission from %s\n",
757 mission_name(mission), xyas(air->x, air->y, plane_owner));
758 if (air->own && (air->own != plane_owner)) {
759 wu(0, air->own, "%s is flying %s mission from %s\n",
760 cname(plane_owner), mission_name(mission),
761 xyas(air->x, air->y, air->own));
764 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
768 air_damage(&b, x, y, mission, victim, s, hardtarget);
776 if (sect.sct_type == SCT_WATER)
777 nreport(plane_owner, N_SHP_BOMB, victim, 1);
779 nreport(plane_owner, N_SCT_BOMB, victim, 1);
782 /* free up all this memory */
785 glp = (struct genlist *)qp;
793 while (qp != (&escorts)) {
801 while (qp != (&bombers)) {
812 cando(int mission, int type)
816 if (type == EF_PLANE)
820 if (type == EF_PLANE)
824 if ((type == EF_PLANE) || (type == EF_SHIP))
832 if (type == EF_PLANE)
845 nameofitem(struct genitem *gp, int type)
849 return prship((struct shpstr *)gp);
851 return prplane((struct plnstr *)gp);
853 return prland((struct lndstr *)gp);
859 mission_short_name(int mission)
884 mission_name(short int mission)
888 return "an interdiction";
892 return "a offensive support";
894 return "a defensive support";
900 return "a sub interdiction";
902 return "an air defense";
904 return "a mysterious";
908 show_mission(int type, struct nstr_item *np)
910 int size, first = 1, radius;
914 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
915 size = max(size, sizeof(struct shpstr));
916 block = (s_char *)malloc(size);
918 while (nxtitem(np, block)) {
919 gp = (struct genitem *)block;
920 if (!player->owner || gp->own == 0)
924 pr("Thing x,y op-sect rad mission\n");
927 pr("%-25s", nameofitem(gp, type));
928 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
929 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
930 gp->mission == MI_OSUPPORT ||
931 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
933 oprange(gp, type, &radius);
934 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
935 if (radius < gp->radius)
938 pr(" %4d", gp->radius);
939 } else if (gp->mission == MI_RESERVE) {
943 getsect(gp->x, gp->y, §);
944 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
947 if (((struct lndstr *)block)->lnd_rad_max == 0)
950 plus += ((struct lndstr *)block)->lnd_rad_max;
951 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
953 } else if (gp->mission == MI_ESCORT) {
956 ((float)((struct plnstr *)block)->pln_range / 2.0)
961 pr(" is on %s mission\n", mission_name(gp->mission));
963 pr(" has no mission.\n");
968 oprange(struct genitem *gp, int type, int *radius)
977 getship(gp->uid, &ship);
978 range = ldround(techfact(gp->tech,
979 (double)ship.shp_frnge / 2.0), 1);
982 getland(gp->uid, &land);
983 range = ldround(techfact((int)land.lnd_tech,
984 (double)land.lnd_frg / 2.0), 1);
987 getplane(gp->uid, &plane);
988 /* missiles go one way, so we can use all the range */
989 if (plchr[(int)plane.pln_type].pl_flags & P_M)
990 range = plane.pln_range;
992 range = ldround((double)plane.pln_range / 2.0, 1);;
996 if ((*radius) > range)
1003 * Remove all planes who cannot go on
1004 * the mission from the plane list.
1007 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1010 struct emp_qelem *qp, *next;
1015 struct plchrstr *pcp;
1017 register int y, bad, bad1;
1020 for (qp = list->q_forw; qp != list; qp = next) {
1022 plp = (struct plist *)qp;
1026 if (pp->pln_effic < 40) {
1032 if (pp->pln_mobil < 1) {
1039 if (ontradingblock(EF_PLANE, (int *)pp)) {
1049 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1051 if ((wantflags & y) == y)
1052 if ((pcp->pl_flags & y) != y) {
1074 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1078 if ((pcp->pl_flags & P_E) ||
1079 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1091 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1093 if ((nowantflags & y) == y)
1094 if ((pcp->pl_flags & y) == y)
1103 if (pp->pln_ship >= 0) {
1104 if (!getship(pp->pln_ship, &ship)) {
1107 putplane(pp->pln_uid, pp);
1112 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1115 if (ship.shp_effic < SHIP_MINEFF) {
1118 /* Can't fly off of inefficient or non-owned, non-allied ships */
1119 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1120 ((ship.shp_own != pp->pln_own) &&
1121 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1127 if (pp->pln_land >= 0) {
1128 if (!getland(pp->pln_land, &land)) {
1131 putplane(pp->pln_uid, pp);
1136 if (!(pcp->pl_flags & P_E))
1138 if (land.lnd_effic < LAND_MINEFF)
1141 /* Can't fly off of inefficient or non-owned, non-allied units */
1142 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1143 ((land.lnd_own != pp->pln_own) &&
1144 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1150 /* Can't fly off units in ships or other units */
1151 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1157 /* Now, check the sector status if not on a plane or unit */
1158 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1159 /* If we can't get the sector, we can't check it, and can't fly */
1160 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1165 /* First, check allied status */
1166 /* Can't fly from non-owned sectors or non-allied sectors */
1167 if ((sect.sct_own != pp->pln_own) &&
1168 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1173 /* non-vtol plane */
1174 if ((pcp->pl_flags & P_V) == 0) {
1175 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1182 if (pcp->pl_flags & P_A) {
1183 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1190 putplane(pp->pln_uid, pp);
1195 * Arm only the planes at x,y
1199 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1200 int mission, struct ichrstr *ip, int flags,
1201 int mission_flags, int *tech)
1203 struct emp_qelem *qp;
1204 struct emp_qelem *next;
1209 for (qp = list->q_forw; qp != list; qp = next) {
1211 plp = (struct plist *)qp;
1213 if (plp->plane.pln_x != x)
1215 if (plp->plane.pln_y != y)
1218 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1223 if (flags & (P_S | P_I)) {
1224 if (plp->pcp->pl_flags & P_S)
1225 mission_flags |= P_S;
1226 if (plp->pcp->pl_flags & P_I)
1227 mission_flags |= P_I;
1229 if (*tech > plp->plane.pln_tech)
1230 *tech = plp->plane.pln_tech;
1231 if (!(plp->pcp->pl_flags & P_H))
1232 /* no stealth on this mission */
1233 mission_flags &= ~P_H;
1234 if (!(plp->pcp->pl_flags & P_X))
1235 /* no stealth on this mission */
1236 mission_flags &= ~P_X;
1237 if (!(plp->pcp->pl_flags & P_A)) {
1238 /* no asw on this mission */
1239 mission_flags &= ~P_A;
1241 if (!(plp->pcp->pl_flags & P_MINE)) {
1242 /* no asw on this mission */
1243 mission_flags &= ~P_MINE;
1247 * Mob costs for missions are 1/2 normal
1250 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1251 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1254 return mission_flags;
1258 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1261 register struct plchrstr *pcp;
1273 if (pp->pln_ship >= 0) {
1274 getship(pp->pln_ship, &ship);
1275 item = ship.shp_item;
1276 } else if (pp->pln_land >= 0) {
1277 getland(pp->pln_land, &land);
1278 item = land.lnd_item;
1280 getsect(pp->pln_x, pp->pln_y, §);
1281 item = sect.sct_item;
1283 if (pcp->pl_fuel > item[I_PETROL]) {
1286 item[I_PETROL] -= pcp->pl_fuel;
1288 if (!(flags & P_F)) {
1294 if (pp->pln_nuketype == -1) {
1296 needed = pp->pln_load;
1300 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1302 itype = ip->i_vtype;
1303 needed = (pp->pln_load * 2) / ip->i_lbs;
1306 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1308 itype = ip->i_vtype;
1309 needed = (pp->pln_load * 2) / ip->i_lbs;
1312 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1315 needed = pp->pln_load / ip->i_lbs;
1318 if (pp->pln_nuketype == -1)
1321 case 'i': /* missile interception */
1324 needed = pp->pln_load;
1330 if (rval < 0 || (itype && needed <= 0)) {
1333 if (item[itype] < needed && (itype == I_SHELL))
1334 item[itype] += supply_commod(plp->plane.pln_own,
1335 plp->plane.pln_x, plp->plane.pln_y,
1337 if (item[itype] < needed) {
1340 item[itype] -= needed;
1342 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1343 plp->bombs = needed;
1347 if (pp->pln_ship >= 0)
1348 putship(ship.shp_uid, &ship);
1349 else if (pp->pln_land >= 0)
1350 putland(land.lnd_uid, &land);
1357 * Return 1 if this x,y pair is in the list
1360 find_airport(struct emp_qelem *airp, coord x, coord y)
1362 struct emp_qelem *qp;
1365 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1366 a = (struct airport *)qp;
1367 if ((a->x == x) && (a->y == y))
1374 /* #*# This needs to be changed to include acc's -KHS */
1376 add_airport(struct emp_qelem *airp, coord x, coord y)
1381 a = (struct airport *)malloc(sizeof(struct airport));
1385 getsect(x, y, §);
1386 a->own = sect.sct_own;
1388 emp_insque((struct emp_qelem *)a, airp);
1392 * Take all the planes in list 1 that
1393 * are at x,y, and put them into list 2.
1396 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1398 struct emp_qelem *qp, *next;
1401 for (qp = l1->q_forw; qp != l1; qp = next) {
1403 plp = (struct plist *)qp;
1405 if (plp->plane.pln_x != x)
1407 if (plp->plane.pln_y != y)
1416 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1417 natid victim, s_char *s, int hardtarget)
1419 struct emp_qelem *qp;
1422 int newdam, dam = 0;
1426 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1427 plp = (struct plist *)qp;
1430 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1437 if (plp->pcp->pl_flags & P_A) {
1438 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1440 "\t%s detects sub movement in %s\n",
1441 prplane(pp), xyas(x, y, pp->pln_own));
1444 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1446 "\t%s tracks %s %s at %s\n",
1447 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1451 "\t%s depth-charging %s %s in %s\n",
1452 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1455 "\t%s pinbombing %s %s in %s\n",
1456 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1458 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1459 if (plp->plane.pln_nuketype != -1)
1461 else if (hardtarget != SECT_HARDTARGET)
1462 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1463 /* Always calculate damage */
1464 if (roll(100) <= hitchance) {
1465 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1467 if (mission == MI_INTERDICT) {
1469 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1470 prplane(pp), nukedam, cname(victim), s);
1475 "\t\thit %s %s for %d damage\n",
1476 cname(victim), s, newdam);
1480 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1481 wu(0, pp->pln_own, "missed\n");
1482 if (mission == MI_SINTERDICT) {
1484 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1485 xyas(x, y, victim));
1486 } else if (*s == 's') {
1488 "SPLASH! Bombs miss your %s in %s\n",
1489 s, xyas(x, y, victim));
1491 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1492 s, xyas(x, y, victim));
1494 /* Now, even though we missed, the bombs
1496 collateral_damage(x, y, newdam, bombers);
1499 /* use up missiles */
1500 if (plp->pcp->pl_flags & P_M) {
1501 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1511 * Check to see if anyone hostile to the victim
1512 * is running an air defense mission on this
1513 * sector. If so, do air combat
1516 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1517 struct emp_qelem *esc_list)
1520 int mission_flags, tech, combat = 0, rel, dist, z;
1521 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1523 struct genlist *glp;
1525 struct genlist mi[MAXNOC];
1531 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1533 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1536 memset(mi, 0, sizeof(mi));
1537 for (z = 1; z < MAXNOC; z++)
1538 emp_initque((struct emp_qelem *)&mi[z]);
1540 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1542 for (cn = 1; cn < MAXNOC; cn++) {
1543 /* Check our relations */
1544 rel = getrel(getnatp(cn), victim);
1549 if (QEMPTY(&mi[cn].queue))
1552 /* Ok, make a list of all the interceptors. Note that this *copies* the
1553 * list from the mission creation. This list must be deleted later. */
1554 emp_initque(&interceptors);
1555 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1558 glp = (struct genlist *)qp;
1559 gp = (struct genitem *)glp->thing;
1560 plp = (struct plist *)qp;
1562 dist = mapdist(x, y, gp->x, gp->y);
1564 plp = (struct plist *)malloc(sizeof(struct plist));
1565 memset(plp, 0, sizeof(struct plist));
1566 plp->pcp = (struct plchrstr *)glp->cp;
1567 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1569 /* missiles go one way, so we can use all the range */
1570 if (!(plp->pcp->pl_flags & P_M))
1572 /* If it's out of range, free it and continue on */
1573 if (dist > plp->plane.pln_range) {
1577 emp_insque(&plp->queue, &interceptors);
1580 /* Remove those who cannot go */
1581 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1583 if (QEMPTY(&interceptors))
1586 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1587 * that the higher numbered planes go into battle (they should be the better ones
1588 * at fighting, if all went well.) */
1590 for (qp = interceptors.q_forw; qp != (&interceptors);
1593 tcount -= (count * 2);
1594 /* Just in case there are more incoming than we have */
1597 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1601 /* Free it up and continue */
1603 glp = (struct genlist *)qp;
1604 free((s_char *)glp);
1608 /* Now, make a list of all the airports these planes are coming from */
1610 for (qp = interceptors.q_forw; qp != (&interceptors);
1612 plp = (struct plist *)qp;
1613 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1614 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1617 /* Now, fly them out one airport at a time */
1618 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1619 struct airport *air;
1621 air = (struct airport *)qp;
1622 dist = mapdist(x, y, air->x, air->y);
1626 /* Split off the interceptors at this base into i */
1627 divide(&interceptors, &i, air->x, air->y);
1631 mission_flags |= P_X; /* stealth (shhh) */
1632 /* gets turned off if not all choppers */
1633 mission_flags |= P_H;
1634 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1635 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1637 /* Did we run out of interceptors? */
1640 /* Did we run out of bombers? */
1641 if (QEMPTY(bomb_list)) {
1642 /* Yes, so we have to put the rest of the interceptors back, and
1643 then continue, or we leak memory */
1648 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1649 mission_flags, &tech);
1651 /* Did we run out of interceptors? */
1654 /* Did we run out of bombers? */
1655 if (QEMPTY(bomb_list)) {
1656 /* Yes, so we have to put the rest of the interceptors back, and
1657 then continue, or we leak memory */
1662 BestAirPath(path, air->x, air->y, x, y);
1663 wu(0, cn, "Flying %s mission from %s\n",
1664 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1665 if (air->own && (air->own != cn)) {
1666 wu(0, air->own, "%s is flying %s mission from %s\n",
1667 cname(cn), mission_name(MI_AIR_DEFENSE),
1668 xyas(air->x, air->y, air->own));
1671 /* Now, fly the planes to the sector */
1672 emp_initque(&empty);
1673 ac_encounter(&i, &empty, air->x, air->y,
1674 path, mission_flags, 1, bomb_list, esc_list);
1676 /* If none made it, continue */
1680 /* Some made it, so now they get to try to fight. */
1681 /* Intercept the escorts first */
1683 if (!QEMPTY(esc_list)) {
1684 mpr(victim, "%s air defense planes intercept!\n",
1686 ac_combat_headers(victim, cn);
1687 ac_airtoair(esc_list, &i, air->own);
1690 /* Now intercept the bombers */
1691 if (!QEMPTY(bomb_list)) {
1693 mpr(victim, "%s air defense planes intercept!\n",
1695 ac_combat_headers(victim, cn);
1697 ac_airtoair(bomb_list, &i, air->own);
1706 /* We have to free all of these, if they are still there, otherwise they get
1707 lost and we leak memory all over the place. */
1708 for (cn = 1; cn < MAXNOC; cn++) {
1709 /* free up all this memory if it's still there */
1710 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1712 glp = (struct genlist *)qp;
1714 free((s_char *)glp);