(main, init_server, emp_server.6): Add -R command line option to set

a seed for the random function.

(nightlybuild.sh): Add the -R 1 for the server.
(nightlybuild.sh): Switch to -R 1 for the fairland as 1 is safer.
Some systems might generate lousy randomness from a
zero seed.

(prng.patch): Not required anymore, -R 1 is used instead.

(main, emp_server.6): Rename -r and -R to -u and -U.  "-R" is now used for random seed.
This commit is contained in:
Ron Koenderink 2007-10-23 03:05:56 +00:00
parent c115ed1327
commit ec484e17a7
5 changed files with 31 additions and 475 deletions

View file

@ -46,7 +46,7 @@
/* src/server/main.c */
extern void shutdwn(int sig);
extern void init_server(void);
extern void init_server(unsigned);
extern void start_server(int);
extern void finish_server(void);

View file

@ -26,10 +26,13 @@ emp_server \- Empire server
.br
.B emp_server
[
.B \-r
.B \-u
|
.BI \-R " service-name"
.BI \-U " service-name"
]\}
[
.BI \-R " random-seed"
]
.SH DESCRIPTION
.B emp_server
is a daemon which listens to the Empire port for players connecting to
@ -61,15 +64,18 @@ Install as a Windows Service with the specified name.\}
Threading debug mode. Implies \fB-d\fR.
.if \nw \{\
.TP
.B \-r
Remove the Windows Service called "Empire Server".
.B \-u
Uninstall the Windows Service called "Empire Server".
.TP
.BI \-R " service-name"
Remove the Windows Service with the specified name.\}
.BI \-U " service-name"
Uninstall the Windows Service with the specified name.\}
.TP
.B \-s
Enable thread stack checking.
.TP
.B \-R " random-seed"
Set the seed for random function.
.TP
.B \-v
Print version information and exit.
.SH OPERANDS

View file

@ -259,7 +259,7 @@ do
echo "Running files and fairland"
echo y | ./files || warn "Error running files"
./fairland -R 0 10 30 >/dev/null || { warn "Error running fairland" ; break ; }
./fairland -R 1 10 30 >/dev/null || { warn "Error running fairland" ; break ; }
[ -s "newcap_script" ] || { warn "fairland did not produce newcap_script" ; break ; }
echo "Done (files & fairland)."
echo ""
@ -279,7 +279,7 @@ do
*)
echo "Starting server with -d in the background"
./emp_server -d &
./emp_server -R 1 -d &
PID="$!"
sleep 1
kill -0 "${PID}" || { warn "emp_server not running ?" ; break ; }

View file

