diff --git a/src/main.h b/src/main.h index 836dfd0..0c95ac3 100644 --- a/src/main.h +++ b/src/main.h @@ -38,7 +38,7 @@ struct { struct text_list{ char **_p; - int count; + unsigned long count; }; enum{ diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000..0a4df0d --- /dev/null +++ b/src/random.c @@ -0,0 +1,18 @@ +#include "random.h" + +uint64_t game_hash(uint64_t seed,int type,int subtype){ + if(type==0){ + splitmix64_seed(seed); + return splitmix64(); + }else if(subtype==0){ + uint64_t new_seed = splitmix64_stateless(seed, type); + splitmix64_seed(new_seed); + return splitmix64(); + } + uint64_t offset = ((uint64_t)type << 32) | (uint64_t)subtype; + uint64_t new_seed = splitmix64_stateless(seed, offset); + splitmix64_seed(new_seed); + return splitmix64(); + return 0; +} + diff --git a/src/random.h b/src/random.h new file mode 100644 index 0000000..0e69993 --- /dev/null +++ b/src/random.h @@ -0,0 +1,14 @@ +#include "splitmix64.h" + +#ifndef RANDOM_H +#define RANDOM_H + +enum { + PLATE_COUNT=1, + PLATE_JITTER_X, + PLATE_JITTER_Y +}; + +uint64_t game_hash(uint64_t seed,int type,int subtype); + +#endif \ No newline at end of file diff --git a/src/splitmix64.h b/src/splitmix64.h new file mode 100644 index 0000000..dd52359 --- /dev/null +++ b/src/splitmix64.h @@ -0,0 +1,70 @@ +#ifndef SPLITMIX64_H +#define SPLITMIX64_H + +/* Modified by D. Lemire, August 2017 */ +/*** +Fast Splittable Pseudorandom Number Generators +Steele Jr, Guy L., Doug Lea, and Christine H. Flood. "Fast splittable +pseudorandom number generators." +ACM SIGPLAN Notices 49.10 (2014): 453-472. +***/ + +/* Written in 2015 by Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include + +// original documentation by Vigna: +/* This is a fixed-increment version of Java 8's SplittableRandom generator + See http://dx.doi.org/10.1145/2714064.2660195 and + http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html + + It is a very fast generator passing BigCrush, and it can be useful if + for some reason you absolutely want 64 bits of state; otherwise, we + rather suggest to use a xoroshiro128+ (for moderately parallel + computations) or xorshift1024* (for massively parallel computations) + generator. */ + +// state for splitmix64 +static uint64_t splitmix64_x; /* The state can be seeded with any value. */ + +// call this one before calling splitmix64 +static inline void splitmix64_seed(uint64_t seed) { splitmix64_x = seed; } + +// floor( ( (1+sqrt(5))/2 ) * 2**64 MOD 2**64) +#define GOLDEN_GAMMA UINT64_C(0x9E3779B97F4A7C15) + +// returns random number, modifies seed[0] +// compared with D. Lemire against +// http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/util/SplittableRandom.java#SplittableRandom.0gamma +static inline uint64_t splitmix64_r(uint64_t *seed) { + uint64_t z = (*seed += GOLDEN_GAMMA); + // David Stafford's Mix13 for MurmurHash3's 64-bit finalizer + z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); + z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); + return z ^ (z >> 31); +} + +// returns random number, modifies splitmix64_x +static inline uint64_t splitmix64(void) { + return splitmix64_r(&splitmix64_x); +} + +// returns the 32 least significant bits of a call to splitmix64 +// this is a simple (inlined) function call followed by a cast +static inline uint32_t splitmix64_cast32(void) { + return (uint32_t)splitmix64(); +} + +// returns the value of splitmix64 "offset" steps from seed +static inline uint64_t splitmix64_stateless(uint64_t seed, uint64_t offset) { + seed += offset*GOLDEN_GAMMA; + return splitmix64_r(&seed); +} + +#endif // SPLITMIX64_H \ No newline at end of file diff --git a/src/terrain.c b/src/terrain.c new file mode 100644 index 0000000..934ef91 --- /dev/null +++ b/src/terrain.c @@ -0,0 +1,89 @@ +#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;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; +} + +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; +} \ No newline at end of file diff --git a/src/terrain.h b/src/terrain.h new file mode 100644 index 0000000..5023f44 --- /dev/null +++ b/src/terrain.h @@ -0,0 +1,61 @@ +#include "raylib.h" +#include "random.h" +#include +#include +#include + +#ifndef TERRAIN_H +#define TERRAIN_H + +#define WORLD_W 40960000UL +#define WORLD_H 40960000UL +#define PLATE_MAX 20UL +#define PLATE_MIN 8UL +#define WORLD_BLOCK_SIZE 512000UL +#define WORLD_BLOCK_COUNT_X (WORLD_W/WORLD_BLOCK_SIZE) +#define WORLD_BLOCK_COUNT_Y (WORLD_H/WORLD_BLOCK_SIZE) +#define WORLD_BLOCK_COUNT (WORLD_BLOCK_COUNT_X*WORLD_BLOCK_COUNT_Y) + +struct Vector2_double { + double x; + double y; +}; + +struct Vector3_double { + double x; + double y; + double z; +}; + +struct Vector2_double_list{ + struct Vector2_double *_p; + unsigned long count; +}; + +struct Plate { + struct Vector2_double seed; + struct Vector2_double speed; + bool is_ocean; + double thick; + unsigned long age; + int rock_type; + int world_block_count; +}; + +struct Plate_list { + struct Plate *_p; + unsigned long count; +}; + +struct World_Block { + int plate_id; + double nearest_plate_dist; +}; + +struct World { + +}; + +struct Plate_list plate_init(uint64_t seed,struct World_Block *block); + +#endif \ No newline at end of file diff --git a/src/ui.h b/src/ui.h index 911087c..eab36c5 100644 --- a/src/ui.h +++ b/src/ui.h @@ -45,12 +45,12 @@ struct Input_Box { struct Button_list { struct Button **_p; - int count; + unsigned long count; }; struct Input_Box_list { struct Input_Box **_p; - int count; + unsigned long count; }; extern float screen_scale;