]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/chance.c
chance: Avoid abuse of O_NONBLOCK for Windows
[empserver] / src / lib / gen / chance.c
index a92fa6fc19bdcb877e68dba50ece75b1910a4c0e..35ed982452627138547a0bf70be616a0693d8240 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2014, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                Ken Stevens, Steve McClure, Markus Armbruster
  *
  *  Empire is free software: you can redistribute it and/or modify
  *  chance.c: Roll dice
  *
  *  Known contributors to this file:
- *     Markus Armbruster, 2006-2012
+ *     Markus Armbruster, 2006-2021
  */
 
 #include <config.h>
 
 #include <fcntl.h>
 #include <math.h>
-#include <stdint.h>
 #include <stdlib.h>
 #include <sys/time.h>
 #include <unistd.h>
@@ -42,7 +41,7 @@
 #include "mt19937ar.h"
 
 /*
- * Return non-zero with probability D.
+ * Return non-zero with probability @d.
  */
 int
 chance(double d)
@@ -51,7 +50,7 @@ chance(double d)
 }
 
 /*
- * Return non-zero with probability PCT%.
+ * Return non-zero with probability @pct%.
  */
 int
 pct_chance(int pct)
@@ -73,8 +72,8 @@ round_up_to_pow2(unsigned val)
 }
 
 /*
- * Return a random number in [0..N-1].
- * N must be in [1..2^31-1].
+ * Return a random number in [0..@n-1].
+ * @n must be in [1..2^31-1].
  */
 int
 roll0(int n)
@@ -89,8 +88,8 @@ roll0(int n)
 }
 
 /*
- * Return a random number in [1..N].
- * N must be in [0..2^31-1].
+ * Return a random number in [1..@n].
+ * @n must be in [0..2^31-1].
  */
 int
 roll(int n)
@@ -99,8 +98,8 @@ roll(int n)
 }
 
 /*
- * Round VAL to nearest integer (on the average).
- * VAL's fractional part is chance to round up.
+ * Round @val to nearest integer (on the average).
+ * @val's fractional part is chance to round up.
  */
 int
 roundavg(double val)
@@ -110,7 +109,7 @@ roundavg(double val)
 }
 
 /*
- * Seed the pseudo-random number generator with SEED.
+ * Seed the pseudo-random number generator with @seed.
  * The sequence of pseudo-random numbers is repeatable by seeding it
  * with the same value.
  */
@@ -120,8 +119,12 @@ seed_prng(unsigned seed)
     init_genrand(seed);
 }
 
-static uint32_t
-djb_hash(uint32_t hash, void *buf, size_t sz)
+/*
+ * Note: this is DJB's hash function when unsigned is 32 bits and hash
+ * is initially 5381.
+ */
+static unsigned
+djb_hash(unsigned hash, void *buf, size_t sz)
 {
     unsigned char *bp;
 
@@ -137,11 +140,12 @@ djb_hash(uint32_t hash, void *buf, size_t sz)
 unsigned
 pick_seed(void)
 {
-    int fd;
-    uint32_t seed;
+    unsigned seed;
     int got_seed = 0;
     struct timeval tv;
     pid_t pid;
+#ifndef _WIN32
+    int fd;
 
     /*
      * Modern systems provide random number devices, but the details
@@ -161,6 +165,7 @@ pick_seed(void)
        got_seed = read(fd, &seed, sizeof(seed)) == sizeof(seed);
        close(fd);
     }
+#endif
 
     if (!got_seed) {
        /* Kernel didn't provide, fall back to hashing time and PID */