68 lines
2.8 KiB
C
68 lines
2.8 KiB
C
#include "terrain.h"
|
|
#include "_internal/private_terrain.h"
|
|
|
|
#include "block.h"
|
|
#include "chunk.h"
|
|
|
|
#include "raylib.h"
|
|
|
|
#include <math.h>
|
|
|
|
/**
|
|
* Iterates through every coordinate in a chunk, calculating terrain height
|
|
* and cave presence to populate the block data array.
|
|
*/
|
|
void generate_terrain(struct chunk_struct *chunk, struct chunk_position_struct* chunks_world_position)
|
|
{
|
|
// Align world position to integers once
|
|
int32_t base_x = chunks_world_position->x;
|
|
int32_t base_y = chunks_world_position->y;
|
|
int32_t base_z = chunks_world_position->z;
|
|
|
|
for (uint8_t x = 0; x < chunk_size; x++)
|
|
{
|
|
for (uint8_t z = 0; z < chunk_size; z++)
|
|
{
|
|
int32_t world_coordinate_x = base_x + x;
|
|
int32_t world_coordinate_z = base_z + z;
|
|
int32_t surface_height =
|
|
(int32_t)floorf(
|
|
_get_terrain_height_at_coordinates(
|
|
world_coordinate_x,
|
|
world_coordinate_z));
|
|
|
|
for (uint8_t y = 0; y < chunk_size; y++)
|
|
{
|
|
int32_t world_coordinate_y = base_y + y;
|
|
uint8_t resulting_block_id = block_air;
|
|
|
|
if (world_coordinate_y <= surface_height)
|
|
{
|
|
resulting_block_id = block_dirt;
|
|
|
|
if (world_coordinate_y < surface_height - 3)
|
|
{
|
|
resulting_block_id = block_stone;
|
|
}
|
|
|
|
if (world_coordinate_y > surface_height - 1)
|
|
{
|
|
resulting_block_id = block_grass;
|
|
}
|
|
|
|
if (_is_cave_present_at_coordinates(
|
|
world_coordinate_x,
|
|
world_coordinate_y,
|
|
world_coordinate_z,
|
|
surface_height))
|
|
{
|
|
resulting_block_id = block_air;
|
|
}
|
|
}
|
|
|
|
chunk->block_data[index_chunk(x, y, z)] = resulting_block_id;
|
|
}
|
|
}
|
|
}
|
|
}
|