]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/edit.c
edit: Fix stack smash in sector key 'L'
[empserver] / src / lib / commands / edit.c
index ff941085d2be3f752f06190618fc44e9e664e606..a53f443219e37f31cbbaabf24e2d4933e612924b 100644 (file)
@@ -38,6 +38,7 @@
 
 #include <ctype.h>
 #include <limits.h>
+#include "actofgod.h"
 #include "commands.h"
 #include "item.h"
 #include "land.h"
 #include "plane.h"
 #include "ship.h"
 
-#define END -1
-
-static int docountry(char, int, char *, struct natstr *);
-static int doland(char, int, char *, struct sctstr *);
-static int doplane(char, int, char *, struct plnstr *);
-static int doship(char, int, char *, struct shpstr *);
-static int dounit(char, int, char *, struct lndstr *);
-static int getin(char *, char **);
-static void pr_land(struct lndstr *);
-static void pr_plane(struct plnstr *);
-static void pr_ship(struct shpstr *);
-static void prnat(struct natstr *);
-static void prsect(struct sctstr *);
-
+static void print_sect(struct sctstr *);
+static void print_nat(struct natstr *);
+static void print_plane(struct plnstr *);
+static void print_land(struct lndstr *);
+static void print_ship(struct shpstr *);
+static char *getin(char *, char **);
+static int edit_nat(struct natstr *, char *, char *);
+static int edit_ship(struct shpstr *, char *, char *);
+static int edit_land(struct lndstr *, char *, char *);
+static int edit_plane(struct plnstr *, char *, char *);
 
 int
 edit(void)
@@ -70,10 +67,8 @@ edit(void)
     struct shpstr ship;
     struct lndstr land;
     char *what;
-    char *ptr;
-    char thing;
+    char *key, *ptr;
     int num;
-    int arg;
     int err;
     int arg_index = 3;
     coord x, y;
