#define CHANCE_H
extern int chance(double);
+extern int roll0(int);
extern int roll(int);
extern int roundavg(double);
+extern void seed_prng(unsigned);
#endif
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1998
+ * Markus Armbruster, 2006-2012
*/
#ifndef DAMAGE_H
#define DPERCENT_DAMAGE(x) (100.0 * (x) / ((x) + 100.0))
#define DMINE_HITCHANCE(x) ((x) / ((x) + 20.0))
#define DMINE_LHITCHANCE(x) ((x) / ((x) + 35.0))
-#define MINE_DAMAGE() (22 + random()%21)
-#define MINE_LDAMAGE() (11 + random()%20)
+#define MINE_DAMAGE() (22 + roll0(21))
+#define MINE_LDAMAGE() (11 + roll0(20))
#define DTORP_HITCHANCE(range, vis) \
(0.9 / ((range)+1) + (((vis) < 6) ? (5-(vis)) * 0.03 : 0.0))
-#define TORP_DAMAGE() (torpedo_damage + (random() % torpedo_damage) + \
- (random() % torpedo_damage))
+#define TORP_DAMAGE() (torpedo_damage + roll0(torpedo_damage) + \
+ roll0(torpedo_damage))
#endif
* Known contributors to this file:
* Pat Loney, 1992
* Steve McClure, 1997
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
sect.sct_mobil = 0;
}
sect.sct_loyal = sect.sct_loyal * 0.5;
- n_cheleft = (random() % 4);
+ n_cheleft = roll0(4);
/* 75% chance some che will get left */
if (n_cheleft) {
/* Ok, now leave anywhere from 16% to 25% of the che */
}
i = pp->pln_tech + pp->pln_effic;
if (chance(1.0 - (i / (i + 50.0)))) {
- dir = DIR_FIRST + random() % 6;
+ dir = DIR_FIRST + roll0(6);
sx = xnorm(sx + diroff[dir][0]);
sy = ynorm(sy + diroff[dir][1]);
pr("Your trajectory was a little off.\n");
* news.c: Show current Empire news
*
* Known contributors to this file:
- *
+ * Markus Armbruster, 2006-2012
*/
#include <config.h>
+#include "chance.h"
#include "commands.h"
#include "news.h"
#include "optlist.h"
/*
* vary the order of the printing of "%d times "
*/
- if ((random() & 3) == 0 && np->nws_ntm > 1) {
+ if (roll0(4) == 0 && np->nws_ntm > 1) {
sprintf(cp, "%s times ", ptr);
cp += strlen(cp);
np->nws_ntm = 1;
strcpy(cp, cname(np->nws_ano));
cp += strlen(cp);
*cp++ = ' ';
- sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS],
+ sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[roll0(NUM_RPTS)],
cname(np->nws_vno));
cp += strlen(cp);
if (np->nws_ntm != 1) {
*
* ---
*
- * chance.c: return 1 if "roll" is under the chance.
+ * chance.c: Roll dice
*
* Known contributors to this file:
*
#include <stdlib.h>
#include "chance.h"
+/*
+ * Return non-zero with probability D.
+ */
int
chance(double d)
{
return d > (random() % 32768) / 32768.0;
}
+/*
+ * Return a random number in [0..N-1].
+ */
+int
+roll0(int n)
+{
+ return random() % n;
+}
+
+/*
+ * Return a random number in [1..N].
+ */
int
roll(int n)
{
}
/*
- * round value to nearest int (on the average). E.g. rounds up
- * with a chance proportional to the size of the fractional part.
+ * Round VAL to nearest integer (on the average).
+ * VAL's fractional part is chance to round up.
*/
int
roundavg(double val)
double flr = floor(val);
return (int)(flr + chance(val - flr));
}
+
+/*
+ * Seed the pseudo-random number generator with SEED.
+ * The sequence of pseudo-random numbers is repeatable by seeding it
+ * with the same value.
+ */
+void
+seed_prng(unsigned seed)
+{
+ srandom(seed);
+}
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1996-2000
- * Markus Armbruster, 2006-2011
+ * Markus Armbruster, 2006-2012
*/
#include <config.h>
* since a single dead guy normally wouldn't cause a commander to
* rethink his strategies, but 50 dead guys might.
*/
- odds += (random() % 11 - 5) / 100.0;
+ odds += (roll0(11) - 5) / 100.0;
if (odds < 0.0)
odds = 0.1;
if (odds > 1.0)
odds = 1.0;
- recalctime = 8 + (random() % 43);
+ recalctime = 8 + roll0(43);
while (!success && ototal) {
if (chance(odds)) {
pr("!");
if (((a_cas + d_cas) % 70) == 69)
pr("\n");
if (recalctime-- <= 0) {
- recalctime = 8 + (random() % 43);
+ recalctime = 8 + roll0(43);
odds = att_calcodds(ototal, dtotal);
- odds += (random() % 11 - 5) / 100.0;
+ odds += (roll0(11) - 5) / 100.0;
if (odds < 0.0)
odds = 0.1;
if (odds > 1.0)
* Known contributors to this file:
* Dave Pare, 1989
* Steve McClure, 1997
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
return 0;
tmp = amt * pct;
lost = tmp / 100;
- if (random() % 100 < tmp % 100)
+ if (roll0(100) < tmp % 100)
lost++;
return amt - lost;
}
* landgun.c: Fire weapons
*
* Known contributors to this file:
- * Markus Armbruster, 2006-2009
+ * Markus Armbruster, 2006-2012
*/
#include <config.h>
double d;
double g = MIN(guns, 7);
- d = (random() % 30 + 20.0) * (g / 7.0);
+ d = (roll0(30) + 20.0) * (g / 7.0);
d *= effic / 100.0;
return d;
}
d = 0.0;
while (guns--)
- d += 10.0 + random() % 6;
+ d += 10.0 + roll0(6);
d *= effic * 0.01;
return d;
}
d = 0.0;
while (guns--)
- d += 5.0 + random() % 6;
+ d += 5.0 + roll0(6);
d *= effic * 0.01;
return d;
}
lp->lnd_item[I_SHELL]--;
dam = fortgun(3 * lp->lnd_effic, 7);
if (item[I_SHELL] > 20)
- dam += seagun(lp->lnd_effic, random() % (item[I_SHELL] / 10));
+ dam += seagun(lp->lnd_effic, roll0(item[I_SHELL] / 10));
if (item[I_PETROL] > 100)
- dam += seagun(lp->lnd_effic, random() % (item[I_PETROL] / 50));
+ dam += seagun(lp->lnd_effic, roll0(item[I_PETROL] / 50));
return dam;
}
* Known contributors to this file:
* Dave Pare, 1986
* Steve McClure, 1996-2000
- * Markus Armbruster, 2007-2010
+ * Markus Armbruster, 2007-2012
*/
#include <config.h>
if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
continue;
}
- n = lp->lnd_effic - (30 + (random() % 100));
+ n = lp->lnd_effic - (30 + roll0(100));
if (n < 0)
n = 0;
lp->lnd_effic = n;
* how spunky are these guys?
* n: random number from -25:75 + (50 - loyalty)
*/
- n = (50 - sp->sct_loyal) + ((random() % 100) - 25);
+ n = (50 - sp->sct_loyal) + (roll0(100) - 25);
if (n > 0 && sp->sct_own == sp->sct_oldown) {
che_count = (civ * n / 3000) + 5;
if (che_count * 2 > civ)
* XXX If this was done right, planes could escape,
* flying to a nearby friendly airport.
*/
- n = pp->pln_effic - (30 + (random() % 100));
+ n = pp->pln_effic - (30 + roll0(100));
if (n < 0)
n = 0;
pp->pln_effic = n;
* Known contributors to this file:
* Dave Pare, 1986
* Steve McClure, 1996
- * Markus Armbruster, 2004-2010
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
}
sp->sct_work = 0;
- sp->sct_loyal += (random() % 8) + 2;
+ sp->sct_loyal += roll0(8) + 2;
}
sctwork = 0;
} else {
if (sp->sct_work < 100)
- sctwork = sp->sct_work + 8 + (random() % 15);
+ sctwork = sp->sct_work + 8 + roll0(15);
if (sctwork > 100)
sctwork = 100;
if (!player->simulation)
*
* Known contributors to this file:
* Steve McClure, 1998-2000
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
}
if (*ptime <= 0) {
*pstage -= 1;
- *ptime = etus / 2 + random() % etus;
+ *ptime = etus / 2 + roll0(etus);
}
return stage;
}
*
* Known contributors to this file:
* Dave Pare, 1986
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
n = roundavg(etu * 0.125);
if (n == 0)
n = 1;
- n = sp->sct_loyal + (random() % n) + 1;
+ n = sp->sct_loyal + roll0(n) + 1;
if (n > 127)
n = 127;
sp->sct_loyal = n;
if (sp->sct_loyal > 65 && mil < civ / 20) {
int work_red;
- work_red = sp->sct_loyal - (50 + (random() % 15));
+ work_red = sp->sct_loyal - (50 + roll0(15));
n = sp->sct_work - work_red;
if (n < 0)
n = 0;
* Known contributors to this file:
* Dave Pare, 1986
* Steve McClure, 1997-2000
- * Markus Armbruster, 2004-2011
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
return;
che_uw = 0;
/* che due to civilian unrest */
- n = 10 - (random() % 20);
+ n = 10 - roll0(20);
che_civ = 3 + (civ * n / 500);
if (che_civ < 0)
che_civ = 0;
che += che_civ;
if (che < CHE_MAX) {
/* che due to uw unrest */
- n = 10 + (random() % 30);
+ n = 10 + roll0(30);
che_uw = 5 + (uw * n / 500);
if (che_uw > uw)
che_uw = uw;
}
if (mil > 0) {
/* military won. */
- n = sp->sct_loyal - (random() % 15);
+ n = sp->sct_loyal - roll0(15);
if (n < 0)
n = 0;
sp->sct_loyal = n;
* guerrillas have to resort to blowing things up.
* Note this disrupts work in the sector.
*/
- n = (random() % 10) + (random() % che);
+ n = roll0(10) + roll0(che);
if (n > 100)
n = 100;
tmp = sp->sct_work - n;
/* loyalty drops during recruitment efforts */
n = sp->sct_loyal;
if (n < 30)
- n += (random() % 5) + 1;
+ n += roll0(5) + 1;
else if (n < 70)
- n += (random() % 10) + 4;
+ n += roll0(10) + 4;
if (n > 127)
n = 127;
sp->sct_loyal = n;
if (sp->sct_oldown != sp->sct_own || n > 100) {
- n = civ * (random() % 3) / 200;
+ n = civ * roll0(3) / 200;
n /= hap_fact(tnat, getnatp(sp->sct_oldown));
if (n + che > CHE_MAX)
n = CHE_MAX - che;
civ -= n;
sp->sct_item[I_CIVIL] = civ;
}
- n = uw * (random() % 3) / 200;
+ n = uw * roll0(3) / 200;
if (n + che > CHE_MAX)
n = CHE_MAX - che;
che += n;
val = roundintby(val, 10);
/* inject a modicum of indeterminism; also
* avoids che preferring certain directions */
- val += random() % 10 - 5;
+ val += roll0(10) - 5;
if (val >= min_mil)
continue;
nicest_sp = nsp;
#include "sys/socket.h"
#endif
+#include "chance.h"
#include "empio.h"
#include "empthread.h"
#include "file.h"
static void
init_server(unsigned seed, int force_bad_state)
{
- srandom(seed);
+ seed_prng(seed);
#if defined(_WIN32)
loc_NTInit();
#endif
* Dave Pare, 1994
* Steve McClure, 1996
* Ron Koenderink, 2005
- * Markus Armbruster, 2007-2010
+ * Markus Armbruster, 2007-2012
*/
#include <config.h>
#include <sys/wait.h>
#endif
#include <time.h>
+#include "chance.h"
#include "empthread.h"
#include "file.h"
#include "game.h"
next_update = update_time[0];
if (next_update) {
if (update_window > 0)
- next_update += random() % update_window;
+ next_update += roll0(update_window);
logerror("Next update at %s", ctime(&next_update));
/* sleep until update is scheduled to go off */
empth_sleep(next_update);
* Known contributors to this file:
* Ken Stevens, 1995
* Steve McClure, 1998
+ * Markus Armbruster, 2004-2012
*/
#include <config.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
+#include "chance.h"
#include "file.h"
#include "optlist.h"
#include "prototypes.h"
#define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
#define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
-#define rnd(x) (random() % (x))
+#define rnd(x) roll0((x))
static int secs; /* number of sectors grown */
static int ctot; /* total number of continents and islands grown */
}
parse_args(argc - optind, argv + optind);
- srandom(rnd_seed);
+ seed_prng(rnd_seed);
empfile_init();
if (emp_config(config_file) < 0)
exit(1);