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
54 #include "prototypes.h"
58 struct emp_qelem queue;
63 static void add_airport(struct emp_qelem *, coord, coord);
64 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
66 static void build_mission_list(struct genlist *, coord, coord, int, natid);
67 static void build_mission_list_type(struct genlist *, coord, coord, int,
69 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
70 static int dosupport(struct genlist *, coord, coord, natid, natid);
71 static int find_airport(struct emp_qelem *, coord, coord);
72 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
73 int, struct ichrstr *, int, int, int *);
74 static void mission_pln_sel(struct emp_qelem *, int, int, int);
75 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
79 * Interdict commodities & transported planes
82 ground_interdict(coord x, coord y, natid victim, s_char *s)
85 int dam = 0, newdam, rel;
86 struct genlist mi[MAXNOC];
89 memset(mi, 0, sizeof(mi));
90 for (z = 1; z < MAXNOC; z++)
91 emp_initque((struct emp_qelem *)&mi[z]);
93 build_mission_list(mi, x, y, MI_INTERDICT, victim);
95 for (cn = 1; cn < MAXNOC; cn++) {
96 rel = getrel(getnatp(cn), victim);
100 if (QEMPTY(&mi[cn].queue))
103 newdam = perform_mission(x, y, victim, &mi[cn].queue,
104 MI_INTERDICT, s, SECT_HARDTARGET);
107 mpr(victim, "%s interdiction mission does %d damage!\n",
111 collateral_damage(x, y, dam, 0);
117 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
125 getsect(x, y, §);
127 coll = ldround((double)dam * collateral_dam, 1);
130 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
131 xyas(x, y, sect.sct_own), coll);
132 sectdamage(§, coll, list);
140 only_subs(struct emp_qelem *list)
142 struct emp_qelem *qp;
146 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
147 glp = (struct genlist *)qp;
149 if (glp->type != EF_SHIP)
151 mcp = (struct mchrstr *)glp->cp;
152 if (!(mcp->m_flags & M_SUB))
156 /* They were all subs! */
162 * Interdict ships & land units
166 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
171 struct genlist mi[MAXNOC];
175 memset(mi, 0, sizeof(mi));
176 for (z = 1; z < MAXNOC; z++)
177 emp_initque((struct emp_qelem *)&mi[z]);
179 build_mission_list(mi, x, y, mission, victim);
181 for (cn = 1; cn < MAXNOC; cn++) {
184 if (mission == MI_SINTERDICT) {
185 if (getrel(getnatp(cn), victim) >= FRIENDLY)
187 } else if (getrel(getnatp(cn), victim) > HOSTILE)
190 if (QEMPTY(&mi[cn].queue))
193 osubs = only_subs(&mi[cn].queue);
194 newdam = perform_mission(x, y, victim, &mi[cn].queue,
195 mission, s, hardtarget);
198 /* If only subs responded, then we don't know who's
202 "Enemy interdiction mission does %d damage!\n",
206 "%s interdiction mission does %d damage!\n",
212 collateral_damage(x, y, dam, 0);
218 * Perform a mission against victim, on behalf of actee
221 off_support(coord x, coord y, natid victim, natid actee)
224 struct genlist mi[MAXNOC];
227 memset(mi, 0, sizeof(mi));
228 for (z = 1; z < MAXNOC; z++)
229 emp_initque((struct emp_qelem *)&mi[z]);
231 build_mission_list(mi, x, y, MI_SUPPORT, victim);
232 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
234 dam = dosupport(mi, x, y, victim, actee);
239 * Perform a mission against victim, on behalf of actee
242 def_support(coord x, coord y, natid victim, natid actee)
245 struct genlist mi[MAXNOC];
248 memset(mi, 0, sizeof(mi));
249 for (z = 1; z < MAXNOC; z++)
250 emp_initque((struct emp_qelem *)&mi[z]);
252 build_mission_list(mi, x, y, MI_SUPPORT, victim);
253 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
255 dam = dosupport(mi, x, y, victim, actee);
260 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
266 for (cn = 1; cn < MAXNOC; cn++) {
267 rel = getrel(getnatp(cn), actee);
268 if ((cn != actee) && (rel != ALLIED))
270 rel = getrel(getnatp(cn), victim);
271 if ((cn != actee) && (rel != AT_WAR))
274 if (QEMPTY(&mi[cn].queue))
277 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
278 "", SECT_HARDTARGET);
284 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
287 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
288 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
289 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
293 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
294 int type, natid victim)
311 size = MAX(sizeof(struct shpstr),sizeof(struct lndstr));
312 size = MAX(size,sizeof(struct plnstr));
313 block = malloc(size);
315 size = sizeof(u_block);
316 block = (s_char *)&u_block;
318 snxtitem_all(&ni, type);
319 while (nxtitem(&ni, block)) {
320 gp = (struct genitem *)block;
328 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
331 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
332 (gp->mission != MI_INTERDICT))
335 relat = getrel(getnatp(gp->own), victim);
336 if (mission == MI_SINTERDICT) {
337 if (relat >= FRIENDLY)
339 else if (type != EF_PLANE && relat > HOSTILE)
341 } else if (relat > HOSTILE)
344 dist = mapdist(x, y, gp->opx, gp->opy);
347 if (mission != MI_RESERVE) /* XXX */
348 oprange(gp, type, &radius);
353 /* Ok, it is within the operations range. */
354 /* Now check from where the object actually is */
355 dist = mapdist(x, y, gp->x, gp->y);
357 oprange(gp, type, &radius);
360 /* Ok, the object can get to where the x,y is */
363 if (mission != MI_AIR_DEFENSE) {
364 getsect(x, y, §);
365 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
368 * If the player->owner of the unit isn't at war
369 * with the victim, and doesn't own the
370 * sect being acted upon, and isn't the
371 * old player->owner of that sect, bounce them.
373 if (sect.sct_type != SCT_WATER &&
374 sect.sct_own != gp->own &&
375 sect.sct_oldown != gp->own)
381 glp = malloc(sizeof(struct genlist));
382 memset(glp, 0, sizeof(struct genlist));
388 glp->cp = (s_char *)&lchr[(int)gp->type];
391 glp->cp = (s_char *)&mchr[(int)gp->type];
394 glp->cp = (s_char *)&plchr[(int)gp->type];
397 glp->thing = malloc(size);
398 memcpy(glp->thing, block, size);
399 emp_insque(&glp->queue, &mi[gp->own].queue);
404 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
411 snxtitem_all(&ni, EF_PLANE);
412 while (nxtitem(&ni, &plane)) {
413 if (plane.pln_own != cn)
416 if (plane.pln_mission != MI_ESCORT)
419 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
421 if (dist > ((int)((float)plane.pln_range / 2.0)))
424 plp = malloc(sizeof(struct plist));
425 memset(plp, 0, sizeof(struct plist));
426 plp->pcp = &plchr[(int)plane.pln_type];
428 emp_insque(&plp->queue, escorts);
433 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
434 int mission, s_char *s, int hardtarget)
436 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
437 struct emp_qelem *newqp;
445 struct plchrstr *pcp;
446 int dam = 0, dam2, mission_flags, tech;
447 natid plane_owner = 0;
448 int gun, shell, md, air_dam = 0;
449 double range2, prb, range, mobcost, hitchance;
451 getsect(x, y, §);
453 emp_initque(&missiles);
454 emp_initque(&bombers);
455 emp_initque(&escorts);
458 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
459 glp = (struct genlist *)qp;
460 gp = (struct genitem *)glp->thing;
462 md = mapdist(x, y, gp->x, gp->y);
464 if (glp->type == EF_LAND) {
465 lp = (struct lndstr *)glp->thing;
467 if (lp->lnd_effic < LAND_MINFIREEFF)
470 if (mission == MI_SINTERDICT)
473 if ((mission == MI_INTERDICT) &&
474 (md > land_max_interdiction_range))
477 if (md > (lp->lnd_frg / 2))
480 if (lnd_getmil(lp) < 1)
483 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
484 range2 = (double)roundrange(range);
489 shell = lp->lnd_item[I_SHELL];
490 gun = lp->lnd_item[I_GUN];
491 if (shell == 0 || gun == 0)
494 if (has_supply(lp)) {
496 putland(lp->lnd_uid, lp);
497 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
498 lp->lnd_ammo, shell), 1);
499 if (sect.sct_type == SCT_WATER) {
500 double dam3 = (double)dam2;
501 if (chance(((double)lp->lnd_acc) / 100.0))
502 dam2 = ldround((dam3 / 2.0), 1);
505 if (sect.sct_type == SCT_WATER)
506 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
508 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
510 "%s fires at %s %s at %s\n",
511 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
513 mpr(victim, "%s %s fires at you at %s\n",
514 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
516 } else if (glp->type == EF_SHIP) {
517 sp = (struct shpstr *)glp->thing;
518 mcp = (struct mchrstr *)glp->cp;
520 if (sp->shp_effic < 60)
522 if (sp->shp_frnge == 0)
524 if (((mission == MI_INTERDICT) ||
525 (mission == MI_SINTERDICT)) &&
526 (md > ship_max_interdiction_range))
528 if (sp->shp_item[I_MILIT] < 1)
531 if ((mcp->m_flags & M_SUB) &&
532 (sect.sct_type != SCT_WATER))
535 if (mission == MI_SINTERDICT) {
536 if (!(mcp->m_flags & M_SONAR))
538 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
540 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
541 range2 *= (double)sp->shp_effic / 200.0;
544 /* can't look all the time */
548 if (mcp->m_flags & M_SUB) {
549 /* If we aren't shooting at "subs" or "ships" don't fire at all from
553 if (sp->shp_mobil < 0)
555 gun = sp->shp_item[I_GUN];
558 shell = sp->shp_item[I_SHELL];
559 if (shell < SHP_TORP_SHELLS)
560 shell += supply_commod(sp->shp_own,
561 sp->shp_x, sp->shp_y, I_SHELL,
562 SHP_TORP_SHELLS - shell);
563 if (shell < SHP_TORP_SHELLS)
566 range = sp->shp_effic * techfact(sp->shp_tech,
567 ((double)sp->shp_frnge)) /
570 range2 = (double)roundrange(range);
574 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
576 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
577 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
578 mobcost = (480.0 / (mobcost +
579 techfact(sp->shp_tech, mobcost)));
580 sp->shp_mobil -= mobcost;
581 putship(sp->shp_uid, sp);
582 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
585 "%s locking on %s %s in %s\n",
586 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
588 "\tEffective torpedo range is %.1f\n", range);
590 "\tWhooosh... Hitchance = %d%%\n",
591 (int)(hitchance * 100));
593 if (hitchance < 1.0 && !chance(hitchance)) {
594 wu(0, sp->shp_own, "\tMissed\n");
596 "Incoming torpedo sighted @ %s missed (whew)!\n",
600 wu(0, sp->shp_own, "\tBOOM!...\n");
601 dam2 = TORP_DAMAGE();
604 nreport(victim, N_TORP_SHIP, 0, 1);
606 "\tTorpedo hit %s %s for %d damage\n",
607 cname(victim), s, dam2);
610 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
611 xyas(x, y, victim), dam2);
613 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
614 range2 = (double)roundrange(range);
617 gun = sp->shp_item[I_GUN];
618 gun = MIN(gun, sp->shp_glim);
619 shell = sp->shp_item[I_SHELL];
621 shell += supply_commod(sp->shp_own,
622 sp->shp_x, sp->shp_y, I_SHELL,
624 gun = MIN(gun, shell);
625 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
629 dam2 = seagun(sp->shp_effic, gun);
633 prb = ((double)md) / range2;
636 dam2 = (int)((float)dam2 / 2.0);
638 if (sect.sct_type == SCT_WATER)
639 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
641 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
643 "%s fires at %s %s at %s\n",
644 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
646 mpr(victim, "%s %s fires at you at %s\n",
647 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
649 sp->shp_item[I_SHELL] = shell - gun;
650 putship(sp->shp_uid, sp);
652 } else if (glp->type == EF_PLANE) {
653 pcp = (struct plchrstr *)glp->cp;
654 if (pcp->pl_flags & P_M)
655 /* units have their own missile interdiction */
656 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
659 /* save planes for later */
660 plp = malloc(sizeof(struct plist));
662 memset(plp, 0, sizeof(struct plist));
664 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
665 if (plp->pcp->pl_flags & P_M)
666 emp_insque(&plp->queue, &missiles);
668 emp_insque(&plp->queue, &bombers);
669 plane_owner = plp->plane.pln_own;
672 if (!QEMPTY(&missiles)) {
673 /* I arbitrarily chose 100 mindam -KHS */
675 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
676 "sector", victim, mission);
677 qp = missiles.q_forw;
678 while (qp != (&missiles)) {
686 if (QEMPTY(&bombers)) {
689 glp = (struct genlist *)qp;
698 * If there are planes performing an
699 * interdict or support mission, find
700 * some escorts for them, if possible.
701 * Up to 2 per bomber, if possible.
703 find_escorts(x, y, plane_owner, &escorts);
705 if (mission == MI_SINTERDICT)
706 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
708 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
710 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
712 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
713 plp = (struct plist *)qp;
714 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
715 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
718 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
722 air = (struct airport *)qp;
723 md = mapdist(x, y, air->x, air->y);
728 /* Split off the bombers at this base into b */
729 divide(&bombers, &b, air->x, air->y);
731 /* Split off the escorts at this base into e */
732 divide(&escorts, &e, air->x, air->y);
736 mission_flags |= P_X; /* stealth (shhh) */
737 mission_flags |= P_H; /* gets turned off if not all choppers */
739 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
740 0, mission_flags, &tech);
746 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
747 P_F | P_ESC, mission_flags, &tech);
749 BestAirPath(pp, air->x, air->y, x, y);
750 wu(0, plane_owner, "Flying %s mission from %s\n",
751 mission_name(mission), xyas(air->x, air->y, plane_owner));
752 if (air->own && (air->own != plane_owner)) {
753 wu(0, air->own, "%s is flying %s mission from %s\n",
754 cname(plane_owner), mission_name(mission),
755 xyas(air->x, air->y, air->own));
758 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
762 air_damage(&b, x, y, mission, victim, s, hardtarget);
770 if (sect.sct_type == SCT_WATER)
771 nreport(plane_owner, N_SHP_BOMB, victim, 1);
773 nreport(plane_owner, N_SCT_BOMB, victim, 1);
776 /* free up all this memory */
779 glp = (struct genlist *)qp;
787 while (qp != (&escorts)) {
795 while (qp != (&bombers)) {
806 cando(int mission, int type)
810 if (type == EF_PLANE)
814 if (type == EF_PLANE)
818 if ((type == EF_PLANE) || (type == EF_SHIP))
826 if (type == EF_PLANE)
839 nameofitem(struct genitem *gp, int type)
843 return prship((struct shpstr *)gp);
845 return prplane((struct plnstr *)gp);
847 return prland((struct lndstr *)gp);
853 mission_name(short int mission)
857 return "an interdiction";
861 return "a offensive support";
863 return "a defensive support";
869 return "a sub interdiction";
871 return "an air defense";
873 return "a mysterious";
877 show_mission(int type, struct nstr_item *np)
880 int first = 1, radius;
884 size = MAX(sizeof(struct lndstr), sizeof(struct plnstr));
885 size = MAX(size, sizeof(struct shpstr));
886 block = malloc(size);
888 while (nxtitem(np, block)) {
889 gp = (struct genitem *)block;
890 if (!player->owner || gp->own == 0)
894 pr("Thing x,y op-sect rad mission\n");
897 pr("%-25s", nameofitem(gp, type));
898 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
899 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
900 gp->mission == MI_OSUPPORT ||
901 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
903 oprange(gp, type, &radius);
904 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
905 if (radius < gp->radius)
908 pr(" %4d", gp->radius);
909 } else if (gp->mission == MI_RESERVE) {
913 getsect(gp->x, gp->y, §);
914 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
917 if (((struct lndstr *)block)->lnd_rad_max == 0)
920 plus += ((struct lndstr *)block)->lnd_rad_max;
921 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
923 } else if (gp->mission == MI_ESCORT) {
926 ((float)((struct plnstr *)block)->pln_range / 2.0)
931 pr(" is on %s mission\n", mission_name(gp->mission));
933 pr(" has no mission.\n");
938 oprange(struct genitem *gp, int type, int *radius)
947 getship(gp->uid, &ship);
948 range = ldround(techfact(gp->tech,
949 (double)ship.shp_frnge / 2.0), 1);
952 getland(gp->uid, &land);
953 range = ldround(techfact((int)land.lnd_tech,
954 (double)land.lnd_frg / 2.0), 1);
957 getplane(gp->uid, &plane);
958 /* missiles go one way, so we can use all the range */
959 if (plchr[(int)plane.pln_type].pl_flags & P_M)
960 range = plane.pln_range;
962 range = ldround((double)plane.pln_range / 2.0, 1);;
966 if ((*radius) > range)
973 * Remove all planes who cannot go on
974 * the mission from the plane list.
977 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
980 struct emp_qelem *qp, *next;
985 struct plchrstr *pcp;
990 for (qp = list->q_forw; qp != list; qp = next) {
992 plp = (struct plist *)qp;
996 if (pp->pln_effic < 40) {
1002 if (pp->pln_mobil < 1) {
1009 if (ontradingblock(EF_PLANE, (int *)pp)) {
1019 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1021 if ((wantflags & y) == y)
1022 if ((pcp->pl_flags & y) != y) {
1044 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1048 if ((pcp->pl_flags & P_E) ||
1049 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1061 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1063 if ((nowantflags & y) == y)
1064 if ((pcp->pl_flags & y) == y)
1073 if (pp->pln_ship >= 0) {
1074 if (!getship(pp->pln_ship, &ship)) {
1077 putplane(pp->pln_uid, pp);
1082 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1085 if (ship.shp_effic < SHIP_MINEFF) {
1088 /* Can't fly off of inefficient or non-owned, non-allied ships */
1089 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1090 ((ship.shp_own != pp->pln_own) &&
1091 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1097 if (pp->pln_land >= 0) {
1098 if (!getland(pp->pln_land, &land)) {
1101 putplane(pp->pln_uid, pp);
1106 if (!(pcp->pl_flags & P_E))
1108 if (land.lnd_effic < LAND_MINEFF)
1111 /* Can't fly off of inefficient or non-owned, non-allied units */
1112 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1113 ((land.lnd_own != pp->pln_own) &&
1114 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1120 /* Can't fly off units in ships or other units */
1121 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1127 /* Now, check the sector status if not on a plane or unit */
1128 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1129 /* If we can't get the sector, we can't check it, and can't fly */
1130 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1135 /* First, check allied status */
1136 /* Can't fly from non-owned sectors or non-allied sectors */
1137 if ((sect.sct_own != pp->pln_own) &&
1138 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1143 /* non-vtol plane */
1144 if ((pcp->pl_flags & P_V) == 0) {
1145 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1152 if (pcp->pl_flags & P_A) {
1153 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1160 putplane(pp->pln_uid, pp);
1165 * Arm only the planes at x,y
1169 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1170 int mission, struct ichrstr *ip, int flags,
1171 int mission_flags, int *tech)
1173 struct emp_qelem *qp;
1174 struct emp_qelem *next;
1179 for (qp = list->q_forw; qp != list; qp = next) {
1181 plp = (struct plist *)qp;
1183 if (plp->plane.pln_x != x)
1185 if (plp->plane.pln_y != y)
1188 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1193 if (flags & (P_S | P_I)) {
1194 if (plp->pcp->pl_flags & P_S)
1195 mission_flags |= P_S;
1196 if (plp->pcp->pl_flags & P_I)
1197 mission_flags |= P_I;
1199 if (*tech > plp->plane.pln_tech)
1200 *tech = plp->plane.pln_tech;
1201 if (!(plp->pcp->pl_flags & P_H))
1202 /* no stealth on this mission */
1203 mission_flags &= ~P_H;
1204 if (!(plp->pcp->pl_flags & P_X))
1205 /* no stealth on this mission */
1206 mission_flags &= ~P_X;
1207 if (!(plp->pcp->pl_flags & P_A)) {
1208 /* no asw on this mission */
1209 mission_flags &= ~P_A;
1211 if (!(plp->pcp->pl_flags & P_MINE)) {
1212 /* no asw on this mission */
1213 mission_flags &= ~P_MINE;
1217 * Mob costs for missions are 1/2 normal
1220 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1221 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1224 return mission_flags;
1228 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1231 struct plchrstr *pcp;
1243 if (pp->pln_ship >= 0) {
1244 getship(pp->pln_ship, &ship);
1245 item = ship.shp_item;
1246 } else if (pp->pln_land >= 0) {
1247 getland(pp->pln_land, &land);
1248 item = land.lnd_item;
1250 getsect(pp->pln_x, pp->pln_y, §);
1251 item = sect.sct_item;
1253 if (pcp->pl_fuel > item[I_PETROL]) {
1256 item[I_PETROL] -= pcp->pl_fuel;
1258 if (!(flags & P_F)) {
1264 if (pp->pln_nuketype == -1) {
1266 needed = pp->pln_load;
1270 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1272 itype = ip->i_vtype;
1273 needed = (pp->pln_load * 2) / ip->i_lbs;
1276 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1278 itype = ip->i_vtype;
1279 needed = (pp->pln_load * 2) / ip->i_lbs;
1282 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1285 needed = pp->pln_load / ip->i_lbs;
1288 if (pp->pln_nuketype == -1)
1291 case 'i': /* missile interception */
1294 needed = pp->pln_load;
1300 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1303 if (itype != I_NONE) {
1304 if (itype == I_SHELL && item[itype] < needed)
1305 item[itype] += supply_commod(plp->plane.pln_own,
1309 if (item[itype] < needed)
1311 item[itype] -= needed;
1313 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1314 plp->bombs = needed;
1318 if (pp->pln_ship >= 0)
1319 putship(ship.shp_uid, &ship);
1320 else if (pp->pln_land >= 0)
1321 putland(land.lnd_uid, &land);
1328 * Return 1 if this x,y pair is in the list
1331 find_airport(struct emp_qelem *airp, coord x, coord y)
1333 struct emp_qelem *qp;
1336 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1337 a = (struct airport *)qp;
1338 if ((a->x == x) && (a->y == y))
1345 /* #*# This needs to be changed to include acc's -KHS */
1347 add_airport(struct emp_qelem *airp, coord x, coord y)
1352 a = malloc(sizeof(struct airport));
1356 getsect(x, y, §);
1357 a->own = sect.sct_own;
1359 emp_insque((struct emp_qelem *)a, airp);
1363 * Take all the planes in list 1 that
1364 * are at x,y, and put them into list 2.
1367 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1369 struct emp_qelem *qp, *next;
1372 for (qp = l1->q_forw; qp != l1; qp = next) {
1374 plp = (struct plist *)qp;
1376 if (plp->plane.pln_x != x)
1378 if (plp->plane.pln_y != y)
1387 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1388 natid victim, s_char *s, int hardtarget)
1390 struct emp_qelem *qp;
1393 int newdam, dam = 0;
1397 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1398 plp = (struct plist *)qp;
1401 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1408 if (plp->pcp->pl_flags & P_A) {
1409 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1411 "\t%s detects sub movement in %s\n",
1412 prplane(pp), xyas(x, y, pp->pln_own));
1415 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1417 "\t%s tracks %s %s at %s\n",
1418 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1422 "\t%s depth-charging %s %s in %s\n",
1423 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1426 "\t%s pinbombing %s %s in %s\n",
1427 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1429 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1430 if (plp->plane.pln_nuketype != -1)
1432 else if (hardtarget != SECT_HARDTARGET)
1433 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1434 /* Always calculate damage */
1435 if (roll(100) <= hitchance) {
1436 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1438 if (mission == MI_INTERDICT) {
1440 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1441 prplane(pp), nukedam, cname(victim), s);
1446 "\t\thit %s %s for %d damage\n",
1447 cname(victim), s, newdam);
1451 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1452 wu(0, pp->pln_own, "missed\n");
1453 if (mission == MI_SINTERDICT) {
1455 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1456 xyas(x, y, victim));
1457 } else if (*s == 's') {
1459 "SPLASH! Bombs miss your %s in %s\n",
1460 s, xyas(x, y, victim));
1462 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1463 s, xyas(x, y, victim));
1465 /* Now, even though we missed, the bombs
1467 collateral_damage(x, y, newdam, bombers);
1470 /* use up missiles */
1471 if (plp->pcp->pl_flags & P_M) {
1472 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1482 * Check to see if anyone hostile to the victim
1483 * is running an air defense mission on this
1484 * sector. If so, do air combat
1487 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1488 struct emp_qelem *esc_list)
1491 int mission_flags, tech, combat = 0, rel, dist, z;
1492 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1494 struct genlist *glp;
1496 struct genlist mi[MAXNOC];
1502 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1504 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1507 memset(mi, 0, sizeof(mi));
1508 for (z = 1; z < MAXNOC; z++)
1509 emp_initque((struct emp_qelem *)&mi[z]);
1511 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1513 for (cn = 1; cn < MAXNOC; cn++) {
1514 /* Check our relations */
1515 rel = getrel(getnatp(cn), victim);
1520 if (QEMPTY(&mi[cn].queue))
1523 /* Ok, make a list of all the interceptors. Note that this *copies* the
1524 * list from the mission creation. This list must be deleted later. */
1525 emp_initque(&interceptors);
1526 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1529 glp = (struct genlist *)qp;
1530 gp = (struct genitem *)glp->thing;
1531 plp = (struct plist *)qp;
1533 dist = mapdist(x, y, gp->x, gp->y);
1535 plp = malloc(sizeof(struct plist));
1536 memset(plp, 0, sizeof(struct plist));
1537 plp->pcp = (struct plchrstr *)glp->cp;
1538 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1540 /* missiles go one way, so we can use all the range */
1541 if (!(plp->pcp->pl_flags & P_M))
1543 /* If it's out of range, free it and continue on */
1544 if (dist > plp->plane.pln_range) {
1548 emp_insque(&plp->queue, &interceptors);
1551 /* Remove those who cannot go */
1552 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1554 if (QEMPTY(&interceptors))
1557 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1558 * that the higher numbered planes go into battle (they should be the better ones
1559 * at fighting, if all went well.) */
1561 for (qp = interceptors.q_forw; qp != (&interceptors);
1564 tcount -= (count * 2);
1565 /* Just in case there are more incoming than we have */
1568 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1572 /* Free it up and continue */
1574 glp = (struct genlist *)qp;
1579 /* Now, make a list of all the airports these planes are coming from */
1581 for (qp = interceptors.q_forw; qp != (&interceptors);
1583 plp = (struct plist *)qp;
1584 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1585 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1588 /* Now, fly them out one airport at a time */
1589 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1590 struct airport *air;
1592 air = (struct airport *)qp;
1593 dist = mapdist(x, y, air->x, air->y);
1597 /* Split off the interceptors at this base into i */
1598 divide(&interceptors, &i, air->x, air->y);
1602 mission_flags |= P_X; /* stealth (shhh) */
1603 /* gets turned off if not all choppers */
1604 mission_flags |= P_H;
1605 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1606 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1608 /* Did we run out of interceptors? */
1611 /* Did we run out of bombers? */
1612 if (QEMPTY(bomb_list)) {
1613 /* Yes, so we have to put the rest of the interceptors back, and
1614 then continue, or we leak memory */
1619 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1620 mission_flags, &tech);
1622 /* Did we run out of interceptors? */
1625 /* Did we run out of bombers? */
1626 if (QEMPTY(bomb_list)) {
1627 /* Yes, so we have to put the rest of the interceptors back, and
1628 then continue, or we leak memory */
1633 BestAirPath(path, air->x, air->y, x, y);
1634 wu(0, cn, "Flying %s mission from %s\n",
1635 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1636 if (air->own && (air->own != cn)) {
1637 wu(0, air->own, "%s is flying %s mission from %s\n",
1638 cname(cn), mission_name(MI_AIR_DEFENSE),
1639 xyas(air->x, air->y, air->own));
1642 /* Now, fly the planes to the sector */
1643 emp_initque(&empty);
1644 ac_encounter(&i, &empty, air->x, air->y,
1645 path, mission_flags, 1, bomb_list, esc_list);
1647 /* If none made it, continue */
1651 /* Some made it, so now they get to try to fight. */
1652 /* Intercept the escorts first */
1654 if (!QEMPTY(esc_list)) {
1655 mpr(victim, "%s air defense planes intercept!\n",
1657 ac_combat_headers(victim, cn);
1658 ac_airtoair(esc_list, &i);
1661 /* Now intercept the bombers */
1662 if (!QEMPTY(bomb_list)) {
1664 mpr(victim, "%s air defense planes intercept!\n",
1666 ac_combat_headers(victim, cn);
1668 ac_airtoair(bomb_list, &i);
1677 /* We have to free all of these, if they are still there, otherwise they get
1678 lost and we leak memory all over the place. */
1679 for (cn = 1; cn < MAXNOC; cn++) {
1680 /* free up all this memory if it's still there */
1681 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1683 glp = (struct genlist *)qp;