diff --git a/src/rpg/world/worldpos.c b/src/rpg/world/worldpos.c index 71715db..77c5a7f 100644 --- a/src/rpg/world/worldpos.c +++ b/src/rpg/world/worldpos.c @@ -6,39 +6,57 @@ */ #include "worldpos.h" +#include "assert/assert.h" bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b) { return a.x == b.x && a.y == b.y && a.z == b.z; } void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out) { + assertNotNull(chunkPos, "Chunk position pointer cannot be NULL"); + assertNotNull(out, "Output world position pointer cannot be NULL"); + out->x = (worldunit_t)(chunkPos->x * CHUNK_WIDTH); out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT); out->z = (worldunit_t)(chunkPos->z * CHUNK_DEPTH); } void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out) { + assertNotNull(worldPos, "World position pointer cannot be NULL"); + assertNotNull(out, "Output chunk position pointer cannot be NULL"); + out->x = (chunkunit_t)(worldPos->x / CHUNK_WIDTH); out->y = (chunkunit_t)(worldPos->y / CHUNK_HEIGHT); out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH); } chunktileindex_t woprldPosToChunkTileIndex(const worldpos_t* worldPos) { + assertNotNull(worldPos, "World position pointer cannot be NULL"); + uint8_t localX = (uint8_t)(worldPos->x % CHUNK_WIDTH); uint8_t localY = (uint8_t)(worldPos->y % CHUNK_HEIGHT); uint8_t localZ = (uint8_t)(worldPos->z % CHUNK_DEPTH); - return (chunktileindex_t)( + chunktileindex_t chunkTileIndex = (chunktileindex_t)( (localZ * CHUNK_WIDTH * CHUNK_HEIGHT) + (localY * CHUNK_WIDTH) + localX ); + assertTrue( + chunkTileIndex < CHUNK_TILE_COUNT, + "Calculated chunk tile index is out of bounds" + ); + return chunkTileIndex; } chunkindex_t chunkPosToIndex(const chunkpos_t* pos) { - return (chunkindex_t)( + assertNotNull(pos, "Chunk position pointer cannot be NULL"); + + chunkindex_t chunkIndex = (chunkindex_t)( (pos->z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) + (pos->y * MAP_CHUNK_WIDTH) + pos->x ); + + return chunkIndex; } \ No newline at end of file