build: Fix inexact calculation of required materials
sector_can_build() computes mat[i] * (effic / 100.0). The division is
inexact. The result gets randomly rounded, so errors are vanishingly
unlikely to screw up material consumption.
However, we require the amount rounded up to be present since commit
1227d2c
. Errors *can* screw that up. Fix by avoiding inexact
computation for that part.
We should probably review rounding of inexact values in general.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
parent
c869c868e2
commit
bfea5a2cf4
1 changed files with 9 additions and 8 deletions
|
@ -28,7 +28,7 @@
|
|||
*
|
||||
* Known contributors to this file:
|
||||
* Steve McClure, 1998-2000
|
||||
* Markus Armbruster, 2004-2014
|
||||
* Markus Armbruster, 2004-2015
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
@ -652,8 +652,8 @@ static int
|
|||
sector_can_build(struct sctstr *sp, short mat[], int work,
|
||||
int effic, char *what)
|
||||
{
|
||||
int i, avail, ret;
|
||||
double needed;
|
||||
int i, avail, ret, req;
|
||||
double used;
|
||||
|
||||
if (player->god)
|
||||
return 1; /* Deity builds ex nihilo */
|
||||
|
@ -674,14 +674,15 @@ sector_can_build(struct sctstr *sp, short mat[], int work,
|
|||
|
||||
ret = 1;
|
||||
for (i = I_NONE + 1; i <= I_MAX; i++) {
|
||||
needed = mat[i] * (effic / 100.0);
|
||||
if (sp->sct_item[i] < needed) {
|
||||
pr("Not enough %s in %s (need %g more)\n",
|
||||
used = mat[i] * effic;
|
||||
req = (used + 99) / 100;
|
||||
if (sp->sct_item[i] < req) {
|
||||
pr("Not enough %s in %s (need %d more)\n",
|
||||
ichr[i].i_name, xyas(sp->sct_x, sp->sct_y, player->cnum),
|
||||
ceil(needed - sp->sct_item[i]));
|
||||
req - sp->sct_item[i]);
|
||||
ret = 0;
|
||||
}
|
||||
mat[i] = roundavg(needed);
|
||||
mat[i] = roundavg(used / 100.0);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue