From 512db5248e043c48499c1bdf057a54e9da3cb136 Mon Sep 17 00:00:00 2001 From: smatz Date: Mon, 18 May 2009 16:21:28 +0000 Subject: [PATCH] (svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get() --- src/ai/ai_core.cpp | 15 +++--- src/ai/ai_gui.cpp | 9 ++-- src/ai/api/ai_group.cpp | 3 +- src/ai/api/ai_sign.cpp | 3 +- src/ai/api/ai_station.cpp | 3 +- src/ai/api/ai_vehicle.cpp | 5 +- src/ai/api/ai_waypoint.cpp | 3 +- src/aircraft_cmd.cpp | 28 ++++------ src/autoreplace_cmd.cpp | 5 +- src/command.cpp | 5 +- src/company_cmd.cpp | 30 +++++------ src/console_cmds.cpp | 9 ++-- src/economy.cpp | 20 ++++--- src/graph_gui.cpp | 4 +- src/group.h | 6 ++- src/group_cmd.cpp | 28 ++++------ src/group_gui.cpp | 8 +-- src/highscore_gui.cpp | 5 +- src/network/network.cpp | 2 +- src/newgrf_industries.cpp | 4 +- src/oldpool.h | 12 +++++ src/order_cmd.cpp | 107 ++++++++++++------------------------- src/road.cpp | 5 +- src/roadveh_cmd.cpp | 32 +++-------- src/saveload/afterload.cpp | 4 +- src/ship_cmd.cpp | 23 +++----- src/signs_cmd.cpp | 7 +-- src/station_cmd.cpp | 6 +-- src/strings.cpp | 4 +- src/timetable_cmd.cpp | 15 +++--- src/town_cmd.cpp | 10 ++-- src/train_cmd.cpp | 48 ++++++----------- src/tunnelbridge_cmd.cpp | 6 +-- src/vehicle_cmd.cpp | 26 +++------ src/waypoint_cmd.cpp | 6 +-- 35 files changed, 201 insertions(+), 305 deletions(-) diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index df470d035d..0eb4fe29da 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -74,7 +74,8 @@ * Effectively collecting garbage once every two months per AI. */ if ((AI::frame_counter & 255) == 0) { CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4); - if (Company::IsValidID(cid) && !IsHumanCompany(cid)) Company::Get(cid)->ai_instance->CollectGarbage(); + Company *com = Company::GetIfValid(cid); + if (com != NULL && !IsHumanCompany(cid)) com->ai_instance->CollectGarbage(); } _current_company = OWNER_NONE; @@ -227,12 +228,12 @@ void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) /* static */ void AI::Save(CompanyID company) { if (!_networking || _network_server) { - assert(Company::IsValidID(company)); - assert(Company::Get(company)->ai_instance != NULL); + Company *c = Company::GetIfValid(company); + assert(c != NULL && c->ai_instance != NULL); CompanyID old_company = _current_company; _current_company = company; - Company::Get(company)->ai_instance->Save(); + c->ai_instance->Save(); _current_company = old_company; } else { AIInstance::SaveEmpty(); @@ -242,12 +243,12 @@ void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) /* static */ void AI::Load(CompanyID company, int version) { if (!_networking || _network_server) { - assert(Company::IsValidID(company)); - assert(Company::Get(company)->ai_instance != NULL); + Company *c = Company::GetIfValid(company); + assert(c != NULL && c->ai_instance != NULL); CompanyID old_company = _current_company; _current_company = company; - Company::Get(company)->ai_instance->Load(version); + c->ai_instance->Load(version); _current_company = old_company; } else { /* Read, but ignore, the load data */ diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index f0ffee121d..3f4746a02d 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -641,7 +641,8 @@ struct AIDebugWindow : public Window { { /* Disable the companies who are not active or not an AI */ for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { - this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, !Company::IsValidID(i) || !Company::Get(i)->is_ai); + Company *c = Company::GetIfValid(i); + this->SetWidgetDisabledState(i + AID_WIDGET_COMPANY_BUTTON_START, c == NULL || !c->is_ai); } this->DisableWidget(AID_WIDGET_RELOAD_TOGGLE); @@ -669,7 +670,8 @@ struct AIDebugWindow : public Window { } for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { - if (Company::IsValidID(i) && Company::Get(i)->is_ai) { + Company *c = Company::GetIfValid(i); + if (c != NULL && c->is_ai) { /* Lower the widget corresponding to this company. */ this->LowerWidget(i + AID_WIDGET_COMPANY_BUTTON_START); @@ -690,7 +692,8 @@ struct AIDebugWindow : public Window { /* Paint the company icons */ for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { - if (!Company::IsValidID(i) || !Company::Get(i)->is_ai) { + Company *c = Company::GetIfValid(i); + if (c == NULL || !c->is_ai) { /* Check if we have the company as an active company */ if (!this->IsWidgetDisabled(i + AID_WIDGET_COMPANY_BUTTON_START)) { /* Bah, company gone :( */ diff --git a/src/ai/api/ai_group.cpp b/src/ai/api/ai_group.cpp index 6f4d25de9d..cbcc8162b6 100644 --- a/src/ai/api/ai_group.cpp +++ b/src/ai/api/ai_group.cpp @@ -16,7 +16,8 @@ /* static */ bool AIGroup::IsValidGroup(GroupID group_id) { - return ::Group::IsValidID(group_id) && ::Group::Get(group_id)->owner == _current_company; + const Group *g = ::Group::GetIfValid(group_id); + return g != NULL && g->owner == _current_company; } /* static */ AIGroup::GroupID AIGroup::CreateGroup(AIVehicle::VehicleType vehicle_type) diff --git a/src/ai/api/ai_sign.cpp b/src/ai/api/ai_sign.cpp index 349bf42a1e..63bce8fdf4 100644 --- a/src/ai/api/ai_sign.cpp +++ b/src/ai/api/ai_sign.cpp @@ -20,7 +20,8 @@ /* static */ bool AISign::IsValidSign(SignID sign_id) { - return ::Sign::IsValidID(sign_id) && ::Sign::Get(sign_id)->owner == _current_company; + const Sign *si = ::Sign::GetIfValid(sign_id); + return si != NULL && si->owner == _current_company; } /* static */ bool AISign::SetName(SignID sign_id, const char *name) diff --git a/src/ai/api/ai_station.cpp b/src/ai/api/ai_station.cpp index cc1167ddc0..5d73b147b8 100644 --- a/src/ai/api/ai_station.cpp +++ b/src/ai/api/ai_station.cpp @@ -17,7 +17,8 @@ /* static */ bool AIStation::IsValidStation(StationID station_id) { - return ::Station::IsValidID(station_id) && ::Station::Get(station_id)->owner == _current_company; + const Station *st = ::Station::GetIfValid(station_id); + return st != NULL && st->owner == _current_company; } /* static */ StationID AIStation::GetStationID(TileIndex tile) diff --git a/src/ai/api/ai_vehicle.cpp b/src/ai/api/ai_vehicle.cpp index 5d1579364c..3e30601fed 100644 --- a/src/ai/api/ai_vehicle.cpp +++ b/src/ai/api/ai_vehicle.cpp @@ -19,9 +19,8 @@ /* static */ bool AIVehicle::IsValidVehicle(VehicleID vehicle_id) { - if (!::Vehicle::IsValidID(vehicle_id)) return false; - const Vehicle *v = ::Vehicle::Get(vehicle_id); - return v->owner == _current_company && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::IsFreeWagon(v))); + const Vehicle *v = ::Vehicle::GetIfValid(vehicle_id); + return v != NULL && v->owner == _current_company && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::IsFreeWagon(v))); } /* static */ int32 AIVehicle::GetNumWagons(VehicleID vehicle_id) diff --git a/src/ai/api/ai_waypoint.cpp b/src/ai/api/ai_waypoint.cpp index 0c1e3194b1..b98274b638 100644 --- a/src/ai/api/ai_waypoint.cpp +++ b/src/ai/api/ai_waypoint.cpp @@ -14,7 +14,8 @@ /* static */ bool AIWaypoint::IsValidWaypoint(WaypointID waypoint_id) { - return ::Waypoint::IsValidID(waypoint_id) && ::Waypoint::Get(waypoint_id)->owner == _current_company; + const Waypoint *wp = ::Waypoint::GetIfValid(waypoint_id); + return wp != NULL && wp->owner == _current_company; } /* static */ WaypointID AIWaypoint::GetWaypointID(TileIndex tile) diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 7ef62bb946..d3314d34f2 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -440,9 +440,8 @@ CommandCost CmdBuildAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uin */ CommandCost CmdSellAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED); @@ -494,9 +493,8 @@ CommandCost CmdSendAircraftToHangar(TileIndex tile, DoCommandFlag flags, uint32 return SendAllVehiclesToDepot(VEH_AIRCRAFT, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1); } - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; if (v->type != VEH_AIRCRAFT) return CMD_ERROR; @@ -518,9 +516,8 @@ CommandCost CmdRefitAircraft(TileIndex tile, DoCommandFlag flags, uint32 p1, uin { byte new_subtype = GB(p2, 8, 8); - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_AIRCRAFT_MUST_BE_STOPPED); @@ -889,8 +886,8 @@ static byte AircraftGetEntryPoint(const Vehicle *v, const AirportFTAClass *apc) * or it will simply crash in next tick */ TileIndex tile = 0; - if (Station::IsValidID(v->u.air.targetairport)) { - const Station *st = Station::Get(v->u.air.targetairport); + const Station *st = Station::GetIfValid(v->u.air.targetairport); + if (st != NULL) { /* Make sure we don't go to INVALID_TILE if the airport has been removed. */ tile = (st->airport_tile != INVALID_TILE) ? st->airport_tile : st->xy; } @@ -921,7 +918,7 @@ static bool AircraftController(Vehicle *v) int count; /* NULL if station is invalid */ - const Station *st = Station::IsValidID(v->u.air.targetairport) ? Station::Get(v->u.air.targetairport) : NULL; + const Station *st = Station::GetIfValid(v->u.air.targetairport); /* INVALID_TILE if there is no station */ TileIndex tile = INVALID_TILE; if (st != NULL) { @@ -2049,11 +2046,8 @@ Station *GetTargetAirportIfValid(const Vehicle *v) { assert(v->type == VEH_AIRCRAFT); - StationID sid = v->u.air.targetairport; - - if (!Station::IsValidID(sid)) return NULL; - - Station *st = Station::Get(sid); + Station *st = Station::GetIfValid(v->u.air.targetairport); + if (st == NULL) return NULL; return st->airport_tile == INVALID_TILE ? NULL : st; } diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index 076cc7187b..066f6bac70 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -608,8 +608,9 @@ CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0); bool nothing_to_do = true; - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - Vehicle *v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; + if (!CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsInDepot()) return CMD_ERROR; if (HASBITS(v->vehstatus, VS_CRASHED)) return CMD_ERROR; diff --git a/src/command.cpp b/src/command.cpp index b80a4e8b13..2ad8e14169 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -590,8 +590,9 @@ bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallbac DEBUG(desync, 1, "cmd: %08x; %08x; %1x; %06x; %08x; %08x; %04x; %s\n", _date, _date_fract, (int)_current_company, tile, p1, p2, cmd & ~CMD_NETWORK_COMMAND, text); /* update last build coordinate of company. */ - if (tile != 0 && Company::IsValidID(_current_company)) { - Company::Get(_current_company)->last_build_coordinate = tile; + if (tile != 0) { + Company *c = Company::GetIfValid(_current_company); + if (c != NULL) c->last_build_coordinate = tile; } /* Actually try and execute the command. If no cost-type is given diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index e63b25a6b9..8659a82d25 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -76,8 +76,8 @@ void SetLocalCompany(CompanyID new_company) _local_company = new_company; /* Do not update the settings if we are in the intro GUI */ - if (Company::IsValidID(new_company) && _game_mode != GM_MENU) { - const Company *c = Company::Get(new_company); + const Company *c = Company::GetIfValid(new_company); + if (_game_mode != GM_MENU && c != NULL) { _settings_client.company = c->settings; InvalidateWindow(WC_GAME_OPTIONS, 0); } @@ -150,8 +150,8 @@ void InvalidateCompanyWindows(const Company *company) bool CheckCompanyHasMoney(CommandCost cost) { if (cost.GetCost() > 0) { - CompanyID company = _current_company; - if (Company::IsValidID(company) && cost.GetCost() > Company::Get(company)->money) { + const Company *c = Company::GetIfValid(_current_company); + if (c != NULL && cost.GetCost() > c->money) { SetDParam(0, cost.GetCost()); _error_message = STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY; return false; @@ -187,9 +187,8 @@ static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost) void SubtractMoneyFromCompany(CommandCost cost) { - CompanyID cid = _current_company; - - if (Company::IsValidID(cid)) SubtractMoneyFromAnyCompany(Company::Get(cid), cost); + Company *c = Company::GetIfValid(_current_company); + if (c != NULL) SubtractMoneyFromAnyCompany(c, cost); } void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cst) @@ -499,9 +498,9 @@ void OnTick_Companies() { if (_game_mode == GM_EDITOR) return; - if (Company::IsValidID((CompanyID)_cur_company_tick_index)) { - Company *c = Company::Get((CompanyID)_cur_company_tick_index); - if (c->name_1 != 0) GenerateCompanyName(c); + Company *c = Company::GetIfValid(_cur_company_tick_index); + if (c != NULL && c->name_1 != 0) { + GenerateCompanyName(c); } if (_next_competitor_start == 0) { @@ -567,9 +566,9 @@ void CompaniesYearlyLoop() */ CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Company::IsValidID(_current_company)) return CMD_ERROR; + Company *c = Company::GetIfValid(_current_company); + if (c == NULL) return CMD_ERROR; - Company *c = Company::Get(_current_company); switch (GB(p1, 0, 3)) { case 0: if (c->settings.engine_renew == HasBit(p2, 0)) return CMD_ERROR; @@ -817,14 +816,11 @@ CommandCost CmdCompanyCtrl(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 break; case 2: { // Delete a company - Company *c; - - if (!Company::IsValidID((CompanyID)p2)) return CMD_ERROR; + Company *c = Company::GetIfValid(p2); + if (c == NULL) return CMD_ERROR; if (!(flags & DC_EXEC)) return CommandCost(); - c = Company::Get((CompanyID)p2); - /* Delete any open window of the company */ DeleteCompanyWindows(c->index); CompanyNewsInformation *cni = MallocT(1); diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index cde830aa0e..66fa867e94 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -716,8 +716,6 @@ DEF_CONSOLE_CMD(ConMoveClient) DEF_CONSOLE_CMD(ConResetCompany) { - CompanyID index; - if (argc == 0) { IConsoleHelp("Remove an idle company from the game. Usage: 'reset_company '"); IConsoleHelp("For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc."); @@ -726,16 +724,15 @@ DEF_CONSOLE_CMD(ConResetCompany) if (argc != 2) return false; - index = (CompanyID)(atoi(argv[1]) - 1); + CompanyID index = (CompanyID)(atoi(argv[1]) - 1); + const Company *c = Company::GetIfValid(index); /* Check valid range */ - if (!Company::IsValidID(index)) { + if (c == NULL) { IConsolePrintF(CC_ERROR, "Company does not exist. Company-id must be between 1 and %d.", MAX_COMPANIES); return true; } - const Company *c = Company::Get(index); - if (c->is_ai) { IConsoleError("Company is owned by an AI."); return true; diff --git a/src/economy.cpp b/src/economy.cpp index 102333dd32..6d73d0d44a 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1889,11 +1889,11 @@ CommandCost CmdBuyShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, { CommandCost cost(EXPENSES_OTHER); + Company *c = Company::GetIfValid(p1); + /* Check if buying shares is allowed (protection against modified clients) * Cannot buy own shares */ - if (!Company::IsValidID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR; - - Company *c = Company::Get((CompanyID)p1); + if (c == NULL || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR; /* Protect new companies from hostile takeovers */ if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_PROTECTED); @@ -1932,11 +1932,11 @@ CommandCost CmdBuyShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, */ CommandCost CmdSellShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { + Company *c = Company::GetIfValid(p1); + /* Check if selling shares is allowed (protection against modified clients) * Cannot sell own shares */ - if (!Company::IsValidID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR; - - Company *c = Company::Get((CompanyID)p1); + if (c == NULL || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR; /* Those lines are here for network-protection (clients can be slow) */ if (GetAmountOwnedBy(c, _current_company) == 0) return CommandCost(); @@ -1965,15 +1965,13 @@ CommandCost CmdSellShareInCompany(TileIndex tile, DoCommandFlag flags, uint32 p1 */ CommandCost CmdBuyCompany(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - CompanyID cid = (CompanyID)p1; + Company *c = Company::GetIfValid(p1); /* Disable takeovers in multiplayer games */ - if (!Company::IsValidID(cid) || _networking) return CMD_ERROR; + if (c == NULL || _networking) return CMD_ERROR; /* Do not allow companies to take over themselves */ - if (cid == _current_company) return CMD_ERROR; - - Company *c = Company::Get(cid); + if ((CompanyID)p1 == _current_company) return CMD_ERROR; if (!c->is_ai) return CMD_ERROR; diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp index 100254b22d..30ef53f56f 100644 --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -425,8 +425,8 @@ public: int numd = 0; for (CompanyID k = COMPANY_FIRST; k < MAX_COMPANIES; k++) { - if (Company::IsValidID(k)) { - c = Company::Get(k); + c = Company::GetIfValid(k); + if (c != NULL) { this->colours[numd] = _colour_gradient[c->colour][6]; for (int j = this->num_on_x_axis, i = 0; --j >= 0;) { this->cost[numd][i] = (j >= c->num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j); diff --git a/src/group.h b/src/group.h index 445e3a66fc..b09da0a171 100644 --- a/src/group.h +++ b/src/group.h @@ -72,12 +72,14 @@ uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e); static inline void IncreaseGroupNumVehicle(GroupID id_g) { - if (Group::IsValidID(id_g)) Group::Get(id_g)->num_vehicle++; + Group *g = Group::GetIfValid(id_g); + if (g != NULL) g->num_vehicle++; } static inline void DecreaseGroupNumVehicle(GroupID id_g) { - if (Group::IsValidID(id_g)) Group::Get(id_g)->num_vehicle--; + Group *g = Group::GetIfValid(id_g); + if (g != NULL) g->num_vehicle--; } diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index aa84541718..2b56667f83 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -105,10 +105,8 @@ CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 */ CommandCost CmdDeleteGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Group::IsValidID(p1)) return CMD_ERROR; - - Group *g = Group::Get(p1); - if (g->owner != _current_company) return CMD_ERROR; + Group *g = Group::GetIfValid(p1); + if (g == NULL || g->owner != _current_company) return CMD_ERROR; if (flags & DC_EXEC) { Vehicle *v; @@ -164,10 +162,8 @@ static bool IsUniqueGroupName(const char *name) */ CommandCost CmdRenameGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Group::IsValidID(p1)) return CMD_ERROR; - - Group *g = Group::Get(p1); - if (g->owner != _current_company) return CMD_ERROR; + Group *g = Group::GetIfValid(p1); + if (g == NULL || g->owner != _current_company) return CMD_ERROR; bool reset = StrEmpty(text); @@ -199,11 +195,10 @@ CommandCost CmdRenameGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 */ CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { + Vehicle *v = Vehicle::GetIfValid(p2); GroupID new_g = p1; - if (!Vehicle::IsValidID(p2) || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g))) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p2); + if (v == NULL || (!Group::IsValidID(new_g) && !IsDefaultGroupID(new_g))) return CMD_ERROR; if (Group::IsValidID(new_g)) { Group *g = Group::Get(new_g); @@ -283,11 +278,10 @@ CommandCost CmdAddSharedVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 */ CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { + Group *g = Group::GetIfValid(p1); VehicleType type = (VehicleType)p2; - if (!Group::IsValidID(p1) || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR; - Group *g = Group::Get(p1); - if (g->owner != _current_company) return CMD_ERROR; + if (g == NULL || g->owner != _current_company || !IsCompanyBuildableVehicleType(type)) return CMD_ERROR; if (flags & DC_EXEC) { GroupID old_g = p1; @@ -320,10 +314,8 @@ CommandCost CmdRemoveAllVehiclesGroup(TileIndex tile, DoCommandFlag flags, uint3 */ CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Group::IsValidID(p1)) return CMD_ERROR; - - Group *g = Group::Get(p1); - if (g->owner != _current_company) return CMD_ERROR; + Group *g = Group::GetIfValid(p1); + if (g == NULL || g->owner != _current_company) return CMD_ERROR; if (flags & DC_EXEC) { g->replace_protection = HasBit(p2, 0); diff --git a/src/group_gui.cpp b/src/group_gui.cpp index ca49ff3e50..2480255634 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -594,13 +594,13 @@ public: break; } - case GRP_WIDGET_REPLACE_PROTECTION: - if (Group::IsValidID(this->group_sel)) { - const Group *g = Group::Get(this->group_sel); - + case GRP_WIDGET_REPLACE_PROTECTION: { + const Group *g = Group::GetIfValid(this->group_sel); + if (g != NULL) { DoCommandP(0, this->group_sel, !g->replace_protection, CMD_SET_GROUP_REPLACE_PROTECTION); } break; + } } } diff --git a/src/highscore_gui.cpp b/src/highscore_gui.cpp index 920364079e..8d33a9b136 100644 --- a/src/highscore_gui.cpp +++ b/src/highscore_gui.cpp @@ -88,14 +88,13 @@ struct EndGameWindow : EndGameHighScoreBaseWindow { virtual void OnPaint() { - const Company *c; uint x, y; this->SetupHighScoreEndWindow(&x, &y); - if (!Company::IsValidID(_local_company)) return; + const Company *c = Company::GetIfValid(_local_company); + if (c == NULL) return; - c = Company::Get(_local_company); /* We need to get performance from last year because the image is shown * at the start of the new year when these things have already been copied */ if (this->background_img == SPR_TYCOON_IMG2_BEGIN) { // Tycoon of the century \o/ diff --git a/src/network/network.cpp b/src/network/network.cpp index c661e1a980..f2d5e497ae 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -97,7 +97,7 @@ extern void StateGameLoop(); */ NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientIndex index) { - return NetworkClientInfo::IsValidID(index) ? NetworkClientInfo::Get(index) : NULL; + return NetworkClientInfo::GetIfValid(index); } /** diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index 4b54a2e93d..46cd2c1058 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -214,8 +214,8 @@ uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte par byte colours; bool is_ai = false; - if (Company::IsValidID(industry->founder)) { - const Company *c = Company::Get(industry->founder); + const Company *c = Company::GetIfValid(industry->founder); + if (c != NULL) { const Livery *l = &c->livery[LS_DEFAULT]; is_ai = c->is_ai; diff --git a/src/oldpool.h b/src/oldpool.h index a21d5fb0aa..ab496d2c3f 100644 --- a/src/oldpool.h +++ b/src/oldpool.h @@ -286,6 +286,18 @@ struct PoolItem { return Tpool->Get(index); } + /** + * Get item with given index + * @param index item to get + * @return NULL for invalid items + */ + static FORCEINLINE T *GetIfValid(uint index) + { + if (index >= Tpool->GetSize()) return NULL; + T *item = Tpool->Get(index); + return item->IsValid() ? item : NULL; + } + /** * Returns size of the pool (in number of items) * @return size of the pool diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 6adb8ca47b..230137efa9 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -411,24 +411,19 @@ static uint GetOrderDistance(const Order *prev, const Order *cur, const Vehicle */ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; VehicleID veh = GB(p1, 0, 16); VehicleOrderID sel_ord = GB(p1, 16, 16); Order new_order(p2); - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - - v = Vehicle::Get(veh); - - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; /* Check if the inserted order is to the correct destination (owner, type), * and has the correct flags if any */ switch (new_order.GetType()) { case OT_GOTO_STATION: { - if (!Station::IsValidID(new_order.GetDestination())) return CMD_ERROR; - - const Station *st = Station::Get(new_order.GetDestination()); + 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; @@ -472,21 +467,17 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 case OT_GOTO_DEPOT: { if (new_order.GetDepotActionType() != ODATFB_NEAREST_DEPOT) { if (v->type == VEH_AIRCRAFT) { - if (!Station::IsValidID(new_order.GetDestination())) return CMD_ERROR; + const Station *st = Station::GetIfValid(new_order.GetDestination()); - const Station *st = Station::Get(new_order.GetDestination()); - - if (!CheckOwnership(st->owner) || + if (st == NULL || !CheckOwnership(st->owner) || !CanVehicleUseStation(v, st) || st->Airport()->nof_depots == 0) { return CMD_ERROR; } } else { - if (!Depot::IsValidID(new_order.GetDestination())) return CMD_ERROR; - - const Depot *dp = Depot::Get(new_order.GetDestination()); + const Depot *dp = Depot::GetIfValid(new_order.GetDestination()); - if (!CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR; + if (dp == NULL || !CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR; switch (v->type) { case VEH_TRAIN: @@ -518,10 +509,8 @@ CommandCost CmdInsertOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 case OT_GOTO_WAYPOINT: { if (v->type != VEH_TRAIN) return CMD_ERROR; - if (!Waypoint::IsValidID(new_order.GetDestination())) return CMD_ERROR; - const Waypoint *wp = Waypoint::Get(new_order.GetDestination()); - - if (!CheckOwnership(wp->owner)) return CMD_ERROR; + const Waypoint *wp = Waypoint::GetIfValid(new_order.GetDestination()); + if (wp == NULL || !CheckOwnership(wp->owner)) return CMD_ERROR; /* Order flags can be any of the following for waypoints: * [non-stop] @@ -660,16 +649,13 @@ static CommandCost DecloneOrder(Vehicle *dst, DoCommandFlag flags) */ CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; VehicleID veh_id = p1; VehicleOrderID sel_ord = p2; Order *order; - if (!Vehicle::IsValidID(veh_id)) return CMD_ERROR; - - v = Vehicle::Get(veh_id); + Vehicle *v = Vehicle::GetIfValid(veh_id); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; /* If we did not select an order, we maybe want to de-clone the orders */ if (sel_ord >= v->GetNumOrders()) @@ -728,16 +714,15 @@ CommandCost CmdDeleteOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 */ CommandCost CmdSkipToOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; VehicleID veh_id = p1; VehicleOrderID sel_ord = p2; - if (!Vehicle::IsValidID(veh_id)) return CMD_ERROR; - - v = Vehicle::Get(veh_id); + Vehicle *v = Vehicle::GetIfValid(veh_id); - if (!CheckOwnership(v->owner) || sel_ord == v->cur_order_index || - sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) return CMD_ERROR; + if (v == NULL || !CheckOwnership(v->owner) || sel_ord == v->cur_order_index || + sel_ord >= v->GetNumOrders() || v->GetNumOrders() < 2) { + return CMD_ERROR; + } if (flags & DC_EXEC) { v->cur_order_index = sel_ord; @@ -772,10 +757,8 @@ CommandCost CmdMoveOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 VehicleOrderID moving_order = GB(p2, 0, 16); VehicleOrderID target_order = GB(p2, 16, 16); - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(veh); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; /* Don't make senseless movements */ if (moving_order >= v->GetNumOrders() || target_order >= v->GetNumOrders() || @@ -852,10 +835,9 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 uint16 data = GB(p2, 4, 11); if (mof >= MOF_END) return CMD_ERROR; - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - Vehicle *v = Vehicle::Get(veh); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; /* Is it a valid order? */ if (sel_ord >= v->GetNumOrders()) return CMD_ERROR; @@ -1072,27 +1054,21 @@ CommandCost CmdModifyOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 */ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *dst; VehicleID veh_src = GB(p1, 16, 16); VehicleID veh_dst = GB(p1, 0, 16); - if (!Vehicle::IsValidID(veh_dst)) return CMD_ERROR; + Vehicle *dst = Vehicle::GetIfValid(veh_dst); - dst = Vehicle::Get(veh_dst); - - if (!CheckOwnership(dst->owner)) return CMD_ERROR; + if (dst == NULL || !CheckOwnership(dst->owner)) return CMD_ERROR; switch (p2) { case CO_SHARE: { - Vehicle *src; - - if (!Vehicle::IsValidID(veh_src)) return CMD_ERROR; - - src = Vehicle::Get(veh_src); + Vehicle *src = Vehicle::GetIfValid(veh_src); /* Sanity checks */ - if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src) + if (src == NULL || !CheckOwnership(src->owner) || dst->type != src->type || dst == src) { return CMD_ERROR; + } /* Trucks can't share orders with busses (and visa versa) */ if (src->type == VEH_ROAD) { @@ -1129,16 +1105,12 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } break; case CO_COPY: { - Vehicle *src; - int delta; - - if (!Vehicle::IsValidID(veh_src)) return CMD_ERROR; - - src = Vehicle::Get(veh_src); + Vehicle *src = Vehicle::GetIfValid(veh_src); /* Sanity checks */ - if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src) + if (src == NULL || !CheckOwnership(src->owner) || dst->type != src->type || dst == src) { return CMD_ERROR; + } /* Trucks can't copy all the orders from busses (and visa versa), * and neither can helicopters and aircarft. */ @@ -1151,7 +1123,7 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } /* make sure there are orders available */ - delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders(); + int delta = dst->IsOrderListShared() ? src->GetNumOrders() + 1 : src->GetNumOrders() - dst->GetNumOrders(); if (!Order::CanAllocateItem(delta) || ((dst->orders.list == NULL || dst->IsOrderListShared()) && !OrderList::CanAllocateItem())) { return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS); @@ -1201,20 +1173,15 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 */ CommandCost CmdOrderRefit(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - const Vehicle *v; - Order *order; VehicleID veh = GB(p1, 0, 16); VehicleOrderID order_number = GB(p2, 16, 8); CargoID cargo = GB(p2, 0, 8); byte subtype = GB(p2, 8, 8); - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - - v = Vehicle::Get(veh); - - if (!CheckOwnership(v->owner)) return CMD_ERROR; + const Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; - order = GetVehicleOrder(v, order_number); + Order *order = GetVehicleOrder(v, order_number); if (order == NULL) return CMD_ERROR; if (flags & DC_EXEC) { @@ -1357,16 +1324,12 @@ void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *bak) */ CommandCost CmdRestoreOrderIndex(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; VehicleOrderID cur_ord = GB(p2, 0, 16); uint16 serv_int = GB(p2, 16, 16); - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - v = Vehicle::Get(p1); - + Vehicle *v = Vehicle::GetIfValid(p1); /* Check the vehicle type and ownership, and if the service interval and order are in range */ - if (!CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (serv_int != GetServiceIntervalClamped(serv_int) || cur_ord >= v->GetNumOrders()) return CMD_ERROR; if (flags & DC_EXEC) { diff --git a/src/road.cpp b/src/road.cpp index 26b3d75f8b..9650ed91c2 100644 --- a/src/road.cpp +++ b/src/road.cpp @@ -84,8 +84,9 @@ bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts) if (company == OWNER_TOWN || _game_mode == GM_EDITOR || IsGeneratingWorld()) { avail_roadtypes = ROADTYPES_ROAD; } else { - if (!Company::IsValidID(company)) return false; - avail_roadtypes = (RoadTypes)Company::Get(company)->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody + Company *c = Company::GetIfValid(company); + if (c == NULL) return false; + avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody } return (rts & ~avail_roadtypes) == 0; } diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index c3d8d58c93..f76f8428ac 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -312,13 +312,8 @@ bool RoadVehicle::IsStoppedInDepot() const */ CommandCost CmdSellRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; - - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - v = Vehicle::Get(p1); - - if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE); @@ -423,11 +418,8 @@ CommandCost CmdSendRoadVehToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1 return SendAllVehiclesToDepot(VEH_ROAD, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1); } - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_ROAD) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_ROAD) return CMD_ERROR; return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK)); } @@ -440,13 +432,8 @@ CommandCost CmdSendRoadVehToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1 */ CommandCost CmdTurnRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; - - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - v = Vehicle::Get(p1); - - if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; if (v->vehstatus & VS_STOPPED || v->vehstatus & VS_CRASHED || @@ -1968,7 +1955,6 @@ void RoadVehicle::OnNewDay() */ CommandCost CmdRefitRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; CommandCost cost(EXPENSES_ROADVEH_RUN); CargoID new_cid = GB(p2, 0, 8); byte new_subtype = GB(p2, 8, 8); @@ -1976,11 +1962,9 @@ CommandCost CmdRefitRoadVeh(TileIndex tile, DoCommandFlag flags, uint32 p1, uint uint16 capacity = CALLBACK_FAILED; uint total_capacity = 0; - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); - if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL || v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR; if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_ROAD_MUST_BE_STOPPED_INSIDE_DEPOT); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 0a86b701df..e350b343e1 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -698,8 +698,8 @@ bool AfterLoadGame() * becomes company 0, unless we are in the scenario editor where all the * companies are 'invalid'. */ - if (!_network_dedicated && Company::IsValidID(COMPANY_FIRST)) { - c = Company::Get(COMPANY_FIRST); + c = Company::GetIfValid(COMPANY_FIRST); + if (!_network_dedicated && c != NULL) { c->settings = _settings_client.company; } } diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 6903859bae..17b56965d3 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -819,13 +819,8 @@ CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 */ CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; - - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - v = Vehicle::Get(p1); - - if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR; if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE); @@ -870,11 +865,8 @@ CommandCost CmdSendShipToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, u return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1); } - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_SHIP) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_SHIP) return CMD_ERROR; return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK)); } @@ -892,17 +884,14 @@ CommandCost CmdSendShipToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, u */ CommandCost CmdRefitShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - Vehicle *v; CommandCost cost(EXPENSES_SHIP_RUN); CargoID new_cid = GB(p2, 0, 8); // gets the cargo number byte new_subtype = GB(p2, 8, 8); uint16 capacity = CALLBACK_FAILED; - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); - if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR; + if (v == NULL || v->type != VEH_SHIP || !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_CAN_T_REFIT_DESTROYED_VEHICLE); diff --git a/src/signs_cmd.cpp b/src/signs_cmd.cpp index a44ca22419..cb8add163c 100644 --- a/src/signs_cmd.cpp +++ b/src/signs_cmd.cpp @@ -66,15 +66,14 @@ CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 */ CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Sign::IsValidID(p1)) return CMD_ERROR; + Sign *si = Sign::GetIfValid(p1); + if (si == NULL) return CMD_ERROR; /* Rename the signs when empty, otherwise remove it */ if (!StrEmpty(text)) { if (strlen(text) >= MAX_LENGTH_SIGN_NAME_BYTES) return CMD_ERROR; if (flags & DC_EXEC) { - Sign *si = Sign::Get(p1); - /* Delete the old name */ free(si->name); /* Assign the new one */ @@ -89,8 +88,6 @@ CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } } else { // Delete sign if (flags & DC_EXEC) { - Sign *si = Sign::Get(p1); - MarkSignDirty(si); delete si; diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index e3317fd43b..4334dfe8c7 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -2911,10 +2911,8 @@ static bool IsUniqueStationName(const char *name) */ CommandCost CmdRenameStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Station::IsValidID(p1)) return CMD_ERROR; - - Station *st = Station::Get(p1); - if (!CheckOwnership(st->owner)) return CMD_ERROR; + Station *st = Station::GetIfValid(p1); + if (st == NULL || !CheckOwnership(st->owner)) return CMD_ERROR; bool reset = StrEmpty(text); diff --git a/src/strings.cpp b/src/strings.cpp index 00aee98a25..2e04113b5a 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -846,8 +846,9 @@ static char *FormatString(char *buff, const char *str, const int64 *argv, uint c case SCC_STATION_NAME: { // {STATION} StationID sid = GetInt32(&argv); + const Station *st = Station::GetIfValid(sid); - if (!Station::IsValidID(sid)) { + if (st == NULL) { /* The station doesn't exist anymore. The only place where we might * be "drawing" an invalid station is in the case of cargo that is * in transit. */ @@ -855,7 +856,6 @@ static char *FormatString(char *buff, const char *str, const int64 *argv, uint c break; } - const Station *st = Station::Get(sid); if (st->name != NULL) { buff = strecpy(buff, st->name, last); } else { diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index 1897d46eb6..d730529169 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -57,10 +57,9 @@ CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, u if (!_settings_game.order.timetabling) return CMD_ERROR; VehicleID veh = GB(p1, 0, 16); - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - Vehicle *v = Vehicle::Get(veh); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; VehicleOrderID order_number = GB(p1, 16, 8); Order *order = GetVehicleOrder(v, order_number); @@ -115,10 +114,9 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, if (!_settings_game.order.timetabling) return CMD_ERROR; VehicleID veh = GB(p1, 0, 16); - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - Vehicle *v = Vehicle::Get(veh); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (flags & DC_EXEC) { v->lateness_counter = 0; @@ -143,10 +141,9 @@ CommandCost CmdAutofillTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, if (!_settings_game.order.timetabling) return CMD_ERROR; VehicleID veh = GB(p1, 0, 16); - if (!Vehicle::IsValidID(veh)) return CMD_ERROR; - Vehicle *v = Vehicle::Get(veh); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(veh); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (flags & DC_EXEC) { if (HasBit(p2, 0)) { diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 76380ded39..2f5efb70d3 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2273,7 +2273,8 @@ static bool IsUniqueTownName(const char *name) */ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Town::IsValidID(p1)) return CMD_ERROR; + Town *t = Town::GetIfValid(p1); + if (t == NULL) return CMD_ERROR; bool reset = StrEmpty(text); @@ -2283,8 +2284,6 @@ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 } if (flags & DC_EXEC) { - Town *t = Town::Get(p1); - free(t->name); t->name = reset ? NULL : strdup(text); @@ -2548,9 +2547,8 @@ uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t) */ CommandCost CmdDoTownAction(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Town::IsValidID(p1) || p2 >= lengthof(_town_action_proc)) return CMD_ERROR; - - Town *t = Town::Get(p1); + Town *t = Town::GetIfValid(p1); + if (t == NULL || p2 >= lengthof(_town_action_proc)) return CMD_ERROR; if (!HasBit(GetMaskOfTownActions(NULL, _current_company, t), p2)) return CMD_ERROR; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 8c83c1bb81..a0cc69e1a7 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1040,11 +1040,8 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u VehicleID s = GB(p1, 0, 16); VehicleID d = GB(p1, 16, 16); - if (!Vehicle::IsValidID(s)) return CMD_ERROR; - - Vehicle *src = Vehicle::Get(s); - - if (src->type != VEH_TRAIN || !CheckOwnership(src->owner)) return CMD_ERROR; + Vehicle *src = Vehicle::GetIfValid(s); + if (src == NULL || src->type != VEH_TRAIN || !CheckOwnership(src->owner)) return CMD_ERROR; /* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */ if (HASBITS(src->vehstatus, VS_CRASHED)) return CMD_ERROR; @@ -1054,9 +1051,8 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u if (d == INVALID_VEHICLE) { dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src); } else { - if (!Vehicle::IsValidID(d)) return CMD_ERROR; - dst = Vehicle::Get(d); - if (dst->type != VEH_TRAIN || !CheckOwnership(dst->owner)) return CMD_ERROR; + dst = Vehicle::GetIfValid(d); + if (dst == NULL || dst->type != VEH_TRAIN || !CheckOwnership(dst->owner)) return CMD_ERROR; /* Do not allow appending to crashed vehicles, too */ if (HASBITS(dst->vehstatus, VS_CRASHED)) return CMD_ERROR; @@ -1395,11 +1391,9 @@ CommandCost CmdSellRailWagon(TileIndex tile, DoCommandFlag flags, uint32 p1, uin /* Check if we deleted a vehicle window */ Window *w = NULL; - if (!Vehicle::IsValidID(p1) || p2 > 1) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; + if (p2 > 1) return CMD_ERROR; if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE); @@ -1952,11 +1946,8 @@ static void ReverseTrainDirection(Vehicle *v) */ CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; if (p2 != 0) { /* turn a single unit around */ @@ -2013,11 +2004,8 @@ CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 */ CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; if (flags & DC_EXEC) v->u.rail.force_proceed = 0x50; @@ -2040,11 +2028,8 @@ CommandCost CmdRefitRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, byte new_subtype = GB(p2, 8, 8); bool only_this = HasBit(p2, 16); - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED); if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE); @@ -2246,11 +2231,8 @@ CommandCost CmdSendTrainToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1); } - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (v->type != VEH_TRAIN) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || v->type != VEH_TRAIN) return CMD_ERROR; return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK)); } diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index ac3ff0005f..489e5a8ed4 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -440,11 +440,11 @@ not_valid_below:; * It's unnecessary to execute this command every time for every bridge. So it is done only * and cost is computed in "bridge_gui.c". For AI, Towns this has to be of course calculated */ - if (!(flags & DC_QUERY_COST) || (Company::IsValidID(_current_company) && Company::Get(_current_company)->is_ai)) { + Company *c = Company::GetIfValid(_current_company); + if (!(flags & DC_QUERY_COST) || (c != NULL && c->is_ai)) { bridge_len += 2; // begin and end tiles/ramps - if (Company::IsValidID(_current_company)) - bridge_len = CalcBridgeLenCostFactor(bridge_len); + if (c != NULL) bridge_len = CalcBridgeLenCostFactor(bridge_len); cost.AddCost((int64)bridge_len * _price.build_bridge * GetBridgeSpec(bridge_type)->price >> 8); diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 72e1df6d26..5a78c747cd 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -61,12 +61,8 @@ CommandCost CmdStartStopVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, /* Disable the effect of p2 bit 0, when DC_AUTOREPLACE is not set */ if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0); - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (!CheckOwnership(v->owner)) return CMD_ERROR; - if (!v->IsPrimaryVehicle()) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; switch (v->type) { case VEH_TRAIN: @@ -334,9 +330,8 @@ CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint CommandCost total_cost(EXPENSES_NEW_VEHICLES); uint32 build_argument = 2; - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL) return CMD_ERROR; Vehicle *v_front = v; Vehicle *w = NULL; Vehicle *w_front = NULL; @@ -532,10 +527,8 @@ CommandCost SendAllVehiclesToDepot(VehicleType type, DoCommandFlag flags, bool s */ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - if (!CheckOwnership(v->owner)) return CMD_ERROR; + Vehicle *v = Vehicle::GetIfValid(p1); + if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; bool reset = StrEmpty(text); @@ -564,12 +557,9 @@ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uin CommandCost CmdChangeServiceInt(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { uint16 serv_int = GetServiceIntervalClamped(p2); // Double check the service interval from the user-input + Vehicle *v = Vehicle::GetIfValid(p1); - if (serv_int != p2 || !Vehicle::IsValidID(p1)) return CMD_ERROR; - - Vehicle *v = Vehicle::Get(p1); - - if (!CheckOwnership(v->owner)) return CMD_ERROR; + if (serv_int != p2 || v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR; if (flags & DC_EXEC) { v->service_interval = serv_int; diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index 3018353612..468d216cb0 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -314,10 +314,8 @@ static bool IsUniqueWaypointName(const char *name) */ CommandCost CmdRenameWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { - if (!Waypoint::IsValidID(p1)) return CMD_ERROR; - - Waypoint *wp = Waypoint::Get(p1); - if (!CheckOwnership(wp->owner)) return CMD_ERROR; + Waypoint *wp = Waypoint::GetIfValid(p1); + if (wp == NULL || !CheckOwnership(wp->owner)) return CMD_ERROR; bool reset = StrEmpty(text);