@ -1,455 +0,0 @@
Index: empserver/include/damage.h
--- empserver/include/damage.h 16 Mar 2005 21:51:57 -0000 1.3
+++ empserver/include/damage.h 17 Mar 2005 10:01:13 -0000
@@ -39,10 +39,10 @@
#define DPERCENT_DAMAGE(x) ((double)(100.0 * (x) / ((x) + 100.0)))
#define DMINE_HITCHANCE(x) ((double) ( (x) / ((x)+20.0) ) )
#define DMINE_LHITCHANCE(x) ((double) ( (x) / ((x)+35.0) ) )
-#define MINE_DAMAGE() (22 + random()%21)
-#define MINE_LDAMAGE() (10 + random()%11)
+#define MINE_DAMAGE() (22 + emp_random()%21)
+#define MINE_LDAMAGE() (10 + emp_random()%11)
#define DTORP_HITCHANCE(range, vis) ((double)(0.9/((range)+1)+(((vis)<6)?(5-(vis))*0.03:0)))
-#define TORP_DAMAGE() (torpedo_damage + (random() % torpedo_damage) + \
- (random() % torpedo_damage))
+#define TORP_DAMAGE() (torpedo_damage + (emp_random() % torpedo_damage) + \
+ (emp_random() % torpedo_damage))
#endif /* _DAMAGE_H_ */
Index: empserver/include/prototypes.h
--- empserver/include/prototypes.h 13 Nov 2005 23:18:37 -0000 1.98
+++ empserver/include/prototypes.h 14 Nov 2005 16:34:10 -0000
@@ -397,6 +397,9 @@
extern s_char *splur(int n);
extern s_char *iesplur(int n);
extern char *plurize(char *buf, int max_len, int n);
+/* chance.c */
+extern void emp_srandom(unsigned int);
+extern long emp_random(void);
/* more in misc.h */
/*
Index: empserver/src/lib/commands/anti.c
--- empserver/src/lib/commands/anti.c 16 Mar 2005 21:56:00 -0000 1.10
+++ empserver/src/lib/commands/anti.c 17 Mar 2005 10:01:14 -0000
@@ -130,7 +130,7 @@
sect.sct_mobil = 0;
}
sect.sct_loyal = sect.sct_loyal * 0.5;
- n_cheleft = (random() % 4);
+ n_cheleft = (emp_random() % 4);
/* 75% chance some che will get left */
if (n_cheleft) {
/* Ok, now leave anywhere from 16% to 25% of the che */
Index: empserver/src/lib/commands/laun.c
--- empserver/src/lib/commands/laun.c 16 Mar 2005 21:56:01 -0000 1.10
+++ empserver/src/lib/commands/laun.c 17 Mar 2005 10:01:14 -0000
@@ -420,7 +420,7 @@
}
i = pp->pln_tech + pp->pln_effic;
if (chance(1.0 - (i / (i + 50.0)))) {
- dir = (random() % 6) + 1;
+ dir = (emp_random() % 6) + 1;
sx += diroff[dir][0];
sy += diroff[dir][1];
pr("Your trajectory was a little off.\n");
Index: empserver/src/lib/commands/mfir.c
--- empserver/src/lib/commands/mfir.c 27 Oct 2005 21:33:17 -0000 1.26
+++ empserver/src/lib/commands/mfir.c 14 Nov 2005 16:31:28 -0000
@@ -559,7 +559,7 @@
/* dam = (int)((double)dam / 2.0);*/
dam =
(int)((double)dam *
- (double)((double)(90 - (random() % 11)) /
+ (double)((double)(90 - (emp_random() % 11)) /
100.0));
if (dam < 0)
dam = 0;
Index: empserver/src/lib/commands/new.c
--- empserver/src/lib/commands/new.c 23 Oct 2005 20:52:46 -0000 1.24
+++ empserver/src/lib/commands/new.c 24 Oct 2005 15:30:24 -0000
@@ -102,8 +102,8 @@
} else {
for (i = 0; i < 300 && !player->aborted; i++) {
/* Both x and y should be either odd or even */
- x = (random() % WORLD_X) - (WORLD_X / 2);
- y = (((random() % WORLD_Y) - (WORLD_Y / 2)) & ~1) | (x & 1);
+ x = (emp_random() % WORLD_X) - (WORLD_X / 2);
+ y = (((emp_random() % WORLD_Y) - (WORLD_Y / 2)) & ~1) | (x & 1);
/*
* If either of the two potential
* sanctuary sectors are already
Index: empserver/src/lib/commands/news.c
--- empserver/src/lib/commands/news.c 16 Mar 2005 21:56:02 -0000 1.9
+++ empserver/src/lib/commands/news.c 17 Mar 2005 10:01:14 -0000
@@ -188,7 +188,7 @@
/*
* vary the order of the printing of "%d times "
*/
- if ((random() & 3) == 0 && np->nws_ntm > 1) {
+ if ((emp_random() & 3) == 0 && np->nws_ntm > 1) {
sprintf(cp, "%s times ", ptr);
cp += strlen(cp);
np->nws_ntm = 1;
@@ -198,7 +198,7 @@
*cp++ = ' ';
if (np->nws_vrb < 1 || np->nws_vrb > N_MAX_VERB)
np->nws_vrb = 0;
- sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS],
+ sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[emp_random() % NUM_RPTS],
cname(np->nws_vno));
cp += strlen(cp);
if (np->nws_ntm != 1) {
Index: empserver/src/lib/commands/sabo.c
--- empserver/src/lib/commands/sabo.c 16 Mar 2005 21:56:03 -0000 1.11
+++ empserver/src/lib/commands/sabo.c 17 Mar 2005 10:01:14 -0000
@@ -87,10 +87,10 @@
dam = landgun(3 * land.lnd_effic, 7);
if (sect.sct_item[I_SHELL] > 20)
dam += seagun(land.lnd_effic,
- random() % (sect.sct_item[I_SHELL] / 10));
+ emp_random() % (sect.sct_item[I_SHELL] / 10));
if (sect.sct_item[I_PETROL] > 100)
dam += seagun(land.lnd_effic,
- random() % (sect.sct_item[I_PETROL] / 50));
+ emp_random() % (sect.sct_item[I_PETROL] / 50));
pr("Explosion in %s causes %d damage.\n",
xyas(land.lnd_x, land.lnd_y, land.lnd_own), dam);
Index: empserver/src/lib/common/damage.c
--- empserver/src/lib/common/damage.c 16 Mar 2005 21:56:04 -0000 1.11
+++ empserver/src/lib/common/damage.c 17 Mar 2005 10:01:14 -0000
@@ -177,7 +177,7 @@
return 0;
tmp = amt * pct;
lost = tmp / 100;
- if ((random() % 100) < (tmp % 100))
+ if ((emp_random() % 100) < (tmp % 100))
lost++;
return amt - lost;
}
Index: empserver/src/lib/gen/chance.c
--- empserver/src/lib/gen/chance.c 16 Mar 2005 21:56:05 -0000 1.5
+++ empserver/src/lib/gen/chance.c 17 Mar 2005 10:01:14 -0000
@@ -31,14 +31,37 @@
*
*/
+#include <stdlib.h>
#include "gen.h"
+#ifndef RANDOM
+#ifdef NOSVIDPRNG
+#define RANDOM random
+#define SRANDOM srandom
+#else /* ! NOSVIDPRNG */
+#define RANDOM lrand48
+#define SRANDOM srand48
+#endif /* ! NOSVIDPRNG */
+#endif /* ! RANDOM */
+
+void
+emp_srandom(unsigned int n)
+{
+ SRANDOM(1);
+}
+
+long
+emp_random(void)
+{
+ return (RANDOM()); /* 5/28/91 by bailey@mcs.kent.edu */
+}
+
int
chance(double d)
{
double roll;
- roll = (random() & 0x7fff);
+ roll = (emp_random() & 0x7fff);
if (d > roll / 32768.0)
return 1;
@@ -48,7 +71,7 @@
int
roll(int n)
{
- return (random() % n) + 1;
+ return (emp_random() % n) + 1;
}
/*
Index: empserver/src/lib/subs/attsub.c
--- empserver/src/lib/subs/attsub.c 2 Oct 2005 14:35:18 -0000 1.30
+++ empserver/src/lib/subs/attsub.c 4 Oct 2005 14:57:37 -0000
@@ -1863,12 +1863,12 @@
* recalculate the odds every 8-50 casualties, not every cacsualty,
* since a single dead guy normally wouldn't cause a commander to
* rethink his strategies, but 50 dead guys might. */
- odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
+ odds = odds + (double)((double)((emp_random() % 11) - 5) / 100.0);
if (odds < 0.0)
odds = 0.1;
if (odds > 1.0)
odds = 1.0;
- recalctime = 8 + (random() % 43);
+ recalctime = 8 + (emp_random() % 43);
while (!success && ototal) {
if (chance(odds)) {
pr("!");
@@ -1884,9 +1884,9 @@
if (((a_cas + d_cas) % 70) == 69)
pr("\n");
if (recalctime-- <= 0) {
- recalctime = 8 + (random() % 43);
+ recalctime = 8 + (emp_random() % 43);
odds = att_calcodds(ototal, dtotal);
- odds = odds + (double)((double)((random() % 11) - 5) / 100.0);
+ odds = odds + (double)((double)((emp_random() % 11) - 5) / 100.0);
if (odds < 0.0)
odds = 0.1;
if (odds > 1.0)
Index: empserver/src/lib/subs/landgun.c
--- empserver/src/lib/subs/landgun.c 16 Mar 2005 21:57:53 -0000 1.5
+++ empserver/src/lib/subs/landgun.c 17 Mar 2005 10:01:14 -0000
@@ -40,7 +40,7 @@
double d;
double g = (double)min(guns, 7);
- d = ((double)(random() % 30) + 20.0) * ((double)g / 7.0);
+ d = ((double)(emp_random() % 30) + 20.0) * ((double)g / 7.0);
d *= ((double)effic);
d /= 100.0;
return d;
@@ -53,7 +53,7 @@
d = 0.0;
while (guns--)
- d += 10.0 + (double)(random() % 6);
+ d += 10.0 + (double)(emp_random() % 6);
d *= ((double)effic) * 0.01;
return d;
}
@@ -65,7 +65,7 @@
shots = min(shots, guns);
while (shots-- > 0)
- d += 5.0 + (double)(random() % 6);
+ d += 5.0 + (double)(emp_random() % 6);
d *= ((double)effic) * 0.01;
if (shells < ammo && ammo != 0)
d *= (double)((double)shells / (double)ammo);
Index: empserver/src/lib/subs/shpsub.c
--- empserver/src/lib/subs/shpsub.c 3 Oct 2005 14:47:59 -0000 1.22
+++ empserver/src/lib/subs/shpsub.c 4 Oct 2005 14:57:37 -0000
@@ -759,7 +759,7 @@
nreport(sp->shp_own, N_HIT_MINE, 0, 1);
- m = 22.0 + (double)(random() % 21);
+ m = 22.0 + (double)(emp_random() % 21);
if (mcp->m_flags & M_SWEEP)
m /= 2.0;
Index: empserver/src/lib/subs/takeover.c
--- empserver/src/lib/subs/takeover.c 16 Mar 2005 21:57:55 -0000 1.14
+++ empserver/src/lib/subs/takeover.c 17 Mar 2005 10:01:14 -0000
@@ -91,7 +91,7 @@
if (!(chance(LND_SPY_DETECT_CHANCE(lp->lnd_effic))))
continue;
}
- n = lp->lnd_effic - (30 + (random() % 100));
+ n = lp->lnd_effic - (30 + (emp_random() % 100));
if (n < 0)
n = 0;
lp->lnd_effic = n;
@@ -120,7 +120,7 @@
* 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) + ((emp_random() % 100) - 25);
che_count = 0;
if (n > 0 && sp->sct_own == sp->sct_oldown) {
che_count = (civ * n / 3000) + 5;
@@ -178,7 +178,7 @@
* 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 + (emp_random() % 100));
if (n < 0)
n = 0;
pp->pln_effic = n;
Index: empserver/src/lib/update/human.c
diff -u -r1.18 human.c
--- empserver/src/lib/update/human.c 2 Oct 2005 19:10:16 -0000 1.18
+++ empserver/src/lib/update/human.c 4 Oct 2005 14:57:37 -0000
@@ -116,12 +116,12 @@
nreport(sp->sct_own, N_DIE_FAMINE, 0, 1);
}
sp->sct_work = 0;
- sp->sct_loyal += (random() % 8) + 2;
+ sp->sct_loyal += (emp_random() % 8) + 2;
}
sctwork = 0;
} else {
if (sp->sct_work < 100)
- sctwork = sp->sct_work + 8 + (random() % 15);
+ sctwork = sp->sct_work + 8 + (emp_random() % 15);
if (sctwork > 100)
sctwork = 100;
if (!player->simulation)
Index: empserver/src/lib/update/plague.c
--- empserver/src/lib/update/plague.c 16 Mar 2005 21:57:57 -0000 1.15
+++ empserver/src/lib/update/plague.c 17 Mar 2005 10:01:15 -0000
@@ -190,7 +190,7 @@
}
if (*ptime <= 0) {
*pstage -= 1;
- *ptime = (etus / 2) + (random() % etus);
+ *ptime = (etus / 2) + (emp_random() % etus);
}
return stage;
}
Index: empserver/src/lib/update/populace.c
--- empserver/src/lib/update/populace.c 16 Mar 2005 21:57:57 -0000 1.9
+++ empserver/src/lib/update/populace.c 17 Mar 2005 10:01:15 -0000
@@ -85,7 +85,7 @@
n = roundavg(etu * 0.125);
if (n == 0)
n = 1;
- n = sp->sct_loyal + (random() % n) + 1;
+ n = sp->sct_loyal + (emp_random() % n) + 1;
if (n > 127)
n = 127;
sp->sct_loyal = n;
@@ -93,7 +93,7 @@
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 + (emp_random() % 15));
n = sp->sct_work - work_red;
if (n < 0)
n = 0;
Index: empserver/src/lib/update/revolt.c
--- empserver/src/lib/update/revolt.c 31 Jul 2005 13:55:49 -0000 1.16
+++ empserver/src/lib/update/revolt.c 11 Aug 2005 09:14:25 -0000
@@ -70,7 +70,7 @@
che_uw = 0;
che_civ = 0;
/* che due to civilian unrest */
- n = 10 - (random() % 20);
+ n = 10 - (emp_random() % 20);
che_civ = 3 + (civ * n / 500);
if (che_civ < 0)
che_civ = 0;
@@ -81,7 +81,7 @@
che += che_civ;
if (che < CHE_MAX) {
/* che due to uw unrest */
- n = 10 + (random() % 30);
+ n = 10 + (emp_random() % 30);
che_uw = 5 + (uw * n / 500);
if (che_uw > uw)
che_uw = uw;
@@ -252,7 +252,7 @@
}
if (mil > 0) {
/* military won. */
- n = sp->sct_loyal - (random() % 15);
+ n = sp->sct_loyal - (emp_random() % 15);
if (n < 0)
n = 0;
sp->sct_loyal = n;
@@ -271,7 +271,7 @@
* Note this disrupts work in the sector.
*/
n = 0;
- n = (random() % 10) + (random() % che);
+ n = (emp_random() % 10) + (emp_random() % che);
if (n > 100)
n = 100;
tmp = sp->sct_work - n;
@@ -351,14 +351,14 @@
/* loyalty drops during recruitment efforts */
n = sp->sct_loyal;
if (n < 30)
- n += (random() % 5) + 1;
+ n += (emp_random() % 5) + 1;
else if (n < 70)
- n += (random() % 10) + 4;
+ n += (emp_random() % 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 * (emp_random() % 3) / 200;
n /= hap_fact(tnat, getnatp(sp->sct_oldown));
if (n + che > CHE_MAX)
n = CHE_MAX - che;
@@ -366,7 +366,7 @@
civ -= n;
sp->sct_item[I_CIVIL] = civ;
}
- n = uw * (random() % 3) / 200;
+ n = uw * (emp_random() % 3) / 200;
if (n + che > CHE_MAX)
n = CHE_MAX - che;
che += n;
@@ -399,7 +399,7 @@
val = roundintby(val, 10);
/* inject a modicum of indeterminism; also
* avoids che preferring certain directions */
- val += random() % 10 - 5;
+ val += emp_random() % 10 - 5;
if (val >= min_mil)
continue;
nicest_sp = nsp;
Index: empserver/src/server/main.c
--- empserver/src/server/main.c 13 Nov 2005 16:39:26 -0000 1.79
+++ empserver/src/server/main.c 14 Nov 2005 16:33:18 -0000
@@ -258,7 +258,7 @@
void
init_server(void)
{
- srandom(time(NULL));
+ emp_srandom(time(NULL));
#if defined(_WIN32)
loc_NTInit();
#endif
Index: empserver/src/server/update.c
--- empserver/src/server/update.c 16 Mar 2005 21:57:58 -0000 1.18
+++ empserver/src/server/update.c 17 Mar 2005 10:01:15 -0000
@@ -70,7 +70,7 @@
time(&now);
next_update_time(&now, &update_time, &delta);
if (update_window > 0) {
- wind = (random() % update_window);
+ wind = (emp_random() % update_window);
update_time += wind;
delta += wind;
}
Index: empserver/src/util/fairland.c
--- empserver/src/util/fairland.c 16 Mar 2005 22:03:16 -0000 1.25
+++ empserver/src/util/fairland.c 17 Mar 2005 10:01:15 -0000
@@ -126,7 +126,7 @@
#if !defined(_WIN32)
#define max(a,b) (a>b?a:b)
#endif
-#define rnd(x) (random() % (x))
+#define rnd(x) (lrand48() % (x))
int secs; /* number of sectors grown */
int ctot; /* total number of continents and islands grown */
@@ -229,7 +229,7 @@
exit(1);
}
}
- srandom(rnd_seed);
+ srand48(rnd_seed);
if (emp_config(config_file))
exit(1);

