src/depot.cpp
branchnoai
changeset 9694 e72987579514
parent 9624 b71483f2330f
child 7909 8df54a2839a1
equal deleted inserted replaced
9693:31fcaa5375a1 9694:e72987579514
    12 #include "map.h"
    12 #include "map.h"
    13 #include "table/strings.h"
    13 #include "table/strings.h"
    14 #include "saveload.h"
    14 #include "saveload.h"
    15 #include "order.h"
    15 #include "order.h"
    16 
    16 
    17 
    17 DEFINE_OLD_POOL_GENERIC(Depot, Depot)
    18 /**
       
    19  * Called if a new block is added to the depot-pool
       
    20  */
       
    21 static void DepotPoolNewBlock(uint start_item)
       
    22 {
       
    23 	Depot *d;
       
    24 
       
    25 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    26 	 * TODO - This is just a temporary stage, this will be removed. */
       
    27 	for (d = GetDepot(start_item); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) d->index = start_item++;
       
    28 }
       
    29 
       
    30 DEFINE_OLD_POOL(Depot, Depot, DepotPoolNewBlock, NULL)
       
    31 
       
    32 
    18 
    33 /**
    19 /**
    34  * Gets a depot from a tile
    20  * Gets a depot from a tile
    35  *
    21  *
    36  * @return Returns the depot if the tile had a depot, else it returns NULL
    22  * @return Returns the depot if the tile had a depot, else it returns NULL
    45 
    31 
    46 	return NULL;
    32 	return NULL;
    47 }
    33 }
    48 
    34 
    49 /**
    35 /**
    50  * Allocate a new depot
       
    51  */
       
    52 Depot *AllocateDepot()
       
    53 {
       
    54 	Depot *d;
       
    55 
       
    56 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    57 	 * TODO - This is just a temporary stage, this will be removed. */
       
    58 	for (d = GetDepot(0); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) {
       
    59 		if (!IsValidDepot(d)) {
       
    60 			DepotID index = d->index;
       
    61 
       
    62 			memset(d, 0, sizeof(Depot));
       
    63 			d->index = index;
       
    64 
       
    65 			return d;
       
    66 		}
       
    67 	}
       
    68 
       
    69 	/* Check if we can add a block to the pool */
       
    70 	if (AddBlockToPool(&_Depot_pool)) return AllocateDepot();
       
    71 
       
    72 	return NULL;
       
    73 }
       
    74 
       
    75 /**
       
    76  * Clean up a depot
    36  * Clean up a depot
    77  */
    37  */
    78 void DestroyDepot(Depot *depot)
    38 Depot::~Depot()
    79 {
    39 {
    80 	/* Clear the tile */
       
    81 	DoClearSquare(depot->xy);
       
    82 
       
    83 	/* Clear the depot from all order-lists */
    40 	/* Clear the depot from all order-lists */
    84 	RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, depot->index);
    41 	RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, this->index);
    85 
    42 
    86 	/* Delete the depot-window */
    43 	/* Delete the depot-window */
    87 	DeleteWindowById(WC_VEHICLE_DEPOT, depot->xy);
    44 	DeleteWindowById(WC_VEHICLE_DEPOT, this->xy);
       
    45 	this->xy = 0;
    88 }
    46 }
    89 
    47 
    90 void InitializeDepots()
    48 void InitializeDepots()
    91 {
    49 {
    92 	CleanPool(&_Depot_pool);
    50 	_Depot_pool.CleanPool();
    93 	AddBlockToPool(&_Depot_pool);
    51 	_Depot_pool.AddBlockToPool();
    94 }
    52 }
    95 
    53 
    96 
    54 
    97 static const SaveLoad _depot_desc[] = {
    55 static const SaveLoad _depot_desc[] = {
    98 	SLE_CONDVAR(Depot, xy,         SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
    56 	SLE_CONDVAR(Depot, xy,         SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
   114 static void Load_DEPT()
    72 static void Load_DEPT()
   115 {
    73 {
   116 	int index;
    74 	int index;
   117 
    75 
   118 	while ((index = SlIterateArray()) != -1) {
    76 	while ((index = SlIterateArray()) != -1) {
   119 		Depot *depot;
    77 		Depot *depot = new (index) Depot();
   120 
       
   121 		if (!AddBlockIfNeeded(&_Depot_pool, index))
       
   122 			error("Depots: failed loading savegame: too many depots");
       
   123 
       
   124 		depot = GetDepot(index);
       
   125 		SlObject(depot, _depot_desc);
    78 		SlObject(depot, _depot_desc);
   126 	}
    79 	}
   127 }
    80 }
   128 
    81 
   129 extern const ChunkHandler _depot_chunk_handlers[] = {
    82 extern const ChunkHandler _depot_chunk_handlers[] = {