Skip to content

Commit 70a6f68

Browse files
icyytearsmlugg
authored andcommitted
feat: legacy mtriggers with some extra spice
1 parent 68abb41 commit 70a6f68

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

src/Features/Speedrun/Categories.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ std::vector<std::string> SpeedrunTimer::GetCategoryRules() {
231231
return g_categories[g_currentCategory].rules;
232232
}
233233

234+
std::string SpeedrunTimer::GetCategoryName() {
235+
return g_currentCategory;
236+
}
237+
234238
void SpeedrunTimer::ResetCategory() {
235239
for (std::string ruleName : g_categories[g_currentCategory].rules) {
236240
auto rule = SpeedrunTimer::GetRule(ruleName);

src/Features/Speedrun/Categories.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace SpeedrunTimer {
2626
bool AddRuleToCategory(std::string category, std::string rule);
2727
bool CreateRule(std::string name, std::string type, std::map<std::string, std::string> params);
2828
std::vector<std::string> GetCategoryRules();
29+
std::string GetCategoryName();
2930
}; // namespace SpeedrunTimer
3031

3132
extern Command sar_speedrun_category;

src/Features/Speedrun/SpeedrunTimer.cpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ static int g_coopLastSyncTick;
152152
// Orange only - the tick we synced, as reported by the engine
153153
static int g_coopLastSyncEngineTick;
154154

155+
static std::string g_chatTextFormat = "!seg -> !tt (!st)";
156+
static Color g_chatTextColor = Color{255, 176, 0};
157+
155158
static void handleCoopPacket(const void *data, size_t size) {
156159
if (!engine->IsOrange()) return;
157160

@@ -445,7 +448,10 @@ void SpeedrunTimer::Start() {
445448
g_speedrun.visitedMaps.push_back(map);
446449

447450
sendCoopPacket(PacketType::START);
448-
toastHud.AddToast(SPEEDRUN_TOAST_TAG, "Speedrun started!");
451+
if (!sar_mtrigger_legacy.GetBool())
452+
toastHud.AddToast(SPEEDRUN_TOAST_TAG, "Speedrun started!");
453+
else
454+
client->Chat(g_chatTextColor, "Speedrun started!");
449455

450456
ghostLeaderboard.SpeedrunStart(g_speedrun.saved);
451457
}
@@ -582,6 +588,14 @@ void SpeedrunTimer::Stop(std::string segName) {
582588
});
583589
}
584590

591+
bool replace(std::string &str, const std::string &from, const std::string &to) {
592+
size_t start_pos = str.find(from);
593+
if (start_pos == std::string::npos)
594+
return false;
595+
str.replace(start_pos, from.length(), to);
596+
return true;
597+
}
598+
585599
void SpeedrunTimer::Split(bool newSplit, std::string segName, bool requested) {
586600
if (!g_speedrun.isRunning) {
587601
return;
@@ -621,8 +635,21 @@ void SpeedrunTimer::Split(bool newSplit, std::string segName, bool requested) {
621635
setTimerAction(TimerAction::SPLIT);
622636
float totalTime = SpeedrunTimer::GetTotalTicks() * *engine->interval_per_tick;
623637
float splitTime = g_speedrun.splits.back().ticks * *engine->interval_per_tick;
624-
std::string text = Utils::ssprintf("%s\n%s (%s)", segName.c_str(), SpeedrunTimer::Format(totalTime).c_str(), SpeedrunTimer::Format(splitTime).c_str());
625-
toastHud.AddToast(SPEEDRUN_TOAST_TAG, text);
638+
if (!sar_mtrigger_legacy.GetBool()) {
639+
std::string text = Utils::ssprintf("%s\n%s (%s)", segName.c_str(), SpeedrunTimer::Format(totalTime).c_str(), SpeedrunTimer::Format(splitTime).c_str());
640+
toastHud.AddToast(SPEEDRUN_TOAST_TAG, text);
641+
} else {
642+
std::string cleanSegName = segName;
643+
replace(cleanSegName, GetCategoryName() + " - ", "");
644+
645+
std::string formattedString = g_chatTextFormat;
646+
replace(formattedString, "!map", GetCategoryName());
647+
replace(formattedString, "!seg", cleanSegName);
648+
replace(formattedString, "!tt", SpeedrunTimer::Format(totalTime));
649+
replace(formattedString, "!st", SpeedrunTimer::Format(splitTime));
650+
651+
client->Chat(g_chatTextColor, formattedString.c_str());
652+
}
626653
}
627654
}
628655

@@ -762,6 +789,8 @@ Variable sar_speedrun_start_on_load("sar_speedrun_start_on_load", "0", 0, 2, "Au
762789
Variable sar_speedrun_offset("sar_speedrun_offset", "0", 0, "Start speedruns with this time on the timer.\n", 0);
763790
Variable sar_speedrun_autostop("sar_speedrun_autostop", "0", 0, 2, "Automatically stop recording demos when a speedrun finishes. If 2, automatically append the run time to the demo name.\n");
764791

792+
Variable sar_mtrigger_legacy("sar_mtrigger_legacy", "0", 0, 1, "\n");
793+
765794
CON_COMMAND(sar_speedrun_start, "sar_speedrun_start - start the speedrun timer\n") {
766795
SpeedrunTimer::Start();
767796
}
@@ -1035,3 +1064,32 @@ ON_EVENT(PRE_TICK) {
10351064
engine->ExecuteCommand("restart_level");
10361065
}
10371066
}
1067+
1068+
CON_COMMAND(sar_mtrigger_legacy_format, "sar_mtrigger_legacy_format <string format> - formatting of the text that is displayed in the chat (!map - for map name, !seg - for segment name, !tt - for total time, !st - for split time). ( def. \"!seg -> !tt (!st)\" )\n") {
1069+
if (args.ArgC() != 2) {
1070+
console->Print(sar_mtrigger_legacy_format.ThisPtr()->m_pszHelpString);
1071+
return;
1072+
}
1073+
1074+
g_chatTextFormat = args[1];
1075+
}
1076+
1077+
CON_COMMAND(sar_mtrigger_legacy_textcolor, "sar_mtrigger_legacy_textcolor <hex code> - the color of the text that is displayed in the chat.\n") {
1078+
if (args.ArgC() != 2) {
1079+
console->Print(sar_mtrigger_legacy_textcolor.ThisPtr()->m_pszHelpString);
1080+
return;
1081+
}
1082+
1083+
const char *color = args[1];
1084+
if (color[0] == '#') {
1085+
++color;
1086+
}
1087+
1088+
int r, g, b;
1089+
int end;
1090+
if (sscanf(color, "%2x%2x%2x%n", &r, &g, &b, &end) != 3 || end != 6) {
1091+
return console->Print("Invalid color code!\n");
1092+
}
1093+
1094+
g_chatTextColor = Color{(uint8_t)r, (uint8_t)g, (uint8_t)b};
1095+
}

src/Features/Speedrun/SpeedrunTimer.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ extern Variable sar_speedrun_start_on_load;
4545
extern Variable sar_speedrun_offset;
4646
extern Variable sar_speedrun_autostop;
4747

48+
extern Variable sar_mtrigger_legacy;
49+
4850
extern Command sar_speedrun_start;
4951
extern Command sar_speedrun_stop;
5052
extern Command sar_speedrun_split;
@@ -53,3 +55,6 @@ extern Command sar_speedrun_resume;
5355
extern Command sar_speedrun_reset;
5456
extern Command sar_speedrun_result;
5557
extern Command sar_speedrun_export;
58+
59+
extern Command sar_mtrigger_legacy_format;
60+
extern Command sar_mtrigger_legacy_textcolor;

src/Modules/Client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ DETOUR(Client::ProcessMovement, void *player, CMoveData *move) {
447447
}
448448

449449
CON_COMMAND(sar_chat, "sar_chat - open the chat HUD\n") {
450-
if (engine->IsCoop()) client->OpenChat();
450+
client->OpenChat();
451451
}
452452

453453
extern Hook g_DrawTranslucentRenderablesHook;

0 commit comments

Comments
 (0)