Rewrite the broken code to move cargo with its carrier

The old code did not move a carrier's cargo (planes, land units,
nukes) when the carrier moved.  Instead, it fixed up the location in
the postread callback.  Anything not going through ef_read(), in
particular the update, saw it in its old, incorrect location, until a
fixed up copy got written back.

Moreover, the timestamp did not change when cargo moved, so
incremental dumps did not pick up the movement.

The new code moves the cargo along with the carrier.

New unit_update_cargo() moves or destroys a carrier's cargo (planes,
land units, nukes) along with the carrier.  Call it from
shp_prewrite(), pln_prewrite() and lnd_prewrite() when the carrier
moves or gets destroyed.

Remove the code to destroy cargo from shp_prewrite(), pln_prewrite(),
lnd_prewrite().

Remove the code to fix up cargo location from pln_postread(),
lnd_postread(), nuk_postread().

This changes the message for ship and land unit cargo getting
destroyed from "sunk" and "MIA" to "lost".
This commit is contained in:
Markus Armbruster 2008-09-12 19:31:48 -04:00
parent e7f5b517a0
commit 3cf29456fe
6 changed files with 45 additions and 181 deletions

View file

@ -56,3 +56,4 @@ extern void unit_list(struct emp_qelem *);
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 *);

View file

@ -37,74 +37,24 @@
#include "land.h"
#include "lost.h"
#include "misc.h"
#include "nsc.h"
#include "optlist.h"
#include "plane.h"
#include "player.h"
#include "prototypes.h"
#include "ship.h"
#include "unit.h"
void
lnd_postread(int n, void *ptr)
{
struct lndstr *llp = ptr;
struct shpstr theship;
struct lndstr theland;
if (llp->lnd_uid != n) {
logerror("lnd_postread: Error - %d != %d, zeroing.\n",
llp->lnd_uid, n);
memset(llp, 0, sizeof(struct lndstr));
}
if (llp->lnd_ship >= 0 && llp->lnd_own
&& llp->lnd_effic >= LAND_MINEFF) {
if (getship(llp->lnd_ship, &theship)
&& (theship.shp_effic >= SHIP_MINEFF)) {
/* wooof! Carriers are a pain */
if (llp->lnd_mission) {
/*
* If the unit is on a mission centered
* on it's loc, the op-area travels with
* the unit.
*/
if ((llp->lnd_opx == llp->lnd_x) &&
(llp->lnd_opy == llp->lnd_y)) {
llp->lnd_opx = theship.shp_x;
llp->lnd_opy = theship.shp_y;
}
}
if (llp->lnd_x != theship.shp_x || llp->lnd_y != theship.shp_y)
time(&llp->lnd_timestamp);
llp->lnd_x = theship.shp_x;
llp->lnd_y = theship.shp_y;
}
}
if (llp->lnd_land >= 0 && llp->lnd_own
&& llp->lnd_effic >= LAND_MINEFF) {
if (getland(llp->lnd_land, &theland)
&& (theland.lnd_effic >= LAND_MINEFF)) {
/* wooof! Carriers are a pain */
if (llp->lnd_mission) {
/*
* If the unit is on a mission centered
* on it's loc, the op-area travels with
* the unit.
*/
if ((llp->lnd_opx == llp->lnd_x) &&
(llp->lnd_opy == llp->lnd_y)) {
llp->lnd_opx = theland.lnd_x;
llp->lnd_opy = theland.lnd_y;
}
}
if (llp->lnd_x != theland.lnd_x || llp->lnd_y != theland.lnd_y)
time(&llp->lnd_timestamp);
llp->lnd_x = theland.lnd_x;
llp->lnd_y = theland.lnd_y;
}
}
if (opt_MOB_ACCESS)
lnd_do_upd_mob(llp);
player->owner = (player->god || llp->lnd_own == player->cnum);
}
@ -114,38 +64,10 @@ lnd_prewrite(int n, void *old, void *new)
struct lndstr *oldlp = old;
struct lndstr *llp = new;
natid own = llp->lnd_own;
struct lndstr *lp;
struct plnstr *pp;
int i;
if (llp->lnd_own && llp->lnd_effic < LAND_MINEFF) {
own = 0;
llp->lnd_ship = llp->lnd_land = -1;
for (i = 0; NULL != (lp = getlandp(i)); i++) {
if (lp->lnd_own && lp->lnd_land == n) {
mpr(lp->lnd_own, "%s MIA!\n", prland(lp));
makelost(EF_LAND, lp->lnd_own, lp->lnd_uid,
lp->lnd_x, lp->lnd_y);
lp->lnd_own = 0;
lp->lnd_effic = 0;
lp->lnd_ship = -1;
lp->lnd_land = -1;
putland(lp->lnd_uid, lp);
}
}
for (i = 0; NULL != (pp = getplanep(i)); i++) {
if (pp->pln_own && pp->pln_land == n) {
mpr(pp->pln_own, "%s MIA!\n", prplane(pp));
makelost(EF_PLANE, pp->pln_own, pp->pln_uid,
pp->pln_x, pp->pln_y);
pp->pln_own = 0;
pp->pln_effic = 0;
pp->pln_ship = -1;
pp->pln_land = -1;
putplane(pp->pln_uid, pp);
}
}
} else {
item_prewrite(llp->lnd_item);
}
@ -163,6 +85,8 @@ lnd_prewrite(int n, void *old, void *new)
llp->lnd_uid, llp->lnd_x, llp->lnd_y);
llp->lnd_own = own;
if (!own || llp->lnd_x != oldlp->lnd_x || llp->lnd_y != oldlp->lnd_y)
unit_update_cargo((struct empobj *)llp);
}
void

