155 static const int height_decimal_bits = 4;
159 static const int amplitude_decimal_bits = 10;
180 return h[x + y * dim_x];
188 #define I2H(i) ((i) << height_decimal_bits)
190 #define H2I(i) ((i) >> height_decimal_bits)
193 #define I2A(i) ((i) << amplitude_decimal_bits)
195 #define A2I(i) ((i) >> amplitude_decimal_bits)
198 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
202 #define FOR_ALL_TILES_IN_HEIGHT(h) for (h = _height_map.h; h < &_height_map.h[_height_map.total_size]; h++)
230 { 3, 3, 3, 3, 4, 5, 7 },
231 { 5, 7, 8, 9, 14, 19, 31 },
232 { 8, 9, 10, 15, 23, 37, 61 },
233 { 10, 11, 17, 19, 49, 63, 73 },
234 { 12, 19, 25, 31, 67, 75, 87 },
272 {16000, 5600, 1968, 688, 240, 16, 16},
273 {24000, 12800, 6400, 2700, 1024, 128, 16},
274 {32000, 19200, 12800, 8000, 3200, 256, 64},
275 {48000, 24000, 19200, 16000, 8000, 512, 320},
287 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
293 amplitude_t amplitude = amplitudes[smoothness][std::max(0, index)];
294 if (index >= 0)
return amplitude;
297 double extrapolation_factor = extrapolation_factors[smoothness];
298 int height_range =
I2H(16);
300 amplitude = (
amplitude_t)(extrapolation_factor * (
double)amplitude);
380 if (amplitude == 0)
continue;
386 for (
int y = 0; y <=
_height_map.size_y; y += step) {
387 for (
int x = 0; x <=
_height_map.size_x; x += step) {
398 for (
int y = 0; y <=
_height_map.size_y; y += 2 * step) {
399 for (
int x = 0; x <=
_height_map.size_x - 2 * step; x += 2 * step) {
408 for (
int y = 0; y <=
_height_map.size_y - 2 * step; y += 2 * step) {
409 for (
int x = 0; x <=
_height_map.size_x; x += step) {
418 for (
int y = 0; y <=
_height_map.size_y; y += step) {
419 for (
int x = 0; x <=
_height_map.size_x; x += step) {
435 if (*h < h_min) h_min = *h;
436 if (*h > h_max) h_max = *h;
444 if (min_ptr !=
nullptr) *min_ptr = h_min;
445 if (max_ptr !=
nullptr) *max_ptr = h_max;
446 if (avg_ptr !=
nullptr) *avg_ptr = h_avg;
452 int *hist = hist_buf - h_min;
472 if (*h < h_min)
continue;
475 fheight = (double)(*h - h_min) / (double)(h_max - h_min);
481 fheight = 2 * fheight - 1;
483 fheight = sin(fheight * M_PI_2);
485 fheight = 0.5 * (fheight + 1);
492 double sine_upper_limit = 0.75;
493 double linear_compression = 2;
494 if (fheight >= sine_upper_limit) {
496 fheight = 1.0 - (1.0 - fheight) / linear_compression;
498 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
500 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
502 fheight = sin(fheight * M_PI_2);
504 fheight = 0.5 * (fheight + 1.0) * m;
513 double sine_lower_limit = 0.5;
514 double linear_compression = 2;
515 if (fheight <= sine_lower_limit) {
517 fheight = fheight / linear_compression;
519 double m = sine_lower_limit / linear_compression;
521 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
523 fheight = sin(fheight * M_PI_2);
525 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
535 *h = (
height_t)(fheight * (h_max - h_min) + h_min);
536 if (*h < 0) *h =
I2H(0);
537 if (*h >= h_max) *h = h_max - 1;
562 struct control_point_t {
567 #define F(fraction) ((height_t)(fraction * mh))
568 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) } };
569 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) } };
570 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) } };
571 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) } };
575 struct control_point_list_t {
577 const control_point_t *list;
579 const control_point_list_t curve_maps[] = {
580 {
lengthof(curve_map_1), curve_map_1 },
581 {
lengthof(curve_map_2), curve_map_2 },
582 {
lengthof(curve_map_3), curve_map_3 },
583 {
lengthof(curve_map_4), curve_map_4 },
591 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
592 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
593 byte *c =
AllocaM(
byte, sx * sy);
595 for (uint i = 0; i < sx * sy; i++) {
596 c[i] = Random() %
lengthof(curve_maps);
603 float fx = (float)(sx * x) /
_height_map.size_x + 1.0f;
606 float xr = 2.0f * (fx - x1) - 1.0f;
607 xr = sin(xr * M_PI_2);
608 xr = sin(xr * M_PI_2);
609 xr = 0.5f * (xr + 1.0f);
610 float xri = 1.0f - xr;
620 float fy = (float)(sy * y) /
_height_map.size_y + 1.0f;
623 float yr = 2.0f * (fy - y1) - 1.0f;
624 yr = sin(yr * M_PI_2);
625 yr = sin(yr * M_PI_2);
626 yr = 0.5f * (yr + 1.0f);
627 float yri = 1.0f - yr;
634 uint corner_a = c[x1 + sx * y1];
635 uint corner_b = c[x1 + sx * y2];
636 uint corner_c = c[x2 + sx * y1];
637 uint corner_d = c[x2 + sx * y2];
641 uint corner_bits = 0;
642 corner_bits |= 1 << corner_a;
643 corner_bits |= 1 << corner_b;
644 corner_bits |= 1 << corner_c;
645 corner_bits |= 1 << corner_d;
650 if (*h <
I2H(1))
continue;
656 for (uint t = 0; t <
lengthof(curve_maps); t++) {
657 if (!
HasBit(corner_bits, t))
continue;
660 const control_point_t *cm = curve_maps[t].list;
661 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
662 const control_point_t &p1 = cm[i];
663 const control_point_t &p2 = cm[i + 1];
665 if (*h >= p1.x && *h < p2.x) {
666 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
675 *h = (
height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
686 height_t h_min, h_max, h_avg, h_water_level;
687 int64 water_tiles, desired_water_tiles;
694 int *hist_buf = CallocT<int>(h_max - h_min + 1);
702 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
703 water_tiles += hist[h_water_level];
704 if (water_tiles >= desired_water_tiles)
break;
715 *h = (
height_t)(((
int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
717 if (*h < 0) *h =
I2H(0);
718 if (*h >= h_max_new) *h = h_max_new - 1;
724 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
749 const int margin = 4;
756 if (
HasBit(water_borders, BORDER_NE)) {
759 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
760 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
761 for (x = 0; x < max_x; x++) {
766 if (
HasBit(water_borders, BORDER_SW)) {
769 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
770 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
779 if (
HasBit(water_borders, BORDER_NW)) {
782 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
783 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
784 for (y = 0; y < max_y; y++) {
789 if (
HasBit(water_borders, BORDER_SE)) {
792 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
793 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
804 const int max_coast_dist_from_edge = 35;
805 const int max_coast_Smooth_depth = 35;
817 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++) {
830 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
832 h = std::min<uint>(h, h_prev + (4 + depth));
863 for (
int y = 0; y <= (int)
_height_map.size_y; y++) {
864 for (
int x = 0; x <= (int)
_height_map.size_x; x++) {
894 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
918 static double int_noise(
const long x,
const long y,
const int prime)
925 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
934 return a + x * (b - a);
944 const int integer_X = (int)x;
945 const int integer_Y = (int)y;
947 const double fractional_X = x - (double)integer_X;
948 const double fractional_Y = y - (double)integer_Y;
950 const double v1 =
int_noise(integer_X, integer_Y, prime);
951 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
952 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
953 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
972 for (
int i = 0; i < 6; i++) {
973 const double frequency = (double)(1 << i);
974 const double amplitude = pow(p, (
double)i);
976 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;