added random room generation with pools + set drawing priorities

This commit is contained in:
Alexandre 2025-01-01 12:13:11 +01:00
parent 6204e6011a
commit 1e98634274
16 changed files with 406 additions and 80 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

@ -37,6 +37,16 @@ int pw(int x, int n) {
return x*pw(x*x, n/2); return x*pw(x*x, n/2);
} }
double pwf(double x, int n) {
if (n<0)
return 0.0;
if (n==0)
return 1.0;
if (n%2==0)
return pwf(x*x, n/2);
return x*pwf(x*x, n/2);
}
int abs(int n) { int abs(int n) {
if(n > 0) { if(n > 0) {
return n; return n;
@ -215,6 +225,29 @@ double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c) {
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) ; 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_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz) {
return (mx*distance_pt_cube_axis(x0, cx, cx+cw)+my*distance_pt_cube_axis(y0, cy, cy+ch)+mz*distance_pt_cube_axis(z0, cz, cz+cd)) ;
}
double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx, double my, double mz, cube_0 c) {
// places the origin at the center of the cube
double x = x0 - (c.x + c.w/2.0) ;
double y = y0 - (c.y + c.h/2.0) ;
double z = z0 - (c.z + c.d/2.0) ;
// rotate the point : y then x
double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ;
double yry = y ;
double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ;
double xrx = xry ;
double yrx = yry*cos(c.vt_angle) - zry*sin(c.vt_angle) ;
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_weighted(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d, mx, my, mz) ;
}
double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) { double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) {
return distance_pt_cube_0_3d(x0, y0, z0, *cb) ; return distance_pt_cube_0_3d(x0, y0, z0, *cb) ;
} }

View File

@ -3,6 +3,7 @@
int ln_baseN(int n, int b); int ln_baseN(int n, int b);
int pw(int x, int n); int pw(int x, int n);
double pwf(double x, int n);
int abs(int n); int abs(int n);
int min(int a, int b); int min(int a, int b);
int max(int a, int b); int max(int a, int b);
@ -33,6 +34,8 @@ double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, do
double distance_pt_cube_axis(double coord, double begin, double end); double distance_pt_cube_axis(double coord, double begin, double end);
double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd); double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd);
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c); double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c);
double distance_pt_cube_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz);
double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx, double my, double mz, cube_0 c);
double distance_pt_cube_3d(double x0, double y0, double z0, cube cb); double distance_pt_cube_3d(double x0, double y0, double z0, cube cb);
void import_digits(SDL_Renderer* renderer); void import_digits(SDL_Renderer* renderer);

View File

