Changeset - r3765:c9eaea3d3f78
[Not reviewed]
master
0 6 0
peter1138 - 18 years ago 2006-05-06 21:46:26
peter1138@openttd.org
(svn r4757) - Newstations: add saveload support for custom station speclists
6 files changed with 53 insertions and 2 deletions:
0 comments (0 inline, 0 general)
newgrf_station.c
Show inline comments
 
@@ -155,24 +155,41 @@ void SetCustomStationSpec(StationSpec *s
 
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
 
{
 
	assert(sclass < STAT_CLASS_MAX);
 
	if (station < station_classes[sclass].stations)
 
		return station_classes[sclass].spec[station];
 

	
 
	// If the custom station isn't defined any more, then the GRF file
 
	// probably was not loaded.
 
	return NULL;
 
}
 

	
 

	
 
const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx)
 
{
 
	StationClassID i;
 
	uint j;
 

	
 
	for (i = STAT_CLASS_DFLT; i < STAT_CLASS_MAX; i++) {
 
		for (j = 0; j < station_classes[i].stations; j++) {
 
			const StationSpec *statspec = station_classes[i].spec[j];
 
			if (statspec == NULL) continue;
 
			if (statspec->grfid == grfid && statspec->localidx == localidx) return statspec;
 
		}
 
	}
 

	
 
	return NULL;
 
}
 

	
 

	
 
/* Evaluate a tile's position within a station, and return the result a bitstuffed format.
 
 * if not centred: .TNLcCpP, if centred: .TNL..CP
 
 * T = Tile layout number (GetStationGfx), N = Number of platforms, L = Length of platforms
 
 * C = Current platform number from start, c = from end
 
 * P = Position along platform from start, p = from end
 
 * if centred, C/P start from the centre and c/p are not available.
 
 */
 
uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
 
{
 
	uint32 retval = 0;
 

	
 
	if (axis == AXIS_X) {
newgrf_station.h
Show inline comments
 
@@ -85,24 +85,25 @@ typedef struct StationClass {
 

	
 
void ResetStationClasses(void);
 
StationClassID AllocateStationClass(uint32 class);
 
void SetStationClassName(StationClassID sclass, StringID name);
 
StringID GetStationClassName(StationClassID sclass);
 
StringID *BuildStationClassDropdown(void);
 

	
 
uint GetNumStationClasses(void);
 
uint GetNumCustomStations(StationClassID sclass);
 

	
 
void SetCustomStationSpec(StationSpec *statspec);
 
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station);
 
const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx);
 

	
 
/* Get sprite offset for a given custom station and station structure (may be
 
 * NULL - that means we are in a build dialog). The station structure is used
 
 * for variational sprite groups. */
 
SpriteID GetCustomStationRelocation(const StationSpec *statspec, const Station *st, TileIndex tile);
 
uint16 GetStationCallback(uint16 callback, uint32 param1, uint32 param2, const StationSpec *statspec, const Station *st, TileIndex tile);
 

	
 
/* Allocate a StationSpec to a Station. This is called once per build operation. */
 
int AllocateSpecToStation(const StationSpec *statspec, Station *st, bool exec);
 

	
 
/* Deallocate a StationSpec from a Station. Called when removing a single station tile. */
 
bool DeallocateSpecFromStation(Station *st, byte specindex);
openttd.c
Show inline comments
 
@@ -1417,14 +1417,16 @@ bool AfterLoadGame(void)
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(26)) {
 
		Station *st;
 
		FOR_ALL_STATIONS(st) {
 
			st->last_vehicle_type = VEH_Invalid;
 
		}
 
	}
 

	
 
	FOR_ALL_PLAYERS(p) p->avail_railtypes = GetPlayerRailtypes(p->index);
 

	
 
	if (!CheckSavegameVersion(27)) AfterLoadStations();
 

	
 
	return true;
 
}
saveload.c
Show inline comments
 
@@ -21,25 +21,25 @@
 
#include "functions.h"
 
#include "hal.h"
 
#include "vehicle.h"
 
#include "station.h"
 
#include "thread.h"
 
#include "town.h"
 
#include "player.h"
 
