src/station.cpp
branchnoai
changeset 9694 e72987579514
parent 9629 66dde6412125
child 9701 d1ac22c62f64
equal deleted inserted replaced
9693:31fcaa5375a1 9694:e72987579514
    62  */
    62  */
    63 Station::~Station()
    63 Station::~Station()
    64 {
    64 {
    65 	DEBUG(station, cDebugCtorLevel, "I-%3d", index);
    65 	DEBUG(station, cDebugCtorLevel, "I-%3d", index);
    66 
    66 
    67 	DeleteName(string_id);
       
    68 	MarkDirty();
    67 	MarkDirty();
    69 	RebuildStationLists();
    68 	RebuildStationLists();
    70 	InvalidateWindowClasses(WC_STATION_LIST);
    69 	InvalidateWindowClasses(WC_STATION_LIST);
    71 
    70 
    72 	DeleteWindowById(WC_STATION_VIEW, index);
    71 	DeleteWindowById(WC_STATION_VIEW, index);
    75 	RemoveOrderFromAllVehicles(OT_GOTO_STATION, index);
    74 	RemoveOrderFromAllVehicles(OT_GOTO_STATION, index);
    76 
    75 
    77 	/* Subsidies need removal as well */
    76 	/* Subsidies need removal as well */
    78 	DeleteSubsidyWithStation(index);
    77 	DeleteSubsidyWithStation(index);
    79 
    78 
    80 	free(speclist);
       
    81 	xy = 0;
    79 	xy = 0;
    82 
    80 
    83 	for (CargoID c = 0; c < NUM_CARGO; c++) {
    81 	for (CargoID c = 0; c < NUM_CARGO; c++) {
    84 		goods[c].cargo.Truncate(0);
    82 		goods[c].cargo.Truncate(0);
    85 	}
    83 	}
    86 }
    84 
    87 
    85 	this->QuickFree();
    88 void* Station::operator new(size_t size)
    86 }
    89 {
    87 
    90 	Station *st = AllocateRaw();
    88 void Station::QuickFree()
    91 	return st;
    89 {
    92 }
    90 	DeleteName(this->string_id);
    93 
    91 	free(this->speclist);
    94 void* Station::operator new(size_t size, int st_idx)
       
    95 {
       
    96 	if (!AddBlockIfNeeded(&_Station_pool, st_idx))
       
    97 		error("Stations: failed loading savegame: too many stations");
       
    98 
       
    99 	Station *st = GetStation(st_idx);
       
   100 	return st;
       
   101 }
       
   102 
       
   103 void Station::operator delete(void *p)
       
   104 {
       
   105 }
       
   106 
       
   107 void Station::operator delete(void *p, int st_idx)
       
   108 {
       
   109 }
    92 }
   110 
    93 
   111 /** Called when new facility is built on the station. If it is the first facility
    94 /** Called when new facility is built on the station. If it is the first facility
   112  * it initializes also 'xy' and 'random_bits' members */
    95  * it initializes also 'xy' and 'random_bits' members */
   113 void Station::AddFacility(byte new_facility_bit, TileIndex facil_xy)
    96 void Station::AddFacility(byte new_facility_bit, TileIndex facil_xy)
   167 
   150 
   168 bool Station::TileBelongsToRailStation(TileIndex tile) const
   151 bool Station::TileBelongsToRailStation(TileIndex tile) const
   169 {
   152 {
   170 	return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == index && IsRailwayStation(tile);
   153 	return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == index && IsRailwayStation(tile);
   171 }
   154 }
   172 
       
   173 /*static*/ Station *Station::AllocateRaw()
       
   174 {
       
   175 	Station *st = NULL;
       
   176 
       
   177 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
   178 	 * TODO - This is just a temporary stage, this will be removed. */
       
   179 	for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) {
       
   180 		if (!st->IsValid()) {
       
   181 			StationID index = st->index;
       
   182 
       
   183 			memset(st, 0, sizeof(Station));
       
   184 			st->index = index;
       
   185 			return st;
       
   186 		}
       
   187 	}
       
   188 
       
   189 	/* Check if we can add a block to the pool */
       
   190 	if (AddBlockToPool(&_Station_pool)) return AllocateRaw();
       
   191 
       
   192 	_error_message = STR_3008_TOO_MANY_STATIONS_LOADING;
       
   193 	return NULL;
       
   194 }
       
   195 
       
   196 
   155 
   197 /** Obtain the length of a platform
   156 /** Obtain the length of a platform
   198  * @pre tile must be a railway station tile
   157  * @pre tile must be a railway station tile
   199  * @param tile A tile that contains the platform in question
   158  * @param tile A tile that contains the platform in question
   200  * @return The length of the platform
   159  * @return The length of the platform
   418 
   377 
   419 /************************************************************************/
   378 /************************************************************************/
   420 /*                     RoadStop implementation                          */
   379 /*                     RoadStop implementation                          */
   421 /************************************************************************/
   380 /************************************************************************/
   422 
   381 
   423 /** Allocates a new RoadStop onto the pool, or recycles an unsed one
       
   424  *  @return a pointer to the new roadstop
       
   425  */
       
   426 void *RoadStop::operator new(size_t size)
       
   427 {
       
   428 	RoadStop *rs = AllocateRaw();
       
   429 	return rs;
       
   430 }
       
   431 
       
   432 /** Gets a RoadStop with a given index and allocates it when needed
       
   433   * @return a pointer to the roadstop
       
   434   */
       
   435 void *RoadStop::operator new(size_t size, int index)
       
   436 {
       
   437 	if (!AddBlockIfNeeded(&_RoadStop_pool, index)) {
       
   438 		error("RoadStops: failed loading savegame: too many RoadStops");
       
   439 	}
       
   440 
       
   441 	RoadStop *rs = GetRoadStop(index);
       
   442 	return rs;
       
   443 }
       
   444 
       
   445 void RoadStop::operator delete(void *p)
       
   446 {
       
   447 }
       
   448 
       
   449 void RoadStop::operator delete(void *p, int index)
       
   450 {
       
   451 }
       
   452 
       
   453 /** Initializes a RoadStop */
   382 /** Initializes a RoadStop */
   454 RoadStop::RoadStop(TileIndex tile) :
   383 RoadStop::RoadStop(TileIndex tile) :
   455 	xy(tile),
   384 	xy(tile),
   456 	status(3), // stop is free
   385 	status(3), // stop is free
   457 	num_vehicles(0),
   386 	num_vehicles(0),
   475 	}
   404 	}
   476 	assert(num_vehicles == 0);
   405 	assert(num_vehicles == 0);
   477 
   406 
   478 	DEBUG(ms, cDebugCtorLevel , "I- at %d[0x%x]", xy, xy);
   407 	DEBUG(ms, cDebugCtorLevel , "I- at %d[0x%x]", xy, xy);
   479 
   408 
   480 	xy = INVALID_TILE;
   409 	xy = 0;
   481 }
       
   482 
       
   483 
       
   484 /** Low-level function for allocating a RoadStop on the pool */
       
   485 RoadStop *RoadStop::AllocateRaw()
       
   486 {
       
   487 	RoadStop *rs;
       
   488 
       
   489 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
   490 	 * TODO - This is just a temporary stage, this will be removed. */
       
   491 	for (rs = GetRoadStop(0); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) {
       
   492 		if (!rs->IsValid()) {
       
   493 			RoadStopID index = rs->index;
       
   494 
       
   495 			memset(rs, 0, sizeof(*rs));
       
   496 			rs->index = index;
       
   497 
       
   498 			return rs;
       
   499 		}
       
   500 	}
       
   501 
       
   502 	/* Check if we can add a block to the pool */
       
   503 	if (AddBlockToPool(&_RoadStop_pool)) return AllocateRaw();
       
   504 
       
   505 	return NULL;
       
   506 }
   410 }
   507 
   411 
   508 /** Determines whether a RoadStop is a valid (i.e. existing) one */
   412 /** Determines whether a RoadStop is a valid (i.e. existing) one */
   509 bool RoadStop::IsValid() const
   413 bool RoadStop::IsValid() const
   510 {
   414 {
   511 	return xy != INVALID_TILE;
   415 	return xy != 0;
   512 }
   416 }
   513 
   417 
   514 /** Checks whether there is a free bay in this road stop */
   418 /** Checks whether there is a free bay in this road stop */
   515 bool RoadStop::HasFreeBay() const
   419 bool RoadStop::HasFreeBay() const
   516 {
   420 {