@ -20,6 +20,8 @@
int* drawOrder; int* drawOrder;
double draw_constant = 0.4 ;
void init_draworder() { void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ; drawOrder = malloc(sizeof(int)*6) ;
} }
@ -203,17 +205,17 @@ void draw_segment(SDL_Renderer* renderer, double sx, double sy, double sz, doubl
double spx ;double spy ;double spz ;double epx ;double epy ;double epz ; double spx ;double spy ;double spz ;double epx ;double epy ;double epz ;
project_to_camera(sx, sy, sz, &spx, &spy, &spz) ; project_to_camera(sx, sy, sz, &spx, &spy, &spz) ;
project_to_camera(ex, ey, ez, &epx, &epy, &epz) ; project_to_camera(ex, ey, ez, &epx, &epy, &epz) ;
if(spz >= 0.4 && epz >= 0.4) { if(spz >= draw_constant && epz >= draw_constant) {
drawLineWithThiccNoColor(renderer, 1, drawLineWithThiccNoColor(renderer, 1,
(int)(1500.0 * (1.0 + (spx / (1.5 * spz * tan_fov))) / 2.0), (int)(1500.0 * (1.0 + (spx / (1.5 * spz * tan_fov))) / 2.0),
(int)(1500.0 * (1.0 + (epx / (1.5 * epz * tan_fov))) / 2.0), (int)(1500.0 * (1.0 + (epx / (1.5 * epz * tan_fov))) / 2.0),
(int)(1000.0 * (1.0 + (spy / (spz * tan_fov))) / 2.0), (int)(1000.0 * (1.0 + (spy / (spz * tan_fov))) / 2.0),
(int)(1000.0 * (1.0 + (epy / (epz * tan_fov))) / 2.0) (int)(1000.0 * (1.0 + (epy / (epz * tan_fov))) / 2.0)
) ; ) ;
} else if(epz >= 0.4) { } else if(epz >= draw_constant) {
double midx = convex_pt(ex, sx, (epz - 0.4)/(epz - spz)) ; double midx = convex_pt(ex, sx, (epz - draw_constant)/(epz - spz)) ;
double midy = convex_pt(ey, sy, (epz - 0.4)/(epz - spz)) ; double midy = convex_pt(ey, sy, (epz - draw_constant)/(epz - spz)) ;
double midz = convex_pt(ez, sz, (epz - 0.4)/(epz - spz)) ; double midz = convex_pt(ez, sz, (epz - draw_constant)/(epz - spz)) ;
project_to_camera(midx, midy, midz, &spx, &spy, &spz) ; project_to_camera(midx, midy, midz, &spx, &spy, &spz) ;
//printf("* %f\n", spz) ; //printf("* %f\n", spz) ;
drawLineWithThiccNoColor(renderer, 1, drawLineWithThiccNoColor(renderer, 1,
@ -222,10 +224,10 @@ void draw_segment(SDL_Renderer* renderer, double sx, double sy, double sz, doubl
(int)(1000.0 * (1.0 + (spy / (spz * tan_fov))) / 2.0), (int)(1000.0 * (1.0 + (spy / (spz * tan_fov))) / 2.0),
(int)(1000.0 * (1.0 + (epy / (epz * tan_fov))) / 2.0) (int)(1000.0 * (1.0 + (epy / (epz * tan_fov))) / 2.0)
) ; ) ;
} else if(spz >= 0.4) { } else if(spz >= draw_constant) {
double midx = convex_pt(sx, ex, (spz - 0.4)/(spz - epz)) ; double midx = convex_pt(sx, ex, (spz - draw_constant)/(spz - epz)) ;
double midy = convex_pt(sy, ey, (spz - 0.4)/(spz - epz)) ; double midy = convex_pt(sy, ey, (spz - draw_constant)/(spz - epz)) ;
double midz = convex_pt(sz, ez, (spz - 0.4)/(spz - epz)) ; double midz = convex_pt(sz, ez, (spz - draw_constant)/(spz - epz)) ;
project_to_camera(midx, midy, midz, &epx, &epy, &epz) ; project_to_camera(midx, midy, midz, &epx, &epy, &epz) ;
//printf("%f *\n", epz) ; //printf("%f *\n", epz) ;
drawLineWithThiccNoColor(renderer, 1, drawLineWithThiccNoColor(renderer, 1,
@ -397,68 +399,68 @@ void renderTriangle(
project_to_camera(x0, y0, z0, &px0, &py0, &pz0); project_to_camera(x0, y0, z0, &px0, &py0, &pz0);
project_to_camera(x1, y1, z1, &px1, &py1, &pz1); project_to_camera(x1, y1, z1, &px1, &py1, &pz1);
project_to_camera(x2, y2, z2, &px2, &py2, &pz2); project_to_camera(x2, y2, z2, &px2, &py2, &pz2);
if(pz0 >= 0.4 && pz1 >= 0.4 && pz2 >= 0.4) { if(pz0 >= draw_constant && pz1 >= draw_constant && pz2 >= draw_constant) {
const SDL_Vertex vtxs[3] = { const SDL_Vertex vtxs[3] = {
construct_vertex(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, red, green, blue), construct_vertex(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, red, green, blue),
construct_vertex(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, red, green, blue), construct_vertex(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, red, green, blue),
construct_vertex(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, red, green, blue), construct_vertex(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, red, green, blue),
}; };
SDL_RenderGeometry(renderer, NULL, vtxs, 3, NULL, 0); SDL_RenderGeometry(renderer, NULL, vtxs, 3, NULL, 0);
} else if((pz0 >= 0.4) + (pz1 >= 0.4) + (pz2 >= 0.4) == 2) { } else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 2) {
double fpx0; double fpy0; double fpz0; double fpx0; double fpy0; double fpz0;
double fpx1; double fpy1; double fpz1; double fpx1; double fpy1; double fpz1;
double mpx0; double mpy0; double mpz0; double mpx0; double mpy0; double mpz0;
double mpx1; double mpy1; double mpz1; double mpx1; double mpy1; double mpz1;
if(pz0 < 0.4) { if(pz0 < draw_constant) {
// pz1 >= 0.4 and pz2 >+ 0.4 // pz1 >= draw_constant and pz2 >+ draw_constant
fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ; fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ;
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ; fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
// 1-0 segment // 1-0 segment
project_to_camera( project_to_camera(
convex_pt(x1, x0, (pz1 - 0.4)/(pz1 - pz0)), convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(y1, y0, (pz1 - 0.4)/(pz1 - pz0)), convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(z1, z0, (pz1 - 0.4)/(pz1 - pz0)), convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
&mpx0, &mpy0, &mpz0) ; &mpx0, &mpy0, &mpz0) ;
// 0-2 segment // 0-2 segment
project_to_camera( project_to_camera(
convex_pt(x2, x0, (pz2 - 0.4)/(pz2 - pz0)), convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(y2, y0, (pz2 - 0.4)/(pz2 - pz0)), convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(z2, z0, (pz2 - 0.4)/(pz2 - pz0)), convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
&mpx1, &mpy1, &mpz1) ; &mpx1, &mpy1, &mpz1) ;
} else if(pz1 < 0.4) { } else if(pz1 < draw_constant) {
// pz0 >= 0.4 and pz2 >+ 0.4 // pz0 >= draw_constant and pz2 >+ draw_constant
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ; fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ; fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
// 0-1 segment // 0-1 segment
project_to_camera( project_to_camera(
convex_pt(x0, x1, (pz0 - 0.4)/(pz0 - pz1)), convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(y0, y1, (pz0 - 0.4)/(pz0 - pz1)), convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(z0, z1, (pz0 - 0.4)/(pz0 - pz1)), convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
&mpx0, &mpy0, &mpz0) ; &mpx0, &mpy0, &mpz0) ;
// 1-2 segment // 1-2 segment
project_to_camera( project_to_camera(
convex_pt(x2, x1, (pz2 - 0.4)/(pz2 - pz1)), convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(y2, y1, (pz2 - 0.4)/(pz2 - pz1)), convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(z2, z1, (pz2 - 0.4)/(pz2 - pz1)), convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
&mpx1, &mpy1, &mpz1) ; &mpx1, &mpy1, &mpz1) ;
} else if(pz2 < 0.4) { } else if(pz2 < draw_constant) {
// pz1 >= 0.4 and pz0 >+ 0.4 // pz1 >= draw_constant and pz0 >+ draw_constant
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ; fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ; fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ;
// 0-2 segment // 0-2 segment
project_to_camera( project_to_camera(
convex_pt(x0, x2, (pz0 - 0.4)/(pz0 - pz2)), convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(y0, y2, (pz0 - 0.4)/(pz0 - pz2)), convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(z0, z2, (pz0 - 0.4)/(pz0 - pz2)), convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
&mpx0, &mpy0, &mpz0) ; &mpx0, &mpy0, &mpz0) ;
// 1-2 segment // 1-2 segment
project_to_camera( project_to_camera(
convex_pt(x1, x2, (pz1 - 0.4)/(pz1 - pz2)), convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(y1, y2, (pz1 - 0.4)/(pz1 - pz2)), convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(z1, z2, (pz1 - 0.4)/(pz1 - pz2)), convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
&mpx1, &mpy1, &mpz1) ; &mpx1, &mpy1, &mpz1) ;
} }
@ -471,39 +473,39 @@ void renderTriangle(
construct_vertex(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, red, green, blue), construct_vertex(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, red, green, blue),
}; };
SDL_RenderGeometry(renderer, NULL, vtxs, 6, NULL, 0); SDL_RenderGeometry(renderer, NULL, vtxs, 6, NULL, 0);
} else if((pz0 >= 0.4) + (pz1 >= 0.4) + (pz2 >= 0.4) == 1) { } else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 1) {
if(pz0 >= 0.4) { if(pz0 >= draw_constant) {
project_to_camera( project_to_camera(
convex_pt(x0, x1, (pz0 - 0.4)/(pz0 - pz1)), convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(y0, y1, (pz0 - 0.4)/(pz0 - pz1)), convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(z0, z1, (pz0 - 0.4)/(pz0 - pz1)), convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
&px1, &py1, &pz1); &px1, &py1, &pz1);
project_to_camera( project_to_camera(
convex_pt(x0, x2, (pz0 - 0.4)/(pz0 - pz2)), convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(y0, y2, (pz0 - 0.4)/(pz0 - pz2)), convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(z0, z2, (pz0 - 0.4)/(pz0 - pz2)), convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
&px2, &py2, &pz2); &px2, &py2, &pz2);
} else if(pz1 >= 0.4) { } else if(pz1 >= draw_constant) {
project_to_camera( project_to_camera(
convex_pt(x1, x0, (pz1 - 0.4)/(pz1 - pz0)), convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(y1, y0, (pz1 - 0.4)/(pz1 - pz0)), convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(z1, z0, (pz1 - 0.4)/(pz1 - pz0)), convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
&px0, &py0, &pz0); &px0, &py0, &pz0);
project_to_camera( project_to_camera(
convex_pt(x1, x2, (pz1 - 0.4)/(pz1 - pz2)), convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(y1, y2, (pz1 - 0.4)/(pz1 - pz2)), convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(z1, z2, (pz1 - 0.4)/(pz1 - pz2)), convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
&px2, &py2, &pz2); &px2, &py2, &pz2);
} else if(pz2 >= 0.4) { } else if(pz2 >= draw_constant) {
project_to_camera( project_to_camera(
convex_pt(x2, x0, (pz2 - 0.4)/(pz2 - pz0)), convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(y2, y0, (pz2 - 0.4)/(pz2 - pz0)), convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(z2, z0, (pz2 - 0.4)/(pz2 - pz0)), convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
&px0, &py0, &pz0); &px0, &py0, &pz0);
project_to_camera( project_to_camera(
convex_pt(x2, x1, (pz2 - 0.4)/(pz2 - pz1)), convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(y2, y1, (pz2 - 0.4)/(pz2 - pz1)), convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(z2, z1, (pz2 - 0.4)/(pz2 - pz1)), convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
&px1, &py1, &pz1); &px1, &py1, &pz1);
} }
const SDL_Vertex vtxs[3] = { const SDL_Vertex vtxs[3] = {
@ -600,7 +602,7 @@ void insertionSort_cb(cube_0* arr, int len) {
for(int k = 0; k < len; k++) { for(int k = 0; k < len; k++) {
int j = k-1 ; int j = k-1 ;
while(j >= 0) { while(j >= 0) {
if(distance_pt_cube_0_3d(camx, camy, camz, arr[j]) < distance_pt_cube_0_3d(camx, camy, camz, arr[j+1])) { if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j]) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j+1])) {
swap_cb(arr, j, j+1); swap_cb(arr, j, j+1);
j -= 1; j -= 1;
} else { } else {
@ -614,7 +616,7 @@ void insertionSort_tp(teleporter* arr, int len) {
for(int k = 0; k < len; k++) { for(int k = 0; k < len; k++) {
int j = k-1 ; int j = k-1 ;
while(j >= 0) { while(j >= 0) {
if(distance_pt_cube_0_3d(camx, camy, camz, arr[j].hitbox) < distance_pt_cube_0_3d(camx, camy, camz, arr[j+1].hitbox)) { if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j].hitbox) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j+1].hitbox)) {
swap_tp(arr, j, j+1); swap_tp(arr, j, j+1);
j -= 1; j -= 1;
} else { } else {

View File

@ -22,47 +22,271 @@ room* current_room ;
int player_chx ; int player_chx ;
int player_chy ; int player_chy ;
void build_chunk_1(int chx, int chy) { entry* pool ;
int pool_size ;
int total_weight ;
void copy_room(room* src, room* dest, int chx, int chy) {
// considering dest has already been malloc'd
dest->chunk_x = chx ;
dest->chunk_y = chy ;
dest->map = malloc(sizeof(cube_0)*src->map_size);
for(int k = 0; k < src->map_size; k++) {
dest->map[k] = create_cube_0(
src->map[k].x, src->map[k].y, src->map[k].z,
src->map[k].w, src->map[k].h, src->map[k].d,
src->map[k].hz_angle, src->map[k].vt_angle, src->map[k].red, src->map[k].green, src->map[k].blue
);
}
dest->map_size = src->map_size ;
dest->tps = malloc(sizeof(teleporter)*src->tps_size);
for(int k = 0; k < src->tps_size; k++) {
dest->tps[k].hitbox = create_cube_0(
src->tps[k].hitbox.x, src->tps[k].hitbox.y, src->tps[k].hitbox.z,
src->tps[k].hitbox.w, src->tps[k].hitbox.h, src->tps[k].hitbox.d,
src->tps[k].hitbox.hz_angle, src->tps[k].hitbox.vt_angle, src->tps[k].hitbox.red, src->tps[k].hitbox.green, src->tps[k].hitbox.blue
);
dest->tps[k].dest_chx = src->tps[k].dest_chx + chx;
dest->tps[k].dest_chy = src->tps[k].dest_chy + chy;
dest->tps[k].dest_x = src->tps[k].dest_x ;
dest->tps[k].dest_y = src->tps[k].dest_y ;
dest->tps[k].dest_z = src->tps[k].dest_z ;
}
dest->tps_size = src->tps_size;
}
void build_starting_chunk(int chx, int chy) {
room* new = malloc(sizeof(room)); room* new = malloc(sizeof(room));
new->chunk_x = chx ; new->chunk_x = chx ;
new->chunk_y = chy ; new->chunk_y = chy ;
new->map = malloc(sizeof(cube_0)*6); new->map = malloc(sizeof(cube_0)*8);
new->map_size = 6 ; new->map_size = 8 ;
new->tps = malloc(sizeof(teleporter)*4); new->tps = malloc(sizeof(teleporter)*4);
new->tps_size = 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[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/8.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/8.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[2] = 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[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[3] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 3.0*3.14159/8.0, 0.0, 255, 255, 255);
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[4] = create_cube_0(0.0, 1.0, 0.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->map[5] = create_cube_0(4.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[6] = create_cube_0(0.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128);
new->map[7] = 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[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[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[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); 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); hashtbl_add(visited, chx, chy, new);
} }
void init_hashtbl() { void init_hashtbl() {
visited = hashtbl_generate(1789); visited = hashtbl_generate(1789);
build_chunk_1(0, 0); build_starting_chunk(0, 0);
current_room = hashtbl_find_opt(visited, 0, 0); current_room = hashtbl_find_opt(visited, 0, 0);
player_chx = 0 ; player_chx = 0 ;
player_chy = 0 ; player_chy = 0 ;
total_weight = 0 ;
}
void get_number_blocks(int* ret_cubes, int* ret_tps, FILE* ptr) {
*ret_cubes = 0 ;
*ret_tps = 0 ;
bool in_blocks = true ;
char c = fgetc(ptr) ;
while(c != EOF && c != '$') {
//printf("%c", c);
if(c == 'B') {
in_blocks = true ;
} else if(c == 'T') {
in_blocks = false ;
} else if(c == '[') {
if(in_blocks) {
*ret_cubes += 1 ;
} else {
*ret_tps += 1 ;
}
}
c = fgetc(ptr);
}
fclose(ptr);
}
void align_to(FILE* ptr, char ch) {
char c = fgetc(ptr);
while(c != EOF && c != ch) {
c = fgetc(ptr);
}
}
int read_int(FILE* ptr, bool print) {
bool is_reading = false;
int buffer = 0 ;
int sign = 1 ;
char c = fgetc(ptr);
while(c != EOF) {
if(c == '-') {
sign = -1 ;
} else if((int)c >= 48 && (int)c <= 57) {
is_reading = true ;
buffer = 10*buffer + (int)c - 48 ;
} else if(is_reading) {
/*if(print) {
printf("%d ; ", buffer*sign);
}*/
return buffer*sign;
}
c = fgetc(ptr);
}
return buffer*sign ;
}
double read_float(FILE* ptr) {
int ent = read_int(ptr, false);
int frac = read_int(ptr, false);
//printf("%d.%d ; ", ent, frac);
return (ent/abs(ent))*(absf((double)ent) + ((double)frac)/(1.0+(double)ln_baseN((int)frac, 10)));
}
void parse_one_room(int id, char* filename) {
//printf(" parsing %s...", filename);
FILE* ptr = fopen(filename, "r");
pool[id].area = malloc(sizeof(room));
int ncubes ;
int ntps ;
FILE* ptr2 = fopen(filename, "r") ;
get_number_blocks(&ncubes, &ntps, ptr2);
//printf("(%d, %d)\n", ncubes, ntps);
pool[id].area->map = malloc(sizeof(cube_0)*ncubes);
pool[id].area->map_size = ncubes;
pool[id].area->tps = malloc(sizeof(teleporter)*ntps);
pool[id].area->tps_size = ntps;
for(int k = 0; k < ncubes; k++) {
align_to(ptr, '[');
double cx = read_float(ptr);
double cy = read_float(ptr);
double cz = read_float(ptr);
double cw = read_float(ptr);
double ch = read_float(ptr);
double cd = read_float(ptr);
double chz = read_float(ptr);
double cvt = read_float(ptr);
int red = read_int(ptr, true);
int green = read_int(ptr, true);
int blue = read_int(ptr, true);
pool[id].area->map[k] = create_cube_0(cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue);
//printf("\n");
}
for(int k = 0; k < ntps; k++) {
align_to(ptr, '[');
double cx = read_float(ptr);
double cy = read_float(ptr);
double cz = read_float(ptr);
double cw = read_float(ptr);
double ch = read_float(ptr);
double cd = read_float(ptr);
double chz = read_float(ptr);
double cvt = read_float(ptr);
int red = read_int(ptr, true);
int green = read_int(ptr, true);
int blue = read_int(ptr, true);
pool[id].area->tps[k].hitbox = create_cube_0(cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue);
pool[id].area->tps[k].dest_chx = read_int(ptr, true);
pool[id].area->tps[k].dest_chy = read_int(ptr, true);
//printf("\n");
}
// debug
/*for(int k = 0; k < ncubes; k++) {
printf("(%lf, %lf, %lf), (%lf, %lf, %lf), (%lf, %lf), (%d, %d, %d)\n",
pool[id].area->map[k].x,
pool[id].area->map[k].y,
pool[id].area->map[k].z,
pool[id].area->map[k].w,
pool[id].area->map[k].h,
pool[id].area->map[k].d,
pool[id].area->map[k].hz_angle,
pool[id].area->map[k].vt_angle,
pool[id].area->map[k].red,
pool[id].area->map[k].green,
pool[id].area->map[k].blue
);
}*/
pool[id].weight = read_int(ptr, true);
total_weight += pool[id].weight ;
fclose(ptr);
}
void parse_rooms(int n_rooms) {
char* name = malloc(sizeof(char)*19); // 1000 rooms max
name[0] = 't' ;
name[1] = 'e' ;
name[2] = 'm' ;
name[3] = 'p' ;
name[4] = 'l' ;
name[5] = 'a' ;
name[6] = 't' ;
name[7] = 'e' ;
name[8] = 's' ;
name[9] = '/' ;
name[10] = 'r' ;
name[11] = 'o' ;
name[12] = 'o' ;
name[13] = 'm' ;
name[14] = '_' ;
name[15] = '0' ;
name[16] = '\0' ;
name[17] = '\0' ;
name[18] = '\0' ;
pool = malloc(sizeof(entry)*n_rooms);
pool_size = n_rooms ;
printf("Parsing...\n");
for(int k = 0; k < n_rooms; k++) {
if(k < 10) {
name[15] = (char)(k%10 + 48) ;
} else if(k < 100) {
name[15] = (char)((k/10)%10 + 48) ;
name[16] = (char)(k%10 + 48) ;
}
parse_one_room(k, name);
}
printf("\nDone.\n");
free(name);
} }
void generate_nearby_chunks(int render_dist) { void generate_nearby_chunks(int render_dist) {
for(int w = -render_dist; w <= render_dist; w++) { for(int w = -render_dist; w <= render_dist; w++) {
for(int h = -render_dist; h <= render_dist; h++) { for(int h = -render_dist; h <= render_dist; h++) {
if(!hashtbl_mem(visited, player_chx + w, player_chy + h)) { if(!hashtbl_mem(visited, player_chx + w, player_chy + h)) {
//printf("generating (%d, %d)...\n", player_chx + w, player_chy + h); printf("generating (%d, %d)... ", player_chx + w, player_chy + h);
build_chunk_1(player_chx + w, player_chy + h); //build_starting_chunk(player_chx + w, player_chy + h);
int pick = rand()%total_weight ;
int sum = 0 ;
for(int k = 0; k < pool_size; k++) {
sum += pool[k].weight ;
if(pick < sum) {
printf("chose %d\n", k);
room* new = malloc(sizeof(room));
copy_room(pool[k].area, new, player_chx + w, player_chy + h);
hashtbl_add(visited, player_chx + w, player_chy + h, new);
k = pool_size+1;
}
}
} }
} }
} }

View File

@ -1,8 +1,23 @@
#ifndef GEN_H #ifndef GEN_H
#define GEN_H #define GEN_H
void build_chunk_1(int chx, int chy); typedef struct entry {
room* area ;
int weight ;
} entry ;
void copy_room(room* src, room* dest, int chx, int chy) ;
void build_starting_chunk(int chx, int chy) ;
void init_hashtbl() ; void init_hashtbl() ;
void get_number_blocks(int* ret_cubes, int* ret_tps, FILE* ptr) ;
void align_to(FILE* ptr, char ch) ;
int read_int(FILE* ptr, bool print) ;
double read_float(FILE* ptr) ;
void parse_one_room(int id, char* filename) ;
void parse_rooms(int n_rooms) ;
void generate_nearby_chunks(int render_dist) ; void generate_nearby_chunks(int render_dist) ;
#endif #endif

View File

@ -47,6 +47,7 @@ int main(int argc, char** argv) {
init_csts() ; init_csts() ;
init_hashtbl() ; init_hashtbl() ;
init_draworder() ; init_draworder() ;
parse_rooms(2);
import_digits(rend) ; import_digits(rend) ;
import_letters(rend) ; import_letters(rend) ;
while(true) { while(true) {

View File

@ -20,7 +20,7 @@
double sensitivity = 0.22 ; double sensitivity = 0.22 ;
double fov = 90.0 ; double fov = 90.0 ;
double speed = 1.0 ; double speed = 1.0 ;
double min_dist = 0.2 ; double min_dist = 0.5 ;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
double camx ; double camx ;
@ -43,6 +43,19 @@ void init_csts() {
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ; tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ;
} }
void set_player_coords(int old_chx, int old_chy) {
for(int k = 0; k < current_room->tps_size; k++) {
if(current_room->tps[k].dest_chx == old_chx && current_room->tps[k].dest_chy == old_chy) {
if(true) {
camx = current_room->tps[k].hitbox.x + current_room->tps[k].hitbox.w/2.0;
camy = current_room->tps[k].hitbox.y + current_room->tps[k].hitbox.h +1.0;
camz = current_room->tps[k].hitbox.z + current_room->tps[k].hitbox.d/2.0;
}
return ;
}
}
}
bool is_colliding() { bool is_colliding() {
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ; double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ;
@ -53,12 +66,12 @@ bool is_colliding() {
for(int k = 0; k < current_room->tps_size; k++) { 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) ; double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->tps[k].hitbox) ;
if(dist <= min_dist) { if(dist <= min_dist) {
int old_chx = player_chx ;
int old_chy = player_chy ;
player_chx = current_room->tps[k].dest_chx ; player_chx = current_room->tps[k].dest_chx ;
player_chy = current_room->tps[k].dest_chy ; 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); current_room = hashtbl_find_opt(visited, player_chx, player_chy);
set_player_coords(old_chx, old_chy);
return true ; return true ;
} }
} }

View File

@ -22,7 +22,7 @@ typedef cube_0* cube ;
typedef struct teleporter { typedef struct teleporter {
cube_0 hitbox ; cube_0 hitbox ;
int dest_chx ; int dest_chx ; // in the pool, these are offsets
int dest_chy ; int dest_chy ;
double dest_x ; double dest_x ;
double dest_y ; double dest_y ;

18
templates/room_0 Normal file
View File

@ -0,0 +1,18 @@
Blocks :
[0.0, 0.0, 0.0, 10.0, 1.0, 10.0, 0.0, 0.0, 255, 255, 255]
[0.0, 9.0, 0.0, 10.0, 1.0, 10.0, 0.0, 0.0, 255, 255, 255]
[0.0, 1.0, 0.0, 1.0, 8.0, 1.0, 0.0, 0.0, 128, 128, 128]
[9.0, 1.0, 0.0, 1.0, 8.0, 1.0, 0.0, 0.0, 128, 128, 128]
[0.0, 1.0, 9.0, 1.0, 8.0, 1.0, 0.0, 0.0, 128, 128, 128]
[9.0, 1.0, 9.0, 1.0, 8.0, 1.0, 0.0, 0.0, 128, 128, 128]
Teleporters :
[4.0, 1.0, 0.0, 2.0, 4.0, 1.0, 0.0, 0.0, 255, 0, 0; 0, -1]
[0.0, 1.0, 4.0, 1.0, 4.0, 2.0, 0.0, 0.0, 255, 255, 0; -1, 0]
[4.0, 1.0, 9.0, 2.0, 4.0, 1.0, 0.0, 0.0, 0, 255, 0; 0, 1]
[9.0, 1.0, 4.0, 1.0, 4.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Weight :
10
$

17
templates/room_1 Normal file
View File

@ -0,0 +1,17 @@
Blocks :
[-1.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 255, 255]
[-5.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 255, 255]
[-1.0, 0.0, -5.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 255, 255]
[3.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 255, 255]
[-1.0, 0.0, 3.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 255, 255]
Teleporters :
[-1.0, 0.0, -9.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 0, 0; 0, -1]
[-9.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 255, 255, 0; -1, 0]
[-1.0, 0.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 255, 0; 0, 1]
[7.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Weight :
10
$