View file

@ -38,10 +38,7 @@
#include "file.h"
#include "lost.h"
#include "misc.h"
#include "nat.h"
#include "nsc.h"
#include "nuke.h"
#include "plane.h"
#include "player.h"
#include "prototypes.h"
@ -49,25 +46,12 @@ void
nuk_postread(int n, void *ptr)
{
struct nukstr *np = ptr;
struct plnstr plane;
if (np->nuk_uid != n) {
logerror("nuk_postread: Error - %d != %d, zeroing.\n",
np->nuk_uid, n);
memset(np, 0, sizeof(struct nukstr));
}
if (np->nuk_plane >= 0 && np->nuk_own && np->nuk_effic >= 0) {
if (getplane(np->nuk_plane, &plane)
&& plane.pln_effic >= PLANE_MINEFF) {
if (np->nuk_x != plane.pln_x || np->nuk_y != plane.pln_y) {
time(&np->nuk_timestamp);
np->nuk_x = plane.pln_x;
np->nuk_y = plane.pln_y;
}
}
}
player->owner = (player->god || np->nuk_own == player->cnum);
}

View file

@ -35,73 +35,24 @@
#include <config.h>
#include "file.h"
#include "land.h"
#include "lost.h"
#include "misc.h"
#include "nuke.h"
#include "optlist.h"
#include "plane.h"
#include "player.h"
#include "prototypes.h"
#include "ship.h"
#include "unit.h"
void
pln_postread(int n, void *ptr)
{
struct plnstr *pp = ptr;
struct shpstr theship;
struct lndstr theland;
if (pp->pln_uid != n) {
logerror("pln_postread: Error - %d != %d, zeroing.\n",
pp->pln_uid, n);
memset(pp, 0, sizeof(struct plnstr));
}
if (pp->pln_ship >= 0 && pp->pln_own && pp->pln_effic >= PLANE_MINEFF) {
if (getship(pp->pln_ship, &theship) &&
(theship.shp_effic >= SHIP_MINEFF)) {
/* wooof! Carriers are a pain */
if (pp->pln_mission) {
/*
* If the plane is on a mission centered
* on it's loc, the op-area travels with
* the plane.
*/
if ((pp->pln_opx == pp->pln_x) &&
(pp->pln_opy == pp->pln_y)) {
pp->pln_opx = theship.shp_x;
pp->pln_opy = theship.shp_y;
}
}
if (pp->pln_x != theship.shp_x || pp->pln_y != theship.shp_y)
time(&pp->pln_timestamp);
pp->pln_x = theship.shp_x;
pp->pln_y = theship.shp_y;
}
}
if (pp->pln_land >= 0 && pp->pln_own && pp->pln_effic >= PLANE_MINEFF) {
if (getland(pp->pln_land, &theland) &&
(theland.lnd_effic >= LAND_MINEFF)) {
/* wooof! Units are a pain, too */
if (pp->pln_mission) {
/*
* If the plane is on a mission centered
* on it's loc, the op-area travels with
* the plane.
*/
if ((pp->pln_opx == pp->pln_x) &&
(pp->pln_opy == pp->pln_y)) {
pp->pln_opx = theland.lnd_x;
pp->pln_opy = theland.lnd_y;
}
}
if (pp->pln_x != theland.lnd_x || pp->pln_y != theland.lnd_y)
time(&pp->pln_timestamp);
pp->pln_x = theland.lnd_x;
pp->pln_y = theland.lnd_y;
}
}
player->owner = (player->god || pp->pln_own == player->cnum);
if (opt_MOB_ACCESS)
pln_do_upd_mob(pp);
@ -113,21 +64,11 @@ pln_prewrite(int n, void *old, void *new)
struct plnstr *oldpp = old;
struct plnstr *pp = new;
natid own = pp->pln_own;
struct nukstr *np;
int i;
if (pp->pln_effic < PLANE_MINEFF) {
own = 0;
pp->pln_effic = 0;
pp->pln_ship = pp->pln_land = -1;
for (i = 0; NULL != (np = getnukep(i)); i++) {
if (np->nuk_own && np->nuk_plane == n) {
mpr(np->nuk_own, "%s lost!\n", prnuke(np));
np->nuk_effic = 0;
np->nuk_plane = -1;
putnuke(np->nuk_uid, np);
}
}
}
if (CANT_HAPPEN(pp->pln_ship >= 0 && pp->pln_land >= 0))
@ -143,6 +84,8 @@ pln_prewrite(int n, void *old, void *new)
pp->pln_uid, pp->pln_x, pp->pln_y);
pp->pln_own = own;
if (!own || pp->pln_x != oldpp->pln_x || pp->pln_y != oldpp->pln_y)
unit_update_cargo((struct empobj *)pp);
}
void

