]> git.pond.sub.org Git - empserver/blobdiff - src/util/fairland.c
Drop add's obscure sector check and wipe functionality
[empserver] / src / util / fairland.c
index 42ee5df28ea7ee25a3b9eee5fa43cec52d113e9d..87e14e6ff8bb42e47c4fb53387c030e92bb66fbd 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
@@ -26,7 +25,7 @@
  *  ---
  *
  *  fairland.c: Create a nice, new world
- * 
+ *
  *  Known contributors to this file:
  *     Ken Stevens, 1995
  *     Steve McClure, 1998
@@ -55,7 +54,7 @@ static int quiet = 0;
 #define FERT_MAX   56
 
 /* raise OIL_MAX for more oil */
-#define OIL_MAX    33
+#define OIL_MAX           33
 
 /* lower IRON_MIN for more iron */
 #define IRON_MIN   22
@@ -66,6 +65,7 @@ static int quiet = 0;
 /* lower URAN_MIN for more uranium */
 #define URAN_MIN   56
 
+#include <assert.h>
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -101,10 +101,8 @@ static int DISTINCT_ISLANDS = 1;
 
 static char *program_name;
 
-#define XSIZE           ((WORLD_X) / 2)        /* basically world x-y size */
-#define YSIZE           (WORLD_Y)
 #define STABLE_CYCLE 4         /* stability required for perterbed capitals */
-#define INFINITY        999    /* a number which means "BIG" */
+#define INFINITY       999     /* a number which means "BIG" */
 
 /* these defines prevent infinite loops:
 */
@@ -122,39 +120,39 @@ static char *program_name;
 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
 #define rnd(x) (random() % (x))
 
-int secs;                      /* number of sectors grown */
-int ctot;                      /* total number of continents and islands grown */
-int *isecs;                    /* array of how large each island is */
+static int secs;               /* number of sectors grown */
+static int ctot;               /* total number of continents and islands grown */
+static 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
+static int nc, sc, di, sp, pm, ni, is, id; /* the 8 args to this program */
+static unsigned long rnd_seed; /* optional seed argument */
+static int *capx, *capy;       /* location of the nc capitals */
+static int *mc, mcc;           /* array and counter used for stability
                                   check when perturbing */
-int spike;                     /* are we spiking? */
-int mind;                      /* the final distance between capitals that
+static int spike;              /* are we spiking? */
+static int mind;               /* the final distance between capitals that
                                   we achieved */
-int dirx[] = { -2, -1, 1, 2, 1, -1 };  /* gyujnb */
-int diry[] = { 0, -1, -1, 0, 1, 1 };
-
-int **own;                     /* owner of the sector.  -1 means water */
-int **elev;                    /* elevation of the sectors */
-int **sectx, **secty;          /* the sectors for each continent */
-int **sectc;                   /* which sectors are on the coast? */
-int *vector;                   /* used for measuring distances */
-int *weight;                   /* used for placing mountains */
-int *dsea, *dmoun;             /* the dist to the ocean and mountain */
-int fl_status;                 /* is anything wrong? */
+static int dirx[] = { -2, -1, 1, 2, 1, -1 }; /* gyujnb */
+static int diry[] = { 0, -1, -1, 0, 1, 1 };
+
+static int **own;              /* owner of the sector.  -1 means water */
+static int **elev;             /* elevation of the sectors */
+static int **sectx, **secty;   /* the sectors for each continent */
+static int **sectc;            /* which sectors are on the coast? */
+static int *vector;            /* used for measuring distances */
+static int *weight;            /* used for placing mountains */
+static int *dsea, *dmoun;      /* the dist to the ocean and mountain */
+static int fl_status;          /* is anything wrong? */
 #define STATUS_NO_ROOM 1       /* there was no room to grow */
 #define NUMTRIES 10            /* keep trying to grow this many times */
 
-const char *numletter =
+static const char *numletter =
     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
 static void help(char *);
 static void usage(void);
 static void parse_args(int argc, char *argv[]);
-static int allocate_memory(void);
+static void allocate_memory(void);
 static void init(void);
 static int drift(void);
 static void grow_continents(void);
