]> git.pond.sub.org Git - empserver/commitdiff
Make damage() use roundavg()
authorMarkus Armbruster <armbru@pond.sub.org>
Tue, 14 Aug 2012 19:51:22 +0000 (21:51 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Wed, 8 May 2013 04:55:20 +0000 (06:55 +0200)
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.

src/lib/subs/damage.c
tests/smoke/smoke.out

index 0005d7dbf2990230eb61ac8522becb78839c3722..a70390448fb5e97d3e2acf6d0182789381872cfe 100644 (file)
@@ -163,16 +163,9 @@ nukedamage(struct nchrstr *ncp, int range, int airburst)
 int
 damage(int amt, int pct)
 {
-    int tmp;
-    int lost;
-
     if (amt <= 0)
        return 0;
-    tmp = amt * pct;
-    lost = tmp / 100;
-    if (roll0(100) < tmp % 100)
-       lost++;
-    return amt - lost;
+    return amt - roundavg(amt * (pct / 100.0));
 }
 
 /* asymptotic damage to commodities, efficiency, and sectors */
index c0fd87ac18e46271aed7debffdd76a952168a6e4..e9b2b5f907c1896c694fb99017f42c267da42182 100644 (file)
@@ -11969,7 +11969,7 @@ CENSUS                   del dst
    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
    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
    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
@@ -12220,7 +12220,7 @@ You have two new telegrams waiting ...
 [0:637] Command : Thu Jan  1 00:00:00 1970
 CENSUS                   del dst
   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
    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
@@ -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    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
-  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   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
@@ -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   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   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   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