Merge pull request #8711 from ZehMatt/ga-toplevel

Refactor direct calls to Query and Execute on game actions.
This commit is contained in:
ζeh Matt 2019-02-15 17:07:53 +01:00 committed by GitHub
commit 416915c883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 37 deletions

View File

@ -155,7 +155,9 @@ private:
auto footpathRemoveAction = FootpathRemoveAction(x * 32, y * 32, tileElement->base_height);
footpathRemoveAction.SetFlags(GetFlags());
auto res = executing ? footpathRemoveAction.Execute() : footpathRemoveAction.Query();
auto res = executing ? GameActions::ExecuteNested(&footpathRemoveAction)
: GameActions::QueryNested(&footpathRemoveAction);
if (res->Error != GA_ERROR::OK)
return MONEY32_UNDEFINED;
@ -171,7 +173,9 @@ private:
tileElement->AsSmallScenery()->GetEntryIndex());
removeSceneryAction.SetFlags(GetFlags());
auto res = executing ? removeSceneryAction.Execute() : removeSceneryAction.Query();
auto res = executing ? GameActions::ExecuteNested(&removeSceneryAction)
: GameActions::QueryNested(&removeSceneryAction);
if (res->Error != GA_ERROR::OK)
return MONEY32_UNDEFINED;
@ -186,7 +190,9 @@ private:
auto wallRemoveAction = WallRemoveAction(wallLocation);
wallRemoveAction.SetFlags(GetFlags());
auto res = executing ? wallRemoveAction.Execute() : wallRemoveAction.Query();
auto res = executing ? GameActions::ExecuteNested(&wallRemoveAction)
: GameActions::QueryNested(&wallRemoveAction);
if (res->Error != GA_ERROR::OK)
return MONEY32_UNDEFINED;
@ -202,7 +208,9 @@ private:
tileElement->AsLargeScenery()->GetSequenceIndex());
removeSceneryAction.SetFlags(GetFlags() | GAME_COMMAND_FLAG_PATH_SCENERY);
auto res = executing ? removeSceneryAction.Execute() : removeSceneryAction.Query();
auto res = executing ? GameActions::ExecuteNested(&removeSceneryAction)
: GameActions::QueryNested(&removeSceneryAction);
if (res->Error != GA_ERROR::OK)
return MONEY32_UNDEFINED;

View File

@ -137,7 +137,7 @@ namespace GameActions
return false;
}
GameActionResult::Ptr Query(const GameAction* action)
static GameActionResult::Ptr QueryInternal(const GameAction* action, bool topLevel)
{
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)
{
gCommandPosition.x = result->Position.x;
gCommandPosition.y = result->Position.y;
gCommandPosition.z = result->Position.z;
}
if (result->Error == GA_ERROR::OK)
{
@ -171,6 +175,16 @@ namespace GameActions
return result;
}
GameActionResult::Ptr Query(const GameAction* action)
{
return QueryInternal(action, true);
}
GameActionResult::Ptr QueryNested(const GameAction* action)
{
return QueryInternal(action, false);
}
static const char* GetRealm()
{
if (network_get_mode() == NETWORK_MODE_CLIENT)
@ -223,7 +237,7 @@ namespace GameActions
network_append_server_log(text);
}
GameActionResult::Ptr Execute(const GameAction* action)
static GameActionResult::Ptr ExecuteInternal(const GameAction* action, bool topLevel)
{
Guard::ArgumentNotNull(action);
@ -250,28 +264,31 @@ namespace GameActions
GameActionResult::Ptr result = Query(action);
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 +300,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,7 +370,18 @@ namespace GameActions
std::copy(result->ErrorMessageArgs.begin(), result->ErrorMessageArgs.end(), gCommonFormatArgs);
context_show_error(result->ErrorTitle, result->ErrorMessage);
}
return result;
}
GameActionResult::Ptr Execute(const GameAction* action)
{
return ExecuteInternal(action, true);
}
GameActionResult::Ptr ExecuteNested(const GameAction* action)
{
return ExecuteInternal(action, false);
}
} // namespace GameActions

