Skip to content

Commit

Permalink
code: fixed overflow on chunk_to_chunkpos conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Sep 9, 2022
1 parent 3758c96 commit 5b42c08
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
8 changes: 4 additions & 4 deletions code/source/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ int8_t librg_chunk_to_chunkpos(librg_world *world, librg_chunk id, int16_t *chun
return LIBRG_CHUNK_INVALID;
}

int16_t z = (int16_t)(id / (wld->worldsize.x * wld->worldsize.y));
int16_t r1 = (int16_t)(id % (wld->worldsize.x * wld->worldsize.y));
int16_t y = r1 / wld->worldsize.x;
int16_t x = r1 % wld->worldsize.x;
int64_t z = (int64_t)(id / (wld->worldsize.x * wld->worldsize.y));
int64_t r1 = (int64_t)(id % (wld->worldsize.x * wld->worldsize.y));
int64_t y = r1 / wld->worldsize.x;
int64_t x = r1 % wld->worldsize.x;

if (chunk_x) *chunk_x = x - librg_util_chunkoffset_line(0, wld->chunkoffset.x, wld->worldsize.x);
if (chunk_y) *chunk_y = y - librg_util_chunkoffset_line(0, wld->chunkoffset.y, wld->worldsize.y);
Expand Down
30 changes: 30 additions & 0 deletions code/tests/cases/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,34 @@ MODULE(query, {

librg_world_destroy(world);
});

IT("should query entities within big worlds", {
librg_world *world = librg_world_create();

r = librg_config_chunksize_set(world, 10, 10, UINT16_MAX); EQUALS(r, LIBRG_OK);
r = librg_config_chunkamount_set(world, 1024, 1024, 1); EQUALS(r, LIBRG_OK);
r = librg_config_chunkoffset_set(world, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID);

r = librg_entity_track(world, 1); EQUALS(r, LIBRG_OK);
r = librg_entity_track(world, 2); EQUALS(r, LIBRG_OK);
r = librg_entity_track(world, 3); EQUALS(r, LIBRG_OK);

librg_chunk test_chunk = 32768;

r = librg_entity_chunk_set(world, 1, test_chunk); EQUALS(r, LIBRG_OK);
r = librg_entity_chunk_set(world, 2, test_chunk); EQUALS(r, LIBRG_OK);
r = librg_entity_chunk_set(world, 3, test_chunk); EQUALS(r, LIBRG_OK);

r = librg_entity_owner_set(world, 2, 100); EQUALS(r, LIBRG_OK);

int64_t results[16] = {0}; size_t amt = 16;
librg_world_query(world, 100, 0, results, &amt);

EQUALS(amt, 3);
EQUALS(results[0], 2); // own entity first
EQUALS(results[1], 1);
EQUALS(results[2], 3);

librg_world_destroy(world);
})
});

0 comments on commit 5b42c08

Please sign in to comment.