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 (except when island placement fails for lack of room).
70 * Place and grow islands in spheres in turn. Place the first sector
71 * randomly, pick an island size, then grow the island to that size.
73 * Growing works as for continents, except the minimum distance for
74 * additional islands applies, and growing simply stops when there is
77 * 4. Compute elevation
79 * Elevate islands one after the other.
81 * First, place the specified number of mountains randomly.
82 * Probability increases with distance to sea.
84 * Last, elevate mountains and the capitals. Pick coastal mountain
85 * elevation randomly from an interval of medium elevations reserved
86 * for them. Pick non-coastal mountain elevation randomly from an
87 * interval of high elevation reserved for them. Set capital
88 * elevation to a fixed, medium value.
90 * In between, elevate the remaining land one by one, working from
91 * mountains towards the sea, and from the elevation just below the
92 * non-coastal mountains' interval linearly down to 1, avoiding the
93 * coastal mountains' interval.
95 * This gives islands of the same size the same set of elevations,
96 * except for mountains.
98 * Elevate sea: pick a random depth from an interval that deepens with
99 * the distance to land.
103 * Sector resources are simple functions of elevation. You can alter
104 * macros OIL_MAX, IRON_MIN, GOLD_MIN, FERT_MAX, and URAN_MIN to
119 #include "prototypes.h"
124 /* The following five numbers refer to elevation under which (in the case of
125 fertility or oil) or over which (in the case of iron, gold, and uranium)
126 sectors with that elevation will contain that resource. Elevation ranges
129 /* raise FERT_MAX for more fertility */
132 /* raise OIL_MAX for more oil */
135 /* lower IRON_MIN for more iron */
138 /* lower GOLD_MIN for more gold */
141 /* lower URAN_MIN for more uranium */
144 /* do not change these 4 defines */
145 #define LANDMIN 1 /* plate altitude for normal land */
146 #define HILLMIN 34 /* plate altitude for hills */
147 #define PLATMIN 36 /* plate altitude for plateau */
148 #define HIGHMIN 98 /* plate altitude for mountains */
150 static void qprint(const char * const fmt, ...)
151 ATTRIBUTE((format (printf, 1, 2)));
154 * Program arguments and options
156 static char *program_name;
157 static int nc, sc; /* number and size of continents */
158 static int ni, is; /* number and size of islands */
159 #define DEFAULT_SPIKE 10
160 static int sp = DEFAULT_SPIKE; /* spike percentage */
161 #define DEFAULT_MOUNTAIN 0
162 static int pm = DEFAULT_MOUNTAIN; /* mountain percentage */
163 #define DEFAULT_CONTDIST 2
164 static int di = DEFAULT_CONTDIST; /* min. distance between continents */
165 #define DEFAULT_ISLDIST 1
166 static int id = DEFAULT_ISLDIST; /* ... continents and islands */
167 /* don't let the islands crash into each other.
168 1 = don't merge, 0 = merge. */
169 static int DISTINCT_ISLANDS = 1;
171 #define DEFAULT_OUTFILE_NAME "newcap_script"
172 static const char *outfile = DEFAULT_OUTFILE_NAME;
174 #define STABLE_CYCLE 4 /* stability required for perterbed capitals */
175 #define INFINITE_ELEVATION 999
177 /* these defines prevent infinite loops:
179 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
180 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
181 #define MOUNTAIN_SEARCH_MAX 1000 /* how long do we try to place mountains */
186 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
187 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
189 static int ctot; /* total number of continents and islands grown */
190 static int *isecs; /* array of how large each island is */
192 static int *capx, *capy; /* location of the nc capitals */
194 static int **own; /* owner of the sector. -1 means water */
197 * Adjacent land sectors
198 * adj_land[XYOFFSET(x, y)] bit d is set exactly when the sector next
199 * to x, y in direction d is land.
201 static unsigned char *adj_land;
205 * Each island is surrounded by an exclusive zone where only it may
206 * grow. The width of the zone depends on minimum distances.
207 * While growing continents, it is @di sectors wide.
208 * While growing additional islands, it is @id sectors wide.
209 * DISTINCT_ISLANDS nullifies the exclusive zone then.
210 * xzone[XYOFFSET(x, y)] is -1 when the sector is in no exclusive
211 * zone, a (non-negative) island number when it is in that island's
212 * exclusive zone and no other, and -2 when it is in multiple
218 * Set of sectors seen already
219 * Increment @cur_seen to empty the set of sectors seen, set
220 * seen[XYOFFSET(x, y)] to @cur_seen to add x,y to the set.
222 static unsigned *seen;
223 static unsigned cur_seen;
226 * Closest continent and "distance"
227 * closest[XYOFFSET(x, y)] is the closest continent's number.
228 * distance[] is complicated; see init_spheres_of_influence().
230 static natid *closest;
231 static unsigned short *distance;
234 * Queue for breadth-first search
236 static int *bfs_queue;
237 static int bfs_queue_head, bfs_queue_tail;
239 static int **elev; /* elevation of the sectors */
240 static int **sectx, **secty; /* the sectors for each continent */
241 static int **sectc; /* which sectors are on the coast? */
242 static int *weight; /* used for placing mountains */
243 static int *dsea, *dmoun; /* the dist to the ocean and mountain */
245 #define NUMTRIES 10 /* keep trying to grow this many times */
247 static const char *numletter =
248 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
250 static void help(char *);
251 static void usage(void);
252 static void parse_args(int argc, char *argv[]);
253 static void allocate_memory(void);
254 static void init(void);
255 static int drift(void);
256 static int grow_continents(void);
257 static void create_elevations(void);
258 static void write_sects(void);
259 static void output(void);
260 static int write_newcap_script(void);
261 static int stable(int);
262 static void elevate_land(void);
263 static void elevate_sea(void);
264 static void set_coastal_flags(void);
266 static void print_vars(void);
267 static void fl_move(int);
268 static void grow_islands(void);
270 /* Debugging aids: */
271 void print_own_map(void);
272 void print_xzone_map(void);
273 void print_closest_map(void);
274 void print_distance_map(void);
275 void print_elev_map(void);
277 /****************************************************************************
279 ****************************************************************************/
282 main(int argc, char *argv[])
285 char *config_file = NULL;
287 unsigned rnd_seed = 0;
290 program_name = argv[0];
292 while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
295 config_file = optarg;
298 DISTINCT_ISLANDS = 0;
304 rnd_seed = strtoul(optarg, NULL, 10);
314 printf("%s\n\n%s", version, legal);
323 rnd_seed = pick_seed();
326 if (emp_config(config_file) < 0)
330 parse_args(argc - optind, argv + optind);
335 qprint("\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
336 qprint("seed is %u\n", rnd_seed);
341 qprint("\ntry #%d (out of %d)...\n", try + 1, NUMTRIES);
342 qprint("placing capitals...\n");
344 qprint("unstable drift\n");
345 qprint("growing continents...\n");
346 done = grow_continents();
347 } while (!done && ++try < NUMTRIES);
349 fprintf(stderr, "%s: world not large enough to hold continents\n",
353 qprint("growing islands:");
355 qprint("\nelevating land...\n");
358 qprint("writing to sectors file...\n");
359 if (!write_newcap_script())
361 if (chdir(gamedir)) {
362 fprintf(stderr, "%s: can't chdir to %s (%s)\n",
363 program_name, gamedir, strerror(errno));
366 if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
369 if (!ef_close(EF_SECTOR))
373 qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
383 puts("Creating a planet with:\n");
384 printf("%d continents\n", nc);
385 printf("continent size: %d\n", sc);
386 printf("number of islands: %d\n", ni);
387 printf("average size of islands: %d\n", is);
388 printf("spike: %d%%\n", sp);
389 printf("%d%% of land is mountain (each continent will have %d mountains)\n",
390 pm, (pm * sc) / 100);
391 printf("minimum distance between continents: %d\n", di);
392 printf("minimum distance from islands to continents: %d\n", id);
393 printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
397 help(char *complaint)
400 fprintf(stderr, "%s: %s\n", program_name, complaint);
401 fprintf(stderr, "Try -h for help.\n");
407 printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
408 " -e CONFIG-FILE configuration file\n"
410 " -i islands may merge\n"
412 " -R SEED seed for random number generator\n"
413 " -s SCRIPT name of script to create (default %s)\n"
414 " -h display this help and exit\n"
415 " -v display version information and exit\n"
416 " NC number of continents\n"
417 " SC continent size\n"
418 " NI number of islands (default NC)\n"
419 " IS average island size (default SC/2)\n"
420 " SP spike percentage: 0 = round, 100 = snake (default %d)\n"
421 " PM percentage of land that is mountain (default %d)\n"
422 " DI minimum distance between continents (default %d)\n"
423 " ID minimum distance from islands to continents (default %d)\n",
424 program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
425 DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
429 parse_args(int argc, char *argv[])
431 int dist_max = mapdist(0, 0, WORLD_X / 2, WORLD_Y / 2);
434 help("missing arguments");
438 help("too many arguments");
443 fprintf(stderr, "%s: number of continents must be > 0\n",
450 fprintf(stderr, "%s: size of continents must be > 1\n",
461 fprintf(stderr, "%s: number of islands must be >= 0\n",
466 fprintf(stderr, "%s: number of islands must be a multiple of"
467 " the number of continents\n",
475 fprintf(stderr, "%s: size of islands must be > 0\n",
482 if (sp < 0 || sp > 100) {
484 "%s: spike percentage must be between 0 and 100\n",
491 if (pm < 0 || pm > 100) {
493 "%s: mountain percentage must be between 0 and 100\n",
501 fprintf(stderr, "%s: distance between continents must be >= 0\n",
506 fprintf(stderr, "%s: distance between continents too large\n",
515 "%s: distance from islands to continents must be >= 0\n",
521 "%s: distance from islands to continents too large\n",
527 /****************************************************************************
528 VARIABLE INITIALIZATION
529 ****************************************************************************/
532 allocate_memory(void)
536 capx = calloc(nc, sizeof(int));
537 capy = calloc(nc, sizeof(int));
538 own = calloc(WORLD_X, sizeof(int *));
539 adj_land = malloc(WORLD_SZ() * sizeof(*adj_land));
540 xzone = malloc(WORLD_SZ() * sizeof(*xzone));
541 seen = calloc(WORLD_SZ(), sizeof(*seen));
542 closest = malloc(WORLD_SZ() * sizeof(*closest));
543 distance = malloc(WORLD_SZ() * sizeof(*distance));
544 bfs_queue = malloc(WORLD_SZ() * sizeof(*bfs_queue));
545 elev = calloc(WORLD_X, sizeof(int *));
546 for (i = 0; i < WORLD_X; ++i) {
547 own[i] = calloc(WORLD_Y, sizeof(int));
548 elev[i] = calloc(WORLD_Y, sizeof(int));
550 sectx = calloc(nc + ni, sizeof(int *));
551 secty = calloc(nc + ni, sizeof(int *));
552 sectc = calloc(nc + ni, sizeof(int *));
553 isecs = calloc(nc + ni, sizeof(int));
554 weight = calloc(MAX(sc, is * 2), sizeof(int));
555 dsea = calloc(MAX(sc, is * 2), sizeof(int));
556 dmoun = calloc(MAX(sc, is * 2), sizeof(int));
557 for (i = 0; i < nc; ++i) {
558 sectx[i] = calloc(sc, sizeof(int));
559 secty[i] = calloc(sc, sizeof(int));
560 sectc[i] = calloc(sc, sizeof(int));
562 for (i = nc; i < nc + ni; ++i) {
563 sectx[i] = calloc(is * 2, sizeof(int));
564 secty[i] = calloc(is * 2, sizeof(int));
565 sectc[i] = calloc(is * 2, sizeof(int));
575 for (i = 0; i < WORLD_X; ++i) {
576 for (j = 0; j < WORLD_Y; ++j) {
580 memset(adj_land, 0, WORLD_SZ() * sizeof(*adj_land));
583 /****************************************************************************
584 DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
585 ****************************************************************************/
588 * How isolated is capital @j at @newx,@newy?
589 * Return the distance to the closest other capital.
592 iso(int j, int newx, int newy)
597 for (i = 0; i < nc; ++i) {
600 md = mapdist(capx[i], capy[i], newx, newy);
610 * Return 1 for a stable drift, 0 for an unstable one.
617 for (i = 0; i < nc; i++) {
618 capy[i] = (2 * i) / WORLD_X;
619 capx[i] = (2 * i) % WORLD_X + capy[i] % 2;
620 if (capy[i] >= WORLD_Y) {
622 "%s: world not big enough for all the continents\n",
628 for (turns = 0; turns < DRIFT_MAX; ++turns) {
631 for (i = 0; i < nc; ++i)
638 * Has the drift stabilized?
639 * @turns is the number of turns so far.
644 static int mc[STABLE_CYCLE];
645 int i, isod, d = 0, stab = 1;
648 for (i = 0; i < STABLE_CYCLE; i++)
652 if (turns <= DRIFT_BEFORE_CHECK)
655 for (i = 0; i < nc; ++i) {
656 isod = iso(i, capx[i], capy[i]);
661 for (i = 0; i < STABLE_CYCLE; ++i)
665 mc[turns % STABLE_CYCLE] = d;
669 /* This routine does the actual drifting
675 int dir, i, newx, newy;
677 dir = DIR_L + roll0(6);
678 for (i = 0; i < 6; i++) {
681 newx = new_x(capx[j] + diroff[dir][0]);
682 newy = new_y(capy[j] + diroff[dir][1]);
684 if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) {
692 /****************************************************************************
694 ****************************************************************************/
696 /* Look for a coastal sector of continent c
704 for (i = 0; i < isecs[c]; ++i) {
706 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
707 nx = new_x(sectx[c][i] + diroff[dir][0]);
708 ny = new_y(secty[c][i] + diroff[dir][1]);
709 if (own[nx][ny] == -1)
715 struct hexagon_iter {
720 * Start iterating around @x0,@y0 at distance @d.
721 * Set *x,*y to coordinates of the first sector.
724 hexagon_first(struct hexagon_iter *iter, int x0, int y0, int n,
727 *x = new_x(x0 - 2 * n);
729 iter->dir = DIR_FIRST;
735 * Continue iteration started with hexagon_first().
736 * Set *x,*y to coordinates of the next sector.
737 * Return whether we're back at the first sector, i.e. iteration is
741 hexagon_next(struct hexagon_iter *iter, int *x, int *y)
743 *x = new_x(*x + diroff[iter->dir][0]);
744 *y = new_y(*y + diroff[iter->dir][1]);
746 if (iter->i == iter->n) {
750 return iter->dir <= DIR_LAST;
754 * Is @x,@y in no exclusive zone other than perhaps @c's?
757 xzone_ok(int c, int x, int y)
759 int off = XYOFFSET(x, y);
761 return xzone[off] == c || xzone[off] == -1;
765 * Add sectors within distance @dist of @x,@y to @c's exclusive zone.
768 xzone_around_sector(int c, int x, int y, int dist)
771 struct hexagon_iter hexit;
773 assert(xzone_ok(c, x, y));
775 xzone[XYOFFSET(x, y)] = c;
776 for (d = 1; d <= dist; d++) {
777 hexagon_first(&hexit, x, y, d, &x1, &y1);
779 off = XYOFFSET(x1, y1);
780 if (xzone[off] == -1)
782 else if (xzone[off] != c)
784 } while (hexagon_next(&hexit, &x1, &y1));
789 * Add sectors within distance @dist to island @c's exclusive zone.
792 xzone_around_island(int c, int dist)
796 for (i = 0; i < isecs[c]; i++)
797 xzone_around_sector(c, sectx[c][i], secty[c][i], dist);
801 * Initialize exclusive zones around @n islands.
808 for (i = 0; i < WORLD_SZ(); i++)
811 for (c = 0; c < n; c++)
812 xzone_around_island(c, id);
816 * Initialize breadth-first search.
823 for (i = 0; i < WORLD_SZ(); i++) {
825 distance[i] = USHRT_MAX;
828 bfs_queue_head = bfs_queue_tail = 0;
832 * Add sector @x,@y to the BFS queue.
833 * It's closest to @c, with distance @dist.
836 bfs_enqueue(int c, int x, int y, int dist)
838 int off = XYOFFSET(x, y);
840 assert(dist < distance[off]);
842 distance[off] = dist;
843 bfs_queue[bfs_queue_tail] = off;
845 if (bfs_queue_tail >= WORLD_SZ())
847 assert(bfs_queue_tail != bfs_queue_head);
851 * Search breadth-first until the queue is empty.
856 int off, dist, i, noff, nx, ny;
859 while (bfs_queue_head != bfs_queue_tail) {
860 off = bfs_queue[bfs_queue_head];
862 if (bfs_queue_head >= WORLD_SZ())
864 dist = distance[off] + 1;
865 sctoff2xy(&x, &y, off);
866 for (i = DIR_FIRST; i <= DIR_LAST; i++) {
867 nx = new_x(x + diroff[i][0]);
868 ny = new_y(y + diroff[i][1]);
869 noff = XYOFFSET(nx, ny);
870 if (dist < distance[noff]) {
871 bfs_enqueue(closest[off], nx, ny, dist);
872 } else if (distance[noff] == dist) {
873 if (closest[off] != closest[noff])
874 closest[noff] = (natid)-1;
876 assert(distance[noff] < dist);
882 * Add island @c's coastal sectors to the BFS queue, with distance 0.
885 bfs_enqueue_island(int c)
889 for (i = 0; i < isecs[c]; i++) {
891 bfs_enqueue(c, sectx[c][i], secty[c][i], 0);
896 * Compute spheres of influence
897 * A continent's sphere of influence is the set of sectors closer to
898 * it than to any other continent.
899 * Set closest[XYOFFSET(x, y)] to the closest continent's number,
900 * -1 if no single continent is closest.
901 * Set distance[XYOFFSET(x, y)] to the distance to the closest coastal
905 init_spheres_of_influence(void)
910 for (c = 0; c < nc; c++)
911 bfs_enqueue_island(c);
916 * Is @x,@y in the same sphere of influence as island @c?
917 * Always true when @c is a continent.
920 is_in_sphere(int c, int x, int y)
922 return c < nc || closest[XYOFFSET(x, y)] == c % nc;
926 * Can island @c grow at @x,@y?
929 can_grow_at(int c, int x, int y)
931 return own[x][y] == -1 && xzone_ok(c, x, y) && is_in_sphere(c, x, y);
935 adj_land_update(int x, int y)
937 int dir, nx, ny, noff;
939 assert(own[x][y] != -1);
941 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
942 nx = new_x(x + diroff[dir][0]);
943 ny = new_y(y + diroff[dir][1]);
944 noff = XYOFFSET(nx, ny);
945 adj_land[noff] |= 1u << DIR_BACK(dir);
950 add_sector(int c, int x, int y)
952 assert(own[x][y] == -1);
953 xzone_around_sector(c, x, y, c < nc ? di : DISTINCT_ISLANDS ? id : 0);
954 sectx[c][isecs[c]] = x;
955 secty[c][isecs[c]] = y;
958 adj_land_update(x, y);
962 grow_weight(int c, int x, int y, int spike)
967 * #Land neighbors is #bits set in adj_land[].
968 * Count them Brian Kernighan's way.
971 for (b = adj_land[XYOFFSET(x, y)]; b; b &= b - 1)
973 assert(n > 0 && n < 7);
976 return (6 - n) * (6 - n);
982 grow_one_sector(int c)
984 int spike = roll0(100) < sp;
985 int wsum, newx, newy, i, x, y, off, dir, nx, ny, noff, w;
987 assert(cur_seen < UINT_MAX);
992 for (i = 0; i < isecs[c]; i++) {
995 off = XYOFFSET(x, y);
997 for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
998 if (adj_land[off] & (1u << dir))
1000 nx = new_x(x + diroff[dir][0]);
1001 ny = new_y(y + diroff[dir][1]);
1002 noff = XYOFFSET(nx, ny);
1003 if (seen[noff] == cur_seen)
1005 assert(seen[noff] < cur_seen);
1006 seen[noff] = cur_seen;
1007 if (!can_grow_at(c, nx, ny))
1009 w = grow_weight(c, nx, ny, spike);
1010 assert(wsum < INT_MAX - w);
1012 if (roll0(wsum) < w) {
1022 add_sector(c, newx, newy);
1027 * Grow the continents.
1028 * Return 1 on success, 0 on error.
1031 grow_continents(void)
1039 for (c = 0; c < nc; ++c) {
1041 if (!can_grow_at(c, capx[c], capy[c])
1042 || !can_grow_at(c, new_x(capx[c] + 2), capy[c])) {
1046 add_sector(c, capx[c], capy[c]);
1047 add_sector(c, new_x(capx[c] + 2), capy[c]);
1051 qprint("No room for continents\n");
1055 for (secs = 2; secs < sc && done; secs++) {
1056 for (c = 0; c < nc; ++c) {
1057 if (!grow_one_sector(c))
1062 for (c = 0; c < nc; ++c)
1066 qprint("Only managed to grow %d out of %d sectors.\n",
1072 /****************************************************************************
1074 ****************************************************************************/
1077 * Place additional island @c's first sector.
1078 * Return 1 on success, 0 on error.
1083 int n, x, y, newx, newy;
1087 for (y = 0; y < WORLD_Y; y++) {
1088 for (x = y % 2; x < WORLD_X; x += 2) {
1089 if (can_grow_at(c, x, y)) {
1100 add_sector(c, newx, newy);
1104 /* Grow all the islands
1110 int stunted_islands = 0;
1114 init_spheres_of_influence();
1116 for (c = nc; c < nc + ni; ++c) {
1117 if (!place_island(c)) {
1118 qprint("\nNo room for island #%d", c - nc + 1);
1122 isiz = roll(is) + roll0(is);
1123 for (secs = 1; secs < isiz; secs++) {
1124 if (!grow_one_sector(c)) {
1131 qprint(" %d(%d)", c - nc + 1, secs);
1135 if (stunted_islands)
1136 qprint("\n%d stunted island%s",
1137 stunted_islands, splur(stunted_islands));
1140 /****************************************************************************
1142 ****************************************************************************/
1144 create_elevations(void)
1148 for (i = 0; i < WORLD_X; i++) {
1149 for (j = 0; j < WORLD_Y; j++)
1150 elev[i][j] = -INFINITE_ELEVATION;
1156 /* Generic function for finding the distance to the closest sea, land, or
1160 distance_to_what(int x, int y, int flag)
1163 struct hexagon_iter hexit;
1165 for (d = 1; d < 5; ++d) {
1166 hexagon_first(&hexit, x, y, d, &px, &py);
1169 case 0: /* distance to sea */
1170 if (own[px][py] == -1)
1173 case 1: /* distance to land */
1174 if (own[px][py] != -1)
1177 case 2: /* distance to mountain */
1178 if (elev[px][py] == INFINITE_ELEVATION)
1182 } while (hexagon_next(&hexit, &px, &py));
1187 #define ELEV elev[sectx[c][i]][secty[c][i]]
1188 #define distance_to_sea() (sectc[c][i]?1:distance_to_what(sectx[c][i], secty[c][i], 0))
1189 #define distance_to_mountain() distance_to_what(sectx[c][i], secty[c][i], 2)
1191 /* Decide where the mountains go
1196 int i, mountain_search, k, c, total, ns, nm, highest, where, h, newk,
1199 for (c = 0; c < ctot; ++c) {
1202 nm = (pm * ns) / 100;
1204 /* Place the mountains */
1206 for (i = 0; i < ns; ++i) {
1207 dsea[i] = distance_to_sea();
1208 weight[i] = (total += (dsea[i] * dsea[i]));
1211 for (k = nm, mountain_search = 0;
1212 k && mountain_search < MOUNTAIN_SEARCH_MAX;
1213 ++mountain_search) {
1215 for (i = 0; i < ns; ++i)
1216 if (r < weight[i] && ELEV == -INFINITE_ELEVATION &&
1218 ((!(capx[c] == sectx[c][i] &&
1219 capy[c] == secty[c][i])) &&
1220 (!(new_x(capx[c] + 2) == sectx[c][i] &&
1221 capy[c] == secty[c][i]))))) {
1222 ELEV = INFINITE_ELEVATION;
1228 /* Elevate land that is not mountain and not capital */
1230 for (i = 0; i < ns; ++i)
1231 dmoun[i] = distance_to_mountain();
1232 dk = (ns - nm - ((c < nc) ? 3 : 1) > 0) ?
1233 (100 * (HIGHMIN - LANDMIN)) / (ns - nm - ((c < nc) ? 3 : 1)) :
1234 100 * INFINITE_ELEVATION;
1235 for (k = 100 * (HIGHMIN - 1);; k -= dk) {
1238 for (i = 0; i < ns; ++i) {
1239 if (ELEV == -INFINITE_ELEVATION &&
1240 (c >= nc || ((!(capx[c] == sectx[c][i] &&
1241 capy[c] == secty[c][i])) &&
1242 (!(new_x(capx[c] + 2) == sectx[c][i] &&
1243 capy[c] == secty[c][i]))))) {
1244 h = 3 * (5 - dmoun[i]) + dsea[i];
1255 if (newk >= HILLMIN && newk < PLATMIN)
1259 elev[sectx[c][where]][secty[c][where]] = newk;
1262 /* Elevate the mountains and capitals */
1264 for (i = 0; i < ns; ++i) {
1265 if (ELEV == INFINITE_ELEVATION) {
1267 ELEV = HILLMIN + roll0(PLATMIN - HILLMIN);
1269 ELEV = HIGHMIN + roll0((256 - HIGHMIN) / 2) +
1270 roll0((256 - HIGHMIN) / 2);
1271 } else if (c < nc &&
1272 (((capx[c] == sectx[c][i] && capy[c] == secty[c][i])) ||
1273 ((new_x(capx[c] + 2) == sectx[c][i] &&
1274 capy[c] == secty[c][i]))))
1280 #define distance_to_land() distance_to_what(x, y, 1)
1287 for (y = 0; y < WORLD_Y; ++y) {
1288 for (x = y % 2; x < WORLD_X; x += 2) {
1289 if (elev[x][y] == -INFINITE_ELEVATION)
1290 elev[x][y] = -roll(distance_to_land() * 20 + 27);
1296 elev_to_sct_type(int elevation)
1298 if (elevation < LANDMIN)
1300 if (elevation < HILLMIN)
1302 if (elevation < PLATMIN)
1304 if (elevation < HIGHMIN)
1309 /****************************************************************************
1311 ****************************************************************************/
1318 fert = LANDMIN - e + 40;
1319 else if (e < FERT_MAX)
1320 fert = (120 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
1331 oil = (LANDMIN - e) * 2 + roll0(2);
1332 else if (e <= OIL_MAX)
1333 oil = (120 * (OIL_MAX - e + 1)) / (OIL_MAX - LANDMIN + 1);
1343 if (e >= IRON_MIN && e < HIGHMIN)
1344 iron = (120 * (e - IRON_MIN + 1)) / (HIGHMIN - IRON_MIN);
1354 if (e >= GOLD_MIN) {
1356 gold = (80 * (e - GOLD_MIN + 1)) / (HIGHMIN - GOLD_MIN);
1358 gold = 100 - 20 * HIGHMIN / e;
1369 if (e >= URAN_MIN && e < HIGHMIN)
1370 uran = (120 * (e - URAN_MIN + 1)) / (HIGHMIN - URAN_MIN);
1377 add_resources(struct sctstr *sct)
1379 sct->sct_fertil = set_fert(sct->sct_elev);
1380 sct->sct_oil = set_oil(sct->sct_elev);
1381 sct->sct_min = set_iron(sct->sct_elev);
1382 sct->sct_gmin = set_gold(sct->sct_elev);
1383 sct->sct_uran = set_uran(sct->sct_elev);
1386 /****************************************************************************
1387 DESIGNATE THE SECTORS
1388 ****************************************************************************/
1396 for (y = 0; y < WORLD_Y; y++) {
1397 for (x = y % 2; x < WORLD_X; x += 2) {
1398 sct = getsectp(x, y);
1399 sct->sct_elev = elev[x][y];
1400 sct->sct_type = elev_to_sct_type(elev[x][y]);
1401 sct->sct_newtype = sct->sct_type;
1402 sct->sct_dterr = own[sct->sct_x][y] + 1;
1406 set_coastal_flags();
1409 /****************************************************************************
1410 PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1411 ****************************************************************************/
1415 int sx, sy, x, y, c, type;
1418 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1423 for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1426 type = elev_to_sct_type(elev[x][y]);
1427 if (type == SCT_WATER)
1429 else if (type == SCT_MOUNT)
1434 assert(0 <= c && c < nc);
1435 if ((x == capx[c] || x == new_x(capx[c] + 2))
1437 printf("%c ", numletter[c % 62]);
1447 * Print a map to help visualize own[][].
1448 * This is for debugging.
1455 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1458 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1462 else if (own[x][y] == -1)
1465 putchar(numletter[own[x][y] % 62]);
1472 * Print a map to help visualize elev[][].
1473 * This is for debugging. It expects the terminal to understand
1474 * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1477 print_elev_map(void)
1479 int sx, sy, x, y, sat;
1481 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1484 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1488 else if (!elev[x][y])
1490 else if (elev[x][y] < 0) {
1491 sat = 256 + elev[x][y] * 2;
1492 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1493 } else if (elev[x][y] < HIGHMIN / 2) {
1494 sat = (HIGHMIN / 2 - elev[x][y]) * 4;
1495 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1496 } else if (elev[x][y] < HIGHMIN) {
1497 sat = 128 + (HIGHMIN - elev[x][y]) * 2;
1498 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1500 sat = 128 + (elev[x][y] - HIGHMIN) * 4 / 5;
1501 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1509 * Print a map to help visualize xzone[].
1510 * This is for debugging.
1513 print_xzone_map(void)
1515 int sx, sy, x, y, off;
1517 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1520 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1522 off = XYOFFSET(x, y);
1525 else if (own[x][y] >= 0)
1527 else if (xzone[off] >= 0)
1528 putchar(numletter[xzone[off] % 62]);
1530 assert(own[x][y] == -1);
1531 putchar(xzone[off] == -1 ? '.' : '!');
1539 * Print a map to help visualize closest[].
1540 * This is for debugging.
1543 print_closest_map(void)
1545 int sx, sy, x, y, off;
1547 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1550 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1552 off = XYOFFSET(x, y);
1555 else if (closest[off] == (natid)-1)
1557 else if (!distance[off]) {
1558 assert(closest[off] == own[x][y]);
1561 putchar(numletter[closest[off] % 62]);
1569 print_distance_map(void)
1571 int sx, sy, x, y, off;
1573 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1576 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1578 off = XYOFFSET(x, y);
1581 else if (closest[off] == (natid)-1)
1583 else if (!distance[off]) {
1584 assert(closest[off] == own[x][y]);
1587 putchar(numletter[distance[off] % 62]);
1595 /***************************************************************************
1596 WRITE A SCRIPT FOR PLACING CAPITALS
1597 ****************************************************************************/
1599 write_newcap_script(void)
1602 FILE *script = fopen(outfile, "w");
1605 fprintf(stderr, "%s: unable to write to %s (%s)\n",
1606 program_name, outfile, strerror(errno));
1610 for (c = 0; c < nc; ++c) {
1611 fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1612 fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
1614 fprintf(script, "add %d visitor visitor v\n", c + 1);
1620 qprint(const char *const fmt, ...)
1626 vfprintf(stdout, fmt, ap);
1632 set_coastal_flags(void)
1637 for (i = 0; i < nc + ni; ++i) {
1638 for (j = 0; j < isecs[i]; j++) {
1639 sp = getsectp(sectx[i][j], secty[i][j]);
1640 sp->sct_coastal = sectc[i][j];