Skip to content

Commit

Permalink
Codechange: Remove unnecessary 'return_cmd_error` macro. (OpenTTD#13160)
Browse files Browse the repository at this point in the history
This macro is a leftover from when errors used to be packed into a single int32_t.

`return CommandCost` is clearer, and doesn't need a macro.
  • Loading branch information
PeterN authored Dec 8, 2024
1 parent 369e8a6 commit 1e77fd0
Show file tree
Hide file tree
Showing 27 changed files with 276 additions and 286 deletions.
10 changes: 0 additions & 10 deletions src/command_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@
*/
static const CommandCost CMD_ERROR = CommandCost(INVALID_STRING_ID);

/**
* Returns from a function with a specific StringID as error.
*
* This macro is used to return from a function. The parameter contains the
* StringID which will be returned.
*
* @param errcode The StringID to return
*/
#define return_cmd_error(errcode) return CommandCost(errcode);

void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, const CommandDataBuffer &cmd_data);

bool IsValidCommand(Commands cmd);
Expand Down
10 changes: 5 additions & 5 deletions src/company_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ CommandCost CheckOwnership(Owner owner, TileIndex tile)
if (owner == _current_company) return CommandCost();

SetDParamsForOwnedBy(owner, tile);
return_cmd_error(STR_ERROR_OWNED_BY);
return CommandCost(STR_ERROR_OWNED_BY);
}

/**
Expand All @@ -388,7 +388,7 @@ CommandCost CheckTileOwnership(TileIndex tile)

/* no need to get the name of the owner unless we're the local company (saves some time) */
if (IsLocalCompany()) SetDParamsForOwnedBy(owner, tile);
return_cmd_error(STR_ERROR_OWNED_BY);
return CommandCost(STR_ERROR_OWNED_BY);
}

/**
Expand Down Expand Up @@ -1172,7 +1172,7 @@ CommandCost CmdRenameCompany(DoCommandFlag flags, const std::string &text)

if (!reset) {
if (Utf8StringLength(text) >= MAX_LENGTH_COMPANY_NAME_CHARS) return CMD_ERROR;
if (!IsUniqueCompanyName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
if (!IsUniqueCompanyName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
}

if (flags & DC_EXEC) {
Expand Down Expand Up @@ -1215,7 +1215,7 @@ CommandCost CmdRenamePresident(DoCommandFlag flags, const std::string &text)

if (!reset) {
if (Utf8StringLength(text) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return CMD_ERROR;
if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
if (!IsUniquePresidentName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
}

if (flags & DC_EXEC) {
Expand Down Expand Up @@ -1301,7 +1301,7 @@ CommandCost CmdGiveMoney(DoCommandFlag flags, Money money, CompanyID dest_compan
CommandCost amount(EXPENSES_OTHER, std::min<Money>(money, 20000000LL));

/* You can only transfer funds that is in excess of your loan */
if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return_cmd_error(STR_ERROR_INSUFFICIENT_FUNDS);
if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CommandCost(STR_ERROR_INSUFFICIENT_FUNDS);
if (!Company::IsValidID(dest_company)) return CMD_ERROR;

