Skip to content

Commit

Permalink
code: fixes to radius checks within query
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Jul 30, 2022
1 parent 630f09e commit ae6e69b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 3 additions & 5 deletions code/source/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,14 @@ librg_chunk librg_chunk_from_chunkpos(librg_world *world, int16_t chunk_x, int16
int16_t chz = librg_util_chunkoffset_line(chunk_z, wld->chunkoffset.z, wld->worldsize.z);

/* return error if the size is too far off the max world limits */
if ((chx <= -wld->worldsize.x || chx >= wld->worldsize.x)
|| (chy <= -wld->worldsize.y || chy >= wld->worldsize.y)
|| (chz <= -wld->worldsize.z || chz >= wld->worldsize.z)) {
if ((chx < 0 || chx >= wld->worldsize.x)
|| (chy < 0 || chy >= wld->worldsize.y)
|| (chz < 0 || chz >= wld->worldsize.z)) {
return LIBRG_CHUNK_INVALID;
}

librg_chunk id = (chz * wld->worldsize.y * wld->worldsize.x) + (chy * wld->worldsize.x) + (chx);

// zpl_printf("(%d, %d, %d): %d\n", chx, chy, chz, id);

if (id < 0 || id > (wld->worldsize.x * wld->worldsize.y * wld->worldsize.z)) {
return LIBRG_CHUNK_INVALID;
}
Expand Down
4 changes: 3 additions & 1 deletion code/source/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ int32_t librg_world_fetch_ownerarray(librg_world *world, const int64_t *owner_id
// =======================================================================//

static LIBRG_ALWAYS_INLINE void librg_util_chunkrange(librg_world *w, librg_table_i64 *ch, int cx, int cy, int cz, int8_t radius) {
/* precalculate the radius power 2 for quicker distance check */
int radius2 = radius * radius;

/* create a "bubble" by cutting off chunks outside of radius using distance checks */
for (int z=-radius; z<=radius; z++) {
for (int y=-radius; y<=radius; y++) {
for (int x=-radius; x<=radius; x++) {
if(x*x+y*y+z*z <= radius2) {
if (x*x+y*y+z*z <= radius2) {
librg_chunk id = librg_chunk_from_chunkpos(w, cx+x, cy+y, cz+z);
if (id != LIBRG_CHUNK_INVALID) librg_table_i64_set(ch, id, 1);
}
Expand Down

0 comments on commit ae6e69b

Please sign in to comment.