yet another API cleanup
This commit is contained in:
parent
2008f70983
commit
b6b3483a39
9 changed files with 134 additions and 114 deletions
79
gen.c
79
gen.c
|
@ -1,64 +1,14 @@
|
|||
#include "cubes.h"
|
||||
#include <math.h>
|
||||
#include "noise.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct chunk *chunks = NULL;
|
||||
int lastI = 0;
|
||||
int seed = 69420;
|
||||
int prevExists = 0;
|
||||
double interpolate(double a0, double a1, double w) {
|
||||
return (a1 - a0) * (3.0 - w * 2.0) * w * w + a0;
|
||||
}
|
||||
|
||||
int hash_combine(int a, int b) { return b + 0x9e3779b9 + (a << 6) + (a >> 2); }
|
||||
|
||||
struct v2f perlin_grad(int x, int y) {
|
||||
int x_hash = hash_combine(x, seed);
|
||||
int y_hash = hash_combine(seed, y);
|
||||
|
||||
double random = hash_combine(x_hash, y_hash) * (M_PI / ~(~0u >> 1));
|
||||
struct v2f out = {cos(random), sin(random)};
|
||||
return out;
|
||||
}
|
||||
|
||||
double perlin_dot(double ix, double iy, double x, double y) {
|
||||
struct v2f grad = perlin_grad(ix, iy);
|
||||
|
||||
double dx = x - (double)ix;
|
||||
double dy = y - (double)iy;
|
||||
return (dx * grad.pos[0] + dy * grad.pos[1]);
|
||||
}
|
||||
|
||||
double perlin_calc(double x, double y, double s) {
|
||||
x /= s;
|
||||
y /= s;
|
||||
|
||||
int x0 = (int)floor(x);
|
||||
int x1 = x0 + 1;
|
||||
int y0 = (int)floor(y);
|
||||
int y1 = y0 + 1;
|
||||
|
||||
float sx = x - (float)x0;
|
||||
float sy = y - (float)y0;
|
||||
|
||||
float n0, n1, ix0, ix1, value;
|
||||
|
||||
n0 = perlin_dot(x0, y0, x, y);
|
||||
n1 = perlin_dot(x1, y0, x, y);
|
||||
ix0 = interpolate(n0, n1, sx);
|
||||
|
||||
n0 = perlin_dot(x0, y1, x, y);
|
||||
n1 = perlin_dot(x1, y1, x, y);
|
||||
ix1 = interpolate(n0, n1, sx);
|
||||
|
||||
value = interpolate(ix0, ix1, sy);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
struct chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
||||
struct chunk gen_chunk(int x, int y, int z, int ci, int h) {
|
||||
|
||||
struct chunk *chunks2 = chunks; // &chunks[CBUF_ALL * ci];
|
||||
|
||||
|
@ -128,22 +78,7 @@ struct chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
|||
|
||||
int c = 0;
|
||||
|
||||
for (int x2 = 0; x2 < CHUNK_LENGTH; x2++) {
|
||||
int x3 = x2 + x * CHUNK_LENGTH;
|
||||
for (int z2 = 0; z2 < CHUNK_LENGTH; z2++) {
|
||||
int z3 = z2 + z * CHUNK_LENGTH;
|
||||
double hp = perlin_calc(x3, z3, 128.0);
|
||||
hp = ((hp > 0.0) ? -1.0 : 1.0) * (pow(fabs(hp), 0.7));
|
||||
hp *= 32.0;
|
||||
int h = hp + 32;
|
||||
|
||||
for (int y2 = 0; y2 < CHUNK_LENGTH; y2++) {
|
||||
int y3 = y2 + y * CHUNK_LENGTH;
|
||||
chunk.blocks[c] = (y3 < h) ? (((int)((hp + 64.0) * 2.98) % 3) + 1) : 0;
|
||||
c++;
|
||||
}
|
||||
}
|
||||
}
|
||||
chunk = noise_chunk(chunk, x, y, z);
|
||||
|
||||
chunks2[i] = chunk;
|
||||
|
||||
|
@ -157,21 +92,21 @@ struct chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
|||
return chunk;
|
||||
}
|
||||
|
||||
void purge_chunks(int ci) {
|
||||
void gen_init() {
|
||||
if (chunks == NULL) {
|
||||
chunks = calloc(CHUNK_ALL, sizeof(struct chunk));
|
||||
}
|
||||
}
|
||||
|
||||
void free_chunks() { free(chunks); }
|
||||
void gen_free() { free(chunks); }
|
||||
|
||||
int cube_exists(int x, int y, int z, struct chunk dat, int ci) {
|
||||
int gen_cube(int x, int y, int z, struct chunk dat, int ci) {
|
||||
if (x < 0 || y < 0 || z < 0)
|
||||
return 0;
|
||||
|
||||
if (dat.exists == 0 || dat.x != (x / CHUNK_LENGTH) ||
|
||||
dat.y != (y / CHUNK_LENGTH) || dat.z != (z / CHUNK_LENGTH)) {
|
||||
dat = fetch_chunk((x / CHUNK_LENGTH), (y / CHUNK_LENGTH),
|
||||
dat = gen_chunk((x / CHUNK_LENGTH), (y / CHUNK_LENGTH),
|
||||
(z / CHUNK_LENGTH), ci, dat.exists);
|
||||
prevExists = dat.exists;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue