2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "tile.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "table/strings.h"
|
|
|
|
#include "saveload.h"
|
|
|
|
#include "order.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
/* Max depots: 64000 (8 * 8000) */
|
|
|
|
DEPOT_POOL_BLOCK_SIZE_BITS = 3, /* In bits, so (1 << 3) == 8 */
|
|
|
|
DEPOT_POOL_MAX_BLOCKS = 8000,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called if a new block is added to the depot-pool
|
|
|
|
*/
|
|
|
|
static void DepotPoolNewBlock(uint start_item)
|
|
|
|
{
|
2006-08-22 15:33:35 +00:00
|
|
|
Depot *d;
|
2005-02-06 10:18:47 +00:00
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
|
|
|
for (d = GetDepot(start_item); d != NULL; d = (d->index + 1 < GetDepotPoolSize()) ? GetDepot(d->index + 1) : NULL) d->index = start_item++;
|
2005-02-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the town-pool */
|
2006-04-18 18:48:50 +00:00
|
|
|
MemoryPool _depot_pool = { "Depots", DEPOT_POOL_MAX_BLOCKS, DEPOT_POOL_BLOCK_SIZE_BITS, sizeof(Depot), &DepotPoolNewBlock, NULL, 0, 0, NULL };
|
2005-02-06 10:18:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a depot from a tile
|
|
|
|
*
|
|
|
|
* @return Returns the depot if the tile had a depot, else it returns NULL
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
Depot *GetDepotByTile(TileIndex tile)
|
2005-02-06 10:18:47 +00:00
|
|
|
{
|
|
|
|
Depot *depot;
|
|
|
|
|
|
|
|
FOR_ALL_DEPOTS(depot) {
|
2006-06-27 21:25:53 +00:00
|
|
|
if (depot->xy == tile) return depot;
|
2005-02-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a new depot
|
|
|
|
*/
|
|
|
|
Depot *AllocateDepot(void)
|
|
|
|
{
|
2006-08-22 15:33:35 +00:00
|
|
|
Depot *d;
|
2005-02-06 10:18:47 +00:00
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
|
|
|
for (d = GetDepot(0); d != NULL; d = (d->index + 1 < GetDepotPoolSize()) ? GetDepot(d->index + 1) : NULL) {
|
|
|
|
if (!IsValidDepot(d)) {
|
2006-08-26 14:44:55 +00:00
|
|
|
DepotID index = d->index;
|
2005-02-06 10:18:47 +00:00
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
memset(d, 0, sizeof(Depot));
|
|
|
|
d->index = index;
|
2005-02-06 10:18:47 +00:00
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
return d;
|
2005-02-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we can add a block to the pool */
|
2006-06-27 21:25:53 +00:00
|
|
|
if (AddBlockToPool(&_depot_pool)) return AllocateDepot();
|
2005-02-06 10:18:47 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-08-26 14:44:55 +00:00
|
|
|
* Clean up a depot
|
2005-02-06 10:18:47 +00:00
|
|
|
*/
|
2006-08-26 14:44:55 +00:00
|
|
|
void DestroyDepot(Depot *depot)
|
2005-02-06 10:18:47 +00:00
|
|
|
{
|
|
|
|
/* Clear the tile */
|
2006-08-26 14:44:55 +00:00
|
|
|
DoClearSquare(depot->xy);
|
2005-02-06 10:18:47 +00:00
|
|
|
|
|
|
|
/* Clear the depot from all order-lists */
|
2006-09-03 08:25:27 +00:00
|
|
|
RemoveOrderFromAllVehicles(OT_GOTO_DEPOT, depot->index);
|
2005-02-06 10:18:47 +00:00
|
|
|
|
|
|
|
/* Delete the depot-window */
|
2006-08-26 14:44:55 +00:00
|
|
|
DeleteWindowById(WC_VEHICLE_DEPOT, depot->xy);
|
2005-02-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 15:39:22 +00:00
|
|
|
void InitializeDepots(void)
|
2005-02-06 10:18:47 +00:00
|
|
|
{
|
|
|
|
CleanPool(&_depot_pool);
|
|
|
|
AddBlockToPool(&_depot_pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _depot_desc[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(Depot, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Depot, xy, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Depot, town_index, SLE_UINT16),
|
2005-02-06 10:18:47 +00:00
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void Save_DEPT(void)
|
|
|
|
{
|
|
|
|
Depot *depot;
|
|
|
|
|
|
|
|
FOR_ALL_DEPOTS(depot) {
|
2006-08-22 15:33:35 +00:00
|
|
|
SlSetArrayIndex(depot->index);
|
|
|
|
SlObject(depot, _depot_desc);
|
2005-02-06 10:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Load_DEPT(void)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
|
|
|
Depot *depot;
|
|
|
|
|
|
|
|
if (!AddBlockIfNeeded(&_depot_pool, index))
|
|
|
|
error("Depots: failed loading savegame: too many depots");
|
|
|
|
|
|
|
|
depot = GetDepot(index);
|
|
|
|
SlObject(depot, _depot_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChunkHandler _depot_chunk_handlers[] = {
|
|
|
|
{ 'DEPT', Save_DEPT, Load_DEPT, CH_ARRAY | CH_LAST},
|
|
|
|
};
|