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
33 * Markus Armbruster, 2003-2009
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 void mission_pln_arm(struct emp_qelem *, coord, coord, int,
74 int, struct ichrstr *);
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);
118 collateral_damage(coord x, coord y, int dam)
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);
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);
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;
296 snxtitem_all(&ni, type);
297 while (nxtitem(&ni, &item)) {
298 gp = (struct empobj *)&item;
306 if ((gp->mission != mission) && (mission != MI_SINTERDICT))
309 if ((gp->mission != mission) && (mission == MI_SINTERDICT) &&
310 (gp->mission != MI_INTERDICT))
313 relat = getrel(getnatp(gp->own), victim);
314 if (mission == MI_SINTERDICT) {
315 if (relat >= FRIENDLY)
317 else if (type != EF_PLANE && relat > HOSTILE)
319 } else if (relat > HOSTILE)
322 if (!in_oparea(gp, x, y))
326 if (mission != MI_AIR_DEFENSE) {
327 getsect(x, y, §);
328 if (getrel(getnatp(gp->own), sect.sct_own) > AT_WAR) {
331 * If the owner of the unit isn't at war
332 * with the victim, and doesn't own the
333 * sect being acted upon, and isn't the
334 * old owner of that sect, bounce them.
336 if (sect.sct_type != SCT_WATER &&
337 sect.sct_own != gp->own &&
338 sect.sct_oldown != gp->own)
344 glp = malloc(sizeof(struct genlist));
345 memset(glp, 0, sizeof(struct genlist));
346 glp->cp = get_empobj_chr(gp);
347 glp->thing = malloc(sizeof(item));
348 memcpy(glp->thing, &item, sizeof(item));
349 emp_insque(&glp->queue, &mi[gp->own].queue);
354 find_escorts(coord x, coord y, natid cn, struct emp_qelem *escorts)
360 snxtitem_all(&ni, EF_PLANE);
361 while (nxtitem(&ni, &plane)) {
362 if (plane.pln_own != cn)
364 if (plane.pln_mission != MI_ESCORT)
366 if (!in_oparea((struct empobj *)&plane, x, y))
368 plp = malloc(sizeof(struct plist));
369 memset(plp, 0, sizeof(struct plist));
370 plp->pcp = &plchr[(int)plane.pln_type];
372 emp_insque(&plp->queue, escorts);
377 perform_mission(coord x, coord y, natid victim, struct emp_qelem *list,
378 int mission, char *s, int hardtarget)
380 struct emp_qelem *qp, missiles, bombers, escorts, airp, b, e;
381 struct emp_qelem *newqp;
389 struct plchrstr *pcp;
391 natid plane_owner = 0;
392 int md, range, air_dam = 0;
393 double hitchance, vrange;
394 int targeting_ships = *s == 's'; /* "subs" or "ships" FIXME gross! */
396 getsect(x, y, §);
398 emp_initque(&missiles);
399 emp_initque(&bombers);
400 emp_initque(&escorts);
403 for (qp = list->q_forw; qp != list; qp = qp->q_forw) {
404 glp = (struct genlist *)qp;
407 md = mapdist(x, y, gp->x, gp->y);
409 if (glp->thing->ef_type == EF_LAND) {
410 lp = (struct lndstr *)glp->thing;
412 if (mission == MI_SINTERDICT)
415 if ((mission == MI_INTERDICT) &&
416 (md > land_max_interdiction_range))
419 range = roundrange(lnd_fire_range(lp));
424 putland(lp->lnd_uid, lp);
428 if (targeting_ships) {
429 if (chance(lnd_acc(lp) / 100.0))
430 dam2 = ldround(dam2 / 2.0, 1);
434 nreport(lp->lnd_own, N_SHP_SHELL, victim, 1);
436 nreport(lp->lnd_own, N_SCT_SHELL, victim, 1);
438 "%s fires at %s %s at %s\n",
439 prland(lp), cname(victim), s, xyas(x, y, lp->lnd_own));
441 mpr(victim, "%s %s fires at you at %s\n",
442 cname(lp->lnd_own), prland(lp), xyas(x, y, victim));
443 } else if (glp->thing->ef_type == EF_SHIP) {
444 sp = (struct shpstr *)glp->thing;
447 if (((mission == MI_INTERDICT) ||
448 (mission == MI_SINTERDICT)) &&
449 (md > ship_max_interdiction_range))
451 if (mission == MI_SINTERDICT) {
452 if (!(mcp->m_flags & M_SONAR))
454 if (!(mcp->m_flags & M_DCH) && !(mcp->m_flags & M_SUBT))
456 vrange = techfact(sp->shp_tech, mcp->m_vrnge);
457 vrange *= sp->shp_effic / 200.0;
460 /* can't look all the time */
464 if (mcp->m_flags & M_SUB) {
465 if (!targeting_ships)
466 continue; /* subs interdict only ships */
467 range = roundrange(torprange(sp));
470 if (!line_of_sight(NULL, x, y, gp->x, gp->y))
472 dam2 = shp_torp(sp, 1);
473 putship(sp->shp_uid, sp);
476 hitchance = shp_torp_hitchance(sp, md);
479 "%s locking on %s %s in %s\n",
480 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
482 "\tEffective torpedo range is %d.0\n", range);
484 "\tWhooosh... Hitchance = %d%%\n",
485 (int)(hitchance * 100));
487 if (!chance(hitchance)) {
488 wu(0, sp->shp_own, "\tMissed\n");
490 "Incoming torpedo sighted @ %s missed (whew)!\n",
494 wu(0, sp->shp_own, "\tBOOM!...\n");
496 nreport(victim, N_TORP_SHIP, 0, 1);
498 "\tTorpedo hit %s %s for %d damage\n",
499 cname(victim), s, dam2);
502 "Incoming torpedo sighted @ %s hits and does %d damage!\n",
503 xyas(x, y, victim), dam2);
505 range = roundrange(shp_fire_range(sp));
508 if (mission == MI_SINTERDICT)
509 dam2 = shp_dchrg(sp);
512 putship(sp->shp_uid, sp);
517 nreport(sp->shp_own, N_SHP_SHELL, victim, 1);
519 nreport(sp->shp_own, N_SCT_SHELL, victim, 1);
521 "%s fires at %s %s at %s\n",
522 prship(sp), cname(victim), s, xyas(x, y, sp->shp_own));
524 mpr(victim, "%s %s fires at you at %s\n",
525 cname(sp->shp_own), prship(sp), xyas(x, y, victim));
527 } else if (glp->thing->ef_type == EF_PLANE) {
529 if (pcp->pl_flags & P_M)
530 /* units have their own missile interdiction */
531 if (hardtarget != SECT_HARDTARGET || pcp->pl_flags & P_MAR)
534 /* save planes for later */
535 plp = malloc(sizeof(struct plist));
537 memset(plp, 0, sizeof(struct plist));
539 memcpy(&plp->plane, glp->thing, sizeof(struct plnstr));
540 if (plp->pcp->pl_flags & P_M)
541 emp_insque(&plp->queue, &missiles);
543 emp_insque(&plp->queue, &bombers);
544 plane_owner = plp->plane.pln_own;
550 if (!QEMPTY(&missiles)) {
551 /* I arbitrarily chose 100 mindam -KHS */
553 msl_launch_mindam(&missiles, x, y, hardtarget, EF_SECTOR, 100,
555 qp = missiles.q_forw;
556 while (qp != (&missiles)) {
564 if (QEMPTY(&bombers)) {
567 glp = (struct genlist *)qp;
576 * If there are planes performing an
577 * interdict or support mission, find
578 * some escorts for them, if possible.
579 * Up to 2 per bomber, if possible.
581 find_escorts(x, y, plane_owner, &escorts);
583 if (mission == MI_SINTERDICT)
584 mission_pln_sel(&bombers, P_T | P_A, 0, hardtarget);
586 mission_pln_sel(&bombers, P_T, P_A, SECT_HARDTARGET);
588 mission_pln_sel(&escorts, P_ESC | P_F, 0, SECT_HARDTARGET);
590 for (qp = bombers.q_forw; qp != (&bombers); qp = qp->q_forw) {
591 plp = (struct plist *)qp;
592 if (!find_airport(&airp, plp->plane.pln_x, plp->plane.pln_y))
593 add_airport(&airp, plp->plane.pln_x, plp->plane.pln_y);
596 for (qp = airp.q_forw; qp != (&airp); qp = qp->q_forw) {
601 air = (struct airport *)qp;
602 md = mapdist(x, y, air->x, air->y);
607 /* Split off the bombers at this base into b */
608 divide(&bombers, &b, air->x, air->y);
610 /* Split off the escorts at this base into e */
611 divide(&escorts, &e, air->x, air->y);
613 mission_pln_arm(&b, air->x, air->y, 2 * md, 'p', NULL);
618 mission_pln_arm(&e, air->x, air->y, 2 * md, 'e', NULL);
620 pp = BestAirPath(buf, air->x, air->y, x, y);
621 if (CANT_HAPPEN(!pp))
623 wu(0, plane_owner, "Flying %s mission from %s to %s\n",
624 mission_name(mission),
625 xyas(air->x, air->y, plane_owner),
626 xyas(x, y, plane_owner));
627 if (air->own && (air->own != plane_owner)) {
628 wu(0, air->own, "%s is flying %s mission from %s to %s\n",
629 cname(plane_owner), mission_name(mission),
630 xyas(air->x, air->y, air->own),
631 xyas(x, y, air->own));
634 ac_encounter(&b, &e, air->x, air->y, pp, 0);
638 air_damage(&b, x, y, mission, victim, s, hardtarget);
647 nreport(plane_owner, N_SHP_BOMB, victim, 1);
649 nreport(plane_owner, N_SCT_BOMB, victim, 1);
652 /* free up all this memory */
655 glp = (struct genlist *)qp;
663 while (qp != (&escorts)) {
671 while (qp != (&bombers)) {
682 cando(int mission, int type)
686 if (type == EF_PLANE)
690 if (type == EF_PLANE)
694 if ((type == EF_PLANE) || (type == EF_SHIP))
702 if (type == EF_PLANE)
715 mission_name(short mission)
719 return "an interdiction";
723 return "an offensive support";
725 return "a defensive support";
731 return "a sub interdiction";
733 return "an air defense";
736 return "a mysterious";
740 * Maximum distance GP can perform its mission.
741 * Note: this has nothing to do with the radius of the op-area.
742 * oprange() governs where the unit *can* strike, the op-area governs
743 * where the player wants it to strike.
746 oprange(struct empobj *gp)
748 switch (gp->ef_type) {
750 return ldround(shp_fire_range((struct shpstr *)gp), 1);
752 if (gp->mission == MI_RESERVE)
753 return lnd_reaction_range((struct lndstr *)gp);
754 return ldround(lnd_fire_range((struct lndstr *)gp), 1);
756 /* missiles go one way, so we can use all the range */
757 if (plchr[(int)gp->type].pl_flags & P_M)
758 return ((struct plnstr *)gp)->pln_range;
759 return ((struct plnstr *)gp)->pln_range / 2;
766 * Does GP's mission op area cover X,Y?
769 in_oparea(struct empobj *gp, coord x, coord y)
771 return mapdist(x, y, gp->opx, gp->opy) <= gp->radius
772 && mapdist(x, y, gp->x, gp->y) <= oprange(gp);
776 * Remove all planes who cannot go on
777 * the mission from the plane list.
780 mission_pln_sel(struct emp_qelem *list, int wantflags, int nowantflags,
783 struct emp_qelem *qp, *next;
785 struct plchrstr *pcp;
788 for (qp = list->q_forw; qp != list; qp = next) {
790 plp = (struct plist *)qp;
794 if (pp->pln_effic < 40) {
800 if (pp->pln_mobil < 1) {
807 if (ontradingblock(EF_PLANE, pp)) {
814 if (!pln_capable(pp, wantflags, nowantflags)) {
820 if (!pln_airbase_ok(pp, 0, 0)) {
826 if (pcp->pl_flags & P_A) {
827 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
834 putplane(pp->pln_uid, pp);
839 * Arm only the planes at x,y
842 mission_pln_arm(struct emp_qelem *list, coord x, coord y, int dist,
843 int mission, struct ichrstr *ip)
845 struct emp_qelem *qp;
846 struct emp_qelem *next;
850 for (qp = list->q_forw; qp != list; qp = next) {
852 plp = (struct plist *)qp;
860 if (CANT_HAPPEN(pp->pln_flags & PLN_LAUNCHED)
861 || mission_pln_equip(plp, ip, mission) < 0) {
867 pp->pln_flags |= PLN_LAUNCHED;
868 pp->pln_mobil -= pln_mobcost(dist, pp, mission);
869 putplane(pp->pln_uid, pp);
874 mission_pln_equip(struct plist *plp, struct ichrstr *ip, char mission)
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;
904 case 'p': /* pinpoint bomb */
907 case 'i': /* missile interception */
911 case 'e': /* escort */
912 case 0: /* plane interception */
920 if (itype != I_NONE) {
921 needed = load / ichr[itype].i_lbs;
924 if (CANT_HAPPEN(nuk_on_plane(pp) >= 0))
926 if (itype == I_SHELL && item[itype] < needed) {
927 if (pp->pln_ship >= 0)
928 shp_supply(&ship, I_SHELL, needed);
929 else if (pp->pln_land >= 0)
930 lnd_supply(&land, I_SHELL, needed);
932 sct_supply(§, I_SHELL, needed);
934 if (item[itype] < needed)
936 item[itype] -= needed;
940 if (pp->pln_ship >= 0)
941 putship(ship.shp_uid, &ship);
942 else if (pp->pln_land >= 0)
943 putland(land.lnd_uid, &land);
950 * Return 1 if this x,y pair is in the list
953 find_airport(struct emp_qelem *airp, coord x, coord y)
955 struct emp_qelem *qp;
958 for (qp = airp->q_forw; qp != airp; qp = qp->q_forw) {
959 a = (struct airport *)qp;
960 if ((a->x == x) && (a->y == y))
967 /* #*# This needs to be changed to include acc's -KHS */
969 add_airport(struct emp_qelem *airp, coord x, coord y)
974 a = malloc(sizeof(struct airport));
978 getsect(x, y, §);
979 a->own = sect.sct_own;
981 emp_insque((struct emp_qelem *)a, airp);
985 * Take all the planes in list 1 that
986 * are at x,y, and put them into list 2.
989 divide(struct emp_qelem *l1, struct emp_qelem *l2, coord x, coord y)
991 struct emp_qelem *qp, *next;
994 for (qp = l1->q_forw; qp != l1; qp = next) {
996 plp = (struct plist *)qp;
998 if (plp->plane.pln_x != x)
1000 if (plp->plane.pln_y != y)
1009 air_damage(struct emp_qelem *bombers, coord x, coord y, int mission,
1010 natid victim, char *s, int hardtarget)
1012 struct emp_qelem *qp;
1015 int newdam, dam = 0;
1018 for (qp = bombers->q_forw; qp != bombers; qp = qp->q_forw) {
1019 plp = (struct plist *)qp;
1022 if ((mission == MI_SINTERDICT) && !(plp->pcp->pl_flags & P_A))
1029 if (plp->pcp->pl_flags & P_A) {
1030 if (roll(100) > pln_identchance(pp, hardtarget, EF_SHIP)) {
1032 "\t%s detects sub movement in %s\n",
1033 prplane(pp), xyas(x, y, pp->pln_own));
1036 if (getrel(getnatp(pp->pln_own), victim) > HOSTILE) {
1038 "\t%s tracks %s %s at %s\n",
1039 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1043 "\t%s depth-charging %s %s in %s\n",
1044 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1047 "\t%s pinbombing %s %s in %s\n",
1048 prplane(pp), cname(victim), s, xyas(x, y, pp->pln_own));
1050 hitchance = pln_hitchance(pp, hardtarget, EF_SHIP);
1051 if (nuk_on_plane(&plp->plane) >= 0)
1053 else if (hardtarget != SECT_HARDTARGET)
1054 wu(0, pp->pln_own, "\t\t%d%% hitchance...", hitchance);
1055 if (roll(100) <= hitchance) {
1056 newdam = pln_damage(&plp->plane, 'p', 1);
1058 "\t\thit %s %s for %d damage\n",
1059 cname(victim), s, newdam);
1062 newdam = pln_damage(&plp->plane, 'p', 0);
1063 wu(0, pp->pln_own, "missed\n");
1064 if (mission == MI_SINTERDICT) {
1066 "RUMBLE... your sub in %s hears a depth-charge explode nearby\n",
1067 xyas(x, y, victim));
1068 } else if (*s == 's') {
1069 mpr(victim, "SPLASH! Bombs miss your %s in %s\n",
1070 s, xyas(x, y, victim));
1072 mpr(victim, "SPLAT! Bombs miss your %s in %s\n",
1073 s, xyas(x, y, victim));
1075 /* Now, even though we missed, the bombs
1077 collateral_damage(x, y, newdam);
1080 /* use up missiles */
1081 if (plp->pcp->pl_flags & P_M)