Skip to content

Commit

Permalink
Tweak motion prediction a little to remove use of deleted units.
Browse files Browse the repository at this point in the history
  • Loading branch information
bolrog committed May 4, 2021
1 parent 5c3acd8 commit fe06a90
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/d2dx/RenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,12 @@ float RenderContext::GetFrameTime() const
return (float)(_frameTimeMs / 1000.0);
}

int32_t RenderContext::GetFrameTimeFp() const
{
auto frameTimeMs = (int64_t)(_frameTimeMs * (65536.0 / 1000.0));
return (int32_t)max(INT_MIN, min(INT_MAX, frameTimeMs));
}

ScreenMode RenderContext::GetScreenMode() const
{
return _screenMode;
Expand Down
1 change: 1 addition & 0 deletions src/d2dx/RenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ namespace d2dx
virtual void ToggleFullscreen() override;

virtual float GetFrameTime() const override;
virtual int32_t GetFrameTimeFp() const override;

virtual ScreenMode GetScreenMode() const override;

Expand Down
14 changes: 9 additions & 5 deletions src/d2dx/UnitMotionPredictor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _Use_decl_annotations_
void UnitMotionPredictor::Update(
IRenderContext* renderContext)
{
const int32_t dt = (int32_t)(renderContext->GetFrameTime() * 65536.0);
const int32_t dt = renderContext->GetFrameTimeFp();
int32_t expiredUnitIndex = -1;

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

UnitMotion& um = _unitMotions.items[i];

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

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

if (max(abs(pos.x - um.predictedPos.x), abs(pos.y - um.predictedPos.y)) > 10 * 65536)
const Offset posWhole{ pos.x >> 16, pos.y >> 16 };
const Offset predictedPosWhole{ um.predictedPos.x >> 16, um.predictedPos.y >> 16 };
const int32_t predictionError = max(abs(posWhole.x - predictedPosWhole.x), abs(posWhole.y - predictedPosWhole.y));

if (predictionError > 10)
{
um.predictedPos = pos;
um.correctedPos = pos;
Expand Down Expand Up @@ -99,10 +103,10 @@ void UnitMotionPredictor::Update(
um.predictedPos.y = (int32_t)(((int64_t)um.predictedPos.y * oneMinusCorrectionAmount + (int64_t)um.correctedPos.y * correctionAmount) >> 16);
//D2DX_DEBUG_LOG("Predicted %f %f", um.predictedPos.x / 65536.0f, um.predictedPos.y / 65536.0f);

int32_t ex = um.correctedPos.x - um.predictedPos.x;
/* int32_t ex = um.correctedPos.x - um.predictedPos.x;
int32_t ey = um.correctedPos.y - um.predictedPos.y;
/* if (unit->dwType == D2::UnitType::Player && (ex != 0 || ey != 0))
if (unit->dwType == D2::UnitType::Player && (ex != 0 || ey != 0))
{
D2DX_DEBUG_LOG("%f, %f, %f, %f, %f, %f, %f, %f",
pos.x / 65536.0f,
Expand Down

0 comments on commit fe06a90

Please sign in to comment.