You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
4.3 KiB
201 lines
4.3 KiB
4 years ago
|
#include "environment.h"
|
||
4 years ago
|
|
||
4 years ago
|
double linear_interpolate(double a0, double a1, double w) {
|
||
|
return (1.0f - w) * a0 + w * a1;
|
||
|
}
|
||
|
|
||
4 years ago
|
int destroy_environment(struct environment *e) {
|
||
|
/*Vect position;*/
|
||
|
/*ArrayList ceil; // doubles*/
|
||
|
/*ArrayList floor; // doubles*/
|
||
|
/*ArrayList bg1; // doubles*/
|
||
|
/*ArrayList bg2; // doubles*/
|
||
|
/*struct colour_pallete colours;*/
|
||
|
/*struct physics_collection physics;*/
|
||
|
|
||
|
arlst_destroy(&e->ceil);
|
||
|
arlst_destroy(&e->floor);
|
||
|
arlst_destroy(&e->bg1);
|
||
|
arlst_destroy(&e->bg2);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
4 years ago
|
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);
|
||
|
}
|
||
|
|
||
4 years ago
|
|
||
|
struct colour_pallete get_pallete (int seed) {
|
||
4 years ago
|
struct colour base = get_random_color(seed);
|
||
4 years ago
|
|
||
|
|
||
|
struct colour c1 = get_hsv(base);
|
||
|
c1 = get_hsv(base);
|
||
|
struct colour_pallete cols;
|
||
|
|
||
|
c1.l = 0.1;
|
||
|
cols.bg = get_rgb(c1);
|
||
|
c1.l += 0.3;
|
||
|
|
||
|
c1.h += 30;
|
||
|
cols.fg1 = get_rgb(c1);
|
||
|
|
||
|
c1.h += 30;
|
||
|
cols.fg2 = get_rgb(c1);
|
||
|
|
||
|
c1.h += 30;
|
||
|
cols.fg3 = get_rgb(c1);
|
||
|
|
||
|
return cols;
|
||
|
|
||
4 years ago
|
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);
|
||
|
}
|
||
|
|
||
4 years ago
|
}
|
||
|
|
||
|
int comp(const void *one, const void *two) {
|
||
4 years ago
|
return *(int*)one >= *(int*)two;
|
||
4 years ago
|
}
|
||
|
|
||
|
struct environment get_scene_at(Vect coordinate, int seed) {
|
||
|
// basic cache for the last room generated
|
||
4 years ago
|
static bool init;
|
||
4 years ago
|
static Vect last_room;
|
||
|
static struct environment e;
|
||
4 years ago
|
static int oldseed;
|
||
4 years ago
|
Vect room_coord;
|
||
4 years ago
|
room_coord.x = (int)coordinate.x - (int)coordinate.x % E_ROOM_WIDTH;
|
||
4 years ago
|
room_coord.y = 0;
|
||
4 years ago
|
|
||
4 years ago
|
if (init && room_coord.x == last_room.x && oldseed == seed) {
|
||
4 years ago
|
return e;
|
||
|
}
|
||
|
last_room = room_coord;
|
||
4 years ago
|
oldseed = seed;
|
||
4 years ago
|
|
||
|
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);
|
||
4 years ago
|
|
||
|
e.colours = get_pallete(seed);
|
||
4 years ago
|
|
||
|
Vect bit;
|
||
|
bit.y = 100 * seed;
|
||
4 years ago
|
Vect pos;
|
||
|
|
||
|
srand(seed);
|
||
|
|
||
|
Vect node;
|
||
|
node.y = 0;
|
||
4 years ago
|
|
||
4 years ago
|
for (int i = 0; i < E_ROOM_WIDTH; i += E_ROOM_RES) {
|
||
4 years ago
|
bit.x = room_coord.x + i;
|
||
4 years ago
|
bit.y = seed;
|
||
|
node.x = bit.x;
|
||
|
|
||
4 years ago
|
int r1 = 1.5*(rand() % 500) - (rand() % 100);
|
||
4 years ago
|
int r2 = 1.5*(rand() % 200) - (rand() % 200);
|
||
|
int r3 = (rand() % 100) - (rand() % 500);
|
||
|
int r4 = 50;
|
||
|
|
||
|
int r[4] = {r1, r2, r3, r4};
|
||
|
qsort(r, 4, sizeof(int), comp);
|
||
4 years ago
|
// r[0] == ceiling
|
||
|
// r[3] == floor
|
||
4 years ago
|
|
||
4 years ago
|
//node.y += r[0];
|
||
|
int h[4] = {node.y,node.y,node.y,node.y};
|
||
|
h[0] += r[0];
|
||
|
h[1] = h[0] + r[1];
|
||
|
h[2] += h[0] + r[2];
|
||
|
h[3] += h[0] + r[3];
|
||
|
qsort(h, 4, sizeof(int), comp);
|
||
4 years ago
|
|
||
|
// node.y = fmod(perlin(bit), 3000);
|
||
|
Vect *z = malloc(sizeof(Vect));
|
||
|
*z = node;
|
||
4 years ago
|
z->y = h[0];
|
||
4 years ago
|
|
||
4 years ago
|
arlst_add(&e.floor, z);
|
||
4 years ago
|
|
||
|
z = malloc(sizeof(Vect));
|
||
|
*z = node;
|
||
4 years ago
|
// z->y += r[1];
|
||
|
z->y = h[1];
|
||
4 years ago
|
arlst_add(&e.bg1, z);
|
||
|
|
||
|
z = malloc(sizeof(Vect));
|
||
|
*z = node;
|
||
4 years ago
|
//z->y += r[2];
|
||
|
z->y = h[2];
|
||
4 years ago
|
arlst_add(&e.bg2, z);
|
||
|
|
||
|
|
||
|
z = malloc(sizeof(Vect));
|
||
|
*z = node;
|
||
4 years ago
|
// z->y += r[3];
|
||
|
z->y = h[3];
|
||
4 years ago
|
arlst_add(&e.ceil, z);
|
||
|
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
Vect *v = arlst_get(&e.floor, 0);
|
||
|
Vect v2 = *(Vect *)arlst_get(&e.ceil, 0);
|
||
|
|
||
|
Vect v3 = vect_add(v2, vect_scalar(vect_add(*v, vect_scalar(v2, -1)) , 0.5));
|
||
|
|
||
|
int d[] = {0, E_ROOM_TILES - 1};
|
||
|
int a;
|
||
|
for (int i = 0; i <= 1; i++) {
|
||
|
a = d[i];
|
||
|
v = arlst_get(&e.floor, a);
|
||
|
v->y = v3.x;
|
||
|
v = arlst_get(&e.ceil, a);
|
||
|
v->y = v3.x;
|
||
|
v = arlst_get(&e.bg1, a);
|
||
|
v->y = v3.x;
|
||
|
v = arlst_get(&e.bg2, a);
|
||
|
v->y = v3.x;
|
||
|
}
|
||
|
|
||
|
// join the end and start together
|
||
|
|
||
|
|
||
|
|
||
4 years ago
|
init = true;
|
||
4 years ago
|
|
||
|
return e;
|
||
|
}
|