2009-01-04 15:32:25 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
/** @file engine_sl.cpp Code handling saving and loading of engines */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "saveload_internal.h"
|
|
|
|
#include "../engine_base.h"
|
2014-04-25 15:40:32 +00:00
|
|
|
#include "../string_func.h"
|
2014-09-13 14:46:03 +00:00
|
|
|
#include <vector>
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
static const SaveLoad _engine_desc[] = {
|
2009-06-17 17:13:30 +00:00
|
|
|
SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Engine, intro_date, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Engine, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Engine, age, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Engine, reliability, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_start, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_max, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_final, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, duration_phase_1, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, duration_phase_2, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, duration_phase_3, SLE_UINT16),
|
|
|
|
|
|
|
|
SLE_CONDNULL(1, 0, 120),
|
|
|
|
SLE_VAR(Engine, flags, SLE_UINT8),
|
2012-12-09 16:55:03 +00:00
|
|
|
SLE_CONDNULL(1, 0, 178), // old preview_company_rank
|
|
|
|
SLE_CONDVAR(Engine, preview_asked, SLE_UINT16, 179, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Engine, preview_company, SLE_UINT8, 179, SL_MAX_VERSION),
|
2009-06-17 17:13:30 +00:00
|
|
|
SLE_VAR(Engine, preview_wait, SLE_UINT8),
|
|
|
|
SLE_CONDNULL(1, 0, 44),
|
|
|
|
SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, 0, 103),
|
|
|
|
SLE_CONDVAR(Engine, company_avail, SLE_UINT16, 104, SL_MAX_VERSION),
|
2014-09-07 16:12:58 +00:00
|
|
|
SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, 193, SL_MAX_VERSION),
|
2009-06-17 17:13:30 +00:00
|
|
|
SLE_CONDSTR(Engine, name, SLE_STR, 0, 84, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2010-08-02 18:24:09 +00:00
|
|
|
SLE_CONDNULL(16, 2, 143), // old reserved space
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2015-04-23 20:07:07 +00:00
|
|
|
static std::vector<Engine*> _temp_engine;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate an Engine structure, but not using the pools.
|
|
|
|
* The allocated Engine must be freed using FreeEngine;
|
|
|
|
* @return Allocated engine.
|
|
|
|
*/
|
|
|
|
static Engine* CallocEngine()
|
|
|
|
{
|
|
|
|
uint8 *zero = CallocT<uint8>(sizeof(Engine));
|
|
|
|
Engine *engine = new (zero) Engine();
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deallocate an Engine constructed by CallocEngine.
|
|
|
|
* @param e Engine to free.
|
|
|
|
*/
|
|
|
|
static void FreeEngine(Engine *e)
|
|
|
|
{
|
|
|
|
if (e != NULL) {
|
|
|
|
e->~Engine();
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
Engine *GetTempDataEngine(EngineID index)
|
|
|
|
{
|
2014-09-13 14:46:03 +00:00
|
|
|
if (index < _temp_engine.size()) {
|
2015-04-23 20:07:07 +00:00
|
|
|
return _temp_engine[index];
|
2014-09-13 14:46:03 +00:00
|
|
|
} else if (index == _temp_engine.size()) {
|
2015-04-23 20:07:07 +00:00
|
|
|
_temp_engine.push_back(CallocEngine());
|
|
|
|
return _temp_engine[index];
|
2014-09-13 14:46:03 +00:00
|
|
|
} else {
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Save_ENGN()
|
|
|
|
{
|
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
SlSetArrayIndex(e->index);
|
|
|
|
SlObject(e, _engine_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Load_ENGN()
|
|
|
|
{
|
|
|
|
/* As engine data is loaded before engines are initialized we need to load
|
|
|
|
* this information into a temporary array. This is then copied into the
|
|
|
|
* engine pool after processing NewGRFs by CopyTempEngineData(). */
|
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
|
|
|
Engine *e = GetTempDataEngine(index);
|
|
|
|
SlObject(e, _engine_desc);
|
2012-12-09 16:55:03 +00:00
|
|
|
|
|
|
|
if (IsSavegameVersionBefore(179)) {
|
|
|
|
/* preview_company_rank was replaced with preview_company and preview_asked.
|
|
|
|
* Just cancel any previews. */
|
|
|
|
e->flags &= ~4; // ENGINE_OFFER_WINDOW_OPEN
|
|
|
|
e->preview_company = INVALID_COMPANY;
|
|
|
|
e->preview_asked = (CompanyMask)-1;
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy data from temporary engine array into the real engine pool.
|
|
|
|
*/
|
|
|
|
void CopyTempEngineData()
|
|
|
|
{
|
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
if (e->index >= _temp_engine.size()) break;
|
|
|
|
|
|
|
|
const Engine *se = GetTempDataEngine(e->index);
|
|
|
|
e->intro_date = se->intro_date;
|
|
|
|
e->age = se->age;
|
|
|
|
e->reliability = se->reliability;
|
|
|
|
e->reliability_spd_dec = se->reliability_spd_dec;
|
|
|
|
e->reliability_start = se->reliability_start;
|
|
|
|
e->reliability_max = se->reliability_max;
|
|
|
|
e->reliability_final = se->reliability_final;
|
|
|
|
e->duration_phase_1 = se->duration_phase_1;
|
|
|
|
e->duration_phase_2 = se->duration_phase_2;
|
|
|
|
e->duration_phase_3 = se->duration_phase_3;
|
|
|
|
e->flags = se->flags;
|
2012-12-09 16:55:03 +00:00
|
|
|
e->preview_asked = se->preview_asked;
|
|
|
|
e->preview_company = se->preview_company;
|
2009-01-04 15:32:25 +00:00
|
|
|
e->preview_wait = se->preview_wait;
|
|
|
|
e->company_avail = se->company_avail;
|
2014-09-07 16:12:58 +00:00
|
|
|
e->company_hidden = se->company_hidden;
|
2014-04-25 15:40:32 +00:00
|
|
|
if (se->name != NULL) e->name = stredup(se->name);
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get rid of temporary data */
|
2015-04-23 20:07:07 +00:00
|
|
|
for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
|
|
|
|
FreeEngine(*it);
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
_temp_engine.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Load_ENGS()
|
|
|
|
{
|
|
|
|
/* Load old separate String ID list into a temporary array. This
|
|
|
|
* was always 256 entries. */
|
|
|
|
StringID names[256];
|
|
|
|
|
|
|
|
SlArray(names, lengthof(names), SLE_STRINGID);
|
|
|
|
|
|
|
|
/* Copy each string into the temporary engine array. */
|
|
|
|
for (EngineID engine = 0; engine < lengthof(names); engine++) {
|
|
|
|
Engine *e = GetTempDataEngine(engine);
|
|
|
|
e->name = CopyFromOldName(names[engine]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-08 16:51:08 +00:00
|
|
|
/** Save and load the mapping between the engine id in the pool, and the grf file it came from. */
|
|
|
|
static const SaveLoad _engine_id_mapping_desc[] = {
|
|
|
|
SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
|
|
|
|
SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
|
|
|
|
SLE_VAR(EngineIDMapping, type, SLE_UINT8),
|
|
|
|
SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void Save_EIDS()
|
|
|
|
{
|
|
|
|
const EngineIDMapping *end = _engine_mngr.End();
|
|
|
|
uint index = 0;
|
|
|
|
for (EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
|
|
|
|
SlSetArrayIndex(index);
|
|
|
|
SlObject(eid, _engine_id_mapping_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Load_EIDS()
|
|
|
|
{
|
|
|
|
_engine_mngr.Clear();
|
|
|
|
|
2010-10-01 16:42:28 +00:00
|
|
|
while (SlIterateArray() != -1) {
|
2009-03-08 16:51:08 +00:00
|
|
|
EngineIDMapping *eid = _engine_mngr.Append();
|
|
|
|
SlObject(eid, _engine_id_mapping_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
extern const ChunkHandler _engine_chunk_handlers[] = {
|
2010-06-13 14:11:59 +00:00
|
|
|
{ 'EIDS', Save_EIDS, Load_EIDS, NULL, NULL, CH_ARRAY },
|
|
|
|
{ 'ENGN', Save_ENGN, Load_ENGN, NULL, NULL, CH_ARRAY },
|
|
|
|
{ 'ENGS', NULL, Load_ENGS, NULL, NULL, CH_RIFF | CH_LAST },
|
2009-01-04 15:32:25 +00:00
|
|
|
};
|