From 7cc68f493d10ed81d24b9e7a0e9631436643869f Mon Sep 17 00:00:00 2001 From: alberth Date: Sat, 13 Mar 2010 17:11:28 +0000 Subject: [PATCH] (svn r19405) -Codechange: CheckOwnership() returns a CommandCost. --- src/aircraft_cmd.cpp | 15 ++++-- src/autoreplace_cmd.cpp | 5 +- src/company_cmd.cpp | 10 ++-- src/functions.h | 2 +- src/order_cmd.cpp | 103 +++++++++++++++++++++++++++++---------- src/road_cmd.cpp | 10 +++- src/roadveh_cmd.cpp | 20 ++++++-- src/ship_cmd.cpp | 14 ++++-- src/station_cmd.cpp | 53 ++++++++++++++------ src/timetable_cmd.cpp | 24 +++++++-- src/train_cmd.cpp | 39 ++++++++++++--- src/tunnelbridge_cmd.cpp | 12 +++-- src/vehicle.cpp | 5 +- src/vehicle_cmd.cpp | 22 +++++++-- src/waypoint_cmd.cpp | 14 ++++-- 15 files changed, 268 insertions(+), 80 deletions(-) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index d53cdb9004..52a011673b 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -405,13 +405,17 @@ CommandCost CmdBuildAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uin CommandCost CmdSellAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Aircraft *v = Aircraft::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE); - CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value); + ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value); if (flags & DC_EXEC) { delete v; @@ -480,7 +484,12 @@ CommandCost CmdRefitAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uin byte new_subtype = GB(p2, 8, 8); Aircraft *v = Aircraft::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE); diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index 26a92e5197..bce3f1ea58 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -628,7 +628,10 @@ CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1 Vehicle *v = Vehicle::GetIfValid(p1); if (v == NULL) return CMD_ERROR; - if (!CheckOwnership(v->owner)) return CMD_ERROR; + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + if (!v->IsInDepot()) return CMD_ERROR; if (v->vehstatus & VS_CRASHED) return CMD_ERROR; diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index e1b3e34f5d..39a889e5e7 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -248,17 +248,17 @@ void GetNameOfOwner(Owner owner, TileIndex tile) * @param owner the owner of the thing to check. * @param tile optional tile to get the right town. * @pre if tile == 0 then the owner can't be OWNER_TOWN. - * @return true iff it's owned by the current company. + * @return A succeeded command iff it's owned by the current company, else a failed command. */ -bool CheckOwnership(Owner owner, TileIndex tile) +CommandCost CheckOwnership(Owner owner, TileIndex tile) { assert(owner < OWNER_END); assert(owner != OWNER_TOWN || tile != 0); - if (owner == _current_company) return true; - _error_message = STR_ERROR_OWNED_BY; + if (owner == _current_company) return CommandCost(); + GetNameOfOwner(owner, tile); - return false; + return_cmd_error(STR_ERROR_OWNED_BY); } /** diff --git a/src/functions.h b/src/functions.h index 6acbb9af7c..a6743b2ac2 100644 --- a/src/functions.h +++ b/src/functions.h @@ -24,7 +24,7 @@ void TileLoopClearHelper(TileIndex tile); bool CheckCompanyHasMoney(CommandCost &cost); void SubtractMoneyFromCompany(CommandCost cost); void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cost); -bool CheckOwnership(Owner owner, TileIndex tile = 0); +CommandCost CheckOwnership(Owner owner, TileIndex tile = 0); CommandCost CheckTileOwnership(TileIndex tile); /* misc functions */ diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index e1985893e3..bd84015d25 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -468,7 +468,11 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 Order new_order(p2); Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Check if the inserted order is to the correct destination (owner, type), * and has the correct flags if any */ @@ -477,7 +481,11 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 const Station *st = Station::GetIfValid(new_order.GetDestination()); if (st == NULL) return CMD_ERROR; - if (st->owner != OWNER_NONE && !CheckOwnership(st->owner)) return CMD_ERROR; + if (st->owner != OWNER_NONE) { + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + } if (!CanVehicleUseStation(v, st)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER); for (Vehicle *u = v->FirstShared(); u != NULL; u = u->NextShared()) { @@ -521,15 +529,23 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 if (v->type == VEH_AIRCRAFT) { const Station *st = Station::GetIfValid(new_order.GetDestination()); - if (st == NULL || !CheckOwnership(st->owner) || - !CanVehicleUseStation(v, st) || - st->GetAirportSpec()->nof_depots == 0) { + if (st == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + + if (!CanVehicleUseStation(v, st) || st->GetAirportSpec()->nof_depots == 0) { return CMD_ERROR; } } else { const Depot *dp = Depot::GetIfValid(new_order.GetDestination()); - if (dp == NULL || !CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR; + if (dp == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(GetTileOwner(dp->xy)); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; switch (v->type) { case VEH_TRAIN: @@ -565,14 +581,22 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 switch (v->type) { default: return CMD_ERROR; - case VEH_TRAIN: + case VEH_TRAIN: { if (!(wp->facilities & FACIL_TRAIN)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER); - if (!CheckOwnership(wp->owner)) return CMD_ERROR; + + CommandCost ret = CheckOwnership(wp->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; break; + } case VEH_SHIP: if (!(wp->facilities & FACIL_DOCK)) return_cmd_error(STR_ERROR_CAN_T_ADD_ORDER); - if (!CheckOwnership(wp->owner) && wp->owner != OWNER_NONE) return CMD_ERROR; + if (wp->owner != OWNER_NONE) { + CommandCost ret = CheckOwnership(wp->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + } break; } @@ -729,7 +753,11 @@ CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 Vehicle *v = Vehicle::GetIfValid(veh_id); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* If we did not select an order, we maybe want to de-clone the orders */ if (sel_ord >= v->GetNumOrders()) @@ -795,10 +823,11 @@ CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 Vehicle *v = Vehicle::GetIfValid(veh_id); - if (v == NULL || !CheckOwnership(v->owner) || sel_ord == v->cur_order_index || - sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) { - return CMD_ERROR; - } + if (v == NULL || sel_ord == v->cur_order_index || sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (flags & DC_EXEC) { v->cur_order_index = sel_ord; @@ -835,7 +864,11 @@ CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 VehicleOrderID target_order = GB(p2, 16, 16); Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Don't make senseless movements */ if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() || @@ -916,7 +949,11 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 if (mof >= MOF_END) return CMD_ERROR; Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Is it a valid order? */ if (sel_ord >= v->GetNumOrders()) return CMD_ERROR; @@ -1139,17 +1176,22 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 VehicleID veh_dst = GB(p1, 0, 16); Vehicle *dst = Vehicle::GetIfValid(veh_dst); + if (dst == NULL) return CMD_ERROR; - if (dst == NULL || !CheckOwnership(dst->owner)) return CMD_ERROR; + CommandCost ret = CheckOwnership(dst->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; switch (p2) { case CO_SHARE: { Vehicle *src = Vehicle::GetIfValid(veh_src); /* Sanity checks */ - if (src == NULL || !CheckOwnership(src->owner) || dst->type != src->type || dst == src) { - return CMD_ERROR; - } + if (src == NULL || dst->type != src->type || dst == src) return CMD_ERROR; + + CommandCost ret = CheckOwnership(src->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Trucks can't share orders with busses (and visa versa) */ if (src->type == VEH_ROAD && RoadVehicle::From(src)->IsBus() != RoadVehicle::From(dst)->IsBus()) { @@ -1188,9 +1230,11 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 Vehicle *src = Vehicle::GetIfValid(veh_src); /* Sanity checks */ - if (src == NULL || !CheckOwnership(src->owner) || dst->type != src->type || dst == src) { - return CMD_ERROR; - } + if (src == NULL || dst->type != src->type || dst == src) return CMD_ERROR; + + CommandCost ret = CheckOwnership(src->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Trucks can't copy all the orders from busses (and visa versa), * and neither can helicopters and aircarft. */ @@ -1264,7 +1308,11 @@ CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 byte subtype = GB(p2, 8, 8); const Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; Order *order = v->GetOrder(order_number); if (order == NULL) return CMD_ERROR; @@ -1416,7 +1464,12 @@ CommandCost CmdRestoreOrderIndex(TileIndex tile, DoCommandFlag flags, uint32 p1, Vehicle *v = Vehicle::GetIfValid(p1); /* Check the vehicle type and ownership, and if the service interval and order are in range */ - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + if (serv_int != GetServiceIntervalClamped(serv_int, v->owner) || cur_ord >= v->GetNumOrders()) return CMD_ERROR; if (flags & DC_EXEC) { diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp index b58c552f8f..c7940cf7f8 100644 --- a/src/road_cmd.cpp +++ b/src/road_cmd.cpp @@ -131,7 +131,9 @@ CommandCost CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, Owner owner, R * by a town */ if (owner != OWNER_TOWN) { if (owner == OWNER_NONE) return CommandCost(); - return CheckOwnership(owner) ? CommandCost() : CMD_ERROR; + CommandCost ret = CheckOwnership(owner); + ret.SetGlobalErrorMessage(); + return ret; } if (!town_check) return CommandCost(); @@ -502,7 +504,11 @@ CommandCost CmdBuildRoad(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 if (crossing) return_cmd_error(STR_ERROR_ONEWAY_ROADS_CAN_T_HAVE_JUNCTION); Owner owner = GetRoadOwner(tile, ROADTYPE_ROAD); - if (owner != OWNER_NONE && !CheckOwnership(owner, tile)) return CMD_ERROR; + if (owner != OWNER_NONE) { + CommandCost ret = CheckOwnership(owner, tile); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + } CommandCost ret = EnsureNoVehicleOnGround(tile); ret.SetGlobalErrorMessage(); diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 6f54f2ae9c..e82bcfcfc7 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -331,7 +331,11 @@ bool RoadVehicle::IsStoppedInDepot() const CommandCost CmdSellRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { RoadVehicle *v = RoadVehicle::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE); @@ -339,7 +343,7 @@ CommandCost CmdSellRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 return_cmd_error(STR_ERROR_ROAD_VEHICLE_MUST_BE_STOPPED_INSIDE_DEPOT); } - CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value); + ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value); if (flags & DC_EXEC) { delete v; @@ -406,7 +410,11 @@ CommandCost CmdSendRoadVehToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1 CommandCost CmdTurnRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { RoadVehicle *v = RoadVehicle::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if ((v->vehstatus & VS_STOPPED) || (v->vehstatus & VS_CRASHED) || @@ -1748,8 +1756,12 @@ CommandCost CmdRefitRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint bool only_this = HasBit(p2, 16); RoadVehicle *v = RoadVehicle::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_ROAD_VEHICLE_MUST_BE_STOPPED_INSIDE_DEPOT); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE); diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index ec907d2d77..6b5cb91f83 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -709,7 +709,11 @@ CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Ship *v = Ship::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE); @@ -717,7 +721,7 @@ CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT); } - CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value); + ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value); if (flags & DC_EXEC) { delete v; @@ -780,8 +784,12 @@ CommandCost CmdRefitShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 byte new_subtype = GB(p2, 8, 8); Ship *v = Ship::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE); diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 3747ae5e18..40efc314c8 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -865,8 +865,10 @@ static CommandCost CheckFlatLandRoadStop(TileArea tile_area, DoCommandFlag flags Owner road_owner = GetRoadOwner(cur_tile, ROADTYPE_ROAD); if (road_owner == OWNER_TOWN) { if (!_settings_game.construction.road_stop_on_town_road) return_cmd_error(STR_ERROR_DRIVE_THROUGH_ON_TOWN_ROAD); - } else if (!_settings_game.construction.road_stop_on_competitor_road && road_owner != OWNER_NONE && !CheckOwnership(road_owner)) { - return CMD_ERROR; + } else if (!_settings_game.construction.road_stop_on_competitor_road && road_owner != OWNER_NONE) { + CommandCost ret = CheckOwnership(road_owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; } num_roadbits += CountBits(GetRoadBits(cur_tile, ROADTYPE_ROAD)); } @@ -874,8 +876,10 @@ static CommandCost CheckFlatLandRoadStop(TileArea tile_area, DoCommandFlag flags /* There is a tram, check if we can build road+tram stop over it. */ if (HasBit(cur_rts, ROADTYPE_TRAM)) { Owner tram_owner = GetRoadOwner(cur_tile, ROADTYPE_TRAM); - if (!_settings_game.construction.road_stop_on_competitor_road && tram_owner != OWNER_NONE && !CheckOwnership(tram_owner)) { - return CMD_ERROR; + if (!_settings_game.construction.road_stop_on_competitor_road && tram_owner != OWNER_NONE) { + CommandCost ret = CheckOwnership(tram_owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; } num_roadbits += CountBits(GetRoadBits(cur_tile, ROADTYPE_TRAM)); } @@ -1383,7 +1387,14 @@ CommandCost RemoveFromRailBaseStation(TileArea ta, SmallVector &affected /* Check ownership of station */ T *st = T::GetByTile(tile); if (st == NULL) continue; - if (_current_company != OWNER_WATER && !CheckOwnership(st->owner)) continue; + + if (_current_company != OWNER_WATER) { + CommandCost ret = CheckOwnership(st->owner); + if (ret.Failed()) { + ret.SetGlobalErrorMessage(); + continue; + } + } /* Do not allow removing from stations if non-uniform stations are not enabled * The check must be here to give correct error message @@ -1529,7 +1540,11 @@ template CommandCost RemoveRailStation(T *st, DoCommandFlag flags) { /* Current company owns the station? */ - if (_current_company != OWNER_WATER && !CheckOwnership(st->owner)) return CMD_ERROR; + if (_current_company != OWNER_WATER) { + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + } /* determine width and height of platforms */ TileArea ta = st->train_station; @@ -1829,8 +1844,10 @@ static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags) { Station *st = Station::GetByTile(tile); - if (_current_company != OWNER_WATER && !CheckOwnership(st->owner)) { - return CMD_ERROR; + if (_current_company != OWNER_WATER) { + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; } bool is_truck = IsTruckStop(tile); @@ -2241,8 +2258,10 @@ static CommandCost RemoveAirport(TileIndex tile, DoCommandFlag flags) { Station *st = Station::GetByTile(tile); - if (_current_company != OWNER_WATER && !CheckOwnership(st->owner)) { - return CMD_ERROR; + if (_current_company != OWNER_WATER) { + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; } tile = st->airport.tile; @@ -2456,12 +2475,14 @@ CommandCost CmdBuildDock(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 static CommandCost RemoveDock(TileIndex tile, DoCommandFlag flags) { Station *st = Station::GetByTile(tile); - if (!CheckOwnership(st->owner)) return CMD_ERROR; + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; TileIndex tile1 = st->dock_tile; TileIndex tile2 = tile1 + TileOffsByDiagDir(GetDockDirection(tile1)); - CommandCost ret = EnsureNoVehicleOnGround(tile1); + ret = EnsureNoVehicleOnGround(tile1); if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile2); ret.SetGlobalErrorMessage(); if (ret.Failed()) return ret; @@ -3194,7 +3215,11 @@ static bool IsUniqueStationName(const char *name) CommandCost CmdRenameStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Station *st = Station::GetIfValid(p1); - if (st == NULL || !CheckOwnership(st->owner)) return CMD_ERROR; + if (st == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(st->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; bool reset = StrEmpty(text); @@ -3441,7 +3466,7 @@ static bool CanRemoveRoadWithStop(TileIndex tile, DoCommandFlag flags) if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); - if ((road_owner != OWNER_TOWN && !CheckOwnership(road_owner)) || !CheckOwnership(tram_owner)) return false; + if ((road_owner != OWNER_TOWN && CheckOwnership(road_owner).Failed()) || CheckOwnership(tram_owner).Failed()) return false; return road_owner != OWNER_TOWN || CheckAllowRemoveRoad(tile, GetAnyRoadBits(tile, ROADTYPE_ROAD), OWNER_TOWN, ROADTYPE_ROAD, flags).Succeeded(); } diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index 548e3237e3..80f8a39bc8 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -68,7 +68,11 @@ CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, u VehicleID veh = GB(p1, 0, 16); Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; + if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; VehicleOrderID order_number = GB(p1, 16, 8); Order *order = v->GetOrder(order_number); @@ -128,7 +132,11 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, VehicleID veh = GB(p1, 0, 16); Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; + if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (flags & DC_EXEC) { v->lateness_counter = 0; @@ -150,7 +158,11 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, if (!_settings_game.order.timetabling) return CMD_ERROR; Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 16)); - if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; + if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */ Date start_date = (Date)p2; @@ -190,7 +202,11 @@ CommandCost CmdAutofillTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, VehicleID veh = GB(p1, 0, 16); Vehicle *v = Vehicle::GetIfValid(veh); - if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; + if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (flags & DC_EXEC) { if (HasBit(p2, 0)) { diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index b4f9689c4d..c3e19eb379 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1169,7 +1169,11 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u bool move_chain = HasBit(p2, 0); Train *src = Train::GetIfValid(s); - if (src == NULL || !CheckOwnership(src->owner)) return CMD_ERROR; + if (src == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(src->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */ if (src->vehstatus & VS_CRASHED) return CMD_ERROR; @@ -1180,7 +1184,11 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u dst = src->IsEngine() ? NULL : FindGoodVehiclePos(src); } else { dst = Train::GetIfValid(d); - if (dst == NULL || !CheckOwnership(dst->owner)) return CMD_ERROR; + if (dst == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(dst->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Do not allow appending to crashed vehicles, too */ if (dst->vehstatus & VS_CRASHED) return CMD_ERROR; @@ -1342,7 +1350,11 @@ CommandCost CmdSellRailWagon(TileIndex tile, DoCommandFlag flags, uint32 p1, uin Window *w = NULL; Train *v = Train::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; /* Sell a chain of vehicles or not? */ bool sell_chain = HasBit(p2, 0); @@ -1372,7 +1384,7 @@ CommandCost CmdSellRailWagon(TileIndex tile, DoCommandFlag flags, uint32 p1, uin ArrangeTrains(&sell_head, NULL, &new_head, v, sell_chain); /* We don't need to validate the second train; it's going to be sold. */ - CommandCost ret = ValidateTrains(NULL, NULL, first, new_head); + ret = ValidateTrains(NULL, NULL, first, new_head); if (ret.Failed()) { /* Restore the train we had. */ RestoreTrainBackup(original); @@ -1815,7 +1827,11 @@ static void ReverseTrainDirection(Train *v) CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Train *v = Train::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (p2 != 0) { /* turn a single unit around */ @@ -1882,7 +1898,12 @@ CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Train *t = Train::GetIfValid(p1); - if (t == NULL || !CheckOwnership(t->owner)) return CMD_ERROR; + if (t == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(t->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + if (flags & DC_EXEC) { /* If we are forced to proceed, cancel that order. @@ -1915,7 +1936,11 @@ CommandCost CmdRefitRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, bool only_this = HasBit(p2, 16); Train *v = Train::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (!v->IsStoppedInDepot()) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE); diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 654a5db3ed..5bf567edac 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -613,12 +613,18 @@ static inline CommandCost CheckAllowRemoveTunnelBridge(TileIndex tile) if (road_owner == OWNER_NONE || road_owner == OWNER_TOWN) road_owner = _current_company; if (tram_owner == OWNER_NONE) tram_owner = _current_company; - return (CheckOwnership(road_owner, tile) && CheckOwnership(tram_owner, tile)) ? CommandCost() : CMD_ERROR; + CommandCost ret = CheckOwnership(road_owner, tile); + if (ret.Succeeded()) ret = CheckOwnership(tram_owner, tile); + ret.SetGlobalErrorMessage(); + return ret; } case TRANSPORT_RAIL: - case TRANSPORT_WATER: - return CheckOwnership(GetTileOwner(tile)) ? CommandCost() : CMD_ERROR; + case TRANSPORT_WATER: { + CommandCost ret = CheckOwnership(GetTileOwner(tile)); + ret.SetGlobalErrorMessage(); + return ret; + } default: NOT_REACHED(); } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 44e3991ff8..0797ee131b 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1665,7 +1665,10 @@ void Vehicle::HandleLoading(bool mode) CommandCost Vehicle::SendToDepot(DoCommandFlag flags, DepotCommand command) { - if (!CheckOwnership(this->owner)) return CMD_ERROR; + CommandCost ret = CheckOwnership(this->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + if (this->vehstatus & VS_CRASHED) return CMD_ERROR; if (this->IsStoppedInDepot()) return CMD_ERROR; diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index f4527696bb..0f96e46156 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -74,7 +74,11 @@ CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0); Vehicle *v = Vehicle::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; + if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; switch (v->type) { case VEH_TRAIN: @@ -406,7 +410,9 @@ CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains */ - if (!CheckOwnership(v->owner)) return CMD_ERROR; + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; if (v->type == VEH_TRAIN && (!Train::From(v)->IsFrontEngine() || Train::From(v)->crash_anim_pos >= 4400)) return CMD_ERROR; @@ -607,7 +613,11 @@ CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool s CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Vehicle *v = Vehicle::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; bool reset = StrEmpty(text); @@ -638,7 +648,11 @@ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uin CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Vehicle *v = Vehicle::GetIfValid(p1); - if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL) return CMD_ERROR; + + CommandCost ret = CheckOwnership(v->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; uint16 serv_int = GetServiceIntervalClamped(p2, v->owner); // Double check the service interval from the user-input if (serv_int != p2) return CMD_ERROR; diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index 04db4fc930..f9f0b43629 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -178,9 +178,11 @@ static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID * if (GetAxisForNewWaypoint(tile) != axis) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK); Owner owner = GetTileOwner(tile); - if (!CheckOwnership(owner)) return CMD_ERROR; + CommandCost ret = CheckOwnership(owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; - CommandCost ret = EnsureNoVehicleOnGround(tile); + ret = EnsureNoVehicleOnGround(tile); ret.SetGlobalErrorMessage(); if (ret.Failed()) return ret; @@ -445,7 +447,13 @@ static bool IsUniqueWaypointName(const char *name) CommandCost CmdRenameWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Waypoint *wp = Waypoint::GetIfValid(p1); - if (wp == NULL || !(CheckOwnership(wp->owner) || wp->owner == OWNER_NONE)) return CMD_ERROR; + if (wp == NULL) return CMD_ERROR; + + if (wp->owner != OWNER_NONE) { + CommandCost ret = CheckOwnership(wp->owner); + ret.SetGlobalErrorMessage(); + if (ret.Failed()) return ret; + } bool reset = StrEmpty(text);