Don't permit convert to spend more money than available

Complicated by the fact that conv() ran the conversion code twice,
first for adding up the cost for chkmoney(), then for actually
converting.  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_conv() back into conv().
This commit is contained in:
Markus Armbruster 2008-08-03 14:40:13 -04:00
parent cc4cf23273
commit 82c916654c

View file

@ -40,41 +40,25 @@
#include "commands.h" #include "commands.h"
#include "land.h" #include "land.h"
static long do_conv(struct nstr_sect nstr, int uwtoconvert, int for_real);
int int
conv(void) conv(void)
{ {
struct natstr *natp; struct natstr *natp;
long cash; struct sctstr sect;
long cost;
struct nstr_sect nstr; struct nstr_sect nstr;
int uwtoconvert; int uwtoconvert, newuw, totaluw, uw;
int maxpop, civ, mil, adj_mob, mob;
double security_extra = 1.0;
struct lndstr land;
struct nstr_item ni;
natp = getnatp(player->cnum); natp = getnatp(player->cnum);
cash = natp->nat_money;
if (!snxtsct(&nstr, player->argp[1])) if (!snxtsct(&nstr, player->argp[1]))
return RET_SYN; return RET_SYN;
uwtoconvert = onearg(player->argp[2], "Number to convert: "); uwtoconvert = onearg(player->argp[2], "Number to convert: ");
if (uwtoconvert < 0) if (uwtoconvert < 0)
return RET_SYN; return RET_SYN;
cost = do_conv(nstr, uwtoconvert, 0);
if (chkmoney(cost, cash, player->argp[3]))
return RET_SYN;
return (int)do_conv(nstr, uwtoconvert, 1);
}
static long
do_conv(struct nstr_sect nstr, int uwtoconvert, int for_real)
{
struct natstr *natp;
struct sctstr sect;
int newuw, totaluw, uw;
int maxpop, civ, mil, adj_mob, mob;
double security_extra = 1.0;
struct lndstr land;
struct nstr_item ni;
long cost = 0;
totaluw = 0; totaluw = 0;
while (nxtsct(&nstr, &sect)) { while (nxtsct(&nstr, &sect)) {
@ -106,8 +90,6 @@ do_conv(struct nstr_sect nstr, int uwtoconvert, int for_real)
*/ */
security_extra += .1; security_extra += .1;
land.lnd_mobil -= 10; land.lnd_mobil -= 10;
if (for_real)
putland(land.lnd_uid, &land);
mil += land.lnd_item[I_MILIT]; mil += land.lnd_item[I_MILIT];
} }
} }
@ -138,9 +120,10 @@ do_conv(struct nstr_sect nstr, int uwtoconvert, int for_real)
newuw = adj_mob; newuw = adj_mob;
if (newuw <= 0) if (newuw <= 0)
continue; continue;
if (!for_real) { if (player->dolcost + newuw * 1.5 > natp->nat_money) {
cost += newuw * 1.5; pr("You can't afford to convert %d civilians in %s!\n",
continue; newuw, xyas(sect.sct_x, sect.sct_y, player->cnum));
break;
} }
player->btused += (newuw - 1) / 100 + 1; player->btused += (newuw - 1) / 100 + 1;
player->dolcost += newuw * 1.5; player->dolcost += newuw * 1.5;
@ -163,8 +146,6 @@ do_conv(struct nstr_sect nstr, int uwtoconvert, int for_real)
putsect(&sect); putsect(&sect);
totaluw += newuw; totaluw += newuw;
} }
if (!for_real)
return cost;
pr("Total civilians converted: %d\n", totaluw); pr("Total civilians converted: %d\n", totaluw);
pr("Paperwork at conversion places ... %d\n", player->btused); pr("Paperwork at conversion places ... %d\n", player->btused);
return RET_OK; return RET_OK;