From cf7b4ded0808fdc92e41a172db1d198d4e366967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sun, 3 Oct 2021 21:13:04 +0300 Subject: [PATCH 1/2] Move static declaration into cpp file --- src/openrct2/actions/StaffHireNewAction.cpp | 8 ++++++++ src/openrct2/actions/StaffHireNewAction.h | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/openrct2/actions/StaffHireNewAction.cpp b/src/openrct2/actions/StaffHireNewAction.cpp index 7c5ba2507b..de91f58366 100644 --- a/src/openrct2/actions/StaffHireNewAction.cpp +++ b/src/openrct2/actions/StaffHireNewAction.cpp @@ -26,6 +26,14 @@ #include "../world/Park.h" #include "../world/Sprite.h" +/* rct2: 0x009929FC */ +static constexpr const PeepSpriteType spriteTypes[] = { + PeepSpriteType::Handyman, + PeepSpriteType::Mechanic, + PeepSpriteType::Security, + PeepSpriteType::EntertainerPanda, +}; + StaffHireNewActionResult::StaffHireNewActionResult() : GameActions::Result(GameActions::Status::Ok, STR_CANT_HIRE_NEW_STAFF) { diff --git a/src/openrct2/actions/StaffHireNewAction.h b/src/openrct2/actions/StaffHireNewAction.h index 47c9d6bb4c..bd4d94dc65 100644 --- a/src/openrct2/actions/StaffHireNewAction.h +++ b/src/openrct2/actions/StaffHireNewAction.h @@ -12,14 +12,6 @@ #include "../peep/Staff.h" #include "GameAction.h" -/* rct2: 0x009929FC */ -static constexpr const PeepSpriteType spriteTypes[] = { - PeepSpriteType::Handyman, - PeepSpriteType::Mechanic, - PeepSpriteType::Security, - PeepSpriteType::EntertainerPanda, -}; - class StaffHireNewActionResult final : public GameActions::Result { public: From 8effeb26b0479d285892a46a9cb74a6648d5610d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Mon, 4 Oct 2021 16:57:39 +0300 Subject: [PATCH 2/2] Refactor action result passing for StaffHireNewAction --- src/openrct2/actions/StaffHireNewAction.cpp | 29 +++++++-------------- src/openrct2/actions/StaffHireNewAction.h | 10 +++---- src/openrct2/peep/Staff.cpp | 5 ++-- src/openrct2/scripting/ScriptEngine.cpp | 9 ++++--- 4 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/openrct2/actions/StaffHireNewAction.cpp b/src/openrct2/actions/StaffHireNewAction.cpp index de91f58366..5615118635 100644 --- a/src/openrct2/actions/StaffHireNewAction.cpp +++ b/src/openrct2/actions/StaffHireNewAction.cpp @@ -34,16 +34,6 @@ static constexpr const PeepSpriteType spriteTypes[] = { PeepSpriteType::EntertainerPanda, }; -StaffHireNewActionResult::StaffHireNewActionResult() - : GameActions::Result(GameActions::Status::Ok, STR_CANT_HIRE_NEW_STAFF) -{ -} - -StaffHireNewActionResult::StaffHireNewActionResult(GameActions::Status error, rct_string_id message) - : GameActions::Result(error, STR_CANT_HIRE_NEW_STAFF, message) -{ -} - StaffHireNewAction::StaffHireNewAction( bool autoPosition, StaffType staffType, EntertainerCostume entertainerType, uint32_t staffOrders) : _autoPosition(autoPosition) @@ -85,8 +75,7 @@ GameActions::Result::Ptr StaffHireNewAction::Execute() const GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const { - auto res = std::make_unique(); - + auto res = MakeResult(); res->Expenditure = ExpenditureType::Wages; if (_staffType >= static_cast(StaffType::Count)) @@ -94,12 +83,12 @@ GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const // Invalid staff type. log_error("Tried to use invalid staff type: %u", static_cast(_staffType)); - return MakeResult(GameActions::Status::InvalidParameters, STR_NONE); + return MakeResult(GameActions::Status::InvalidParameters, STR_CANT_HIRE_NEW_STAFF, STR_NONE); } if (GetNumFreeEntities() < 400) { - return MakeResult(GameActions::Status::NoFreeElements, STR_TOO_MANY_PEOPLE_IN_GAME); + return MakeResult(GameActions::Status::NoFreeElements, STR_CANT_HIRE_NEW_STAFF, STR_TOO_MANY_PEOPLE_IN_GAME); } if (_staffType == static_cast(StaffType::Entertainer)) @@ -109,7 +98,7 @@ GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const // Invalid entertainer costume log_error("Tried to use invalid entertainer type: %u", static_cast(_entertainerType)); - return MakeResult(GameActions::Status::InvalidParameters, STR_NONE); + return MakeResult(GameActions::Status::InvalidParameters, STR_CANT_HIRE_NEW_STAFF, STR_NONE); } uint32_t availableCostumes = staff_get_available_entertainer_costumes(); @@ -118,7 +107,7 @@ GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const // Entertainer costume unavailable log_error("Tried to use unavailable entertainer type: %u", static_cast(_entertainerType)); - return MakeResult(GameActions::Status::InvalidParameters, STR_NONE); + return MakeResult(GameActions::Status::InvalidParameters, STR_CANT_HIRE_NEW_STAFF, STR_NONE); } } @@ -133,20 +122,22 @@ GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const if (staffIndex == STAFF_MAX_COUNT) { // Too many staff members exist already. - return MakeResult(GameActions::Status::NoFreeElements, STR_TOO_MANY_STAFF_IN_GAME); + return MakeResult(GameActions::Status::NoFreeElements, STR_CANT_HIRE_NEW_STAFF, STR_TOO_MANY_STAFF_IN_GAME); } Staff* newPeep = CreateEntity(); if (newPeep == nullptr) { // Too many peeps exist already. - return MakeResult(GameActions::Status::NoFreeElements, STR_TOO_MANY_PEOPLE_IN_GAME); + return MakeResult(GameActions::Status::NoFreeElements, STR_CANT_HIRE_NEW_STAFF, STR_TOO_MANY_PEOPLE_IN_GAME); } if (execute == false) { // In query we just want to see if we can obtain a sprite slot. sprite_remove(newPeep); + + res->SetData(StaffHireNewActionResult{ SPRITE_INDEX_NULL }); } else { @@ -240,7 +231,7 @@ GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const gStaffPatrolAreas[staffIndex * STAFF_PATROL_AREA_SIZE + i] = 0; } - res->peepSriteIndex = newPeep->sprite_index; + res->SetData(StaffHireNewActionResult{ newPeep->sprite_index }); } return res; diff --git a/src/openrct2/actions/StaffHireNewAction.h b/src/openrct2/actions/StaffHireNewAction.h index bd4d94dc65..5b06979fa6 100644 --- a/src/openrct2/actions/StaffHireNewAction.h +++ b/src/openrct2/actions/StaffHireNewAction.h @@ -12,16 +12,12 @@ #include "../peep/Staff.h" #include "GameAction.h" -class StaffHireNewActionResult final : public GameActions::Result +struct StaffHireNewActionResult { -public: - StaffHireNewActionResult(); - StaffHireNewActionResult(GameActions::Status error, rct_string_id message); - - uint32_t peepSriteIndex = SPRITE_INDEX_NULL; + uint16_t StaffEntityId = SPRITE_INDEX_NULL; }; -DEFINE_GAME_ACTION(StaffHireNewAction, GameCommand::HireNewStaffMember, StaffHireNewActionResult) +DEFINE_GAME_ACTION(StaffHireNewAction, GameCommand::HireNewStaffMember, GameActions::Result) { private: bool _autoPosition{}; diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index 41e1829500..7b927b487d 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -120,12 +120,13 @@ bool staff_hire_new_member(StaffType staffType, EntertainerCostume entertainerTy } auto hireStaffAction = StaffHireNewAction(autoPosition, staffType, entertainerType, staffOrders); - hireStaffAction.SetCallback([=](const GameAction*, const StaffHireNewActionResult* res) -> void { + hireStaffAction.SetCallback([=](const GameAction*, const GameActions::Result* res) -> void { if (res->Error != GameActions::Status::Ok) return; + auto actionResult = res->GetData(); // Open window for new staff. - auto* staff = GetEntity(res->peepSriteIndex); + auto* staff = GetEntity(actionResult.StaffEntityId); auto intent = Intent(WC_PEEP); intent.putExtra(INTENT_EXTRA_PEEP, staff); context_open_intent(&intent); diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 1208beeb0c..c90c4583e7 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -892,10 +892,13 @@ DukValue ScriptEngine::GameActionResultToDuk(const GameAction& action, const std } else if (action.GetType() == GameCommand::HireNewStaffMember) { - auto& staffHireResult = static_cast(*result.get()); - if (staffHireResult.peepSriteIndex != SPRITE_INDEX_NULL) + if (result->Error == GameActions::Status::Ok) { - obj.Set("peep", staffHireResult.peepSriteIndex); + const auto actionResult = result->GetData(); + if (actionResult.StaffEntityId != SPRITE_INDEX_NULL) + { + obj.Set("peep", actionResult.StaffEntityId); + } } }