@@ -129,68 +124,65 @@ edit(void)
     if (!player->argp[3]) {
        switch (ewhat) {
        case 'l':
-           prsect(&sect);
+           print_sect(&sect);
            break;
        case 'c':
-           prnat(np);
+           print_nat(np);
            break;
        case 'p':
-           pr_plane(&plane);
+           print_plane(&plane);
            break;
        case 's':
-           pr_ship(&ship);
+           print_ship(&ship);
            break;
        case 'u':
-           pr_land(&land);
+           print_land(&land);
            break;
        }
     }
     for (;;) {
        if (player->argp[arg_index]) {
            if (player->argp[arg_index+1]) {
-               thing = player->argp[arg_index++][0];
+               key = player->argp[arg_index++];
                ptr = player->argp[arg_index++];
-               arg = atoi(ptr);
            } else
                return RET_SYN;
        } else if (arg_index == 3) {
-           err = getin(buf, &ptr);
-           if (err < 0)
+           key = getin(buf, &ptr);
+           if (!key)
                return RET_SYN;
-           if (err == 0) {
+           if (!*key) {
                switch (ewhat) {
                case 'c':
-                   prnat(np);
+                   print_nat(np);
                    break;
                case 'l':
-                   prsect(&sect);
+                   print_sect(&sect);
                    break;
                case 's':
-                   pr_ship(&ship);
+                   print_ship(&ship);
                    break;
                case 'u':
-                   pr_land(&land);
+                   print_land(&land);
                    break;
                case 'p':
-                   pr_plane(&plane);
+                   print_plane(&plane);
                    break;
                }
                return RET_OK;
            }
-           thing = err;
-           arg = atoi(ptr);
        } else
            return RET_OK;
 
        switch (ewhat) {
        case 'c':
-           if ((err = docountry(thing, arg, ptr, np)) != RET_OK)
+           if ((err = edit_nat(np, key, ptr)) != RET_OK)
                return err;
            break;
        case 'l':
            if (!check_sect_ok(&sect))
                return RET_FAIL;
-           if ((err = doland(thing, arg, ptr, &sect)) != RET_OK)
+           if ((err = edit_sect(&sect, key, ptr)) != RET_OK)
                return err;
            if (!putsect(&sect))
                return RET_FAIL;
@@ -198,7 +190,7 @@ edit(void)
        case 's':
            if (!check_ship_ok(&ship))
                return RET_FAIL;
-           if ((err = doship(thing, arg, ptr, &ship)) != RET_OK)
+           if ((err = edit_ship(&ship, key, ptr)) != RET_OK)
                return err;
            if (!ef_ensure_space(EF_SHIP, ship.shp_uid, 50))
                return RET_FAIL;
@@ -208,7 +200,7 @@ edit(void)
        case 'u':
            if (!check_land_ok(&land))
                return RET_FAIL;
-           if ((err = dounit(thing, arg, ptr, &land)) != RET_OK)
+           if ((err = edit_land(&land, key, ptr)) != RET_OK)
                return err;
            if (!ef_ensure_space(EF_LAND, land.lnd_uid, 50))
                return RET_FAIL;
@@ -218,7 +210,7 @@ edit(void)
        case 'p':
            if (!check_plane_ok(&plane))
                return RET_FAIL;
-           if ((err = doplane(thing, arg, ptr, &plane)) != RET_OK)
+           if ((err = edit_plane(&plane, key, ptr)) != RET_OK)
                return err;
            if (!ef_ensure_space(EF_PLANE, plane.pln_uid, 50))
                return RET_FAIL;
@@ -230,18 +222,10 @@ edit(void)
 }
 
 static void
-benefit(natid who, int good)
+benefit(natid who, int goodness)
 {
-    if (!opt_GODNEWS)
-       return;
-
-    if (good) {
-       if (who)
-           nreport(player->cnum, N_AIDS, who, 1);
-    } else {
-       if (who)
-           nreport(player->cnum, N_HURTS, who, 1);
-    }
+    if (opt_GODNEWS && getnatp(who)->nat_stat != STAT_GOD && goodness)
+       nreport(player->cnum, goodness > 0 ? N_AIDS : N_HURTS, who, 1);
 }
 
 static void
@@ -249,16 +233,16 @@ noise(struct sctstr *sptr, char *name, int old, int new)
 {
     pr("%s of %s changed from %d to %d\n",
        name, xyas(sptr->sct_x, sptr->sct_y, player->cnum), old, new);
-    if (sptr->sct_own)
-       wu(player->cnum, sptr->sct_own,
+    if (sptr->sct_own && sptr->sct_own != player->cnum && new != old)
+       wu(0, sptr->sct_own,
           "%s in %s was changed from %d to %d by an act of %s\n",
           name, xyas(sptr->sct_x, sptr->sct_y, sptr->sct_own),
           old, new, cname(player->cnum));
-    benefit(sptr->sct_own, old < new);
+    benefit(sptr->sct_own, new - old);
 }
 
 static void
-prsect(struct sctstr *sect)
+print_sect(struct sctstr *sect)
 {
     pr("Location <L>: %s\t", xyas(sect->sct_x, sect->sct_y, player->cnum));
     pr("Distribution sector <D>: %s\n",
@@ -281,9 +265,8 @@ prsect(struct sctstr *sect)
     pr("Defense %% <d>: %d\n", sect->sct_defense);
 }
 
-
 static void
-prnat(struct natstr *np)
+print_nat(struct natstr *np)
 {
     int i;
 
@@ -315,8 +298,9 @@ prnat(struct natstr *np)
 }
 
 static void
-pr_plane(struct plnstr *plane)
+print_plane(struct plnstr *plane)
 {
+    pr("%s %s\n", prnatid(plane->pln_own), prplane(plane));
     pr("UID <U>: %d\t\t", plane->pln_uid);
     pr("Owner <O>: %d\t\t", plane->pln_own);
     pr("Location <l>: %s\n",
@@ -332,8 +316,9 @@ pr_plane(struct plnstr *plane)
 }
 
 static void
-pr_land(struct lndstr *land)
+print_land(struct lndstr *land)
 {
+    pr("%s %s\n", prnatid(land->lnd_own), prland(land));
     pr("UID <U>: %d\n", land->lnd_uid);
     pr("Owner <O>: %d\n", land->lnd_own);
     pr("Location <L>: %s\n", xyas(land->lnd_x, land->lnd_y, player->cnum));
@@ -366,13 +351,9 @@ pr_land(struct lndstr *land)
 }
 
 static void
-pr_ship(struct shpstr *ship)
+print_ship(struct shpstr *ship)
 {
-    struct natstr *natp;
-
-    if (!(natp = getnatp(ship->shp_own)))
-       return;
-    pr("%s (#%d) %s\n", natp->nat_cnam, ship->shp_own, prship(ship));
+    pr("%s %s\n", prnatid(ship->shp_own), prship(ship));
     pr("UID <U>: %d\n", ship->shp_uid);
     pr("Owner <O>: %d\t\t\t", ship->shp_own);
     pr("Location <L>: %s\n", xyas(ship->shp_x, ship->shp_y, player->cnum));
@@ -402,37 +383,26 @@ pr_ship(struct shpstr *ship)
     pr("\n");
 }
 
-static int
-errcheck(int num, int min, int max)
-{
-    if (num < min)
-       return min;
-    else if (num > max)
-       return max;
-    return num;
-}
-
-static int
+static char *
 getin(char *buf, char **valp)
 {
+    char line[1024];
+    char *argp[128];
     char *p;
-    unsigned char thing;
 
-    p = getstarg(NULL, "%c xxxxx -- thing value : ", buf);
+    *valp = NULL;
+    p = getstarg(NULL, "%c xxxxx -- thing value : ", line);
     if (!p)
-       return -1;
-    if (!*p)
-       return 0;
-    for (; isspace(*p); p++) ;
-    if (!*p)
-       return -1;
-    thing = *p;
-    for (; *p && !isspace(*p); p++) ;
-    for (; isspace(*p); p++) ;
-    if (!*p)
-       return -1;
-    *valp = p;
-    return thing;
+       return NULL;
+    switch (parse(p, buf, argp, NULL, NULL, NULL)) {
+    case 0:
+       return "";
+    case 1:
+       return NULL;
+    default:
+       *valp = argp[1];
+       return argp[0];
+    }
 }
 
 #if 0  /* not needed right now */
@@ -443,216 +413,259 @@ warn_deprecated(char key)
 }
 #endif
 
-static int
-doland(char op, int arg, char *p, struct sctstr *sect)
+int
+edit_sect_i(struct sctstr *sect, char *key, int arg)
 {
-    natid newown, oldown;
-    coord newx, newy;
-    int new, old;
-    int des;
-    switch (op) {
+    int new;
+
+    switch (*key) {
     case 'o':
-       if (arg < 0)
+       if (arg < 0 || arg >= MAXNOC)
            return RET_SYN;
-       newown = (natid)errcheck(arg, 0, MAXNOC - 1);
-       pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
+       pr("Owner of %s changed from %s to %s.\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
-          cname(sect->sct_own), sect->sct_own, cname(newown), newown);
-       if (sect->sct_own) {
-           wu(player->cnum, sect->sct_own,
-              "Sector %s lost to deity intervention\n",
-              xyas(sect->sct_x, sect->sct_y, sect->sct_own));
-       }
-       benefit(sect->sct_own, 0);
-       sect->sct_own = newown;
-       if (newown) {
-           wu(player->cnum, newown,
-              "Sector %s gained from deity intervention\n",
-              xyas(sect->sct_x, sect->sct_y, newown));
-       }
-       benefit(newown, 1);
+          prnatid(sect->sct_own), prnatid(arg));
+       if (arg == sect->sct_own)
+           break;
+       report_god_takes("Sector ",
+                        xyas(sect->sct_x, sect->sct_y, sect->sct_own),
+                        sect->sct_own);
+       report_god_gives("Sector ",
+                        xyas(sect->sct_x, sect->sct_y, arg),
+                        arg);
+       sect->sct_own = arg;
        break;
     case 'O':
-       if (arg < 0)
+       if (arg < 0 || arg >= MAXNOC)
            return RET_SYN;
-       oldown = (natid)errcheck(arg, 0, MAXNOC - 1);
-       pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
+       pr("Old owner of %s changed from %s to %s.\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
-          cname(sect->sct_oldown),
-          sect->sct_oldown, cname(oldown), oldown);
-       sect->sct_oldown = oldown;
+          prnatid(sect->sct_oldown), prnatid(arg));
+       if (arg == sect->sct_oldown)
+           break;
+       if (sect->sct_own && sect->sct_own != player->cnum)
+           wu(0, sect->sct_own,
+              "Old owner of %s changed from %s to %s by an act of %s\n",
+              xyas(sect->sct_x, sect->sct_y, player->cnum),
+              prnatid(sect->sct_oldown), prnatid(arg),
+              cname(player->cnum));
+       sect->sct_oldown = arg;
        break;
     case 'e':
-       new = errcheck(arg, 0, 100);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Efficiency", sect->sct_effic, new);
        sect->sct_effic = new;
        break;
     case 'm':
-       new = errcheck(arg, -127, 255);
+       new = LIMIT_TO(arg, -127, 127);
        noise(sect, "Mobility", sect->sct_mobil, new);
        sect->sct_mobil = new;
        break;
     case 'i':
-       new = errcheck(arg, 0, 127);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Iron ore content", sect->sct_min, new);
        sect->sct_min = (unsigned char)new;
        break;
     case 'g':
-       new = errcheck(arg, 0, 127);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Gold content", sect->sct_gmin, new);
        sect->sct_gmin = (unsigned char)new;
        break;
     case 'f':
-       new = errcheck(arg, 0, 127);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Fertility", sect->sct_fertil, new);
        sect->sct_fertil = (unsigned char)new;
        break;
     case 'c':
-       new = errcheck(arg, 0, 127);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Oil content", sect->sct_oil, new);
        sect->sct_oil = (unsigned char)new;
        break;
     case 'u':
-       new = errcheck(arg, 0, 127);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Uranium content", sect->sct_uran, new);
        sect->sct_uran = (unsigned char)new;
        break;
     case 'w':
-       new = errcheck(arg, 0, 100);
+       new = LIMIT_TO(arg, 0, 100);
        noise(sect, "Workforce percentage", sect->sct_work, new);
        sect->sct_work = (unsigned char)new;
        break;
     case 'l':
-       new = errcheck(arg, 0, 127);
-       pr("Loyalty of %s changed from %d to %d%%\n",
+       new = LIMIT_TO(arg, 0, 127);
+       pr("Loyalty of %s changed from %d to %d\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
           sect->sct_loyal, new);
        sect->sct_loyal = (unsigned char)new;
        break;
     case 'x':
-       old = sect->sct_che;
-       new = errcheck(arg, 0, CHE_MAX);
+       new = LIMIT_TO(arg, 0, CHE_MAX);
        pr("Guerillas in %s changed from %d to %d\n",
-          xyas(sect->sct_x, sect->sct_y, player->cnum), old, new);
+          xyas(sect->sct_x, sect->sct_y, player->cnum),
+          sect->sct_che, new);
        sect->sct_che = new;
        break;
     case 'X':
-       old = sect->sct_che_target;
-       new = errcheck(arg, 0, MAXNOC - 1);
-       pr("Che target of %s changed from %s (#%d) to %s (#%d).\n",
+       if (arg < 0 || arg >= MAXNOC)
+           return RET_SYN;
+       pr("Che target of %s changed from %s to %s.\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
-          cname(old), old, cname(new), new);
-       sect->sct_che_target = new;
-       if (new == 0)
+          prnatid(sect->sct_che_target), prnatid(arg));
+       sect->sct_che_target = arg;
+       if (arg == 0)
            sect->sct_che = 0;
        break;
     case 'p':
-       old = sect->sct_pstage;
-       new = errcheck(arg, 0, PLG_EXPOSED);
-       pr("Plague stage of %s changed from %d to %d%%\n",
-          xyas(sect->sct_x, sect->sct_y, player->cnum), old, new);
+       new = LIMIT_TO(arg, 0, PLG_EXPOSED);
+       pr("Plague stage of %s changed from %d to %d\n",
+          xyas(sect->sct_x, sect->sct_y, player->cnum),
+          sect->sct_pstage, new);
        sect->sct_pstage = new;
        break;
     case 't':
-       old = sect->sct_ptime;
-       new = errcheck(arg, 0, 255);
-       pr("Plague time of %s changed from %d to %d%%\n",
-          xyas(sect->sct_x, sect->sct_y, player->cnum), old, new);
+       new = LIMIT_TO(arg, 0, 32767);
+       pr("Plague time of %s changed from %d to %d\n",
+          xyas(sect->sct_x, sect->sct_y, player->cnum),
+          sect->sct_ptime, new);
        sect->sct_ptime = new;
        break;
     case 'F':
-       old = sect->sct_fallout;
-       new = errcheck(arg, 0, FALLOUT_MAX);
-       pr("Fallout for sector %s changed from %d to %d\n",
-          xyas(sect->sct_x, sect->sct_y, player->cnum), old, new);
+       new = LIMIT_TO(arg, 0, FALLOUT_MAX);
+       noise(sect, "Fallout", sect->sct_fallout, new);
        sect->sct_fallout = new;
        break;
     case 'a':
-       new = errcheck(arg, 0, 9999);
+       new = LIMIT_TO(arg, 0, 9999);
        noise(sect, "Available workforce", sect->sct_avail, new);
        sect->sct_avail = new;
        break;
     case 'M':
-       new = errcheck(arg, 0, MINES_MAX);
+       new = LIMIT_TO(arg, 0, MINES_MAX);
+       if (sect->sct_own == sect->sct_oldown)
+           noise(sect, "Mines", sect->sct_mines, new);
+       else
+           pr("Mines of %s changed from %d to %d\n",
+              xyas(sect->sct_x, sect->sct_y, player->cnum),
+              sect->sct_mines, new);
        sect->sct_mines = new;
-       pr("Mines changed to %d\n", new);
        break;
+    case 'R':
+       new = LIMIT_TO(arg, 0, 100);
+       noise(sect, "Road percentage", sect->sct_road, new);
+       sect->sct_road = new;
+       break;
+    case 'r':
+       new = LIMIT_TO(arg, 0, 100);
+       noise(sect, "Rail percentage", sect->sct_rail, new);
+       sect->sct_rail = new;
+       break;
+    case 'd':
+       new = LIMIT_TO(arg, 0, 100);
+       noise(sect, "Defense percentage", sect->sct_defense, new);
+       sect->sct_defense = new;
+       break;
+    default:
+       pr("huh? (%s)\n", key);
+       return RET_SYN;
+    }
+    return RET_OK;
+}
+
+int
+edit_sect(struct sctstr *sect, char *key, char *p)
+{
+    coord newx, newy;
+    int new;
+    struct sctstr newsect;
+
+    switch (*key) {
     case 'L':
        if (!sarg_xy(p, &newx, &newy))
            return RET_SYN;
+       getsect(newx, newy, &newsect);
+       pr("Sector %s duplicated to %s\n",
+          xyas(sect->sct_x, sect->sct_y, player->cnum),
+          xyas(newx, newy, player->cnum));
+       report_god_takes("Sector ", xyas(newx, newy, newsect.sct_own),
+                        newsect.sct_own);
+       report_god_gives("Sector ", xyas(newx, newy, sect->sct_own),
+                        sect->sct_own);
        sect->sct_x = newx;
        sect->sct_y = newy;
-       ef_set_uid(EF_SECTOR, &sect, XYOFFSET(newx, newy));
+       ef_set_uid(EF_SECTOR, sect, XYOFFSET(newx, newy));
        break;
     case 'D':
        if (!sarg_xy(p, &newx, &newy))
            return RET_SYN;
-       pr("Distribution location for sector %s changed from %s to %s\n",
+       pr("Distribution sector of sector %s changed from %s to %s\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
           xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum),
           xyas(newx, newy, player->cnum));
+       if (newx == sect->sct_dist_x && newy == sect->sct_dist_y)
+           break;
+       if (sect->sct_own && sect->sct_own != player->cnum)
+           wu(0, sect->sct_own,
+              "Distribution sector of sector %s changed from %s to %s"
+              " by an act of %s\n",
+              xyas(sect->sct_x, sect->sct_y, player->cnum),
+              xyas(sect->sct_dist_x, sect->sct_dist_y, player->cnum),
+              xyas(newx, newy, player->cnum),
+              cname(player->cnum));
        sect->sct_dist_x = newx;
        sect->sct_dist_y = newy;
        break;
     case 's':
-       des = sct_typematch(p);
-       if (des < 0)
+       new = sct_typematch(p);
+       if (new < 0)
            return RET_SYN;
-       pr("Designation for sector %s changed from %c to %c\n",
+       pr("Designation of sector %s changed from %c to %c\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
-          dchr[sect->sct_type].d_mnem, dchr[des].d_mnem);
-       set_coastal(sect, sect->sct_type, des);
-       sect->sct_type = des;
+          dchr[sect->sct_type].d_mnem, dchr[new].d_mnem);
+       if (new == sect->sct_type)
+           break;
+       if (sect->sct_own && sect->sct_own != player->cnum)
+           wu(0, sect->sct_own,
+              "Designation of sector %s changed from %c to %c"
+              " by an act of %s\n",
+              xyas(sect->sct_x, sect->sct_y, player->cnum),
+              dchr[sect->sct_type].d_mnem, dchr[new].d_mnem,
+              cname(player->cnum));
+       set_coastal(sect, sect->sct_type, new);
+       sect->sct_type = new;
        break;
     case 'S':
-       des = sct_typematch(p);
-       if (des < 0)
+       new = sct_typematch(p);
+       if (new < 0)
            return RET_SYN;
-       pr("New designation for sector %s changed from %c to %c\n",
+       pr("New designation of sector %s changed from %c to %c\n",
           xyas(sect->sct_x, sect->sct_y, player->cnum),
-          dchr[sect->sct_newtype].d_mnem, dchr[des].d_mnem);
-       sect->sct_newtype = des;
-       break;
-    case 'R':
-       if (arg > 100)
-           arg = 100;
-       if (arg < 0)
-           arg = 0;
-       noise(sect, "Road percentage", sect->sct_road, arg);
-       sect->sct_road = arg;
-       break;
-    case 'r':
-       if (arg > 100)
-           arg = 100;
-       if (arg < 0)
-           arg = 0;
-       noise(sect, "Rail percentage", sect->sct_rail, arg);
-       sect->sct_rail = arg;
-       break;
-    case 'd':
-       if (arg > 100)
-           arg = 100;
-       if (arg < 0)
-           arg = 0;
-       noise(sect, "Defense percentage", sect->sct_defense, arg);
-       sect->sct_defense = arg;
+          dchr[sect->sct_newtype].d_mnem, dchr[new].d_mnem);
+       if (new == sect->sct_newtype)
+           break;
+       if (sect->sct_own && sect->sct_own != player->cnum)
+           wu(0, sect->sct_own,
+              "New designation of sector %s changed from %c to %c"
+              " by an act of %s\n",
+              xyas(sect->sct_x, sect->sct_y, player->cnum),
+              dchr[sect->sct_newtype].d_mnem, dchr[new].d_mnem,
+              cname(player->cnum));
+       sect->sct_newtype = new;
        break;
     default:
-       pr("huh? (%c)\n", op);
-       return RET_SYN;
+       return edit_sect_i(sect, key, atoi(p));
     }
     return RET_OK;
 }
 
-
 static int
-docountry(char op, int arg, char *p, struct natstr *np)
+edit_nat(struct natstr *np, char *key, char *p)
 {
     coord newx, newy;
     natid nat = np->nat_cnum;
+    int arg = atoi(p);
     float farg = (float)atof(p);
 
-    switch (op) {
+    switch (*key) {
     case 'n':
        if (!check_nat_name(p, nat))
            return RET_SYN;
@@ -665,20 +678,25 @@ docountry(char op, int arg, char *p, struct natstr *np)
        strncpy(np->nat_pnam, p, sizeof(np->nat_pnam) - 1);
        break;
     case 't':
+       arg = LIMIT_TO(arg, 0, USHRT_MAX);
        np->nat_tgms = arg;
        break;
     case 'b':
-       arg = errcheck(arg, 0, 1024);
+       arg = LIMIT_TO(arg, 0, max_btus);
        pr("BTU's changed from %d to %d\n", np->nat_btu, arg);
        np->nat_btu = arg;
        break;
     case 'm':
-       benefit(nat, np->nat_reserve < arg);
+       arg = LIMIT_TO(arg, 0, INT_MAX);
+       benefit(nat, arg - np->nat_reserve);
        pr("Military reserves changed from %d to %d\n",
           np->nat_reserve, arg);
-       wu(player->cnum, nat,
-          "Military reserves changed from %d to %d by divine intervention.\n",
-          np->nat_reserve, arg);
+       if (arg == np->nat_reserve)
+           break;
+       if (nat != player->cnum)
+           wu(0, nat,
+              "Military reserves changed from %d to %d by an act of %s\n",
+              np->nat_reserve, arg, cname(player->cnum));
        np->nat_reserve = arg;
        break;
     case 'c':
@@ -700,61 +718,71 @@ docountry(char op, int arg, char *p, struct natstr *np)
        np->nat_yorg = newy;
        break;
     case 's':
-       np->nat_stat = errcheck(arg, STAT_UNUSED, STAT_GOD);
+       np->nat_stat = LIMIT_TO(arg, STAT_UNUSED, STAT_GOD);
        break;
     case 'u':
-       arg = errcheck(arg, 0, m_m_p_d * 60);
+       arg = LIMIT_TO(arg, 0, m_m_p_d * 60);
        pr("Number of seconds used changed from %d to %d.\n",
           np->nat_timeused, arg);
        np->nat_timeused = arg;
        break;
     case 'M':
        pr("Money changed from %d to %d\n", np->nat_money, arg);
-       wu(player->cnum, nat,
-          "Money changed from %d to %d by divine intervention.\n",
-          np->nat_money, arg);
+       if (arg == np->nat_money)
+           break;
+       if (nat != player->cnum)
+           wu(0, nat, "Money changed from %d to %d by an act of %s\n",
+              np->nat_money, arg, cname(player->cnum));
        np->nat_money = arg;
        break;
     case 'T':
+       farg = MAX(0.0, farg);
        pr("Tech changed from %.2f to %.2f.\n",
           np->nat_level[NAT_TLEV], farg);
        np->nat_level[NAT_TLEV] = farg;
        break;
     case 'R':
+       farg = MAX(0.0, farg);
        pr("Research changed from %.2f to %.2f.\n",
           np->nat_level[NAT_RLEV], farg);
        np->nat_level[NAT_RLEV] = farg;
        break;
     case 'E':
+       farg = MAX(0.0, farg);
        pr("Education changed from %.2f to %.2f.\n",
           np->nat_level[NAT_ELEV], farg);
        np->nat_level[NAT_ELEV] = farg;
        break;
     case 'H':
+       farg = MAX(0.0, farg);
        pr("Happiness changed from %.2f to %.2f.\n",
           np->nat_level[NAT_HLEV], farg);
        np->nat_level[NAT_HLEV] = farg;
        break;
     default:
-       pr("huh? (%c)\n", op);
+       pr("huh? (%s)\n", key);
        break;
     }
     putnat(np);
     return RET_OK;
 }
 
-
 static int
-doship(char op, int arg, char *p, struct shpstr *ship)
+edit_ship(struct shpstr *ship, char *key, char *p)
 {
+    struct mchrstr *mcp = &mchr[ship->shp_type];
+    int arg = atoi(p);
     coord newx, newy;
+    struct ichrstr *ip;
 
     newx = newy = 0;
-    switch (op) {
+    switch (*key) {
     case 'a':
+       arg = LIMIT_TO(arg, 0, PLG_EXPOSED);
        ship->shp_pstage = arg;
        break;
     case 'b':
+       arg = LIMIT_TO(arg, 0, 32767);
        ship->shp_ptime = arg;
        break;
     case 'R':
@@ -767,15 +795,17 @@ doship(char op, int arg, char *p, struct shpstr *ship)
        ef_set_uid(EF_SHIP, ship, arg);
        break;
     case 'O':
-       if (ship->shp_own)
-           wu(player->cnum, ship->shp_own,
-              "%s taken from you by deity intervention!\n", prship(ship));
-       if (arg && arg < MAXNOC) {
-           wu(player->cnum, (natid)arg,
-              "%s given to you by deity intervention!\n", prship(ship));
-           ship->shp_own = (natid)arg;
-       } else if (!arg)
-           ship->shp_effic = 0;
+       if (arg < 0 || arg >= MAXNOC)
+           return RET_SYN;
+       if (arg == ship->shp_own)
+           break;
+       if (ship->shp_own && ship->shp_own != player->cnum)
+           wu(0, ship->shp_own, "%s taken from you by an act of %s!\n",
+              prship(ship), cname(player->cnum));
+       if (arg && arg != player->cnum)
+           wu(0, arg, "%s given to you by an act of %s!\n",
+              prship(ship), cname(player->cnum));
+       ship->shp_own = arg;
        break;
     case 'L':
        if (!sarg_xy(p, &newx, &newy))
@@ -784,14 +814,14 @@ doship(char op, int arg, char *p, struct shpstr *ship)
        ship->shp_y = newy;
        break;
     case 'T':
-       shp_set_tech(ship,
-                    errcheck(arg,
-                             mchr[(int)ship->shp_type].m_tech, SHRT_MAX));
+       arg = LIMIT_TO(arg, mcp->m_tech, SHRT_MAX);
+       shp_set_tech(ship, arg);
        break;
     case 'E':
-       ship->shp_effic = errcheck(arg, SHIP_MINEFF, 100);
+       ship->shp_effic = LIMIT_TO(arg, SHIP_MINEFF, 100);
        break;
     case 'M':
+       arg = LIMIT_TO(arg, -127, 127);
        ship->shp_mobil = arg;
        break;
     case 'F':
@@ -805,75 +835,61 @@ doship(char op, int arg, char *p, struct shpstr *ship)
        }
        break;
     case 'c':
-       ship->shp_item[I_CIVIL] = arg;
-       break;
     case 'm':
-       ship->shp_item[I_MILIT] = arg;
-       break;
     case 'u':
-       ship->shp_item[I_UW] = arg;
-       break;
     case 'f':
-       ship->shp_item[I_FOOD] = arg;
-       break;
     case 's':
-       ship->shp_item[I_SHELL] = arg;
-       break;
     case 'g':
-       ship->shp_item[I_GUN] = arg;
-       break;
     case 'p':
-       ship->shp_item[I_PETROL] = arg;
-       break;
     case 'i':
-       ship->shp_item[I_IRON] = arg;
-       break;
     case 'd':
-       ship->shp_item[I_DUST] = arg;
-       break;
     case 'o':
-       ship->shp_item[I_OIL] = arg;
-       break;
     case 'l':
-       ship->shp_item[I_LCM] = arg;
-       break;
     case 'h':
-       ship->shp_item[I_HCM] = arg;
-       break;
     case 'r':
-       ship->shp_item[I_RAD] = arg;
+       ip = item_by_name(key);
+       arg = LIMIT_TO(arg, 0, mchr[ship->shp_type].m_item[ip->i_uid]);
+       ship->shp_item[ip->i_uid] = arg;
        break;
     default:
-       pr("huh? (%c)\n", op);
+       pr("huh? (%s)\n", key);
        return RET_FAIL;
     }
     return RET_OK;
 }
 
 static int
-dounit(char op, int arg, char *p, struct lndstr *land)
+edit_land(struct lndstr *land, char *key, char *p)
 {
+    struct lchrstr *lcp = &lchr[land->lnd_type];
+    int arg = atoi(p);
     coord newx, newy;
+    struct ichrstr *ip;
 
     newx = newy = 0;
-    switch (op) {
+    switch (*key) {
     case 'Y':
+       if (arg < -1 || arg >= ef_nelem(EF_LAND))
+           return RET_SYN;
+       if (arg >= 0 && arg != land->lnd_land)
+           land->lnd_ship = -1;
        land->lnd_land = arg;
        break;
     case 'U':
        ef_set_uid(EF_LAND, land, arg);
        break;
     case 'O':
-       if (land->lnd_own)
-           wu(player->cnum, land->lnd_own,
-              "%s taken from you by deity intervention!\n", prland(land));
-
-       if (arg && arg < MAXNOC) {
-           wu(player->cnum, (natid)arg,
-              "%s given to you by deity intervention!\n", prland(land));
-           land->lnd_own = (natid)arg;
-       } else if (!arg)
-           land->lnd_effic = 0;
+       if (arg < 0 || arg >= MAXNOC)
+           return RET_SYN;
+       if (arg == land->lnd_own)
+           break;
+       if (land->lnd_own && land->lnd_own != player->cnum)
+           wu(0, land->lnd_own, "%s taken from you by an act of %s!\n",
+              prland(land), cname(player->cnum));
+       if (arg && arg != player->cnum)
+           wu(0, arg, "%s given to you by an act of %s!\n",
+              prland(land), cname(player->cnum));
+       land->lnd_own = arg;
        break;
     case 'L':
        if (!sarg_xy(p, &newx, &newy))
@@ -882,15 +898,15 @@ dounit(char op, int arg, char *p, struct lndstr *land)
        land->lnd_y = newy;
        break;
     case 'e':
-       land->lnd_effic = errcheck(arg, LAND_MINEFF, 100);
+       land->lnd_effic = LIMIT_TO(arg, LAND_MINEFF, 100);
        break;
     case 'M':
+       arg = LIMIT_TO(arg, -127, 127);
        land->lnd_mobil = arg;
        break;
     case 't':
-       lnd_set_tech(land,
-                    errcheck(arg,
-                             lchr[(int)land->lnd_type].l_tech, SHRT_MAX));
+       arg = LIMIT_TO(arg, lcp->l_tech, SHRT_MAX);
+       lnd_set_tech(land, arg);
        break;
     case 'a':
        if (p[0] == '~')
@@ -903,12 +919,17 @@ dounit(char op, int arg, char *p, struct lndstr *land)
        }
        break;
     case 'F':
-       land->lnd_harden = errcheck(arg, 0, 255);
+       land->lnd_harden = LIMIT_TO(arg, 0, 127);
        break;
     case 'S':
+       if (arg < -1 || arg >= ef_nelem(EF_SHIP))
+           return RET_SYN;
+       if (arg >= 0 && arg != land->lnd_ship)
+           land->lnd_land = -1;
        land->lnd_ship = arg;
        break;
     case 'Z':
+       arg = LIMIT_TO(arg, 0, 100);
        land->lnd_retreat = arg;
        break;
     case 'R':
@@ -918,58 +939,37 @@ dounit(char op, int arg, char *p, struct lndstr *land)
        land->lnd_rflags = arg;
        break;
     case 'c':
-       land->lnd_item[I_CIVIL] = arg;
-       break;
     case 'm':
-       land->lnd_item[I_MILIT] = arg;
-       break;
     case 'u':
-       land->lnd_item[I_UW] = arg;
-       break;
     case 'f':
-       land->lnd_item[I_FOOD] = arg;
-       break;
     case 's':
-       land->lnd_item[I_SHELL] = arg;
-       break;
     case 'g':
-       land->lnd_item[I_GUN] = arg;
-       break;
     case 'p':
-       land->lnd_item[I_PETROL] = arg;
-       break;
     case 'i':
-       land->lnd_item[I_IRON] = arg;
-       break;
     case 'd':
-       land->lnd_item[I_DUST] = arg;
-       break;
     case 'o':
-       land->lnd_item[I_OIL] = arg;
-       break;
     case 'l':
-       land->lnd_item[I_LCM] = arg;
-       break;
     case 'h':
-       land->lnd_item[I_HCM] = arg;
-       break;
     case 'r':
-       land->lnd_item[I_RAD] = arg;
+       ip = item_by_name(key);
+       arg = LIMIT_TO(arg, 0, lchr[land->lnd_type].l_item[ip->i_uid]);
+       land->lnd_item[ip->i_uid] = arg;
        break;
     default:
-       pr("huh? (%c)\n", op);
+       pr("huh? (%s)\n", key);
        return RET_FAIL;
     }
     return RET_OK;
 }
 
-
 static int
-doplane(char op, int arg, char *p, struct plnstr *plane)
+edit_plane(struct plnstr *plane, char *key, char *p)
 {
+    struct plchrstr *pcp = &plchr[plane->pln_type];
+    int arg = atoi(p);
     coord newx, newy;
 
-    switch (op) {
+    switch (*key) {
     case 'U':
        ef_set_uid(EF_PLANE, plane, arg);
        break;
@@ -980,27 +980,27 @@ doplane(char op, int arg, char *p, struct plnstr *plane)
        plane->pln_y = newy;
        break;
     case 'O':
-       if (plane->pln_own)
-           wu(player->cnum, plane->pln_own,
-              "%s taken from you by deity intervention!\n",
-              prplane(plane));
-       if (arg && arg < MAXNOC) {
-           plane->pln_own = (natid)arg;
-           wu(player->cnum, plane->pln_own,
-              "%s given to you by deity intervention!\n", prplane(plane));
-       } else if (!arg)
-           plane->pln_effic = 0;
+       if (arg < 0 || arg >= MAXNOC)
+           return RET_SYN;
+       if (arg == plane->pln_own)
+           break;
+       if (plane->pln_own && plane->pln_own != player->cnum)
+           wu(0, plane->pln_own, "%s taken from you by an act of %s!\n",
+              prplane(plane), cname(player->cnum));
+       if (arg && arg != player->cnum)
+           wu(0, arg, "%s given to you by an act of %s!\n",
+              prplane(plane), cname(player->cnum));
+       plane->pln_own = arg;
        break;
     case 'e':
-       plane->pln_effic = errcheck(arg, PLANE_MINEFF, 100);
+       plane->pln_effic = LIMIT_TO(arg, PLANE_MINEFF, 100);
        break;
     case 'm':
-       plane->pln_mobil = errcheck(arg, -127, 255);
+       plane->pln_mobil = LIMIT_TO(arg, -127, 127);
        break;
     case 't':
-       pln_set_tech(plane,
-                    errcheck(arg,
-                             plchr[(int)plane->pln_type].pl_tech, SHRT_MAX));
+       arg = LIMIT_TO(arg, pcp->pl_tech, SHRT_MAX);
+       pln_set_tech(plane, arg);
        break;
     case 'w':
        if (p[0] == '~')
@@ -1013,19 +1013,28 @@ doplane(char op, int arg, char *p, struct plnstr *plane)
        }
        break;
     case 'r':
+       arg = LIMIT_TO(arg, 0, pl_range(pcp, plane->pln_tech));
        plane->pln_range = (unsigned char)arg;
        break;
     case 's':
+       if (arg < -1 || arg >= ef_nelem(EF_SHIP))
+           return RET_SYN;
+       if (arg >= 0 && arg != plane->pln_ship)
+           plane->pln_land = -1;
        plane->pln_ship = arg;
        break;
     case 'y':
+       if (arg < -1 || arg >= ef_nelem(EF_LAND))
+           return RET_SYN;
+       if (arg >= 0 && arg != plane->pln_land)
+           plane->pln_ship = -1;
        plane->pln_land = arg;
        break;
     case 'f':
        plane->pln_flags = arg;
        break;
     default:
-       pr("huh? (%c)\n", op);
+       pr("huh? (%s)\n", key);
        return RET_FAIL;
     }
     return RET_OK;