2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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
53 #include "prototypes.h"
57 struct emp_qelem queue;
62 static void add_airport(struct emp_qelem *, coord, coord);
63 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
65 static void build_mission_list(struct genlist *, coord, coord, int, natid);
66 static void build_mission_list_type(struct genlist *, coord, coord, int,
68 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
69 static int dosupport(struct genlist *, coord, coord, natid, natid);
70 static int find_airport(struct emp_qelem *, coord, coord);
71 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
72 int, struct ichrstr *, int, int, int *);
73 static void mission_pln_sel(struct emp_qelem *, int, int, int);
74 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
78 * Interdict commodities & transported planes
81 ground_interdict(coord x, coord y, natid victim, s_char *s)
84 int dam = 0, newdam, rel;
85 struct genlist mi[MAXNOC];
88 memset(mi, 0, sizeof(mi));
89 for (z = 1; z < MAXNOC; z++)
90 emp_initque((struct emp_qelem *)&mi[z]);
92 build_mission_list(mi, x, y, MI_INTERDICT, victim);
94 for (cn = 1; cn < MAXNOC; cn++) {
95 rel = getrel(getnatp(cn), victim);
99 if (QEMPTY(&mi[cn].queue))
102 newdam = perform_mission(x, y, victim, &mi[cn].queue,
103 MI_INTERDICT, s, SECT_HARDTARGET);
106 mpr(victim, "%s interdiction mission does %d damage!\n",
110 collateral_damage(x, y, dam, 0);
116 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
124 getsect(x, y, §);
126 coll = ldround((double)dam * collateral_dam, 1);
129 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
130 xyas(x, y, sect.sct_own), coll);
131 sectdamage(§, coll, list);
139 only_subs(struct emp_qelem *list)
141 struct emp_qelem *qp;
145 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
146 glp = (struct genlist *)qp;
148 if (glp->type != EF_SHIP)
150 mcp = (struct mchrstr *)glp->cp;
151 if (!(mcp->m_flags & M_SUB))
155 /* They were all subs! */
161 * Interdict ships & land units
165 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
170 struct genlist mi[MAXNOC];
174 memset(mi, 0, sizeof(mi));
175 for (z = 1; z < MAXNOC; z++)
176 emp_initque((struct emp_qelem *)&mi[z]);
178 build_mission_list(mi, x, y, mission, victim);
180 for (cn = 1; cn < MAXNOC; cn++) {
183 if (mission == MI_SINTERDICT) {
184 if (getrel(getnatp(cn), victim) >= FRIENDLY)
186 } else if (getrel(getnatp(cn), victim) > HOSTILE)
189 if (QEMPTY(&mi[cn].queue))
192 osubs = only_subs(&mi[cn].queue);
193 newdam = perform_mission(x, y, victim, &mi[cn].queue,
194 mission, s, hardtarget);
197 /* If only subs responded, then we don't know who's
201 "Enemy interdiction mission does %d damage!\n",
205 "%s interdiction mission does %d damage!\n",
211 collateral_damage(x, y, dam, 0);
217 * Perform a mission against victim, on behalf of actee
220 off_support(coord x, coord y, natid victim, natid actee)
223 struct genlist mi[MAXNOC];
226 memset(mi, 0, sizeof(mi));
227 for (z = 1; z < MAXNOC; z++)
228 emp_initque((struct emp_qelem *)&mi[z]);
230 build_mission_list(mi, x, y, MI_SUPPORT, victim);
231 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
233 dam = dosupport(mi, x, y, victim, actee);
238 * Perform a mission against victim, on behalf of actee
241 def_support(coord x, coord y, natid victim, natid actee)
244 struct genlist mi[MAXNOC];
247 memset(mi, 0, sizeof(mi));
248 for (z = 1; z < MAXNOC; z++)
249 emp_initque((struct emp_qelem *)&mi[z]);
251 build_mission_list(mi, x, y, MI_SUPPORT, victim);
252 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
254 dam = dosupport(mi, x, y, victim, actee);
259 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
265 for (cn = 1; cn < MAXNOC; cn++) {
266 rel = getrel(getnatp(cn), actee);
267 if ((cn != actee) && (rel != ALLIED))
269 rel = getrel(getnatp(cn), victim);
270 if ((cn != actee) && (rel != AT_WAR))
273 if (QEMPTY(&mi[cn].queue))
276 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
277 "", SECT_HARDTARGET);
283 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
286 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
287 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
288 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
292 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
293 int type, natid victim)
310 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
311 size = max(size,sizeof(struct plnstr));
312 block = (s_char *)malloc(size);
314 size = sizeof(u_block);
315 block = (s_char *)&u_block;
317 snxtitem_all(&ni, type);
318 while (nxtitem(&ni, block)) {
319 gp = (struct genitem *)block;
327 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
330 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
331 (gp->mission != MI_INTERDICT))
334 relat = getrel(getnatp(gp->own), victim);
335 if (mission == MI_SINTERDICT) {
336 if (relat >= FRIENDLY)
338 else if (type != EF_PLANE && relat > HOSTILE)
340 } else if (relat > HOSTILE)
343 dist = mapdist(x, y, gp->opx, gp->opy);
346 if (mission != MI_RESERVE) /* XXX */
347 oprange(gp, type, &radius);
352 /* Ok, it is within the operations range. */
353 /* Now check from where the object actually is */
354 dist = mapdist(x, y, gp->x, gp->y);
356 oprange(gp, type, &radius);
359 /* Ok, the object can get to where the x,y is */
362 if (mission != MI_AIR_DEFENSE) {
363 getsect(x, y, (s_char *)§);
364 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
367 * If the player->owner of the unit isn't at war
368 * with the victim, and doesn't own the
369 * sect being acted upon, and isn't the
370 * old player->owner of that sect, bounce them.
372 if (sect.sct_type != SCT_WATER &&
373 sect.sct_own != gp->own &&
374 sect.sct_oldown != gp->own)
380 glp = (struct genlist *)malloc(sizeof(struct genlist));
381 memset(glp, 0, sizeof(struct genlist));
387 glp->cp = (s_char *)&lchr[(int)gp->type];
390 glp->cp = (s_char *)&mchr[(int)gp->type];
393 glp->cp = (s_char *)&plchr[(int)gp->type];
396 glp->thing = (s_char *)malloc(size);
397 memcpy(glp->thing, block, size);
398 emp_insque(&glp->queue, &mi[gp->own].queue);
403 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
410 snxtitem_all(&ni, EF_PLANE);
411 while (nxtitem(&ni, (s_char *)&plane)) {
412 if (plane.pln_own != cn)
415 if (plane.pln_mission != MI_ESCORT)
418 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
420 if (dist > ((int)((float)plane.pln_range / 2.0)))
423 plp = (struct plist *)malloc(sizeof(struct plist));
424 memset(plp, 0, sizeof(struct plist));
425 plp->pcp = &plchr[(int)plane.pln_type];
427 emp_insque(&plp->queue, escorts);
432 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
433 int mission, s_char *s, int hardtarget)
435 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
436 struct emp_qelem *newqp;
444 struct plchrstr *pcp;
445 int dam = 0, dam2, mission_flags, tech;
446 natid plane_owner = 0;
447 int gun, shell, md, air_dam = 0;
448 double range2, prb, range, mobcost, hitchance;
450 getsect(x, y, §);
452 emp_initque(&missiles);
453 emp_initque(&bombers);
454 emp_initque(&escorts);
457 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
458 glp = (struct genlist *)qp;
459 gp = (struct genitem *)glp->thing;
461 md = mapdist(x, y, gp->x, gp->y);
463 if (glp->type == EF_LAND) {
464 lp = (struct lndstr *)glp->thing;
466 if (lp->lnd_effic < LAND_MINFIREEFF)
469 if (mission == MI_SINTERDICT)
472 if ((mission == MI_INTERDICT) &&
473 (md > land_max_interdiction_range))
476 if (md > (lp->lnd_frg / 2))
479 if (lnd_getmil(lp) < 1)
482 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
483 range2 = (double)roundrange(range);
488 shell = lp->lnd_item[I_SHELL];
489 gun = lp->lnd_item[I_GUN];
490 if (shell == 0 || gun == 0)
493 if (has_supply(lp)) {
495 putland(lp->lnd_uid, lp);
496 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
497 lp->lnd_ammo, shell), 1);
498 if (sect.sct_type == SCT_WATER) {
499 double dam3 = (double)dam2;
500 if (chance(((double)lp->lnd_acc) / 100.0))
501 dam2 = ldround((dam3 / 2.0), 1);
504 if (sect.sct_type == SCT_WATER)
505 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
507 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
509 "%s fires at %s %s at %s\n",
510 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
512 mpr(victim, "%s %s fires at you at %s\n",
513 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
515 } else if (glp->type == EF_SHIP) {
516 sp = (struct shpstr *)glp->thing;
517 mcp = (struct mchrstr *)glp->cp;
519 if (sp->shp_effic < 60)
521 if (sp->shp_frnge == 0)
523 if (((mission == MI_INTERDICT) ||
524 (mission == MI_SINTERDICT)) &&
525 (md > ship_max_interdiction_range))
527 if (sp->shp_item[I_MILIT] < 1)
530 if ((mcp->m_flags & M_SUB) &&
531 (sect.sct_type != SCT_WATER))
534 if (mission == MI_SINTERDICT) {
535 if (!(mcp->m_flags & M_SONAR))
537 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
539 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
540 range2 *= (double)sp->shp_effic / 200.0;
543 /* can't look all the time */
547 if (mcp->m_flags & M_SUB) {
548 /* If we aren't shooting at "subs" or "ships" don't fire at all from
552 if (sp->shp_mobil < (s_char)0)
554 gun = sp->shp_item[I_GUN];
557 shell = sp->shp_item[I_SHELL];
558 if (shell < SHP_TORP_SHELLS)
559 shell += supply_commod(sp->shp_own,
560 sp->shp_x, sp->shp_y, I_SHELL,
561 SHP_TORP_SHELLS - shell);
562 if (shell < SHP_TORP_SHELLS)
565 range = sp->shp_effic * techfact(sp->shp_tech,
566 ((double)sp->shp_frnge)) /
569 range2 = (double)roundrange(range);
573 if (!line_of_sight((s_char **)0, x, y, gp->x, gp->y))
575 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
576 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
577 mobcost = (480.0 / (mobcost +
578 techfact(sp->shp_tech, mobcost)));
579 sp->shp_mobil -= mobcost;
580 putship(sp->shp_uid, sp);
581 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
584 "%s locking on %s %s in %s\n",
585 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
587 "\tEffective torpedo range is %.1f\n", range);
589 "\tWhooosh... Hitchance = %d%%\n",
590 (int)(hitchance * 100));
592 if (hitchance < 1.0 && !chance(hitchance)) {
593 wu(0, sp->shp_own, "\tMissed\n");
595 "Incoming torpedo sighted @ %s missed (whew)!\n",
599 wu(0, sp->shp_own, "\tBOOM!...\n");
600 dam2 = TORP_DAMAGE();
603 nreport(victim, N_TORP_SHIP, sp->shp_own, 1);
605 "\tTorpedo hit %s %s for %d damage\n",
606 cname(victim), s, dam2);
609 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
610 xyas(x, y, victim), dam2);
612 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
613 range2 = (double)roundrange(range);
616 gun = sp->shp_item[I_GUN];
617 gun = min(gun, sp->shp_glim);
618 shell = sp->shp_item[I_SHELL];
620 shell += supply_commod(sp->shp_own,
621 sp->shp_x, sp->shp_y, I_SHELL,
623 gun = min(gun, shell);
624 gun = min(gun, sp->shp_item[I_MILIT] / 2.0);
628 dam2 = seagun(sp->shp_effic, gun);
632 prb = ((double)md) / range2;
635 dam2 = (int)((float)dam2 / 2.0);
637 if (sect.sct_type == SCT_WATER)
638 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
640 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
642 "%s fires at %s %s at %s\n",
643 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
645 mpr(victim, "%s %s fires at you at %s\n",
646 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
648 sp->shp_item[I_SHELL] = shell - gun;
649 putship(sp->shp_uid, sp);
651 } else if (glp->type == EF_PLANE) {
652 pcp = (struct plchrstr *)glp->cp;
653 if (pcp->pl_flags & P_M)
654 /* units have their own missile interdiction */
655 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
658 /* save planes for later */
659 plp = (struct plist *)malloc(sizeof(struct plist));
661 memset(plp, 0, sizeof(struct plist));
663 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
664 if (plp->pcp->pl_flags & P_M)
665 emp_insque(&plp->queue, &missiles);
667 emp_insque(&plp->queue, &bombers);
668 plane_owner = plp->plane.pln_own;
671 if (!QEMPTY(&missiles)) {
672 /* I arbitrarily chose 100 mindam -KHS */
674 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
675 "sector", victim, mission);
676 qp = missiles.q_forw;
677 while (qp != (&missiles)) {
685 if (QEMPTY(&bombers)) {
688 glp = (struct genlist *)qp;
697 * If there are planes performing an
698 * interdict or support mission, find
699 * some escorts for them, if possible.
700 * Up to 2 per bomber, if possible.
702 find_escorts(x, y, plane_owner, &escorts);
704 if (mission == MI_SINTERDICT)
705 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
707 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
709 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
711 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
712 plp = (struct plist *)qp;
713 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
714 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
717 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
721 air = (struct airport *)qp;
722 md = mapdist(x, y, air->x, air->y);
727 /* Split off the bombers at this base into b */
728 divide(&bombers, &b, air->x, air->y);
730 /* Split off the escorts at this base into e */
731 divide(&escorts, &e, air->x, air->y);
735 mission_flags |= P_X; /* stealth (shhh) */
736 mission_flags |= P_H; /* gets turned off if not all choppers */
738 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
739 0, mission_flags, &tech);
745 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
746 P_F | P_ESC, mission_flags, &tech);
748 BestAirPath(pp, air->x, air->y, x, y);
749 wu(0, plane_owner, "Flying %s mission from %s\n",
750 mission_name(mission), xyas(air->x, air->y, plane_owner));
751 if (air->own && (air->own != plane_owner)) {
752 wu(0, air->own, "%s is flying %s mission from %s\n",
753 cname(plane_owner), mission_name(mission),
754 xyas(air->x, air->y, air->own));
757 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
761 air_damage(&b, x, y, mission, victim, s, hardtarget);
769 if (sect.sct_type == SCT_WATER)
770 nreport(plane_owner, N_SHP_BOMB, victim, 1);
772 nreport(plane_owner, N_SCT_BOMB, victim, 1);
775 /* free up all this memory */
778 glp = (struct genlist *)qp;
786 while (qp != (&escorts)) {
794 while (qp != (&bombers)) {
805 cando(int mission, int type)
809 if (type == EF_PLANE)
813 if (type == EF_PLANE)
817 if ((type == EF_PLANE) || (type == EF_SHIP))
825 if (type == EF_PLANE)
838 nameofitem(struct genitem *gp, int type)
842 return prship((struct shpstr *)gp);
844 return prplane((struct plnstr *)gp);
846 return prland((struct lndstr *)gp);
852 mission_short_name(int mission)
877 mission_name(short int mission)
881 return "an interdiction";
885 return "a offensive support";
887 return "a defensive support";
893 return "a sub interdiction";
895 return "an air defense";
897 return "a mysterious";
901 show_mission(int type, struct nstr_item *np)
903 int size, first = 1, radius;
907 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
908 size = max(size, sizeof(struct shpstr));
909 block = (s_char *)malloc(size);
911 while (nxtitem(np, block)) {
912 gp = (struct genitem *)block;
913 if (!player->owner || gp->own == 0)
917 pr("Thing x,y op-sect rad mission\n");
920 pr("%-25s", nameofitem(gp, type));
921 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
922 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
923 gp->mission == MI_OSUPPORT ||
924 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
926 oprange(gp, type, &radius);
927 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
928 if (radius < gp->radius)
931 pr(" %4d", gp->radius);
932 } else if (gp->mission == MI_RESERVE) {
936 getsect(gp->x, gp->y, §);
937 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
940 if (((struct lndstr *)block)->lnd_rad_max == 0)
943 plus += ((struct lndstr *)block)->lnd_rad_max;
944 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
946 } else if (gp->mission == MI_ESCORT) {
949 ((float)((struct plnstr *)block)->pln_range / 2.0)
954 pr(" is on %s mission\n", mission_name(gp->mission));
956 pr(" has no mission.\n");
961 oprange(struct genitem *gp, int type, int *radius)
970 getship(gp->uid, &ship);
971 range = ldround(techfact(gp->tech,
972 (double)ship.shp_frnge / 2.0), 1);
975 getland(gp->uid, &land);
976 range = ldround(techfact((int)land.lnd_tech,
977 (double)land.lnd_frg / 2.0), 1);
980 getplane(gp->uid, &plane);
981 /* missiles go one way, so we can use all the range */
982 if (plchr[(int)plane.pln_type].pl_flags & P_M)
983 range = plane.pln_range;
985 range = ldround((double)plane.pln_range / 2.0, 1);;
989 if ((*radius) > range)
996 * Remove all planes who cannot go on
997 * the mission from the plane list.
1000 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1003 struct emp_qelem *qp, *next;
1008 struct plchrstr *pcp;
1013 for (qp = list->q_forw; qp != list; qp = next) {
1015 plp = (struct plist *)qp;
1019 if (pp->pln_effic < 40) {
1025 if (pp->pln_mobil < 1) {
1032 if (ontradingblock(EF_PLANE, (int *)pp)) {
1042 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1044 if ((wantflags & y) == y)
1045 if ((pcp->pl_flags & y) != y) {
1067 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1071 if ((pcp->pl_flags & P_E) ||
1072 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1084 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1086 if ((nowantflags & y) == y)
1087 if ((pcp->pl_flags & y) == y)
1096 if (pp->pln_ship >= 0) {
1097 if (!getship(pp->pln_ship, &ship)) {
1100 putplane(pp->pln_uid, pp);
1105 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1108 if (ship.shp_effic < SHIP_MINEFF) {
1111 /* Can't fly off of inefficient or non-owned, non-allied ships */
1112 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1113 ((ship.shp_own != pp->pln_own) &&
1114 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1120 if (pp->pln_land >= 0) {
1121 if (!getland(pp->pln_land, &land)) {
1124 putplane(pp->pln_uid, pp);
1129 if (!(pcp->pl_flags & P_E))
1131 if (land.lnd_effic < LAND_MINEFF)
1134 /* Can't fly off of inefficient or non-owned, non-allied units */
1135 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1136 ((land.lnd_own != pp->pln_own) &&
1137 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1143 /* Can't fly off units in ships or other units */
1144 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1150 /* Now, check the sector status if not on a plane or unit */
1151 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1152 /* If we can't get the sector, we can't check it, and can't fly */
1153 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1158 /* First, check allied status */
1159 /* Can't fly from non-owned sectors or non-allied sectors */
1160 if ((sect.sct_own != pp->pln_own) &&
1161 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1166 /* non-vtol plane */
1167 if ((pcp->pl_flags & P_V) == 0) {
1168 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1175 if (pcp->pl_flags & P_A) {
1176 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1183 putplane(pp->pln_uid, pp);
1188 * Arm only the planes at x,y
1192 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1193 int mission, struct ichrstr *ip, int flags,
1194 int mission_flags, int *tech)
1196 struct emp_qelem *qp;
1197 struct emp_qelem *next;
1202 for (qp = list->q_forw; qp != list; qp = next) {
1204 plp = (struct plist *)qp;
1206 if (plp->plane.pln_x != x)
1208 if (plp->plane.pln_y != y)
1211 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1216 if (flags & (P_S | P_I)) {
1217 if (plp->pcp->pl_flags & P_S)
1218 mission_flags |= P_S;
1219 if (plp->pcp->pl_flags & P_I)
1220 mission_flags |= P_I;
1222 if (*tech > plp->plane.pln_tech)
1223 *tech = plp->plane.pln_tech;
1224 if (!(plp->pcp->pl_flags & P_H))
1225 /* no stealth on this mission */
1226 mission_flags &= ~P_H;
1227 if (!(plp->pcp->pl_flags & P_X))
1228 /* no stealth on this mission */
1229 mission_flags &= ~P_X;
1230 if (!(plp->pcp->pl_flags & P_A)) {
1231 /* no asw on this mission */
1232 mission_flags &= ~P_A;
1234 if (!(plp->pcp->pl_flags & P_MINE)) {
1235 /* no asw on this mission */
1236 mission_flags &= ~P_MINE;
1240 * Mob costs for missions are 1/2 normal
1243 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1244 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1247 return mission_flags;
1251 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1254 struct plchrstr *pcp;
1266 if (pp->pln_ship >= 0) {
1267 getship(pp->pln_ship, &ship);
1268 item = ship.shp_item;
1269 } else if (pp->pln_land >= 0) {
1270 getland(pp->pln_land, &land);
1271 item = land.lnd_item;
1273 getsect(pp->pln_x, pp->pln_y, §);
1274 item = sect.sct_item;
1276 if (pcp->pl_fuel > item[I_PETROL]) {
1279 item[I_PETROL] -= pcp->pl_fuel;
1281 if (!(flags & P_F)) {
1287 if (pp->pln_nuketype == -1) {
1289 needed = pp->pln_load;
1293 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1295 itype = ip->i_vtype;
1296 needed = (pp->pln_load * 2) / ip->i_lbs;
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_V | P_C)) == 0)
1308 needed = pp->pln_load / ip->i_lbs;
1311 if (pp->pln_nuketype == -1)
1314 case 'i': /* missile interception */
1317 needed = pp->pln_load;
1323 if (rval < 0 || (itype && needed <= 0)) {
1326 if (item[itype] < needed && (itype == I_SHELL))
1327 item[itype] += supply_commod(plp->plane.pln_own,
1328 plp->plane.pln_x, plp->plane.pln_y,
1330 if (item[itype] < needed) {
1333 item[itype] -= needed;
1335 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1336 plp->bombs = needed;
1340 if (pp->pln_ship >= 0)
1341 putship(ship.shp_uid, &ship);
1342 else if (pp->pln_land >= 0)
1343 putland(land.lnd_uid, &land);
1350 * Return 1 if this x,y pair is in the list
1353 find_airport(struct emp_qelem *airp, coord x, coord y)
1355 struct emp_qelem *qp;
1358 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1359 a = (struct airport *)qp;
1360 if ((a->x == x) && (a->y == y))
1367 /* #*# This needs to be changed to include acc's -KHS */
1369 add_airport(struct emp_qelem *airp, coord x, coord y)
1374 a = (struct airport *)malloc(sizeof(struct airport));
1378 getsect(x, y, §);
1379 a->own = sect.sct_own;
1381 emp_insque((struct emp_qelem *)a, airp);
1385 * Take all the planes in list 1 that
1386 * are at x,y, and put them into list 2.
1389 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1391 struct emp_qelem *qp, *next;
1394 for (qp = l1->q_forw; qp != l1; qp = next) {
1396 plp = (struct plist *)qp;
1398 if (plp->plane.pln_x != x)
1400 if (plp->plane.pln_y != y)
1409 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1410 natid victim, s_char *s, int hardtarget)
1412 struct emp_qelem *qp;
1415 int newdam, dam = 0;
1419 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1420 plp = (struct plist *)qp;
1423 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1430 if (plp->pcp->pl_flags & P_A) {
1431 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1433 "\t%s detects sub movement in %s\n",
1434 prplane(pp), xyas(x, y, pp->pln_own));
1437 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1439 "\t%s tracks %s %s at %s\n",
1440 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1444 "\t%s depth-charging %s %s in %s\n",
1445 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1448 "\t%s pinbombing %s %s in %s\n",
1449 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1451 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1452 if (plp->plane.pln_nuketype != -1)
1454 else if (hardtarget != SECT_HARDTARGET)
1455 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1456 /* Always calculate damage */
1457 if (roll(100) <= hitchance) {
1458 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1460 if (mission == MI_INTERDICT) {
1462 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1463 prplane(pp), nukedam, cname(victim), s);
1468 "\t\thit %s %s for %d damage\n",
1469 cname(victim), s, newdam);
1473 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1474 wu(0, pp->pln_own, "missed\n");
1475 if (mission == MI_SINTERDICT) {
1477 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1478 xyas(x, y, victim));
1479 } else if (*s == 's') {
1481 "SPLASH! Bombs miss your %s in %s\n",
1482 s, xyas(x, y, victim));
1484 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1485 s, xyas(x, y, victim));
1487 /* Now, even though we missed, the bombs
1489 collateral_damage(x, y, newdam, bombers);
1492 /* use up missiles */
1493 if (plp->pcp->pl_flags & P_M) {
1494 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1504 * Check to see if anyone hostile to the victim
1505 * is running an air defense mission on this
1506 * sector. If so, do air combat
1509 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1510 struct emp_qelem *esc_list)
1513 int mission_flags, tech, combat = 0, rel, dist, z;
1514 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1516 struct genlist *glp;
1518 struct genlist mi[MAXNOC];
1524 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1526 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1529 memset(mi, 0, sizeof(mi));
1530 for (z = 1; z < MAXNOC; z++)
1531 emp_initque((struct emp_qelem *)&mi[z]);
1533 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1535 for (cn = 1; cn < MAXNOC; cn++) {
1536 /* Check our relations */
1537 rel = getrel(getnatp(cn), victim);
1542 if (QEMPTY(&mi[cn].queue))
1545 /* Ok, make a list of all the interceptors. Note that this *copies* the
1546 * list from the mission creation. This list must be deleted later. */
1547 emp_initque(&interceptors);
1548 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1551 glp = (struct genlist *)qp;
1552 gp = (struct genitem *)glp->thing;
1553 plp = (struct plist *)qp;
1555 dist = mapdist(x, y, gp->x, gp->y);
1557 plp = (struct plist *)malloc(sizeof(struct plist));
1558 memset(plp, 0, sizeof(struct plist));
1559 plp->pcp = (struct plchrstr *)glp->cp;
1560 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1562 /* missiles go one way, so we can use all the range */
1563 if (!(plp->pcp->pl_flags & P_M))
1565 /* If it's out of range, free it and continue on */
1566 if (dist > plp->plane.pln_range) {
1570 emp_insque(&plp->queue, &interceptors);
1573 /* Remove those who cannot go */
1574 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1576 if (QEMPTY(&interceptors))
1579 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1580 * that the higher numbered planes go into battle (they should be the better ones
1581 * at fighting, if all went well.) */
1583 for (qp = interceptors.q_forw; qp != (&interceptors);
1586 tcount -= (count * 2);
1587 /* Just in case there are more incoming than we have */
1590 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1594 /* Free it up and continue */
1596 glp = (struct genlist *)qp;
1597 free((s_char *)glp);
1601 /* Now, make a list of all the airports these planes are coming from */
1603 for (qp = interceptors.q_forw; qp != (&interceptors);
1605 plp = (struct plist *)qp;
1606 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1607 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1610 /* Now, fly them out one airport at a time */
1611 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1612 struct airport *air;
1614 air = (struct airport *)qp;
1615 dist = mapdist(x, y, air->x, air->y);
1619 /* Split off the interceptors at this base into i */
1620 divide(&interceptors, &i, air->x, air->y);
1624 mission_flags |= P_X; /* stealth (shhh) */
1625 /* gets turned off if not all choppers */
1626 mission_flags |= P_H;
1627 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1628 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1630 /* Did we run out of interceptors? */
1633 /* Did we run out of bombers? */
1634 if (QEMPTY(bomb_list)) {
1635 /* Yes, so we have to put the rest of the interceptors back, and
1636 then continue, or we leak memory */
1641 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1642 mission_flags, &tech);
1644 /* Did we run out of interceptors? */
1647 /* Did we run out of bombers? */
1648 if (QEMPTY(bomb_list)) {
1649 /* Yes, so we have to put the rest of the interceptors back, and
1650 then continue, or we leak memory */
1655 BestAirPath(path, air->x, air->y, x, y);
1656 wu(0, cn, "Flying %s mission from %s\n",
1657 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1658 if (air->own && (air->own != cn)) {
1659 wu(0, air->own, "%s is flying %s mission from %s\n",
1660 cname(cn), mission_name(MI_AIR_DEFENSE),
1661 xyas(air->x, air->y, air->own));
1664 /* Now, fly the planes to the sector */
1665 emp_initque(&empty);
1666 ac_encounter(&i, &empty, air->x, air->y,
1667 path, mission_flags, 1, bomb_list, esc_list);
1669 /* If none made it, continue */
1673 /* Some made it, so now they get to try to fight. */
1674 /* Intercept the escorts first */
1676 if (!QEMPTY(esc_list)) {
1677 mpr(victim, "%s air defense planes intercept!\n",
1679 ac_combat_headers(victim, cn);
1680 ac_airtoair(esc_list, &i);
1683 /* Now intercept the bombers */
1684 if (!QEMPTY(bomb_list)) {
1686 mpr(victim, "%s air defense planes intercept!\n",
1688 ac_combat_headers(victim, cn);
1690 ac_airtoair(bomb_list, &i);
1699 /* We have to free all of these, if they are still there, otherwise they get
1700 lost and we leak memory all over the place. */
1701 for (cn = 1; cn < MAXNOC; cn++) {
1702 /* free up all this memory if it's still there */
1703 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1705 glp = (struct genlist *)qp;
1707 free((s_char *)glp);