2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, 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
57 #include "prototypes.h"
61 struct emp_qelem queue;
66 s_char *mission_name(short int mission);
67 s_char *nameofitem(s_char *buf, struct genitem *gp, int type);
70 * Interdict commodities & transported planes
73 ground_interdict(coord x, coord y, natid victim, s_char *s)
76 int dam = 0, newdam, rel;
77 struct genlist mi[MAXNOC];
80 bzero((s_char *)mi, sizeof(mi));
81 for (z = 1; z < MAXNOC; z++)
82 emp_initque((struct emp_qelem *)&mi[z]);
84 build_mission_list(mi, x, y, MI_INTERDICT, victim);
86 for (cn = 1; cn < MAXNOC; cn++) {
87 rel = getrel(getnatp(cn), victim);
91 if (QEMPTY(&mi[cn].queue))
94 newdam = perform_mission(x, y, victim, &mi[cn].queue,
95 MI_INTERDICT, s, SECT_HARDTARGET);
98 mpr(victim, "%s interdiction mission does %d damage!\n",
102 collateral_damage(x, y, dam, 0);
108 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
110 extern double collateral_dam;
117 getsect(x, y, §);
119 coll = ldround((double)dam * collateral_dam, 1);
122 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
123 xyas(x, y, sect.sct_own), coll);
124 sectdamage(§, coll, list);
132 only_subs(struct emp_qelem *list)
134 struct emp_qelem *qp;
139 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
140 glp = (struct genlist *)qp;
141 gp = (struct genitem *)glp->thing;
143 if (glp->type != EF_SHIP)
145 mcp = (struct mchrstr *)glp->cp;
146 if (!(mcp->m_flags & M_SUB))
150 /* They were all subs! */
156 * Interdict ships & land units
160 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
165 struct genlist mi[MAXNOC];
169 bzero((s_char *)mi, sizeof(mi));
170 for (z = 1; z < MAXNOC; z++)
171 emp_initque((struct emp_qelem *)&mi[z]);
173 build_mission_list(mi, x, y, mission, victim);
175 for (cn = 1; cn < MAXNOC; cn++) {
178 if (mission == MI_SINTERDICT) {
179 if (getrel(getnatp(cn), victim) >= FRIENDLY)
181 } else if (getrel(getnatp(cn), victim) > HOSTILE)
184 if (QEMPTY(&mi[cn].queue))
187 osubs = only_subs(&mi[cn].queue);
188 newdam = perform_mission(x, y, victim, &mi[cn].queue,
189 mission, s, hardtarget);
192 /* If only subs responded, then we don't know who's
196 "Enemy interdiction mission does %d damage!\n",
200 "%s interdiction mission does %d damage!\n",
206 collateral_damage(x, y, dam, 0);
212 * Perform a mission against victim, on behalf of actee
215 off_support(coord x, coord y, natid victim, natid actee)
218 struct genlist mi[MAXNOC];
221 bzero((s_char *)mi, sizeof(mi));
222 for (z = 1; z < MAXNOC; z++)
223 emp_initque((struct emp_qelem *)&mi[z]);
225 build_mission_list(mi, x, y, MI_SUPPORT, victim);
226 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
228 dam = dosupport(mi, x, y, victim, actee);
233 * Perform a mission against victim, on behalf of actee
236 def_support(coord x, coord y, natid victim, natid actee)
239 struct genlist mi[MAXNOC];
242 bzero((s_char *)mi, sizeof(mi));
243 for (z = 1; z < MAXNOC; z++)
244 emp_initque((struct emp_qelem *)&mi[z]);
246 build_mission_list(mi, x, y, MI_SUPPORT, victim);
247 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
249 dam = dosupport(mi, x, y, victim, actee);
254 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
260 for (cn = 1; cn < MAXNOC; cn++) {
261 rel = getrel(getnatp(cn), actee);
262 if ((cn != actee) && (rel != ALLIED))
264 rel = getrel(getnatp(cn), victim);
265 if ((cn != actee) && (rel != AT_WAR))
268 if (QEMPTY(&mi[cn].queue))
271 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
272 "", SECT_HARDTARGET);
278 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
281 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
282 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
283 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
287 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
288 int type, natid victim)
305 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
306 size = max(size,sizeof(struct plnstr));
307 block = (s_char *)malloc(size);
309 size = sizeof(u_block);
310 block = (s_char *)&u_block;
312 snxtitem_all(&ni, type);
313 while (nxtitem(&ni, block)) {
314 gp = (struct genitem *)block;
322 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
325 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
326 (gp->mission != MI_INTERDICT))
329 relat = getrel(getnatp(gp->own), victim);
330 if (mission == MI_SINTERDICT) {
331 if (relat >= FRIENDLY)
333 else if (type != EF_PLANE && relat > HOSTILE)
335 } else if (relat > HOSTILE)
338 dist = mapdist(x, y, gp->opx, gp->opy);
341 if (mission != MI_RESERVE) /* XXX */
342 oprange(gp, type, &radius);
347 /* Ok, it is within the operations range. */
348 /* Now check from where the object actually is */
349 dist = mapdist(x, y, gp->x, gp->y);
351 oprange(gp, type, &radius);
354 /* Ok, the object can get to where the x,y is */
357 if (mission != MI_AIR_DEFENSE) {
358 getsect(x, y, (s_char *)§);
359 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
362 * If the player->owner of the unit isn't at war
363 * with the victim, and doesn't own the
364 * sect being acted upon, and isn't the
365 * old player->owner of that sect, bounce them.
367 if (sect.sct_type != SCT_WATER &&
368 sect.sct_own != gp->own &&
369 sect.sct_oldown != gp->own)
375 glp = (struct genlist *)malloc(sizeof(struct genlist));
376 bzero((s_char *)glp, sizeof(struct genlist));
382 glp->cp = (s_char *)&lchr[(int)gp->type];
385 glp->cp = (s_char *)&mchr[(int)gp->type];
388 glp->cp = (s_char *)&plchr[(int)gp->type];
391 glp->thing = (s_char *)malloc(size);
392 bcopy(block, glp->thing, size);
393 emp_insque(&glp->queue, &mi[gp->own].queue);
398 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
405 snxtitem_all(&ni, EF_PLANE);
406 while (nxtitem(&ni, (s_char *)&plane)) {
407 if (plane.pln_own != cn)
410 if (plane.pln_mission != MI_ESCORT)
413 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
415 if (dist > ((int)((float)plane.pln_range / 2.0)))
418 plp = (struct plist *)malloc(sizeof(struct plist));
419 bzero((s_char *)plp, sizeof(struct plist));
420 plp->pcp = &plchr[(int)plane.pln_type];
421 bcopy((s_char *)&plane, (s_char *)&plp->plane,
422 sizeof(struct plnstr));
423 emp_insque(&plp->queue, escorts);
428 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
429 int mission, s_char *s, int hardtarget)
431 extern int land_max_interdiction_range;
432 extern int ship_max_interdiction_range;
433 s_char *mission_name(short int mission), *nameofitem(s_char *buf,
436 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
437 struct emp_qelem *newqp;
446 struct plchrstr *pcp;
447 int dam = 0, dam2, mission_flags, tech;
448 natid plane_owner = 0;
449 int gun, shell, md, air_dam = 0;
450 double range2, prb, range, mobcost, hitchance;
451 extern int torpedo_damage;
453 getsect(x, y, §);
455 emp_initque(&missiles);
456 emp_initque(&bombers);
457 emp_initque(&escorts);
460 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
461 glp = (struct genlist *)qp;
462 gp = (struct genitem *)glp->thing;
464 md = mapdist(x, y, gp->x, gp->y);
466 if (glp->type == EF_LAND) {
467 lp = (struct lndstr *)glp->thing;
468 lcp = (struct lchrstr *)glp->cp;
470 if (lp->lnd_effic < LAND_MINFIREEFF)
473 if (mission == MI_SINTERDICT)
476 if ((mission == MI_INTERDICT) &&
477 (md > land_max_interdiction_range))
480 if (md > (lp->lnd_frg / 2))
483 if (lnd_getmil(lp) < 1)
486 range = techfact((int)lp->lnd_tech, (double)lp->lnd_frg / 2.0);
487 range2 = (double)roundrange(range);
492 shell = getvar(V_SHELL, (s_char *)lp, EF_LAND);
493 gun = getvar(V_GUN, (s_char *)lp, EF_LAND);
494 if (shell == 0 || gun == 0)
497 if (has_supply(lp)) {
499 putland(lp->lnd_uid, lp);
500 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
501 lp->lnd_ammo, shell), 1);
502 if (sect.sct_type == SCT_WATER) {
503 double dam3 = (double)dam2;
504 if (chance(((double)lp->lnd_acc) / 100.0))
505 dam2 = ldround((dam3 / 2.0), 1);
508 if (sect.sct_type == SCT_WATER)
509 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
511 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
513 "%s fires at %s %s at %s\n",
514 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
516 mpr(victim, "%s %s fires at you at %s\n",
517 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
519 } else if (glp->type == EF_SHIP) {
520 sp = (struct shpstr *)glp->thing;
521 mcp = (struct mchrstr *)glp->cp;
523 if (sp->shp_effic < 60)
525 if (sp->shp_frnge == 0)
527 if (((mission == MI_INTERDICT) ||
528 (mission == MI_SINTERDICT)) &&
529 (md > ship_max_interdiction_range))
531 if (getvar(V_MILIT, (s_char *)sp, EF_SHIP) < 1)
534 if ((mcp->m_flags & M_SUB) &&
535 (sect.sct_type != SCT_WATER))
538 if (mission == MI_SINTERDICT) {
539 if (!(mcp->m_flags & M_SONAR))
541 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
543 range2 = techfact(sp->shp_tech, (double)mcp->m_vrnge);
544 range2 *= (double)sp->shp_effic / 200.0;
547 /* can't look all the time */
551 if (mcp->m_flags & M_SUB) {
552 /* If we aren't shooting at "subs" or "ships" don't fire at all from
556 if (sp->shp_mobil < (s_char)0)
558 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
561 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
563 shell += supply_commod(sp->shp_own,
564 sp->shp_x, sp->shp_y, I_SHELL,
569 range = sp->shp_effic * techfact(sp->shp_tech,
570 ((double)sp->shp_frnge)) /
573 range2 = (double)roundrange(range);
577 if (!line_of_sight((s_char **)0, x, y, gp->x, gp->y))
579 putvar(V_SHELL, shell - 3, (s_char *)sp, EF_SHIP);
580 mobcost = sp->shp_effic * 0.01 * sp->shp_speed;
581 mobcost = (480.0 / (mobcost +
582 techfact(sp->shp_tech, mobcost)));
583 sp->shp_mobil -= mobcost;
584 putship(sp->shp_uid, sp);
585 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
588 "%s locking on %s %s in %s\n",
589 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
591 "\tEffective torpedo range is %.1f\n", range);
593 "\tWhooosh... Hitchance = %d%%\n",
594 (int)(hitchance * 100));
596 if (hitchance < 1.0 && !chance(hitchance)) {
597 wu(0, sp->shp_own, "\tMissed\n");
599 "Incoming torpedo sighted @ %s missed (whew)!\n",
603 wu(0, sp->shp_own, "\tBOOM!...\n");
604 dam2 = TORP_DAMAGE();
607 nreport(victim, N_TORP_SHIP, sp->shp_own, 1);
609 "\tTorpedo hit %s %s for %d damage\n",
610 cname(victim), s, dam2);
613 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
614 xyas(x, y, victim), dam2);
616 range = techfact(sp->shp_tech, (double)mcp->m_frnge / 2.0);
617 range2 = (double)roundrange(range);
620 gun = getvar(V_GUN, (s_char *)sp, EF_SHIP);
621 gun = min(gun, sp->shp_glim);
622 shell = getvar(V_SHELL, (s_char *)sp, EF_SHIP);
624 shell += supply_commod(sp->shp_own,
625 sp->shp_x, sp->shp_y, I_SHELL,
627 gun = min(gun, shell);
628 gun = min(gun, (int)((float)getvar(V_MILIT,
634 dam2 = seagun(sp->shp_effic, gun);
638 prb = ((double)md) / range2;
641 dam2 = (int)((float)dam2 / 2.0);
643 if (sect.sct_type == SCT_WATER)
644 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
646 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
648 "%s fires at %s %s at %s\n",
649 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
651 mpr(victim, "%s fires at you at %s\n",
652 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
654 putvar(V_SHELL, shell - gun, (s_char *)sp, EF_SHIP);
655 putship(sp->shp_uid, sp);
657 } else if (glp->type == EF_PLANE) {
658 pcp = (struct plchrstr *)glp->cp;
659 if (pcp->pl_flags & P_M)
660 /* units have their own missile interdiction */
661 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
664 /* save planes for later */
665 plp = (struct plist *)malloc(sizeof(struct plist));
667 bzero((s_char *)plp, sizeof(struct plist));
669 bcopy(glp->thing, (s_char *)&plp->plane,
670 sizeof(struct plnstr));
671 if (plp->pcp->pl_flags & P_M)
672 emp_insque(&plp->queue, &missiles);
674 emp_insque(&plp->queue, &bombers);
675 plane_owner = plp->plane.pln_own;
678 if (!QEMPTY(&missiles)) {
679 /* I arbitrarily chose 100 mindam -KHS */
681 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
682 "sector", victim, mission);
683 qp = missiles.q_forw;
684 while (qp != (&missiles)) {
690 if (QEMPTY(&bombers)) {
693 glp = (struct genlist *)qp;
702 * If there are planes performing an
703 * interdict or support mission, find
704 * some escorts for them, if possible.
705 * Up to 2 per bomber, if possible.
707 find_escorts(x, y, plane_owner, &escorts);
709 if (mission == MI_SINTERDICT)
710 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
712 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
714 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
716 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
717 plp = (struct plist *)qp;
718 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
719 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
722 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
726 air = (struct airport *)qp;
727 md = mapdist(x, y, air->x, air->y);
732 /* Split off the bombers at this base into b */
733 divide(&bombers, &b, air->x, air->y);
735 /* Split off the escorts at this base into e */
736 divide(&escorts, &e, air->x, air->y);
740 mission_flags |= P_X; /* stealth (shhh) */
741 mission_flags |= P_H; /* gets turned off if not all choppers */
743 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
744 0, mission_flags, &tech);
750 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
751 P_F | P_ESC, mission_flags, &tech);
753 BestAirPath(pp, air->x, air->y, x, y);
754 wu(0, plane_owner, "Flying %s mission from %s\n",
755 mission_name(mission), xyas(air->x, air->y, plane_owner));
756 if (air->own && (air->own != plane_owner)) {
757 wu(0, air->own, "%s is flying %s mission from %s\n",
758 cname(plane_owner), mission_name(mission),
759 xyas(air->x, air->y, air->own));
762 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
766 air_damage(&b, x, y, mission, victim, s, hardtarget);
774 if (sect.sct_type == SCT_WATER)
775 nreport(plane_owner, N_SHP_BOMB, victim, 1);
777 nreport(plane_owner, N_SCT_BOMB, victim, 1);
780 /* free up all this memory */
783 glp = (struct genlist *)qp;
791 while (qp != (&escorts)) {
799 while (qp != (&bombers)) {
810 cando(int mission, int type)
814 if (type == EF_PLANE)
818 if (type == EF_PLANE)
822 if ((type == EF_PLANE) || (type == EF_SHIP))
830 if (type == EF_PLANE)
843 nameofitem(s_char *buf, struct genitem *gp, int type)
847 return prship((struct shpstr *)gp);
850 return prplane((struct plnstr *)gp);
853 return prland((struct lndstr *)gp);
859 mission_short_name(int mission)
884 mission_name(short int mission)
888 return "an interdiction";
892 return "a offensive support";
894 return "a defensive support";
900 return "a sub interdiction";
902 return "an air defense";
904 return "a mysterious";
908 show_mission(int type, struct nstr_item *np)
910 int size, first = 1, radius;
915 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
916 size = max(size, sizeof(struct shpstr));
917 block = (s_char *)malloc(size);
919 while (nxtitem(np, block)) {
920 gp = (struct genitem *)block;
921 if (!player->owner || gp->own == 0)
925 pr("Thing x,y op-sect rad mission\n");
928 pr("%-25s", nameofitem(buf, gp, type));
929 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
930 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
931 gp->mission == MI_OSUPPORT ||
932 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
934 oprange(gp, type, &radius);
935 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
936 if (radius < gp->radius)
939 pr(" %4d", gp->radius);
940 } else if (gp->mission == MI_RESERVE) {
944 getsect(gp->x, gp->y, §);
945 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
948 if (((struct lndstr *)block)->lnd_rad_max == 0)
951 plus += ((struct lndstr *)block)->lnd_rad_max;
952 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
954 } else if (gp->mission == MI_ESCORT) {
957 ((float)((struct plnstr *)block)->pln_range / 2.0)
962 pr(" is on %s mission\n", mission_name(gp->mission));
964 pr(" has no mission.\n");
969 oprange(struct genitem *gp, int type, int *radius)
971 double techfact(int, double);
979 getship(gp->uid, &ship);
980 range = ldround(techfact(gp->tech,
981 (double)ship.shp_frnge / 2.0), 1);
984 getland(gp->uid, &land);
985 range = ldround(techfact((int)land.lnd_tech,
986 (double)land.lnd_frg / 2.0), 1);
989 getplane(gp->uid, &plane);
990 /* missiles go one way, so we can use all the range */
991 if (plchr[(int)plane.pln_type].pl_flags & P_M)
992 range = plane.pln_range;
994 range = ldround((double)plane.pln_range / 2.0, 1);;
998 if ((*radius) > range)
1005 * Remove all planes who cannot go on
1006 * the mission from the plane list.
1009 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1012 struct emp_qelem *qp, *next;
1017 struct plchrstr *pcp;
1019 register int y, bad, bad1;
1022 for (qp = list->q_forw; qp != list; qp = next) {
1024 plp = (struct plist *)qp;
1028 if (pp->pln_effic < 40) {
1034 if (pp->pln_mobil < 1) {
1041 if (ontradingblock(EF_PLANE, (int *)pp)) {
1051 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1053 if ((wantflags & y) == y)
1054 if ((pcp->pl_flags & y) != y) {
1076 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1080 if ((pcp->pl_flags & P_E) ||
1081 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1093 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1095 if ((nowantflags & y) == y)
1096 if ((pcp->pl_flags & y) == y)
1105 if (pp->pln_ship >= 0) {
1106 if (!getship(pp->pln_ship, &ship)) {
1109 putplane(pp->pln_uid, pp);
1114 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1117 if (ship.shp_effic < SHIP_MINEFF) {
1120 /* Can't fly off of ships < 50%, or non-owned or non-allied ships */
1121 if ((ship.shp_effic < 50) ||
1122 ((ship.shp_own != pp->pln_own) &&
1123 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1129 if (pp->pln_land >= 0) {
1130 if (!getland(pp->pln_land, &land)) {
1133 putplane(pp->pln_uid, pp);
1138 if (!(pcp->pl_flags & P_E))
1140 if (land.lnd_effic < LAND_MINEFF)
1143 /* Can't fly off of units < 50%, or non-owned or non-allied units */
1144 if ((land.lnd_effic < 50) ||
1145 ((land.lnd_own != pp->pln_own) &&
1146 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1152 /* Can't fly off units in ships or other units */
1153 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1159 /* Now, check the sector status if not on a plane or unit */
1160 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1161 /* If we can't get the sector, we can't check it, and can't fly */
1162 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1167 /* First, check allied status */
1168 /* Can't fly from non-owned sectors or non-allied sectors */
1169 if ((sect.sct_own != pp->pln_own) &&
1170 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1175 /* non-vtol plane */
1176 if ((pcp->pl_flags & P_V) == 0) {
1177 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1184 if (pcp->pl_flags & P_A) {
1185 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1192 putplane(pp->pln_uid, pp);
1197 * Arm only the planes at x,y
1201 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1202 int mission, struct ichrstr *ip, int flags,
1203 int mission_flags, int *tech)
1205 struct emp_qelem *qp;
1206 struct emp_qelem *next;
1211 for (qp = list->q_forw; qp != list; qp = next) {
1213 plp = (struct plist *)qp;
1215 if (plp->plane.pln_x != x)
1217 if (plp->plane.pln_y != y)
1220 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1225 if (flags & (P_S | P_I)) {
1226 if (plp->pcp->pl_flags & P_S)
1227 mission_flags |= P_S;
1228 if (plp->pcp->pl_flags & P_I)
1229 mission_flags |= P_I;
1231 if (*tech > plp->plane.pln_tech)
1232 *tech = plp->plane.pln_tech;
1233 if (!(plp->pcp->pl_flags & P_H))
1234 /* no stealth on this mission */
1235 mission_flags &= ~P_H;
1236 if (!(plp->pcp->pl_flags & P_X))
1237 /* no stealth on this mission */
1238 mission_flags &= ~P_X;
1239 if (!(plp->pcp->pl_flags & P_A)) {
1240 /* no asw on this mission */
1241 mission_flags &= ~P_A;
1243 if (!(plp->pcp->pl_flags & P_MINE)) {
1244 /* no asw on this mission */
1245 mission_flags &= ~P_MINE;
1249 * Mob costs for missions are 1/2 normal
1252 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1253 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1256 return mission_flags;
1260 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1263 register struct plchrstr *pcp;
1277 if (pp->pln_ship >= 0) {
1278 getship(pp->pln_ship, &ship);
1280 ptr = (s_char *)&ship;
1281 } else if (pp->pln_land >= 0) {
1282 getland(pp->pln_land, &land);
1284 ptr = (s_char *)&land;
1286 getsect(pp->pln_x, pp->pln_y, §);
1288 ptr = (s_char *)§
1290 getvec(VT_ITEM, vec, ptr, type);
1291 if (pcp->pl_fuel > vec[I_PETROL]) {
1294 vec[I_PETROL] -= pcp->pl_fuel;
1296 if (!(flags & P_F)) {
1302 if (pp->pln_nuketype == -1) {
1304 needed = pp->pln_load;
1308 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1311 needed = (pp->pln_load * 2) / ip->i_lbs;
1314 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1317 needed = (pp->pln_load * 2) / ip->i_lbs;
1320 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1323 needed = pp->pln_load / ip->i_lbs;
1326 if (pp->pln_nuketype == -1)
1329 case 'i': /* missile interception */
1332 needed = pp->pln_load;
1338 if (rval < 0 || (item && needed <= 0)) {
1341 if ((vec[item] < needed) && (item == I_SHELL))
1342 vec[item] += supply_commod(plp->plane.pln_own,
1343 plp->plane.pln_x, plp->plane.pln_y,
1345 if (vec[item] < needed) {
1348 vec[item] -= needed;
1350 if (item == I_SHELL && (mission == 's' || mission == 'p'))
1351 plp->bombs = needed;
1355 putvec(VT_ITEM, vec, ptr, type);
1356 if (type == EF_SHIP)
1357 putship(ship.shp_uid, &ship);
1358 else if (type == EF_LAND)
1359 putland(land.lnd_uid, &land);
1366 * Return 1 if this x,y pair is in the list
1369 find_airport(struct emp_qelem *airp, coord x, coord y)
1371 struct emp_qelem *qp;
1374 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1375 a = (struct airport *)qp;
1376 if ((a->x == x) && (a->y == y))
1383 /* #*# This needs to be changed to include acc's -KHS */
1385 add_airport(struct emp_qelem *airp, coord x, coord y)
1390 a = (struct airport *)malloc(sizeof(struct airport));
1394 getsect(x, y, §);
1395 a->own = sect.sct_own;
1397 emp_insque((struct emp_qelem *)a, airp);
1401 * Take all the planes in list 1 that
1402 * are at x,y, and put them into list 2.
1405 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1407 struct emp_qelem *qp, *next;
1410 for (qp = l1->q_forw; qp != l1; qp = next) {
1412 plp = (struct plist *)qp;
1414 if (plp->plane.pln_x != x)
1416 if (plp->plane.pln_y != y)
1425 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1426 natid victim, s_char *s, int hardtarget)
1428 struct emp_qelem *qp;
1431 int newdam, dam = 0;
1435 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1436 plp = (struct plist *)qp;
1439 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1446 if (plp->pcp->pl_flags & P_A) {
1447 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1449 "\t%s detects sub movement in %s\n",
1450 prplane(pp), xyas(x, y, pp->pln_own));
1453 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1455 "\t%s tracks %s %s at %s\n",
1456 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1460 "\t%s depth-charging %s %s in %s\n",
1461 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1464 "\t%s pinbombing %s %s in %s\n",
1465 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1467 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1468 if (plp->plane.pln_nuketype != -1)
1470 else if (hardtarget != SECT_HARDTARGET)
1471 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1472 /* Always calculate damage */
1473 if (roll(100) <= hitchance) {
1474 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1476 if (mission == MI_INTERDICT) {
1478 "\t\tnuclear warhead does %d damage to %s %s\n",
1479 prplane(pp), nukedam, cname(victim), s);
1484 "\t\thit %s %s for %d damage\n",
1485 cname(victim), s, newdam);
1489 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1490 wu(0, pp->pln_own, "missed\n");
1491 if (mission == MI_SINTERDICT) {
1493 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1494 xyas(x, y, victim));
1495 } else if (*s == 's') {
1497 "SPLASH! Bombs miss your %s in %s\n",
1498 s, xyas(x, y, victim));
1500 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1501 s, xyas(x, y, victim));
1503 /* Now, even though we missed, the bombs
1505 collateral_damage(x, y, newdam, bombers);
1508 /* use up missiles */
1509 if (plp->pcp->pl_flags & P_M) {
1510 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1520 * Check to see if anyone hostile to the victim
1521 * is running an air defense mission on this
1522 * sector. If so, do air combat
1525 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1526 struct emp_qelem *esc_list)
1529 int mission_flags, tech, combat = 0, rel, dist, z;
1530 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1532 struct genlist *glp;
1534 struct genlist mi[MAXNOC];
1540 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1542 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1545 bzero((s_char *)mi, sizeof(mi));
1546 for (z = 1; z < MAXNOC; z++)
1547 emp_initque((struct emp_qelem *)&mi[z]);
1549 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1551 for (cn = 1; cn < MAXNOC; cn++) {
1552 /* Check our relations */
1553 rel = getrel(getnatp(cn), victim);
1558 if (QEMPTY(&mi[cn].queue))
1561 /* Ok, make a list of all the interceptors. Note that this *copies* the
1562 * list from the mission creation. This list must be deleted later. */
1563 emp_initque(&interceptors);
1564 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1567 glp = (struct genlist *)qp;
1568 gp = (struct genitem *)glp->thing;
1569 plp = (struct plist *)qp;
1571 dist = mapdist(x, y, gp->x, gp->y);
1573 plp = (struct plist *)malloc(sizeof(struct plist));
1574 bzero((s_char *)plp, sizeof(struct plist));
1575 plp->pcp = (struct plchrstr *)glp->cp;
1576 bcopy(glp->thing, (s_char *)&plp->plane,
1577 sizeof(struct plnstr));
1579 /* missiles go one way, so we can use all the range */
1580 if (!(plp->pcp->pl_flags & P_M))
1582 /* If it's out of range, free it and continue on */
1583 if (dist > plp->plane.pln_range) {
1587 emp_insque(&plp->queue, &interceptors);
1590 /* Remove those who cannot go */
1591 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1593 if (QEMPTY(&interceptors))
1596 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1597 * that the higher numbered planes go into battle (they should be the better ones
1598 * at fighting, if all went well.) */
1600 for (qp = interceptors.q_forw; qp != (&interceptors);
1603 tcount -= (count * 2);
1604 /* Just in case there are more incoming than we have */
1607 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1611 /* Free it up and continue */
1613 glp = (struct genlist *)qp;
1614 free((s_char *)glp);
1618 /* Now, make a list of all the airports these planes are coming from */
1620 for (qp = interceptors.q_forw; qp != (&interceptors);
1622 plp = (struct plist *)qp;
1623 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1624 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1627 /* Now, fly them out one airport at a time */
1628 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1629 struct airport *air;
1631 air = (struct airport *)qp;
1632 dist = mapdist(x, y, air->x, air->y);
1636 /* Split off the interceptors at this base into i */
1637 divide(&interceptors, &i, air->x, air->y);
1641 mission_flags |= P_X; /* stealth (shhh) */
1642 /* gets turned off if not all choppers */
1643 mission_flags |= P_H;
1644 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1645 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1647 /* Did we run out of interceptors? */
1650 /* Did we run out of bombers? */
1651 if (QEMPTY(bomb_list)) {
1652 /* Yes, so we have to put the rest of the interceptors back, and
1653 then continue, or we leak memory */
1658 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1659 mission_flags, &tech);
1661 /* Did we run out of interceptors? */
1664 /* Did we run out of bombers? */
1665 if (QEMPTY(bomb_list)) {
1666 /* Yes, so we have to put the rest of the interceptors back, and
1667 then continue, or we leak memory */
1672 BestAirPath(path, air->x, air->y, x, y);
1673 wu(0, cn, "Flying %s mission from %s\n",
1674 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1675 if (air->own && (air->own != cn)) {
1676 wu(0, air->own, "%s is flying %s mission from %s\n",
1677 cname(cn), mission_name(MI_AIR_DEFENSE),
1678 xyas(air->x, air->y, air->own));
1681 /* Now, fly the planes to the sector */
1682 emp_initque(&empty);
1683 ac_encounter(&i, &empty, air->x, air->y,
1684 path, mission_flags, 1, bomb_list, esc_list);
1686 /* If none made it, continue */
1690 /* Some made it, so now they get to try to fight. */
1691 /* Intercept the escorts first */
1693 if (!QEMPTY(esc_list)) {
1694 mpr(victim, "%s air defense planes intercept!\n",
1696 ac_combat_headers(victim, cn);
1697 ac_airtoair(esc_list, &i, air->own);
1700 /* Now intercept the bombers */
1701 if (!QEMPTY(bomb_list)) {
1703 mpr(victim, "%s air defense planes intercept!\n",
1705 ac_combat_headers(victim, cn);
1707 ac_airtoair(bomb_list, &i, air->own);
1716 /* We have to free all of these, if they are still there, otherwise they get
1717 lost and we leak memory all over the place. */
1718 for (cn = 1; cn < MAXNOC; cn++) {
1719 /* free up all this memory if it's still there */
1720 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1722 glp = (struct genlist *)qp;
1724 free((s_char *)glp);