voxelThing/source/chunkStructures.c

76 lines
2.2 KiB
C

// chunkStructures.c
// This file contains functions for placing structures inside chunks, as well as the definitions for chunk datastructures...
// TODO: Should probably keep datastructures and chunk structures separate.
#include "chunkStructures.h"
#include "blockTypes.h"
#include "raylib.h"
#include "rlgl.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void InitChunk(Chunk *chunk, int chunkX, int chunkZ) {
chunk->mesh = (Mesh){ 0 };
chunk->x = chunkX;
chunk->z = chunkZ;
// Zero out block data
for (int x = 0; x < CHUNK_SIZE_X; x++) {
for (int y = 0; y < CHUNK_SIZE_Y; y++) {
for (int z = 0; z < CHUNK_SIZE_Z; z++) {
chunk->blocks[x][y][z] = (Block){ .type = 0 };
}
}
}
}
// Places a tree at the position specified by x, y, z
void PlaceTreeAt(Chunk *chunk, int x, int y, int z) {
// Trunk (6 blocks tall)
for (int i = 1; i <= 6; i++) {
if (y + i >= CHUNK_SIZE_Y) break;
chunk->blocks[x][y + i][z].type = BLOCK_LOG;
}
// Canopy (2 layers of leaves)
for (int dy = 4; dy <= 6; dy++) {
for (int dx = -2; dx <= 2; dx++) {
for (int dz = -2; dz <= 2; dz++) {
int cx = x + dx;
int cy = y + dy;
int cz = z + dz;
if (cx < 0 || cx >= CHUNK_SIZE_X ||
cy < 0 || cy >= CHUNK_SIZE_Y ||
cz < 0 || cz >= CHUNK_SIZE_Z)
continue;
// Keep the center clear above trunk
if (dx == 0 && dz == 0 && dy == 4) continue;
// Slightly sparser at corners
if (abs(dx) == 2 && abs(dz) == 2) continue;
chunk->blocks[cx][cy][cz].type = BLOCK_LEAF;
}
}
}
}
int IsBlockFaceExposed(Chunk *chunk, int x, int y, int z, int dir) {
int nx = x + faceOffsets[dir][0];
int ny = y + faceOffsets[dir][1];
int nz = z + faceOffsets[dir][2];
if (nx < 0 || ny < 0 || nz < 0 ||
nx >= CHUNK_SIZE_X || ny >= CHUNK_SIZE_Y || nz >= CHUNK_SIZE_Z) {
return 1; // face is exposed at chunk boundary
}
return chunk->blocks[nx][ny][nz].type == 0;
}