]> git.pond.sub.org Git - empserver/blob - src/util/land.c
Indented with src/scripts/indent-emp.
[empserver] / src / util / land.c
1 /*
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
5  *
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.
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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  land.c: New version of empcre - Create the land masses in the game
29  * 
30  *  Known contributors to this file:
31  *     James Anderson, 1987
32  *     Dave Pare, 1989
33  */
34
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include "var.h"
38 #include "misc.h"
39 #include "power.h"
40 #include "nat.h"
41 #include "sect.h"
42 #include "gamesdef.h"
43 #include "file.h"
44 #include "xy.h"
45 #include "prototypes.h"
46
47 #define rnd(x) (random() % (x))
48
49 #define XPLATES         ((WORLD_X) / 2) /* basically world x-y size */
50 #define YPLATES         (WORLD_Y)
51 #define XSIZE           (XPLATES)
52 #define YSIZE           (YPLATES)
53
54 #define BIGV            256     /* used in making altitude */
55 #define SMALLV          128     /* ex-ocean: rnd(SMALLV) - rnd(BIGV) */
56
57 typedef enum plates {
58     OCEAN, ISLAND, CONTINENT
59 } plate_e;
60
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 */
65
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)
70
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 */
77
78 struct sctstr sects[YSIZE][XSIZE];
79
80 plate_e plates[YPLATES][XPLATES];
81 int world[YSIZE][XSIZE];
82
83 int sectrange = SECTRANGE;
84 int mincontdist = MINCONTDIST;
85 int chunksize = CHUNKSIZE;
86 int numchunks = NUMCHUNKS;
87 int numisle = NUMISLE;
88
89 static void initworld();
90 static void growcont();
91 static void newcont();
92 static int verify();
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);
98
99
100 int
101 main(argc, argv)
102 int argc;
103 s_char **argv;
104 {
105     extern struct empfile empfile[];
106     register int n;
107     int x, y;
108     int i, j;
109     time_t now;
110     int fd;
111     int left;
112     int big;
113
114     if (argc > 1 && argc != 6) {
115         printf
116             ("usage: %s sectrange mincontdist chunksize numchunks numisle\n",
117              *argv);
118         return -1;
119     }
120     if (argc == 6) {
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]);
126     }
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);
133     if (fd < 0) {
134         perror(empfile[EF_SECTOR].file);
135         return -1;
136     }
137     time(&now);
138     srandom(now + getpid());
139     initworld((plate_e *)plates);
140     left = NUMLAND;
141     printf("Creating continents");
142     while (left > 0) {
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);
147         }
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) {
152                 printf(".");
153                 fflush(stdout);
154             }
155         }
156     }
157     printf("\n");
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;
163     }
164     printf("\n");
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));
171     if (n < 0) {
172         perror(empfile[EF_SECTOR].file);
173         return -1;
174     }
175     if (n != sizeof(sects)) {
176         printf("%s: partial write\n", empfile[EF_SECTOR].file);
177         return -1;
178     }
179     close(fd);
180     exit(0);
181 }
182
183 static void
184 initworld(plates)
185 register plate_e *plates;
186 {
187     register int i;
188
189     for (i = 0; i < XPLATES * YPLATES; i++)
190         *plates++ = OCEAN;
191 }
192
193 static void
194 growcont(plates, xp, yp)
195 register plate_e plates[YPLATES][XPLATES];
196 int *xp;
197 int *yp;
198 {
199     int x, y;
200
201     /* merge with another one */
202     while (plates[(y = rnd(YPLATES))][(x = rnd(XPLATES))] == OCEAN) ;
203     *xp = x;
204     *yp = y;
205 }
206
207 static void
208 newcont(plates, xp, yp, dist)
209 register plate_e plates[YPLATES][XPLATES];
210 int *xp;
211 int *yp;
212 int dist;
213 {
214     register int x, y;
215     int i;
216
217     for (i = 0; i < 30; i++) {
218         y = rnd(YPLATES);
219         x = rnd(XPLATES);
220         if (verify(plates, x, y, OCEAN, dist))
221             break;
222     }
223     if (i == 30) {
224         growcont(plates, xp, yp);
225     } else {
226         *xp = x;
227         *yp = y;
228     }
229 }
230
231 /*
232  * verify that with "dist", there are only type "what" sectors
233  * returns 0 if fail, 1 if success.
234  */
235 static int
236 verify(plates, x, y, what, dist)
237 register plate_e plates[YPLATES][XPLATES];
238 int x;
239 int y;
240 int what;
241 int dist;
242 {
243     register int xbase, ybase;
244     register int x1, y1;
245
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)
252                 return 0;
253         }
254     }
255     return 1;
256 }
257
258 static int
259 makeland(plates, x, y, dist, what)
260 register plate_e plates[YPLATES][XPLATES];
261 int x;
262 int y;
263 int dist;
264 int what;
265 {
266     register int xbase, ybase;
267     register int xfail, yfail;
268     register int x1, y1;
269     int created;
270
271     created = 0;
272     for (ybase = y - dist; ybase <= y + dist; ybase++) {
273         yfail = y - ybase;
274         if (yfail < 0)
275             yfail = -yfail;
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)
280                 continue;
281             xfail = x - x1;
282             if (xfail < 0)
283                 xfail = -xfail;
284             if (xfail < yfail)
285                 xfail = yfail;
286             if (xfail < dist - 1 || !rnd(xfail + 1) || !rnd(xfail + 1)) {
287                 plates[y1][x1] = what;
288                 created++;
289             }
290         }
291     }
292     return created;
293 }
294
295 static void
296 make_altitude(plates, world)
297 register plate_e plates[YPLATES][XPLATES];
298 register int world[YSIZE][XSIZE];
299 {
300     register int x, y;
301
302     for (y = 0; y < YPLATES; y++) {
303         for (x = 0; x < XPLATES; x++) {
304             switch (plates[y][x]) {
305             case OCEAN:
306                                 /*-BIGV, -SMALLV/2, SMALLV*/
307                 world[y][x] = rnd(SMALLV) - rnd(BIGV);
308                 break;
309             case ISLAND:
310                                 /*-BIGV, 0, BIGV*/
311                 world[y][x] = rnd(BIGV) - rnd(BIGV) + 2;
312                 break;
313             case CONTINENT:
314                                 /*-SMALLV, SMALLV/2, BIGV*/
315                 world[y][x] = rnd(BIGV) - rnd(SMALLV);
316             }
317         }
318     }
319 }
320
321 static int
322 total_land(world, xbase, ybase, range)
323 register int world[YSIZE][XSIZE];
324 register int xbase;
325 int ybase;
326 register int range;
327 {
328     register int x;
329     register int xmax;
330     register int total;
331     register int *row;
332     int y;
333     int ymax;
334
335     total = 0;
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];
342     }
343     return total;
344 }
345
346 static void
347 make_sects(world, sects)
348 register int world[YSIZE][XSIZE];
349 struct sctstr *sects;
350 {
351     register struct sctstr *sct;
352     register int i;
353     register int x, y;
354     int elev[12 + 12 + 3];      /* # sects from -12 to 12 in steps of 10 elev */
355     int range;
356     int rangesq;
357     int total;
358     int sum;
359
360     for (i = 0; i < 12 + 12 + 3; i++)
361         elev[i] = 0;
362     sum = 0;
363     sct = sects;
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;
378             else
379                 sct->sct_type = SCT_MOUNT;
380             sct->sct_elev = total;
381             sct->sct_newtype = sct->sct_type;
382             sum += total;
383             if (total < -129)
384                 elev[0]++;
385             else if (total > 129)
386                 elev[26]++;
387             else
388                 elev[13 + total / 10]++;
389         }
390     }
391     for (i = 0; i < 12 + 12 + 3; i++)
392         if (elev[i] != 0)
393             printf("%4d sectors elevation %4d to %4d\n",
394                    elev[i], 10 * i - 140, 10 * i - 130);
395 }
396
397 static void
398 land_sct_init(coord x, coord y, s_char *ptr)
399 {
400     struct sctstr *sp = (struct sctstr *)ptr;
401
402     sp->ef_type = EF_SECTOR;
403     sp->sct_x = x;
404     sp->sct_y = y;
405     sp->sct_dist_x = x;
406     sp->sct_dist_y = y;
407 }