Skip to content

Commit b3d1f2b

Browse files
committed
Lua bindings to get current crouch amount or set an override
1 parent 37f9f0a commit b3d1f2b

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8888

8989
- New `AHuman` INI and Lua (R/W) property `MaxCrouchRotation`, which determines how much the actor will rotate when ducking to avoid low ceilings above them. This can be set to 0 to never duck. Defaults to a quarter of Pi * 1.25 (roughly 56 degrees).
9090

91+
- New `AHuman` Lua (R/W) property `CrouchAmountOverride`, which enforces that the actor crouch a certain amount, where 0 means fully standing and 1 is fully crouching. This override can be disabled by setting it to -1.0.
92+
93+
- New `AHuman` Lua (R) property `CrouchAmount`, which returns how much the actor is crouching, where 0 means fully standing and 1 is fully crouching.
94+
9195
- New `MOPixel` INI and Lua (R/W) property `Staininess`, which defines how likely a pixel is to stain a surface when it collides with it. Staining a surface changes that surface's `Color` to that of this `MOPixel`, without changing the underlying material. Value can be between 0 and 1. Defaults to 0 (never stain).
9296

9397
- New `Activity` INI and Lua (R/W) property `AllowsUserSaving`, which can be used to enable/disable manual user saving/loading. This defaults to true for all `GAScripted` with an `OnSave()` function, but false otherwise. Lua `ActivityMan::SaveGame()` function now forces a save even if `AllowsUserSaving` is disabled. This allows mods and scripted gamemodes to handle saving in their own way (for example, only allowing saving at set points).

