]> git.pond.sub.org Git - empserver/blobdiff - src/util/fairland.c
Make fairland obey file locks
[empserver] / src / util / fairland.c
index 2533b5b6ba59ec6ee14d31c27c741987f393b2fc..42ee5df28ea7ee25a3b9eee5fa43cec52d113e9d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -66,24 +66,19 @@ static int quiet = 0;
 /* lower URAN_MIN for more uranium */
 #define URAN_MIN   56
 
-#if defined(_WIN32)
-#include "../lib/gen/getopt.h"
-#endif
-
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
+#include <unistd.h>
+#include "file.h"
 #include "misc.h"
-#include "power.h"
 #include "nat.h"
-#include "sect.h"
-#include "file.h"
-#include "xy.h"
 #include "optlist.h"
+#include "power.h"
 #include "prototypes.h"
+#include "sect.h"
 #include "version.h"
+#include "xy.h"
 
 /* do not change these 4 defines */
 #define LANDMIN                1       /* plate altitude for normal land */
@@ -149,9 +144,6 @@ 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 */
-FILE *sect_fptr;                       /* the file we write everything to */
-struct sctstr **sects;
-struct sctstr *sectsbuf;
 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 */
@@ -168,15 +160,12 @@ static int drift(void);
 static void grow_continents(void);
 static void create_elevations(void);
 static void write_sects(void);
-static int write_file(void);
 static void output(void);
 static int write_newcap_script(void);
 static int stable(void);
 static void elevate_land(void);
 static void elevate_sea(void);
 static int map_symbol(int x, int y);
-static void fl_sct_init(coord x, coord y, s_char *ptr,
-                       time_t timestamp);
 static void set_coastal_flags(void);
 
 static void print_vars(void);
@@ -232,11 +221,14 @@ main(int argc, char *argv[])
            exit(1);
        }
     }
+    parse_args(argc - optind, argv + optind);
+
     srandom(rnd_seed);
-    if (emp_config(config_file))
+    empfile_init();
+    if (emp_config(config_file) < 0)
        exit(1);
+    empfile_fixup();
 
-    parse_args(argc - optind, argv + optind);
     if (allocate_memory() == -1)
        exit(-1);
     print_vars();
@@ -265,12 +257,24 @@ main(int argc, char *argv[])
     qprint("designating sectors...\n");
     if (ORE)
        qprint("adding resources...\n");
+    write_newcap_script();
+
+    if (chdir(gamedir)) {
+       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");
+       exit(1);
+    }
     write_sects();
     qprint("writing to sectors file...\n");
-    if (write_file() == -1)
+    if (!ef_close(EF_SECTOR))
        exit(-1);
+
     output();
-    write_newcap_script();
+    qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
+          outfile);
     if (!ORE)
        qprint("\t*** Resources have not been added ***\n");
     exit(0);
@@ -433,20 +437,7 @@ static int
 allocate_memory(void)
 {
     int i;
-    char *fname;
 
-    fname = malloc(strlen(gamedir) + 1 + strlen(empfile[EF_SECTOR].file) + 1);
-    sprintf(fname, "%s/%s", gamedir, empfile[EF_SECTOR].file);
-    sect_fptr = fopen(fname, "wb");
-    if (sect_fptr == NULL) {
-       perror(fname);
-       return -1;
-    }
-    free(fname);
-    sectsbuf = calloc((YSIZE * XSIZE), sizeof(struct sctstr));
-    sects = calloc(YSIZE, sizeof(struct sctstr *));
-    for (i = 0; i < YSIZE; i++)
-       sects[i] = &sectsbuf[XSIZE * i];
     capx = calloc(nc, sizeof(int));
     capy = calloc(nc, sizeof(int));
     vector = calloc(WORLD_X + WORLD_Y, sizeof(int));
@@ -493,7 +484,7 @@ init(void)
        }
     }
 
-    for (i = 0; i < nc; ++i, xx += 2) {
+    for (i = 0; i < nc; ++i) {
        if (xx >= WORLD_X) {
            ++yy;
            xx = yy % 2;
@@ -504,6 +495,7 @@ init(void)
        }
        capx[i] = xx;
        capy[i] = yy;
+       xx += 2;
     }
     for (i = 0; i < STABLE_CYCLE; ++i)
        mc[i] = i;
@@ -839,7 +831,7 @@ grow_islands(void)
        find_coast(c);
        qprint(" %d(%d)", c - nc + 1, secs);
        isecs[c] = secs;
-       ctot = c;
+       ctot++;
     }
 }
 
