Skip to content

Commit 886ea72

Browse files
committed
refactor: use tickrate everywhere
1 parent 8747741 commit 886ea72

File tree

11 files changed

+35
-44
lines changed

11 files changed

+35
-44
lines changed

src/Features/Camera.cpp

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ CameraState Camera::InterpolateStates(float time) {
209209
LAST };
210210

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

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

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

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

350350
Vector forward, right, up;
351-
float cp, sp, cy, sy, cr, sr;
352-
cp = cosf(DEG2RAD(state.angles.x));
353-
sp = sinf(DEG2RAD(state.angles.x));
354-
cy = cosf(DEG2RAD(state.angles.y));
355-
sy = sinf(DEG2RAD(state.angles.y));
356-
cr = cosf(DEG2RAD(state.angles.z));
357-
sr = sinf(DEG2RAD(state.angles.z));
358-
359-
forward.x = cp * cy;
360-
forward.y = cp * sy;
361-
forward.z = -sp;
362-
363-
right.x = (-1 * sr * sp * cy + -1 * cr * -sy);
364-
right.y = (-1 * sr * sp * sy + -1 * cr * cy);
365-
right.z = -1 * sr * cp;
366-
367-
up = right.Cross(forward);
368-
351+
Math::AngleVectors(state.angles, &forward, &right, &up);
369352

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

@@ -405,7 +388,7 @@ void Camera::OverrideView(CViewSetup *m_View) {
405388
}
406389

407390
if (timeOffsetRefreshRequested) {
408-
timeOffset = engine->GetClientTime() - engine->demoplayer->GetTick() / 60.0f;
391+
timeOffset = engine->GetClientTime() - engine->demoplayer->GetTick() * engine->GetIPT();
409392
timeOffsetRefreshRequested = false;
410393
}
411394

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

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

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

906889
// changing in-game ticks to seconds.
907-
float maxTime = maxTimeTicks / 60.0f;
908-
float minTime = minTimeTicks / 60.0f;
890+
float maxTime = maxTimeTicks * engine->GetIPT();
891+
float minTime = minTimeTicks * engine->GetIPT();
909892

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

src/Features/Demo/DemoParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ bool DemoParser::Parse(std::string filePath, Demo *demo, bool ghostRequest, std:
228228
}
229229

230230
if (cmd.find("__END__") != std::string::npos) {
231-
console->ColorMsg(Color(0, 255, 0, 255), "Segment length -> %d ticks: %.3fs\n", tick, tick / 60.f);
231+
console->ColorMsg(Color(0, 255, 0, 255), "Segment length -> %d ticks: %.3fs\n", tick, tick * engine->GetIPT());
232232
demo->segmentTicks = tick;
233233
}
234234
break;

