Codechange: Don't use globals for story/goal/sign/group command proc return values.

This commit is contained in:
Michael Lutz 2021-11-30 22:59:23 +01:00
parent 3e85e833a7
commit 57b82e2e99
23 changed files with 105 additions and 236 deletions

View File

@ -405,7 +405,7 @@ static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head,
if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command<CMD_CLONE_ORDER>::Do(DC_EXEC, CO_SHARE, new_head->index, old_head->index));
/* Copy group membership */
if (cost.Succeeded() && old_head != new_head) cost.AddCost(Command<CMD_ADD_VEHICLE_GROUP>::Do(DC_EXEC, old_head->group_id, new_head->index, false));
if (cost.Succeeded() && old_head != new_head) cost.AddCost(std::get<0>(Command<CMD_ADD_VEHICLE_GROUP>::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()) {

View File

@ -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<CommandCost, GoalID> 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 };
}
/**

View File

@ -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<CommandCost, GoalID> 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);

View File

@ -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 */

View File

@ -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 */

View File

@ -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<CommandCost, GroupID> 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<CommandCost, GroupID> 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 };
}
/**

View File

@ -23,10 +23,10 @@ enum class AlterGroupMode : byte {
SetParent, ///< Change group parent.
};
CommandCost CmdCreateGroup(DoCommandFlag flags, VehicleType vt, GroupID parent_group);
std::tuple<CommandCost, GroupID> 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<CommandCost, GroupID> 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 */

View File

@ -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);
}
/**

View File

@ -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;

View File

@ -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<bool, bool, bool> DoCommandPrep();
static bool DoCommandProcessResult(const CommandCost &res, Script_SuspendCallbackProc *callback, bool estimate_only);

View File

@ -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<SignID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnGroupID(ScriptInstance *instance)
{
instance->engine->InsertResult(ScriptObject::GetNewGroupID());
instance->engine->InsertResult(EndianBufferReader::ToValue<GroupID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnGoalID(ScriptInstance *instance)
{
instance->engine->InsertResult(ScriptObject::GetNewGoalID());
instance->engine->InsertResult(EndianBufferReader::ToValue<GoalID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnStoryPageID(ScriptInstance *instance)
{
instance->engine->InsertResult(ScriptObject::GetNewStoryPageID());
instance->engine->InsertResult(EndianBufferReader::ToValue<StoryPageID>(ScriptObject::GetLastCommandResData()));
}
/* static */ void ScriptInstance::DoCommandReturnStoryPageElementID(ScriptInstance *instance)
{
instance->engine->InsertResult(ScriptObject::GetNewStoryPageElementID());
instance->engine->InsertResult(EndianBufferReader::ToValue<StoryPageElementID>(ScriptObject::GetLastCommandResData()));
}
ScriptStorage *ScriptInstance::GetStorage()

View File

@ -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<int> 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),

View File

@ -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<CommandCost, SignID> 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();
}

View File

@ -13,10 +13,12 @@
#include "command_type.h"
#include "signs_type.h"
CommandCost CmdPlaceSign(DoCommandFlag flags, TileIndex tile, const std::string &text);
std::tuple<CommandCost, SignID> 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 */

View File

@ -14,7 +14,6 @@
#include "tile_type.h"
struct Window;
extern SignID _new_sign_id;
void UpdateAllSignVirtCoords();
void PlaceProc_Sign(TileIndex tile);

View File

@ -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<CommandCost, StoryPageID> 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<CommandCost, StoryPageElementID> 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 };
}
/**

View File

@ -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<CommandCost, StoryPageID> CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text);
std::tuple<CommandCost, StoryPageElementID> 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);

View File

@ -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.

View File

@ -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 */

View File

@ -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

View File

@ -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<CommandCost, TownID> 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<bool> 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 };
}
/**

View File

@ -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<CommandCost, TownID> 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 */

View File

@ -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 <typename Tcallback>
void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, Tcallback cc)
{
std::string name;