UnitMotion - improve variable names and reuse a duplicated function (cleanup def47cb7ae)

This:
 - cleans up a code duplication and clarifies the intent.
 - reorders things around for clarity
 - improves variable names.

Commented By: elexis
Differential Revision: https://code.wildfiregames.com/D1840
This was SVN commit r22277.
This commit is contained in:
wraitii 2019-05-13 16:47:51 +00:00
parent ac47803221
commit 859d96fba5
10 changed files with 83 additions and 82 deletions

View File

@ -858,7 +858,7 @@ Formation.prototype.ComputeMotionParameters = function()
minSpeed *= this.GetSpeedMultiplier();
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
cmpUnitMotion.SetSpeedRatio(minSpeed / cmpUnitMotion.GetWalkSpeed());
cmpUnitMotion.SetSpeedMultiplier(minSpeed / cmpUnitMotion.GetWalkSpeed());
};
Formation.prototype.ShapeUpdate = function()

View File

@ -502,7 +502,7 @@ GuiInterface.prototype.GetEntityState = function(player, ent)
if (cmpUnitMotion)
ret.speed = {
"walk": cmpUnitMotion.GetWalkSpeed(),
"run": cmpUnitMotion.GetWalkSpeed() * cmpUnitMotion.GetRunSpeedMultiplier()
"run": cmpUnitMotion.GetWalkSpeed() * cmpUnitMotion.GetRunMultiplier()
};
return ret;

View File

@ -1660,7 +1660,7 @@ UnitAI.prototype.UnitFsmSpec = {
},
"leave": function(msg) {
this.ResetMoveSpeed();
this.ResetSpeedMultiplier();
this.StopTimer();
this.SetDefaultAnimationVariant();
},
@ -1675,13 +1675,13 @@ UnitAI.prototype.UnitFsmSpec = {
{
var speed = cmpUnitAI.GetWalkSpeed();
if (speed < this.GetWalkSpeed())
this.SetMoveSpeedRatio(speed / this.GetWalkSpeed());
this.SetSpeedMultiplier(speed / this.GetWalkSpeed());
}
}
},
"MoveCompleted": function() {
this.ResetMoveSpeed();
this.ResetSpeedMultiplier();
if (!this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange))
this.SetNextState("GUARDING");
},
@ -1739,15 +1739,15 @@ UnitAI.prototype.UnitFsmSpec = {
// Run quickly
this.SelectAnimation("move");
this.SetMoveSpeedRatio(this.GetRunMultiplier());
this.SetSpeedMultiplier(this.GetRunMultiplier());
},
"HealthChanged": function() {
this.SetMoveSpeedRatio(this.GetRunMultiplier());
this.SetSpeedMultiplier(this.GetRunMultiplier());
},
"leave": function() {
this.ResetMoveSpeed();
this.ResetSpeedMultiplier();
},
"MoveCompleted": function() {
@ -2031,7 +2031,7 @@ UnitAI.prototype.UnitFsmSpec = {
if (cmpUnitAI && cmpUnitAI.IsFleeing())
{
// Run after a fleeing target
this.SetMoveSpeedRatio(this.GetRunMultiplier());
this.SetSpeedMultiplier(this.GetRunMultiplier());
}
this.StartTimer(1000, 1000);
},
@ -3975,7 +3975,7 @@ UnitAI.prototype.GetRunMultiplier = function()
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
if (!cmpUnitMotion)
return 0;
return cmpUnitMotion.GetRunSpeedMultiplier();
return cmpUnitMotion.GetRunMultiplier();
};
/**
@ -5582,18 +5582,18 @@ UnitAI.prototype.GetStanceName = function()
/*
* Make the unit walk at its normal pace.
*/
UnitAI.prototype.ResetMoveSpeed = function()
UnitAI.prototype.ResetSpeedMultiplier = function()
{
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
if (cmpUnitMotion)
cmpUnitMotion.SetSpeedRatio(1);
cmpUnitMotion.SetSpeedMultiplier(1);
};
UnitAI.prototype.SetMoveSpeedRatio = function(speed)
UnitAI.prototype.SetSpeedMultiplier = function(speed)
{
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion);
if (cmpUnitMotion)
cmpUnitMotion.SetSpeedRatio(speed);
cmpUnitMotion.SetSpeedMultiplier(speed);
};
UnitAI.prototype.SetHeldPosition = function(x, z)

View File

