added infinite room generation

This commit is contained in:
Alexandre 2024-12-31 11:57:12 +01:00
parent ce8563cc94
commit 1ebbd622b5
15 changed files with 186 additions and 41 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -119,19 +119,19 @@ bool str_equal(char* s1, char* s2) {
// ------------------------------------------------------------------------------------------------ //
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
cube_0 cb ;
cb.red = r ;
cb.green = g ;
cb.blue = b ;
cb.x = x ;
cb.y = y ;
cb.z = z ;
cb.w = w ;
cb.h = h ;
cb.d = d ;
cb.hz_angle = hz_a ;
cb.vt_angle = vt_a ;
return cb ;
cube_0* cb = malloc(sizeof(cube_0)) ;
cb->red = r ;
cb->green = g ;
cb->blue = b ;
cb->x = x ;
cb->y = y ;
cb->z = z ;
cb->w = w ;
cb->h = h ;
cb->d = d ;
cb->hz_angle = hz_a ;
cb->vt_angle = vt_a ;
return *cb ;
}
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b) {
@ -144,6 +144,20 @@ void free_cube(cube c) {
free(c) ;
}
teleporter create_teleporter(
double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b,
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
) {
teleporter* tp = malloc(sizeof(teleporter)) ;
tp->dest_chx = chx_dest ;
tp->dest_chy = chy_dest ;
tp->dest_x = x_dest ;
tp->dest_y = y_dest ;
tp->dest_z = z_dest ;
tp->hitbox = create_cube_0(x, y, z, w, h, d, hz_a, vt_a, r, g, b);
return *tp ;
}
// ------------------------------------------------------------------------------------------------ //
double convex_pt(double a, double b, double theta) {
@ -198,7 +212,7 @@ double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c) {
double zrx = zry*cos(c.vt_angle) + yry*sin(c.vt_angle) ;
// now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center
return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w/2.0, c.h/2.0, c.d/2.0) ;
return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d) ;
}
double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) {

View File

@ -18,6 +18,11 @@ bool str_equal(char* s1, char* s2);
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b);
void free_cube(cube c);
teleporter create_teleporter(
double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b,
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
);
double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez);

View File

@ -294,10 +294,25 @@ void drawOutlineOfCube_0(SDL_Renderer* renderer, cube_0 c) {
drawSegmentRotated(renderer, c.x + c.w, c.y + c.h, c.z, c.x + c.w, c.y + c.h, c.z + c.d, c.hz_angle, c.vt_angle, c.x + c.w/2.0, c.y + c.h/2.0, c.z + c.d/2.0) ;
}
void drawCurrentRoom(SDL_Renderer* renderer) {
for(int k = 0; k < current_room->map_size; k++) {
drawOutlineOfCube_0(renderer, current_room->map[k]);
}
for(int k = 0; k < current_room->tps_size; k++) {
drawOutlineOfCube_0(renderer, current_room->tps[k].hitbox);
}
}
// -------------------------------------------------------------------------------------------------------------------------------- //
void drawData(SDL_Renderer* renderer) {
drawNumberToRenderer(renderer, digits, (int)camx, 10, 10, 75/2, 105/2, 0) ;
drawNumberToRenderer(renderer, digits, (int)camy, 10, 60, 75/2, 105/2, 0) ;
drawNumberToRenderer(renderer, digits, (int)camz, 10, 110, 75/2, 105/2, 0) ;
drawNumberToRenderer(renderer, digits, (int)(rot_hz*180.0/3.14159), 10, 160, 75/2, 105/2, 0) ;
drawNumberToRenderer(renderer, digits, (int)(rot_vt*180.0/3.14159), 10, 210, 75/2, 105/2, 0) ;
drawNumberToRenderer(renderer, digits, player_chx, 310, 10, 75/2, 105/2, 0);
drawNumberToRenderer(renderer, digits, player_chy, 310, 60, 75/2, 105/2, 0);
}

View File

