2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * mission.c: Mission subroutines for planes/ships/units
30 * Known contributors to this file:
32 * Steve McClure, 1996-2000
48 #include "prototypes.h"
53 struct emp_qelem queue; /* list of units */
54 int type; /* unit type: EF_SHIP, EF_PLANE, EF_LAND */
55 void *cp; /* pointer to desc of thing */
56 void *thing; /* thing's struct */
60 struct emp_qelem queue;
65 static void add_airport(struct emp_qelem *, coord, coord);
66 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
68 static void build_mission_list(struct genlist *, coord, coord, int, natid);
69 static void build_mission_list_type(struct genlist *, coord, coord, int,
71 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
72 static int dosupport(struct genlist *, coord, coord, natid, natid);
73 static int find_airport(struct emp_qelem *, coord, coord);
74 static int mission_pln_arm(struct emp_qelem *, coord, coord, int,
75 int, struct ichrstr *, int, int, int *);
76 static void mission_pln_sel(struct emp_qelem *, int, int, int);
77 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
81 * Interdict commodities & transported planes
84 ground_interdict(coord x, coord y, natid victim, char *s)
87 int dam = 0, newdam, rel;
88 struct genlist mi[MAXNOC];
91 memset(mi, 0, sizeof(mi));
92 for (z = 1; z < MAXNOC; z++)
93 emp_initque((struct emp_qelem *)&mi[z]);
95 build_mission_list(mi, x, y, MI_INTERDICT, victim);
97 for (cn = 1; cn < MAXNOC; cn++) {
98 rel = getrel(getnatp(cn), victim);
102 if (QEMPTY(&mi[cn].queue))
105 newdam = perform_mission(x, y, victim, &mi[cn].queue,
106 MI_INTERDICT, s, SECT_HARDTARGET);
109 mpr(victim, "%s interdiction mission does %d damage!\n",
113 collateral_damage(x, y, dam, 0);
119 collateral_damage(coord x, coord y, int dam, struct emp_qelem *list)
127 getsect(x, y, §);
129 coll = ldround((double)dam * collateral_dam, 1);
132 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
133 xyas(x, y, sect.sct_own), coll);
134 sectdamage(§, coll, list);
142 only_subs(struct emp_qelem *list)
144 struct emp_qelem *qp;
148 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
149 glp = (struct genlist *)qp;
151 if (glp->type != EF_SHIP)
154 if (!(mcp->m_flags & M_SUB))
158 /* They were all subs! */
164 * Interdict ships & land units
167 unit_interdict(coord x, coord y, natid victim, 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
201 mpr(victim, "%s interdiction mission does %d damage!\n",
202 osubs ? "Enemy" : cname(cn), newdam);
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 memset(mi, 0, 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 memset(mi, 0, 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)
293 union empobj_storage item;
299 snxtitem_all(&ni, type);
300 while (nxtitem(&ni, &item)) {
301 gp = (struct empobj *)&item;
309 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
312 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
313 (gp->mission != MI_INTERDICT))
316 relat = getrel(getnatp(gp->own), victim);
317 if (mission == MI_SINTERDICT) {
318 if (relat >= FRIENDLY)
320 else if (type != EF_PLANE && relat > HOSTILE)
322 } else if (relat > HOSTILE)
325 dist = mapdist(x, y, gp->opx, gp->opy);
328 if (mission != MI_RESERVE) /* XXX */
329 oprange(gp, &radius);
334 /* Ok, it is within the operations range. */
335 /* Now check from where the object actually is */
336 dist = mapdist(x, y, gp->x, gp->y);
338 oprange(gp, &radius);
341 /* Ok, the object can get to where the x,y is */
344 if (mission != MI_AIR_DEFENSE) {
345 getsect(x, y, §);
346 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
349 * If the player->owner of the unit isn't at war
350 * with the victim, and doesn't own the
351 * sect being acted upon, and isn't the
352 * old player->owner of that sect, bounce them.
354 if (sect.sct_type != SCT_WATER &&
355 sect.sct_own != gp->own &&
356 sect.sct_oldown != gp->own)
362 glp = malloc(sizeof(struct genlist));
363 memset(glp, 0, sizeof(struct genlist));
365 glp->cp = get_empobj_chr(gp);
366 glp->thing = malloc(sizeof(item));
367 memcpy(glp->thing, &item, sizeof(item));
368 emp_insque(&glp->queue, &mi[gp->own].queue);
373 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
380 snxtitem_all(&ni, EF_PLANE);
381 while (nxtitem(&ni, &plane)) {
382 if (plane.pln_own != cn)
385 if (plane.pln_mission != MI_ESCORT)
388 dist = mapdist(x, y, plane.pln_x, plane.pln_y);
389 if (dist > plane.pln_range / 2)
392 plp = malloc(sizeof(struct plist));
393 memset(plp, 0, sizeof(struct plist));
394 plp->pcp = &plchr[(int)plane.pln_type];
396 emp_insque(&plp->queue, escorts);
401 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
402 int mission, char *s, int hardtarget)
404 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
405 struct emp_qelem *newqp;
413 struct plchrstr *pcp;
414 int dam = 0, dam2, mission_flags, tech;
415 natid plane_owner = 0;
416 int gun, shell, md, range, air_dam = 0;
417 double prb, hitchance, vrange;
419 getsect(x, y, §);
421 emp_initque(&missiles);
422 emp_initque(&bombers);
423 emp_initque(&escorts);
426 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
427 glp = (struct genlist *)qp;
430 md = mapdist(x, y, gp->x, gp->y);
432 if (glp->type == EF_LAND) {
435 if (lp->lnd_effic < LAND_MINFIREEFF)
438 if (mission == MI_SINTERDICT)
441 if ((mission == MI_INTERDICT) &&
442 (md > land_max_interdiction_range))
445 if ((lp->lnd_ship != -1) || (lp->lnd_land != -1))
448 if (lp->lnd_item[I_MILIT] < 1)
451 range = roundrange(effrange(lp->lnd_frg, lp->lnd_tech));
455 shell = lp->lnd_item[I_SHELL];
456 gun = lp->lnd_item[I_GUN];
457 if (shell == 0 || gun == 0)
460 if (has_supply(lp)) {
462 putland(lp->lnd_uid, lp);
463 dam2 = ldround(landunitgun(lp->lnd_effic, lp->lnd_dam, gun,
464 lp->lnd_ammo, shell), 1);
465 if (sect.sct_type == SCT_WATER) {
466 if (chance(lp->lnd_acc / 100.0))
467 dam2 = ldround(dam2 / 2.0, 1);
470 if (sect.sct_type == SCT_WATER)
471 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
473 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
475 "%s fires at %s %s at %s\n",
476 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
478 mpr(victim, "%s %s fires at you at %s\n",
479 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
481 } else if (glp->type == EF_SHIP) {
485 if (sp->shp_effic < 60)
487 if (sp->shp_frnge == 0)
489 if (((mission == MI_INTERDICT) ||
490 (mission == MI_SINTERDICT)) &&
491 (md > ship_max_interdiction_range))
493 if (sp->shp_item[I_MILIT] < 1)
496 if ((mcp->m_flags & M_SUB) &&
497 (sect.sct_type != SCT_WATER))
500 if (mission == MI_SINTERDICT) {
501 if (!(mcp->m_flags & M_SONAR))
503 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
505 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
506 vrange *= sp->shp_effic / 200.0;
509 /* can't look all the time */
513 if (mcp->m_flags & M_SUB) {
514 /* If we aren't shooting at "subs" or "ships" don't fire at all from
518 if (sp->shp_mobil < 0)
520 gun = sp->shp_item[I_GUN];
523 shell = sp->shp_item[I_SHELL];
524 if (shell < SHP_TORP_SHELLS)
525 shell += supply_commod(sp->shp_own,
526 sp->shp_x, sp->shp_y, I_SHELL,
527 SHP_TORP_SHELLS - shell);
528 if (shell < SHP_TORP_SHELLS)
531 range = roundrange(torprange(sp));
535 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
537 sp->shp_item[I_SHELL] = shell - SHP_TORP_SHELLS;
538 sp->shp_mobil -= shp_mobcost(sp) / 2.0;
539 putship(sp->shp_uid, sp);
540 hitchance = DTORP_HITCHANCE(md, sp->shp_visib);
543 "%s locking on %s %s in %s\n",
544 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
546 "\tEffective torpedo range is %d.0\n", range);
548 "\tWhooosh... Hitchance = %d%%\n",
549 (int)(hitchance * 100));
551 if (hitchance < 1.0 && !chance(hitchance)) {
552 wu(0, sp->shp_own, "\tMissed\n");
554 "Incoming torpedo sighted @ %s missed (whew)!\n",
558 wu(0, sp->shp_own, "\tBOOM!...\n");
559 dam2 = TORP_DAMAGE();
562 nreport(victim, N_TORP_SHIP, 0, 1);
564 "\tTorpedo hit %s %s for %d damage\n",
565 cname(victim), s, dam2);
568 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
569 xyas(x, y, victim), dam2);
571 range = roundrange(effrange(sp->shp_frnge, sp->shp_tech));
574 gun = sp->shp_item[I_GUN];
575 gun = MIN(gun, sp->shp_glim);
576 shell = sp->shp_item[I_SHELL];
578 shell += supply_commod(sp->shp_own,
579 sp->shp_x, sp->shp_y, I_SHELL,
581 gun = MIN(gun, shell);
582 gun = MIN(gun, sp->shp_item[I_MILIT] / 2.0);
586 dam2 = seagun(sp->shp_effic, gun);
590 prb = (double)md / range;
595 if (sect.sct_type == SCT_WATER)
596 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
598 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
600 "%s fires at %s %s at %s\n",
601 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
603 mpr(victim, "%s %s fires at you at %s\n",
604 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
606 sp->shp_item[I_SHELL] = shell - gun;
607 putship(sp->shp_uid, sp);
609 } else if (glp->type == EF_PLANE) {
611 if (pcp->pl_flags & P_M)
612 /* units have their own missile interdiction */
613 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
616 /* save planes for later */
617 plp = malloc(sizeof(struct plist));
619 memset(plp, 0, sizeof(struct plist));
621 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
622 if (plp->pcp->pl_flags & P_M)
623 emp_insque(&plp->queue, &missiles);
625 emp_insque(&plp->queue, &bombers);
626 plane_owner = plp->plane.pln_own;
629 if (!QEMPTY(&missiles)) {
630 /* I arbitrarily chose 100 mindam -KHS */
632 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
633 "sector", victim, mission);
634 qp = missiles.q_forw;
635 while (qp != (&missiles)) {
643 if (QEMPTY(&bombers)) {
646 glp = (struct genlist *)qp;
655 * If there are planes performing an
656 * interdict or support mission, find
657 * some escorts for them, if possible.
658 * Up to 2 per bomber, if possible.
660 find_escorts(x, y, plane_owner, &escorts);
662 if (mission == MI_SINTERDICT)
663 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
665 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
667 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
669 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
670 plp = (struct plist *)qp;
671 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
672 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
675 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
680 air = (struct airport *)qp;
681 md = mapdist(x, y, air->x, air->y);
686 /* Split off the bombers at this base into b */
687 divide(&bombers, &b, air->x, air->y);
689 /* Split off the escorts at this base into e */
690 divide(&escorts, &e, air->x, air->y);
694 mission_flags |= P_X; /* stealth (shhh) */
695 mission_flags |= P_H; /* gets turned off if not all choppers */
697 mission_flags = mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0,
698 0, mission_flags, &tech);
703 mission_flags = mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0,
704 P_F | P_ESC, mission_flags, &tech);
706 pp = BestAirPath(buf, air->x, air->y, x, y);
707 if (CANT_HAPPEN(!pp))
709 wu(0, plane_owner, "Flying %s mission from %s\n",
710 mission_name(mission), xyas(air->x, air->y, plane_owner));
711 if (air->own && (air->own != plane_owner)) {
712 wu(0, air->own, "%s is flying %s mission from %s\n",
713 cname(plane_owner), mission_name(mission),
714 xyas(air->x, air->y, air->own));
717 ac_encounter(&b, &e, air->x, air->y, pp, mission_flags, 0, 0, 0);
721 air_damage(&b, x, y, mission, victim, s, hardtarget);
729 if (sect.sct_type == SCT_WATER)
730 nreport(plane_owner, N_SHP_BOMB, victim, 1);
732 nreport(plane_owner, N_SCT_BOMB, victim, 1);
735 /* free up all this memory */
738 glp = (struct genlist *)qp;
746 while (qp != (&escorts)) {
754 while (qp != (&bombers)) {
765 cando(int mission, int type)
769 if (type == EF_PLANE)
773 if (type == EF_PLANE)
777 if ((type == EF_PLANE) || (type == EF_SHIP))
785 if (type == EF_PLANE)
798 mission_name(short mission)
802 return "an interdiction";
806 return "a offensive support";
808 return "a defensive support";
814 return "a sub interdiction";
816 return "an air defense";
818 return "a mysterious";
822 show_mission(int type, struct nstr_item *np)
824 int first = 1, radius;
825 union empobj_storage item;
828 while (nxtitem(np, &item)) {
829 gp = (struct empobj *)&item;
830 if (!player->owner || gp->own == 0)
834 pr("Thing x,y op-sect rad mission\n");
837 pr("%-25s", obj_nameof(gp));
838 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
839 if (gp->mission == MI_INTERDICT || gp->mission == MI_SUPPORT ||
840 gp->mission == MI_OSUPPORT ||
841 gp->mission == MI_DSUPPORT || gp->mission == MI_AIR_DEFENSE) {
843 oprange(gp, &radius);
844 prxy(" %3d,%-3d", gp->opx, gp->opy, player->cnum);
845 if (radius < gp->radius)
848 pr(" %4d", gp->radius);
849 } else if (gp->mission == MI_RESERVE) {
853 getsect(gp->x, gp->y, §);
854 if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
857 if (item.land.lnd_rad_max == 0)
860 plus += item.land.lnd_rad_max;
861 prxy(" %3d,%-3d", gp->x, gp->y, player->cnum);
863 } else if (gp->mission == MI_ESCORT) {
865 pr(" %4d", item.plane.pln_range / 2);
869 pr(" is on %s mission\n", mission_name(gp->mission));
871 pr(" has no mission.\n");
876 oprange(struct empobj *gp, int *radius)
880 switch (gp->ef_type) {
882 range = ldround(effrange(((struct shpstr *)gp)->shp_frnge,
883 ((struct shpstr *)gp)->shp_tech), 1);
886 range = ldround(effrange(((struct lndstr *)gp)->lnd_frg,
887 ((struct lndstr *)gp)->lnd_tech), 1);
890 /* missiles go one way, so we can use all the range */
891 if (plchr[(int)gp->type].pl_flags & P_M)
892 range = ((struct plnstr *)gp)->pln_range;
894 range = ((struct plnstr *)gp)->pln_range / 2;
897 CANT_HAPPEN("bad TYPE");
908 * Remove all planes who cannot go on
909 * the mission from the plane list.
912 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
915 struct emp_qelem *qp, *next;
920 struct plchrstr *pcp;
925 for (qp = list->q_forw; qp != list; qp = next) {
927 plp = (struct plist *)qp;
931 if (pp->pln_effic < 40) {
937 if (pp->pln_mobil < 1) {
944 if (ontradingblock(EF_PLANE, pp)) {
954 for (x = 0; x < sizeof(wantflags) * 8; x++) {
956 if ((wantflags & y) == y)
957 if ((pcp->pl_flags & y) != y) {
979 if ((pcp->pl_flags & P_ESC) || (pcp->pl_flags & P_F))
983 if ((pcp->pl_flags & P_E) ||
984 (pcp->pl_flags & P_K) || (pcp->pl_flags & P_L))
996 for (x = 0; x < sizeof(nowantflags) * 8; x++) {
998 if ((nowantflags & y) == y)
999 if ((pcp->pl_flags & y) == y)
1008 if (pp->pln_ship >= 0) {
1009 if (!getship(pp->pln_ship, &ship)) {
1012 putplane(pp->pln_uid, pp);
1017 if (!can_be_on_ship(pp->pln_uid, ship.shp_uid)) {
1020 if (ship.shp_effic < SHIP_MINEFF) {
1023 /* Can't fly off of inefficient or non-owned, non-allied ships */
1024 if ((ship.shp_effic < SHP_AIROPS_EFF) ||
1025 ((ship.shp_own != pp->pln_own) &&
1026 (getrel(getnatp(ship.shp_own), pp->pln_own) != ALLIED))) {
1032 if (pp->pln_land >= 0) {
1033 if (!getland(pp->pln_land, &land)) {
1036 putplane(pp->pln_uid, pp);
1041 if (!(pcp->pl_flags & P_E))
1043 if (land.lnd_effic < LAND_MINEFF)
1046 /* Can't fly off of inefficient or non-owned, non-allied units */
1047 if ((land.lnd_effic < LND_AIROPS_EFF) ||
1048 ((land.lnd_own != pp->pln_own) &&
1049 (getrel(getnatp(land.lnd_own), pp->pln_own) != ALLIED))) {
1055 /* Can't fly off units in ships or other units */
1056 if ((land.lnd_ship >= 0) || (land.lnd_land >= 0)) {
1062 /* Now, check the sector status if not on a plane or unit */
1063 if ((pp->pln_ship < 0) && (pp->pln_land < 0)) {
1064 /* If we can't get the sector, we can't check it, and can't fly */
1065 if (!getsect(pp->pln_x, pp->pln_y, §)) {
1070 /* First, check allied status */
1071 /* Can't fly from non-owned sectors or non-allied sectors */
1072 if ((sect.sct_own != pp->pln_own) &&
1073 (getrel(getnatp(sect.sct_own), pp->pln_own) != ALLIED)) {
1078 /* non-vtol plane */
1079 if ((pcp->pl_flags & P_V) == 0) {
1080 if ((sect.sct_type != SCT_AIRPT) || (sect.sct_effic < 40)) {
1087 if (pcp->pl_flags & P_A) {
1088 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1095 putplane(pp->pln_uid, pp);
1100 * Arm only the planes at x,y
1103 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
1104 int mission, struct ichrstr *ip, int flags,
1105 int mission_flags, int *tech)
1107 struct emp_qelem *qp;
1108 struct emp_qelem *next;
1113 for (qp = list->q_forw; qp != list; qp = next) {
1115 plp = (struct plist *)qp;
1117 if (plp->plane.pln_x != x)
1119 if (plp->plane.pln_y != y)
1122 if (mission_pln_equip(plp, ip, flags, mission) < 0) {
1127 if (flags & (P_S | P_I)) {
1128 if (plp->pcp->pl_flags & P_S)
1129 mission_flags |= P_S;
1130 if (plp->pcp->pl_flags & P_I)
1131 mission_flags |= P_I;
1133 if (*tech > plp->plane.pln_tech)
1134 *tech = plp->plane.pln_tech;
1135 if (!(plp->pcp->pl_flags & P_H))
1136 /* no stealth on this mission */
1137 mission_flags &= ~P_H;
1138 if (!(plp->pcp->pl_flags & P_X))
1139 /* no stealth on this mission */
1140 mission_flags &= ~P_X;
1141 if (!(plp->pcp->pl_flags & P_A)) {
1142 /* no asw on this mission */
1143 mission_flags &= ~P_A;
1145 if (!(plp->pcp->pl_flags & P_MINE)) {
1146 /* no asw on this mission */
1147 mission_flags &= ~P_MINE;
1151 * Mob costs for missions are 1/2 normal
1154 /* plp->plane.pln_mobil -= pln_mobcost(dist,&plp->plane,flags)/2;*/
1155 plp->plane.pln_mobil -= pln_mobcost(dist, &plp->plane, flags);
1158 return mission_flags;
1162 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
1165 struct plchrstr *pcp;
1177 if (pp->pln_ship >= 0) {
1178 getship(pp->pln_ship, &ship);
1179 item = ship.shp_item;
1180 } else if (pp->pln_land >= 0) {
1181 getland(pp->pln_land, &land);
1182 item = land.lnd_item;
1184 getsect(pp->pln_x, pp->pln_y, §);
1185 item = sect.sct_item;
1187 if (pcp->pl_fuel > item[I_PETROL]) {
1190 item[I_PETROL] -= pcp->pl_fuel;
1192 if (!(flags & P_F)) {
1198 if (pp->pln_nuketype == -1) {
1200 needed = pp->pln_load;
1204 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1207 needed = (pp->pln_load * 2) / ip->i_lbs;
1210 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
1213 needed = (pp->pln_load * 2) / ip->i_lbs;
1216 if ((pcp->pl_flags & (P_V | P_C)) == 0)
1219 needed = pp->pln_load / ip->i_lbs;
1222 if (pp->pln_nuketype == -1)
1225 case 'i': /* missile interception */
1228 needed = pp->pln_load;
1234 if (rval < 0 || (itype != I_NONE && needed <= 0)) {
1237 if (itype != I_NONE) {
1238 if (itype == I_SHELL && item[itype] < needed)
1239 item[itype] += supply_commod(plp->plane.pln_own,
1243 if (item[itype] < needed)
1245 item[itype] -= needed;
1247 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
1248 plp->bombs = needed;
1252 if (pp->pln_ship >= 0)
1253 putship(ship.shp_uid, &ship);
1254 else if (pp->pln_land >= 0)
1255 putland(land.lnd_uid, &land);
1262 * Return 1 if this x,y pair is in the list
1265 find_airport(struct emp_qelem *airp, coord x, coord y)
1267 struct emp_qelem *qp;
1270 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
1271 a = (struct airport *)qp;
1272 if ((a->x == x) && (a->y == y))
1279 /* #*# This needs to be changed to include acc's -KHS */
1281 add_airport(struct emp_qelem *airp, coord x, coord y)
1286 a = malloc(sizeof(struct airport));
1290 getsect(x, y, §);
1291 a->own = sect.sct_own;
1293 emp_insque((struct emp_qelem *)a, airp);
1297 * Take all the planes in list 1 that
1298 * are at x,y, and put them into list 2.
1301 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1303 struct emp_qelem *qp, *next;
1306 for (qp = l1->q_forw; qp != l1; qp = next) {
1308 plp = (struct plist *)qp;
1310 if (plp->plane.pln_x != x)
1312 if (plp->plane.pln_y != y)
1321 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1322 natid victim, char *s, int hardtarget)
1324 struct emp_qelem *qp;
1327 int newdam, dam = 0;
1331 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1332 plp = (struct plist *)qp;
1335 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1342 if (plp->pcp->pl_flags & P_A) {
1343 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1345 "\t%s detects sub movement in %s\n",
1346 prplane(pp), xyas(x, y, pp->pln_own));
1349 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1351 "\t%s tracks %s %s at %s\n",
1352 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1356 "\t%s depth-charging %s %s in %s\n",
1357 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1360 "\t%s pinbombing %s %s in %s\n",
1361 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1363 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1364 if (plp->plane.pln_nuketype != -1)
1366 else if (hardtarget != SECT_HARDTARGET)
1367 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1368 /* Always calculate damage */
1369 if (roll(100) <= hitchance) {
1370 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1372 if (mission == MI_INTERDICT) {
1374 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1375 prplane(pp), nukedam, cname(victim), s);
1380 "\t\thit %s %s for %d damage\n",
1381 cname(victim), s, newdam);
1385 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1386 wu(0, pp->pln_own, "missed\n");
1387 if (mission == MI_SINTERDICT) {
1389 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1390 xyas(x, y, victim));
1391 } else if (*s == 's') {
1392 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1393 s, xyas(x, y, victim));
1395 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1396 s, xyas(x, y, victim));
1398 /* Now, even though we missed, the bombs
1400 collateral_damage(x, y, newdam, bombers);
1403 /* use up missiles */
1404 if (plp->pcp->pl_flags & P_M)
1412 * Check to see if anyone hostile to the victim
1413 * is running an air defense mission on this
1414 * sector. If so, do air combat
1417 air_defense(coord x, coord y, natid victim, struct emp_qelem *bomb_list,
1418 struct emp_qelem *esc_list)
1421 int mission_flags, tech, combat = 0, rel, dist, z;
1422 struct emp_qelem *qp, interceptors, airp, i, empty, *next;
1424 struct genlist *glp;
1426 struct genlist mi[MAXNOC];
1433 for (qp = bomb_list->q_forw; qp != bomb_list; qp = qp->q_forw)
1435 for (qp = esc_list->q_forw; qp != esc_list; qp = qp->q_forw)
1438 memset(mi, 0, sizeof(mi));
1439 for (z = 1; z < MAXNOC; z++)
1440 emp_initque((struct emp_qelem *)&mi[z]);
1442 build_mission_list_type(mi, x, y, MI_AIR_DEFENSE, EF_PLANE, victim);
1444 for (cn = 1; cn < MAXNOC; cn++) {
1445 /* Check our relations */
1446 rel = getrel(getnatp(cn), victim);
1451 if (QEMPTY(&mi[cn].queue))
1454 /* Ok, make a list of all the interceptors. Note that this *copies* the
1455 * list from the mission creation. This list must be deleted later. */
1456 emp_initque(&interceptors);
1457 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1459 glp = (struct genlist *)qp;
1462 dist = mapdist(x, y, gp->x, gp->y);
1464 plp = malloc(sizeof(struct plist));
1465 memset(plp, 0, sizeof(struct plist));
1467 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
1469 /* missiles go one way, so we can use all the range */
1470 if (!(plp->pcp->pl_flags & P_M))
1472 /* If it's out of range, free it and continue on */
1473 if (dist > plp->plane.pln_range) {
1477 emp_insque(&plp->queue, &interceptors);
1480 /* Remove those who cannot go */
1481 mission_pln_sel(&interceptors, P_F, 0, SECT_HARDTARGET);
1483 if (QEMPTY(&interceptors))
1486 /* Now, delete all the extras, but delete the first ones, not the last ones, so
1487 * that the higher numbered planes go into battle (they should be the better ones
1488 * at fighting, if all went well.) */
1490 for (qp = interceptors.q_forw; qp != (&interceptors);
1493 tcount -= count * 2;
1494 /* Just in case there are more incoming than we have */
1497 for (qp = interceptors.q_forw; qp != (&interceptors); qp = next) {
1501 /* Free it up and continue */
1503 glp = (struct genlist *)qp;
1508 /* Now, make a list of all the airports these planes are coming from */
1510 for (qp = interceptors.q_forw; qp != (&interceptors);
1512 plp = (struct plist *)qp;
1513 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
1514 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
1517 /* Now, fly them out one airport at a time */
1518 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
1519 struct airport *air;
1521 air = (struct airport *)qp;
1522 dist = mapdist(x, y, air->x, air->y);
1526 /* Split off the interceptors at this base into i */
1527 divide(&interceptors, &i, air->x, air->y);
1531 mission_flags |= P_X; /* stealth (shhh) */
1532 /* gets turned off if not all choppers */
1533 mission_flags |= P_H;
1534 sam_intercept(bomb_list, &i, cn, victim, x, y, 0);
1535 sam_intercept(esc_list, &i, cn, victim, x, y, 1);
1537 /* Did we run out of interceptors? */
1540 /* Did we run out of bombers? */
1541 if (QEMPTY(bomb_list)) {
1542 /* Yes, so we have to put the rest of the interceptors back, and
1543 then continue, or we leak memory */
1548 mission_pln_arm(&i, air->x, air->y, 2 * dist, 'r', 0, P_F,
1549 mission_flags, &tech);
1551 /* Did we run out of interceptors? */
1554 /* Did we run out of bombers? */
1555 if (QEMPTY(bomb_list)) {
1556 /* Yes, so we have to put the rest of the interceptors back, and
1557 then continue, or we leak memory */
1562 path = BestAirPath(buf, air->x, air->y, x, y);
1563 if (CANT_HAPPEN(!path))
1565 wu(0, cn, "Flying %s mission from %s\n",
1566 mission_name(MI_AIR_DEFENSE), xyas(air->x, air->y, cn));
1567 if (air->own && (air->own != cn)) {
1568 wu(0, air->own, "%s is flying %s mission from %s\n",
1569 cname(cn), mission_name(MI_AIR_DEFENSE),
1570 xyas(air->x, air->y, air->own));
1573 /* Now, fly the planes to the sector */
1574 emp_initque(&empty);
1575 ac_encounter(&i, &empty, air->x, air->y,
1576 path, mission_flags, 1, bomb_list, esc_list);
1578 /* If none made it, continue */
1582 /* Some made it, so now they get to try to fight. */
1583 /* Intercept the escorts first */
1585 if (!QEMPTY(esc_list)) {
1586 mpr(victim, "%s air defense planes intercept!\n",
1588 ac_combat_headers(victim, cn);
1589 ac_airtoair(esc_list, &i);
1592 /* Now intercept the bombers */
1593 if (!QEMPTY(bomb_list)) {
1595 mpr(victim, "%s air defense planes intercept!\n",
1597 ac_combat_headers(victim, cn);
1599 ac_airtoair(bomb_list, &i);
1608 /* We have to free all of these, if they are still there, otherwise they get
1609 lost and we leak memory all over the place. */
1610 for (cn = 1; cn < MAXNOC; cn++) {
1611 /* free up all this memory if it's still there */
1612 for (qp = mi[cn].queue.q_forw; qp != (&mi[cn].queue); qp = next) {
1614 glp = (struct genlist *)qp;