View File

@ -244,8 +244,15 @@ namespace GameActions
bool IsValidId(uint32_t id);
GameAction::Ptr Create(uint32_t id);
GameAction::Ptr Clone(const GameAction* action);
// This should be used if a round trip is to be expected.
GameActionResult::Ptr Query(const GameAction* action);
GameActionResult::Ptr Execute(const GameAction* action);
// This should be used from within game actions.
GameActionResult::Ptr QueryNested(const GameAction* action);
GameActionResult::Ptr ExecuteNested(const GameAction* action);
GameActionFactory Register(uint32_t id, GameActionFactory action);
template<typename T> static GameActionFactory Register()

View File

@ -264,14 +264,10 @@ private:
auto setMazeTrack = MazeSetTrackAction(x, y, z, false, direction, _rideIndex, GC_SET_MAZE_TRACK_FILL);
setMazeTrack.SetFlags(GetFlags());
auto queryRes = setMazeTrack.Query();
if (queryRes->Error == GA_ERROR::OK)
auto execRes = GameActions::ExecuteNested(&setMazeTrack);
if (execRes->Error == GA_ERROR::OK)
{
auto execRes = setMazeTrack.Execute();
if (execRes->Error == GA_ERROR::OK)
{
return execRes->Cost;
}
return execRes->Cost;
}
return MONEY32_UNDEFINED;

View File

@ -30,7 +30,7 @@
// This string specifies which version of network stream current build uses.
// It is used for making sure only compatible builds get connected, even within
// single OpenRCT2 version.
#define NETWORK_STREAM_VERSION "36"
#define NETWORK_STREAM_VERSION "37"
#define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION
static rct_peep* _pickup_peep = nullptr;

View File

@ -1328,8 +1328,10 @@ static money32 lower_land(
newSlope &= TILE_ELEMENT_SURFACE_SLOPE_MASK;
auto landSetHeightAction = LandSetHeightAction({ x_coord, y_coord }, height, newSlope);
auto res = (flags & GAME_COMMAND_FLAG_APPLY) ? landSetHeightAction.Execute() : landSetHeightAction.Query();
landSetHeightAction.SetFlags(flags);
auto res = (flags & GAME_COMMAND_FLAG_APPLY) ? GameActions::ExecuteNested(&landSetHeightAction)
: GameActions::QueryNested(&landSetHeightAction);
if (res->Error != GA_ERROR::OK)
{
return MONEY32_UNDEFINED;
@ -1580,7 +1582,9 @@ static money32 smooth_land_tile(
}
auto landSetHeightAction = LandSetHeightAction({ x, y }, targetBaseZ, slope);
auto res = (flags & GAME_COMMAND_FLAG_APPLY) ? landSetHeightAction.Execute() : landSetHeightAction.Query();
landSetHeightAction.SetFlags(flags);
auto res = (flags & GAME_COMMAND_FLAG_APPLY) ? GameActions::ExecuteNested(&landSetHeightAction)
: GameActions::QueryNested(&landSetHeightAction);
if (res->Error == GA_ERROR::OK)
{
@ -1725,8 +1729,9 @@ static money32 smooth_land_row_by_edge(
}
}
auto landSetHeightAction = LandSetHeightAction({ x, y }, targetBaseZ, slope);
auto res = (flags & GAME_COMMAND_FLAG_APPLY) ? landSetHeightAction.Execute() : landSetHeightAction.Query();
landSetHeightAction.SetFlags(flags);
auto res = (flags & GAME_COMMAND_FLAG_APPLY) ? GameActions::ExecuteNested(&landSetHeightAction)
: GameActions::QueryNested(&landSetHeightAction);
if (res->Error == GA_ERROR::OK)
{
totalCost += res->Cost;