Skip to content

Commit

Permalink
refactor: use tickrate everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisAMJ committed Sep 29, 2024
1 parent 8747741 commit 886ea72
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 44 deletions.
35 changes: 9 additions & 26 deletions src/Features/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ CameraState Camera::InterpolateStates(float time) {
LAST };

//reading 4 frames closest to time
float frameTime = time * 60.0f;
float frameTime = time * sar.game->Tickrate();
int frames[4] = {INT_MIN, INT_MIN, INT_MAX, INT_MAX};
for (auto const &state : camera->states) {
int stateTime = state.first;
Expand Down Expand Up @@ -285,7 +285,7 @@ void Camera::DrawInWorld() const {
MeshId mesh_cams = OverlayRender::createMesh(RenderCallback::none, RenderCallback::constant({ 255, 0, 0 }, true));
MeshId mesh_currentCam = OverlayRender::createMesh(RenderCallback::none, RenderCallback::constant({ 255, 255, 0 }, true));

float frameTime = 1.0f / 60;
float frameTime = 1.0f / 30; // don't draw a line for every frame, that's just too much
int maxTimeTicks = 0;
int minTimeTicks = INT_MAX;
for (auto const &state : camera->states) {
Expand All @@ -294,8 +294,8 @@ void Camera::DrawInWorld() const {
}

// changing in-game ticks to seconds.
float maxTime = maxTimeTicks * frameTime;
float minTime = minTimeTicks * frameTime;
float maxTime = maxTimeTicks * engine->GetIPT();
float minTime = minTimeTicks * engine->GetIPT();

// for each frame, calculate interpolated path
Vector pos = camera->InterpolateStates(minTime).origin;
Expand Down Expand Up @@ -348,24 +348,7 @@ void Camera::DrawInWorld() const {
);

Vector forward, right, up;
float cp, sp, cy, sy, cr, sr;
cp = cosf(DEG2RAD(state.angles.x));
sp = sinf(DEG2RAD(state.angles.x));
cy = cosf(DEG2RAD(state.angles.y));
sy = sinf(DEG2RAD(state.angles.y));
cr = cosf(DEG2RAD(state.angles.z));
sr = sinf(DEG2RAD(state.angles.z));

forward.x = cp * cy;
forward.y = cp * sy;
forward.z = -sp;

right.x = (-1 * sr * sp * cy + -1 * cr * -sy);
right.y = (-1 * sr * sp * sy + -1 * cr * cy);
right.z = -1 * sr * cp;

up = right.Cross(forward);

Math::AngleVectors(state.angles, &forward, &right, &up);

float fovScalar = tanf(DEG2RAD(state.fov / 2));

Expand Down Expand Up @@ -405,7 +388,7 @@ void Camera::OverrideView(CViewSetup *m_View) {
}

if (timeOffsetRefreshRequested) {
timeOffset = engine->GetClientTime() - engine->demoplayer->GetTick() / 60.0f;
timeOffset = engine->GetClientTime() - engine->demoplayer->GetTick() * engine->GetIPT();
timeOffsetRefreshRequested = false;
}

Expand Down Expand Up @@ -839,7 +822,7 @@ CON_COMMAND(sar_cam_path_goto, "sar_cam_path_goto <frame> [skipto] - sends the c
return console->Print("This frame does not exist.\n");
}

camera->currentState = camera->InterpolateStates(i * (1.0f / 60));
camera->currentState = camera->InterpolateStates(i * engine->GetIPT());

if (args.ArgC() == 3 && std::atoi(args[2]) == 1) {
if (engine->demoplayer->IsPlaying()) {
Expand Down Expand Up @@ -904,8 +887,8 @@ CON_COMMAND(sar_cam_path_export,
}

// changing in-game ticks to seconds.
float maxTime = maxTimeTicks / 60.0f;
float minTime = minTimeTicks / 60.0f;
float maxTime = maxTimeTicks * engine->GetIPT();
float minTime = minTimeTicks * engine->GetIPT();

if (daVinci) file << "C={\n";

Expand Down
2 changes: 1 addition & 1 deletion src/Features/Demo/DemoParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ bool DemoParser::Parse(std::string filePath, Demo *demo, bool ghostRequest, std:
}

if (cmd.find("__END__") != std::string::npos) {
console->ColorMsg(Color(0, 255, 0, 255), "Segment length -> %d ticks: %.3fs\n", tick, tick / 60.f);
console->ColorMsg(Color(0, 255, 0, 255), "Segment length -> %d ticks: %.3fs\n", tick, tick * engine->GetIPT());
demo->segmentTicks = tick;
}
break;
Expand Down
6 changes: 3 additions & 3 deletions src/Features/Demo/GhostLeaderboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Variable ghost_leaderboard_y("ghost_leaderboard_y", "10", 0, "The x position of

static std::string formatTicks(int ticks) {
if (ticks == INT_MAX) return "-";
return SpeedrunTimer::Format((float)ticks / 60.0);
return SpeedrunTimer::Format((float)ticks / sar.game->Tickrate());
}

void GhostLeaderboardHud::Paint(int slot) {
Expand Down Expand Up @@ -239,7 +239,7 @@ void GhostLeaderboardHud::SpeedrunStart(int ticks) {
return;
} else { // mode == 1
// race
this->lastLiveUpdate = (int)(engine->GetHostTime() * 60.0f);
this->lastLiveUpdate = (int)(engine->GetHostTime() * sar.game->Tickrate());
for (auto &ent : ghostLeaderboard.entries) {
ent.ticks = ticks;
ent.waiting = false;
Expand All @@ -251,7 +251,7 @@ void GhostLeaderboardHud::SpeedrunStart(int ticks) {
void GhostLeaderboardHud::UpdateLive() {
if (ghost_leaderboard_mode.GetInt() != 1) return;

int newTime = (int)(engine->GetHostTime() * 60.0f);
int newTime = (int)(engine->GetHostTime() * sar.game->Tickrate());
int delta = newTime - this->lastLiveUpdate;
this->lastLiveUpdate = newTime;

Expand Down
2 changes: 1 addition & 1 deletion src/Features/DiscordRichPresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ ON_EVENT(FRAME) {
int host, server, client;
engine->GetTicks(host, server, client);

if (host > lastExecutedPresence + 60) {
if (host > lastExecutedPresence + sar.game->Tickrate()) {
lastExecutedPresence = host;
UpdateDiscordRichPresence();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Hud/PlacementHelperHud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ ON_EVENT(RENDER) {
auto deferringToPortal = placementHelper->field<bool>("m_bDeferringToPortal");

auto currentTime = server->gpGlobals->curtime;
auto remainingBlockedTimeTicks = (int)(fmaxf(disableTime - currentTime, 0.0f) * 60.0f);
auto remainingBlockedTimeTicks = (int)(fmaxf(disableTime - currentTime, 0.0f) * sar.game->Tickrate());
auto placementBlocked = deferringToPortal || (remainingBlockedTimeTicks > 0);

auto sphereColor = Color(0, 200, 0, 64);
Expand Down
10 changes: 5 additions & 5 deletions src/Features/Speedrun/SpeedrunTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "Utils.hpp"

#define SPEEDRUN_PACKET_TYPE "srtimer"
#define SYNC_INTERVAL 60 // Sync every second, just in case
#define SYNC_INTERVAL 1.0f // Sync every second, just in case

enum PacketType {
SYNC,
Expand Down Expand Up @@ -316,7 +316,7 @@ int SpeedrunTimer::GetOffsetTicks() {

if (*end) {
// Try to parse as a time instead
ticks = std::round(SpeedrunTimer::UnFormat(offset) * 60.0f);
ticks = std::round(SpeedrunTimer::UnFormat(offset) * sar.game->Tickrate());
}

return ticks;
Expand Down Expand Up @@ -443,7 +443,7 @@ void SpeedrunTimer::Update() {

if (engine->IsCoop() && !engine->IsOrange()) {
int tick = getCurrentTick();
if (tick < g_coopLastSyncTick || tick >= g_coopLastSyncTick + SYNC_INTERVAL) {
if (tick < g_coopLastSyncTick || tick >= g_coopLastSyncTick + roundf(SYNC_INTERVAL / engine->GetIPT())) {
sendCoopPacket(PacketType::SYNC);
g_coopLastSyncTick = tick;
}
Expand Down Expand Up @@ -990,11 +990,11 @@ CON_COMMAND(sar_speedrun_recover, "sar_speedrun_recover <ticks|time> - recover a
long ticks = strtol(time, &end, 10);
if (*end) {
// Try to parse as a time instead
ticks = std::round(SpeedrunTimer::UnFormat(time) * 60.0f);
ticks = std::round(SpeedrunTimer::UnFormat(time) * sar.game->Tickrate());
}

g_speedrun.recovery = ticks;
console->Print("Timer will start on next load at %s\n", SpeedrunTimer::Format(ticks / 60.0f).c_str());
console->Print("Timer will start on next load at %s\n", SpeedrunTimer::Format(ticks * engine->GetIPT()).c_str());
}

CON_COMMAND(sar_speedrun_export_all, "sar_speedrun_export_all <filename> - export the results of many speedruns to the specified CSV file\n") {
Expand Down
3 changes: 2 additions & 1 deletion src/Features/SteamTimeline.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "SteamTimeline.hpp"

#include "Event.hpp"
#include "Modules/Engine.hpp"
#include "Modules/SteamAPI.hpp"
#include "Speedrun/SpeedrunTimer.hpp"

Expand Down Expand Up @@ -42,7 +43,7 @@ ON_EVENT(SPEEDRUN_FINISH) {
if (!steam->hasLoaded) return;
std::chrono::duration<float> offset = std::chrono::system_clock::now() - g_speedrunStart;

auto fl_time = SpeedrunTimer::GetTotalTicks() / 60.0f;
auto fl_time = SpeedrunTimer::GetTotalTicks() * engine->GetIPT();
auto time = SpeedrunTimer::Format(fl_time);

steam->g_timeline->SetTimelineStateDescription(("Speedrun " + time).c_str(), -offset.count());
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Stitcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ void Stitcher::OverrideView(CViewSetup *view) {
if (delta < 0) {
delta = 0;
last_cl_time = cl_time;
} else if (delta >= 1.0f / 60.0f) {
} else if (delta >= engine->GetIPT()) {
doMovement(delta);
last_cl_time = cl_time;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Features/Tas/TasPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ void SetPlaybackVars(bool active) {
old_interpolate = cl_interpolate.GetBool();
old_motionblur = mat_motion_blur_enabled.GetBool();
in_forceuser.SetValue(tasPlayer->playbackInfo.coopControlSlot >= 0 ? tasPlayer->playbackInfo.coopControlSlot : 100);
host_framerate.SetValue(60);
auto tickrate = 1.0f / engine->GetIPT();
if (fabsf(tickrate - roundf(tickrate)) < 0.0001f) {
// if it's close to a whole number, set it as an int
// (preempting floating point errors)
host_framerate.SetValue((int)roundf(tickrate));
} else {
host_framerate.SetValue(tickrate);
}
if (!sar_tas_interpolate.GetBool() && tasPlayer->IsUsingTools()) {
cl_interpolate.SetValue(false);
mat_motion_blur_enabled.SetValue(false);
Expand Down Expand Up @@ -90,7 +97,7 @@ void SetPlaybackVars(bool active) {
fps_max.SetValue(0);
} else if (tasPlayer->GetTick() >= sar_tas_skipto.GetInt()) {
engine->SetSkipping(false);
fps_max.SetValue((int)(sar_tas_playback_rate.GetFloat() * 60.0f));
fps_max.SetValue((int)(sar_tas_playback_rate.GetFloat() * sar.game->Tickrate()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Modules/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ void Host_AccumulateTime_Detour(float dt) {
Host_AccumulateTime_Hook.Enable();
} else if (g_advance > 0) {
Host_AccumulateTime_Hook.Disable();
Host_AccumulateTime(1.0f/60);
Host_AccumulateTime(engine->GetIPT());
Host_AccumulateTime_Hook.Enable();
--g_advance;
} else {
Expand All @@ -720,7 +720,7 @@ void Host_AccumulateTime_Detour(float dt) {
// HACK: Force frametime to equal 2 ticks when loading
// Limits host_timescale effect on load times, faster loads
if (g_loadstate == LOADING && sar_loads_uncap.GetBool()) {
*host_frametime = *host_frametime_unbounded = 2.0f/60;
*host_frametime = *host_frametime_unbounded = 2.0f * engine->GetIPT();
}

if (*host_frametime != *host_frametime_unbounded) {
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ static void __cdecl AcceptInput_Hook(void *thisptr, const char *inputName, void
}
if (end) {
if (transition_time != 0) {
auto time = (session->GetTick() - transition_time) / 60.0f;
auto time = (session->GetTick() - transition_time) * engine->GetIPT();
toastHud.AddToast("transition", Utils::ssprintf("Transition lost %.3f sec", time));
transition_time = 0;
}
Expand Down

0 comments on commit 886ea72

Please sign in to comment.