mirror of https://github.com/OpenTTD/OpenTTD
(svn r6975) Use the pool macros for the Vehicle pool
parent
ef862a2973
commit
953344fdcc
2
date.c
2
date.c
|
@ -196,7 +196,7 @@ static const Month _autosave_months[] = {
|
||||||
*/
|
*/
|
||||||
static void RunVehicleDayProc(uint daytick)
|
static void RunVehicleDayProc(uint daytick)
|
||||||
{
|
{
|
||||||
uint total = _vehicle_pool.total_items;
|
uint total = GetVehiclePoolSize();
|
||||||
uint i;
|
uint i;
|
||||||
|
|
||||||
for (i = daytick; i < total; i += DAY_TICKS) {
|
for (i = daytick; i < total; i += DAY_TICKS) {
|
||||||
|
|
|
@ -1196,7 +1196,7 @@ static bool LoadOldVehicle(LoadgameState *ls, int num)
|
||||||
|
|
||||||
_current_vehicle_id = num * _old_vehicle_multiplier + i;
|
_current_vehicle_id = num * _old_vehicle_multiplier + i;
|
||||||
|
|
||||||
if (!AddBlockIfNeeded(&_vehicle_pool, _current_vehicle_id))
|
if (!AddBlockIfNeeded(&_Vehicle_pool, _current_vehicle_id))
|
||||||
error("Vehicles: failed loading savegame: too many vehicles");
|
error("Vehicles: failed loading savegame: too many vehicles");
|
||||||
|
|
||||||
v = GetVehicle(_current_vehicle_id);
|
v = GetVehicle(_current_vehicle_id);
|
||||||
|
|
|
@ -257,7 +257,7 @@ static void UnInitializeDynamicVariables(void)
|
||||||
CleanPool(&_town_pool);
|
CleanPool(&_town_pool);
|
||||||
CleanPool(&_industry_pool);
|
CleanPool(&_industry_pool);
|
||||||
CleanPool(&_station_pool);
|
CleanPool(&_station_pool);
|
||||||
CleanPool(&_vehicle_pool);
|
CleanPool(&_Vehicle_pool);
|
||||||
CleanPool(&_sign_pool);
|
CleanPool(&_sign_pool);
|
||||||
CleanPool(&_order_pool);
|
CleanPool(&_order_pool);
|
||||||
|
|
||||||
|
|
|
@ -1253,7 +1253,7 @@ static void *IntToReference(uint index, SLRefType rt)
|
||||||
return GetOrder(index);
|
return GetOrder(index);
|
||||||
}
|
}
|
||||||
case REF_VEHICLE: {
|
case REF_VEHICLE: {
|
||||||
if (!AddBlockIfNeeded(&_vehicle_pool, index))
|
if (!AddBlockIfNeeded(&_Vehicle_pool, index))
|
||||||
error("Vehicles: failed loading savegame: too many vehicles");
|
error("Vehicles: failed loading savegame: too many vehicles");
|
||||||
return GetVehicle(index);
|
return GetVehicle(index);
|
||||||
}
|
}
|
||||||
|
@ -1286,7 +1286,7 @@ static void *IntToReference(uint index, SLRefType rt)
|
||||||
if (index == INVALID_VEHICLE)
|
if (index == INVALID_VEHICLE)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!AddBlockIfNeeded(&_vehicle_pool, index))
|
if (!AddBlockIfNeeded(&_Vehicle_pool, index))
|
||||||
error("Vehicles: failed loading savegame: too many vehicles");
|
error("Vehicles: failed loading savegame: too many vehicles");
|
||||||
return GetVehicle(index);
|
return GetVehicle(index);
|
||||||
}
|
}
|
||||||
|
|
30
vehicle.c
30
vehicle.c
|
@ -79,10 +79,6 @@ const uint32 _send_to_depot_proc_table[] = {
|
||||||
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
/* Max vehicles: 64000 (512 * 125) */
|
|
||||||
VEHICLES_POOL_BLOCK_SIZE_BITS = 9, /* In bits, so (1 << 9) == 512 */
|
|
||||||
VEHICLES_POOL_MAX_BLOCKS = 125,
|
|
||||||
|
|
||||||
BLOCKS_FOR_SPECIAL_VEHICLES = 2, ///< Blocks needed for special vehicles
|
BLOCKS_FOR_SPECIAL_VEHICLES = 2, ///< Blocks needed for special vehicles
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -95,11 +91,11 @@ static void VehiclePoolNewBlock(uint start_item)
|
||||||
|
|
||||||
/* 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 (v = GetVehicle(start_item); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) v->index = start_item++;
|
for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) v->index = start_item++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the vehicle-pool */
|
/* Initialize the vehicle-pool */
|
||||||
MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, NULL, 0, 0, NULL };
|
DEFINE_POOL(Vehicle, Vehicle, VehiclePoolNewBlock, NULL)
|
||||||
|
|
||||||
void VehicleServiceInDepot(Vehicle *v)
|
void VehicleServiceInDepot(Vehicle *v)
|
||||||
{
|
{
|
||||||
|
@ -297,9 +293,9 @@ Vehicle *ForceAllocateSpecialVehicle(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 (v = GetVehicle(0); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
|
for (v = GetVehicle(0); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
|
||||||
/* No more room for the special vehicles, return NULL */
|
/* No more room for the special vehicles, return NULL */
|
||||||
if (v->index >= (1 << _vehicle_pool.block_size_bits) * BLOCKS_FOR_SPECIAL_VEHICLES)
|
if (v->index >= (1 << Vehicle_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!IsValidVehicle(v)) return InitializeVehicle(v);
|
if (!IsValidVehicle(v)) return InitializeVehicle(v);
|
||||||
|
@ -314,26 +310,26 @@ Vehicle *ForceAllocateSpecialVehicle(void)
|
||||||
* *skip_vehicles is an offset to where in the array we should begin looking
|
* *skip_vehicles is an offset to where in the array we should begin looking
|
||||||
* this is to avoid looping though the same vehicles more than once after we learned that they are not free
|
* this is to avoid looping though the same vehicles more than once after we learned that they are not free
|
||||||
* this feature is used by AllocateVehicles() since it need to allocate more than one and when
|
* this feature is used by AllocateVehicles() since it need to allocate more than one and when
|
||||||
* another block is added to _vehicle_pool, since we only do that when we know it's already full
|
* another block is added to _Vehicle_pool, since we only do that when we know it's already full
|
||||||
*/
|
*/
|
||||||
static Vehicle *AllocateSingleVehicle(VehicleID *skip_vehicles)
|
static Vehicle *AllocateSingleVehicle(VehicleID *skip_vehicles)
|
||||||
{
|
{
|
||||||
/* See note by ForceAllocateSpecialVehicle() why we skip the
|
/* See note by ForceAllocateSpecialVehicle() why we skip the
|
||||||
* first blocks */
|
* first blocks */
|
||||||
Vehicle *v;
|
Vehicle *v;
|
||||||
const int offset = (1 << VEHICLES_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES;
|
const int offset = (1 << Vehicle_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES;
|
||||||
|
|
||||||
/* 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. */
|
||||||
if (*skip_vehicles < (_vehicle_pool.total_items - offset)) { // make sure the offset in the array is not larger than the array itself
|
if (*skip_vehicles < (_Vehicle_pool.total_items - offset)) { // make sure the offset in the array is not larger than the array itself
|
||||||
for (v = GetVehicle(offset + *skip_vehicles); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
|
for (v = GetVehicle(offset + *skip_vehicles); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
|
||||||
(*skip_vehicles)++;
|
(*skip_vehicles)++;
|
||||||
if (!IsValidVehicle(v)) return InitializeVehicle(v);
|
if (!IsValidVehicle(v)) return InitializeVehicle(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if we can add a block to the pool */
|
/* Check if we can add a block to the pool */
|
||||||
if (AddBlockToPool(&_vehicle_pool))
|
if (AddBlockToPool(&_Vehicle_pool))
|
||||||
return AllocateSingleVehicle(skip_vehicles);
|
return AllocateSingleVehicle(skip_vehicles);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -451,10 +447,10 @@ void InitializeVehicles(void)
|
||||||
/* Clean the vehicle pool, and reserve enough blocks
|
/* Clean the vehicle pool, and reserve enough blocks
|
||||||
* for the special vehicles, plus one for all the other
|
* for the special vehicles, plus one for all the other
|
||||||
* vehicles (which is increased on-the-fly) */
|
* vehicles (which is increased on-the-fly) */
|
||||||
CleanPool(&_vehicle_pool);
|
CleanPool(&_Vehicle_pool);
|
||||||
AddBlockToPool(&_vehicle_pool);
|
AddBlockToPool(&_Vehicle_pool);
|
||||||
for (i = 0; i < BLOCKS_FOR_SPECIAL_VEHICLES; i++)
|
for (i = 0; i < BLOCKS_FOR_SPECIAL_VEHICLES; i++)
|
||||||
AddBlockToPool(&_vehicle_pool);
|
AddBlockToPool(&_Vehicle_pool);
|
||||||
|
|
||||||
for (i = 0; i < lengthof(_vehicle_position_hash); i++) {
|
for (i = 0; i < lengthof(_vehicle_position_hash); i++) {
|
||||||
_vehicle_position_hash[i] = INVALID_VEHICLE;
|
_vehicle_position_hash[i] = INVALID_VEHICLE;
|
||||||
|
@ -3177,7 +3173,7 @@ static void Load_VEHS(void)
|
||||||
while ((index = SlIterateArray()) != -1) {
|
while ((index = SlIterateArray()) != -1) {
|
||||||
Vehicle *v;
|
Vehicle *v;
|
||||||
|
|
||||||
if (!AddBlockIfNeeded(&_vehicle_pool, index))
|
if (!AddBlockIfNeeded(&_Vehicle_pool, index))
|
||||||
error("Vehicles: failed loading savegame: too many vehicles");
|
error("Vehicles: failed loading savegame: too many vehicles");
|
||||||
|
|
||||||
v = GetVehicle(index);
|
v = GetVehicle(index);
|
||||||
|
|
20
vehicle.h
20
vehicle.h
|
@ -359,23 +359,7 @@ Direction GetDirectionTowards(const Vehicle* v, int x, int y);
|
||||||
#define BEGIN_ENUM_WAGONS(v) do {
|
#define BEGIN_ENUM_WAGONS(v) do {
|
||||||
#define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
|
#define END_ENUM_WAGONS(v) } while ( (v=v->next) != NULL);
|
||||||
|
|
||||||
extern MemoryPool _vehicle_pool;
|
DECLARE_POOL(Vehicle, Vehicle, 9, 125)
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the pointer to the vehicle with index 'index'
|
|
||||||
*/
|
|
||||||
static inline Vehicle *GetVehicle(VehicleID index)
|
|
||||||
{
|
|
||||||
return (Vehicle*)GetItemFromPool(&_vehicle_pool, index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current size of the VehiclePool
|
|
||||||
*/
|
|
||||||
static inline uint16 GetVehiclePoolSize(void)
|
|
||||||
{
|
|
||||||
return _vehicle_pool.total_items;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline VehicleID GetVehicleArraySize(void)
|
static inline VehicleID GetVehicleArraySize(void)
|
||||||
{
|
{
|
||||||
|
@ -403,7 +387,7 @@ static inline void DeleteVehicle(Vehicle *v)
|
||||||
v->type = 0;
|
v->type = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
|
#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
|
||||||
#define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
|
#define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue