]> git.pond.sub.org Git - empserver/blob - src/util/fairland.c
74c96059742ec626ed0961c4ca6eaf68a1693e6c
[empserver] / src / util / fairland.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  fairland.c: Create a nice, new world
28  *
29  *  Known contributors to this file:
30  *     Ken Stevens, 1995
31  *     Steve McClure, 1998
32  *     Markus Armbruster, 2004-2020
33  */
34
35 /*
36  * How fairland works
37  *
38  * 1. Place capitals
39  *
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.
43  *
44  * 2. Grow start islands ("continents")
45  *
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
49  * specified size.
50  *
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.
59  *
60  * If growing fails due to lack of room, start over.  If it fails too
61  * many times, give up and terminate unsuccessfully.
62  *
63  * 3. Place and grow additional islands
64  *
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.
69  *
70  * First, split the specified number of island sectors per continent
71  * randomly into the island sizes.  Sort by size so that larger
72  * islands are grown before smaller ones, to give the large ones the
73  * best chance to grow to their planned size.
74  *
75  * Then place one island's first sector into each sphere, using
76  * weighted random sampling with weights favoring sectors away from
77  * land and other spheres.  Add one sector to each island in turn,
78  * until they have the intended size.  Repeat until the specified
79  * number of islands has been grown.
80  *
81  * If placement fails due to lack of room, start over, just like for
82  * continents.
83  *
84  * Growing works as for continents, except the minimum distance for
85  * additional islands applies, and growing simply stops when any of
86  * the islands being grown lacks the room to grow further.  The number
87  * of sectors not grown carries over to the next island size.
88  *
89  * 4. Compute elevation
90  *
91  * First, use a simple random hill algorithm to assign raw elevations:
92  * initialize elevation to zero, then randomly raise circular hills on
93  * land / lower circular depressions at sea.  Their size and height
94  * depends on the distance to the coast.
95  *
96  * Then, elevate islands one after the other.
97  *
98  * Set the capitals' elevation to a fixed value.  Process the
99  * remaining sectors in order of increasing raw elevation, first
100  * non-mountains, then mountains.  Non-mountain elevation starts at 1,
101  * and increases linearly to just below "high" elevation.  Mountain
102  * elevation starts at "high" elevation, and increases linearly.
103  *
104  * This gives islands of the same size the same set of elevations.
105  * Larger islands get more and taller mountains.
106  *
107  * Finally, elevate sea: normalize the raw elevations to [-127:-1].
108  *
109  * 5. Set resources
110  *
111  * Sector resources are simple functions of elevation.  You can alter
112  * iron_conf[], gold_conf[], fert_conf[], oil_conf[], and uran_conf[]
113  * to customize them.
114  */
115
116 #include <config.h>
117
118 #include <assert.h>
119 #include <errno.h>
120 #include <limits.h>
121 #include <stdarg.h>
122 #include <stdio.h>
123 #include <unistd.h>
124 #include "chance.h"
125 #include "optlist.h"
126 #include "path.h"
127 #include "prototypes.h"
128 #include "sect.h"
129 #include "version.h"
130 #include "xy.h"
131
132 /* do not change these defines */
133 #define LANDMIN         1       /* plate altitude for normal land */
134 #define PLATMIN         36      /* plate altitude for plateau */
135 #define HIGHMIN         98      /* plate altitude for mountains */
136
137 /*
138  * Resource configuration
139
140  * Resources are determined by elevation.  The map from elevation to
141  * resource is defined as a linear interpolation of resource data
142  * points (elev, res) defined in the tables below.  Elevations range
143  * from -127 to 127, and resource values from 0 to 100.
144  */
145
146 struct resource_point {
147     int elev;
148     double res;
149 };
150
151 struct resource_point iron_conf[] = {
152     { -127, 0 },
153     { 21, 0 },
154     { 84, 120.0 * 63 / 76 },
155     { 85, 100 },
156     { HIGHMIN - 1, 100 },
157     { HIGHMIN , 0 },
158     { 127, 0 } };
159
160 struct resource_point gold_conf[] = {
161     { -127, 0 },
162     { 35, 0 },
163     { HIGHMIN - 1, 80 },
164     { HIGHMIN, 80 },
165     { 127, 85 } };
166
167 struct resource_point fert_conf[] = {
168     { -127, 100 },
169     { -59, 100 },
170     { LANDMIN - 1, 41 },
171     { LANDMIN, 100 },
172     { 10, 100 },
173     { 11, 120.0 * 45 / 55 },
174     { 56, 0 },
175     { 127, 0 } };
176
177 struct resource_point oil_conf[] = {
178     { -127, 100 },
179     { -49, 100 },
180     { LANDMIN - 1, 2 },
181     { LANDMIN, 100 },
182     { 6, 100 },
183     { 7, 120.0 * 27 / 33 },
184     { 34, 0 },
185     { 127, 0 } };
186
187 struct resource_point uran_conf[] = {
188     { -127, 0 },
189     { 55, 0 },
190     { 90, 100 },
191     { 97, 100 },
192     { 98, 0 },
193     { 127, 0 } };
194
195 static void qprint(const char * const fmt, ...)
196     ATTRIBUTE((format (printf, 1, 2)));
197
198 /*
199  * Program arguments and options
200  */
201 static char *program_name;
202 static int nc, sc;              /* number and size of continents */
203 static int ni, is;              /* number and size of islands */
204 #define DEFAULT_SPIKE 10
205 static int sp = DEFAULT_SPIKE;  /* spike percentage */
206 #define DEFAULT_MOUNTAIN 0
207 static int pm = DEFAULT_MOUNTAIN; /* mountain percentage */
208 #define DEFAULT_CONTDIST 2
209 static int di = DEFAULT_CONTDIST; /* min. distance between continents */
210 #define DEFAULT_ISLDIST 1
211 static int id = DEFAULT_ISLDIST;  /* ... continents and islands */
212 /* don't let the islands crash into each other.
213    1 = don't merge, 0 = merge. */
214 static int DISTINCT_ISLANDS = 1;
215 static int quiet;
216 #define DEFAULT_OUTFILE_NAME "newcap_script"
217 static const char *outfile = DEFAULT_OUTFILE_NAME;
218
219 #define STABLE_CYCLE 4          /* stability required for perterbed capitals */
220 #define DRIFT_BEFORE_CHECK ((WORLD_X + WORLD_Y)/2)
221 #define DRIFT_MAX ((WORLD_X + WORLD_Y)*2)
222
223 /* handy macros:
224 */
225
226 #define new_x(newx) (((newx) + WORLD_X) % WORLD_X)
227 #define new_y(newy) (((newy) + WORLD_Y) % WORLD_Y)
228
229 /*
230  * Island sizes
231  * isecs[i] is the size of the i-th island.
232  */
233 static int *isecs;
234
235 static int *capx, *capy;        /* location of the nc capitals */
236
237 static int **own;               /* owner of the sector.  -1 means water */
238
239 /*
240  * Adjacent land sectors
241  * adj_land[XYOFFSET(x, y)] bit d is set exactly when the sector next
242  * to x, y in direction d is land.
243  */
244 static unsigned char *adj_land;
245
246 /*
247  * Elevation at x,y
248  * elev[XYOFFSET(x, y)] is x,y's elevation.
249  */
250 static short *elev;
251
252 /*
253  * Exclusive zones
254  * Each island is surrounded by an exclusive zone where only it may
255  * grow.  The width of the zone depends on minimum distances.
256  * While growing continents, it is @di sectors wide.
257  * While growing additional islands, it is @id sectors wide.
258  * DISTINCT_ISLANDS nullifies the exclusive zone then.
259  * xzone[XYOFFSET(x, y)] is -1 when the sector is in no exclusive
260  * zone, a (non-negative) island number when it is in that island's
261  * exclusive zone and no other, and -2 when it is in multiple
262  * exclusive zones.
263  */
264 static short *xzone;
265
266 /*
267  * Set of sectors seen already
268  * Increment @cur_seen to empty the set of sectors seen, set
269  * seen[XYOFFSET(x, y)] to @cur_seen to add x,y to the set.
270  */
271 static unsigned *seen;
272 static unsigned cur_seen;
273
274 /*
275  * Closest continent and "distance"
276  * closest[XYOFFSET(x, y)] is the closest continent's number.
277  * distance[] is complicated; see init_spheres_of_influence() and
278  * init_distance_to_coast().
279  */
280 static natid *closest;
281 static unsigned short *distance;
282
283 /*
284  * Queue for breadth-first search
285  */
286 static int *bfs_queue;
287 static int bfs_queue_head, bfs_queue_tail;
288
289 static int **sectx, **secty;    /* the sectors for each continent */
290
291 #define NUMTRIES 10             /* keep trying to grow this many times */
292
293 static const char *numletter =
294     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
295
296 static void help(char *);
297 static void usage(void);
298 static void parse_args(int argc, char *argv[]);
299 static void allocate_memory(void);
300 static void init(void);
301 static int drift(void);
302 static int grow_continents(void);
303 static void create_elevations(void);
304 static void write_sects(void);
305 static void output(void);
306 static int write_newcap_script(void);
307 static int stable(int);
308 static void elevate_prep(void);
309 static void elevate_land(void);
310 static void elevate_sea(void);
311
312 static void print_vars(void);
313 static void fl_move(int);
314 static int grow_islands(void);
315
316 /* Debugging aids: */
317 void print_own_map(void);
318 void print_xzone_map(void);
319 void print_closest_map(void);
320 void print_distance_map(void);
321 void print_elev_map(void);
322
323 /****************************************************************************
324   MAIN
325 ****************************************************************************/
326
327 int
328 main(int argc, char *argv[])
329 {
330     int opt;
331     char *config_file = NULL;
332     int try, done;
333     unsigned rnd_seed = 0;
334     int seed_set = 0;
335
336     program_name = argv[0];
337
338     while ((opt = getopt(argc, argv, "e:hiqR:s:v")) != EOF) {
339         switch (opt) {
340         case 'e':
341             config_file = optarg;
342             break;
343         case 'i':
344             DISTINCT_ISLANDS = 0;
345             break;
346         case 'q':
347             quiet = 1;
348             break;
349         case 'R':
350             rnd_seed = strtoul(optarg, NULL, 10);
351             seed_set = 1;
352             break;
353         case 's':
354             outfile = optarg;
355             break;
356         case 'h':
357             usage();
358             exit(0);
359         case 'v':
360             printf("%s\n\n%s", version, legal);
361             exit(0);
362         default:
363             help(NULL);
364             exit(1);
365         }
366     }
367
368     if (!seed_set)
369         rnd_seed = pick_seed();
370     seed_prng(rnd_seed);
371     empfile_init();
372     if (emp_config(config_file) < 0)
373         exit(1);
374     empfile_fixup();
375
376     parse_args(argc - optind, argv + optind);
377
378     allocate_memory();
379     print_vars();
380
381     qprint("\n        #*# ...fairland rips open a rift in the datumplane... #*#\n\n");
382     qprint("seed is %u\n", rnd_seed);
383     try = 0;
384     do {
385         init();
386         if (try)
387             qprint("\ntry #%d (out of %d)...\n", try + 1, NUMTRIES);
388         qprint("placing capitals...\n");
389         if (!drift())
390             qprint("unstable drift\n");
391         qprint("growing continents...\n");
392         done = grow_continents();
393         if (!done)
394             continue;
395         qprint("growing islands:");
396         done = grow_islands();
397     } while (!done && ++try < NUMTRIES);
398     if (!done) {
399         fprintf(stderr, "%s: world not large enough for this much land\n",
400                 program_name);
401         exit(1);
402     }
403     qprint("elevating land...\n");
404     create_elevations();
405
406     qprint("writing to sectors file...\n");
407     if (!write_newcap_script())
408         exit(1);
409     if (chdir(gamedir)) {
410         fprintf(stderr, "%s: can't chdir to %s (%s)\n",
411                 program_name, gamedir, strerror(errno));
412         exit(1);
413     }
414     if (!ef_open(EF_SECTOR, EFF_MEM | EFF_NOTIME))
415         exit(1);
416     write_sects();
417     if (!ef_close(EF_SECTOR))
418         exit(1);
419
420     output();
421     qprint("\n\nA script for adding all the countries can be found in \"%s\".\n",
422            outfile);
423     exit(0);
424 }
425
426 static void
427 print_vars(void)
428 {
429     if (quiet)
430         return;
431     puts("Creating a planet with:\n");
432     printf("%d continents\n", nc);
433     printf("continent size: %d\n", sc);
434     printf("number of islands: %d\n", ni);
435     printf("average size of islands: %d\n", is);
436     printf("spike: %d%%\n", sp);
437     printf("%d%% of land is mountain (each continent will have %d mountains)\n",
438            pm, (pm * sc) / 100);
439     printf("minimum distance between continents: %d\n", di);
440     printf("minimum distance from islands to continents: %d\n", id);
441     printf("World dimensions: %dx%d\n", WORLD_X, WORLD_Y);
442 }
443
444 static void
445 help(char *complaint)
446 {
447     if (complaint)
448         fprintf(stderr, "%s: %s\n", program_name, complaint);
449     fprintf(stderr, "Try -h for help.\n");
450 }
451
452 static void
453 usage(void)
454 {
455     printf("Usage: %s [OPTION]... NC SC [NI] [IS] [SP] [PM] [DI] [ID]\n"
456            "  -e CONFIG-FILE  configuration file\n"
457            "                  (default %s)\n"
458            "  -i              islands may merge\n"
459            "  -q              quiet\n"
460            "  -R SEED         seed for random number generator\n"
461            "  -s SCRIPT       name of script to create (default %s)\n"
462            "  -h              display this help and exit\n"
463            "  -v              display version information and exit\n"
464            "  NC              number of continents\n"
465            "  SC              continent size\n"
466            "  NI              number of islands (default NC)\n"
467            "  IS              average island size (default SC/2)\n"
468            "  SP              spike percentage: 0 = round, 100 = snake (default %d)\n"
469            "  PM              percentage of land that is mountain (default %d)\n"
470            "  DI              minimum distance between continents (default %d)\n"
471            "  ID              minimum distance from islands to continents (default %d)\n",
472            program_name, dflt_econfig, DEFAULT_OUTFILE_NAME,
473            DEFAULT_SPIKE, DEFAULT_MOUNTAIN, DEFAULT_CONTDIST, DEFAULT_ISLDIST);
474 }
475
476 static void
477 parse_args(int argc, char *argv[])
478 {
479     int dist_max = mapdist(0, 0, WORLD_X / 2, WORLD_Y / 2);
480
481     if (argc < 2) {
482         help("missing arguments");
483         exit(1);
484     }
485     if (argc > 8) {
486         help("too many arguments");
487         exit(1);
488     }
489     nc = atoi(argv[0]);
490     if (nc < 1) {
491         fprintf(stderr, "%s: number of continents must be > 0\n",
492                 program_name);
493         exit(1);
494     }
495
496     sc = atoi(argv[1]);
497     if (sc < 2) {
498         fprintf(stderr, "%s: size of continents must be > 1\n",
499                 program_name);
500         exit(1);
501     }
502
503     ni = nc;
504     is = sc / 2;
505
506     if (argc > 2)
507         ni = atoi(argv[2]);
508     if (ni < 0) {
509         fprintf(stderr, "%s: number of islands must be >= 0\n",
510                 program_name);
511         exit(1);
512     }
513     if (ni % nc) {
514         fprintf(stderr, "%s: number of islands must be a multiple of"
515                 " the number of continents\n",
516                 program_name);
517         exit(1);
518     }
519
520     if (argc > 3)
521         is = atoi(argv[3]);
522     if (is < 1) {
523         fprintf(stderr, "%s: size of islands must be > 0\n",
524                 program_name);
525         exit(1);
526     }
527
528     if (argc > 4)
529         sp = atoi(argv[4]);
530     if (sp < 0 || sp > 100) {
531         fprintf(stderr,
532                 "%s: spike percentage must be between 0 and 100\n",
533                 program_name);
534         exit(1);
535     }
536
537     if (argc > 5)
538         pm = atoi(argv[5]);
539     if (pm < 0 || pm > 100) {
540         fprintf(stderr,
541                 "%s: mountain percentage must be between 0 and 100\n",
542                 program_name);
543         exit(1);
544     }
545
546     if (argc > 6)
547         di = atoi(argv[6]);
548     if (di < 0) {
549         fprintf(stderr, "%s: distance between continents must be >= 0\n",
550                 program_name);
551         exit(1);
552     }
553     if (di > dist_max) {
554         fprintf(stderr, "%s: distance between continents too large\n",
555                 program_name);
556         exit(1);
557     }
558
559     if (argc > 7)
560         id = atoi(argv[7]);
561     if (id < 0) {
562         fprintf(stderr,
563                 "%s: distance from islands to continents must be >= 0\n",
564                 program_name);
565         exit(1);
566     }
567     if (id > dist_max) {
568         fprintf(stderr,
569                 "%s: distance from islands to continents too large\n",
570                 program_name);
571         exit(1);
572     }
573 }
574
575 /****************************************************************************
576   VARIABLE INITIALIZATION
577 ****************************************************************************/
578
579 static void
580 allocate_memory(void)
581 {
582     int i;
583
584     capx = calloc(nc, sizeof(int));
585     capy = calloc(nc, sizeof(int));
586     own = calloc(WORLD_X, sizeof(int *));
587     adj_land = malloc(WORLD_SZ() * sizeof(*adj_land));
588     elev = calloc(WORLD_SZ(), sizeof(*elev));
589     xzone = malloc(WORLD_SZ() * sizeof(*xzone));
590     seen = calloc(WORLD_SZ(), sizeof(*seen));
591     closest = malloc(WORLD_SZ() * sizeof(*closest));
592     distance = malloc(WORLD_SZ() * sizeof(*distance));
593     bfs_queue = malloc(WORLD_SZ() * sizeof(*bfs_queue));
594     for (i = 0; i < WORLD_X; ++i) {
595         own[i] = calloc(WORLD_Y, sizeof(int));
596     }
597     sectx = calloc(nc + ni, sizeof(int *));
598     secty = calloc(nc + ni, sizeof(int *));
599     isecs = calloc(nc + ni, sizeof(int));
600     for (i = 0; i < nc; ++i) {
601         sectx[i] = calloc(sc, sizeof(int));
602         secty[i] = calloc(sc, sizeof(int));
603     }
604     for (i = nc; i < nc + ni; ++i) {
605         sectx[i] = calloc(is * 2, sizeof(int));
606         secty[i] = calloc(is * 2, sizeof(int));
607     }
608
609 }
610
611 static void
612 init(void)
613 {
614     int i, j;
615
616     for (i = 0; i < WORLD_X; ++i) {
617         for (j = 0; j < WORLD_Y; ++j) {
618             own[i][j] = -1;
619         }
620     }
621     memset(adj_land, 0, WORLD_SZ() * sizeof(*adj_land));
622 }
623
624 /****************************************************************************
625   DRIFT THE CAPITALS UNTIL THEY ARE AS FAR AWAY FROM EACH OTHER AS POSSIBLE
626 ****************************************************************************/
627
628 /*
629  * How isolated is capital @j at @newx,@newy?
630  * Return the distance to the closest other capital.
631  */
632 static int
633 iso(int j, int newx, int newy)
634 {
635     int d = INT_MAX;
636     int i, md;
637
638     for (i = 0; i < nc; ++i) {
639         if (i == j)
640             continue;
641         md = mapdist(capx[i], capy[i], newx, newy);
642         if (md < d)
643             d = md;
644     }
645
646     return d;
647 }
648
649 /*
650  * Drift the capitals
651  * Return 1 for a stable drift, 0 for an unstable one.
652  */
653 static int
654 drift(void)
655 {
656     int turns, i;
657
658     for (i = 0; i < nc; i++) {
659         capy[i] = (2 * i) / WORLD_X;
660         capx[i] = (2 * i) % WORLD_X + capy[i] % 2;
661         if (capy[i] >= WORLD_Y) {
662             fprintf(stderr,
663                     "%s: world not big enough for all the continents\n",
664                     program_name);
665             exit(1);
666         }
667     }
668
669     for (turns = 0; turns < DRIFT_MAX; ++turns) {
670         if (stable(turns))
671             return 1;
672         for (i = 0; i < nc; ++i)
673             fl_move(i);
674     }
675     return 0;
676 }
677
678 /*
679  * Has the drift stabilized?
680  * @turns is the number of turns so far.
681  */
682 static int
683 stable(int turns)
684 {
685     static int mc[STABLE_CYCLE];
686     int i, isod, d = 0, stab = 1;
687
688     if (!turns) {
689         for (i = 0; i < STABLE_CYCLE; i++)
690             mc[i] = i;
691     }
692
693     if (turns <= DRIFT_BEFORE_CHECK)
694         return 0;
695
696     for (i = 0; i < nc; ++i) {
697         isod = iso(i, capx[i], capy[i]);
698         if (isod > d)
699             d = isod;
700     }
701
702     for (i = 0; i < STABLE_CYCLE; ++i)
703         if (d != mc[i])
704             stab = 0;
705
706     mc[turns % STABLE_CYCLE] = d;
707     return stab;
708 }
709
710 /* This routine does the actual drifting
711 */
712
713 static void
714 fl_move(int j)
715 {
716     int dir, i, newx, newy;
717
718     dir = DIR_L + roll0(6);
719     for (i = 0; i < 6; i++) {
720         if (dir > DIR_LAST)
721             dir -= 6;
722         newx = new_x(capx[j] + diroff[dir][0]);
723         newy = new_y(capy[j] + diroff[dir][1]);
724         dir++;
725         if (iso(j, newx, newy) >= iso(j, capx[j], capy[j])) {
726             capx[j] = newx;
727             capy[j] = newy;
728             return;
729         }
730     }
731 }
732
733 /****************************************************************************
734   GROW THE CONTINENTS
735 ****************************************************************************/
736
737 static int
738 is_coastal(int x, int y)
739 {
740     return adj_land[XYOFFSET(x, y)]
741         != (1u << (DIR_LAST + 1)) - (1u << DIR_FIRST);
742 }
743
744 struct hexagon_iter {
745     int dir, i, n;
746 };
747
748 /*
749  * Start iterating around @x0,@y0 at distance @d.
750  * Set *x,*y to coordinates of the first sector.
751  */
752 static inline void
753 hexagon_first(struct hexagon_iter *iter, int x0, int y0, int n,
754               int *x, int *y)
755 {
756     *x = new_x(x0 - 2 * n);
757     *y = y0;
758     iter->dir = DIR_FIRST;
759     iter->i = 0;
760     iter->n = n;
761 }
762
763 /*
764  * Continue iteration started with hexagon_first().
765  * Set *x,*y to coordinates of the next sector.
766  * Return whether we're back at the first sector, i.e. iteration is
767  * complete.
768  */
769 static inline int
770 hexagon_next(struct hexagon_iter *iter, int *x, int *y)
771 {
772     *x = new_x(*x + diroff[iter->dir][0]);
773     *y = new_y(*y + diroff[iter->dir][1]);
774     iter->i++;
775     if (iter->i == iter->n) {
776         iter->i = 0;
777         iter->dir++;
778     }
779     return iter->dir <= DIR_LAST;
780 }
781
782 /*
783  * Is @x,@y in no exclusive zone other than perhaps @c's?
784  */
785 static int
786 xzone_ok(int c, int x, int y)
787 {
788     int off = XYOFFSET(x, y);
789
790     return xzone[off] == c || xzone[off] == -1;
791 }
792
793 /*
794  * Add sectors within distance @dist of @x,@y to @c's exclusive zone.
795  */
796 static void
797 xzone_around_sector(int c, int x, int y, int dist)
798 {
799     int d, x1, y1, off;
800     struct hexagon_iter hexit;
801
802     assert(xzone_ok(c, x, y));
803
804     xzone[XYOFFSET(x, y)] = c;
805     for (d = 1; d <= dist; d++) {
806         hexagon_first(&hexit, x, y, d, &x1, &y1);
807         do {
808             off = XYOFFSET(x1, y1);
809             if (xzone[off] == -1)
810                 xzone[off] = c;
811             else if (xzone[off] != c)
812                 xzone[off] = -2;
813         } while (hexagon_next(&hexit, &x1, &y1));
814     }
815 }
816
817 /*
818  * Add sectors within distance @dist to island @c's exclusive zone.
819  */
820 static void
821 xzone_around_island(int c, int dist)
822 {
823     int i;
824
825     for (i = 0; i < isecs[c]; i++)
826         xzone_around_sector(c, sectx[c][i], secty[c][i], dist);
827 }
828
829 /*
830  * Initialize exclusive zones around @n islands.
831  */
832 static void
833 xzone_init(int n)
834 {
835     int i, c;
836
837     for (i = 0; i < WORLD_SZ(); i++)
838         xzone[i] = -1;
839
840     for (c = 0; c < n; c++)
841         xzone_around_island(c, id);
842 }
843
844 /*
845  * Initialize breadth-first search.
846  */
847 static void
848 bfs_init(void)
849 {
850     int i;
851
852     for (i = 0; i < WORLD_SZ(); i++) {
853         closest[i] = -1;
854         distance[i] = USHRT_MAX;
855     }
856
857     bfs_queue_head = bfs_queue_tail = 0;
858 }
859
860 /*
861  * Add sector @x,@y to the BFS queue.
862  * It's closest to @c, with distance @dist.
863  */
864 static void
865 bfs_enqueue(int c, int x, int y, int dist)
866 {
867     int off = XYOFFSET(x, y);
868
869     assert(dist < distance[off]);
870     closest[off] = c;
871     distance[off] = dist;
872     bfs_queue[bfs_queue_tail] = off;
873     bfs_queue_tail++;
874     if (bfs_queue_tail >= WORLD_SZ())
875         bfs_queue_tail = 0;
876     assert(bfs_queue_tail != bfs_queue_head);
877 }
878
879 /*
880  * Search breadth-first until the queue is empty.
881  */
882 static void
883 bfs_run_queue(void)
884 {
885     int off, dist, i, noff, nx, ny;
886     coord x, y;
887
888     while (bfs_queue_head != bfs_queue_tail) {
889         off = bfs_queue[bfs_queue_head];
890         bfs_queue_head++;
891         if (bfs_queue_head >= WORLD_SZ())
892             bfs_queue_head = 0;
893         dist = distance[off] + 1;
894         sctoff2xy(&x, &y, off);
895         for (i = DIR_FIRST; i <= DIR_LAST; i++) {
896             nx = new_x(x + diroff[i][0]);
897             ny = new_y(y + diroff[i][1]);
898             noff = XYOFFSET(nx, ny);
899             if (dist < distance[noff]) {
900                 bfs_enqueue(closest[off], nx, ny, dist);
901             } else if (distance[noff] == dist) {
902                 if (closest[off] != closest[noff])
903                     closest[noff] = (natid)-1;
904             } else
905                 assert(distance[noff] < dist);
906         }
907     }
908 }
909
910 /*
911  * Add island @c's coastal sectors to the BFS queue, with distance 0.
912  */
913 static void
914 bfs_enqueue_island(int c)
915 {
916     int i;
917
918     for (i = 0; i < isecs[c]; i++) {
919         if (is_coastal(sectx[c][i], secty[c][i]))
920             bfs_enqueue(c, sectx[c][i], secty[c][i], 0);
921     }
922 }
923
924 /*
925  * Enqueue spheres of influence borders for breadth-first search.
926  */
927 static void
928 bfs_enqueue_border(void)
929 {
930     int x, y, off, dir, nx, ny, noff;
931
932     for (y = 0; y < WORLD_Y; y++) {
933         for (x = y % 2; x < WORLD_X; x += 2) {
934             off = XYOFFSET(x, y);
935             if (distance[off] <= id + 1)
936                 continue;
937             if (closest[off] == (natid)-1)
938                 continue;
939             for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
940                 nx = new_x(x + diroff[dir][0]);
941                 ny = new_y(y + diroff[dir][1]);
942                 noff = XYOFFSET(nx, ny);
943                 if (closest[noff] != closest[off]) {
944                     bfs_enqueue(closest[off], x, y, id + 1);
945                     break;
946                 }
947             }
948         }
949     }
950 }
951
952 /*
953  * Compute spheres of influence
954  * A continent's sphere of influence is the set of sectors closer to
955  * it than to any other continent.
956  * Set closest[XYOFFSET(x, y)] to the closest continent's number,
957  * -1 if no single continent is closest.
958  * Set distance[XYOFFSET(x, y)] to the minimum of the distance to the
959  * closest coastal land sector and the distance to just outside the
960  * sphere of influence plus @id.  For sea sectors within a continent's
961  * sphere of influence, distance[off] - id is the distance to the
962  * border of the area where additional islands can be placed.
963  */
964 static void
965 init_spheres_of_influence(void)
966 {
967     int c;
968
969     bfs_init();
970     for (c = 0; c < nc; c++)
971         bfs_enqueue_island(c);
972     bfs_run_queue();
973     bfs_enqueue_border();
974     bfs_run_queue();
975 }
976
977 /*
978  * Precompute distance to coast
979  * Set distance[XYOFFSET(x, y)] to the distance to the closest coastal
980  * land sector.
981  * Set closest[XYOFFSET(x, y)] to the closest continent's number,
982  * -1 if no single continent is closest.
983  */
984 static void
985 init_distance_to_coast(void)
986 {
987     int c;
988
989     bfs_init();
990     for (c = 0; c < nc + ni; c++)
991         bfs_enqueue_island(c);
992     bfs_run_queue();
993 }
994
995 /*
996  * Is @x,@y in the same sphere of influence as island @c?
997  * Always true when @c is a continent.
998  */
999 static int
1000 is_in_sphere(int c, int x, int y)
1001 {
1002     return c < nc || closest[XYOFFSET(x, y)] == c % nc;
1003 }
1004
1005 /*
1006  * Can island @c grow at @x,@y?
1007  */
1008 static int
1009 can_grow_at(int c, int x, int y)
1010 {
1011     return own[x][y] == -1 && xzone_ok(c, x, y) && is_in_sphere(c, x, y);
1012 }
1013
1014 static void
1015 adj_land_update(int x, int y)
1016 {
1017     int is_land = own[x][y] != -1;
1018     int dir, nx, ny, noff;
1019
1020     for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
1021         nx = new_x(x + diroff[dir][0]);
1022         ny = new_y(y + diroff[dir][1]);
1023         noff = XYOFFSET(nx, ny);
1024         if (is_land)
1025             adj_land[noff] |= 1u << DIR_BACK(dir);
1026         else
1027             adj_land[noff] &= ~(1u << DIR_BACK(dir));
1028     }
1029 }
1030
1031 static void
1032 add_sector(int c, int x, int y)
1033 {
1034     assert(own[x][y] == -1);
1035     xzone_around_sector(c, x, y, c < nc ? di : DISTINCT_ISLANDS ? id : 0);
1036     sectx[c][isecs[c]] = x;
1037     secty[c][isecs[c]] = y;
1038     isecs[c]++;
1039     own[x][y] = c;
1040     adj_land_update(x, y);
1041 }
1042
1043 static int grow_weight(int c, int x, int y, int spike)
1044 {
1045     int n, b;
1046
1047     /*
1048      * #Land neighbors is #bits set in adj_land[].
1049      * Count them Brian Kernighan's way.
1050      */
1051     n = 0;
1052     for (b = adj_land[XYOFFSET(x, y)]; b; b &= b - 1)
1053         n++;
1054     assert(n > 0 && n < 7);
1055
1056     if (spike)
1057         return (6 - n) * (6 - n);
1058
1059     return n * n * n;
1060 }
1061
1062 static int
1063 grow_one_sector(int c)
1064 {
1065     int spike = roll0(100) < sp;
1066     int wsum, newx, newy, i, x, y, off, dir, nx, ny, noff, w;
1067
1068     assert(cur_seen < UINT_MAX);
1069     cur_seen++;
1070     wsum = 0;
1071     newx = newy = -1;
1072
1073     for (i = 0; i < isecs[c]; i++) {
1074         x = sectx[c][i];
1075         y = secty[c][i];
1076         off = XYOFFSET(x, y);
1077
1078         for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
1079             if (adj_land[off] & (1u << dir))
1080                 continue;
1081             nx = new_x(x + diroff[dir][0]);
1082             ny = new_y(y + diroff[dir][1]);
1083             noff = XYOFFSET(nx, ny);
1084             if (seen[noff] == cur_seen)
1085                 continue;
1086             assert(seen[noff] < cur_seen);
1087             seen[noff] = cur_seen;
1088             if (!can_grow_at(c, nx, ny))
1089                 continue;
1090             w = grow_weight(c, nx, ny, spike);
1091             assert(wsum < INT_MAX - w);
1092             wsum += w;
1093             if (roll0(wsum) < w) {
1094                 newx = nx;
1095                 newy = ny;
1096             }
1097         }
1098     }
1099
1100     if (!wsum)
1101         return 0;
1102
1103     add_sector(c, newx, newy);
1104     return 1;
1105 }
1106
1107 /*
1108  * Grow the continents.
1109  * Return 1 on success, 0 on error.
1110  */
1111 static int
1112 grow_continents(void)
1113 {
1114     int done = 1;
1115     int c, secs;
1116
1117     xzone_init(0);
1118
1119     for (c = 0; c < nc; ++c) {
1120         isecs[c] = 0;
1121         if (!can_grow_at(c, capx[c], capy[c])
1122             || !can_grow_at(c, new_x(capx[c] + 2), capy[c])) {
1123             done = 0;
1124             continue;
1125         }
1126         add_sector(c, capx[c], capy[c]);
1127         add_sector(c, new_x(capx[c] + 2), capy[c]);
1128     }
1129
1130     if (!done) {
1131         qprint("No room for continents\n");
1132         return 0;
1133     }
1134
1135     for (secs = 2; secs < sc && done; secs++) {
1136         for (c = 0; c < nc; ++c) {
1137             if (!grow_one_sector(c))
1138                 done = 0;
1139         }
1140     }
1141
1142     if (!done)
1143         qprint("Only managed to grow %d out of %d sectors.\n",
1144                secs - 1, sc);
1145     return done;
1146 }
1147
1148 /****************************************************************************
1149   GROW THE ISLANDS
1150 ****************************************************************************/
1151
1152 /*
1153  * Place additional island @c's first sector.
1154  * Return 1 on success, 0 on error.
1155  */
1156 static int
1157 place_island(int c, int isiz)
1158 {
1159     int n, x, y, d, w, newx, newy;
1160
1161     n = 0;
1162
1163     for (y = 0; y < WORLD_Y; y++) {
1164         for (x = y % 2; x < WORLD_X; x += 2) {
1165             if (can_grow_at(c, x, y)) {
1166                 d = distance[XYOFFSET(x, y)];
1167                 assert(d > id);
1168                 w = (d - id) * (d - id);
1169                 n += MIN(w, (isiz + 2) / 3);
1170                 if (roll0(n) < w) {
1171                     newx = x;
1172                     newy = y;
1173                 }
1174             }
1175         }
1176     }
1177
1178     if (n)
1179         add_sector(c, newx, newy);
1180     return n;
1181 }
1182
1183 static int
1184 int_cmp(const void *a, const void *b)
1185 {
1186     return *(int *)b - *(int *)a;
1187 }
1188
1189 static int *
1190 size_islands(void)
1191 {
1192     int n = ni / nc;
1193     int *isiz = malloc(n * sizeof(*isiz));
1194     int r0, r1, i;
1195
1196     isiz[0] = n * is;
1197     r1 = roll0(is);
1198     for (i = 1; i < n; i++) {
1199         r0 = r1;
1200         r1 = roll0(is);
1201         isiz[i] = is + r1 - r0;
1202         isiz[0] -= isiz[i];
1203     }
1204
1205     qsort(isiz, n, sizeof(*isiz), int_cmp);
1206     return isiz;
1207 }
1208
1209 /*
1210  * Grow the additional islands.
1211  * Return 1 on success, 0 on error.
1212  */
1213 static int
1214 grow_islands(void)
1215 {
1216     int *island_size = size_islands();
1217     int xzone_valid = 0;
1218     int carry = 0;
1219     int i, j, c, done, secs, isiz, x, y;
1220
1221     init_spheres_of_influence();
1222
1223     for (i = 0; i < ni / nc; i++) {
1224         c = nc + i * nc;
1225
1226         if (!xzone_valid)
1227             xzone_init(c);
1228
1229         carry += island_size[i];
1230         isiz = MIN(2 * is, carry);
1231
1232         for (j = 0; j < nc; j++) {
1233             isecs[c + j] = 0;
1234             if (!place_island(c + j, isiz)) {
1235                 qprint("\nNo room for island #%d\n", c - nc + j + 1);
1236                 free(island_size);
1237                 return 0;
1238             }
1239         }
1240
1241         done = 1;
1242         for (secs = 1; secs < isiz && done; secs++) {
1243             for (j = 0; j < nc; j++) {
1244                 if (!grow_one_sector(c + j))
1245                     done = 0;
1246             }
1247         }
1248
1249         if (!done) {
1250             secs--;
1251             for (j = 0; j < nc; j++) {
1252                 if (isecs[c + j] != secs) {
1253                     isecs[c + j]--;
1254                     assert(isecs[c + j] == secs);
1255                     x = sectx[c + j][secs];
1256                     y = secty[c + j][secs];
1257                     own[x][y] = -1;
1258                     adj_land_update(x, y);
1259                 }
1260             }
1261             xzone_valid = 0;
1262         }
1263
1264         for (j = 0; j < nc; j++)
1265             qprint(" %d(%d)", c - nc + j + 1, isecs[c + j]);
1266
1267         carry -= secs;
1268     }
1269
1270     free(island_size);
1271     qprint("\n");
1272
1273     if (carry)
1274         qprint("Only managed to grow %d out of %d island sectors.\n",
1275                is * ni - carry * nc, is * ni);
1276
1277     return 1;
1278 }
1279
1280 /****************************************************************************
1281   CREATE ELEVATIONS
1282 ****************************************************************************/
1283 static void
1284 create_elevations(void)
1285 {
1286     elevate_prep();
1287     elevate_land();
1288     elevate_sea();
1289 }
1290
1291 static int
1292 elev_cmp(const void *p, const void *q)
1293 {
1294     int a = *(int *)p;
1295     int b = *(int *)q;
1296     int delev = elev[a] - elev[b];
1297
1298     return delev ? delev : a - b;
1299 }
1300
1301 static void
1302 elevate_prep(void)
1303 {
1304     int n = WORLD_SZ() * 8;
1305     int off0, r, sign, elevation, d, x1, y1, off1;
1306     coord x0, y0;
1307     struct hexagon_iter hexit;
1308
1309     init_distance_to_coast();
1310
1311     while (n > 0) {
1312         off0 = roll0(WORLD_SZ());
1313         sctoff2xy(&x0, &y0, off0);
1314         if (own[x0][y0] == -1) {
1315             r = roll(MIN(3, distance[off0]));
1316             sign = -1;
1317         } else {
1318             r = roll(MIN(3, distance[off0]) + 1);
1319             sign = 1;
1320         }
1321         elevation = elev[off0] + sign * r * r;
1322         elev[off0] = LIMIT_TO(elevation, SHRT_MIN, SHRT_MAX);
1323         n--;
1324         for (d = 1; d < r; d++) {
1325             hexagon_first(&hexit, x0, y0, d, &x1, &y1);
1326             do {
1327                 off1 = XYOFFSET(x1, y1);
1328                 elevation = elev[off1] + sign * (r * r - d * d);
1329                 elev[off1] = LIMIT_TO(elevation, SHRT_MIN, SHRT_MAX);
1330                 n--;
1331             } while (hexagon_next(&hexit, &x1, &y1));
1332         }
1333     }
1334 }
1335
1336 static void
1337 elevate_land(void)
1338 {
1339     int *off = malloc(MAX(sc, is * 2) * sizeof(*off));
1340     int max_nm = (pm * MAX(sc, is * 2)) / 100;
1341     int c, nm, i0, n, i;
1342     double elevation, delta;
1343
1344     for (c = 0; c < nc + ni; c++) {
1345         nm = (pm * isecs[c]) / 100;
1346         i0 = c < nc ? 2 : 0;
1347         n = isecs[c] - i0;
1348         for (i = 0; i < i0; i++)
1349             elev[XYOFFSET(sectx[c][i], secty[c][i])] = PLATMIN;
1350         for (i = 0; i < n; i++)
1351             off[i] = XYOFFSET(sectx[c][i0 + i], secty[c][i0 + i]);
1352         qsort(off, n, sizeof(*off), elev_cmp);
1353         delta = (double)(HIGHMIN - LANDMIN - 1) / (n - nm - 1);
1354         elevation = LANDMIN;
1355         for (i = 0; i < n - nm; i++) {
1356             elev[off[i]] = (int)(elevation + 0.5);
1357             elevation += delta;
1358         }
1359         elevation = HIGHMIN;
1360         delta = (127.0 - HIGHMIN) / max_nm;
1361         for (; i < n; i++) {
1362             elevation += delta;
1363             elev[off[i]] = (int)(elevation + 0.5);
1364         }
1365     }
1366
1367     free(off);
1368 }
1369
1370 static void
1371 elevate_sea(void)
1372 {
1373     int i, min;
1374
1375     min = 0;
1376     for (i = 0; i < WORLD_SZ(); i++) {
1377         if (elev[i] < min)
1378             min = elev[i];
1379     }
1380
1381     for (i = 0; i < WORLD_SZ(); i++) {
1382         if (elev[i] < 0)
1383             elev[i] = -1 - 126 * elev[i] / min;
1384     }
1385 }
1386
1387 static int
1388 elev_to_sct_type(int elevation)
1389 {
1390     if (elevation < LANDMIN)
1391         return SCT_WATER;
1392     if (elevation < HIGHMIN)
1393         return SCT_RURAL;
1394     return SCT_MOUNT;
1395 }
1396
1397 /****************************************************************************
1398   ADD THE RESOURCES
1399 ****************************************************************************/
1400
1401 /*
1402  * Map elevation @elev to a resource value according to @conf.
1403  * This is a linear interpolation on the data points in @conf.
1404  */
1405 static int
1406 elev_to_resource(int elev, struct resource_point conf[])
1407 {
1408     int i, elev1, elev2, delev;
1409     double res1, res2, dres;
1410
1411     for (i = 1; elev > conf[i].elev; i++) ;
1412     assert(conf[i - 1].elev <= elev);
1413
1414     elev1 = conf[i - 1].elev;
1415     elev2 = conf[i].elev;
1416     delev = elev2 - elev1;
1417     res1 = conf[i - 1].res;
1418     res2 = conf[i].res;
1419     dres = res2 - res1;
1420     return (int)(res1 + ((elev - elev1) * dres) / delev);
1421 }
1422
1423 static void
1424 add_resources(struct sctstr *sct)
1425 {
1426     sct->sct_min = elev_to_resource(sct->sct_elev, iron_conf);
1427     sct->sct_gmin = elev_to_resource(sct->sct_elev, gold_conf);
1428     sct->sct_fertil = elev_to_resource(sct->sct_elev, fert_conf);
1429     sct->sct_oil = elev_to_resource(sct->sct_elev, oil_conf);
1430     sct->sct_uran = elev_to_resource(sct->sct_elev, uran_conf);
1431 }
1432
1433 /****************************************************************************
1434   DESIGNATE THE SECTORS
1435 ****************************************************************************/
1436
1437 static void
1438 write_sects(void)
1439 {
1440     struct sctstr *sct;
1441     int x, y;
1442
1443     for (y = 0; y < WORLD_Y; y++) {
1444         for (x = y % 2; x < WORLD_X; x += 2) {
1445             sct = getsectp(x, y);
1446             sct->sct_elev = elev[sct->sct_uid];
1447             sct->sct_type = elev_to_sct_type(sct->sct_elev);
1448             sct->sct_newtype = sct->sct_type;
1449             sct->sct_dterr = own[sct->sct_x][y] + 1;
1450             sct->sct_coastal = is_coastal(sct->sct_x, sct->sct_y);
1451             add_resources(sct);
1452         }
1453     }
1454 }
1455
1456 /****************************************************************************
1457   PRINT A PICTURE OF THE MAP TO YOUR SCREEN
1458 ****************************************************************************/
1459 static void
1460 output(void)
1461 {
1462     int sx, sy, x, y, off, c, type;
1463
1464     if (quiet == 0) {
1465         for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1466             y = YNORM(sy);
1467             puts("");
1468             if (y % 2)
1469                 printf(" ");
1470             for (sx = -WORLD_X / 2 + y % 2; sx < WORLD_X / 2; sx += 2) {
1471                 x = XNORM(sx);
1472                 off = XYOFFSET(x, y);
1473                 c = own[x][y];
1474                 type = elev_to_sct_type(elev[off]);
1475                 if (type == SCT_WATER)
1476                     printf(". ");
1477                 else if (type == SCT_MOUNT)
1478                     printf("^ ");
1479                 else if (c >= nc)
1480                     printf("%% ");
1481                 else {
1482                     assert(0 <= c && c < nc);
1483                     if ((x == capx[c] || x == new_x(capx[c] + 2))
1484                         && y == capy[c])
1485                         printf("%c ", numletter[c % 62]);
1486                     else
1487                         printf("# ");
1488                 }
1489             }
1490         }
1491     }
1492 }
1493
1494 /*
1495  * Print a map to help visualize own[][].
1496  * This is for debugging.
1497  */
1498 void
1499 print_own_map(void)
1500 {
1501     int sx, sy, x, y;
1502
1503     for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1504         y = YNORM(sy);
1505         printf("%4d ", sy);
1506         for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1507             x = XNORM(sx);
1508             if ((x + y) & 1)
1509                 putchar(' ');
1510             else if (own[x][y] == -1)
1511                 putchar('.');
1512             else
1513                 putchar(numletter[own[x][y] % 62]);
1514         }
1515         putchar('\n');
1516     }
1517 }
1518
1519 /*
1520  * Print a map to help visualize elev[].
1521  * This is for debugging.  It expects the terminal to understand
1522  * 24-bit color escape sequences \e[48;2;$red;$green;$blue;m.
1523  */
1524 void
1525 print_elev_map(void)
1526 {
1527     int sx, sy, x, y, off, sat;
1528
1529     for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1530         y = YNORM(sy);
1531         printf("%4d ", sy);
1532         for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1533             x = XNORM(sx);
1534             off = XYOFFSET(x, y);
1535             if ((x + y) & 1)
1536                 putchar(' ');
1537             else if (!elev[off])
1538                 putchar(' ');
1539             else if (elev[off] < 0) {
1540                 sat = 256 + elev[off] * 2;
1541                 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat, 255);
1542             } else if (elev[off] < HIGHMIN / 2) {
1543                 sat = (HIGHMIN / 2 - elev[off]) * 4;
1544                 printf("\033[48;2;%d;%d;%dm \033[0m", sat, 255, sat);
1545             } else if (elev[off] < HIGHMIN) {
1546                 sat = 128 + (HIGHMIN - elev[off]) * 2;
1547                 printf("\033[48;2;%d;%d;%dm \033[0m", sat, sat / 2, sat / 4);
1548             } else {
1549                 sat = 128 + (elev[off] - HIGHMIN) * 2;
1550                 printf("\033[48;2;%d;%d;%dm^\033[0m", sat, sat, sat);
1551             }
1552         }
1553         putchar('\n');
1554     }
1555 }
1556
1557 /*
1558  * Print a map to help visualize xzone[].
1559  * This is for debugging.
1560  */
1561 void
1562 print_xzone_map(void)
1563 {
1564     int sx, sy, x, y, off;
1565
1566     for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1567         y = YNORM(sy);
1568         printf("%4d ", sy);
1569         for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1570             x = XNORM(sx);
1571             off = XYOFFSET(x, y);
1572             if ((x + y) & 1)
1573                 putchar(' ');
1574             else if (own[x][y] >= 0)
1575                 putchar('-');
1576             else if (xzone[off] >= 0)
1577                 putchar(numletter[xzone[off] % 62]);
1578             else {
1579                 assert(own[x][y] == -1);
1580                 putchar(xzone[off] == -1 ? '.' : '!');
1581             }
1582         }
1583         putchar('\n');
1584     }
1585 }
1586
1587 /*
1588  * Print a map to help visualize closest[].
1589  * This is for debugging.
1590  */
1591 void
1592 print_closest_map(void)
1593 {
1594     int sx, sy, x, y, off;
1595
1596     for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1597         y = YNORM(sy);
1598         printf("%4d ", sy);
1599         for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1600             x = XNORM(sx);
1601             off = XYOFFSET(x, y);
1602             if ((x + y) & 1)
1603                 putchar(' ');
1604             else if (closest[off] == (natid)-1)
1605                 putchar('.');
1606             else if (!distance[off]) {
1607                 assert(closest[off] == own[x][y]);
1608                 putchar('-');
1609             } else {
1610                 putchar(numletter[closest[off] % 62]);
1611             }
1612         }
1613         printf("\n");
1614     }
1615 }
1616
1617 void
1618 print_distance_map(void)
1619 {
1620     int sx, sy, x, y, off;
1621
1622     for (sy = -WORLD_Y / 2; sy < WORLD_Y / 2; sy++) {
1623         y = YNORM(sy);
1624         printf("%4d ", sy);
1625         for (sx = -WORLD_X / 2; sx < WORLD_X / 2; sx++) {
1626             x = XNORM(sx);
1627             off = XYOFFSET(x, y);
1628             if ((x + y) & 1)
1629                 putchar(' ');
1630             else if (closest[off] == (natid)-1)
1631                 putchar('.');
1632             else if (!distance[off]) {
1633                 assert(closest[off] == own[x][y]);
1634                 putchar('-');
1635             } else {
1636                 putchar(numletter[distance[off] % 62]);
1637             }
1638         }
1639         printf("\n");
1640     }
1641 }
1642
1643
1644 /***************************************************************************
1645   WRITE A SCRIPT FOR PLACING CAPITALS
1646 ****************************************************************************/
1647 static int
1648 write_newcap_script(void)
1649 {
1650     int c;
1651     FILE *script = fopen(outfile, "w");
1652
1653     if (!script) {
1654         fprintf(stderr, "%s: unable to write to %s (%s)\n",
1655                 program_name, outfile, strerror(errno));
1656         return 0;
1657     }
1658
1659     for (c = 0; c < nc; ++c) {
1660         fprintf(script, "add %d %d %d p\n", c + 1, c + 1, c + 1);
1661         fprintf(script, "newcap %d %d,%d\n", c + 1, capx[c], capy[c]);
1662     }
1663     fprintf(script, "add %d visitor visitor v\n", c + 1);
1664     fclose(script);
1665     return 1;
1666 }
1667
1668 static void
1669 qprint(const char *const fmt, ...)
1670 {
1671     va_list ap;
1672
1673     if (!quiet) {
1674         va_start(ap, fmt);
1675         vfprintf(stdout, fmt, ap);
1676         va_end(ap);
1677     }
1678 }