changeset 11949:c89301974eb8 draft

(svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
author smatz <smatz@openttd.org>
date Mon, 18 May 2009 16:21:28 +0000
parents ad1ecf6b26e4
children 13ee49bccba3
files src/ai/ai_core.cpp src/ai/ai_gui.cpp src/ai/api/ai_group.cpp src/ai/api/ai_sign.cpp src/ai/api/ai_station.cpp src/ai/api/ai_vehicle.cpp src/ai/api/ai_waypoint.cpp src/aircraft_cmd.cpp src/autoreplace_cmd.cpp src/command.cpp src/company_cmd.cpp src/console_cmds.cpp src/economy.cpp src/graph_gui.cpp src/group.h src/group_cmd.cpp src/group_gui.cpp src/highscore_gui.cpp src/network/network.cpp src/newgrf_industries.cpp src/oldpool.h src/order_cmd.cpp src/road.cpp src/roadveh_cmd.cpp src/saveload/afterload.cpp src/ship_cmd.cpp src/signs_cmd.cpp src/station_cmd.cpp src/strings.cpp src/timetable_cmd.cpp src/town_cmd.cpp src/train_cmd.cpp src/tunnelbridge_cmd.cpp src/vehicle_cmd.cpp src/waypoint_cmd.cpp
diffstat 35 files changed, 201 insertions(+), 305 deletions(-) [+]
line wrap: on
line diff
--- 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 @@
 /* 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 @@
 /* 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 */
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -641,7 +641,8 @@
 	{
 		/* 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 @@
 			}
 
 			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 @@
 
 		/* 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 :( */
--- 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)
--- 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)
--- 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)
--- 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)
--- 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)
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -440,9 +440,8 @@
  */
 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 @@
 		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 @@
 {
 	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 @@
 	 * 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 @@
 	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 @@
 {
 	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;
 }
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -608,8 +608,9 @@
 	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;
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -590,8 +590,9 @@
 	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
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -76,8 +76,8 @@
 	_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 @@
 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 @@
 
 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 @@
 {
 	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 @@
  */
 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 @@
 			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<CompanyNewsInformation>(1);
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -716,8 +716,6 @@
 
 DEF_CONSOLE_CMD(ConResetCompany)
 {
-	CompanyID index;
-
 	if (argc == 0) {
 		IConsoleHelp("Remove an idle company from the game. Usage: 'reset_company <company-id>'");
 		IConsoleHelp("For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc.");
@@ -726,16 +724,15 @@
 
 	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;
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1889,11 +1889,11 @@
 {
 	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 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 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;
 
--- a/src/graph_gui.cpp
+++ b/src/graph_gui.cpp
@@ -425,8 +425,8 @@
 
 		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);
--- a/src/group.h
+++ b/src/group.h
@@ -72,12 +72,14 @@
 
 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--;
 }
 
 
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -105,10 +105,8 @@
  */
 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 @@
  */
 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 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 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 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);
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -594,13 +594,13 @@
 				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;
+			}
 		}
 	}
 
--- a/src/highscore_gui.cpp
+++ b/src/highscore_gui.cpp
@@ -88,14 +88,13 @@
 
 	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/
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -97,7 +97,7 @@
  */
 NetworkClientInfo *NetworkFindClientInfoFromIndex(ClientIndex index)
 {
-	return NetworkClientInfo::IsValidID(index) ? NetworkClientInfo::Get(index) : NULL;
+	return NetworkClientInfo::GetIfValid(index);
 }
 
 /**
--- a/src/newgrf_industries.cpp
+++ b/src/newgrf_industries.cpp
@@ -214,8 +214,8 @@
 			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;
--- a/src/oldpool.h
+++ b/src/oldpool.h
@@ -287,6 +287,18 @@
 	}
 
 	/**
+	 * 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
 	 */
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -411,24 +411,19 @@
  */
 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 @@
 		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::GetIfValid(new_order.GetDestination());
 
-					const Depot *dp = Depot::Get(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 @@
 		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 @@
  */
 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;
+	Vehicle *v = Vehicle::GetIfValid(veh_id);
 
-	v = Vehicle::Get(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 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;
+	Vehicle *v = Vehicle::GetIfValid(veh_id);
 
-	v = Vehicle::Get(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 @@
 	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 @@
 	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 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 @@
 		} 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 @@
 			}
 
 			/* 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 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);
+	const Vehicle *v = Vehicle::GetIfValid(veh);
+	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
-	if (!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 @@
  */
 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) {
--- a/src/road.cpp
+++ b/src/road.cpp
@@ -84,8 +84,9 @@
 	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;
 }
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -312,13 +312,8 @@
  */
 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 @@
 		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 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 @@
  */
 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 @@
 	uint16 capacity = CALLBACK_FAILED;
 	uint total_capacity = 0;
 
-	if (!Vehicle::IsValidID(p1)) return CMD_ERROR;
+	Vehicle *v = Vehicle::GetIfValid(p1);
 
-	v = Vehicle::Get(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);
 
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -698,8 +698,8 @@
 		 * 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;
 		}
 	}
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -819,13 +819,8 @@
  */
 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 @@
 		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 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;
+	Vehicle *v = Vehicle::GetIfValid(p1);
 
-	v = Vehicle::Get(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);
 
--- a/src/signs_cmd.cpp
+++ b/src/signs_cmd.cpp
@@ -66,15 +66,14 @@
  */
 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 @@
 		}
 	} else { // Delete sign
 		if (flags & DC_EXEC) {
-			Sign *si = Sign::Get(p1);
-
 			MarkSignDirty(si);
 			delete si;
 
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -2911,10 +2911,8 @@
  */
 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);
 
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -846,8 +846,9 @@
 
 			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 @@
 					break;
 				}
 
-				const Station *st = Station::Get(sid);
 				if (st->name != NULL) {
 					buff = strecpy(buff, st->name, last);
 				} else {
--- a/src/timetable_cmd.cpp
+++ b/src/timetable_cmd.cpp
@@ -57,10 +57,9 @@
 	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 @@
 	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 @@
 	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)) {
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -2273,7 +2273,8 @@
  */
 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 @@
 	}
 
 	if (flags & DC_EXEC) {
-		Town *t = Town::Get(p1);
-
 		free(t->name);
 		t->name = reset ? NULL : strdup(text);
 
@@ -2548,9 +2547,8 @@
  */
 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;
 
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1040,11 +1040,8 @@
 	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 @@
 	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 @@
 	/* 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 @@
  */
 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 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 @@
 	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 @@
 		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));
 }
--- a/src/tunnelbridge_cmd.cpp
+++ b/src/tunnelbridge_cmd.cpp
@@ -440,11 +440,11 @@
 	 * 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);
 
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -61,12 +61,8 @@
 	/* 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 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 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 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
-
-	if (serv_int != p2 || !Vehicle::IsValidID(p1)) return CMD_ERROR;
+	Vehicle *v = Vehicle::GetIfValid(p1);
 
-	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;
--- a/src/waypoint_cmd.cpp
+++ b/src/waypoint_cmd.cpp
@@ -314,10 +314,8 @@
  */
 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);