/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2009, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure
*
* This program is free software; you can redistribute it and/or modify
* ---
*
* attsub.c: Attack subroutines
- *
+ *
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1996-2000
- * Markus Armbruster, 2006
+ * Markus Armbruster, 2006-2009
*/
#include <config.h>
static int take_def(int combat_mode, struct emp_qelem *list,
struct combat *off, struct combat *def);
-static int get_land(int combat_mode, struct combat *def, int uid,
+static int get_land(int combat_mode, struct combat *def,
struct ulist *llp, int victim_land);
char *att_mode[] = {
/*
* This is the combat object "type" based integrity check. It basically
- * splits along three divisions: ship/sector, attacker/defender,
+ * splits along three divisions: ship/sector, attacker/defender,
* first time/not first time.
*/
if ((com->mob - com->mobcost) < -127)
sect.sct_mobil = -127;
else
- sect.sct_mobil = (short)(com->mob - com->mobcost);
+ sect.sct_mobil = com->mob - com->mobcost;
} else {
if ((com->mob - com->mobcost) < 0)
sect.sct_mobil = 0;
else
- sect.sct_mobil = (short)(com->mob - com->mobcost);
+ sect.sct_mobil = com->mob - com->mobcost;
}
}
- makelost(EF_SECTOR, sect.sct_own, 0, sect.sct_x, sect.sct_y);
- makenotlost(EF_SECTOR, com->own, 0, sect.sct_x, sect.sct_y);
sect.sct_own = com->own;
if (com->plague) {
if (sect.sct_pstage == PLG_HEALTHY)
else
land.lnd_mobil = (signed char)(com->mob - com->mobcost);
}
- makelost(EF_LAND, land.lnd_own, land.lnd_uid,
- land.lnd_x, land.lnd_y);
land.lnd_own = com->own;
- makenotlost(EF_LAND, land.lnd_own, land.lnd_uid,
- land.lnd_x, land.lnd_y);
if (com->plague) {
if (land.lnd_pstage == PLG_HEALTHY)
land.lnd_pstage = PLG_EXPOSED;
}
if (!(com->lnd_lcp->l_flags & L_SPY))
land.lnd_item[I_MILIT] = com->mil;
- lnd_count_units(&land);
if (com->own == player->cnum) {
land.lnd_mission = 0;
land.lnd_rflags = 0;
else
ship.shp_mobil = (signed char)(com->mob - com->mobcost);
}
- makelost(EF_SHIP, ship.shp_own, ship.shp_uid,
- ship.shp_x, ship.shp_y);
ship.shp_own = com->own;
- makenotlost(EF_SHIP, ship.shp_own, ship.shp_uid,
- ship.shp_x, ship.shp_y);
if (com->plague) {
if (ship.shp_pstage == PLG_HEALTHY)
ship.shp_pstage = PLG_EXPOSED;
}
ship.shp_item[I_MILIT] = com->mil;
- count_units(&ship);
if (com->own == player->cnum) {
ship.shp_mission = 0;
ship.shp_rflags = 0;
if (att_get_combat(off, 0) < 0)
return abort_attack();
if (off->type == EF_SHIP &&
+ !(off->x == def->x && off->y == def->y) &&
(!getsect(off->x, off->y, §) ||
sect.sct_type != SCT_WATER)) {
pr("%s can not %s from that far inland!\n",
}
getship(off->shp_uid, &aship);
getship(def->shp_uid, &dship);
- if (techfact(aship.shp_tech, 1.0) * aship.shp_speed * off->eff
- <= techfact(dship.shp_tech, 1.0) * dship.shp_speed * def->eff) {
+ if (techfact(aship.shp_tech, shp_speed(&aship)) * off->eff
+ <= techfact(dship.shp_tech, shp_speed(&dship)) * def->eff) {
pr("Victim ship moves faster than you do!\n");
if (def->own)
wu(0, def->own,
}
if (off->type == EF_SECTOR) {
getsect(off->x, off->y, §);
- sectdamage(§, dam, 0);
+ sectdamage(§, dam);
putsect(§);
pr("Enemy fleet at %s does %d damage to %s\n",
xyas(def->x, def->y, player->cnum), dam, prcom(0, off));
*fortp = *shipp = 0;
*landp = *planep = 0;
- if (!(p = getstarg(player->argp[offset], "Use fort support? ",
- buf)))
+ p = getstarg(player->argp[offset], "Use fort support? ", buf);
+ if (!p)
return RET_SYN;
if ((*p == 'y') || (*p == 'Y'))
*fortp = 1;
- if (!(p = getstarg(player->argp[offset + 1], "Use ship support? ",
- buf)))
+ p = getstarg(player->argp[offset + 1], "Use ship support? ", buf);
+ if (!p)
return RET_SYN;
if ((*p == 'y') || (*p == 'Y'))
*shipp = 1;
- if (!(p = getstarg(player->argp[offset + 2], "Use land support? ",
- buf)))
+ p = getstarg(player->argp[offset + 2], "Use land support? ", buf);
+ if (!p)
return RET_SYN;
if ((*p == 'y') || (*p == 'Y'))
*landp = 1;
- if (!(p = getstarg(player->argp[offset + 3], "Use plane support? ",
- buf)))
+ p = getstarg(player->argp[offset + 3], "Use plane support? ", buf);
+ if (!p)
return RET_SYN;
if ((*p == 'y') || (*p == 'Y'))
case EF_SHIP:
/* the 2 in the formula below is a fudge factor */
getship(def->shp_uid, &ship);
- off->mobcost += (def->eff / 100) * (ship.shp_speed / 2);
+ off->mobcost += (def->eff / 100) * (shp_speed(&ship) / 2);
}
}
}
sprintf(prompt, "Number of mil from %s (max %d) : ",
prcom(0, off), mob_support);
}
- if ((attacking_mil = onearg(0, prompt)) < 0)
+ if ((attacking_mil = onearg(NULL, prompt)) < 0)
abort_attack();
if (att_abort(combat_mode, off, def))
return 0;
army = '~';
for (;;) {
p = getstring(prompt, buf);
- if (player->aborted || (p && *p == 'q')) {
+ if (!p || *p == 'q') {
abort_attack();
return 'N';
}
- if (!p || !*p)
+ if (!*p)
return 'n';
if (tolower(*p) == 'y' || tolower(*p) == 'n')
return *p;
{
struct nstr_item ni;
struct lndstr land;
- double mobcost;
+ double pathcost, mobcost;
+ int reqmob;
struct ulist *llp;
struct lchrstr *lcp;
double att_val;
pr("Land units are not able to board this kind of ship\n");
return;
}
+ if (def->type == EF_SHIP
+ && (def->shp_mcp->m_flags & (M_SUPPLY | M_SUB)) != M_SUPPLY
+ && !(lcp->l_flags & L_LIGHT)) {
+ pr("Only light land units can board this kind of ship\n");
+ continue;
+ }
if (land.lnd_mobil <= 0) {
pr("%s is out of mobility, and cannot %s\n",
prland(&land), att_mode[combat_mode]);
* of high-mobility sectors (mountains): for those we
* still require attack mobility.
*/
- mobcost = att_mobcost(off->own, def, lnd_mobtype(&land));
- if (mobcost < 1.0) {
+ pathcost = att_mobcost(off->own, def, lnd_mobtype(&land));
+ mobcost = lnd_pathcost(&land, pathcost);
+ if (pathcost < 1.0) {
if (land.lnd_mobil <= 0) {
pr("%s is out of mobility\n", prland(&land));
continue;
}
} else {
- mobcost = lnd_pathcost(&land, mobcost);
- if (land.lnd_mobil < mobcost) {
+ reqmob = MIN(land_mob_max, (int)ceil(mobcost));
+ if (land.lnd_mobil < reqmob) {
pr("%s does not have enough mobility (%d needed)\n",
- prland(&land), (int)ceil(mobcost));
+ prland(&land), reqmob);
continue;
}
}
pr("%s has no offensive strength\n", prland(&land));
continue;
}
- resupply_all(&land);
- putland(land.lnd_uid, &land);
- if (!has_supply(&land)) {
+ if (!lnd_supply_all(&land)) {
pr("%s is out of supply, and cannot %s\n",
prland(&land), att_mode[combat_mode]);
continue;
memset(llp, 0, sizeof(struct ulist));
emp_insque(&llp->queue, olist);
llp->mobil = mobcost;
- if (!get_land(combat_mode, def, land.lnd_uid, llp, 0))
- continue;
+ llp->unit.land = land;
+ llp->x = llp->unit.land.lnd_x;
+ llp->y = llp->unit.land.lnd_y;
+ llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
+ llp->eff = llp->unit.land.lnd_effic;
if (lnd_spyval(&land) > *a_spyp)
*a_spyp = lnd_spyval(&land);
if (((struct lchrstr *)llp->chrp)->l_flags & L_ENGINEER)
eff = com->eff / 100.0;
if (com->own == player->cnum) {
str = com->sct_dcp->d_ostr;
- eff = 1.0 + ((str - 1.0) * eff);
+ eff = 1.0 + (str - 1.0) * eff;
} else
eff = sector_strength(getsectp(com->x, com->y));
} else if (com->type == EF_SHIP && com->own != player->cnum) {
getship(com->shp_uid, &ship);
- eff = 1.0 + ship.shp_armor / 100.0;
+ eff = 1.0 + shp_armor(&ship) / 100.0;
}
return eff;
}
continue;
if (def->type == EF_LAND && land.lnd_land != def->lnd_uid)
continue;
- if (!list) { /* Just estimating the enemy strength */
- intelligence_report(player->cnum, &land, a_spy,
- "Scouts report defending unit:");
- continue;
- }
+ intelligence_report(player->cnum, &land, a_spy,
+ "Scouts report defending unit:");
if (!(llp = malloc(sizeof(struct ulist)))) {
logerror("Malloc failed in attack!\n");
abort_attack();
}
memset(llp, 0, sizeof(struct ulist));
emp_insque(&llp->queue, list);
- llp->supplied = has_supply(&land);
- if (!get_land(A_DEFEND, def, land.lnd_uid, llp, 1))
- continue;
+ llp->supplied = lnd_supply_all(&land);
+ llp->unit.land = land;
+ llp->x = llp->unit.land.lnd_x;
+ llp->y = llp->unit.land.lnd_y;
+ llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
+ llp->eff = llp->unit.land.lnd_effic;
if (lnd_spyval(&land) > *d_spyp)
*d_spyp = lnd_spyval(&land);
}
for (qp = olist->q_forw; qp != olist; qp = next) {
next = qp->q_forw;
llp = (struct ulist *)qp;
- if (check && !get_land(combat_mode, 0, llp->unit.land.lnd_uid, llp, 0))
+ if (check &&
+ !get_land(combat_mode, NULL, llp, 0))
continue;
if (combat_mode == A_ATTACK) {
w = -1;
for (qp = list->q_forw; qp != list; qp = next) {
next = qp->q_forw;
llp = (struct ulist *)qp;
- if (check && !get_land(A_DEFEND, def, llp->unit.land.lnd_uid, llp, 1))
+ if (check && !get_land(A_DEFEND, def, llp, 1))
continue;
d_unit = defense_val(&llp->unit.land);
if (!llp->supplied)
*/
static int
-get_land(int combat_mode, struct combat *def, int uid, struct ulist *llp,
+get_land(int combat_mode, struct combat *def, struct ulist *llp,
int victim_land)
{
struct lndstr *lp = &llp->unit.land;
char buf[512];
- getland(uid, lp);
+ getland(llp->unit.land.lnd_uid, lp);
- if (!llp->chrp) { /* first time */
- llp->x = llp->unit.land.lnd_x;
- llp->y = llp->unit.land.lnd_y;
- llp->chrp = (struct empobj_chr *)&lchr[(int)llp->unit.land.lnd_type];
- } else { /* not first time */
- if (lp->lnd_effic < LAND_MINEFF) {
- sprintf(buf, "was destroyed and is no longer a part of the %s",
+ if (lp->lnd_effic < LAND_MINEFF) {
+ sprintf(buf, "was destroyed and is no longer a part of the %s",
+ att_mode[combat_mode]);
+ lnd_delete(llp, buf);
+ return 0;
+ }
+ if (victim_land) {
+ if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
+ lnd_delete(llp,
+ "left to go fight another battle and is no longer a part of the defense");
+ return 0;
+ }
+ } else {
+ if (lp->lnd_own != player->cnum) {
+ sprintf(buf,
+ "was destroyed and is no longer a part of the %s",
att_mode[combat_mode]);
lnd_delete(llp, buf);
return 0;
}
- if (victim_land) {
- if (lp->lnd_x != def->x || lp->lnd_y != def->y) {
- lnd_delete(llp,
- "left to go fight another battle and is no longer a part of the defense");
- return 0;
- }
- } else {
- if (lp->lnd_own != player->cnum) {
- sprintf(buf,
- "was destroyed and is no longer a part of the %s",
- att_mode[combat_mode]);
- lnd_delete(llp, buf);
- return 0;
- }
- if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
- sprintf(buf,
- "left to fight another battle and is no longer a part of the %s",
- att_mode[combat_mode]);
- lnd_delete(llp, buf);
- return 0;
- }
- if (lp->lnd_effic < llp->eff) {
- sprintf(buf, "damaged from %d%% to %d%%",
- llp->eff, lp->lnd_effic);
- lnd_print(llp, buf);
- }
+ if (lp->lnd_x != llp->x || lp->lnd_y != llp->y) {
+ sprintf(buf,
+ "left to fight another battle and is no longer a part of the %s",
+ att_mode[combat_mode]);
+ lnd_delete(llp, buf);
+ return 0;
+ }
+ if (lp->lnd_effic < llp->eff) {
+ sprintf(buf, "damaged from %d%% to %d%%",
+ llp->eff, lp->lnd_effic);
+ lnd_print(llp, buf);
}
}
llp->eff = llp->unit.land.lnd_effic;
emp_remque((struct emp_qelem *)llp);
free(llp);
} else
- get_land(A_ATTACK, 0, llp->unit.land.lnd_uid, llp, 0);
+ get_land(A_ATTACK, NULL, llp, 0);
}
}
double new_land = 0;
double mobcost;
double pathcost;
- int dist;
- int radius;
int origx, origy;
double eff = att_combat_eff(def);
char buf[1024];
while (nxtitem(&ni, &land) && dtotal + new_land * eff < 1.2 * ototal) {
if (!land.lnd_own)
continue;
- if (!land.lnd_rad_max)
+ if (land.lnd_mission != MI_RESERVE)
continue;
if ((land.lnd_x == def->x) && (land.lnd_y == def->y))
continue;
continue;
/* Only supplied units can react */
- if (!has_supply(&land))
+ if (list ? !lnd_supply_all(&land) : !lnd_could_be_supplied(&land))
continue;
- dist = mapdist(land.lnd_x, land.lnd_y, def->x, def->y);
-
- getsect(land.lnd_x, land.lnd_y, §);
- /* Units on efficient headquarters can react 1 farther */
- if ((sect.sct_type == SCT_HEADQ) && (sect.sct_effic >= 60))
- radius = land.lnd_rad_max + 1;
- else
- radius = land.lnd_rad_max;
-
- if (land.lnd_mission == MI_RESERVE)
- radius += 2;
-
- if (dist > radius)
+ if (!in_oparea((struct empobj *)&land, def->x, def->y))
continue;
+ getsect(land.lnd_x, land.lnd_y, §);
getsect(def->x, def->y, &dsect);
if (!BestLandPath(buf, §, &dsect, &pathcost,
lnd_mobtype(&land)))
llp->y = origy;
llp->chrp = (struct empobj_chr *)&lchr[(int)land.lnd_type];
llp->unit.land = land;
+ llp->eff = land.lnd_effic;
emp_insque(&llp->queue, list);
if (lnd_spyval(&land) > *d_spyp)
*d_spyp = lnd_spyval(&land);
getsect(def->x, def->y, §);
if (sect.sct_oldown != player->cnum) {
- mines = MIN(sect.sct_mines, 20);
+ mines = SCT_LANDMINES(§);
+ mines = MIN(mines, 20);
if (a_engineer)
mines = ldround(mines / 2.0, 1);
if (mines > 0) {
att_infect_units(dlist, def->plague);
/*
- * Fighting is slightly random. There is always that last little
+ * Fighting is slightly random. There is always that last little
* effort you see people put in. Or the stray bullet that takes out
* an officer and the rest go into chaos. Things like that.
* Thus, we have added a very slight random factor that will sometimes
send_reacting_units_home(dlist);
/* putland the defending land */
- lnd_put(dlist, 0);
+ unit_put(dlist, 0);
/* putland the attacking land */
put_land(olist);
{
int n;
int occuppied = 0;
- struct ulist *llp, *delete_me = 0;
+ struct ulist *llp, *delete_me = NULL;
char buf[1024];
struct sctstr sect;
struct shpstr ship;
putsect(§);
} else if (def->type == EF_SHIP) {
getship(def->shp_uid, &ship);
- takeover_ship(&ship, player->cnum, 1);
+ takeover_ship(&ship, player->cnum);
putship(ship.shp_uid, &ship);
} else if (def->type == EF_LAND) {
getland(def->lnd_uid, &land);
- takeover_land(&land, player->cnum, 1);
+ takeover_land(&land, player->cnum);
putland(land.lnd_uid, &land);
}
if (delete_me)
*answerp = 'N';
if (*answerp == 'Y')
continue;
+ if (!get_land(A_ATTACK, def, llp, 0))
+ continue;
if (*answerp != 'N') {
- if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
- continue;
sprintf(prompt, "Move in with %s (%c %d%%) [ynYNq?] ",
prland(&llp->unit.land),
- llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
+ llp->unit.land.lnd_army ? llp->unit.land.lnd_army : '~',
llp->unit.land.lnd_effic);
*answerp = att_prompt(prompt, llp->unit.land.lnd_army);
if (player->aborted || att_get_combat(def, 0) < 0)
*answerp = 'N';
- if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
+ if (!get_land(A_ATTACK, def, llp, 0))
continue;
}
if (*answerp == 'y' || *answerp == 'Y')
for (qp = olist->q_forw; qp != olist; qp = next) {
next = qp->q_forw;
llp = (struct ulist *)qp;
- if (!get_land(A_ATTACK, def, llp->unit.land.lnd_uid, llp, 0))
+ if (!get_land(A_ATTACK, def, llp, 0))
continue;
sprintf(buf, "stays in %s",
xyas(llp->unit.land.lnd_x, llp->unit.land.lnd_y,
for (qp = olist->q_forw; qp != olist; qp = next) {
next = qp->q_forw;
llp = (struct ulist *)qp;
- if (!get_land(combat_mode, def, llp->unit.land.lnd_uid, llp, 0))
+ if (!get_land(combat_mode, def, llp, 0))
continue;
take_move_in_mob(combat_mode, llp, off, def);
llp->unit.land.lnd_x = def->x;
}
if (QEMPTY(olist))
return;
- lnd_put(olist, 0);
+ unit_put(olist, 0);
}
/*
return;
sprintf(prompt, "How many mil to move in from %s (%d max)? ",
xyas(off->x, off->y, player->cnum), mob_support);
- if (!(p = getstring(prompt, buf)) || !*p || (num_mil = atoi(p)) <= 0)
+ p = getstring(prompt, buf);
+ if (!p || !*p || (num_mil = atoi(p)) <= 0)
return;
/* Make sure we don't move in more than we can support mobility-wise */
if (num_mil > mob_support)
take_move_in_mob(int combat_mode, struct ulist *llp, struct combat *off,
struct combat *def)
{
- int mobcost;
- int new;
+ double mob = llp->unit.land.lnd_mobil;
+ double gain = etu_per_update * land_mob_scale;
+ double mobcost, moblim;
switch (combat_mode) {
case A_ATTACK:
mobcost = lnd_pathcost(&llp->unit.land,
att_mobcost(off->own, def,
lnd_mobtype(&llp->unit.land)));
- new = llp->unit.land.lnd_mobil - mobcost;
- if (new < -127)
- new = -127;
- llp->unit.land.lnd_mobil = new;
break;
case A_ASSAULT:
- if (off->shp_mcp->m_flags & M_LAND) {
- if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
- llp->unit.land.lnd_mobil -=
- (float)etu_per_update * land_mob_scale * 0.5;
- else
- llp->unit.land.lnd_mobil -= (float)etu_per_update *
- land_mob_scale;
+ /*
+ * Set mobcost to basic assault cost, moblim to maximum
+ * mobility to keep when assaulting from non-landing ship
+ */
+ if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE) {
+ mobcost = gain / 2.0;
+ moblim = 0;
} else {
- if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
- llp->unit.land.lnd_mobil = 0;
- else
- llp->unit.land.lnd_mobil = -(float)etu_per_update *
- land_mob_scale;
+ mobcost = gain;
+ moblim = -gain;
}
+ if (!(off->shp_mcp->m_flags & M_LAND))
+ /* Not a landing ship, ensure we go to or below moblim */
+ mobcost = MAX(mobcost, mob - moblim);
break;
case A_BOARD:
- /* I arbitrarily chose the numbers 10 and 40 below -KHS */
if (((struct lchrstr *)llp->chrp)->l_flags & L_MARINE)
- llp->unit.land.lnd_mobil -= 10;
+ mobcost = 10;
else
- llp->unit.land.lnd_mobil -= 40;
+ mobcost = 40;
break;
+ default:
+ CANT_REACH();
+ mobcost = 0;
}
+
+ mob -= mobcost;
+ if (mob < -127.0)
+ mob = -127.0;
+ llp->unit.land.lnd_mobil = (signed char)mob;
llp->unit.land.lnd_harden = 0;
}
/*
* sector_strength - Everyone starts at 1. You can get up to a max
* of d_dstr, depending on how much you build up the
- * defenses of the sector.
+ * defenses of the sector.
*/
double