if (flags & DC_EXEC) {
Expand Down
2 changes: 1 addition & 1 deletion src/depot_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ CommandCost CmdRenameDepot(DoCommandFlag flags, DepotID depot_id, const std::str

if (!reset) {
if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR;
if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
if (!IsUniqueDepotName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
}

if (flags & DC_EXEC) {
Expand Down
2 changes: 1 addition & 1 deletion src/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ CommandCost CmdBuyCompany(DoCommandFlag flags, CompanyID target_company, bool ho
if (target_company == _current_company) return CMD_ERROR;

/* Do not allow takeover if the resulting company would have too many vehicles. */
if (!CheckTakeoverVehicleLimit(_current_company, target_company)) return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
if (!CheckTakeoverVehicleLimit(_current_company, target_company)) return CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);

/* Get the cost here as the company is deleted in DoAcquireCompany.
* For bankruptcy this amount is calculated when the offer was made;
Expand Down
2 changes: 1 addition & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ CommandCost CmdRenameEngine(DoCommandFlag flags, EngineID engine_id, const std::

if (!reset) {
if (Utf8StringLength(text) >= MAX_LENGTH_ENGINE_NAME_CHARS) return CMD_ERROR;
if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
if (!IsUniqueEngineName(text)) return CommandCost(STR_ERROR_NAME_MUST_BE_UNIQUE);
}

if (flags & DC_EXEC) {
Expand Down
2 changes: 1 addition & 1 deletion src/group_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ CommandCost CmdAlterGroup(DoCommandFlag flags, AlterGroupMode mode, GroupID grou

/* Ensure request parent isn't child of group.
* This is the only place that infinite loops are prevented. */
if (GroupIsInGroup(pg->index, g->index)) return_cmd_error(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
if (GroupIsInGroup(pg->index, g->index)) return CommandCost(STR_ERROR_GROUP_CAN_T_SET_PARENT_RECURSION);
}

if (flags & DC_EXEC) {
Expand Down
42 changes: 21 additions & 21 deletions src/industry_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
SetDParam(1, indspec->name);
return_cmd_error(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
return CommandCost(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
}

if (flags & DC_EXEC) {
Expand Down Expand Up @@ -1273,7 +1273,7 @@ static CommandCost CheckNewIndustry_Forest(TileIndex tile)
{
if (_settings_game.game_creation.landscape == LT_ARCTIC) {
if (GetTileZ(tile) < HighestSnowLine() + 2) {
return_cmd_error(STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED);
return CommandCost(STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED);
}
}
return CommandCost();
Expand Down Expand Up @@ -1313,7 +1313,7 @@ static CommandCost CheckNewIndustry_OilRefinery(TileIndex tile)

if (CheckScaledDistanceFromEdge(TileAddXY(tile, 1, 1), _settings_game.game_creation.oil_refinery_limit)) return CommandCost();

return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
return CommandCost(STR_ERROR_CAN_ONLY_BE_POSITIONED);
}

extern bool _ignore_restrictions;
Expand All @@ -1330,7 +1330,7 @@ static CommandCost CheckNewIndustry_OilRig(TileIndex tile)
if (TileHeight(tile) == 0 &&
CheckScaledDistanceFromEdge(TileAddXY(tile, 1, 1), _settings_game.game_creation.oil_refinery_limit)) return CommandCost();

return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
return CommandCost(STR_ERROR_CAN_ONLY_BE_POSITIONED);
}

/**
Expand All @@ -1342,7 +1342,7 @@ static CommandCost CheckNewIndustry_Farm(TileIndex tile)
{
if (_settings_game.game_creation.landscape == LT_ARCTIC) {
if (GetTileZ(tile) + 2 >= HighestSnowLine()) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}
}
return CommandCost();
Expand All @@ -1356,7 +1356,7 @@ static CommandCost CheckNewIndustry_Farm(TileIndex tile)
static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
{
if (GetTropicZone(tile) == TROPICZONE_DESERT) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}
return CommandCost();
}
Expand All @@ -1369,7 +1369,7 @@ static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
static CommandCost CheckNewIndustry_Water(TileIndex tile)
{
if (GetTropicZone(tile) != TROPICZONE_DESERT) {
return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
return CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
}
return CommandCost();
}
Expand All @@ -1382,7 +1382,7 @@ static CommandCost CheckNewIndustry_Water(TileIndex tile)
static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
{
if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
return CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
}
return CommandCost();
}
Expand All @@ -1395,7 +1395,7 @@ static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
static CommandCost CheckNewIndustry_BubbleGen(TileIndex tile)
{
if (GetTileZ(tile) > 4) {
return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS);
return CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS);
}
return CommandCost();
}
Expand Down Expand Up @@ -1439,7 +1439,7 @@ static CommandCost FindTownForIndustry(TileIndex tile, IndustryType type, Town *
for (const IndustryID &industry : Industry::industries[type]) {
if (Industry::Get(industry)->town == *t) {
*t = nullptr;
return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
return CommandCost(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
}
}

Expand Down Expand Up @@ -1479,28 +1479,28 @@ static CommandCost CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTil
TileIndex cur_tile = TileAddWrap(tile, it.ti.x, it.ti.y);

if (!IsValidTile(cur_tile)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}

if (gfx == GFX_WATERTILE_SPECIALCHECK) {
if (!IsWaterTile(cur_tile) ||
!IsTileFlat(cur_tile)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}
} else {
CommandCost ret = EnsureNoVehicleOnGround(cur_tile);
if (ret.Failed()) return ret;
if (IsBridgeAbove(cur_tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
if (IsBridgeAbove(cur_tile)) return CommandCost(STR_ERROR_SITE_UNSUITABLE);

const IndustryTileSpec *its = GetIndustryTileSpec(gfx);

/* Perform land/water check if not disabled */
if (!HasBit(its->slopes_refused, 5) && ((HasTileWaterClass(cur_tile) && IsTileOnWater(cur_tile)) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
if (!HasBit(its->slopes_refused, 5) && ((HasTileWaterClass(cur_tile) && IsTileOnWater(cur_tile)) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return CommandCost(STR_ERROR_SITE_UNSUITABLE);

if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) || // Tile must be a house
((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) { // Tile is allowed to be a house (and it is a house)
if (!IsTileType(cur_tile, MP_HOUSE)) {
return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS);
return CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS);
}

/* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */
Expand Down Expand Up @@ -1564,7 +1564,7 @@ static CommandCost CheckIfIndustryTileSlopes(TileIndex tile, const IndustryTileL
if (!refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions)) {
return CommandCost();
}
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}

/**
Expand All @@ -1577,11 +1577,11 @@ static CommandCost CheckIfIndustryTileSlopes(TileIndex tile, const IndustryTileL
static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, IndustryType type, const Town *t)
{
if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->cache.population < 1200) {
return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
return CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
}

if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_NEAR_TOWN_CENTER);
return CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_NEAR_TOWN_CENTER);
}

return CommandCost();
Expand Down Expand Up @@ -1702,7 +1702,7 @@ static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, Indus
/* Within 14 tiles from another industry is considered close */
if (DistanceMax(tile, Industry::Get(industry)->location.tile) > 14) continue;

return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
return CommandCost(STR_ERROR_INDUSTRY_TOO_CLOSE);
}
}
return CommandCost();
Expand Down Expand Up @@ -2021,10 +2021,10 @@ static CommandCost CreateNewIndustryHelper(TileIndex tile, IndustryType type, Do

if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world &&
!_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, layout)) {
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}

if (!Industry::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_INDUSTRIES);
if (!Industry::CanAllocateItem()) return CommandCost(STR_ERROR_TOO_MANY_INDUSTRIES);

if (flags & DC_EXEC) {
*ip = new Industry(tile);
Expand Down
6 changes: 3 additions & 3 deletions src/landscape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,14 @@ CommandCost CmdLandscapeClear(DoCommandFlag flags, TileIndex tile)
bool do_clear = false;
/* Test for stuff which results in water when cleared. Then add the cost to also clear the water. */
if ((flags & DC_FORCE_CLEAR_TILE) && HasTileWaterClass(tile) && IsTileOnWater(tile) && !IsWaterTile(tile) && !IsCoastTile(tile)) {
if ((flags & DC_AUTO) && GetWaterClass(tile) == WATER_CLASS_CANAL) return_cmd_error(STR_ERROR_MUST_DEMOLISH_CANAL_FIRST);
if ((flags & DC_AUTO) && GetWaterClass(tile) == WATER_CLASS_CANAL) return CommandCost(STR_ERROR_MUST_DEMOLISH_CANAL_FIRST);
do_clear = true;
cost.AddCost(GetWaterClass(tile) == WATER_CLASS_CANAL ? _price[PR_CLEAR_CANAL] : _price[PR_CLEAR_WATER]);
}

Company *c = (flags & (DC_AUTO | DC_BANKRUPT)) ? nullptr : Company::GetIfValid(_current_company);
if (c != nullptr && (int)GB(c->clear_limit, 16, 16) < 1) {
return_cmd_error(STR_ERROR_CLEARING_LIMIT_REACHED);
return CommandCost(STR_ERROR_CLEARING_LIMIT_REACHED);
}

const ClearedObjectArea *coa = FindClearedObject(tile);
Expand All @@ -677,7 +677,7 @@ CommandCost CmdLandscapeClear(DoCommandFlag flags, TileIndex tile)

/* If a object is removed, it leaves either bare land or water. */
if ((flags & DC_NO_WATER) && HasTileWaterClass(tile) && IsTileOnWater(tile)) {
return_cmd_error(STR_ERROR_CAN_T_BUILD_ON_WATER);
return CommandCost(STR_ERROR_CAN_T_BUILD_ON_WATER);
}
} else {
cost.AddCost(_tile_type_procs[GetTileType(tile)]->clear_tile_proc(tile, flags));
Expand Down
6 changes: 3 additions & 3 deletions src/misc_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ CommandCost CmdIncreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
Money max_loan = c->GetMaxLoan();
if (c->current_loan >= max_loan) {
SetDParam(0, max_loan);
return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
return CommandCost(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
}

Money loan;
Expand Down Expand Up @@ -87,7 +87,7 @@ CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
{
Company *c = Company::Get(_current_company);

if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
if (c->current_loan == 0) return CommandCost(STR_ERROR_LOAN_ALREADY_REPAYED);

Money loan;
switch (cmd) {
Expand All @@ -107,7 +107,7 @@ CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)

if (GetAvailableMoneyForCommand() < loan) {
SetDParam(0, loan);
return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
return CommandCost(STR_ERROR_CURRENCY_REQUIRED);
}

if (flags & DC_EXEC) {
Expand Down
4 changes: 2 additions & 2 deletions src/newgrf_industrytiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ CommandCost PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind
uint16_t callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | (uint32_t)layout_index, gfx, &ind, ind_tile);
if (callback_res == CALLBACK_FAILED) {
if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}
if (its->grf_prop.grffile->grf_version < 7) {
if (callback_res != 0) return CommandCost();
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
return CommandCost(STR_ERROR_SITE_UNSUITABLE);
}

return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile, STR_ERROR_SITE_UNSUITABLE);
Expand Down
Loading

0 comments on commit 1e77fd0

Please sign in to comment.