Skip to content

Commit fe06a90

Browse files
committed
Tweak motion prediction a little to remove use of deleted units.
1 parent 5c3acd8 commit fe06a90

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/d2dx/RenderContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,12 @@ float RenderContext::GetFrameTime() const
991991
return (float)(_frameTimeMs / 1000.0);
992992
}
993993

994+
int32_t RenderContext::GetFrameTimeFp() const
995+
{
996+
auto frameTimeMs = (int64_t)(_frameTimeMs * (65536.0 / 1000.0));
997+
return (int32_t)max(INT_MIN, min(INT_MAX, frameTimeMs));
998+
}
999+
9941000
ScreenMode RenderContext::GetScreenMode() const
9951001
{
9961002
return _screenMode;

src/d2dx/RenderContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ namespace d2dx
109109
virtual void ToggleFullscreen() override;
110110

111111
virtual float GetFrameTime() const override;
112+
virtual int32_t GetFrameTimeFp() const override;
112113

113114
virtual ScreenMode GetScreenMode() const override;
114115

src/d2dx/UnitMotionPredictor.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ _Use_decl_annotations_
3434
void UnitMotionPredictor::Update(
3535
IRenderContext* renderContext)
3636
{
37-
const int32_t dt = (int32_t)(renderContext->GetFrameTime() * 65536.0);
37+
const int32_t dt = renderContext->GetFrameTimeFp();
3838
int32_t expiredUnitIndex = -1;
3939

4040
for (int32_t i = 0; i < _unitsCount; ++i)
@@ -49,7 +49,7 @@ void UnitMotionPredictor::Update(
4949

5050
UnitMotion& um = _unitMotions.items[i];
5151

52-
if ((_frame - um.lastUsedFrame) > 5 || unit->dwUnitId == 0xFFFFFFFF || unit->dwUnitId == 0)
52+
if ((_frame - um.lastUsedFrame) > 1 || unit->dwUnitId == 0xFFFFFFFF || unit->dwUnitId == 0)
5353
{
5454
_unitPtrs.items[i] = nullptr;
5555
expiredUnitIndex = i;
@@ -58,7 +58,11 @@ void UnitMotionPredictor::Update(
5858

5959
const Offset pos = _gameHelper->GetUnitPos(unit);
6060

61-
if (max(abs(pos.x - um.predictedPos.x), abs(pos.y - um.predictedPos.y)) > 10 * 65536)
61+
const Offset posWhole{ pos.x >> 16, pos.y >> 16 };
62+
const Offset predictedPosWhole{ um.predictedPos.x >> 16, um.predictedPos.y >> 16 };
63+
const int32_t predictionError = max(abs(posWhole.x - predictedPosWhole.x), abs(posWhole.y - predictedPosWhole.y));
64+
65+
if (predictionError > 10)
6266
{
6367
um.predictedPos = pos;
6468
um.correctedPos = pos;
@@ -99,10 +103,10 @@ void UnitMotionPredictor::Update(
99103
um.predictedPos.y = (int32_t)(((int64_t)um.predictedPos.y * oneMinusCorrectionAmount + (int64_t)um.correctedPos.y * correctionAmount) >> 16);
100104
//D2DX_DEBUG_LOG("Predicted %f %f", um.predictedPos.x / 65536.0f, um.predictedPos.y / 65536.0f);
101105

102-
int32_t ex = um.correctedPos.x - um.predictedPos.x;
106+
/* int32_t ex = um.correctedPos.x - um.predictedPos.x;
103107
int32_t ey = um.correctedPos.y - um.predictedPos.y;
104108
105-
/* if (unit->dwType == D2::UnitType::Player && (ex != 0 || ey != 0))
109+
if (unit->dwType == D2::UnitType::Player && (ex != 0 || ey != 0))
106110
{
107111
D2DX_DEBUG_LOG("%f, %f, %f, %f, %f, %f, %f, %f",
108112
pos.x / 65536.0f,

0 commit comments

Comments
 (0)