154 static const int height_decimal_bits = 4;
158 static const int amplitude_decimal_bits = 10;
163 std::vector<Height> h;
178 return h[x + y * dim_x];
186 #define I2H(i) ((i) << height_decimal_bits)
188 #define H2I(i) ((i) >> height_decimal_bits)
191 #define I2A(i) ((i) << amplitude_decimal_bits)
193 #define A2I(i) ((i) >> amplitude_decimal_bits)
196 #define A2H(a) ((a) >> (amplitude_decimal_bits - height_decimal_bits))
231 { 3, 3, 3, 3, 4, 5, 7 },
232 { 5, 7, 8, 9, 14, 19, 31 },
233 { 8, 9, 10, 15, 23, 37, 61 },
234 { 10, 11, 17, 19, 49, 63, 73 },
235 { 12, 19, 25, 31, 67, 75, 87 },
246 return I2H(max_height_from_table);
266 static const Amplitude amplitudes[][7] = {
268 {16000, 5600, 1968, 688, 240, 16, 16},
269 {24000, 12800, 6400, 2700, 1024, 128, 16},
270 {32000, 19200, 12800, 8000, 3200, 256, 64},
271 {48000, 24000, 19200, 16000, 8000, 512, 320},
283 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
288 int index = frequency -
MAX_TGP_FREQUENCIES +
static_cast<int>(std::size(amplitudes[smoothness]));
289 Amplitude amplitude = amplitudes[smoothness][std::max(0, index)];
290 if (index >= 0)
return amplitude;
293 double extrapolation_factor = extrapolation_factors[smoothness];
294 int height_range =
I2H(16);
296 amplitude = (
Amplitude)(extrapolation_factor * (
double)amplitude);
372 if (amplitude == 0)
continue;
378 for (
int y = 0; y <=
_height_map.size_y; y += step) {
379 for (
int x = 0; x <=
_height_map.size_x; x += step) {
390 for (
int y = 0; y <=
_height_map.size_y; y += 2 * step) {
391 for (
int x = 0; x <=
_height_map.size_x - 2 * step; x += 2 * step) {
394 Height h01 = (h00 + h02) / 2;
400 for (
int y = 0; y <=
_height_map.size_y - 2 * step; y += 2 * step) {
401 for (
int x = 0; x <=
_height_map.size_x; x += step) {
404 Height h10 = (h00 + h20) / 2;
410 for (
int y = 0; y <=
_height_map.size_y; y += step) {
411 for (
int x = 0; x <=
_height_map.size_x; x += step) {
421 Height h_min, h_max, h_avg;
427 if (h < h_min) h_min = h;
428 if (h > h_max) h_max = h;
436 if (min_ptr !=
nullptr) *min_ptr = h_min;
437 if (max_ptr !=
nullptr) *max_ptr = h_max;
438 if (avg_ptr !=
nullptr) *avg_ptr = h_avg;
444 int *hist = hist_buf - h_min;
461 if (h < h_min)
continue;
464 fheight = (double)(h - h_min) / (double)(h_max - h_min);
470 fheight = 2 * fheight - 1;
472 fheight = sin(fheight * M_PI_2);
474 fheight = 0.5 * (fheight + 1);
481 double sine_upper_limit = 0.75;
482 double linear_compression = 2;
483 if (fheight >= sine_upper_limit) {
485 fheight = 1.0 - (1.0 - fheight) / linear_compression;
487 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
489 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
491 fheight = sin(fheight * M_PI_2);
493 fheight = 0.5 * (fheight + 1.0) * m;
502 double sine_lower_limit = 0.5;
503 double linear_compression = 2;
504 if (fheight <= sine_lower_limit) {
506 fheight = fheight / linear_compression;
508 double m = sine_lower_limit / linear_compression;
510 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
512 fheight = sin(fheight * M_PI_2);
514 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
524 h = (
Height)(fheight * (h_max - h_min) + h_min);
525 if (h < 0) h =
I2H(0);
526 if (h >= h_max) h = h_max - 1;
551 struct ControlPoint {
556 #define F(fraction) ((Height)(fraction * mh))
557 const ControlPoint curve_map_1[] = { { F(0.0), F(0.0) }, { F(0.8), F(0.13) }, { F(1.0), F(0.4) } };
558 const ControlPoint 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) } };
559 const ControlPoint 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) } };
560 const ControlPoint 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) } };
563 static const std::span<const ControlPoint> curve_maps[] = { curve_map_1, curve_map_2, curve_map_3, curve_map_4 };
565 std::array<
Height, std::size(curve_maps)> ht{};
569 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
570 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
571 std::vector<uint8_t> c(
static_cast<size_t>(sx) * sy);
573 for (uint i = 0; i < sx * sy; i++) {
574 c[i] =
RandomRange(
static_cast<uint32_t
>(std::size(curve_maps)));
581 float fx = (float)(sx * x) /
_height_map.size_x + 1.0f;
584 float xr = 2.0f * (fx - x1) - 1.0f;
585 xr = sin(xr * M_PI_2);
586 xr = sin(xr * M_PI_2);
587 xr = 0.5f * (xr + 1.0f);
588 float xri = 1.0f - xr;
598 float fy = (float)(sy * y) /
_height_map.size_y + 1.0f;
601 float yr = 2.0f * (fy - y1) - 1.0f;
602 yr = sin(yr * M_PI_2);
603 yr = sin(yr * M_PI_2);
604 yr = 0.5f * (yr + 1.0f);
605 float yri = 1.0f - yr;
612 uint corner_a = c[x1 + sx * y1];
613 uint corner_b = c[x1 + sx * y2];
614 uint corner_c = c[x2 + sx * y1];
615 uint corner_d = c[x2 + sx * y2];
619 uint corner_bits = 0;
620 corner_bits |= 1 << corner_a;
621 corner_bits |= 1 << corner_b;
622 corner_bits |= 1 << corner_c;
623 corner_bits |= 1 << corner_d;
628 if (*h <
I2H(1))
continue;
634 for (
size_t t = 0; t < std::size(curve_maps); t++) {
635 if (!
HasBit(corner_bits,
static_cast<uint8_t
>(t)))
continue;
637 [[maybe_unused]]
bool found =
false;
638 auto &cm = curve_maps[t];
639 for (
size_t i = 0; i < cm.size() - 1; i++) {
640 const ControlPoint &p1 = cm[i];
641 const ControlPoint &p2 = cm[i + 1];
643 if (*h >= p1.x && *h < p2.x) {
644 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
655 *h = (
Height)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
666 Height h_min, h_max, h_avg, h_water_level;
667 int64_t water_tiles, desired_water_tiles;
673 std::vector<int> hist_buf(h_max - h_min + 1);
681 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
682 water_tiles += hist[h_water_level];
683 if (water_tiles >= desired_water_tiles)
break;
694 h = (
Height)(((
int)h_max_new) * (h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
696 if (h < 0) h =
I2H(0);
697 if (h >= h_max_new) h = h_max_new - 1;
701 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
726 const int margin = 4;
733 if (
HasBit(water_borders, BORDER_NE)) {
736 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
737 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
738 for (x = 0; x < max_x; x++) {
743 if (
HasBit(water_borders, BORDER_SW)) {
746 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
747 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
756 if (
HasBit(water_borders, BORDER_NW)) {
759 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
760 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
761 for (y = 0; y < max_y; y++) {
766 if (
HasBit(water_borders, BORDER_SE)) {
769 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
770 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
781 const int max_coast_dist_from_edge = 35;
782 const int max_coast_Smooth_depth = 35;
794 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++) {
807 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
809 h =
static_cast<Height>(std::min<uint>(h, h_prev + (4 + depth)));
840 for (
int y = 0; y <= (int)
_height_map.size_y; y++) {
841 for (
int x = 0; x <= (int)
_height_map.size_x; x++) {
871 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
895 static double int_noise(
const long x,
const long y,
const int prime)
902 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
911 return a + x * (b - a);
921 const int integer_X = (int)x;
922 const int integer_Y = (int)y;
924 const double fractional_X = x - (double)integer_X;
925 const double fractional_Y = y - (double)integer_Y;
927 const double v1 =
int_noise(integer_X, integer_Y, prime);
928 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
929 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
930 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
949 for (
int i = 0; i < 6; i++) {
950 const double frequency = (double)(1 << i);
951 const double amplitude = pow(p, (
double)i);
953 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;