2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * fairland.c: Create a nice, new world
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2020
40 * Place the capitals on the torus in such a way so as to maximize
41 * their distances from one another. This uses the perturbation
42 * technique of calculus of variations.
44 * 2. Grow start islands ("continents")
46 * For all continents, add the first sector at the capital's location,
47 * and the second right to it. These are the capital sectors. Then
48 * add one sector to each continent in turn, obeying the minimum
49 * distance between continents, until they have the specified size.
51 * The kind of shape they grow into is determined by the "spike
52 * percentage" --- the higher the spike, the more spindly they will
53 * be. If you lower the spike, the continents will be more round.
55 * If growing fails due to lack of room, start over. If it fails too
56 * many times, give up and terminate unsuccessfully.
58 * 3. Place and grow additional islands
60 * Place and grow islands one after the other. Place the first sector
61 * randomly, pick an island size, then grow the island to that size.
63 * Growing works as for continents, except the minimum distance for
64 * additional islands applies, and growing simply stops when there is
67 * 4. Compute elevation
69 * Elevate islands one after the other.
71 * First, place the specified number of mountains randomly.
72 * Probability increases with distance to sea.
74 * Last, elevate mountains and the capitals. Pick coastal mountain
75 * elevation randomly from an interval of medium elevations reserved
76 * for them. Pick non-coastal mountain elevation randomly from an
77 * interval of high elevation reserved for them. Set capital
78 * elevation to a fixed, medium value.
80 * In between, elevate the remaining land one by one, working from
81 * mountains towards the sea, and from the elevation just below the
82 * non-coastal mountains' interval linearly down to 1, avoiding the
83 * coastal mountains' interval.
85 * This gives islands of the same size the same set of elevations,
86 * except for mountains.
88 * Elevate sea: pick a random depth from an interval that deepens with
89 * the distance to land.
93 * Sector resources are simple functions of elevation. You can alter
94 * macros OIL_MAX, IRON_MIN, GOLD_MIN, FERT_MAX, and URAN_MIN to
107 #include "prototypes.h"
112 /* The following five numbers refer to elevation under which (in the case of
113 fertility or oil) or over which (in the case of iron, gold, and uranium)
114 sectors with that elevation will contain that resource. Elevation ranges
117 /* raise FERT_MAX for more fertility */
120 /* raise OIL_MAX for more oil */
123 /* lower IRON_MIN for more iron */
126 /* lower GOLD_MIN for more gold */
129 /* lower URAN_MIN for more uranium */
132 /* do not change these 4 defines */
133 #define LANDMIN 1 /* plate altitude for normal land */
134 #define HILLMIN 34 /* plate altitude for hills */
135 #define PLATMIN 36 /* plate altitude for plateau */
136 #define HIGHMIN 98 /* plate altitude for mountains */
138 static void qprint(const char * const fmt, ...)
139 ATTRIBUTE((format (printf, 1, 2)));
142 * Program arguments and options
144 static char *program_name;
145 static int nc, sc; /* number and size of continents */
146 static int ni, is; /* number and size of islands */
147 #define DEFAULT_SPIKE 10
148 static int sp = DEFAULT_SPIKE; /* spike percentage */
149 #define DEFAULT_MOUNTAIN 0
150 static int pm = DEFAULT_MOUNTAIN; /* mountain percentage */
151 #define DEFAULT_CONTDIST 2
152 static int di = DEFAULT_CONTDIST; /* min. distance between continents */
153 #define DEFAULT_ISLDIST 1
154 static int id = DEFAULT_ISLDIST; /* ... continents and islands */
155 /* don't let the islands crash into each other.
156 1 = don't merge, 0 = merge. */
157 static int DISTINCT_ISLANDS = 1;
159 #define DEFAULT_OUTFILE_NAME "newcap_script"
160 static const char *outfile = DEFAULT_OUTFILE_NAME;
162 #define STABLE_CYCLE 4 /* stability required for perterbed capitals */
163 #define INFINITY 999 /* a number which means "BIG" */
165 /* these defines prevent infinite loops:
168 #define COAST_SEARCH_MAX 200 /* how many times do we look for a coast sector
169 when growing continents and islands */
170 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
171 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
172 #define MOUNTAIN_SEARCH_MAX 1000 /* how long do we try to place mountains */
177 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
178 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
180 static int ctot; /* total number of continents and islands grown */
181 static int *isecs; /* array of how large each island is */
183 static int *capx, *capy; /* location of the nc capitals */
184 static int dirx[] = { -2, -1, 1, 2, 1, -1 }; /* gyujnb */
185 static int diry[] = { 0, -1, -1, 0, 1, 1 };
187 static int **own; /* owner of the sector. -1 means water */
188 static int **elev; /* elevation of the sectors */
189 static int **sectx, **secty; /* the sectors for each continent */
190 static int **sectc; /* which sectors are on the coast? */
191 static int *vector; /* used for measuring distances */
192 static int *weight; /* used for placing mountains */
193 static int *dsea, *dmoun; /* the dist to the ocean and mountain */
195 #define NUMTRIES 10 /* keep trying to grow this many times */
197 static const char *numletter =
198 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
200 static void help(char *);
201 static void usage(void);
202 static void parse_args(int argc, char *argv[]);
203 static void allocate_memory(void);
204 static void init(void);
205 static int drift(void);
206 static int grow_continents(void);
207 static void create_elevations(void);
208 static void write_sects(void);
209 static void output(void);
210 static int write_newcap_script(void);
211 static int stable(int);
212 static void elevate_land(void);
213 static void elevate_sea(void);
214 static void set_coastal_flags(void);
216 static void print_vars(void);
217 static void fl_move(int);
218 static void grow_islands(void);
220 /* Debugging aids: */
221 void print_own_map(void);
222 void print_elev_map(void);
224 /****************************************************************************
226 ****************************************************************************/
229 main(int argc, char *argv[])
232 char *config_file = NULL;
234 unsigned rnd_seed = 0;
237 program_name = argv[0];
239 while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
242 config_file = optarg;
245 DISTINCT_ISLANDS = 0;
251 rnd_seed = strtoul(optarg, NULL, 10);
261 printf("%s\n\n%s", version, legal);
270 rnd_seed = pick_seed();
273 if (emp_config(config_file) < 0)
277 parse_args(argc - optind, argv + optind);
282 qprint("\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
283 qprint("seed is %u\n", rnd_seed);
288 qprint("\ntry #%d (out of %d)...\n", try + 1, NUMTRIES);
289 qprint("placing capitals...\n");
291 qprint("unstable drift\n");
292 qprint("growing continents...\n");
293 done = grow_continents();
294 } while (!done && ++try < NUMTRIES);
296 fprintf(stderr, "%s: world not large enough to hold continents\n",
300 qprint("growing islands:");
302 qprint("\nelevating land...\n");
305 qprint("writing to sectors file...\n");
306 if (!write_newcap_script())
308 if (chdir(gamedir)) {
309 fprintf(stderr, "%s: can't chdir to %s (%s)\n",
310 program_name, gamedir, strerror(errno));
313 if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
316 if (!ef_close(EF_SECTOR))
320 qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
330 puts("Creating a planet with:\n");
331 printf("%d continents\n", nc);
332 printf("continent size: %d\n", sc);
333 printf("number of islands: %d\n", ni);
334 printf("average size of islands: %d\n", is);
335 printf("spike: %d%%\n", sp);
336 printf("%d%% of land is mountain (each continent will have %d mountains)\n",
337 pm, (pm * sc) / 100);
338 printf("minimum distance between continents: %d\n", di);
339 printf("minimum distance from islands to continents: %d\n", id);
340 printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
344 help(char *complaint)
347 fprintf(stderr, "%s: %s\n", program_name, complaint);
348 fprintf(stderr, "Try -h for help.\n");
354 printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
355 " -e CONFIG-FILE configuration file\n"
357 " -i islands may merge\n"
359 " -R SEED seed for random number generator\n"
360 " -s SCRIPT name of script to create (default %s)\n"
361 " -h display this help and exit\n"
362 " -v display version information and exit\n"
363 " NC number of continents\n"
364 " SC continent size\n"
365 " NI number of islands (default NC)\n"
366 " IS average island size (default SC/2)\n"
367 " SP spike percentage: 0 = round, 100 = snake (default %d)\n"
368 " PM percentage of land that is mountain (default %d)\n"
369 " DI minimum distance between continents (default %d)\n"
370 " ID minimum distance from islands to continents (default %d)\n",
371 program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
372 DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
376 parse_args(int argc, char *argv[])
378 int dist_max = mapdist(0, 0, WORLD_X / 2, WORLD_Y / 2);
381 help("missing arguments");
385 help("too many arguments");
390 fprintf(stderr, "%s: number of continents must be > 0\n",
397 fprintf(stderr, "%s: size of continents must be > 1\n",
408 fprintf(stderr, "%s: number of islands must be >= 0\n",
416 fprintf(stderr, "%s: size of islands must be > 0\n",
423 if (sp < 0 || sp > 100) {
425 "%s: spike percentage must be between 0 and 100\n",
432 if (pm < 0 || pm > 100) {
434 "%s: mountain percentage must be between 0 and 100\n",
442 fprintf(stderr, "%s: distance between continents must be >= 0\n",
447 fprintf(stderr, "%s: distance between continents too large\n",
456 "%s: distance from islands to continents must be >= 0\n",
462 "%s: distance from islands to continents too large\n",
468 /****************************************************************************
469 VARIABLE INITIALIZATION
470 ****************************************************************************/
473 allocate_memory(void)
477 capx = calloc(nc, sizeof(int));
478 capy = calloc(nc, sizeof(int));
479 vector = calloc(WORLD_X + WORLD_Y, sizeof(int));
480 own = calloc(WORLD_X, sizeof(int *));
481 elev = calloc(WORLD_X, sizeof(int *));
482 for (i = 0; i < WORLD_X; ++i) {
483 own[i] = calloc(WORLD_Y, sizeof(int));
484 elev[i] = calloc(WORLD_Y, sizeof(int));
486 sectx = calloc(nc + ni, sizeof(int *));
487 secty = calloc(nc + ni, sizeof(int *));
488 sectc = calloc(nc + ni, sizeof(int *));
489 isecs = calloc(nc + ni, sizeof(int));
490 weight = calloc(MAX(sc, is * 2), sizeof(int));
491 dsea = calloc(MAX(sc, is * 2), sizeof(int));
492 dmoun = calloc(MAX(sc, is * 2), sizeof(int));
493 for (i = 0; i < nc; ++i) {
494 sectx[i] = calloc(sc, sizeof(int));
495 secty[i] = calloc(sc, sizeof(int));
496 sectc[i] = calloc(sc, sizeof(int));
498 for (i = nc; i < nc + ni; ++i) {
499 sectx[i] = calloc(is * 2, sizeof(int));
500 secty[i] = calloc(is * 2, sizeof(int));
501 sectc[i] = calloc(is * 2, sizeof(int));
511 for (i = 0; i < WORLD_X; ++i) {
512 for (j = 0; j < WORLD_Y; ++j) {
518 /****************************************************************************
519 DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
520 ****************************************************************************/
522 /* How isolated is capital j?
525 iso(int j, int newx, int newy)
527 int i, md, d = WORLD_X + WORLD_Y;
529 for (i = 0; i < nc; ++i) {
532 md = mapdist(capx[i], capy[i], newx, newy);
542 * Return 1 for a stable drift, 0 for an unstable one.
549 for (i = 0; i < nc; i++) {
550 capy[i] = (2 * i) / WORLD_X;
551 capx[i] = (2 * i) % WORLD_X + capy[i] % 2;
552 if (capy[i] >= WORLD_Y) {
554 "%s: world not big enough for all the continents\n",
560 for (turns = 0; turns < DRIFT_MAX; ++turns) {
563 for (i = 0; i < nc; ++i)
570 * Has the drift stabilized?
571 * @turns is the number of turns so far.
576 static int mc[STABLE_CYCLE];
577 int i, isod, d = 0, stab = 1;
580 for (i = 0; i < STABLE_CYCLE; i++)
584 if (turns <= DRIFT_BEFORE_CHECK)
587 for (i = 0; i < nc; ++i) {
588 isod = iso(i, capx[i], capy[i]);
593 for (i = 0; i < STABLE_CYCLE; ++i)
597 mc[turns % STABLE_CYCLE] = d;
601 /* This routine does the actual drifting
607 int i, n, newx, newy;
609 for (i = roll0(6), n = 0; n < 6; i = (i + 1) % 6, ++n) {
610 newx = new_x(capx[j] + dirx[i]);
611 newy = new_y(capy[j] + diry[i]);
612 if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) {
620 /****************************************************************************
622 ****************************************************************************/
624 /* Look for a coastal sector of continent c
632 for (i = 0; i < isecs[c]; ++i) {
634 for (j = 0; j < 6; ++j)
635 if (own[new_x(sectx[c][i] + dirx[j])][new_y(secty[c][i] + diry[j])] == -1)
640 /* Used for measuring distances
652 for (i = 1; i < n && vector[i] == vector[i - 1]; ++i) ;
655 return i > 1 || vector[0] > 0;
658 /* Test to see if we're allowed to grow there: the arguments di and id
661 try_to_grow(int c, int newx, int newy, int d)
665 if (own[newx][newy] != -1)
668 for (i = 1; i <= d; ++i) {
669 for (j = 0; j < i; ++j)
674 for (j = 0; j < i; ++j) {
675 px = new_x(px + dirx[vector[j]]);
676 py = new_y(py + diry[vector[j]]);
678 if (own[px][py] != -1 &&
680 (DISTINCT_ISLANDS || own[px][py] < nc))
682 } while (next_vector(i));
684 sectx[c][isecs[c]] = newx;
685 secty[c][isecs[c]] = newy;
691 /* Move along the coast in a clockwise direction.
695 next_coast(int c, int x, int y, int *xp, int *yp)
697 int i, nx, ny, wat = 0;
705 for (i = 0; i < 12; ++i) {
706 nx = new_x(x + dirx[i % 6]);
707 ny = new_y(y + diry[i % 6]);
708 if (own[nx][ny] == -1)
710 if (wat && own[nx][ny] == c) {
718 /* Choose a sector to grow from
722 new_try(int c, int spike)
731 i = starti = (spike && sectc[c][secs - 1]) ? secs - 1 : roll0(secs);
736 } while (i != starti);
743 /* Grow continent c by 1 sector
747 grow_one_sector(int c)
749 int spike = roll0(100) < sp;
750 int done, coast_search, try1, x, y, newx, newy, i, n, sx, sy;
752 if ((try1 = new_try(c, spike)) == -1)
754 x = sx = sectx[c][try1];
755 y = sy = secty[c][try1];
760 for (i = roll0(6), n = 0; n < 12 && !done; i = (i + 1) % 6, ++n) {
761 newx = new_x(x + dirx[i]);
762 newy = new_y(y + diry[i]);
764 (own[new_x(x+dirx[(i+5)%6])][new_y(y+diry[(i+5)%6])] == -1 &&
765 own[new_x(x+dirx[(i+1)%6])][new_y(y+diry[(i+1)%6])] == -1))
766 if (try_to_grow(c, newx, newy, c < nc ? di : id))
770 for (i = roll0(6), n = 0; n < 6 && !done; i = (i + 1) % 6, ++n) {
771 newx = new_x(x + dirx[i]);
772 newy = new_y(y + diry[i]);
773 if (try_to_grow(c, newx, newy, c < nc ? di : id))
776 next_coast(c, x, y, &x, &y);
778 } while (!done && coast_search < COAST_SEARCH_MAX &&
779 (isecs[c] == 1 || x != sx || y != sy));
784 * Grow the continents.
785 * Return 1 on success, 0 on error.
788 grow_continents(void)
793 for (c = 0; c < nc; ++c) {
795 if (!try_to_grow(c, capx[c], capy[c], di)
796 || !try_to_grow(c, new_x(capx[c] + 2), capy[c], di)) {
803 qprint("No room for continents\n");
807 for (secs = 2; secs < sc && done; secs++) {
808 for (c = 0; c < nc; ++c) {
810 if (!grow_one_sector(c))
815 for (c = 0; c < nc; ++c)
819 qprint("Only managed to grow %d out of %d sectors.\n",
825 /****************************************************************************
827 ****************************************************************************/
829 /* Choose a place to start growing an island from
832 place_island(int c, int *xp, int *yp)
835 int ssy = roll0(WORLD_Y);
836 int ssx = new_x(roll0(WORLD_X / 2) * 2 + ssy % 2);
838 if (ssx > WORLD_X - 2)
839 ssx = new_x(ssx + 2);
840 for (d = di + id; d >= id; --d) {
844 for (*yp = sy; *xp != sx || *yp != sy; *xp += 2) {
845 if (*xp >= WORLD_X) {
846 *yp = new_y(*yp + 1);
848 if (*xp == sx && *yp == sy)
851 if (try_to_grow(c, *xp, *yp, d))
858 /* Grow all the islands
864 int c, secs, x, y, isiz;
866 for (c = nc; c < nc + ni; ++c) {
868 if (!place_island(c, &x, &y))
870 isiz = roll(is) + roll0(is);
874 } while (secs < isiz && grow_one_sector(c));
876 qprint(" %d(%d)", c - nc + 1, secs);
881 /****************************************************************************
883 ****************************************************************************/
885 create_elevations(void)
889 for (i = 0; i < WORLD_X; i++) {
890 for (j = 0; j < WORLD_Y; j++)
891 elev[i][j] = -INFINITY;
897 /* Generic function for finding the distance to the closest sea, land, or
901 distance_to_what(int x, int y, int flag)
905 for (d = 1; d < 5; ++d) {
906 for (j = 0; j < d; ++j)
911 for (j = 0; j < d; ++j) {
912 px = new_x(px + dirx[vector[j]]);
913 py = new_y(py + diry[vector[j]]);
916 case 0: /* distance to sea */
917 if (own[px][py] == -1)
920 case 1: /* distance to land */
921 if (own[px][py] != -1)
924 case 2: /* distance to mountain */
925 if (elev[px][py] == INFINITY)
929 } while (next_vector(d));
934 #define ELEV elev[sectx[c][i]][secty[c][i]]
935 #define distance_to_sea() (sectc[c][i]?1:distance_to_what(sectx[c][i], secty[c][i], 0))
936 #define distance_to_mountain() distance_to_what(sectx[c][i], secty[c][i], 2)
938 /* Decide where the mountains go
943 int i, mountain_search, k, c, total, ns, nm, highest, where, h, newk,
946 for (c = 0; c < ctot; ++c) {
949 nm = (pm * ns) / 100;
951 /* Place the mountains */
953 for (i = 0; i < ns; ++i) {
954 dsea[i] = distance_to_sea();
955 weight[i] = (total += (dsea[i] * dsea[i]));
958 for (k = nm, mountain_search = 0;
959 k && mountain_search < MOUNTAIN_SEARCH_MAX;
962 for (i = 0; i < ns; ++i)
963 if (r < weight[i] && ELEV == -INFINITY &&
965 ((!(capx[c] == sectx[c][i] &&
966 capy[c] == secty[c][i])) &&
967 (!(new_x(capx[c] + 2) == sectx[c][i] &&
968 capy[c] == secty[c][i]))))) {
975 /* Elevate land that is not mountain and not capital */
977 for (i = 0; i < ns; ++i)
978 dmoun[i] = distance_to_mountain();
979 dk = (ns - nm - ((c < nc) ? 3 : 1) > 0) ?
980 (100 * (HIGHMIN - LANDMIN)) / (ns - nm - ((c < nc) ? 3 : 1)) :
982 for (k = 100 * (HIGHMIN - 1);; k -= dk) {
985 for (i = 0; i < ns; ++i) {
986 if (ELEV != INFINITY &&
987 (c >= nc || ((!(capx[c] == sectx[c][i] &&
988 capy[c] == secty[c][i])) &&
989 (!(new_x(capx[c] + 2) == sectx[c][i] &&
990 capy[c] == secty[c][i]))))) {
991 h = 3 * (5 - dmoun[i]) + dsea[i];
1001 if (newk >= HILLMIN && newk < PLATMIN)
1005 elev[sectx[c][where]][secty[c][where]] = newk;
1006 dsea[where] = -INFINITY;
1007 dmoun[where] = INFINITY;
1010 /* Elevate the mountains and capitals */
1012 for (i = 0; i < ns; ++i) {
1013 if (ELEV == INFINITY) {
1015 ELEV = HILLMIN + roll0(PLATMIN - HILLMIN);
1017 ELEV = HIGHMIN + roll0((256 - HIGHMIN) / 2) +
1018 roll0((256 - HIGHMIN) / 2);
1019 } else if (c < nc &&
1020 (((capx[c] == sectx[c][i] && capy[c] == secty[c][i])) ||
1021 ((new_x(capx[c] + 2) == sectx[c][i] &&
1022 capy[c] == secty[c][i]))))
1028 #define distance_to_land() distance_to_what(x, y, 1)
1035 for (y = 0; y < WORLD_Y; ++y) {
1036 for (x = y % 2; x < WORLD_X; x += 2) {
1037 if (elev[x][y] == -INFINITY)
1038 elev[x][y] = -roll(distance_to_land() * 20 + 27);
1044 elev_to_sct_type(int elevation)
1046 if (elevation < LANDMIN)
1048 if (elevation < HILLMIN)
1050 if (elevation < PLATMIN)
1052 if (elevation < HIGHMIN)
1057 /****************************************************************************
1059 ****************************************************************************/
1066 fert = LANDMIN - e + 40;
1067 else if (e < FERT_MAX)
1068 fert = (120 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
1079 oil = (LANDMIN - e) * 2 + roll0(2);
1080 else if (e <= OIL_MAX)
1081 oil = (120 * (OIL_MAX - e + 1)) / (OIL_MAX - LANDMIN + 1);
1091 if (e >= IRON_MIN && e < HIGHMIN)
1092 iron = (120 * (e - IRON_MIN + 1)) / (HIGHMIN - IRON_MIN);
1102 if (e >= GOLD_MIN) {
1104 gold = (80 * (e - GOLD_MIN + 1)) / (HIGHMIN - GOLD_MIN);
1106 gold = 100 - 20 * HIGHMIN / e;
1117 if (e >= URAN_MIN && e < HIGHMIN)
1118 uran = (120 * (e - URAN_MIN + 1)) / (HIGHMIN - URAN_MIN);
1125 add_resources(struct sctstr *sct)
1127 sct->sct_fertil = set_fert(sct->sct_elev);
1128 sct->sct_oil = set_oil(sct->sct_elev);
1129 sct->sct_min = set_iron(sct->sct_elev);
1130 sct->sct_gmin = set_gold(sct->sct_elev);
1131 sct->sct_uran = set_uran(sct->sct_elev);
1134 /****************************************************************************
1135 DESIGNATE THE SECTORS
1136 ****************************************************************************/
1144 for (y = 0; y < WORLD_Y; y++) {
1145 for (x = y % 2; x < WORLD_X; x += 2) {
1146 sct = getsectp(x, y);
1147 sct->sct_elev = elev[x][y];
1148 sct->sct_type = elev_to_sct_type(elev[x][y]);
1149 sct->sct_newtype = sct->sct_type;
1150 sct->sct_dterr = own[sct->sct_x][y] + 1;
1154 set_coastal_flags();
1157 /****************************************************************************
1158 PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1159 ****************************************************************************/
1163 int sx, sy, x, y, c, type;
1166 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1171 for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1174 type = elev_to_sct_type(elev[x][y]);
1175 if (type == SCT_WATER)
1177 else if (type == SCT_MOUNT)
1182 assert(0 <= c && c < nc);
1183 if ((x == capx[c] || x == new_x(capx[c] + 2))
1185 printf("%c ", numletter[c % 62]);
1195 * Print a map to help visualize own[][].
1196 * This is for debugging.
1203 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1206 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1210 else if (own[x][y] == -1)
1213 putchar(numletter[own[x][y] % 62]);
1220 * Print a map to help visualize elev[][].
1221 * This is for debugging. It expects the terminal to understand
1222 * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1225 print_elev_map(void)
1227 int sx, sy, x, y, sat;
1229 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1232 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1236 else if (!elev[x][y])
1238 else if (elev[x][y] < 0) {
1239 sat = 256 + elev[x][y] * 2;
1240 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1241 } else if (elev[x][y] < HIGHMIN / 2) {
1242 sat = (HIGHMIN / 2 - elev[x][y]) * 4;
1243 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1244 } else if (elev[x][y] < HIGHMIN) {
1245 sat = 128 + (HIGHMIN - elev[x][y]) * 2;
1246 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1248 sat = 128 + (elev[x][y] - HIGHMIN) * 4 / 5;
1249 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1256 /***************************************************************************
1257 WRITE A SCRIPT FOR PLACING CAPITALS
1258 ****************************************************************************/
1260 write_newcap_script(void)
1263 FILE *script = fopen(outfile, "w");
1266 fprintf(stderr, "%s: unable to write to %s (%s)\n",
1267 program_name, outfile, strerror(errno));
1271 for (c = 0; c < nc; ++c) {
1272 fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1273 fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
1275 fprintf(script, "add %d visitor visitor v\n", c + 1);
1281 qprint(const char *const fmt, ...)
1287 vfprintf(stdout, fmt, ap);
1293 set_coastal_flags(void)
1298 for (i = 0; i < nc + ni; ++i) {
1299 for (j = 0; j < isecs[i]; j++) {
1300 sp = getsectp(sectx[i][j], secty[i][j]);
1301 sp->sct_coastal = sectc[i][j];