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 },
254 {16000, 5600, 1968, 688, 240, 16, 16},
255 {24000, 12800, 6400, 2700, 1024, 128, 16},
256 {32000, 19200, 12800, 8000, 3200, 256, 64},
257 {48000, 24000, 19200, 16000, 8000, 512, 320},
269 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
276 if (index >= 0)
return amplitude;
279 double extrapolation_factor = extrapolation_factors[smoothness];
280 int height_range =
I2H(16);
282 amplitude = (
amplitude_t)(extrapolation_factor * (
double)amplitude);
298 return x >= 0 && x < _height_map.size_x && y >= 0 && y < _height_map.size_y;
314 _height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
315 _height_map.dim_x = _height_map.size_x + 1;
316 _height_map.h = CallocT<height_t>(_height_map.total_size);
328 _height_map.h = NULL;
352 assert(_height_map.h != NULL);
362 if (amplitude == 0)
continue;
364 const int step = 1 << (MAX_TGP_FREQUENCIES - frequency - 1);
368 for (
int y = 0; y <= _height_map.size_y; y += step) {
369 for (
int x = 0; x <= _height_map.size_x; x += step) {
371 _height_map.
height(x, y) = height;
380 for (
int y = 0; y <= _height_map.size_y; y += 2 * step) {
381 for (
int x = 0; x <= _height_map.size_x - 2 * step; x += 2 * step) {
385 _height_map.
height(x + 1 * step, y) = h01;
390 for (
int y = 0; y <= _height_map.size_y - 2 * step; y += 2 * step) {
391 for (
int x = 0; x <= _height_map.size_x; x += step) {
395 _height_map.
height(x, y + 1 * step) = h10;
400 for (
int y = 0; y <= _height_map.size_y; y += step) {
401 for (
int x = 0; x <= _height_map.size_x; x += step) {
413 h_min = h_max = _height_map.
height(0, 0);
417 if (*h < h_min) h_min = *h;
418 if (*h > h_max) h_max = *h;
423 h_avg = (
height_t)(h_accu / (_height_map.size_x * _height_map.size_y));
426 if (min_ptr != NULL) *min_ptr = h_min;
427 if (max_ptr != NULL) *max_ptr = h_max;
428 if (avg_ptr != NULL) *avg_ptr = h_avg;
434 int *hist = hist_buf - h_min;
454 if (*h < h_min)
continue;
457 fheight = (double)(*h - h_min) / (double)(h_max - h_min);
463 fheight = 2 * fheight - 1;
465 fheight = sin(fheight * M_PI_2);
467 fheight = 0.5 * (fheight + 1);
474 double sine_upper_limit = 0.75;
475 double linear_compression = 2;
476 if (fheight >= sine_upper_limit) {
478 fheight = 1.0 - (1.0 - fheight) / linear_compression;
480 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
482 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
484 fheight = sin(fheight * M_PI_2);
486 fheight = 0.5 * (fheight + 1.0) * m;
495 double sine_lower_limit = 0.5;
496 double linear_compression = 2;
497 if (fheight <= sine_lower_limit) {
499 fheight = fheight / linear_compression;
501 double m = sine_lower_limit / linear_compression;
503 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
505 fheight = sin(fheight * M_PI_2);
507 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
517 *h = (
height_t)(fheight * (h_max - h_min) + h_min);
518 if (*h < 0) *h =
I2H(0);
519 if (*h >= h_max) *h = h_max - 1;
544 struct control_point_t {
549 #define F(fraction) ((height_t)(fraction * mh))
550 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) } };
551 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) } };
552 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) } };
553 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) } };
557 struct control_point_list_t {
559 const control_point_t *list;
561 const control_point_list_t curve_maps[] = {
562 {
lengthof(curve_map_1), curve_map_1 },
563 {
lengthof(curve_map_2), curve_map_2 },
564 {
lengthof(curve_map_3), curve_map_3 },
565 {
lengthof(curve_map_4), curve_map_4 },
572 float factor = sqrt((
float)_height_map.size_x / (
float)_height_map.size_y);
573 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
574 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
575 byte *c =
AllocaM(byte, sx * sy);
577 for (uint i = 0; i < sx * sy; i++) {
578 c[i] = Random() %
lengthof(curve_maps);
582 for (
int x = 0; x < _height_map.size_x; x++) {
585 float fx = (float)(sx * x) / _height_map.size_x + 1.0f;
588 float xr = 2.0f * (fx - x1) - 1.0f;
589 xr = sin(xr * M_PI_2);
590 xr = sin(xr * M_PI_2);
591 xr = 0.5f * (xr + 1.0f);
592 float xri = 1.0f - xr;
599 for (
int y = 0; y < _height_map.size_y; y++) {
602 float fy = (float)(sy * y) / _height_map.size_y + 1.0f;
605 float yr = 2.0f * (fy - y1) - 1.0f;
606 yr = sin(yr * M_PI_2);
607 yr = sin(yr * M_PI_2);
608 yr = 0.5f * (yr + 1.0f);
609 float yri = 1.0f - yr;
616 uint corner_a = c[x1 + sx * y1];
617 uint corner_b = c[x1 + sx * y2];
618 uint corner_c = c[x2 + sx * y1];
619 uint corner_d = c[x2 + sx * y2];
623 uint corner_bits = 0;
624 corner_bits |= 1 << corner_a;
625 corner_bits |= 1 << corner_b;
626 corner_bits |= 1 << corner_c;
627 corner_bits |= 1 << corner_d;
632 if (*h <
I2H(1))
continue;
638 for (uint t = 0; t <
lengthof(curve_maps); t++) {
639 if (!
HasBit(corner_bits, t))
continue;
642 const control_point_t *cm = curve_maps[t].list;
643 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
644 const control_point_t &p1 = cm[i];
645 const control_point_t &p2 = cm[i + 1];
647 if (*h >= p1.x && *h < p2.x) {
648 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
657 *h = (
height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
668 height_t h_min, h_max, h_avg, h_water_level;
669 int64 water_tiles, desired_water_tiles;
676 int *hist_buf = CallocT<int>(h_max - h_min + 1);
681 desired_water_tiles =
A2I(((int64)water_percent) * (int64)(_height_map.size_x * _height_map.size_y));
684 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
685 water_tiles += hist[h_water_level];
686 if (water_tiles >= desired_water_tiles)
break;
697 *h = (
height_t)(((
int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
699 if (*h < 0) *h =
I2H(0);
700 if (*h >= h_max_new) *h = h_max_new - 1;
706 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
731 const int margin = 4;
737 for (y = 0; y <= _height_map.size_y; y++) {
738 if (
HasBit(water_borders, BORDER_NE)) {
741 max_x =
max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
742 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
743 for (x = 0; x < max_x; x++) {
744 _height_map.
height(x, y) = 0;
748 if (
HasBit(water_borders, BORDER_SW)) {
751 max_x =
max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
752 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
753 for (x = _height_map.size_x; x > (_height_map.size_x - 1 - max_x); x--) {
754 _height_map.
height(x, y) = 0;
760 for (x = 0; x <= _height_map.size_x; x++) {
761 if (
HasBit(water_borders, BORDER_NW)) {
764 max_y =
max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
765 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
766 for (y = 0; y < max_y; y++) {
767 _height_map.
height(x, y) = 0;
771 if (
HasBit(water_borders, BORDER_SE)) {
774 max_y =
max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
775 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
776 for (y = _height_map.size_y; y > (_height_map.size_y - 1 - max_y); y--) {
777 _height_map.
height(x, y) = 0;
786 const int max_coast_dist_from_edge = 35;
787 const int max_coast_Smooth_depth = 35;
799 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++) {
801 if (_height_map.
height(x, y) >=
I2H(1))
break;
804 if (
IsValidXY(x + dir_y, y + dir_x) && _height_map.
height(x + dir_y, y + dir_x) > 0)
break;
807 if (
IsValidXY(x - dir_y, y - dir_x) && _height_map.
height(x - dir_y, y - dir_x) > 0)
break;
812 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
813 h = _height_map.
height(x, y);
814 h =
min(h, h_prev + (4 + depth));
815 _height_map.
height(x, y) = h;
825 for (x = 0; x < _height_map.size_x; x++) {
830 for (y = 0; y < _height_map.size_y; y++) {
845 for (
int y = 0; y <= (int)_height_map.size_y; y++) {
846 for (
int x = 0; x <= (int)_height_map.size_x; x++) {
847 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;
848 if (_height_map.
height(x, y) > h_max) _height_map.
height(x, y) = h_max;
851 for (
int y = _height_map.size_y; y >= 0; y--) {
852 for (
int x = _height_map.size_x; x >= 0; x--) {
853 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;
854 if (_height_map.
height(x, y) > h_max) _height_map.
height(x, y) = h_max;
876 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
900 static double int_noise(
const long x,
const long y,
const int prime)
907 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
916 return a + x * (b - a);
926 const int integer_X = (int)x;
927 const int integer_Y = (int)y;
929 const double fractional_X = x - (double)integer_X;
930 const double fractional_Y = y - (double)integer_Y;
932 const double v1 =
int_noise(integer_X, integer_Y, prime);
933 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
934 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
935 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
954 for (
int i = 0; i < 6; i++) {
955 const double frequency = (double)(1 << i);
956 const double amplitude = pow(p, (
double)i);
958 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;
998 for (
int y = 0; y < _height_map.size_y - 1; y++)
MakeVoid(_height_map.size_x * y);
999 for (
int x = 0; x < _height_map.size_x; x++)
MakeVoid(x);
1005 for (
int y = 0; y < _height_map.size_y; y++) {
1006 for (
int x = 0; x < _height_map.size_x; x++) {