Console: Add commands for conditional execution from game date
This commit is contained in:
@@ -487,6 +487,15 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
|
||||
DEBUG(console, 8, "Token %d is: '%s'", i, tokens[i]);
|
||||
}
|
||||
|
||||
IConsoleCmdExecTokens(t_index, tokens, recurse_count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a given command passed to us as tokens
|
||||
* @param cmdstr string to be parsed and executed
|
||||
*/
|
||||
void IConsoleCmdExecTokens(uint token_count, char *tokens[], const uint recurse_count)
|
||||
{
|
||||
if (StrEmpty(tokens[0])) return; // don't execute empty commands
|
||||
/* 2. Determine type of command (cmd or alias) and execute
|
||||
* First try commands, then aliases. Execute
|
||||
@@ -498,7 +507,7 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
|
||||
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
||||
switch (chr) {
|
||||
case CHR_ALLOW:
|
||||
if (!cmd->proc(t_index, tokens)) { // index started with 0
|
||||
if (!cmd->proc(token_count, tokens)) { // index started with 0
|
||||
cmd->proc(0, nullptr); // if command failed, give help
|
||||
}
|
||||
return;
|
||||
@@ -508,10 +517,10 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
|
||||
}
|
||||
}
|
||||
|
||||
t_index--;
|
||||
token_count--;
|
||||
IConsoleAlias *alias = IConsoleAliasGet(tokens[0]);
|
||||
if (alias != nullptr) {
|
||||
IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
|
||||
IConsoleAliasExec(alias, token_count, &tokens[1], recurse_count + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@@ -2832,6 +2832,54 @@ DEF_CONSOLE_CMD(ConSwitchBaseset)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool ConConditionalCommon(byte argc, char *argv[], int value, const char *value_name, const char *name)
|
||||
{
|
||||
if (argc < 4) {
|
||||
IConsolePrintF(CC_WARNING, "- Execute command if %s is within the specified range. Usage: '%s <minimum> <maximum> <command...>'", value_name, name);
|
||||
return true;
|
||||
}
|
||||
|
||||
int min_value = atoi(argv[1]);
|
||||
int max_value = atoi(argv[2]);
|
||||
|
||||
if (value >= min_value && value <= max_value) IConsoleCmdExecTokens(argc - 3, argv + 3);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConIfYear)
|
||||
{
|
||||
return ConConditionalCommon(argc, argv, _cur_date_ymd.year, "the current year (in game)", "if_year");
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConIfMonth)
|
||||
{
|
||||
return ConConditionalCommon(argc, argv, _cur_date_ymd.month + 1, "the current month (in game)", "if_month");
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConIfDay)
|
||||
{
|
||||
return ConConditionalCommon(argc, argv, _cur_date_ymd.day, "the current day of the month (in game)", "if_day");
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConIfHour)
|
||||
{
|
||||
Minutes minutes = _scaled_date_ticks / _settings_time.ticks_per_minute + _settings_time.clock_offset;
|
||||
return ConConditionalCommon(argc, argv, MINUTES_HOUR(minutes), "the current hour (in game, assuming time is in minutes)", "if_hour");
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConIfMinute)
|
||||
{
|
||||
Minutes minutes = _scaled_date_ticks / _settings_time.ticks_per_minute + _settings_time.clock_offset;
|
||||
return ConConditionalCommon(argc, argv, MINUTES_MINUTE(minutes), "the current minute (in game, assuming time is in minutes)", "if_minute");
|
||||
}
|
||||
|
||||
DEF_CONSOLE_CMD(ConIfHourMinute)
|
||||
{
|
||||
Minutes minutes = _scaled_date_ticks / _settings_time.ticks_per_minute + _settings_time.clock_offset;
|
||||
return ConConditionalCommon(argc, argv, (MINUTES_HOUR(minutes) * 100) + MINUTES_MINUTE(minutes), "the current hour and minute 0000 - 2359 (in game, assuming time is in minutes)", "if_hour_minute");
|
||||
}
|
||||
|
||||
#ifdef _DEBUG
|
||||
/******************
|
||||
* debug commands
|
||||
@@ -3012,6 +3060,14 @@ void IConsoleStdLibRegister()
|
||||
IConsoleAliasRegister("min_players", "setting min_active_clients %+");
|
||||
IConsoleAliasRegister("reload_cfg", "setting reload_cfg %+");
|
||||
|
||||
/* conditionals */
|
||||
IConsoleCmdRegister("if_year", ConIfYear);
|
||||
IConsoleCmdRegister("if_month", ConIfMonth);
|
||||
IConsoleCmdRegister("if_day", ConIfDay);
|
||||
IConsoleCmdRegister("if_hour", ConIfHour);
|
||||
IConsoleCmdRegister("if_minute", ConIfMinute);
|
||||
IConsoleCmdRegister("if_hour_minute", ConIfHourMinute);
|
||||
|
||||
/* debugging stuff */
|
||||
#ifdef _DEBUG
|
||||
IConsoleDebugLibRegister();
|
||||
|
@@ -29,6 +29,7 @@ void IConsoleError(const char *string);
|
||||
|
||||
/* Parser */
|
||||
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count = 0);
|
||||
void IConsoleCmdExecTokens(uint token_count, char *tokens[], const uint recurse_count = 0);
|
||||
|
||||
bool IsValidConsoleColour(TextColour c);
|
||||
|
||||
|
Reference in New Issue
Block a user