OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

engine_sl.cpp
Go to the documentation of this file.
1 /* $Id: engine_sl.cpp 26816 2014-09-13 14:46:03Z 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
53 Engine *GetTempDataEngine(EngineID index)
54 {
55 if (index < _temp_engine.size()) {
56 return &_temp_engine[index];
57 } else if (index == _temp_engine.size()) {
58 uint8 zero[sizeof(Engine)];
59 memset(zero, 0, sizeof(zero));
60 Engine *engine = new (zero) Engine();
61
62 /* Adding 'engine' to the vector makes a shallow copy, so we do not want to destruct 'engine' */
63 _temp_engine.push_back(*engine);
64
65 return &_temp_engine[index];
66 } else {
67 NOT_REACHED();
68 }
69 }
70
71 static void Save_ENGN()
72 {
73 Engine *e;
74 FOR_ALL_ENGINES(e) {
75 SlSetArrayIndex(e->index);
76 SlObject(e, _engine_desc);
77 }
78 }
79
80 static void Load_ENGN()
81 {
82 /* As engine data is loaded before engines are initialized we need to load
83 * this information into a temporary array. This is then copied into the
84 * engine pool after processing NewGRFs by CopyTempEngineData(). */
85 int index;
86 while ((index = SlIterateArray()) != -1) {
87 Engine *e = GetTempDataEngine(index);
88 SlObject(e, _engine_desc);
89
90 if (IsSavegameVersionBefore(179)) {
91 /* preview_company_rank was replaced with preview_company and preview_asked.
92 * Just cancel any previews. */
93 e->flags &= ~4; // ENGINE_OFFER_WINDOW_OPEN
94 e->preview_company = INVALID_COMPANY;
95 e->preview_asked = (CompanyMask)-1;
96 }
97 }
98 }
99
103 void CopyTempEngineData()
104 {
105 Engine *e;
106 FOR_ALL_ENGINES(e) {
107 if (e->index >= _temp_engine.size()) break;
108
109 const Engine *se = GetTempDataEngine(e->index);
110 e->intro_date = se->intro_date;
111 e->age = se->age;
112 e->reliability = se->reliability;
113 e->reliability_spd_dec = se->reliability_spd_dec;
114 e->reliability_start = se->reliability_start;
115 e->reliability_max = se->reliability_max;
116 e->reliability_final = se->reliability_final;
117 e->duration_phase_1 = se->duration_phase_1;
118 e->duration_phase_2 = se->duration_phase_2;
119 e->duration_phase_3 = se->duration_phase_3;
120 e->flags = se->flags;
121 e->preview_asked = se->preview_asked;
122 e->preview_company = se->preview_company;
123 e->preview_wait = se->preview_wait;
124 e->company_avail = se->company_avail;
125 e->company_hidden = se->company_hidden;
126 if (se->name != NULL) e->name = stredup(se->name);
127 }
128
129 /* Get rid of temporary data */
130 _temp_engine.clear();
131 }
132
133 static void Load_ENGS()
134 {
135 /* Load old separate String ID list into a temporary array. This
136 * was always 256 entries. */
137 StringID names[256];
138
139 SlArray(names, lengthof(names), SLE_STRINGID);
140
141 /* Copy each string into the temporary engine array. */
142 for (EngineID engine = 0; engine < lengthof(names); engine++) {
143 Engine *e = GetTempDataEngine(engine);
144 e->name = CopyFromOldName(names[engine]);
145 }
146 }
147
149 static const SaveLoad _engine_id_mapping_desc[] = {
150 SLE_VAR(EngineIDMapping, grfid, SLE_UINT32),
151 SLE_VAR(EngineIDMapping, internal_id, SLE_UINT16),
152 SLE_VAR(EngineIDMapping, type, SLE_UINT8),
153 SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
154 SLE_END()
155 };
156
157 static void Save_EIDS()
158 {
159 const EngineIDMapping *end = _engine_mngr.End();
160 uint index = 0;
161 for (EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
162 SlSetArrayIndex(index);
163 SlObject(eid, _engine_id_mapping_desc);
164 }
165 }
166
167 static void Load_EIDS()
168 {
169 _engine_mngr.Clear();
170
171 while (SlIterateArray() != -1) {
172 EngineIDMapping *eid = _engine_mngr.Append();
173 SlObject(eid, _engine_id_mapping_desc);
174 }
175 }
176
177 extern const ChunkHandler _engine_chunk_handlers[] = {
178 { 'EIDS', Save_EIDS, Load_EIDS, NULL, NULL, CH_ARRAY },
179 { 'ENGN', Save_ENGN, Load_ENGN, NULL, NULL, CH_ARRAY },
180 { 'ENGS', NULL, Load_ENGS, NULL, NULL, CH_RIFF | CH_LAST },
181 };