Add function to enqueue a DoCommandP call

This commit is contained in:
Jonathan G Rennison
2022-09-14 21:55:28 +01:00
parent 5cb6d2240a
commit bd2593ca48
4 changed files with 41 additions and 0 deletions

View File

@@ -34,6 +34,7 @@
#include "debug_desync.h" #include "debug_desync.h"
#include "order_backup.h" #include "order_backup.h"
#include <array> #include <array>
#include <deque>
#include "table/strings.h" #include "table/strings.h"
@@ -585,6 +586,12 @@ struct CommandLog {
static CommandLog _command_log; static CommandLog _command_log;
static CommandLog _command_log_aux; static CommandLog _command_log_aux;
struct CommandQueueItem {
CommandContainer cmd;
CompanyID company;
};
static std::deque<CommandQueueItem> _command_queue;
void ClearCommandLog() void ClearCommandLog()
{ {
_command_log.Reset(); _command_log.Reset();
@@ -973,6 +980,33 @@ CommandCost DoCommandPScript(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, ui
return res; return res;
} }
void ExecuteCommandQueue()
{
while (!_command_queue.empty()) {
Backup<CompanyID> cur_company(_current_company, FILE_LINE);
cur_company.Change(_command_queue.front().company);
DoCommandP(&_command_queue.front().cmd);
cur_company.Restore();
_command_queue.pop_front();
}
}
void ClearCommandQueue()
{
_command_queue.clear();
}
void EnqueueDoCommandP(CommandContainer cmd)
{
if (_docommand_recursive == 0) {
DoCommandP(&cmd);
} else {
CommandQueueItem &item = _command_queue.emplace_back();
item.cmd = std::move(cmd);
item.company = _current_company;
}
}
/** /**
* Helper to deduplicate the code for returning. * Helper to deduplicate the code for returning.

View File

@@ -85,6 +85,10 @@ static inline DoCommandFlag CommandFlagsToDCFlags(CommandFlags cmd_flags)
void ClearCommandLog(); void ClearCommandLog();
char *DumpCommandLog(char *buffer, const char *last); char *DumpCommandLog(char *buffer, const char *last);
void ExecuteCommandQueue();
void ClearCommandQueue();
void EnqueueDoCommandP(CommandContainer cmd);
/*** All command callbacks that exist ***/ /*** All command callbacks that exist ***/
/* ai/ai_instance.cpp */ /* ai/ai_instance.cpp */

View File

@@ -79,6 +79,7 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin
ViewportMapClearTunnelCache(); ViewportMapClearTunnelCache();
ClearCommandLog(); ClearCommandLog();
ClearCommandQueue();
ClearSpecialEventsLog(); ClearSpecialEventsLog();
ClearDesyncMsgLog(); ClearDesyncMsgLog();

View File

@@ -467,6 +467,7 @@ static void ShutdownGame()
ClearVehicleTickCaches(); ClearVehicleTickCaches();
InvalidateTemplateReplacementImages(); InvalidateTemplateReplacementImages();
ClearCommandLog(); ClearCommandLog();
ClearCommandQueue();
ClearSpecialEventsLog(); ClearSpecialEventsLog();
ClearDesyncMsgLog(); ClearDesyncMsgLog();
@@ -2078,6 +2079,7 @@ void GameLoop()
/* Singleplayer */ /* Singleplayer */
StateGameLoop(); StateGameLoop();
} }
ExecuteCommandQueue();
if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) { if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) {
extern std::mutex _cur_palette_mutex; extern std::mutex _cur_palette_mutex;