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
52 #include "prototypes.h"
56 struct emp_qelem queue;
61 static void add_airport(struct emp_qelem *, coord, coord);
62 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
64 static void build_mission_list(struct genlist *, coord, coord, int, natid);
65 static void build_mission_list_type(struct genlist *, coord, coord, int,
67 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
68 static int dosupport(struct genlist *, coord, coord, natid, natid);
69 static int find_airport(struct emp_qelem *, coord, coord);
70 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
71 int, struct ichrstr *, int, int, int *);
72 static void mission_pln_sel(struct emp_qelem *, int, int, int);
73 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
77 * Interdict commodities & transported planes
80 ground_interdict(coord x, coord y, natid victim, s_char *s)
83 int dam = 0, newdam, rel;
84 struct genlist mi[MAXNOC];
87 memset(mi, 0, sizeof(mi));
88 for (z = 1; z < MAXNOC; z++)
89 emp_initque((struct emp_qelem *)&mi[z]);
91 build_mission_list(mi, x, y, MI_INTERDICT, victim);
93 for (cn = 1; cn < MAXNOC; cn++) {
94 rel = getrel(getnatp(cn), victim);
98 if (QEMPTY(&mi[cn].queue))
101 newdam = perform_mission(x, y, victim, &mi[cn].queue,
102 MI_INTERDICT, s, SECT_HARDTARGET);
105 mpr(victim, "%s interdiction mission does %d damage!\n",
109 collateral_damage(x, y, dam, 0);
115 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
123 getsect(x, y, §);
125 coll = ldround((double)dam * collateral_dam, 1);
128 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
129 xyas(x, y, sect.sct_own), coll);
130 sectdamage(§, coll, list);
138 only_subs(struct emp_qelem *list)
140 struct emp_qelem *qp;
144 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
145 glp = (struct genlist *)qp;
147 if (glp->type != EF_SHIP)
149 mcp = (struct mchrstr *)glp->cp;
150 if (!(mcp->m_flags & M_SUB))
154 /* They were all subs! */
160 * Interdict ships & land units
164 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
169 struct genlist mi[MAXNOC];
173 memset(mi, 0, sizeof(mi));
174 for (z = 1; z < MAXNOC; z++)
175 emp_initque((struct emp_qelem *)&mi[z]);
177 build_mission_list(mi, x, y, mission, victim);
179 for (cn = 1; cn < MAXNOC; cn++) {
182 if (mission == MI_SINTERDICT) {
183 if (getrel(getnatp(cn), victim) >= FRIENDLY)
185 } else if (getrel(getnatp(cn), victim) > HOSTILE)
188 if (QEMPTY(&mi[cn].queue))
191 osubs = only_subs(&mi[cn].queue);
192 newdam = perform_mission(x, y, victim, &mi[cn].queue,
193 mission, s, hardtarget);
196 /* If only subs responded, then we don't know who's
200 "Enemy interdiction mission does %d damage!\n",
204 "%s interdiction mission does %d damage!\n",
210 collateral_damage(x, y, dam, 0);
216 * Perform a mission against victim, on behalf of actee
219 off_support(coord x, coord y, natid victim, natid actee)
222 struct genlist mi[MAXNOC];
225 memset(mi, 0, sizeof(mi));
226 for (z = 1; z < MAXNOC; z++)
227 emp_initque((struct emp_qelem *)&mi[z]);
229 build_mission_list(mi, x, y, MI_SUPPORT, victim);
230 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
232 dam = dosupport(mi, x, y, victim, actee);
237 * Perform a mission against victim, on behalf of actee
240 def_support(coord x, coord y, natid victim, natid actee)
243 struct genlist mi[MAXNOC];
246 memset(mi, 0, sizeof(mi));
247 for (z = 1; z < MAXNOC; z++)
248 emp_initque((struct emp_qelem *)&mi[z]);
250 build_mission_list(mi, x, y, MI_SUPPORT, victim);
251 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
253 dam = dosupport(mi, x, y, victim, actee);
258 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
264 for (cn = 1; cn < MAXNOC; cn++) {
265 rel = getrel(getnatp(cn), actee);
266 if ((cn != actee) && (rel != ALLIED))
268 rel = getrel(getnatp(cn), victim);
269 if ((cn != actee) && (rel != AT_WAR))
272 if (QEMPTY(&mi[cn].queue))
275 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
276 "", SECT_HARDTARGET);
282 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
285 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
286 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
287 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
291 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
292 int type, natid victim)
309 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
310 size = max(size,sizeof(struct plnstr));
311 block = malloc(size);
313 size = sizeof(u_block);
314 block = (s_char *)&u_block;
316 snxtitem_all(&ni, type);
317 while (nxtitem(&ni, block)) {
318 gp = (struct genitem *)block;
326 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
329 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
330 (gp->mission != MI_INTERDICT))
333 relat = getrel(getnatp(gp->own), victim);
334 if (mission == MI_SINTERDICT) {
335 if (relat >= FRIENDLY)
337 else if (type != EF_PLANE && relat > HOSTILE)
339 } else if (relat > HOSTILE)
342 dist = mapdist(x, y, gp->opx, gp->opy);
345 if (mission != MI_RESERVE) /* XXX */
346 oprange(gp, type, &radius);
351 /* Ok, it is within the operations range. */
352 /* Now check from where the object actually is */
353 dist = mapdist(x, y, gp->x, gp->y);
355 oprange(gp, type, &radius);
358 /* Ok, the object can get to where the x,y is */
361 if (mission != MI_AIR_DEFENSE) {
362 getsect(x, y, §);
363 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
366 * If the player->owner of the unit isn't at war
367 * with the victim, and doesn't own the
368 * sect being acted upon, and isn't the
369 * old player->owner of that sect, bounce them.
371 if (sect.sct_type != SCT_WATER &&
372 sect.sct_own != gp->own &&
373 sect.sct_oldown != gp->own)
379 glp = malloc(sizeof(struct genlist));
380 memset(glp, 0, sizeof(struct genlist));
386 glp->cp = (s_char *)&lchr[(int)gp->type];
389 glp->cp = (s_char *)&mchr[(int)gp->type];
392 glp->cp = (s_char *)&plchr[(int)gp->type];
395 glp->thing = malloc(size);
396 memcpy(glp->thing, block, size);
397 emp_insque(&glp->queue, &mi[gp->own].queue);
402 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
409 snxtitem_all(&ni, EF_PLANE);
410 while (nxtitem(&ni, &plane)) {
411 if (plane.pln_own != cn)
414 if (plane.pln_mission != MI_ESCORT)
417 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
419 if (dist > ((int)((float)plane.pln_range / 2.0)))
422 plp = malloc(sizeof(struct plist));
423 memset(plp, 0, sizeof(struct plist));
424 plp->pcp = &plchr[(int)plane.pln_type];
426 emp_insque(&plp->queue, escorts);
431 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
432 int mission, s_char *s, int hardtarget)
434 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
435 struct emp_qelem *newqp;
443 struct plchrstr *pcp;
444 int dam = 0, dam2, mission_flags, tech;
445 natid plane_owner = 0;
446 int gun, shell, md, air_dam = 0;
447 double range2, prb, range, mobcost, hitchance;
449 getsect(x, y, §);
451 emp_initque(&missiles);
452 emp_initque(&bombers);
453 emp_initque(&escorts);
456 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
457 glp = (struct genlist *)qp;
458 gp = (struct genitem *)glp->thing;
460 md = mapdist(x, y, gp->x, gp->y);
462 if (glp->type == EF_LAND) {
463 lp = (struct lndstr *)glp->thing;
465 if (lp->lnd_effic < LAND_MINFIREEFF)
468 if (mission == MI_SINTERDICT)
471 if ((mission == MI_INTERDICT) &&
472 (md > land_max_interdiction_range))
475 if (md > (lp->lnd_frg / 2))
478 if (lnd_getmil(lp) < 1)
481 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
482 range2 = (double)roundrange(range);
487 shell = lp->lnd_item[I_SHELL];
488 gun = lp->lnd_item[I_GUN];
489 if (shell == 0 || gun == 0)
492 if (has_supply(lp)) {
494 putland(lp->lnd_uid, lp);
495 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
496 lp->lnd_ammo, shell), 1);
497 if (sect.sct_type == SCT_WATER) {
498 double dam3 = (double)dam2;
499 if (chance(((double)lp->lnd_acc) / 100.0))
500 dam2 = ldround((dam3 / 2.0), 1);
503 if (sect.sct_type == SCT_WATER)
504 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
506 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
508 "%s fires at %s %s at %s\n",
509 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
511 mpr(victim, "%s %s fires at you at %s\n",
512 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
514 } else if (glp->type == EF_SHIP) {
515 sp = (struct shpstr *)glp->thing;
516 mcp = (struct mchrstr *)glp->cp;
518 if (sp->shp_effic < 60)
520 if (sp->shp_frnge == 0)
522 if (((mission == MI_INTERDICT) ||
523 (mission == MI_SINTERDICT)) &&
524 (md > ship_max_interdiction_range))
526 if (sp->shp_item[I_MILIT] < 1)
529 if ((mcp->m_flags & M_SUB) &&
530 (sect.sct_type != SCT_WATER))
533 if (mission == MI_SINTERDICT) {
534 if (!(mcp->m_flags & M_SONAR))
536 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
538 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
539 range2 *= (double)sp->shp_effic / 200.0;
542 /* can't look all the time */
546 if (mcp->m_flags & M_SUB) {
547 /* If we aren't shooting at "subs" or "ships" don't fire at all from
551 if (sp->shp_mobil < 0)
553 gun = sp->shp_item[I_GUN];
556 shell = sp->shp_item[I_SHELL];
557 if (shell < SHP_TORP_SHELLS)
558 shell += supply_commod(sp->shp_own,
559 sp->shp_x, sp->shp_y, I_SHELL,
560 SHP_TORP_SHELLS - shell);
561 if (shell < SHP_TORP_SHELLS)
564 range = sp->shp_effic * techfact(sp->shp_tech,
565 ((double)sp->shp_frnge)) /
568 range2 = (double)roundrange(range);
572 if (!line_of_sight((s_char **)0, x, y, gp->x, gp->y))
574 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
575 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
576 mobcost = (480.0 / (mobcost +
577 techfact(sp->shp_tech, mobcost)));
578 sp->shp_mobil -= mobcost;
579 putship(sp->shp_uid, sp);
580 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
583 "%s locking on %s %s in %s\n",
584 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
586 "\tEffective torpedo range is %.1f\n", range);
588 "\tWhooosh... Hitchance = %d%%\n",
589 (int)(hitchance * 100));
591 if (hitchance < 1.0 && !chance(hitchance)) {
592 wu(0, sp->shp_own, "\tMissed\n");
594 "Incoming torpedo sighted @ %s missed (whew)!\n",
598 wu(0, sp->shp_own, "\tBOOM!...\n");
599 dam2 = TORP_DAMAGE();
602 nreport(victim, N_TORP_SHIP, 0, 1);
604 "\tTorpedo hit %s %s for %d damage\n",
605 cname(victim), s, dam2);
608 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
609 xyas(x, y, victim), dam2);
611 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
612 range2 = (double)roundrange(range);
615 gun = sp->shp_item[I_GUN];
616 gun = min(gun, sp->shp_glim);
617 shell = sp->shp_item[I_SHELL];
619 shell += supply_commod(sp->shp_own,
620 sp->shp_x, sp->shp_y, I_SHELL,
622 gun = min(gun, shell);
623 gun = min(gun, sp->shp_item[I_MILIT] / 2.0);
627 dam2 = seagun(sp->shp_effic, gun);
631 prb = ((double)md) / range2;
634 dam2 = (int)((float)dam2 / 2.0);
636 if (sect.sct_type == SCT_WATER)
637 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
639 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
641 "%s fires at %s %s at %s\n",
642 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
644 mpr(victim, "%s %s fires at you at %s\n",
645 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
647 sp->shp_item[I_SHELL] = shell - gun;
648 putship(sp->shp_uid, sp);
650 } else if (glp->type == EF_PLANE) {
651 pcp = (struct plchrstr *)glp->cp;
652 if (pcp->pl_flags & P_M)
653 /* units have their own missile interdiction */
654 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
657 /* save planes for later */
658 plp = malloc(sizeof(struct plist));
660 memset(plp, 0, sizeof(struct plist));
662 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
663 if (plp->pcp->pl_flags & P_M)
664 emp_insque(&plp->queue, &missiles);
666 emp_insque(&plp->queue, &bombers);
667 plane_owner = plp->plane.pln_own;
670 if (!QEMPTY(&missiles)) {
671 /* I arbitrarily chose 100 mindam -KHS */
673 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
674 "sector", victim, mission);
675 qp = missiles.q_forw;
676 while (qp != (&missiles)) {
684 if (QEMPTY(&bombers)) {
687 glp = (struct genlist *)qp;
696 * If there are planes performing an
697 * interdict or support mission, find
698 * some escorts for them, if possible.
699 * Up to 2 per bomber, if possible.
701 find_escorts(x, y, plane_owner, &escorts);
703 if (mission == MI_SINTERDICT)
704 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
706 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
708 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
710 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
711 plp = (struct plist *)qp;
712 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
713 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
716 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
720 air = (struct airport *)qp;
721 md = mapdist(x, y, air->x, air->y);
726 /* Split off the bombers at this base into b */
727 divide(&bombers, &b, air->x, air->y);
729 /* Split off the escorts at this base into e */
730 divide(&escorts, &e, air->x, air->y);
734 mission_flags |= P_X; /* stealth (shhh) */
735 mission_flags |= P_H; /* gets turned off if not all choppers */
737 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
738 0, mission_flags, &tech);
744 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
745 P_F | P_ESC, mission_flags, &tech);
747 BestAirPath(pp, air->x, air->y, x, y);
748 wu(0, plane_owner, "Flying %s mission from %s\n",
749 mission_name(mission), xyas(air->x, air->y, plane_owner));
750 if (air->own && (air->own != plane_owner)) {
751 wu(0, air->own, "%s is flying %s mission from %s\n",
752 cname(plane_owner), mission_name(mission),
753 xyas(air->x, air->y, air->own));
756 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
760 air_damage(&b, x, y, mission, victim, s, hardtarget);
768 if (sect.sct_type == SCT_WATER)
769 nreport(plane_owner, N_SHP_BOMB, victim, 1);
771 nreport(plane_owner, N_SCT_BOMB, victim, 1);
774 /* free up all this memory */
777 glp = (struct genlist *)qp;
785 while (qp != (&escorts)) {
793 while (qp != (&bombers)) {
804 cando(int mission, int type)
808 if (type == EF_PLANE)
812 if (type == EF_PLANE)
816 if ((type == EF_PLANE) || (type == EF_SHIP))
824 if (type == EF_PLANE)
837 nameofitem(struct genitem *gp, int type)
841 return prship((struct shpstr *)gp);
843 return prplane((struct plnstr *)gp);
845 return prland((struct lndstr *)gp);
851 mission_name(short int mission)
855 return "an interdiction";
859 return "a offensive support";
861 return "a defensive support";
867 return "a sub interdiction";
869 return "an air defense";
871 return "a mysterious";
875 show_mission(int type, struct nstr_item *np)
877 int size, first = 1, radius;
881 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
882 size = max(size, sizeof(struct shpstr));
883 block = malloc(size);
885 while (nxtitem(np, block)) {
886 gp = (struct genitem *)block;
887 if (!player->owner || gp->own == 0)
891 pr("Thing x,y op-sect rad mission\n");
894 pr("%-25s", nameofitem(gp, type));
895 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
896 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
897 gp->mission == MI_OSUPPORT ||
898 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
900 oprange(gp, type, &radius);
901 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
902 if (radius < gp->radius)
905 pr(" %4d", gp->radius);
906 } else if (gp->mission == MI_RESERVE) {
910 getsect(gp->x, gp->y, §);
911 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
914 if (((struct lndstr *)block)->lnd_rad_max == 0)
917 plus += ((struct lndstr *)block)->lnd_rad_max;
918 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
920 } else if (gp->mission == MI_ESCORT) {
923 ((float)((struct plnstr *)block)->pln_range / 2.0)
928 pr(" is on %s mission\n", mission_name(gp->mission));
930 pr(" has no mission.\n");
935 oprange(struct genitem *gp, int type, int *radius)
944 getship(gp->uid, &ship);
945 range = ldround(techfact(gp->tech,
946 (double)ship.shp_frnge / 2.0), 1);
949 getland(gp->uid, &land);
950 range = ldround(techfact((int)land.lnd_tech,
951 (double)land.lnd_frg / 2.0), 1);
954 getplane(gp->uid, &plane);
955 /* missiles go one way, so we can use all the range */
956 if (plchr[(int)plane.pln_type].pl_flags & P_M)
957 range = plane.pln_range;
959 range = ldround((double)plane.pln_range / 2.0, 1);;
963 if ((*radius) > range)
970 * Remove all planes who cannot go on
971 * the mission from the plane list.
974 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
977 struct emp_qelem *qp, *next;
982 struct plchrstr *pcp;
987 for (qp = list->q_forw; qp != list; qp = next) {
989 plp = (struct plist *)qp;
993 if (pp->pln_effic < 40) {
999 if (pp->pln_mobil < 1) {
1006 if (ontradingblock(EF_PLANE, (int *)pp)) {
1016 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1018 if ((wantflags & y) == y)
1019 if ((pcp->pl_flags & y) != y) {
1041 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1045 if ((pcp->pl_flags & P_E) ||
1046 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1058 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1060 if ((nowantflags & y) == y)
1061 if ((pcp->pl_flags & y) == y)
1070 if (pp->pln_ship >= 0) {
1071 if (!getship(pp->pln_ship, &ship)) {
1074 putplane(pp->pln_uid, pp);
1079 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1082 if (ship.shp_effic < SHIP_MINEFF) {
1085 /* Can't fly off of inefficient or non-owned, non-allied ships */
1086 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1087 ((ship.shp_own != pp->pln_own) &&
1088 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1094 if (pp->pln_land >= 0) {
1095 if (!getland(pp->pln_land, &land)) {
1098 putplane(pp->pln_uid, pp);
1103 if (!(pcp->pl_flags & P_E))
1105 if (land.lnd_effic < LAND_MINEFF)
1108 /* Can't fly off of inefficient or non-owned, non-allied units */
1109 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1110 ((land.lnd_own != pp->pln_own) &&
1111 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1117 /* Can't fly off units in ships or other units */
1118 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1124 /* Now, check the sector status if not on a plane or unit */
1125 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1126 /* If we can't get the sector, we can't check it, and can't fly */
1127 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1132 /* First, check allied status */
1133 /* Can't fly from non-owned sectors or non-allied sectors */
1134 if ((sect.sct_own != pp->pln_own) &&
1135 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1140 /* non-vtol plane */
1141 if ((pcp->pl_flags & P_V) == 0) {
1142 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1149 if (pcp->pl_flags & P_A) {
1150 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1157 putplane(pp->pln_uid, pp);
1162 * Arm only the planes at x,y
1166 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1167 int mission, struct ichrstr *ip, int flags,
1168 int mission_flags, int *tech)
1170 struct emp_qelem *qp;
1171 struct emp_qelem *next;
1176 for (qp = list->q_forw; qp != list; qp = next) {
1178 plp = (struct plist *)qp;
1180 if (plp->plane.pln_x != x)
1182 if (plp->plane.pln_y != y)
1185 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1190 if (flags & (P_S | P_I)) {
1191 if (plp->pcp->pl_flags & P_S)
1192 mission_flags |= P_S;
1193 if (plp->pcp->pl_flags & P_I)
1194 mission_flags |= P_I;
1196 if (*tech > plp->plane.pln_tech)
1197 *tech = plp->plane.pln_tech;
1198 if (!(plp->pcp->pl_flags & P_H))
1199 /* no stealth on this mission */
1200 mission_flags &= ~P_H;
1201 if (!(plp->pcp->pl_flags & P_X))
1202 /* no stealth on this mission */
1203 mission_flags &= ~P_X;
1204 if (!(plp->pcp->pl_flags & P_A)) {
1205 /* no asw on this mission */
1206 mission_flags &= ~P_A;
1208 if (!(plp->pcp->pl_flags & P_MINE)) {
1209 /* no asw on this mission */
1210 mission_flags &= ~P_MINE;
1214 * Mob costs for missions are 1/2 normal
1217 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1218 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1221 return mission_flags;
1225 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1228 struct plchrstr *pcp;
1240 if (pp->pln_ship >= 0) {
1241 getship(pp->pln_ship, &ship);
1242 item = ship.shp_item;
1243 } else if (pp->pln_land >= 0) {
1244 getland(pp->pln_land, &land);
1245 item = land.lnd_item;
1247 getsect(pp->pln_x, pp->pln_y, §);
1248 item = sect.sct_item;
1250 if (pcp->pl_fuel > item[I_PETROL]) {
1253 item[I_PETROL] -= pcp->pl_fuel;
1255 if (!(flags & P_F)) {
1261 if (pp->pln_nuketype == -1) {
1263 needed = pp->pln_load;
1267 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1269 itype = ip->i_vtype;
1270 needed = (pp->pln_load * 2) / ip->i_lbs;
1273 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1275 itype = ip->i_vtype;
1276 needed = (pp->pln_load * 2) / ip->i_lbs;
1279 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1282 needed = pp->pln_load / ip->i_lbs;
1285 if (pp->pln_nuketype == -1)
1288 case 'i': /* missile interception */
1291 needed = pp->pln_load;
1297 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1300 if (itype != I_NONE) {
1301 if (itype == I_SHELL && item[itype] < needed)
1302 item[itype] += supply_commod(plp->plane.pln_own,
1306 if (item[itype] < needed)
1308 item[itype] -= needed;
1310 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1311 plp->bombs = needed;
1315 if (pp->pln_ship >= 0)
1316 putship(ship.shp_uid, &ship);
1317 else if (pp->pln_land >= 0)
1318 putland(land.lnd_uid, &land);
1325 * Return 1 if this x,y pair is in the list
1328 find_airport(struct emp_qelem *airp, coord x, coord y)
1330 struct emp_qelem *qp;
1333 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1334 a = (struct airport *)qp;
1335 if ((a->x == x) && (a->y == y))
1342 /* #*# This needs to be changed to include acc's -KHS */
1344 add_airport(struct emp_qelem *airp, coord x, coord y)
1349 a = malloc(sizeof(struct airport));
1353 getsect(x, y, §);
1354 a->own = sect.sct_own;
1356 emp_insque((struct emp_qelem *)a, airp);
1360 * Take all the planes in list 1 that
1361 * are at x,y, and put them into list 2.
1364 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1366 struct emp_qelem *qp, *next;
1369 for (qp = l1->q_forw; qp != l1; qp = next) {
1371 plp = (struct plist *)qp;
1373 if (plp->plane.pln_x != x)
1375 if (plp->plane.pln_y != y)
1384 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1385 natid victim, s_char *s, int hardtarget)
1387 struct emp_qelem *qp;
1390 int newdam, dam = 0;
1394 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1395 plp = (struct plist *)qp;
1398 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1405 if (plp->pcp->pl_flags & P_A) {
1406 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1408 "\t%s detects sub movement in %s\n",
1409 prplane(pp), xyas(x, y, pp->pln_own));
1412 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1414 "\t%s tracks %s %s at %s\n",
1415 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1419 "\t%s depth-charging %s %s in %s\n",
1420 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1423 "\t%s pinbombing %s %s in %s\n",
1424 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1426 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1427 if (plp->plane.pln_nuketype != -1)
1429 else if (hardtarget != SECT_HARDTARGET)
1430 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1431 /* Always calculate damage */
1432 if (roll(100) <= hitchance) {
1433 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1435 if (mission == MI_INTERDICT) {
1437 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1438 prplane(pp), nukedam, cname(victim), s);
1443 "\t\thit %s %s for %d damage\n",
1444 cname(victim), s, newdam);
1448 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1449 wu(0, pp->pln_own, "missed\n");
1450 if (mission == MI_SINTERDICT) {
1452 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1453 xyas(x, y, victim));
1454 } else if (*s == 's') {
1456 "SPLASH! Bombs miss your %s in %s\n",
1457 s, xyas(x, y, victim));
1459 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1460 s, xyas(x, y, victim));
1462 /* Now, even though we missed, the bombs
1464 collateral_damage(x, y, newdam, bombers);
1467 /* use up missiles */
1468 if (plp->pcp->pl_flags & P_M) {
1469 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1479 * Check to see if anyone hostile to the victim
1480 * is running an air defense mission on this
1481 * sector. If so, do air combat
1484 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1485 struct emp_qelem *esc_list)
1488 int mission_flags, tech, combat = 0, rel, dist, z;
1489 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1491 struct genlist *glp;
1493 struct genlist mi[MAXNOC];
1499 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1501 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1504 memset(mi, 0, sizeof(mi));
1505 for (z = 1; z < MAXNOC; z++)
1506 emp_initque((struct emp_qelem *)&mi[z]);
1508 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1510 for (cn = 1; cn < MAXNOC; cn++) {
1511 /* Check our relations */
1512 rel = getrel(getnatp(cn), victim);
1517 if (QEMPTY(&mi[cn].queue))
1520 /* Ok, make a list of all the interceptors. Note that this *copies* the
1521 * list from the mission creation. This list must be deleted later. */
1522 emp_initque(&interceptors);
1523 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1526 glp = (struct genlist *)qp;
1527 gp = (struct genitem *)glp->thing;
1528 plp = (struct plist *)qp;
1530 dist = mapdist(x, y, gp->x, gp->y);
1532 plp = malloc(sizeof(struct plist));
1533 memset(plp, 0, sizeof(struct plist));
1534 plp->pcp = (struct plchrstr *)glp->cp;
1535 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1537 /* missiles go one way, so we can use all the range */
1538 if (!(plp->pcp->pl_flags & P_M))
1540 /* If it's out of range, free it and continue on */
1541 if (dist > plp->plane.pln_range) {
1545 emp_insque(&plp->queue, &interceptors);
1548 /* Remove those who cannot go */
1549 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1551 if (QEMPTY(&interceptors))
1554 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1555 * that the higher numbered planes go into battle (they should be the better ones
1556 * at fighting, if all went well.) */
1558 for (qp = interceptors.q_forw; qp != (&interceptors);
1561 tcount -= (count * 2);
1562 /* Just in case there are more incoming than we have */
1565 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1569 /* Free it up and continue */
1571 glp = (struct genlist *)qp;
1576 /* Now, make a list of all the airports these planes are coming from */
1578 for (qp = interceptors.q_forw; qp != (&interceptors);
1580 plp = (struct plist *)qp;
1581 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1582 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1585 /* Now, fly them out one airport at a time */
1586 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1587 struct airport *air;
1589 air = (struct airport *)qp;
1590 dist = mapdist(x, y, air->x, air->y);
1594 /* Split off the interceptors at this base into i */
1595 divide(&interceptors, &i, air->x, air->y);
1599 mission_flags |= P_X; /* stealth (shhh) */
1600 /* gets turned off if not all choppers */
1601 mission_flags |= P_H;
1602 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1603 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1605 /* Did we run out of interceptors? */
1608 /* Did we run out of bombers? */
1609 if (QEMPTY(bomb_list)) {
1610 /* Yes, so we have to put the rest of the interceptors back, and
1611 then continue, or we leak memory */
1616 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1617 mission_flags, &tech);
1619 /* Did we run out of interceptors? */
1622 /* Did we run out of bombers? */
1623 if (QEMPTY(bomb_list)) {
1624 /* Yes, so we have to put the rest of the interceptors back, and
1625 then continue, or we leak memory */
1630 BestAirPath(path, air->x, air->y, x, y);
1631 wu(0, cn, "Flying %s mission from %s\n",
1632 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1633 if (air->own && (air->own != cn)) {
1634 wu(0, air->own, "%s is flying %s mission from %s\n",
1635 cname(cn), mission_name(MI_AIR_DEFENSE),
1636 xyas(air->x, air->y, air->own));
1639 /* Now, fly the planes to the sector */
1640 emp_initque(&empty);
1641 ac_encounter(&i, &empty, air->x, air->y,
1642 path, mission_flags, 1, bomb_list, esc_list);
1644 /* If none made it, continue */
1648 /* Some made it, so now they get to try to fight. */
1649 /* Intercept the escorts first */
1651 if (!QEMPTY(esc_list)) {
1652 mpr(victim, "%s air defense planes intercept!\n",
1654 ac_combat_headers(victim, cn);
1655 ac_airtoair(esc_list, &i);
1658 /* Now intercept the bombers */
1659 if (!QEMPTY(bomb_list)) {
1661 mpr(victim, "%s air defense planes intercept!\n",
1663 ac_combat_headers(victim, cn);
1665 ac_airtoair(bomb_list, &i);
1674 /* We have to free all of these, if they are still there, otherwise they get
1675 lost and we leak memory all over the place. */
1676 for (cn = 1; cn < MAXNOC; cn++) {
1677 /* free up all this memory if it's still there */
1678 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1680 glp = (struct genlist *)qp;