#include "saveload.h"
 
#include "network.h"
 
#include "variables.h"
 
#include <setjmp.h>
 

	
 
const uint16 SAVEGAME_VERSION = 26;
 
const uint16 SAVEGAME_VERSION = 27;
 
uint16 _sl_version;       /// the major savegame version identifier
 
byte   _sl_minor_version; /// the minor savegame version, DO NOT USE!
 

	
 
typedef void WriterProc(uint len);
 
typedef uint ReaderProc(void);
 

	
 
typedef uint ReferenceToIntProc(const void *obj, SLRefType rt);
 
typedef void *IntToReferenceProc(uint index, SLRefType rt);
 

	
 
/** The saveload struct, containing reader-writer functions, bufffer, version, etc. */
 
static struct {
 
	bool save;                           /// are we doing a save or a load atm. True when saving
station.h
Show inline comments
 
@@ -185,24 +185,25 @@ static inline uint16 GetRoadStopPoolSize
 
	return _roadstop_pool.total_items;
 
}
 

	
 
#define FOR_ALL_ROADSTOPS_FROM(rs, start) for (rs = GetRoadStop(start); rs != NULL; rs = (rs->index + 1 < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1) : NULL)
 
#define FOR_ALL_ROADSTOPS(rs) FOR_ALL_ROADSTOPS_FROM(rs, 0)
 

	
 
/* End of stuff for ROADSTOPS */
 

	
 

	
 
VARDEF bool _station_sort_dirty[MAX_PLAYERS];
 
VARDEF bool _global_station_sort_dirty;
 

	
 
void AfterLoadStations(void);
 
void GetProductionAroundTiles(AcceptedCargo produced, TileIndex tile, int w, int h, int rad);
 
void GetAcceptanceAroundTiles(AcceptedCargo accepts, TileIndex tile, int w, int h, int rad);
 
uint GetStationPlatforms(const Station *st, TileIndex tile);
 

	
 

	
 
const DrawTileSprites *GetStationTileLayout(byte gfx);
 
void StationPickerDrawSprite(int x, int y, RailType railtype, int image);
 

	
 
RoadStop * GetRoadStopByTile(TileIndex tile, RoadStopType type);
 
RoadStop * GetPrimaryRoadStop(const Station *st, RoadStopType type);
 
uint GetNumRoadStops(const Station* st, RoadStopType type);
 
RoadStop * AllocateRoadStop( void );
station_cmd.c
Show inline comments
 
@@ -2812,24 +2812,40 @@ void InitializeStations(void)
 
	/* Clean the roadstop pool and create 1 block in it */
 
	CleanPool(&_roadstop_pool);
 
	AddBlockToPool(&_roadstop_pool);
 

	
 
	_station_tick_ctr = 0;
 

	
 
	// set stations to be sorted on load of savegame
 
	memset(_station_sort_dirty, true, sizeof(_station_sort_dirty));
 
	_global_station_sort_dirty = true; // load of savegame
 
}
 

	
 

	
 
void AfterLoadStations(void)
 
{
 
	Station *st;
 
	uint i;
 

	
 
	/* Update the speclists of all stations to point to the currently loaded custom stations. */
 
	FOR_ALL_STATIONS(st) {
 
		for (i = 0; i < st->num_specs; i++) {
 
			if (st->speclist[i].grfid == 0) continue;
 

	
 
			st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx);
 
		}
 
	}
 
}
 

	
 

	
 
