]> git.pond.sub.org Git - empserver/commitdiff
Don't permit demobilize to spend more money than available
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 3 Aug 2008 18:14:07 +0000 (14:14 -0400)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 9 Aug 2008 12:34:00 +0000 (08:34 -0400)
Complicated by the fact that demo() ran the demobilization code twice,
first for adding up the cost for chkmoney(), then for actually
demobilizing.  chkmoney() asks the player to confirm when he's about
to spend more than half his cash.  Get rid of that, not worth the
complexity.  This merges do_demo() back into demo().

It also removes the command's virtually undocumented fourth argument.
Update player_coms[] accordingly.  While there, make it require money;
it won't do anything useful without money anyway.

src/lib/commands/demo.c
src/lib/player/empmod.c

index 1a4674f53a1643fd3857e19a870ff1c23980d752..39c1b7ad1a73695207967e9de976b86df120cf8b 100644 (file)
  *
  */
 
-static long do_demo(struct natstr *natp, struct nstr_sect nstr, int number,
-                   char *p, int for_real);
-
 int
 demo(void)
 {
     struct natstr *natp;
-    long cash;
-    long cost;
     int number;
     char *p;
     char buf[1024];
     struct nstr_sect nstr;
+    struct sctstr sect;
+    int mil_demob;
+    int mil;
+    int civ;
+    int deltamil;
+    int reserves;
 
     natp = getnatp(player->cnum);
-    cash = natp->nat_money;
     if (!snxtsct(&nstr, player->argp[1]))
        return RET_SYN;
     if (!(p = getstarg(player->argp[2], "Number to de-mobilize : ", buf)))
@@ -66,23 +66,6 @@ demo(void)
        return RET_SYN;
     if (*p != 'y' && *p != 'n')
        return RET_SYN;
-    cost = do_demo(natp, nstr, number, p, 0);
-    if (chkmoney(cost, cash, player->argp[4]))
-       return RET_SYN;
-    return (int)do_demo(natp, nstr, number, p, 1);
-}
-
-static long
-do_demo(struct natstr *natp, struct nstr_sect nstr, int number, char *p,
-       int for_real)
-{
-    struct sctstr sect;
-    int mil_demob;
-    int mil;
-    int civ;
-    int deltamil;
-    int reserves;
-    long cost = 0;
 
     mil_demob = 0;
     reserves = 0;
@@ -99,14 +82,15 @@ do_demo(struct natstr *natp, struct nstr_sect nstr, int number, char *p,
            continue;
        if (deltamil > ITEM_MAX - civ)
            deltamil = ITEM_MAX - civ;
+       if (player->dolcost + deltamil * 5 > natp->nat_money) {
+           pr("You can't afford to demobilize %d military in %s!\n",
+              deltamil, xyas(sect.sct_x, sect.sct_y, player->cnum));
+           break;
+       }
+       player->dolcost += deltamil * 5;
        civ += deltamil;
        mil -= deltamil;
        mil_demob += deltamil;
-       if (!for_real) {
-           cost += deltamil * 5;
-           continue;
-       }
-       player->dolcost += deltamil * 5;
        pr("%d demobilized in %s (%d mil left)\n",
           deltamil, xyas(sect.sct_x, sect.sct_y, player->cnum), mil);
        if (*p == 'y')
@@ -115,11 +99,9 @@ do_demo(struct natstr *natp, struct nstr_sect nstr, int number, char *p,
        sect.sct_item[I_CIVIL] = civ;
        putsect(&sect);
     }
-    if (!for_real)
-       return cost;
     if (!mil_demob) {
        pr("No eligible sectors/military for demobilization\n");
-       return (long)RET_FAIL;
+       return RET_FAIL;
     }
     pr("Total new civilians : %d\n", mil_demob);
     if (*p == 'y')
index 621d44131e3169cf7cc23a375a2ed314b48782d4..2a2a2913fcb5c6fdec5f534854a40695817d9efc 100644 (file)
@@ -87,8 +87,8 @@ struct cmndstr player_coms[] = {
     {"declare <all|friend|neut|hos|war> <NATS>",
      2, decl, C_MOD, NORM + CAP},
     {"deliver <COMM> <SECTS> [q|<THRESH>] [<DIR>]", 1, deli, C_MOD, NORM},
-    {"demobilize <SECTS> <NUM> <active-reserve?> [sure?]",
-     1, demo, C_MOD, NORM + CAP},
+    {"demobilize <SECTS> <NUM> <active-reserve?>", 1, demo, C_MOD,
+     NORM + MONEY + CAP},
     {"designate <SECTS> <type> [sure?]", 1, desi, C_MOD, NORM},
     {"disable", 0, disa, C_MOD, GOD},
     {"disarm <PLANE>", 2, disarm, C_MOD, NORM + MONEY + CAP},