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++)
237 { 3, 3, 3, 3, 4, 5, 7 },
238 { 5, 7, 8, 9, 14, 19, 31 },
239 { 8, 9, 10, 15, 23, 37, 61 },
240 { 10, 11, 17, 19, 49, 63, 73 },
241 { 12, 19, 25, 31, 67, 75, 87 },
252 return I2H(max_height_from_table);
274 {16000, 5600, 1968, 688, 240, 16, 16},
275 {24000, 12800, 6400, 2700, 1024, 128, 16},
276 {32000, 19200, 12800, 8000, 3200, 256, 64},
277 {48000, 24000, 19200, 16000, 8000, 512, 320},
289 static const double extrapolation_factors[] = { 3.3, 2.8, 2.3, 1.8 };
295 amplitude_t amplitude = amplitudes[smoothness][std::max(0, index)];
296 if (index >= 0)
return amplitude;
299 double extrapolation_factor = extrapolation_factors[smoothness];
300 int height_range =
I2H(16);
302 amplitude = (
amplitude_t)(extrapolation_factor * (
double)amplitude);
382 if (amplitude == 0)
continue;
388 for (
int y = 0; y <=
_height_map.size_y; y += step) {
389 for (
int x = 0; x <=
_height_map.size_x; x += step) {
400 for (
int y = 0; y <=
_height_map.size_y; y += 2 * step) {
401 for (
int x = 0; x <=
_height_map.size_x - 2 * step; x += 2 * step) {
410 for (
int y = 0; y <=
_height_map.size_y - 2 * step; y += 2 * step) {
411 for (
int x = 0; x <=
_height_map.size_x; x += step) {
420 for (
int y = 0; y <=
_height_map.size_y; y += step) {
421 for (
int x = 0; x <=
_height_map.size_x; x += step) {
437 if (*h < h_min) h_min = *h;
438 if (*h > h_max) h_max = *h;
446 if (min_ptr !=
nullptr) *min_ptr = h_min;
447 if (max_ptr !=
nullptr) *max_ptr = h_max;
448 if (avg_ptr !=
nullptr) *avg_ptr = h_avg;
454 int *hist = hist_buf - h_min;
474 if (*h < h_min)
continue;
477 fheight = (double)(*h - h_min) / (double)(h_max - h_min);
483 fheight = 2 * fheight - 1;
485 fheight = sin(fheight * M_PI_2);
487 fheight = 0.5 * (fheight + 1);
494 double sine_upper_limit = 0.75;
495 double linear_compression = 2;
496 if (fheight >= sine_upper_limit) {
498 fheight = 1.0 - (1.0 - fheight) / linear_compression;
500 double m = 1.0 - (1.0 - sine_upper_limit) / linear_compression;
502 fheight = 2.0 * fheight / sine_upper_limit - 1.0;
504 fheight = sin(fheight * M_PI_2);
506 fheight = 0.5 * (fheight + 1.0) * m;
515 double sine_lower_limit = 0.5;
516 double linear_compression = 2;
517 if (fheight <= sine_lower_limit) {
519 fheight = fheight / linear_compression;
521 double m = sine_lower_limit / linear_compression;
523 fheight = 2.0 * ((fheight - sine_lower_limit) / (1.0 - sine_lower_limit)) - 1.0;
525 fheight = sin(fheight * M_PI_2);
527 fheight = 0.5 * ((1.0 - m) * fheight + (1.0 + m));
537 *h = (
height_t)(fheight * (h_max - h_min) + h_min);
538 if (*h < 0) *h =
I2H(0);
539 if (*h >= h_max) *h = h_max - 1;
564 struct control_point_t {
569 #define F(fraction) ((height_t)(fraction * mh))
570 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) } };
571 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) } };
572 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) } };
573 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) } };
577 struct control_point_list_t {
579 const control_point_t *list;
581 const control_point_list_t curve_maps[] = {
582 {
lengthof(curve_map_1), curve_map_1 },
583 {
lengthof(curve_map_2), curve_map_2 },
584 {
lengthof(curve_map_3), curve_map_3 },
585 {
lengthof(curve_map_4), curve_map_4 },
593 uint sx =
Clamp((
int)(((1 << level) * factor) + 0.5), 1, 128);
594 uint sy =
Clamp((
int)(((1 << level) / factor) + 0.5), 1, 128);
595 byte *c =
AllocaM(
byte, sx * sy);
597 for (uint i = 0; i < sx * sy; i++) {
598 c[i] = Random() %
lengthof(curve_maps);
605 float fx = (float)(sx * x) /
_height_map.size_x + 1.0f;
608 float xr = 2.0f * (fx - x1) - 1.0f;
609 xr = sin(xr * M_PI_2);
610 xr = sin(xr * M_PI_2);
611 xr = 0.5f * (xr + 1.0f);
612 float xri = 1.0f - xr;
622 float fy = (float)(sy * y) /
_height_map.size_y + 1.0f;
625 float yr = 2.0f * (fy - y1) - 1.0f;
626 yr = sin(yr * M_PI_2);
627 yr = sin(yr * M_PI_2);
628 yr = 0.5f * (yr + 1.0f);
629 float yri = 1.0f - yr;
636 uint corner_a = c[x1 + sx * y1];
637 uint corner_b = c[x1 + sx * y2];
638 uint corner_c = c[x2 + sx * y1];
639 uint corner_d = c[x2 + sx * y2];
643 uint corner_bits = 0;
644 corner_bits |= 1 << corner_a;
645 corner_bits |= 1 << corner_b;
646 corner_bits |= 1 << corner_c;
647 corner_bits |= 1 << corner_d;
652 if (*h <
I2H(1))
continue;
658 for (uint t = 0; t <
lengthof(curve_maps); t++) {
659 if (!
HasBit(corner_bits, t))
continue;
664 const control_point_t *cm = curve_maps[t].list;
665 for (uint i = 0; i < curve_maps[t].length - 1; i++) {
666 const control_point_t &p1 = cm[i];
667 const control_point_t &p2 = cm[i + 1];
669 if (*h >= p1.x && *h < p2.x) {
670 ht[t] = p1.y + (*h - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
681 *h = (
height_t)((ht[corner_a] * yri + ht[corner_b] * yr) * xri + (ht[corner_c] * yri + ht[corner_d] * yr) * xr);
692 height_t h_min, h_max, h_avg, h_water_level;
693 int64 water_tiles, desired_water_tiles;
700 int *hist_buf = CallocT<int>(h_max - h_min + 1);
708 for (h_water_level = h_min, water_tiles = 0; h_water_level < h_max; h_water_level++) {
709 water_tiles += hist[h_water_level];
710 if (water_tiles >= desired_water_tiles)
break;
721 *h = (
height_t)(((
int)h_max_new) * (*h - h_water_level) / (h_max - h_water_level)) +
I2H(1);
723 if (*h < 0) *h =
I2H(0);
724 if (*h >= h_max_new) *h = h_max_new - 1;
730 static double perlin_coast_noise_2D(
const double x,
const double y,
const double p,
const int prime);
755 const int margin = 4;
762 if (
HasBit(water_borders, BORDER_NE)) {
765 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
766 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
767 for (x = 0; x < max_x; x++) {
772 if (
HasBit(water_borders, BORDER_SW)) {
775 max_x = std::max((smallest_size * smallest_size / 64) + max_x, (smallest_size * smallest_size / 64) + margin - max_x);
776 if (smallest_size < 8 && max_x > 5) max_x /= 1.5;
785 if (
HasBit(water_borders, BORDER_NW)) {
788 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
789 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
790 for (y = 0; y < max_y; y++) {
795 if (
HasBit(water_borders, BORDER_SE)) {
798 max_y = std::max((smallest_size * smallest_size / 64) + max_y, (smallest_size * smallest_size / 64) + margin - max_y);
799 if (smallest_size < 8 && max_y > 5) max_y /= 1.5;
810 const int max_coast_dist_from_edge = 35;
811 const int max_coast_Smooth_depth = 35;
823 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 for (depth = 0;
IsValidXY(x, y) && depth <= max_coast_Smooth_depth; depth++, x += dir_x, y += dir_y) {
838 h = std::min<uint>(h, h_prev + (4 + depth));
869 for (
int y = 0; y <= (int)
_height_map.size_y; y++) {
870 for (
int x = 0; x <= (int)
_height_map.size_x; x++) {
900 if (water_borders == BORDERS_RANDOM) water_borders =
GB(Random(), 0, 4);
924 static double int_noise(
const long x,
const long y,
const int prime)
931 return 1.0 - (double)((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0;
940 return a + x * (b - a);
950 const int integer_X = (int)x;
951 const int integer_Y = (int)y;
953 const double fractional_X = x - (double)integer_X;
954 const double fractional_Y = y - (double)integer_Y;
956 const double v1 =
int_noise(integer_X, integer_Y, prime);
957 const double v2 =
int_noise(integer_X + 1, integer_Y, prime);
958 const double v3 =
int_noise(integer_X, integer_Y + 1, prime);
959 const double v4 =
int_noise(integer_X + 1, integer_Y + 1, prime);
978 for (
int i = 0; i < 6; i++) {
979 const double frequency = (double)(1 << i);
980 const double amplitude = pow(p, (
double)i);
982 total +=
interpolated_noise((x * frequency) / 64.0, (y * frequency) / 64.0, prime) * amplitude;