From f3651f17e5d539d20c1433ecab23c754bfaeef54 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sat, 13 Sep 2008 18:35:50 -0400 Subject: [PATCH] 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. --- include/prototypes.h | 1 - include/unit.h | 1 + src/lib/commands/scra.c | 119 ++++++-------------------------------- src/lib/commands/scut.c | 103 ++++----------------------------- src/lib/subs/unitsub.c | 41 +++++++++++++ src/lib/update/nav_ship.c | 5 +- 6 files changed, 74 insertions(+), 196 deletions(-) diff --git a/include/prototypes.h b/include/prototypes.h index f7c79bee..8efbcf1e 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -66,7 +66,6 @@ extern int do_unit_move(struct emp_qelem *land_list, int *together, double *minmob, double *maxmob); extern int count_pop(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, int old, int new); extern int line_of_sight(char **rad, int ax, int ay, int bx, int by); diff --git a/include/unit.h b/include/unit.h index 6f0a11e9..8e2f51d6 100644 --- a/include/unit.h +++ b/include/unit.h @@ -57,3 +57,4 @@ extern void unit_put(struct emp_qelem *list, natid actor); extern char *unit_path(int, struct empobj *, char *); extern void unit_view(struct emp_qelem *); extern void unit_update_cargo(struct empobj *); +extern void unit_drop_cargo(struct empobj *, natid); diff --git a/src/lib/commands/scra.c b/src/lib/commands/scra.c index e3ce6be3..c7beacfa 100644 --- a/src/lib/commands/scra.c +++ b/src/lib/commands/scra.c @@ -37,6 +37,7 @@ #include "commands.h" #include "empobj.h" #include "optlist.h" +#include "unit.h" int scra(void) @@ -50,10 +51,6 @@ scra(void) struct lchrstr *lp; char *p; i_type i; - struct nstr_item ni2; - struct plnstr plane; - struct lndstr land; - struct sctstr sect2; char prompt[128]; char buf[1024]; float eff; @@ -147,6 +144,17 @@ scra(void) prship(&item.ship)); 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 { if (!player->owner && 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) { eff = item.ship.shp_effic / 100.0; 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++) { sect.sct_item[i] += item.ship.shp_item[i]; } sect.sct_item[I_LCM] += mp->m_lcm * 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) { eff = item.land.lnd_effic / 100.0; 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_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 { eff = item.land.lnd_effic / 100.0; pp = &plchr[(int)item.plane.pln_type]; 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_MILIT] += pp->pl_crew; - item.plane.pln_effic = 0; - putplane(item.plane.pln_uid, &item.plane); } - pr("%s scrapped in %s\n", - obj_nameof(&item.gen), - xyas(item.gen.x, item.gen.y, player->cnum)); + item.gen.effic = 0; + put_empobj(type, item.gen.uid, &item.gen); for (i = I_NONE + 1; i <= I_MAX; i++) { if (sect.sct_item[i] > ITEM_MAX) sect.sct_item[i] = ITEM_MAX; diff --git a/src/lib/commands/scut.c b/src/lib/commands/scut.c index f5adb17a..1de0369e 100644 --- a/src/lib/commands/scut.c +++ b/src/lib/commands/scut.c @@ -37,8 +37,7 @@ #include "commands.h" #include "empobj.h" #include "optlist.h" - -static void scuttle_land(struct lndstr *); +#include "unit.h" int scut(void) @@ -50,6 +49,7 @@ scut(void) char *p; char prompt[128]; char buf[1024]; + struct sctstr sect; if (!(p = getstarg(player->argp[1], "Ship, land, or plane? ", buf))) return RET_SYN; @@ -126,26 +126,25 @@ scut(void) if (type == EF_SHIP) { mp = &mchr[(int)item.ship.shp_type]; - if (opt_TRADESHIPS) { - if (mp->m_flags & M_TRADE) - if (!scuttle_tradeship(&item.ship, 1)) - continue; + if (opt_TRADESHIPS && (mp->m_flags & M_TRADE)) { + if (!scuttle_tradeship(&item.ship, 1)) + continue; } - scuttle_ship(&item.ship); } else if (type == EF_LAND) { if (item.land.lnd_ship >= 0) { pr("%s is on a ship, and cannot be scuttled!\n", prland(&item.land)); continue; } - scuttle_land(&item.land); - } else { - item.plane.pln_effic = 0; - putplane(item.plane.pln_uid, &item.plane); } pr("%s scuttled in %s\n", obj_nameof(&item.gen), 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; @@ -225,85 +224,3 @@ scuttle_tradeship(struct shpstr *sp, int interactive) 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); -} diff --git a/src/lib/subs/unitsub.c b/src/lib/subs/unitsub.c index 25a01c2e..272b4270 100644 --- a/src/lib/subs/unitsub.c +++ b/src/lib/subs/unitsub.c @@ -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); + } + } +} diff --git a/src/lib/update/nav_ship.c b/src/lib/update/nav_ship.c index f49c2643..f34b5670 100644 --- a/src/lib/update/nav_ship.c +++ b/src/lib/update/nav_ship.c @@ -66,7 +66,10 @@ scuttle_it(struct shpstr *sp) } wu(0, sp->shp_own, "Scuttling %s in sector %s\n", 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