From c4b1596c9784358e6f3e718d1003b663773e1d63 Mon Sep 17 00:00:00 2001 From: Michal Charemza Date: Mon, 1 Jul 2024 10:33:42 +0100 Subject: [PATCH] Add: [Console] schedule command to execute a script file next in-game month (#12761) --- src/console_cmds.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 4656ecc52f..5b370cc0f5 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -29,7 +29,7 @@ #include "strings_func.h" #include "viewport_func.h" #include "window_func.h" -#include "timer/timer_game_calendar.h" +#include "timer/timer.h" #include "company_func.h" #include "gamelog.h" #include "ai/ai.hpp" @@ -53,6 +53,24 @@ /* scriptfile handling */ static uint _script_current_depth; ///< Depth of scripts running (used to abort execution when #ConReturn is encountered). +/* Scheduled execution handling. */ +static std::string _scheduled_monthly_script; ///< Script scheduled to execute by the 'schedule' console command (empty if no script is scheduled). + +/** Timer that runs every month of game time for the 'schedule' console command. */ +static IntervalTimer _scheduled_monthly_timer = {{TimerGameCalendar::MONTH, TimerGameCalendar::Priority::NONE}, [](auto) { + if (_scheduled_monthly_script.empty()) { + return; + } + + /* Clear the schedule before rather than after the script to allow the script to itself call + * schedule without it getting immediately cleared. */ + const std::string filename = _scheduled_monthly_script; + _scheduled_monthly_script.clear(); + + IConsolePrint(CC_DEFAULT, "Executing scheduled script file '{}'...", filename); + IConsoleCmdExec(std::string("exec") + " " + filename); +}}; + /** File list storage for the console, for caching the last 'ls' command. */ class ConsoleFileList : public FileList { public: @@ -1154,6 +1172,35 @@ DEF_CONSOLE_CMD(ConExec) return true; } +DEF_CONSOLE_CMD(ConSchedule) +{ + if (argc < 3 || std::string_view(argv[1]) != "on-next-calendar-month") { + IConsolePrint(CC_HELP, "Schedule a local script to execute later. Usage: 'schedule on-next-calendar-month