]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/edit.c
prnat() prnatid(): New, common country name (#number) formatting
[empserver] / src / lib / commands / edit.c
index ff941085d2be3f752f06190618fc44e9e664e606..63a4948e366d709fd81ae9ad39d12cd39975f703 100644 (file)
 #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 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 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 int edit_sect(struct sctstr *, char, int, char *);
+static int edit_nat(struct natstr *, char, int, char *);
+static int edit_ship(struct shpstr *, char, int, char *);
+static int edit_land(struct lndstr *, char, int, char *);
+static int edit_plane(struct plnstr *, char, int, char *);
 
 int
 edit(void)
@@ -129,19 +126,19 @@ 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;
        }
     }
@@ -160,19 +157,19 @@ edit(void)
            if (err == 0) {
                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;
@@ -184,13 +181,13 @@ edit(void)
 
        switch (ewhat) {
        case 'c':
-           if ((err = docountry(thing, arg, ptr, np)) != RET_OK)
+           if ((err = edit_nat(np, thing, arg, 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, thing, arg, ptr)) != RET_OK)
                return err;
            if (!putsect(&sect))
                return RET_FAIL;
@@ -198,7 +195,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, thing, arg, ptr)) != RET_OK)
                return err;
            if (!ef_ensure_space(EF_SHIP, ship.shp_uid, 50))
                return RET_FAIL;
@@ -208,7 +205,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, thing, arg, ptr)) != RET_OK)
                return err;
            if (!ef_ensure_space(EF_LAND, land.lnd_uid, 50))
                return RET_FAIL;
@@ -218,7 +215,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, thing, arg, ptr)) != RET_OK)
                return err;
            if (!ef_ensure_space(EF_PLANE, plane.pln_uid, 50))
                return RET_FAIL;
@@ -258,7 +255,7 @@ noise(struct sctstr *sptr, char *name, int old, int new)
 }
 
 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 +278,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,7 +311,7 @@ prnat(struct natstr *np)
 }
 
 static void
-pr_plane(struct plnstr *plane)
+print_plane(struct plnstr *plane)
 {
     pr("UID <U>: %d\t\t", plane->pln_uid);
     pr("Owner <O>: %d\t\t", plane->pln_own);
@@ -332,7 +328,7 @@ pr_plane(struct plnstr *plane)
 }
 
 static void
-pr_land(struct lndstr *land)
+print_land(struct lndstr *land)
 {
     pr("UID <U>: %d\n", land->lnd_uid);
     pr("Owner <O>: %d\n", land->lnd_own);
@@ -366,13 +362,13 @@ 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", prnat(natp), 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,16 +398,6 @@ 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
 getin(char *buf, char **valp)
 {
@@ -444,7 +430,7 @@ warn_deprecated(char key)
 #endif
 
 static int
-doland(char op, int arg, char *p, struct sctstr *sect)
+edit_sect(struct sctstr *sect, char op, int arg, char *p)
 {
     natid newown, oldown;
     coord newx, newy;
@@ -454,10 +440,10 @@ doland(char op, int arg, char *p, struct sctstr *sect)
     case 'o':
        if (arg < 0)
            return RET_SYN;
-       newown = (natid)errcheck(arg, 0, MAXNOC - 1);
-       pr("Owner of %s changed from %s (#%d) to %s (#%d).\n",
+       newown = (natid)LIMIT_TO(arg, 0, MAXNOC - 1);
+       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);
+          prnatid(sect->sct_own), prnatid(newown));
        if (sect->sct_own) {
            wu(player->cnum, sect->sct_own,
               "Sector %s lost to deity intervention\n",
@@ -475,55 +461,54 @@ doland(char op, int arg, char *p, struct sctstr *sect)
     case 'O':
        if (arg < 0)
            return RET_SYN;
-       oldown = (natid)errcheck(arg, 0, MAXNOC - 1);
-       pr("Old owner of %s changed from %s (#%d) to %s (#%d).\n",
+       oldown = (natid)LIMIT_TO(arg, 0, MAXNOC - 1);
+       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);
+          prnatid(sect->sct_oldown), prnatid(oldown));
        sect->sct_oldown = oldown;
        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, 255);
        noise(sect, "Mobility", sect->sct_mobil, new);
        sect->sct_mobil = new;
        break;
     case 'i':
-       new = errcheck(arg, 0, 127);
+       new = LIMIT_TO(arg, 0, 127);
        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, 127);
        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, 127);
        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, 127);
        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, 127);
        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);
