尽力了,写的我有点恶心

This commit is contained in:
hinomori_minori
2026-09-17 16:36:11 +09:00
parent ba99a4cfec
commit aa72e0f203
16 changed files with 1924 additions and 104 deletions
+162 -48
View File
@@ -1,72 +1,72 @@
#include "terrain.h"
static int plate_count(uint64_t seed){
return PLATE_MIN + game_hash(seed,PLATE_COUNT,0) % (PLATE_MAX - PLATE_MIN + 1);
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;
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;
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;
(_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;
(_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;
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;
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;
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++){
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 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;
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;
}
}
@@ -80,9 +80,9 @@ static void lloyd_iterate(struct World_Block *_p,struct Plate_list _i,int _t){
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;
(_i._p+i)->is_ocean=true;
}else{
(_i._p+i)->is_ocean = false;
(_i._p+i)->is_ocean=false;
}
}
}
@@ -100,11 +100,11 @@ static void plate_thick(struct Plate_list _i,uint64_t seed){
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=1+(double)game_hash(seed,PLATE_SPEED_X,i)/UINT64_MAX*9;
(_i._p+i)->speed.y=1+(double)game_hash(seed,PLATE_SPEED_Y,i)/UINT64_MAX*9;
(_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*5;
(_i._p+i)->speed.y=(double)game_hash(seed,PLATE_SPEED_Y,i)/UINT64_MAX*5;
(_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;
}
}
}
@@ -119,7 +119,119 @@ static void plate_age(struct Plate_list _i,uint64_t seed){
}
}
struct Plate_list plate_init(uint64_t seed,struct World_Block *block){
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);
}
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 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);
}
}
}
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;
}
}
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;
}
}
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;
}
}
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);
@@ -131,5 +243,7 @@ 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;
}