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
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 = getvar(V_SHELL, (s_char *)lp, EF_LAND);
496 gun = getvar(V_GUN, (s_char *)lp, EF_LAND);
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 (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 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 = getvar(V_GUN, (s_char *)sp, EF_SHIP);
564 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
566 shell += supply_commod(sp->shp_own,
567 sp->shp_x, sp->shp_y, I_SHELL,
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 putvar(V_SHELL, shell - 3, (s_char *)sp, EF_SHIP);
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 = getvar(V_GUN, (s_char *)sp, EF_SHIP);
624 gun = min(gun, sp->shp_glim);
625 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
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, (int)((float)getvar(V_MILIT,
637 dam2 = seagun(sp->shp_effic, gun);
641 prb = ((double)md) / range2;
644 dam2 = (int)((float)dam2 / 2.0);
646 if (sect.sct_type == SCT_WATER)
647 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
649 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
651 "%s fires at %s %s at %s\n",
652 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
654 mpr(victim, "%s fires at you at %s\n",
655 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
657 putvar(V_SHELL, shell - gun, (s_char *)sp, EF_SHIP);
658 putship(sp->shp_uid, sp);
660 } else if (glp->type == EF_PLANE) {
661 pcp = (struct plchrstr *)glp->cp;
662 if (pcp->pl_flags & P_M)
663 /* units have their own missile interdiction */
664 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
667 /* save planes for later */
668 plp = (struct plist *)malloc(sizeof(struct plist));
670 memset(plp, 0, sizeof(struct plist));
672 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
673 if (plp->pcp->pl_flags & P_M)
674 emp_insque(&plp->queue, &missiles);
676 emp_insque(&plp->queue, &bombers);
677 plane_owner = plp->plane.pln_own;
680 if (!QEMPTY(&missiles)) {
681 /* I arbitrarily chose 100 mindam -KHS */
683 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
684 "sector", victim, mission);
685 qp = missiles.q_forw;
686 while (qp != (&missiles)) {
694 if (QEMPTY(&bombers)) {
697 glp = (struct genlist *)qp;
706 * If there are planes performing an
707 * interdict or support mission, find
708 * some escorts for them, if possible.
709 * Up to 2 per bomber, if possible.
711 find_escorts(x, y, plane_owner, &escorts);
713 if (mission == MI_SINTERDICT)
714 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
716 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
718 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
720 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
721 plp = (struct plist *)qp;
722 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
723 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
726 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
730 air = (struct airport *)qp;
731 md = mapdist(x, y, air->x, air->y);
736 /* Split off the bombers at this base into b */
737 divide(&bombers, &b, air->x, air->y);
739 /* Split off the escorts at this base into e */
740 divide(&escorts, &e, air->x, air->y);
744 mission_flags |= P_X; /* stealth (shhh) */
745 mission_flags |= P_H; /* gets turned off if not all choppers */
747 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
748 0, mission_flags, &tech);
754 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
755 P_F | P_ESC, mission_flags, &tech);
757 BestAirPath(pp, air->x, air->y, x, y);
758 wu(0, plane_owner, "Flying %s mission from %s\n",
759 mission_name(mission), xyas(air->x, air->y, plane_owner));
760 if (air->own && (air->own != plane_owner)) {
761 wu(0, air->own, "%s is flying %s mission from %s\n",
762 cname(plane_owner), mission_name(mission),
763 xyas(air->x, air->y, air->own));
766 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
770 air_damage(&b, x, y, mission, victim, s, hardtarget);
778 if (sect.sct_type == SCT_WATER)
779 nreport(plane_owner, N_SHP_BOMB, victim, 1);
781 nreport(plane_owner, N_SCT_BOMB, victim, 1);
784 /* free up all this memory */
787 glp = (struct genlist *)qp;
795 while (qp != (&escorts)) {
803 while (qp != (&bombers)) {
814 cando(int mission, int type)
818 if (type == EF_PLANE)
822 if (type == EF_PLANE)
826 if ((type == EF_PLANE) || (type == EF_SHIP))
834 if (type == EF_PLANE)
847 nameofitem(s_char *buf, struct genitem *gp, int type)
851 return prship((struct shpstr *)gp);
854 return prplane((struct plnstr *)gp);
857 return prland((struct lndstr *)gp);
863 mission_short_name(int mission)
888 mission_name(short int mission)
892 return "an interdiction";
896 return "a offensive support";
898 return "a defensive support";
904 return "a sub interdiction";
906 return "an air defense";
908 return "a mysterious";
912 show_mission(int type, struct nstr_item *np)
914 int size, first = 1, radius;
919 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
920 size = max(size, sizeof(struct shpstr));
921 block = (s_char *)malloc(size);
923 while (nxtitem(np, block)) {
924 gp = (struct genitem *)block;
925 if (!player->owner || gp->own == 0)
929 pr("Thing x,y op-sect rad mission\n");
932 pr("%-25s", nameofitem(buf, gp, type));
933 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
934 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
935 gp->mission == MI_OSUPPORT ||
936 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
938 oprange(gp, type, &radius);
939 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
940 if (radius < gp->radius)
943 pr(" %4d", gp->radius);
944 } else if (gp->mission == MI_RESERVE) {
948 getsect(gp->x, gp->y, §);
949 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
952 if (((struct lndstr *)block)->lnd_rad_max == 0)
955 plus += ((struct lndstr *)block)->lnd_rad_max;
956 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
958 } else if (gp->mission == MI_ESCORT) {
961 ((float)((struct plnstr *)block)->pln_range / 2.0)
966 pr(" is on %s mission\n", mission_name(gp->mission));
968 pr(" has no mission.\n");
973 oprange(struct genitem *gp, int type, int *radius)
982 getship(gp->uid, &ship);
983 range = ldround(techfact(gp->tech,
984 (double)ship.shp_frnge / 2.0), 1);
987 getland(gp->uid, &land);
988 range = ldround(techfact((int)land.lnd_tech,
989 (double)land.lnd_frg / 2.0), 1);
992 getplane(gp->uid, &plane);
993 /* missiles go one way, so we can use all the range */
994 if (plchr[(int)plane.pln_type].pl_flags & P_M)
995 range = plane.pln_range;
997 range = ldround((double)plane.pln_range / 2.0, 1);;
1001 if ((*radius) > range)
1008 * Remove all planes who cannot go on
1009 * the mission from the plane list.
1012 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1015 struct emp_qelem *qp, *next;
1020 struct plchrstr *pcp;
1022 register int y, bad, bad1;
1025 for (qp = list->q_forw; qp != list; qp = next) {
1027 plp = (struct plist *)qp;
1031 if (pp->pln_effic < 40) {
1037 if (pp->pln_mobil < 1) {
1044 if (ontradingblock(EF_PLANE, (int *)pp)) {
1054 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1056 if ((wantflags & y) == y)
1057 if ((pcp->pl_flags & y) != y) {
1079 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1083 if ((pcp->pl_flags & P_E) ||
1084 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1096 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1098 if ((nowantflags & y) == y)
1099 if ((pcp->pl_flags & y) == y)
1108 if (pp->pln_ship >= 0) {
1109 if (!getship(pp->pln_ship, &ship)) {
1112 putplane(pp->pln_uid, pp);
1117 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1120 if (ship.shp_effic < SHIP_MINEFF) {
1123 /* Can't fly off of ships < 50%, or non-owned or non-allied ships */
1124 if ((ship.shp_effic < 50) ||
1125 ((ship.shp_own != pp->pln_own) &&
1126 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1132 if (pp->pln_land >= 0) {
1133 if (!getland(pp->pln_land, &land)) {
1136 putplane(pp->pln_uid, pp);
1141 if (!(pcp->pl_flags & P_E))
1143 if (land.lnd_effic < LAND_MINEFF)
1146 /* Can't fly off of units < 50%, or non-owned or non-allied units */
1147 if ((land.lnd_effic < 50) ||
1148 ((land.lnd_own != pp->pln_own) &&
1149 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1155 /* Can't fly off units in ships or other units */
1156 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1162 /* Now, check the sector status if not on a plane or unit */
1163 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1164 /* If we can't get the sector, we can't check it, and can't fly */
1165 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1170 /* First, check allied status */
1171 /* Can't fly from non-owned sectors or non-allied sectors */
1172 if ((sect.sct_own != pp->pln_own) &&
1173 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1178 /* non-vtol plane */
1179 if ((pcp->pl_flags & P_V) == 0) {
1180 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1187 if (pcp->pl_flags & P_A) {
1188 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1195 putplane(pp->pln_uid, pp);
1200 * Arm only the planes at x,y
1204 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1205 int mission, struct ichrstr *ip, int flags,
1206 int mission_flags, int *tech)
1208 struct emp_qelem *qp;
1209 struct emp_qelem *next;
1214 for (qp = list->q_forw; qp != list; qp = next) {
1216 plp = (struct plist *)qp;
1218 if (plp->plane.pln_x != x)
1220 if (plp->plane.pln_y != y)
1223 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1228 if (flags & (P_S | P_I)) {
1229 if (plp->pcp->pl_flags & P_S)
1230 mission_flags |= P_S;
1231 if (plp->pcp->pl_flags & P_I)
1232 mission_flags |= P_I;
1234 if (*tech > plp->plane.pln_tech)
1235 *tech = plp->plane.pln_tech;
1236 if (!(plp->pcp->pl_flags & P_H))
1237 /* no stealth on this mission */
1238 mission_flags &= ~P_H;
1239 if (!(plp->pcp->pl_flags & P_X))
1240 /* no stealth on this mission */
1241 mission_flags &= ~P_X;
1242 if (!(plp->pcp->pl_flags & P_A)) {
1243 /* no asw on this mission */
1244 mission_flags &= ~P_A;
1246 if (!(plp->pcp->pl_flags & P_MINE)) {
1247 /* no asw on this mission */
1248 mission_flags &= ~P_MINE;
1252 * Mob costs for missions are 1/2 normal
1255 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1256 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1259 return mission_flags;
1263 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1266 register struct plchrstr *pcp;
1280 if (pp->pln_ship >= 0) {
1281 getship(pp->pln_ship, &ship);
1283 ptr = (s_char *)&ship;
1284 } else if (pp->pln_land >= 0) {
1285 getland(pp->pln_land, &land);
1287 ptr = (s_char *)&land;
1289 getsect(pp->pln_x, pp->pln_y, §);
1291 ptr = (s_char *)§
1293 getvec(VT_ITEM, vec, ptr, type);
1294 if (pcp->pl_fuel > vec[I_PETROL]) {
1297 vec[I_PETROL] -= pcp->pl_fuel;
1299 if (!(flags & P_F)) {
1305 if (pp->pln_nuketype == -1) {
1307 needed = pp->pln_load;
1311 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1314 needed = (pp->pln_load * 2) / ip->i_lbs;
1317 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1320 needed = (pp->pln_load * 2) / ip->i_lbs;
1323 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1326 needed = pp->pln_load / ip->i_lbs;
1329 if (pp->pln_nuketype == -1)
1332 case 'i': /* missile interception */
1335 needed = pp->pln_load;
1341 if (rval < 0 || (item && needed <= 0)) {
1344 if ((vec[item] < needed) && (item == I_SHELL))
1345 vec[item] += supply_commod(plp->plane.pln_own,
1346 plp->plane.pln_x, plp->plane.pln_y,
1348 if (vec[item] < needed) {
1351 vec[item] -= needed;
1353 if (item == I_SHELL && (mission == 's' || mission == 'p'))
1354 plp->bombs = needed;
1358 putvec(VT_ITEM, vec, ptr, type);
1359 if (type == EF_SHIP)
1360 putship(ship.shp_uid, &ship);
1361 else if (type == EF_LAND)
1362 putland(land.lnd_uid, &land);
1369 * Return 1 if this x,y pair is in the list
1372 find_airport(struct emp_qelem *airp, coord x, coord y)
1374 struct emp_qelem *qp;
1377 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1378 a = (struct airport *)qp;
1379 if ((a->x == x) && (a->y == y))
1386 /* #*# This needs to be changed to include acc's -KHS */
1388 add_airport(struct emp_qelem *airp, coord x, coord y)
1393 a = (struct airport *)malloc(sizeof(struct airport));
1397 getsect(x, y, §);
1398 a->own = sect.sct_own;
1400 emp_insque((struct emp_qelem *)a, airp);
1404 * Take all the planes in list 1 that
1405 * are at x,y, and put them into list 2.
1408 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1410 struct emp_qelem *qp, *next;
1413 for (qp = l1->q_forw; qp != l1; qp = next) {
1415 plp = (struct plist *)qp;
1417 if (plp->plane.pln_x != x)
1419 if (plp->plane.pln_y != y)
1428 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1429 natid victim, s_char *s, int hardtarget)
1431 struct emp_qelem *qp;
1434 int newdam, dam = 0;
1438 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1439 plp = (struct plist *)qp;
1442 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1449 if (plp->pcp->pl_flags & P_A) {
1450 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1452 "\t%s detects sub movement in %s\n",
1453 prplane(pp), xyas(x, y, pp->pln_own));
1456 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1458 "\t%s tracks %s %s at %s\n",
1459 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1463 "\t%s depth-charging %s %s in %s\n",
1464 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1467 "\t%s pinbombing %s %s in %s\n",
1468 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1470 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1471 if (plp->plane.pln_nuketype != -1)
1473 else if (hardtarget != SECT_HARDTARGET)
1474 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1475 /* Always calculate damage */
1476 if (roll(100) <= hitchance) {
1477 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1479 if (mission == MI_INTERDICT) {
1481 "\t\tnuclear warhead does %d damage to %s %s\n",
1482 prplane(pp), nukedam, cname(victim), s);
1487 "\t\thit %s %s for %d damage\n",
1488 cname(victim), s, newdam);
1492 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1493 wu(0, pp->pln_own, "missed\n");
1494 if (mission == MI_SINTERDICT) {
1496 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1497 xyas(x, y, victim));
1498 } else if (*s == 's') {
1500 "SPLASH! Bombs miss your %s in %s\n",
1501 s, xyas(x, y, victim));
1503 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1504 s, xyas(x, y, victim));
1506 /* Now, even though we missed, the bombs
1508 collateral_damage(x, y, newdam, bombers);
1511 /* use up missiles */
1512 if (plp->pcp->pl_flags & P_M) {
1513 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1523 * Check to see if anyone hostile to the victim
1524 * is running an air defense mission on this
1525 * sector. If so, do air combat
1528 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1529 struct emp_qelem *esc_list)
1532 int mission_flags, tech, combat = 0, rel, dist, z;
1533 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1535 struct genlist *glp;
1537 struct genlist mi[MAXNOC];
1543 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1545 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1548 memset(mi, 0, sizeof(mi));
1549 for (z = 1; z < MAXNOC; z++)
1550 emp_initque((struct emp_qelem *)&mi[z]);
1552 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1554 for (cn = 1; cn < MAXNOC; cn++) {
1555 /* Check our relations */
1556 rel = getrel(getnatp(cn), victim);
1561 if (QEMPTY(&mi[cn].queue))
1564 /* Ok, make a list of all the interceptors. Note that this *copies* the
1565 * list from the mission creation. This list must be deleted later. */
1566 emp_initque(&interceptors);
1567 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1570 glp = (struct genlist *)qp;
1571 gp = (struct genitem *)glp->thing;
1572 plp = (struct plist *)qp;
1574 dist = mapdist(x, y, gp->x, gp->y);
1576 plp = (struct plist *)malloc(sizeof(struct plist));
1577 memset(plp, 0, sizeof(struct plist));
1578 plp->pcp = (struct plchrstr *)glp->cp;
1579 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1581 /* missiles go one way, so we can use all the range */
1582 if (!(plp->pcp->pl_flags & P_M))
1584 /* If it's out of range, free it and continue on */
1585 if (dist > plp->plane.pln_range) {
1589 emp_insque(&plp->queue, &interceptors);
1592 /* Remove those who cannot go */
1593 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1595 if (QEMPTY(&interceptors))
1598 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1599 * that the higher numbered planes go into battle (they should be the better ones
1600 * at fighting, if all went well.) */
1602 for (qp = interceptors.q_forw; qp != (&interceptors);
1605 tcount -= (count * 2);
1606 /* Just in case there are more incoming than we have */
1609 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1613 /* Free it up and continue */
1615 glp = (struct genlist *)qp;
1616 free((s_char *)glp);
1620 /* Now, make a list of all the airports these planes are coming from */
1622 for (qp = interceptors.q_forw; qp != (&interceptors);
1624 plp = (struct plist *)qp;
1625 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1626 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1629 /* Now, fly them out one airport at a time */
1630 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1631 struct airport *air;
1633 air = (struct airport *)qp;
1634 dist = mapdist(x, y, air->x, air->y);
1638 /* Split off the interceptors at this base into i */
1639 divide(&interceptors, &i, air->x, air->y);
1643 mission_flags |= P_X; /* stealth (shhh) */
1644 /* gets turned off if not all choppers */
1645 mission_flags |= P_H;
1646 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1647 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1649 /* Did we run out of interceptors? */
1652 /* Did we run out of bombers? */
1653 if (QEMPTY(bomb_list)) {
1654 /* Yes, so we have to put the rest of the interceptors back, and
1655 then continue, or we leak memory */
1660 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1661 mission_flags, &tech);
1663 /* Did we run out of interceptors? */
1666 /* Did we run out of bombers? */
1667 if (QEMPTY(bomb_list)) {
1668 /* Yes, so we have to put the rest of the interceptors back, and
1669 then continue, or we leak memory */
1674 BestAirPath(path, air->x, air->y, x, y);
1675 wu(0, cn, "Flying %s mission from %s\n",
1676 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1677 if (air->own && (air->own != cn)) {
1678 wu(0, air->own, "%s is flying %s mission from %s\n",
1679 cname(cn), mission_name(MI_AIR_DEFENSE),
1680 xyas(air->x, air->y, air->own));
1683 /* Now, fly the planes to the sector */
1684 emp_initque(&empty);
1685 ac_encounter(&i, &empty, air->x, air->y,
1686 path, mission_flags, 1, bomb_list, esc_list);
1688 /* If none made it, continue */
1692 /* Some made it, so now they get to try to fight. */
1693 /* Intercept the escorts first */
1695 if (!QEMPTY(esc_list)) {
1696 mpr(victim, "%s air defense planes intercept!\n",
1698 ac_combat_headers(victim, cn);
1699 ac_airtoair(esc_list, &i, air->own);
1702 /* Now intercept the bombers */
1703 if (!QEMPTY(bomb_list)) {
1705 mpr(victim, "%s air defense planes intercept!\n",
1707 ac_combat_headers(victim, cn);
1709 ac_airtoair(bomb_list, &i, air->own);
1718 /* We have to free all of these, if they are still there, otherwise they get
1719 lost and we leak memory all over the place. */
1720 for (cn = 1; cn < MAXNOC; cn++) {
1721 /* free up all this memory if it's still there */
1722 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1724 glp = (struct genlist *)qp;
1726 free((s_char *)glp);