View file

@ -35,15 +35,13 @@
#include <config.h>
#include "file.h"
#include "land.h"
#include "lost.h"
#include "misc.h"
#include "nsc.h"
#include "optlist.h"
#include "plane.h"
#include "player.h"
#include "prototypes.h"
#include "ship.h"
#include "unit.h"
void
shp_postread(int n, void *ptr)
@ -67,32 +65,10 @@ shp_prewrite(int n, void *old, void *new)
struct shpstr *oldsp = old;
struct shpstr *sp = new;
natid own = sp->shp_own;
struct lndstr *lp;
struct plnstr *pp;
int i;
if (own && sp->shp_effic < SHIP_MINEFF) {
mpr(own, "\t%s sunk!\n", prship(sp));
own = 0;
for (i = 0; NULL != (lp = getlandp(i)); i++) {
if (lp->lnd_own && lp->lnd_ship == n) {
mpr(lp->lnd_own, "%s sunk!\n", prland(lp));
lp->lnd_effic = 0;
lp->lnd_ship = -1;
lp->lnd_land = -1;
putland(lp->lnd_uid, lp);
}
}
for (i = 0; NULL != (pp = getplanep(i)); i++) {
if (pp->pln_own && pp->pln_ship == n) {
mpr(pp->pln_own, "%s sunk!\n", prplane(pp));
pp->pln_effic = 0;
pp->pln_ship = -1;
pp->pln_land = -1;
putplane(pp->pln_uid, pp);
}
}
} else {
item_prewrite(sp->shp_item);
}
@ -103,6 +79,8 @@ shp_prewrite(int n, void *old, void *new)
sp->shp_uid, sp->shp_x, sp->shp_y);
sp->shp_own = own;
if (!own || sp->shp_x != oldsp->shp_x || sp->shp_y != oldsp->shp_y)
unit_update_cargo((struct empobj *)sp);
}
char *

View file

@ -204,3 +204,37 @@ unit_view(struct emp_qelem *list)
sect.sct_effic, dchr[sect.sct_type].d_name);
}
}
/*
* Update cargo of CARRIER for movement or destruction.
* If the carrier is destroyed, destroy its cargo (planes, land units,
* nukes).
* Else update their location to the carrier's. Any op sectors equal
* to location get updated, too.
*/
void
unit_update_cargo(struct empobj *carrier)
{
int cargo_type;
struct nstr_item ni;
union empobj_storage obj;
for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
while (nxtitem(&ni, &obj)) {
if (!carrier->own) {
mpr(obj.gen.own, "%s lost!\n", obj_nameof(&obj.gen));
obj.gen.effic = 0;
} else {
/* mission op-area centered on the obj travels with it */
if (obj.gen.opx == obj.gen.x && obj.gen.opy == obj.gen.y) {
obj.gen.opx = carrier->x;
obj.gen.opy = carrier->y;
}
obj.gen.x = carrier->x;
obj.gen.y = carrier->y;
}
put_empobj(cargo_type, obj.gen.uid, &obj);
}
}
}