OpenTTD
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 
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),
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  SLE_CONDNULL(16, SLV_2, SLV_144), // old reserved space
45 
46  SLE_END()
47 };
48 
49 static std::vector<Engine*> _temp_engine;
50 
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 
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
110  e->preview_asked = (CompanyMask)-1;
111  }
112  }
113 }
114 
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  if (se->name != nullptr) e->name = stredup(se->name);
141  }
142 
143  /* Get rid of temporary data */
144  for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
145  FreeEngine(*it);
146  }
147  _temp_engine.clear();
148 }
149 
150 static void Load_ENGS()
151 {
152  /* Load old separate String ID list into a temporary array. This
153  * was always 256 entries. */
154  StringID names[256];
155 
156  SlArray(names, lengthof(names), SLE_STRINGID);
157 
158  /* Copy each string into the temporary engine array. */
159  for (EngineID engine = 0; engine < lengthof(names); engine++) {
160  Engine *e = GetTempDataEngine(engine);
161  e->name = CopyFromOldName(names[engine]);
162  }
163 }
164 
167  SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
168  SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
169  SLE_VAR(EngineIDMapping, type, SLE_UINT8),
170  SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
171  SLE_END()
172 };
173 
174 static void Save_EIDS()
175 {
176  uint index = 0;
177  for (EngineIDMapping &eid : _engine_mngr) {
178  SlSetArrayIndex(index);
179  SlObject(&eid, _engine_id_mapping_desc);
180  index++;
181  }
182 }
183 
184 static void Load_EIDS()
185 {
186  _engine_mngr.clear();
187 
188  while (SlIterateArray() != -1) {
189  /*C++17: EngineIDMapping *eid = &*/ _engine_mngr.emplace_back();
190  EngineIDMapping *eid = &_engine_mngr.back();
191  SlObject(eid, _engine_id_mapping_desc);
192  }
193 }
194 
195 extern const ChunkHandler _engine_chunk_handlers[] = {
196  { 'EIDS', Save_EIDS, Load_EIDS, nullptr, nullptr, CH_ARRAY },
197  { 'ENGN', Save_ENGN, Load_ENGN, nullptr, nullptr, CH_ARRAY },
198  { 'ENGS', nullptr, Load_ENGS, nullptr, nullptr, CH_RIFF | CH_LAST },
199 };
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:642
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:27
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:25
179 24810
Definition: saveload.h:256
193 26802
Definition: saveload.h:274
void CopyTempEngineData()
Copy data from temporary engine array into the real engine pool.
Definition: engine_sl.cpp:118
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition: saveload.h:33
#define SLE_CONDSTR(base, variable, type, length, from, to)
Storage of a string in some savegame versions.
Definition: saveload.h:566
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:23
char * CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
Definition: strings_sl.cpp:59
104 14735
Definition: saveload.h:166
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:166
121 16694
Definition: saveload.h:187
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
byte flags
Flags of the engine.
Definition: engine_base.h:33
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:31
void SlArray(void *array, size_t length, VarType conv)
Save/Load an array.
Definition: saveload.cpp:995
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:26
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:534
Highest possible saveload version.
Definition: saveload.h:305
First savegame version.
Definition: saveload.h:30
uint16 duration_phase_3
Third reliability phase on months, decaying to reliability_final.
Definition: engine_base.h:32
uint16 duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max...
Definition: engine_base.h:30
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:37
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:136
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:28
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
84 11822
Definition: saveload.h:142
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:29
char * name
Custom name of engine.
Definition: engine_base.h:22
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
Handlers and description of chunk.
Definition: saveload.h:356
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:651
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:35
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:36
An invalid company.
Definition: company_type.h:30
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
144 20334
Definition: saveload.h:214
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:34
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1546
static Engine * CallocEngine()
Allocate an Engine structure, but not using the pools.
Definition: engine_sl.cpp:56
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
SaveLoad type struct.
Definition: saveload.h:496
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:594
45 8501
Definition: saveload.h:96
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:620
31 5999
Definition: saveload.h:79
Declaration of functions used in more save/load files.
Last chunk in this array.
Definition: saveload.h:391
static void FreeEngine(Engine *e)
Deallocate an Engine constructed by CallocEngine.
Definition: engine_sl.cpp:67