mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 18:39:46 +00:00
Merge pull request #1078 from ostermine/fix/server-tick-rate
fix headless tick rate: schedule ticks by deadlines
This commit is contained in:
commit
5983e981ed
1 changed files with 13 additions and 11 deletions
|
|
@ -35,16 +35,16 @@ void ServerMainloop::run() {
|
||||||
|
|
||||||
double targetDelta = 1.0 / static_cast<double>(coreParams.tps);
|
double targetDelta = 1.0 / static_cast<double>(coreParams.tps);
|
||||||
double delta = targetDelta;
|
double delta = targetDelta;
|
||||||
auto begin = system_clock::now();
|
const auto tickDuration =
|
||||||
auto startupTime = begin;
|
duration_cast<steady_clock::duration>(duration<double>(targetDelta));
|
||||||
|
auto startupTime = steady_clock::now();
|
||||||
|
auto nextTick = startupTime;
|
||||||
|
|
||||||
while (!engine.isQuitSignal() && !engine.getAppScripts().isFinished()) {
|
while (!engine.isQuitSignal() && !engine.getAppScripts().isFinished()) {
|
||||||
if (coreParams.testMode) {
|
if (coreParams.testMode) {
|
||||||
time.step(delta);
|
time.step(delta);
|
||||||
} else {
|
} else {
|
||||||
auto now = system_clock::now();
|
time.update(duration<double>(nextTick - startupTime).count());
|
||||||
time.update(
|
|
||||||
duration_cast<microseconds>(now - startupTime).count() / 1e6);
|
|
||||||
delta = time.getDelta();
|
delta = time.getDelta();
|
||||||
}
|
}
|
||||||
if (controller) {
|
if (controller) {
|
||||||
|
|
@ -55,13 +55,15 @@ void ServerMainloop::run() {
|
||||||
engine.postUpdate();
|
engine.postUpdate();
|
||||||
|
|
||||||
if (!coreParams.testMode) {
|
if (!coreParams.testMode) {
|
||||||
auto end = system_clock::now();
|
nextTick += tickDuration;
|
||||||
int64_t millis = targetDelta * 1000 -
|
auto now = steady_clock::now();
|
||||||
duration_cast<microseconds>(end - begin).count() / 1000;
|
if (nextTick > now) {
|
||||||
if (millis > 0) {
|
platform::sleep(
|
||||||
platform::sleep(millis);
|
duration_cast<milliseconds>(nextTick - now).count()
|
||||||
|
);
|
||||||
|
} else if (now - nextTick > tickDuration * 5) {
|
||||||
|
nextTick = now;
|
||||||
}
|
}
|
||||||
begin = system_clock::now();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.info() << "script finished";
|
logger.info() << "script finished";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue