#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)/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;iplate_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=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; }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; } } } 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; } } } 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); return list; }