78 lines
2.4 KiB
C
78 lines
2.4 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>
|
|
|
|
bool SaveChunk(const Chunk *chunk, const char *filename) {
|
|
FILE *fp = fopen(filename, "wb");
|
|
if (!fp) return false;
|
|
size_t written = fwrite(chunk->blocks, sizeof(Block), CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z, fp);
|
|
fclose(fp);
|
|
return written == CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z;
|
|
}
|
|
|
|
bool LoadChunk(Chunk *chunk, const char *filename) {
|
|
FILE *fp = fopen(filename, "rb");
|
|
if (!fp) return false;
|
|
size_t read = fread(chunk->blocks, sizeof(Block), CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z, fp);
|
|
fclose(fp);
|
|
return read == CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
|
|
|
|
|