summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortruelight <truelight@openttd.org>2006-08-22 18:15:17 +0000
committertruelight <truelight@openttd.org>2006-08-22 18:15:17 +0000
commitb23f719ab91a34be3e9d44d1165c789edd4df513 (patch)
tree8fdd9f9cb1552a9d33e7f6c2bbdeccff776cfc62
parent1331b4aa9a525bb87c76c38bbc42d33d099bd026 (diff)
downloadopenttd-b23f719ab91a34be3e9d44d1165c789edd4df513.tar.xz
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
-Codechange: IsValidXXXID now also checks if XXX is really valid, not if the number is within range Both changes again in preperation of the new mem-pool system, which requires this. IsValidXXXID is not a bit less pretty, but that will be cleaned up after the new mem-pool system
-rw-r--r--aircraft_cmd.c8
-rw-r--r--depot.h10
-rw-r--r--order_cmd.c57
-rw-r--r--road_cmd.c2
-rw-r--r--roadveh_cmd.c10
-rw-r--r--ship_cmd.c8
-rw-r--r--signs.c2
-rw-r--r--signs.h10
-rw-r--r--station.h10
-rw-r--r--station_cmd.c7
-rw-r--r--town.h4
-rw-r--r--town_cmd.c9
-rw-r--r--train_cmd.c14
-rw-r--r--vehicle.c8
-rw-r--r--vehicle.h4
-rw-r--r--waypoint.c2
-rw-r--r--waypoint.h10
17 files changed, 88 insertions, 87 deletions
diff --git a/aircraft_cmd.c b/aircraft_cmd.c
index 1f9da7aa6..6dd814da0 100644
--- a/aircraft_cmd.c
+++ b/aircraft_cmd.c
@@ -426,7 +426,7 @@ int32 CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -456,7 +456,7 @@ int32 CmdStartStopAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
uint16 callback;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -500,7 +500,7 @@ int32 CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uint32 p2
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -568,7 +568,7 @@ int32 CmdRefitAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
const AircraftVehicleInfo *avi;
uint16 callback = CALLBACK_FAILED;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
diff --git a/depot.h b/depot.h
index 4d79e179e..864237cf0 100644
--- a/depot.h
+++ b/depot.h
@@ -35,11 +35,6 @@ static inline uint16 GetDepotPoolSize(void)
return _depot_pool.total_items;
}
-static inline bool IsDepotIndex(uint index)
-{
- return index < GetDepotPoolSize();
-}
-
/**
* Check if a depot really exists.
*/
@@ -48,6 +43,11 @@ static inline bool IsValidDepot(const Depot* depot)
return depot->xy != 0;
}
+static inline bool IsValidDepotID(uint index)
+{
+ return index < GetDepotPoolSize() && IsValidDepot(GetDepot(index));
+}
+
#define FOR_ALL_DEPOTS_FROM(d, start) for (d = GetDepot(start); d != NULL; d = (d->index + 1 < GetDepotPoolSize()) ? GetDepot(d->index + 1) : NULL) if (IsValidDepot(d))
#define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0)
diff --git a/order_cmd.c b/order_cmd.c
index 0f84d40f6..3cca2aa0f 100644
--- a/order_cmd.c
+++ b/order_cmd.c
@@ -179,9 +179,11 @@ int32 CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
OrderID sel_ord = GB(p1, 16, 16);
Order new_order = UnpackOrder(p2);
- if (!IsVehicleIndex(veh)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh)) return CMD_ERROR;
+
v = GetVehicle(veh);
- if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
+
+ if (!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 */
@@ -189,11 +191,10 @@ int32 CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
case OT_GOTO_STATION: {
const Station *st;
- if (!IsStationIndex(new_order.station)) return CMD_ERROR;
+ if (!IsValidStationID(new_order.station)) return CMD_ERROR;
st = GetStation(new_order.station);
- if (!IsValidStation(st) ||
- (st->airport_type != AT_OILRIG && !IsBuoy(st) && !CheckOwnership(st->owner))) {
+ if (st->airport_type != AT_OILRIG && !IsBuoy(st) && !CheckOwnership(st->owner)) {
return CMD_ERROR;
}
@@ -251,11 +252,10 @@ int32 CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (v->type == VEH_Aircraft) {
const Station* st;
- if (!IsStationIndex(new_order.station)) return CMD_ERROR;
+ if (!IsValidStationID(new_order.station)) return CMD_ERROR;
st = GetStation(new_order.station);
- if (!IsValidStation(st) ||
- (st->airport_type != AT_OILRIG && !CheckOwnership(st->owner)) ||
+ if ((st->airport_type != AT_OILRIG && !CheckOwnership(st->owner)) ||
!(st->facilities & FACIL_AIRPORT) ||
GetAirport(st->airport_type)->nof_depots == 0) {
return CMD_ERROR;
@@ -263,11 +263,10 @@ int32 CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
} else {
const Depot* dp;
- if (!IsDepotIndex(new_order.station)) return CMD_ERROR;
+ if (!IsValidDepotID(new_order.station)) return CMD_ERROR;
dp = GetDepot(new_order.station);
- if (!IsValidDepot(dp) || !CheckOwnership(GetTileOwner(dp->xy)))
- return CMD_ERROR;
+ if (!CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR;
switch (v->type) {
case VEH_Train:
@@ -309,7 +308,7 @@ int32 CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (v->type != VEH_Train) return CMD_ERROR;
- if (!IsWaypointIndex(new_order.station)) return CMD_ERROR;
+ if (!IsValidWaypointID(new_order.station)) return CMD_ERROR;
wp = GetWaypoint(new_order.station);
if (!CheckOwnership(GetTileOwner(wp->xy))) return CMD_ERROR;
@@ -442,9 +441,11 @@ int32 CmdDeleteOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
OrderID sel_ord = p2;
Order *order;
- if (!IsVehicleIndex(veh_id)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh_id)) return CMD_ERROR;
+
v = GetVehicle(veh_id);
- if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
+
+ if (!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->num_orders)
@@ -514,9 +515,11 @@ int32 CmdSkipOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
VehicleID veh_id = p1;
- if (!IsVehicleIndex(veh_id)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh_id)) return CMD_ERROR;
+
v = GetVehicle(veh_id);
- if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
+
+ if (!CheckOwnership(v->owner)) return CMD_ERROR;
if (flags & DC_EXEC) {
/* Goto next order */
@@ -561,11 +564,12 @@ int32 CmdModifyOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
OrderID sel_ord = GB(p1, 16, 16); // XXX - automatically truncated to 8 bits.
VehicleID veh = GB(p1, 0, 16);
- if (!IsVehicleIndex(veh)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh)) return CMD_ERROR;
if (p2 != OFB_FULL_LOAD && p2 != OFB_UNLOAD && p2 != OFB_NON_STOP && p2 != OFB_TRANSFER) return CMD_ERROR;
v = GetVehicle(veh);
- if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
+
+ if (!CheckOwnership(v->owner)) return CMD_ERROR;
/* Is it a valid order? */
if (sel_ord >= v->num_orders) return CMD_ERROR;
@@ -628,22 +632,22 @@ int32 CmdCloneOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
VehicleID veh_src = GB(p1, 16, 16);
VehicleID veh_dst = GB(p1, 0, 16);
- if (!IsVehicleIndex(veh_dst)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh_dst)) return CMD_ERROR;
dst = GetVehicle(veh_dst);
- if (!IsValidVehicle(dst) || !CheckOwnership(dst->owner)) return CMD_ERROR;
+ if (!CheckOwnership(dst->owner)) return CMD_ERROR;
switch (p2) {
case CO_SHARE: {
Vehicle *src;
- if (!IsVehicleIndex(veh_src)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh_src)) return CMD_ERROR;
src = GetVehicle(veh_src);
/* Sanity checks */
- if (!IsValidVehicle(src) || !CheckOwnership(src->owner) || dst->type != src->type || dst == src)
+ if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src)
return CMD_ERROR;
/* Trucks can't share orders with busses (and visa versa) */
@@ -685,12 +689,12 @@ int32 CmdCloneOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *src;
int delta;
- if (!IsVehicleIndex(veh_src)) return CMD_ERROR;
+ if (!IsValidVehicleID(veh_src)) return CMD_ERROR;
src = GetVehicle(veh_src);
/* Sanity checks */
- if (!IsValidVehicle(src) || !CheckOwnership(src->owner) || dst->type != src->type || dst == src)
+ if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src)
return CMD_ERROR;
/* Trucks can't copy all the orders from busses (and visa versa) */
@@ -844,11 +848,12 @@ int32 CmdRestoreOrderIndex(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
OrderID cur_ord = GB(p2, 0, 16);
uint16 serv_int = GB(p2, 16, 16);
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
+
/* Check the vehicle type and ownership, and if the service interval and order are in range */
- if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
+ if (!CheckOwnership(v->owner)) return CMD_ERROR;
if (serv_int != GetServiceIntervalClamped(serv_int) || cur_ord >= v->num_orders) return CMD_ERROR;
if (flags & DC_EXEC) {
diff --git a/road_cmd.c b/road_cmd.c
index 6624df88f..6b4956a31 100644
--- a/road_cmd.c
+++ b/road_cmd.c
@@ -293,7 +293,7 @@ int32 CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
* if a non-player is building the road */
- if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsTownIndex(p2)) return CMD_ERROR;
+ if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsValidTownID(p2)) return CMD_ERROR;
pieces = p1;
tileh = GetTileSlope(tile, NULL);
diff --git a/roadveh_cmd.c b/roadveh_cmd.c
index 1f3f81083..42562ee1f 100644
--- a/roadveh_cmd.c
+++ b/roadveh_cmd.c
@@ -212,7 +212,7 @@ int32 CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
uint16 callback;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -262,7 +262,7 @@ int32 CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -364,7 +364,7 @@ int32 CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
const Depot *dep;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -411,7 +411,7 @@ int32 CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1757,7 +1757,7 @@ int32 CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
byte new_subtype = GB(p2, 8, 8);
uint16 capacity = CALLBACK_FAILED;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
diff --git a/ship_cmd.c b/ship_cmd.c
index d5b5a1dfc..367742802 100644
--- a/ship_cmd.c
+++ b/ship_cmd.c
@@ -929,7 +929,7 @@ int32 CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -964,7 +964,7 @@ int32 CmdStartStopShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
uint16 callback;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1002,7 +1002,7 @@ int32 CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
const Depot *dep;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1056,7 +1056,7 @@ int32 CmdRefitShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
byte new_subtype = GB(p2, 8, 8);
uint16 capacity = CALLBACK_FAILED;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
diff --git a/signs.c b/signs.c
index 95583a493..7ce30a810 100644
--- a/signs.c
+++ b/signs.c
@@ -147,7 +147,7 @@ int32 CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
*/
int32 CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
- if (!IsSignIndex(p1)) return CMD_ERROR;
+ if (!IsValidSignID(p1)) return CMD_ERROR;
/* If _cmd_text 0 means the new text for the sign is non-empty.
* So rename the sign. If it is empty, it has no name, so delete it */
diff --git a/signs.h b/signs.h
index 78d1e32e4..ce74cd38b 100644
--- a/signs.h
+++ b/signs.h
@@ -34,11 +34,6 @@ static inline uint16 GetSignPoolSize(void)
return _sign_pool.total_items;
}
-static inline bool IsSignIndex(uint index)
-{
- return index < GetSignPoolSize();
-}
-
/**
* Check if a Sign really exists.
*/
@@ -47,6 +42,11 @@ static inline bool IsValidSign(const Sign *si)
return si->str != STR_NULL;
}
+static inline bool IsValidSignID(uint index)
+{
+ return index < GetSignPoolSize() && IsValidSign(GetSign(index));
+}
+
#define FOR_ALL_SIGNS_FROM(ss, start) for (ss = GetSign(start); ss != NULL; ss = (ss->index + 1 < GetSignPoolSize()) ? GetSign(ss->index + 1) : NULL) if (IsValidSign(ss))
#define FOR_ALL_SIGNS(ss) FOR_ALL_SIGNS_FROM(ss, 0)
diff --git a/station.h b/station.h
index c28e6d093..1a343410d 100644
--- a/station.h
+++ b/station.h
@@ -161,11 +161,6 @@ static inline uint16 GetStationPoolSize(void)
return _station_pool.total_items;
}
-static inline bool IsStationIndex(StationID index)
-{
- return index < GetStationPoolSize();
-}
-
/**
* Check if a station really exists.
*/
@@ -174,6 +169,11 @@ static inline bool IsValidStation(const Station *st)
return st->xy != 0;
}
+static inline bool IsValidStationID(StationID index)
+{
+ return index < GetStationPoolSize() && IsValidStation(GetStation(index));
+}
+
#define FOR_ALL_STATIONS_FROM(st, start) for (st = GetStation(start); st != NULL; st = (st->index + 1 < GetStationPoolSize()) ? GetStation(st->index + 1) : NULL) if (IsValidStation(st))
#define FOR_ALL_STATIONS(st) FOR_ALL_STATIONS_FROM(st, 0)
diff --git a/station_cmd.c b/station_cmd.c
index 974d7be1d..e5124c927 100644
--- a/station_cmd.c
+++ b/station_cmd.c
@@ -2573,8 +2573,7 @@ void OnTick_Station(void)
i = _station_tick_ctr;
if (++_station_tick_ctr == GetStationPoolSize()) _station_tick_ctr = 0;
- st = GetStation(i);
- if (IsValidStation(st)) StationHandleBigTick(st);
+ if (IsValidStationID(i)) StationHandleBigTick(GetStation(i));
FOR_ALL_STATIONS(st) {
StationHandleSmallTick(st);
@@ -2627,10 +2626,10 @@ int32 CmdRenameStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
StringID str;
Station *st;
- if (!IsStationIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+ if (!IsValidStationID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
st = GetStation(p1);
- if (!IsValidStation(st) || !CheckOwnership(st->owner)) return CMD_ERROR;
+ if (!CheckOwnership(st->owner)) return CMD_ERROR;
str = AllocateNameUnique(_cmd_text, 6);
if (str == 0) return CMD_ERROR;
diff --git a/town.h b/town.h
index 16c5dbffa..4bccadb1b 100644
--- a/town.h
+++ b/town.h
@@ -179,9 +179,9 @@ static inline uint16 GetTownPoolSize(void)
return _town_pool.total_items;
}
-static inline bool IsTownIndex(uint index)
+static inline bool IsValidTownID(uint index)
{
- return index < GetTownPoolSize();
+ return index < GetTownPoolSize() && IsValidTown(GetTown(index));
}
#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1 < GetTownPoolSize()) ? GetTown(t->index + 1) : NULL) if (IsValidTown(t))
diff --git a/town_cmd.c b/town_cmd.c
index e43726dce..46837cbde 100644
--- a/town_cmd.c
+++ b/town_cmd.c
@@ -409,14 +409,11 @@ void OnTick_Town(void)
_cur_town_iter >= TOWN_GROWTH_FREQUENCY;
_cur_town_iter -= TOWN_GROWTH_FREQUENCY) {
uint32 i = _cur_town_ctr;
- Town *t;
if (++_cur_town_ctr >= GetTownPoolSize())
_cur_town_ctr = 0;
- t = GetTown(i);
-
- if (IsValidTown(t)) TownTickHandler(t);
+ if (IsValidTownID(i)) TownTickHandler(GetTown(i));
}
}
@@ -1348,7 +1345,7 @@ int32 CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
StringID str;
Town *t;
- if (!IsTownIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+ if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
t = GetTown(p1);
@@ -1605,7 +1602,7 @@ int32 CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
int32 cost;
Town *t;
- if (!IsTownIndex(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
+ if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
t = GetTown(p1);
diff --git a/train_cmd.c b/train_cmd.c
index fad5245af..f5e539ce3 100644
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -951,7 +951,7 @@ int32 CmdMoveRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
VehicleID d = GB(p1, 16, 16);
Vehicle *src, *dst, *src_head, *dst_head;
- if (!IsVehicleIndex(s)) return CMD_ERROR;
+ if (!IsValidVehicleID(s)) return CMD_ERROR;
src = GetVehicle(s);
@@ -1230,7 +1230,7 @@ int32 CmdStartStopTrain(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
uint16 callback;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1275,7 +1275,7 @@ int32 CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *new_f = NULL;
int32 cost = 0;
- if (!IsVehicleIndex(p1) || p2 > 2) return CMD_ERROR;
+ if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1671,7 +1671,7 @@ int32 CmdReverseTrainDirection(TileIndex tile, uint32 flags, uint32 p1, uint32 p
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1720,7 +1720,7 @@ int32 CmdForceTrainProceed(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
Vehicle *v;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1746,7 +1746,7 @@ int32 CmdRefitRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
int32 cost;
uint num;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1930,7 +1930,7 @@ int32 CmdSendTrainToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
TrainFindDepotData tfdd;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
diff --git a/vehicle.c b/vehicle.c
index 02c74f718..d14ad67cc 100644
--- a/vehicle.c
+++ b/vehicle.c
@@ -1526,7 +1526,7 @@ int32 CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
int cost, total_cost = 0;
uint32 build_argument = 2;
- if (!IsVehicleIndex(p1)) return CMD_ERROR;
+ if (!IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
v_front = v;
w = NULL;
@@ -1907,7 +1907,7 @@ int32 CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
StringID str;
- if (!IsVehicleIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+ if (!IsValidVehicleID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
v = GetVehicle(p1);
@@ -1940,11 +1940,11 @@ int32 CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle* v;
uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
- if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
+ if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;
v = GetVehicle(p1);
- if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
+ if (!CheckOwnership(v->owner)) return CMD_ERROR;
if (flags & DC_EXEC) {
v->service_interval = serv_int;
diff --git a/vehicle.h b/vehicle.h
index 760cfb8ea..6a434a15c 100644
--- a/vehicle.h
+++ b/vehicle.h
@@ -375,9 +375,9 @@ static inline bool IsValidVehicle(const Vehicle *v)
*
* @return Returns true if the vehicle-id is in range
*/
-static inline bool IsVehicleIndex(uint index)
+static inline bool IsValidVehicleID(uint index)
{
- return index < GetVehiclePoolSize();
+ return index < GetVehiclePoolSize() && IsValidVehicle(GetVehicle(index));
}
/* Returns order 'index' of a vehicle or NULL when it doesn't exists */
diff --git a/waypoint.c b/waypoint.c
index ca9c9987f..d5cd4987d 100644
--- a/waypoint.c
+++ b/waypoint.c
@@ -319,7 +319,7 @@ int32 CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
Waypoint *wp;
- if (!IsWaypointIndex(p1)) return CMD_ERROR;
+ if (!IsValidWaypointID(p1)) return CMD_ERROR;
if (_cmd_text[0] != '\0') {
StringID str = AllocateNameUnique(_cmd_text, 0);
diff --git a/waypoint.h b/waypoint.h
index aac46518a..74ba5d8e4 100644
--- a/waypoint.h
+++ b/waypoint.h
@@ -42,11 +42,6 @@ static inline uint16 GetWaypointPoolSize(void)
return _waypoint_pool.total_items;
}
-static inline bool IsWaypointIndex(uint index)
-{
- return index < GetWaypointPoolSize();
-}
-
/**
* Check if a Waypoint really exists.
*/
@@ -55,6 +50,11 @@ static inline bool IsValidWaypoint(const Waypoint *wp)
return wp->xy != 0;
}
+static inline bool IsValidWaypointID(uint index)
+{
+ return index < GetWaypointPoolSize() && IsValidWaypoint(GetWaypoint(index));
+}
+
#define FOR_ALL_WAYPOINTS_FROM(wp, start) for (wp = GetWaypoint(start); wp != NULL; wp = (wp->index + 1 < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1) : NULL) if (IsValidWaypoint(wp))
#define FOR_ALL_WAYPOINTS(wp) FOR_ALL_WAYPOINTS_FROM(wp, 0)