fix: regression in tile bounding box clip

This commit is contained in:
Tropical 2026-03-29 14:55:36 -05:00
parent dcc937da5e
commit 0e56f2c660
4 changed files with 67 additions and 66 deletions

View file

@ -2010,7 +2010,7 @@ AABB Tile::getTileAABB(Level* level, int x, int y, int z) {
// 4J Stu - Added this so that the TLS shape is correct for this tile // 4J Stu - Added this so that the TLS shape is correct for this tile
if (tls->tileId != this->id) updateDefaultShape(); if (tls->tileId != this->id) updateDefaultShape();
return AABB(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1, return AABB(x + tls->xx0, y + tls->yy0, z + tls->zz0, x + tls->xx1,
y + tls->yy1, z + tls->zz1); y + tls->yy1, z + tls->zz1);
} }
void Tile::addAABBs(Level* level, int x, int y, int z, AABB* box, void Tile::addAABBs(Level* level, int x, int y, int z, AABB* box,
@ -2119,52 +2119,51 @@ float Tile::getExplosionResistance(std::shared_ptr<Entity> source) {
return explosionResistance / 5.0f; return explosionResistance / 5.0f;
} }
HitResult* Tile::clip(Level* level, int xt, int yt, int zt, Vec3* a, Vec3* b) { HitResult* Tile::clip(Level* level, int xt, int yt, int zt, Vec3* a_,
Vec3* b_) {
updateShape(level, xt, yt, zt); updateShape(level, xt, yt, zt);
*a = a->add(-xt, -yt, -zt); Vec3 a = a_->add(-xt, -yt, -zt);
*b = b->add(-xt, -yt, -zt); Vec3 b = b_->add(-xt, -yt, -zt);
ThreadStorage* tls = m_tlsShape; ThreadStorage* tls = m_tlsShape;
auto xh0 = a->clipX(*b, tls->xx0);
auto xh1 = a->clipX(*b, tls->xx1);
auto yh0 = a->clipY(*b, tls->yy0); auto xh0 = a.clipX(b, tls->xx0);
auto yh1 = a->clipY(*b, tls->yy1); auto xh1 = a.clipX(b, tls->xx1);
auto zh0 = a->clipZ(*b, tls->zz0); auto yh0 = a.clipY(b, tls->yy0);
auto zh1 = a->clipZ(*b, tls->zz1); auto yh1 = a.clipY(b, tls->yy1);
auto zh0 = a.clipZ(b, tls->zz0);
auto zh1 = a.clipZ(b, tls->zz1);
std::optional<Vec3> closest = std::nullopt; std::optional<Vec3> closest = std::nullopt;
if (xh0.has_value() and containsX(&*xh0) and // 4jcraft NOTE: containsX does a nullopt check and will short circuit so
(!closest.has_value() or // dereffing in distanceToSqr is fine.
a->distanceToSqr(*xh0) < a->distanceToSqr(*closest)))
if (containsX(xh0) && (!closest.has_value() ||
a.distanceToSqr(*xh0) < a.distanceToSqr(*closest)))
closest = xh0; closest = xh0;
if (xh1.has_value() and containsX(&*xh1) and if (containsX(xh1) && (!closest.has_value() ||
(!closest.has_value() or a.distanceToSqr(*xh1) < a.distanceToSqr(*closest)))
a->distanceToSqr(*xh1) < a->distanceToSqr(*closest)))
closest = xh1; closest = xh1;
if (yh0.has_value() and containsY(&*yh0) and if (containsY(yh0) && (!closest.has_value() ||
(!closest.has_value() or a.distanceToSqr(*yh0) < a.distanceToSqr(*closest)))
a->distanceToSqr(*yh0) < a->distanceToSqr(*closest)))
closest = yh0; closest = yh0;
if (yh1.has_value() and containsY(&*yh1) and if (containsY(yh1) && (!closest.has_value() ||
(!closest.has_value() or a.distanceToSqr(*yh1) < a.distanceToSqr(*closest)))
a->distanceToSqr(*yh1) < a->distanceToSqr(*closest)))
closest = yh1; closest = yh1;
if (zh0.has_value() and containsZ(&*zh0) and if (containsZ(zh0) && (!closest.has_value() ||
(!closest.has_value() or a.distanceToSqr(*zh0) < a.distanceToSqr(*closest)))
a->distanceToSqr(*zh0) < a->distanceToSqr(*closest)))
closest = zh0; closest = zh0;
if (zh1.has_value() and containsZ(&*zh1) and if (containsZ(zh1) && (!closest.has_value() ||
(!closest.has_value() or a.distanceToSqr(*zh1) < a.distanceToSqr(*closest)))
a->distanceToSqr(*zh1) < a->distanceToSqr(*closest)))
closest = zh1; closest = zh1;
if (!closest.has_value()) return nullptr; if (!closest.has_value()) return nullptr;
@ -2178,12 +2177,11 @@ HitResult* Tile::clip(Level* level, int xt, int yt, int zt, Vec3* a, Vec3* b) {
if (closest == zh0) face = Facing::NORTH; if (closest == zh0) face = Facing::NORTH;
if (closest == zh1) face = Facing::SOUTH; if (closest == zh1) face = Facing::SOUTH;
Vec3 res = closest->add(xt, yt, zt); return new HitResult(xt, yt, zt, face, closest->add(xt, yt, zt));
return new HitResult(xt, yt, zt, face, res);
} }
bool Tile::containsX(Vec3* v) { bool Tile::containsX(const std::optional<Vec3>& v) {
if (v == NULL) return false; if (!v.has_value()) return false;
ThreadStorage* tls = m_tlsShape; ThreadStorage* tls = m_tlsShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile // 4J Stu - Added this so that the TLS shape is correct for this tile
@ -2192,8 +2190,8 @@ bool Tile::containsX(Vec3* v) {
v->z <= tls->zz1; v->z <= tls->zz1;
} }
bool Tile::containsY(Vec3* v) { bool Tile::containsY(const std::optional<Vec3>& v) {
if (v == NULL) return false; if (!v.has_value()) return false;
ThreadStorage* tls = m_tlsShape; ThreadStorage* tls = m_tlsShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile // 4J Stu - Added this so that the TLS shape is correct for this tile
@ -2202,8 +2200,8 @@ bool Tile::containsY(Vec3* v) {
v->z <= tls->zz1; v->z <= tls->zz1;
} }
bool Tile::containsZ(Vec3* v) { bool Tile::containsZ(const std::optional<Vec3>& v) {
if (v == NULL) return false; if (!v.has_value()) return false;
ThreadStorage* tls = m_tlsShape; ThreadStorage* tls = m_tlsShape;
// 4J Stu - Added this so that the TLS shape is correct for this tile // 4J Stu - Added this so that the TLS shape is correct for this tile

View file

@ -668,9 +668,9 @@ public:
Vec3* b); Vec3* b);
private: private:
virtual bool containsX(Vec3* v); virtual bool containsX(const std::optional<Vec3>& v);
virtual bool containsY(Vec3* v); virtual bool containsY(const std::optional<Vec3>& v);
virtual bool containsZ(Vec3* v); virtual bool containsZ(const std::optional<Vec3>& v);
public: public:
virtual void wasExploded(Level* level, int x, int y, int z, virtual void wasExploded(Level* level, int x, int y, int z,

View file

@ -184,37 +184,37 @@ HitResult* AABB::clip(const Vec3& a, const Vec3& b) const {
auto zh0 = a.clipZ(b, z0); auto zh0 = a.clipZ(b, z0);
auto zh1 = a.clipZ(b, z1); auto zh1 = a.clipZ(b, z1);
if (!(xh0.has_value() and containsX(*xh0))) xh0 = std::nullopt; if (!containsX(xh0)) xh0 = std::nullopt;
if (!(xh1.has_value() and containsX(*xh1))) xh1 = std::nullopt; if (!containsX(xh1)) xh1 = std::nullopt;
if (!(yh0.has_value() and containsY(*yh0))) yh0 = std::nullopt; if (!containsY(yh0)) yh0 = std::nullopt;
if (!(yh1.has_value() and containsY(*yh1))) yh1 = std::nullopt; if (!containsY(yh1)) yh1 = std::nullopt;
if (!(zh0.has_value() and containsZ(*zh0))) zh0 = std::nullopt; if (!containsZ(zh0)) zh0 = std::nullopt;
if (!(zh1.has_value() and containsZ(*zh1))) zh1 = std::nullopt; if (!containsZ(zh1)) zh1 = std::nullopt;
std::optional<Vec3> closest = std::nullopt; std::optional<Vec3> closest = std::nullopt;
if (xh0.has_value() and (!closest.has_value() or if (xh0.has_value() && (!closest.has_value() ||
a.distanceToSqr(*xh0) < a.distanceToSqr(*closest))) a.distanceToSqr(*xh0) < a.distanceToSqr(*closest)))
closest = xh0; closest = xh0;
if (xh1.has_value() and (!closest.has_value() or if (xh1.has_value() && (!closest.has_value() ||
a.distanceToSqr(*xh1) < a.distanceToSqr(*closest))) a.distanceToSqr(*xh1) < a.distanceToSqr(*closest)))
closest = xh1; closest = xh1;
if (yh0.has_value() and (!closest.has_value() or if (yh0.has_value() && (!closest.has_value() ||
a.distanceToSqr(*yh0) < a.distanceToSqr(*closest))) a.distanceToSqr(*yh0) < a.distanceToSqr(*closest)))
closest = yh0; closest = yh0;
if (yh1.has_value() and (!closest.has_value() or if (yh1.has_value() && (!closest.has_value() ||
a.distanceToSqr(*yh1) < a.distanceToSqr(*closest))) a.distanceToSqr(*yh1) < a.distanceToSqr(*closest)))
closest = yh1; closest = yh1;
if (zh0.has_value() and (!closest.has_value() or if (zh0.has_value() && (!closest.has_value() ||
a.distanceToSqr(*zh0) < a.distanceToSqr(*closest))) a.distanceToSqr(*zh0) < a.distanceToSqr(*closest)))
closest = zh0; closest = zh0;
if (zh1.has_value() and (!closest.has_value() or if (zh1.has_value() && (!closest.has_value() ||
a.distanceToSqr(*zh1) < a.distanceToSqr(*closest))) a.distanceToSqr(*zh1) < a.distanceToSqr(*closest)))
closest = zh1; closest = zh1;
if (!closest.has_value()) return nullptr; if (!closest.has_value()) return nullptr;
@ -231,16 +231,19 @@ HitResult* AABB::clip(const Vec3& a, const Vec3& b) const {
return new HitResult(0, 0, 0, face, *closest); return new HitResult(0, 0, 0, face, *closest);
} }
bool AABB::containsX(const Vec3& v) const { bool AABB::containsX(const std::optional<Vec3>& v) const {
return v.y >= y0 && v.y <= y1 && v.z >= z0 && v.z <= z1; if (!v.has_value()) return false;
return v->y >= y0 && v->y <= y1 && v->z >= z0 && v->z <= z1;
} }
bool AABB::containsY(const Vec3& v) const { bool AABB::containsY(const std::optional<Vec3>& v) const {
return v.x >= x0 && v.x <= x1 && v.z >= z0 && v.z <= z1; if (!v.has_value()) return false;
return v->x >= x0 && v->x <= x1 && v->z >= z0 && v->z <= z1;
} }
bool AABB::containsZ(const Vec3& v) const { bool AABB::containsZ(const std::optional<Vec3>& v) const {
return v.x >= x0 && v.x <= x1 && v.y >= y0 && v.y <= y1; if (!v.has_value()) return false;
return v->x >= x0 && v->x <= x1 && v->y >= y0 && v->y <= y1;
} }
std::wstring AABB::toString() const { std::wstring AABB::toString() const {

View file

@ -28,8 +28,8 @@ public:
double getSize() const; double getSize() const;
AABB shrink(double xa, double ya, double za) const; AABB shrink(double xa, double ya, double za) const;
HitResult* clip(const Vec3& a, const Vec3& b) const; HitResult* clip(const Vec3& a, const Vec3& b) const;
bool containsX(const Vec3& v) const; bool containsX(const std::optional<Vec3>& v) const;
bool containsY(const Vec3& v) const; bool containsY(const std::optional<Vec3>& v) const;
bool containsZ(const Vec3& v) const; bool containsZ(const std::optional<Vec3>& v) const;
std::wstring toString() const; std::wstring toString() const;
}; };