const TileTypeProcs _tile_type_station_procs = {
 
	DrawTile_Station,           /* draw_tile_proc */
 
	GetSlopeZ_Station,          /* get_slope_z_proc */
 
	ClearTile_Station,          /* clear_tile_proc */
 
	GetAcceptedCargo_Station,   /* get_accepted_cargo_proc */
 
	GetTileDesc_Station,        /* get_tile_desc_proc */
 
	GetTileTrackStatus_Station, /* get_tile_track_status_proc */
 
	ClickTile_Station,          /* click_tile_proc */
 
	AnimateTile_Station,        /* animate_tile_proc */
 
	TileLoop_Station,           /* tile_loop_clear */
 
	ChangeTileOwner_Station,    /* change_tile_owner_clear */
 
	NULL,                       /* get_produced_cargo_proc */
 
@@ -2897,59 +2913,73 @@ static const SaveLoad _station_desc[] = 
 
	SLE_CONDVAR(Station,last_vehicle_type,          SLE_UINT8 , 26, SL_MAX_VERSION),
 

	
 
	// Was custom station class and id
 
	SLE_CONDNULL(2, 3, 25),
 
	SLE_CONDVAR(Station,build_date,        SLE_UINT16, 3, SL_MAX_VERSION),
 

	
 
	SLE_CONDREF(Station,bus_stops,         REF_ROADSTOPS, 6, SL_MAX_VERSION),
 
	SLE_CONDREF(Station,truck_stops,       REF_ROADSTOPS, 6, SL_MAX_VERSION),
 

	
 
	/* Used by newstations for graphic variations */
 
	SLE_CONDVAR(Station,random_bits,       SLE_UINT16, 27, SL_MAX_VERSION),
 
	SLE_CONDVAR(Station,waiting_triggers,  SLE_UINT8,  27, SL_MAX_VERSION),
 
	SLE_CONDVAR(Station,num_specs,         SLE_UINT8,  27, SL_MAX_VERSION),
 

	
 
	// reserve extra space in savegame here. (currently 32 bytes)
 
	SLE_CONDNULL(32, 2, SL_MAX_VERSION),
 

	
 
	SLE_END()
 
};
 

	
 
static const SaveLoad _goods_desc[] = {
 
	SLE_VAR(GoodsEntry,waiting_acceptance, SLE_UINT16),
 
	SLE_VAR(GoodsEntry,days_since_pickup,  SLE_UINT8),
 
	SLE_VAR(GoodsEntry,rating,             SLE_UINT8),
 
	SLE_CONDVAR(GoodsEntry,enroute_from,   SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
 
	SLE_CONDVAR(GoodsEntry,enroute_from,   SLE_UINT16, 7, SL_MAX_VERSION),
 
	SLE_VAR(GoodsEntry,enroute_time,       SLE_UINT8),
 
	SLE_VAR(GoodsEntry,last_speed,         SLE_UINT8),
 
	SLE_VAR(GoodsEntry,last_age,           SLE_UINT8),
 
	SLE_CONDVAR(GoodsEntry,feeder_profit,  SLE_INT32, 14, SL_MAX_VERSION),
 

	
 
	SLE_END()
 
};
 

	
 
static const SaveLoad _station_speclist_desc[] = {
 
	SLE_CONDVAR(StationSpecList, grfid,    SLE_UINT32, 27, SL_MAX_VERSION),
 
	SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8,  27, SL_MAX_VERSION),
 

	
 
	SLE_END()
 
};
 

	
 

	
 
static void SaveLoad_STNS(Station *st)
 
{
 
	int i;
 
	uint i;
 

	
 
	SlObject(st, _station_desc);
 
	for (i = 0; i != NUM_CARGO; i++) {
 
		SlObject(&st->goods[i], _goods_desc);
 

	
 
		/* In older versions, enroute_from had 0xFF as INVALID_STATION, is now 0xFFFF */
 
		if (CheckSavegameVersion(7) && st->goods[i].enroute_from == 0xFF) {
 
			st->goods[i].enroute_from = INVALID_STATION;
 
		}
 
	}
 

	
 
	if (st->num_specs != 0) {
 
		/* Allocate speclist memory when loading a game */
 
		if (st->speclist == NULL) st->speclist = calloc(st->num_specs, sizeof(*st->speclist));
 
		for (i = 0; i < st->num_specs; i++) SlObject(&st->speclist[i], _station_speclist_desc);
 
	}
 
}
 

	
 
static void Save_STNS(void)
 
{
 
	Station *st;
 
	// Write the stations
 
	FOR_ALL_STATIONS(st) {
 
		if (st->xy != 0) {
 
			SlSetArrayIndex(st->index);
 
			SlAutolength((AutolengthProc*)SaveLoad_STNS, st);
 
		}
 
	}
0 comments (0 inline, 0 general)