Make damage() use roundavg()

Turns damage() into a one-liner.

damage() now uses random() % 32768 in chance() instead of random() %
100 inline, therefore can round differently for the same pseudo-random
number.  Update expected smoke test results accordingly.

Aside: "random() % n" distributes evenly only when n is a power of
two.  100 isn't.  However, because random() yields at least 31 bits,
and 100 is so much smaller than 2^31, the error is vanishingly small.
This commit is contained in:
Markus Armbruster 2012-08-14 21:51:22 +02:00
parent 5bf310e6b0
commit c53158eee0
2 changed files with 5 additions and 12 deletions

View file

@ -163,16 +163,9 @@ nukedamage(struct nchrstr *ncp, int range, int airburst)
int int
damage(int amt, int pct) damage(int amt, int pct)
{ {
int tmp;
int lost;
if (amt <= 0) if (amt <= 0)
return 0; return 0;
tmp = amt * pct; return amt - roundavg(amt * (pct / 100.0));
lost = tmp / 100;
if (roll0(100) < tmp % 100)
lost++;
return amt - lost;
} }
/* asymptotic damage to commodities, efficiency, and sectors */ /* asymptotic damage to commodities, efficiency, and sectors */

View file

@ -11969,7 +11969,7 @@ CENSUS del dst
5,-1 h 100% 127 .. .. 740 5 0 0 100% 188 0 1 5,-1 h 100% 127 .. .. 740 5 0 0 100% 188 0 1
7,-1 = 100% 127 .. .. 100 0 0 0 100% 59 0 1 7,-1 = 100% 127 .. .. 100 0 0 0 100% 59 0 1
9,-1 = 100% 127 .. .. 100 0 0 0 100% 59 0 1 9,-1 = 100% 127 .. .. 100 0 0 0 100% 59 0 1
11,-1 ) 91% 111 .. 0. 910 25 1 0 100% 552 0 1 11,-1 ) 91% 112 .. 0. 910 25 1 0 100% 551 0 1
-2,0 j 100% 127 .. .. 1000 0 0 0 100% 0 0 1 -2,0 j 100% 127 .. .. 1000 0 0 0 100% 0 0 1
0,0 m 100% 127 .. .. 1000 0 0 0 100% 0 0 0,0 m 100% 127 .. .. 1000 0 0 0 100% 0 0
2,0 m 100% 127 .. 5. 999 0 579 0 100% 0 0 2,0 m 100% 127 .. 5. 999 0 579 0 100% 0 0
@ -12220,7 +12220,7 @@ You have two new telegrams waiting ...
[0:637] Command : Thu Jan 1 00:00:00 1970 [0:637] Command : Thu Jan 1 00:00:00 1970
CENSUS del dst CENSUS del dst
sect eff prd mob uf uf old civ mil uw food work avail ter fall coa sect eff prd mob uf uf old civ mil uw food work avail ter fall coa
-6,-4 f 94% 119 .. .. 722 52 0 0 100% 457 0 1 -6,-4 f 94% 119 .. .. 722 52 0 0 100% 458 0 1
-4,-4 -o 95% 121 .. .. 730 0 0 0 100% 476 0 1 -4,-4 -o 95% 121 .. .. 730 0 0 0 100% 476 0 1
0,-4 k 100% 127 .. .. 769 0 0 0 100% 1 0 1 0,-4 k 100% 127 .. .. 769 0 0 0 100% 1 0 1
-5,-3 o 100% 127 .. .. 769 0 0 0 100% 0 0 1 -5,-3 o 100% 127 .. .. 769 0 0 0 100% 0 0 1
@ -12391,7 +12391,7 @@ own sect eff prd mob uf uf old civ mil uw food work avail fall coa
1 7,-15 % 100% 118 .. .. 770 0 0 0 100% 343 0 1 1 7,-15 % 100% 118 .. .. 770 0 0 0 100% 343 0 1
1 9,-15 t 100% 123 .. .. 770 0 0 0 100% 359 0 1 1 9,-15 t 100% 123 .. .. 770 0 0 0 100% 359 0 1
1 11,-15 l 100% 122 .. .. 769 0 0 0 100% 149 0 1 1 11,-15 l 100% 122 .. .. 769 0 0 0 100% 149 0 1
8 17,-15 f 94% 119 .. .. 722 52 0 0 100% 457 0 1 8 17,-15 f 94% 119 .. .. 722 52 0 0 100% 458 0 1
8 19,-15 -o 95% 121 .. .. 730 0 0 0 100% 476 0 1 8 19,-15 -o 95% 121 .. .. 730 0 0 0 100% 476 0 1
8 23,-15 k 100% 127 .. .. 769 0 0 0 100% 1 0 1 8 23,-15 k 100% 127 .. .. 769 0 0 0 100% 1 0 1
1 4,-14 b 100% 127 .. .. 1000 0 0 0 100% 299 0 1 1 4,-14 b 100% 127 .. .. 1000 0 0 0 100% 299 0 1
@ -12400,7 +12400,7 @@ own sect eff prd mob uf uf old civ mil uw food work avail fall coa
1 10,-14 h 100% 127 .. .. 740 5 0 0 100% 188 0 1 1 10,-14 h 100% 127 .. .. 740 5 0 0 100% 188 0 1
1 12,-14 = 100% 127 .. .. 100 0 0 0 100% 59 0 1 1 12,-14 = 100% 127 .. .. 100 0 0 0 100% 59 0 1
1 14,-14 = 100% 127 .. .. 100 0 0 0 100% 59 0 1 1 14,-14 = 100% 127 .. .. 100 0 0 0 100% 59 0 1
1 16,-14 ) 91% 111 .. 0. 910 25 1 0 100% 552 0 1 1 16,-14 ) 91% 112 .. 0. 910 25 1 0 100% 551 0 1
8 18,-14 o 100% 127 .. .. 769 0 0 0 100% 0 0 1 8 18,-14 o 100% 127 .. .. 769 0 0 0 100% 0 0 1
8 20,-14 - 100% 127 .. .. 769 0 0 0 100% 500 0 1 8 20,-14 - 100% 127 .. .. 769 0 0 0 100% 500 0 1
8 22,-14 ! 100% 127 .. .. 769 0 0 0 100% 474 0 1 8 22,-14 ! 100% 127 .. .. 769 0 0 0 100% 474 0 1