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, (double)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 * techfact(sp->shp_tech,
561 ((double)sp->shp_frnge)) /
564 range2 = (double)roundrange(range);
568 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
570 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
571 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
572 mobcost = (480.0 / (mobcost +
573 techfact(sp->shp_tech, mobcost)));
574 sp->shp_mobil -= mobcost;
575 putship(sp->shp_uid, sp);
576 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
579 "%s locking on %s %s in %s\n",
580 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
582 "\tEffective torpedo range is %.1f\n", range);
584 "\tWhooosh... Hitchance = %d%%\n",
585 (int)(hitchance * 100));
587 if (hitchance < 1.0 && !chance(hitchance)) {
588 wu(0, sp->shp_own, "\tMissed\n");
590 "Incoming torpedo sighted @ %s missed (whew)!\n",
594 wu(0, sp->shp_own, "\tBOOM!...\n");
595 dam2 = TORP_DAMAGE();
598 nreport(victim, N_TORP_SHIP, 0, 1);
600 "\tTorpedo hit %s %s for %d damage\n",
601 cname(victim), s, dam2);
604 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
605 xyas(x, y, victim), dam2);
607 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
608 range2 = (double)roundrange(range);
611 gun = sp->shp_item[I_GUN];
612 gun = MIN(gun, sp->shp_glim);
613 shell = sp->shp_item[I_SHELL];
615 shell += supply_commod(sp->shp_own,
616 sp->shp_x, sp->shp_y, I_SHELL,
618 gun = MIN(gun, shell);
619 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
623 dam2 = seagun(sp->shp_effic, gun);
627 prb = ((double)md) / range2;
632 if (sect.sct_type == SCT_WATER)
633 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
635 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
637 "%s fires at %s %s at %s\n",
638 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
640 mpr(victim, "%s %s fires at you at %s\n",
641 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
643 sp->shp_item[I_SHELL] = shell - gun;
644 putship(sp->shp_uid, sp);
646 } else if (glp->type == EF_PLANE) {
648 if (pcp->pl_flags & P_M)
649 /* units have their own missile interdiction */
650 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
653 /* save planes for later */
654 plp = malloc(sizeof(struct plist));
656 memset(plp, 0, sizeof(struct plist));
658 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
659 if (plp->pcp->pl_flags & P_M)
660 emp_insque(&plp->queue, &missiles);
662 emp_insque(&plp->queue, &bombers);
663 plane_owner = plp->plane.pln_own;
666 if (!QEMPTY(&missiles)) {
667 /* I arbitrarily chose 100 mindam -KHS */
669 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
670 "sector", victim, mission);
671 qp = missiles.q_forw;
672 while (qp != (&missiles)) {
680 if (QEMPTY(&bombers)) {
683 glp = (struct genlist *)qp;
692 * If there are planes performing an
693 * interdict or support mission, find
694 * some escorts for them, if possible.
695 * Up to 2 per bomber, if possible.
697 find_escorts(x, y, plane_owner, &escorts);
699 if (mission == MI_SINTERDICT)
700 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
702 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
704 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
706 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
707 plp = (struct plist *)qp;
708 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
709 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
712 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
717 air = (struct airport *)qp;
718 md = mapdist(x, y, air->x, air->y);
723 /* Split off the bombers at this base into b */
724 divide(&bombers, &b, air->x, air->y);
726 /* Split off the escorts at this base into e */
727 divide(&escorts, &e, air->x, air->y);
731 mission_flags |= P_X; /* stealth (shhh) */
732 mission_flags |= P_H; /* gets turned off if not all choppers */
734 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
735 0, mission_flags, &tech);
740 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
741 P_F | P_ESC, mission_flags, &tech);
743 pp = BestAirPath(buf, air->x, air->y, x, y);
744 if (CANT_HAPPEN(!pp))
746 wu(0, plane_owner, "Flying %s mission from %s\n",
747 mission_name(mission), xyas(air->x, air->y, plane_owner));
748 if (air->own && (air->own != plane_owner)) {
749 wu(0, air->own, "%s is flying %s mission from %s\n",
750 cname(plane_owner), mission_name(mission),
751 xyas(air->x, air->y, air->own));
754 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
758 air_damage(&b, x, y, mission, victim, s, hardtarget);
766 if (sect.sct_type == SCT_WATER)
767 nreport(plane_owner, N_SHP_BOMB, victim, 1);
769 nreport(plane_owner, N_SCT_BOMB, victim, 1);
772 /* free up all this memory */
775 glp = (struct genlist *)qp;
783 while (qp != (&escorts)) {
791 while (qp != (&bombers)) {
802 cando(int mission, int type)
806 if (type == EF_PLANE)
810 if (type == EF_PLANE)
814 if ((type == EF_PLANE) || (type == EF_SHIP))
822 if (type == EF_PLANE)
835 nameofitem(struct genitem *gp, int type)
839 return prship((struct shpstr *)gp);
841 return prplane((struct plnstr *)gp);
843 return prland((struct lndstr *)gp);
849 mission_name(short mission)
853 return "an interdiction";
857 return "a offensive support";
859 return "a defensive support";
865 return "a sub interdiction";
867 return "an air defense";
869 return "a mysterious";
873 show_mission(int type, struct nstr_item *np)
875 int first = 1, radius;
879 while (nxtitem(np, &item)) {
880 gp = (struct genitem *)&item;
881 if (!player->owner || gp->own == 0)
885 pr("Thing x,y op-sect rad mission\n");
888 pr("%-25s", nameofitem(gp, type));
889 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
890 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
891 gp->mission == MI_OSUPPORT ||
892 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
894 oprange(gp, type, &radius);
895 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
896 if (radius < gp->radius)
899 pr(" %4d", gp->radius);
900 } else if (gp->mission == MI_RESERVE) {
904 getsect(gp->x, gp->y, §);
905 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
908 if (item.land.lnd_rad_max == 0)
911 plus += item.land.lnd_rad_max;
912 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
914 } else if (gp->mission == MI_ESCORT) {
916 pr(" %4d", (int)(item.plane.pln_range / 2.0));
920 pr(" is on %s mission\n", mission_name(gp->mission));
922 pr(" has no mission.\n");
927 oprange(struct genitem *gp, int type, int *radius)
936 getship(gp->uid, &ship);
937 range = ldround(techfact(gp->tech,
938 (double)ship.shp_frnge / 2.0), 1);
941 getland(gp->uid, &land);
942 range = ldround(techfact((int)land.lnd_tech,
943 (double)land.lnd_frg / 2.0), 1);
946 getplane(gp->uid, &plane);
947 /* missiles go one way, so we can use all the range */
948 if (plchr[(int)plane.pln_type].pl_flags & P_M)
949 range = plane.pln_range;
951 range = ldround((double)plane.pln_range / 2.0, 1);;
954 CANT_HAPPEN("bad TYPE");
965 * Remove all planes who cannot go on
966 * the mission from the plane list.
969 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
972 struct emp_qelem *qp, *next;
977 struct plchrstr *pcp;
982 for (qp = list->q_forw; qp != list; qp = next) {
984 plp = (struct plist *)qp;
988 if (pp->pln_effic < 40) {
994 if (pp->pln_mobil < 1) {
1001 if (ontradingblock(EF_PLANE, pp)) {
1011 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1013 if ((wantflags & y) == y)
1014 if ((pcp->pl_flags & y) != y) {
1036 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1040 if ((pcp->pl_flags & P_E) ||
1041 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1053 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1055 if ((nowantflags & y) == y)
1056 if ((pcp->pl_flags & y) == y)
1065 if (pp->pln_ship >= 0) {
1066 if (!getship(pp->pln_ship, &ship)) {
1069 putplane(pp->pln_uid, pp);
1074 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1077 if (ship.shp_effic < SHIP_MINEFF) {
1080 /* Can't fly off of inefficient or non-owned, non-allied ships */
1081 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1082 ((ship.shp_own != pp->pln_own) &&
1083 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1089 if (pp->pln_land >= 0) {
1090 if (!getland(pp->pln_land, &land)) {
1093 putplane(pp->pln_uid, pp);
1098 if (!(pcp->pl_flags & P_E))
1100 if (land.lnd_effic < LAND_MINEFF)
1103 /* Can't fly off of inefficient or non-owned, non-allied units */
1104 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1105 ((land.lnd_own != pp->pln_own) &&
1106 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1112 /* Can't fly off units in ships or other units */
1113 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1119 /* Now, check the sector status if not on a plane or unit */
1120 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1121 /* If we can't get the sector, we can't check it, and can't fly */
1122 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1127 /* First, check allied status */
1128 /* Can't fly from non-owned sectors or non-allied sectors */
1129 if ((sect.sct_own != pp->pln_own) &&
1130 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1135 /* non-vtol plane */
1136 if ((pcp->pl_flags & P_V) == 0) {
1137 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1144 if (pcp->pl_flags & P_A) {
1145 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1152 putplane(pp->pln_uid, pp);
1157 * Arm only the planes at x,y
1161 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1162 int mission, struct ichrstr *ip, int flags,
1163 int mission_flags, int *tech)
1165 struct emp_qelem *qp;
1166 struct emp_qelem *next;
1171 for (qp = list->q_forw; qp != list; qp = next) {
1173 plp = (struct plist *)qp;
1175 if (plp->plane.pln_x != x)
1177 if (plp->plane.pln_y != y)
1180 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1185 if (flags & (P_S | P_I)) {
1186 if (plp->pcp->pl_flags & P_S)
1187 mission_flags |= P_S;
1188 if (plp->pcp->pl_flags & P_I)
1189 mission_flags |= P_I;
1191 if (*tech > plp->plane.pln_tech)
1192 *tech = plp->plane.pln_tech;
1193 if (!(plp->pcp->pl_flags & P_H))
1194 /* no stealth on this mission */
1195 mission_flags &= ~P_H;
1196 if (!(plp->pcp->pl_flags & P_X))
1197 /* no stealth on this mission */
1198 mission_flags &= ~P_X;
1199 if (!(plp->pcp->pl_flags & P_A)) {
1200 /* no asw on this mission */
1201 mission_flags &= ~P_A;
1203 if (!(plp->pcp->pl_flags & P_MINE)) {
1204 /* no asw on this mission */
1205 mission_flags &= ~P_MINE;
1209 * Mob costs for missions are 1/2 normal
1212 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1213 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1216 return mission_flags;
1220 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1223 struct plchrstr *pcp;
1235 if (pp->pln_ship >= 0) {
1236 getship(pp->pln_ship, &ship);
1237 item = ship.shp_item;
1238 } else if (pp->pln_land >= 0) {
1239 getland(pp->pln_land, &land);
1240 item = land.lnd_item;
1242 getsect(pp->pln_x, pp->pln_y, §);
1243 item = sect.sct_item;
1245 if (pcp->pl_fuel > item[I_PETROL]) {
1248 item[I_PETROL] -= pcp->pl_fuel;
1250 if (!(flags & P_F)) {
1256 if (pp->pln_nuketype == -1) {
1258 needed = pp->pln_load;
1262 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1265 needed = (pp->pln_load * 2) / ip->i_lbs;
1268 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1271 needed = (pp->pln_load * 2) / ip->i_lbs;
1274 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1277 needed = pp->pln_load / ip->i_lbs;
1280 if (pp->pln_nuketype == -1)
1283 case 'i': /* missile interception */
1286 needed = pp->pln_load;
1292 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1295 if (itype != I_NONE) {
1296 if (itype == I_SHELL && item[itype] < needed)
1297 item[itype] += supply_commod(plp->plane.pln_own,
1301 if (item[itype] < needed)
1303 item[itype] -= needed;
1305 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1306 plp->bombs = needed;
1310 if (pp->pln_ship >= 0)
1311 putship(ship.shp_uid, &ship);
1312 else if (pp->pln_land >= 0)
1313 putland(land.lnd_uid, &land);
1320 * Return 1 if this x,y pair is in the list
1323 find_airport(struct emp_qelem *airp, coord x, coord y)
1325 struct emp_qelem *qp;
1328 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1329 a = (struct airport *)qp;
1330 if ((a->x == x) && (a->y == y))
1337 /* #*# This needs to be changed to include acc's -KHS */
1339 add_airport(struct emp_qelem *airp, coord x, coord y)
1344 a = malloc(sizeof(struct airport));
1348 getsect(x, y, §);
1349 a->own = sect.sct_own;
1351 emp_insque((struct emp_qelem *)a, airp);
1355 * Take all the planes in list 1 that
1356 * are at x,y, and put them into list 2.
1359 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1361 struct emp_qelem *qp, *next;
1364 for (qp = l1->q_forw; qp != l1; qp = next) {
1366 plp = (struct plist *)qp;
1368 if (plp->plane.pln_x != x)
1370 if (plp->plane.pln_y != y)
1379 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1380 natid victim, char *s, int hardtarget)
1382 struct emp_qelem *qp;
1385 int newdam, dam = 0;
1389 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1390 plp = (struct plist *)qp;
1393 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1400 if (plp->pcp->pl_flags & P_A) {
1401 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1403 "\t%s detects sub movement in %s\n",
1404 prplane(pp), xyas(x, y, pp->pln_own));
1407 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1409 "\t%s tracks %s %s at %s\n",
1410 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1414 "\t%s depth-charging %s %s in %s\n",
1415 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1418 "\t%s pinbombing %s %s in %s\n",
1419 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1421 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1422 if (plp->plane.pln_nuketype != -1)
1424 else if (hardtarget != SECT_HARDTARGET)
1425 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1426 /* Always calculate damage */
1427 if (roll(100) <= hitchance) {
1428 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1430 if (mission == MI_INTERDICT) {
1432 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1433 prplane(pp), nukedam, cname(victim), s);
1438 "\t\thit %s %s for %d damage\n",
1439 cname(victim), s, newdam);
1443 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1444 wu(0, pp->pln_own, "missed\n");
1445 if (mission == MI_SINTERDICT) {
1447 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1448 xyas(x, y, victim));
1449 } else if (*s == 's') {
1450 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1451 s, xyas(x, y, victim));
1453 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1454 s, xyas(x, y, victim));
1456 /* Now, even though we missed, the bombs
1458 collateral_damage(x, y, newdam, bombers);
1461 /* use up missiles */
1462 if (plp->pcp->pl_flags & P_M) {
1463 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1473 * Check to see if anyone hostile to the victim
1474 * is running an air defense mission on this
1475 * sector. If so, do air combat
1478 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1479 struct emp_qelem *esc_list)
1482 int mission_flags, tech, combat = 0, rel, dist, z;
1483 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1485 struct genlist *glp;
1487 struct genlist mi[MAXNOC];
1494 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1496 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1499 memset(mi, 0, sizeof(mi));
1500 for (z = 1; z < MAXNOC; z++)
1501 emp_initque((struct emp_qelem *)&mi[z]);
1503 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1505 for (cn = 1; cn < MAXNOC; cn++) {
1506 /* Check our relations */
1507 rel = getrel(getnatp(cn), victim);
1512 if (QEMPTY(&mi[cn].queue))
1515 /* Ok, make a list of all the interceptors. Note that this *copies* the
1516 * list from the mission creation. This list must be deleted later. */
1517 emp_initque(&interceptors);
1518 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1520 glp = (struct genlist *)qp;
1523 dist = mapdist(x, y, gp->x, gp->y);
1525 plp = malloc(sizeof(struct plist));
1526 memset(plp, 0, sizeof(struct plist));
1528 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1530 /* missiles go one way, so we can use all the range */
1531 if (!(plp->pcp->pl_flags & P_M))
1533 /* If it's out of range, free it and continue on */
1534 if (dist > plp->plane.pln_range) {
1538 emp_insque(&plp->queue, &interceptors);
1541 /* Remove those who cannot go */
1542 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1544 if (QEMPTY(&interceptors))
1547 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1548 * that the higher numbered planes go into battle (they should be the better ones
1549 * at fighting, if all went well.) */
1551 for (qp = interceptors.q_forw; qp != (&interceptors);
1554 tcount -= (count * 2);
1555 /* Just in case there are more incoming than we have */
1558 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1562 /* Free it up and continue */
1564 glp = (struct genlist *)qp;
1569 /* Now, make a list of all the airports these planes are coming from */
1571 for (qp = interceptors.q_forw; qp != (&interceptors);
1573 plp = (struct plist *)qp;
1574 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1575 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1578 /* Now, fly them out one airport at a time */
1579 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1580 struct airport *air;
1582 air = (struct airport *)qp;
1583 dist = mapdist(x, y, air->x, air->y);
1587 /* Split off the interceptors at this base into i */
1588 divide(&interceptors, &i, air->x, air->y);
1592 mission_flags |= P_X; /* stealth (shhh) */
1593 /* gets turned off if not all choppers */
1594 mission_flags |= P_H;
1595 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1596 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1598 /* Did we run out of interceptors? */
1601 /* Did we run out of bombers? */
1602 if (QEMPTY(bomb_list)) {
1603 /* Yes, so we have to put the rest of the interceptors back, and
1604 then continue, or we leak memory */
1609 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1610 mission_flags, &tech);
1612 /* Did we run out of interceptors? */
1615 /* Did we run out of bombers? */
1616 if (QEMPTY(bomb_list)) {
1617 /* Yes, so we have to put the rest of the interceptors back, and
1618 then continue, or we leak memory */
1623 path = BestAirPath(buf, air->x, air->y, x, y);
1624 if (CANT_HAPPEN(!path))
1626 wu(0, cn, "Flying %s mission from %s\n",
1627 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1628 if (air->own && (air->own != cn)) {
1629 wu(0, air->own, "%s is flying %s mission from %s\n",
1630 cname(cn), mission_name(MI_AIR_DEFENSE),
1631 xyas(air->x, air->y, air->own));
1634 /* Now, fly the planes to the sector */
1635 emp_initque(&empty);
1636 ac_encounter(&i, &empty, air->x, air->y,
1637 path, mission_flags, 1, bomb_list, esc_list);
1639 /* If none made it, continue */
1643 /* Some made it, so now they get to try to fight. */
1644 /* Intercept the escorts first */
1646 if (!QEMPTY(esc_list)) {
1647 mpr(victim, "%s air defense planes intercept!\n",
1649 ac_combat_headers(victim, cn);
1650 ac_airtoair(esc_list, &i);
1653 /* Now intercept the bombers */
1654 if (!QEMPTY(bomb_list)) {
1656 mpr(victim, "%s air defense planes intercept!\n",
1658 ac_combat_headers(victim, cn);
1660 ac_airtoair(bomb_list, &i);
1669 /* We have to free all of these, if they are still there, otherwise they get
1670 lost and we leak memory all over the place. */
1671 for (cn = 1; cn < MAXNOC; cn++) {
1672 /* free up all this memory if it's still there */
1673 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1675 glp = (struct genlist *)qp;