tron@2186: /* $Id$ */ tron@2186: truelight@1313: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@1313: #include "depot.h" tron@2163: #include "functions.h" truelight@1313: #include "tile.h" truelight@1313: #include "map.h" truelight@1313: #include "table/strings.h" truelight@1313: #include "saveload.h" truelight@1313: #include "order.h" truelight@1313: truelight@1313: truelight@1313: /** truelight@1313: * Called if a new block is added to the depot-pool truelight@1313: */ truelight@1313: static void DepotPoolNewBlock(uint start_item) truelight@1313: { truelight@4346: Depot *d; truelight@1313: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4973: for (d = GetDepot(start_item); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) d->index = start_item++; truelight@1313: } truelight@1313: matthijs@5216: DEFINE_OLD_POOL(Depot, Depot, DepotPoolNewBlock, NULL) truelight@1313: truelight@1313: truelight@1313: /** truelight@1313: * Gets a depot from a tile truelight@1313: * truelight@1313: * @return Returns the depot if the tile had a depot, else it returns NULL truelight@1313: */ tron@1977: Depot *GetDepotByTile(TileIndex tile) truelight@1313: { truelight@1313: Depot *depot; truelight@1313: truelight@1313: FOR_ALL_DEPOTS(depot) { tron@4077: if (depot->xy == tile) return depot; truelight@1313: } truelight@1313: truelight@1313: return NULL; truelight@1313: } truelight@1313: truelight@1313: /** truelight@1313: * Allocate a new depot truelight@1313: */ truelight@1313: Depot *AllocateDepot(void) truelight@1313: { truelight@4346: Depot *d; truelight@1313: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4973: for (d = GetDepot(0); d != NULL; d = (d->index + 1U < GetDepotPoolSize()) ? GetDepot(d->index + 1U) : NULL) { truelight@4346: if (!IsValidDepot(d)) { truelight@4388: DepotID index = d->index; truelight@1313: truelight@4346: memset(d, 0, sizeof(Depot)); truelight@4346: d->index = index; truelight@1313: truelight@4346: return d; truelight@1313: } truelight@1313: } truelight@1313: truelight@1313: /* Check if we can add a block to the pool */ tron@4973: if (AddBlockToPool(&_Depot_pool)) return AllocateDepot(); truelight@1313: truelight@1313: return NULL; truelight@1313: } truelight@1313: truelight@1313: /** truelight@4388: * Clean up a depot truelight@1313: */ truelight@4388: void DestroyDepot(Depot *depot) truelight@1313: { truelight@1313: /* Clear the tile */ truelight@4388: DoClearSquare(depot->xy); truelight@1313: truelight@1313: /* Clear the depot from all order-lists */ tron@4527: RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, depot->index); truelight@1313: truelight@1313: /* Delete the depot-window */ truelight@4388: DeleteWindowById(WC_VEHICLE_DEPOT, depot->xy); truelight@1313: } truelight@1313: truelight@4347: void InitializeDepots(void) truelight@1313: { tron@4973: CleanPool(&_Depot_pool); tron@4973: AddBlockToPool(&_Depot_pool); truelight@1313: } truelight@1313: truelight@1313: Darkvater@1881: static const SaveLoad _depot_desc[] = { rubidium@4344: SLE_CONDVAR(Depot, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Depot, xy, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@4344: SLE_VAR(Depot, town_index, SLE_UINT16), truelight@1313: SLE_END() truelight@1313: }; truelight@1313: truelight@1313: static void Save_DEPT(void) truelight@1313: { truelight@1313: Depot *depot; truelight@1313: truelight@1313: FOR_ALL_DEPOTS(depot) { truelight@4346: SlSetArrayIndex(depot->index); truelight@4346: SlObject(depot, _depot_desc); truelight@1313: } truelight@1313: } truelight@1313: truelight@1313: static void Load_DEPT(void) truelight@1313: { truelight@1313: int index; truelight@1313: truelight@1313: while ((index = SlIterateArray()) != -1) { truelight@1313: Depot *depot; truelight@1313: tron@4973: if (!AddBlockIfNeeded(&_Depot_pool, index)) truelight@1313: error("Depots: failed loading savegame: too many depots"); truelight@1313: truelight@1313: depot = GetDepot(index); truelight@1313: SlObject(depot, _depot_desc); truelight@1313: } truelight@1313: } truelight@1313: truelight@1313: const ChunkHandler _depot_chunk_handlers[] = { truelight@1313: { 'DEPT', Save_DEPT, Load_DEPT, CH_ARRAY | CH_LAST}, truelight@1313: };