]> git.pond.sub.org Git - empserver/commitdiff
Fix cargo giveaway in scrap and scuttle
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 14 Sep 2008 02:06:44 +0000 (22:06 -0400)
committerMarkus Armbruster <armbru@pond.sub.org>
Mon, 15 Sep 2008 23:40:43 +0000 (19:40 -0400)
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
src/lib/subs/unitsub.c

index 8e2f51d68ecd7aba152f2ccf1c360a80e01c4a85..34b9c99b18378d74f6a13e31eca499e00b6c276c 100644 (file)
@@ -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);
index 272b4270fefd0326e31701166d589516f1000d6d..ebd4d5e1732dc021d282288dfd6c1ec17fc664f9 100644 (file)
@@ -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);
        }
     }