@@ -229,8 +227,7 @@ main(int argc, char *argv[])
        exit(1);
     empfile_fixup();
 
-    if (allocate_memory() == -1)
-       exit(-1);
+    allocate_memory();
     print_vars();
 
     do {
@@ -263,14 +260,12 @@ main(int argc, char *argv[])
        fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
        exit(EXIT_FAILURE);
     }
-    if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME, WORLD_SZ())) {
-       perror("ef_open");
+    if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
        exit(1);
-    }
     write_sects();
     qprint("writing to sectors file...\n");
     if (!ef_close(EF_SECTOR))
-       exit(-1);
+       exit(1);
 
     output();
     qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
@@ -422,10 +417,9 @@ parse_args(int argc, char *argv[])
        exit(1);
     }
     if (nc * sc + nc * my_sqrt(sc) * 2 * (di + 1) > WORLD_X * WORLD_Y) {
-       puts("fairland: error -- world not big enough to fit continents.");
-       puts("arguments must satisfy:");
+       puts("fairland: warning -- world might be too small to fit continents.");
+       puts("arguments should satisfy:");
        puts("nc*sc*sc + nc*sqrt(sc)*2*(di+1) < WORLD_X * WORLD_Y");
-       exit(1);
     }
 }
 
@@ -433,7 +427,7 @@ parse_args(int argc, char *argv[])
   VARIABLE INITIALIZATION
 ****************************************************************************/
 
-static int
+static void
 allocate_memory(void)
 {
     int i;
@@ -466,7 +460,6 @@ allocate_memory(void)
        sectc[i] = calloc(is * 2, sizeof(int));
     }
 
-    return 0;
 }
 
 static void
@@ -691,12 +684,8 @@ new_try(int c)
                return i;
            i = (i + 1) % secs;
        } while (i != starti);
-       if (c < nc) {
-           printf("fairland: BUG -- couldn't find coast for continent %c, sector %d.\nPlease mail stevens@math.utoronto.ca.\n",
-                  c + 'a', secs);
-           exit(1);
-       } else
-           return -1;
+       assert(c >= nc);
+       return -1;
     }
     return -1;
 }
@@ -1078,10 +1067,10 @@ write_sects(void)
     struct sctstr *sct;
     int c, x, y, total;
 
-    for (y = 0; y < YSIZE; y++) {
-       for (x = 0; x < XSIZE; x++) {
-           sct = getsectp(x * 2 + (y & 1), y);
-           total = elev[sct->sct_x][y];
+    for (y = 0; y < WORLD_Y; y++) {
+       for (x = y % 2; x < WORLD_X; x += 2) {
+           sct = getsectp(x, y);
+           total = elev[x][y];
            if (total < LANDMIN) {
                sct->sct_type = SCT_WATER;
            } else if (total < HILLMIN)
@@ -1129,7 +1118,9 @@ output(void)
        }
     }
     if (AIRPORT_MARKER)
-       printf("\n\nEach continent is marked by a \"*\" on the map (to distinguish them from\nthe islands).  You can redesignate these airfields to wilderness sectors\none at a time, each time you add a new country to the game.\n");
+       printf("\n\nEach continent is marked by a \"*\" on the map (to distinguish them from\n"
+              "the islands).  You can redesignate these airfields to wilderness sectors\n"
+              "one at a time, each time you add a new country to the game.\n");
 }
 
 static int
@@ -1162,19 +1153,18 @@ write_newcap_script(void)
     }
 
     for (c = 0; c < nc; ++c) {
-       fprintf(script, "add %d %d %d n i\n", c + 1, c + 1, c + 1);
+       fprintf(script, "add %d %d %d n\n", c + 1, c + 1, c + 1);
        if (AIRPORT_MARKER)
            fprintf(script, "des %d,%d -\n", capx[c], capy[c]);
        fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
     }
-    fprintf(script, "add %d visitor visitor v i\n", c + 1);
-    ++c;
+    fprintf(script, "add %d visitor visitor v\n", c + 1);
     fclose(script);
     return 0;
 }
 
 static void
-qprint(const char * const fmt, ...)
+qprint(const char *const fmt, ...)
 {
     va_list ap;