stream world data from pxl8d to pxl8
This commit is contained in:
parent
39ee0fefb7
commit
a71a9840b2
55 changed files with 5290 additions and 2131 deletions
|
|
@ -1,413 +1,125 @@
|
|||
#include "pxl8_world.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pxl8_bsp.h"
|
||||
#include "pxl8_gen.h"
|
||||
#include "pxl8_log.h"
|
||||
#include "pxl8_math.h"
|
||||
#include "pxl8_mem.h"
|
||||
|
||||
#define PXL8_WORLD_ENTITY_CAPACITY 256
|
||||
|
||||
struct pxl8_world {
|
||||
pxl8_bsp bsp;
|
||||
bool loaded;
|
||||
pxl8_chunk* active_chunk;
|
||||
pxl8_block_registry* block_registry;
|
||||
pxl8_chunk_cache* chunk_cache;
|
||||
pxl8_entity_pool* entities;
|
||||
};
|
||||
|
||||
pxl8_world* pxl8_world_create(void) {
|
||||
pxl8_world* world = (pxl8_world*)pxl8_calloc(1, sizeof(pxl8_world));
|
||||
if (!world) {
|
||||
pxl8_error("Failed to allocate world");
|
||||
pxl8_world* world = pxl8_calloc(1, sizeof(pxl8_world));
|
||||
if (!world) return NULL;
|
||||
|
||||
world->block_registry = pxl8_block_registry_create();
|
||||
world->chunk_cache = pxl8_chunk_cache_create();
|
||||
world->entities = pxl8_entity_pool_create(PXL8_WORLD_ENTITY_CAPACITY);
|
||||
|
||||
if (!world->block_registry || !world->chunk_cache || !world->entities) {
|
||||
pxl8_world_destroy(world);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
world->loaded = false;
|
||||
|
||||
return world;
|
||||
}
|
||||
|
||||
void pxl8_world_destroy(pxl8_world* world) {
|
||||
if (!world) return;
|
||||
|
||||
if (world->loaded) {
|
||||
pxl8_bsp_destroy(&world->bsp);
|
||||
}
|
||||
|
||||
pxl8_block_registry_destroy(world->block_registry);
|
||||
pxl8_chunk_cache_destroy(world->chunk_cache);
|
||||
pxl8_entity_pool_destroy(world->entities);
|
||||
pxl8_free(world);
|
||||
}
|
||||
|
||||
pxl8_result pxl8_world_generate(pxl8_world* world, pxl8_gfx* gfx, const pxl8_procgen_params* params) {
|
||||
if (!world || !gfx || !params) {
|
||||
pxl8_error("Invalid arguments to pxl8_world_generate");
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (world->loaded) {
|
||||
pxl8_bsp_destroy(&world->bsp);
|
||||
world->loaded = false;
|
||||
}
|
||||
|
||||
memset(&world->bsp, 0, sizeof(pxl8_bsp));
|
||||
|
||||
pxl8_result result = pxl8_procgen(&world->bsp, params);
|
||||
if (result != PXL8_OK) {
|
||||
pxl8_error("Failed to generate world: %d", result);
|
||||
pxl8_bsp_destroy(&world->bsp);
|
||||
return result;
|
||||
}
|
||||
|
||||
world->loaded = true;
|
||||
return PXL8_OK;
|
||||
pxl8_chunk_cache* pxl8_world_chunk_cache(pxl8_world* world) {
|
||||
if (!world) return NULL;
|
||||
return world->chunk_cache;
|
||||
}
|
||||
|
||||
pxl8_result pxl8_world_load(pxl8_world* world, const char* path) {
|
||||
if (!world || !path) return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
|
||||
if (world->loaded) {
|
||||
pxl8_bsp_destroy(&world->bsp);
|
||||
world->loaded = false;
|
||||
}
|
||||
|
||||
memset(&world->bsp, 0, sizeof(pxl8_bsp));
|
||||
|
||||
pxl8_result result = pxl8_bsp_load(path, &world->bsp);
|
||||
if (result != PXL8_OK) {
|
||||
pxl8_error("Failed to load world: %s", path);
|
||||
return result;
|
||||
}
|
||||
|
||||
world->loaded = true;
|
||||
pxl8_info("Loaded world: %s", path);
|
||||
|
||||
return PXL8_OK;
|
||||
pxl8_chunk* pxl8_world_active_chunk(pxl8_world* world) {
|
||||
if (!world) return NULL;
|
||||
return world->active_chunk;
|
||||
}
|
||||
|
||||
void pxl8_world_unload(pxl8_world* world) {
|
||||
if (!world || !world->loaded) return;
|
||||
|
||||
pxl8_bsp_destroy(&world->bsp);
|
||||
world->loaded = false;
|
||||
void pxl8_world_set_active_chunk(pxl8_world* world, pxl8_chunk* chunk) {
|
||||
if (!world) return;
|
||||
world->active_chunk = chunk;
|
||||
}
|
||||
|
||||
pxl8_result pxl8_world_apply_textures(pxl8_world* world, const pxl8_world_texture* textures, u32 count) {
|
||||
if (!world || !world->loaded || !textures || count == 0) {
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
pxl8_block_registry* pxl8_world_block_registry(pxl8_world* world) {
|
||||
if (!world) return NULL;
|
||||
return world->block_registry;
|
||||
}
|
||||
|
||||
pxl8_bsp* bsp = &world->bsp;
|
||||
pxl8_entity_pool* pxl8_world_entities(pxl8_world* world) {
|
||||
if (!world) return NULL;
|
||||
return world->entities;
|
||||
}
|
||||
|
||||
u32 max_materials = count * 6;
|
||||
bsp->materials = pxl8_calloc(max_materials, sizeof(pxl8_gfx_material));
|
||||
if (!bsp->materials) {
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
bsp->num_materials = 0;
|
||||
|
||||
for (u32 face_idx = 0; face_idx < bsp->num_faces; face_idx++) {
|
||||
pxl8_bsp_face* face = &bsp->faces[face_idx];
|
||||
pxl8_vec3 normal = bsp->planes[face->plane_id].normal;
|
||||
|
||||
u32 matched_texture_idx = count;
|
||||
for (u32 tex_idx = 0; tex_idx < count; tex_idx++) {
|
||||
if (textures[tex_idx].rule && textures[tex_idx].rule(&normal, face, bsp)) {
|
||||
matched_texture_idx = tex_idx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (matched_texture_idx >= count) {
|
||||
pxl8_warn("No texture rule matched for face %u", face_idx);
|
||||
continue;
|
||||
}
|
||||
|
||||
const pxl8_world_texture* matched = &textures[matched_texture_idx];
|
||||
|
||||
pxl8_vec3 u_axis, v_axis;
|
||||
if (fabsf(normal.y) > 0.9f) {
|
||||
u_axis = (pxl8_vec3){1.0f, 0.0f, 0.0f};
|
||||
v_axis = (pxl8_vec3){0.0f, 0.0f, 1.0f};
|
||||
} else if (fabsf(normal.x) > 0.7f) {
|
||||
u_axis = (pxl8_vec3){0.0f, 1.0f, 0.0f};
|
||||
v_axis = (pxl8_vec3){0.0f, 0.0f, 1.0f};
|
||||
} else {
|
||||
u_axis = (pxl8_vec3){1.0f, 0.0f, 0.0f};
|
||||
v_axis = (pxl8_vec3){0.0f, 1.0f, 0.0f};
|
||||
}
|
||||
|
||||
u32 material_idx = bsp->num_materials;
|
||||
bool found_existing = false;
|
||||
for (u32 i = 0; i < bsp->num_materials; i++) {
|
||||
if (strcmp(bsp->materials[i].name, matched->name) == 0 &&
|
||||
bsp->materials[i].texture_id == matched->texture_id &&
|
||||
bsp->materials[i].u_axis.x == u_axis.x &&
|
||||
bsp->materials[i].u_axis.y == u_axis.y &&
|
||||
bsp->materials[i].u_axis.z == u_axis.z &&
|
||||
bsp->materials[i].v_axis.x == v_axis.x &&
|
||||
bsp->materials[i].v_axis.y == v_axis.y &&
|
||||
bsp->materials[i].v_axis.z == v_axis.z) {
|
||||
material_idx = i;
|
||||
found_existing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found_existing) {
|
||||
if (bsp->num_materials >= max_materials) {
|
||||
pxl8_error("Too many unique material entries");
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
pxl8_gfx_material* mat = &bsp->materials[material_idx];
|
||||
memcpy(mat->name, matched->name, sizeof(mat->name));
|
||||
mat->name[sizeof(mat->name) - 1] = '\0';
|
||||
mat->texture_id = matched->texture_id;
|
||||
mat->u_offset = 0.0f;
|
||||
mat->v_offset = 0.0f;
|
||||
mat->u_axis = u_axis;
|
||||
mat->v_axis = v_axis;
|
||||
mat->alpha = 255;
|
||||
mat->dither = true;
|
||||
mat->double_sided = true;
|
||||
mat->dynamic_lighting = true;
|
||||
|
||||
bsp->num_materials++;
|
||||
}
|
||||
|
||||
face->material_id = material_idx;
|
||||
}
|
||||
|
||||
pxl8_info("Applied %u textures to %u faces, created %u materials",
|
||||
count, bsp->num_faces, bsp->num_materials);
|
||||
|
||||
return PXL8_OK;
|
||||
pxl8_entity pxl8_world_spawn(pxl8_world* world) {
|
||||
if (!world || !world->entities) return PXL8_ENTITY_INVALID;
|
||||
return pxl8_entity_spawn(world->entities);
|
||||
}
|
||||
|
||||
bool pxl8_world_check_collision(const pxl8_world* world, pxl8_vec3 pos, f32 radius) {
|
||||
if (!world || !world->loaded) return false;
|
||||
(void)radius;
|
||||
|
||||
const pxl8_bsp* bsp = &world->bsp;
|
||||
if (!world || !world->active_chunk) return false;
|
||||
|
||||
for (u32 i = 0; i < bsp->num_faces; i++) {
|
||||
const pxl8_bsp_face* face = &bsp->faces[i];
|
||||
const pxl8_bsp_plane* plane = &bsp->planes[face->plane_id];
|
||||
|
||||
if (fabsf(plane->normal.y) > 0.7f) {
|
||||
continue;
|
||||
}
|
||||
|
||||
f32 dist = plane->normal.x * pos.x +
|
||||
plane->normal.y * pos.y +
|
||||
plane->normal.z * pos.z - plane->dist;
|
||||
|
||||
if (fabsf(dist) > radius) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pxl8_vec3 closest_point = {
|
||||
pos.x - plane->normal.x * dist,
|
||||
pos.y - plane->normal.y * dist,
|
||||
pos.z - plane->normal.z * dist
|
||||
};
|
||||
|
||||
if (closest_point.x < face->aabb_min.x - radius || closest_point.x > face->aabb_max.x + radius ||
|
||||
closest_point.y < face->aabb_min.y - radius || closest_point.y > face->aabb_max.y + radius ||
|
||||
closest_point.z < face->aabb_min.z - radius || closest_point.z > face->aabb_max.z + radius) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (world->active_chunk->type == PXL8_CHUNK_BSP && world->active_chunk->bsp) {
|
||||
return pxl8_bsp_point_solid(world->active_chunk->bsp, pos);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool pxl8_world_is_loaded(const pxl8_world* world) {
|
||||
return world && world->loaded;
|
||||
pxl8_vec3 pxl8_world_resolve_collision(const pxl8_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius) {
|
||||
if (!world || !world->active_chunk) return to;
|
||||
|
||||
if (world->active_chunk->type == PXL8_CHUNK_BSP && world->active_chunk->bsp) {
|
||||
return pxl8_bsp_trace(world->active_chunk->bsp, from, to, radius);
|
||||
}
|
||||
|
||||
return to;
|
||||
}
|
||||
|
||||
pxl8_vec3 pxl8_world_resolve_collision(const pxl8_world* world, pxl8_vec3 from, pxl8_vec3 to, f32 radius) {
|
||||
if (!world || !world->loaded) return to;
|
||||
void pxl8_world_update(pxl8_world* world, f32 dt) {
|
||||
(void)dt;
|
||||
if (!world) return;
|
||||
|
||||
const pxl8_bsp* bsp = &world->bsp;
|
||||
pxl8_vec3 pos = to;
|
||||
pxl8_vec3 original_velocity = {to.x - from.x, to.y - from.y, to.z - from.z};
|
||||
|
||||
pxl8_vec3 clip_planes[5];
|
||||
u32 num_planes = 0;
|
||||
|
||||
const f32 edge_epsilon = 1.2f;
|
||||
const f32 radius_min = -radius + edge_epsilon;
|
||||
const f32 radius_max = radius - edge_epsilon;
|
||||
|
||||
for (i32 iteration = 0; iteration < 4; iteration++) {
|
||||
bool collided = false;
|
||||
|
||||
for (u32 i = 0; i < bsp->num_faces; i++) {
|
||||
const pxl8_bsp_face* face = &bsp->faces[i];
|
||||
const pxl8_bsp_plane* plane = &bsp->planes[face->plane_id];
|
||||
|
||||
if (fabsf(plane->normal.y) > 0.7f) continue;
|
||||
|
||||
f32 dist = plane->normal.x * pos.x +
|
||||
plane->normal.y * pos.y +
|
||||
plane->normal.z * pos.z - plane->dist;
|
||||
|
||||
f32 abs_dist = fabsf(dist);
|
||||
if (abs_dist > radius) continue;
|
||||
|
||||
pxl8_vec3 closest_point = {
|
||||
pos.x - plane->normal.x * dist,
|
||||
pos.y - plane->normal.y * dist,
|
||||
pos.z - plane->normal.z * dist
|
||||
};
|
||||
|
||||
if (closest_point.x < face->aabb_min.x + radius_min || closest_point.x > face->aabb_max.x + radius_max ||
|
||||
closest_point.y < face->aabb_min.y + radius_min || closest_point.y > face->aabb_max.y + radius_max ||
|
||||
closest_point.z < face->aabb_min.z + radius_min || closest_point.z > face->aabb_max.z + radius_max) {
|
||||
continue;
|
||||
}
|
||||
|
||||
f32 penetration = radius - abs_dist;
|
||||
if (penetration > 0.01f) {
|
||||
pxl8_vec3 push_dir;
|
||||
if (dist < 0) {
|
||||
push_dir.x = -plane->normal.x;
|
||||
push_dir.y = -plane->normal.y;
|
||||
push_dir.z = -plane->normal.z;
|
||||
} else {
|
||||
push_dir.x = plane->normal.x;
|
||||
push_dir.y = plane->normal.y;
|
||||
push_dir.z = plane->normal.z;
|
||||
}
|
||||
|
||||
bool is_new_plane = true;
|
||||
for (u32 p = 0; p < num_planes; p++) {
|
||||
if (pxl8_vec3_dot(push_dir, clip_planes[p]) > 0.99f) {
|
||||
is_new_plane = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_new_plane && num_planes < 5) {
|
||||
clip_planes[num_planes++] = push_dir;
|
||||
}
|
||||
|
||||
pos.x += push_dir.x * penetration;
|
||||
pos.y += push_dir.y * penetration;
|
||||
pos.z += push_dir.z * penetration;
|
||||
|
||||
collided = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!collided) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (num_planes >= 3) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (num_planes == 2) {
|
||||
f32 orig_vel_len_sq = original_velocity.x * original_velocity.x +
|
||||
original_velocity.y * original_velocity.y +
|
||||
original_velocity.z * original_velocity.z;
|
||||
|
||||
if (orig_vel_len_sq > 0.000001f) {
|
||||
f32 vdot0 = pxl8_vec3_dot(original_velocity, clip_planes[0]);
|
||||
f32 vdot1 = pxl8_vec3_dot(original_velocity, clip_planes[1]);
|
||||
f32 dot0 = fabsf(vdot0);
|
||||
f32 dot1 = fabsf(vdot1);
|
||||
|
||||
pxl8_vec3 slide_vel;
|
||||
if (dot0 < dot1) {
|
||||
slide_vel.x = original_velocity.x - clip_planes[0].x * vdot0;
|
||||
slide_vel.y = original_velocity.y - clip_planes[0].y * vdot0;
|
||||
slide_vel.z = original_velocity.z - clip_planes[0].z * vdot0;
|
||||
} else {
|
||||
slide_vel.x = original_velocity.x - clip_planes[1].x * vdot1;
|
||||
slide_vel.y = original_velocity.y - clip_planes[1].y * vdot1;
|
||||
slide_vel.z = original_velocity.z - clip_planes[1].z * vdot1;
|
||||
}
|
||||
|
||||
f32 slide_len = sqrtf(slide_vel.x * slide_vel.x + slide_vel.y * slide_vel.y + slide_vel.z * slide_vel.z);
|
||||
|
||||
if (slide_len > 0.01f) {
|
||||
pos.x += slide_vel.x;
|
||||
pos.y += slide_vel.y;
|
||||
pos.z += slide_vel.z;
|
||||
|
||||
pxl8_vec3 crease_dir = pxl8_vec3_cross(clip_planes[0], clip_planes[1]);
|
||||
f32 crease_len = pxl8_vec3_length(crease_dir);
|
||||
if (crease_len > 0.01f && fabsf(crease_dir.y / crease_len) > 0.9f) {
|
||||
f32 bias0 = pxl8_vec3_dot(original_velocity, clip_planes[0]);
|
||||
f32 bias1 = pxl8_vec3_dot(original_velocity, clip_planes[1]);
|
||||
|
||||
if (bias0 < 0 && bias1 < 0) {
|
||||
const f32 corner_push = 0.1f;
|
||||
pxl8_vec3 push_away = {
|
||||
clip_planes[0].x + clip_planes[1].x,
|
||||
clip_planes[0].y + clip_planes[1].y,
|
||||
clip_planes[0].z + clip_planes[1].z
|
||||
};
|
||||
f32 push_len_sq = push_away.x * push_away.x + push_away.y * push_away.y + push_away.z * push_away.z;
|
||||
if (push_len_sq > 0.000001f) {
|
||||
f32 inv_push_len = corner_push / sqrtf(push_len_sq);
|
||||
pos.x += push_away.x * inv_push_len;
|
||||
pos.y += push_away.y * inv_push_len;
|
||||
pos.z += push_away.z * inv_push_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f32 horizontal_movement_sq = (pos.x - from.x) * (pos.x - from.x) +
|
||||
(pos.z - from.z) * (pos.z - from.z);
|
||||
f32 desired_horizontal_sq = (to.x - from.x) * (to.x - from.x) +
|
||||
(to.z - from.z) * (to.z - from.z);
|
||||
|
||||
if (desired_horizontal_sq > 0.01f && horizontal_movement_sq < desired_horizontal_sq * 0.25f) {
|
||||
const f32 max_step_height = 0.4f;
|
||||
|
||||
pxl8_vec3 step_up = pos;
|
||||
step_up.y += max_step_height;
|
||||
|
||||
if (!pxl8_world_check_collision(world, step_up, radius)) {
|
||||
pxl8_vec3 step_forward = {
|
||||
step_up.x + (to.x - pos.x),
|
||||
step_up.y,
|
||||
step_up.z + (to.z - pos.z)
|
||||
};
|
||||
|
||||
if (!pxl8_world_check_collision(world, step_forward, radius)) {
|
||||
pos = step_forward;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pos;
|
||||
pxl8_chunk_cache_tick(world->chunk_cache);
|
||||
}
|
||||
|
||||
void pxl8_world_render(pxl8_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos) {
|
||||
if (!world || !gfx || !world->loaded) {
|
||||
static int count = 0;
|
||||
if (count++ < 10) {
|
||||
pxl8_debug("world_render: early return - world=%p, gfx=%p, loaded=%d",
|
||||
(void*)world, (void*)gfx, world ? world->loaded : -1);
|
||||
if (!world || !gfx || !world->active_chunk) return;
|
||||
|
||||
if (world->active_chunk->type == PXL8_CHUNK_BSP && world->active_chunk->bsp) {
|
||||
pxl8_bsp_render(gfx, world->active_chunk->bsp, camera_pos);
|
||||
}
|
||||
}
|
||||
|
||||
void pxl8_world_sync(pxl8_world* world, pxl8_net* net) {
|
||||
if (!world || !net) return;
|
||||
|
||||
u8 chunk_type = pxl8_net_chunk_type(net);
|
||||
u32 chunk_id = pxl8_net_chunk_id(net);
|
||||
|
||||
if (chunk_type == PXL8_CHUNK_TYPE_BSP && chunk_id != 0) {
|
||||
pxl8_chunk* chunk = pxl8_chunk_cache_get_bsp(world->chunk_cache, chunk_id);
|
||||
if (chunk && chunk->bsp) {
|
||||
if (world->active_chunk != chunk) {
|
||||
world->active_chunk = chunk;
|
||||
pxl8_debug("[CLIENT] Synced BSP chunk id=%u as active (verts=%u faces=%u)",
|
||||
chunk_id, chunk->bsp->num_vertices, chunk->bsp->num_faces);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
pxl8_bsp_render(gfx, &world->bsp, camera_pos);
|
||||
}
|
||||
|
||||
void pxl8_world_set_wireframe(pxl8_world* world, bool enabled) {
|
||||
if (!world || !world->loaded) return;
|
||||
|
||||
for (u32 i = 0; i < world->bsp.num_materials; i++) {
|
||||
world->bsp.materials[i].wireframe = enabled;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue