2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2009, 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
47 #include "prototypes.h"
52 struct emp_qelem queue; /* list of units */
53 void *cp; /* pointer to desc of thing */
54 struct empobj *thing; /* thing's struct */
58 struct emp_qelem queue;
63 static void add_airport(struct emp_qelem *, coord, coord);
64 static int air_damage(struct emp_qelem *, coord, coord, int, natid,
66 static void build_mission_list(struct genlist *, coord, coord, int, natid);
67 static void build_mission_list_type(struct genlist *, coord, coord, int,
69 static void divide(struct emp_qelem *, struct emp_qelem *, coord, coord);
70 static int dosupport(struct genlist *, coord, coord, natid, natid);
71 static int find_airport(struct emp_qelem *, coord, coord);
72 static void mission_pln_arm(struct emp_qelem *, coord, coord, int,
73 int, struct ichrstr *, int);
74 static void mission_pln_sel(struct emp_qelem *, int, int, int);
75 static int perform_mission(coord, coord, natid, struct emp_qelem *, int,
79 * Interdict commodities & transported planes
82 ground_interdict(coord x, coord y, natid victim, char *s)
85 int dam = 0, newdam, rel;
86 struct genlist mi[MAXNOC];
89 memset(mi, 0, sizeof(mi));
90 for (z = 1; z < MAXNOC; z++)
91 emp_initque((struct emp_qelem *)&mi[z]);
93 build_mission_list(mi, x, y, MI_INTERDICT, victim);
95 for (cn = 1; cn < MAXNOC; cn++) {
96 rel = getrel(getnatp(cn), victim);
100 if (QEMPTY(&mi[cn].queue))
103 newdam = perform_mission(x, y, victim, &mi[cn].queue,
104 MI_INTERDICT, s, SECT_HARDTARGET);
107 mpr(victim, "%s interdiction mission does %d damage!\n",
111 collateral_damage(x, y, dam);
117 collateral_damage(coord x, coord y, int dam)
125 getsect(x, y, §);
127 coll = ldround((double)dam * collateral_dam, 1);
130 mpr(sect.sct_own, "%s takes %d%% collateral damage\n",
131 xyas(x, y, sect.sct_own), coll);
132 sectdamage(§, coll);
140 only_subs(struct emp_qelem *list)
142 struct emp_qelem *qp;
146 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
147 glp = (struct genlist *)qp;
149 if (glp->thing->ef_type != EF_SHIP)
152 if (!(mcp->m_flags & M_SUB))
156 /* They were all subs! */
162 * Interdict ships & land units
165 unit_interdict(coord x, coord y, natid victim, char *s, int hardtarget,
170 struct genlist mi[MAXNOC];
174 memset(mi, 0, sizeof(mi));
175 for (z = 1; z < MAXNOC; z++)
176 emp_initque((struct emp_qelem *)&mi[z]);
178 build_mission_list(mi, x, y, mission, victim);
180 for (cn = 1; cn < MAXNOC; cn++) {
183 if (mission == MI_SINTERDICT) {
184 if (getrel(getnatp(cn), victim) >= FRIENDLY)
186 } else if (getrel(getnatp(cn), victim) > HOSTILE)
189 if (QEMPTY(&mi[cn].queue))
192 osubs = only_subs(&mi[cn].queue);
193 newdam = perform_mission(x, y, victim, &mi[cn].queue,
194 mission, s, hardtarget);
197 /* If only subs responded, then we don't know who's
199 mpr(victim, "%s interdiction mission does %d damage!\n",
200 osubs ? "Enemy" : cname(cn), newdam);
204 collateral_damage(x, y, dam);
210 * Perform a mission against victim, on behalf of actee
213 off_support(coord x, coord y, natid victim, natid actee)
216 struct genlist mi[MAXNOC];
219 memset(mi, 0, sizeof(mi));
220 for (z = 1; z < MAXNOC; z++)
221 emp_initque((struct emp_qelem *)&mi[z]);
223 build_mission_list(mi, x, y, MI_SUPPORT, victim);
224 build_mission_list(mi, x, y, MI_OSUPPORT, victim);
226 dam = dosupport(mi, x, y, victim, actee);
231 * Perform a mission against victim, on behalf of actee
234 def_support(coord x, coord y, natid victim, natid actee)
237 struct genlist mi[MAXNOC];
240 memset(mi, 0, sizeof(mi));
241 for (z = 1; z < MAXNOC; z++)
242 emp_initque((struct emp_qelem *)&mi[z]);
244 build_mission_list(mi, x, y, MI_SUPPORT, victim);
245 build_mission_list(mi, x, y, MI_DSUPPORT, victim);
247 dam = dosupport(mi, x, y, victim, actee);
252 dosupport(struct genlist *mi, coord x, coord y, natid victim, natid actee)
258 for (cn = 1; cn < MAXNOC; cn++) {
259 rel = getrel(getnatp(cn), actee);
260 if ((cn != actee) && (rel != ALLIED))
262 rel = getrel(getnatp(cn), victim);
263 if ((cn != actee) && (rel != AT_WAR))
266 if (QEMPTY(&mi[cn].queue))
269 dam += perform_mission(x, y, victim, &mi[cn].queue, MI_SUPPORT,
270 "", SECT_HARDTARGET);
276 build_mission_list(struct genlist *mi, coord x, coord y, int mission,
279 build_mission_list_type(mi, x, y, mission, EF_LAND, victim);
280 build_mission_list_type(mi, x, y, mission, EF_SHIP, victim);
281 build_mission_list_type(mi, x, y, mission, EF_PLANE, victim);
285 build_mission_list_type(struct genlist *mi, coord x, coord y, int mission,
286 int type, natid victim)
291 union empobj_storage item;
295 snxtitem_all(&ni, type);
296 while (nxtitem(&ni, &item)) {
297 gp = (struct empobj *)&item;
305 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
308 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
309 (gp->mission != MI_INTERDICT))
312 relat = getrel(getnatp(gp->own), victim);
313 if (mission == MI_SINTERDICT) {
314 if (relat >= FRIENDLY)
316 else if (type != EF_PLANE && relat > HOSTILE)
318 } else if (relat > HOSTILE)
321 if (!in_oparea(gp, x, y))
325 if (mission != MI_AIR_DEFENSE) {
326 getsect(x, y, §);
327 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
330 * If the owner of the unit isn't at war
331 * with the victim, and doesn't own the
332 * sect being acted upon, and isn't the
333 * old owner of that sect, bounce them.
335 if (sect.sct_type != SCT_WATER &&
336 sect.sct_own != gp->own &&
337 sect.sct_oldown != gp->own)
343 glp = malloc(sizeof(struct genlist));
344 memset(glp, 0, sizeof(struct genlist));
345 glp->cp = get_empobj_chr(gp);
346 glp->thing = malloc(sizeof(item));
347 memcpy(glp->thing, &item, sizeof(item));
348 emp_insque(&glp->queue, &mi[gp->own].queue);
353 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
359 snxtitem_all(&ni, EF_PLANE);
360 while (nxtitem(&ni, &plane)) {
361 if (plane.pln_own != cn)
363 if (plane.pln_mission != MI_ESCORT)
365 if (!in_oparea((struct empobj *)&plane, x, y))
367 plp = malloc(sizeof(struct plist));
368 memset(plp, 0, sizeof(struct plist));
369 plp->pcp = &plchr[(int)plane.pln_type];
371 emp_insque(&plp->queue, escorts);
376 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
377 int mission, char *s, int hardtarget)
379 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
380 struct emp_qelem *newqp;
388 struct plchrstr *pcp;
390 natid plane_owner = 0;
391 int md, range, air_dam = 0;
392 double hitchance, vrange;
393 int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
395 getsect(x, y, §);
397 emp_initque(&missiles);
398 emp_initque(&bombers);
399 emp_initque(&escorts);
402 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
403 glp = (struct genlist *)qp;
406 md = mapdist(x, y, gp->x, gp->y);
408 if (glp->thing->ef_type == EF_LAND) {
409 lp = (struct lndstr *)glp->thing;
411 if (mission == MI_SINTERDICT)
414 if ((mission == MI_INTERDICT) &&
415 (md > land_max_interdiction_range))
418 range = roundrange(lnd_fire_range(lp));
423 putland(lp->lnd_uid, lp);
427 if (targeting_ships) {
428 if (chance(lnd_acc(lp) / 100.0))
429 dam2 = ldround(dam2 / 2.0, 1);
433 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
435 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
437 "%s fires at %s %s at %s\n",
438 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
440 mpr(victim, "%s %s fires at you at %s\n",
441 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
442 } else if (glp->thing->ef_type == EF_SHIP) {
443 sp = (struct shpstr *)glp->thing;
446 if (((mission == MI_INTERDICT) ||
447 (mission == MI_SINTERDICT)) &&
448 (md > ship_max_interdiction_range))
450 if (mission == MI_SINTERDICT) {
451 if (!(mcp->m_flags & M_SONAR))
453 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
455 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
456 vrange *= sp->shp_effic / 200.0;
459 /* can't look all the time */
463 if (mcp->m_flags & M_SUB) {
464 if (!targeting_ships)
465 continue; /* subs interdict only ships */
466 range = roundrange(torprange(sp));
469 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
471 dam2 = shp_torp(sp, 1);
472 putship(sp->shp_uid, sp);
475 hitchance = shp_torp_hitchance(sp, md);
478 "%s locking on %s %s in %s\n",
479 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
481 "\tEffective torpedo range is %d.0\n", range);
483 "\tWhooosh... Hitchance = %d%%\n",
484 (int)(hitchance * 100));
486 if (!chance(hitchance)) {
487 wu(0, sp->shp_own, "\tMissed\n");
489 "Incoming torpedo sighted @ %s missed (whew)!\n",
493 wu(0, sp->shp_own, "\tBOOM!...\n");
495 nreport(victim, N_TORP_SHIP, 0, 1);
497 "\tTorpedo hit %s %s for %d damage\n",
498 cname(victim), s, dam2);
501 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
502 xyas(x, y, victim), dam2);
504 range = roundrange(shp_fire_range(sp));
507 if (mission == MI_SINTERDICT)
508 dam2 = shp_dchrg(sp);
511 putship(sp->shp_uid, sp);
516 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
518 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
520 "%s fires at %s %s at %s\n",
521 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
523 mpr(victim, "%s %s fires at you at %s\n",
524 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
526 } else if (glp->thing->ef_type == EF_PLANE) {
528 if (pcp->pl_flags & P_M)
529 /* units have their own missile interdiction */
530 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
533 /* save planes for later */
534 plp = malloc(sizeof(struct plist));
536 memset(plp, 0, sizeof(struct plist));
538 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
539 if (plp->pcp->pl_flags & P_M)
540 emp_insque(&plp->queue, &missiles);
542 emp_insque(&plp->queue, &bombers);
543 plane_owner = plp->plane.pln_own;
549 if (!QEMPTY(&missiles)) {
550 /* I arbitrarily chose 100 mindam -KHS */
552 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
553 "sector", victim, mission);
554 qp = missiles.q_forw;
555 while (qp != (&missiles)) {
563 if (QEMPTY(&bombers)) {
566 glp = (struct genlist *)qp;
575 * If there are planes performing an
576 * interdict or support mission, find
577 * some escorts for them, if possible.
578 * Up to 2 per bomber, if possible.
580 find_escorts(x, y, plane_owner, &escorts);
582 if (mission == MI_SINTERDICT)
583 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
585 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
587 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
589 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
590 plp = (struct plist *)qp;
591 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
592 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
595 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
600 air = (struct airport *)qp;
601 md = mapdist(x, y, air->x, air->y);
606 /* Split off the bombers at this base into b */
607 divide(&bombers, &b, air->x, air->y);
609 /* Split off the escorts at this base into e */
610 divide(&escorts, &e, air->x, air->y);
612 mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', 0, 0);
617 mission_pln_arm(&e, air->x, air->y, 2 * md, 'p', 0, P_F | P_ESC);
619 pp = BestAirPath(buf, air->x, air->y, x, y);
620 if (CANT_HAPPEN(!pp))
622 wu(0, plane_owner, "Flying %s mission from %s to %s\n",
623 mission_name(mission),
624 xyas(air->x, air->y, plane_owner),
625 xyas(x, y, plane_owner));
626 if (air->own && (air->own != plane_owner)) {
627 wu(0, air->own, "%s is flying %s mission from %s to %s\n",
628 cname(plane_owner), mission_name(mission),
629 xyas(air->x, air->y, air->own),
630 xyas(x, y, air->own));
633 ac_encounter(&b, &e, air->x, air->y, pp, 0);
637 air_damage(&b, x, y, mission, victim, s, hardtarget);
646 nreport(plane_owner, N_SHP_BOMB, victim, 1);
648 nreport(plane_owner, N_SCT_BOMB, victim, 1);
651 /* free up all this memory */
654 glp = (struct genlist *)qp;
662 while (qp != (&escorts)) {
670 while (qp != (&bombers)) {
681 cando(int mission, int type)
685 if (type == EF_PLANE)
689 if (type == EF_PLANE)
693 if ((type == EF_PLANE) || (type == EF_SHIP))
701 if (type == EF_PLANE)
714 mission_name(short mission)
718 return "an interdiction";
722 return "an offensive support";
724 return "a defensive support";
730 return "a sub interdiction";
732 return "an air defense";
735 return "a mysterious";
739 * Maximum distance GP can perform its mission.
740 * Note: this has nothing to do with the radius of the op-area.
741 * oprange() governs where the unit *can* strike, the op-area governs
742 * where the player wants it to strike.
745 oprange(struct empobj *gp)
747 switch (gp->ef_type) {
749 return ldround(shp_fire_range((struct shpstr *)gp), 1);
751 if (gp->mission == MI_RESERVE)
752 return lnd_reaction_range((struct lndstr *)gp);
753 return ldround(lnd_fire_range((struct lndstr *)gp), 1);
755 /* missiles go one way, so we can use all the range */
756 if (plchr[(int)gp->type].pl_flags & P_M)
757 return ((struct plnstr *)gp)->pln_range;
758 return ((struct plnstr *)gp)->pln_range / 2;
765 * Does GP's mission op area cover X,Y?
768 in_oparea(struct empobj *gp, coord x, coord y)
770 return mapdist(x, y, gp->opx, gp->opy) <= gp->radius
771 && mapdist(x, y, gp->x, gp->y) <= oprange(gp);
775 * Remove all planes who cannot go on
776 * the mission from the plane list.
779 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
782 struct emp_qelem *qp, *next;
784 struct plchrstr *pcp;
787 for (qp = list->q_forw; qp != list; qp = next) {
789 plp = (struct plist *)qp;
793 if (pp->pln_effic < 40) {
799 if (pp->pln_mobil < 1) {
806 if (ontradingblock(EF_PLANE, pp)) {
813 if (!pln_capable(pp, wantflags, nowantflags)) {
819 if (!pln_airbase_ok(pp, 0, 0)) {
825 if (pcp->pl_flags & P_A) {
826 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
833 putplane(pp->pln_uid, pp);
838 * Arm only the planes at x,y
841 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
842 int mission, struct ichrstr *ip, int flags)
844 struct emp_qelem *qp;
845 struct emp_qelem *next;
849 for (qp = list->q_forw; qp != list; qp = next) {
851 plp = (struct plist *)qp;
859 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
860 || mission_pln_equip(plp, ip, flags, mission) < 0) {
866 pp->pln_flags |= PLN_LAUNCHED;
867 pp->pln_mobil -= pln_mobcost(dist, pp, flags);
868 putplane(pp->pln_uid, pp);
873 mission_pln_equip(struct plist *plp, struct ichrstr *ip, int flags,
876 struct plchrstr *pcp;
887 if (pp->pln_ship >= 0) {
888 getship(pp->pln_ship, &ship);
889 item = ship.shp_item;
890 } else if (pp->pln_land >= 0) {
891 getland(pp->pln_land, &land);
892 item = land.lnd_item;
894 getsect(pp->pln_x, pp->pln_y, §);
895 item = sect.sct_item;
897 if (pcp->pl_fuel > item[I_PETROL]) {
900 item[I_PETROL] -= pcp->pl_fuel;
901 if (!(flags & P_F)) {
906 case 's': /* strategic bomb */
907 case 'p': /* pinpoint bomb */
908 if (nuk_on_plane(pp) < 0) {
913 case 't': /* transport */
915 if ((pcp->pl_flags & P_C) == 0 || ip == 0)
918 needed = (load * 2) / ip->i_lbs;
920 case 'a': /* paradrop */
921 if ((pcp->pl_flags & (P_V | P_C)) == 0)
924 needed = load / ip->i_lbs;
926 case 'i': /* missile interception */
932 case 'r': /* reconnaissance */
933 case 0: /* plane interception */
939 if (itype != I_NONE && needed <= 0)
941 if (itype != I_NONE) {
942 if (itype == I_SHELL && item[itype] < needed)
943 item[itype] += supply_commod(plp->plane.pln_own,
947 if (item[itype] < needed)
949 item[itype] -= needed;
951 if (itype == I_SHELL && (mission == 's' || mission == 'p'))
956 if (pp->pln_ship >= 0)
957 putship(ship.shp_uid, &ship);
958 else if (pp->pln_land >= 0)
959 putland(land.lnd_uid, &land);
966 * Return 1 if this x,y pair is in the list
969 find_airport(struct emp_qelem *airp, coord x, coord y)
971 struct emp_qelem *qp;
974 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
975 a = (struct airport *)qp;
976 if ((a->x == x) && (a->y == y))
983 /* #*# This needs to be changed to include acc's -KHS */
985 add_airport(struct emp_qelem *airp, coord x, coord y)
990 a = malloc(sizeof(struct airport));
994 getsect(x, y, §);
995 a->own = sect.sct_own;
997 emp_insque((struct emp_qelem *)a, airp);
1001 * Take all the planes in list 1 that
1002 * are at x,y, and put them into list 2.
1005 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
1007 struct emp_qelem *qp, *next;
1010 for (qp = l1->q_forw; qp != l1; qp = next) {
1012 plp = (struct plist *)qp;
1014 if (plp->plane.pln_x != x)
1016 if (plp->plane.pln_y != y)
1025 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1026 natid victim, char *s, int hardtarget)
1028 struct emp_qelem *qp;
1031 int newdam, dam = 0;
1035 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1036 plp = (struct plist *)qp;
1039 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1046 if (plp->pcp->pl_flags & P_A) {
1047 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1049 "\t%s detects sub movement in %s\n",
1050 prplane(pp), xyas(x, y, pp->pln_own));
1053 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1055 "\t%s tracks %s %s at %s\n",
1056 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1060 "\t%s depth-charging %s %s in %s\n",
1061 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1064 "\t%s pinbombing %s %s in %s\n",
1065 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1067 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1068 if (nuk_on_plane(&plp->plane) >= 0)
1070 else if (hardtarget != SECT_HARDTARGET)
1071 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1072 /* Always calculate damage */
1073 if (roll(100) <= hitchance) {
1074 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 1);
1076 if (mission == MI_INTERDICT) {
1078 "\t\tnuclear warhead on plane %s does %d damage to %s %s\n",
1079 prplane(pp), nukedam, cname(victim), s);
1084 "\t\thit %s %s for %d damage\n",
1085 cname(victim), s, newdam);
1089 newdam = pln_damage(&plp->plane, x, y, 'p', &nukedam, 0);
1090 wu(0, pp->pln_own, "missed\n");
1091 if (mission == MI_SINTERDICT) {
1093 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1094 xyas(x, y, victim));
1095 } else if (*s == 's') {
1096 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1097 s, xyas(x, y, victim));
1099 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1100 s, xyas(x, y, victim));
1102 /* Now, even though we missed, the bombs
1104 collateral_damage(x, y, newdam);
1107 /* use up missiles */
1108 if (plp->pcp->pl_flags & P_M)