Skip to content

Commit

Permalink
Fixed faulty floor division causing incorrect structure check (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubitect committed Oct 17, 2023
1 parent 5851553 commit 31f1892
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions finders.c
Original file line number Diff line number Diff line change
Expand Up @@ -1192,8 +1192,8 @@ int isViableStructurePos(int structureType, Generator *g, int x, int z, uint32_t
if (!getStructureConfig(Fortress, g->mc, &sc))
return 0;
Pos rp = {
(int)chunkX / sc.regionSize - (x < 0),
(int)chunkZ / sc.regionSize - (z < 0)
floordiv(x, sc.regionSize << 4),
floordiv(z, sc.regionSize << 4)
};
if (!getStructurePos(Bastion, g->mc, g->seed, rp.x, rp.z, &rp))
return 1;
Expand Down Expand Up @@ -1377,10 +1377,10 @@ int isViableStructurePos(int structureType, Generator *g, int x, int z, uint32_t
goto L_not_viable;
int cx0 = (chunkX-10), cx1 = (chunkX+10);
int cz0 = (chunkZ-10), cz1 = (chunkZ+10);
int rx0 = cx0 / vilconf.regionSize - (cx0 < 0);
int rx1 = cx1 / vilconf.regionSize - (cx1 < 0);
int rz0 = cz0 / vilconf.regionSize - (cz0 < 0);
int rz1 = cz1 / vilconf.regionSize - (cz1 < 0);
int rx0 = floordiv(cx0, vilconf.regionSize);
int rx1 = floordiv(cx1, vilconf.regionSize);
int rz0 = floordiv(cz0, vilconf.regionSize);
int rz1 = floordiv(cz1, vilconf.regionSize);
int rx, rz;
for (rz = rz0; rz <= rz1; rz++)
{
Expand Down
8 changes: 8 additions & 0 deletions rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ uint32_t rotr32(uint32_t a, uint8_t b)
return (a >> b) | (a << (32-b));
}

/// integer floor divide
static inline ATTR(const, always_inline)
int32_t floordiv(int32_t a, int32_t b)
{
int32_t q = a / b;
int32_t r = a % b;
return q - ((a ^ b) < 0 && !!r);
}

///=============================================================================
/// C implementation of Java Random
Expand Down

0 comments on commit 31f1892

Please sign in to comment.