做完了很多东西
This commit is contained in:
+163
-100
@@ -1,5 +1,92 @@
|
||||
#include "terrain.h"
|
||||
|
||||
int wrap_block_x(int bx) {
|
||||
bx = bx % (int)WORLD_BLOCK_COUNT_X;
|
||||
if (bx < 0) bx += (int)WORLD_BLOCK_COUNT_X;
|
||||
return bx;
|
||||
}
|
||||
|
||||
int wrap_block_y(int by) {
|
||||
if (by < 0) by = 0;
|
||||
if (by >= (int)WORLD_BLOCK_COUNT_Y) by = (int)WORLD_BLOCK_COUNT_Y - 1;
|
||||
return by;
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
// 板块属性空间混合
|
||||
// ================================================================
|
||||
// 板块归属是 512km 网格上的离散值,直接用会在边界留下硬多边形。
|
||||
// 这里用高斯核对邻域 World_Block 的板块属性做加权平均,把它变成连续场。
|
||||
//
|
||||
// sigma = 0.6 个 World_Block ≈ 307 km:两块交界处邻居权重 exp(-0.5*(256/307)^2)
|
||||
// ≈ 0.71,即过渡带半宽约 ±600 km,和地球板块边界变形带的量级相符。
|
||||
//
|
||||
// 半径取 2 块:被截断的最大权重出现在 2 块外的对角(1448 km),
|
||||
// exp(-0.5*(1448/307)^2) ≈ 3e-4,可以忽略。取 3 块只会把每次采样的
|
||||
// exp 次数从 25 涨到 49,换不到可测的精度。
|
||||
#define PLATE_BLEND_RADIUS 2
|
||||
#define PLATE_BLEND_SIGMA (WORLD_BLOCK_SIZE * 0.6)
|
||||
|
||||
void plate_blend_at(struct World_Block *blocks,
|
||||
struct Plate_list plates,
|
||||
double wx, double wy,
|
||||
struct Plate_Blend *out) {
|
||||
int cx = (int)floor(wx / (double)WORLD_BLOCK_SIZE);
|
||||
int cy = (int)floor(wy / (double)WORLD_BLOCK_SIZE);
|
||||
|
||||
const double sigma2 = PLATE_BLEND_SIGMA * PLATE_BLEND_SIGMA;
|
||||
const double inv_2s2 = 1.0 / (2.0 * sigma2);
|
||||
|
||||
double total = 0.0;
|
||||
double sum_oc = 0.0;
|
||||
double sum_age = 0.0;
|
||||
double sum_thk = 0.0;
|
||||
double sum_sx = 0.0;
|
||||
double sum_sy = 0.0;
|
||||
|
||||
for (int dy = -PLATE_BLEND_RADIUS; dy <= PLATE_BLEND_RADIUS; dy++) {
|
||||
for (int dx = -PLATE_BLEND_RADIUS; dx <= PLATE_BLEND_RADIUS; dx++) {
|
||||
int bx = wrap_block_x(cx + dx);
|
||||
int by = wrap_block_y(cy + dy);
|
||||
int bi = by * (int)WORLD_BLOCK_COUNT_X + bx;
|
||||
|
||||
double bxc = ((double)bx + 0.5) * WORLD_BLOCK_SIZE;
|
||||
double byc = ((double)by + 0.5) * WORLD_BLOCK_SIZE;
|
||||
|
||||
double ddx = bxc - wx;
|
||||
// 注意:WORLD_W 是 UL,-WORLD_W 会先按无符号回绕成巨大的正数,
|
||||
// 必须先把半边宽算成 double 再取负,否则判断恒为真、权重全被压成 0。
|
||||
double half_w = (double)WORLD_W * 0.5;
|
||||
if (ddx > half_w) ddx -= (double)WORLD_W;
|
||||
if (ddx < -half_w) ddx += (double)WORLD_W;
|
||||
double ddy = byc - wy;
|
||||
|
||||
double w = exp(-(ddx*ddx + ddy*ddy) * inv_2s2);
|
||||
|
||||
int pid = blocks[bi].plate_id;
|
||||
struct Plate *p = plates._p + pid;
|
||||
|
||||
sum_oc += w * (p->is_ocean ? 1.0 : 0.0);
|
||||
sum_age += w * (double)p->age;
|
||||
sum_thk += w * p->thick;
|
||||
sum_sx += w * p->speed.x;
|
||||
sum_sy += w * p->speed.y;
|
||||
total += w;
|
||||
}
|
||||
}
|
||||
|
||||
if (total < 1e-12) {
|
||||
out->is_ocean = 0; out->age = 0; out->thick = 0;
|
||||
out->speed_x = 0; out->speed_y = 0;
|
||||
return;
|
||||
}
|
||||
out->is_ocean = sum_oc / total;
|
||||
out->age = sum_age / total;
|
||||
out->thick = sum_thk / total;
|
||||
out->speed_x = sum_sx / total;
|
||||
out->speed_y = sum_sy / total;
|
||||
}
|
||||
|
||||
static int plate_count(uint64_t seed){
|
||||
return PLATE_MIN+game_hash(seed,PLATE_COUNT,0)%(PLATE_MAX-PLATE_MIN+1);
|
||||
}
|
||||
@@ -119,116 +206,93 @@ static void plate_age(struct Plate_list _i,uint64_t seed){
|
||||
}
|
||||
}
|
||||
|
||||
static double block_dist(int i,int j) {
|
||||
double ix=WORLD_BLOCK_SIZE*(i%WORLD_BLOCK_COUNT_X);
|
||||
double iy=WORLD_BLOCK_SIZE*(i/WORLD_BLOCK_COUNT_X);
|
||||
double jx=WORLD_BLOCK_SIZE*(j%WORLD_BLOCK_COUNT_X);
|
||||
double jy=WORLD_BLOCK_SIZE*(j/WORLD_BLOCK_COUNT_X);
|
||||
double dx=fabs(ix-jx);
|
||||
if (dx>WORLD_W/2) dx=WORLD_W-dx;
|
||||
double dy=fabs(iy-jy);
|
||||
return sqrt(dx*dx+dy*dy);
|
||||
// ================================================================
|
||||
// 高程
|
||||
// ================================================================
|
||||
// 旧实现用"最近的其他板块 + 边界法线"算边界抬升,问题有两个:
|
||||
// 1) 板块基准高程直接按 is_ocean 二选一,边界上是几百到几千米的断崖;
|
||||
// 2) other_plate_id 在中轴线上会突然换人,抬升量跟着跳变。
|
||||
// 现在改成:先混合出连续的板块属性,再用混合速度场的辐合量做抬升。
|
||||
// 辐合(两块相向)→ 挤压抬升,形成山脉;
|
||||
// 辐散(两块背离)→ 张裂下沉,形成裂谷/洋中脊。
|
||||
// 这两项都是混合场的连续函数,所以边界不再有多边形硬边。
|
||||
|
||||
// 海洋/陆地基准高程,分别算完再用混合后的 is_ocean 插值。
|
||||
// 洋壳公式本来是给 5~12km 厚的玄武岩壳用的:过渡带上混合出来的厚度可能
|
||||
// 是陆壳的 50~70km,直接代进去会算出 -60km 这种无意义的深度,把海岸线
|
||||
// 拖成深渊。所以先把输入夹回各自公式的适用区间。
|
||||
static double plate_base_elev_ocean(double age, double thick) {
|
||||
double t = thick < 5.0 ? 5.0 : (thick > 12.0 ? 12.0 : thick);
|
||||
double a = age < 0.0 ? 0.0 : (age > 200.0 ? 200.0 : age);
|
||||
return -2000.0 - (a / 200.0) * 2000.0 - (t - 5.0) * 400.0;
|
||||
}
|
||||
|
||||
static struct Vector2_double block_dir(int i,int j) {
|
||||
double ix=WORLD_BLOCK_SIZE*(i%WORLD_BLOCK_COUNT_X);
|
||||
double iy=WORLD_BLOCK_SIZE*(i/WORLD_BLOCK_COUNT_X);
|
||||
double jx=WORLD_BLOCK_SIZE*(j%WORLD_BLOCK_COUNT_X);
|
||||
double jy=WORLD_BLOCK_SIZE*(j/WORLD_BLOCK_COUNT_X);
|
||||
double dx=jx-ix;
|
||||
if (dx>WORLD_W/2) dx -= WORLD_W;
|
||||
if (dx<-WORLD_W/2) dx+=WORLD_W;
|
||||
double dy=jy-iy;
|
||||
double len=sqrt(dx*dx+dy*dy);
|
||||
struct Vector2_double v;
|
||||
if (len<1e-9) { v.x=0;v.y=0;}
|
||||
else { v.x=dx/len;v.y=dy/len;}
|
||||
return v;
|
||||
static double plate_base_elev_land(double age, double thick) {
|
||||
return 200.0 + (thick - 30.0) * 20.0 + (age / 4000.0) * 500.0;
|
||||
}
|
||||
|
||||
static void plate_boundary_find(struct World_Block *_p,struct Plate_list _i) {
|
||||
for (int i=0;i<WORLD_BLOCK_COUNT;i++) {
|
||||
(_p+i)->boundary_dist=WORLD_W;
|
||||
(_p+i)->other_plate_id=-1;
|
||||
(_p+i)->boundary_normal.x=0;
|
||||
(_p+i)->boundary_normal.y=0;
|
||||
int ix=i%WORLD_BLOCK_COUNT_X;
|
||||
int iy=i/WORLD_BLOCK_COUNT_X;
|
||||
int my_plate=(_p+i)->plate_id;
|
||||
double best_dist=WORLD_W;
|
||||
int best_j=-1;
|
||||
for (int dy=-PLATE_SEARCH_RADIUS;dy<=PLATE_SEARCH_RADIUS;dy++) {
|
||||
int jy=iy+dy;
|
||||
if (jy<0 || jy>=WORLD_BLOCK_COUNT_Y) continue;
|
||||
for (int dx=-PLATE_SEARCH_RADIUS;dx<=PLATE_SEARCH_RADIUS;dx++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int jx=(ix+dx+WORLD_BLOCK_COUNT_X)%WORLD_BLOCK_COUNT_X;
|
||||
int j=jy*WORLD_BLOCK_COUNT_X+jx;
|
||||
if ((_p+j)->plate_id == my_plate) continue;
|
||||
double d=block_dist(i,j);
|
||||
if (d<best_dist) {
|
||||
best_dist=d;
|
||||
best_j=j;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (best_j>=0) {
|
||||
(_p+i)->boundary_dist=best_dist;
|
||||
(_p+i)->other_plate_id=(_p+best_j)->plate_id;
|
||||
(_p+i)->boundary_normal=block_dir(i,best_j);
|
||||
// 每单位"速度/World_Block"的辐合量抬升多少米。
|
||||
// 混合会削弱速度场的梯度,实测辐合量多在 ±1 以内、峰值约 8,
|
||||
// 这个系数把收敛边界抬成 1~3km 的山系。
|
||||
#define UPLIFT_PER_CONVERGENCE 420.0
|
||||
// 张裂下沉比挤压抬升弱一些,不然洋中脊会挖出过深的沟
|
||||
#define SUBSIDENCE_SCALE 0.55
|
||||
|
||||
static double clamp01d(double x) {
|
||||
return x < 0.0 ? 0.0 : (x > 1.0 ? 1.0 : x);
|
||||
}
|
||||
|
||||
static void plate_elevation(struct World_Block *_p, struct Plate_list _i,
|
||||
uint64_t seed) {
|
||||
const int CX = (int)WORLD_BLOCK_COUNT_X;
|
||||
const int CY = (int)WORLD_BLOCK_COUNT_Y;
|
||||
|
||||
// 1) 先把每个 World_Block 的板块属性混合出来,存成临时场
|
||||
struct Plate_Blend *blend = malloc(sizeof(struct Plate_Blend) * WORLD_BLOCK_COUNT);
|
||||
if (blend == NULL) return;
|
||||
|
||||
for (int j = 0; j < CY; j++) {
|
||||
for (int k = 0; k < CX; k++) {
|
||||
double wx = ((double)k + 0.5) * WORLD_BLOCK_SIZE;
|
||||
double wy = ((double)j + 0.5) * WORLD_BLOCK_SIZE;
|
||||
plate_blend_at(_p, _i, wx, wy, &blend[j * CX + k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static double plate_base_elev(struct Plate *p) {
|
||||
if (p->is_ocean) {
|
||||
return -2000.0-(p->age/200.0)*2000.0-(p->thick-5.0)*400.0;
|
||||
} else {
|
||||
return 200.0+(p->thick-30.0)*20.0+(p->age/4000.0)*500.0;
|
||||
}
|
||||
}
|
||||
// 2) 基准高程 + 速度场辐合 + 随机起伏
|
||||
for (int j = 0; j < CY; j++) {
|
||||
// y 方向不到两极,中心差分在边界退化为单侧差分
|
||||
int jm = (j > 0) ? j - 1 : j;
|
||||
int jp = (j < CY - 1) ? j + 1 : j;
|
||||
|
||||
static double plate_boundary_elev(struct Plate *my,struct Plate *other,double conv,double dist) {
|
||||
double decay=exp(-dist/PLATE_BOUNDARY_INFLUENCE);
|
||||
double elev=0;
|
||||
if (conv>0) {
|
||||
if (!my->is_ocean && !other->is_ocean) {
|
||||
double tf=(my->thick+other->thick)/100.0;
|
||||
elev=conv*400.0*tf*decay;
|
||||
} else if (!my->is_ocean && other->is_ocean) {
|
||||
elev=conv*250.0*decay;
|
||||
} else if (my->is_ocean && !other->is_ocean) {
|
||||
elev=-conv*400.0*decay;
|
||||
} else {
|
||||
elev=conv*200.0*decay;
|
||||
}
|
||||
} else {
|
||||
if (!my->is_ocean && !other->is_ocean) {
|
||||
elev=conv*150.0*decay;
|
||||
} else if (my->is_ocean && other->is_ocean) {
|
||||
elev=-conv*250.0*decay;
|
||||
} else {
|
||||
elev=conv*50.0*decay;
|
||||
for (int k = 0; k < CX; k++) {
|
||||
int km = wrap_block_x(k - 1);
|
||||
int kp = wrap_block_x(k + 1);
|
||||
int idx = j * CX + k;
|
||||
|
||||
const struct Plate_Blend *b = &blend[idx];
|
||||
|
||||
double oc = clamp01d(b->is_ocean);
|
||||
double base = plate_base_elev_land(b->age, b->thick) * (1.0 - oc)
|
||||
+ plate_base_elev_ocean(b->age, b->thick) * oc;
|
||||
|
||||
// 速度场散度(按块做中心差分,x 环绕、y 夹紧)
|
||||
double dvx = (blend[j * CX + kp].speed_x - blend[j * CX + km].speed_x) * 0.5;
|
||||
double dvy = (blend[jp * CX + k].speed_y - blend[jm * CX + k].speed_y) * 0.5;
|
||||
double conv = -(dvx + dvy); // >0 = 辐合
|
||||
|
||||
double uplift = conv * UPLIFT_PER_CONVERGENCE;
|
||||
if (uplift < 0.0) uplift *= SUBSIDENCE_SCALE;
|
||||
|
||||
double noise = ((double)game_hash(seed, PLATE_HEIGHT, idx) / UINT64_MAX
|
||||
- 0.5) * 200.0;
|
||||
|
||||
_p[idx].elevation = base + uplift + noise;
|
||||
_p[idx].convergence = conv;
|
||||
}
|
||||
}
|
||||
return elev;
|
||||
}
|
||||
|
||||
static void plate_elevation(struct World_Block *_p,struct Plate_list _i,uint64_t seed) {
|
||||
for (int i=0;i<WORLD_BLOCK_COUNT;i++) {
|
||||
struct Plate *my=_i._p+(_p+i)->plate_id;
|
||||
double elev=plate_base_elev(my);
|
||||
if ((_p+i)->other_plate_id>=0) {
|
||||
struct Plate *other=_i._p+(_p+i)->other_plate_id;
|
||||
double rel_x=my->speed.x-other->speed.x;
|
||||
double rel_y=my->speed.y-other->speed.y;
|
||||
double conv=rel_x*(_p+i)->boundary_normal.x+rel_y*(_p+i)->boundary_normal.y;
|
||||
elev+=plate_boundary_elev(my,other,conv,(_p+i)->boundary_dist);
|
||||
}
|
||||
double noise=((double)game_hash(seed,PLATE_HEIGHT,i)/UINT64_MAX-0.5)*200.0;
|
||||
elev+=noise;
|
||||
(_p+i)->elevation=elev;
|
||||
}
|
||||
free(blend);
|
||||
}
|
||||
|
||||
struct Plate_list plate_init(uint64_t seed, struct World_Block *block){
|
||||
@@ -243,7 +307,6 @@ struct Plate_list plate_init(uint64_t seed, struct World_Block *block){
|
||||
plate_thick(list,seed);
|
||||
plate_speed(list,seed);
|
||||
plate_age(list,seed);
|
||||
plate_boundary_find(block, list);
|
||||
plate_elevation(block, list, seed);
|
||||
return list;
|
||||
}
|
||||
Reference in New Issue
Block a user