2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * mission.c: Mission subroutines for planes/ships/units
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
53 #include "prototypes.h"
57 struct emp_qelem queue;
62 static void add_airport(struct emp_qelem *, coord, coord);
63 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
65 static void build_mission_list(struct genlist *, coord, coord, int, natid);
66 static void build_mission_list_type(struct genlist *, coord, coord, int,
68 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
69 static int dosupport(struct genlist *, coord, coord, natid, natid);
70 static int find_airport(struct emp_qelem *, coord, coord);
71 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
72 int, struct ichrstr *, int, int, int *);
73 static void mission_pln_sel(struct emp_qelem *, int, int, int);
74 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
78 * Interdict commodities & transported planes
81 ground_interdict(coord x, coord y, natid victim, s_char *s)
84 int dam = 0, newdam, rel;
85 struct genlist mi[MAXNOC];
88 memset(mi, 0, sizeof(mi));
89 for (z = 1; z < MAXNOC; z++)
90 emp_initque((struct emp_qelem *)&mi[z]);
92 build_mission_list(mi, x, y, MI_INTERDICT, victim);
94 for (cn = 1; cn < MAXNOC; cn++) {
95 rel = getrel(getnatp(cn), victim);
99 if (QEMPTY(&mi[cn].queue))
102 newdam = perform_mission(x, y, victim, &mi[cn].queue,
103 MI_INTERDICT, s, SECT_HARDTARGET);
106 mpr(victim, "%s interdiction mission does %d damage!\n",
110 collateral_damage(x, y, dam, 0);
116 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
124 getsect(x, y, §);
126 coll = ldround((double)dam * collateral_dam, 1);
129 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
130 xyas(x, y, sect.sct_own), coll);
131 sectdamage(§, coll, list);
139 only_subs(struct emp_qelem *list)
141 struct emp_qelem *qp;
146 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
147 glp = (struct genlist *)qp;
148 gp = (struct genitem *)glp->thing;
150 if (glp->type != EF_SHIP)
152 mcp = (struct mchrstr *)glp->cp;
153 if (!(mcp->m_flags & M_SUB))
157 /* They were all subs! */
163 * Interdict ships & land units
167 unit_interdict(coord x, coord y, natid victim, s_char *s, int hardtarget,
172 struct genlist mi[MAXNOC];
176 memset(mi, 0, sizeof(mi));
177 for (z = 1; z < MAXNOC; z++)
178 emp_initque((struct emp_qelem *)&mi[z]);
180 build_mission_list(mi, x, y, mission, victim);
182 for (cn = 1; cn < MAXNOC; cn++) {
185 if (mission == MI_SINTERDICT) {
186 if (getrel(getnatp(cn), victim) >= FRIENDLY)
188 } else if (getrel(getnatp(cn), victim) > HOSTILE)
191 if (QEMPTY(&mi[cn].queue))
194 osubs = only_subs(&mi[cn].queue);
195 newdam = perform_mission(x, y, victim, &mi[cn].queue,
196 mission, s, hardtarget);
199 /* If only subs responded, then we don't know who's
203 "Enemy interdiction mission does %d damage!\n",
207 "%s interdiction mission does %d damage!\n",
213 collateral_damage(x, y, dam, 0);
219 * Perform a mission against victim, on behalf of actee
222 off_support(coord x, coord y, natid victim, natid actee)
225 struct genlist mi[MAXNOC];
228 memset(mi, 0, sizeof(mi));
229 for (z = 1; z < MAXNOC; z++)
230 emp_initque((struct emp_qelem *)&mi[z]);
232 build_mission_list(mi, x, y, MI_SUPPORT, victim);
233 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
235 dam = dosupport(mi, x, y, victim, actee);
240 * Perform a mission against victim, on behalf of actee
243 def_support(coord x, coord y, natid victim, natid actee)
246 struct genlist mi[MAXNOC];
249 memset(mi, 0, sizeof(mi));
250 for (z = 1; z < MAXNOC; z++)
251 emp_initque((struct emp_qelem *)&mi[z]);
253 build_mission_list(mi, x, y, MI_SUPPORT, victim);
254 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
256 dam = dosupport(mi, x, y, victim, actee);
261 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
267 for (cn = 1; cn < MAXNOC; cn++) {
268 rel = getrel(getnatp(cn), actee);
269 if ((cn != actee) && (rel != ALLIED))
271 rel = getrel(getnatp(cn), victim);
272 if ((cn != actee) && (rel != AT_WAR))
275 if (QEMPTY(&mi[cn].queue))
278 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
279 "", SECT_HARDTARGET);
285 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
288 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
289 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
290 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
294 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
295 int type, natid victim)
312 size = max(sizeof(struct shpstr),sizeof(struct lndstr));
313 size = max(size,sizeof(struct plnstr));
314 block = (s_char *)malloc(size);
316 size = sizeof(u_block);
317 block = (s_char *)&u_block;
319 snxtitem_all(&ni, type);
320 while (nxtitem(&ni, block)) {
321 gp = (struct genitem *)block;
329 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
332 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
333 (gp->mission != MI_INTERDICT))
336 relat = getrel(getnatp(gp->own), victim);
337 if (mission == MI_SINTERDICT) {
338 if (relat >= FRIENDLY)
340 else if (type != EF_PLANE && relat > HOSTILE)
342 } else if (relat > HOSTILE)
345 dist = mapdist(x, y, gp->opx, gp->opy);
348 if (mission != MI_RESERVE) /* XXX */
349 oprange(gp, type, &radius);
354 /* Ok, it is within the operations range. */
355 /* Now check from where the object actually is */
356 dist = mapdist(x, y, gp->x, gp->y);
358 oprange(gp, type, &radius);
361 /* Ok, the object can get to where the x,y is */
364 if (mission != MI_AIR_DEFENSE) {
365 getsect(x, y, (s_char *)§);
366 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
369 * If the player->owner of the unit isn't at war
370 * with the victim, and doesn't own the
371 * sect being acted upon, and isn't the
372 * old player->owner of that sect, bounce them.
374 if (sect.sct_type != SCT_WATER &&
375 sect.sct_own != gp->own &&
376 sect.sct_oldown != gp->own)
382 glp = (struct genlist *)malloc(sizeof(struct genlist));
383 memset(glp, 0, sizeof(struct genlist));
389 glp->cp = (s_char *)&lchr[(int)gp->type];
392 glp->cp = (s_char *)&mchr[(int)gp->type];
395 glp->cp = (s_char *)&plchr[(int)gp->type];
398 glp->thing = (s_char *)malloc(size);
399 memcpy(glp->thing, block, size);
400 emp_insque(&glp->queue, &mi[gp->own].queue);
405 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
412 snxtitem_all(&ni, EF_PLANE);
413 while (nxtitem(&ni, (s_char *)&plane)) {
414 if (plane.pln_own != cn)
417 if (plane.pln_mission != MI_ESCORT)
420 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
422 if (dist > ((int)((float)plane.pln_range / 2.0)))
425 plp = (struct plist *)malloc(sizeof(struct plist));
426 memset(plp, 0, sizeof(struct plist));
427 plp->pcp = &plchr[(int)plane.pln_type];
429 emp_insque(&plp->queue, escorts);
434 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
435 int mission, s_char *s, int hardtarget)
437 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
438 struct emp_qelem *newqp;
447 struct plchrstr *pcp;
448 int dam = 0, dam2, mission_flags, tech;
449 natid plane_owner = 0;
450 int gun, shell, md, air_dam = 0;
451 double range2, prb, range, mobcost, hitchance;
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 = lp->lnd_item[I_SHELL];
493 gun = lp->lnd_item[I_GUN];
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 (sp->shp_item[I_MILIT] < 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 = sp->shp_item[I_GUN];
561 shell = sp->shp_item[I_SHELL];
562 if (shell < SHP_TORP_SHELLS)
563 shell += supply_commod(sp->shp_own,
564 sp->shp_x, sp->shp_y, I_SHELL,
565 SHP_TORP_SHELLS - shell);
566 if (shell < SHP_TORP_SHELLS)
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 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
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 = sp->shp_item[I_GUN];
621 gun = min(gun, sp->shp_glim);
622 shell = sp->shp_item[I_SHELL];
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, sp->shp_item[I_MILIT] / 2.0);
632 dam2 = seagun(sp->shp_effic, gun);
636 prb = ((double)md) / range2;
639 dam2 = (int)((float)dam2 / 2.0);
641 if (sect.sct_type == SCT_WATER)
642 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
644 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
646 "%s fires at %s %s at %s\n",
647 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
649 mpr(victim, "%s %s fires at you at %s\n",
650 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
652 sp->shp_item[I_SHELL] = shell - gun;
653 putship(sp->shp_uid, sp);
655 } else if (glp->type == EF_PLANE) {
656 pcp = (struct plchrstr *)glp->cp;
657 if (pcp->pl_flags & P_M)
658 /* units have their own missile interdiction */
659 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
662 /* save planes for later */
663 plp = (struct plist *)malloc(sizeof(struct plist));
665 memset(plp, 0, sizeof(struct plist));
667 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
668 if (plp->pcp->pl_flags & P_M)
669 emp_insque(&plp->queue, &missiles);
671 emp_insque(&plp->queue, &bombers);
672 plane_owner = plp->plane.pln_own;
675 if (!QEMPTY(&missiles)) {
676 /* I arbitrarily chose 100 mindam -KHS */
678 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
679 "sector", victim, mission);
680 qp = missiles.q_forw;
681 while (qp != (&missiles)) {
689 if (QEMPTY(&bombers)) {
692 glp = (struct genlist *)qp;
701 * If there are planes performing an
702 * interdict or support mission, find
703 * some escorts for them, if possible.
704 * Up to 2 per bomber, if possible.
706 find_escorts(x, y, plane_owner, &escorts);
708 if (mission == MI_SINTERDICT)
709 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
711 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
713 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
715 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
716 plp = (struct plist *)qp;
717 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
718 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
721 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
725 air = (struct airport *)qp;
726 md = mapdist(x, y, air->x, air->y);
731 /* Split off the bombers at this base into b */
732 divide(&bombers, &b, air->x, air->y);
734 /* Split off the escorts at this base into e */
735 divide(&escorts, &e, air->x, air->y);
739 mission_flags |= P_X; /* stealth (shhh) */
740 mission_flags |= P_H; /* gets turned off if not all choppers */
742 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
743 0, mission_flags, &tech);
749 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
750 P_F | P_ESC, mission_flags, &tech);
752 BestAirPath(pp, air->x, air->y, x, y);
753 wu(0, plane_owner, "Flying %s mission from %s\n",
754 mission_name(mission), xyas(air->x, air->y, plane_owner));
755 if (air->own && (air->own != plane_owner)) {
756 wu(0, air->own, "%s is flying %s mission from %s\n",
757 cname(plane_owner), mission_name(mission),
758 xyas(air->x, air->y, air->own));
761 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
765 air_damage(&b, x, y, mission, victim, s, hardtarget);
773 if (sect.sct_type == SCT_WATER)
774 nreport(plane_owner, N_SHP_BOMB, victim, 1);
776 nreport(plane_owner, N_SCT_BOMB, victim, 1);
779 /* free up all this memory */
782 glp = (struct genlist *)qp;
790 while (qp != (&escorts)) {
798 while (qp != (&bombers)) {
809 cando(int mission, int type)
813 if (type == EF_PLANE)
817 if (type == EF_PLANE)
821 if ((type == EF_PLANE) || (type == EF_SHIP))
829 if (type == EF_PLANE)
842 nameofitem(struct genitem *gp, int type)
846 return prship((struct shpstr *)gp);
848 return prplane((struct plnstr *)gp);
850 return prland((struct lndstr *)gp);
856 mission_short_name(int mission)
881 mission_name(short int mission)
885 return "an interdiction";
889 return "a offensive support";
891 return "a defensive support";
897 return "a sub interdiction";
899 return "an air defense";
901 return "a mysterious";
905 show_mission(int type, struct nstr_item *np)
907 int size, first = 1, radius;
911 size = max(sizeof(struct lndstr), sizeof(struct plnstr));
912 size = max(size, sizeof(struct shpstr));
913 block = (s_char *)malloc(size);
915 while (nxtitem(np, block)) {
916 gp = (struct genitem *)block;
917 if (!player->owner || gp->own == 0)
921 pr("Thing x,y op-sect rad mission\n");
924 pr("%-25s", nameofitem(gp, type));
925 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
926 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
927 gp->mission == MI_OSUPPORT ||
928 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
930 oprange(gp, type, &radius);
931 pr(" %7s", xyas(gp->opx, gp->opy, player->cnum));
932 if (radius < gp->radius)
935 pr(" %4d", gp->radius);
936 } else if (gp->mission == MI_RESERVE) {
940 getsect(gp->x, gp->y, §);
941 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
944 if (((struct lndstr *)block)->lnd_rad_max == 0)
947 plus += ((struct lndstr *)block)->lnd_rad_max;
948 pr(" %7s", xyas(gp->x, gp->y, player->cnum));
950 } else if (gp->mission == MI_ESCORT) {
953 ((float)((struct plnstr *)block)->pln_range / 2.0)
958 pr(" is on %s mission\n", mission_name(gp->mission));
960 pr(" has no mission.\n");
965 oprange(struct genitem *gp, int type, int *radius)
974 getship(gp->uid, &ship);
975 range = ldround(techfact(gp->tech,
976 (double)ship.shp_frnge / 2.0), 1);
979 getland(gp->uid, &land);
980 range = ldround(techfact((int)land.lnd_tech,
981 (double)land.lnd_frg / 2.0), 1);
984 getplane(gp->uid, &plane);
985 /* missiles go one way, so we can use all the range */
986 if (plchr[(int)plane.pln_type].pl_flags & P_M)
987 range = plane.pln_range;
989 range = ldround((double)plane.pln_range / 2.0, 1);;
993 if ((*radius) > range)
1000 * Remove all planes who cannot go on
1001 * the mission from the plane list.
1004 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
1007 struct emp_qelem *qp, *next;
1012 struct plchrstr *pcp;
1017 for (qp = list->q_forw; qp != list; qp = next) {
1019 plp = (struct plist *)qp;
1023 if (pp->pln_effic < 40) {
1029 if (pp->pln_mobil < 1) {
1036 if (ontradingblock(EF_PLANE, (int *)pp)) {
1046 for (x = 0; x < sizeof(wantflags) * 8; x++) {
1048 if ((wantflags & y) == y)
1049 if ((pcp->pl_flags & y) != y) {
1071 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
1075 if ((pcp->pl_flags & P_E) ||
1076 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
1088 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
1090 if ((nowantflags & y) == y)
1091 if ((pcp->pl_flags & y) == y)
1100 if (pp->pln_ship >= 0) {
1101 if (!getship(pp->pln_ship, &ship)) {
1104 putplane(pp->pln_uid, pp);
1109 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1112 if (ship.shp_effic < SHIP_MINEFF) {
1115 /* Can't fly off of inefficient or non-owned, non-allied ships */
1116 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1117 ((ship.shp_own != pp->pln_own) &&
1118 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1124 if (pp->pln_land >= 0) {
1125 if (!getland(pp->pln_land, &land)) {
1128 putplane(pp->pln_uid, pp);
1133 if (!(pcp->pl_flags & P_E))
1135 if (land.lnd_effic < LAND_MINEFF)
1138 /* Can't fly off of inefficient or non-owned, non-allied units */
1139 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1140 ((land.lnd_own != pp->pln_own) &&
1141 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1147 /* Can't fly off units in ships or other units */
1148 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1154 /* Now, check the sector status if not on a plane or unit */
1155 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1156 /* If we can't get the sector, we can't check it, and can't fly */
1157 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1162 /* First, check allied status */
1163 /* Can't fly from non-owned sectors or non-allied sectors */
1164 if ((sect.sct_own != pp->pln_own) &&
1165 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1170 /* non-vtol plane */
1171 if ((pcp->pl_flags & P_V) == 0) {
1172 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1179 if (pcp->pl_flags & P_A) {
1180 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1187 putplane(pp->pln_uid, pp);
1192 * Arm only the planes at x,y
1196 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1197 int mission, struct ichrstr *ip, int flags,
1198 int mission_flags, int *tech)
1200 struct emp_qelem *qp;
1201 struct emp_qelem *next;
1206 for (qp = list->q_forw; qp != list; qp = next) {
1208 plp = (struct plist *)qp;
1210 if (plp->plane.pln_x != x)
1212 if (plp->plane.pln_y != y)
1215 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1220 if (flags & (P_S | P_I)) {
1221 if (plp->pcp->pl_flags & P_S)
1222 mission_flags |= P_S;
1223 if (plp->pcp->pl_flags & P_I)
1224 mission_flags |= P_I;
1226 if (*tech > plp->plane.pln_tech)
1227 *tech = plp->plane.pln_tech;
1228 if (!(plp->pcp->pl_flags & P_H))
1229 /* no stealth on this mission */
1230 mission_flags &= ~P_H;
1231 if (!(plp->pcp->pl_flags & P_X))
1232 /* no stealth on this mission */
1233 mission_flags &= ~P_X;
1234 if (!(plp->pcp->pl_flags & P_A)) {
1235 /* no asw on this mission */
1236 mission_flags &= ~P_A;
1238 if (!(plp->pcp->pl_flags & P_MINE)) {
1239 /* no asw on this mission */
1240 mission_flags &= ~P_MINE;
1244 * Mob costs for missions are 1/2 normal
1247 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1248 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1251 return mission_flags;
1255 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1258 struct plchrstr *pcp;
1270 if (pp->pln_ship >= 0) {
1271 getship(pp->pln_ship, &ship);
1272 item = ship.shp_item;
1273 } else if (pp->pln_land >= 0) {
1274 getland(pp->pln_land, &land);
1275 item = land.lnd_item;
1277 getsect(pp->pln_x, pp->pln_y, §);
1278 item = sect.sct_item;
1280 if (pcp->pl_fuel > item[I_PETROL]) {
1283 item[I_PETROL] -= pcp->pl_fuel;
1285 if (!(flags & P_F)) {
1291 if (pp->pln_nuketype == -1) {
1293 needed = pp->pln_load;
1297 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1299 itype = ip->i_vtype;
1300 needed = (pp->pln_load * 2) / ip->i_lbs;
1303 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1305 itype = ip->i_vtype;
1306 needed = (pp->pln_load * 2) / ip->i_lbs;
1309 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1312 needed = pp->pln_load / ip->i_lbs;
1315 if (pp->pln_nuketype == -1)
1318 case 'i': /* missile interception */
1321 needed = pp->pln_load;
1327 if (rval < 0 || (itype && needed <= 0)) {
1330 if (item[itype] < needed && (itype == I_SHELL))
1331 item[itype] += supply_commod(plp->plane.pln_own,
1332 plp->plane.pln_x, plp->plane.pln_y,
1334 if (item[itype] < needed) {
1337 item[itype] -= needed;
1339 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1340 plp->bombs = needed;
1344 if (pp->pln_ship >= 0)
1345 putship(ship.shp_uid, &ship);
1346 else if (pp->pln_land >= 0)
1347 putland(land.lnd_uid, &land);
1354 * Return 1 if this x,y pair is in the list
1357 find_airport(struct emp_qelem *airp, coord x, coord y)
1359 struct emp_qelem *qp;
1362 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1363 a = (struct airport *)qp;
1364 if ((a->x == x) && (a->y == y))
1371 /* #*# This needs to be changed to include acc's -KHS */
1373 add_airport(struct emp_qelem *airp, coord x, coord y)
1378 a = (struct airport *)malloc(sizeof(struct airport));
1382 getsect(x, y, §);
1383 a->own = sect.sct_own;
1385 emp_insque((struct emp_qelem *)a, airp);
1389 * Take all the planes in list 1 that
1390 * are at x,y, and put them into list 2.
1393 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1395 struct emp_qelem *qp, *next;
1398 for (qp = l1->q_forw; qp != l1; qp = next) {
1400 plp = (struct plist *)qp;
1402 if (plp->plane.pln_x != x)
1404 if (plp->plane.pln_y != y)
1413 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1414 natid victim, s_char *s, int hardtarget)
1416 struct emp_qelem *qp;
1419 int newdam, dam = 0;
1423 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1424 plp = (struct plist *)qp;
1427 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1434 if (plp->pcp->pl_flags & P_A) {
1435 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1437 "\t%s detects sub movement in %s\n",
1438 prplane(pp), xyas(x, y, pp->pln_own));
1441 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1443 "\t%s tracks %s %s at %s\n",
1444 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1448 "\t%s depth-charging %s %s in %s\n",
1449 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1452 "\t%s pinbombing %s %s in %s\n",
1453 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1455 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1456 if (plp->plane.pln_nuketype != -1)
1458 else if (hardtarget != SECT_HARDTARGET)
1459 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1460 /* Always calculate damage */
1461 if (roll(100) <= hitchance) {
1462 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1464 if (mission == MI_INTERDICT) {
1466 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1467 prplane(pp), nukedam, cname(victim), s);
1472 "\t\thit %s %s for %d damage\n",
1473 cname(victim), s, newdam);
1477 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1478 wu(0, pp->pln_own, "missed\n");
1479 if (mission == MI_SINTERDICT) {
1481 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1482 xyas(x, y, victim));
1483 } else if (*s == 's') {
1485 "SPLASH! Bombs miss your %s in %s\n",
1486 s, xyas(x, y, victim));
1488 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1489 s, xyas(x, y, victim));
1491 /* Now, even though we missed, the bombs
1493 collateral_damage(x, y, newdam, bombers);
1496 /* use up missiles */
1497 if (plp->pcp->pl_flags & P_M) {
1498 makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x,
1508 * Check to see if anyone hostile to the victim
1509 * is running an air defense mission on this
1510 * sector. If so, do air combat
1513 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1514 struct emp_qelem *esc_list)
1517 int mission_flags, tech, combat = 0, rel, dist, z;
1518 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1520 struct genlist *glp;
1522 struct genlist mi[MAXNOC];
1528 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1530 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1533 memset(mi, 0, sizeof(mi));
1534 for (z = 1; z < MAXNOC; z++)
1535 emp_initque((struct emp_qelem *)&mi[z]);
1537 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1539 for (cn = 1; cn < MAXNOC; cn++) {
1540 /* Check our relations */
1541 rel = getrel(getnatp(cn), victim);
1546 if (QEMPTY(&mi[cn].queue))
1549 /* Ok, make a list of all the interceptors. Note that this *copies* the
1550 * list from the mission creation. This list must be deleted later. */
1551 emp_initque(&interceptors);
1552 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1555 glp = (struct genlist *)qp;
1556 gp = (struct genitem *)glp->thing;
1557 plp = (struct plist *)qp;
1559 dist = mapdist(x, y, gp->x, gp->y);
1561 plp = (struct plist *)malloc(sizeof(struct plist));
1562 memset(plp, 0, sizeof(struct plist));
1563 plp->pcp = (struct plchrstr *)glp->cp;
1564 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1566 /* missiles go one way, so we can use all the range */
1567 if (!(plp->pcp->pl_flags & P_M))
1569 /* If it's out of range, free it and continue on */
1570 if (dist > plp->plane.pln_range) {
1574 emp_insque(&plp->queue, &interceptors);
1577 /* Remove those who cannot go */
1578 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1580 if (QEMPTY(&interceptors))
1583 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1584 * that the higher numbered planes go into battle (they should be the better ones
1585 * at fighting, if all went well.) */
1587 for (qp = interceptors.q_forw; qp != (&interceptors);
1590 tcount -= (count * 2);
1591 /* Just in case there are more incoming than we have */
1594 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1598 /* Free it up and continue */
1600 glp = (struct genlist *)qp;
1601 free((s_char *)glp);
1605 /* Now, make a list of all the airports these planes are coming from */
1607 for (qp = interceptors.q_forw; qp != (&interceptors);
1609 plp = (struct plist *)qp;
1610 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1611 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1614 /* Now, fly them out one airport at a time */
1615 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1616 struct airport *air;
1618 air = (struct airport *)qp;
1619 dist = mapdist(x, y, air->x, air->y);
1623 /* Split off the interceptors at this base into i */
1624 divide(&interceptors, &i, air->x, air->y);
1628 mission_flags |= P_X; /* stealth (shhh) */
1629 /* gets turned off if not all choppers */
1630 mission_flags |= P_H;
1631 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1632 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1634 /* Did we run out of interceptors? */
1637 /* Did we run out of bombers? */
1638 if (QEMPTY(bomb_list)) {
1639 /* Yes, so we have to put the rest of the interceptors back, and
1640 then continue, or we leak memory */
1645 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1646 mission_flags, &tech);
1648 /* Did we run out of interceptors? */
1651 /* Did we run out of bombers? */
1652 if (QEMPTY(bomb_list)) {
1653 /* Yes, so we have to put the rest of the interceptors back, and
1654 then continue, or we leak memory */
1659 BestAirPath(path, air->x, air->y, x, y);
1660 wu(0, cn, "Flying %s mission from %s\n",
1661 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1662 if (air->own && (air->own != cn)) {
1663 wu(0, air->own, "%s is flying %s mission from %s\n",
1664 cname(cn), mission_name(MI_AIR_DEFENSE),
1665 xyas(air->x, air->y, air->own));
1668 /* Now, fly the planes to the sector */
1669 emp_initque(&empty);
1670 ac_encounter(&i, &empty, air->x, air->y,
1671 path, mission_flags, 1, bomb_list, esc_list);
1673 /* If none made it, continue */
1677 /* Some made it, so now they get to try to fight. */
1678 /* Intercept the escorts first */
1680 if (!QEMPTY(esc_list)) {
1681 mpr(victim, "%s air defense planes intercept!\n",
1683 ac_combat_headers(victim, cn);
1684 ac_airtoair(esc_list, &i, air->own);
1687 /* Now intercept the bombers */
1688 if (!QEMPTY(bomb_list)) {
1690 mpr(victim, "%s air defense planes intercept!\n",
1692 ac_combat_headers(victim, cn);
1694 ac_airtoair(bomb_list, &i, air->own);
1703 /* We have to free all of these, if they are still there, otherwise they get
1704 lost and we leak memory all over the place. */
1705 for (cn = 1; cn < MAXNOC; cn++) {
1706 /* free up all this memory if it's still there */
1707 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1709 glp = (struct genlist *)qp;
1711 free((s_char *)glp);