Maze generation!

This commit is contained in:
Carlos Sanchez 2024-08-16 04:30:28 -04:00
parent 171fde56a7
commit a4f0540210
2 changed files with 79 additions and 9 deletions

@ -1 +1 @@
Subproject commit 11f20ce5e2b3053ce777151928500be797f3893f
Subproject commit 775397220d63a5f92ad1ad16c0409c515ce13c30

86
maze.c
View File

@ -34,6 +34,79 @@
#define MAZESIZE 31
#define HSCALE 2.0
// Generate a (square) maze
void gen_maze(haloo3d_obj *mazewalls, int size) {
uint8_t *mazevisit;
int *mazestack;
mallocordie(mazevisit, sizeof(uint8_t) * size * size);
mallocordie(mazestack, sizeof(int) * size * size);
memset(mazevisit, 0, size * size);
for (int i = 0; i < size * size; i++) {
mazestack[i] = -1;
}
// Push current cell onto stack, mark as visited
int x = size / 2;
int y = size / 2;
mazevisit[x + y * size] = 1;
mazestack[0] = x + y * size;
int mazetop = 1;
struct vec2i visitable[4];
int visitmax = 0;
// Now let's make a maze!
while (mazetop) {
mazetop--;
visitmax = 0;
x = mazestack[mazetop] % size;
y = mazestack[mazetop] / size;
if (x > 0 && !mazevisit[x - 1 + y * size]) {
visitable[visitmax].x = -1;
visitable[visitmax++].y = 0;
}
if (x < size - 1 && !mazevisit[x + 1 + y * size]) {
visitable[visitmax].x = 1;
visitable[visitmax++].y = 0;
}
if (y > 0 && !mazevisit[x + (y - 1) * size]) {
visitable[visitmax].x = 0;
visitable[visitmax++].y = -1;
}
if (y < size - 1 && !mazevisit[x + (y + 1) * size]) {
visitable[visitmax].x = 0;
visitable[visitmax++].y = 1;
}
// You can generate a random location!
if (visitmax) {
mazetop++; // mazestack already has our location
struct vec2i movedir = visitable[rand() % visitmax];
int facebase = 4 * (x + y * size);
// Faces are... somewhere.
haloo3d_vertexi *face; // This is actually 6 vertices, the quad
// Only y == -21 moves in the direction of facebase
if (movedir.y == 1) {
facebase += 4 * size; // backwards?
} else if (movedir.x == -1) {
facebase += 2;
} else if (movedir.x == 1) {
facebase += 6;
}
face = mazewalls->faces[facebase];
for (int i = 0; i < 6; i++) {
face[i].posi = 0; // this should make the triangles degenerate
}
int nx = x + movedir.x;
int ny = y + movedir.y;
// Push onto stack and set visited
mazestack[mazetop++] = nx + ny * size;
mazevisit[nx + ny * size] = 1;
}
}
free(mazevisit);
free(mazestack);
}
int main() { // int argc, char **argv) {
haloo3d_easystore storage;
@ -69,6 +142,7 @@ int main() { // int argc, char **argv) {
haloo3d_gen_plane(flooro, MAZESIZE);
haloo3d_gen_plane(ceilo, MAZESIZE);
haloo3d_gen_grid(wallo, MAZESIZE);
gen_maze(wallo, MAZESIZE);
uint16_t cols[4] = {0xFD93, 0xFB83, 0xFEEE, 0xFDDD};
haloo3d_fb_init_tex(floort, 64, 64);
@ -107,7 +181,7 @@ int main() { // int argc, char **argv) {
vec3(floori->scale.v, HSCALE, 1, HSCALE);
vec3(ceili->scale.v, HSCALE, 1, HSCALE);
vec3(walli->scale.v, HSCALE, 0, HSCALE);
ceili->pos.y = 1;
ceili->pos.y = 1; //-1;
eprintf("Setup all object instances\n");
unigi_type_event event;
@ -126,6 +200,9 @@ int main() { // int argc, char **argv) {
unigi_graphics_init();
unigi_window_create(res, "maze.exe"); // render.printbuf);
// render.camera.pos.y = 5;
// render.camera.pitch = 2.2;
// -----------------------------------
// Actual rendering
// -----------------------------------
@ -137,13 +214,6 @@ int main() { // int argc, char **argv) {
render.camera.yaw += 0.008;
haloo3d_easyrender_beginframe(&render);
haloo3d_fb_clear(&render.window, 0xF000);
// memset(render.window.buffer, 0,
// sizeof(uint16_t) * render.window.width * render.window.height);
// for (int j = 0; j < 100; j++) {
// ceilt->buffer[rand() % (ceilt->width * ceilt->height)] =
// 0xF000 | (rand() & 0xFFF);
// }
walli->scale.y = fabs(sin(3 * render.camera.yaw));
// render.camera.up.x = sin(render.camera.yaw);