OpenTTD Source  12.0-beta2
engine_sl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 
12 #include "saveload.h"
14 
15 #include "saveload_internal.h"
16 #include "../engine_base.h"
17 #include "../string_func.h"
18 #include <vector>
19 
20 #include "../safeguards.h"
21 
22 static const SaveLoad _engine_desc[] = {
23  SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
24  SLE_CONDVAR(Engine, intro_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
25  SLE_CONDVAR(Engine, age, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
26  SLE_CONDVAR(Engine, age, SLE_INT32, SLV_31, SL_MAX_VERSION),
27  SLE_VAR(Engine, reliability, SLE_UINT16),
28  SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
29  SLE_VAR(Engine, reliability_start, SLE_UINT16),
30  SLE_VAR(Engine, reliability_max, SLE_UINT16),
31  SLE_VAR(Engine, reliability_final, SLE_UINT16),
32  SLE_VAR(Engine, duration_phase_1, SLE_UINT16),
33  SLE_VAR(Engine, duration_phase_2, SLE_UINT16),
34  SLE_VAR(Engine, duration_phase_3, SLE_UINT16),
35  SLE_VAR(Engine, flags, SLE_UINT8),
36  SLE_CONDVAR(Engine, preview_asked, SLE_UINT16, SLV_179, SL_MAX_VERSION),
37  SLE_CONDVAR(Engine, preview_company, SLE_UINT8, SLV_179, SL_MAX_VERSION),
38  SLE_VAR(Engine, preview_wait, SLE_UINT8),
39  SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
40  SLE_CONDVAR(Engine, company_avail, SLE_UINT16, SLV_104, SL_MAX_VERSION),
41  SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, SLV_193, SL_MAX_VERSION),
43 };
44 
45 static std::vector<Engine*> _temp_engine;
46 
53 {
54  uint8 *zero = CallocT<uint8>(sizeof(Engine));
55  Engine *engine = new (zero) Engine();
56  return engine;
57 }
58 
63 static void FreeEngine(Engine *e)
64 {
65  if (e != nullptr) {
66  e->~Engine();
67  free(e);
68  }
69 }
70 
71 Engine *GetTempDataEngine(EngineID index)
72 {
73  if (index < _temp_engine.size()) {
74  return _temp_engine[index];
75  } else if (index == _temp_engine.size()) {
76  _temp_engine.push_back(CallocEngine());
77  return _temp_engine[index];
78  } else {
79  NOT_REACHED();
80  }
81 }
82 
84  ENGNChunkHandler() : ChunkHandler('ENGN', CH_TABLE) {}
85 
86  void Save() const override
87  {
88  SlTableHeader(_engine_desc);
89 
90  for (Engine *e : Engine::Iterate()) {
91  SlSetArrayIndex(e->index);
92  SlObject(e, _engine_desc);
93  }
94  }
95 
96  void Load() const override
97  {
98  const std::vector<SaveLoad> slt = SlCompatTableHeader(_engine_desc, _engine_sl_compat);
99 
100  /* As engine data is loaded before engines are initialized we need to load
101  * this information into a temporary array. This is then copied into the
102  * engine pool after processing NewGRFs by CopyTempEngineData(). */
103  int index;
104  while ((index = SlIterateArray()) != -1) {
105  Engine *e = GetTempDataEngine(index);
106  SlObject(e, slt);
107 
109  /* preview_company_rank was replaced with preview_company and preview_asked.
110  * Just cancel any previews. */
111  e->flags &= ~4; // ENGINE_OFFER_WINDOW_OPEN
113  e->preview_asked = (CompanyMask)-1;
114  }
115  }
116  }
117 };
118 
123 {
124  for (Engine *e : Engine::Iterate()) {
125  if (e->index >= _temp_engine.size()) break;
126 
127  const Engine *se = GetTempDataEngine(e->index);
128  e->intro_date = se->intro_date;
129  e->age = se->age;
130  e->reliability = se->reliability;
131  e->reliability_spd_dec = se->reliability_spd_dec;
132  e->reliability_start = se->reliability_start;
133  e->reliability_max = se->reliability_max;
134  e->reliability_final = se->reliability_final;
135  e->duration_phase_1 = se->duration_phase_1;
136  e->duration_phase_2 = se->duration_phase_2;
137  e->duration_phase_3 = se->duration_phase_3;
138  e->flags = se->flags;
139  e->preview_asked = se->preview_asked;
140  e->preview_company = se->preview_company;
141  e->preview_wait = se->preview_wait;
142  e->company_avail = se->company_avail;
143  e->company_hidden = se->company_hidden;
144  e->name = se->name;
145  }
146 
147  ResetTempEngineData();
148 }
149 
150 void ResetTempEngineData()
151 {
152  /* Get rid of temporary data */
153  for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
154  FreeEngine(*it);
155  }
156  _temp_engine.clear();
157 }
158 
161 
162  void Load() const override
163  {
164  /* Load old separate String ID list into a temporary array. This
165  * was always 256 entries. */
166  StringID names[256];
167 
168  SlCopy(names, lengthof(names), SLE_STRINGID);
169 
170  /* Copy each string into the temporary engine array. */
171  for (EngineID engine = 0; engine < lengthof(names); engine++) {
172  Engine *e = GetTempDataEngine(engine);
173  e->name = CopyFromOldName(names[engine]);
174  }
175  }
176 };
177 
180  SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
181  SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
182  SLE_VAR(EngineIDMapping, type, SLE_UINT8),
183  SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
184 };
185 
187  EIDSChunkHandler() : ChunkHandler('EIDS', CH_TABLE) {}
188 
189  void Save() const override
190  {
192 
193  uint index = 0;
194  for (EngineIDMapping &eid : _engine_mngr) {
195  SlSetArrayIndex(index);
197  index++;
198  }
199  }
200 
201  void Load() const override
202  {
204 
205  _engine_mngr.clear();
206 
207  while (SlIterateArray() != -1) {
208  EngineIDMapping *eid = &_engine_mngr.emplace_back();
209  SlObject(eid, slt);
210  }
211  }
212 };
213 
214 static const EIDSChunkHandler EIDS;
215 static const ENGNChunkHandler ENGN;
216 static const ENGSChunkHandler ENGS;
217 static const ChunkHandlerRef engine_chunk_handlers[] = {
218  EIDS,
219  ENGN,
220  ENGS,
221 };
222 
223 extern const ChunkHandlerTable _engine_chunk_handlers(engine_chunk_handlers);
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:34
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:38
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:32
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:744
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:43
SlCopy
void SlCopy(void *object, size_t length, VarType conv)
Copy a list of SL_VARs to/from a savegame.
Definition: saveload.cpp:1151
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:702
SLV_84
@ SLV_84
84 11822
Definition: saveload.h:147
Engine::duration_phase_1
uint16 duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition: engine_base.h:36
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:798
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:33
ENGNChunkHandler
Definition: engine_sl.cpp:83
saveload.h
ENGNChunkHandler::Load
void Load() const override
Load the chunk.
Definition: engine_sl.cpp:96
SLV_104
@ SLV_104
104 14735
Definition: saveload.h:171
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:41
Engine
Definition: engine_base.h:27
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
Engine::company_hidden
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company.
Definition: engine_base.h:44
SLV_179
@ SLV_179
179 24810
Definition: saveload.h:261
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
CallocEngine
static Engine * CallocEngine()
Allocate an Engine structure, but not using the pools.
Definition: engine_sl.cpp:52
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
CH_READONLY
@ CH_READONLY
Chunk is never saved.
Definition: saveload.h:402
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:84
ENGSChunkHandler
Definition: engine_sl.cpp:159
_engine_sl_compat
const SaveLoadCompat _engine_sl_compat[]
Original field order for _engine_desc.
Definition: engine_sl_compat.h:16
CopyFromOldName
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:60
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:40
EIDSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: engine_sl.cpp:201
CopyTempEngineData
void CopyTempEngineData()
Copy data from temporary engine array into the real engine pool.
Definition: engine_sl.cpp:122
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
FreeEngine
static void FreeEngine(Engine *e)
Deallocate an Engine constructed by CallocEngine.
Definition: engine_sl.cpp:63
EngineIDMapping
Definition: engine_base.h:167
Engine::reliability_final
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:35
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:342
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Engine::name
std::string name
Custom name of engine.
Definition: engine_base.h:28
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:772
Pool::PoolItem<&_engine_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
ENGSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: engine_sl.cpp:162
SLV_193
@ SLV_193
193 26802
Definition: saveload.h:279
engine_sl_compat.h
EIDSChunkHandler
Definition: engine_sl.cpp:186
EIDSChunkHandler::Save
void Save() const override
Save the chunk.
Definition: engine_sl.cpp:189
_engine_id_mapping_desc
static const SaveLoad _engine_id_mapping_desc[]
Save and load the mapping between the engine id in the pool, and the grf file it came from.
Definition: engine_sl.cpp:179
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:42
saveload_internal.h
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
_engine_id_mapping_sl_compat
const SaveLoadCompat _engine_id_mapping_sl_compat[]
Original field order for _engine_id_mapping_desc.
Definition: engine_sl_compat.h:41
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:39
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:29
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:31
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:37
ENGNChunkHandler::Save
void Save() const override
Save the chunk.
Definition: engine_sl.cpp:86