File: height_map.c

package info (click to toggle)
freeciv 3.2.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 286,492 kB
  • sloc: ansic: 484,452; cpp: 37,766; sh: 10,374; makefile: 7,425; python: 2,938; xml: 652; sed: 11
file content (293 lines) | stat: -rw-r--r-- 10,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***********************************************************************
 Freeciv - Copyright (C) 1996-2007 - The Freeciv Project
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "rand.h"

/* common */
#include "map.h"

/* server/generator */
#include "mapgen_topology.h"
#include "mapgen_utils.h"

#include "height_map.h"

int *height_map = NULL;
int hmap_shore_level = 0, hmap_mountain_level = 0;

/**********************************************************************//**
  Factor by which to lower height map near poles in normalize_hmap_poles
**************************************************************************/
static float hmap_pole_factor(struct tile *ptile)
{
  float factor = 1.0;

  if (near_singularity(ptile)) {
    /* Map edge near pole: clamp to what linear ramp would give us at pole
     * (maybe greater than 0) */
    factor = (100 - wld.map.server.flatpoles) / 100.0;
  } else if (wld.map.server.flatpoles > 0) {
    /* Linear ramp down from 100% at 2.5*ICE_BASE_LEVEL to (100-flatpoles) %
     * at the poles */
    factor = 1 - ((1 - (map_colatitude(ptile) / (2.5 * ICE_BASE_LEVEL)))
                  * wld.map.server.flatpoles / 100);
  }
  if (wld.map.server.separatepoles
      && map_colatitude(ptile) >= 2 * ICE_BASE_LEVEL) {
    /* A band of low height to try to separate the pole (this function is
     * only assumed to be called <= 2.5*ICE_BASE_LEVEL) */
    factor = MIN(factor, 0.1);
  }
  return factor;
}

/**********************************************************************//**
  Lower the land near the map edges and (optionally) the polar region to
  avoid too much land there.

  See also renormalize_hmap_poles
**************************************************************************/
void normalize_hmap_poles(void)
{
  whole_map_iterate(&(wld.map), ptile) {
    if (map_colatitude(ptile) <= 2.5 * ICE_BASE_LEVEL) {
      hmap(ptile) *= hmap_pole_factor(ptile);
    } else if (near_singularity(ptile)) {
      /* Near map edge but not near pole. */
      hmap(ptile) = 0;
    }
  } whole_map_iterate_end;
}

/**********************************************************************//**
  Invert (most of) the effects of normalize_hmap_poles so that we have
  accurate heights for texturing the poles.
**************************************************************************/
void renormalize_hmap_poles(void)
{
  whole_map_iterate(&(wld.map), ptile) {
    if (hmap(ptile) == 0) {
      /* Nothing left to restore. */
    } else if (map_colatitude(ptile) <= 2.5 * ICE_BASE_LEVEL) {
      float factor = hmap_pole_factor(ptile);

      if (factor > 0) {
        /* Invert the previously applied function */
        hmap(ptile) /= factor;
      }
    }
  } whole_map_iterate_end;
}

/**********************************************************************//**
  Create uncorrelated rand map and do some call to smoth to correlate
  it a little and create random shapes
**************************************************************************/
void make_random_hmap(int smooth)
{
  int i = 0;
  height_map = fc_malloc(sizeof(*height_map) * MAP_INDEX_SIZE);

  INITIALIZE_ARRAY(height_map, MAP_INDEX_SIZE, fc_rand(1000 * smooth));

  for (; i < smooth; i++) {
    smooth_int_map(height_map, TRUE);
  }

  adjust_int_map(height_map, 0, hmap_max_level);
}

/**********************************************************************//**
  Recursive function which does the work for generator 5.

  All (x0,y0) and (x1,y1) are in native coordinates.
**************************************************************************/
static void gen5rec(int step, int xl, int yt, int xr, int yb)
{
  int val[2][2];
  int x1wrap = xr; /* to wrap correctly */ 
  int y1wrap = yb; 

  /* All x and y values are native. */

  if (((yb - yt <= 0) || (xr - xl <= 0)) 
      || ((yb - yt == 1) && (xr - xl == 1))) {
    return;
  }

  if (xr == wld.map.xsize) {
    x1wrap = 0;
  }
  if (yb == wld.map.ysize) {
    y1wrap = 0;
  }

  val[0][0] = hmap(native_pos_to_tile(&(wld.map), xl, yt));
  val[0][1] = hmap(native_pos_to_tile(&(wld.map), xl, y1wrap));
  val[1][0] = hmap(native_pos_to_tile(&(wld.map), x1wrap, yt));
  val[1][1] = hmap(native_pos_to_tile(&(wld.map), x1wrap, y1wrap));

  /* set midpoints of sides to avg of side's vertices plus a random factor */
  /* unset points are zero, don't reset if set */
#define set_midpoints(X, Y, V)						\
  {									\
    struct tile *ptile = native_pos_to_tile(&(wld.map), (X), (Y));	\
    if (map_colatitude(ptile) <= ICE_BASE_LEVEL / 2) {			\
      /* possibly flatten poles, or possibly not (even at map edge) */	\
      hmap(ptile) = (V) * (100 - wld.map.server.flatpoles) / 100;	\
    } else if (near_singularity(ptile)					\
               || hmap(ptile) != 0) {					\
      /* do nothing */							\
    } else {								\
      hmap(ptile) = (V);						\
    }									\
  }

  set_midpoints((xl + xr) / 2, yt,
                (val[0][0] + val[1][0]) / 2 + (int)fc_rand(step) - step / 2);
  set_midpoints((xl + xr) / 2,  y1wrap,
                (val[0][1] + val[1][1]) / 2 + (int)fc_rand(step) - step / 2);
  set_midpoints(xl, (yt + yb)/2,
                (val[0][0] + val[0][1]) / 2 + (int)fc_rand(step) - step / 2);
  set_midpoints(x1wrap,  (yt + yb) / 2,
                (val[1][0] + val[1][1]) / 2 + (int)fc_rand(step) - step / 2);

  /* set middle to average of midpoints plus a random factor, if not set */
  set_midpoints((xl + xr) / 2, (yt + yb) / 2,
                ((val[0][0] + val[0][1] + val[1][0] + val[1][1]) / 4
                 + (int)fc_rand(step) - step / 2));

#undef set_midpoints

  /* now call recursively on the four subrectangles */
  gen5rec(2 * step / 3, xl, yt, (xr + xl) / 2, (yb + yt) / 2);
  gen5rec(2 * step / 3, xl, (yb + yt) / 2, (xr + xl) / 2, yb);
  gen5rec(2 * step / 3, (xr + xl) / 2, yt, xr, (yb + yt) / 2);
  gen5rec(2 * step / 3, (xr + xl) / 2, (yb + yt) / 2, xr, yb);
}

