155 static const int height_decimal_bits = 4;
159 static const int amplitude_decimal_bits = 10;
164 std::vector<height_t> h;
179 return h[x + y * dim_x];
187 #define I2H(i) ((i) << height_decimal_bits)
189 #define H2I(i) ((i) >> height_decimal_bits)
192 #define I2A(i) ((i) << amplitude_decimal_bits)
194 #define A2I(i) ((i) >> amplitude_decimal_bits)
197 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
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 },
247 return I2H(max_height_from_table);
269 {16000, 5600, 1968, 688, 240, 16, 16},
270 {24000, 12800, 6400, 2700, 1024, 128, 16},
271 {32000, 19200, 12800, 8000, 3200, 256, 64},
272 {48000, 24000, 19200, 16000, 8000, 512, 320},
284 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
290 amplitude_t amplitude = amplitudes[smoothness][std::max(0, index)];
291 if (index >= 0)
return amplitude;
294 double extrapolation_factor = extrapolation_factors[smoothness];
295 int height_range =
I2H(16);
297 amplitude = (
amplitude_t)(extrapolation_factor * (
double)amplitude);
373 if (amplitude == 0)
continue;
379 for (
int y = 0; y <=
_height_map.size_y; y += step) {
380 for (
int x = 0; x <=
_height_map.size_x; x += step) {
391 for (
int y = 0; y <=
_height_map.size_y; y += 2 * step) {
392 for (
int x = 0; x <=
_height_map.size_x - 2 * step; x += 2 * step) {
401 for (
int y = 0; y <=
_height_map.size_y - 2 * step; y += 2 * step) {
402 for (
int x = 0; x <=
_height_map.size_x; x += step) {
411 for (
int y = 0; y <=
_height_map.size_y; y += step) {
412 for (
int x = 0; x <=
_height_map.size_x; x += step) {
428 if (h < h_min) h_min = h;
429 if (h > h_max) h_max = h;
437 if (min_ptr !=
nullptr) *min_ptr = h_min;
438 if (max_ptr !=
nullptr) *max_ptr = h_max;
439 if (avg_ptr !=
nullptr) *avg_ptr = h_avg;
445 int *hist = hist_buf - h_min;
462 if (h < h_min)
continue;
465 fheight = (double)(h - h_min) / (double)(h_max - h_min);
471 fheight = 2 * fheight - 1;
473 fheight = sin(fheight * M_PI_2);
475 fheight = 0.5 * (fheight + 1);
482 double sine_upper_limit = 0.75;
483 double linear_compression = 2;
484 if (fheight >= sine_upper_limit) {
486 fheight = 1.0 - (1.0 - fheight) / linear_compression;
488 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
490 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
492 fheight = sin(fheight * M_PI_2);
494 fheight = 0.5 * (fheight + 1.0) * m;
503 double sine_lower_limit = 0.5;
504 double linear_compression = 2;
505 if (fheight <= sine_lower_limit) {
507 fheight = fheight / linear_compression;
509 double m = sine_lower_limit / linear_compression;
511 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
513 fheight = sin(fheight * M_PI_2);
515 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
525 h = (
height_t)(fheight * (h_max - h_min) + h_min);
526 if (h < 0) h =
I2H(0);
527 if (h >= h_max) h = h_max - 1;
552 struct control_point_t {
557 #define F(fraction) ((height_t)(fraction * mh))
558 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) } };
559 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) } };
560 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) } };
561 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) } };
565 struct control_point_list_t {
567 const control_point_t *list;
569 const control_point_list_t curve_maps[] = {
570 {
lengthof(curve_map_1), curve_map_1 },
571 {
lengthof(curve_map_2), curve_map_2 },
572 {
lengthof(curve_map_3), curve_map_3 },
573 {
lengthof(curve_map_4), curve_map_4 },
581 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
582 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
583 byte *c =
AllocaM(
byte, sx * sy);
585 for (uint i = 0; i < sx * sy; i++) {
586 c[i] = Random() %
lengthof(curve_maps);
593 float fx = (float)(sx * x) /
_height_map.size_x + 1.0f;
596 float xr = 2.0f * (fx - x1) - 1.0f;
597 xr = sin(xr * M_PI_2);
598 xr = sin(xr * M_PI_2);
599 xr = 0.5f * (xr + 1.0f);
600 float xri = 1.0f - xr;
610 float fy = (float)(sy * y) /
_height_map.size_y + 1.0f;
613 float yr = 2.0f * (fy - y1) - 1.0f;
614 yr = sin(yr * M_PI_2);
615 yr = sin(yr * M_PI_2);
616 yr = 0.5f * (yr + 1.0f);
617 float yri = 1.0f - yr;
624 uint corner_a = c[x1 + sx * y1];
625 uint corner_b = c[x1 + sx * y2];
626 uint corner_c = c[x2 + sx * y1];
627 uint corner_d = c[x2 + sx * y2];
631 uint corner_bits = 0;
632 corner_bits |= 1 << corner_a;
633 corner_bits |= 1 << corner_b;
634 corner_bits |= 1 << corner_c;
635 corner_bits |= 1 << corner_d;
640 if (*h <
I2H(1))
continue;
646 for (uint t = 0; t <
lengthof(curve_maps); t++) {
647 if (!
HasBit(corner_bits, t))
continue;
649 [[maybe_unused]]
bool found =
false;
650 const control_point_t *cm = curve_maps[t].list;
651 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
652 const control_point_t &p1 = cm[i];
653 const control_point_t &p2 = cm[i + 1];
655 if (*h >= p1.x && *h < p2.x) {
656 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
667 *h = (
height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
678 height_t h_min, h_max, h_avg, h_water_level;
679 int64 water_tiles, desired_water_tiles;
685 int *hist_buf = CallocT<int>(h_max - h_min + 1);
693 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
694 water_tiles += hist[h_water_level];
695 if (water_tiles >= desired_water_tiles)
break;
706 h = (
height_t)(((
int)h_max_new) * (h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
708 if (h < 0) h =
I2H(0);
709 if (h >= h_max_new) h = h_max_new - 1;
715 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
740 const int margin = 4;
747 if (
HasBit(water_borders, BORDER_NE)) {
750 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
751 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
752 for (x = 0; x < max_x; x++) {
757 if (
HasBit(water_borders, BORDER_SW)) {
760 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
761 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
770 if (
HasBit(water_borders, BORDER_NW)) {
773 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
774 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
775 for (y = 0; y < max_y; y++) {
780 if (
HasBit(water_borders, BORDER_SE)) {
783 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
784 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
795 const int max_coast_dist_from_edge = 35;
796 const int max_coast_Smooth_depth = 35;
808 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++) {
821 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
823 h = std::min<uint>(h, h_prev + (4 + depth));
854 for (
int y = 0; y <= (int)
_height_map.size_y; y++) {
855 for (
int x = 0; x <= (int)
_height_map.size_x; x++) {
885 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
909 static double int_noise(
const long x,
const long y,
const int prime)
916 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
925 return a + x * (b - a);
935 const int integer_X = (int)x;
936 const int integer_Y = (int)y;
938 const double fractional_X = x - (double)integer_X;
939 const double fractional_Y = y - (double)integer_Y;
941 const double v1 =
int_noise(integer_X, integer_Y, prime);
942 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
943 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
944 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
963 for (
int i = 0; i < 6; i++) {
964 const double frequency = (double)(1 << i);
965 const double amplitude = pow(p, (
double)i);
967 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;