2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * mission.c: Mission subroutines for planes/ships/units
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
48 #include "prototypes.h"
53 struct emp_qelem queue; /* list of units */
54 void *cp; /* pointer to desc of thing */
55 struct empobj *thing; /* thing's struct */
59 struct emp_qelem queue;
64 static void add_airport(struct emp_qelem *, coord, coord);
65 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
67 static void build_mission_list(struct genlist *, coord, coord, int, natid);
68 static void build_mission_list_type(struct genlist *, coord, coord, int,
70 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
71 static int dosupport(struct genlist *, coord, coord, natid, natid);
72 static int find_airport(struct emp_qelem *, coord, coord);
73 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
74 int, struct ichrstr *, int, int, int *);
75 static void mission_pln_sel(struct emp_qelem *, int, int, int);
76 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
80 * Interdict commodities & transported planes
83 ground_interdict(coord x, coord y, natid victim, char *s)
86 int dam = 0, newdam, rel;
87 struct genlist mi[MAXNOC];
90 memset(mi, 0, sizeof(mi));
91 for (z = 1; z < MAXNOC; z++)
92 emp_initque((struct emp_qelem *)&mi[z]);
94 build_mission_list(mi, x, y, MI_INTERDICT, victim);
96 for (cn = 1; cn < MAXNOC; cn++) {
97 rel = getrel(getnatp(cn), victim);
101 if (QEMPTY(&mi[cn].queue))
104 newdam = perform_mission(x, y, victim, &mi[cn].queue,
105 MI_INTERDICT, s, SECT_HARDTARGET);
108 mpr(victim, "%s interdiction mission does %d damage!\n",
112 collateral_damage(x, y, dam, 0);
118 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
126 getsect(x, y, §);
128 coll = ldround((double)dam * collateral_dam, 1);
131 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
132 xyas(x, y, sect.sct_own), coll);
133 sectdamage(§, coll, list);
141 only_subs(struct emp_qelem *list)
143 struct emp_qelem *qp;
147 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
148 glp = (struct genlist *)qp;
150 if (glp->thing->ef_type != EF_SHIP)
153 if (!(mcp->m_flags & M_SUB))
157 /* They were all subs! */
163 * Interdict ships & land units
166 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
171 struct genlist mi[MAXNOC];
175 memset(mi, 0, sizeof(mi));
176 for (z = 1; z < MAXNOC; z++)
177 emp_initque((struct emp_qelem *)&mi[z]);
179 build_mission_list(mi, x, y, mission, victim);
181 for (cn = 1; cn < MAXNOC; cn++) {
184 if (mission == MI_SINTERDICT) {
185 if (getrel(getnatp(cn), victim) >= FRIENDLY)
187 } else if (getrel(getnatp(cn), victim) > HOSTILE)
190 if (QEMPTY(&mi[cn].queue))
193 osubs = only_subs(&mi[cn].queue);
194 newdam = perform_mission(x, y, victim, &mi[cn].queue,
195 mission, s, hardtarget);
198 /* If only subs responded, then we don't know who's
200 mpr(victim, "%s interdiction mission does %d damage!\n",
201 osubs ? "Enemy" : cname(cn), newdam);
205 collateral_damage(x, y, dam, 0);
211 * Perform a mission against victim, on behalf of actee
214 off_support(coord x, coord y, natid victim, natid actee)
217 struct genlist mi[MAXNOC];
220 memset(mi, 0, sizeof(mi));
221 for (z = 1; z < MAXNOC; z++)
222 emp_initque((struct emp_qelem *)&mi[z]);
224 build_mission_list(mi, x, y, MI_SUPPORT, victim);
225 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
227 dam = dosupport(mi, x, y, victim, actee);
232 * Perform a mission against victim, on behalf of actee
235 def_support(coord x, coord y, natid victim, natid actee)
238 struct genlist mi[MAXNOC];
241 memset(mi, 0, sizeof(mi));
242 for (z = 1; z < MAXNOC; z++)
243 emp_initque((struct emp_qelem *)&mi[z]);
245 build_mission_list(mi, x, y, MI_SUPPORT, victim);
246 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
248 dam = dosupport(mi, x, y, victim, actee);
253 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
259 for (cn = 1; cn < MAXNOC; cn++) {
260 rel = getrel(getnatp(cn), actee);
261 if ((cn != actee) && (rel != ALLIED))
263 rel = getrel(getnatp(cn), victim);
264 if ((cn != actee) && (rel != AT_WAR))
267 if (QEMPTY(&mi[cn].queue))
270 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
271 "", SECT_HARDTARGET);
277 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
280 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
281 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
282 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
286 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
287 int type, natid victim)
292 union empobj_storage item;
298 snxtitem_all(&ni, type);
299 while (nxtitem(&ni, &item)) {
300 gp = (struct empobj *)&item;
308 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
311 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
312 (gp->mission != MI_INTERDICT))
315 relat = getrel(getnatp(gp->own), victim);
316 if (mission == MI_SINTERDICT) {
317 if (relat >= FRIENDLY)
319 else if (type != EF_PLANE && relat > HOSTILE)
321 } else if (relat > HOSTILE)
324 dist = mapdist(x, y, gp->opx, gp->opy);
327 if (mission != MI_RESERVE) /* XXX */
328 oprange(gp, &radius);
333 /* Ok, it is within the operations range. */
334 /* Now check from where the object actually is */
335 dist = mapdist(x, y, gp->x, gp->y);
337 oprange(gp, &radius);
340 /* Ok, the object can get to where the x,y is */
343 if (mission != MI_AIR_DEFENSE) {
344 getsect(x, y, §);
345 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
348 * If the player->owner of the unit isn't at war
349 * with the victim, and doesn't own the
350 * sect being acted upon, and isn't the
351 * old player->owner of that sect, bounce them.
353 if (sect.sct_type != SCT_WATER &&
354 sect.sct_own != gp->own &&
355 sect.sct_oldown != gp->own)
361 glp = malloc(sizeof(struct genlist));
362 memset(glp, 0, sizeof(struct genlist));
363 glp->cp = get_empobj_chr(gp);
364 glp->thing = malloc(sizeof(item));
365 memcpy(glp->thing, &item, sizeof(item));
366 emp_insque(&glp->queue, &mi[gp->own].queue);
371 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
378 snxtitem_all(&ni, EF_PLANE);
379 while (nxtitem(&ni, &plane)) {
380 if (plane.pln_own != cn)
383 if (plane.pln_mission != MI_ESCORT)
386 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
387 if (dist > plane.pln_range / 2)
390 plp = malloc(sizeof(struct plist));
391 memset(plp, 0, sizeof(struct plist));
392 plp->pcp = &plchr[(int)plane.pln_type];
394 emp_insque(&plp->queue, escorts);
399 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
400 int mission, char *s, int hardtarget)
402 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
403 struct emp_qelem *newqp;
411 struct plchrstr *pcp;
412 int dam = 0, dam2, mission_flags, tech;
413 natid plane_owner = 0;
414 int gun, shell, md, range, air_dam = 0;
415 double prb, hitchance, vrange;
417 getsect(x, y, §);
419 emp_initque(&missiles);
420 emp_initque(&bombers);
421 emp_initque(&escorts);
424 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
425 glp = (struct genlist *)qp;
428 md = mapdist(x, y, gp->x, gp->y);
430 if (glp->thing->ef_type == EF_LAND) {
431 lp = (struct lndstr *)glp->thing;
433 if (lp->lnd_effic < LAND_MINFIREEFF)
436 if (mission == MI_SINTERDICT)
439 if ((mission == MI_INTERDICT) &&
440 (md > land_max_interdiction_range))
443 if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
446 if (lp->lnd_item[I_MILIT] < 1)
449 range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
453 shell = lp->lnd_item[I_SHELL];
454 gun = lp->lnd_item[I_GUN];
455 if (shell == 0 || gun == 0)
458 if (has_supply(lp)) {
460 putland(lp->lnd_uid, lp);
461 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
462 lp->lnd_ammo, shell), 1);
463 if (sect.sct_type == SCT_WATER) {
464 if (chance(lp->lnd_acc / 100.0))
465 dam2 = ldround(dam2 / 2.0, 1);
468 if (sect.sct_type == SCT_WATER)
469 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
471 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
473 "%s fires at %s %s at %s\n",
474 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
476 mpr(victim, "%s %s fires at you at %s\n",
477 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
479 } else if (glp->thing->ef_type == EF_SHIP) {
480 sp = (struct shpstr *)glp->thing;
483 if (sp->shp_effic < 60)
485 if (sp->shp_frnge == 0)
487 if (((mission == MI_INTERDICT) ||
488 (mission == MI_SINTERDICT)) &&
489 (md > ship_max_interdiction_range))
491 if (sp->shp_item[I_MILIT] < 1)
494 if ((mcp->m_flags & M_SUB) &&
495 (sect.sct_type != SCT_WATER))
498 if (mission == MI_SINTERDICT) {
499 if (!(mcp->m_flags & M_SONAR))
501 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
503 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
504 vrange *= sp->shp_effic / 200.0;
507 /* can't look all the time */
511 if (mcp->m_flags & M_SUB) {
512 /* If we aren't shooting at "subs" or "ships" don't fire at all from
516 if (sp->shp_mobil < 0)
518 gun = sp->shp_item[I_GUN];
521 shell = sp->shp_item[I_SHELL];
522 if (shell < SHP_TORP_SHELLS)
523 shell += supply_commod(sp->shp_own,
524 sp->shp_x, sp->shp_y, I_SHELL,
525 SHP_TORP_SHELLS - shell);
526 if (shell < SHP_TORP_SHELLS)
529 range = roundrange(torprange(sp));
533 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
535 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
536 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
537 putship(sp->shp_uid, sp);
538 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
541 "%s locking on %s %s in %s\n",
542 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
544 "\tEffective torpedo range is %d.0\n", range);
546 "\tWhooosh... Hitchance = %d%%\n",
547 (int)(hitchance * 100));
549 if (hitchance < 1.0 && !chance(hitchance)) {
550 wu(0, sp->shp_own, "\tMissed\n");
552 "Incoming torpedo sighted @ %s missed (whew)!\n",
556 wu(0, sp->shp_own, "\tBOOM!...\n");
557 dam2 = TORP_DAMAGE();
560 nreport(victim, N_TORP_SHIP, 0, 1);
562 "\tTorpedo hit %s %s for %d damage\n",
563 cname(victim), s, dam2);
566 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
567 xyas(x, y, victim), dam2);
569 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
572 gun = sp->shp_item[I_GUN];
573 gun = MIN(gun, sp->shp_glim);
574 shell = sp->shp_item[I_SHELL];
576 shell += supply_commod(sp->shp_own,
577 sp->shp_x, sp->shp_y, I_SHELL,
579 gun = MIN(gun, shell);
580 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
584 dam2 = seagun(sp->shp_effic, gun);
588 prb = (double)md / range;
593 if (sect.sct_type == SCT_WATER)
594 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
596 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
598 "%s fires at %s %s at %s\n",
599 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
601 mpr(victim, "%s %s fires at you at %s\n",
602 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
604 sp->shp_item[I_SHELL] = shell - gun;
605 putship(sp->shp_uid, sp);
607 } else if (glp->thing->ef_type == EF_PLANE) {
609 if (pcp->pl_flags & P_M)
610 /* units have their own missile interdiction */
611 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
614 /* save planes for later */
615 plp = malloc(sizeof(struct plist));
617 memset(plp, 0, sizeof(struct plist));
619 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
620 if (plp->pcp->pl_flags & P_M)
621 emp_insque(&plp->queue, &missiles);
623 emp_insque(&plp->queue, &bombers);
624 plane_owner = plp->plane.pln_own;
627 if (!QEMPTY(&missiles)) {
628 /* I arbitrarily chose 100 mindam -KHS */
630 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
631 "sector", victim, mission);
632 qp = missiles.q_forw;
633 while (qp != (&missiles)) {
641 if (QEMPTY(&bombers)) {
644 glp = (struct genlist *)qp;
653 * If there are planes performing an
654 * interdict or support mission, find
655 * some escorts for them, if possible.
656 * Up to 2 per bomber, if possible.
658 find_escorts(x, y, plane_owner, &escorts);
660 if (mission == MI_SINTERDICT)
661 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
663 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
665 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
667 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
668 plp = (struct plist *)qp;
669 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
670 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
673 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
678 air = (struct airport *)qp;
679 md = mapdist(x, y, air->x, air->y);
684 /* Split off the bombers at this base into b */
685 divide(&bombers, &b, air->x, air->y);
687 /* Split off the escorts at this base into e */
688 divide(&escorts, &e, air->x, air->y);
692 mission_flags |= P_X; /* stealth (shhh) */
693 mission_flags |= P_H; /* gets turned off if not all choppers */
695 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
696 0, mission_flags, &tech);
701 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
702 P_F | P_ESC, mission_flags, &tech);
704 pp = BestAirPath(buf, air->x, air->y, x, y);
705 if (CANT_HAPPEN(!pp))
707 wu(0, plane_owner, "Flying %s mission from %s\n",
708 mission_name(mission), xyas(air->x, air->y, plane_owner));
709 if (air->own && (air->own != plane_owner)) {
710 wu(0, air->own, "%s is flying %s mission from %s\n",
711 cname(plane_owner), mission_name(mission),
712 xyas(air->x, air->y, air->own));
715 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
719 air_damage(&b, x, y, mission, victim, s, hardtarget);
727 if (sect.sct_type == SCT_WATER)
728 nreport(plane_owner, N_SHP_BOMB, victim, 1);
730 nreport(plane_owner, N_SCT_BOMB, victim, 1);
733 /* free up all this memory */
736 glp = (struct genlist *)qp;
744 while (qp != (&escorts)) {
752 while (qp != (&bombers)) {
763 cando(int mission, int type)
767 if (type == EF_PLANE)
771 if (type == EF_PLANE)
775 if ((type == EF_PLANE) || (type == EF_SHIP))
783 if (type == EF_PLANE)
796 mission_name(short mission)
800 return "an interdiction";
804 return "a offensive support";
806 return "a defensive support";
812 return "a sub interdiction";
814 return "an air defense";
816 return "a mysterious";
820 show_mission(int type, struct nstr_item *np)
822 int first = 1, radius;
823 union empobj_storage item;
826 while (nxtitem(np, &item)) {
827 gp = (struct empobj *)&item;
828 if (!player->owner || gp->own == 0)
832 pr("Thing x,y op-sect rad mission\n");
835 pr("%-25s", obj_nameof(gp));
836 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
837 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
838 gp->mission == MI_OSUPPORT ||
839 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
841 oprange(gp, &radius);
842 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
843 if (radius < gp->radius)
846 pr(" %4d", gp->radius);
847 } else if (gp->mission == MI_RESERVE) {
851 getsect(gp->x, gp->y, §);
852 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
855 if (item.land.lnd_rad_max == 0)
858 plus += item.land.lnd_rad_max;
859 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
861 } else if (gp->mission == MI_ESCORT) {
863 pr(" %4d", item.plane.pln_range / 2);
867 pr(" is on %s mission\n", mission_name(gp->mission));
869 pr(" has no mission.\n");
874 oprange(struct empobj *gp, int *radius)
878 switch (gp->ef_type) {
880 range = ldround(effrange(((struct shpstr *)gp)->shp_frnge,
881 ((struct shpstr *)gp)->shp_tech), 1);
884 range = ldround(effrange(((struct lndstr *)gp)->lnd_frg,
885 ((struct lndstr *)gp)->lnd_tech), 1);
888 /* missiles go one way, so we can use all the range */
889 if (plchr[(int)gp->type].pl_flags & P_M)
890 range = ((struct plnstr *)gp)->pln_range;
892 range = ((struct plnstr *)gp)->pln_range / 2;
895 CANT_HAPPEN("bad TYPE");
906 * Remove all planes who cannot go on
907 * the mission from the plane list.
910 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
913 struct emp_qelem *qp, *next;
918 struct plchrstr *pcp;
923 for (qp = list->q_forw; qp != list; qp = next) {
925 plp = (struct plist *)qp;
929 if (pp->pln_effic < 40) {
935 if (pp->pln_mobil < 1) {
942 if (ontradingblock(EF_PLANE, pp)) {
952 for (x = 0; x < sizeof(wantflags) * 8; x++) {
954 if ((wantflags & y) == y)
955 if ((pcp->pl_flags & y) != y) {
977 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
981 if ((pcp->pl_flags & P_E) ||
982 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
994 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
996 if ((nowantflags & y) == y)
997 if ((pcp->pl_flags & y) == y)
1006 if (pp->pln_ship >= 0) {
1007 if (!getship(pp->pln_ship, &ship)) {
1010 putplane(pp->pln_uid, pp);
1015 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1018 if (ship.shp_effic < SHIP_MINEFF) {
1021 /* Can't fly off of inefficient or non-owned, non-allied ships */
1022 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1023 ((ship.shp_own != pp->pln_own) &&
1024 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1030 if (pp->pln_land >= 0) {
1031 if (!getland(pp->pln_land, &land)) {
1034 putplane(pp->pln_uid, pp);
1039 if (!(pcp->pl_flags & P_E))
1041 if (land.lnd_effic < LAND_MINEFF)
1044 /* Can't fly off of inefficient or non-owned, non-allied units */
1045 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1046 ((land.lnd_own != pp->pln_own) &&
1047 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1053 /* Can't fly off units in ships or other units */
1054 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1060 /* Now, check the sector status if not on a plane or unit */
1061 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1062 /* If we can't get the sector, we can't check it, and can't fly */
1063 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1068 /* First, check allied status */
1069 /* Can't fly from non-owned sectors or non-allied sectors */
1070 if ((sect.sct_own != pp->pln_own) &&
1071 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1076 /* non-vtol plane */
1077 if ((pcp->pl_flags & P_V) == 0) {
1078 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1085 if (pcp->pl_flags & P_A) {
1086 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1093 putplane(pp->pln_uid, pp);
1098 * Arm only the planes at x,y
1101 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1102 int mission, struct ichrstr *ip, int flags,
1103 int mission_flags, int *tech)
1105 struct emp_qelem *qp;
1106 struct emp_qelem *next;
1111 for (qp = list->q_forw; qp != list; qp = next) {
1113 plp = (struct plist *)qp;
1115 if (plp->plane.pln_x != x)
1117 if (plp->plane.pln_y != y)
1120 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1125 if (flags & (P_S | P_I)) {
1126 if (plp->pcp->pl_flags & P_S)
1127 mission_flags |= P_S;
1128 if (plp->pcp->pl_flags & P_I)
1129 mission_flags |= P_I;
1131 if (*tech > plp->plane.pln_tech)
1132 *tech = plp->plane.pln_tech;
1133 if (!(plp->pcp->pl_flags & P_H))
1134 /* no stealth on this mission */
1135 mission_flags &= ~P_H;
1136 if (!(plp->pcp->pl_flags & P_X))
1137 /* no stealth on this mission */
1138 mission_flags &= ~P_X;
1139 if (!(plp->pcp->pl_flags & P_A)) {
1140 /* no asw on this mission */
1141 mission_flags &= ~P_A;
1143 if (!(plp->pcp->pl_flags & P_MINE)) {
1144 /* no asw on this mission */
1145 mission_flags &= ~P_MINE;
1149 * Mob costs for missions are 1/2 normal
1152 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1153 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1156 return mission_flags;
1160 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1163 struct plchrstr *pcp;
1175 if (pp->pln_ship >= 0) {
1176 getship(pp->pln_ship, &ship);
1177 item = ship.shp_item;
1178 } else if (pp->pln_land >= 0) {
1179 getland(pp->pln_land, &land);
1180 item = land.lnd_item;
1182 getsect(pp->pln_x, pp->pln_y, §);
1183 item = sect.sct_item;
1185 if (pcp->pl_fuel > item[I_PETROL]) {
1188 item[I_PETROL] -= pcp->pl_fuel;
1190 if (!(flags & P_F)) {
1196 if (pp->pln_nuketype == -1) {
1198 needed = pp->pln_load;
1202 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1205 needed = (pp->pln_load * 2) / ip->i_lbs;
1208 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1211 needed = (pp->pln_load * 2) / ip->i_lbs;
1214 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1217 needed = pp->pln_load / ip->i_lbs;
1220 if (pp->pln_nuketype == -1)
1223 case 'i': /* missile interception */
1226 needed = pp->pln_load;
1232 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1235 if (itype != I_NONE) {
1236 if (itype == I_SHELL && item[itype] < needed)
1237 item[itype] += supply_commod(plp->plane.pln_own,
1241 if (item[itype] < needed)
1243 item[itype] -= needed;
1245 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1246 plp->bombs = needed;
1250 if (pp->pln_ship >= 0)
1251 putship(ship.shp_uid, &ship);
1252 else if (pp->pln_land >= 0)
1253 putland(land.lnd_uid, &land);
1260 * Return 1 if this x,y pair is in the list
1263 find_airport(struct emp_qelem *airp, coord x, coord y)
1265 struct emp_qelem *qp;
1268 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1269 a = (struct airport *)qp;
1270 if ((a->x == x) && (a->y == y))
1277 /* #*# This needs to be changed to include acc's -KHS */
1279 add_airport(struct emp_qelem *airp, coord x, coord y)
1284 a = malloc(sizeof(struct airport));
1288 getsect(x, y, §);
1289 a->own = sect.sct_own;
1291 emp_insque((struct emp_qelem *)a, airp);
1295 * Take all the planes in list 1 that
1296 * are at x,y, and put them into list 2.
1299 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1301 struct emp_qelem *qp, *next;
1304 for (qp = l1->q_forw; qp != l1; qp = next) {
1306 plp = (struct plist *)qp;
1308 if (plp->plane.pln_x != x)
1310 if (plp->plane.pln_y != y)
1319 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1320 natid victim, char *s, int hardtarget)
1322 struct emp_qelem *qp;
1325 int newdam, dam = 0;
1329 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1330 plp = (struct plist *)qp;
1333 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1340 if (plp->pcp->pl_flags & P_A) {
1341 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1343 "\t%s detects sub movement in %s\n",
1344 prplane(pp), xyas(x, y, pp->pln_own));
1347 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1349 "\t%s tracks %s %s at %s\n",
1350 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1354 "\t%s depth-charging %s %s in %s\n",
1355 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1358 "\t%s pinbombing %s %s in %s\n",
1359 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1361 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1362 if (plp->plane.pln_nuketype != -1)
1364 else if (hardtarget != SECT_HARDTARGET)
1365 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1366 /* Always calculate damage */
1367 if (roll(100) <= hitchance) {
1368 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1370 if (mission == MI_INTERDICT) {
1372 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1373 prplane(pp), nukedam, cname(victim), s);
1378 "\t\thit %s %s for %d damage\n",
1379 cname(victim), s, newdam);
1383 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1384 wu(0, pp->pln_own, "missed\n");
1385 if (mission == MI_SINTERDICT) {
1387 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1388 xyas(x, y, victim));
1389 } else if (*s == 's') {
1390 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1391 s, xyas(x, y, victim));
1393 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1394 s, xyas(x, y, victim));
1396 /* Now, even though we missed, the bombs
1398 collateral_damage(x, y, newdam, bombers);
1401 /* use up missiles */
1402 if (plp->pcp->pl_flags & P_M)
1410 * Check to see if anyone hostile to the victim
1411 * is running an air defense mission on this
1412 * sector. If so, do air combat
1415 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1416 struct emp_qelem *esc_list)
1419 int mission_flags, tech, combat = 0, rel, dist, z;
1420 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1422 struct genlist *glp;
1424 struct genlist mi[MAXNOC];
1431 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1433 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1436 memset(mi, 0, sizeof(mi));
1437 for (z = 1; z < MAXNOC; z++)
1438 emp_initque((struct emp_qelem *)&mi[z]);
1440 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1442 for (cn = 1; cn < MAXNOC; cn++) {
1443 /* Check our relations */
1444 rel = getrel(getnatp(cn), victim);
1449 if (QEMPTY(&mi[cn].queue))
1452 /* Ok, make a list of all the interceptors. Note that this *copies* the
1453 * list from the mission creation. This list must be deleted later. */
1454 emp_initque(&interceptors);
1455 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1457 glp = (struct genlist *)qp;
1460 dist = mapdist(x, y, gp->x, gp->y);
1462 plp = malloc(sizeof(struct plist));
1463 memset(plp, 0, sizeof(struct plist));
1465 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1467 /* missiles go one way, so we can use all the range */
1468 if (!(plp->pcp->pl_flags & P_M))
1470 /* If it's out of range, free it and continue on */
1471 if (dist > plp->plane.pln_range) {
1475 emp_insque(&plp->queue, &interceptors);
1478 /* Remove those who cannot go */
1479 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1481 if (QEMPTY(&interceptors))
1484 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1485 * that the higher numbered planes go into battle (they should be the better ones
1486 * at fighting, if all went well.) */
1488 for (qp = interceptors.q_forw; qp != (&interceptors);
1491 tcount -= count * 2;
1492 /* Just in case there are more incoming than we have */
1495 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1499 /* Free it up and continue */
1501 glp = (struct genlist *)qp;
1506 /* Now, make a list of all the airports these planes are coming from */
1508 for (qp = interceptors.q_forw; qp != (&interceptors);
1510 plp = (struct plist *)qp;
1511 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1512 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1515 /* Now, fly them out one airport at a time */
1516 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1517 struct airport *air;
1519 air = (struct airport *)qp;
1520 dist = mapdist(x, y, air->x, air->y);
1524 /* Split off the interceptors at this base into i */
1525 divide(&interceptors, &i, air->x, air->y);
1529 mission_flags |= P_X; /* stealth (shhh) */
1530 /* gets turned off if not all choppers */
1531 mission_flags |= P_H;
1532 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1533 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1535 /* Did we run out of interceptors? */
1538 /* Did we run out of bombers? */
1539 if (QEMPTY(bomb_list)) {
1540 /* Yes, so we have to put the rest of the interceptors back, and
1541 then continue, or we leak memory */
1546 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1547 mission_flags, &tech);
1549 /* Did we run out of interceptors? */
1552 /* Did we run out of bombers? */
1553 if (QEMPTY(bomb_list)) {
1554 /* Yes, so we have to put the rest of the interceptors back, and
1555 then continue, or we leak memory */
1560 path = BestAirPath(buf, air->x, air->y, x, y);
1561 if (CANT_HAPPEN(!path))
1563 wu(0, cn, "Flying %s mission from %s\n",
1564 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1565 if (air->own && (air->own != cn)) {
1566 wu(0, air->own, "%s is flying %s mission from %s\n",
1567 cname(cn), mission_name(MI_AIR_DEFENSE),
1568 xyas(air->x, air->y, air->own));
1571 /* Now, fly the planes to the sector */
1572 emp_initque(&empty);
1573 ac_encounter(&i, &empty, air->x, air->y,
1574 path, mission_flags, 1, bomb_list, esc_list);
1576 /* If none made it, continue */
1580 /* Some made it, so now they get to try to fight. */
1581 /* Intercept the escorts first */
1583 if (!QEMPTY(esc_list)) {
1584 mpr(victim, "%s air defense planes intercept!\n",
1586 ac_combat_headers(victim, cn);
1587 ac_airtoair(esc_list, &i);
1590 /* Now intercept the bombers */
1591 if (!QEMPTY(bomb_list)) {
1593 mpr(victim, "%s air defense planes intercept!\n",
1595 ac_combat_headers(victim, cn);
1597 ac_airtoair(bomb_list, &i);
1606 /* We have to free all of these, if they are still there, otherwise they get
1607 lost and we leak memory all over the place. */
1608 for (cn = 1; cn < MAXNOC; cn++) {
1609 /* free up all this memory if it's still there */
1610 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1612 glp = (struct genlist *)qp;