Files
MinecraftConsoles/Minecraft.Client/Timer.cpp
ModMaker101 a9be52c41a Project modernization (#630)
* Fixed boats falling and a TP glitch #266

* Replaced every C-style cast with C++ ones

* Replaced every C-style cast with C++ ones

* Fixed boats falling and a TP glitch #266

* Updated NULL to nullptr and fixing some type issues

* Modernized and fixed a few bugs

- Replaced most instances of `NULL` with `nullptr`.
- Replaced most `shared_ptr(new ...)` with `make_shared`.
- Removed the `nullptr` macro as it was interfering with the actual nullptr keyword in some instances.

* Fixing more conflicts

* Replace int loops with size_t and start work on overrides
2026-03-08 09:56:03 +07:00

139 lines
3.3 KiB
C++

#include "stdafx.h"
#include "Timer.h"
#include "..\Minecraft.World\System.h"
Timer::Timer(float ticksPerSecond)
{
// 4J - added initialisers
lastTime = 0;
ticks = 0;
a = 0;
timeScale = 1;
passedTime = 0;
accumMs = 0;
adjustTime = 1.0;
this->ticksPerSecond = ticksPerSecond;
lastMs = System::currentTimeMillis();
lastMsSysTime = System::nanoTime() / 1000000;
}
void Timer::advanceTime()
{
int64_t nowMs = System::currentTimeMillis();
int64_t passedMs = nowMs - lastMs;
// 4J - Use high-resolution timer for 'now' in seconds
double now = System::nanoTime() / 1000000000.0;
if (passedMs > 1000)
{
lastTime = now;
}
else if (passedMs < 0)
{
lastTime = now;
}
else
{
accumMs += passedMs;
if (accumMs > 1000)
{
const int64_t msSysTime = static_cast<int64_t>(now * 1000.0);
const int64_t passedMsSysTime = msSysTime - lastMsSysTime;
const double adjustTimeT = accumMs / static_cast<double>(passedMsSysTime);
adjustTime += (adjustTimeT - adjustTime) * 0.2f;
lastMsSysTime = msSysTime;
accumMs = 0;
}
if (accumMs < 0)
{
lastMsSysTime = static_cast<int64_t>(now * 1000.0);
}
}
lastMs = nowMs;
double passedSeconds = (now - lastTime) * adjustTime;
lastTime = now;
if (passedSeconds < 0) passedSeconds = 0;
if (passedSeconds > 1) passedSeconds = 1;
passedTime = static_cast<float>(passedTime + (passedSeconds * timeScale * ticksPerSecond));
ticks = static_cast<int>(passedTime);
passedTime -= ticks;
if (ticks > MAX_TICKS_PER_UPDATE) ticks = MAX_TICKS_PER_UPDATE;
a = passedTime;
}
void Timer::advanceTimeQuickly()
{
double passedSeconds = static_cast<double>(MAX_TICKS_PER_UPDATE) / static_cast<double>(ticksPerSecond);
passedTime = static_cast<float>(passedTime + (passedSeconds * timeScale * ticksPerSecond));
ticks = static_cast<int>(passedTime);
passedTime -= ticks;
a = passedTime;
lastMs = System::currentTimeMillis();
lastMsSysTime = System::nanoTime() / 1000000;
}
void Timer::skipTime()
{
int64_t nowMs = System::currentTimeMillis();
int64_t passedMs = nowMs - lastMs;
int64_t msSysTime = System::nanoTime() / 1000000;
double now = msSysTime / 1000.0;
if (passedMs > 1000)
{
lastTime = now;
}
else if (passedMs < 0)
{
lastTime = now;
}
else
{
accumMs += passedMs;
if (accumMs > 1000)
{
int64_t passedMsSysTime = msSysTime - lastMsSysTime;
double adjustTimeT = accumMs / static_cast<double>(passedMsSysTime);
adjustTime += (adjustTimeT - adjustTime) * 0.2f;
lastMsSysTime = msSysTime;
accumMs = 0;
}
if (accumMs < 0)
{
lastMsSysTime = msSysTime;
}
}
lastMs = nowMs;
double passedSeconds = (now - lastTime) * adjustTime;
lastTime = now;
if (passedSeconds < 0) passedSeconds = 0;
if (passedSeconds > 1) passedSeconds = 1;
passedTime = static_cast<float>(passedTime + (passedSeconds * timeScale * ticksPerSecond));
ticks = static_cast<int>(0);
if (ticks > MAX_TICKS_PER_UPDATE) ticks = MAX_TICKS_PER_UPDATE;
passedTime -= ticks;
}