@@ -1010,9 +1002,9 @@ set_fert(int e)
     if (e < LANDMIN)
        fert = LANDMIN - e + 40;
     else if (e < FERT_MAX)
-       fert = (140 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
-    if (fert > 120)
-       fert = 120;
+       fert = (120 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
+    if (fert > 100)
+       fert = 100;
     return fert;
 }
 
@@ -1085,13 +1077,10 @@ write_sects(void)
 {
     struct sctstr *sct;
     int c, x, y, total;
-    time_t current_time = time(NULL);
 
-    /*  sct = &sects[0][0]; */
-    sct = sectsbuf;
     for (y = 0; y < YSIZE; y++) {
-       for (x = 0; x < XSIZE; x++, sct++) {
-           fl_sct_init(x * 2 + (y & 01), y, (s_char *)sct, current_time);
+       for (x = 0; x < XSIZE; x++) {
+           sct = getsectp(x * 2 + (y & 1), y);
            total = elev[sct->sct_x][y];
            if (total < LANDMIN) {
                sct->sct_type = SCT_WATER;
@@ -1111,33 +1100,13 @@ write_sects(void)
     }
     if (AIRPORT_MARKER)
        for (c = 0; c < nc; ++c) {
-           sects[capy[c]][capx[c] / 2 + capy[c] % 2].sct_type = SCT_AIRPT;
-           sects[capy[c]][capx[c] / 2 + capy[c] % 2].sct_newtype = SCT_AIRPT;
+           sct = getsectp(capx[c], capy[c]);
+           sct->sct_type = SCT_AIRPT;
+           sct->sct_newtype = SCT_AIRPT;
        }
     set_coastal_flags();
 }
 
-/****************************************************************************
-  WRITE ALL THIS STUFF TO THE FILE
-****************************************************************************/
-static int
-write_file(void)
-{
-    int n;
-
-    n = fwrite(sectsbuf, sizeof(struct sctstr), YSIZE * XSIZE, sect_fptr);
-    if (n <= 0) {
-       perror(empfile[EF_SECTOR].file);
-       return -1;
-    }
-    if (n != YSIZE * XSIZE) {
-       printf("%s:partial write\n", empfile[EF_SECTOR].file);
-       return -1;
-    }
-    fclose(sect_fptr);
-    return 0;
-}
-
 /****************************************************************************
   PRINT A PICTURE OF THE MAP TO YOUR SCREEN
 ****************************************************************************/
@@ -1201,8 +1170,6 @@ write_newcap_script(void)
     fprintf(script, "add %d visitor visitor v i\n", c + 1);
     ++c;
     fclose(script);
-    qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
-          outfile);
     return 0;
 }
 
@@ -1218,33 +1185,23 @@ qprint(const char * const fmt, ...)
     }
 }
 
-static void
-fl_sct_init(coord x, coord y, s_char *ptr, time_t timestamp)
-{
-    struct sctstr *sp = (struct sctstr *)ptr;
-
-    sp->ef_type = EF_SECTOR;
-    sp->sct_x = x;
-    sp->sct_y = y;
-    sp->sct_dist_x = x;
-    sp->sct_dist_y = y;
-    sp->sct_road = 0;
-    sp->sct_rail = 0;
-    sp->sct_defense = 0;
-    sp->sct_timestamp = timestamp;
-    sp->sct_coastal = 1;
-}
-
 static void
 set_coastal_flags(void)
 {
     int i, j;
+    struct sctstr *sp;
 
     qprint("setting coastal flags...\n");
-    for (i = 0; i < nc; ++i)
-       for (j = 0; j < sc; j++)
-           sects[secty[i][j]][sectx[i][j] / 2].sct_coastal = sectc[i][j];
-    for (i = nc; i < nc + ni; ++i)
-       for (j = 0; j < isecs[i]; j++)
-           sects[secty[i][j]][sectx[i][j] / 2].sct_coastal = sectc[i][j];
+    for (i = 0; i < nc; ++i) {
+       for (j = 0; j < sc; j++) {
+           sp = getsectp(sectx[i][j], secty[i][j]);
+           sp->sct_coastal = sectc[i][j];
+       }
+    }
+    for (i = nc; i < nc + ni; ++i) {
+       for (j = 0; j < isecs[i]; j++) {
+           sp = getsectp(sectx[i][j], secty[i][j]);
+           sp->sct_coastal = sectc[i][j];
+       }
+    }
 }