/**********************************************************************//**
  Generator 5 makes earthlike worlds with one or more large continents and
  a scattering of smaller islands. It does so by dividing the world into
  blocks and on each block raising or lowering the corners, then the
  midpoints and middle and so on recursively.  Fiddling with 'xdiv' and
  'ydiv' will change the size of the initial blocks and, if the map does not
  wrap in at least one direction, fiddling with 'avoidedge' will change the
  likelihood of continents butting up to non-wrapped edges.

  All X and Y values used in this function are in native coordinates.

  extra_div can be increased to break the world up into more, smaller
  islands.  This is used in conjunction with the startpos setting.
**************************************************************************/
void make_pseudofractal1_hmap(int extra_div)
{
  const bool xnowrap = !current_wrap_has_flag(WRAP_X);
  const bool ynowrap = !current_wrap_has_flag(WRAP_Y);

  /*
   * How many blocks should the x and y directions be divided into
   * initially.
   */
  const int xdiv = 5 + extra_div;
  const int ydiv = 5 + extra_div;

  int xdiv2 = xdiv + (xnowrap ? 1 : 0);
  int ydiv2 = ydiv + (ynowrap ? 1 : 0);

  int xmax = wld.map.xsize - (xnowrap ? 1 : 0);
  int ymax = wld.map.ysize - (ynowrap ? 1 : 0);
  int x_current, y_current;
  /* just need something > log(max(xsize, ysize)) for the recursion */
  int step = wld.map.xsize + wld.map.ysize;
  /* edges are avoided more strongly as this increases */
  int avoidedge = (100 - wld.map.server.landpercent) * step / 100 + step / 3;

  height_map = fc_malloc(sizeof(*height_map) * MAP_INDEX_SIZE);

  /* initialize map */
  INITIALIZE_ARRAY(height_map, MAP_INDEX_SIZE, 0);

  /* set initial points */
  for (x_current = 0; x_current < xdiv2; x_current++) {
    for (y_current = 0; y_current < ydiv2; y_current++) {
      do_in_map_pos(&(wld.map), ptile,
                    (x_current * xmax / xdiv), (y_current * ymax / ydiv)) {
        /* set initial points */
        hmap(ptile) = fc_rand(2 * step) - (2 * step) / 2;

	if (near_singularity(ptile)) {
	  /* avoid edges (topological singularities) */
	  hmap(ptile) -= avoidedge;
	}

	if (map_colatitude(ptile) <= ICE_BASE_LEVEL / 2 ) {
	  /* separate poles and avoid too much land at poles */
          hmap(ptile) -= fc_rand(avoidedge * wld.map.server.flatpoles / 100);
	}
      } do_in_map_pos_end;
    }
  }

  /* calculate recursively on each block */
  for (x_current = 0; x_current < xdiv; x_current++) {
    for (y_current = 0; y_current < ydiv; y_current++) {
      gen5rec(step, x_current * xmax / xdiv, y_current * ymax / ydiv, 
	      (x_current + 1) * xmax / xdiv, (y_current + 1) * ymax / ydiv);
    }
  }

  /* put in some random fuzz */
  whole_map_iterate(&(wld.map), ptile) {
    hmap(ptile) = 8 * hmap(ptile) + fc_rand(4) - 2;
  } whole_map_iterate_end;

  adjust_int_map(height_map, 0, hmap_max_level);
}

/**********************************************************************//**
  We don't want huge areas of grass/plains,
  so we put in a hill here and there, where it gets too 'clean'

  Return TRUE if the terrain around the given map position is "clean".  This
  means that all the terrain for 2 squares around it is not mountain or hill.
**************************************************************************/
bool area_is_too_flat(struct tile *ptile, int thill, int my_height)
{
  int higher_than_me = 0;

  square_iterate(&(wld.map), ptile, 2, tile1) {
    if (hmap(tile1) > thill) {
      return FALSE;
    }
    if (hmap(tile1) > my_height) {
      if (map_distance(ptile, tile1) == 1) {
        return FALSE;
      }
      if (++higher_than_me > 2) {
        return FALSE;
      }
    }
  } square_iterate_end;

  if ((thill - hmap_shore_level) * higher_than_me
      > (my_height - hmap_shore_level) * 4) {
    return FALSE;
  }

  return TRUE;
}