voxel-test/cubes.c
2024-07-20 22:00:39 -04:00

294 lines
No EOL
7.1 KiB
C

// the messiest part of this whole codebase
#include "cubes.h"
#include "gen.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct args {
struct v3f *cube;
struct v3f *text;
struct v3i pos;
int a;
int i;
int a3;
int *i2;
};
int ox = -1024;
int oy = -1024;
int oz = -1024;
struct chunk *chunk_old = NULL;
void cubes_rect(struct v3f *cube, int *i, double x1, double x2, double y1,
double y2, int is_text, int text_x, int text_y) {
struct v3f a2 = {{x1, y1, 0}};
if (is_text) {
a2.pos[0] = TEXT_GAP_H * text_x;
a2.pos[1] = TEXT_GAP_V * text_y;
a2.pos[2] = 0;
}
struct v3f b = a2;
struct v3f c = a2;
struct v3f d = a2;
if (is_text) {
b.pos[0] += TEXT_GAP_H;
c.pos[1] += TEXT_GAP_V;
d.pos[0] += TEXT_GAP_H;
d.pos[1] += TEXT_GAP_V;
} else {
b.pos[0] = x2;
c.pos[1] = y2;
d.pos[0] = x2;
d.pos[1] = y2;
}
cube[*i] = a2;
cube[*i + 1] = b;
cube[*i + 2] = c;
cube[*i + 3] = c;
cube[*i + 4] = b;
cube[*i + 5] = d;
}
void cubes_face(struct v3f *cube, int i, int x, int y, int z, int x2, int y2,
int z2, int *j, int is_text, struct chunk dat, int ci, int a3) {
struct v3i a = {{x, y, z}};
struct v3f a2 = {{x, y, z}};
int exists = 0;
if (is_text) {
a.pos[i]--;
int type = gen_cube(x, y, z, dat, ci, -1);
int typeB = gen_cube(a.pos[0], a.pos[1], a.pos[2], dat, ci, -1);
exists = ((!!type) != (!!typeB));
a2.pos[0] = exists ? TEXT_GAP_H * (type ? (type - 1) : (typeB - 1)) : -1.0;
a2.pos[1] = exists ? TEXT_GAP_V * (i + (!!type) * 3) : -1.0;
a2.pos[2] = 0;
}
struct v3f b = a2;
struct v3f c = a2;
struct v3f d = a2;
if (is_text) {
b.pos[0] += TEXT_GAP_H;
c.pos[1] += TEXT_GAP_V;
d.pos[0] += TEXT_GAP_H;
d.pos[1] += TEXT_GAP_V;
} else {
b.pos[(i == 0) ? 1 : 0]++;
c.pos[(i == 2) ? 1 : 2]++;
d.pos[(i == 0) ? 1 : 0]++;
d.pos[(i == 2) ? 1 : 2]++;
}
cube[*j] = a2;
cube[*j + 1] = b;
cube[*j + 2] = c;
cube[*j + 3] = c;
cube[*j + 4] = b;
cube[*j + 5] = d;
if (is_text && !exists) {
*j = (*j - 6 * a3);
return;
}
}
void *cubes_chunk(void *args) {
struct args args2 = ((struct args *)args)[0];
int i = args2.i;
int a3 = args2.a3;
int a2 = args2.a;
int a = a2 % a3;
int *i2 = args2.i2;
int x2 = args2.pos.pos[0];
int y2 = args2.pos.pos[1];
int z2 = args2.pos.pos[2];
struct v3f *cube = args2.cube;
struct v3f *text = args2.text;
struct v3f vNul = {{-1.0, -1.0, -1.0}};
if (x2 < 0 || y2 < 0 || z2 < 0) {
return 0;
}
// int is_text = args2.is_text;
int the_j = a2;
struct chunk chunk =
gen_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH, z2 / CHUNK_LENGTH, a, 0);
if (chunk.exists != 0 && the_j > -1)
chunk_old[the_j] = chunk;
printf("Loading world... (%i / %i)\n", a2, CHUNK_ALL);
if (i > CBLOCK_ALL * 18) {
i2[a2] = i;
return (void *)i;
}
for (int y4 = 0; y4 < CHUNK_LENGTH; y4++) {
for (int x4 = 0; x4 < CHUNK_LENGTH; x4++) {
for (int z4 = 0; z4 < CHUNK_LENGTH; z4++) {
int y3 = y2 + y4;
int x3 = x2 + x4;
int z3 = z2 + z4;
for (int m = 0; m < 3; m++) {
cubes_face(cube, m, x3, y3, z3, x4, y4, z4, &i, 0, chunk, a, a3);
cubes_face(text, m, x3, y3, z3, x4, y4, z4, &i, 1, chunk, a, a3);
i += 6 * a3;
if (i > CBLOCK_ALL * 18) {
i2[a2] = i;
return (void *)i;
}
}
}
}
}
i2[a2] = i;
return (void *)i;
}
void cubes_free() { free(chunk_old); }
void cubes_refresh(int x, int y, int z, struct chunk dat, int state) {
for (int i = 0; i < CHUNK_ALL; i++) {
if (chunk_old[i].x == x && chunk_old[i].y == y && chunk_old[i].z == z && chunk_old[i].exists != 0) {
chunk_old[i].exists = state;
break;
}
}
if (state == 0)
gen_save(dat);
}
int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
struct v3f *textO, int x, int y, int z, int is_purge, int *i2) {
int i = 0;
x *= CHUNK_LENGTH;
y *= CHUNK_LENGTH;
z *= CHUNK_LENGTH;
int a3 = CHUNK_DIAMETER_H;
int a = 0;
struct args args[a3];
int thread_return[a3];
if (is_purge) {
gen_init();
}
if (!chunk_old) {
chunk_old = calloc((CHUNK_ALL), sizeof(struct chunk));
}
pthread_t thread_id[a3];
int j = 0;
struct v3f vNul = {{-1.0, -1.0, -1.0}};
for (int i = 0; i < CBLOCK_ALL * 18; i++) {
cubeO[i] = vNul;
textO[i] = vNul;
}
for (int y2 = y - (CHUNK_RADIUS_V)*CHUNK_LENGTH;
y2 <= y + (CHUNK_RADIUS_V)*CHUNK_LENGTH; y2 += CHUNK_LENGTH) {
for (int x2 = x - (CHUNK_RADIUS_H)*CHUNK_LENGTH;
x2 <= x + (CHUNK_RADIUS_H)*CHUNK_LENGTH; x2 += CHUNK_LENGTH) {
for (int z2 = z - (CHUNK_RADIUS_H)*CHUNK_LENGTH;
z2 <= z + (CHUNK_RADIUS_H)*CHUNK_LENGTH; z2 += CHUNK_LENGTH) {
struct v3i pos = {
{x - (((x2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) *
CHUNK_LENGTH,
y - (((y2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_V)) - CHUNK_RADIUS_V) *
CHUNK_LENGTH,
z - (((z2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) *
CHUNK_LENGTH}};
int a2 = a % a3;
args[a2].cube = cube;
args[a2].text = text;
args[a2].pos = pos;
// args[a2].is_text = is_text;
args[a2].a = a;
args[a2].i = i;
args[a2].a3 = a3;
args[a2].i2 = i2;
/*gen_chunk((void *)&args); */
int ib = i;
struct chunk old_chunk = chunk_old[a];
int cached =
(old_chunk.x == pos.pos[0] / CHUNK_LENGTH &&
old_chunk.y == pos.pos[1] / CHUNK_LENGTH &&
old_chunk.z == pos.pos[2] / CHUNK_LENGTH && old_chunk.exists != 0 && old_chunk.exists != 255);
if (cached) {
thread_id[a2] = -1;
ib = i2[a];
} else if (pos.pos[0] < 0 || pos.pos[1] < 0 || pos.pos[2] < 0) {
i2[a] = -6;
thread_id[a2] = -1;
} else {
pthread_create(&(thread_id[a2]), NULL, cubes_chunk,
(void *)&(args[a2]));
}
i += 6;
a++;
}
for (int k = 0; k < a3; k++) {
if (thread_id[k] == -1) {
continue;
}
pthread_join((thread_id[k]), (void **)&thread_return[k]);
}
int max = 0;
int min = 0xFFFFFFF;
for (int a9 = 0; a9 < a3; a9++) {
int ii = a9 + a - CHUNK_DIAMETER_H;
/*for (int a10 = i2[ii];
a10 < (BLOCK_ALL * 18) *
((ii / CHUNK_DIAMETER_H + 1) * CHUNK_DIAMETER_H) && a10 <
CBLOCK_ALL * 18; a10 += 6 * a3) { cube[a10] = vNul; text[a10] = vNul;
}*/
max = (max > i2[ii]) ? max : i2[ii];
min = (min < i2[ii]) ? min : i2[ii];
}
if (max > i - 6 * a3) {
memcpy(&(cubeO[j]), &(cube[i - 6 * a3]),
(max - (i - 6 * a3)) * sizeof(struct v3f));
memcpy(&(textO[j]), &(text[i - 6 * a3]),
(max - (i - 6 * a3)) * sizeof(struct v3f));
j += max - (i - 6 * a3);
}
i = (BLOCK_ALL * 18) * a; // i = max + 6;
}
}
ox = x;
oy = y;
oz = z;
return j; // i;
}