/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* Empire is free software: you can redistribute it and/or modify
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1998-2000
- * Markus Armbruster, 2004-2014
+ * Markus Armbruster, 2004-2016
*/
#include <config.h>
#include "combat.h"
#include "damage.h"
#include "empobj.h"
-#include "file.h"
#include "misc.h"
#include "mission.h"
#include "news.h"
#include "unit.h"
#include "xy.h"
+static void lnd_mar_put_one(struct ulist *);
static int lnd_check_one_mines(struct ulist *, int);
-static void lnd_stays(natid, char *, struct ulist *);
static int lnd_hit_mine(struct lndstr *);
static int has_helpful_engineer(coord, coord, natid);
lcp = &lchr[(int)lp->lnd_type];
-/* Spies always count as 1 during assaults. If they are the only ones
- in the assault, they get to sneak on anyway. */
-
- if (lcp->l_flags & L_SPY && combat_mode == A_ASSAULT)
- return 1;
-
men = lp->lnd_item[I_MILIT];
value = men * lnd_att(lp) * lp->lnd_effic / 100.0;
signed char orig;
int mob;
+ if (CANT_HAPPEN(lchr[llp->unit.land.lnd_type].l_flags & L_SPY))
+ return 0;
+
taken = llp->unit.land.lnd_item[I_MILIT];
- /* Spies always die */
- if (lchr[llp->unit.land.lnd_type].l_flags & L_SPY)
- llp->unit.land.lnd_effic = 0;
- else {
- eff_eq = ldround(cas * 100.0 /
- lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
- llp->unit.land.lnd_effic -= eff_eq;
- lnd_submil(&llp->unit.land, cas);
- }
+ eff_eq = ldround(cas * 100.0 /
+ lchr[llp->unit.land.lnd_type].l_item[I_MILIT], 1);
+ llp->unit.land.lnd_effic -= eff_eq;
+ lnd_submil(&llp->unit.land, cas);
if (llp->unit.land.lnd_effic < LAND_MINEFF) {
- sprintf(buf, "dies %s %s!",
- combat_mode ? att_mode[combat_mode] : "defending",
+ if (llp->unit.land.lnd_own == player->cnum)
+ pr("\n");
+ sprintf(buf, "dies %sing %s!",
+ att_mode[combat_mode],
xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
llp->unit.land.lnd_own));
lnd_print(llp->unit.land.lnd_own, llp, buf);
ret_chance = llp->unit.land.lnd_retreat - llp->unit.land.lnd_effic;
if (pct_chance(ret_chance)) {
- pr("\n");
+ if (llp->unit.land.lnd_own == player->cnum)
+ pr("\n");
lnd_print(llp->unit.land.lnd_own, llp, "fails morale check!");
llp->unit.land.lnd_mission = 0;
llp->unit.land.lnd_harden = 0;
}
void
-intelligence_report(int destination, struct lndstr *lp, int spy,
+intelligence_report(natid destination, struct lndstr *lp, int spy,
char *mess)
{
int vis = lnd_vis(lp);
char buf1[80], buf2[80], buf3[80];
- if (destination == 0)
- return;
-
- if (lp->lnd_own == 0)
+ if (!destination || !lp->lnd_own)
return;
- memset(buf1, 0, sizeof(buf1));
- memset(buf2, 0, sizeof(buf2));
- memset(buf3, 0, sizeof(buf3));
if (chance((spy + vis) / 10.0)) {
- if (destination == player->cnum)
- pr("%s %s", mess, prland(lp));
- else
- sprintf(buf1, "%s %s", mess, prland(lp));
+ sprintf(buf1, "%s %s", mess, prland(lp));
if (chance((spy + vis) / 20.0)) {
- if (destination == player->cnum)
- pr(" (eff %d, mil %d",
- roundintby(lp->lnd_effic, 5),
- roundintby(lp->lnd_item[I_MILIT], 10));
- else
- sprintf(buf2, " (eff %d, mil %d",
- roundintby(lp->lnd_effic, 5),
- roundintby(lp->lnd_item[I_MILIT], 10));
+ sprintf(buf2, " (eff %d, mil %d",
+ roundintby(lp->lnd_effic, 5),
+ roundintby(lp->lnd_item[I_MILIT], 10));
if (chance((spy + vis) / 20.0)) {
int t;
t = lp->lnd_tech - 20 + roll(40);
t = MAX(t, 0);
- if (destination == player->cnum)
- pr(", tech %d)\n", t);
- else
- sprintf(buf3, ", tech %d)\n", t);
+ sprintf(buf3, ", tech %d)", t);
} else {
- if (destination == player->cnum)
- pr(")\n");
- else
- sprintf(buf3, ")\n");
+ sprintf(buf3, ")");
}
} else {
- if (destination == player->cnum)
- pr("\n");
- else
- sprintf(buf2, "\n");
+ buf2[0] = buf3[0] = 0;
}
+ if (destination == player->cnum)
+ pr("%s%s%s\n", buf1, buf2, buf3);
+ else
+ wu(0, destination, "%s%s%s\n", buf1, buf2, buf3);
+ }
+}
+
+int
+lnd_may_mar(struct lndstr *lp, struct lndstr *ldr, char *suffix)
+{
+ struct sctstr sect;
+ int mobtype;
+
+ if (!lp->lnd_own || !getsect(lp->lnd_x, lp->lnd_y, §)) {
+ CANT_REACH();
+ return 0;
+ }
+
+ if (opt_MARKET && ontradingblock(EF_LAND, lp)) {
+ mpr(lp->lnd_own, "%s is on the trading block%s\n",
+ prland(lp), suffix);
+ return 0;
+ }
+
+ if (lp->lnd_ship >= 0) {
+ mpr(lp->lnd_own, "%s is on a ship%s\n", prland(lp), suffix);
+ return 0;
+ }
+ if (lp->lnd_land >= 0) {
+ mpr(lp->lnd_own, "%s is on a unit%s\n", prland(lp), suffix);
+ return 0;
+ }
+
+ if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
+ !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
+ lp->lnd_item[I_MILIT] == 0) {
+ mpr(lp->lnd_own, "%s has no mil on it to guide it%s\n",
+ prland(lp), suffix);
+ return 0;
+ }
+
+ switch (lnd_check_mar(lp, §)) {
+ case LND_STUCK_NOT:
+ break;
+ case LND_STUCK_NO_RAIL:
+ mpr(lp->lnd_own, "%s is stuck off the rail system%s\n",
+ prland(lp), suffix);
+ return 0;
+ default:
+ CANT_REACH();
+ /* fall through */
+ case LND_STUCK_IMPASSABLE:
+ mpr(lp->lnd_own, "%s is stuck%s\n", prland(lp), suffix);
+ return 0;
+ }
+
+ if (relations_with(sect.sct_own, lp->lnd_own) != ALLIED &&
+ !(lchr[lp->lnd_type].l_flags & L_SPY) &&
+ sect.sct_own) {
+ mpr(lp->lnd_own, "%s has been kidnapped by %s%s\n",
+ prland(lp), cname(sect.sct_own), suffix);
+ return 0;
+ }
+
+ if (ldr && (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y)) {
+ mpr(lp->lnd_own, "%s is not with the leader%s\n",
+ prland(lp), suffix);
+ return 0;
}
- if (destination != player->cnum) {
- wu(0, destination, "%s%s%s", buf1, buf2, buf3);
+ /*
+ * The marching code gets confused when trains and non-trains
+ * march together. Disallow for now.
+ */
+ mobtype = lnd_mobtype(lp);
+ if (!ldr || mobtype == lnd_mobtype(ldr))
+ ;
+ else if (mobtype == MOB_RAIL) {
+ mpr(lp->lnd_own,
+ "%s is a train and can't march with the leader%s\n",
+ prland(lp), suffix);
+ return 0;
+ } else {
+ mpr(lp->lnd_own, "%s can't rail-march with the leading train%s\n",
+ prland(lp), suffix);
+ return 0;
}
+
+ return 1;
}
void
lnd_sel(struct nstr_item *ni, struct emp_qelem *list)
{
- struct lndstr land;
- int this_mot;
- int mobtype = MOB_MOVE; /* indeterminate */
+ struct lndstr land, *ldr = NULL;
+ struct ulist *llp;
emp_initque(list);
while (nxtitem(ni, &land)) {
*/
if (!land.lnd_own || land.lnd_own != player->cnum)
continue;
- if (opt_MARKET) {
- if (ontradingblock(EF_LAND, &land)) {
- pr("unit #%d inelligible - it's for sale.\n",
- land.lnd_uid);
- continue;
- }
- }
- /*
- * The marching code gets confused when trains and non-trains
- * march together. Disallow for now.
- */
- this_mot = lnd_mobtype(&land);
- if (this_mot != mobtype) {
- if (mobtype == MOB_MOVE)
- mobtype = this_mot;
- else if (mobtype == MOB_MARCH) {
- pr("%s is a train and can't march with the leader.\n",
- prland(&land));
- continue;
- } else {
- pr("%s can't rail-march with the leading train.\n",
- prland(&land));
- continue;
- }
- }
+ if (!lnd_may_mar(&land, ldr, ""))
+ continue;
land.lnd_mission = 0;
land.lnd_rflags = 0;
- land.lnd_harden = 0;
memset(land.lnd_rpath, 0, sizeof(land.lnd_rpath));
putland(land.lnd_uid, &land);
- lnd_insque(&land, list);
+ llp = lnd_insque(&land, list);
+ if (!ldr)
+ ldr = &llp->unit.land;
}
}
/*
- * Append LP to LIST.
+ * Append @lp to @list.
* Return the new list link.
*/
struct ulist *
return mlp;
}
-/* This function assumes that the list was created by lnd_sel */
void
-lnd_mar(struct emp_qelem *list, double *minmobp, double *maxmobp,
- natid actor)
+lnd_mar_stay_behind(struct emp_qelem *list, natid actor)
{
struct emp_qelem *qp;
struct emp_qelem *next;
struct ulist *llp;
struct lndstr *lp, *ldr = NULL;
- struct sctstr sect;
- char mess[128];
+ char and_stays[32];
- *minmobp = 9876.0;
- *maxmobp = -9876.0;
for (qp = list->q_back; qp != list; qp = next) {
next = qp->q_back;
llp = (struct ulist *)qp;
lp = &llp->unit.land;
getland(lp->lnd_uid, lp);
+
if (lp->lnd_own != actor) {
mpr(actor, "%s was disbanded at %s\n",
prland(lp), xyas(lp->lnd_x, lp->lnd_y, actor));
free(llp);
continue;
}
- if (lp->lnd_ship >= 0) {
- lnd_stays(actor, "is on a ship", llp);
- continue;
- }
- if (lp->lnd_land >= 0) {
- lnd_stays(actor, "is on a unit", llp);
- continue;
- }
- if (!getsect(lp->lnd_x, lp->lnd_y, §)) {
- lnd_stays(actor, "was sucked into the sky by a strange looking spaceland", llp); /* heh -KHS */
- continue;
- }
- if (!(lchr[lp->lnd_type].l_flags & L_SPY) &&
- !(lchr[lp->lnd_type].l_flags & L_TRAIN) &&
- lp->lnd_item[I_MILIT] == 0) {
- lnd_stays(actor, "has no mil on it to guide it", llp);
- continue;
- }
- switch (lnd_check_mar(lp, §)) {
- case LND_STUCK_NOT:
- break;
- case LND_STUCK_NO_RAIL:
- lnd_stays(actor, "is stuck off the rail system", llp);
- continue;
- default:
- CANT_REACH();
- /* fall through */
- case LND_STUCK_IMPASSABLE:
- lnd_stays(actor, "is stuck", llp);
- continue;
- }
- if (relations_with(sect.sct_own, actor) != ALLIED &&
- !(lchr[lp->lnd_type].l_flags & L_SPY) &&
- sect.sct_own) {
- sprintf(mess, "has been kidnapped by %s", cname(sect.sct_own));
- lnd_stays(actor, mess, llp);
+
+ snprintf(and_stays, sizeof(and_stays), " & stays in %s",
+ xyas(lp->lnd_x, lp->lnd_y, actor));
+ if (!lnd_may_mar(lp, ldr, and_stays)) {
+ lnd_mar_put_one(llp);
continue;
}
+
if (!ldr)
ldr = lp;
- else if (lp->lnd_x != ldr->lnd_x || lp->lnd_y != ldr->lnd_y) {
- lnd_stays(actor, "is not with the leader", llp);
- continue;
- }
if (lp->lnd_mobil + 1 < (int)llp->mobil) {
llp->mobil = lp->lnd_mobil;
}
- if (llp->mobil < *minmobp)
- *minmobp = llp->mobil;
- if (llp->mobil > *maxmobp)
- *maxmobp = llp->mobil;
}
}
lnd_put_one(llp);
}
-static void
+void
lnd_mar_put(struct emp_qelem *list, natid actor)
{
struct emp_qelem *qp, *next;
}
/*
- * Sweep landmines with engineers in LAND_LIST for ACTOR.
- * All land units in LAND_LIST must be in the same sector.
- * If EXPLICIT is non-zero, this is for an explicit sweep command from
+ * Sweep landmines with engineers in @land_list for @actor.
+ * All land units in @land_list must be in the same sector.
+ * If @explicit is non-zero, this is for an explicit sweep command from
* a player. Else it's an automatic "on the move" sweep.
- * If TAKEMOB is non-zero, require and charge mobility.
+ * If @takemob is non-zero, require and charge mobility.
* Return non-zero when the land units should stop.
*/
int
sect.sct_item[I_SHELL] = sshells;
putland(llp->unit.land.lnd_uid, &llp->unit.land);
putsect(§);
- if (lnd_check_one_mines(llp, 1)) {
- stopping = 1;
- emp_remque(qp);
- free(qp);
- }
+ stopping |= lnd_check_one_mines(llp, 1);
}
return stopping;
}
sect.sct_mines--;
putsect(§);
putland(llp->unit.land.lnd_uid, &llp->unit.land);
- if (!llp->unit.land.lnd_own)
- return 1;
+ if (!llp->unit.land.lnd_own) {
+ emp_remque(&llp->queue);
+ free(llp);
+ }
+ return 1;
}
return 0;
}
{
struct emp_qelem *qp;
struct emp_qelem *next;
- struct ulist *llp;
int stopping = 0;
int with_eng = !!lnd_find_capable(land_list, L_ENGINEER);
for (qp = land_list->q_back; qp != land_list; qp = next) {
next = qp->q_back;
- llp = (struct ulist *)qp;
- if (lnd_check_one_mines(llp, with_eng)) {
- stopping = 1;
- emp_remque(qp);
- free(qp);
- }
+ stopping |= lnd_check_one_mines((struct ulist *)qp, with_eng);
}
return stopping;
}
-static void
-lnd_stays(natid actor, char *str, struct ulist *llp)
-{
- mpr(actor, "%s %s & stays in %s\n",
- prland(&llp->unit.land), str,
- xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
- lnd_mar_put_one(llp);
-}
-
-/* Return whether and why SP would be stuck in SECTP. */
+/* Return whether and why SP would be stuck in SECTP. */
enum lnd_stuck
lnd_check_mar(struct lndstr *lp, struct sctstr *sectp)
{
stopping = 1;
if (msl_hit(&plp->plane, hardtarget, EF_LAND,
N_LND_MISS, N_LND_SMISS, sublaunch, victim)) {
- newdam = pln_damage(&plp->plane, 'p', 1);
+ newdam = pln_damage(&plp->plane, 'p', "");
dam += newdam;
} else {
- newdam = pln_damage(&plp->plane, 'p', 0);
+ newdam = pln_damage(&plp->plane, 'p', NULL);
collateral_damage(newx, newy, newdam);
}
use_up_msl:
/*
* Ask user to confirm sector abandonment, if any.
- * All land units in LIST must be in the same sector.
- * If removing the land units in LIST would abandon their sector, ask
+ * All land units in @list must be in the same sector, owned by the
+ * player, and not loaded onto anything.
+ * If removing the land units in @list would abandon their sector, ask
* the user to confirm.
* Return zero when abandonment was declined, else non-zero.
*/
{
struct ulist *llp;
struct sctstr sect;
+ int abandon;
struct emp_qelem *qp;
if (QEMPTY(list))
return 1;
llp = (struct ulist *)list->q_back;
getsect(llp->unit.land.lnd_x, llp->unit.land.lnd_y, §);
- if (!abandon_askyn(§, I_CIVIL, 0, llp))
- return 0;
+ abandon = abandon_askyn(§, I_CIVIL, 0, llp);
if (!check_sect_ok(§))
return 0;
for (qp = list->q_back; qp != list; qp = qp->q_back) {
if (!check_land_ok(&((struct ulist *)qp)->unit.land))
return 0;
}
- return 1;
+ return abandon;
}
int
-lnd_mar_one_sector(struct emp_qelem *list, int dir, natid actor)
+lnd_mar_dir(struct emp_qelem *list, int dir, natid actor)
{
struct sctstr sect, osect;
struct emp_qelem *qp;
coord newx;
coord newy;
int move;
- int stopping = 0;
- int visible;
- char dp[80];
- int rel;
+ enum relations rel;
int oldown;
if (CANT_HAPPEN(QEMPTY(list)))
return 1;
- if (dir <= DIR_STOP || dir >= DIR_VIEW) {
+ if (dir <= DIR_STOP || dir > DIR_LAST) {
+ CANT_HAPPEN(dir != DIR_STOP);
lnd_mar_put(list, actor);
return 1;
}
llp = (struct ulist *)qp;
if (rel != ALLIED
&& !(lchr[llp->unit.land.lnd_type].l_flags & L_SPY)) {
- sprintf(dp, "can't go to %s", xyas(newx, newy, actor));
- lnd_stays(actor, dp, llp);
+ mpr(actor, "%s can't go to %s & stays in %s\n",
+ prland(&llp->unit.land), xyas(newx, newy, actor),
+ xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
+ lnd_mar_put_one(llp);
continue;
}
if (llp->mobil <= 0.0) {
- lnd_stays(actor, "is out of mobility", llp);
+ mpr(actor, "%s is out of mobility & stays in %s\n",
+ prland(&llp->unit.land),
+ xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y, actor));
+ lnd_mar_put_one(llp);
continue;
}
llp->unit.land.lnd_x = newx;
}
}
}
+
+ return QEMPTY(list);
+}
+
+int
+lnd_mar_gauntlet(struct emp_qelem *list, int interdict, natid actor)
+{
+ struct ulist *mlp = (struct ulist *)list->q_back;
+ coord newx, newy;
+ int stopping, visible;
+ struct emp_qelem *qp, *next;
+ struct ulist *llp;
+
+ if (CANT_HAPPEN(QEMPTY(list)))
+ return 1;
+ newx = mlp->unit.land.lnd_x;
+ newy = mlp->unit.land.lnd_y;
+ stopping = lnd_sweep(list, 0, 1, actor);
if (QEMPTY(list))
- return stopping;
- stopping |= lnd_sweep(list, 0, 1, actor);
- if (QEMPTY(list))
- return stopping;
+ return 1;
stopping |= lnd_check_mines(list);
if (QEMPTY(list))
- return stopping;
+ return 1;
visible = 0;
for (qp = list->q_back; qp != list; qp = next) {
if (!(lchr[(int)llp->unit.land.lnd_type].l_flags & L_SPY))
visible = 1;
}
- if (visible)
+ if (visible && interdict)
stopping |= lnd_interdict(list, newx, newy, actor);
return stopping;
}
/*
- * Fire land unit support against VICTIM for ATTACKER, at X,Y.
- * If DEFENDING, this is defensive support, else offensive support.
+ * Fire land unit support against @victim for @attacker, at @x,@y.
+ * If @defending, this is defensive support, else offensive support.
* Return total damage.
*/
int
}
/*
- * Increase fortification value of LP.
- * Fortification costs mobility. Use up to MOB mobility.
+ * Increase fortification value of @lp.
+ * Fortification costs mobility. Use up to @mob mobility.
* Return actual fortification increase.
*/
int
}
/*
- * Is there a engineer unit at X,Y that can help nation CN?
+ * Is there a engineer unit at @x,@y that can help nation @cn?
*/
static int
has_helpful_engineer(coord x, coord y, natid cn)
}
/*
- * Set LP's tech to TLEV along with everything else that depends on it.
+ * Set @lp's tech to @tlev along with everything else that depends on it.
*/
void
lnd_set_tech(struct lndstr *lp, int tlev)