Index: ps/trunk/binaries/data/mods/public/simulation/components/Formation.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/Formation.js +++ ps/trunk/binaries/data/mods/public/simulation/components/Formation.js @@ -858,7 +858,7 @@ 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() Index: ps/trunk/binaries/data/mods/public/simulation/components/GuiInterface.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/GuiInterface.js +++ ps/trunk/binaries/data/mods/public/simulation/components/GuiInterface.js @@ -502,7 +502,7 @@ if (cmpUnitMotion) ret.speed = { "walk": cmpUnitMotion.GetWalkSpeed(), - "run": cmpUnitMotion.GetWalkSpeed() * cmpUnitMotion.GetRunSpeedMultiplier() + "run": cmpUnitMotion.GetWalkSpeed() * cmpUnitMotion.GetRunMultiplier() }; return ret; Index: ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js +++ ps/trunk/binaries/data/mods/public/simulation/components/UnitAI.js @@ -1660,7 +1660,7 @@ }, "leave": function(msg) { - this.ResetMoveSpeed(); + this.ResetSpeedMultiplier(); this.StopTimer(); this.SetDefaultAnimationVariant(); }, @@ -1675,13 +1675,13 @@ { 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 @@ // 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 @@ 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 @@ var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); if (!cmpUnitMotion) return 0; - return cmpUnitMotion.GetRunSpeedMultiplier(); + return cmpUnitMotion.GetRunMultiplier(); }; /** @@ -5582,18 +5582,18 @@ /* * 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) Index: ps/trunk/binaries/data/mods/public/simulation/components/UnitMotionFlying.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/UnitMotionFlying.js +++ ps/trunk/binaries/data/mods/public/simulation/components/UnitMotionFlying.js @@ -304,12 +304,12 @@ 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 @@ return this.speed; }; -UnitMotionFlying.prototype.GetSpeedRatio = function() +UnitMotionFlying.prototype.GetSpeedMultiplier = function() { return this.GetCurrentSpeed() / this.GetWalkSpeed(); } Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js @@ -138,7 +138,7 @@ 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 @@ 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() { }, Index: ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UnitMotionFlying.js =================================================================== --- ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UnitMotionFlying.js +++ ps/trunk/binaries/data/mods/public/simulation/components/tests/test_UnitMotionFlying.js @@ -26,12 +26,12 @@ "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 @@ 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(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 Index: ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp =================================================================== --- ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp +++ ps/trunk/source/simulation2/components/CCmpUnitMotion.cpp @@ -132,7 +132,7 @@ 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 @@ 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 @@ 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 @@ 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 cmpPathfinder(GetSystemEntity()); if (cmpPathfinder) @@ -338,7 +338,7 @@ 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 @@ 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 @@ 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 @@ return m_WalkSpeed; } + virtual fixed GetRunMultiplier() const + { + return m_RunMultiplier; + } + virtual pass_class_t GetPassabilityClass() const { return m_PassClass; @@ -866,7 +867,7 @@ 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 Index: ps/trunk/source/simulation2/components/ICmpUnitMotion.h =================================================================== --- ps/trunk/source/simulation2/components/ICmpUnitMotion.h +++ ps/trunk/source/simulation2/components/ICmpUnitMotion.h @@ -87,7 +87,7 @@ 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 @@ 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; /** - * Set the current movement speed. - * @param speed A ratio of GetWalkSpeed(). + * Returns the ratio of GetSpeed() / GetWalkSpeed(). */ - virtual void SetSpeedRatio(fixed ratio) = 0; + virtual fixed GetSpeedMultiplier() const = 0; /** - * Get the unit theoretical speed in metres per second. - * This is affected by SetSpeedRatio. + * Set the current movement speed. + * @param speed A multiplier of GetWalkSpeed(). */ - virtual fixed GetSpeed() const = 0; + virtual void SetSpeedMultiplier(fixed multiplier) = 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). + * Get the speed at which the unit intends to move. + * (regardless of whether the unit is moving or not right now). */ - virtual fixed GetWalkSpeed() const = 0; + virtual fixed GetSpeed() const = 0; + /** * Set whether the unit will turn to face the target point after finishing moving. */ Index: ps/trunk/source/simulation2/components/ICmpUnitMotion.cpp =================================================================== --- ps/trunk/source/simulation2/components/ICmpUnitMotion.cpp +++ ps/trunk/source/simulation2/components/ICmpUnitMotion.cpp @@ -31,11 +31,11 @@ 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 @@ return m_Script.Call("GetCurrentSpeed"); } - virtual void SetSpeedRatio(fixed ratio) - { - m_Script.CallVoid("SetSpeedRatio", ratio); - } - - virtual fixed GetRunSpeedMultiplier() const - { - return m_Script.Call("GetRunSpeedMultiplier"); - } - virtual bool IsMoving() const { return m_Script.Call("IsMoving"); @@ -112,6 +102,21 @@ return m_Script.Call("GetWalkSpeed"); } + virtual fixed GetRunMultiplier() const + { + return m_Script.Call("GetRunMultiplier"); + } + + virtual void SetSpeedMultiplier(fixed multiplier) + { + m_Script.CallVoid("SetSpeedMultiplier", multiplier); + } + + virtual fixed GetSpeedMultiplier() const + { + return m_Script.Call("GetSpeedMultiplier"); + } + virtual void SetFacePointAfterMove(bool facePointAfterMove) { m_Script.CallVoid("SetFacePointAfterMove", facePointAfterMove); @@ -122,11 +127,6 @@ return m_Script.Call("GetPassabilityClass"); } - virtual fixed GetSpeedRatio() const - { - return m_Script.Call("GetSpeedRatio"); - } - virtual std::string GetPassabilityClassName() const { return m_Script.Call("GetPassabilityClassName"); Index: ps/trunk/source/tools/atlas/GameInterface/ActorViewer.cpp =================================================================== --- ps/trunk/source/tools/atlas/GameInterface/ActorViewer.cpp +++ ps/trunk/source/tools/atlas/GameInterface/ActorViewer.cpp @@ -395,7 +395,7 @@ { CmpPtr 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