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, until they have the
51 * Growth uses weighted random sampling to pick one sector from the
52 * set of adjacent sea sectors that aren't too close to another
53 * continent. Growth operates in spiking mode with a chance given by
54 * the spike percentage. When "spiking", a sector's weight increases
55 * with number of adjacent sea sectors. This directs the growth away
56 * from land, resulting in spikes. When not spiking, the weight
57 * increases with the number of adjacent land sectors. This makes the
58 * island more rounded.
60 * If growing fails due to lack of room, start over. If it fails too
61 * many times, give up and terminate unsuccessfully.
63 * 3. Place and grow additional islands
65 * Each continent has a "sphere of influence": the set of sectors
66 * closer to it than to any other continent. Each island is entirely
67 * in one such sphere, and each sphere contains the same number of
68 * islands with the same sizes.
70 * First, split the specified number of island sectors per continent
71 * randomly into the island sizes. Sort by size so that larger
72 * islands are grown before smaller ones, to give the large ones the
73 * best chance to grow to their planned size.
75 * Then place one island's first sector into each sphere, using
76 * weighted random sampling with weights favoring sectors away from
77 * land and other spheres. Add one sector to each island in turn,
78 * until they have the intended size. Repeat until the specified
79 * number of islands has been grown.
81 * If placement fails due to lack of room, start over, just like for
84 * Growing works as for continents, except the minimum distance for
85 * additional islands applies, and growing simply stops when any of
86 * the islands being grown lacks the room to grow further. The number
87 * of sectors not grown carries over to the next island size.
89 * 4. Compute elevation
91 * First, use a simple random hill algorithm to assign raw elevations:
92 * initialize elevation to zero, then randomly raise circular hills on
93 * land / lower circular depressions at sea. Their size and height
94 * depends on the distance to the coast.
96 * Then, elevate islands one after the other.
98 * Set the capitals' elevation to a fixed value. Process the
99 * remaining sectors in order of increasing raw elevation, first
100 * non-mountains, then mountains. Non-mountain elevation starts at 1,
101 * and increases linearly to just below "high" elevation. Mountain
102 * elevation starts at "high" elevation, and increases linearly.
104 * This gives islands of the same size the same set of elevations.
105 * Larger islands get more and taller mountains.
107 * Finally, elevate sea: normalize the raw elevations to [-127:-1].
111 * Sector resources are simple functions of elevation. You can alter
112 * iron_conf[], gold_conf[], fert_conf[], oil_conf[], and uran_conf[]
127 #include "prototypes.h"
133 * Number of retries when growing land fails
137 /* do not change these defines */
138 #define LANDMIN 1 /* plate altitude for normal land */
139 #define PLATMIN 36 /* plate altitude for plateau */
140 #define HIGHMIN 98 /* plate altitude for mountains */
143 * Resource configuration
145 * Resources are determined by elevation. The map from elevation to
146 * resource is defined as a linear interpolation of resource data
147 * points (elev, res) defined in the tables below. Elevations range
148 * from -127 to 127, and resource values from 0 to 100.
151 struct resource_point {
155 struct resource_point iron_conf[] = {
159 { HIGHMIN - 1, 100 },
163 struct resource_point gold_conf[] = {
170 struct resource_point fert_conf[] = {
179 struct resource_point oil_conf[] = {
188 struct resource_point uran_conf[] = {
196 static void qprint(const char * const fmt, ...)
197 ATTRIBUTE((format (printf, 1, 2)));
200 * Program arguments and options
202 static char *program_name;
203 static int nc, sc; /* number and size of continents */
204 static int ni, is; /* number and size of islands */
205 #define DEFAULT_SPIKE 10
206 static int sp = DEFAULT_SPIKE; /* spike percentage */
207 #define DEFAULT_MOUNTAIN 0
208 static int pm = DEFAULT_MOUNTAIN; /* mountain percentage */
209 #define DEFAULT_CONTDIST 2
210 static int di = DEFAULT_CONTDIST; /* min. distance between continents */
211 #define DEFAULT_ISLDIST 1
212 static int id = DEFAULT_ISLDIST; /* ... continents and islands */
213 /* don't let the islands crash into each other.
214 1 = don't merge, 0 = merge. */
215 static int DISTINCT_ISLANDS = 1;
217 #define DEFAULT_OUTFILE_NAME "newcap_script"
218 static const char *outfile = DEFAULT_OUTFILE_NAME;
220 #define STABLE_CYCLE 4 /* stability required for perterbed capitals */
221 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
222 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
227 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
228 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
236 * The i-th capital is at cap[i].
238 static struct xy *cap;
242 * isecs[i] is the size of the i-th island.
248 * The i-th island's j-th sector is at sect[i][j].
254 * own[XYOFFSET(x, y)] is x,y's island number, -1 if water.
259 * Adjacent land sectors
260 * adj_land[XYOFFSET(x, y)] bit d is set exactly when the sector next
261 * to x, y in direction d is land.
263 static unsigned char *adj_land;
267 * elev[XYOFFSET(x, y)] is x,y's elevation.
273 * Each island is surrounded by an exclusive zone where only it may
274 * grow. The width of the zone depends on minimum distances.
275 * While growing continents, it is @di sectors wide.
276 * While growing additional islands, it is @id sectors wide.
277 * DISTINCT_ISLANDS nullifies the exclusive zone then.
278 * xzone[XYOFFSET(x, y)] is -1 when the sector is in no exclusive
279 * zone, a (non-negative) island number when it is in that island's
280 * exclusive zone and no other, and -2 when it is in multiple
286 * Set of sectors seen already
287 * Increment @cur_seen to empty the set of sectors seen, set
288 * seen[XYOFFSET(x, y)] to @cur_seen to add x,y to the set.
290 static unsigned *seen;
291 static unsigned cur_seen;
294 * Closest continent and "distance"
295 * closest[XYOFFSET(x, y)] is the closest continent's number.
296 * distance[] is complicated; see init_spheres_of_influence() and
297 * init_distance_to_coast().
299 static natid *closest;
300 static unsigned short *distance;
303 * Queue for breadth-first search
305 static int *bfs_queue;
306 static int bfs_queue_head, bfs_queue_tail;
308 static const char *numletter =
309 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
311 static void help(char *);
312 static void usage(void);
313 static void parse_args(int argc, char *argv[]);
314 static void allocate_memory(void);
315 static void init(void);
316 static int drift(void);
317 static int grow_continents(void);
318 static void create_elevations(void);
319 static void write_sects(void);
320 static void output(void);
321 static int write_newcap_script(void);
322 static int stable(int);
323 static void elevate_prep(void);
324 static void elevate_land(void);
325 static void elevate_sea(void);
327 static void print_vars(void);
328 static void drift_capital(int);
329 static int grow_islands(void);
331 /* Debugging aids: */
332 void print_own_map(void);
333 void print_xzone_map(void);
334 void print_closest_map(void);
335 void print_distance_map(void);
336 void print_elev_map(void);
338 /****************************************************************************
340 ****************************************************************************/
343 main(int argc, char *argv[])
346 char *config_file = NULL;
348 unsigned rnd_seed = 0;
351 program_name = argv[0];
353 while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
356 config_file = optarg;
359 DISTINCT_ISLANDS = 0;
365 rnd_seed = strtoul(optarg, NULL, 10);
375 printf("%s\n\n%s", version, legal);
384 rnd_seed = pick_seed();
387 if (emp_config(config_file) < 0)
391 parse_args(argc - optind, argv + optind);
396 qprint("\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
397 qprint("seed is %u\n", rnd_seed);
402 qprint("\ntry #%d (out of %d)...\n", try + 1, NUMTRIES);
403 qprint("placing capitals...\n");
405 qprint("unstable drift\n");
406 qprint("growing continents...\n");
407 done = grow_continents();
410 qprint("growing islands:");
411 done = grow_islands();
412 } while (!done && ++try < NUMTRIES);
414 fprintf(stderr, "%s: world not large enough for this much land\n",
418 qprint("elevating land...\n");
421 qprint("writing to sectors file...\n");
422 if (!write_newcap_script())
424 if (chdir(gamedir)) {
425 fprintf(stderr, "%s: can't chdir to %s (%s)\n",
426 program_name, gamedir, strerror(errno));
429 if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
432 if (!ef_close(EF_SECTOR))
436 qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
446 puts("Creating a planet with:\n");
447 printf("%d continents\n", nc);
448 printf("continent size: %d\n", sc);
449 printf("number of islands: %d\n", ni);
450 printf("average size of islands: %d\n", is);
451 printf("spike: %d%%\n", sp);
452 printf("%d%% of land is mountain (each continent will have %d mountains)\n",
453 pm, (pm * sc) / 100);
454 printf("minimum distance between continents: %d\n", di);
455 printf("minimum distance from islands to continents: %d\n", id);
456 printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
460 qprint(const char *const fmt, ...)
466 vfprintf(stdout, fmt, ap);
472 help(char *complaint)
475 fprintf(stderr, "%s: %s\n", program_name, complaint);
476 fprintf(stderr, "Try -h for help.\n");
482 printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
483 " -e CONFIG-FILE configuration file\n"
485 " -i islands may merge\n"
487 " -R SEED seed for random number generator\n"
488 " -s SCRIPT name of script to create (default %s)\n"
489 " -h display this help and exit\n"
490 " -v display version information and exit\n"
491 " NC number of continents\n"
492 " SC continent size\n"
493 " NI number of islands (default NC)\n"
494 " IS average island size (default SC/2)\n"
495 " SP spike percentage: 0 = round, 100 = snake (default %d)\n"
496 " PM percentage of land that is mountain (default %d)\n"
497 " DI minimum distance between continents (default %d)\n"
498 " ID minimum distance from islands to continents (default %d)\n",
499 program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
500 DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
504 parse_args(int argc, char *argv[])
506 int dist_max = mapdist(0, 0, WORLD_X / 2, WORLD_Y / 2);
509 help("missing arguments");
513 help("too many arguments");
518 fprintf(stderr, "%s: number of continents must be > 0\n",
525 fprintf(stderr, "%s: size of continents must be > 1\n",
536 fprintf(stderr, "%s: number of islands must be >= 0\n",
541 fprintf(stderr, "%s: number of islands must be a multiple of"
542 " the number of continents\n",
550 fprintf(stderr, "%s: size of islands must be > 0\n",
557 if (sp < 0 || sp > 100) {
559 "%s: spike percentage must be between 0 and 100\n",
566 if (pm < 0 || pm > 100) {
568 "%s: mountain percentage must be between 0 and 100\n",
576 fprintf(stderr, "%s: distance between continents must be >= 0\n",
581 fprintf(stderr, "%s: distance between continents too large\n",
590 "%s: distance from islands to continents must be >= 0\n",
596 "%s: distance from islands to continents too large\n",
602 /****************************************************************************
603 VARIABLE INITIALIZATION
604 ****************************************************************************/
607 allocate_memory(void)
611 cap = malloc(nc * sizeof(*cap));
612 own = malloc(WORLD_SZ() * sizeof(*own));
613 adj_land = malloc(WORLD_SZ() * sizeof(*adj_land));
614 elev = calloc(WORLD_SZ(), sizeof(*elev));
615 xzone = malloc(WORLD_SZ() * sizeof(*xzone));
616 seen = calloc(WORLD_SZ(), sizeof(*seen));
617 closest = malloc(WORLD_SZ() * sizeof(*closest));
618 distance = malloc(WORLD_SZ() * sizeof(*distance));
619 bfs_queue = malloc(WORLD_SZ() * sizeof(*bfs_queue));
620 isecs = calloc(nc + ni, sizeof(int));
621 sect = malloc((nc + ni) * sizeof(*sect));
622 for (i = 0; i < nc; i++)
623 sect[i] = malloc(sc * sizeof(**sect));
624 for (i = nc; i < nc + ni; i++)
625 sect[i] = malloc(is * 2 * sizeof(**sect));
633 for (i = 0; i < WORLD_SZ(); i++)
635 memset(adj_land, 0, WORLD_SZ() * sizeof(*adj_land));
638 /****************************************************************************
639 DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
640 ****************************************************************************/
643 * How isolated is capital @j at @newx,@newy?
644 * Return the distance to the closest other capital.
647 iso(int j, int newx, int newy)
652 for (i = 0; i < nc; ++i) {
655 md = mapdist(cap[i].x, cap[i].y, newx, newy);
665 * Return 1 for a stable drift, 0 for an unstable one.
672 for (i = 0; i < nc; i++) {
673 cap[i].y = (2 * i) / WORLD_X;
674 cap[i].x = (2 * i) % WORLD_X + cap[i].y % 2;
675 if (cap[i].y >= WORLD_Y) {
677 "%s: world not big enough for all the continents\n",
683 for (turns = 0; turns < DRIFT_MAX; ++turns) {
686 for (i = 0; i < nc; ++i)
693 * Has the drift stabilized?
694 * @turns is the number of turns so far.
699 static int mc[STABLE_CYCLE];
700 int i, isod, d = 0, stab = 1;
703 for (i = 0; i < STABLE_CYCLE; i++)
707 if (turns <= DRIFT_BEFORE_CHECK)
710 for (i = 0; i < nc; ++i) {
711 isod = iso(i, cap[i].x, cap[i].y);
716 for (i = 0; i < STABLE_CYCLE; ++i)
720 mc[turns % STABLE_CYCLE] = d;
726 * Move it to an adjacent sector where it is at least as isolated.
732 int dir, i, newx, newy;
734 dir = DIR_L + roll0(6);
735 for (i = 0; i < 6; i++) {
738 newx = new_x(cap[j].x + diroff[dir][0]);
739 newy = new_y(cap[j].y + diroff[dir][1]);
741 if (iso(j, newx, newy) >= iso(j, cap[j].x, cap[j].y)) {
749 /****************************************************************************
751 ****************************************************************************/
754 is_coastal(int x, int y)
756 return adj_land[XYOFFSET(x, y)]
757 != (1u << (DIR_LAST + 1)) - (1u << DIR_FIRST);
760 struct hexagon_iter {
765 * Start iterating around @x0,@y0 at distance @d.
766 * Set *x,*y to coordinates of the first sector.
769 hexagon_first(struct hexagon_iter *iter, int x0, int y0, int n,
772 *x = new_x(x0 - 2 * n);
774 iter->dir = DIR_FIRST;
780 * Continue iteration started with hexagon_first().
781 * Set *x,*y to coordinates of the next sector.
782 * Return whether we're back at the first sector, i.e. iteration is
786 hexagon_next(struct hexagon_iter *iter, int *x, int *y)
788 *x = new_x(*x + diroff[iter->dir][0]);
789 *y = new_y(*y + diroff[iter->dir][1]);
791 if (iter->i == iter->n) {
795 return iter->dir <= DIR_LAST;
799 * Is @x,@y in no exclusive zone other than perhaps @c's?
802 xzone_ok(int c, int x, int y)
804 int off = XYOFFSET(x, y);
806 return xzone[off] == c || xzone[off] == -1;
810 * Add sectors within distance @dist of @x,@y to @c's exclusive zone.
813 xzone_around_sector(int c, int x, int y, int dist)
816 struct hexagon_iter hexit;
818 assert(xzone_ok(c, x, y));
820 xzone[XYOFFSET(x, y)] = c;
821 for (d = 1; d <= dist; d++) {
822 hexagon_first(&hexit, x, y, d, &x1, &y1);
824 off = XYOFFSET(x1, y1);
825 if (xzone[off] == -1)
827 else if (xzone[off] != c)
829 } while (hexagon_next(&hexit, &x1, &y1));
834 * Add sectors within distance @dist to island @c's exclusive zone.
837 xzone_around_island(int c, int dist)
841 for (i = 0; i < isecs[c]; i++)
842 xzone_around_sector(c, sect[c][i].x, sect[c][i].y, dist);
846 * Initialize exclusive zones around @n islands.
853 for (i = 0; i < WORLD_SZ(); i++)
856 for (c = 0; c < n; c++)
857 xzone_around_island(c, id);
861 * Initialize breadth-first search.
868 for (i = 0; i < WORLD_SZ(); i++) {
870 distance[i] = USHRT_MAX;
873 bfs_queue_head = bfs_queue_tail = 0;
877 * Add sector @x,@y to the BFS queue.
878 * It's closest to @c, with distance @dist.
881 bfs_enqueue(int c, int x, int y, int dist)
883 int off = XYOFFSET(x, y);
885 assert(dist < distance[off]);
887 distance[off] = dist;
888 bfs_queue[bfs_queue_tail] = off;
890 if (bfs_queue_tail >= WORLD_SZ())
892 assert(bfs_queue_tail != bfs_queue_head);
896 * Search breadth-first until the queue is empty.
901 int off, dist, i, noff, nx, ny;
904 while (bfs_queue_head != bfs_queue_tail) {
905 off = bfs_queue[bfs_queue_head];
907 if (bfs_queue_head >= WORLD_SZ())
909 dist = distance[off] + 1;
910 sctoff2xy(&x, &y, off);
911 for (i = DIR_FIRST; i <= DIR_LAST; i++) {
912 nx = new_x(x + diroff[i][0]);
913 ny = new_y(y + diroff[i][1]);
914 noff = XYOFFSET(nx, ny);
915 if (dist < distance[noff]) {
916 bfs_enqueue(closest[off], nx, ny, dist);
917 } else if (distance[noff] == dist) {
918 if (closest[off] != closest[noff])
919 closest[noff] = (natid)-1;
921 assert(distance[noff] < dist);
927 * Add island @c's coastal sectors to the BFS queue, with distance 0.
930 bfs_enqueue_island(int c)
934 for (i = 0; i < isecs[c]; i++) {
935 if (is_coastal(sect[c][i].x, sect[c][i].y))
936 bfs_enqueue(c, sect[c][i].x, sect[c][i].y, 0);
941 * Enqueue spheres of influence borders for breadth-first search.
944 bfs_enqueue_border(void)
946 int x, y, off, dir, nx, ny, noff;
948 for (y = 0; y < WORLD_Y; y++) {
949 for (x = y % 2; x < WORLD_X; x += 2) {
950 off = XYOFFSET(x, y);
951 if (distance[off] <= id + 1)
953 if (closest[off] == (natid)-1)
955 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
956 nx = new_x(x + diroff[dir][0]);
957 ny = new_y(y + diroff[dir][1]);
958 noff = XYOFFSET(nx, ny);
959 if (closest[noff] != closest[off]) {
960 bfs_enqueue(closest[off], x, y, id + 1);
969 * Compute spheres of influence
970 * A continent's sphere of influence is the set of sectors closer to
971 * it than to any other continent.
972 * Set closest[XYOFFSET(x, y)] to the closest continent's number,
973 * -1 if no single continent is closest.
974 * Set distance[XYOFFSET(x, y)] to the minimum of the distance to the
975 * closest coastal land sector and the distance to just outside the
976 * sphere of influence plus @id. For sea sectors within a continent's
977 * sphere of influence, distance[off] - id is the distance to the
978 * border of the area where additional islands can be placed.
981 init_spheres_of_influence(void)
986 for (c = 0; c < nc; c++)
987 bfs_enqueue_island(c);
989 bfs_enqueue_border();
994 * Precompute distance to coast
995 * Set distance[XYOFFSET(x, y)] to the distance to the closest coastal
997 * Set closest[XYOFFSET(x, y)] to the closest continent's number,
998 * -1 if no single continent is closest.
1001 init_distance_to_coast(void)
1006 for (c = 0; c < nc + ni; c++)
1007 bfs_enqueue_island(c);
1012 * Is @x,@y in the same sphere of influence as island @c?
1013 * Always true when @c is a continent.
1016 is_in_sphere(int c, int x, int y)
1018 return c < nc || closest[XYOFFSET(x, y)] == c % nc;
1022 * Can island @c grow at @x,@y?
1025 can_grow_at(int c, int x, int y)
1027 return own[XYOFFSET(x, y)] == -1 && xzone_ok(c, x, y)
1028 && is_in_sphere(c, x, y);
1032 adj_land_update(int x, int y)
1034 int is_land = own[XYOFFSET(x, y)] != -1;
1035 int dir, nx, ny, noff;
1037 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
1038 nx = new_x(x + diroff[dir][0]);
1039 ny = new_y(y + diroff[dir][1]);
1040 noff = XYOFFSET(nx, ny);
1042 adj_land[noff] |= 1u << DIR_BACK(dir);
1044 adj_land[noff] &= ~(1u << DIR_BACK(dir));
1049 add_sector(int c, int x, int y)
1051 int off = XYOFFSET(x, y);
1053 assert(own[off] == -1);
1054 xzone_around_sector(c, x, y, c < nc ? di : DISTINCT_ISLANDS ? id : 0);
1055 sect[c][isecs[c]].x = x;
1056 sect[c][isecs[c]].y = y;
1059 adj_land_update(x, y);
1062 static int grow_weight(int c, int x, int y, int spike)
1067 * #Land neighbors is #bits set in adj_land[].
1068 * Count them Brian Kernighan's way.
1071 for (b = adj_land[XYOFFSET(x, y)]; b; b &= b - 1)
1073 assert(n > 0 && n < 7);
1076 return (6 - n) * (6 - n);
1082 grow_one_sector(int c)
1084 int spike = roll0(100) < sp;
1085 int wsum, newx, newy, i, x, y, off, dir, nx, ny, noff, w;
1087 assert(cur_seen < UINT_MAX);
1092 for (i = 0; i < isecs[c]; i++) {
1095 off = XYOFFSET(x, y);
1097 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
1098 if (adj_land[off] & (1u << dir))
1100 nx = new_x(x + diroff[dir][0]);
1101 ny = new_y(y + diroff[dir][1]);
1102 noff = XYOFFSET(nx, ny);
1103 if (seen[noff] == cur_seen)
1105 assert(seen[noff] < cur_seen);
1106 seen[noff] = cur_seen;
1107 if (!can_grow_at(c, nx, ny))
1109 w = grow_weight(c, nx, ny, spike);
1110 assert(wsum < INT_MAX - w);
1112 if (roll0(wsum) < w) {
1122 add_sector(c, newx, newy);
1127 * Grow the continents.
1128 * Return 1 on success, 0 on error.
1131 grow_continents(void)
1138 for (c = 0; c < nc; ++c) {
1140 if (!can_grow_at(c, cap[c].x, cap[c].y)
1141 || !can_grow_at(c, new_x(cap[c].x + 2), cap[c].y)) {
1145 add_sector(c, cap[c].x, cap[c].y);
1146 add_sector(c, new_x(cap[c].x + 2), cap[c].y);
1150 qprint("No room for continents\n");
1154 for (secs = 2; secs < sc && done; secs++) {
1155 for (c = 0; c < nc; ++c) {
1156 if (!grow_one_sector(c))
1162 qprint("Only managed to grow %d out of %d sectors.\n",
1167 /****************************************************************************
1169 ****************************************************************************/
1172 * Place additional island @c's first sector.
1173 * Return 1 on success, 0 on error.
1176 place_island(int c, int isiz)
1178 int n, x, y, d, w, newx, newy;
1182 for (y = 0; y < WORLD_Y; y++) {
1183 for (x = y % 2; x < WORLD_X; x += 2) {
1184 if (can_grow_at(c, x, y)) {
1185 d = distance[XYOFFSET(x, y)];
1187 w = (d - id) * (d - id);
1188 n += MIN(w, (isiz + 2) / 3);
1198 add_sector(c, newx, newy);
1203 int_cmp(const void *a, const void *b)
1205 return *(int *)b - *(int *)a;
1212 int *isiz = malloc(n * sizeof(*isiz));
1217 for (i = 1; i < n; i++) {
1220 isiz[i] = is + r1 - r0;
1224 qsort(isiz, n, sizeof(*isiz), int_cmp);
1229 * Grow the additional islands.
1230 * Return 1 on success, 0 on error.
1235 int *island_size = size_islands();
1236 int xzone_valid = 0;
1238 int i, j, c, done, secs, isiz, x, y;
1240 init_spheres_of_influence();
1242 for (i = 0; i < ni / nc; i++) {
1248 carry += island_size[i];
1249 isiz = MIN(2 * is, carry);
1251 for (j = 0; j < nc; j++) {
1253 if (!place_island(c + j, isiz)) {
1254 qprint("\nNo room for island #%d\n", c - nc + j + 1);
1261 for (secs = 1; secs < isiz && done; secs++) {
1262 for (j = 0; j < nc; j++) {
1263 if (!grow_one_sector(c + j))
1270 for (j = 0; j < nc; j++) {
1271 if (isecs[c + j] != secs) {
1273 assert(isecs[c + j] == secs);
1274 x = sect[c + j][secs].x;
1275 y = sect[c + j][secs].y;
1276 own[XYOFFSET(x, y)] = -1;
1277 adj_land_update(x, y);
1283 for (j = 0; j < nc; j++)
1284 qprint(" %d(%d)", c - nc + j + 1, isecs[c + j]);
1293 qprint("Only managed to grow %d out of %d island sectors.\n",
1294 is * ni - carry * nc, is * ni);
1299 /****************************************************************************
1301 ****************************************************************************/
1303 create_elevations(void)
1311 elev_cmp(const void *p, const void *q)
1315 int delev = elev[a] - elev[b];
1317 return delev ? delev : a - b;
1323 int n = WORLD_SZ() * 8;
1324 int off0, r, sign, elevation, d, x1, y1, off1;
1326 struct hexagon_iter hexit;
1328 init_distance_to_coast();
1331 off0 = roll0(WORLD_SZ());
1332 sctoff2xy(&x0, &y0, off0);
1333 if (own[off0] == -1) {
1334 r = roll(MIN(3, distance[off0]));
1337 r = roll(MIN(3, distance[off0]) + 1);
1340 elevation = elev[off0] + sign * r * r;
1341 elev[off0] = LIMIT_TO(elevation, SHRT_MIN, SHRT_MAX);
1343 for (d = 1; d < r; d++) {
1344 hexagon_first(&hexit, x0, y0, d, &x1, &y1);
1346 off1 = XYOFFSET(x1, y1);
1347 elevation = elev[off1] + sign * (r * r - d * d);
1348 elev[off1] = LIMIT_TO(elevation, SHRT_MIN, SHRT_MAX);
1350 } while (hexagon_next(&hexit, &x1, &y1));
1358 int *off = malloc(MAX(sc, is * 2) * sizeof(*off));
1359 int max_nm = (pm * MAX(sc, is * 2)) / 100;
1360 int c, nm, i0, n, i;
1361 double elevation, delta;
1363 for (c = 0; c < nc + ni; c++) {
1364 nm = (pm * isecs[c]) / 100;
1365 i0 = c < nc ? 2 : 0;
1367 for (i = 0; i < i0; i++)
1368 elev[XYOFFSET(sect[c][i].x, sect[c][i].y)] = PLATMIN;
1369 for (i = 0; i < n; i++)
1370 off[i] = XYOFFSET(sect[c][i0 + i].x, sect[c][i0 + i].y);
1371 qsort(off, n, sizeof(*off), elev_cmp);
1372 delta = (double)(HIGHMIN - LANDMIN - 1) / (n - nm - 1);
1373 elevation = LANDMIN;
1374 for (i = 0; i < n - nm; i++) {
1375 elev[off[i]] = (int)(elevation + 0.5);
1378 elevation = HIGHMIN;
1379 delta = (127.0 - HIGHMIN) / max_nm;
1380 for (; i < n; i++) {
1382 elev[off[i]] = (int)(elevation + 0.5);
1395 for (i = 0; i < WORLD_SZ(); i++) {
1400 for (i = 0; i < WORLD_SZ(); i++) {
1402 elev[i] = -1 - 126 * elev[i] / min;
1407 elev_to_sct_type(int elevation)
1409 if (elevation < LANDMIN)
1411 if (elevation < HIGHMIN)
1416 /****************************************************************************
1418 ****************************************************************************/
1421 * Map elevation @elev to a resource value according to @conf.
1422 * This is a linear interpolation on the data points in @conf.
1425 elev_to_resource(int elev, struct resource_point conf[])
1427 int i, elev1, elev2, delev, res1, res2, dres;
1429 for (i = 1; elev > conf[i].elev; i++) ;
1430 assert(conf[i - 1].elev <= elev);
1432 elev1 = conf[i - 1].elev;
1433 elev2 = conf[i].elev;
1434 delev = elev2 - elev1;
1435 res1 = conf[i - 1].res;
1438 return (int)(res1 + (double)((elev - elev1) * dres) / delev);
1442 add_resources(struct sctstr *sct)
1444 sct->sct_min = elev_to_resource(sct->sct_elev, iron_conf);
1445 sct->sct_gmin = elev_to_resource(sct->sct_elev, gold_conf);
1446 sct->sct_fertil = elev_to_resource(sct->sct_elev, fert_conf);
1447 sct->sct_oil = elev_to_resource(sct->sct_elev, oil_conf);
1448 sct->sct_uran = elev_to_resource(sct->sct_elev, uran_conf);
1451 /****************************************************************************
1452 DESIGNATE THE SECTORS
1453 ****************************************************************************/
1461 for (i = 0; i < WORLD_SZ(); i++) {
1463 sp->sct_elev = elev[i];
1464 sp->sct_type = elev_to_sct_type(sp->sct_elev);
1465 sp->sct_newtype = sp->sct_type;
1466 sp->sct_dterr = own[i] + 1;
1467 sp->sct_coastal = is_coastal(sp->sct_x, sp->sct_y);
1472 /****************************************************************************
1473 PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1474 ****************************************************************************/
1478 int sx, sy, x, y, off, c, type;
1481 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1486 for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1488 off = XYOFFSET(x, y);
1490 type = elev_to_sct_type(elev[off]);
1491 if (type == SCT_WATER)
1493 else if (type == SCT_MOUNT)
1498 assert(0 <= c && c < nc);
1499 if ((x == cap[c].x || x == new_x(cap[c].x + 2))
1501 printf("%c ", numletter[c % 62]);
1511 * Print a map to help visualize own[].
1512 * This is for debugging.
1517 int sx, sy, x, y, off;
1519 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1522 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1524 off = XYOFFSET(x, y);
1527 else if (own[off] == -1)
1530 putchar(numletter[own[off] % 62]);
1537 * Print a map to help visualize elev[].
1538 * This is for debugging. It expects the terminal to understand
1539 * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1542 print_elev_map(void)
1544 int sx, sy, x, y, off, sat;
1546 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1549 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1551 off = XYOFFSET(x, y);
1554 else if (!elev[off])
1556 else if (elev[off] < 0) {
1557 sat = 256 + elev[off] * 2;
1558 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1559 } else if (elev[off] < HIGHMIN / 2) {
1560 sat = (HIGHMIN / 2 - elev[off]) * 4;
1561 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1562 } else if (elev[off] < HIGHMIN) {
1563 sat = 128 + (HIGHMIN - elev[off]) * 2;
1564 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1566 sat = 128 + (elev[off] - HIGHMIN) * 2;
1567 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1575 * Print a map to help visualize xzone[].
1576 * This is for debugging.
1579 print_xzone_map(void)
1581 int sx, sy, x, y, off;
1583 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1586 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1588 off = XYOFFSET(x, y);
1591 else if (own[off] >= 0)
1593 else if (xzone[off] >= 0)
1594 putchar(numletter[xzone[off] % 62]);
1596 assert(own[off] == -1);
1597 putchar(xzone[off] == -1 ? '.' : '!');
1605 * Print a map to help visualize closest[].
1606 * This is for debugging.
1609 print_closest_map(void)
1611 int sx, sy, x, y, off;
1613 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1616 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1618 off = XYOFFSET(x, y);
1621 else if (closest[off] == (natid)-1)
1623 else if (!distance[off]) {
1624 assert(closest[off] == own[off]);
1627 putchar(numletter[closest[off] % 62]);
1635 print_distance_map(void)
1637 int sx, sy, x, y, off;
1639 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1642 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1644 off = XYOFFSET(x, y);
1647 else if (closest[off] == (natid)-1)
1649 else if (!distance[off]) {
1650 assert(closest[off] == own[off]);
1653 putchar(numletter[distance[off] % 62]);
1661 /***************************************************************************
1662 WRITE A SCRIPT FOR PLACING CAPITALS
1663 ****************************************************************************/
1665 write_newcap_script(void)
1668 FILE *script = fopen(outfile, "w");
1671 fprintf(stderr, "%s: unable to write to %s (%s)\n",
1672 program_name, outfile, strerror(errno));
1676 for (c = 0; c < nc; ++c) {
1677 fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1678 fprintf(script, "newcap %d %d,%d\n", c + 1, cap[c].x, cap[c].y);
1680 fprintf(script, "add %d visitor visitor v\n", c + 1);