From 57b82e2e99aa30a649d1fd80f192ccf5338dc7c4 Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Tue, 30 Nov 2021 22:59:23 +0100 Subject: Codechange: Don't use globals for story/goal/sign/group command proc return values. --- src/autoreplace_cmd.cpp | 2 +- src/goal.cpp | 32 +++++++++++------------ src/goal_cmd.h | 2 +- src/goal_type.h | 3 +-- src/group.h | 2 -- src/group_cmd.cpp | 34 ++++++++++++------------- src/group_cmd.h | 8 +++--- src/group_gui.cpp | 16 +++++++----- src/script/api/script_object.cpp | 55 ---------------------------------------- src/script/api/script_object.hpp | 55 ---------------------------------------- src/script/script_instance.cpp | 11 ++++---- src/script/script_storage.hpp | 10 -------- src/signs_cmd.cpp | 21 +++++++-------- src/signs_cmd.h | 4 ++- src/signs_func.h | 1 - src/story.cpp | 31 +++++++++++----------- src/story_cmd.h | 4 +-- src/story_type.h | 2 -- src/terraform_cmd.h | 1 - src/town.h | 1 - src/town_cmd.cpp | 35 +++++++++++++------------ src/town_cmd.h | 4 +-- src/town_gui.cpp | 7 ++--- 23 files changed, 105 insertions(+), 236 deletions(-) diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index f2ef317f0..9fef03839 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -405,7 +405,7 @@ static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command::Do(DC_EXEC, CO_SHARE, new_head->index, old_head->index)); /* Copy group membership */ - if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command::Do(DC_EXEC, old_head->group_id, new_head->index, false)); + if (cost.Succeeded() && old_head != new_head) cost.AddCost(std::get<0>(Command::Do(DC_EXEC, old_head->group_id, new_head->index, false))); /* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */ if (cost.Succeeded()) { diff --git a/src/goal.cpp b/src/goal.cpp index 99322ec2c..447763866 100644 --- a/src/goal.cpp +++ b/src/goal.cpp @@ -28,8 +28,6 @@ #include "safeguards.h" -GoalID _new_goal_id; - GoalPool _goal_pool("Goal"); INSTANTIATE_POOL_METHODS(Goal) @@ -42,43 +40,43 @@ INSTANTIATE_POOL_METHODS(Goal) * @param text Text of the goal. * @return the cost of this operation or an error */ -CommandCost CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text) +std::tuple CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text) { - if (!Goal::CanAllocateItem()) return CMD_ERROR; + if (!Goal::CanAllocateItem()) return { CMD_ERROR, INVALID_GOAL }; - if (_current_company != OWNER_DEITY) return CMD_ERROR; - if (text.empty()) return CMD_ERROR; - if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR; + if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_GOAL }; + if (text.empty()) return { CMD_ERROR, INVALID_GOAL }; + if (company != INVALID_COMPANY && !Company::IsValidID(company)) return { CMD_ERROR, INVALID_GOAL }; switch (type) { case GT_NONE: - if (dest != 0) return CMD_ERROR; + if (dest != 0) return { CMD_ERROR, INVALID_GOAL }; break; case GT_TILE: - if (!IsValidTile(dest)) return CMD_ERROR; + if (!IsValidTile(dest)) return { CMD_ERROR, INVALID_GOAL }; break; case GT_INDUSTRY: - if (!Industry::IsValidID(dest)) return CMD_ERROR; + if (!Industry::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL }; break; case GT_TOWN: - if (!Town::IsValidID(dest)) return CMD_ERROR; + if (!Town::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL }; break; case GT_COMPANY: - if (!Company::IsValidID(dest)) return CMD_ERROR; + if (!Company::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL }; break; case GT_STORY_PAGE: { - if (!StoryPage::IsValidID(dest)) return CMD_ERROR; + if (!StoryPage::IsValidID(dest)) return { CMD_ERROR, INVALID_GOAL }; CompanyID story_company = StoryPage::Get(dest)->company; - if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return CMD_ERROR; + if (company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != company) return { CMD_ERROR, INVALID_GOAL }; break; } - default: return CMD_ERROR; + default: return { CMD_ERROR, INVALID_GOAL }; } if (flags & DC_EXEC) { @@ -97,10 +95,10 @@ CommandCost CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, } if (Goal::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0); - _new_goal_id = g->index; + return { CommandCost(), g->index }; } - return CommandCost(); + return { CommandCost(), INVALID_GOAL }; } /** diff --git a/src/goal_cmd.h b/src/goal_cmd.h index 5c3e4954f..5be2044e2 100644 --- a/src/goal_cmd.h +++ b/src/goal_cmd.h @@ -14,7 +14,7 @@ #include "command_type.h" #include "goal_type.h" -CommandCost CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text); +std::tuple CmdCreateGoal(DoCommandFlag flags, CompanyID company, GoalType type, GoalTypeID dest, const std::string &text); CommandCost CmdRemoveGoal(DoCommandFlag flags, GoalID goal); CommandCost CmdSetGoalText(DoCommandFlag flags, GoalID goal, const std::string &text); CommandCost CmdSetGoalProgress(DoCommandFlag flags, GoalID goal, const std::string &text); diff --git a/src/goal_type.h b/src/goal_type.h index 5bbd8c258..27ec758a9 100644 --- a/src/goal_type.h +++ b/src/goal_type.h @@ -37,7 +37,6 @@ static const GoalTypeID INVALID_GOALTYPE = 0xFFFFFFFF; ///< Invalid/unknown inde typedef uint16 GoalID; ///< ID of a goal struct Goal; - -extern GoalID _new_goal_id; +static const GoalID INVALID_GOAL = 0xFFFF; ///< Constant representing a non-existing goal. #endif /* GOAL_TYPE_H */ diff --git a/src/group.h b/src/group.h index e0f0b9196..6d55c9385 100644 --- a/src/group.h +++ b/src/group.h @@ -113,6 +113,4 @@ void RemoveVehicleFromGroup(const Vehicle *v); void RemoveAllGroupsForCompany(const CompanyID company); bool GroupIsInGroup(GroupID search, GroupID group); -extern GroupID _new_group_id; - #endif /* GROUP_H */ diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index 4b34ad382..c8772e5c1 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -25,8 +25,6 @@ #include "safeguards.h" -GroupID _new_group_id; - GroupPool _group_pool("Group"); INSTANTIATE_POOL_METHODS(Group) @@ -299,16 +297,16 @@ Group::Group(Owner owner) * @param parent_group parent groupid * @return the cost of this operation or an error */ -CommandCost CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group) +std::tuple CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group) { - if (!IsCompanyBuildableVehicleType(vt)) return CMD_ERROR; + if (!IsCompanyBuildableVehicleType(vt)) return { CMD_ERROR, INVALID_GROUP }; - if (!Group::CanAllocateItem()) return CMD_ERROR; + if (!Group::CanAllocateItem()) return { CMD_ERROR, INVALID_GROUP }; const Group *pg = Group::GetIfValid(parent_group); if (pg != nullptr) { - if (pg->owner != _current_company) return CMD_ERROR; - if (pg->vehicle_type != vt) return CMD_ERROR; + if (pg->owner != _current_company) return { CMD_ERROR, INVALID_GROUP }; + if (pg->vehicle_type != vt) return { CMD_ERROR, INVALID_GROUP }; } if (flags & DC_EXEC) { @@ -328,13 +326,13 @@ CommandCost CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_g g->flags = pg->flags; } - _new_group_id = g->index; - InvalidateWindowData(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, _current_company).Pack()); InvalidateWindowData(WC_COMPANY_COLOUR, g->owner, g->vehicle_type); + + return { CommandCost(), g->index }; } - return CommandCost(); + return { CommandCost(), INVALID_GROUP}; } @@ -497,26 +495,26 @@ static void AddVehicleToGroup(Vehicle *v, GroupID new_g) * @param add_shared Add shared vehicles as well. * @return the cost of this operation or an error */ -CommandCost CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared) +std::tuple CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared) { Vehicle *v = Vehicle::GetIfValid(veh_id); GroupID new_g = group_id; - if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return CMD_ERROR; + if (v == nullptr || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g) && new_g != NEW_GROUP)) return { CMD_ERROR, INVALID_GROUP }; if (Group::IsValidID(new_g)) { Group *g = Group::Get(new_g); - if (g->owner != _current_company || g->vehicle_type != v->type) return CMD_ERROR; + if (g->owner != _current_company || g->vehicle_type != v->type) return { CMD_ERROR, INVALID_GROUP }; } - if (v->owner != _current_company || !v->IsPrimaryVehicle()) return CMD_ERROR; + if (v->owner != _current_company || !v->IsPrimaryVehicle()) return { CMD_ERROR, INVALID_GROUP }; if (new_g == NEW_GROUP) { /* Create new group. */ - CommandCost ret = CmdCreateGroup(flags, v->type, INVALID_GROUP); - if (ret.Failed()) return ret; + auto [ret, group_id] = CmdCreateGroup(flags, v->type, INVALID_GROUP); + if (ret.Failed()) return { ret, group_id }; - new_g = _new_group_id; + new_g = group_id; } if (flags & DC_EXEC) { @@ -541,7 +539,7 @@ CommandCost CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID InvalidateWindowData(WC_VEHICLE_DETAILS, v->index); } - return CommandCost(); + return { CommandCost(), new_g }; } /** diff --git a/src/group_cmd.h b/src/group_cmd.h index d3c25fda1..f84e37859 100644 --- a/src/group_cmd.h +++ b/src/group_cmd.h @@ -23,10 +23,10 @@ enum class AlterGroupMode : byte { SetParent, ///< Change group parent. }; -CommandCost CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group); +std::tuple CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group); CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text); CommandCost CmdDeleteGroup(DoCommandFlag flags, GroupID group_id); -CommandCost CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared); +std::tuple CmdAddVehicleGroup(DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared); CommandCost CmdAddSharedVehicleGroup(DoCommandFlag flags, GroupID id_g, VehicleType type); CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlag flags, GroupID group_id); CommandCost CmdSetGroupFlag(DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive); @@ -41,7 +41,7 @@ DEF_CMD_TRAIT(CMD_REMOVE_ALL_VEHICLES_GROUP, CmdRemoveAllVehiclesGroup, 0, CMDT_ DEF_CMD_TRAIT(CMD_SET_GROUP_FLAG, CmdSetGroupFlag, 0, CMDT_ROUTE_MANAGEMENT) DEF_CMD_TRAIT(CMD_SET_GROUP_LIVERY, CmdSetGroupLivery, 0, CMDT_ROUTE_MANAGEMENT) -void CcCreateGroup(Commands cmd, const CommandCost &result, VehicleType vt, GroupID parent_group); -void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID, VehicleID veh_id, bool); +void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group); +void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool); #endif /* GROUP_CMD_H */ diff --git a/src/group_gui.cpp b/src/group_gui.cpp index f614551f7..503ddddd9 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -1143,40 +1143,42 @@ static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner o * Opens a 'Rename group' window for newly created group. * @param veh_type Vehicle type. */ -static void CcCreateGroup(VehicleType veh_type) +static void CcCreateGroup(GroupID gid, VehicleType veh_type) { VehicleGroupWindow *w = FindVehicleGroupWindow(veh_type, _current_company); - if (w != nullptr) w->ShowRenameGroupWindow(_new_group_id, true); + if (w != nullptr) w->ShowRenameGroupWindow(gid, true); } /** * Opens a 'Rename group' window for newly created group. * @param cmd Unused. * @param result Did command succeed? + * @param new_group ID of the created group. * @param vt Vehicle type. - * @param parent_group Parent group of the enw group. + * @param parent_group Parent group of the new group. * @see CmdCreateGroup */ -void CcCreateGroup(Commands cmd, const CommandCost &result, VehicleType vt, GroupID parent_group) +void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group) { if (result.Failed()) return; assert(vt <= VEH_AIRCRAFT); - CcCreateGroup(vt); + CcCreateGroup(new_group, vt); } /** * Open rename window after adding a vehicle to a new group via drag and drop. * @param cmd Unused. * @param result Did command succeed? + * @param new_group ID of the created group. * @param veh_id vehicle to add to a group */ -void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID, VehicleID veh_id, bool) +void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool) { if (result.Failed()) return; assert(Vehicle::IsValidID(veh_id)); - CcCreateGroup(Vehicle::Get(veh_id)->type); + CcCreateGroup(new_group, Vehicle::Get(veh_id)->type); } /** diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index dad79309a..f95433bd9 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -161,11 +161,6 @@ ScriptObject::ActiveInstance::~ActiveInstance() GetStorage()->last_command_res = res; /* Also store the results of various global variables */ SetNewVehicleID(_new_vehicle_id); - SetNewSignID(_new_sign_id); - SetNewGroupID(_new_group_id); - SetNewGoalID(_new_goal_id); - SetNewStoryPageID(_new_story_page_id); - SetNewStoryPageElementID(_new_story_page_element_id); } /* static */ bool ScriptObject::GetLastCommandRes() @@ -193,56 +188,6 @@ ScriptObject::ActiveInstance::~ActiveInstance() return GetStorage()->new_vehicle_id; } -/* static */ void ScriptObject::SetNewSignID(SignID sign_id) -{ - GetStorage()->new_sign_id = sign_id; -} - -/* static */ SignID ScriptObject::GetNewSignID() -{ - return GetStorage()->new_sign_id; -} - -/* static */ void ScriptObject::SetNewGroupID(GroupID group_id) -{ - GetStorage()->new_group_id = group_id; -} - -/* static */ GroupID ScriptObject::GetNewGroupID() -{ - return GetStorage()->new_group_id; -} - -/* static */ void ScriptObject::SetNewGoalID(GoalID goal_id) -{ - GetStorage()->new_goal_id = goal_id; -} - -/* static */ GroupID ScriptObject::GetNewGoalID() -{ - return GetStorage()->new_goal_id; -} - -/* static */ void ScriptObject::SetNewStoryPageID(StoryPageID story_page_id) -{ - GetStorage()->new_story_page_id = story_page_id; -} - -/* static */ GroupID ScriptObject::GetNewStoryPageID() -{ - return GetStorage()->new_story_page_id; -} - -/* static */ void ScriptObject::SetNewStoryPageElementID(StoryPageElementID story_page_element_id) -{ - GetStorage()->new_story_page_element_id = story_page_element_id; -} - -/* static */ GroupID ScriptObject::GetNewStoryPageElementID() -{ - return GetStorage()->new_story_page_element_id; -} - /* static */ void ScriptObject::SetAllowDoCommand(bool allow) { GetStorage()->allow_do_command = allow; diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index f9a66f8a8..d90fec351 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -197,31 +197,6 @@ protected: */ static VehicleID GetNewVehicleID(); - /** - * Get the latest stored new_sign_id. - */ - static SignID GetNewSignID(); - - /** - * Get the latest stored new_group_id. - */ - static GroupID GetNewGroupID(); - - /** - * Get the latest stored new_goal_id. - */ - static GoalID GetNewGoalID(); - - /** - * Get the latest stored new_story_page_id. - */ - static StoryPageID GetNewStoryPageID(); - - /** - * Get the latest stored new_story_page_id. - */ - static StoryPageID GetNewStoryPageElementID(); - /** * Store a allow_do_command per company. * @param allow The new allow. @@ -305,36 +280,6 @@ private: */ static void SetNewVehicleID(VehicleID vehicle_id); - /** - * Store a new_sign_id per company. - * @param sign_id The new SignID. - */ - static void SetNewSignID(SignID sign_id); - - /** - * Store a new_group_id per company. - * @param group_id The new GroupID. - */ - static void SetNewGroupID(GroupID group_id); - - /** - * Store a new_goal_id per company. - * @param goal_id The new GoalID. - */ - static void SetNewGoalID(GoalID goal_id); - - /** - * Store a new_story_page_id per company. - * @param story_page_id The new StoryPageID. - */ - static void SetNewStoryPageID(StoryPageID story_page_id); - - /** - * Store a new_story_page_id per company. - * @param story_page_id The new StoryPageID. - */ - static void SetNewStoryPageElementID(StoryPageElementID story_page_element_id); - /* Helper functions for DoCommand. */ static std::tuple DoCommandPrep(); static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only); diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index 0dfdd4b2a..9d978f833 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -26,6 +26,7 @@ #include "../company_base.h" #include "../company_func.h" #include "../fileio_func.h" +#include "../misc/endian_buffer.hpp" #include "../safeguards.h" @@ -274,27 +275,27 @@ void ScriptInstance::CollectGarbage() /* static */ void ScriptInstance::DoCommandReturnSignID(ScriptInstance *instance) { - instance->engine->InsertResult(ScriptObject::GetNewSignID()); + instance->engine->InsertResult(EndianBufferReader::ToValue(ScriptObject::GetLastCommandResData())); } /* static */ void ScriptInstance::DoCommandReturnGroupID(ScriptInstance *instance) { - instance->engine->InsertResult(ScriptObject::GetNewGroupID()); + instance->engine->InsertResult(EndianBufferReader::ToValue(ScriptObject::GetLastCommandResData())); } /* static */ void ScriptInstance::DoCommandReturnGoalID(ScriptInstance *instance) { - instance->engine->InsertResult(ScriptObject::GetNewGoalID()); + instance->engine->InsertResult(EndianBufferReader::ToValue(ScriptObject::GetLastCommandResData())); } /* static */ void ScriptInstance::DoCommandReturnStoryPageID(ScriptInstance *instance) { - instance->engine->InsertResult(ScriptObject::GetNewStoryPageID()); + instance->engine->InsertResult(EndianBufferReader::ToValue(ScriptObject::GetLastCommandResData())); } /* static */ void ScriptInstance::DoCommandReturnStoryPageElementID(ScriptInstance *instance) { - instance->engine->InsertResult(ScriptObject::GetNewStoryPageElementID()); + instance->engine->InsertResult(EndianBufferReader::ToValue(ScriptObject::GetLastCommandResData())); } ScriptStorage *ScriptInstance::GetStorage() diff --git a/src/script/script_storage.hpp b/src/script/script_storage.hpp index b8d1038a0..f95eb7e71 100644 --- a/src/script/script_storage.hpp +++ b/src/script/script_storage.hpp @@ -50,11 +50,6 @@ private: CommandDataBuffer last_cmd_ret; ///< The extra data returned by the last command. VehicleID new_vehicle_id; ///< The ID of the new Vehicle. - SignID new_sign_id; ///< The ID of the new Sign. - GroupID new_group_id; ///< The ID of the new Group. - GoalID new_goal_id; ///< The ID of the new Goal. - StoryPageID new_story_page_id; ///< The ID of the new StoryPage. - StoryPageID new_story_page_element_id; ///< The ID of the new StoryPageElement. std::vector callback_value; ///< The values which need to survive a callback. @@ -79,11 +74,6 @@ public: last_tile (INVALID_TILE), last_cmd (CMD_END), new_vehicle_id (0), - new_sign_id (0), - new_group_id (0), - new_goal_id (0), - new_story_page_id (0), - new_story_page_element_id(0), /* calback_value (can't be set) */ road_type (INVALID_ROADTYPE), rail_type (INVALID_RAILTYPE), diff --git a/src/signs_cmd.cpp b/src/signs_cmd.cpp index f50e37583..139d874c6 100644 --- a/src/signs_cmd.cpp +++ b/src/signs_cmd.cpp @@ -23,9 +23,6 @@ #include "safeguards.h" -/** The last built sign. */ -SignID _new_sign_id; - /** * Place a sign at the given coordinates. Ownership of sign has * no effect whatsoever except for the colour the sign gets for easy recognition, @@ -33,15 +30,15 @@ SignID _new_sign_id; * @param tile tile to place sign at * @param flags type of operation * @param text contents of the sign - * @return the cost of this operation or an error + * @return the cost of this operation + the ID of the new sign or an error */ -CommandCost CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text) +std::tuple CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text) { /* Try to locate a new sign */ - if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS); + if (!Sign::CanAllocateItem()) return { CommandCost(STR_ERROR_TOO_MANY_SIGNS), INVALID_SIGN }; /* Check sign text length if any */ - if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR; + if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return { CMD_ERROR, INVALID_SIGN }; /* When we execute, really make the sign */ if (flags & DC_EXEC) { @@ -57,10 +54,10 @@ CommandCost CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string } si->UpdateVirtCoord(); InvalidateWindowData(WC_SIGN_LIST, 0, 0); - _new_sign_id = si->index; + return { CommandCost(), si->index }; } - return CommandCost(); + return { CommandCost(), INVALID_SIGN }; } /** @@ -107,13 +104,13 @@ CommandCost CmdRenameSign(DoCommandFlag flags, SignID sign_id, const std::string * Callback function that is called after a sign is placed * @param cmd unused * @param result of the operation - * @param tile unused + * @param new_sign ID of the placed sign. */ -void CcPlaceSign(Commands cmd, const CommandCost &result, TileIndex tile) +void CcPlaceSign(Commands cmd, const CommandCost &result, SignID new_sign) { if (result.Failed()) return; - ShowRenameSignWindow(Sign::Get(_new_sign_id)); + ShowRenameSignWindow(Sign::Get(new_sign)); ResetObjectToPlace(); } diff --git a/src/signs_cmd.h b/src/signs_cmd.h index 1c4e1b465..627cfa648 100644 --- a/src/signs_cmd.h +++ b/src/signs_cmd.h @@ -13,10 +13,12 @@ #include "command_type.h" #include "signs_type.h" -CommandCost CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text); +std::tuple CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text); CommandCost CmdRenameSign(DoCommandFlag flags, SignID sign_id, const std::string &text); DEF_CMD_TRAIT(CMD_PLACE_SIGN, CmdPlaceSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT) DEF_CMD_TRAIT(CMD_RENAME_SIGN, CmdRenameSign, CMD_DEITY, CMDT_OTHER_MANAGEMENT) +void CcPlaceSign(Commands cmd, const CommandCost &result, SignID new_sign); + #endif /* SIGNS_CMD_H */ diff --git a/src/signs_func.h b/src/signs_func.h index af677201c..18f27fc81 100644 --- a/src/signs_func.h +++ b/src/signs_func.h @@ -14,7 +14,6 @@ #include "tile_type.h" struct Window; -extern SignID _new_sign_id; void UpdateAllSignVirtCoords(); void PlaceProc_Sign(TileIndex tile); diff --git a/src/story.cpp b/src/story.cpp index 259bfbd61..8de4df89f 100644 --- a/src/story.cpp +++ b/src/story.cpp @@ -30,8 +30,6 @@ #include "safeguards.h" -StoryPageElementID _new_story_page_element_id; -StoryPageID _new_story_page_id; uint32 _story_page_element_next_sort_value; uint32 _story_page_next_sort_value; @@ -202,12 +200,12 @@ bool StoryPageButtonData::ValidateVehicleType() const * @param text Title of the story page. Null is allowed in which case a generic page title is provided by OpenTTD. * @return the cost of this operation or an error */ -CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text) +std::tuple CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text) { - if (!StoryPage::CanAllocateItem()) return CMD_ERROR; + if (!StoryPage::CanAllocateItem()) return { CMD_ERROR, INVALID_STORY_PAGE }; - if (_current_company != OWNER_DEITY) return CMD_ERROR; - if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR; + if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_STORY_PAGE }; + if (company != INVALID_COMPANY && !Company::IsValidID(company)) return { CMD_ERROR, INVALID_STORY_PAGE }; if (flags & DC_EXEC) { if (_story_page_pool.items == 0) { @@ -228,11 +226,11 @@ CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std InvalidateWindowClassesData(WC_STORY_BOOK, -1); if (StoryPage::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0); - _new_story_page_id = s->index; _story_page_next_sort_value++; + return { CommandCost(), s->index }; } - return CommandCost(); + return { CommandCost(), INVALID_STORY_PAGE }; } /** @@ -245,20 +243,21 @@ CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std * @param text Text content in case it is a text or location page element * @return the cost of this operation or an error */ -CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text) +std::tuple CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text) { - if (!StoryPageElement::CanAllocateItem()) return CMD_ERROR; + if (!StoryPageElement::CanAllocateItem()) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT }; /* Allow at most 128 elements per page. */ uint16 element_count = 0; for (StoryPageElement *iter : StoryPageElement::Iterate()) { if (iter->page == page_id) element_count++; } - if (element_count >= 128) return CMD_ERROR; + if (element_count >= 128) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT }; + + if (_current_company != OWNER_DEITY) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT }; + if (!StoryPage::IsValidID(page_id)) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT }; + if (!VerifyElementContentParameters(page_id, type, tile, reference, text.c_str())) return { CMD_ERROR, INVALID_STORY_PAGE_ELEMENT }; - if (_current_company != OWNER_DEITY) return CMD_ERROR; - if (!StoryPage::IsValidID(page_id)) return CMD_ERROR; - if (!VerifyElementContentParameters(page_id, type, tile, reference, text.c_str())) return CMD_ERROR; if (flags & DC_EXEC) { if (_story_page_element_pool.items == 0) { @@ -274,11 +273,11 @@ CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, Story InvalidateWindowClassesData(WC_STORY_BOOK, page_id); - _new_story_page_element_id = pe->index; _story_page_element_next_sort_value++; + return { CommandCost(), pe->index }; } - return CommandCost(); + return { CommandCost(), INVALID_STORY_PAGE_ELEMENT }; } /** diff --git a/src/story_cmd.h b/src/story_cmd.h index 9dbfd412a..29b1b45f2 100644 --- a/src/story_cmd.h +++ b/src/story_cmd.h @@ -16,8 +16,8 @@ #include "story_type.h" #include "vehicle_type.h" -CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text); -CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text); +std::tuple CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text); +std::tuple CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text); CommandCost CmdUpdateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, uint32 reference, const std::string &text); CommandCost CmdSetStoryPageTitle(DoCommandFlag flags, StoryPageID page_id, const std::string &text); CommandCost CmdSetStoryPageDate(DoCommandFlag flags, StoryPageID page_id, Date date); diff --git a/src/story_type.h b/src/story_type.h index e919564a7..386abe5e2 100644 --- a/src/story_type.h +++ b/src/story_type.h @@ -18,8 +18,6 @@ struct StoryPageElement; struct StoryPage; enum StoryPageElementType : byte; -extern StoryPageElementID _new_story_page_element_id; -extern StoryPageID _new_story_page_id; static const StoryPageElementID INVALID_STORY_PAGE_ELEMENT = 0xFFFF; ///< Constant representing a non-existing story page element. static const StoryPageID INVALID_STORY_PAGE = 0xFFFF; ///< Constant representing a non-existing story page. diff --git a/src/terraform_cmd.h b/src/terraform_cmd.h index 861e0d994..4daeeac48 100644 --- a/src/terraform_cmd.h +++ b/src/terraform_cmd.h @@ -21,7 +21,6 @@ DEF_CMD_TRAIT(CMD_TERRAFORM_LAND, CmdTerraformLand, CMD_ALL_TILES | CMD_AUTO, DEF_CMD_TRAIT(CMD_LEVEL_LAND, CmdLevelLand, CMD_ALL_TILES | CMD_AUTO | CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION) // test run might clear tiles multiple times, in execution that only happens once CommandCallback CcPlaySound_EXPLOSION; -CommandCallback CcPlaceSign; CommandCallback CcTerraform; #endif /* TERRAFORM_CMD_H */ diff --git a/src/town.h b/src/town.h index e186951e6..294be7b53 100644 --- a/src/town.h +++ b/src/town.h @@ -228,7 +228,6 @@ enum TownActions { DECLARE_ENUM_AS_BIT_SET(TownActions) extern const byte _town_action_costs[TACT_COUNT]; -extern TownID _new_town_id; /** * Set the default name for a depot/waypoint diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index ea5a81eb7..5afc1d2c9 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -59,8 +59,6 @@ #include "safeguards.h" -TownID _new_town_id; - /* Initialize the town-pool */ TownPool _town_pool("Town"); INSTANTIATE_POOL_METHODS(Town) @@ -1937,41 +1935,41 @@ static bool IsUniqueTownName(const std::string &name) * @param text Custom name for the town. If empty, the town name parts will be used. * @return the cost of this operation or an error */ -CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text) +std::tuple CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text) { TownNameParams par(_settings_game.game_creation.town_name); - if (size >= TSZ_END) return CMD_ERROR; - if (layout >= NUM_TLS) return CMD_ERROR; + if (size >= TSZ_END) return { CMD_ERROR, INVALID_TOWN }; + if (layout >= NUM_TLS) return { CMD_ERROR, INVALID_TOWN }; /* Some things are allowed only in the scenario editor and for game scripts. */ if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY) { - if (_settings_game.economy.found_town == TF_FORBIDDEN) return CMD_ERROR; - if (size == TSZ_LARGE) return CMD_ERROR; - if (random_location) return CMD_ERROR; + if (_settings_game.economy.found_town == TF_FORBIDDEN) return { CMD_ERROR, INVALID_TOWN }; + if (size == TSZ_LARGE) return { CMD_ERROR, INVALID_TOWN }; + if (random_location) return { CMD_ERROR, INVALID_TOWN }; if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT && layout != _settings_game.economy.town_layout) { - return CMD_ERROR; + return { CMD_ERROR, INVALID_TOWN }; } } else if (_current_company == OWNER_DEITY && random_location) { /* Random parameter is not allowed for Game Scripts. */ - return CMD_ERROR; + return { CMD_ERROR, INVALID_TOWN }; } if (text.empty()) { /* If supplied name is empty, townnameparts has to generate unique automatic name */ - if (!VerifyTownName(townnameparts, &par)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE); + if (!VerifyTownName(townnameparts, &par)) return { CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE), INVALID_TOWN }; } else { /* If name is not empty, it has to be unique custom name */ - if (Utf8StringLength(text) >= MAX_LENGTH_TOWN_NAME_CHARS) return CMD_ERROR; - if (!IsUniqueTownName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE); + if (Utf8StringLength(text) >= MAX_LENGTH_TOWN_NAME_CHARS) return { CMD_ERROR, INVALID_TOWN }; + if (!IsUniqueTownName(text)) return { CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE), INVALID_TOWN }; } /* Allocate town struct */ - if (!Town::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_TOWNS); + if (!Town::CanAllocateItem()) return { CommandCost(STR_ERROR_TOO_MANY_TOWNS), INVALID_TOWN }; if (!random_location) { CommandCost ret = TownCanBePlacedHere(tile); - if (ret.Failed()) return ret; + if (ret.Failed()) return { ret, INVALID_TOWN }; } static const byte price_mult[][TSZ_RANDOM + 1] = {{ 15, 25, 40, 25 }, { 20, 35, 55, 35 }}; @@ -1984,10 +1982,11 @@ CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, boo cost.MultiplyCost(mult); /* Create the town */ + TownID new_town = INVALID_TOWN; if (flags & DC_EXEC) { if (cost.GetCost() > GetAvailableMoneyForCommand()) { _additional_cash_required = cost.GetCost(); - return CommandCost(EXPENSES_OTHER); + return { CommandCost(EXPENSES_OTHER), INVALID_TOWN }; } Backup old_generating_world(_generating_world, true, FILE_LINE); @@ -1998,7 +1997,7 @@ CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, boo if (t == nullptr) { cost = CommandCost(STR_ERROR_NO_SPACE_FOR_TOWN); } else { - _new_town_id = t->index; + new_town = t->index; } } else { t = new Town(tile); @@ -2032,7 +2031,7 @@ CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, boo Game::NewEvent(new ScriptEventTownFounded(t->index)); } } - return cost; + return { cost, new_town }; } /** diff --git a/src/town_cmd.h b/src/town_cmd.h index 4c9782e48..e98fa571e 100644 --- a/src/town_cmd.h +++ b/src/town_cmd.h @@ -16,7 +16,7 @@ enum TownEffect : byte; -CommandCost CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text); +std::tuple CmdFoundTown(DoCommandFlag flags, TileIndex tile, TownSize size, bool city, TownLayout layout, bool random_location, uint32 townnameparts, const std::string &text); CommandCost CmdRenameTown(DoCommandFlag flags, TownID town_id, const std::string &text); CommandCost CmdDoTownAction(DoCommandFlag flags, TownID town_id, uint8 action); CommandCost CmdTownGrowthRate(DoCommandFlag flags, TownID town_id, uint16 growth_rate); @@ -37,6 +37,6 @@ DEF_CMD_TRAIT(CMD_EXPAND_TOWN, CmdExpandTown, CMD_DEITY, DEF_CMD_TRAIT(CMD_DELETE_TOWN, CmdDeleteTown, CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION) CommandCallback CcFoundTown; -CommandCallback CcFoundRandomTown; +void CcFoundRandomTown(Commands cmd, const CommandCost &result, TownID town_id); #endif /* TOWN_CMD_H */ diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 0c5f86ab7..fbc6a842a 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -1017,9 +1017,9 @@ void CcFoundTown(Commands cmd, const CommandCost &result, TileIndex tile) if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace(); } -void CcFoundRandomTown(Commands cmd, const CommandCost &result, TileIndex tile) +void CcFoundRandomTown(Commands cmd, const CommandCost &result, TownID town_id) { - if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy); + if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(town_id)->xy); } static const NWidgetPart _nested_found_town_widgets[] = { @@ -1150,7 +1150,8 @@ public: this->SetDirty(); } - void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc) + template + void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, Tcallback cc) { std::string name; -- cgit v1.2.3-70-g09d2