From caa490642cc62cc6c7192ba05290742b76f4fe56 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 15 Feb 2019 08:43:05 +0100 Subject: [PATCH] Refactor GameActions to allow non-top level actions. --- src/openrct2/actions/GameAction.cpp | 56 +++++++++++++++++------------ src/openrct2/actions/GameAction.h | 4 +-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/openrct2/actions/GameAction.cpp b/src/openrct2/actions/GameAction.cpp index efcdfd207b..b6ec252a75 100644 --- a/src/openrct2/actions/GameAction.cpp +++ b/src/openrct2/actions/GameAction.cpp @@ -137,7 +137,7 @@ namespace GameActions return false; } - GameActionResult::Ptr Query(const GameAction* action) + GameActionResult::Ptr Query(const GameAction* action, bool topLevel /* = true */) { Guard::ArgumentNotNull(action); @@ -155,9 +155,13 @@ namespace GameActions auto result = action->Query(); - gCommandPosition.x = result->Position.x; - gCommandPosition.y = result->Position.y; - gCommandPosition.z = result->Position.z; + // Only top level actions affect the command position. + if (topLevel == true) + { + gCommandPosition.x = result->Position.x; + gCommandPosition.y = result->Position.y; + gCommandPosition.z = result->Position.z; + } if (result->Error == GA_ERROR::OK) { @@ -223,7 +227,7 @@ namespace GameActions network_append_server_log(text); } - GameActionResult::Ptr Execute(const GameAction* action) + GameActionResult::Ptr Execute(const GameAction* action, bool topLevel /* = true */) { Guard::ArgumentNotNull(action); @@ -247,31 +251,34 @@ namespace GameActions } } - GameActionResult::Ptr result = Query(action); + GameActionResult::Ptr result = Query(action, topLevel); if (result->Error == GA_ERROR::OK) { - // Networked games send actions to the server to be run - if (network_get_mode() == NETWORK_MODE_CLIENT) + if (topLevel) { - // As a client we have to wait or send it first. - if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED)) + // Networked games send actions to the server to be run + if (network_get_mode() == NETWORK_MODE_CLIENT) { - log_verbose("[%s] GameAction::Execute %s (Out)", GetRealm(), action->GetName()); - network_send_game_action(action); + // As a client we have to wait or send it first. + if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED)) + { + log_verbose("[%s] GameAction::Execute %s (Out)", GetRealm(), action->GetName()); + network_send_game_action(action); - return result; + return result; + } } - } - else if (network_get_mode() == NETWORK_MODE_SERVER) - { - // If player is the server it would execute right away as where clients execute the commands - // at the beginning of the frame, so we have to put them into the queue. - if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED)) + else if (network_get_mode() == NETWORK_MODE_SERVER) { - log_verbose("[%s] GameAction::Execute %s (Queue)", GetRealm(), action->GetName()); - network_enqueue_game_action(action); + // If player is the server it would execute right away as where clients execute the commands + // at the beginning of the frame, so we have to put them into the queue. + if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && !(flags & GAME_COMMAND_FLAG_NETWORKED)) + { + log_verbose("[%s] GameAction::Execute %s (Queue)", GetRealm(), action->GetName()); + network_enqueue_game_action(action); - return result; + return result; + } } } @@ -283,6 +290,10 @@ namespace GameActions LogActionFinish(logContext, action, result); + // If not top level just give away the result. + if (topLevel == false) + return result; + gCommandPosition.x = result->Position.x; gCommandPosition.y = result->Position.y; gCommandPosition.z = result->Position.z; @@ -349,6 +360,7 @@ namespace GameActions std::copy(result->ErrorMessageArgs.begin(), result->ErrorMessageArgs.end(), gCommonFormatArgs); context_show_error(result->ErrorTitle, result->ErrorMessage); } + return result; } diff --git a/src/openrct2/actions/GameAction.h b/src/openrct2/actions/GameAction.h index daeb95cbb5..ec2b166e6f 100644 --- a/src/openrct2/actions/GameAction.h +++ b/src/openrct2/actions/GameAction.h @@ -244,8 +244,8 @@ namespace GameActions bool IsValidId(uint32_t id); GameAction::Ptr Create(uint32_t id); GameAction::Ptr Clone(const GameAction* action); - GameActionResult::Ptr Query(const GameAction* action); - GameActionResult::Ptr Execute(const GameAction* action); + GameActionResult::Ptr Query(const GameAction* action, bool topLevel = true); + GameActionResult::Ptr Execute(const GameAction* action, bool topLevel = true); GameActionFactory Register(uint32_t id, GameActionFactory action); template static GameActionFactory Register()