(svn r8707) -Codechange: Turn IsValidStation into a method of Station

This commit is contained in:
celestar 2007-02-13 15:42:52 +00:00
parent 6f68ac46b8
commit 8eab3964b4
6 changed files with 18 additions and 17 deletions

View File

@ -558,7 +558,7 @@ int32 CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uint32 p2
StationID next_airport_index = v->u.air.targetairport; StationID next_airport_index = v->u.air.targetairport;
const Station *st = GetStation(next_airport_index); const Station *st = GetStation(next_airport_index);
/* If the station is not a valid airport or if it has no hangars */ /* If the station is not a valid airport or if it has no hangars */
if (!IsValidStation(st) || st->airport_tile == 0 || GetAirport(st->airport_type)->nof_depots == 0) { if (!st->IsValid() || st->airport_tile == 0 || GetAirport(st->airport_type)->nof_depots == 0) {
StationID station; StationID station;
// the aircraft has to search for a hangar on its own // the aircraft has to search for a hangar on its own
@ -695,7 +695,7 @@ static void CheckIfAircraftNeedsService(Vehicle *v)
st = GetStation(v->current_order.dest); st = GetStation(v->current_order.dest);
// only goto depot if the target airport has terminals (eg. it is airport) // only goto depot if the target airport has terminals (eg. it is airport)
if (IsValidStation(st) && st->airport_tile != 0 && GetAirport(st->airport_type)->terminals != NULL) { if (st->IsValid() && st->airport_tile != 0 && GetAirport(st->airport_type)->terminals != NULL) {
// printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index); // printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index);
// v->u.air.targetairport = st->index; // v->u.air.targetairport = st->index;
v->current_order.type = OT_GOTO_DEPOT; v->current_order.type = OT_GOTO_DEPOT;

View File

@ -623,7 +623,7 @@ static bool LoadOldStation(LoadgameState *ls, int num)
if (!LoadChunk(ls, st, station_chunk)) if (!LoadChunk(ls, st, station_chunk))
return false; return false;
if (IsValidStation(st)) { if (st->IsValid()) {
if (st->train_tile) { if (st->train_tile) {
/* Calculate the trainst_w and trainst_h */ /* Calculate the trainst_w and trainst_h */
uint w = GB(_old_platforms, 3, 3); uint w = GB(_old_platforms, 3, 3);

View File

@ -162,7 +162,7 @@ bool Station::TileBelongsToRailStation(TileIndex tile) const
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items. /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */ * TODO - This is just a temporary stage, this will be removed. */
for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) { for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) {
if (!IsValidStation(st)) { if (!st->IsValid()) {
StationID index = st->index; StationID index = st->index;
memset(st, 0, sizeof(Station)); memset(st, 0, sizeof(Station));
@ -187,6 +187,14 @@ bool Station::IsBuoy() const
return (this->had_vehicle_of_type & HVOT_BUOY) != 0; return (this->had_vehicle_of_type & HVOT_BUOY) != 0;
} }
/** Determines whether a station exists
* @todo replace 0 by INVALID_TILE
*/
bool Station::IsValid() const
{
return xy != 0;
}
/************************************************************************/ /************************************************************************/
/* StationRect implementation */ /* StationRect implementation */

View File

@ -168,6 +168,7 @@ struct Station {
void MarkTilesDirty() const; void MarkTilesDirty() const;
bool TileBelongsToRailStation(TileIndex tile) const; bool TileBelongsToRailStation(TileIndex tile) const;
bool IsBuoy() const; bool IsBuoy() const;
bool IsValid() const;
protected: protected:
static Station *AllocateRaw(void); static Station *AllocateRaw(void);
@ -237,20 +238,12 @@ static inline uint GetNumStations(void)
return GetStationPoolSize(); return GetStationPoolSize();
} }
/**
* Check if a station really exists.
*/
static inline bool IsValidStation(const Station *st)
{
return st->xy != 0;
}
static inline bool IsValidStationID(StationID index) static inline bool IsValidStationID(StationID index)
{ {
return index < GetStationPoolSize() && IsValidStation(GetStation(index)); return index < GetStationPoolSize() && GetStation(index)->IsValid();
} }
#define FOR_ALL_STATIONS_FROM(st, start) for (st = GetStation(start); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) if (IsValidStation(st)) #define FOR_ALL_STATIONS_FROM(st, start) for (st = GetStation(start); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) if (st->IsValid())
#define FOR_ALL_STATIONS(st) FOR_ALL_STATIONS_FROM(st, 0) #define FOR_ALL_STATIONS(st) FOR_ALL_STATIONS_FROM(st, 0)

View File

@ -54,7 +54,7 @@ static void StationPoolCleanBlock(uint start_item, uint end_item)
for (i = start_item; i <= end_item; i++) { for (i = start_item; i <= end_item; i++) {
Station *st = GetStation(i); Station *st = GetStation(i);
if (IsValidStation(st)) st->~Station(); if (st->IsValid()) st->~Station();
} }
} }
@ -167,7 +167,7 @@ static Station *AllocateStation(void)
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items. /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
* TODO - This is just a temporary stage, this will be removed. */ * TODO - This is just a temporary stage, this will be removed. */
for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) { for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) {
if (!IsValidStation(st)) { if (!st->IsValid()) {
StationID index = st->index; StationID index = st->index;
memset(st, 0, sizeof(Station)); memset(st, 0, sizeof(Station));

View File

@ -802,7 +802,7 @@ static char* FormatString(char* buff, const char* str, const int32* argv, uint c
case SCC_STATION_NAME: { // {STATION} case SCC_STATION_NAME: { // {STATION}
const Station* st = GetStation(GetInt32(&argv)); const Station* st = GetStation(GetInt32(&argv));
if (!IsValidStation(st)) { // station doesn't exist anymore if (!st->IsValid()) { // station doesn't exist anymore
buff = GetStringWithArgs(buff, STR_UNKNOWN_DESTINATION, NULL, last); buff = GetStringWithArgs(buff, STR_UNKNOWN_DESTINATION, NULL, last);
} else { } else {
int32 temp[2]; int32 temp[2];