+       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);
@@ -531,49 +516,49 @@ doland(char op, int arg, char *p, struct sctstr *sect)
        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);
        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",
+       new = LIMIT_TO(arg, 0, MAXNOC - 1);
+       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);
+          prnatid(old), prnatid(new));
        sect->sct_che_target = new;
        if (new == 0)
            sect->sct_che = 0;
        break;
     case 'p':
        old = sect->sct_pstage;
-       new = errcheck(arg, 0, PLG_EXPOSED);
+       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), old, new);
        sect->sct_pstage = new;
        break;
     case 't':
        old = sect->sct_ptime;
-       new = errcheck(arg, 0, 255);
+       new = LIMIT_TO(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);
        sect->sct_ptime = new;
        break;
     case 'F':
        old = sect->sct_fallout;
-       new = errcheck(arg, 0, FALLOUT_MAX);
+       new = LIMIT_TO(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);
        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);
        sect->sct_mines = new;
        pr("Mines changed to %d\n", new);
        break;
@@ -614,28 +599,19 @@ doland(char op, int arg, char *p, struct sctstr *sect)
        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;
+       new = LIMIT_TO(arg, 0, 100);
+       noise(sect, "Road percentage", sect->sct_road, new);
+       sect->sct_road = new;
        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;
+       new = LIMIT_TO(arg, 0, 100);
+       noise(sect, "Rail percentage", sect->sct_rail, new);
+       sect->sct_rail = new;
        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;
+       new = LIMIT_TO(arg, 0, 100);
+       noise(sect, "Defense percentage", sect->sct_defense, new);
+       sect->sct_defense = new;
        break;
     default:
        pr("huh? (%c)\n", op);
@@ -644,9 +620,8 @@ doland(char op, int arg, char *p, struct sctstr *sect)
     return RET_OK;
 }
 
-
 static int
-docountry(char op, int arg, char *p, struct natstr *np)
+edit_nat(struct natstr *np, char op, int arg, char *p)
 {
     coord newx, newy;
     natid nat = np->nat_cnum;
@@ -668,7 +643,7 @@ docountry(char op, int arg, char *p, struct natstr *np)
        np->nat_tgms = arg;
        break;
     case 'b':
-       arg = errcheck(arg, 0, 1024);
+       arg = LIMIT_TO(arg, 0, 1024);
        pr("BTU's changed from %d to %d\n", np->nat_btu, arg);
        np->nat_btu = arg;
        break;
@@ -700,10 +675,10 @@ 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;
@@ -743,9 +718,8 @@ docountry(char op, int arg, char *p, struct natstr *np)
     return RET_OK;
 }
 
-
 static int
-doship(char op, int arg, char *p, struct shpstr *ship)
+edit_ship(struct shpstr *ship, char op, int arg, char *p)
 {
     coord newx, newy;
 
@@ -784,12 +758,11 @@ 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, mchr[(int)ship->shp_type].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':
        ship->shp_mobil = arg;
@@ -851,7 +824,7 @@ doship(char op, int arg, char *p, struct shpstr *ship)
 }
 
 static int
-dounit(char op, int arg, char *p, struct lndstr *land)
+edit_land(struct lndstr *land, char op, int arg, char *p)
 {
     coord newx, newy;
 
@@ -882,15 +855,14 @@ 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':
        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, lchr[(int)land->lnd_type].l_tech, SHRT_MAX);
+       lnd_set_tech(land, arg);
        break;
     case 'a':
        if (p[0] == '~')
@@ -903,7 +875,7 @@ 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, 255);
        break;
     case 'S':
        land->lnd_ship = arg;
@@ -963,9 +935,8 @@ dounit(char op, int arg, char *p, struct lndstr *land)
     return RET_OK;
 }
 
-
 static int
-doplane(char op, int arg, char *p, struct plnstr *plane)
+edit_plane(struct plnstr *plane, char op, int arg, char *p)
 {
     coord newx, newy;
 
@@ -992,15 +963,14 @@ doplane(char op, int arg, char *p, struct plnstr *plane)
            plane->pln_effic = 0;
        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, 255);
        break;
     case 't':
-       pln_set_tech(plane,
-                    errcheck(arg,
-                             plchr[(int)plane->pln_type].pl_tech, SHRT_MAX));
+       arg = LIMIT_TO(arg, plchr[(int)plane->pln_type].pl_tech, SHRT_MAX);
+       pln_set_tech(plane, arg);
        break;
     case 'w':
        if (p[0] == '~')