]> git.pond.sub.org Git - empserver/commitdiff
(rnd_seed, main, parse_args, allocate_memory): New option -R to set
authorMarkus Armbruster <armbru@pond.sub.org>
Tue, 23 Mar 2004 14:54:48 +0000 (14:54 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Tue, 23 Mar 2004 14:54:48 +0000 (14:54 +0000)
the PRNG seed.  Print the seed.  From Marc Olzheim.

man/fairland.1
src/util/fairland.c

index 081ebd7e6062cddd8260f3e4fa3bedfaf65ec9bc..503373e92fd33b4aba21c9eaec9fe71310c39508 100644 (file)
@@ -13,6 +13,9 @@ fairland \- Empire land generator
 [
 .BI \-e\ configfile
 ]
 [
 .BI \-e\ configfile
 ]
+[
+.BI \-R\ seed
+]
 .IR nc
 .IR sc
 [
 .IR nc
 .IR sc
 [
@@ -77,6 +80,14 @@ to use the game configuration specified in
 .I configfile
 (by default, the file "econfig" in the data directory is used)
 .TP
 .I configfile
 (by default, the file "econfig" in the data directory is used)
 .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 
 number of continents
 .TP
 .IR nc 
 number of continents
 .TP
index 1ec4c9dae88845d52cb62667707e590feaab1372..f887f42ca360b5f36bba2b61c50aa360c903e297 100644 (file)
@@ -69,6 +69,7 @@ static int quiet = 0;
 #endif /* aix or linux */
 
 #include <stdio.h>
 #endif /* aix or linux */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <fcntl.h>
 #include "var.h"
 #include "misc.h"
 #include <fcntl.h>
 #include "var.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 nc, sc, di, sp, pm, ni, is, id;    /* the 8 arguments to this program */
 int *isecs;                    /* array of how large each island is */
 
 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 *mc, mcc;                  /* array and counter used for stability
                                   check when perturbing */
 int *capx, *capy;              /* location of the nc capitals */
 int *mc, mcc;                  /* array and counter used for stability
                                   check when perturbing */
@@ -189,8 +191,11 @@ main(int argc, char *argv[])
     char tbuf[512];
     int i = 0;
 
     char tbuf[512];
     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) {
        case 'a':
            AIRPORT_MARKER = 1;
        switch (opt) {
        case 'a':
            AIRPORT_MARKER = 1;
@@ -210,9 +215,13 @@ main(int argc, char *argv[])
        case 's':
            outfile = optarg;
            break;
        case 's':
            outfile = optarg;
            break;
+       case 'R':
+           rnd_seed = strtoul(optarg, NULL, 10);
+           break;
        }
     }
 #endif
        }
     }
 #endif
+    srandom(rnd_seed);
     if (config_file == NULL) {
        sprintf(tbuf, "%s/econfig", datadir);
        config_file = tbuf;
     if (config_file == NULL) {
        sprintf(tbuf, "%s/econfig", datadir);
        config_file = tbuf;
@@ -233,6 +242,8 @@ main(int argc, char *argv[])
        if (!quiet && i)
            printf("\ntry #%d (out of %d)...", i + 1, NUMTRIES);
        qprint("\n\n        #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
        if (!quiet && i)
            printf("\ntry #%d (out of %d)...", i + 1, NUMTRIES);
        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");
        if (!drift())
            qprint("fairland: unstable drift -- try increasisg DRIFT_MAX\n");
        qprint("placing capitals...\n");
        if (!drift())
            qprint("fairland: unstable drift -- try increasisg DRIFT_MAX\n");
@@ -298,10 +309,11 @@ parse_args(int argc, char *argv[])
 {
     if (argc < 2 || argc > 8) {
        puts("fairland syntax:\n");
 {
     if (argc < 2 || argc > 8) {
        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("-a = Airport marker for continents, -i = islands not distinct");
        puts("-q = quiet, -o = no ore produced");
        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);
        puts("nc = number of continents [MANDATORY]");
        puts("sc = continent size [MANDATORY]");
               outfile);
        puts("nc = number of continents [MANDATORY]");
        puts("sc = continent size [MANDATORY]");
@@ -399,7 +411,6 @@ static int
 allocate_memory(void)
 {
     int i;
 allocate_memory(void)
 {
     int i;
-    time_t now;
 
 #if !defined(_WIN32)
     the_file =
 
 #if !defined(_WIN32)
     the_file =
@@ -418,12 +429,6 @@ allocate_memory(void)
     sects = (struct sctstr **)calloc(YSIZE, sizeof(struct sctstr *));
     for (i = 0; i < YSIZE; i++)
        sects[i] = &sectsbuf[XSIZE * i];
     sects = (struct sctstr **)calloc(YSIZE, sizeof(struct sctstr *));
     for (i = 0; i < YSIZE; i++)
        sects[i] = &sectsbuf[XSIZE * i];
-    time(&now);
-#if !defined(_WIN32)
-    srandom(now + getpid());
-#else
-    srandom(now);
-#endif
     capx = (int *)calloc(nc, sizeof(int));
     capy = (int *)calloc(nc, sizeof(int));
     vector = (int *)calloc(WORLD_X + WORLD_Y, sizeof(int));
     capx = (int *)calloc(nc, sizeof(int));
     capy = (int *)calloc(nc, sizeof(int));
     vector = (int *)calloc(WORLD_X + WORLD_Y, sizeof(int));