89 lines
3.2 KiB
C
89 lines
3.2 KiB
C
#include "terrain.h"
|
|
|
|
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/G)/UINT64_MAX;
|
|
double jy = (double)game_hash(seed,PLATE_JITTER_Y,i%G)/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;
|
|
}
|
|
|
|
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);
|
|
return list;
|
|
} |