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, range, air_dam = 0;
440 double prb, mobcost, hitchance, vrange;
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 ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
471 if (lnd_getmil(lp) < 1)
474 range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
478 shell = lp->lnd_item[I_SHELL];
479 gun = lp->lnd_item[I_GUN];
480 if (shell == 0 || gun == 0)
483 if (has_supply(lp)) {
485 putland(lp->lnd_uid, lp);
486 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
487 lp->lnd_ammo, shell), 1);
488 if (sect.sct_type == SCT_WATER) {
489 double dam3 = (double)dam2;
490 if (chance(((double)lp->lnd_acc) / 100.0))
491 dam2 = ldround((dam3 / 2.0), 1);
494 if (sect.sct_type == SCT_WATER)
495 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
497 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
499 "%s fires at %s %s at %s\n",
500 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
502 mpr(victim, "%s %s fires at you at %s\n",
503 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
505 } else if (glp->type == EF_SHIP) {
509 if (sp->shp_effic < 60)
511 if (sp->shp_frnge == 0)
513 if (((mission == MI_INTERDICT) ||
514 (mission == MI_SINTERDICT)) &&
515 (md > ship_max_interdiction_range))
517 if (sp->shp_item[I_MILIT] < 1)
520 if ((mcp->m_flags & M_SUB) &&
521 (sect.sct_type != SCT_WATER))
524 if (mission == MI_SINTERDICT) {
525 if (!(mcp->m_flags & M_SONAR))
527 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
529 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
530 vrange *= (double)sp->shp_effic / 200.0;
533 /* can't look all the time */
537 if (mcp->m_flags & M_SUB) {
538 /* If we aren't shooting at "subs" or "ships" don't fire at all from
542 if (sp->shp_mobil < 0)
544 gun = sp->shp_item[I_GUN];
547 shell = sp->shp_item[I_SHELL];
548 if (shell < SHP_TORP_SHELLS)
549 shell += supply_commod(sp->shp_own,
550 sp->shp_x, sp->shp_y, I_SHELL,
551 SHP_TORP_SHELLS - shell);
552 if (shell < SHP_TORP_SHELLS)
555 range = roundrange(torprange(sp));
559 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
561 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
562 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
563 mobcost = (480.0 / (mobcost +
564 techfact(sp->shp_tech, mobcost)));
565 sp->shp_mobil -= mobcost;
566 putship(sp->shp_uid, sp);
567 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
570 "%s locking on %s %s in %s\n",
571 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
573 "\tEffective torpedo range is %d.0\n", range);
575 "\tWhooosh... Hitchance = %d%%\n",
576 (int)(hitchance * 100));
578 if (hitchance < 1.0 && !chance(hitchance)) {
579 wu(0, sp->shp_own, "\tMissed\n");
581 "Incoming torpedo sighted @ %s missed (whew)!\n",
585 wu(0, sp->shp_own, "\tBOOM!...\n");
586 dam2 = TORP_DAMAGE();
589 nreport(victim, N_TORP_SHIP, 0, 1);
591 "\tTorpedo hit %s %s for %d damage\n",
592 cname(victim), s, dam2);
595 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
596 xyas(x, y, victim), dam2);
598 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
601 gun = sp->shp_item[I_GUN];
602 gun = MIN(gun, sp->shp_glim);
603 shell = sp->shp_item[I_SHELL];
605 shell += supply_commod(sp->shp_own,
606 sp->shp_x, sp->shp_y, I_SHELL,
608 gun = MIN(gun, shell);
609 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
613 dam2 = seagun(sp->shp_effic, gun);
617 prb = (double)md / range;
622 if (sect.sct_type == SCT_WATER)
623 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
625 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
627 "%s fires at %s %s at %s\n",
628 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
630 mpr(victim, "%s %s fires at you at %s\n",
631 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
633 sp->shp_item[I_SHELL] = shell - gun;
634 putship(sp->shp_uid, sp);
636 } else if (glp->type == EF_PLANE) {
638 if (pcp->pl_flags & P_M)
639 /* units have their own missile interdiction */
640 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
643 /* save planes for later */
644 plp = malloc(sizeof(struct plist));
646 memset(plp, 0, sizeof(struct plist));
648 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
649 if (plp->pcp->pl_flags & P_M)
650 emp_insque(&plp->queue, &missiles);
652 emp_insque(&plp->queue, &bombers);
653 plane_owner = plp->plane.pln_own;
656 if (!QEMPTY(&missiles)) {
657 /* I arbitrarily chose 100 mindam -KHS */
659 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
660 "sector", victim, mission);
661 qp = missiles.q_forw;
662 while (qp != (&missiles)) {
670 if (QEMPTY(&bombers)) {
673 glp = (struct genlist *)qp;
682 * If there are planes performing an
683 * interdict or support mission, find
684 * some escorts for them, if possible.
685 * Up to 2 per bomber, if possible.
687 find_escorts(x, y, plane_owner, &escorts);
689 if (mission == MI_SINTERDICT)
690 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
692 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
694 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
696 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
697 plp = (struct plist *)qp;
698 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
699 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
702 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
707 air = (struct airport *)qp;
708 md = mapdist(x, y, air->x, air->y);
713 /* Split off the bombers at this base into b */
714 divide(&bombers, &b, air->x, air->y);
716 /* Split off the escorts at this base into e */
717 divide(&escorts, &e, air->x, air->y);
721 mission_flags |= P_X; /* stealth (shhh) */
722 mission_flags |= P_H; /* gets turned off if not all choppers */
724 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
725 0, mission_flags, &tech);
730 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
731 P_F | P_ESC, mission_flags, &tech);
733 pp = BestAirPath(buf, air->x, air->y, x, y);
734 if (CANT_HAPPEN(!pp))
736 wu(0, plane_owner, "Flying %s mission from %s\n",
737 mission_name(mission), xyas(air->x, air->y, plane_owner));
738 if (air->own && (air->own != plane_owner)) {
739 wu(0, air->own, "%s is flying %s mission from %s\n",
740 cname(plane_owner), mission_name(mission),
741 xyas(air->x, air->y, air->own));
744 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
748 air_damage(&b, x, y, mission, victim, s, hardtarget);
756 if (sect.sct_type == SCT_WATER)
757 nreport(plane_owner, N_SHP_BOMB, victim, 1);
759 nreport(plane_owner, N_SCT_BOMB, victim, 1);
762 /* free up all this memory */
765 glp = (struct genlist *)qp;
773 while (qp != (&escorts)) {
781 while (qp != (&bombers)) {
792 cando(int mission, int type)
796 if (type == EF_PLANE)
800 if (type == EF_PLANE)
804 if ((type == EF_PLANE) || (type == EF_SHIP))
812 if (type == EF_PLANE)
825 nameofitem(struct genitem *gp, int type)
829 return prship((struct shpstr *)gp);
831 return prplane((struct plnstr *)gp);
833 return prland((struct lndstr *)gp);
839 mission_name(short mission)
843 return "an interdiction";
847 return "a offensive support";
849 return "a defensive support";
855 return "a sub interdiction";
857 return "an air defense";
859 return "a mysterious";
863 show_mission(int type, struct nstr_item *np)
865 int first = 1, radius;
869 while (nxtitem(np, &item)) {
870 gp = (struct genitem *)&item;
871 if (!player->owner || gp->own == 0)
875 pr("Thing x,y op-sect rad mission\n");
878 pr("%-25s", nameofitem(gp, type));
879 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
880 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
881 gp->mission == MI_OSUPPORT ||
882 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
884 oprange(gp, type, &radius);
885 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
886 if (radius < gp->radius)
889 pr(" %4d", gp->radius);
890 } else if (gp->mission == MI_RESERVE) {
894 getsect(gp->x, gp->y, §);
895 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
898 if (item.land.lnd_rad_max == 0)
901 plus += item.land.lnd_rad_max;
902 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
904 } else if (gp->mission == MI_ESCORT) {
906 pr(" %4d", (int)(item.plane.pln_range / 2.0));
910 pr(" is on %s mission\n", mission_name(gp->mission));
912 pr(" has no mission.\n");
917 oprange(struct genitem *gp, int type, int *radius)
926 getship(gp->uid, &ship);
927 range = ldround(effrange(ship.shp_frnge, ship.shp_tech), 1);
930 getland(gp->uid, &land);
931 range = ldround(effrange(land.lnd_frg, land.lnd_tech), 1);
934 getplane(gp->uid, &plane);
935 /* missiles go one way, so we can use all the range */
936 if (plchr[(int)plane.pln_type].pl_flags & P_M)
937 range = plane.pln_range;
939 range = ldround((double)plane.pln_range / 2.0, 1);;
942 CANT_HAPPEN("bad TYPE");
953 * Remove all planes who cannot go on
954 * the mission from the plane list.
957 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
960 struct emp_qelem *qp, *next;
965 struct plchrstr *pcp;
970 for (qp = list->q_forw; qp != list; qp = next) {
972 plp = (struct plist *)qp;
976 if (pp->pln_effic < 40) {
982 if (pp->pln_mobil < 1) {
989 if (ontradingblock(EF_PLANE, pp)) {
999 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1001 if ((wantflags & y) == y)
1002 if ((pcp->pl_flags & y) != y) {
1024 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1028 if ((pcp->pl_flags & P_E) ||
1029 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1041 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1043 if ((nowantflags & y) == y)
1044 if ((pcp->pl_flags & y) == y)
1053 if (pp->pln_ship >= 0) {
1054 if (!getship(pp->pln_ship, &ship)) {
1057 putplane(pp->pln_uid, pp);
1062 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1065 if (ship.shp_effic < SHIP_MINEFF) {
1068 /* Can't fly off of inefficient or non-owned, non-allied ships */
1069 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1070 ((ship.shp_own != pp->pln_own) &&
1071 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1077 if (pp->pln_land >= 0) {
1078 if (!getland(pp->pln_land, &land)) {
1081 putplane(pp->pln_uid, pp);
1086 if (!(pcp->pl_flags & P_E))
1088 if (land.lnd_effic < LAND_MINEFF)
1091 /* Can't fly off of inefficient or non-owned, non-allied units */
1092 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1093 ((land.lnd_own != pp->pln_own) &&
1094 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1100 /* Can't fly off units in ships or other units */
1101 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1107 /* Now, check the sector status if not on a plane or unit */
1108 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1109 /* If we can't get the sector, we can't check it, and can't fly */
1110 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1115 /* First, check allied status */
1116 /* Can't fly from non-owned sectors or non-allied sectors */
1117 if ((sect.sct_own != pp->pln_own) &&
1118 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1123 /* non-vtol plane */
1124 if ((pcp->pl_flags & P_V) == 0) {
1125 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1132 if (pcp->pl_flags & P_A) {
1133 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1140 putplane(pp->pln_uid, pp);
1145 * Arm only the planes at x,y
1149 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1150 int mission, struct ichrstr *ip, int flags,
1151 int mission_flags, int *tech)
1153 struct emp_qelem *qp;
1154 struct emp_qelem *next;
1159 for (qp = list->q_forw; qp != list; qp = next) {
1161 plp = (struct plist *)qp;
1163 if (plp->plane.pln_x != x)
1165 if (plp->plane.pln_y != y)
1168 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1173 if (flags & (P_S | P_I)) {
1174 if (plp->pcp->pl_flags & P_S)
1175 mission_flags |= P_S;
1176 if (plp->pcp->pl_flags & P_I)
1177 mission_flags |= P_I;
1179 if (*tech > plp->plane.pln_tech)
1180 *tech = plp->plane.pln_tech;
1181 if (!(plp->pcp->pl_flags & P_H))
1182 /* no stealth on this mission */
1183 mission_flags &= ~P_H;
1184 if (!(plp->pcp->pl_flags & P_X))
1185 /* no stealth on this mission */
1186 mission_flags &= ~P_X;
1187 if (!(plp->pcp->pl_flags & P_A)) {
1188 /* no asw on this mission */
1189 mission_flags &= ~P_A;
1191 if (!(plp->pcp->pl_flags & P_MINE)) {
1192 /* no asw on this mission */
1193 mission_flags &= ~P_MINE;
1197 * Mob costs for missions are 1/2 normal
1200 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1201 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1204 return mission_flags;
1208 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1211 struct plchrstr *pcp;
1223 if (pp->pln_ship >= 0) {
1224 getship(pp->pln_ship, &ship);
1225 item = ship.shp_item;
1226 } else if (pp->pln_land >= 0) {
1227 getland(pp->pln_land, &land);
1228 item = land.lnd_item;
1230 getsect(pp->pln_x, pp->pln_y, §);
1231 item = sect.sct_item;
1233 if (pcp->pl_fuel > item[I_PETROL]) {
1236 item[I_PETROL] -= pcp->pl_fuel;
1238 if (!(flags & P_F)) {
1244 if (pp->pln_nuketype == -1) {
1246 needed = pp->pln_load;
1250 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1253 needed = (pp->pln_load * 2) / ip->i_lbs;
1256 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1259 needed = (pp->pln_load * 2) / ip->i_lbs;
1262 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1265 needed = pp->pln_load / ip->i_lbs;
1268 if (pp->pln_nuketype == -1)
1271 case 'i': /* missile interception */
1274 needed = pp->pln_load;
1280 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1283 if (itype != I_NONE) {
1284 if (itype == I_SHELL && item[itype] < needed)
1285 item[itype] += supply_commod(plp->plane.pln_own,
1289 if (item[itype] < needed)
1291 item[itype] -= needed;
1293 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1294 plp->bombs = needed;
1298 if (pp->pln_ship >= 0)
1299 putship(ship.shp_uid, &ship);
1300 else if (pp->pln_land >= 0)
1301 putland(land.lnd_uid, &land);
1308 * Return 1 if this x,y pair is in the list
1311 find_airport(struct emp_qelem *airp, coord x, coord y)
1313 struct emp_qelem *qp;
1316 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1317 a = (struct airport *)qp;
1318 if ((a->x == x) && (a->y == y))
1325 /* #*# This needs to be changed to include acc's -KHS */
1327 add_airport(struct emp_qelem *airp, coord x, coord y)
1332 a = malloc(sizeof(struct airport));
1336 getsect(x, y, §);
1337 a->own = sect.sct_own;
1339 emp_insque((struct emp_qelem *)a, airp);
1343 * Take all the planes in list 1 that
1344 * are at x,y, and put them into list 2.
1347 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1349 struct emp_qelem *qp, *next;
1352 for (qp = l1->q_forw; qp != l1; qp = next) {
1354 plp = (struct plist *)qp;
1356 if (plp->plane.pln_x != x)
1358 if (plp->plane.pln_y != y)
1367 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1368 natid victim, char *s, int hardtarget)
1370 struct emp_qelem *qp;
1373 int newdam, dam = 0;
1377 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1378 plp = (struct plist *)qp;
1381 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1388 if (plp->pcp->pl_flags & P_A) {
1389 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1391 "\t%s detects sub movement in %s\n",
1392 prplane(pp), xyas(x, y, pp->pln_own));
1395 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1397 "\t%s tracks %s %s at %s\n",
1398 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1402 "\t%s depth-charging %s %s in %s\n",
1403 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1406 "\t%s pinbombing %s %s in %s\n",
1407 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1409 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1410 if (plp->plane.pln_nuketype != -1)
1412 else if (hardtarget != SECT_HARDTARGET)
1413 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1414 /* Always calculate damage */
1415 if (roll(100) <= hitchance) {
1416 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1418 if (mission == MI_INTERDICT) {
1420 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1421 prplane(pp), nukedam, cname(victim), s);
1426 "\t\thit %s %s for %d damage\n",
1427 cname(victim), s, newdam);
1431 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1432 wu(0, pp->pln_own, "missed\n");
1433 if (mission == MI_SINTERDICT) {
1435 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1436 xyas(x, y, victim));
1437 } else if (*s == 's') {
1438 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1439 s, xyas(x, y, victim));
1441 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1442 s, xyas(x, y, victim));
1444 /* Now, even though we missed, the bombs
1446 collateral_damage(x, y, newdam, bombers);
1449 /* use up missiles */
1450 if (plp->pcp->pl_flags & P_M) {
1451 makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
1452 pp->pln_x, pp->pln_y);
1461 * Check to see if anyone hostile to the victim
1462 * is running an air defense mission on this
1463 * sector. If so, do air combat
1466 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1467 struct emp_qelem *esc_list)
1470 int mission_flags, tech, combat = 0, rel, dist, z;
1471 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1473 struct genlist *glp;
1475 struct genlist mi[MAXNOC];
1482 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1484 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1487 memset(mi, 0, sizeof(mi));
1488 for (z = 1; z < MAXNOC; z++)
1489 emp_initque((struct emp_qelem *)&mi[z]);
1491 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1493 for (cn = 1; cn < MAXNOC; cn++) {
1494 /* Check our relations */
1495 rel = getrel(getnatp(cn), victim);
1500 if (QEMPTY(&mi[cn].queue))
1503 /* Ok, make a list of all the interceptors. Note that this *copies* the
1504 * list from the mission creation. This list must be deleted later. */
1505 emp_initque(&interceptors);
1506 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1508 glp = (struct genlist *)qp;
1511 dist = mapdist(x, y, gp->x, gp->y);
1513 plp = malloc(sizeof(struct plist));
1514 memset(plp, 0, sizeof(struct plist));
1516 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1518 /* missiles go one way, so we can use all the range */
1519 if (!(plp->pcp->pl_flags & P_M))
1521 /* If it's out of range, free it and continue on */
1522 if (dist > plp->plane.pln_range) {
1526 emp_insque(&plp->queue, &interceptors);
1529 /* Remove those who cannot go */
1530 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1532 if (QEMPTY(&interceptors))
1535 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1536 * that the higher numbered planes go into battle (they should be the better ones
1537 * at fighting, if all went well.) */
1539 for (qp = interceptors.q_forw; qp != (&interceptors);
1542 tcount -= (count * 2);
1543 /* Just in case there are more incoming than we have */
1546 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1550 /* Free it up and continue */
1552 glp = (struct genlist *)qp;
1557 /* Now, make a list of all the airports these planes are coming from */
1559 for (qp = interceptors.q_forw; qp != (&interceptors);
1561 plp = (struct plist *)qp;
1562 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1563 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1566 /* Now, fly them out one airport at a time */
1567 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1568 struct airport *air;
1570 air = (struct airport *)qp;
1571 dist = mapdist(x, y, air->x, air->y);
1575 /* Split off the interceptors at this base into i */
1576 divide(&interceptors, &i, air->x, air->y);
1580 mission_flags |= P_X; /* stealth (shhh) */
1581 /* gets turned off if not all choppers */
1582 mission_flags |= P_H;
1583 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1584 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1586 /* Did we run out of interceptors? */
1589 /* Did we run out of bombers? */
1590 if (QEMPTY(bomb_list)) {
1591 /* Yes, so we have to put the rest of the interceptors back, and
1592 then continue, or we leak memory */
1597 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1598 mission_flags, &tech);
1600 /* Did we run out of interceptors? */
1603 /* Did we run out of bombers? */
1604 if (QEMPTY(bomb_list)) {
1605 /* Yes, so we have to put the rest of the interceptors back, and
1606 then continue, or we leak memory */
1611 path = BestAirPath(buf, air->x, air->y, x, y);
1612 if (CANT_HAPPEN(!path))
1614 wu(0, cn, "Flying %s mission from %s\n",
1615 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1616 if (air->own && (air->own != cn)) {
1617 wu(0, air->own, "%s is flying %s mission from %s\n",
1618 cname(cn), mission_name(MI_AIR_DEFENSE),
1619 xyas(air->x, air->y, air->own));
1622 /* Now, fly the planes to the sector */
1623 emp_initque(&empty);
1624 ac_encounter(&i, &empty, air->x, air->y,
1625 path, mission_flags, 1, bomb_list, esc_list);
1627 /* If none made it, continue */
1631 /* Some made it, so now they get to try to fight. */
1632 /* Intercept the escorts first */
1634 if (!QEMPTY(esc_list)) {
1635 mpr(victim, "%s air defense planes intercept!\n",
1637 ac_combat_headers(victim, cn);
1638 ac_airtoair(esc_list, &i);
1641 /* Now intercept the bombers */
1642 if (!QEMPTY(bomb_list)) {
1644 mpr(victim, "%s air defense planes intercept!\n",
1646 ac_combat_headers(victim, cn);
1648 ac_airtoair(bomb_list, &i);
1657 /* We have to free all of these, if they are still there, otherwise they get
1658 lost and we leak memory all over the place. */
1659 for (cn = 1; cn < MAXNOC; cn++) {
1660 /* free up all this memory if it's still there */
1661 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1663 glp = (struct genlist *)qp;