Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Update crouching in our own function, and reduce speed when crouching
Browse files Browse the repository at this point in the history
  • Loading branch information
Causeless committed Dec 27, 2023
1 parent cbb2726 commit 6fe7421
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
64 changes: 37 additions & 27 deletions Entities/AHuman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,6 @@ void AHuman::OnNewMovePath()
void AHuman::UpdateWalkAngle(AHuman::Layer whichLayer) {
if (m_Controller.IsState(BODY_JUMP)) {
m_WalkAngle[whichLayer] = Matrix(c_QuarterPI * GetFlipFactor());
m_WalkPathOffset.Reset();
} else {
float rayLength = 15.0F;
Vector hipPos = m_Pos;
Expand All @@ -1718,37 +1717,46 @@ void AHuman::UpdateWalkAngle(AHuman::Layer whichLayer) {
Matrix walkAngle;
walkAngle.SetDegAngle(terrainRotationDegs);
m_WalkAngle[whichLayer] = walkAngle;
}
}

if (m_pHead) {
// Cast a ray above our head to either side to determine whether we need to crouch
float desiredCrouchHeadRoom = std::floor(m_pHead->GetRadius() + 2.0f);
float toPredicted = std::floor(m_Vel.m_X * m_pHead->GetRadius()); // Check where we'll be a second from now
Vector hitPosStart = (m_pHead->GetPos() + Vector(0.0F, m_SpriteRadius * 0.5F)).Floor();
Vector hitPosPredictedStart = (m_pHead->GetPos() + Vector(toPredicted, m_SpriteRadius * 0.5F)).Floor();
Vector hitPos, hitPosPredicted;
g_SceneMan.CastStrengthRay(hitPosStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 10.0F, hitPos, 0, g_MaterialGrass);
g_SceneMan.CastStrengthRay(hitPosPredictedStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 10.0F, hitPosPredicted, 0, g_MaterialGrass);

// Don't do it if we're already hitting, we're probably in a weird spot
if (hitPosStart == hitPos) {
hitPos.m_X = 0.0F;
}
//////////////////////////////////////////////////////////////////////////////////////////

if (hitPosPredictedStart == hitPosPredicted) {
hitPosPredicted.m_X = 0.0F;
}
void AHuman::UpdateCrouching() {
if (!m_Controller.IsState(BODY_JUMP) && m_pHead) {
// Cast a ray above our head to either side to determine whether we need to crouch
float desiredCrouchHeadRoom = std::floor(m_pHead->GetRadius() + 2.0f);
float toPredicted = std::floor(m_Vel.m_X * m_pHead->GetRadius()); // Check where we'll be a second from now
Vector hitPosStart = (m_pHead->GetPos() + Vector(0.0F, m_SpriteRadius * 0.5F)).Floor();
Vector hitPosPredictedStart = (m_pHead->GetPos() + Vector(toPredicted, m_SpriteRadius * 0.5F)).Floor();
Vector hitPos, hitPosPredicted;
g_SceneMan.CastStrengthRay(hitPosStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 10.0F, hitPos, 0, g_MaterialGrass);
g_SceneMan.CastStrengthRay(hitPosPredictedStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 10.0F, hitPosPredicted, 0, g_MaterialGrass);

float headroom = m_pHead->GetPos().m_Y - std::max(hitPos.m_Y, hitPosPredicted.m_Y);
float adjust = desiredCrouchHeadRoom - headroom;
float walkPathYOffset = std::clamp(LERP(0.0F, 1.0F, -m_WalkPathOffset.m_Y, adjust, 0.3F), 0.0F, m_MaxWalkPathCrouchShift);
m_WalkPathOffset.m_Y = -walkPathYOffset;
// Don't do it if we're already hitting, we're probably in a weird spot
if (hitPosStart == hitPos) {
hitPos.m_X = 0.0F;
}

// Adjust our X offset to try to keep our legs under our centre-of-mass
float predictedPosition = ((m_pHead->GetPos().m_X - m_Pos.m_X) * 0.15F) + m_Vel.m_X;
m_WalkPathOffset.m_X = predictedPosition;
} else {
m_WalkPathOffset.Reset();
if (hitPosPredictedStart == hitPosPredicted) {
hitPosPredicted.m_X = 0.0F;
}

float headroom = m_pHead->GetPos().m_Y - std::max(hitPos.m_Y, hitPosPredicted.m_Y);
float adjust = desiredCrouchHeadRoom - headroom;
float walkPathYOffset = std::clamp(LERP(0.0F, 1.0F, -m_WalkPathOffset.m_Y, adjust, 0.3F), 0.0F, m_MaxWalkPathCrouchShift);
m_WalkPathOffset.m_Y = -walkPathYOffset;

// If crouching, move at third speed
float travelSpeedMultiplier = LERP(0.0F, m_MaxWalkPathCrouchShift, 1.0F, 0.5F, -m_WalkPathOffset.m_Y);
m_Paths[FGROUND][WALK].SetTravelSpeedMultiplier(travelSpeedMultiplier);
m_Paths[BGROUND][WALK].SetTravelSpeedMultiplier(travelSpeedMultiplier);

// Adjust our X offset to try to keep our legs under our centre-of-mass
float predictedPosition = ((m_pHead->GetPos().m_X - m_Pos.m_X) * 0.15F) + m_Vel.m_X;
m_WalkPathOffset.m_X = predictedPosition;
} else {
m_WalkPathOffset.Reset();
}
}

Expand Down Expand Up @@ -2226,6 +2234,8 @@ void AHuman::PreControllerUpdate()

m_StrideFrame = false;

UpdateCrouching();

if (m_Status == STABLE && !m_LimbPushForcesAndCollisionsDisabled && m_MoveState != NOMOVE)
{
// This exists to support disabling foot collisions if the limbpath has that flag set.
Expand Down
5 changes: 5 additions & 0 deletions Entities/AHuman.h
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ DefaultPieMenuNameGetter("Default Human Pie Menu");
/// <param name="whichLayer">The Layer in question.</param>
void UpdateWalkAngle(AHuman::Layer whichLayer);

/// <summary>
/// Detects overhead ceilings and crouches for them.
/// </summary>
void UpdateCrouching();

/// <summary>
/// Gets the walk path rotation for the specified Layer.
/// </summary>
Expand Down
13 changes: 10 additions & 3 deletions Entities/LimbPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ void LimbPath::Clear()
// m_CurrentSegment = 0;
m_FootCollisionsDisabledSegment = -1;
m_SegProgress = 0.0;
for (int i = 0; i < SPEEDCOUNT; ++i)
for (int i = 0; i < SPEEDCOUNT; ++i) {
m_TravelSpeed[i] = 0.0;
}
m_TravelSpeedMultiplier = 1.0F;
m_WhichSpeed = NORMAL;
m_PushForce = 0.0;
m_JointPos.Reset();
Expand Down Expand Up @@ -129,8 +131,10 @@ int LimbPath::Create(const LimbPath &reference)
m_FootCollisionsDisabledSegment = reference.m_FootCollisionsDisabledSegment;

m_SegProgress = reference.m_SegProgress;
for (int i = 0; i < SPEEDCOUNT; ++i)
for (int i = 0; i < SPEEDCOUNT; ++i) {
m_TravelSpeed[i] = reference.m_TravelSpeed[i];
}
m_TravelSpeedMultiplier = reference.m_TravelSpeedMultiplier;
m_PushForce = reference.m_PushForce;
m_TimeLeft = reference.m_TimeLeft;
m_TotalLength = reference.m_TotalLength;
Expand Down Expand Up @@ -182,6 +186,7 @@ int LimbPath::ReadProperty(const std::string_view &propName, Reader &reader)
reader >> m_TravelSpeed[FAST];
//m_TravelSpeed[FAST] = m_TravelSpeed[FAST] * 2;
});
MatchProperty("TravelSpeedMultiplier", { reader >> m_TravelSpeedMultiplier; });
MatchProperty("PushForce", {
reader >> m_PushForce;
//m_PushForce = m_PushForce / 1.5;
Expand Down Expand Up @@ -221,6 +226,8 @@ int LimbPath::Save(Writer &writer) const
writer << m_TravelSpeed[NORMAL];
writer.NewProperty("FastTravelSpeed");
writer << m_TravelSpeed[FAST];
writer.NewProperty("TravelSpeedMultiplier");
writer << m_TravelSpeedMultiplier;
writer.NewProperty("PushForce");
writer << m_PushForce;

Expand Down Expand Up @@ -309,7 +316,7 @@ Vector LimbPath::GetCurrentVel(const Vector &limbPos)
{
Vector returnVel;
Vector distVect = g_SceneMan.ShortestDistance(limbPos, GetCurrentSegTarget());
float adjustedTravelSpeed = m_TravelSpeed[m_WhichSpeed] / (1.0F + std::abs(m_JointVel.GetY()) * 0.1F);
float adjustedTravelSpeed = (m_TravelSpeed[m_WhichSpeed] / (1.0F + std::abs(m_JointVel.GetY()) * 0.1F)) * m_TravelSpeedMultiplier;

if (IsStaticPoint())
{
Expand Down
22 changes: 19 additions & 3 deletions Entities/LimbPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ ClassInfoGetters;
// Arguments: None.
// Return value: A float describing the speed in m/s.

float GetSpeed() const { return m_TravelSpeed[m_WhichSpeed]; }
float GetSpeed() const { return m_TravelSpeed[m_WhichSpeed] * m_TravelSpeedMultiplier; }


//////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -240,6 +240,18 @@ ClassInfoGetters;

float GetSpeed(int speedPreset) const { if (speedPreset == SLOW || speedPreset == NORMAL || speedPreset == FAST) return m_TravelSpeed[speedPreset]; else return 0; }

/// <summary>
/// Sets the current travel speed multiplier.
/// </summary>
/// <param="newValue">The new travel speed multiplier.</returns>
void SetTravelSpeedMultiplier(float newValue) { m_TravelSpeedMultiplier = newValue; }

/// <summary>
/// Gets the current travel speed multiplier.
/// </summary>
/// <returns>The current travel speed multiplier.</returns>
float GetTravelSpeedMultiplier() const { return m_TravelSpeedMultiplier; }


//////////////////////////////////////////////////////////////////////////////////////////
// Method: GetPushForce
Expand Down Expand Up @@ -286,7 +298,7 @@ ClassInfoGetters;
// Arguments: None.
// Return value: The total time (ms) this should take to travel along, if unobstructed.

float GetTotalPathTime() const { return ((m_TotalLength * c_MPP) / m_TravelSpeed[m_WhichSpeed]) * 1000; }
float GetTotalPathTime() const { return ((m_TotalLength * c_MPP) / (m_TravelSpeed[m_WhichSpeed] * m_TravelSpeedMultiplier)) * 1000; }


//////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -297,7 +309,7 @@ ClassInfoGetters;
// Arguments: None.
// Return value: The total time (ms) this should take to travel along, if unobstructed.

float GetRegularPathTime() const { return ((m_RegularLength * c_MPP) / m_TravelSpeed[m_WhichSpeed]) * 1000; }
float GetRegularPathTime() const { return ((m_RegularLength * c_MPP) / (m_TravelSpeed[m_WhichSpeed] * m_TravelSpeedMultiplier)) * 1000; }


//////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -657,6 +669,10 @@ ClassInfoGetters;

// The constant speed that the limb traveling this path has in m/s.
float m_TravelSpeed[SPEEDCOUNT];

// The current travel speed multiplier
float m_TravelSpeedMultiplier;

// The current speed setting.
int m_WhichSpeed;

Expand Down
1 change: 1 addition & 0 deletions Lua/LuaBindingsEntities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ namespace RTE {

.property("StartOffset", &LimbPath::GetStartOffset, &LimbPath::SetStartOffset)
.property("SegmentCount", &LimbPath::GetSegCount)
.property("TravelSpeedMultiplier", &LimbPath::GetTravelSpeedMultiplier, &LimbPath::SetTravelSpeedMultiplier)

.def("GetSegment", &LimbPath::GetSegment);
}
Expand Down

0 comments on commit 6fe7421

Please sign in to comment.