2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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
57 #include "prototypes.h"
61 struct emp_qelem queue;
66 static void add_airport(struct emp_qelem *, coord, coord);
67 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
69 static void build_mission_list(struct genlist *, coord, coord, int, natid);
70 static void build_mission_list_type(struct genlist *, coord, coord, int,
72 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
73 static int dosupport(struct genlist *, coord, coord, natid, natid);
74 static int find_airport(struct emp_qelem *, coord, coord);
75 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
76 int, struct ichrstr *, int, int, int *);
77 static void mission_pln_sel(struct emp_qelem *, int, int, int);
78 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
82 * Interdict commodities & transported planes
85 ground_interdict(coord x, coord y, natid victim, s_char *s)
88 int dam = 0, newdam, rel;
89 struct genlist mi[MAXNOC];
92 memset(mi, 0, sizeof(mi));
93 for (z = 1; z < MAXNOC; z++)
94 emp_initque((struct emp_qelem *)&mi[z]);
96 build_mission_list(mi, x, y, MI_INTERDICT, victim);
98 for (cn = 1; cn < MAXNOC; cn++) {
99 rel = getrel(getnatp(cn), victim);
103 if (QEMPTY(&mi[cn].queue))
106 newdam = perform_mission(x, y, victim, &mi[cn].queue,
107 MI_INTERDICT, s, SECT_HARDTARGET);
110 mpr(victim, "%s interdiction mission does %d damage!\n",
114 collateral_damage(x, y, dam, 0);
120 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
122 extern double collateral_dam;
129 getsect(x, y, §);
131 coll = ldround((double)dam * collateral_dam, 1);
134 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
135 xyas(x, y, sect.sct_own), coll);
136 sectdamage(§, coll, list);
144 only_subs(struct emp_qelem *list)
146 struct emp_qelem *qp;
151 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
152 glp = (struct genlist *)qp;
153 gp = (struct genitem *)glp->thing;
155 if (glp->type != EF_SHIP)
157 mcp = (struct mchrstr *)glp->cp;
158 if (!(mcp->m_flags & M_SUB))
162 /* They were all subs! */
168 * Interdict ships & land units
172 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
177 struct genlist mi[MAXNOC];
181 memset(mi, 0, sizeof(mi));
182 for (z = 1; z < MAXNOC; z++)
183 emp_initque((struct emp_qelem *)&mi[z]);
185 build_mission_list(mi, x, y, mission, victim);
187 for (cn = 1; cn < MAXNOC; cn++) {
190 if (mission == MI_SINTERDICT) {
191 if (getrel(getnatp(cn), victim) >= FRIENDLY)
193 } else if (getrel(getnatp(cn), victim) > HOSTILE)
196 if (QEMPTY(&mi[cn].queue))
199 osubs = only_subs(&mi[cn].queue);
200 newdam = perform_mission(x, y, victim, &mi[cn].queue,
201 mission, s, hardtarget);
204 /* If only subs responded, then we don't know who's
208 "Enemy interdiction mission does %d damage!\n",
212 "%s interdiction mission does %d damage!\n",
218 collateral_damage(x, y, dam, 0);
224 * Perform a mission against victim, on behalf of actee
227 off_support(coord x, coord y, natid victim, natid actee)
230 struct genlist mi[MAXNOC];
233 memset(mi, 0, sizeof(mi));
234 for (z = 1; z < MAXNOC; z++)
235 emp_initque((struct emp_qelem *)&mi[z]);
237 build_mission_list(mi, x, y, MI_SUPPORT, victim);
238 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
240 dam = dosupport(mi, x, y, victim, actee);
245 * Perform a mission against victim, on behalf of actee
248 def_support(coord x, coord y, natid victim, natid actee)
251 struct genlist mi[MAXNOC];
254 memset(mi, 0, sizeof(mi));
255 for (z = 1; z < MAXNOC; z++)
256 emp_initque((struct emp_qelem *)&mi[z]);
258 build_mission_list(mi, x, y, MI_SUPPORT, victim);
259 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
261 dam = dosupport(mi, x, y, victim, actee);
266 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
272 for (cn = 1; cn < MAXNOC; cn++) {
273 rel = getrel(getnatp(cn), actee);
274 if ((cn != actee) && (rel != ALLIED))
276 rel = getrel(getnatp(cn), victim);
277 if ((cn != actee) && (rel != AT_WAR))
280 if (QEMPTY(&mi[cn].queue))
283 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
284 "", SECT_HARDTARGET);
290 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
293 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
294 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
295 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
299 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
300 int type, natid victim)
317 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
318 size = max(size,sizeof(struct plnstr));
319 block = (s_char *)malloc(size);
321 size = sizeof(u_block);
322 block = (s_char *)&u_block;
324 snxtitem_all(&ni, type);
325 while (nxtitem(&ni, block)) {
326 gp = (struct genitem *)block;
334 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
337 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
338 (gp->mission != MI_INTERDICT))
341 relat = getrel(getnatp(gp->own), victim);
342 if (mission == MI_SINTERDICT) {
343 if (relat >= FRIENDLY)
345 else if (type != EF_PLANE && relat > HOSTILE)
347 } else if (relat > HOSTILE)
350 dist = mapdist(x, y, gp->opx, gp->opy);
353 if (mission != MI_RESERVE) /* XXX */
354 oprange(gp, type, &radius);
359 /* Ok, it is within the operations range. */
360 /* Now check from where the object actually is */
361 dist = mapdist(x, y, gp->x, gp->y);
363 oprange(gp, type, &radius);
366 /* Ok, the object can get to where the x,y is */
369 if (mission != MI_AIR_DEFENSE) {
370 getsect(x, y, (s_char *)§);
371 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
374 * If the player->owner of the unit isn't at war
375 * with the victim, and doesn't own the
376 * sect being acted upon, and isn't the
377 * old player->owner of that sect, bounce them.
379 if (sect.sct_type != SCT_WATER &&
380 sect.sct_own != gp->own &&
381 sect.sct_oldown != gp->own)
387 glp = (struct genlist *)malloc(sizeof(struct genlist));
388 memset(glp, 0, sizeof(struct genlist));
394 glp->cp = (s_char *)&lchr[(int)gp->type];
397 glp->cp = (s_char *)&mchr[(int)gp->type];
400 glp->cp = (s_char *)&plchr[(int)gp->type];
403 glp->thing = (s_char *)malloc(size);
404 memcpy(glp->thing, block, size);
405 emp_insque(&glp->queue, &mi[gp->own].queue);
410 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
417 snxtitem_all(&ni, EF_PLANE);
418 while (nxtitem(&ni, (s_char *)&plane)) {
419 if (plane.pln_own != cn)
422 if (plane.pln_mission != MI_ESCORT)
425 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
427 if (dist > ((int)((float)plane.pln_range / 2.0)))
430 plp = (struct plist *)malloc(sizeof(struct plist));
431 memset(plp, 0, sizeof(struct plist));
432 plp->pcp = &plchr[(int)plane.pln_type];
434 emp_insque(&plp->queue, escorts);
439 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
440 int mission, s_char *s, int hardtarget)
442 extern int land_max_interdiction_range;
443 extern int ship_max_interdiction_range;
444 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
445 struct emp_qelem *newqp;
454 struct plchrstr *pcp;
455 int dam = 0, dam2, mission_flags, tech;
456 natid plane_owner = 0;
457 int gun, shell, md, air_dam = 0;
458 double range2, prb, range, mobcost, hitchance;
459 extern int torpedo_damage;
461 getsect(x, y, §);
463 emp_initque(&missiles);
464 emp_initque(&bombers);
465 emp_initque(&escorts);
468 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
469 glp = (struct genlist *)qp;
470 gp = (struct genitem *)glp->thing;
472 md = mapdist(x, y, gp->x, gp->y);
474 if (glp->type == EF_LAND) {
475 lp = (struct lndstr *)glp->thing;
476 lcp = (struct lchrstr *)glp->cp;
478 if (lp->lnd_effic < LAND_MINFIREEFF)
481 if (mission == MI_SINTERDICT)
484 if ((mission == MI_INTERDICT) &&
485 (md > land_max_interdiction_range))
488 if (md > (lp->lnd_frg / 2))
491 if (lnd_getmil(lp) < 1)
494 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
495 range2 = (double)roundrange(range);
500 shell = getvar(V_SHELL, (s_char *)lp, EF_LAND);
501 gun = getvar(V_GUN, (s_char *)lp, EF_LAND);
502 if (shell == 0 || gun == 0)
505 if (has_supply(lp)) {
507 putland(lp->lnd_uid, lp);
508 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
509 lp->lnd_ammo, shell), 1);
510 if (sect.sct_type == SCT_WATER) {
511 double dam3 = (double)dam2;
512 if (chance(((double)lp->lnd_acc) / 100.0))
513 dam2 = ldround((dam3 / 2.0), 1);
516 if (sect.sct_type == SCT_WATER)
517 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
519 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
521 "%s fires at %s %s at %s\n",
522 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
524 mpr(victim, "%s %s fires at you at %s\n",
525 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
527 } else if (glp->type == EF_SHIP) {
528 sp = (struct shpstr *)glp->thing;
529 mcp = (struct mchrstr *)glp->cp;
531 if (sp->shp_effic < 60)
533 if (sp->shp_frnge == 0)
535 if (((mission == MI_INTERDICT) ||
536 (mission == MI_SINTERDICT)) &&
537 (md > ship_max_interdiction_range))
539 if (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 1)
542 if ((mcp->m_flags & M_SUB) &&
543 (sect.sct_type != SCT_WATER))
546 if (mission == MI_SINTERDICT) {
547 if (!(mcp->m_flags & M_SONAR))
549 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
551 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
552 range2 *= (double)sp->shp_effic / 200.0;
555 /* can't look all the time */
559 if (mcp->m_flags & M_SUB) {
560 /* If we aren't shooting at "subs" or "ships" don't fire at all from
564 if (sp->shp_mobil < (s_char)0)
566 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
569 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
571 shell += supply_commod(sp->shp_own,
572 sp->shp_x, sp->shp_y, I_SHELL,
577 range = sp->shp_effic * techfact(sp->shp_tech,
578 ((double)sp->shp_frnge)) /
581 range2 = (double)roundrange(range);
585 if (!line_of_sight((s_char **)0, x, y, gp->x, gp->y))
587 putvar(V_SHELL, shell - 3, (s_char *)sp, EF_SHIP);
588 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
589 mobcost = (480.0 / (mobcost +
590 techfact(sp->shp_tech, mobcost)));
591 sp->shp_mobil -= mobcost;
592 putship(sp->shp_uid, sp);
593 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
596 "%s locking on %s %s in %s\n",
597 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
599 "\tEffective torpedo range is %.1f\n", range);
601 "\tWhooosh... Hitchance = %d%%\n",
602 (int)(hitchance * 100));
604 if (hitchance < 1.0 && !chance(hitchance)) {
605 wu(0, sp->shp_own, "\tMissed\n");
607 "Incoming torpedo sighted @ %s missed (whew)!\n",
611 wu(0, sp->shp_own, "\tBOOM!...\n");
612 dam2 = TORP_DAMAGE();
615 nreport(victim, N_TORP_SHIP, sp->shp_own, 1);
617 "\tTorpedo hit %s %s for %d damage\n",
618 cname(victim), s, dam2);
621 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
622 xyas(x, y, victim), dam2);
624 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
625 range2 = (double)roundrange(range);
628 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
629 gun = min(gun, sp->shp_glim);
630 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
632 shell += supply_commod(sp->shp_own,
633 sp->shp_x, sp->shp_y, I_SHELL,
635 gun = min(gun, shell);
636 gun = min(gun, (int)((float)getvar(V_MILIT,
642 dam2 = seagun(sp->shp_effic, gun);
646 prb = ((double)md) / range2;
649 dam2 = (int)((float)dam2 / 2.0);
651 if (sect.sct_type == SCT_WATER)
652 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
654 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
656 "%s fires at %s %s at %s\n",
657 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
659 mpr(victim, "%s fires at you at %s\n",
660 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
662 putvar(V_SHELL, shell - gun, (s_char *)sp, EF_SHIP);
663 putship(sp->shp_uid, sp);
665 } else if (glp->type == EF_PLANE) {
666 pcp = (struct plchrstr *)glp->cp;
667 if (pcp->pl_flags & P_M)
668 /* units have their own missile interdiction */
669 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
672 /* save planes for later */
673 plp = (struct plist *)malloc(sizeof(struct plist));
675 memset(plp, 0, sizeof(struct plist));
677 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
678 if (plp->pcp->pl_flags & P_M)
679 emp_insque(&plp->queue, &missiles);
681 emp_insque(&plp->queue, &bombers);
682 plane_owner = plp->plane.pln_own;
685 if (!QEMPTY(&missiles)) {
686 /* I arbitrarily chose 100 mindam -KHS */
688 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
689 "sector", victim, mission);
690 qp = missiles.q_forw;
691 while (qp != (&missiles)) {
699 if (QEMPTY(&bombers)) {
702 glp = (struct genlist *)qp;
711 * If there are planes performing an
712 * interdict or support mission, find
713 * some escorts for them, if possible.
714 * Up to 2 per bomber, if possible.
716 find_escorts(x, y, plane_owner, &escorts);
718 if (mission == MI_SINTERDICT)
719 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
721 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
723 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
725 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
726 plp = (struct plist *)qp;
727 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
728 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
731 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
735 air = (struct airport *)qp;
736 md = mapdist(x, y, air->x, air->y);
741 /* Split off the bombers at this base into b */
742 divide(&bombers, &b, air->x, air->y);
744 /* Split off the escorts at this base into e */
745 divide(&escorts, &e, air->x, air->y);
749 mission_flags |= P_X; /* stealth (shhh) */
750 mission_flags |= P_H; /* gets turned off if not all choppers */
752 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
753 0, mission_flags, &tech);
759 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
760 P_F | P_ESC, mission_flags, &tech);
762 BestAirPath(pp, air->x, air->y, x, y);
763 wu(0, plane_owner, "Flying %s mission from %s\n",
764 mission_name(mission), xyas(air->x, air->y, plane_owner));
765 if (air->own && (air->own != plane_owner)) {
766 wu(0, air->own, "%s is flying %s mission from %s\n",
767 cname(plane_owner), mission_name(mission),
768 xyas(air->x, air->y, air->own));
771 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
775 air_damage(&b, x, y, mission, victim, s, hardtarget);
783 if (sect.sct_type == SCT_WATER)
784 nreport(plane_owner, N_SHP_BOMB, victim, 1);
786 nreport(plane_owner, N_SCT_BOMB, victim, 1);
789 /* free up all this memory */
792 glp = (struct genlist *)qp;
800 while (qp != (&escorts)) {
808 while (qp != (&bombers)) {
819 cando(int mission, int type)
823 if (type == EF_PLANE)
827 if (type == EF_PLANE)
831 if ((type == EF_PLANE) || (type == EF_SHIP))
839 if (type == EF_PLANE)
852 nameofitem(s_char *buf, struct genitem *gp, int type)
856 return prship((struct shpstr *)gp);
859 return prplane((struct plnstr *)gp);
862 return prland((struct lndstr *)gp);
868 mission_short_name(int mission)
893 mission_name(short int mission)
897 return "an interdiction";
901 return "a offensive support";
903 return "a defensive support";
909 return "a sub interdiction";
911 return "an air defense";
913 return "a mysterious";
917 show_mission(int type, struct nstr_item *np)
919 int size, first = 1, radius;
924 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
925 size = max(size, sizeof(struct shpstr));
926 block = (s_char *)malloc(size);
928 while (nxtitem(np, block)) {
929 gp = (struct genitem *)block;
930 if (!player->owner || gp->own == 0)
934 pr("Thing x,y op-sect rad mission\n");
937 pr("%-25s", nameofitem(buf, gp, type));
938 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
939 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
940 gp->mission == MI_OSUPPORT ||
941 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
943 oprange(gp, type, &radius);
944 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
945 if (radius < gp->radius)
948 pr(" %4d", gp->radius);
949 } else if (gp->mission == MI_RESERVE) {
953 getsect(gp->x, gp->y, §);
954 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
957 if (((struct lndstr *)block)->lnd_rad_max == 0)
960 plus += ((struct lndstr *)block)->lnd_rad_max;
961 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
963 } else if (gp->mission == MI_ESCORT) {
966 ((float)((struct plnstr *)block)->pln_range / 2.0)
971 pr(" is on %s mission\n", mission_name(gp->mission));
973 pr(" has no mission.\n");
978 oprange(struct genitem *gp, int type, int *radius)
987 getship(gp->uid, &ship);
988 range = ldround(techfact(gp->tech,
989 (double)ship.shp_frnge / 2.0), 1);
992 getland(gp->uid, &land);
993 range = ldround(techfact((int)land.lnd_tech,
994 (double)land.lnd_frg / 2.0), 1);
997 getplane(gp->uid, &plane);
998 /* missiles go one way, so we can use all the range */
999 if (plchr[(int)plane.pln_type].pl_flags & P_M)
1000 range = plane.pln_range;
1002 range = ldround((double)plane.pln_range / 2.0, 1);;
1006 if ((*radius) > range)
1013 * Remove all planes who cannot go on
1014 * the mission from the plane list.
1017 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1020 struct emp_qelem *qp, *next;
1025 struct plchrstr *pcp;
1027 register int y, bad, bad1;
1030 for (qp = list->q_forw; qp != list; qp = next) {
1032 plp = (struct plist *)qp;
1036 if (pp->pln_effic < 40) {
1042 if (pp->pln_mobil < 1) {
1049 if (ontradingblock(EF_PLANE, (int *)pp)) {
1059 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1061 if ((wantflags & y) == y)
1062 if ((pcp->pl_flags & y) != y) {
1084 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1088 if ((pcp->pl_flags & P_E) ||
1089 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1101 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1103 if ((nowantflags & y) == y)
1104 if ((pcp->pl_flags & y) == y)
1113 if (pp->pln_ship >= 0) {
1114 if (!getship(pp->pln_ship, &ship)) {
1117 putplane(pp->pln_uid, pp);
1122 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1125 if (ship.shp_effic < SHIP_MINEFF) {
1128 /* Can't fly off of ships < 50%, or non-owned or non-allied ships */
1129 if ((ship.shp_effic < 50) ||
1130 ((ship.shp_own != pp->pln_own) &&
1131 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1137 if (pp->pln_land >= 0) {
1138 if (!getland(pp->pln_land, &land)) {
1141 putplane(pp->pln_uid, pp);
1146 if (!(pcp->pl_flags & P_E))
1148 if (land.lnd_effic < LAND_MINEFF)
1151 /* Can't fly off of units < 50%, or non-owned or non-allied units */
1152 if ((land.lnd_effic < 50) ||
1153 ((land.lnd_own != pp->pln_own) &&
1154 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1160 /* Can't fly off units in ships or other units */
1161 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1167 /* Now, check the sector status if not on a plane or unit */
1168 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1169 /* If we can't get the sector, we can't check it, and can't fly */
1170 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1175 /* First, check allied status */
1176 /* Can't fly from non-owned sectors or non-allied sectors */
1177 if ((sect.sct_own != pp->pln_own) &&
1178 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1183 /* non-vtol plane */
1184 if ((pcp->pl_flags & P_V) == 0) {
1185 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1192 if (pcp->pl_flags & P_A) {
1193 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1200 putplane(pp->pln_uid, pp);
1205 * Arm only the planes at x,y
1209 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1210 int mission, struct ichrstr *ip, int flags,
1211 int mission_flags, int *tech)
1213 struct emp_qelem *qp;
1214 struct emp_qelem *next;
1219 for (qp = list->q_forw; qp != list; qp = next) {
1221 plp = (struct plist *)qp;
1223 if (plp->plane.pln_x != x)
1225 if (plp->plane.pln_y != y)
1228 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1233 if (flags & (P_S | P_I)) {
1234 if (plp->pcp->pl_flags & P_S)
1235 mission_flags |= P_S;
1236 if (plp->pcp->pl_flags & P_I)
1237 mission_flags |= P_I;
1239 if (*tech > plp->plane.pln_tech)
1240 *tech = plp->plane.pln_tech;
1241 if (!(plp->pcp->pl_flags & P_H))
1242 /* no stealth on this mission */
1243 mission_flags &= ~P_H;
1244 if (!(plp->pcp->pl_flags & P_X))
1245 /* no stealth on this mission */
1246 mission_flags &= ~P_X;
1247 if (!(plp->pcp->pl_flags & P_A)) {
1248 /* no asw on this mission */
1249 mission_flags &= ~P_A;
1251 if (!(plp->pcp->pl_flags & P_MINE)) {
1252 /* no asw on this mission */
1253 mission_flags &= ~P_MINE;
1257 * Mob costs for missions are 1/2 normal
1260 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1261 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1264 return mission_flags;
1268 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1271 register struct plchrstr *pcp;
1285 if (pp->pln_ship >= 0) {
1286 getship(pp->pln_ship, &ship);
1288 ptr = (s_char *)&ship;
1289 } else if (pp->pln_land >= 0) {
1290 getland(pp->pln_land, &land);
1292 ptr = (s_char *)&land;
1294 getsect(pp->pln_x, pp->pln_y, §);
1296 ptr = (s_char *)§
1298 getvec(VT_ITEM, vec, ptr, type);
1299 if (pcp->pl_fuel > vec[I_PETROL]) {
1302 vec[I_PETROL] -= pcp->pl_fuel;
1304 if (!(flags & P_F)) {
1310 if (pp->pln_nuketype == -1) {
1312 needed = pp->pln_load;
1316 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1319 needed = (pp->pln_load * 2) / ip->i_lbs;
1322 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1325 needed = (pp->pln_load * 2) / ip->i_lbs;
1328 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1331 needed = pp->pln_load / ip->i_lbs;
1334 if (pp->pln_nuketype == -1)
1337 case 'i': /* missile interception */
1340 needed = pp->pln_load;
1346 if (rval < 0 || (item && needed <= 0)) {
1349 if ((vec[item] < needed) && (item == I_SHELL))
1350 vec[item] += supply_commod(plp->plane.pln_own,
1351 plp->plane.pln_x, plp->plane.pln_y,
1353 if (vec[item] < needed) {
1356 vec[item] -= needed;
1358 if (item == I_SHELL && (mission == 's' || mission == 'p'))
1359 plp->bombs = needed;
1363 putvec(VT_ITEM, vec, ptr, type);
1364 if (type == EF_SHIP)
1365 putship(ship.shp_uid, &ship);
1366 else if (type == EF_LAND)
1367 putland(land.lnd_uid, &land);
1374 * Return 1 if this x,y pair is in the list
1377 find_airport(struct emp_qelem *airp, coord x, coord y)
1379 struct emp_qelem *qp;
1382 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1383 a = (struct airport *)qp;
1384 if ((a->x == x) && (a->y == y))
1391 /* #*# This needs to be changed to include acc's -KHS */
1393 add_airport(struct emp_qelem *airp, coord x, coord y)
1398 a = (struct airport *)malloc(sizeof(struct airport));
1402 getsect(x, y, §);
1403 a->own = sect.sct_own;
1405 emp_insque((struct emp_qelem *)a, airp);
1409 * Take all the planes in list 1 that
1410 * are at x,y, and put them into list 2.
1413 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1415 struct emp_qelem *qp, *next;
1418 for (qp = l1->q_forw; qp != l1; qp = next) {
1420 plp = (struct plist *)qp;
1422 if (plp->plane.pln_x != x)
1424 if (plp->plane.pln_y != y)
1433 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1434 natid victim, s_char *s, int hardtarget)
1436 struct emp_qelem *qp;
1439 int newdam, dam = 0;
1443 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1444 plp = (struct plist *)qp;
1447 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1454 if (plp->pcp->pl_flags & P_A) {
1455 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1457 "\t%s detects sub movement in %s\n",
1458 prplane(pp), xyas(x, y, pp->pln_own));
1461 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1463 "\t%s tracks %s %s at %s\n",
1464 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1468 "\t%s depth-charging %s %s in %s\n",
1469 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1472 "\t%s pinbombing %s %s in %s\n",
1473 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1475 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1476 if (plp->plane.pln_nuketype != -1)
1478 else if (hardtarget != SECT_HARDTARGET)
1479 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1480 /* Always calculate damage */
1481 if (roll(100) <= hitchance) {
1482 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1484 if (mission == MI_INTERDICT) {
1486 "\t\tnuclear warhead does %d damage to %s %s\n",
1487 prplane(pp), nukedam, cname(victim), s);
1492 "\t\thit %s %s for %d damage\n",
1493 cname(victim), s, newdam);
1497 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1498 wu(0, pp->pln_own, "missed\n");
1499 if (mission == MI_SINTERDICT) {
1501 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1502 xyas(x, y, victim));
1503 } else if (*s == 's') {
1505 "SPLASH! Bombs miss your %s in %s\n",
1506 s, xyas(x, y, victim));
1508 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1509 s, xyas(x, y, victim));
1511 /* Now, even though we missed, the bombs
1513 collateral_damage(x, y, newdam, bombers);
1516 /* use up missiles */
1517 if (plp->pcp->pl_flags & P_M) {
1518 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1528 * Check to see if anyone hostile to the victim
1529 * is running an air defense mission on this
1530 * sector. If so, do air combat
1533 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1534 struct emp_qelem *esc_list)
1537 int mission_flags, tech, combat = 0, rel, dist, z;
1538 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1540 struct genlist *glp;
1542 struct genlist mi[MAXNOC];
1548 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1550 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1553 memset(mi, 0, sizeof(mi));
1554 for (z = 1; z < MAXNOC; z++)
1555 emp_initque((struct emp_qelem *)&mi[z]);
1557 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1559 for (cn = 1; cn < MAXNOC; cn++) {
1560 /* Check our relations */
1561 rel = getrel(getnatp(cn), victim);
1566 if (QEMPTY(&mi[cn].queue))
1569 /* Ok, make a list of all the interceptors. Note that this *copies* the
1570 * list from the mission creation. This list must be deleted later. */
1571 emp_initque(&interceptors);
1572 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1575 glp = (struct genlist *)qp;
1576 gp = (struct genitem *)glp->thing;
1577 plp = (struct plist *)qp;
1579 dist = mapdist(x, y, gp->x, gp->y);
1581 plp = (struct plist *)malloc(sizeof(struct plist));
1582 memset(plp, 0, sizeof(struct plist));
1583 plp->pcp = (struct plchrstr *)glp->cp;
1584 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1586 /* missiles go one way, so we can use all the range */
1587 if (!(plp->pcp->pl_flags & P_M))
1589 /* If it's out of range, free it and continue on */
1590 if (dist > plp->plane.pln_range) {
1594 emp_insque(&plp->queue, &interceptors);
1597 /* Remove those who cannot go */
1598 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1600 if (QEMPTY(&interceptors))
1603 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1604 * that the higher numbered planes go into battle (they should be the better ones
1605 * at fighting, if all went well.) */
1607 for (qp = interceptors.q_forw; qp != (&interceptors);
1610 tcount -= (count * 2);
1611 /* Just in case there are more incoming than we have */
1614 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1618 /* Free it up and continue */
1620 glp = (struct genlist *)qp;
1621 free((s_char *)glp);
1625 /* Now, make a list of all the airports these planes are coming from */
1627 for (qp = interceptors.q_forw; qp != (&interceptors);
1629 plp = (struct plist *)qp;
1630 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1631 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1634 /* Now, fly them out one airport at a time */
1635 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1636 struct airport *air;
1638 air = (struct airport *)qp;
1639 dist = mapdist(x, y, air->x, air->y);
1643 /* Split off the interceptors at this base into i */
1644 divide(&interceptors, &i, air->x, air->y);
1648 mission_flags |= P_X; /* stealth (shhh) */
1649 /* gets turned off if not all choppers */
1650 mission_flags |= P_H;
1651 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1652 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1654 /* Did we run out of interceptors? */
1657 /* Did we run out of bombers? */
1658 if (QEMPTY(bomb_list)) {
1659 /* Yes, so we have to put the rest of the interceptors back, and
1660 then continue, or we leak memory */
1665 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1666 mission_flags, &tech);
1668 /* Did we run out of interceptors? */
1671 /* Did we run out of bombers? */
1672 if (QEMPTY(bomb_list)) {
1673 /* Yes, so we have to put the rest of the interceptors back, and
1674 then continue, or we leak memory */
1679 BestAirPath(path, air->x, air->y, x, y);
1680 wu(0, cn, "Flying %s mission from %s\n",
1681 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1682 if (air->own && (air->own != cn)) {
1683 wu(0, air->own, "%s is flying %s mission from %s\n",
1684 cname(cn), mission_name(MI_AIR_DEFENSE),
1685 xyas(air->x, air->y, air->own));
1688 /* Now, fly the planes to the sector */
1689 emp_initque(&empty);
1690 ac_encounter(&i, &empty, air->x, air->y,
1691 path, mission_flags, 1, bomb_list, esc_list);
1693 /* If none made it, continue */
1697 /* Some made it, so now they get to try to fight. */
1698 /* Intercept the escorts first */
1700 if (!QEMPTY(esc_list)) {
1701 mpr(victim, "%s air defense planes intercept!\n",
1703 ac_combat_headers(victim, cn);
1704 ac_airtoair(esc_list, &i, air->own);
1707 /* Now intercept the bombers */
1708 if (!QEMPTY(bomb_list)) {
1710 mpr(victim, "%s air defense planes intercept!\n",
1712 ac_combat_headers(victim, cn);
1714 ac_airtoair(bomb_list, &i, air->own);
1723 /* We have to free all of these, if they are still there, otherwise they get
1724 lost and we leak memory all over the place. */
1725 for (cn = 1; cn < MAXNOC; cn++) {
1726 /* free up all this memory if it's still there */
1727 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1729 glp = (struct genlist *)qp;
1731 free((s_char *)glp);