Skip to content

Commit

Permalink
BaseBuilding is a BoxBuilding
Browse files Browse the repository at this point in the history
  • Loading branch information
atupone committed Mar 6, 2024
1 parent cb34304 commit 488f58b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 205 deletions.
28 changes: 3 additions & 25 deletions include/BaseBuilding.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
#ifndef BZF_BASE_BUILDING_H
#define BZF_BASE_BUILDING_H

#include "common.h"
#include <string>
#include "Obstacle.h"
#include "BoxBuilding.h"

class BaseBuilding : public Obstacle
class BaseBuilding : public BoxBuilding
{

friend class ObstacleModifier;
Expand All @@ -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;
Expand All @@ -70,9 +51,6 @@ class BaseBuilding : public Obstacle

std::string userTextures[2];

private:
void finalize();

private:
static const char* typeName;
int team;
Expand Down
2 changes: 1 addition & 1 deletion include/BoxBuilding.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class BoxBuilding : public Obstacle

std::string userTextures[2];

private:
protected:
void finalize();

private:
Expand Down
185 changes: 6 additions & 179 deletions src/obstacle/BaseBuilding.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,16 @@ 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()
{
// do nothing
}

void BaseBuilding::finalize()
{
Obstacle::setExtents();
return;
}

Obstacle* BaseBuilding::copyWithTransform(const MeshTransform& xform) const
{
float newPos[3], newSize[3], newAngle;
Expand All @@ -71,186 +63,34 @@ 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()))
&& ((p[2]+height) > getPosition()[2])
&& 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
{
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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down

0 comments on commit 488f58b

Please sign in to comment.