Changeset - r20251:8c2b509af318
[Not reviewed]
master
0 8 1
rubidium - 11 years ago 2013-05-19 14:17:02
rubidium@openttd.org
(svn r25258) -Codechange: save linkgraph (fonsinchen)
9 files changed with 159 insertions and 14 deletions:
0 comments (0 inline, 0 general)
projects/openttd_vs100.vcxproj
Show inline comments
 
@@ -808,12 +808,13 @@
 
    <ClCompile Include="..\src\saveload\game_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\gamelog_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\goal_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\group_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\industry_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\labelmaps_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\linkgraph_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\map_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\misc_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\newgrf_sl.cpp" />
 
    <ClInclude Include="..\src\saveload\newgrf_sl.h" />
 
    <ClCompile Include="..\src\saveload\object_sl.cpp" />
 
    <ClCompile Include="..\src\saveload\oldloader.cpp" />
projects/openttd_vs100.vcxproj.filters
Show inline comments
 
@@ -1659,12 +1659,15 @@
 
    <ClCompile Include="..\src\saveload\industry_sl.cpp">
 
      <Filter>Save/Load handlers</Filter>
 
    </ClCompile>
 
    <ClCompile Include="..\src\saveload\labelmaps_sl.cpp">
 
      <Filter>Save/Load handlers</Filter>
 
    </ClCompile>
 
    <ClCompile Include="..\src\saveload\linkgraph_sl.cpp">
 
      <Filter>Save/Load handlers</Filter>
 
    </ClCompile>
 
    <ClCompile Include="..\src\saveload\map_sl.cpp">
 
      <Filter>Save/Load handlers</Filter>
 
    </ClCompile>
 
    <ClCompile Include="..\src\saveload\misc_sl.cpp">
 
      <Filter>Save/Load handlers</Filter>
 
    </ClCompile>
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -2536,12 +2536,16 @@
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\labelmaps_sl.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\linkgraph_sl.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\map_sl.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\misc_sl.cpp"
 
				>
projects/openttd_vs90.vcproj
Show inline comments
 
@@ -2533,12 +2533,16 @@
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\labelmaps_sl.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\linkgraph_sl.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\map_sl.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\saveload\misc_sl.cpp"
 
				>
source.list
Show inline comments
 
@@ -567,12 +567,13 @@ saveload/engine_sl.cpp
 
saveload/game_sl.cpp
 
saveload/gamelog_sl.cpp
 
saveload/goal_sl.cpp
 
saveload/group_sl.cpp
 
saveload/industry_sl.cpp
 
saveload/labelmaps_sl.cpp
 
saveload/linkgraph_sl.cpp
 
saveload/map_sl.cpp
 
saveload/misc_sl.cpp
 
saveload/newgrf_sl.cpp
 
saveload/newgrf_sl.h
 
saveload/object_sl.cpp
 
saveload/oldloader.cpp
src/saveload/linkgraph_sl.cpp
Show inline comments
 
new file 100644
 
/* $Id$ */
 

	
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file linkgraph_sl.cpp Code handling saving and loading of link graphs */
 

	
 
#include "../stdafx.h"
 
#include "../linkgraph/linkgraph.h"
 
#include "../settings_internal.h"
 
#include "saveload.h"
 

	
 
typedef LinkGraph::BaseNode Node;
 
typedef LinkGraph::BaseEdge Edge;
 

	
 
static uint _num_nodes;
 

	
 
/**
 
 * Get a SaveLoad array for a link graph.
 
 * @return SaveLoad array for link graph.
 
 */
 
const SaveLoad *GetLinkGraphDesc()
 
{
 
	static const SaveLoad link_graph_desc[] = {
 
		 SLE_VAR(LinkGraph, last_compression, SLE_UINT32),
 
		SLEG_VAR(_num_nodes,                  SLE_UINT16),
 
		 SLE_VAR(LinkGraph, cargo,            SLE_UINT8),
 
		 SLE_END()
 
	};
 
	return link_graph_desc;
 
}
 

	
 
/* Edges and nodes are saved in the correct order, so we don't need to save their IDs. */
 

	
 
/**
 
 * SaveLoad desc for a link graph node.
 
 */
 
static const SaveLoad _node_desc[] = {
 
	SLE_VAR(Node, supply,      SLE_UINT32),
 
	SLE_VAR(Node, demand,      SLE_UINT32),
 
	SLE_VAR(Node, station,     SLE_UINT16),
 
	SLE_VAR(Node, last_update, SLE_UINT32),
 
	SLE_END()
 
};
 

	
 
/**
 
 * SaveLoad desc for a link graph edge.
 
 */
 
