From 488f58bfd8e6ed00bdd43dabc329ce6cfa2a06b2 Mon Sep 17 00:00:00 2001 From: Alfredo Tupone Date: Tue, 5 Mar 2024 21:06:28 +0100 Subject: [PATCH] BaseBuilding is a BoxBuilding --- include/BaseBuilding.h | 28 +---- include/BoxBuilding.h | 2 +- src/obstacle/BaseBuilding.cxx | 185 ++-------------------------------- 3 files changed, 10 insertions(+), 205 deletions(-) diff --git a/include/BaseBuilding.h b/include/BaseBuilding.h index 576291b9a4..26bf61fd7c 100644 --- a/include/BaseBuilding.h +++ b/include/BaseBuilding.h @@ -17,11 +17,9 @@ #ifndef BZF_BASE_BUILDING_H #define BZF_BASE_BUILDING_H -#include "common.h" -#include -#include "Obstacle.h" +#include "BoxBuilding.h" -class BaseBuilding : public Obstacle +class BaseBuilding : public BoxBuilding { friend class ObstacleModifier; @@ -37,28 +35,11 @@ class BaseBuilding : public Obstacle const char* getType() const; static const char* getClassName(); // const - bool isFlatTop() const; - - float intersect(const Ray &) const; - void getNormal(const float *p, float *n) const; - void get3DNormal(const float* p, float* n) const; - bool inCylinder(const float* p, float radius, float height) const; - bool inBox(const float* p, float angle, - float halfWidth, float halfBreadth, float height) const; bool inMovingBox(const float* oldP, float oldAngle, const float *newP, float newAngle, float halfWidth, float halfBreadth, float height) const; - bool isCrossing(const float* p, float angle, - float halfWidth, float halfBreadth, float height, - float* plane) const; - - bool getHitNormal(const float *pos1, float azimuth1, - const float *pos2, float azimuth2, - float halfWidth, float halfBreadth, - float height, - float *normal) const; - void getCorner(int index, float *pos) const; + int getTeam() const; int packSize() const; @@ -70,9 +51,6 @@ class BaseBuilding : public Obstacle std::string userTextures[2]; -private: - void finalize(); - private: static const char* typeName; int team; diff --git a/include/BoxBuilding.h b/include/BoxBuilding.h index 58e3f85e46..8f52f234be 100644 --- a/include/BoxBuilding.h +++ b/include/BoxBuilding.h @@ -71,7 +71,7 @@ class BoxBuilding : public Obstacle std::string userTextures[2]; -private: +protected: void finalize(); private: diff --git a/src/obstacle/BaseBuilding.cxx b/src/obstacle/BaseBuilding.cxx index a12587cd43..ebd9df76cc 100644 --- a/src/obstacle/BaseBuilding.cxx +++ b/src/obstacle/BaseBuilding.cxx @@ -27,11 +27,9 @@ BaseBuilding::BaseBuilding() BaseBuilding::BaseBuilding(const float *p, float rotation, const float *_size, int _team, bool rico) : - Obstacle(p, rotation, _size[0], _size[1], _size[2], false, false, rico), + BoxBuilding(p, rotation, _size[0], _size[1], _size[2], false, false, rico), team(_team) { - finalize(); - return; } BaseBuilding::~BaseBuilding() @@ -39,12 +37,6 @@ BaseBuilding::~BaseBuilding() // do nothing } -void BaseBuilding::finalize() -{ - Obstacle::setExtents(); - return; -} - Obstacle* BaseBuilding::copyWithTransform(const MeshTransform& xform) const { float newPos[3], newSize[3], newAngle; @@ -71,36 +63,6 @@ const char* BaseBuilding::getClassName() return typeName; } -float BaseBuilding::intersect(const Ray &r) const -{ - return timeRayHitsBlock(r, getPosition(), getRotation(), - getWidth(), getBreadth(), getHeight()); -} - -void BaseBuilding::getNormal(const float *p, float *n) const -{ - getNormalRect(p, getPosition(), getRotation(), getWidth(), getBreadth(), n); -} - -void BaseBuilding::get3DNormal(const float* p, float* n) const -{ - // This bit of cruft causes bullets to bounce of buildings in the z direction - if (fabs(p[2] - getPosition()[2]) < Epsilon) - { - n[0] = 0.0f; - n[1] = 0.0f; - n[2] = -1.0f; - } - else if (fabs(p[2] - (getPosition()[2] + getHeight())) < Epsilon) - { - n[0] = 0.0f; - n[1] = 0.0f; - n[2] = 1.0f; - } // end cruftiness - else - getNormal(p, n); -} - bool BaseBuilding::inCylinder(const float *p, float radius, float height) const { return (p[2] < (getPosition()[2] + getHeight())) @@ -108,123 +70,15 @@ bool BaseBuilding::inCylinder(const float *p, float radius, float hei && testRectCircle(getPosition(), getRotation(), getWidth(), getBreadth(), p, radius); } -bool BaseBuilding::inBox(const float *p, float _angle, - float dx, float dy, float height) const -{ - return (p[2] < (getPosition()[2] + getHeight())) - && ((p[2]+height) >= getPosition()[2]) - && testRectRect(getPosition(), getRotation(), getWidth(), getBreadth(), - p, _angle, dx, dy); -} - bool BaseBuilding::inMovingBox(const float* oldP, float, const float *p, float _angle, float dx, float dy, float height) const { - float topBaseHeight = getPosition()[2] + getHeight(); - float higherZ; - float lowerZ; // if a base is just the ground (z == 0 && height == 0) no collision // ground is already handled - if (topBaseHeight <= 0.0) - return false; - if (oldP[2] > p[2]) - { - higherZ = oldP[2]; - lowerZ = p[2]; - } - else - { - higherZ = p[2]; - lowerZ = oldP[2]; - } - if (lowerZ >= topBaseHeight) - return false; - if ((higherZ + height) < getPosition()[2]) + if (!getPosition()[2] && !getHeight()) return false; - return testRectRect(getPosition(), getRotation(), getWidth(), getBreadth(), - p, _angle, dx, dy); -} - -bool BaseBuilding::isCrossing(const float *p, float _angle, - float dx, float dy, float height, - float *plane) const -{ - // if not inside or contained, then not crossing - if (!inBox(p, _angle, dx, dy, height) || - testRectInRect(getPosition(), getRotation(), - getWidth(), getBreadth(), p, _angle, dx, dy)) - return false; - if (!plane) return true; - - // it's crossing -- choose which wall is being crossed (this - // is a guestimate, should really do a careful test). Just - // see which wall the point is closest to - const float *p2 = getPosition(); - const float a2 = getRotation(); - const float c = cosf(-a2), s = sinf(-a2); - const float x = c * (p[0] - p2[0]) - s * (p[1] - p2[1]); - const float y = c * (p[1] - p2[1]) - s * (p[0] - p2[0]); - float pw[2]; - if (fabsf(fabsf(x) - getWidth()) < fabsf(fabsf(y) - getBreadth())) - { - plane[0] = ((x < 0.0) ? -cosf(a2) : cosf(a2)); - plane[1] = ((x < 0.0) ? -sinf(a2) : sinf(a2)); - pw[0] = p2[0] + getWidth() * plane[0]; - pw[1] = p2[1] + getWidth() * plane[1]; - } - else - { - plane[0] = ((y < 0.0) ? sinf(a2) : -sinf(a2)); - plane[1] = ((y < 0.0) ? cosf(a2) : -cosf(a2)); - pw[0] = p2[0] + getBreadth() * plane[0]; - pw[1] = p2[1] + getBreadth() * plane[1]; - } - - // now finish off plane equation - plane[2] = 0.0; - plane[3] = -(plane[0] * pw[0] + plane[1] * pw[1]); - return true; -} - -bool BaseBuilding::getHitNormal(const float *pos1, float azimuth1, - const float *pos2, float azimuth2, - float halfWidth, float halfBreadth, float, - float *normal) const -{ - return Obstacle::getHitNormal(pos1, azimuth1, pos2, azimuth2, halfWidth, halfBreadth, - getPosition(), getRotation(), getWidth(), getBreadth(), - getHeight(), normal) >= 0.0f; -} - -void BaseBuilding::getCorner(int index, float *_pos) const -{ - const float *base = getPosition(); - const float c = cosf(getRotation()); - const float s = sinf(getRotation()); - const float w = getWidth(); - const float b = getBreadth(); - switch (index & 3) - { - case 0: - _pos[0] = base[0] + c * w - s * b; - _pos[1] = base[1] + s * w + c * b; - break; - case 1: - _pos[0] = base[0] - c * w - s * b; - _pos[1] = base[1] - s * w + c * b; - break; - case 2: - _pos[0] = base[0] - c * w + s * b; - _pos[1] = base[1] - s * w - c * b; - break; - case 3: - _pos[0] = base[0] + c * w + s * b; - _pos[1] = base[1] + s * w - c * b; - break; - } - _pos[2] = base[2]; - if (index >= 4) _pos[2] += getHeight(); + return BoxBuilding::inMovingBox(oldP, 0.0f, p, _angle, dx, dy, height); } int BaseBuilding::getTeam() const @@ -232,25 +86,11 @@ int BaseBuilding::getTeam() const return team; } -bool BaseBuilding::isFlatTop() const -{ - return true; -} - - void* BaseBuilding::pack(void* buf) const { buf = nboPackUShort(buf, (uint16_t) team); - buf = nboPackVector(buf, pos); - buf = nboPackFloat(buf, angle); - buf = nboPackVector(buf, size); - - unsigned char stateByte = 0; - stateByte |= isDriveThrough() ? _DRIVE_THRU : 0; - stateByte |= isShootThrough() ? _SHOOT_THRU : 0; - stateByte |= canRicochet() ? _RICOCHET : 0; - buf = nboPackUByte(buf, stateByte); + buf = BoxBuilding::pack(buf); return buf; } @@ -262,17 +102,7 @@ const void* BaseBuilding::unpack(const void* buf) buf = nboUnpackUShort(buf, shortTeam); team = (int)shortTeam; - buf = nboUnpackVector(buf, pos); - buf = nboUnpackFloat(buf, angle); - buf = nboUnpackVector(buf, size); - - unsigned char stateByte; - buf = nboUnpackUByte(buf, stateByte); - driveThrough = (stateByte & _DRIVE_THRU) != 0; - shootThrough = (stateByte & _SHOOT_THRU) != 0; - ricochet = (stateByte & _RICOCHET) != 0; - - finalize(); + buf = BoxBuilding::unpack(buf); return buf; } @@ -282,10 +112,7 @@ int BaseBuilding::packSize() const { int fullSize = 0; fullSize += sizeof(uint16_t); // team - fullSize += sizeof(float[3]); // pos - fullSize += sizeof(float); // rotation - fullSize += sizeof(float[3]); // size - fullSize += sizeof(uint8_t); // state bits + fullSize += BoxBuilding::packSize(); return fullSize; }