(rnd_seed, main, parse_args, allocate_memory): New option -R to set

the PRNG seed.  Print the seed.  From Marc Olzheim.
This commit is contained in:
Markus Armbruster 2004-03-23 14:54:48 +00:00
parent 0c47be1cb5
commit 43c511e8ad
2 changed files with 26 additions and 10 deletions

View file

@ -13,6 +13,9 @@ fairland \- Empire land generator
[ [
.BI \-e\ configfile .BI \-e\ configfile
] ]
[
.BI \-R\ seed
]
.IR nc .IR nc
.IR sc .IR sc
[ [
@ -77,6 +80,14 @@ to use the game configuration specified in
.I configfile .I configfile
(by default, the file "econfig" in the data directory is used) (by default, the file "econfig" in the data directory is used)
.TP .TP
.BI \-R\ seed
Use
.I seed
as seed for the pseudo-random number generator. This can be used to reproduce
a previously created world. (by default the current time and process id of
the server are used as seed.) The seed used to generate a world is printed
along with the map.
.TP
.IR nc .IR nc
number of continents number of continents
.TP .TP

View file

@ -69,6 +69,7 @@ static int quiet = 0;
#endif /* aix or linux */ #endif /* aix or linux */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include "var.h" #include "var.h"
#include "misc.h" #include "misc.h"
@ -127,6 +128,7 @@ int ctot; /* total number of continents and islands grown */
int *isecs; /* array of how large each island is */ int *isecs; /* array of how large each island is */
int nc, sc, di, sp, pm, ni, is, id; /* the 8 arguments to this program */ int nc, sc, di, sp, pm, ni, is, id; /* the 8 arguments to this program */
unsigned long rnd_seed; /* optional seed can be passed as an argument */
int *capx, *capy; /* location of the nc capitals */ int *capx, *capy; /* location of the nc capitals */
int *mc, mcc; /* array and counter used for stability int *mc, mcc; /* array and counter used for stability
check when perturbing */ check when perturbing */
@ -189,8 +191,11 @@ main(int argc, char *argv[])
char tbuf[512]; char tbuf[512];
int i = 0; int i = 0;
rnd_seed = time(NULL);
#if !defined(_WIN32) #if !defined(_WIN32)
while ((opt = getopt(argc, argv, "ae:ioqs:")) != EOF) { rnd_seed += getpid();
while ((opt = getopt(argc, argv, "ae:ioqs:R:")) != EOF) {
switch (opt) { switch (opt) {
case 'a': case 'a':
AIRPORT_MARKER = 1; AIRPORT_MARKER = 1;
@ -210,9 +215,13 @@ main(int argc, char *argv[])
case 's': case 's':
outfile = optarg; outfile = optarg;
break; break;
case 'R':
rnd_seed = strtoul(optarg, NULL, 10);
break;
} }
} }
#endif #endif
srandom(rnd_seed);
if (config_file == NULL) { if (config_file == NULL) {
sprintf(tbuf, "%s/econfig", datadir); sprintf(tbuf, "%s/econfig", datadir);
config_file = tbuf; config_file = tbuf;
@ -233,6 +242,8 @@ main(int argc, char *argv[])
if (!quiet && i) if (!quiet && i)
printf("\ntry #%d (out of %d)...", i + 1, NUMTRIES); printf("\ntry #%d (out of %d)...", i + 1, NUMTRIES);
qprint("\n\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n"); qprint("\n\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
if (!quiet)
printf("seed is %lu\n", rnd_seed);
qprint("placing capitals...\n"); qprint("placing capitals...\n");
if (!drift()) if (!drift())
qprint("fairland: unstable drift -- try increasisg DRIFT_MAX\n"); qprint("fairland: unstable drift -- try increasisg DRIFT_MAX\n");
@ -298,10 +309,11 @@ parse_args(int argc, char *argv[])
{ {
if (argc < 2 || argc > 8) { if (argc < 2 || argc > 8) {
puts("fairland syntax:\n"); puts("fairland syntax:\n");
puts("fairland [-e config] [-aiqo] [-s script] <nc> <sc> [<ni>] [<is>] [<sp>] [<pm>] [<di>] [<id>]"); puts("fairland [-e config] [-aiqo] [-s script] [-R seed] <nc> <sc> [<ni>] [<is>] [<sp>] [<pm>] [<di>] [<id>]");
puts("-q = quiet, -o = no ore produced"); puts("-q = quiet, -o = no ore produced");
puts("-a = Airport marker for continents, -i = islands not distinct"); puts("-a = Airport marker for continents, -i = islands not distinct");
printf("-e = read config file, -s = name of script (default %s)\n", puts("-R = seed to use for random, -e = read config file");
printf("-s = name of script (default %s)\n",
outfile); outfile);
puts("nc = number of continents [MANDATORY]"); puts("nc = number of continents [MANDATORY]");
puts("sc = continent size [MANDATORY]"); puts("sc = continent size [MANDATORY]");
@ -399,7 +411,6 @@ static int
allocate_memory(void) allocate_memory(void)
{ {
int i; int i;
time_t now;
#if !defined(_WIN32) #if !defined(_WIN32)
the_file = the_file =
@ -418,12 +429,6 @@ allocate_memory(void)
sects = (struct sctstr **)calloc(YSIZE, sizeof(struct sctstr *)); sects = (struct sctstr **)calloc(YSIZE, sizeof(struct sctstr *));
for (i = 0; i < YSIZE; i++) for (i = 0; i < YSIZE; i++)
sects[i] = &sectsbuf[XSIZE * i]; sects[i] = &sectsbuf[XSIZE * i];
time(&now);
#if !defined(_WIN32)
srandom(now + getpid());
#else
srandom(now);
#endif
capx = (int *)calloc(nc, sizeof(int)); capx = (int *)calloc(nc, sizeof(int));
capy = (int *)calloc(nc, sizeof(int)); capy = (int *)calloc(nc, sizeof(int));
vector = (int *)calloc(WORLD_X + WORLD_Y, sizeof(int)); vector = (int *)calloc(WORLD_X + WORLD_Y, sizeof(int));