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 * Pick an island size, and place one island's first sector into each
71 * sphere, randomly. Then add one sector to each island in turn,
72 * until they have the intended size. Repeat until the specified
73 * number of islands has been grown.
75 * If placement fails due to lack of room, start over, just like for
78 * Growing works as for continents, except the minimum distance for
79 * additional islands applies, and growing simply stops when any of
80 * the islands being grown lacks the room to grow further.
82 * 4. Compute elevation
84 * Elevate islands one after the other.
86 * First, place the specified number of mountains randomly.
87 * Probability increases with distance to sea.
89 * Last, elevate mountains and the capitals. Pick coastal mountain
90 * elevation randomly from an interval of medium elevations reserved
91 * for them. Pick non-coastal mountain elevation randomly from an
92 * interval of high elevation reserved for them. Set capital
93 * elevation to a fixed, medium value.
95 * In between, elevate the remaining land one by one, working from
96 * mountains towards the sea, and from the elevation just below the
97 * non-coastal mountains' interval linearly down to 1, avoiding the
98 * coastal mountains' interval.
100 * This gives islands of the same size the same set of elevations,
101 * except for mountains.
103 * Elevate sea: pick a random depth from an interval that deepens with
104 * the distance to land.
108 * Sector resources are simple functions of elevation. You can alter
109 * macros OIL_MAX, IRON_MIN, GOLD_MIN, FERT_MAX, and URAN_MIN to
124 #include "prototypes.h"
129 /* The following five numbers refer to elevation under which (in the case of
130 fertility or oil) or over which (in the case of iron, gold, and uranium)
131 sectors with that elevation will contain that resource. Elevation ranges
134 /* raise FERT_MAX for more fertility */
137 /* raise OIL_MAX for more oil */
140 /* lower IRON_MIN for more iron */
143 /* lower GOLD_MIN for more gold */
146 /* lower URAN_MIN for more uranium */
149 /* do not change these 4 defines */
150 #define LANDMIN 1 /* plate altitude for normal land */
151 #define HILLMIN 34 /* plate altitude for hills */
152 #define PLATMIN 36 /* plate altitude for plateau */
153 #define HIGHMIN 98 /* plate altitude for mountains */
155 static void qprint(const char * const fmt, ...)
156 ATTRIBUTE((format (printf, 1, 2)));
159 * Program arguments and options
161 static char *program_name;
162 static int nc, sc; /* number and size of continents */
163 static int ni, is; /* number and size of islands */
164 #define DEFAULT_SPIKE 10
165 static int sp = DEFAULT_SPIKE; /* spike percentage */
166 #define DEFAULT_MOUNTAIN 0
167 static int pm = DEFAULT_MOUNTAIN; /* mountain percentage */
168 #define DEFAULT_CONTDIST 2
169 static int di = DEFAULT_CONTDIST; /* min. distance between continents */
170 #define DEFAULT_ISLDIST 1
171 static int id = DEFAULT_ISLDIST; /* ... continents and islands */
172 /* don't let the islands crash into each other.
173 1 = don't merge, 0 = merge. */
174 static int DISTINCT_ISLANDS = 1;
176 #define DEFAULT_OUTFILE_NAME "newcap_script"
177 static const char *outfile = DEFAULT_OUTFILE_NAME;
179 #define STABLE_CYCLE 4 /* stability required for perterbed capitals */
180 #define INFINITE_ELEVATION 999
182 /* these defines prevent infinite loops:
184 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
185 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
186 #define MOUNTAIN_SEARCH_MAX 1000 /* how long do we try to place mountains */
191 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
192 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
196 * isecs[i] is the size of the i-th island.
200 static int *capx, *capy; /* location of the nc capitals */
202 static int **own; /* owner of the sector. -1 means water */
205 * Adjacent land sectors
206 * adj_land[XYOFFSET(x, y)] bit d is set exactly when the sector next
207 * to x, y in direction d is land.
209 static unsigned char *adj_land;
213 * Each island is surrounded by an exclusive zone where only it may
214 * grow. The width of the zone depends on minimum distances.
215 * While growing continents, it is @di sectors wide.
216 * While growing additional islands, it is @id sectors wide.
217 * DISTINCT_ISLANDS nullifies the exclusive zone then.
218 * xzone[XYOFFSET(x, y)] is -1 when the sector is in no exclusive
219 * zone, a (non-negative) island number when it is in that island's
220 * exclusive zone and no other, and -2 when it is in multiple
226 * Set of sectors seen already
227 * Increment @cur_seen to empty the set of sectors seen, set
228 * seen[XYOFFSET(x, y)] to @cur_seen to add x,y to the set.
230 static unsigned *seen;
231 static unsigned cur_seen;
234 * Closest continent and "distance"
235 * closest[XYOFFSET(x, y)] is the closest continent's number.
236 * distance[] is complicated; see init_spheres_of_influence().
238 static natid *closest;
239 static unsigned short *distance;
242 * Queue for breadth-first search
244 static int *bfs_queue;
245 static int bfs_queue_head, bfs_queue_tail;
247 static int **elev; /* elevation of the sectors */
248 static int **sectx, **secty; /* the sectors for each continent */
249 static int **sectc; /* which sectors are on the coast? */
250 static int *weight; /* used for placing mountains */
251 static int *dsea, *dmoun; /* the dist to the ocean and mountain */
253 #define NUMTRIES 10 /* keep trying to grow this many times */
255 static const char *numletter =
256 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
258 static void help(char *);
259 static void usage(void);
260 static void parse_args(int argc, char *argv[]);
261 static void allocate_memory(void);
262 static void init(void);
263 static int drift(void);
264 static int grow_continents(void);
265 static void create_elevations(void);
266 static void write_sects(void);
267 static void output(void);
268 static int write_newcap_script(void);
269 static int stable(int);
270 static void elevate_land(void);
271 static void elevate_sea(void);
272 static void set_coastal_flags(void);
274 static void print_vars(void);
275 static void fl_move(int);
276 static int grow_islands(void);
278 /* Debugging aids: */
279 void print_own_map(void);
280 void print_xzone_map(void);
281 void print_closest_map(void);
282 void print_distance_map(void);
283 void print_elev_map(void);
285 /****************************************************************************
287 ****************************************************************************/
290 main(int argc, char *argv[])
293 char *config_file = NULL;
295 unsigned rnd_seed = 0;
298 program_name = argv[0];
300 while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
303 config_file = optarg;
306 DISTINCT_ISLANDS = 0;
312 rnd_seed = strtoul(optarg, NULL, 10);
322 printf("%s\n\n%s", version, legal);
331 rnd_seed = pick_seed();
334 if (emp_config(config_file) < 0)
338 parse_args(argc - optind, argv + optind);
343 qprint("\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
344 qprint("seed is %u\n", rnd_seed);
349 qprint("\ntry #%d (out of %d)...\n", try + 1, NUMTRIES);
350 qprint("placing capitals...\n");
352 qprint("unstable drift\n");
353 qprint("growing continents...\n");
354 done = grow_continents();
357 qprint("growing islands:");
358 done = grow_islands();
359 } while (!done && ++try < NUMTRIES);
361 fprintf(stderr, "%s: world not large enough for this much land\n",
365 qprint("elevating land...\n");
368 qprint("writing to sectors file...\n");
369 if (!write_newcap_script())
371 if (chdir(gamedir)) {
372 fprintf(stderr, "%s: can't chdir to %s (%s)\n",
373 program_name, gamedir, strerror(errno));
376 if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
379 if (!ef_close(EF_SECTOR))
383 qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
393 puts("Creating a planet with:\n");
394 printf("%d continents\n", nc);
395 printf("continent size: %d\n", sc);
396 printf("number of islands: %d\n", ni);
397 printf("average size of islands: %d\n", is);
398 printf("spike: %d%%\n", sp);
399 printf("%d%% of land is mountain (each continent will have %d mountains)\n",
400 pm, (pm * sc) / 100);
401 printf("minimum distance between continents: %d\n", di);
402 printf("minimum distance from islands to continents: %d\n", id);
403 printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
407 help(char *complaint)
410 fprintf(stderr, "%s: %s\n", program_name, complaint);
411 fprintf(stderr, "Try -h for help.\n");
417 printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
418 " -e CONFIG-FILE configuration file\n"
420 " -i islands may merge\n"
422 " -R SEED seed for random number generator\n"
423 " -s SCRIPT name of script to create (default %s)\n"
424 " -h display this help and exit\n"
425 " -v display version information and exit\n"
426 " NC number of continents\n"
427 " SC continent size\n"
428 " NI number of islands (default NC)\n"
429 " IS average island size (default SC/2)\n"
430 " SP spike percentage: 0 = round, 100 = snake (default %d)\n"
431 " PM percentage of land that is mountain (default %d)\n"
432 " DI minimum distance between continents (default %d)\n"
433 " ID minimum distance from islands to continents (default %d)\n",
434 program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
435 DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
439 parse_args(int argc, char *argv[])
441 int dist_max = mapdist(0, 0, WORLD_X / 2, WORLD_Y / 2);
444 help("missing arguments");
448 help("too many arguments");
453 fprintf(stderr, "%s: number of continents must be > 0\n",
460 fprintf(stderr, "%s: size of continents must be > 1\n",
471 fprintf(stderr, "%s: number of islands must be >= 0\n",
476 fprintf(stderr, "%s: number of islands must be a multiple of"
477 " the number of continents\n",
485 fprintf(stderr, "%s: size of islands must be > 0\n",
492 if (sp < 0 || sp > 100) {
494 "%s: spike percentage must be between 0 and 100\n",
501 if (pm < 0 || pm > 100) {
503 "%s: mountain percentage must be between 0 and 100\n",
511 fprintf(stderr, "%s: distance between continents must be >= 0\n",
516 fprintf(stderr, "%s: distance between continents too large\n",
525 "%s: distance from islands to continents must be >= 0\n",
531 "%s: distance from islands to continents too large\n",
537 /****************************************************************************
538 VARIABLE INITIALIZATION
539 ****************************************************************************/
542 allocate_memory(void)
546 capx = calloc(nc, sizeof(int));
547 capy = calloc(nc, sizeof(int));
548 own = calloc(WORLD_X, sizeof(int *));
549 adj_land = malloc(WORLD_SZ() * sizeof(*adj_land));
550 xzone = malloc(WORLD_SZ() * sizeof(*xzone));
551 seen = calloc(WORLD_SZ(), sizeof(*seen));
552 closest = malloc(WORLD_SZ() * sizeof(*closest));
553 distance = malloc(WORLD_SZ() * sizeof(*distance));
554 bfs_queue = malloc(WORLD_SZ() * sizeof(*bfs_queue));
555 elev = calloc(WORLD_X, sizeof(int *));
556 for (i = 0; i < WORLD_X; ++i) {
557 own[i] = calloc(WORLD_Y, sizeof(int));
558 elev[i] = calloc(WORLD_Y, sizeof(int));
560 sectx = calloc(nc + ni, sizeof(int *));
561 secty = calloc(nc + ni, sizeof(int *));
562 sectc = calloc(nc + ni, sizeof(int *));
563 isecs = calloc(nc + ni, sizeof(int));
564 weight = calloc(MAX(sc, is * 2), sizeof(int));
565 dsea = calloc(MAX(sc, is * 2), sizeof(int));
566 dmoun = calloc(MAX(sc, is * 2), sizeof(int));
567 for (i = 0; i < nc; ++i) {
568 sectx[i] = calloc(sc, sizeof(int));
569 secty[i] = calloc(sc, sizeof(int));
570 sectc[i] = calloc(sc, sizeof(int));
572 for (i = nc; i < nc + ni; ++i) {
573 sectx[i] = calloc(is * 2, sizeof(int));
574 secty[i] = calloc(is * 2, sizeof(int));
575 sectc[i] = calloc(is * 2, sizeof(int));
585 for (i = 0; i < WORLD_X; ++i) {
586 for (j = 0; j < WORLD_Y; ++j) {
590 memset(adj_land, 0, WORLD_SZ() * sizeof(*adj_land));
593 /****************************************************************************
594 DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
595 ****************************************************************************/
598 * How isolated is capital @j at @newx,@newy?
599 * Return the distance to the closest other capital.
602 iso(int j, int newx, int newy)
607 for (i = 0; i < nc; ++i) {
610 md = mapdist(capx[i], capy[i], newx, newy);
620 * Return 1 for a stable drift, 0 for an unstable one.
627 for (i = 0; i < nc; i++) {
628 capy[i] = (2 * i) / WORLD_X;
629 capx[i] = (2 * i) % WORLD_X + capy[i] % 2;
630 if (capy[i] >= WORLD_Y) {
632 "%s: world not big enough for all the continents\n",
638 for (turns = 0; turns < DRIFT_MAX; ++turns) {
641 for (i = 0; i < nc; ++i)
648 * Has the drift stabilized?
649 * @turns is the number of turns so far.
654 static int mc[STABLE_CYCLE];
655 int i, isod, d = 0, stab = 1;
658 for (i = 0; i < STABLE_CYCLE; i++)
662 if (turns <= DRIFT_BEFORE_CHECK)
665 for (i = 0; i < nc; ++i) {
666 isod = iso(i, capx[i], capy[i]);
671 for (i = 0; i < STABLE_CYCLE; ++i)
675 mc[turns % STABLE_CYCLE] = d;
679 /* This routine does the actual drifting
685 int dir, i, newx, newy;
687 dir = DIR_L + roll0(6);
688 for (i = 0; i < 6; i++) {
691 newx = new_x(capx[j] + diroff[dir][0]);
692 newy = new_y(capy[j] + diroff[dir][1]);
694 if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) {
702 /****************************************************************************
704 ****************************************************************************/
706 /* Look for a coastal sector of continent c
714 for (i = 0; i < isecs[c]; ++i) {
716 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
717 nx = new_x(sectx[c][i] + diroff[dir][0]);
718 ny = new_y(secty[c][i] + diroff[dir][1]);
719 if (own[nx][ny] == -1)
725 struct hexagon_iter {
730 * Start iterating around @x0,@y0 at distance @d.
731 * Set *x,*y to coordinates of the first sector.
734 hexagon_first(struct hexagon_iter *iter, int x0, int y0, int n,
737 *x = new_x(x0 - 2 * n);
739 iter->dir = DIR_FIRST;
745 * Continue iteration started with hexagon_first().
746 * Set *x,*y to coordinates of the next sector.
747 * Return whether we're back at the first sector, i.e. iteration is
751 hexagon_next(struct hexagon_iter *iter, int *x, int *y)
753 *x = new_x(*x + diroff[iter->dir][0]);
754 *y = new_y(*y + diroff[iter->dir][1]);
756 if (iter->i == iter->n) {
760 return iter->dir <= DIR_LAST;
764 * Is @x,@y in no exclusive zone other than perhaps @c's?
767 xzone_ok(int c, int x, int y)
769 int off = XYOFFSET(x, y);
771 return xzone[off] == c || xzone[off] == -1;
775 * Add sectors within distance @dist of @x,@y to @c's exclusive zone.
778 xzone_around_sector(int c, int x, int y, int dist)
781 struct hexagon_iter hexit;
783 assert(xzone_ok(c, x, y));
785 xzone[XYOFFSET(x, y)] = c;
786 for (d = 1; d <= dist; d++) {
787 hexagon_first(&hexit, x, y, d, &x1, &y1);
789 off = XYOFFSET(x1, y1);
790 if (xzone[off] == -1)
792 else if (xzone[off] != c)
794 } while (hexagon_next(&hexit, &x1, &y1));
799 * Add sectors within distance @dist to island @c's exclusive zone.
802 xzone_around_island(int c, int dist)
806 for (i = 0; i < isecs[c]; i++)
807 xzone_around_sector(c, sectx[c][i], secty[c][i], dist);
811 * Initialize exclusive zones around @n islands.
818 for (i = 0; i < WORLD_SZ(); i++)
821 for (c = 0; c < n; c++)
822 xzone_around_island(c, id);
826 * Initialize breadth-first search.
833 for (i = 0; i < WORLD_SZ(); i++) {
835 distance[i] = USHRT_MAX;
838 bfs_queue_head = bfs_queue_tail = 0;
842 * Add sector @x,@y to the BFS queue.
843 * It's closest to @c, with distance @dist.
846 bfs_enqueue(int c, int x, int y, int dist)
848 int off = XYOFFSET(x, y);
850 assert(dist < distance[off]);
852 distance[off] = dist;
853 bfs_queue[bfs_queue_tail] = off;
855 if (bfs_queue_tail >= WORLD_SZ())
857 assert(bfs_queue_tail != bfs_queue_head);
861 * Search breadth-first until the queue is empty.
866 int off, dist, i, noff, nx, ny;
869 while (bfs_queue_head != bfs_queue_tail) {
870 off = bfs_queue[bfs_queue_head];
872 if (bfs_queue_head >= WORLD_SZ())
874 dist = distance[off] + 1;
875 sctoff2xy(&x, &y, off);
876 for (i = DIR_FIRST; i <= DIR_LAST; i++) {
877 nx = new_x(x + diroff[i][0]);
878 ny = new_y(y + diroff[i][1]);
879 noff = XYOFFSET(nx, ny);
880 if (dist < distance[noff]) {
881 bfs_enqueue(closest[off], nx, ny, dist);
882 } else if (distance[noff] == dist) {
883 if (closest[off] != closest[noff])
884 closest[noff] = (natid)-1;
886 assert(distance[noff] < dist);
892 * Add island @c's coastal sectors to the BFS queue, with distance 0.
895 bfs_enqueue_island(int c)
899 for (i = 0; i < isecs[c]; i++) {
901 bfs_enqueue(c, sectx[c][i], secty[c][i], 0);
906 * Compute spheres of influence
907 * A continent's sphere of influence is the set of sectors closer to
908 * it than to any other continent.
909 * Set closest[XYOFFSET(x, y)] to the closest continent's number,
910 * -1 if no single continent is closest.
911 * Set distance[XYOFFSET(x, y)] to the distance to the closest coastal
915 init_spheres_of_influence(void)
920 for (c = 0; c < nc; c++)
921 bfs_enqueue_island(c);
926 * Is @x,@y in the same sphere of influence as island @c?
927 * Always true when @c is a continent.
930 is_in_sphere(int c, int x, int y)
932 return c < nc || closest[XYOFFSET(x, y)] == c % nc;
936 * Can island @c grow at @x,@y?
939 can_grow_at(int c, int x, int y)
941 return own[x][y] == -1 && xzone_ok(c, x, y) && is_in_sphere(c, x, y);
945 adj_land_update(int x, int y)
947 int is_land = own[x][y] != -1;
948 int dir, nx, ny, noff;
950 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
951 nx = new_x(x + diroff[dir][0]);
952 ny = new_y(y + diroff[dir][1]);
953 noff = XYOFFSET(nx, ny);
955 adj_land[noff] |= 1u << DIR_BACK(dir);
957 adj_land[noff] &= ~(1u << DIR_BACK(dir));
962 add_sector(int c, int x, int y)
964 assert(own[x][y] == -1);
965 xzone_around_sector(c, x, y, c < nc ? di : DISTINCT_ISLANDS ? id : 0);
966 sectx[c][isecs[c]] = x;
967 secty[c][isecs[c]] = y;
970 adj_land_update(x, y);
973 static int grow_weight(int c, int x, int y, int spike)
978 * #Land neighbors is #bits set in adj_land[].
979 * Count them Brian Kernighan's way.
982 for (b = adj_land[XYOFFSET(x, y)]; b; b &= b - 1)
984 assert(n > 0 && n < 7);
987 return (6 - n) * (6 - n);
993 grow_one_sector(int c)
995 int spike = roll0(100) < sp;
996 int wsum, newx, newy, i, x, y, off, dir, nx, ny, noff, w;
998 assert(cur_seen < UINT_MAX);
1003 for (i = 0; i < isecs[c]; i++) {
1006 off = XYOFFSET(x, y);
1008 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
1009 if (adj_land[off] & (1u << dir))
1011 nx = new_x(x + diroff[dir][0]);
1012 ny = new_y(y + diroff[dir][1]);
1013 noff = XYOFFSET(nx, ny);
1014 if (seen[noff] == cur_seen)
1016 assert(seen[noff] < cur_seen);
1017 seen[noff] = cur_seen;
1018 if (!can_grow_at(c, nx, ny))
1020 w = grow_weight(c, nx, ny, spike);
1021 assert(wsum < INT_MAX - w);
1023 if (roll0(wsum) < w) {
1033 add_sector(c, newx, newy);
1038 * Grow the continents.
1039 * Return 1 on success, 0 on error.
1042 grow_continents(void)
1049 for (c = 0; c < nc; ++c) {
1051 if (!can_grow_at(c, capx[c], capy[c])
1052 || !can_grow_at(c, new_x(capx[c] + 2), capy[c])) {
1056 add_sector(c, capx[c], capy[c]);
1057 add_sector(c, new_x(capx[c] + 2), capy[c]);
1061 qprint("No room for continents\n");
1065 for (secs = 2; secs < sc && done; secs++) {
1066 for (c = 0; c < nc; ++c) {
1067 if (!grow_one_sector(c))
1072 for (c = 0; c < nc; ++c)
1076 qprint("Only managed to grow %d out of %d sectors.\n",
1081 /****************************************************************************
1083 ****************************************************************************/
1086 * Place additional island @c's first sector.
1087 * Return 1 on success, 0 on error.
1092 int n, x, y, newx, newy;
1096 for (y = 0; y < WORLD_Y; y++) {
1097 for (x = y % 2; x < WORLD_X; x += 2) {
1098 if (can_grow_at(c, x, y)) {
1109 add_sector(c, newx, newy);
1114 * Grow the additional islands.
1115 * Return 1 on success, 0 on error.
1121 int stunted_islands = 0;
1122 int xzone_valid = 0;
1123 int i, j, c, done, secs, isiz, x, y;
1125 init_spheres_of_influence();
1127 for (i = 0; i < n; i++) {
1133 isiz = roll(is) + roll0(is);
1135 for (j = 0; j < nc; j++) {
1137 if (!place_island(c + j)) {
1138 qprint("\nNo room for island #%d\n", c - nc + j + 1);
1144 for (secs = 1; secs < isiz && done; secs++) {
1145 for (j = 0; j < nc; j++) {
1146 if (!grow_one_sector(c + j))
1153 for (j = 0; j < nc; j++) {
1154 if (isecs[c + j] != secs) {
1156 assert(isecs[c + j] == secs);
1157 x = sectx[c + j][secs];
1158 y = secty[c + j][secs];
1160 adj_land_update(x, y);
1166 for (j = 0; j < nc; j++)
1167 stunted_islands += isecs[c + j] != isiz;
1169 for (j = 0; j < nc; j++)
1170 qprint(" %d(%d)", c - nc + j + 1, isecs[c + j]);
1175 if (stunted_islands)
1176 qprint("%d stunted island%s\n",
1177 stunted_islands, splur(stunted_islands));
1179 for (c = nc; c < nc + ni; c++)
1185 /****************************************************************************
1187 ****************************************************************************/
1189 create_elevations(void)
1193 for (i = 0; i < WORLD_X; i++) {
1194 for (j = 0; j < WORLD_Y; j++)
1195 elev[i][j] = -INFINITE_ELEVATION;
1201 /* Generic function for finding the distance to the closest sea, land, or
1205 distance_to_what(int x, int y, int flag)
1208 struct hexagon_iter hexit;
1210 for (d = 1; d < 5; ++d) {
1211 hexagon_first(&hexit, x, y, d, &px, &py);
1214 case 0: /* distance to sea */
1215 if (own[px][py] == -1)
1218 case 1: /* distance to land */
1219 if (own[px][py] != -1)
1222 case 2: /* distance to mountain */
1223 if (elev[px][py] == INFINITE_ELEVATION)
1227 } while (hexagon_next(&hexit, &px, &py));
1232 #define ELEV elev[sectx[c][i]][secty[c][i]]
1233 #define distance_to_sea() (sectc[c][i]?1:distance_to_what(sectx[c][i], secty[c][i], 0))
1234 #define distance_to_mountain() distance_to_what(sectx[c][i], secty[c][i], 2)
1236 /* Decide where the mountains go
1241 int i, mountain_search, k, c, total, ns, nm, highest, where, h, newk,
1244 for (c = 0; c < nc + ni; ++c) {
1247 nm = (pm * ns) / 100;
1249 /* Place the mountains */
1251 for (i = 0; i < ns; ++i) {
1252 dsea[i] = distance_to_sea();
1253 weight[i] = (total += (dsea[i] * dsea[i]));
1256 for (k = nm, mountain_search = 0;
1257 k && mountain_search < MOUNTAIN_SEARCH_MAX;
1258 ++mountain_search) {
1260 for (i = 0; i < ns; ++i)
1261 if (r < weight[i] && ELEV == -INFINITE_ELEVATION &&
1263 ((!(capx[c] == sectx[c][i] &&
1264 capy[c] == secty[c][i])) &&
1265 (!(new_x(capx[c] + 2) == sectx[c][i] &&
1266 capy[c] == secty[c][i]))))) {
1267 ELEV = INFINITE_ELEVATION;
1273 /* Elevate land that is not mountain and not capital */
1275 for (i = 0; i < ns; ++i)
1276 dmoun[i] = distance_to_mountain();
1277 dk = (ns - nm - ((c < nc) ? 3 : 1) > 0) ?
1278 (100 * (HIGHMIN - LANDMIN)) / (ns - nm - ((c < nc) ? 3 : 1)) :
1279 100 * INFINITE_ELEVATION;
1280 for (k = 100 * (HIGHMIN - 1);; k -= dk) {
1283 for (i = 0; i < ns; ++i) {
1284 if (ELEV == -INFINITE_ELEVATION &&
1285 (c >= nc || ((!(capx[c] == sectx[c][i] &&
1286 capy[c] == secty[c][i])) &&
1287 (!(new_x(capx[c] + 2) == sectx[c][i] &&
1288 capy[c] == secty[c][i]))))) {
1289 h = 3 * (5 - dmoun[i]) + dsea[i];
1300 if (newk >= HILLMIN && newk < PLATMIN)
1304 elev[sectx[c][where]][secty[c][where]] = newk;
1307 /* Elevate the mountains and capitals */
1309 for (i = 0; i < ns; ++i) {
1310 if (ELEV == INFINITE_ELEVATION) {
1312 ELEV = HILLMIN + roll0(PLATMIN - HILLMIN);
1314 ELEV = HIGHMIN + roll0((256 - HIGHMIN) / 2) +
1315 roll0((256 - HIGHMIN) / 2);
1316 } else if (c < nc &&
1317 (((capx[c] == sectx[c][i] && capy[c] == secty[c][i])) ||
1318 ((new_x(capx[c] + 2) == sectx[c][i] &&
1319 capy[c] == secty[c][i]))))
1325 #define distance_to_land() distance_to_what(x, y, 1)
1332 for (y = 0; y < WORLD_Y; ++y) {
1333 for (x = y % 2; x < WORLD_X; x += 2) {
1334 if (elev[x][y] == -INFINITE_ELEVATION)
1335 elev[x][y] = -roll(distance_to_land() * 20 + 27);
1341 elev_to_sct_type(int elevation)
1343 if (elevation < LANDMIN)
1345 if (elevation < HILLMIN)
1347 if (elevation < PLATMIN)
1349 if (elevation < HIGHMIN)
1354 /****************************************************************************
1356 ****************************************************************************/
1363 fert = LANDMIN - e + 40;
1364 else if (e < FERT_MAX)
1365 fert = (120 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
1376 oil = (LANDMIN - e) * 2 + roll0(2);
1377 else if (e <= OIL_MAX)
1378 oil = (120 * (OIL_MAX - e + 1)) / (OIL_MAX - LANDMIN + 1);
1388 if (e >= IRON_MIN && e < HIGHMIN)
1389 iron = (120 * (e - IRON_MIN + 1)) / (HIGHMIN - IRON_MIN);
1399 if (e >= GOLD_MIN) {
1401 gold = (80 * (e - GOLD_MIN + 1)) / (HIGHMIN - GOLD_MIN);
1403 gold = 100 - 20 * HIGHMIN / e;
1414 if (e >= URAN_MIN && e < HIGHMIN)
1415 uran = (120 * (e - URAN_MIN + 1)) / (HIGHMIN - URAN_MIN);
1422 add_resources(struct sctstr *sct)
1424 sct->sct_fertil = set_fert(sct->sct_elev);
1425 sct->sct_oil = set_oil(sct->sct_elev);
1426 sct->sct_min = set_iron(sct->sct_elev);
1427 sct->sct_gmin = set_gold(sct->sct_elev);
1428 sct->sct_uran = set_uran(sct->sct_elev);
1431 /****************************************************************************
1432 DESIGNATE THE SECTORS
1433 ****************************************************************************/
1441 for (y = 0; y < WORLD_Y; y++) {
1442 for (x = y % 2; x < WORLD_X; x += 2) {
1443 sct = getsectp(x, y);
1444 sct->sct_elev = elev[x][y];
1445 sct->sct_type = elev_to_sct_type(elev[x][y]);
1446 sct->sct_newtype = sct->sct_type;
1447 sct->sct_dterr = own[sct->sct_x][y] + 1;
1451 set_coastal_flags();
1454 /****************************************************************************
1455 PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1456 ****************************************************************************/
1460 int sx, sy, x, y, c, type;
1463 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1468 for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1471 type = elev_to_sct_type(elev[x][y]);
1472 if (type == SCT_WATER)
1474 else if (type == SCT_MOUNT)
1479 assert(0 <= c && c < nc);
1480 if ((x == capx[c] || x == new_x(capx[c] + 2))
1482 printf("%c ", numletter[c % 62]);
1492 * Print a map to help visualize own[][].
1493 * This is for debugging.
1500 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1503 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1507 else if (own[x][y] == -1)
1510 putchar(numletter[own[x][y] % 62]);
1517 * Print a map to help visualize elev[][].
1518 * This is for debugging. It expects the terminal to understand
1519 * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1522 print_elev_map(void)
1524 int sx, sy, x, y, sat;
1526 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1529 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1533 else if (!elev[x][y])
1535 else if (elev[x][y] < 0) {
1536 sat = 256 + elev[x][y] * 2;
1537 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1538 } else if (elev[x][y] < HIGHMIN / 2) {
1539 sat = (HIGHMIN / 2 - elev[x][y]) * 4;
1540 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1541 } else if (elev[x][y] < HIGHMIN) {
1542 sat = 128 + (HIGHMIN - elev[x][y]) * 2;
1543 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1545 sat = 128 + (elev[x][y] - HIGHMIN) * 4 / 5;
1546 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1554 * Print a map to help visualize xzone[].
1555 * This is for debugging.
1558 print_xzone_map(void)
1560 int sx, sy, x, y, off;
1562 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1565 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1567 off = XYOFFSET(x, y);
1570 else if (own[x][y] >= 0)
1572 else if (xzone[off] >= 0)
1573 putchar(numletter[xzone[off] % 62]);
1575 assert(own[x][y] == -1);
1576 putchar(xzone[off] == -1 ? '.' : '!');
1584 * Print a map to help visualize closest[].
1585 * This is for debugging.
1588 print_closest_map(void)
1590 int sx, sy, x, y, off;
1592 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1595 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1597 off = XYOFFSET(x, y);
1600 else if (closest[off] == (natid)-1)
1602 else if (!distance[off]) {
1603 assert(closest[off] == own[x][y]);
1606 putchar(numletter[closest[off] % 62]);
1614 print_distance_map(void)
1616 int sx, sy, x, y, off;
1618 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1621 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1623 off = XYOFFSET(x, y);
1626 else if (closest[off] == (natid)-1)
1628 else if (!distance[off]) {
1629 assert(closest[off] == own[x][y]);
1632 putchar(numletter[distance[off] % 62]);
1640 /***************************************************************************
1641 WRITE A SCRIPT FOR PLACING CAPITALS
1642 ****************************************************************************/
1644 write_newcap_script(void)
1647 FILE *script = fopen(outfile, "w");
1650 fprintf(stderr, "%s: unable to write to %s (%s)\n",
1651 program_name, outfile, strerror(errno));
1655 for (c = 0; c < nc; ++c) {
1656 fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1657 fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
1659 fprintf(script, "add %d visitor visitor v\n", c + 1);
1665 qprint(const char *const fmt, ...)
1671 vfprintf(stdout, fmt, ap);
1677 set_coastal_flags(void)
1682 for (i = 0; i < nc + ni; ++i) {
1683 for (j = 0; j < isecs[i]; j++) {
1684 sp = getsectp(sectx[i][j], secty[i][j]);
1685 sp->sct_coastal = sectc[i][j];