ULTIMA8: Update box struct better match rect methods

This commit is contained in:
Matthew Jimenez 2023-01-18 21:54:07 -06:00
parent 0d5d55b973
commit 2e11704ed9
3 changed files with 42 additions and 53 deletions

View file

@ -35,65 +35,54 @@ struct Box {
Box(int nx, int ny, int nz, int nxd, int nyd, int nzd) Box(int nx, int ny, int nz, int nxd, int nyd, int nzd)
: _x(nx), _y(ny), _z(nz), _xd(nxd), _yd(nyd), _zd(nzd) {} : _x(nx), _y(ny), _z(nz), _xd(nxd), _yd(nyd), _zd(nzd) {}
void Set(int nx, int ny, int nz, int nxd, int nyd, int nzd) { // Check if the Box is empty (its width, height, or depth is 0) or invalid (its width, height, or depth are negative).
_x = nx; bool isEmpty() const {
_y = ny;
_z = nz;
_xd = nxd;
_yd = nyd;
_zd = nzd;
}
void Set(Box &o) {
*this = o;
}
// Check to see if a Box is 'valid'
bool IsValid() const {
return _xd > 0 && _yd > 0 && _zd > 0; return _xd > 0 && _yd > 0 && _zd > 0;
} }
// Check to see if a Box is 'valid'
bool isValid() const {
return _xd >= 0 && _yd >= 0 && _zd >= 0;
}
// Check to see if a point is within the Box // Check to see if a point is within the Box
bool InBox(int px, int py, int pz) const { bool contains(int px, int py, int pz) const {
return (px > (_x - _xd) && py > (_y - _yd) && pz >= _z && return (px > (_x - _xd) && py > (_y - _yd) && pz >= _z &&
px <= _x && py <= _y && pz < (_z + _zd)); px <= _x && py <= _y && pz < (_z + _zd));
} }
// Move the Box (Relative) // Move the Box (Relative)
void MoveRel(int32 dx, int32 dy, int32 dz) { void translate(int32 dx, int32 dy, int32 dz) {
_x += dx; _x += dx;
_y += dy; _y += dy;
_z += dz; _z += dz;
} }
// Move the Box (Absolute) // Move the Box (Absolute)
void MoveAbs(int32 nx, int32 ny, int32 nz) { void moveTo(int32 nx, int32 ny, int32 nz) {
_x = nx; _x = nx;
_y = ny; _y = ny;
_z = nz; _z = nz;
} }
// Resize the Box (Relative)
void ResizeRel(int32 dxd, int32 dyd, int32 dzd) {
_xd += dxd;
_yd += dyd;
_zd += dzd;
}
// Resize the Box (Absolute) // Resize the Box (Absolute)
void ResizeAbs(int32 nxd, int32 nyd, int32 nzd) { void resize(int32 nxd, int32 nyd, int32 nzd) {
_xd = nxd; _xd = nxd;
_yd = nyd; _yd = nyd;
_zd = nzd; _zd = nzd;
} }
bool Overlaps(const Box &o) const { bool overlaps(const Box &o) const {
if (_x <= o._x - o._xd || o._x <= _x - _xd) return false; if (_x <= o._x - o._xd || o._x <= _x - _xd) return false;
if (_y <= o._y - o._yd || o._y <= _y - _yd) return false; if (_y <= o._y - o._yd || o._y <= _y - _yd) return false;
if (_z + _zd <= o._z || o._z + o._zd <= _z) return false; if (_z + _zd <= o._z || o._z + o._zd <= _z) return false;
return true; return true;
} }
bool operator == (const Box &o) const { bool operator==(const Box &rhs) const { return equals(rhs); }
bool operator!=(const Box &rhs) const { return !equals(rhs); }
bool equals(const Box &o) const {
return (_x == o._x && _y == o._y && _z == o._z && return (_x == o._x && _y == o._y && _z == o._z &&
_xd == o._xd && _yd == o._yd && _zd == o._zd); _xd == o._xd && _yd == o._yd && _zd == o._zd);
} }

View file

@ -462,8 +462,8 @@ void AnimationTracker::checkWeaponHit() {
Box abox = a->getWorldBox(); Box abox = a->getWorldBox();
abox.MoveAbs(_x, _y, _z); abox.moveTo(_x, _y, _z);
abox.MoveRel(Direction_XFactor(_dir) * 32 * range, Direction_YFactor(_dir) * 32 * range, 0); abox.translate(Direction_XFactor(_dir) * 32 * range, Direction_YFactor(_dir) * 32 * range, 0);
#ifdef WATCHACTOR #ifdef WATCHACTOR
if (a->getObjId() == watchactor) { if (a->getObjId() == watchactor) {
@ -489,7 +489,7 @@ void AnimationTracker::checkWeaponHit() {
Box ibox = item->getWorldBox(); Box ibox = item->getWorldBox();
if (abox.Overlaps(ibox)) { if (abox.overlaps(ibox)) {
hit = itemid; hit = itemid;
#ifdef WATCHACTOR #ifdef WATCHACTOR
if (a->getObjId() == watchactor) { if (a->getObjId() == watchactor) {

View file

@ -11,39 +11,39 @@ class U8BoxTestSuite : public CxxTest::TestSuite {
void test_simple_box() { void test_simple_box() {
Ultima::Ultima8::Box box; Ultima::Ultima8::Box box;
TS_ASSERT(!box.InBox(0,0,1)); TS_ASSERT(!box.contains(0,0,1));
TS_ASSERT(!box.InBox(0,1,0)); TS_ASSERT(!box.contains(0, 1, 0));
TS_ASSERT(!box.InBox(1,0,0)); TS_ASSERT(!box.contains(1, 0, 0));
box.ResizeRel(1,1,1); box.resize(1, 1, 1);
TS_ASSERT(!box.InBox(-1,0,0)); TS_ASSERT(!box.contains(-1, 0, 0));
TS_ASSERT(!box.InBox(0,-1,0)); TS_ASSERT(!box.contains(0, -1, 0));
TS_ASSERT(!box.InBox(0,0,-1)); TS_ASSERT(!box.contains(0, 0, -1));
TS_ASSERT(box.Overlaps(box)); TS_ASSERT(box.overlaps(box));
TS_ASSERT(box == box); TS_ASSERT(box == box);
box.ResizeRel(1,1,1); box.resize(2, 2, 2);
TS_ASSERT(box.IsValid()); TS_ASSERT(box.isValid());
TS_ASSERT(box.Overlaps(box)); TS_ASSERT(box.overlaps(box));
TS_ASSERT(box == box); TS_ASSERT(box == box);
// Note: These tests expect Box has reversed coordinates in x and y. // Note: These tests expect Box has reversed coordinates in x and y.
TS_ASSERT(box.InBox(-1,-1,1)); TS_ASSERT(box.contains(-1, -1, 1));
TS_ASSERT(box.InBox(-1,-1,0)); TS_ASSERT(box.contains(-1, -1, 0));
box.MoveRel(0, 0, 1); box.moveTo(0, 0, 1);
TS_ASSERT(!box.InBox(-1,-1,0)); TS_ASSERT(!box.contains(-1, -1, 0));
TS_ASSERT(box.InBox(-1,-1,2)); TS_ASSERT(box.contains(-1, -1, 2));
Ultima::Ultima8::Box box2(box); Ultima::Ultima8::Box box2(box);
TS_ASSERT(box == box2); TS_ASSERT(box == box2);
TS_ASSERT(box.Overlaps(box2)); TS_ASSERT(box.overlaps(box2));
TS_ASSERT(box2 == box); TS_ASSERT(box2 == box);
TS_ASSERT(box2.Overlaps(box)); TS_ASSERT(box2.overlaps(box));
Ultima::Ultima8::Box box3(0, 0, 0, 2, 2, 3); Ultima::Ultima8::Box box3(0, 0, 0, 2, 2, 3);
TS_ASSERT(!(box2 == box3)); TS_ASSERT(box2 != box3);
TS_ASSERT(box2.Overlaps(box3)); TS_ASSERT(box2.overlaps(box3));
TS_ASSERT(box3.Overlaps(box2)); TS_ASSERT(box3.overlaps(box2));
box3.ResizeAbs(1,1,1); box3.resize(1, 1, 1);
TS_ASSERT(!box3.Overlaps(box2)); TS_ASSERT(!box3.overlaps(box2));
} }
}; };