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 static int quiet = 0;
114 /* If you don't specify these command line arguments, then these are the
116 #define DEFAULT_SPIKE 10
117 #define DEFAULT_MOUNTAIN 0
118 #define DEFAULT_CONTDIST 2
119 #define DEFAULT_ISLDIST 1
121 /* The following five numbers refer to elevation under which (in the case of
122 fertility or oil) or over which (in the case of iron, gold, and uranium)
123 sectors with that elevation will contain that resource. Elevation ranges
126 /* raise FERT_MAX for more fertility */
129 /* raise OIL_MAX for more oil */
132 /* lower IRON_MIN for more iron */
135 /* lower GOLD_MIN for more gold */
138 /* lower URAN_MIN for more uranium */
141 /* do not change these 4 defines */
142 #define LANDMIN 1 /* plate altitude for normal land */
143 #define HILLMIN 34 /* plate altitude for hills */
144 #define PLATMIN 36 /* plate altitude for plateau */
145 #define HIGHMIN 98 /* plate altitude for mountains */
147 static void qprint(const char * const fmt, ...)
148 ATTRIBUTE((format (printf, 1, 2)));
150 #define DEFAULT_OUTFILE_NAME "newcap_script"
151 static const char *outfile = DEFAULT_OUTFILE_NAME;
153 /* don't let the islands crash into each other.
154 1 = don't merge, 0 = merge. */
155 static int DISTINCT_ISLANDS = 1;
157 static char *program_name;
159 #define STABLE_CYCLE 4 /* stability required for perterbed capitals */
160 #define INFINITY 999 /* a number which means "BIG" */
162 /* these defines prevent infinite loops:
165 #define COAST_SEARCH_MAX 200 /* how many times do we look for a coast sector
166 when growing continents and islands */
167 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
168 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
169 #define MOUNTAIN_SEARCH_MAX 1000 /* how long do we try to place mountains */
174 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
175 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
177 static int secs; /* number of sectors grown */
178 static int ctot; /* total number of continents and islands grown */
179 static int *isecs; /* array of how large each island is */
181 static int nc, sc, di, sp, pm, ni, is, id; /* the 8 args to this program */
182 static int *capx, *capy; /* location of the nc capitals */
183 static int *mc, mcc; /* array and counter used for stability
184 check when perturbing */
185 static int spike; /* are we spiking? */
186 static int mind; /* the final distance between capitals that
188 static int dirx[] = { -2, -1, 1, 2, 1, -1 }; /* gyujnb */
189 static int diry[] = { 0, -1, -1, 0, 1, 1 };
191 static int **own; /* owner of the sector. -1 means water */
192 static int **elev; /* elevation of the sectors */
193 static int **sectx, **secty; /* the sectors for each continent */
194 static int **sectc; /* which sectors are on the coast? */
195 static int *vector; /* used for measuring distances */
196 static int *weight; /* used for placing mountains */
197 static int *dsea, *dmoun; /* the dist to the ocean and mountain */
198 static int fl_status; /* is anything wrong? */
199 #define STATUS_NO_ROOM 1 /* there was no room to grow */
200 #define NUMTRIES 10 /* keep trying to grow this many times */
202 static const char *numletter =
203 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
205 static void help(char *);
206 static void usage(void);
207 static void parse_args(int argc, char *argv[]);
208 static void allocate_memory(void);
209 static void init(void);
210 static int drift(void);
211 static void grow_continents(void);
212 static void create_elevations(void);
213 static void write_sects(void);
214 static void output(void);
215 static int write_newcap_script(void);
216 static int stable(void);
217 static void elevate_land(void);
218 static void elevate_sea(void);
219 static int map_symbol(int x, int y);
220 static void set_coastal_flags(void);
222 static void print_vars(void);
223 static void fl_move(int);
224 static void grow_islands(void);
226 /* Debugging aids: */
227 void print_own_map(void);
228 void print_elev_map(void);
230 /****************************************************************************
232 ****************************************************************************/
235 main(int argc, char *argv[])
238 char *config_file = NULL;
240 unsigned rnd_seed = 0;
243 program_name = argv[0];
245 while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
248 config_file = optarg;
251 DISTINCT_ISLANDS = 0;
257 rnd_seed = strtoul(optarg, NULL, 10);
267 printf("%s\n\n%s", version, legal);
274 parse_args(argc - optind, argv + optind);
277 rnd_seed = pick_seed();
280 if (emp_config(config_file) < 0)
287 qprint("\n #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
288 qprint("seed is %u\n", rnd_seed);
292 qprint("\ntry #%d (out of %d)...\n", i + 1, NUMTRIES);
293 qprint("placing capitals...\n");
295 qprint("fairland: unstable drift -- try increasing DRIFT_MAX\n");
296 qprint("growing continents...\n");
298 } while (fl_status && ++i < NUMTRIES);
300 fputs("ERROR: World not large enough to hold continents\n",
304 qprint("growing islands:");
306 qprint("\nelevating land...\n");
308 qprint("designating sectors...\n");
309 qprint("adding resources...\n");
310 if (!write_newcap_script())
313 if (chdir(gamedir)) {
314 fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
317 if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
320 qprint("writing to sectors file...\n");
321 if (!ef_close(EF_SECTOR))
325 qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
335 puts("Creating a planet with:\n");
336 printf("%d continents\n", nc);
337 printf("continent size: %d\n", sc);
338 printf("number of islands: %d\n", ni);
339 printf("average size of islands: %d\n", is);
340 printf("spike: %d%%\n", sp);
341 printf("%d%% of land is mountain (each continent will have %d mountains)\n",
342 pm, (pm * sc) / 100);
343 printf("minimum distance between continents: %d\n", di);
344 printf("minimum distance from islands to continents: %d\n", id);
345 printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
353 for (i = 1; i * i < n * 10000; ++i) ;
354 return (i + 50) / 100;
357 /****************************************************************************
358 PARSE COMMAND LINE ARGUMENTS
359 ****************************************************************************/
362 help(char *complaint)
365 fprintf(stderr, "%s: %s\n", program_name, complaint);
366 fprintf(stderr, "Try -h for help.\n");
372 printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
373 " -e CONFIG-FILE configuration file\n"
375 " -i islands may merge\n"
377 " -R SEED seed for random number generator\n"
378 " -s SCRIPT name of script to create (default %s)\n"
379 " -h display this help and exit\n"
380 " -v display version information and exit\n"
381 " NC number of continents\n"
382 " SC continent size\n"
383 " NI number of islands (default NC)\n"
384 " IS average island size (default SC/2)\n"
385 " SP spike percentage: 0 = round, 100 = snake (default %d)\n"
386 " PM percentage of land that is mountain (default %d)\n"
387 " DI minimum distance between continents (default %d)\n"
388 " ID minimum distance from islands to continents (default %d)\n",
389 program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
390 DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
394 parse_args(int argc, char *argv[])
397 help("missing arguments");
401 help("too many arguments");
406 puts("fairland: error -- number of continents must be > 0");
412 puts("fairland: error -- size of continents must be > 0");
432 sp = LIMIT_TO(sp, 0, 100);
437 pm = DEFAULT_MOUNTAIN;
444 di = DEFAULT_CONTDIST;
447 puts("fairland: error -- distance between continents must be >= 0");
450 if (di > WORLD_X / 2 || di > WORLD_Y / 2) {
451 puts("fairland: error -- distance between continents too large");
458 id = DEFAULT_ISLDIST;
460 puts("fairland: error -- distance from islands to continents must be >= 0");
463 if (id > WORLD_X || id > WORLD_Y) {
464 puts("fairland: error -- distance from islands to continents too large");
467 if (nc * sc + nc * my_sqrt(sc) * 2 * (di + 1) > WORLD_X * WORLD_Y) {
468 puts("fairland: warning -- world might be too small to fit continents.");
469 puts("arguments should satisfy:");
470 puts("nc*sc*sc + nc*sqrt(sc)*2*(di+1) < WORLD_X * WORLD_Y");
474 /****************************************************************************
475 VARIABLE INITIALIZATION
476 ****************************************************************************/
479 allocate_memory(void)
483 capx = calloc(nc, sizeof(int));
484 capy = calloc(nc, sizeof(int));
485 vector = calloc(WORLD_X + WORLD_Y, sizeof(int));
486 mc = calloc(STABLE_CYCLE, sizeof(int));
487 own = calloc(WORLD_X, sizeof(int *));
488 elev = calloc(WORLD_X, sizeof(int *));
489 for (i = 0; i < WORLD_X; ++i) {
490 own[i] = calloc(WORLD_Y, sizeof(int));
491 elev[i] = calloc(WORLD_Y, sizeof(int));
493 sectx = calloc(nc + ni, sizeof(int *));
494 secty = calloc(nc + ni, sizeof(int *));
495 sectc = calloc(nc + ni, sizeof(int *));
496 isecs = calloc(nc + ni, sizeof(int));
497 weight = calloc(MAX(sc, is * 2), sizeof(int));
498 dsea = calloc(MAX(sc, is * 2), sizeof(int));
499 dmoun = calloc(MAX(sc, is * 2), sizeof(int));
500 for (i = 0; i < nc; ++i) {
501 sectx[i] = calloc(sc, sizeof(int));
502 secty[i] = calloc(sc, sizeof(int));
503 sectc[i] = calloc(sc, sizeof(int));
505 for (i = nc; i < nc + ni; ++i) {
506 sectx[i] = calloc(is * 2, sizeof(int));
507 secty[i] = calloc(is * 2, sizeof(int));
508 sectc[i] = calloc(is * 2, sizeof(int));
516 int i, j, xx = 0, yy = 0;
521 for (i = 0; i < WORLD_X; ++i) {
522 for (j = 0; j < WORLD_Y; ++j) {
524 elev[i][j] = -INFINITY;
528 for (i = 0; i < nc; ++i) {
533 puts("fairland error: world not big enough for all the continents.\n");
541 for (i = 0; i < STABLE_CYCLE; ++i)
545 /****************************************************************************
546 DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
547 ****************************************************************************/
549 /* How isolated is capital j?
552 iso(int j, int newx, int newy)
554 int i, md, d = WORLD_X + WORLD_Y;
556 for (i = 0; i < nc; ++i) {
559 md = mapdist(capx[i], capy[i], newx, newy);
567 /* Drift all the capitals
574 for (turns = 0; turns < DRIFT_MAX; ++turns) {
575 if (turns > DRIFT_BEFORE_CHECK && (mind = stable()))
577 for (i = 0; i < nc; ++i)
583 /* Check to see if we have stabilized--can we stop drifting the capitals?
589 int i, isod, d = 0, stab = 1;
591 for (i = 0; i < nc; ++i) {
592 isod = iso(i, capx[i], capy[i]);
596 for (i = 0; i < STABLE_CYCLE; ++i)
600 mcc = (mcc + 1) % STABLE_CYCLE;
604 /* This routine does the actual drifting
610 int i, n, newx, newy;
612 for (i = roll0(6), n = 0; n < 6; i = (i + 1) % 6, ++n) {
613 newx = new_x(capx[j] + dirx[i]);
614 newy = new_y(capy[j] + diry[i]);
615 if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) {
623 /****************************************************************************
625 ****************************************************************************/
627 /* Look for a coastal sector of continent c
635 for (i = 0; i < secs; ++i) {
637 for (j = 0; j < 6; ++j)
638 if (own[new_x(sectx[c][i] + dirx[j])][new_y(secty[c][i] + diry[j])] == -1)
643 /* Used for measuring distances
655 for (i = 1; i < n && vector[i] == vector[i - 1]; ++i) ;
658 return i > 1 || vector[0] > 0;
661 /* Test to see if we're allowed to grow there: the arguments di and id
664 try_to_grow(int c, int newx, int newy, int d)
668 for (i = 1; i <= d; ++i) {
669 for (j = 0; j < i; ++j)
674 for (j = 0; j < i; ++j) {
675 px = new_x(px + dirx[vector[j]]);
676 py = new_y(py + diry[vector[j]]);
678 if (own[px][py] != -1 &&
680 (DISTINCT_ISLANDS || own[px][py] < nc))
682 } while (next_vector(i));
684 sectx[c][secs] = newx;
685 secty[c][secs] = newy;
690 /* Move along the coast in a clockwise direction.
694 next_coast(int c, int x, int y, int *xp, int *yp)
696 int i, nx, ny, wat = 0;
704 for (i = 0; i < 12; ++i) {
705 nx = new_x(x + dirx[i % 6]);
706 ny = new_y(y + diry[i % 6]);
707 if (own[nx][ny] == -1)
709 if (wat && own[nx][ny] == c) {
717 /* Choose a sector to grow from
729 i = starti = (spike && sectc[c][secs - 1]) ? secs - 1 : roll0(secs);
734 } while (i != starti);
741 /* Grow continent c by 1 sector
745 grow_one_sector(int c)
747 int done, coast_search, try1, x, y, newx, newy, i, n, sx, sy;
749 spike = roll0(100) < sp;
750 if ((try1 = new_try(c)) == -1)
752 x = sx = sectx[c][try1];
753 y = sy = secty[c][try1];
758 for (i = roll0(6), n = 0; n < 12 && !done; i = (i + 1) % 6, ++n) {
759 newx = new_x(x + dirx[i]);
760 newy = new_y(y + diry[i]);
761 if (own[newx][newy] == -1 &&
763 (own[new_x(x+dirx[(i+5)%6])][new_y(y+diry[(i+5)%6])] == -1 &&
764 own[new_x(x+dirx[(i+1)%6])][new_y(y+diry[(i+1)%6])] == -1)))
765 if (try_to_grow(c, newx, newy, c < nc ? di : id))
769 for (i = roll0(6), n = 0; n < 6 && !done; i = (i + 1) % 6, ++n) {
770 newx = new_x(x + dirx[i]);
771 newy = new_y(y + diry[i]);
772 if (own[newx][newy] == -1)
773 if (try_to_grow(c, newx, newy, c < nc ? di : id))
776 next_coast(c, x, y, &x, &y);
778 } while (!done && coast_search < COAST_SEARCH_MAX &&
779 (secs == 1 || x != sx || y != sy));
780 if (!done && c < nc) {
781 qprint("fairland: error -- continent %c had no room to grow!\n",
783 fl_status |= STATUS_NO_ROOM;
788 /* Grow all the continents
791 grow_continents(void)
795 for (c = 0; c < nc; ++c) {
796 sectx[c][0] = capx[c];
797 secty[c][0] = capy[c];
798 own[sectx[c][0]][secty[c][0]] = c;
799 sectx[c][1] = new_x(capx[c] + 2);
800 secty[c][1] = capy[c];
801 own[sectx[c][1]][secty[c][1]] = c;
804 for (secs = 2; secs < sc && !fl_status; ++secs) {
805 for (c = 0; c < nc; ++c) {
810 for (c = 0; c < nc; ++c)
814 qprint("Only managed to grow %d out of %d sectors.\n", secs, sc);
818 /****************************************************************************
820 ****************************************************************************/
822 /* Choose a place to start growing an island from
825 place_island(int c, int *xp, int *yp)
828 int ssy = roll0(WORLD_Y);
829 int ssx = new_x(roll0(WORLD_X / 2) * 2 + ssy % 2);
831 if (ssx > WORLD_X - 2)
832 ssx = new_x(ssx + 2);
833 for (d = di + id; d >= id; --d) {
837 for (*yp = sy; *xp != sx || *yp != sy; *xp += 2) {
838 if (*xp >= WORLD_X) {
839 *yp = new_y(*yp + 1);
841 if (*xp == sx && *yp == sy)
844 if (own[*xp][*yp] == -1 && try_to_grow(c, *xp, *yp, d))
851 /* Grow all the islands
859 for (c = nc; c < nc + ni; ++c) {
861 if (!place_island(c, &x, &y))
863 isiz = roll(is) + roll0(is);
867 } while (secs < isiz && grow_one_sector(c));
869 qprint(" %d(%d)", c - nc + 1, secs);
875 /****************************************************************************
877 ****************************************************************************/
879 create_elevations(void)
885 /* Generic function for finding the distance to the closest sea, land, or
889 distance_to_what(int x, int y, int flag)
893 for (d = 1; d < 5; ++d) {
894 for (j = 0; j < d; ++j)
899 for (j = 0; j < d; ++j) {
900 px = new_x(px + dirx[vector[j]]);
901 py = new_y(py + diry[vector[j]]);
904 case 0: /* distance to sea */
905 if (own[px][py] == -1)
908 case 1: /* distance to land */
909 if (own[px][py] != -1)
912 case 2: /* distance to mountain */
913 if (elev[px][py] == INFINITY)
917 } while (next_vector(d));
922 #define ELEV elev[sectx[c][i]][secty[c][i]]
923 #define distance_to_sea() (sectc[c][i]?1:distance_to_what(sectx[c][i], secty[c][i], 0))
924 #define distance_to_mountain() distance_to_what(sectx[c][i], secty[c][i], 2)
926 /* Decide where the mountains go
931 int i, mountain_search, k, c, total, ns, nm, highest, where, h, newk,
934 for (c = 0; c < ctot; ++c) {
936 ns = (c < nc) ? sc : isecs[c];
937 nm = (pm * ns) / 100;
939 /* Place the mountains */
941 for (i = 0; i < ns; ++i) {
942 dsea[i] = distance_to_sea();
943 weight[i] = (total += (dsea[i] * dsea[i]));
946 for (k = nm, mountain_search = 0;
947 k && mountain_search < MOUNTAIN_SEARCH_MAX;
950 for (i = 0; i < ns; ++i)
951 if (r < weight[i] && ELEV == -INFINITY &&
953 ((!(capx[c] == sectx[c][i] &&
954 capy[c] == secty[c][i])) &&
955 (!(new_x(capx[c] + 2) == sectx[c][i] &&
956 capy[c] == secty[c][i]))))) {
963 /* Elevate land that is not mountain and not capital */
965 for (i = 0; i < ns; ++i)
966 dmoun[i] = distance_to_mountain();
967 dk = (ns - nm - ((c < nc) ? 3 : 1) > 0) ?
968 (100 * (HIGHMIN - LANDMIN)) / (ns - nm - ((c < nc) ? 3 : 1)) :
970 for (k = 100 * (HIGHMIN - 1);; k -= dk) {
973 for (i = 0; i < ns; ++i) {
974 if (ELEV != INFINITY &&
975 (c >= nc || ((!(capx[c] == sectx[c][i] &&
976 capy[c] == secty[c][i])) &&
977 (!(new_x(capx[c] + 2) == sectx[c][i] &&
978 capy[c] == secty[c][i]))))) {
979 h = 3 * (5 - dmoun[i]) + dsea[i];
989 if (newk >= HILLMIN && newk < PLATMIN)
993 elev[sectx[c][where]][secty[c][where]] = newk;
994 dsea[where] = -INFINITY;
995 dmoun[where] = INFINITY;
998 /* Elevate the mountains and capitals */
1000 for (i = 0; i < ns; ++i) {
1001 if (ELEV == INFINITY) {
1003 ELEV = HILLMIN + roll0(PLATMIN - HILLMIN);
1005 ELEV = HIGHMIN + roll0((256 - HIGHMIN) / 2) +
1006 roll0((256 - HIGHMIN) / 2);
1007 } else if (c < nc &&
1008 (((capx[c] == sectx[c][i] && capy[c] == secty[c][i])) ||
1009 ((new_x(capx[c] + 2) == sectx[c][i] &&
1010 capy[c] == secty[c][i]))))
1016 #define distance_to_land() distance_to_what(x, y, 1)
1023 for (y = 0; y < WORLD_Y; ++y) {
1024 for (x = y % 2; x < WORLD_X; x += 2) {
1025 if (elev[x][y] == -INFINITY)
1026 elev[x][y] = -roll(distance_to_land() * 20 + 27);
1031 /****************************************************************************
1033 ****************************************************************************/
1040 fert = LANDMIN - e + 40;
1041 else if (e < FERT_MAX)
1042 fert = (120 * (FERT_MAX - e)) / (FERT_MAX - LANDMIN);
1053 oil = (LANDMIN - e) * 2 + roll0(2);
1054 else if (e <= OIL_MAX)
1055 oil = (120 * (OIL_MAX - e + 1)) / (OIL_MAX - LANDMIN + 1);
1065 if (e >= IRON_MIN && e < HIGHMIN)
1066 iron = (120 * (e - IRON_MIN + 1)) / (HIGHMIN - IRON_MIN);
1076 if (e >= GOLD_MIN) {
1078 gold = (80 * (e - GOLD_MIN + 1)) / (HIGHMIN - GOLD_MIN);
1080 gold = 100 - 20 * HIGHMIN / e;
1091 if (e >= URAN_MIN && e < HIGHMIN)
1092 uran = (120 * (e - URAN_MIN + 1)) / (HIGHMIN - URAN_MIN);
1099 add_resources(struct sctstr *sct)
1101 sct->sct_fertil = set_fert(sct->sct_elev);
1102 sct->sct_oil = set_oil(sct->sct_elev);
1103 sct->sct_min = set_iron(sct->sct_elev);
1104 sct->sct_gmin = set_gold(sct->sct_elev);
1105 sct->sct_uran = set_uran(sct->sct_elev);
1108 /****************************************************************************
1109 DESIGNATE THE SECTORS
1110 ****************************************************************************/
1118 for (y = 0; y < WORLD_Y; y++) {
1119 for (x = y % 2; x < WORLD_X; x += 2) {
1120 sct = getsectp(x, y);
1122 if (total < LANDMIN) {
1123 sct->sct_type = SCT_WATER;
1124 } else if (total < HILLMIN)
1125 sct->sct_type = SCT_RURAL;
1126 else if (total < PLATMIN)
1127 sct->sct_type = SCT_MOUNT;
1128 else if (total < HIGHMIN)
1129 sct->sct_type = SCT_RURAL;
1131 sct->sct_type = SCT_MOUNT;
1132 sct->sct_elev = total;
1133 sct->sct_newtype = sct->sct_type;
1134 sct->sct_dterr = own[sct->sct_x][y] + 1;
1138 set_coastal_flags();
1141 /****************************************************************************
1142 PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1143 ****************************************************************************/
1150 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1155 for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1157 if (own[x][y] == -1)
1160 printf("%c ", map_symbol(x, y));
1168 map_symbol(int x, int y)
1172 for (c = 0; c < nc; ++c)
1173 if ((x == capx[c] && y == capy[c])
1174 || (x == new_x(capx[c] + 2) && y == capy[c]))
1175 return numletter[own[x][y] % 62];
1176 if ((elev[x][y] >= HILLMIN && elev[x][y] < PLATMIN)
1177 || elev[x][y] >= HIGHMIN)
1179 return own[x][y] >= nc ? '%' : '#';
1183 * Print a map to help visualize own[][].
1184 * This is for debugging.
1191 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1194 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1198 else if (own[x][y] == -1)
1201 putchar(numletter[own[x][y] % 62]);
1208 * Print a map to help visualize elev[][].
1209 * This is for debugging. It expects the terminal to understand
1210 * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1213 print_elev_map(void)
1215 int sx, sy, x, y, sat;
1217 for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1220 for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1224 else if (!elev[x][y])
1226 else if (elev[x][y] < 0) {
1227 sat = 256 + elev[x][y] * 2;
1228 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1229 } else if (elev[x][y] < HIGHMIN / 2) {
1230 sat = (HIGHMIN / 2 - elev[x][y]) * 4;
1231 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1232 } else if (elev[x][y] < HIGHMIN) {
1233 sat = 128 + (HIGHMIN - elev[x][y]) * 2;
1234 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1236 sat = 128 + (elev[x][y] - HIGHMIN) * 4 / 5;
1237 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1244 /***************************************************************************
1245 WRITE A SCRIPT FOR PLACING CAPITALS
1246 ****************************************************************************/
1248 write_newcap_script(void)
1251 FILE *script = fopen(outfile, "w");
1254 printf("fairland: error, unable to write to %s.\n", outfile);
1258 for (c = 0; c < nc; ++c) {
1259 fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1260 fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
1262 fprintf(script, "add %d visitor visitor v\n", c + 1);
1268 qprint(const char *const fmt, ...)
1274 vfprintf(stdout, fmt, ap);
1280 set_coastal_flags(void)
1285 qprint("setting coastal flags...\n");
1286 for (i = 0; i < nc; ++i) {
1287 for (j = 0; j < sc; j++) {
1288 sp = getsectp(sectx[i][j], secty[i][j]);
1289 sp->sct_coastal = sectc[i][j];
1292 for (i = nc; i < nc + ni; ++i) {
1293 for (j = 0; j < isecs[i]; j++) {
1294 sp = getsectp(sectx[i][j], secty[i][j]);
1295 sp->sct_coastal = sectc[i][j];