Refactor and fix scuttle and scrap code
Factor unit_drop_cargo() out of scra(), scuttle_ship(), scuttle_land(), fix it up: * Some messages were sent as bulletins instead of printing them. * Nukes were always destroyed. They're now treated exactly like other cargo. * scuttle destroyed some cargo silently, and listed other cargo as "scuttled". It now simply lets unit_update_cargo() running from carrier prewrite callbacks list all cargo "lost". Simplify its callers. scuttle_ship() and scuttle_land() are now trivial, inline and remove.
This commit is contained in:
parent
3318e4e4e5
commit
f3651f17e5
6 changed files with 74 additions and 196 deletions
|
@ -66,7 +66,6 @@ extern int do_unit_move(struct emp_qelem *land_list, int *together,
|
||||||
double *minmob, double *maxmob);
|
double *minmob, double *maxmob);
|
||||||
extern int count_pop(int);
|
extern int count_pop(int);
|
||||||
extern int scuttle_tradeship(struct shpstr *, int);
|
extern int scuttle_tradeship(struct shpstr *, int);
|
||||||
extern void scuttle_ship(struct shpstr *);
|
|
||||||
extern void resnoise(struct sctstr *sptr, int public_amt, char *name,
|
extern void resnoise(struct sctstr *sptr, int public_amt, char *name,
|
||||||
int old, int new);
|
int old, int new);
|
||||||
extern int line_of_sight(char **rad, int ax, int ay, int bx, int by);
|
extern int line_of_sight(char **rad, int ax, int ay, int bx, int by);
|
||||||
|
|
|
@ -57,3 +57,4 @@ extern void unit_put(struct emp_qelem *list, natid actor);
|
||||||
extern char *unit_path(int, struct empobj *, char *);
|
extern char *unit_path(int, struct empobj *, char *);
|
||||||
extern void unit_view(struct emp_qelem *);
|
extern void unit_view(struct emp_qelem *);
|
||||||
extern void unit_update_cargo(struct empobj *);
|
extern void unit_update_cargo(struct empobj *);
|
||||||
|
extern void unit_drop_cargo(struct empobj *, natid);
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "empobj.h"
|
#include "empobj.h"
|
||||||
#include "optlist.h"
|
#include "optlist.h"
|
||||||
|
#include "unit.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
scra(void)
|
scra(void)
|
||||||
|
@ -50,10 +51,6 @@ scra(void)
|
||||||
struct lchrstr *lp;
|
struct lchrstr *lp;
|
||||||
char *p;
|
char *p;
|
||||||
i_type i;
|
i_type i;
|
||||||
struct nstr_item ni2;
|
|
||||||
struct plnstr plane;
|
|
||||||
struct lndstr land;
|
|
||||||
struct sctstr sect2;
|
|
||||||
char prompt[128];
|
char prompt[128];
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
float eff;
|
float eff;
|
||||||
|
@ -147,6 +144,17 @@ scra(void)
|
||||||
prship(&item.ship));
|
prship(&item.ship));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (opt_TRADESHIPS
|
||||||
|
&& (mchr[item.ship.shp_type].m_flags & M_TRADE)) {
|
||||||
|
pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
|
||||||
|
sprintf(prompt,
|
||||||
|
"Are you really sure that you want to scrap %s (n)? ",
|
||||||
|
prship(&item.ship));
|
||||||
|
if (!confirm(prompt)) {
|
||||||
|
pr("%s not scrapped\n", prship(&item.ship));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!player->owner
|
if (!player->owner
|
||||||
&& getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
|
&& getrel(getnatp(sect.sct_own), player->cnum) != ALLIED) {
|
||||||
|
@ -162,65 +170,18 @@ scra(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pr("%s scrapped in %s\n",
|
||||||
|
obj_nameof(&item.gen),
|
||||||
|
xyas(item.gen.x, item.gen.y, player->cnum));
|
||||||
|
unit_drop_cargo(&item.gen, sect.sct_own);
|
||||||
if (type == EF_SHIP) {
|
if (type == EF_SHIP) {
|
||||||
eff = item.ship.shp_effic / 100.0;
|
eff = item.ship.shp_effic / 100.0;
|
||||||
mp = &mchr[(int)item.ship.shp_type];
|
mp = &mchr[(int)item.ship.shp_type];
|
||||||
if (opt_TRADESHIPS) {
|
|
||||||
if (mp->m_flags & M_TRADE) {
|
|
||||||
pr("WARNING: You only collect money from trade ships if you \"scuttle\" them!\n");
|
|
||||||
sprintf(prompt,
|
|
||||||
"Are you really sure that you want to scrap %s (n)? ",
|
|
||||||
prship(&item.ship));
|
|
||||||
if (!confirm(prompt)) {
|
|
||||||
pr("%s not scrapped\n", prship(&item.ship));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
||||||
sect.sct_item[i] += item.ship.shp_item[i];
|
sect.sct_item[i] += item.ship.shp_item[i];
|
||||||
}
|
}
|
||||||
sect.sct_item[I_LCM] += mp->m_lcm * 2 / 3 * eff;
|
sect.sct_item[I_LCM] += mp->m_lcm * 2 / 3 * eff;
|
||||||
sect.sct_item[I_HCM] += mp->m_hcm * 2 / 3 * eff;
|
sect.sct_item[I_HCM] += mp->m_hcm * 2 / 3 * eff;
|
||||||
getsect(item.ship.shp_x, item.ship.shp_y, §2);
|
|
||||||
snxtitem_cargo(&ni2, EF_PLANE, EF_SHIP, item.ship.shp_uid);
|
|
||||||
while (nxtitem(&ni2, &plane)) {
|
|
||||||
wu(0, plane.pln_own,
|
|
||||||
"Plane %d transferred off ship %d to %s\n",
|
|
||||||
ni2.cur, item.ship.shp_uid,
|
|
||||||
xyas(plane.pln_x, plane.pln_y, player->cnum));
|
|
||||||
plane.pln_ship = -1;
|
|
||||||
if (sect2.sct_own != plane.pln_own) {
|
|
||||||
wu(0, plane.pln_own,
|
|
||||||
"%s given to %s\n", prplane(&plane),
|
|
||||||
cname(sect2.sct_own));
|
|
||||||
wu(0, sect2.sct_own,
|
|
||||||
"%s given to you by %s\n", prplane(&plane),
|
|
||||||
cname(player->cnum));
|
|
||||||
}
|
|
||||||
plane.pln_own = sect2.sct_own;
|
|
||||||
putplane(plane.pln_uid, &plane);
|
|
||||||
}
|
|
||||||
snxtitem_cargo(&ni2, EF_LAND, EF_SHIP, item.ship.shp_uid);
|
|
||||||
while (nxtitem(&ni2, &land)) {
|
|
||||||
wu(0, land.lnd_own,
|
|
||||||
"Land unit %d transferred off ship %d to %s\n",
|
|
||||||
ni2.cur, item.ship.shp_uid,
|
|
||||||
xyas(land.lnd_x, land.lnd_y, player->cnum));
|
|
||||||
land.lnd_ship = -1;
|
|
||||||
if (sect2.sct_own != land.lnd_own) {
|
|
||||||
wu(0, land.lnd_own,
|
|
||||||
"%s given to %s\n", prland(&land),
|
|
||||||
cname(sect2.sct_own));
|
|
||||||
wu(0, sect2.sct_own,
|
|
||||||
"%s given to you by %s\n", prland(&land),
|
|
||||||
cname(player->cnum));
|
|
||||||
}
|
|
||||||
land.lnd_own = sect2.sct_own;
|
|
||||||
putland(land.lnd_uid, &land);
|
|
||||||
}
|
|
||||||
item.ship.shp_effic = 0;
|
|
||||||
putship(item.ship.shp_uid, &item.ship);
|
|
||||||
} else if (type == EF_LAND) {
|
} else if (type == EF_LAND) {
|
||||||
eff = item.land.lnd_effic / 100.0;
|
eff = item.land.lnd_effic / 100.0;
|
||||||
lp = &lchr[(int)item.land.lnd_type];
|
lp = &lchr[(int)item.land.lnd_type];
|
||||||
|
@ -229,59 +190,15 @@ scra(void)
|
||||||
}
|
}
|
||||||
sect.sct_item[I_LCM] += lp->l_lcm * 2 / 3 * eff;
|
sect.sct_item[I_LCM] += lp->l_lcm * 2 / 3 * eff;
|
||||||
sect.sct_item[I_HCM] += lp->l_hcm * 2 / 3 * eff;
|
sect.sct_item[I_HCM] += lp->l_hcm * 2 / 3 * eff;
|
||||||
getsect(item.land.lnd_x, item.land.lnd_y, §2);
|
|
||||||
|
|
||||||
snxtitem_cargo(&ni2, EF_LAND, EF_LAND, item.land.lnd_uid);
|
|
||||||
while (nxtitem(&ni2, &land)) {
|
|
||||||
wu(0, land.lnd_own,
|
|
||||||
"Land unit %d transferred off land unit %d to %s\n",
|
|
||||||
land.lnd_uid, item.land.lnd_uid,
|
|
||||||
xyas(land.lnd_x, land.lnd_y, player->cnum));
|
|
||||||
land.lnd_land = -1;
|
|
||||||
if (sect2.sct_own != land.lnd_own) {
|
|
||||||
wu(0, land.lnd_own,
|
|
||||||
"%s given to %s\n", prland(&land),
|
|
||||||
cname(sect2.sct_own));
|
|
||||||
wu(0, sect2.sct_own,
|
|
||||||
"%s given to you by %s\n", prland(&land),
|
|
||||||
cname(player->cnum));
|
|
||||||
}
|
|
||||||
land.lnd_own = sect2.sct_own;
|
|
||||||
putland(land.lnd_uid, &land);
|
|
||||||
}
|
|
||||||
|
|
||||||
snxtitem_cargo(&ni2, EF_PLANE, EF_LAND, item.land.lnd_uid);
|
|
||||||
while (nxtitem(&ni2, &plane)) {
|
|
||||||
wu(0, plane.pln_own,
|
|
||||||
"Plane %d transferred off land unit %d to %s\n",
|
|
||||||
ni2.cur, item.land.lnd_uid,
|
|
||||||
xyas(plane.pln_x, plane.pln_y, player->cnum));
|
|
||||||
plane.pln_land = -1;
|
|
||||||
if (sect2.sct_own != plane.pln_own) {
|
|
||||||
wu(0, plane.pln_own,
|
|
||||||
"%s given to %s\n", prplane(&plane),
|
|
||||||
cname(sect2.sct_own));
|
|
||||||
wu(0, sect2.sct_own,
|
|
||||||
"%s given to you by %s\n", prplane(&plane),
|
|
||||||
cname(player->cnum));
|
|
||||||
}
|
|
||||||
plane.pln_own = sect2.sct_own;
|
|
||||||
putplane(plane.pln_uid, &plane);
|
|
||||||
}
|
|
||||||
item.land.lnd_effic = 0;
|
|
||||||
putland(item.land.lnd_uid, &item.land);
|
|
||||||
} else {
|
} else {
|
||||||
eff = item.land.lnd_effic / 100.0;
|
eff = item.land.lnd_effic / 100.0;
|
||||||
pp = &plchr[(int)item.plane.pln_type];
|
pp = &plchr[(int)item.plane.pln_type];
|
||||||
sect.sct_item[I_LCM] += pp->pl_lcm * 2 / 3 * eff;
|
sect.sct_item[I_LCM] += pp->pl_lcm * 2 / 3 * eff;
|
||||||
sect.sct_item[I_HCM] += pp->pl_hcm * 2 / 3 * eff;
|
sect.sct_item[I_HCM] += pp->pl_hcm * 2 / 3 * eff;
|
||||||
sect.sct_item[I_MILIT] += pp->pl_crew;
|
sect.sct_item[I_MILIT] += pp->pl_crew;
|
||||||
item.plane.pln_effic = 0;
|
|
||||||
putplane(item.plane.pln_uid, &item.plane);
|
|
||||||
}
|
}
|
||||||
pr("%s scrapped in %s\n",
|
item.gen.effic = 0;
|
||||||
obj_nameof(&item.gen),
|
put_empobj(type, item.gen.uid, &item.gen);
|
||||||
xyas(item.gen.x, item.gen.y, player->cnum));
|
|
||||||
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
||||||
if (sect.sct_item[i] > ITEM_MAX)
|
if (sect.sct_item[i] > ITEM_MAX)
|
||||||
sect.sct_item[i] = ITEM_MAX;
|
sect.sct_item[i] = ITEM_MAX;
|
||||||
|
|
|
@ -37,8 +37,7 @@
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
#include "empobj.h"
|
#include "empobj.h"
|
||||||
#include "optlist.h"
|
#include "optlist.h"
|
||||||
|
#include "unit.h"
|
||||||
static void scuttle_land(struct lndstr *);
|
|
||||||
|
|
||||||
int
|
int
|
||||||
scut(void)
|
scut(void)
|
||||||
|
@ -50,6 +49,7 @@ scut(void)
|
||||||
char *p;
|
char *p;
|
||||||
char prompt[128];
|
char prompt[128];
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
|
struct sctstr sect;
|
||||||
|
|
||||||
if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
|
if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf)))
|
||||||
return RET_SYN;
|
return RET_SYN;
|
||||||
|
@ -126,26 +126,25 @@ scut(void)
|
||||||
|
|
||||||
if (type == EF_SHIP) {
|
if (type == EF_SHIP) {
|
||||||
mp = &mchr[(int)item.ship.shp_type];
|
mp = &mchr[(int)item.ship.shp_type];
|
||||||
if (opt_TRADESHIPS) {
|
if (opt_TRADESHIPS && (mp->m_flags & M_TRADE)) {
|
||||||
if (mp->m_flags & M_TRADE)
|
|
||||||
if (!scuttle_tradeship(&item.ship, 1))
|
if (!scuttle_tradeship(&item.ship, 1))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
scuttle_ship(&item.ship);
|
|
||||||
} else if (type == EF_LAND) {
|
} else if (type == EF_LAND) {
|
||||||
if (item.land.lnd_ship >= 0) {
|
if (item.land.lnd_ship >= 0) {
|
||||||
pr("%s is on a ship, and cannot be scuttled!\n",
|
pr("%s is on a ship, and cannot be scuttled!\n",
|
||||||
prland(&item.land));
|
prland(&item.land));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
scuttle_land(&item.land);
|
|
||||||
} else {
|
|
||||||
item.plane.pln_effic = 0;
|
|
||||||
putplane(item.plane.pln_uid, &item.plane);
|
|
||||||
}
|
}
|
||||||
pr("%s scuttled in %s\n",
|
pr("%s scuttled in %s\n",
|
||||||
obj_nameof(&item.gen),
|
obj_nameof(&item.gen),
|
||||||
xyas(item.gen.x, item.gen.y, player->cnum));
|
xyas(item.gen.x, item.gen.y, player->cnum));
|
||||||
|
getsect(item.gen.x, item.gen.y, §);
|
||||||
|
if (sect.sct_own == item.gen.own)
|
||||||
|
unit_drop_cargo(&item.gen, sect.sct_own);
|
||||||
|
item.gen.effic = 0;
|
||||||
|
put_empobj(type, item.gen.uid, &item.gen);
|
||||||
}
|
}
|
||||||
|
|
||||||
return RET_OK;
|
return RET_OK;
|
||||||
|
@ -225,85 +224,3 @@ scuttle_tradeship(struct shpstr *sp, int interactive)
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
scuttle_ship(struct shpstr *sp)
|
|
||||||
{
|
|
||||||
struct nstr_item ni;
|
|
||||||
struct sctstr sect;
|
|
||||||
struct plnstr plane;
|
|
||||||
struct lndstr land;
|
|
||||||
|
|
||||||
getsect(sp->shp_x, sp->shp_y, §);
|
|
||||||
snxtitem_cargo(&ni, EF_PLANE, EF_SHIP, sp->shp_uid);
|
|
||||||
while (nxtitem(&ni, &plane)) {
|
|
||||||
plane.pln_ship = -1;
|
|
||||||
if (sect.sct_own != sp->shp_own) {
|
|
||||||
wu(0, plane.pln_own, "Plane %d scuttled in %s\n",
|
|
||||||
plane.pln_uid,
|
|
||||||
xyas(plane.pln_x, plane.pln_y, plane.pln_own));
|
|
||||||
plane.pln_effic = 0;
|
|
||||||
} else {
|
|
||||||
wu(0, plane.pln_own,
|
|
||||||
"Plane %d transferred off ship %d to %s\n",
|
|
||||||
plane.pln_uid, sp->shp_uid,
|
|
||||||
xyas(plane.pln_x, plane.pln_y, plane.pln_own));
|
|
||||||
}
|
|
||||||
putplane(plane.pln_uid, &plane);
|
|
||||||
}
|
|
||||||
snxtitem_cargo(&ni, EF_LAND, EF_SHIP, sp->shp_uid);
|
|
||||||
while (nxtitem(&ni, &land)) {
|
|
||||||
land.lnd_ship = -1;
|
|
||||||
if (sect.sct_own == sp->shp_own) {
|
|
||||||
wu(0, land.lnd_own,
|
|
||||||
"Land unit %d transferred off ship %d to %s\n",
|
|
||||||
land.lnd_uid, sp->shp_uid,
|
|
||||||
xyas(land.lnd_x, land.lnd_y, land.lnd_own));
|
|
||||||
putland(land.lnd_uid, &land);
|
|
||||||
} else
|
|
||||||
scuttle_land(&land);
|
|
||||||
}
|
|
||||||
sp->shp_effic = 0;
|
|
||||||
putship(sp->shp_uid, sp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
scuttle_land(struct lndstr *lp)
|
|
||||||
{
|
|
||||||
struct nstr_item ni;
|
|
||||||
struct sctstr sect;
|
|
||||||
struct plnstr plane;
|
|
||||||
struct lndstr land;
|
|
||||||
|
|
||||||
getsect(lp->lnd_x, lp->lnd_y, §);
|
|
||||||
snxtitem_cargo(&ni, EF_PLANE, EF_LAND, lp->lnd_uid);
|
|
||||||
while (nxtitem(&ni, &plane)) {
|
|
||||||
plane.pln_land = -1;
|
|
||||||
if (sect.sct_own != lp->lnd_own) {
|
|
||||||
wu(0, plane.pln_own, "Plane %d scuttled in %s\n",
|
|
||||||
plane.pln_uid,
|
|
||||||
xyas(plane.pln_x, plane.pln_y, plane.pln_own));
|
|
||||||
plane.pln_effic = 0;
|
|
||||||
} else {
|
|
||||||
wu(0, plane.pln_own,
|
|
||||||
"Plane %d transferred off unit %d to %s\n",
|
|
||||||
plane.pln_uid, lp->lnd_uid,
|
|
||||||
xyas(plane.pln_x, plane.pln_y, plane.pln_own));
|
|
||||||
}
|
|
||||||
putplane(plane.pln_uid, &plane);
|
|
||||||
}
|
|
||||||
snxtitem_cargo(&ni, EF_LAND, EF_LAND, lp->lnd_uid);
|
|
||||||
while (nxtitem(&ni, &land)) {
|
|
||||||
land.lnd_land = -1;
|
|
||||||
if (sect.sct_own == lp->lnd_own) {
|
|
||||||
wu(0, land.lnd_own,
|
|
||||||
"Land unit %d transferred off unit %d to %s\n",
|
|
||||||
land.lnd_uid, lp->lnd_uid,
|
|
||||||
xyas(land.lnd_x, land.lnd_y, land.lnd_own));
|
|
||||||
putland(land.lnd_uid, &land);
|
|
||||||
} else
|
|
||||||
scuttle_land(&land);
|
|
||||||
}
|
|
||||||
lp->lnd_effic = 0;
|
|
||||||
putland(lp->lnd_uid, lp);
|
|
||||||
}
|
|
||||||
|
|
|
@ -238,3 +238,44 @@ unit_update_cargo(struct empobj *carrier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Drop cargo of UNIT.
|
||||||
|
* Give it to NEWOWN, unless it's zero.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
unit_drop_cargo(struct empobj *unit, natid newown)
|
||||||
|
{
|
||||||
|
int type;
|
||||||
|
struct nstr_item ni;
|
||||||
|
union empobj_storage cargo;
|
||||||
|
|
||||||
|
for (type = EF_PLANE; type <= EF_NUKE; type++) {
|
||||||
|
snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
|
||||||
|
while (nxtitem(&ni, &cargo)) {
|
||||||
|
switch (type) {
|
||||||
|
case EF_PLANE:
|
||||||
|
cargo.plane.pln_ship = cargo.plane.pln_land = -1;
|
||||||
|
break;
|
||||||
|
case EF_LAND:
|
||||||
|
cargo.land.lnd_ship = cargo.land.lnd_land = -1;
|
||||||
|
break;
|
||||||
|
case EF_NUKE:
|
||||||
|
cargo.nuke.nuk_plane = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
|
||||||
|
obj_nameof(&cargo.gen),
|
||||||
|
ef_nameof(unit->ef_type), unit->uid,
|
||||||
|
xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
|
||||||
|
if (newown) {
|
||||||
|
mpr(cargo.gen.own, "%s given to %s\n",
|
||||||
|
obj_nameof(&cargo.gen), cname(newown));
|
||||||
|
mpr(newown, "%s given to you by %s\n",
|
||||||
|
obj_nameof(&cargo.gen), cname(cargo.gen.own));
|
||||||
|
cargo.gen.own = newown;
|
||||||
|
}
|
||||||
|
put_empobj(type, cargo.gen.uid, &cargo.gen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -66,7 +66,10 @@ scuttle_it(struct shpstr *sp)
|
||||||
}
|
}
|
||||||
wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
|
wu(0, sp->shp_own, "Scuttling %s in sector %s\n",
|
||||||
prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
|
prship(sp), xyas(sp->shp_x, sp->shp_y, sp->shp_own));
|
||||||
scuttle_ship(sp);
|
if (sectp->sct_own == sp->shp_own)
|
||||||
|
unit_drop_cargo((struct empobj *)sp, sectp->sct_own);
|
||||||
|
sp->shp_effic = 0;
|
||||||
|
putship(sp->shp_uid, sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue