OpenTTD Source: src/saveload/engine_sl.cpp Source File

OpenTTD Source 1.11.0-RC1

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 #include "saveload_internal.h"
12 #include "../engine_base.h"
13 #include "../string_func.h"
14 #include <vector>
15
16 #include "../safeguards.h"
17
18 static const SaveLoad _engine_desc[] = {
19 SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
20 SLE_CONDVAR(Engine, intro_date, SLE_INT32, SLV_31, SL_MAX_VERSION),
21 SLE_CONDVAR(Engine, age, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
22 SLE_CONDVAR(Engine, age, SLE_INT32, SLV_31, SL_MAX_VERSION),
23 SLE_VAR(Engine, reliability, SLE_UINT16),
24 SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
25 SLE_VAR(Engine, reliability_start, SLE_UINT16),
26 SLE_VAR(Engine, reliability_max, SLE_UINT16),
27 SLE_VAR(Engine, reliability_final, SLE_UINT16),
28 SLE_VAR(Engine, duration_phase_1, SLE_UINT16),
29 SLE_VAR(Engine, duration_phase_2, SLE_UINT16),
30 SLE_VAR(Engine, duration_phase_3, SLE_UINT16),
31
32 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_121),
33 SLE_VAR(Engine, flags, SLE_UINT8),
34 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_179), // old preview_company_rank
35 SLE_CONDVAR(Engine, preview_asked, SLE_UINT16, SLV_179, SL_MAX_VERSION),
36 SLE_CONDVAR(Engine, preview_company, SLE_UINT8, SLV_179, SL_MAX_VERSION),
37 SLE_VAR(Engine, preview_wait, SLE_UINT8),
38 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_45),
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),
42 SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION),
43
44 SLE_CONDNULL(16, SLV_2, SLV_144), // old reserved space
45
46 SLE_END()
47 };
48
49 static std::vector<Engine*> _temp_engine;
50
56 static Engine* CallocEngine()
57 {
58 uint8 *zero = CallocT<uint8>(sizeof(Engine));
59 Engine *engine = new (zero) Engine();
60 return engine;
61 }
62
67 static void FreeEngine(Engine *e)
68 {
69 if (e != nullptr) {
70 e->~Engine();
71 free(e);
72 }
73 }
74
75 Engine *GetTempDataEngine(EngineID index)
76 {
77 if (index < _temp_engine.size()) {
78 return _temp_engine[index];
79 } else if (index == _temp_engine.size()) {
80 _temp_engine.push_back(CallocEngine());
81 return _temp_engine[index];
82 } else {
83 NOT_REACHED();
84 }
85 }
86
87 static void Save_ENGN()
88 {
89 for (Engine *e : Engine::Iterate()) {
90 SlSetArrayIndex(e->index);
91 SlObject(e, _engine_desc);
92 }
93 }
94
95 static void Load_ENGN()
96 {
97 /* As engine data is loaded before engines are initialized we need to load
98 * this information into a temporary array. This is then copied into the
99 * engine pool after processing NewGRFs by CopyTempEngineData(). */
100 int index;
101 while ((index = SlIterateArray()) != -1) {
102 Engine *e = GetTempDataEngine(index);
103 SlObject(e, _engine_desc);
104
105 if (IsSavegameVersionBefore(SLV_179)) {
106 /* preview_company_rank was replaced with preview_company and preview_asked.
107 * Just cancel any previews. */
108 e->flags &= ~4; // ENGINE_OFFER_WINDOW_OPEN
109 e->preview_company = INVALID_COMPANY;
110 e->preview_asked = (CompanyMask)-1;
111 }
112 }
113 }
114
118 void CopyTempEngineData()
119 {
120 for (Engine *e : Engine::Iterate()) {
121 if (e->index >= _temp_engine.size()) break;
122
123 const Engine *se = GetTempDataEngine(e->index);
124 e->intro_date = se->intro_date;
125 e->age = se->age;
126 e->reliability = se->reliability;
127 e->reliability_spd_dec = se->reliability_spd_dec;
128 e->reliability_start = se->reliability_start;
129 e->reliability_max = se->reliability_max;
130 e->reliability_final = se->reliability_final;
131 e->duration_phase_1 = se->duration_phase_1;
132 e->duration_phase_2 = se->duration_phase_2;
133 e->duration_phase_3 = se->duration_phase_3;
134 e->flags = se->flags;
135 e->preview_asked = se->preview_asked;
136 e->preview_company = se->preview_company;
137 e->preview_wait = se->preview_wait;
138 e->company_avail = se->company_avail;
139 e->company_hidden = se->company_hidden;
140 e->name = se->name;
141 }
142
143 ResetTempEngineData();
144 }
145
146 void ResetTempEngineData()
147 {
148 /* Get rid of temporary data */
149 for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
150 FreeEngine(*it);
151 }
152 _temp_engine.clear();
153 }
154
155 static void Load_ENGS()
156 {
157 /* Load old separate String ID list into a temporary array. This
158 * was always 256 entries. */
159 StringID names[256];
160
161 SlArray(names, lengthof(names), SLE_STRINGID);
162
163 /* Copy each string into the temporary engine array. */
164 for (EngineID engine = 0; engine < lengthof(names); engine++) {
165 Engine *e = GetTempDataEngine(engine);
166 e->name = CopyFromOldName(names[engine]);
167 }
168 }
169
171 static const SaveLoad _engine_id_mapping_desc[] = {
172 SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
173 SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
174 SLE_VAR(EngineIDMapping, type, SLE_UINT8),
175 SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
176 SLE_END()
177 };
178
179 static void Save_EIDS()
180 {
181 uint index = 0;
182 for (EngineIDMapping &eid : _engine_mngr) {
183 SlSetArrayIndex(index);
184 SlObject(&eid, _engine_id_mapping_desc);
185 index++;
186 }
187 }
188
189 static void Load_EIDS()
190 {
191 _engine_mngr.clear();
192
193 while (SlIterateArray() != -1) {
194 EngineIDMapping *eid = &_engine_mngr.emplace_back();
195 SlObject(eid, _engine_id_mapping_desc);
196 }
197 }
198
199 extern const ChunkHandler _engine_chunk_handlers[] = {
200 { 'EIDS', Save_EIDS, Load_EIDS, nullptr, nullptr, CH_ARRAY },
201 { 'ENGN', Save_ENGN, Load_ENGN, nullptr, nullptr, CH_ARRAY },
202 { 'ENGS', nullptr, Load_ENGS, nullptr, nullptr, CH_RIFF | CH_LAST },
203 };
SLV_144
@ SLV_144
144 20334
Definition: saveload.h:215
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:28
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:32
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:26
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:593
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:37
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:227
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
SLV_84
@ SLV_84
84 11822
Definition: saveload.h:143
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:30
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:647
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:27
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
SLV_104
@ SLV_104
104 14735
Definition: saveload.h:167
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:35
Engine
Definition: engine_base.h:21
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
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:38
SLV_179
@ SLV_179
179 24810
Definition: saveload.h:257
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:677
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:56
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:80
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
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:34
CopyTempEngineData
void CopyTempEngineData()
Copy data from temporary engine array into the real engine pool.
Definition: engine_sl.cpp:118
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SLV_45
@ SLV_45
45 8501
Definition: saveload.h:97
FreeEngine
static void FreeEngine(Engine *e)
Deallocate an Engine constructed by CallocEngine.
Definition: engine_sl.cpp:67
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
EngineIDMapping
Definition: engine_base.h:163
SLV_2
@ SLV_2
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition: saveload.h:34
Engine::reliability_final
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:29
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
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:22
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
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:378
SLV_193
@ SLV_193
193 26802
Definition: saveload.h:275
_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:171
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:36
saveload_internal.h
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
SLV_121
@ SLV_121
121 16694
Definition: saveload.h:188
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:33
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1051
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:23
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:25
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:31