]> git.pond.sub.org Git - empserver/commitdiff
Refactor and fix scuttle and scrap code
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 13 Sep 2008 22:35:50 +0000 (18:35 -0400)
committerMarkus Armbruster <armbru@pond.sub.org>
Mon, 15 Sep 2008 23:40:13 +0000 (19:40 -0400)
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
include/unit.h
src/lib/commands/scra.c
src/lib/commands/scut.c
src/lib/subs/unitsub.c
src/lib/update/nav_ship.c

index f7c79bee0b093863ff585fa35145572b327f7273..8efbcf1ee6017dd9e2393337393a7eca3eadf770 100644 (file)
@@ -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);
index 6f0a11e9d3399ff830047d8102dfba7f9796f03f..8e2f51d68ecd7aba152f2ccf1c360a80e01c4a85 100644 (file)
@@ -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);
index e3ce6be3d5c5f69178afce6ea6fdb0dd055790b6..c7beacfaba121d4e1fe07a260b8c07a01753da75 100644 (file)
@@ -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, &sect2);
-           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, &sect2);
-
-           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;
index f5adb17aa1f914323ba60bda87b974f4fde2f090..1de0369e3bf2f94d187da03108db709f8607865b 100644 (file)
@@ -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, &sect);
+       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, &sect);
-    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, &sect);
-    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);
-}
index 25a01c2e82186e16bbc7690f526c7ad6e3c9ce9e..272b4270fefd0326e31701166d589516f1000d6d 100644 (file)
@@ -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);
+       }
+    }
+}
index f49c2643e9fb6699e7d6feb687a2e99ffa859c54..f34b56709cfc875c0060cb9eb06083077ca1ccd8 100644 (file)
@@ -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