@ -304,12 +304,12 @@ UnitMotionFlying.prototype.GetWalkSpeed = function()
return +this.template.MaxSpeed;
};
UnitMotionFlying.prototype.SetSpeedRatio = function()
UnitMotionFlying.prototype.SetSpeedMultiplier = function()
{
// ignore this, the speed is always the walk speed
};
UnitMotionFlying.prototype.GetRunSpeedMultiplier = function()
UnitMotionFlying.prototype.GetRunMultiplier = function()
{
return 1;
};
@ -319,7 +319,7 @@ UnitMotionFlying.prototype.GetCurrentSpeed = function()
return this.speed;
};
UnitMotionFlying.prototype.GetSpeedRatio = function()
UnitMotionFlying.prototype.GetSpeedMultiplier = function()
{
return this.GetCurrentSpeed() / this.GetWalkSpeed();
}

View File

@ -138,7 +138,7 @@ function TestFormationExiting(mode)
AddMock(controller, IID_UnitMotion, {
GetWalkSpeed: function() { return 1; },
SetSpeedRatio: function(speed) { },
SetSpeedMultiplier: function(speed) { },
MoveToPointRange: function(x, z, minRange, maxRange) { },
GetPassabilityClassName: function() { return "default"; },
});
@ -282,7 +282,7 @@ function TestMoveIntoFormationWhileAttacking()
AddMock(controller, IID_UnitMotion, {
GetWalkSpeed: function() { return 1; },
SetSpeedRatio: function(speed) { },
SetSpeedMultiplier: function(speed) { },
MoveToPointRange: function(x, z, minRange, maxRange) { },
IsInTargetRange: function(target, min, max) { return true; },
StopMoving: function() { },

View File

@ -26,12 +26,12 @@ let cmpUnitMotionFlying = ConstructComponent(entity, "UnitMotionFlying", {
"PassabilityClass": "unrestricted"
});
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedRatio(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetRunSpeedMultiplier(), 1);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedMultiplier(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetRunMultiplier(), 1);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
cmpUnitMotionFlying.SetSpeedRatio(2);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedRatio(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetRunSpeedMultiplier(), 1);
cmpUnitMotionFlying.SetSpeedMultiplier(2);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedMultiplier(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetRunMultiplier(), 1);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetPassabilityClassName(), "unrestricted");
@ -80,7 +80,7 @@ AddMock(entity, IID_WaterManager, {
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedRatio(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedMultiplier(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.MoveToTargetRange(target, 0, 10), true);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.MoveToPointRange(100, 200, 0, 20), true);
@ -100,7 +100,7 @@ TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.75);
TS_ASSERT_EQUALS(height, 55);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 1);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedRatio(), 1);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetSpeedMultiplier(), 1);
TS_ASSERT_EQUALS(height, 105);
// Fly

View File

