2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * land.c: New version of empcre - Create the land masses in the game
30 * Known contributors to this file:
31 * James Anderson, 1987
45 #include "prototypes.h"
47 #define rnd(x) (random() % (x))
49 #define XPLATES ((WORLD_X) / 2) /* basically world x-y size */
50 #define YPLATES (WORLD_Y)
51 #define XSIZE (XPLATES)
52 #define YSIZE (YPLATES)
54 #define BIGV 256 /* used in making altitude */
55 #define SMALLV 128 /* ex-ocean: rnd(SMALLV) - rnd(BIGV) */
58 OCEAN, ISLAND, CONTINENT
61 #define LANDMIN 1 /* plate altitude for normal land */
62 #define HILLMIN 34 /* plate altitude for hills */
63 #define PLATMIN 36 /* plate altitude for plateau */
64 #define HIGHMIN 98 /* plate altitude for mountains */
66 #define LANDCH 60 /* land plate percentage */
67 #define NUMLAND (YPLATES * XPLATES * LANDCH)/100
68 #define NUMISLE NUMLAND/5 /* 1 isle for 5 land */
69 #define NUMWATER (XPLATES * YPLATES) - (NUMLAND + NUMISLE)
71 #define SECTRANGE 3 /* smoothing area */
72 #define MINCONTDIST 2 /* minimum continent distance */
73 #define CHUNKSIZE 2 /* basic land block size */
74 #define NEWCONTDIST (rnd(mincontdist) + mincontdist)
75 /* dist away from others for newcont */
76 #define NUMCHUNKS 70 /* number of CHUNKS per cont */
78 struct sctstr sects[YSIZE][XSIZE];
80 plate_e plates[YPLATES][XPLATES];
81 int world[YSIZE][XSIZE];
83 int sectrange = SECTRANGE;
84 int mincontdist = MINCONTDIST;
85 int chunksize = CHUNKSIZE;
86 int numchunks = NUMCHUNKS;
87 int numisle = NUMISLE;
89 static void initworld();
90 static void growcont();
91 static void newcont();
93 static int makeland();
94 static void make_altitude();
95 static int total_land();
96 static void make_sects();
97 static void land_sct_init(coord x, coord y, s_char *ptr);
105 extern struct empfile empfile[];
114 if (argc > 1 && argc != 6) {
116 ("usage: %s sectrange mincontdist chunksize numchunks numisle\n",
121 sectrange = atoi(argv[1]);
122 mincontdist = atoi(argv[2]);
123 chunksize = atoi(argv[3]);
124 numchunks = atoi(argv[4]);
125 numisle = atoi(argv[5]);
127 printf("sectrange: %d\n", sectrange);
128 printf("mincontdist: %d\n", mincontdist);
129 printf("chunksize: %d\n", chunksize);
130 printf("numchunks: %d\n", numchunks);
131 printf("numisle: %d\n", numisle);
132 fd = open(empfile[EF_SECTOR].file, O_RDWR | O_CREAT | O_TRUNC, 0660);
134 perror(empfile[EF_SECTOR].file);
138 srandom(now + getpid());
139 initworld((plate_e *)plates);
141 printf("Creating continents");
143 big = (left / (numchunks * 3 * chunksize * chunksize)) + 1;
144 for (n = 0; n < big; n++) {
145 newcont(plates, &x, &y, NEWCONTDIST);
146 left -= makeland(plates, x, y, chunksize, CONTINENT);
148 for (n = 0; n < big * numchunks; n++) {
149 growcont(plates, &x, &y);
150 left -= makeland(plates, x, y, chunksize, CONTINENT);
151 if ((n % numchunks) == 0) {
158 printf("Creating islands");
159 for (n = 0; n < numisle; n++) {
160 /* find an open spot */
161 while (plates[(i = rnd(YPLATES))][(j = rnd(XPLATES))] != OCEAN) ;
162 plates[i][j] = ISLAND;
165 printf("Making altitude\n");
166 make_altitude(plates, world);
167 printf("Creating sectors\n");
168 make_sects(world, sects);
169 printf("Writing sectors\n");
170 n = write(fd, sects, sizeof(sects));
172 perror(empfile[EF_SECTOR].file);
175 if (n != sizeof(sects)) {
176 printf("%s: partial write\n", empfile[EF_SECTOR].file);
185 register plate_e *plates;
189 for (i = 0; i < XPLATES * YPLATES; i++)
194 growcont(plates, xp, yp)
195 register plate_e plates[YPLATES][XPLATES];
201 /* merge with another one */
202 while (plates[(y = rnd(YPLATES))][(x = rnd(XPLATES))] == OCEAN) ;
208 newcont(plates, xp, yp, dist)
209 register plate_e plates[YPLATES][XPLATES];
217 for (i = 0; i < 30; i++) {
220 if (verify(plates, x, y, OCEAN, dist))
224 growcont(plates, xp, yp);
232 * verify that with "dist", there are only type "what" sectors
233 * returns 0 if fail, 1 if success.
236 verify(plates, x, y, what, dist)
237 register plate_e plates[YPLATES][XPLATES];
243 register int xbase, ybase;
246 for (ybase = y - dist; ybase <= y + dist; ybase++) {
247 for (xbase = x - dist; xbase <= x + dist; xbase++) {
248 /* normalize to world coords */
249 y1 = ybase < 0 ? ybase + YPLATES : ybase % YPLATES;
250 x1 = xbase < 0 ? xbase + XPLATES : xbase % XPLATES;
251 if (plates[y1][x1] != what)
259 makeland(plates, x, y, dist, what)
260 register plate_e plates[YPLATES][XPLATES];
266 register int xbase, ybase;
267 register int xfail, yfail;
272 for (ybase = y - dist; ybase <= y + dist; ybase++) {
276 y1 = ybase < 0 ? ybase + YPLATES : ybase % YPLATES;
277 for (xbase = x - dist; xbase <= x + dist; xbase++) {
278 x1 = xbase < 0 ? xbase + XPLATES : xbase % XPLATES;
279 if (plates[y1][x1] != OCEAN)
286 if (xfail < dist - 1 || !rnd(xfail + 1) || !rnd(xfail + 1)) {
287 plates[y1][x1] = what;
296 make_altitude(plates, world)
297 register plate_e plates[YPLATES][XPLATES];
298 register int world[YSIZE][XSIZE];
302 for (y = 0; y < YPLATES; y++) {
303 for (x = 0; x < XPLATES; x++) {
304 switch (plates[y][x]) {
306 /*-BIGV, -SMALLV/2, SMALLV*/
307 world[y][x] = rnd(SMALLV) - rnd(BIGV);
311 world[y][x] = rnd(BIGV) - rnd(BIGV) + 2;
314 /*-SMALLV, SMALLV/2, BIGV*/
315 world[y][x] = rnd(BIGV) - rnd(SMALLV);
322 total_land(world, xbase, ybase, range)
323 register int world[YSIZE][XSIZE];
336 xmax = xbase + range;
337 ymax = ybase + range;
338 for (y = ybase; y < ymax; y++) {
339 row = world[y % YSIZE];
340 for (x = xbase; x < xmax; x++)
341 total += row[x % XSIZE];
347 make_sects(world, sects)
348 register int world[YSIZE][XSIZE];
349 struct sctstr *sects;
351 register struct sctstr *sct;
354 int elev[12 + 12 + 3]; /* # sects from -12 to 12 in steps of 10 elev */
360 for (i = 0; i < 12 + 12 + 3; i++)
364 for (y = 0; y < YSIZE; y++) {
365 for (x = 0; x < XSIZE; x++, sct++) {
366 land_sct_init(x * 2 + (y & 01), y, (s_char *)sct);
367 range = 3 + rnd(sectrange);
368 rangesq = range * range;
369 total = total_land(world, x, y, range) / rangesq;
370 if (total < LANDMIN) {
371 sct->sct_type = SCT_WATER;
372 } else if (total < HILLMIN)
373 sct->sct_type = SCT_RURAL;
374 else if (total < PLATMIN)
375 sct->sct_type = SCT_MOUNT;
376 else if (total < HIGHMIN)
377 sct->sct_type = SCT_RURAL;
379 sct->sct_type = SCT_MOUNT;
380 sct->sct_elev = total;
381 sct->sct_newtype = sct->sct_type;
385 else if (total > 129)
388 elev[13 + total / 10]++;
391 for (i = 0; i < 12 + 12 + 3; i++)
393 printf("%4d sectors elevation %4d to %4d\n",
394 elev[i], 10 * i - 140, 10 * i - 130);
398 land_sct_init(coord x, coord y, s_char *ptr)
400 struct sctstr *sp = (struct sctstr *)ptr;
402 sp->ef_type = EF_SECTOR;