Added some extra checks around world positions, revealing bug. Likely going to sign all world coordinates.
This commit is contained in:
@@ -6,39 +6,57 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "worldpos.h"
|
#include "worldpos.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
|
||||||
bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b) {
|
bool_t worldPosIsEqual(const worldpos_t a, const worldpos_t b) {
|
||||||
return a.x == b.x && a.y == b.y && a.z == b.z;
|
return a.x == b.x && a.y == b.y && a.z == b.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
void chunkPosToWorldPos(const chunkpos_t* chunkPos, worldpos_t* out) {
|
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->x = (worldunit_t)(chunkPos->x * CHUNK_WIDTH);
|
||||||
out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT);
|
out->y = (worldunit_t)(chunkPos->y * CHUNK_HEIGHT);
|
||||||
out->z = (worldunit_t)(chunkPos->z * CHUNK_DEPTH);
|
out->z = (worldunit_t)(chunkPos->z * CHUNK_DEPTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void worldPosToChunkPos(const worldpos_t* worldPos, chunkpos_t* out) {
|
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->x = (chunkunit_t)(worldPos->x / CHUNK_WIDTH);
|
||||||
out->y = (chunkunit_t)(worldPos->y / CHUNK_HEIGHT);
|
out->y = (chunkunit_t)(worldPos->y / CHUNK_HEIGHT);
|
||||||
out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH);
|
out->z = (chunkunit_t)(worldPos->z / CHUNK_DEPTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
chunktileindex_t woprldPosToChunkTileIndex(const worldpos_t* worldPos) {
|
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 localX = (uint8_t)(worldPos->x % CHUNK_WIDTH);
|
||||||
uint8_t localY = (uint8_t)(worldPos->y % CHUNK_HEIGHT);
|
uint8_t localY = (uint8_t)(worldPos->y % CHUNK_HEIGHT);
|
||||||
uint8_t localZ = (uint8_t)(worldPos->z % CHUNK_DEPTH);
|
uint8_t localZ = (uint8_t)(worldPos->z % CHUNK_DEPTH);
|
||||||
|
|
||||||
return (chunktileindex_t)(
|
chunktileindex_t chunkTileIndex = (chunktileindex_t)(
|
||||||
(localZ * CHUNK_WIDTH * CHUNK_HEIGHT) +
|
(localZ * CHUNK_WIDTH * CHUNK_HEIGHT) +
|
||||||
(localY * CHUNK_WIDTH) +
|
(localY * CHUNK_WIDTH) +
|
||||||
localX
|
localX
|
||||||
);
|
);
|
||||||
|
assertTrue(
|
||||||
|
chunkTileIndex < CHUNK_TILE_COUNT,
|
||||||
|
"Calculated chunk tile index is out of bounds"
|
||||||
|
);
|
||||||
|
return chunkTileIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkindex_t chunkPosToIndex(const chunkpos_t* pos) {
|
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->z * MAP_CHUNK_WIDTH * MAP_CHUNK_HEIGHT) +
|
||||||
(pos->y * MAP_CHUNK_WIDTH) +
|
(pos->y * MAP_CHUNK_WIDTH) +
|
||||||
pos->x
|
pos->x
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return chunkIndex;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user