From 8e15b9c0001a0a95edae0b324a34c8b6a357a7e3 Mon Sep 17 00:00:00 2001 From: Atrik Date: Thu, 8 Jan 2026 14:45:17 +0100 Subject: [PATCH] Fix unit acceleration reset on upgrade Fixes #7625 --- .../data/mods/public/simulation/helpers/Transform.js | 11 +++++++++++ source/simulation2/components/CCmpUnitMotion.h | 12 +++++++++++- source/simulation2/components/ICmpUnitMotion.cpp | 8 +++++++- source/simulation2/components/ICmpUnitMotion.h | 7 ++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/binaries/data/mods/public/simulation/helpers/Transform.js b/binaries/data/mods/public/simulation/helpers/Transform.js index c63b2920df..cacd668cce 100644 --- a/binaries/data/mods/public/simulation/helpers/Transform.js +++ b/binaries/data/mods/public/simulation/helpers/Transform.js @@ -60,6 +60,17 @@ function ChangeEntityTemplate(oldEnt, newTemplate) cmpNewPosition.SetHeightOffset(cmpPosition.GetHeightOffset()); } + const cmpUnitMotion = Engine.QueryInterface(oldEnt, IID_UnitMotion); + const cmpNewUnitMotion = Engine.QueryInterface(newEnt, IID_UnitMotion); + if (cmpUnitMotion && cmpNewUnitMotion) + { + const currentSpeed = cmpUnitMotion.GetCurrentSpeed(); + cmpNewUnitMotion.SetCurrentSpeed(currentSpeed); + + const acceleration = cmpUnitMotion.GetAcceleration(); + cmpNewUnitMotion.SetAcceleration(acceleration); + } + // Prevent spawning subunits on occupied positions. const cmpTurretHolder = Engine.QueryInterface(oldEnt, IID_TurretHolder); const cmpNewTurretHolder = Engine.QueryInterface(newEnt, IID_TurretHolder); diff --git a/source/simulation2/components/CCmpUnitMotion.h b/source/simulation2/components/CCmpUnitMotion.h index 62bd9f9dee..c9b8c70257 100644 --- a/source/simulation2/components/CCmpUnitMotion.h +++ b/source/simulation2/components/CCmpUnitMotion.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -541,6 +541,16 @@ public: return m_CurrentSpeed; } + void SetCurrentSpeed(const fixed& speed) override + { + m_CurrentSpeed = speed; + + if (speed == fixed::Zero()) + m_LastTurnSpeed = fixed::Zero(); + else + m_LastTurnSpeed = speed; + } + void SetFacePointAfterMove(bool facePointAfterMove) override { m_FacePointAfterMove = facePointAfterMove; diff --git a/source/simulation2/components/ICmpUnitMotion.cpp b/source/simulation2/components/ICmpUnitMotion.cpp index f19791a49b..750c853e15 100644 --- a/source/simulation2/components/ICmpUnitMotion.cpp +++ b/source/simulation2/components/ICmpUnitMotion.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -32,6 +32,7 @@ DEFINE_INTERFACE_METHOD("PossiblyAtDestination", ICmpUnitMotion, PossiblyAtDesti DEFINE_INTERFACE_METHOD("FaceTowardsPoint", ICmpUnitMotion, FaceTowardsPoint) DEFINE_INTERFACE_METHOD("StopMoving", ICmpUnitMotion, StopMoving) DEFINE_INTERFACE_METHOD("GetCurrentSpeed", ICmpUnitMotion, GetCurrentSpeed) +DEFINE_INTERFACE_METHOD("SetCurrentSpeed", ICmpUnitMotion, SetCurrentSpeed) DEFINE_INTERFACE_METHOD("IsMoveRequested", ICmpUnitMotion, IsMoveRequested) DEFINE_INTERFACE_METHOD("GetSpeed", ICmpUnitMotion, GetSpeed) DEFINE_INTERFACE_METHOD("GetWalkSpeed", ICmpUnitMotion, GetWalkSpeed) @@ -98,6 +99,11 @@ public: return m_Script.Call("GetCurrentSpeed"); } + void SetCurrentSpeed(const fixed& speed) override + { + m_Script.CallVoid("SetCurrentSpeed", speed); + } + bool IsMoveRequested() const override { return m_Script.Call("IsMoveRequested"); diff --git a/source/simulation2/components/ICmpUnitMotion.h b/source/simulation2/components/ICmpUnitMotion.h index 14e666e44b..ce0a3c12c9 100644 --- a/source/simulation2/components/ICmpUnitMotion.h +++ b/source/simulation2/components/ICmpUnitMotion.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -104,6 +104,11 @@ public: */ virtual fixed GetCurrentSpeed() const = 0; + /** + * Set the speed. + */ + virtual void SetCurrentSpeed(const fixed& speed) = 0; + /** * @returns true if the unit has a destination. */