View file

@ -106,10 +106,11 @@ print_usage(char *program_name)
#endif
" -p threading debug mode, implies -d\n"
#ifdef _WIN32
" -r remove service `%s'\n"
" -R NAME remove service NAME\n"
" -u uninstall service `%s'\n"
" -U NAME uninstall service NAME\n"
#endif
" -s enable stack checking\n"
" -R RANDOM-SEED random seed\n"
" -v display version information and exit\n",
program_name, dflt_econfig
#ifdef _WIN32
@ -130,13 +131,14 @@ main(int argc, char **argv)
#endif
char *config_file = NULL;
int op, sig;
unsigned seed = time(NULL);
#ifdef _WIN32
# define XOPTS "iI:rR:"
# define XOPTS "iI:uU:"
#else
# define XOPTS
#endif
while ((op = getopt(argc, argv, "de:hpsv" XOPTS)) != EOF) {
while ((op = getopt(argc, argv, "de:hpsR:v" XOPTS)) != EOF) {
switch (op) {
case 'p':
flags |= EMPTH_PRINT;
@ -155,16 +157,19 @@ main(int argc, char **argv)
case 'i':
install_service_set++;
break;
case 'R':
case 'U':
service_name = optarg;
/* fall through */
case 'r':
case 'u':
remove_service_set++;
break;
#endif /* _WIN32 */
case 's':
flags |= EMPTH_STACKCHECK;
break;
case 'R':
seed = strtoul(optarg, NULL, 10);
break;
case 'v':
printf("%s\n\n%s", version, legal);
return EXIT_SUCCESS;
@ -181,7 +186,7 @@ main(int argc, char **argv)
if ((debug || flags || config_file != NULL) &&
remove_service_set) {
fprintf(stderr, "Can't use -p, -s, -d or -e with either "
"-r or -R options\n");
"-u or -U options\n");
exit(EXIT_FAILURE);
}
if ((debug || flags) && install_service_set) {
@ -190,7 +195,7 @@ main(int argc, char **argv)
exit(EXIT_FAILURE);
}
if (install_service_set && remove_service_set) {
fprintf(stderr, "Can't use both -r or -R and -i or -I "
fprintf(stderr, "Can't use both -u or -U and -i or -I "
"options\n");
exit(EXIT_FAILURE);
}
@ -237,7 +242,7 @@ main(int argc, char **argv)
return install_service(program_name, service_name, config_file);
#endif /* _WIN32 */
init_server();
init_server(seed);
#if defined(_WIN32)
if (daemonize != 0) {
@ -295,9 +300,9 @@ main(int argc, char **argv)
* Initialize for serving, acquire resources.
*/
void
init_server(void)
init_server(unsigned seed)
{
srandom(time(NULL));
srandom(seed);
#if defined(_WIN32)
loc_NTInit();
#endif