@ -29,6 +29,7 @@ void axialRotation_Z(double* x, double* z, double theta, double cst_x, double cs
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void draw_segment(SDL_Renderer* renderer, double sx, double sy, double sz, double ex, double ey, double ez);
void drawOutlineOfCube_0(SDL_Renderer* renderer, cube_0 c);
void drawCurrentRoom(SDL_Renderer* renderer);
void drawData(SDL_Renderer* renderer);

View File

@ -17,4 +17,53 @@
#include "generation.h"
hashtbl visited ;
room* current_room ;
int player_chx ;
int player_chy ;
void build_chunk_1(int chx, int chy) {
room* new = malloc(sizeof(room));
new->chunk_x = chx ;
new->chunk_y = chy ;
new->map = malloc(sizeof(cube_0)*6);
new->map_size = 6 ;
new->tps = malloc(sizeof(teleporter)*4);
new->tps_size = 4 ;
new->map[0] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
new->map[1] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 3.14159/4.0, 0.0, 255, 255, 255);
new->map[2] = create_cube_0(0.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[3] = create_cube_0(4.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[4] = create_cube_0(0.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[5] = create_cube_0(4.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->tps[0] = create_teleporter(-1.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 255, 0, 0 , chx-1, chy , 2.5, 2.5, 2.5);
new->tps[1] = create_teleporter(2.0, 1.0, -1.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 255, 255, 0, chx , chy+1, 2.5, 2.5, 2.5);
new->tps[2] = create_teleporter(5.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 0, 255, 0 , chx+1, chy , 2.5, 2.5, 2.5);
new->tps[3] = create_teleporter(2.0, 1.0, 5.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 0, 0, 255 , chx , chy-1, 2.5, 2.5, 2.5);
//printf("linked (%d, %d) to :\n", chx, chy);
//printf(" {%d, %d}; {%d, %d}; {%d, %d}; {%d, %d}\n", chx-1, chy, chx, chy+1, chx+1, chy, chx, chy-1);
hashtbl_add(visited, chx, chy, new);
}
void init_hashtbl() {
visited = hashtbl_generate(1789);
build_chunk_1(0, 0);
current_room = hashtbl_find_opt(visited, 0, 0);
player_chx = 0 ;
player_chy = 0 ;
}
void generate_nearby_chunks(int render_dist) {
for(int w = -render_dist; w <= render_dist; w++) {
for(int h = -render_dist; h <= render_dist; h++) {
if(!hashtbl_mem(visited, player_chx + w, player_chy + h)) {
//printf("generating (%d, %d)...\n", player_chx + w, player_chy + h);
build_chunk_1(player_chx + w, player_chy + h);
}
}
}
}

View File

@ -1,6 +1,8 @@
#ifndef GEN_H
#define GEN_H
void build_chunk_1(int chx, int chy);
void init_hashtbl();
void generate_nearby_chunks(int render_dist);
#endif

View File

@ -45,19 +45,17 @@ int main(int argc, char** argv) {
/* -------------------------------------------------------- */
init_csts() ;
init_hashtbl() ;
import_digits(rend) ;
import_letters(rend) ;
cube_0 cb = create_cube_0(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0, 3.14159/4.0, 255, 255, 255) ;
cube_0 cb2 = create_cube_0(1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 0, 255) ;
while(true) {
resetRenderer(rend) ;
SDL_SetRenderDrawColor(rend, 255, 255, 255, 255) ;
playerActions() ;
drawData(rend) ;
drawOutlineOfCube_0(rend, cb) ;
drawOutlineOfCube_0(rend, cb2) ;
//draw_segment(rend, 1.0, 1.0, 1.0, 10.0, 2.0, -1.0) ;
generate_nearby_chunks(1);
drawCurrentRoom(rend);
updateRenderer(rend) ;
usleep(1000000/60) ;

View File

@ -17,9 +17,10 @@
#include "move.h"
// ---------------------------------------------------------------------------------------------------- //
double sensitivity = 0.3 ;
double sensitivity = 0.22 ;
double fov = 90.0 ;
double speed = 1.0 ;
double min_dist = 0.2 ;
// ---------------------------------------------------------------------------------------------------- //
double camx ;
@ -34,14 +35,36 @@ double tan_fov ;
bool has_changed ;
void init_csts() {
camx = 0.0 ;
camy = 0.0 ;
camz = 0.0 ;
camx = 3.0 ;
camy = 3.0 ;
camz = 3.0 ;
rot_hz = 0.0 ;
rot_vt = 0.0 ;
rot_vt = 180.0 ;
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ;
}
bool is_colliding() {
for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ;
if(dist <= min_dist) {
return true ;
}
}
for(int k = 0; k < current_room->tps_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->tps[k].hitbox) ;
if(dist <= min_dist) {
player_chx = current_room->tps[k].dest_chx ;
player_chy = current_room->tps[k].dest_chy ;
camx = current_room->tps[k].dest_x ;
camy = current_room->tps[k].dest_y ;
camz = current_room->tps[k].dest_z ;
current_room = hashtbl_find_opt(visited, player_chx, player_chy);
return true ;
}
}
return false ;
}
void playerActions() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
@ -50,39 +73,69 @@ void playerActions() {
break;
case SDL_MOUSEMOTION:
has_changed = true ;
rot_hz -= sensitivity * event.motion.xrel / 100.0 ;
rot_hz += sensitivity * event.motion.xrel / 100.0 ;
rot_vt -= sensitivity * event.motion.yrel / 100.0 ;
if(rot_hz >= 2*3.141592) {
rot_hz -= 2*3.141592 ;
} else if(rot_hz < 0.0) {
rot_hz += 2*3.141592 ;
}
if(rot_vt >= 2*3.141592) {
rot_vt -= 2*3.141592 ;
} else if(rot_vt < 0.0) {
rot_vt += 2*3.141592 ;
if(rot_vt <= 3.14159/2.0) {
rot_vt = 3.14159/2.0;
} else if(rot_vt >= 3.0*3.14159/2.0) {
rot_vt = 3.0*3.14159/2.0 ;
}
break;
case SDL_KEYDOWN:
has_changed = true ;
switch (event.key.keysym.sym) {
case SDLK_z:
camz += speed*cos(rot_hz)*cos(rot_vt);
camx -= speed*sin(rot_hz)*cos(rot_vt);
camy += speed*sin(rot_vt);
for(int k = 0; k < 10; k++) {
camz += speed*cos(rot_hz)*cos(rot_vt)/10;
camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
camy += speed*sin(rot_vt)/10;
if(is_colliding()) {
camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
camx += speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= speed*sin(rot_vt)/10;
k=11;
}
}
break;
case SDLK_q:
camx -= speed*cos(rot_hz);
camz -= speed*sin(rot_hz);
for(int k = 0; k < 10; k++) {
camx -= speed*cos(rot_hz)/10;
camz -= speed*sin(rot_hz)/10;
if(is_colliding()) {
camx += speed*cos(rot_hz)/10;
camz += speed*sin(rot_hz)/10;
k=11;
}
}
break;
case SDLK_s:
camz -= speed*cos(rot_hz)*cos(rot_vt);
camx += speed*sin(rot_hz)*cos(rot_vt);
camy -= speed*sin(rot_vt);
for(int k = 0; k < 10; k++) {
camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
camx += speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= speed*sin(rot_vt)/10;
if(is_colliding()) {
camz += speed*cos(rot_hz)*cos(rot_vt)/10;
camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
camy += speed*sin(rot_vt)/10;
k=11;
}
}
break;
case SDLK_d:
camx += speed*cos(rot_hz);
camz += speed*sin(rot_hz);
for(int k = 0; k < 10; k++) {
camx += speed*cos(rot_hz)/10;
camz += speed*sin(rot_hz)/10;
if(is_colliding()) {
camx -= speed*cos(rot_hz)/10;
camz -= speed*sin(rot_hz)/10;
k=11;
}
}
break;
case SDLK_t:

View File

@ -26,13 +26,17 @@ typedef struct teleporter {
int dest_chy ;
double dest_x ;
double dest_y ;
double dest_z ;
} teleporter ;
struct room {
// (0, 0, 0) = bottom, left and down
int chunk_x ;
int chunk_y ;
cube* map ;
cube_0* map ;
int map_size ;
teleporter* tps ;
int tps_size ;
} ;
typedef struct room room ;
@ -72,5 +76,9 @@ extern double tan_fov ;
extern bool has_changed ;
extern hashtbl visited ;
extern room* current_room ;
extern int player_chx ;
extern int player_chy ;
#endif