2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * mission.c: Mission subroutines for planes/ships/units
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
55 #include "prototypes.h"
59 struct emp_qelem queue; /* list of units */
60 int type; /* unit type: EF_SHIP, EF_PLANE, EF_LAND */
61 void *cp; /* pointer to desc of thing */
62 void *thing; /* thing's struct */
66 struct emp_qelem queue;
77 static void add_airport(struct emp_qelem *, coord, coord);
78 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
80 static void build_mission_list(struct genlist *, coord, coord, int, natid);
81 static void build_mission_list_type(struct genlist *, coord, coord, int,
83 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
84 static int dosupport(struct genlist *, coord, coord, natid, natid);
85 static int find_airport(struct emp_qelem *, coord, coord);
86 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
87 int, struct ichrstr *, int, int, int *);
88 static void mission_pln_sel(struct emp_qelem *, int, int, int);
89 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
93 * Interdict commodities & transported planes
96 ground_interdict(coord x, coord y, natid victim, char *s)
99 int dam = 0, newdam, rel;
100 struct genlist mi[MAXNOC];
103 memset(mi, 0, sizeof(mi));
104 for (z = 1; z < MAXNOC; z++)
105 emp_initque((struct emp_qelem *)&mi[z]);
107 build_mission_list(mi, x, y, MI_INTERDICT, victim);
109 for (cn = 1; cn < MAXNOC; cn++) {
110 rel = getrel(getnatp(cn), victim);
114 if (QEMPTY(&mi[cn].queue))
117 newdam = perform_mission(x, y, victim, &mi[cn].queue,
118 MI_INTERDICT, s, SECT_HARDTARGET);
121 mpr(victim, "%s interdiction mission does %d damage!\n",
125 collateral_damage(x, y, dam, 0);
131 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
139 getsect(x, y, §);
141 coll = ldround((double)dam * collateral_dam, 1);
144 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
145 xyas(x, y, sect.sct_own), coll);
146 sectdamage(§, coll, list);
154 only_subs(struct emp_qelem *list)
156 struct emp_qelem *qp;
160 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
161 glp = (struct genlist *)qp;
163 if (glp->type != EF_SHIP)
166 if (!(mcp->m_flags & M_SUB))
170 /* They were all subs! */
176 * Interdict ships & land units
180 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
185 struct genlist mi[MAXNOC];
189 memset(mi, 0, sizeof(mi));
190 for (z = 1; z < MAXNOC; z++)
191 emp_initque((struct emp_qelem *)&mi[z]);
193 build_mission_list(mi, x, y, mission, victim);
195 for (cn = 1; cn < MAXNOC; cn++) {
198 if (mission == MI_SINTERDICT) {
199 if (getrel(getnatp(cn), victim) >= FRIENDLY)
201 } else if (getrel(getnatp(cn), victim) > HOSTILE)
204 if (QEMPTY(&mi[cn].queue))
207 osubs = only_subs(&mi[cn].queue);
208 newdam = perform_mission(x, y, victim, &mi[cn].queue,
209 mission, s, hardtarget);
212 /* If only subs responded, then we don't know who's
214 mpr(victim, "%s interdiction mission does %d damage!\n",
215 osubs ? "Enemy" : cname(cn), newdam);
219 collateral_damage(x, y, dam, 0);
225 * Perform a mission against victim, on behalf of actee
228 off_support(coord x, coord y, natid victim, natid actee)
231 struct genlist mi[MAXNOC];
234 memset(mi, 0, sizeof(mi));
235 for (z = 1; z < MAXNOC; z++)
236 emp_initque((struct emp_qelem *)&mi[z]);
238 build_mission_list(mi, x, y, MI_SUPPORT, victim);
239 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
241 dam = dosupport(mi, x, y, victim, actee);
246 * Perform a mission against victim, on behalf of actee
249 def_support(coord x, coord y, natid victim, natid actee)
252 struct genlist mi[MAXNOC];
255 memset(mi, 0, sizeof(mi));
256 for (z = 1; z < MAXNOC; z++)
257 emp_initque((struct emp_qelem *)&mi[z]);
259 build_mission_list(mi, x, y, MI_SUPPORT, victim);
260 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
262 dam = dosupport(mi, x, y, victim, actee);
267 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
273 for (cn = 1; cn < MAXNOC; cn++) {
274 rel = getrel(getnatp(cn), actee);
275 if ((cn != actee) && (rel != ALLIED))
277 rel = getrel(getnatp(cn), victim);
278 if ((cn != actee) && (rel != AT_WAR))
281 if (QEMPTY(&mi[cn].queue))
284 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
285 "", SECT_HARDTARGET);
291 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
294 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
295 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
296 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
300 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
301 int type, natid victim)
312 snxtitem_all(&ni, type);
313 while (nxtitem(&ni, &item)) {
314 gp = (struct genitem *)&item;
322 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
325 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
326 (gp->mission != MI_INTERDICT))
329 relat = getrel(getnatp(gp->own), victim);
330 if (mission == MI_SINTERDICT) {
331 if (relat >= FRIENDLY)
333 else if (type != EF_PLANE && relat > HOSTILE)
335 } else if (relat > HOSTILE)
338 dist = mapdist(x, y, gp->opx, gp->opy);
341 if (mission != MI_RESERVE) /* XXX */
342 oprange(gp, type, &radius);
347 /* Ok, it is within the operations range. */
348 /* Now check from where the object actually is */
349 dist = mapdist(x, y, gp->x, gp->y);
351 oprange(gp, type, &radius);
354 /* Ok, the object can get to where the x,y is */
357 if (mission != MI_AIR_DEFENSE) {
358 getsect(x, y, §);
359 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
362 * If the player->owner of the unit isn't at war
363 * with the victim, and doesn't own the
364 * sect being acted upon, and isn't the
365 * old player->owner of that sect, bounce them.
367 if (sect.sct_type != SCT_WATER &&
368 sect.sct_own != gp->own &&
369 sect.sct_oldown != gp->own)
375 glp = malloc(sizeof(struct genlist));
376 memset(glp, 0, sizeof(struct genlist));
380 glp->cp = &lchr[(int)gp->type];
383 glp->cp = &mchr[(int)gp->type];
386 glp->cp = &plchr[(int)gp->type];
389 glp->thing = malloc(sizeof(item));
390 memcpy(glp->thing, &item, sizeof(item));
391 emp_insque(&glp->queue, &mi[gp->own].queue);
396 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
403 snxtitem_all(&ni, EF_PLANE);
404 while (nxtitem(&ni, &plane)) {
405 if (plane.pln_own != cn)
408 if (plane.pln_mission != MI_ESCORT)
411 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
412 if (dist > plane.pln_range / 2)
415 plp = malloc(sizeof(struct plist));
416 memset(plp, 0, sizeof(struct plist));
417 plp->pcp = &plchr[(int)plane.pln_type];
419 emp_insque(&plp->queue, escorts);
424 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
425 int mission, char *s, int hardtarget)
427 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
428 struct emp_qelem *newqp;
436 struct plchrstr *pcp;
437 int dam = 0, dam2, mission_flags, tech;
438 natid plane_owner = 0;
439 int gun, shell, md, air_dam = 0;
440 double range2, prb, range, mobcost, hitchance;
442 getsect(x, y, §);
444 emp_initque(&missiles);
445 emp_initque(&bombers);
446 emp_initque(&escorts);
449 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
450 glp = (struct genlist *)qp;
453 md = mapdist(x, y, gp->x, gp->y);
455 if (glp->type == EF_LAND) {
458 if (lp->lnd_effic < LAND_MINFIREEFF)
461 if (mission == MI_SINTERDICT)
464 if ((mission == MI_INTERDICT) &&
465 (md > land_max_interdiction_range))
468 if (md > (lp->lnd_frg / 2))
471 if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
474 if (lnd_getmil(lp) < 1)
477 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
478 range2 = (double)roundrange(range);
483 shell = lp->lnd_item[I_SHELL];
484 gun = lp->lnd_item[I_GUN];
485 if (shell == 0 || gun == 0)
488 if (has_supply(lp)) {
490 putland(lp->lnd_uid, lp);
491 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
492 lp->lnd_ammo, shell), 1);
493 if (sect.sct_type == SCT_WATER) {
494 double dam3 = (double)dam2;
495 if (chance(((double)lp->lnd_acc) / 100.0))
496 dam2 = ldround((dam3 / 2.0), 1);
499 if (sect.sct_type == SCT_WATER)
500 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
502 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
504 "%s fires at %s %s at %s\n",
505 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
507 mpr(victim, "%s %s fires at you at %s\n",
508 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
510 } else if (glp->type == EF_SHIP) {
514 if (sp->shp_effic < 60)
516 if (sp->shp_frnge == 0)
518 if (((mission == MI_INTERDICT) ||
519 (mission == MI_SINTERDICT)) &&
520 (md > ship_max_interdiction_range))
522 if (sp->shp_item[I_MILIT] < 1)
525 if ((mcp->m_flags & M_SUB) &&
526 (sect.sct_type != SCT_WATER))
529 if (mission == MI_SINTERDICT) {
530 if (!(mcp->m_flags & M_SONAR))
532 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
534 range2 = techfact(sp->shp_tech, mcp->m_vrnge);
535 range2 *= (double)sp->shp_effic / 200.0;
538 /* can't look all the time */
542 if (mcp->m_flags & M_SUB) {
543 /* If we aren't shooting at "subs" or "ships" don't fire at all from
547 if (sp->shp_mobil < 0)
549 gun = sp->shp_item[I_GUN];
552 shell = sp->shp_item[I_SHELL];
553 if (shell < SHP_TORP_SHELLS)
554 shell += supply_commod(sp->shp_own,
555 sp->shp_x, sp->shp_y, I_SHELL,
556 SHP_TORP_SHELLS - shell);
557 if (shell < SHP_TORP_SHELLS)
560 range = sp->shp_effic
561 * techfact(sp->shp_tech, sp->shp_frnge) / 100.0;
563 range2 = (double)roundrange(range);
567 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
569 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
570 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
571 mobcost = (480.0 / (mobcost +
572 techfact(sp->shp_tech, mobcost)));
573 sp->shp_mobil -= mobcost;
574 putship(sp->shp_uid, sp);
575 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
578 "%s locking on %s %s in %s\n",
579 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
581 "\tEffective torpedo range is %.1f\n", range);
583 "\tWhooosh... Hitchance = %d%%\n",
584 (int)(hitchance * 100));
586 if (hitchance < 1.0 && !chance(hitchance)) {
587 wu(0, sp->shp_own, "\tMissed\n");
589 "Incoming torpedo sighted @ %s missed (whew)!\n",
593 wu(0, sp->shp_own, "\tBOOM!...\n");
594 dam2 = TORP_DAMAGE();
597 nreport(victim, N_TORP_SHIP, 0, 1);
599 "\tTorpedo hit %s %s for %d damage\n",
600 cname(victim), s, dam2);
603 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
604 xyas(x, y, victim), dam2);
606 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
607 range2 = (double)roundrange(range);
610 gun = sp->shp_item[I_GUN];
611 gun = MIN(gun, sp->shp_glim);
612 shell = sp->shp_item[I_SHELL];
614 shell += supply_commod(sp->shp_own,
615 sp->shp_x, sp->shp_y, I_SHELL,
617 gun = MIN(gun, shell);
618 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
622 dam2 = seagun(sp->shp_effic, gun);
626 prb = ((double)md) / range2;
631 if (sect.sct_type == SCT_WATER)
632 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
634 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
636 "%s fires at %s %s at %s\n",
637 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
639 mpr(victim, "%s %s fires at you at %s\n",
640 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
642 sp->shp_item[I_SHELL] = shell - gun;
643 putship(sp->shp_uid, sp);
645 } else if (glp->type == EF_PLANE) {
647 if (pcp->pl_flags & P_M)
648 /* units have their own missile interdiction */
649 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
652 /* save planes for later */
653 plp = malloc(sizeof(struct plist));
655 memset(plp, 0, sizeof(struct plist));
657 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
658 if (plp->pcp->pl_flags & P_M)
659 emp_insque(&plp->queue, &missiles);
661 emp_insque(&plp->queue, &bombers);
662 plane_owner = plp->plane.pln_own;
665 if (!QEMPTY(&missiles)) {
666 /* I arbitrarily chose 100 mindam -KHS */
668 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
669 "sector", victim, mission);
670 qp = missiles.q_forw;
671 while (qp != (&missiles)) {
679 if (QEMPTY(&bombers)) {
682 glp = (struct genlist *)qp;
691 * If there are planes performing an
692 * interdict or support mission, find
693 * some escorts for them, if possible.
694 * Up to 2 per bomber, if possible.
696 find_escorts(x, y, plane_owner, &escorts);
698 if (mission == MI_SINTERDICT)
699 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
701 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
703 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
705 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
706 plp = (struct plist *)qp;
707 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
708 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
711 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
716 air = (struct airport *)qp;
717 md = mapdist(x, y, air->x, air->y);
722 /* Split off the bombers at this base into b */
723 divide(&bombers, &b, air->x, air->y);
725 /* Split off the escorts at this base into e */
726 divide(&escorts, &e, air->x, air->y);
730 mission_flags |= P_X; /* stealth (shhh) */
731 mission_flags |= P_H; /* gets turned off if not all choppers */
733 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
734 0, mission_flags, &tech);
739 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
740 P_F | P_ESC, mission_flags, &tech);
742 pp = BestAirPath(buf, air->x, air->y, x, y);
743 if (CANT_HAPPEN(!pp))
745 wu(0, plane_owner, "Flying %s mission from %s\n",
746 mission_name(mission), xyas(air->x, air->y, plane_owner));
747 if (air->own && (air->own != plane_owner)) {
748 wu(0, air->own, "%s is flying %s mission from %s\n",
749 cname(plane_owner), mission_name(mission),
750 xyas(air->x, air->y, air->own));
753 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
757 air_damage(&b, x, y, mission, victim, s, hardtarget);
765 if (sect.sct_type == SCT_WATER)
766 nreport(plane_owner, N_SHP_BOMB, victim, 1);
768 nreport(plane_owner, N_SCT_BOMB, victim, 1);
771 /* free up all this memory */
774 glp = (struct genlist *)qp;
782 while (qp != (&escorts)) {
790 while (qp != (&bombers)) {
801 cando(int mission, int type)
805 if (type == EF_PLANE)
809 if (type == EF_PLANE)
813 if ((type == EF_PLANE) || (type == EF_SHIP))
821 if (type == EF_PLANE)
834 nameofitem(struct genitem *gp, int type)
838 return prship((struct shpstr *)gp);
840 return prplane((struct plnstr *)gp);
842 return prland((struct lndstr *)gp);
848 mission_name(short mission)
852 return "an interdiction";
856 return "a offensive support";
858 return "a defensive support";
864 return "a sub interdiction";
866 return "an air defense";
868 return "a mysterious";
872 show_mission(int type, struct nstr_item *np)
874 int first = 1, radius;
878 while (nxtitem(np, &item)) {
879 gp = (struct genitem *)&item;
880 if (!player->owner || gp->own == 0)
884 pr("Thing x,y op-sect rad mission\n");
887 pr("%-25s", nameofitem(gp, type));
888 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
889 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
890 gp->mission == MI_OSUPPORT ||
891 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
893 oprange(gp, type, &radius);
894 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
895 if (radius < gp->radius)
898 pr(" %4d", gp->radius);
899 } else if (gp->mission == MI_RESERVE) {
903 getsect(gp->x, gp->y, §);
904 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
907 if (item.land.lnd_rad_max == 0)
910 plus += item.land.lnd_rad_max;
911 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
913 } else if (gp->mission == MI_ESCORT) {
915 pr(" %4d", (int)(item.plane.pln_range / 2.0));
919 pr(" is on %s mission\n", mission_name(gp->mission));
921 pr(" has no mission.\n");
926 oprange(struct genitem *gp, int type, int *radius)
935 getship(gp->uid, &ship);
936 range = ldround(techfact(gp->tech,
937 (double)ship.shp_frnge / 2.0), 1);
940 getland(gp->uid, &land);
941 range = ldround(techfact((int)land.lnd_tech,
942 (double)land.lnd_frg / 2.0), 1);
945 getplane(gp->uid, &plane);
946 /* missiles go one way, so we can use all the range */
947 if (plchr[(int)plane.pln_type].pl_flags & P_M)
948 range = plane.pln_range;
950 range = ldround((double)plane.pln_range / 2.0, 1);;
953 CANT_HAPPEN("bad TYPE");
964 * Remove all planes who cannot go on
965 * the mission from the plane list.
968 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
971 struct emp_qelem *qp, *next;
976 struct plchrstr *pcp;
981 for (qp = list->q_forw; qp != list; qp = next) {
983 plp = (struct plist *)qp;
987 if (pp->pln_effic < 40) {
993 if (pp->pln_mobil < 1) {
1000 if (ontradingblock(EF_PLANE, pp)) {
1010 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1012 if ((wantflags & y) == y)
1013 if ((pcp->pl_flags & y) != y) {
1035 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1039 if ((pcp->pl_flags & P_E) ||
1040 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1052 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1054 if ((nowantflags & y) == y)
1055 if ((pcp->pl_flags & y) == y)
1064 if (pp->pln_ship >= 0) {
1065 if (!getship(pp->pln_ship, &ship)) {
1068 putplane(pp->pln_uid, pp);
1073 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1076 if (ship.shp_effic < SHIP_MINEFF) {
1079 /* Can't fly off of inefficient or non-owned, non-allied ships */
1080 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1081 ((ship.shp_own != pp->pln_own) &&
1082 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1088 if (pp->pln_land >= 0) {
1089 if (!getland(pp->pln_land, &land)) {
1092 putplane(pp->pln_uid, pp);
1097 if (!(pcp->pl_flags & P_E))
1099 if (land.lnd_effic < LAND_MINEFF)
1102 /* Can't fly off of inefficient or non-owned, non-allied units */
1103 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1104 ((land.lnd_own != pp->pln_own) &&
1105 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1111 /* Can't fly off units in ships or other units */
1112 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1118 /* Now, check the sector status if not on a plane or unit */
1119 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1120 /* If we can't get the sector, we can't check it, and can't fly */
1121 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1126 /* First, check allied status */
1127 /* Can't fly from non-owned sectors or non-allied sectors */
1128 if ((sect.sct_own != pp->pln_own) &&
1129 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1134 /* non-vtol plane */
1135 if ((pcp->pl_flags & P_V) == 0) {
1136 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1143 if (pcp->pl_flags & P_A) {
1144 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1151 putplane(pp->pln_uid, pp);
1156 * Arm only the planes at x,y
1160 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1161 int mission, struct ichrstr *ip, int flags,
1162 int mission_flags, int *tech)
1164 struct emp_qelem *qp;
1165 struct emp_qelem *next;
1170 for (qp = list->q_forw; qp != list; qp = next) {
1172 plp = (struct plist *)qp;
1174 if (plp->plane.pln_x != x)
1176 if (plp->plane.pln_y != y)
1179 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1184 if (flags & (P_S | P_I)) {
1185 if (plp->pcp->pl_flags & P_S)
1186 mission_flags |= P_S;
1187 if (plp->pcp->pl_flags & P_I)
1188 mission_flags |= P_I;
1190 if (*tech > plp->plane.pln_tech)
1191 *tech = plp->plane.pln_tech;
1192 if (!(plp->pcp->pl_flags & P_H))
1193 /* no stealth on this mission */
1194 mission_flags &= ~P_H;
1195 if (!(plp->pcp->pl_flags & P_X))
1196 /* no stealth on this mission */
1197 mission_flags &= ~P_X;
1198 if (!(plp->pcp->pl_flags & P_A)) {
1199 /* no asw on this mission */
1200 mission_flags &= ~P_A;
1202 if (!(plp->pcp->pl_flags & P_MINE)) {
1203 /* no asw on this mission */
1204 mission_flags &= ~P_MINE;
1208 * Mob costs for missions are 1/2 normal
1211 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1212 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1215 return mission_flags;
1219 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1222 struct plchrstr *pcp;
1234 if (pp->pln_ship >= 0) {
1235 getship(pp->pln_ship, &ship);
1236 item = ship.shp_item;
1237 } else if (pp->pln_land >= 0) {
1238 getland(pp->pln_land, &land);
1239 item = land.lnd_item;
1241 getsect(pp->pln_x, pp->pln_y, §);
1242 item = sect.sct_item;
1244 if (pcp->pl_fuel > item[I_PETROL]) {
1247 item[I_PETROL] -= pcp->pl_fuel;
1249 if (!(flags & P_F)) {
1255 if (pp->pln_nuketype == -1) {
1257 needed = pp->pln_load;
1261 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1264 needed = (pp->pln_load * 2) / ip->i_lbs;
1267 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1270 needed = (pp->pln_load * 2) / ip->i_lbs;
1273 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1276 needed = pp->pln_load / ip->i_lbs;
1279 if (pp->pln_nuketype == -1)
1282 case 'i': /* missile interception */
1285 needed = pp->pln_load;
1291 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1294 if (itype != I_NONE) {
1295 if (itype == I_SHELL && item[itype] < needed)
1296 item[itype] += supply_commod(plp->plane.pln_own,
1300 if (item[itype] < needed)
1302 item[itype] -= needed;
1304 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1305 plp->bombs = needed;
1309 if (pp->pln_ship >= 0)
1310 putship(ship.shp_uid, &ship);
1311 else if (pp->pln_land >= 0)
1312 putland(land.lnd_uid, &land);
1319 * Return 1 if this x,y pair is in the list
1322 find_airport(struct emp_qelem *airp, coord x, coord y)
1324 struct emp_qelem *qp;
1327 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1328 a = (struct airport *)qp;
1329 if ((a->x == x) && (a->y == y))
1336 /* #*# This needs to be changed to include acc's -KHS */
1338 add_airport(struct emp_qelem *airp, coord x, coord y)
1343 a = malloc(sizeof(struct airport));
1347 getsect(x, y, §);
1348 a->own = sect.sct_own;
1350 emp_insque((struct emp_qelem *)a, airp);
1354 * Take all the planes in list 1 that
1355 * are at x,y, and put them into list 2.
1358 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1360 struct emp_qelem *qp, *next;
1363 for (qp = l1->q_forw; qp != l1; qp = next) {
1365 plp = (struct plist *)qp;
1367 if (plp->plane.pln_x != x)
1369 if (plp->plane.pln_y != y)
1378 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1379 natid victim, char *s, int hardtarget)
1381 struct emp_qelem *qp;
1384 int newdam, dam = 0;
1388 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1389 plp = (struct plist *)qp;
1392 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1399 if (plp->pcp->pl_flags & P_A) {
1400 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1402 "\t%s detects sub movement in %s\n",
1403 prplane(pp), xyas(x, y, pp->pln_own));
1406 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1408 "\t%s tracks %s %s at %s\n",
1409 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1413 "\t%s depth-charging %s %s in %s\n",
1414 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1417 "\t%s pinbombing %s %s in %s\n",
1418 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1420 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1421 if (plp->plane.pln_nuketype != -1)
1423 else if (hardtarget != SECT_HARDTARGET)
1424 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1425 /* Always calculate damage */
1426 if (roll(100) <= hitchance) {
1427 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1429 if (mission == MI_INTERDICT) {
1431 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1432 prplane(pp), nukedam, cname(victim), s);
1437 "\t\thit %s %s for %d damage\n",
1438 cname(victim), s, newdam);
1442 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1443 wu(0, pp->pln_own, "missed\n");
1444 if (mission == MI_SINTERDICT) {
1446 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1447 xyas(x, y, victim));
1448 } else if (*s == 's') {
1449 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1450 s, xyas(x, y, victim));
1452 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1453 s, xyas(x, y, victim));
1455 /* Now, even though we missed, the bombs
1457 collateral_damage(x, y, newdam, bombers);
1460 /* use up missiles */
1461 if (plp->pcp->pl_flags & P_M) {
1462 makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
1463 pp->pln_x, pp->pln_y);
1472 * Check to see if anyone hostile to the victim
1473 * is running an air defense mission on this
1474 * sector. If so, do air combat
1477 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1478 struct emp_qelem *esc_list)
1481 int mission_flags, tech, combat = 0, rel, dist, z;
1482 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1484 struct genlist *glp;
1486 struct genlist mi[MAXNOC];
1493 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1495 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1498 memset(mi, 0, sizeof(mi));
1499 for (z = 1; z < MAXNOC; z++)
1500 emp_initque((struct emp_qelem *)&mi[z]);
1502 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1504 for (cn = 1; cn < MAXNOC; cn++) {
1505 /* Check our relations */
1506 rel = getrel(getnatp(cn), victim);
1511 if (QEMPTY(&mi[cn].queue))
1514 /* Ok, make a list of all the interceptors. Note that this *copies* the
1515 * list from the mission creation. This list must be deleted later. */
1516 emp_initque(&interceptors);
1517 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1519 glp = (struct genlist *)qp;
1522 dist = mapdist(x, y, gp->x, gp->y);
1524 plp = malloc(sizeof(struct plist));
1525 memset(plp, 0, sizeof(struct plist));
1527 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1529 /* missiles go one way, so we can use all the range */
1530 if (!(plp->pcp->pl_flags & P_M))
1532 /* If it's out of range, free it and continue on */
1533 if (dist > plp->plane.pln_range) {
1537 emp_insque(&plp->queue, &interceptors);
1540 /* Remove those who cannot go */
1541 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1543 if (QEMPTY(&interceptors))
1546 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1547 * that the higher numbered planes go into battle (they should be the better ones
1548 * at fighting, if all went well.) */
1550 for (qp = interceptors.q_forw; qp != (&interceptors);
1553 tcount -= (count * 2);
1554 /* Just in case there are more incoming than we have */
1557 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1561 /* Free it up and continue */
1563 glp = (struct genlist *)qp;
1568 /* Now, make a list of all the airports these planes are coming from */
1570 for (qp = interceptors.q_forw; qp != (&interceptors);
1572 plp = (struct plist *)qp;
1573 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1574 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1577 /* Now, fly them out one airport at a time */
1578 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1579 struct airport *air;
1581 air = (struct airport *)qp;
1582 dist = mapdist(x, y, air->x, air->y);
1586 /* Split off the interceptors at this base into i */
1587 divide(&interceptors, &i, air->x, air->y);
1591 mission_flags |= P_X; /* stealth (shhh) */
1592 /* gets turned off if not all choppers */
1593 mission_flags |= P_H;
1594 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1595 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1597 /* Did we run out of interceptors? */
1600 /* Did we run out of bombers? */
1601 if (QEMPTY(bomb_list)) {
1602 /* Yes, so we have to put the rest of the interceptors back, and
1603 then continue, or we leak memory */
1608 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1609 mission_flags, &tech);
1611 /* Did we run out of interceptors? */
1614 /* Did we run out of bombers? */
1615 if (QEMPTY(bomb_list)) {
1616 /* Yes, so we have to put the rest of the interceptors back, and
1617 then continue, or we leak memory */
1622 path = BestAirPath(buf, air->x, air->y, x, y);
1623 if (CANT_HAPPEN(!path))
1625 wu(0, cn, "Flying %s mission from %s\n",
1626 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1627 if (air->own && (air->own != cn)) {
1628 wu(0, air->own, "%s is flying %s mission from %s\n",
1629 cname(cn), mission_name(MI_AIR_DEFENSE),
1630 xyas(air->x, air->y, air->own));
1633 /* Now, fly the planes to the sector */
1634 emp_initque(&empty);
1635 ac_encounter(&i, &empty, air->x, air->y,
1636 path, mission_flags, 1, bomb_list, esc_list);
1638 /* If none made it, continue */
1642 /* Some made it, so now they get to try to fight. */
1643 /* Intercept the escorts first */
1645 if (!QEMPTY(esc_list)) {
1646 mpr(victim, "%s air defense planes intercept!\n",
1648 ac_combat_headers(victim, cn);
1649 ac_airtoair(esc_list, &i);
1652 /* Now intercept the bombers */
1653 if (!QEMPTY(bomb_list)) {
1655 mpr(victim, "%s air defense planes intercept!\n",
1657 ac_combat_headers(victim, cn);
1659 ac_airtoair(bomb_list, &i);
1668 /* We have to free all of these, if they are still there, otherwise they get
1669 lost and we leak memory all over the place. */
1670 for (cn = 1; cn < MAXNOC; cn++) {
1671 /* free up all this memory if it's still there */
1672 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1674 glp = (struct genlist *)qp;