OpenTTD

engine_sl.cpp
Go to the documentation of this file.
1 /* $Id: engine_sl.cpp 27243 2015-04-23 20:07:07Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "saveload_internal.h"
14 #include "../engine_base.h"
15 #include "../string_func.h"
16 #include <vector>
17
18 #include "../safeguards.h"
19
20 static const SaveLoad _engine_desc[] = {
21 SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
22 SLE_CONDVAR(Engine, intro_date, SLE_INT32, 31, SL_MAX_VERSION),
23 SLE_CONDVAR(Engine, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
24 SLE_CONDVAR(Engine, age, SLE_INT32, 31, SL_MAX_VERSION),
25 SLE_VAR(Engine, reliability, SLE_UINT16),
26 SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
27 SLE_VAR(Engine, reliability_start, SLE_UINT16),
28 SLE_VAR(Engine, reliability_max, SLE_UINT16),
29 SLE_VAR(Engine, reliability_final, SLE_UINT16),
30 SLE_VAR(Engine, duration_phase_1, SLE_UINT16),
31 SLE_VAR(Engine, duration_phase_2, SLE_UINT16),
32 SLE_VAR(Engine, duration_phase_3, SLE_UINT16),
33
34 SLE_CONDNULL(1, 0, 120),
35 SLE_VAR(Engine, flags, SLE_UINT8),
36 SLE_CONDNULL(1, 0, 178), // old preview_company_rank
37 SLE_CONDVAR(Engine, preview_asked, SLE_UINT16, 179, SL_MAX_VERSION),
38 SLE_CONDVAR(Engine, preview_company, SLE_UINT8, 179, SL_MAX_VERSION),
39 SLE_VAR(Engine, preview_wait, SLE_UINT8),
40 SLE_CONDNULL(1, 0, 44),
41 SLE_CONDVAR(Engine, company_avail, SLE_FILE_U8 | SLE_VAR_U16, 0, 103),
42 SLE_CONDVAR(Engine, company_avail, SLE_UINT16, 104, SL_MAX_VERSION),
43 SLE_CONDVAR(Engine, company_hidden, SLE_UINT16, 193, SL_MAX_VERSION),
44 SLE_CONDSTR(Engine, name, SLE_STR, 0, 84, SL_MAX_VERSION),
45
46 SLE_CONDNULL(16, 2, 143), // old reserved space
47
48 SLE_END()
49 };
50
51 static std::vector<Engine*> _temp_engine;
52
58 static Engine* CallocEngine()
59 {
60 uint8 *zero = CallocT<uint8>(sizeof(Engine));
61 Engine *engine = new (zero) Engine();
62 return engine;
63 }
64
69 static void FreeEngine(Engine *e)
70 {
71 if (e != NULL) {
72 e->~Engine();
73 free(e);
74 }
75 }
76
77 Engine *GetTempDataEngine(EngineID index)
78 {
79 if (index < _temp_engine.size()) {
80 return _temp_engine[index];
81 } else if (index == _temp_engine.size()) {
82 _temp_engine.push_back(CallocEngine());
83 return _temp_engine[index];
84 } else {
85 NOT_REACHED();
86 }
87 }
88
89 static void Save_ENGN()
90 {
91 Engine *e;
92 FOR_ALL_ENGINES(e) {
93 SlSetArrayIndex(e->index);
94 SlObject(e, _engine_desc);
95 }
96 }
97
98 static void Load_ENGN()
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, _engine_desc);
107
108 if (IsSavegameVersionBefore(179)) {
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
112 e->preview_company = INVALID_COMPANY;
113 e->preview_asked = (CompanyMask)-1;
114 }
115 }
116 }
117
121 void CopyTempEngineData()
122 {
123 Engine *e;
124 FOR_ALL_ENGINES(e) {
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 if (se->name != NULL) e->name = stredup(se->name);
145 }
146
147 /* Get rid of temporary data */
148 for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
149 FreeEngine(*it);
150 }
151 _temp_engine.clear();
152 }
153
154 static void Load_ENGS()
155 {
156 /* Load old separate String ID list into a temporary array. This
157 * was always 256 entries. */
158 StringID names[256];
159
160 SlArray(names, lengthof(names), SLE_STRINGID);
161
162 /* Copy each string into the temporary engine array. */
163 for (EngineID engine = 0; engine < lengthof(names); engine++) {
164 Engine *e = GetTempDataEngine(engine);
165 e->name = CopyFromOldName(names[engine]);
166 }
167 }
168
170 static const SaveLoad _engine_id_mapping_desc[] = {
171 SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
172 SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
173 SLE_VAR(EngineIDMapping, type, SLE_UINT8),
174 SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
175 SLE_END()
176 };
177
178 static void Save_EIDS()
179 {
180 const EngineIDMapping *end = _engine_mngr.End();
181 uint index = 0;
182 for (EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
183 SlSetArrayIndex(index);
184 SlObject(eid, _engine_id_mapping_desc);
185 }
186 }
187
188 static void Load_EIDS()
189 {
190 _engine_mngr.Clear();
191
192 while (SlIterateArray() != -1) {
193 EngineIDMapping *eid = _engine_mngr.Append();
194 SlObject(eid, _engine_id_mapping_desc);
195 }
196 }
197
198 extern const ChunkHandler _engine_chunk_handlers[] = {
199 { 'EIDS', Save_EIDS, Load_EIDS, NULL, NULL, CH_ARRAY },
200 { 'ENGN', Save_ENGN, Load_ENGN, NULL, NULL, CH_ARRAY },
201 { 'ENGS', NULL, Load_ENGS, NULL, NULL, CH_RIFF | CH_LAST },
202 };
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:344
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:29
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:27
CopyTempEngineData
void CopyTempEngineData()
Copy data from temporary engine array into the real engine pool.
Definition: engine_sl.cpp:121
SLE_CONDSTR
#define SLE_CONDSTR(base, variable, type, length, from, to)
Storage of a string in some savegame versions.
Definition: saveload.h:278
EngineIDMapping
Definition: engine_base.h:148
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:25
CopyFromOldName
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:61
_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:170
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:40
SmallVector::Clear
void Clear()
Remove all items from the list.
Definition: smallvec_type.hpp:98
SmallVector::Begin
const T * Begin() const
Get the pointer to the first item (const)
Definition: smallvec_type.hpp:272
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:35
SmallVector::End
const T * End() const
Get the pointer behind the last valid item (const)
Definition: smallvec_type.hpp:292
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:33
SmallVector::Append
T * Append(uint to_add=1)
Append an item and return it.
Definition: smallvec_type.hpp:134
SlArray
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:1186
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:28
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase on months, decaying to reliability_final.
Definition: engine_base.h:34
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:32
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:39
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:126
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:30
SL_MAX_VERSION
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Engine::reliability_final
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:31
Engine::name
char * name
Custom name of engine.
Definition: engine_base.h:24
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
Engine
Definition: engine_base.h:23
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
Engine::preview_wait
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:38
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:36
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
CallocEngine
static Engine * CallocEngine()
Allocate an Engine structure, but not using the pools.
Definition: engine_sl.cpp:58
Engine::preview_company
CompanyByte preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:37
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
INVALID_COMPANY
An invalid company.
Definition: company_type.h:32
saveload_internal.h
Declaration of functions used in more save/load files.
CH_LAST
Last chunk in this array.
Definition: saveload.h:104
FreeEngine
static void FreeEngine(Engine *e)
Deallocate an Engine constructed by CallocEngine.
Definition: engine_sl.cpp:69