2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * fairland.c: Create a nice, new world
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2020
40 * Place the capitals on the torus in such a way so as to maximize
41 * their distances from one another. This uses the perturbation
42 * technique of calculus of variations.
44 * 2. Grow start islands ("continents")
46 * For all continents, add the first sector at the capital's location,
47 * and the second right to it. These are the capital sectors. Then
48 * add one sector to each continent in turn, obeying the minimum
49 * distance between continents, until they have the specified size.
51 * The kind of shape they grow into is determined by the "spike
52 * percentage" --- the higher the spike, the more spindly they will
53 * be. If you lower the spike, the continents will be more round.
55 * If growing fails due to lack of room, start over. If it fails too
56 * many times, give up and terminate unsuccessfully.
58 * 3. Place and grow additional islands
60 * Place and grow islands one after the other. Place the first sector
61 * randomly, pick an island size, then grow the island to that size.
63 * Growing works as for continents, except the minimum distance for
64 * additional islands applies, and growing simply stops when there is
67 * 4. Compute elevation
69 * Elevate islands one after the other.
71 * First, place the specified number of mountains randomly.
72 * Probability increases with distance to sea.
74 * Last, elevate mountains and the capitals. Pick coastal mountain
75 * elevation randomly from an interval of medium elevations reserved
76 * for them. Pick non-coastal mountain elevation randomly from an
77 * interval of high elevation reserved for them. Set capital
78 * elevation to a fixed, medium value.
80 * In between, elevate the remaining land one by one, working from
81 * mountains towards the sea, and from the elevation just below the
82 * non-coastal mountains' interval linearly down to 1, avoiding the
83 * coastal mountains' interval.
85 * This gives islands of the same size the same set of elevations,
86 * except for mountains.
88 * Elevate sea: pick a random depth from an interval that deepens with
89 * the distance to land.
93 * Sector resources are simple functions of elevation. You can alter
94 * macros OIL_MAX, IRON_MIN, GOLD_MIN, FERT_MAX, and URAN_MIN to
107 #include "prototypes.h"
112 /* The following five numbers refer to elevation under which (in the case of
113 fertility or oil) or over which (in the case of iron, gold, and uranium)
114 sectors with that elevation will contain that resource. Elevation ranges
117 /* raise FERT_MAX for more fertility */
120 /* raise OIL_MAX for more oil */
123 /* lower IRON_MIN for more iron */
126 /* lower GOLD_MIN for more gold */
129 /* lower URAN_MIN for more uranium */
132 /* do not change these 4 defines */
133 #define LANDMIN 1 /* plate altitude for normal land */
134 #define HILLMIN 34 /* plate altitude for hills */
135 #define PLATMIN 36 /* plate altitude for plateau */
136 #define HIGHMIN 98 /* plate altitude for mountains */
138 static void qprint(const char * const fmt, ...)
139 ATTRIBUTE((format (printf, 1, 2)));
142 * Program arguments and options
144 static char *program_name;
145 static int nc, sc; /* number and size of continents */
146 static int ni, is; /* number and size of islands */
147 #define DEFAULT_SPIKE 10
148 static int sp; /* spike percentage */
149 #define DEFAULT_MOUNTAIN 0
150 static int pm; /* mountain percentage */
151 #define DEFAULT_CONTDIST 2
152 static int di; /* min. distance between continents */
153 #define DEFAULT_ISLDIST 1
154 static int id; /* ... continents and islands */
155 /* don't let the islands crash into each other.
156 1 = don't merge, 0 = merge. */
157 static int DISTINCT_ISLANDS = 1;
159 #define DEFAULT_OUTFILE_NAME "newcap_script"
160 static const char *outfile = DEFAULT_OUTFILE_NAME;
162 #define STABLE_CYCLE 4 /* stability required for perterbed capitals */
163 #define INFINITY 999 /* a number which means "BIG" */
165 /* these defines prevent infinite loops:
168 #define COAST_SEARCH_MAX 200 /* how many times do we look for a coast sector
169 when growing continents and islands */
170 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
171 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
172 #define MOUNTAIN_SEARCH_MAX 1000 /* how long do we try to place mountains */
177 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
178 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
180 static int secs; /* number of sectors grown */
181 static int ctot; /* total number of continents and islands grown */
182 static int *isecs; /* array of how large each island is */
184 static int *capx, *capy; /* location of the nc capitals */
185 static int *mc, mcc; /* array and counter used for stability
186 check when perturbing */
187 static int spike; /* are we spiking? */
188 static int mind; /* the final distance between capitals that
190 static int dirx[] = { -2, -1, 1, 2, 1, -1 }; /* gyujnb */
191 static int diry[] = { 0, -1, -1, 0, 1, 1 };
193 static int **own; /* owner of the sector. -1 means water */
194 static int **elev; /* elevation of the sectors */
195 static int **sectx, **secty; /* the sectors for each continent */
196 static int **sectc; /* which sectors are on the coast? */
197 static int *vector; /* used for measuring distances */
198 static int *weight; /* used for placing mountains */
199 static int *dsea, *dmoun; /* the dist to the ocean and mountain */
200 static int fl_status; /* is anything wrong? */
201 #define STATUS_NO_ROOM 1 /* there was no room to grow */
202 #define NUMTRIES 10 /* keep trying to grow this many times */
204 static const char *numletter =
205 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
207 static void help(char *);
208 static void usage(void);
209 static void parse_args(int argc, char *argv[]);
210 static void allocate_memory(void);
211 static void init(void);
212 static int drift(void);
213 static void grow_continents(void);
214 static void create_elevations(void);
215 static void write_sects(void);
216 static void output(void);
217 static int write_newcap_script(void);
218 static int stable(void);
219 static void elevate_land(void);
220 static void elevate_sea(void);
221 static int map_symbol(int x, int y);
222 static void set_coastal_flags(void);
224 static void print_vars(void);
225 static void fl_move(int);
226 static void grow_islands(void);
228 /* Debugging aids: */
229 void print_own_map(void);
230 void print_elev_map(void);
232 /****************************************************************************
234 ****************************************************************************/
237 main(int argc, char *argv[])
240 char *config_file = NULL;
242 unsigned rnd_seed = 0;
245 program_name = argv[0];
247 while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
250 config_file = optarg;
253 DISTINCT_ISLANDS = 0;
259 rnd_seed = strtoul(optarg, NULL, 10);
269 printf("%s\n\n%s", version, legal);
276 parse_args(argc - optind, argv + optind);
279 rnd_seed = pick_seed();
282 if (emp_config(config_file) < 0)
289 qprint("\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
290 qprint("seed is %u\n", rnd_seed);
294 qprint("\ntry #%d (out of %d)...\n", i + 1, NUMTRIES);
295 qprint("placing capitals...\n");
297 qprint("fairland: unstable drift -- try increasing DRIFT_MAX\n");
298 qprint("growing continents...\n");
300 } while (fl_status && ++i < NUMTRIES);
302 fputs("ERROR: World not large enough to hold continents\n",
306 qprint("growing islands:");
308 qprint("\nelevating land...\n");
310 qprint("designating sectors...\n");
311 qprint("adding resources...\n");
312 if (!write_newcap_script())
315 if (chdir(gamedir)) {
316 fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
319 if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
322 qprint("writing to sectors file...\n");
323 if (!ef_close(EF_SECTOR))
327 qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
337 puts("Creating a planet with:\n");
338 printf("%d continents\n", nc);
339 printf("continent size: %d\n", sc);
340 printf("number of islands: %d\n", ni);
341 printf("average size of islands: %d\n", is);
342 printf("spike: %d%%\n", sp);
343 printf("%d%% of land is mountain (each continent will have %d mountains)\n",
344 pm, (pm * sc) / 100);
345 printf("minimum distance between continents: %d\n", di);
346 printf("minimum distance from islands to continents: %d\n", id);
347 printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
351 help(char *complaint)
354 fprintf(stderr, "%s: %s\n", program_name, complaint);
355 fprintf(stderr, "Try -h for help.\n");
361 printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
362 " -e CONFIG-FILE configuration file\n"
364 " -i islands may merge\n"
366 " -R SEED seed for random number generator\n"
367 " -s SCRIPT name of script to create (default %s)\n"
368 " -h display this help and exit\n"
369 " -v display version information and exit\n"
370 " NC number of continents\n"
371 " SC continent size\n"
372 " NI number of islands (default NC)\n"
373 " IS average island size (default SC/2)\n"
374 " SP spike percentage: 0 = round, 100 = snake (default %d)\n"
375 " PM percentage of land that is mountain (default %d)\n"
376 " DI minimum distance between continents (default %d)\n"
377 " ID minimum distance from islands to continents (default %d)\n",
378 program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
379 DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
383 parse_args(int argc, char *argv[])
386 help("missing arguments");
390 help("too many arguments");
395 puts("fairland: error -- number of continents must be > 0");
401 puts("fairland: error -- size of continents must be > 0");
421 sp = LIMIT_TO(sp, 0, 100);
426 pm = DEFAULT_MOUNTAIN;
433 di = DEFAULT_CONTDIST;
436 puts("fairland: error -- distance between continents must be >= 0");
439 if (di > WORLD_X / 2 || di > WORLD_Y / 2) {
440 puts("fairland: error -- distance between continents too large");
447 id = DEFAULT_ISLDIST;
449 puts("fairland: error -- distance from islands to continents must be >= 0");
452 if (id > WORLD_X || id > WORLD_Y) {
453 puts("fairland: error -- distance from islands to continents too large");
458 /****************************************************************************
459 VARIABLE INITIALIZATION
460 ****************************************************************************/
463 allocate_memory(void)
467 capx = calloc(nc, sizeof(int));
468 capy = calloc(nc, sizeof(int));
469 vector = calloc(WORLD_X + WORLD_Y, sizeof(int));
470 mc = calloc(STABLE_CYCLE, sizeof(int));
471 own = calloc(WORLD_X, sizeof(int *));
472 elev = calloc(WORLD_X, sizeof(int *));
473 for (i = 0; i < WORLD_X; ++i) {
474 own[i] = calloc(WORLD_Y, sizeof(int));
475 elev[i] = calloc(WORLD_Y, sizeof(int));
477 sectx = calloc(nc + ni, sizeof(int *));
478 secty = calloc(nc + ni, sizeof(int *));
479 sectc = calloc(nc + ni, sizeof(int *));
480 isecs = calloc(nc + ni, sizeof(int));
481 weight = calloc(MAX(sc, is * 2), sizeof(int));
482 dsea = calloc(MAX(sc, is * 2), sizeof(int));
483 dmoun = calloc(MAX(sc, is * 2), sizeof(int));
484 for (i = 0; i < nc; ++i) {
485 sectx[i] = calloc(sc, sizeof(int));
486 secty[i] = calloc(sc, sizeof(int));
487 sectc[i] = calloc(sc, sizeof(int));
489 for (i = nc; i < nc + ni; ++i) {
490 sectx[i] = calloc(is * 2, sizeof(int));
491 secty[i] = calloc(is * 2, sizeof(int));
492 sectc[i] = calloc(is * 2, sizeof(int));
500 int i, j, xx = 0, yy = 0;
505 for (i = 0; i < WORLD_X; ++i) {
506 for (j = 0; j < WORLD_Y; ++j) {
508 elev[i][j] = -INFINITY;
512 for (i = 0; i < nc; ++i) {
517 puts("fairland error: world not big enough for all the continents.\n");
525 for (i = 0; i < STABLE_CYCLE; ++i)
529 /****************************************************************************
530 DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
531 ****************************************************************************/
533 /* How isolated is capital j?
536 iso(int j, int newx, int newy)
538 int i, md, d = WORLD_X + WORLD_Y;
540 for (i = 0; i < nc; ++i) {
543 md = mapdist(capx[i], capy[i], newx, newy);
551 /* Drift all the capitals
558 for (turns = 0; turns < DRIFT_MAX; ++turns) {
559 if (turns > DRIFT_BEFORE_CHECK && (mind = stable()))
561 for (i = 0; i < nc; ++i)
567 /* Check to see if we have stabilized--can we stop drifting the capitals?
573 int i, isod, d = 0, stab = 1;
575 for (i = 0; i < nc; ++i) {
576 isod = iso(i, capx[i], capy[i]);
580 for (i = 0; i < STABLE_CYCLE; ++i)
584 mcc = (mcc + 1) % STABLE_CYCLE;
588 /* This routine does the actual drifting
594 int i, n, newx, newy;
596 for (i = roll0(6), n = 0; n < 6; i = (i + 1) % 6, ++n) {
597 newx = new_x(capx[j] + dirx[i]);
598 newy = new_y(capy[j] + diry[i]);
599 if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) {
607 /****************************************************************************
609 ****************************************************************************/
611 /* Look for a coastal sector of continent c
619 for (i = 0; i < secs; ++i) {
621 for (j = 0; j < 6; ++j)
622 if (own[new_x(sectx[c][i] + dirx[j])][new_y(secty[c][i] + diry[j])] == -1)
627 /* Used for measuring distances
639 for (i = 1; i < n && vector[i] == vector[i - 1]; ++i) ;
642 return i > 1 || vector[0] > 0;
645 /* Test to see if we're allowed to grow there: the arguments di and id
648 try_to_grow(int c, int newx, int newy, int d)
652 for (i = 1; i <= d; ++i) {
653 for (j = 0; j < i; ++j)
658 for (j = 0; j < i; ++j) {
659 px = new_x(px + dirx[vector[j]]);
660 py = new_y(py + diry[vector[j]]);
662 if (own[px][py] != -1 &&
664 (DISTINCT_ISLANDS || own[px][py] < nc))
666 } while (next_vector(i));
668 sectx[c][secs] = newx;
669 secty[c][secs] = newy;
674 /* Move along the coast in a clockwise direction.
678 next_coast(int c, int x, int y, int *xp, int *yp)
680 int i, nx, ny, wat = 0;
688 for (i = 0; i < 12; ++i) {
689 nx = new_x(x + dirx[i % 6]);
690 ny = new_y(y + diry[i % 6]);
691 if (own[nx][ny] == -1)
693 if (wat && own[nx][ny] == c) {
701 /* Choose a sector to grow from
713 i = starti = (spike && sectc[c][secs - 1]) ? secs - 1 : roll0(secs);
718 } while (i != starti);
725 /* Grow continent c by 1 sector
729 grow_one_sector(int c)
731 int done, coast_search, try1, x, y, newx, newy, i, n, sx, sy;
733 spike = roll0(100) < sp;
734 if ((try1 = new_try(c)) == -1)
736 x = sx = sectx[c][try1];
737 y = sy = secty[c][try1];
742 for (i = roll0(6), n = 0; n < 12 && !done; i = (i + 1) % 6, ++n) {
743 newx = new_x(x + dirx[i]);
744 newy = new_y(y + diry[i]);
745 if (own[newx][newy] == -1 &&
747 (own[new_x(x+dirx[(i+5)%6])][new_y(y+diry[(i+5)%6])] == -1 &&
748 own[new_x(x+dirx[(i+1)%6])][new_y(y+diry[(i+1)%6])] == -1)))
749 if (try_to_grow(c, newx, newy, c < nc ? di : id))
753 for (i = roll0(6), n = 0; n < 6 && !done; i = (i + 1) % 6, ++n) {
754 newx = new_x(x + dirx[i]);
755 newy = new_y(y + diry[i]);
756 if (own[newx][newy] == -1)
757 if (try_to_grow(c, newx, newy, c < nc ? di : id))
760 next_coast(c, x, y, &x, &y);
762 } while (!done && coast_search < COAST_SEARCH_MAX &&
763 (secs == 1 || x != sx || y != sy));
764 if (!done && c < nc) {
765 qprint("fairland: error -- continent %c had no room to grow!\n",
767 fl_status |= STATUS_NO_ROOM;
772 /* Grow all the continents
775 grow_continents(void)
779 for (c = 0; c < nc; ++c) {
780 sectx[c][0] = capx[c];
781 secty[c][0] = capy[c];
782 own[sectx[c][0]][secty[c][0]] = c;
783 sectx[c][1] = new_x(capx[c] + 2);
784 secty[c][1] = capy[c];
785 own[sectx[c][1]][secty[c][1]] = c;
788 for (secs = 2; secs < sc && !fl_status; ++secs) {
789 for (c = 0; c < nc; ++c) {
794 for (c = 0; c < nc; ++c)
798 qprint("Only managed to grow %d out of %d sectors.\n", secs, sc);
802 /****************************************************************************
804 ****************************************************************************/
806 /* Choose a place to start growing an island from
809 place_island(int c, int *xp, int *yp)
812 int ssy = roll0(WORLD_Y);
813 int ssx = new_x(roll0(WORLD_X / 2) * 2 + ssy % 2);
815 if (ssx > WORLD_X - 2)
816 ssx = new_x(ssx + 2);
817 for (d = di + id; d >= id; --d) {
821 for (*yp = sy; *xp != sx || *yp != sy; *xp += 2) {
822 if (*xp >= WORLD_X) {
823 *yp = new_y(*yp + 1);
825 if (*xp == sx && *yp == sy)
828 if (own[*xp][*yp] == -1 && try_to_grow(c, *xp, *yp, d))
835 /* Grow all the islands
843 for (c = nc; c < nc + ni; ++c) {
845 if (!place_island(c, &x, &y))
847 isiz = roll(is) + roll0(is);
851 } while (secs < isiz && grow_one_sector(c));
853 qprint(" %d(%d)", c - nc + 1, secs);
859 /****************************************************************************
861 ****************************************************************************/
863 create_elevations(void)
869 /* Generic function for finding the distance to the closest sea, land, or
873 distance_to_what(int x, int y, int flag)
877 for (d = 1; d < 5; ++d) {
878 for (j = 0; j < d; ++j)
883 for (j = 0; j < d; ++j) {
884 px = new_x(px + dirx[vector[j]]);
885 py = new_y(py + diry[vector[j]]);
888 case 0: /* distance to sea */
889 if (own[px][py] == -1)
892 case 1: /* distance to land */
893 if (own[px][py] != -1)
896 case 2: /* distance to mountain */
897 if (elev[px][py] == INFINITY)
901 } while (next_vector(d));
906 #define ELEV elev[sectx[c][i]][secty[c][i]]
907 #define distance_to_sea() (sectc[c][i]?1:distance_to_what(sectx[c][i], secty[c][i], 0))
908 #define distance_to_mountain() distance_to_what(sectx[c][i], secty[c][i], 2)
910 /* Decide where the mountains go
915 int i, mountain_search, k, c, total, ns, nm, highest, where, h, newk,
918 for (c = 0; c < ctot; ++c) {
920 ns = (c < nc) ? sc : isecs[c];
921 nm = (pm * ns) / 100;
923 /* Place the mountains */
925 for (i = 0; i < ns; ++i) {
926 dsea[i] = distance_to_sea();
927 weight[i] = (total += (dsea[i] * dsea[i]));
930 for (k = nm, mountain_search = 0;
931 k && mountain_search < MOUNTAIN_SEARCH_MAX;
934 for (i = 0; i < ns; ++i)
935 if (r < weight[i] && ELEV == -INFINITY &&
937 ((!(capx[c] == sectx[c][i] &&
938 capy[c] == secty[c][i])) &&
939 (!(new_x(capx[c] + 2) == sectx[c][i] &&
940 capy[c] == secty[c][i]))))) {
947 /* Elevate land that is not mountain and not capital */
949 for (i = 0; i < ns; ++i)
950 dmoun[i] = distance_to_mountain();
951 dk = (ns - nm - ((c < nc) ? 3 : 1) > 0) ?
952 (100 * (HIGHMIN - LANDMIN)) / (ns - nm - ((c < nc) ? 3 : 1)) :
954 for (k = 100 * (HIGHMIN - 1);; k -= dk) {
957 for (i = 0; i < ns; ++i) {
958 if (ELEV != INFINITY &&
959 (c >= nc || ((!(capx[c] == sectx[c][i] &&
960 capy[c] == secty[c][i])) &&
961 (!(new_x(capx[c] + 2) == sectx[c][i] &&
962 capy[c] == secty[c][i]))))) {
963 h = 3 * (5 - dmoun[i]) + dsea[i];
973 if (newk >= HILLMIN && newk < PLATMIN)
977 elev[sectx[c][where]][secty[c][where]] = newk;
978 dsea[where] = -INFINITY;
979 dmoun[where] = INFINITY;
982 /* Elevate the mountains and capitals */
984 for (i = 0; i < ns; ++i) {
985 if (ELEV == INFINITY) {
987 ELEV = HILLMIN + roll0(PLATMIN - HILLMIN);
989 ELEV = HIGHMIN + roll0((256 - HIGHMIN) / 2) +
990 roll0((256 - HIGHMIN) / 2);
992 (((capx[c] == sectx[c][i] && capy[c] == secty[c][i])) ||
993 ((new_x(capx[c] + 2) == sectx[c][i] &&
994 capy[c] == secty[c][i]))))
1000 #define distance_to_land() distance_to_what(x, y, 1)
1007 for (y = 0; y < WORLD_Y; ++y) {
1008 for (x = y % 2; x < WORLD_X; x += 2) {
1009 if (elev[x][y] == -INFINITY)
1010 elev[x][y] = -roll(distance_to_land() * 20 + 27);
1015 /****************************************************************************
1017 ****************************************************************************/
1024 fert = LANDMIN - e + 40;
1025 else if (e < FERT_MAX)
1026 fert = (120 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
1037 oil = (LANDMIN - e) * 2 + roll0(2);
1038 else if (e <= OIL_MAX)
1039 oil = (120 * (OIL_MAX - e + 1)) / (OIL_MAX - LANDMIN + 1);
1049 if (e >= IRON_MIN && e < HIGHMIN)
1050 iron = (120 * (e - IRON_MIN + 1)) / (HIGHMIN - IRON_MIN);
1060 if (e >= GOLD_MIN) {
1062 gold = (80 * (e - GOLD_MIN + 1)) / (HIGHMIN - GOLD_MIN);
1064 gold = 100 - 20 * HIGHMIN / e;
1075 if (e >= URAN_MIN && e < HIGHMIN)
1076 uran = (120 * (e - URAN_MIN + 1)) / (HIGHMIN - URAN_MIN);
1083 add_resources(struct sctstr *sct)
1085 sct->sct_fertil = set_fert(sct->sct_elev);
1086 sct->sct_oil = set_oil(sct->sct_elev);
1087 sct->sct_min = set_iron(sct->sct_elev);
1088 sct->sct_gmin = set_gold(sct->sct_elev);
1089 sct->sct_uran = set_uran(sct->sct_elev);
1092 /****************************************************************************
1093 DESIGNATE THE SECTORS
1094 ****************************************************************************/
1102 for (y = 0; y < WORLD_Y; y++) {
1103 for (x = y % 2; x < WORLD_X; x += 2) {
1104 sct = getsectp(x, y);
1106 if (total < LANDMIN) {
1107 sct->sct_type = SCT_WATER;
1108 } else if (total < HILLMIN)
1109 sct->sct_type = SCT_RURAL;
1110 else if (total < PLATMIN)
1111 sct->sct_type = SCT_MOUNT;
1112 else if (total < HIGHMIN)
1113 sct->sct_type = SCT_RURAL;
1115 sct->sct_type = SCT_MOUNT;
1116 sct->sct_elev = total;
1117 sct->sct_newtype = sct->sct_type;
1118 sct->sct_dterr = own[sct->sct_x][y] + 1;
1122 set_coastal_flags();
1125 /****************************************************************************
1126 PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1127 ****************************************************************************/
1134 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1139 for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1141 if (own[x][y] == -1)
1144 printf("%c ", map_symbol(x, y));
1152 map_symbol(int x, int y)
1156 for (c = 0; c < nc; ++c)
1157 if ((x == capx[c] && y == capy[c])
1158 || (x == new_x(capx[c] + 2) && y == capy[c]))
1159 return numletter[own[x][y] % 62];
1160 if ((elev[x][y] >= HILLMIN && elev[x][y] < PLATMIN)
1161 || elev[x][y] >= HIGHMIN)
1163 return own[x][y] >= nc ? '%' : '#';
1167 * Print a map to help visualize own[][].
1168 * This is for debugging.
1175 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1178 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1182 else if (own[x][y] == -1)
1185 putchar(numletter[own[x][y] % 62]);
1192 * Print a map to help visualize elev[][].
1193 * This is for debugging. It expects the terminal to understand
1194 * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1197 print_elev_map(void)
1199 int sx, sy, x, y, sat;
1201 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1204 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1208 else if (!elev[x][y])
1210 else if (elev[x][y] < 0) {
1211 sat = 256 + elev[x][y] * 2;
1212 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1213 } else if (elev[x][y] < HIGHMIN / 2) {
1214 sat = (HIGHMIN / 2 - elev[x][y]) * 4;
1215 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1216 } else if (elev[x][y] < HIGHMIN) {
1217 sat = 128 + (HIGHMIN - elev[x][y]) * 2;
1218 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1220 sat = 128 + (elev[x][y] - HIGHMIN) * 4 / 5;
1221 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1228 /***************************************************************************
1229 WRITE A SCRIPT FOR PLACING CAPITALS
1230 ****************************************************************************/
1232 write_newcap_script(void)
1235 FILE *script = fopen(outfile, "w");
1238 printf("fairland: error, unable to write to %s.\n", outfile);
1242 for (c = 0; c < nc; ++c) {
1243 fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1244 fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
1246 fprintf(script, "add %d visitor visitor v\n", c + 1);
1252 qprint(const char *const fmt, ...)
1258 vfprintf(stdout, fmt, ap);
1264 set_coastal_flags(void)
1269 qprint("setting coastal flags...\n");
1270 for (i = 0; i < nc; ++i) {
1271 for (j = 0; j < sc; j++) {
1272 sp = getsectp(sectx[i][j], secty[i][j]);
1273 sp->sct_coastal = sectc[i][j];
1276 for (i = nc; i < nc + ni; ++i) {
1277 for (j = 0; j < isecs[i]; j++) {
1278 sp = getsectp(sectx[i][j], secty[i][j]);
1279 sp->sct_coastal = sectc[i][j];