src/depot.cpp
changeset 5835 e0ff603ae0b7
parent 5726 8f399788f6c9
child 5650 aefc131bf5ce
equal deleted inserted replaced
5834:7bf92d5a5a0f 5835:e0ff603ae0b7
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "depot.h"
       
     6 #include "functions.h"
       
     7 #include "tile.h"
       
     8 #include "map.h"
       
     9 #include "table/strings.h"
       
    10 #include "saveload.h"
       
    11 #include "order.h"
       
    12 
       
    13 
       
    14 /**
       
    15  * Called if a new block is added to the depot-pool
       
    16  */
       
    17 static void DepotPoolNewBlock(uint start_item)
       
    18 {
       
    19 	Depot *d;
       
    20 
       
    21 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    22 	 * TODO - This is just a temporary stage, this will be removed. */
       
    23 	for (d = GetDepot(start_item); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) d->index = start_item++;
       
    24 }
       
    25 
       
    26 DEFINE_OLD_POOL(Depot, Depot, DepotPoolNewBlock, NULL)
       
    27 
       
    28 
       
    29 /**
       
    30  * Gets a depot from a tile
       
    31  *
       
    32  * @return Returns the depot if the tile had a depot, else it returns NULL
       
    33  */
       
    34 Depot *GetDepotByTile(TileIndex tile)
       
    35 {
       
    36 	Depot *depot;
       
    37 
       
    38 	FOR_ALL_DEPOTS(depot) {
       
    39 		if (depot->xy == tile) return depot;
       
    40 	}
       
    41 
       
    42 	return NULL;
       
    43 }
       
    44 
       
    45 /**
       
    46  * Allocate a new depot
       
    47  */
       
    48 Depot *AllocateDepot(void)
       
    49 {
       
    50 	Depot *d;
       
    51 
       
    52 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    53 	 * TODO - This is just a temporary stage, this will be removed. */
       
    54 	for (d = GetDepot(0); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) {
       
    55 		if (!IsValidDepot(d)) {
       
    56 			DepotID index = d->index;
       
    57 
       
    58 			memset(d, 0, sizeof(Depot));
       
    59 			d->index = index;
       
    60 
       
    61 			return d;
       
    62 		}
       
    63 	}
       
    64 
       
    65 	/* Check if we can add a block to the pool */
       
    66 	if (AddBlockToPool(&_Depot_pool)) return AllocateDepot();
       
    67 
       
    68 	return NULL;
       
    69 }
       
    70 
       
    71 /**
       
    72  * Clean up a depot
       
    73  */
       
    74 void DestroyDepot(Depot *depot)
       
    75 {
       
    76 	/* Clear the tile */
       
    77 	DoClearSquare(depot->xy);
       
    78 
       
    79 	/* Clear the depot from all order-lists */
       
    80 	RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, depot->index);
       
    81 
       
    82 	/* Delete the depot-window */
       
    83 	DeleteWindowById(WC_VEHICLE_DEPOT, depot->xy);
       
    84 }
       
    85 
       
    86 void InitializeDepots(void)
       
    87 {
       
    88 	CleanPool(&_Depot_pool);
       
    89 	AddBlockToPool(&_Depot_pool);
       
    90 }
       
    91 
       
    92 
       
    93 static const SaveLoad _depot_desc[] = {
       
    94 	SLE_CONDVAR(Depot, xy,         SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
       
    95 	SLE_CONDVAR(Depot, xy,         SLE_UINT32,                 6, SL_MAX_VERSION),
       
    96 	    SLE_VAR(Depot, town_index, SLE_UINT16),
       
    97 	SLE_END()
       
    98 };
       
    99 
       
   100 static void Save_DEPT(void)
       
   101 {
       
   102 	Depot *depot;
       
   103 
       
   104 	FOR_ALL_DEPOTS(depot) {
       
   105 		SlSetArrayIndex(depot->index);
       
   106 		SlObject(depot, _depot_desc);
       
   107 	}
       
   108 }
       
   109 
       
   110 static void Load_DEPT(void)
       
   111 {
       
   112 	int index;
       
   113 
       
   114 	while ((index = SlIterateArray()) != -1) {
       
   115 		Depot *depot;
       
   116 
       
   117 		if (!AddBlockIfNeeded(&_Depot_pool, index))
       
   118 			error("Depots: failed loading savegame: too many depots");
       
   119 
       
   120 		depot = GetDepot(index);
       
   121 		SlObject(depot, _depot_desc);
       
   122 	}
       
   123 }
       
   124 
       
   125 const ChunkHandler _depot_chunk_handlers[] = {
       
   126 	{ 'DEPT', Save_DEPT, Load_DEPT, CH_ARRAY | CH_LAST},
       
   127 };