157 static const int height_decimal_bits = 4;
161 static const int amplitude_decimal_bits = 10;
182 return h[x + y * dim_x];
190 #define I2H(i) ((i) << height_decimal_bits)
192 #define H2I(i) ((i) >> height_decimal_bits)
195 #define I2A(i) ((i) << amplitude_decimal_bits)
197 #define A2I(i) ((i) >> amplitude_decimal_bits)
200 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
204 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
232 { 3, 3, 3, 3, 4, 5, 7 },
233 { 5, 7, 8, 9, 14, 19, 31 },
234 { 8, 9, 10, 15, 23, 37, 61 },
235 { 10, 11, 17, 19, 49, 63, 73 },
236 { 12, 19, 25, 31, 67, 75, 87 },
253 static const amplitude_t amplitudes_small[][10] = {
255 {60000, 2273, 4142, 2253, 421, 213, 137, 177, 37, 16},
256 {50000, 2273, 4142, 2253, 421, 213, 137, 177, 37, 61},
257 {40000, 2273, 4142, 2253, 421, 213, 137, 177, 37, 91},
258 {30000, 2273, 4142, 2253, 421, 213, 137, 177, 37, 161},
263 static const amplitude_t amplitudes_middle[][10] = {
264 {55000, 2273, 5142, 253, 2421, 213, 137, 177, 37, 16},
265 {45000, 2273, 5142, 253, 2421, 213, 137, 177, 37, 61},
266 {35000, 2273, 5142, 253, 2421, 213, 137, 177, 37, 91},
267 {25000, 2273, 5142, 253, 2421, 213, 137, 177, 37, 161},
272 static const amplitude_t amplitudes_large[][10] = {
274 {55000, 2273, 5142, 253, 421, 2213, 137, 177, 37, 16},
275 {45000, 2273, 5142, 253, 421, 2213, 137, 177, 37, 61},
276 {35000, 2273, 5142, 253, 421, 2213, 137, 177, 37, 91},
277 {25000, 2273, 5142, 253, 421, 2213, 137, 177, 37, 161},
294 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
301 if (smallest_size < 9) {
303 amplitude = amplitudes_small[smoothness][
max(0, index)];
304 }
else if (smallest_size == 9) {
306 amplitude = amplitudes_middle[smoothness][
max(0, index)];
309 amplitude = amplitudes_large[smoothness][
max(0, index)];
311 if (index >= 0)
return amplitude;
314 double extrapolation_factor = extrapolation_factors[smoothness];
315 int height_range =
I2H(16);
317 amplitude = (
amplitude_t)(extrapolation_factor * (
double)amplitude);
333 return x >= 0 && x < _height_map.size_x && y >= 0 && y < _height_map.size_y;
349 _height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
350 _height_map.dim_x = _height_map.size_x + 1;
351 _height_map.h = CallocT<height_t>(_height_map.total_size);
363 _height_map.h = NULL;
387 assert(_height_map.h != NULL);
397 if (amplitude == 0)
continue;
399 const int step = 1 << (MAX_TGP_FREQUENCIES - frequency - 1);
403 for (
int y = 0; y <= _height_map.size_y; y += step) {
404 for (
int x = 0; x <= _height_map.size_x; x += step) {
406 _height_map.
height(x, y) = height;
415 for (
int y = 0; y <= _height_map.size_y; y += 2 * step) {
416 for (
int x = 0; x <= _height_map.size_x - 2 * step; x += 2 * step) {
420 _height_map.
height(x + 1 * step, y) = h01;
425 for (
int y = 0; y <= _height_map.size_y - 2 * step; y += 2 * step) {
426 for (
int x = 0; x <= _height_map.size_x; x += step) {
430 _height_map.
height(x, y + 1 * step) = h10;
435 for (
int y = 0; y <= _height_map.size_y; y += step) {
436 for (
int x = 0; x <= _height_map.size_x; x += step) {
448 h_min = h_max = _height_map.
height(0, 0);
452 if (*h < h_min) h_min = *h;
453 if (*h > h_max) h_max = *h;
458 h_avg = (
height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
461 if (min_ptr != NULL) *min_ptr = h_min;
462 if (max_ptr != NULL) *max_ptr = h_max;
463 if (avg_ptr != NULL) *avg_ptr = h_avg;
469 int *hist = hist_buf - h_min;
489 if (*h < h_min)
continue;
492 fheight = (double)(*h - h_min) / (double)(h_max - h_min);
498 fheight = 2 * fheight - 1;
500 fheight = sin(fheight * M_PI_2);
502 fheight = 0.5 * (fheight + 1);
509 double sine_upper_limit = 0.75;
510 double linear_compression = 2;
511 if (fheight >= sine_upper_limit) {
513 fheight = 1.0 - (1.0 - fheight) / linear_compression;
515 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
517 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
519 fheight = sin(fheight * M_PI_2);
521 fheight = 0.5 * (fheight + 1.0) * m;
530 double sine_lower_limit = 0.5;
531 double linear_compression = 2;
532 if (fheight <= sine_lower_limit) {
534 fheight = fheight / linear_compression;
536 double m = sine_lower_limit / linear_compression;
538 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
540 fheight = sin(fheight * M_PI_2);
542 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
552 *h = (
height_t)(fheight * (h_max - h_min) + h_min);
553 if (*h < 0) *h =
I2H(0);
554 if (*h >= h_max) *h = h_max - 1;
579 struct control_point_t {
584 #define F(fraction) ((height_t)(fraction * mh))
585 const control_point_t curve_map_1[] = { { F(0.0), F(0.0) }, { F(0.8), F(0.13) }, { F(1.0), F(0.4) } };
586 const control_point_t curve_map_2[] = { { F(0.0), F(0.0) }, { F(0.53), F(0.13) }, { F(0.8), F(0.27) }, { F(1.0), F(0.6) } };
587 const control_point_t curve_map_3[] = { { F(0.0), F(0.0) }, { F(0.53), F(0.27) }, { F(0.8), F(0.57) }, { F(1.0), F(0.8) } };
588 const control_point_t curve_map_4[] = { { F(0.0), F(0.0) }, { F(0.4), F(0.3) }, { F(0.7), F(0.8) }, { F(0.92), F(0.99) }, { F(1.0), F(0.99) } };
592 struct control_point_list_t {
594 const control_point_t *list;
596 const control_point_list_t curve_maps[] = {
597 {
lengthof(curve_map_1), curve_map_1 },
598 {
lengthof(curve_map_2), curve_map_2 },
599 {
lengthof(curve_map_3), curve_map_3 },
600 {
lengthof(curve_map_4), curve_map_4 },
607 float factor = sqrt((
float)_height_map.size_x / (
float)_height_map.size_y);
608 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
609 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
610 byte *c =
AllocaM(byte, sx * sy);
612 for (uint i = 0; i < sx * sy; i++) {
613 c[i] = Random() %
lengthof(curve_maps);
617 for (
int x = 0; x < _height_map.size_x; x++) {
620 float fx = (float)(sx * x) / _height_map.size_x + 1.0f;
623 float xr = 2.0f * (fx - x1) - 1.0f;
624 xr = sin(xr * M_PI_2);
625 xr = sin(xr * M_PI_2);
626 xr = 0.5f * (xr + 1.0f);
627 float xri = 1.0f - xr;
634 for (
int y = 0; y < _height_map.size_y; y++) {
637 float fy = (float)(sy * y) / _height_map.size_y + 1.0f;
640 float yr = 2.0f * (fy - y1) - 1.0f;
641 yr = sin(yr * M_PI_2);
642 yr = sin(yr * M_PI_2);
643 yr = 0.5f * (yr + 1.0f);
644 float yri = 1.0f - yr;
651 uint corner_a = c[x1 + sx * y1];
652 uint corner_b = c[x1 + sx * y2];
653 uint corner_c = c[x2 + sx * y1];
654 uint corner_d = c[x2 + sx * y2];
658 uint corner_bits = 0;
659 corner_bits |= 1 << corner_a;
660 corner_bits |= 1 << corner_b;
661 corner_bits |= 1 << corner_c;
662 corner_bits |= 1 << corner_d;
667 if (*h <
I2H(1))
continue;
673 for (uint t = 0; t <
lengthof(curve_maps); t++) {
674 if (!
HasBit(corner_bits, t))
continue;
677 const control_point_t *cm = curve_maps[t].list;
678 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
679 const control_point_t &p1 = cm[i];
680 const control_point_t &p2 = cm[i + 1];
682 if (*h >= p1.x && *h < p2.x) {
683 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
692 *h = (
height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
703 height_t h_min, h_max, h_avg, h_water_level;
704 int64 water_tiles, desired_water_tiles;
711 int *hist_buf = CallocT<int>(h_max - h_min + 1);
716 desired_water_tiles =
A2I(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y));
719 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
720 water_tiles += hist[h_water_level];
721 if (water_tiles >= desired_water_tiles)
break;
732 *h = (
height_t)(((
int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
734 if (*h < 0) *h =
I2H(0);
735 if (*h >= h_max_new) *h = h_max_new - 1;
741 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
766 const int margin = 4;
772 for (y = 0; y <= _height_map.size_y; y++) {
773 if (
HasBit(water_borders, BORDER_NE)) {
776 max_x =
max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
777 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
778 for (x = 0; x < max_x; x++) {
779 _height_map.
height(x, y) = 0;
783 if (
HasBit(water_borders, BORDER_SW)) {
786 max_x =
max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
787 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
788 for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
789 _height_map.
height(x, y) = 0;
795 for (x = 0; x <= _height_map.size_x; x++) {
796 if (
HasBit(water_borders, BORDER_NW)) {
799 max_y =
max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
800 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
801 for (y = 0; y < max_y; y++) {
802 _height_map.
height(x, y) = 0;
806 if (
HasBit(water_borders, BORDER_SE)) {
809 max_y =
max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
810 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
811 for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
812 _height_map.
height(x, y) = 0;
821 const int max_coast_dist_from_edge = 35;
822 const int max_coast_Smooth_depth = 35;
834 for (x = org_x, y = org_y, ed = 0;
IsValidXY(x, y) && ed < max_coast_dist_from_edge; x += dir_x, y += dir_y, ed++) {
836 if (_height_map.
height(x, y) >=
I2H(1))
break;
839 if (
IsValidXY(x + dir_y, y + dir_x) && _height_map.
height(x + dir_y, y + dir_x) > 0)
break;
842 if (
IsValidXY(x - dir_y, y - dir_x) && _height_map.
height(x - dir_y, y - dir_x) > 0)
break;
847 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
848 h = _height_map.
height(x, y);
849 h =
min(h, h_prev + (4 + depth));
850 _height_map.
height(x, y) = h;
860 for (x = 0; x < _height_map.size_x; x++) {
865 for (y = 0; y < _height_map.size_y; y++) {
880 for (
int y = 0; y <= (int)_height_map.size_y; y++) {
881 for (
int x = 0; x <= (int)_height_map.size_x; x++) {
882 height_t h_max =
min(_height_map.
height(x > 0 ? x - 1 : x, y), _height_map.
height(x, y > 0 ? y - 1 : y)) + dh_max;
883 if (_height_map.
height(x, y) > h_max) _height_map.
height(x, y) = h_max;
886 for (
int y = _height_map.size_y; y >= 0; y--) {
887 for (
int x = _height_map.size_x; x >= 0; x--) {
888 height_t h_max =
min(_height_map.
height(x < _height_map.size_x ? x + 1 : x, y), _height_map.
height(x, y < _height_map.size_y ? y + 1 : y)) + dh_max;
889 if (_height_map.
height(x, y) > h_max) _height_map.
height(x, y) = h_max;
911 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
935 static double int_noise(
const long x,
const long y,
const int prime)
942 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
951 return a + x * (b - a);
961 const int integer_X = (int)x;
962 const int integer_Y = (int)y;
964 const double fractional_X = x - (double)integer_X;
965 const double fractional_Y = y - (double)integer_Y;
967 const double v1 =
int_noise(integer_X, integer_Y, prime);
968 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
969 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
970 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
989 for (
int i = 0; i < 6; i++) {
990 const double frequency = (double)(1 << i);
991 const double amplitude = pow(p, (
double)i);
993 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
1033 for (
int y = 0; y < _height_map.size_y - 1; y++)
MakeVoid(_height_map.size_x * y);
1034 for (
int x = 0; x < _height_map.size_x; x++)
MakeVoid(x);
1040 for (
int y = 0; y < _height_map.size_y; y++) {
1041 for (
int x = 0; x < _height_map.size_x; x++) {