From d2b1bef0f51959791d47e6fa1d80fd5191d224a2 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sat, 13 Sep 2008 22:06:44 -0400 Subject: [PATCH] Fix cargo giveaway in scrap and scuttle When giving away cargo by scrapping or scuttling its carrier, the cargo's cargo wasn't given away. Happened for instance when a ship carrying a land unit carrying a SAM got scrapped. Also, wing, army and mission weren't cleared. To fix, create unit_give_away() and use it in unit_drop_cargo(). --- include/unit.h | 1 + src/lib/subs/unitsub.c | 45 +++++++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/include/unit.h b/include/unit.h index 8e2f51d6..34b9c99b 100644 --- a/include/unit.h +++ b/include/unit.h @@ -58,3 +58,4 @@ 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); +extern void unit_give_away(struct empobj *, natid, natid); diff --git a/src/lib/subs/unitsub.c b/src/lib/subs/unitsub.c index 272b4270..ebd4d5e1 100644 --- a/src/lib/subs/unitsub.c +++ b/src/lib/subs/unitsub.c @@ -268,13 +268,44 @@ unit_drop_cargo(struct empobj *unit, natid newown) 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; - } + if (newown) + unit_give_away(&cargo.gen, newown, cargo.gen.own); + put_empobj(type, cargo.gen.uid, &cargo.gen); + } + } +} + +/* + * Give UNIT and its cargo to RECIPIENT. + * No action if RECIPIENT already owns UNIT. + * If GIVER is non-zero, inform RECIPIENT and GIVER of the transaction. + * Clears mission and group on the units given away. + */ +void +unit_give_away(struct empobj *unit, natid recipient, natid giver) +{ + int type; + struct nstr_item ni; + union empobj_storage cargo; + + if (unit->own == recipient) + return; + + if (giver) { + mpr(unit->own, "%s given to %s\n", + obj_nameof(unit), cname(recipient)); + mpr(recipient, "%s given to you by %s\n", + obj_nameof(unit), cname(giver)); + } + + unit->own = recipient; + unit->mission = 0; + unit->group = 0; + + for (type = EF_PLANE; type <= EF_NUKE; type++) { + snxtitem_cargo(&ni, type, unit->ef_type, unit->uid); + while (nxtitem(&ni, &cargo)) { + unit_give_away(&cargo.gen, recipient, giver); put_empobj(type, cargo.gen.uid, &cargo.gen); } }