1
1
Fork 0
Browse Source

environment basic stuff untested

thread-physics
alistair 4 years ago
parent
commit
45bd157edb
  1. 102
      environment.c

102
environment.c

@ -0,0 +1,102 @@ @@ -0,0 +1,102 @@
#include "c-colours/colours.h"
#include "datastructures/datatypes.h"
#include "vect.h"
#define ROOM_WIDTH 2000
enum {
E_ROOM_WIDTH = 2000,
E_ROOM_RES = 30,
E_ROOM_TILES = E_ROOM_WIDTH / E_ROOM_RES,
};
struct environment {
int level;
Vect position;
ArrayList ceil;
ArrayList floor;
ArrayList bg1;
ArrayList bg2;
ArrayList colours;
};
double linear_interpolate(double a0, double a1, double w) {
return (1.0f - w) * a0 + w * a1;
}
double perlin(Vect coordinate) {
Vect a = coordinate;
Vect b = coordinate;
a.x = (int)a.x;
a.y = (int)a.y;
b.x = a.x + 1;
b.y = a.y + 1;
double sx = coordinate.x - a.x;
double sy = coordinate.y - a.y;
Vect d = b;
d.y = a.y;
double n0 = vect_dot(a, coordinate);
double n1 = vect_dot(d, coordinate);
double ix0 = linear_interpolate(n0, n1, sx);
Vect c;
c.x = a.x;
c.y = b.y;
n0 = vect_dot(c, coordinate);
n1 = vect_dot(b, coordinate);
double ix1 = linear_interpolate(n0, n1, sy);
return linear_interpolate(ix0, ix1, sy);
}
ArrayList get_colours(int seed) {
struct colour base = get_random_color(seed);
struct colour *adj = get_adjacent(base, 20, 4);
ArrayList l = new_arlst_wfree(4, free);
for (int i = 0; i < 4; i++) {
arlst_add(&l, adj + i);
}
return l;
}
struct environment get_scene_at(Vect coordinate, int seed) {
// basic cache for the last room generated
static Vect last_room;
static struct environment e;
Vect room_coord;
room_coord.x = (int)coordinate.x - (int)coordinate.x % ROOM_WIDTH;
room_coord.y = 0;
if (room_coord.x == last_room.x) {
return e;
}
last_room = room_coord;
e.floor = new_arlst_wfree(E_ROOM_TILES, free);
e.ceil = new_arlst_wfree(E_ROOM_TILES, free);
e.bg1 = new_arlst_wfree(E_ROOM_TILES, free);
e.bg2 = new_arlst_wfree(E_ROOM_TILES, free);
e.colours = get_colours(seed);
Vect bit;
bit.y = 100 * seed;
for (int i = 0; i < E_ROOM_TILES; i += E_ROOM_RES) {
bit.x = room_coord.x + i;
double *z = malloc(sizeof(double));
*z = perlin(bit);
arlst_add(&e.floor, z);
arlst_add(&e.ceil, z + 90);
arlst_add(&e.bg1, z + 20);
arlst_add(&e.bg2, z + 70);
}
return e;
}
Loading…
Cancel
Save