polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 6d083f7766ee054fcd6e7f04e5e613200d498ab5
parent 52add751b7654133ac6edb29757ab37b1987ff02
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 21 Jul 2019 15:08:54 -0700

terrain: make terrain actually random

Diffstat:
Msrc/perlin.c | 20+++++++++-----------
Msrc/perlin.h | 2+-
Msrc/terrain.c | 7++++---
Msrc/terrain.h | 17+++++++++--------
4 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/perlin.c b/src/perlin.c @@ -1,7 +1,5 @@ #include <stdio.h> -static int SEED = 0; - static int hash[] = { 208,34,231,213,32,248,233,56,161,78,24,140,71,48,140,254,245,255,247,247,40, 185,248,251,245,28,124,204,204,76,36,1,107,28,234,163,202,224,245,128,167,204, @@ -16,8 +14,8 @@ static int hash[] = { 135,176,183,191,253,115,184,21,233,58,129,233,142,39,128,211,118,137,139,255, 114,20,218,113,154,27,127,246,250,1,8,198,250,209,92,222,173,21,88,102,219}; -int noise2(int x, int y) { - int tmp = hash[(y + SEED) % 256]; +int noise2(int seed, int x, int y) { + int tmp = hash[(y + seed) % 256]; return hash[(tmp + x) % 256]; } @@ -29,21 +27,21 @@ float smooth_inter(float x, float y, float s) { return lin_inter(x, y, s * s * (3-2*s)); } -float noise2d(float x, float y) { +float noise2d(int seed, float x, float y) { int x_int = x; int y_int = y; float x_frac = x - x_int; float y_frac = y - y_int; - int s = noise2(x_int, y_int); - int t = noise2(x_int+1, y_int); - int u = noise2(x_int, y_int+1); - int v = noise2(x_int+1, y_int+1); + int s = noise2(seed, x_int, y_int); + int t = noise2(seed, x_int+1, y_int); + int u = noise2(seed, x_int, y_int+1); + int v = noise2(seed, x_int+1, y_int+1); float low = smooth_inter(s, t, x_frac); float high = smooth_inter(u, v, x_frac); return smooth_inter(low, high, y_frac); } -float perlin2d(float x, float y, float freq, int depth) { +float perlin2d(int seed, float x, float y, float freq, int depth) { float xa = x*freq; float ya = y*freq; float amp = 1.0; @@ -54,7 +52,7 @@ float perlin2d(float x, float y, float freq, int depth) { for(i=0; i<depth; i++) { div += 256 * amp; - fin += noise2d(xa, ya) * amp; + fin += noise2d(seed, xa, ya) * amp; amp /= 2; xa *= 2; ya *= 2; diff --git a/src/perlin.h b/src/perlin.h @@ -2,6 +2,6 @@ #ifndef POLYADVENT_PERLIN_H #define POLYADVENT_PERLIN_H -float perlin2d(float x, float y, float freq, int depth); +float perlin2d(int seed, float x, float y, float freq, int depth); #endif /* POLYADVENT_PERLIN_H */ diff --git a/src/terrain.c b/src/terrain.c @@ -25,9 +25,9 @@ double old_noisy_boi(struct terrain *t, double x, double y) { struct perlin_settings *s = &t->settings; /* x *= s->scale; */ /* y *= s->scale; */ - double e = perlin2d(x, y, s->freq, s->depth) - + s->o1s * perlin2d(s->o1 * x, s->o1 * y, s->freq, s->depth) - + s->o2s * perlin2d(s->o2 * x, s->o2 * y, s->freq, s->depth); + double e = perlin2d(t->settings.seed, x, y, s->freq, s->depth) + + s->o1s * perlin2d(t->settings.seed, s->o1 * x, s->o1 * y, s->freq, s->depth) + + s->o2s * perlin2d(t->settings.seed, s->o2 * x, s->o2 * y, s->freq, s->depth); return pow(e, s->exp) * s->amplitude; } @@ -110,6 +110,7 @@ void gen_terrain_samples(struct terrain *terrain, float scale) { void create_terrain(struct terrain *terrain, float scale, int seed) { u32 i; const double size = terrain->size; + terrain->settings.seed = seed; float tmp1[3], tmp2[3]; if (!terrain->n_samples) { diff --git a/src/terrain.h b/src/terrain.h @@ -9,14 +9,15 @@ struct point; struct perlin_settings { - double freq; - int depth; - double amplitude; - double ox, oy; - double o1, o1s; - double o2, o2s; - double scale; - double exp; + double freq; + int depth; + int seed; + double amplitude; + double ox, oy; + double o1, o1s; + double o2, o2s; + double scale; + double exp; };