static const SaveLoad _edge_desc[] = {
 
	SLE_VAR(Edge, distance,    SLE_UINT32),
 
	SLE_VAR(Edge, capacity,    SLE_UINT32),
 
	SLE_VAR(Edge, usage,       SLE_UINT32),
 
	SLE_VAR(Edge, last_update, SLE_UINT32),
 
	SLE_VAR(Edge, next_edge,   SLE_UINT16),
 
	SLE_END()
 
};
 

	
 
/**
 
 * Save/load a link graph.
 
 * @param comp Link graph to be saved or loaded.
 
 */
 
void SaveLoad_LinkGraph(LinkGraph &lg)
 
{
 
	uint size = lg.Size();
 
	for (NodeID from = 0; from < size; ++from) {
 
		Node *node = &lg.nodes[from];
 
		SlObject(node, _node_desc);
 
		for (NodeID to = 0; to < size; ++to) {
 
			SlObject(&lg.edges[from][to], _edge_desc);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Save a link graph.
 
 * @param lg LinkGraph to be saved.
 
 */
 
static void DoSave_LGRP(LinkGraph *lg)
 
{
 
	_num_nodes = lg->Size();
 
	SlObject(lg, GetLinkGraphDesc());
 
	SaveLoad_LinkGraph(*lg);
 
}
 

	
 
/**
 
 * Load all link graphs.
 
 */
 
static void Load_LGRP()
 
{
 
	int index;
 
	while ((index = SlIterateArray()) != -1) {
 
		if (!LinkGraph::CanAllocateItem()) {
 
			/* Impossible as they have been present in previous game. */
 
			NOT_REACHED();
 
		}
 
		LinkGraph *lg = new (index) LinkGraph();
 
		SlObject(lg, GetLinkGraphDesc());
 
		lg->Init(_num_nodes);
 
		SaveLoad_LinkGraph(*lg);
 
	}
 
}
 

	
 
/**
 
 * Save all link graphs.
 
 */
 
static void Save_LGRP()
 
{
 
	LinkGraph *lg;
 
	FOR_ALL_LINK_GRAPHS(lg) {
 
		SlSetArrayIndex(lg->index);
 
		SlAutolength((AutolengthProc*)DoSave_LGRP, lg);
 
	}
 
}
 

	
 
extern const ChunkHandler _linkgraph_chunk_handlers[] = {
 
	{ 'LGRP', Save_LGRP, Load_LGRP, NULL,      NULL, CH_ARRAY | CH_LAST }
 
};
src/saveload/saveload.cpp
Show inline comments
 
@@ -32,12 +32,13 @@
 
#include "../core/endian_func.hpp"
 
#include "../vehicle_base.h"
 
#include "../company_func.h"
 
#include "../date_func.h"
 
#include "../autoreplace_base.h"
 
#include "../roadstop_base.h"
 
#include "../linkgraph/linkgraph.h"
 
#include "../statusbar_gui.h"
 
#include "../fileio_func.h"
 
#include "../gamelog.h"
 
#include "../string_func.h"
 
#include "../fios.h"
 
#include "../error.h"
 
@@ -422,12 +423,13 @@ extern const ChunkHandler _game_chunk_ha
 
extern const ChunkHandler _animated_tile_chunk_handlers[];
 
extern const ChunkHandler _newgrf_chunk_handlers[];
 
extern const ChunkHandler _group_chunk_handlers[];
 
extern const ChunkHandler _cargopacket_chunk_handlers[];
 
extern const ChunkHandler _autoreplace_chunk_handlers[];
 
extern const ChunkHandler _labelmaps_chunk_handlers[];
 
extern const ChunkHandler _linkgraph_chunk_handlers[];
 
extern const ChunkHandler _airport_chunk_handlers[];
 
extern const ChunkHandler _object_chunk_handlers[];
 
extern const ChunkHandler _persistent_storage_chunk_handlers[];
 

	
 
/** Array of all chunks in a savegame, \c NULL terminated. */
 
static const ChunkHandler * const _chunk_handlers[] = {
 
@@ -456,12 +458,13 @@ static const ChunkHandler * const _chunk
 
	_animated_tile_chunk_handlers,
 
	_newgrf_chunk_handlers,
 
	_group_chunk_handlers,
 
	_cargopacket_chunk_handlers,
 
	_autoreplace_chunk_handlers,
 
	_labelmaps_chunk_handlers,
 
	_linkgraph_chunk_handlers,
 
	_airport_chunk_handlers,
 
	_object_chunk_handlers,
 
	_persistent_storage_chunk_handlers,
 
	NULL,
 
};
 

	
 
@@ -1209,16 +1212,17 @@ static size_t ReferenceToInt(const void 
 
		case REF_VEHICLE_OLD: // Old vehicles we save as new ones
 
		case REF_VEHICLE:   return ((const  Vehicle*)obj)->index + 1;
 
		case REF_STATION:   return ((const  Station*)obj)->index + 1;
 
		case REF_TOWN:      return ((const     Town*)obj)->index + 1;
 
		case REF_ORDER:     return ((const    Order*)obj)->index + 1;
 
		case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
 
		case REF_ENGINE_RENEWS: return ((const       EngineRenew*)obj)->index + 1;
 
		case REF_CARGO_PACKET:  return ((const       CargoPacket*)obj)->index + 1;
 
		case REF_ORDERLIST:     return ((const         OrderList*)obj)->index + 1;
 
		case REF_STORAGE:       return ((const PersistentStorage*)obj)->index + 1;
 
		case REF_ENGINE_RENEWS:  return ((const       EngineRenew*)obj)->index + 1;
 
		case REF_CARGO_PACKET:   return ((const       CargoPacket*)obj)->index + 1;
 
		case REF_ORDERLIST:      return ((const         OrderList*)obj)->index + 1;
 
		case REF_STORAGE:        return ((const PersistentStorage*)obj)->index + 1;
 
		case REF_LINK_GRAPH:     return ((const         LinkGraph*)obj)->index + 1;
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Pointers cannot be loaded from a savegame, so this function
 
@@ -1286,12 +1290,16 @@ static void *IntToReference(size_t index
 
			SlErrorCorrupt("Referencing invalid CargoPacket");
 

	
 
		case REF_STORAGE:
 
			if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
 
			SlErrorCorrupt("Referencing invalid PersistentStorage");
 

	
 
		case REF_LINK_GRAPH:
 
			if (LinkGraph::IsValidID(index)) return LinkGraph::Get(index);
 
			SlErrorCorrupt("Referencing invalid LinkGraph");
 

	
 
		default: NOT_REACHED();
 
	}
 
}
 

	
 
/**
 
 * Return the size in bytes of a list
src/saveload/saveload.h
Show inline comments
 
@@ -70,22 +70,24 @@ struct ChunkHandler {
 
struct NullStruct {
 
	byte null;
 
};
 

	
 
/** Type of reference (#SLE_REF, #SLE_CONDREF). */
 
enum SLRefType {
 
	REF_ORDER         = 0, ///< Load/save a reference to an order.
 
	REF_VEHICLE       = 1, ///< Load/save a reference to a vehicle.
 
	REF_STATION       = 2, ///< Load/save a reference to a station.
 
	REF_TOWN          = 3, ///< Load/save a reference to a town.
 
	REF_VEHICLE_OLD   = 4, ///< Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
 
	REF_ROADSTOPS     = 5, ///< Load/save a reference to a bus/truck stop.
 
	REF_ENGINE_RENEWS = 6, ///< Load/save a reference to an engine renewal (autoreplace).
 
	REF_CARGO_PACKET  = 7, ///< Load/save a reference to a cargo packet.
 
	REF_ORDERLIST     = 8, ///< Load/save a reference to an orderlist.
 
	REF_STORAGE       = 9, ///< Load/save a reference to a persistent storage.
 
	REF_ORDER          =  0, ///< Load/save a reference to an order.
 
	REF_VEHICLE        =  1, ///< Load/save a reference to a vehicle.
 
	REF_STATION        =  2, ///< Load/save a reference to a station.
 
	REF_TOWN           =  3, ///< Load/save a reference to a town.
 
	REF_VEHICLE_OLD    =  4, ///< Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
 
	REF_ROADSTOPS      =  5, ///< Load/save a reference to a bus/truck stop.
 
	REF_ENGINE_RENEWS  =  6, ///< Load/save a reference to an engine renewal (autoreplace).
 
	REF_CARGO_PACKET   =  7, ///< Load/save a reference to a cargo packet.
 
	REF_ORDERLIST      =  8, ///< Load/save a reference to an orderlist.
 
	REF_STORAGE        =  9, ///< Load/save a reference to a persistent storage.
 
	REF_LINK_GRAPH     = 10, ///< Load/save a reference to a link graph.
 
	REF_LINK_GRAPH_JOB = 11, ///< Load/save a reference to a link graph job.
 
};
 

	
 
/** Highest possible savegame version. */
 
#define SL_MAX_VERSION 255
 

	
 
/** Flags of a chunk. */
src/saveload/saveload_internal.h
Show inline comments
 
@@ -28,12 +28,13 @@ const SaveLoad *GetBaseStationDescriptio
 

	
 
void AfterLoadVehicles(bool part_of_load);
 
void FixupTrainLengths();
 
void AfterLoadStations();
 
void AfterLoadRoadStops();
 
void AfterLoadLabelMaps();
 
void AfterLoadLinkGraphs();
 
void AfterLoadCompanyStats();
 
void UpdateHousesAndTowns();
 

	
 
void UpdateOldAircraft();
 

	
 
void SaveViewportBeforeSaveGame();
0 comments (0 inline, 0 general)