src/Features/Demo/GhostLeaderboard.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Variable ghost_leaderboard_y("ghost_leaderboard_y", "10", 0, "The x position of
1919

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

2525
void GhostLeaderboardHud::Paint(int slot) {
@@ -239,7 +239,7 @@ void GhostLeaderboardHud::SpeedrunStart(int ticks) {
239239
return;
240240
} else { // mode == 1
241241
// race
242-
this->lastLiveUpdate = (int)(engine->GetHostTime() * 60.0f);
242+
this->lastLiveUpdate = (int)(engine->GetHostTime() * sar.game->Tickrate());
243243
for (auto &ent : ghostLeaderboard.entries) {
244244
ent.ticks = ticks;
245245
ent.waiting = false;
@@ -251,7 +251,7 @@ void GhostLeaderboardHud::SpeedrunStart(int ticks) {
251251
void GhostLeaderboardHud::UpdateLive() {
252252
if (ghost_leaderboard_mode.GetInt() != 1) return;
253253

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

src/Features/DiscordRichPresence.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ ON_EVENT(FRAME) {
153153
int host, server, client;
154154
engine->GetTicks(host, server, client);
155155

156-
if (host > lastExecutedPresence + 60) {
156+
if (host > lastExecutedPresence + sar.game->Tickrate()) {
157157
lastExecutedPresence = host;
158158
UpdateDiscordRichPresence();
159159
}

src/Features/Hud/PlacementHelperHud.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ ON_EVENT(RENDER) {
8888
auto deferringToPortal = placementHelper->field<bool>("m_bDeferringToPortal");
8989

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

9494
auto sphereColor = Color(0, 200, 0, 64);

src/Features/Speedrun/SpeedrunTimer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "Utils.hpp"
3030

3131
#define SPEEDRUN_PACKET_TYPE "srtimer"
32-
#define SYNC_INTERVAL 60 // Sync every second, just in case
32+
#define SYNC_INTERVAL 1.0f // Sync every second, just in case
3333

3434
enum PacketType {
3535
SYNC,
@@ -316,7 +316,7 @@ int SpeedrunTimer::GetOffsetTicks() {
316316

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

322322
return ticks;
@@ -443,7 +443,7 @@ void SpeedrunTimer::Update() {
443443

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

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

10001000
CON_COMMAND(sar_speedrun_export_all, "sar_speedrun_export_all <filename> - export the results of many speedruns to the specified CSV file\n") {

src/Features/SteamTimeline.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "SteamTimeline.hpp"
22

33
#include "Event.hpp"
4+
#include "Modules/Engine.hpp"
45
#include "Modules/SteamAPI.hpp"
56
#include "Speedrun/SpeedrunTimer.hpp"
67

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

45-
auto fl_time = SpeedrunTimer::GetTotalTicks() / 60.0f;
46+
auto fl_time = SpeedrunTimer::GetTotalTicks() * engine->GetIPT();
4647
auto time = SpeedrunTimer::Format(fl_time);
4748

4849
steam->g_timeline->SetTimelineStateDescription(("Speedrun " + time).c_str(), -offset.count());

src/Features/Stitcher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void Stitcher::OverrideView(CViewSetup *view) {
629629
if (delta < 0) {
630630
delta = 0;
631631
last_cl_time = cl_time;
632-
} else if (delta >= 1.0f / 60.0f) {
632+
} else if (delta >= engine->GetIPT()) {
633633
doMovement(delta);
634634
last_cl_time = cl_time;
635635
}

src/Features/Tas/TasPlayer.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ void SetPlaybackVars(bool active) {
5151
old_interpolate = cl_interpolate.GetBool();
5252
old_motionblur = mat_motion_blur_enabled.GetBool();
5353
in_forceuser.SetValue(tasPlayer->playbackInfo.coopControlSlot >= 0 ? tasPlayer->playbackInfo.coopControlSlot : 100);
54-
host_framerate.SetValue(60);
54+
auto tickrate = 1.0f / engine->GetIPT();
55+
if (fabsf(tickrate - roundf(tickrate)) < 0.0001f) {
56+
// if it's close to a whole number, set it as an int
57+
// (preempting floating point errors)
58+
host_framerate.SetValue((int)roundf(tickrate));
59+
} else {
60+
host_framerate.SetValue(tickrate);
61+
}
5562
if (!sar_tas_interpolate.GetBool() && tasPlayer->IsUsingTools()) {
5663
cl_interpolate.SetValue(false);
5764
mat_motion_blur_enabled.SetValue(false);
@@ -90,7 +97,7 @@ void SetPlaybackVars(bool active) {
9097
fps_max.SetValue(0);
9198
} else if (tasPlayer->GetTick() >= sar_tas_skipto.GetInt()) {
9299
engine->SetSkipping(false);
93-
fps_max.SetValue((int)(sar_tas_playback_rate.GetFloat() * 60.0f));
100+
fps_max.SetValue((int)(sar_tas_playback_rate.GetFloat() * sar.game->Tickrate()));
94101
}
95102
}
96103

src/Modules/Engine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ void Host_AccumulateTime_Detour(float dt) {
709709
Host_AccumulateTime_Hook.Enable();
710710
} else if (g_advance > 0) {
711711
Host_AccumulateTime_Hook.Disable();
712-
Host_AccumulateTime(1.0f/60);
712+
Host_AccumulateTime(engine->GetIPT());
713713
Host_AccumulateTime_Hook.Enable();
714714
--g_advance;
715715
} else {
@@ -720,7 +720,7 @@ void Host_AccumulateTime_Detour(float dt) {
720720
// HACK: Force frametime to equal 2 ticks when loading
721721
// Limits host_timescale effect on load times, faster loads
722722
if (g_loadstate == LOADING && sar_loads_uncap.GetBool()) {
723-
*host_frametime = *host_frametime_unbounded = 2.0f/60;
723+
*host_frametime = *host_frametime_unbounded = 2.0f * engine->GetIPT();
724724
}
725725

726726
if (*host_frametime != *host_frametime_unbounded) {

0 commit comments

Comments
 (0)