Files
industrial-genesis/src/terrain.c
T
2026-09-18 19:42:20 +09:00

312 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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);
}
static void plate_seed(struct Plate_list _i,uint64_t seed){
int G=ceil(sqrt(_i.count));
double cw=WORLD_W/G;
double ch=WORLD_H/G;
for(int i=0;i<_i.count;i++){
double jx=(double)game_hash(seed,PLATE_JITTER_X,i)/UINT64_MAX;
double jy=(double)game_hash(seed,PLATE_JITTER_Y,i)/UINT64_MAX;
double x=(i%G +jx)*cw;
if(x>WORLD_W)x-=WORLD_W;
(_i._p+i)->seed.x=x;
(_i._p+i)->seed.y=(i/G +jy)*ch;
}
return;
}
static void lloyd_init(struct World_Block *_p,struct Plate_list _i){
for(int i=0;i<WORLD_BLOCK_COUNT;i++){
(_p+i)->plate_id=_i.count;
(_p+i)->nearest_plate_dist=WORLD_W<<1;
}
return;
}
static void lloyd_relaxed(struct World_Block *_p,struct Plate_list _i){
for(int i=0;i<_i.count;i++){
(_i._p+i)->world_block_count=0;
}
for(int i=0;i<WORLD_BLOCK_COUNT;i++){
(_p+i)->nearest_plate_dist=WORLD_W*2;
for(int j=0;j<_i.count;j++){
double block_x=WORLD_BLOCK_SIZE/2+WORLD_BLOCK_SIZE*(i%WORLD_BLOCK_COUNT_X);
double block_y=WORLD_BLOCK_SIZE/2+WORLD_BLOCK_SIZE*(i/WORLD_BLOCK_COUNT_X);
double dx=fabs(block_x-(_i._p+j)->seed.x);
if(dx>WORLD_W/2) dx=WORLD_W-dx;
double dy=fabs(block_y-(_i._p+j)->seed.y);
double dist=sqrt(dx*dx+dy*dy);
if(dist<(_p+i)->nearest_plate_dist){
(_p+i)->nearest_plate_dist=dist;
(_p+i)->plate_id=j;
}
}
(_i._p+(_p+i)->plate_id)->world_block_count++;
}
struct Vector3 plate_mass_center[_i.count];
memset(plate_mass_center,0,sizeof(plate_mass_center));
for(int i=0;i<_i.count;i++){
int count=(_i._p+i)->world_block_count;
if(count == 0) continue;
for(int j=0;j<WORLD_BLOCK_COUNT;j++){
if((_p+j)->plate_id == i){
double block_x=WORLD_BLOCK_SIZE/2+WORLD_BLOCK_SIZE*(j%WORLD_BLOCK_COUNT_X);
double block_y=WORLD_BLOCK_SIZE/2+WORLD_BLOCK_SIZE*(j/WORLD_BLOCK_COUNT_X);
double theta=2.0*PI*block_x/WORLD_W;
plate_mass_center[i].x+=cos(theta)/count;
plate_mass_center[i].y+=sin(theta)/count;
plate_mass_center[i].z+=block_y/count;
}
}
double theta_bar=atan2(plate_mass_center[i].y,plate_mass_center[i].x);
double x_bar=WORLD_W*theta_bar/(2.0*PI);
if(x_bar<0) x_bar+=WORLD_W;
(_i._p+i)->seed.x=x_bar;
(_i._p+i)->seed.y=plate_mass_center[i].z;
}
}
static void lloyd_iterate(struct World_Block *_p,struct Plate_list _i,int _t){
for(int i=0;i<_t;i++){
lloyd_relaxed(_p,_i);
}
return;
}
static void plate_is_ocean(struct Plate_list _i,uint64_t seed){
for(int i=0;i<_i.count;i++){
if((double)game_hash(seed,PLATE_IS_OCEAN,i)/UINT64_MAX>0.4){
(_i._p+i)->is_ocean=true;
}else{
(_i._p+i)->is_ocean=false;
}
}
}
static void plate_thick(struct Plate_list _i,uint64_t seed){
for(int i=0;i<_i.count;i++){
if((_i._p+i)->is_ocean){
(_i._p+i)->thick=5+(double)game_hash(seed,PLATE_THICK,i)/UINT64_MAX*5;
}else{
(_i._p+i)->thick=30+(double)game_hash(seed,PLATE_THICK,i)/UINT64_MAX*40;
}
}
}
static void plate_speed(struct Plate_list _i,uint64_t seed){
for(int i=0;i<_i.count;i++){
if((_i._p+i)->is_ocean){
(_i._p+i)->speed.x=(double)game_hash(seed,PLATE_SPEED_X,i)/UINT64_MAX*20-10;
(_i._p+i)->speed.y=(double)game_hash(seed,PLATE_SPEED_Y,i)/UINT64_MAX*20-10;
}else{
(_i._p+i)->speed.x=(double)game_hash(seed,PLATE_SPEED_X,i)/UINT64_MAX*10-5;
(_i._p+i)->speed.y=(double)game_hash(seed,PLATE_SPEED_Y,i)/UINT64_MAX*10-5;
}
}
}
static void plate_age(struct Plate_list _i,uint64_t seed){
for(int i=0;i<_i.count;i++){
if((_i._p+i)->is_ocean){
(_i._p+i)->age=(double)game_hash(seed,PLATE_AGE,i)/UINT64_MAX*200;
}else{
(_i._p+i)->age=200+(double)game_hash(seed,PLATE_AGE,i)/UINT64_MAX*3800;
}
}
}
// ================================================================
// 高程
// ================================================================
// 旧实现用"最近的其他板块 + 边界法线"算边界抬升,问题有两个:
// 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 double plate_base_elev_land(double age, double thick) {
return 200.0 + (thick - 30.0) * 20.0 + (age / 4000.0) * 500.0;
}
// 每单位"速度/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]);
}
}
// 2) 基准高程 + 速度场辐合 + 随机起伏
for (int j = 0; j < CY; j++) {
// y 方向不到两极,中心差分在边界退化为单侧差分
int jm = (j > 0) ? j - 1 : j;
int jp = (j < CY - 1) ? j + 1 : j;
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;
}
}
free(blend);
}
struct Plate_list plate_init(uint64_t seed, struct World_Block *block){
int count = plate_count(seed);
struct Plate_list list;
list._p = malloc(sizeof(struct Plate)*count);
list.count = count;
plate_seed(list,seed);
lloyd_init(block,list);
lloyd_iterate(block,list,2);
plate_is_ocean(list,seed);
plate_thick(list,seed);
plate_speed(list,seed);
plate_age(list,seed);
plate_elevation(block, list, seed);
return list;
}