@ -132,7 +132,7 @@ public:
bool m_FormationController;
fixed m_TemplateWalkSpeed, m_TemplateRunSpeedMultiplier;
fixed m_TemplateWalkSpeed, m_TemplateRunMultiplier;
pass_class_t m_PassClass;
std::string m_PassClassName;
@ -141,7 +141,7 @@ public:
entity_pos_t m_Clearance;
// cached for efficiency
fixed m_WalkSpeed, m_RunSpeedMultiplier;
fixed m_WalkSpeed, m_RunMultiplier;
bool m_Moving;
bool m_FacePointAfterMove;
@ -233,10 +233,10 @@ public:
entity_pos_t m_TargetMinRange;
entity_pos_t m_TargetMaxRange;
// Actual unit speed, after technology and ratio
// If the entity moves, it will do so at m_WalkSpeed * m_SpeedMultiplier.
fixed m_SpeedMultiplier;
// This caches the resulting speed from m_WalkSpeed * m_SpeedMultiplier for convenience.
fixed m_Speed;
// Convenience variable to avoid recomputing the ratio every time. Synchronised.
fixed m_SpeedRatio;
// Current mean speed (over the last turn).
fixed m_CurSpeed;
@ -283,12 +283,12 @@ public:
m_FacePointAfterMove = true;
m_WalkSpeed = m_TemplateWalkSpeed = m_Speed = paramNode.GetChild("WalkSpeed").ToFixed();
m_SpeedRatio = fixed::FromInt(1);
m_SpeedMultiplier = fixed::FromInt(1);
m_CurSpeed = fixed::Zero();
m_RunSpeedMultiplier = m_TemplateRunSpeedMultiplier = fixed::FromInt(1);
m_RunMultiplier = m_TemplateRunMultiplier = fixed::FromInt(1);
if (paramNode.GetChild("RunMultiplier").IsOk())
m_RunSpeedMultiplier = m_TemplateRunSpeedMultiplier = paramNode.GetChild("RunMultiplier").ToFixed();
m_RunMultiplier = m_TemplateRunMultiplier = paramNode.GetChild("RunMultiplier").ToFixed();
CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity());
if (cmpPathfinder)
@ -338,7 +338,7 @@ public:
serialize.NumberFixed_Unbounded("target min range", m_TargetMinRange);
serialize.NumberFixed_Unbounded("target max range", m_TargetMaxRange);
serialize.NumberFixed_Unbounded("speed ratio", m_SpeedRatio);
serialize.NumberFixed_Unbounded("speed multiplier", m_SpeedMultiplier);
serialize.NumberFixed_Unbounded("current speed", m_CurSpeed);
@ -419,11 +419,12 @@ public:
break;
m_WalkSpeed = cmpValueModificationManager->ApplyModifications(L"UnitMotion/WalkSpeed", m_TemplateWalkSpeed, GetEntityId());
m_RunSpeedMultiplier = cmpValueModificationManager->ApplyModifications(L"UnitMotion/RunMultiplier", m_TemplateRunSpeedMultiplier, GetEntityId());
m_RunMultiplier = cmpValueModificationManager->ApplyModifications(L"UnitMotion/RunMultiplier", m_TemplateRunMultiplier, GetEntityId());
// Adjust our speed. UnitMotion cannot know if this speed is on purpose or not so always adjust and let unitAI and such adapt.
m_SpeedRatio = std::min(m_SpeedRatio, m_RunSpeedMultiplier);
m_Speed = m_SpeedRatio.Multiply(GetWalkSpeed());
// For MT_Deserialize compute m_Speed from the serialized m_SpeedMultiplier.
// For MT_ValueModification and MT_OwnershipChanged, adjust m_SpeedMultiplier if needed
// (in case then new m_RunMultiplier value is lower than the old).
SetSpeedMultiplier(m_SpeedMultiplier);
break;
}
@ -441,20 +442,15 @@ public:
return m_Moving;
}
virtual fixed GetSpeedRatio() const
virtual fixed GetSpeedMultiplier() const
{
return m_SpeedRatio;
return m_SpeedMultiplier;
}
virtual fixed GetRunSpeedMultiplier() const
virtual void SetSpeedMultiplier(fixed multiplier)
{
return m_RunSpeedMultiplier;
}
virtual void SetSpeedRatio(fixed ratio)
{
m_SpeedRatio = std::min(ratio, m_RunSpeedMultiplier);
m_Speed = m_SpeedRatio.Multiply(GetWalkSpeed());
m_SpeedMultiplier = std::min(multiplier, m_RunMultiplier);
m_Speed = m_SpeedMultiplier.Multiply(GetWalkSpeed());
}
virtual fixed GetSpeed() const
@ -467,6 +463,11 @@ public:
return m_WalkSpeed;
}
virtual fixed GetRunMultiplier() const
{
return m_RunMultiplier;
}
virtual pass_class_t GetPassabilityClass() const
{
return m_PassClass;
@ -866,7 +867,7 @@ void CCmpUnitMotion::Move(fixed dt)
fixed basicSpeed = m_Speed;
// If in formation, run to keep up; otherwise just walk
if (IsFormationMember())
basicSpeed = m_Speed.Multiply(m_RunSpeedMultiplier);
basicSpeed = m_Speed.Multiply(m_RunMultiplier);
// Find the speed factor of the underlying terrain
// (We only care about the tile we start on - it doesn't matter if we're moving

View File

@ -31,11 +31,11 @@ DEFINE_INTERFACE_METHOD_3("MoveToFormationOffset", void, ICmpUnitMotion, MoveToF
DEFINE_INTERFACE_METHOD_2("FaceTowardsPoint", void, ICmpUnitMotion, FaceTowardsPoint, entity_pos_t, entity_pos_t)
DEFINE_INTERFACE_METHOD_0("StopMoving", void, ICmpUnitMotion, StopMoving)
DEFINE_INTERFACE_METHOD_CONST_0("GetCurrentSpeed", fixed, ICmpUnitMotion, GetCurrentSpeed)
DEFINE_INTERFACE_METHOD_CONST_0("GetRunSpeedMultiplier", fixed, ICmpUnitMotion, GetRunSpeedMultiplier)
DEFINE_INTERFACE_METHOD_1("SetSpeedRatio", void, ICmpUnitMotion, SetSpeedRatio, fixed)
DEFINE_INTERFACE_METHOD_CONST_0("IsMoving", bool, ICmpUnitMotion, IsMoving)
DEFINE_INTERFACE_METHOD_CONST_0("GetSpeed", fixed, ICmpUnitMotion, GetSpeed)
DEFINE_INTERFACE_METHOD_CONST_0("GetWalkSpeed", fixed, ICmpUnitMotion, GetWalkSpeed)
DEFINE_INTERFACE_METHOD_CONST_0("GetRunMultiplier", fixed, ICmpUnitMotion, GetRunMultiplier)
DEFINE_INTERFACE_METHOD_1("SetSpeedMultiplier", void, ICmpUnitMotion, SetSpeedMultiplier, fixed)
DEFINE_INTERFACE_METHOD_CONST_0("GetPassabilityClassName", std::string, ICmpUnitMotion, GetPassabilityClassName)
DEFINE_INTERFACE_METHOD_CONST_0("GetUnitClearance", entity_pos_t, ICmpUnitMotion, GetUnitClearance)
DEFINE_INTERFACE_METHOD_1("SetFacePointAfterMove", void, ICmpUnitMotion, SetFacePointAfterMove, bool)
@ -87,16 +87,6 @@ public:
return m_Script.Call<fixed>("GetCurrentSpeed");
}
virtual void SetSpeedRatio(fixed ratio)
{
m_Script.CallVoid("SetSpeedRatio", ratio);
}
virtual fixed GetRunSpeedMultiplier() const
{
return m_Script.Call<fixed>("GetRunSpeedMultiplier");
}
virtual bool IsMoving() const
{
return m_Script.Call<bool>("IsMoving");
@ -112,6 +102,21 @@ public:
return m_Script.Call<fixed>("GetWalkSpeed");
}
virtual fixed GetRunMultiplier() const
{
return m_Script.Call<fixed>("GetRunMultiplier");
}
virtual void SetSpeedMultiplier(fixed multiplier)
{
m_Script.CallVoid("SetSpeedMultiplier", multiplier);
}
virtual fixed GetSpeedMultiplier() const
{
return m_Script.Call<fixed>("GetSpeedMultiplier");
}
virtual void SetFacePointAfterMove(bool facePointAfterMove)
{
m_Script.CallVoid("SetFacePointAfterMove", facePointAfterMove);
@ -122,11 +127,6 @@ public:
return m_Script.Call<pass_class_t>("GetPassabilityClass");
}
virtual fixed GetSpeedRatio() const
{
return m_Script.Call<fixed>("GetSpeedRatio");
}
virtual std::string GetPassabilityClassName() const
{
return m_Script.Call<std::string>("GetPassabilityClassName");

View File

@ -87,7 +87,7 @@ public:
virtual void StopMoving() = 0;
/**
* Get the current movement speed.
* Get the distance travelled over the last turn.
*/
virtual fixed GetCurrentSpeed() const = 0;
@ -97,32 +97,32 @@ public:
virtual bool IsMoving() const = 0;
/**
* Get how much faster/slower we are than normal.
* Get the unit template walk speed after modifications.
*/
virtual fixed GetSpeedRatio() const = 0;
virtual fixed GetWalkSpeed() const = 0;
/**
* Get how much faster than our regular speed we can go.
* Get the unit template running (i.e. max) speed after modifications.
*/
virtual fixed GetRunSpeedMultiplier() const = 0;
virtual fixed GetRunMultiplier() const = 0;
/**
* Returns the ratio of GetSpeed() / GetWalkSpeed().
*/
virtual fixed GetSpeedMultiplier() const = 0;
/**
* Set the current movement speed.
* @param speed A ratio of GetWalkSpeed().
* @param speed A multiplier of GetWalkSpeed().
*/
virtual void SetSpeedRatio(fixed ratio) = 0;
virtual void SetSpeedMultiplier(fixed multiplier) = 0;
/**
* Get the unit theoretical speed in metres per second.
* This is affected by SetSpeedRatio.
* Get the speed at which the unit intends to move.
* (regardless of whether the unit is moving or not right now).
*/
virtual fixed GetSpeed() const = 0;
/**
* Get the unit "raw"/template walk speed after technologies.
* Calls to SetSpeedRatio have no effect on this (as that affects actual speed, not template).
*/
virtual fixed GetWalkSpeed() const = 0;
/**
* Set whether the unit will turn to face the target point after finishing moving.
*/

View File

@ -395,7 +395,7 @@ void ActorViewer::SetActor(const CStrW& name, const CStrW& animation, player_id_
{
CmpPtr<ICmpUnitMotion> cmpUnitMotion(m.Simulation2, m.Entity);
if (cmpUnitMotion)
speed = cmpUnitMotion->GetWalkSpeed().ToFloat() * cmpUnitMotion->GetRunSpeedMultiplier().ToFloat();
speed = cmpUnitMotion->GetWalkSpeed().ToFloat() * cmpUnitMotion->GetRunMultiplier().ToFloat();
else
speed = 12.f; // typical unit speed