Skip to content

Commit

Permalink
refactor: use event for sar_on_stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisAMJ committed Oct 4, 2024
1 parent e989e77 commit 37e6083
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/cvars.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@
|sar_on_speedrun_finish|cmd|sar_on_speedrun_finish \<command> [args]... - registers a command to be run when a speedrun finishes|
|sar_on_speedrun_finish_clear|cmd|sar_on_speedrun_finish_clear - clears commands registered on event "speedrun_finish"|
|sar_on_speedrun_finish_list|cmd|sar_on_speedrun_finish_list - lists commands registered on event "speedrun_finish"|
|sar_on_stuck|cmd|sar_on_stuck \<command> [args]... - registers a command to be run when the player gets stuck (singleplayer) (requires cheats)|
|sar_on_stuck_clear|cmd|sar_on_stuck_clear - clears commands registered on event "stuck"|
|sar_on_stuck_list|cmd|sar_on_stuck_list - lists commands registered on event "stuck"|
|sar_on_tas_end|cmd|sar_on_tas_end \<command> [args]... - registers a command to be run when TAS script playback ends|
|sar_on_tas_end_clear|cmd|sar_on_tas_end_clear - clears commands registered on event "tas_end"|
|sar_on_tas_end_list|cmd|sar_on_tas_end_list - lists commands registered on event "tas_end"|
Expand Down
1 change: 1 addition & 0 deletions src/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Event {
SPEEDRUN_FINISH,
RENDERER_START,
RENDERER_FINISH,
STUCK,
};

template <EventType E>
Expand Down
7 changes: 7 additions & 0 deletions src/Features/ConfigPlus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Modules/Client.hpp"
#include "Modules/Engine.hpp"
#include "Modules/FileSystem.hpp"
#include "Modules/Server.hpp"

#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -855,6 +856,7 @@ MK_SAR_ON(cfg_message, "when partner sends a custom message (_sar_cfg_message sv
MK_SAR_ON(speedrun_finish, "when a speedrun finishes", true)
MK_SAR_ON(renderer_start, "when renderer starts", true)
MK_SAR_ON(renderer_finish, "when renderer finishes", true)
MK_SAR_ON(stuck, "when the player gets stuck (singleplayer) (requires cheats)", true)

ON_EVENT_P(SESSION_START, 1000000) {
RUN_EXECS(load);
Expand Down Expand Up @@ -910,6 +912,11 @@ ON_EVENT(RENDERER_START) {
ON_EVENT(RENDERER_FINISH) {
RUN_EXECS(renderer_finish);
}
ON_EVENT(STUCK) {
if (sv_cheats.GetBool()) {
RUN_EXECS(stuck);
}
}

struct Seq {
std::queue<std::string> commands;
Expand Down
17 changes: 4 additions & 13 deletions src/Modules/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,14 @@ DETOUR_T(int, Server::TryPlayerMove, Vector *pFirstDest, CGameTrace *pFirstTrace
return result;
}

std::string onStuckCommands;
CON_COMMAND(sar_on_stuck, "sar_on_stuck <command> - registers a command to be run when the player gets stuck.\n") {
if (args.ArgC() < 2) return console->Print(sar_on_stuck.ThisPtr()->m_pszHelpString);
if (!sv_cheats.GetBool()) return console->Print("sar_on_stuck requires sv_cheats.\n");

const char *cmd = Utils::ArgContinuation(args, 1);
onStuckCommands = std::string(cmd);
}

// CGameMovement::CheckStuck
static bool wasStuck = false;
DETOUR_T(int, Server::CheckStuck) {
int result = Server::CheckStuck(thisptr);

if (sv_cheats.GetBool() && result == 1 && !onStuckCommands.empty()) {
engine->ExecuteCommand(onStuckCommands.c_str());
onStuckCommands.clear();
if (result == 1 && !wasStuck) { // rising edge
Event::Trigger<Event::STUCK>({});
}
wasStuck = result == 1;

return result;
}
Expand Down

0 comments on commit 37e6083

Please sign in to comment.