2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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
55 #include "prototypes.h"
59 struct emp_qelem queue;
64 static void add_airport(struct emp_qelem *, coord, coord);
65 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
67 static void build_mission_list(struct genlist *, coord, coord, int, natid);
68 static void build_mission_list_type(struct genlist *, coord, coord, int,
70 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
71 static int dosupport(struct genlist *, coord, coord, natid, natid);
72 static int find_airport(struct emp_qelem *, coord, coord);
73 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
74 int, struct ichrstr *, int, int, int *);
75 static void mission_pln_sel(struct emp_qelem *, int, int, int);
76 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
80 * Interdict commodities & transported planes
83 ground_interdict(coord x, coord y, natid victim, s_char *s)
86 int dam = 0, newdam, rel;
87 struct genlist mi[MAXNOC];
90 memset(mi, 0, sizeof(mi));
91 for (z = 1; z < MAXNOC; z++)
92 emp_initque((struct emp_qelem *)&mi[z]);
94 build_mission_list(mi, x, y, MI_INTERDICT, victim);
96 for (cn = 1; cn < MAXNOC; cn++) {
97 rel = getrel(getnatp(cn), victim);
101 if (QEMPTY(&mi[cn].queue))
104 newdam = perform_mission(x, y, victim, &mi[cn].queue,
105 MI_INTERDICT, s, SECT_HARDTARGET);
108 mpr(victim, "%s interdiction mission does %d damage!\n",
112 collateral_damage(x, y, dam, 0);
118 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
126 getsect(x, y, §);
128 coll = ldround((double)dam * collateral_dam, 1);
131 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
132 xyas(x, y, sect.sct_own), coll);
133 sectdamage(§, coll, list);
141 only_subs(struct emp_qelem *list)
143 struct emp_qelem *qp;
148 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
149 glp = (struct genlist *)qp;
150 gp = (struct genitem *)glp->thing;
152 if (glp->type != EF_SHIP)
154 mcp = (struct mchrstr *)glp->cp;
155 if (!(mcp->m_flags & M_SUB))
159 /* They were all subs! */
165 * Interdict ships & land units
169 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
174 struct genlist mi[MAXNOC];
178 memset(mi, 0, sizeof(mi));
179 for (z = 1; z < MAXNOC; z++)
180 emp_initque((struct emp_qelem *)&mi[z]);
182 build_mission_list(mi, x, y, mission, victim);
184 for (cn = 1; cn < MAXNOC; cn++) {
187 if (mission == MI_SINTERDICT) {
188 if (getrel(getnatp(cn), victim) >= FRIENDLY)
190 } else if (getrel(getnatp(cn), victim) > HOSTILE)
193 if (QEMPTY(&mi[cn].queue))
196 osubs = only_subs(&mi[cn].queue);
197 newdam = perform_mission(x, y, victim, &mi[cn].queue,
198 mission, s, hardtarget);
201 /* If only subs responded, then we don't know who's
205 "Enemy interdiction mission does %d damage!\n",
209 "%s interdiction mission does %d damage!\n",
215 collateral_damage(x, y, dam, 0);
221 * Perform a mission against victim, on behalf of actee
224 off_support(coord x, coord y, natid victim, natid actee)
227 struct genlist mi[MAXNOC];
230 memset(mi, 0, sizeof(mi));
231 for (z = 1; z < MAXNOC; z++)
232 emp_initque((struct emp_qelem *)&mi[z]);
234 build_mission_list(mi, x, y, MI_SUPPORT, victim);
235 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
237 dam = dosupport(mi, x, y, victim, actee);
242 * Perform a mission against victim, on behalf of actee
245 def_support(coord x, coord y, natid victim, natid actee)
248 struct genlist mi[MAXNOC];
251 memset(mi, 0, sizeof(mi));
252 for (z = 1; z < MAXNOC; z++)
253 emp_initque((struct emp_qelem *)&mi[z]);
255 build_mission_list(mi, x, y, MI_SUPPORT, victim);
256 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
258 dam = dosupport(mi, x, y, victim, actee);
263 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
269 for (cn = 1; cn < MAXNOC; cn++) {
270 rel = getrel(getnatp(cn), actee);
271 if ((cn != actee) && (rel != ALLIED))
273 rel = getrel(getnatp(cn), victim);
274 if ((cn != actee) && (rel != AT_WAR))
277 if (QEMPTY(&mi[cn].queue))
280 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
281 "", SECT_HARDTARGET);
287 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
290 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
291 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
292 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
296 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
297 int type, natid victim)
314 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
315 size = max(size,sizeof(struct plnstr));
316 block = (s_char *)malloc(size);
318 size = sizeof(u_block);
319 block = (s_char *)&u_block;
321 snxtitem_all(&ni, type);
322 while (nxtitem(&ni, block)) {
323 gp = (struct genitem *)block;
331 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
334 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
335 (gp->mission != MI_INTERDICT))
338 relat = getrel(getnatp(gp->own), victim);
339 if (mission == MI_SINTERDICT) {
340 if (relat >= FRIENDLY)
342 else if (type != EF_PLANE && relat > HOSTILE)
344 } else if (relat > HOSTILE)
347 dist = mapdist(x, y, gp->opx, gp->opy);
350 if (mission != MI_RESERVE) /* XXX */
351 oprange(gp, type, &radius);
356 /* Ok, it is within the operations range. */
357 /* Now check from where the object actually is */
358 dist = mapdist(x, y, gp->x, gp->y);
360 oprange(gp, type, &radius);
363 /* Ok, the object can get to where the x,y is */
366 if (mission != MI_AIR_DEFENSE) {
367 getsect(x, y, (s_char *)§);
368 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
371 * If the player->owner of the unit isn't at war
372 * with the victim, and doesn't own the
373 * sect being acted upon, and isn't the
374 * old player->owner of that sect, bounce them.
376 if (sect.sct_type != SCT_WATER &&
377 sect.sct_own != gp->own &&
378 sect.sct_oldown != gp->own)
384 glp = (struct genlist *)malloc(sizeof(struct genlist));
385 memset(glp, 0, sizeof(struct genlist));
391 glp->cp = (s_char *)&lchr[(int)gp->type];
394 glp->cp = (s_char *)&mchr[(int)gp->type];
397 glp->cp = (s_char *)&plchr[(int)gp->type];
400 glp->thing = (s_char *)malloc(size);
401 memcpy(glp->thing, block, size);
402 emp_insque(&glp->queue, &mi[gp->own].queue);
407 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
414 snxtitem_all(&ni, EF_PLANE);
415 while (nxtitem(&ni, (s_char *)&plane)) {
416 if (plane.pln_own != cn)
419 if (plane.pln_mission != MI_ESCORT)
422 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
424 if (dist > ((int)((float)plane.pln_range / 2.0)))
427 plp = (struct plist *)malloc(sizeof(struct plist));
428 memset(plp, 0, sizeof(struct plist));
429 plp->pcp = &plchr[(int)plane.pln_type];
431 emp_insque(&plp->queue, escorts);
436 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
437 int mission, s_char *s, int hardtarget)
439 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
440 struct emp_qelem *newqp;
449 struct plchrstr *pcp;
450 int dam = 0, dam2, mission_flags, tech;
451 natid plane_owner = 0;
452 int gun, shell, md, air_dam = 0;
453 double range2, prb, range, mobcost, hitchance;
455 getsect(x, y, §);
457 emp_initque(&missiles);
458 emp_initque(&bombers);
459 emp_initque(&escorts);
462 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
463 glp = (struct genlist *)qp;
464 gp = (struct genitem *)glp->thing;
466 md = mapdist(x, y, gp->x, gp->y);
468 if (glp->type == EF_LAND) {
469 lp = (struct lndstr *)glp->thing;
470 lcp = (struct lchrstr *)glp->cp;
472 if (lp->lnd_effic < LAND_MINFIREEFF)
475 if (mission == MI_SINTERDICT)
478 if ((mission == MI_INTERDICT) &&
479 (md > land_max_interdiction_range))
482 if (md > (lp->lnd_frg / 2))
485 if (lnd_getmil(lp) < 1)
488 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
489 range2 = (double)roundrange(range);
494 shell = lp->lnd_item[I_SHELL];
495 gun = lp->lnd_item[I_GUN];
496 if (shell == 0 || gun == 0)
499 if (has_supply(lp)) {
501 putland(lp->lnd_uid, lp);
502 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
503 lp->lnd_ammo, shell), 1);
504 if (sect.sct_type == SCT_WATER) {
505 double dam3 = (double)dam2;
506 if (chance(((double)lp->lnd_acc) / 100.0))
507 dam2 = ldround((dam3 / 2.0), 1);
510 if (sect.sct_type == SCT_WATER)
511 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
513 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
515 "%s fires at %s %s at %s\n",
516 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
518 mpr(victim, "%s %s fires at you at %s\n",
519 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
521 } else if (glp->type == EF_SHIP) {
522 sp = (struct shpstr *)glp->thing;
523 mcp = (struct mchrstr *)glp->cp;
525 if (sp->shp_effic < 60)
527 if (sp->shp_frnge == 0)
529 if (((mission == MI_INTERDICT) ||
530 (mission == MI_SINTERDICT)) &&
531 (md > ship_max_interdiction_range))
533 if (sp->shp_item[I_MILIT] < 1)
536 if ((mcp->m_flags & M_SUB) &&
537 (sect.sct_type != SCT_WATER))
540 if (mission == MI_SINTERDICT) {
541 if (!(mcp->m_flags & M_SONAR))
543 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
545 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
546 range2 *= (double)sp->shp_effic / 200.0;
549 /* can't look all the time */
553 if (mcp->m_flags & M_SUB) {
554 /* If we aren't shooting at "subs" or "ships" don't fire at all from
558 if (sp->shp_mobil < (s_char)0)
560 gun = sp->shp_item[I_GUN];
563 shell = sp->shp_item[I_SHELL];
564 if (shell < SHP_TORP_SHELLS)
565 shell += supply_commod(sp->shp_own,
566 sp->shp_x, sp->shp_y, I_SHELL,
567 SHP_TORP_SHELLS - shell);
568 if (shell < SHP_TORP_SHELLS)
571 range = sp->shp_effic * techfact(sp->shp_tech,
572 ((double)sp->shp_frnge)) /
575 range2 = (double)roundrange(range);
579 if (!line_of_sight((s_char **)0, x, y, gp->x, gp->y))
581 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
582 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
583 mobcost = (480.0 / (mobcost +
584 techfact(sp->shp_tech, mobcost)));
585 sp->shp_mobil -= mobcost;
586 putship(sp->shp_uid, sp);
587 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
590 "%s locking on %s %s in %s\n",
591 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
593 "\tEffective torpedo range is %.1f\n", range);
595 "\tWhooosh... Hitchance = %d%%\n",
596 (int)(hitchance * 100));
598 if (hitchance < 1.0 && !chance(hitchance)) {
599 wu(0, sp->shp_own, "\tMissed\n");
601 "Incoming torpedo sighted @ %s missed (whew)!\n",
605 wu(0, sp->shp_own, "\tBOOM!...\n");
606 dam2 = TORP_DAMAGE();
609 nreport(victim, N_TORP_SHIP, sp->shp_own, 1);
611 "\tTorpedo hit %s %s for %d damage\n",
612 cname(victim), s, dam2);
615 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
616 xyas(x, y, victim), dam2);
618 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
619 range2 = (double)roundrange(range);
622 gun = sp->shp_item[I_GUN];
623 gun = min(gun, sp->shp_glim);
624 shell = sp->shp_item[I_SHELL];
626 shell += supply_commod(sp->shp_own,
627 sp->shp_x, sp->shp_y, I_SHELL,
629 gun = min(gun, shell);
630 gun = min(gun, sp->shp_item[I_MILIT] / 2.0);
634 dam2 = seagun(sp->shp_effic, gun);
638 prb = ((double)md) / range2;
641 dam2 = (int)((float)dam2 / 2.0);
643 if (sect.sct_type == SCT_WATER)
644 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
646 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
648 "%s fires at %s %s at %s\n",
649 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
651 mpr(victim, "%s %s fires at you at %s\n",
652 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
654 sp->shp_item[I_SHELL] = shell - gun;
655 putship(sp->shp_uid, sp);
657 } else if (glp->type == EF_PLANE) {
658 pcp = (struct plchrstr *)glp->cp;
659 if (pcp->pl_flags & P_M)
660 /* units have their own missile interdiction */
661 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
664 /* save planes for later */
665 plp = (struct plist *)malloc(sizeof(struct plist));
667 memset(plp, 0, sizeof(struct plist));
669 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
670 if (plp->pcp->pl_flags & P_M)
671 emp_insque(&plp->queue, &missiles);
673 emp_insque(&plp->queue, &bombers);
674 plane_owner = plp->plane.pln_own;
677 if (!QEMPTY(&missiles)) {
678 /* I arbitrarily chose 100 mindam -KHS */
680 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
681 "sector", victim, mission);
682 qp = missiles.q_forw;
683 while (qp != (&missiles)) {
691 if (QEMPTY(&bombers)) {
694 glp = (struct genlist *)qp;
703 * If there are planes performing an
704 * interdict or support mission, find
705 * some escorts for them, if possible.
706 * Up to 2 per bomber, if possible.
708 find_escorts(x, y, plane_owner, &escorts);
710 if (mission == MI_SINTERDICT)
711 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
713 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
715 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
717 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
718 plp = (struct plist *)qp;
719 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
720 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
723 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
727 air = (struct airport *)qp;
728 md = mapdist(x, y, air->x, air->y);
733 /* Split off the bombers at this base into b */
734 divide(&bombers, &b, air->x, air->y);
736 /* Split off the escorts at this base into e */
737 divide(&escorts, &e, air->x, air->y);
741 mission_flags |= P_X; /* stealth (shhh) */
742 mission_flags |= P_H; /* gets turned off if not all choppers */
744 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
745 0, mission_flags, &tech);
751 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
752 P_F | P_ESC, mission_flags, &tech);
754 BestAirPath(pp, air->x, air->y, x, y);
755 wu(0, plane_owner, "Flying %s mission from %s\n",
756 mission_name(mission), xyas(air->x, air->y, plane_owner));
757 if (air->own && (air->own != plane_owner)) {
758 wu(0, air->own, "%s is flying %s mission from %s\n",
759 cname(plane_owner), mission_name(mission),
760 xyas(air->x, air->y, air->own));
763 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
767 air_damage(&b, x, y, mission, victim, s, hardtarget);
775 if (sect.sct_type == SCT_WATER)
776 nreport(plane_owner, N_SHP_BOMB, victim, 1);
778 nreport(plane_owner, N_SCT_BOMB, victim, 1);
781 /* free up all this memory */
784 glp = (struct genlist *)qp;
792 while (qp != (&escorts)) {
800 while (qp != (&bombers)) {
811 cando(int mission, int type)
815 if (type == EF_PLANE)
819 if (type == EF_PLANE)
823 if ((type == EF_PLANE) || (type == EF_SHIP))
831 if (type == EF_PLANE)
844 nameofitem(struct genitem *gp, int type)
848 return prship((struct shpstr *)gp);
850 return prplane((struct plnstr *)gp);
852 return prland((struct lndstr *)gp);
858 mission_short_name(int mission)
883 mission_name(short int mission)
887 return "an interdiction";
891 return "a offensive support";
893 return "a defensive support";
899 return "a sub interdiction";
901 return "an air defense";
903 return "a mysterious";
907 show_mission(int type, struct nstr_item *np)
909 int size, first = 1, radius;
913 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
914 size = max(size, sizeof(struct shpstr));
915 block = (s_char *)malloc(size);
917 while (nxtitem(np, block)) {
918 gp = (struct genitem *)block;
919 if (!player->owner || gp->own == 0)
923 pr("Thing x,y op-sect rad mission\n");
926 pr("%-25s", nameofitem(gp, type));
927 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
928 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
929 gp->mission == MI_OSUPPORT ||
930 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
932 oprange(gp, type, &radius);
933 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
934 if (radius < gp->radius)
937 pr(" %4d", gp->radius);
938 } else if (gp->mission == MI_RESERVE) {
942 getsect(gp->x, gp->y, §);
943 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
946 if (((struct lndstr *)block)->lnd_rad_max == 0)
949 plus += ((struct lndstr *)block)->lnd_rad_max;
950 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
952 } else if (gp->mission == MI_ESCORT) {
955 ((float)((struct plnstr *)block)->pln_range / 2.0)
960 pr(" is on %s mission\n", mission_name(gp->mission));
962 pr(" has no mission.\n");
967 oprange(struct genitem *gp, int type, int *radius)
976 getship(gp->uid, &ship);
977 range = ldround(techfact(gp->tech,
978 (double)ship.shp_frnge / 2.0), 1);
981 getland(gp->uid, &land);
982 range = ldround(techfact((int)land.lnd_tech,
983 (double)land.lnd_frg / 2.0), 1);
986 getplane(gp->uid, &plane);
987 /* missiles go one way, so we can use all the range */
988 if (plchr[(int)plane.pln_type].pl_flags & P_M)
989 range = plane.pln_range;
991 range = ldround((double)plane.pln_range / 2.0, 1);;
995 if ((*radius) > range)
1002 * Remove all planes who cannot go on
1003 * the mission from the plane list.
1006 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1009 struct emp_qelem *qp, *next;
1014 struct plchrstr *pcp;
1019 for (qp = list->q_forw; qp != list; qp = next) {
1021 plp = (struct plist *)qp;
1025 if (pp->pln_effic < 40) {
1031 if (pp->pln_mobil < 1) {
1038 if (ontradingblock(EF_PLANE, (int *)pp)) {
1048 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1050 if ((wantflags & y) == y)
1051 if ((pcp->pl_flags & y) != y) {
1073 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1077 if ((pcp->pl_flags & P_E) ||
1078 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1090 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1092 if ((nowantflags & y) == y)
1093 if ((pcp->pl_flags & y) == y)
1102 if (pp->pln_ship >= 0) {
1103 if (!getship(pp->pln_ship, &ship)) {
1106 putplane(pp->pln_uid, pp);
1111 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1114 if (ship.shp_effic < SHIP_MINEFF) {
1117 /* Can't fly off of inefficient or non-owned, non-allied ships */
1118 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1119 ((ship.shp_own != pp->pln_own) &&
1120 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1126 if (pp->pln_land >= 0) {
1127 if (!getland(pp->pln_land, &land)) {
1130 putplane(pp->pln_uid, pp);
1135 if (!(pcp->pl_flags & P_E))
1137 if (land.lnd_effic < LAND_MINEFF)
1140 /* Can't fly off of inefficient or non-owned, non-allied units */
1141 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1142 ((land.lnd_own != pp->pln_own) &&
1143 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1149 /* Can't fly off units in ships or other units */
1150 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1156 /* Now, check the sector status if not on a plane or unit */
1157 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1158 /* If we can't get the sector, we can't check it, and can't fly */
1159 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1164 /* First, check allied status */
1165 /* Can't fly from non-owned sectors or non-allied sectors */
1166 if ((sect.sct_own != pp->pln_own) &&
1167 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1172 /* non-vtol plane */
1173 if ((pcp->pl_flags & P_V) == 0) {
1174 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1181 if (pcp->pl_flags & P_A) {
1182 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1189 putplane(pp->pln_uid, pp);
1194 * Arm only the planes at x,y
1198 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1199 int mission, struct ichrstr *ip, int flags,
1200 int mission_flags, int *tech)
1202 struct emp_qelem *qp;
1203 struct emp_qelem *next;
1208 for (qp = list->q_forw; qp != list; qp = next) {
1210 plp = (struct plist *)qp;
1212 if (plp->plane.pln_x != x)
1214 if (plp->plane.pln_y != y)
1217 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1222 if (flags & (P_S | P_I)) {
1223 if (plp->pcp->pl_flags & P_S)
1224 mission_flags |= P_S;
1225 if (plp->pcp->pl_flags & P_I)
1226 mission_flags |= P_I;
1228 if (*tech > plp->plane.pln_tech)
1229 *tech = plp->plane.pln_tech;
1230 if (!(plp->pcp->pl_flags & P_H))
1231 /* no stealth on this mission */
1232 mission_flags &= ~P_H;
1233 if (!(plp->pcp->pl_flags & P_X))
1234 /* no stealth on this mission */
1235 mission_flags &= ~P_X;
1236 if (!(plp->pcp->pl_flags & P_A)) {
1237 /* no asw on this mission */
1238 mission_flags &= ~P_A;
1240 if (!(plp->pcp->pl_flags & P_MINE)) {
1241 /* no asw on this mission */
1242 mission_flags &= ~P_MINE;
1246 * Mob costs for missions are 1/2 normal
1249 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1250 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1253 return mission_flags;
1257 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1260 struct plchrstr *pcp;
1272 if (pp->pln_ship >= 0) {
1273 getship(pp->pln_ship, &ship);
1274 item = ship.shp_item;
1275 } else if (pp->pln_land >= 0) {
1276 getland(pp->pln_land, &land);
1277 item = land.lnd_item;
1279 getsect(pp->pln_x, pp->pln_y, §);
1280 item = sect.sct_item;
1282 if (pcp->pl_fuel > item[I_PETROL]) {
1285 item[I_PETROL] -= pcp->pl_fuel;
1287 if (!(flags & P_F)) {
1293 if (pp->pln_nuketype == -1) {
1295 needed = pp->pln_load;
1299 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1301 itype = ip->i_vtype;
1302 needed = (pp->pln_load * 2) / ip->i_lbs;
1305 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1307 itype = ip->i_vtype;
1308 needed = (pp->pln_load * 2) / ip->i_lbs;
1311 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1314 needed = pp->pln_load / ip->i_lbs;
1317 if (pp->pln_nuketype == -1)
1320 case 'i': /* missile interception */
1323 needed = pp->pln_load;
1329 if (rval < 0 || (itype && needed <= 0)) {
1332 if (item[itype] < needed && (itype == I_SHELL))
1333 item[itype] += supply_commod(plp->plane.pln_own,
1334 plp->plane.pln_x, plp->plane.pln_y,
1336 if (item[itype] < needed) {
1339 item[itype] -= needed;
1341 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1342 plp->bombs = needed;
1346 if (pp->pln_ship >= 0)
1347 putship(ship.shp_uid, &ship);
1348 else if (pp->pln_land >= 0)
1349 putland(land.lnd_uid, &land);
1356 * Return 1 if this x,y pair is in the list
1359 find_airport(struct emp_qelem *airp, coord x, coord y)
1361 struct emp_qelem *qp;
1364 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1365 a = (struct airport *)qp;
1366 if ((a->x == x) && (a->y == y))
1373 /* #*# This needs to be changed to include acc's -KHS */
1375 add_airport(struct emp_qelem *airp, coord x, coord y)
1380 a = (struct airport *)malloc(sizeof(struct airport));
1384 getsect(x, y, §);
1385 a->own = sect.sct_own;
1387 emp_insque((struct emp_qelem *)a, airp);
1391 * Take all the planes in list 1 that
1392 * are at x,y, and put them into list 2.
1395 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1397 struct emp_qelem *qp, *next;
1400 for (qp = l1->q_forw; qp != l1; qp = next) {
1402 plp = (struct plist *)qp;
1404 if (plp->plane.pln_x != x)
1406 if (plp->plane.pln_y != y)
1415 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1416 natid victim, s_char *s, int hardtarget)
1418 struct emp_qelem *qp;
1421 int newdam, dam = 0;
1425 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1426 plp = (struct plist *)qp;
1429 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1436 if (plp->pcp->pl_flags & P_A) {
1437 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1439 "\t%s detects sub movement in %s\n",
1440 prplane(pp), xyas(x, y, pp->pln_own));
1443 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1445 "\t%s tracks %s %s at %s\n",
1446 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1450 "\t%s depth-charging %s %s in %s\n",
1451 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1454 "\t%s pinbombing %s %s in %s\n",
1455 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1457 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1458 if (plp->plane.pln_nuketype != -1)
1460 else if (hardtarget != SECT_HARDTARGET)
1461 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1462 /* Always calculate damage */
1463 if (roll(100) <= hitchance) {
1464 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1466 if (mission == MI_INTERDICT) {
1468 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1469 prplane(pp), nukedam, cname(victim), s);
1474 "\t\thit %s %s for %d damage\n",
1475 cname(victim), s, newdam);
1479 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1480 wu(0, pp->pln_own, "missed\n");
1481 if (mission == MI_SINTERDICT) {
1483 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1484 xyas(x, y, victim));
1485 } else if (*s == 's') {
1487 "SPLASH! Bombs miss your %s in %s\n",
1488 s, xyas(x, y, victim));
1490 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1491 s, xyas(x, y, victim));
1493 /* Now, even though we missed, the bombs
1495 collateral_damage(x, y, newdam, bombers);
1498 /* use up missiles */
1499 if (plp->pcp->pl_flags & P_M) {
1500 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1510 * Check to see if anyone hostile to the victim
1511 * is running an air defense mission on this
1512 * sector. If so, do air combat
1515 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1516 struct emp_qelem *esc_list)
1519 int mission_flags, tech, combat = 0, rel, dist, z;
1520 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1522 struct genlist *glp;
1524 struct genlist mi[MAXNOC];
1530 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1532 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1535 memset(mi, 0, sizeof(mi));
1536 for (z = 1; z < MAXNOC; z++)
1537 emp_initque((struct emp_qelem *)&mi[z]);
1539 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1541 for (cn = 1; cn < MAXNOC; cn++) {
1542 /* Check our relations */
1543 rel = getrel(getnatp(cn), victim);
1548 if (QEMPTY(&mi[cn].queue))
1551 /* Ok, make a list of all the interceptors. Note that this *copies* the
1552 * list from the mission creation. This list must be deleted later. */
1553 emp_initque(&interceptors);
1554 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1557 glp = (struct genlist *)qp;
1558 gp = (struct genitem *)glp->thing;
1559 plp = (struct plist *)qp;
1561 dist = mapdist(x, y, gp->x, gp->y);
1563 plp = (struct plist *)malloc(sizeof(struct plist));
1564 memset(plp, 0, sizeof(struct plist));
1565 plp->pcp = (struct plchrstr *)glp->cp;
1566 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1568 /* missiles go one way, so we can use all the range */
1569 if (!(plp->pcp->pl_flags & P_M))
1571 /* If it's out of range, free it and continue on */
1572 if (dist > plp->plane.pln_range) {
1576 emp_insque(&plp->queue, &interceptors);
1579 /* Remove those who cannot go */
1580 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1582 if (QEMPTY(&interceptors))
1585 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1586 * that the higher numbered planes go into battle (they should be the better ones
1587 * at fighting, if all went well.) */
1589 for (qp = interceptors.q_forw; qp != (&interceptors);
1592 tcount -= (count * 2);
1593 /* Just in case there are more incoming than we have */
1596 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1600 /* Free it up and continue */
1602 glp = (struct genlist *)qp;
1603 free((s_char *)glp);
1607 /* Now, make a list of all the airports these planes are coming from */
1609 for (qp = interceptors.q_forw; qp != (&interceptors);
1611 plp = (struct plist *)qp;
1612 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1613 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1616 /* Now, fly them out one airport at a time */
1617 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1618 struct airport *air;
1620 air = (struct airport *)qp;
1621 dist = mapdist(x, y, air->x, air->y);
1625 /* Split off the interceptors at this base into i */
1626 divide(&interceptors, &i, air->x, air->y);
1630 mission_flags |= P_X; /* stealth (shhh) */
1631 /* gets turned off if not all choppers */
1632 mission_flags |= P_H;
1633 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1634 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1636 /* Did we run out of interceptors? */
1639 /* Did we run out of bombers? */
1640 if (QEMPTY(bomb_list)) {
1641 /* Yes, so we have to put the rest of the interceptors back, and
1642 then continue, or we leak memory */
1647 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1648 mission_flags, &tech);
1650 /* Did we run out of interceptors? */
1653 /* Did we run out of bombers? */
1654 if (QEMPTY(bomb_list)) {
1655 /* Yes, so we have to put the rest of the interceptors back, and
1656 then continue, or we leak memory */
1661 BestAirPath(path, air->x, air->y, x, y);
1662 wu(0, cn, "Flying %s mission from %s\n",
1663 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1664 if (air->own && (air->own != cn)) {
1665 wu(0, air->own, "%s is flying %s mission from %s\n",
1666 cname(cn), mission_name(MI_AIR_DEFENSE),
1667 xyas(air->x, air->y, air->own));
1670 /* Now, fly the planes to the sector */
1671 emp_initque(&empty);
1672 ac_encounter(&i, &empty, air->x, air->y,
1673 path, mission_flags, 1, bomb_list, esc_list);
1675 /* If none made it, continue */
1679 /* Some made it, so now they get to try to fight. */
1680 /* Intercept the escorts first */
1682 if (!QEMPTY(esc_list)) {
1683 mpr(victim, "%s air defense planes intercept!\n",
1685 ac_combat_headers(victim, cn);
1686 ac_airtoair(esc_list, &i, air->own);
1689 /* Now intercept the bombers */
1690 if (!QEMPTY(bomb_list)) {
1692 mpr(victim, "%s air defense planes intercept!\n",
1694 ac_combat_headers(victim, cn);
1696 ac_airtoair(bomb_list, &i, air->own);
1705 /* We have to free all of these, if they are still there, otherwise they get
1706 lost and we leak memory all over the place. */
1707 for (cn = 1; cn < MAXNOC; cn++) {
1708 /* free up all this memory if it's still there */
1709 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1711 glp = (struct genlist *)qp;
1713 free((s_char *)glp);