GS: Add GSAsyncMode(bool) class to set async mode of script DoCommands

In asynchronous mode, don't wait for result of executed command,
just fire-and-forget, and return estimated cost/result
This commit is contained in:
Jonathan G Rennison
2023-05-23 20:52:08 +01:00
parent df6c35a48a
commit cd9930542d
9 changed files with 214 additions and 5 deletions

View File

@@ -589,6 +589,7 @@ enum CommandLogEntryFlag : uint16 {
CLEF_TWICE = 0x100, ///< command logged twice (only sending and execution)
CLEF_RANDOM = 0x200, ///< command changed random seed
CLEF_ORDER_BACKUP = 0x400, ///< command changed order backups
CLEF_SCRIPT_ASYNC = 0x800, ///< command run by AI/game script - asynchronous
};
DECLARE_ENUM_AS_BIT_SET(CommandLogEntryFlag)
@@ -650,6 +651,11 @@ static void DumpSubCommandLogEntry(char *&buffer, const char *last, const Comman
return entry.log_flags & flag ? c : '-';
};
auto script_fc = [&]() -> char {
if (!(entry.log_flags & CLEF_SCRIPT)) return '-';
return (entry.log_flags & CLEF_SCRIPT_ASYNC) ? 'A' : 'a';
};
YearMonthDay ymd;
ConvertDateToYMD(entry.date, &ymd);
buffer += seprintf(buffer, last, "%4i-%02i-%02i, %2i, %3i", ymd.year, ymd.month + 1, ymd.day, entry.date_fract, entry.tick_skip_counter);
@@ -658,7 +664,7 @@ static void DumpSubCommandLogEntry(char *&buffer, const char *last, const Comman
}
buffer += seprintf(buffer, last, " | %c%c%c%c%c%c%c%c%c%c%c | ",
fc(CLEF_ORDER_BACKUP, 'o'), fc(CLEF_RANDOM, 'r'), fc(CLEF_TWICE, '2'),
fc(CLEF_SCRIPT, 'a'), fc(CLEF_AUX_DATA, 'b'), fc(CLEF_MY_CMD, 'm'), fc(CLEF_ONLY_SENDING, 's'),
script_fc(), fc(CLEF_AUX_DATA, 'b'), fc(CLEF_MY_CMD, 'm'), fc(CLEF_ONLY_SENDING, 's'),
fc(CLEF_ESTIMATE_ONLY, 'e'), fc(CLEF_TEXT, 't'), fc(CLEF_GENERATING_WORLD, 'g'), fc(CLEF_CMD_FAILED, 'f'));
buffer += seprintf(buffer, last, " %7d x %7d, p1: 0x%08X, p2: 0x%08X, ",
TileX(entry.tile), TileY(entry.tile), entry.p1, entry.p2);
@@ -1024,7 +1030,7 @@ bool DoCommandPEx(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd, C
return res.Succeeded();
}
CommandCost DoCommandPScript(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only, const CommandAuxiliaryBase *aux_data)
CommandCost DoCommandPScript(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd, CommandCallback *callback, const char *text, bool my_cmd, bool estimate_only, bool asynchronous, const CommandAuxiliaryBase *aux_data)
{
GameRandomSeedChecker random_state;
uint order_backup_update_counter = OrderBackup::GetUpdateCounter();
@@ -1033,6 +1039,7 @@ CommandCost DoCommandPScript(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, ui
CommandLogEntryFlag log_flags;
log_flags = CLEF_SCRIPT;
if (asynchronous) log_flags |= CLEF_SCRIPT_ASYNC;
if (!StrEmpty(text)) log_flags |= CLEF_TEXT;
if (estimate_only) log_flags |= CLEF_ESTIMATE_ONLY;
if (_networking && !(cmd & CMD_NETWORK_COMMAND)) log_flags |= CLEF_ONLY_SENDING;