Entities/AHuman.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void AHuman::Clear()
6767
m_ProneTimer.Reset();
6868
m_MaxWalkPathCrouchShift = 6.0F;
6969
m_MaxCrouchRotation = c_QuarterPI * 1.25F;
70+
m_CrouchAmountOverride = -1.0F;
7071
for (int i = 0; i < MOVEMENTSTATECOUNT; ++i) {
7172
m_Paths[FGROUND][i].Reset();
7273
m_Paths[BGROUND][i].Reset();
@@ -1726,27 +1727,33 @@ void AHuman::UpdateWalkAngle(AHuman::Layer whichLayer) {
17261727

17271728
void AHuman::UpdateCrouching() {
17281729
if (!m_Controller.IsState(BODY_JUMP) && m_pHead) {
1729-
// Cast a ray above our head to either side to determine whether we need to crouch
1730-
float desiredCrouchHeadRoom = std::floor(m_pHead->GetRadius() + 2.0f);
1731-
float toPredicted = std::floor(m_Vel.m_X * m_pHead->GetRadius()); // Check where we'll be a second from now
1732-
Vector hitPosStart = (m_pHead->GetPos() + Vector(0.0F, m_SpriteRadius * 0.5F)).Floor();
1733-
Vector hitPosPredictedStart = (m_pHead->GetPos() + Vector(toPredicted, m_SpriteRadius * 0.5F)).Floor();
1734-
Vector hitPos, hitPosPredicted;
1735-
g_SceneMan.CastStrengthRay(hitPosStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 1.0F, hitPos, 0, g_MaterialGrass);
1736-
g_SceneMan.CastStrengthRay(hitPosPredictedStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 1.0F, hitPosPredicted, 0, g_MaterialGrass);
1737-
1738-
// Don't do it if we're already hitting, we're probably in a weird spot
1739-
if (hitPosStart.m_Y - hitPos.m_Y <= 2.0F) {
1740-
hitPos.m_Y = 0.0F;
1741-
}
1730+
float walkPathYOffset = 0.0F;
1731+
if (m_CrouchAmountOverride == -1.0F) {
1732+
// Cast a ray above our head to either side to determine whether we need to crouch
1733+
float desiredCrouchHeadRoom = std::floor(m_pHead->GetRadius() + 2.0f);
1734+
float toPredicted = std::floor(m_Vel.m_X * m_pHead->GetRadius()); // Check where we'll be a second from now
1735+
Vector hitPosStart = (m_pHead->GetPos() + Vector(0.0F, m_SpriteRadius * 0.5F)).Floor();
1736+
Vector hitPosPredictedStart = (m_pHead->GetPos() + Vector(toPredicted, m_SpriteRadius * 0.5F)).Floor();
1737+
Vector hitPos, hitPosPredicted;
1738+
g_SceneMan.CastStrengthRay(hitPosStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 1.0F, hitPos, 0, g_MaterialGrass);
1739+
g_SceneMan.CastStrengthRay(hitPosPredictedStart, Vector(0.0F, -desiredCrouchHeadRoom + m_SpriteRadius * -0.5F), 1.0F, hitPosPredicted, 0, g_MaterialGrass);
1740+
1741+
// Don't do it if we're already hitting, we're probably in a weird spot
1742+
if (hitPosStart.m_Y - hitPos.m_Y <= 2.0F) {
1743+
hitPos.m_Y = 0.0F;
1744+
}
17421745

1743-
if (hitPosPredictedStart.m_Y - hitPosPredicted.m_Y <= 2.0F) {
1744-
hitPosPredicted.m_Y = 0.0F;
1746+
if (hitPosPredictedStart.m_Y - hitPosPredicted.m_Y <= 2.0F) {
1747+
hitPosPredicted.m_Y = 0.0F;
1748+
}
1749+
1750+
float headroom = m_pHead->GetPos().m_Y - std::max(hitPos.m_Y, hitPosPredicted.m_Y);
1751+
float adjust = desiredCrouchHeadRoom - headroom;
1752+
walkPathYOffset = std::clamp(LERP(0.0F, 1.0F, -m_WalkPathOffset.m_Y, adjust, 0.3F), 0.0F, m_MaxWalkPathCrouchShift);
1753+
} else {
1754+
walkPathYOffset = m_CrouchAmountOverride * m_MaxWalkPathCrouchShift;
17451755
}
17461756

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

17521759
// If crouching, move at reduced speed

Entities/AHuman.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,24 @@ DefaultPieMenuNameGetter("Default Human Pie Menu");
892892
/// <param name="newValue">The new value for this AHuman's max crouch rotation adjustment.</param>
893893
void SetMaxCrouchRotation(float newValue) { m_MaxCrouchRotation = newValue; }
894894

895+
/// <summary>
896+
/// Gets this AHuman's current crouch amount. 0.0 == fully standing, 1.0 == fully crouched.
897+
/// </summary>
898+
/// <returns>This AHuman's current crouch amount.</returns>
899+
float GetCrouchAmount() const { return (m_WalkPathOffset.m_Y * -1.0F) / m_MaxWalkPathCrouchShift; }
900+
901+
/// <summary>
902+
/// Gets this AHuman's current crouch amount override. 0.0 == fully standing, 1.0 == fully crouched, -1 == no override.
903+
/// </summary>
904+
/// <returns>This AHuman's current crouch amount override.</returns>
905+
float GetCrouchAmountOverride() const { return m_CrouchAmountOverride; }
906+
907+
/// <summary>
908+
/// Sets this AHuman's current crouch amount override.
909+
/// </summary>
910+
/// <param name="newValue">The new value for this AHuman's current crouch amount override.</param>
911+
void SetCrouchAmountOverride(float newValue) { m_CrouchAmountOverride = newValue; }
912+
895913
/// <summary>
896914
/// Gets this AHuman's stride sound. Ownership is NOT transferred!
897915
/// </summary>
@@ -968,6 +986,8 @@ DefaultPieMenuNameGetter("Default Human Pie Menu");
968986
float m_MaxWalkPathCrouchShift;
969987
// The maximum amount we will duck our head down to avoid obstacles above us.
970988
float m_MaxCrouchRotation;
989+
// The script-set forced crouching amount. 0.0 == fully standing, 1.0 == fully crouched, -1 == no override.
990+
float m_CrouchAmountOverride;
971991
// Limb paths for different movement states.
972992
// [0] is for the foreground limbs, and [1] is for BG.
973993
LimbPath m_Paths[2][MOVEMENTSTATECOUNT];

Lua/LuaBindingsEntities.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ namespace RTE {
462462
.property("BGFoot", &AHuman::GetBGFoot, &LuaAdaptersPropertyOwnershipSafetyFaker::AHumanSetBGFoot)
463463
.property("MaxWalkPathCrouchShift", &AHuman::GetMaxWalkPathCrouchShift, &AHuman::SetMaxWalkPathCrouchShift)
464464
.property("MaxCrouchRotation", &AHuman::GetMaxCrouchRotation, &AHuman::SetMaxCrouchRotation)
465+
.property("CrouchAmount", &AHuman::GetCrouchAmount)
466+
.property("CrouchAmountOverride", &AHuman::GetCrouchAmountOverride, &AHuman::SetCrouchAmountOverride)
465467
.property("StrideSound", &AHuman::GetStrideSound, &LuaAdaptersPropertyOwnershipSafetyFaker::AHumanSetStrideSound)
466468
.property("UpperBodyState", &AHuman::GetUpperBodyState, &AHuman::SetUpperBodyState)
467469
.property("MovementState", &AHuman::GetMovementState, &AHuman::SetMovementState)

0 commit comments

Comments
 (0)