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

OpenTTD Source 1.11.0-RC1

company_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 "../company_func.h"
12 #include "../company_manager_face.h"
13 #include "../fios.h"
14 #include "../tunnelbridge_map.h"
15 #include "../tunnelbridge.h"
16 #include "../station_base.h"
17 #include "../strings_func.h"
18
19 #include "saveload.h"
20
21 #include "table/strings.h"
22
23 #include "../safeguards.h"
24
43 CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face)
44 {
45 CompanyManagerFace cmf = 0;
46 GenderEthnicity ge = GE_WM;
47
48 if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
49 if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
50
51 SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
52 SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
53 SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
54 SetCompanyManagerFaceBits(cmf, CMFV_CHIN, ge, ScaleCompanyManagerFaceValue(CMFV_CHIN, ge, GB(face, 4, 2)));
55 SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS, ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face, 6, 4)));
56 SetCompanyManagerFaceBits(cmf, CMFV_HAIR, ge, ScaleCompanyManagerFaceValue(CMFV_HAIR, ge, GB(face, 16, 4)));
57 SetCompanyManagerFaceBits(cmf, CMFV_JACKET, ge, ScaleCompanyManagerFaceValue(CMFV_JACKET, ge, GB(face, 20, 2)));
58 SetCompanyManagerFaceBits(cmf, CMFV_COLLAR, ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR, ge, GB(face, 22, 2)));
59 SetCompanyManagerFaceBits(cmf, CMFV_GLASSES, ge, GB(face, 28, 1));
60
61 uint lips = GB(face, 10, 4);
62 if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
63 SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true);
64 SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE, ge, std::max(lips, 1U) - 1);
65 } else {
66 if (!HasBit(ge, GENDER_FEMALE)) {
67 lips = lips * 15 / 16;
68 lips -= 3;
69 if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
70 } else {
71 lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips);
72 }
73 SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips);
74
75 uint nose = GB(face, 13, 3);
76 if (ge == GE_WF) {
77 nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
78 } else {
79 nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose);
80 }
81 SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose);
82 }
83
84 uint tie_earring = GB(face, 24, 4);
85 if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
86 if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true);
87 SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2));
88 }
89
90 return cmf;
91 }
92
94 void AfterLoadCompanyStats()
95 {
96 /* Reset infrastructure statistics to zero. */
97 for (Company *c : Company::Iterate()) MemSetT(&c->infrastructure, 0);
98
99 /* Collect airport count. */
100 for (const Station *st : Station::Iterate()) {
101 if ((st->facilities & FACIL_AIRPORT) && Company::IsValidID(st->owner)) {
102 Company::Get(st->owner)->infrastructure.airport++;
103 }
104 }
105
106 Company *c;
107 for (TileIndex tile = 0; tile < MapSize(); tile++) {
108 switch (GetTileType(tile)) {
109 case MP_RAILWAY:
110 c = Company::GetIfValid(GetTileOwner(tile));
111 if (c != nullptr) {
112 uint pieces = 1;
113 if (IsPlainRail(tile)) {
114 TrackBits bits = GetTrackBits(tile);
115 pieces = CountBits(bits);
116 if (TracksOverlap(bits)) pieces *= pieces;
117 }
118 c->infrastructure.rail[GetRailType(tile)] += pieces;
119
120 if (HasSignals(tile)) c->infrastructure.signal += CountBits(GetPresentSignals(tile));
121 }
122 break;
123
124 case MP_ROAD: {
125 if (IsLevelCrossing(tile)) {
126 c = Company::GetIfValid(GetTileOwner(tile));
127 if (c != nullptr) c->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR;
128 }
129
130 /* Iterate all present road types as each can have a different owner. */
131 FOR_ALL_ROADTRAMTYPES(rtt) {
132 RoadType rt = GetRoadType(tile, rtt);
133 if (rt == INVALID_ROADTYPE) continue;
134 c = Company::GetIfValid(IsRoadDepot(tile) ? GetTileOwner(tile) : GetRoadOwner(tile, rtt));
135 /* A level crossings and depots have two road bits. */
136 if (c != nullptr) c->infrastructure.road[rt] += IsNormalRoad(tile) ? CountBits(GetRoadBits(tile, rtt)) : 2;
137 }
138 break;
139 }
140
141 case MP_STATION:
142 c = Company::GetIfValid(GetTileOwner(tile));
143 if (c != nullptr && GetStationType(tile) != STATION_AIRPORT && !IsBuoy(tile)) c->infrastructure.station++;
144
145 switch (GetStationType(tile)) {
146 case STATION_RAIL:
147 case STATION_WAYPOINT:
148 if (c != nullptr && !IsStationTileBlocked(tile)) c->infrastructure.rail[GetRailType(tile)]++;
149 break;
150
151 case STATION_BUS:
152 case STATION_TRUCK: {
153 /* Iterate all present road types as each can have a different owner. */
154 FOR_ALL_ROADTRAMTYPES(rtt) {
155 RoadType rt = GetRoadType(tile, rtt);
156 if (rt == INVALID_ROADTYPE) continue;
157 c = Company::GetIfValid(GetRoadOwner(tile, rtt));
158 if (c != nullptr) c->infrastructure.road[rt] += 2; // A road stop has two road bits.
159 }
160 break;
161 }
162
163 case STATION_DOCK:
164 case STATION_BUOY:
165 if (GetWaterClass(tile) == WATER_CLASS_CANAL) {
166 if (c != nullptr) c->infrastructure.water++;
167 }
168 break;
169
170 default:
171 break;
172 }
173 break;
174
175 case MP_WATER:
176 if (IsShipDepot(tile) || IsLock(tile)) {
177 c = Company::GetIfValid(GetTileOwner(tile));
178 if (c != nullptr) {
179 if (IsShipDepot(tile)) c->infrastructure.water += LOCK_DEPOT_TILE_FACTOR;
180 if (IsLock(tile) && GetLockPart(tile) == LOCK_PART_MIDDLE) {
181 /* The middle tile specifies the owner of the lock. */
182 c->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // the middle tile specifies the owner of the
183 break; // do not count the middle tile as canal
184 }
185 }
186 }
187 FALLTHROUGH;
188
189 case MP_OBJECT:
190 if (GetWaterClass(tile) == WATER_CLASS_CANAL) {
191 c = Company::GetIfValid(GetTileOwner(tile));
192 if (c != nullptr) c->infrastructure.water++;
193 }
194 break;
195
196 case MP_TUNNELBRIDGE: {
197 /* Only count the tunnel/bridge if we're on the northern end tile. */
198 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
199 if (tile < other_end) {
200 /* Count each tunnel/bridge TUNNELBRIDGE_TRACKBIT_FACTOR times to simulate
201 * the higher structural maintenance needs, and don't forget the end tiles. */
202 uint len = (GetTunnelBridgeLength(tile, other_end) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
203
204 switch (GetTunnelBridgeTransportType(tile)) {
205 case TRANSPORT_RAIL:
206 c = Company::GetIfValid(GetTileOwner(tile));
207 if (c != nullptr) c->infrastructure.rail[GetRailType(tile)] += len;
208 break;
209
210 case TRANSPORT_ROAD: {
211 /* Iterate all present road types as each can have a different owner. */
212 FOR_ALL_ROADTRAMTYPES(rtt) {
213 RoadType rt = GetRoadType(tile, rtt);
214 if (rt == INVALID_ROADTYPE) continue;
215 c = Company::GetIfValid(GetRoadOwner(tile, rtt));
216 if (c != nullptr) c->infrastructure.road[rt] += len * 2; // A full diagonal road has two road bits.
217 }
218 break;
219 }
220
221 case TRANSPORT_WATER:
222 c = Company::GetIfValid(GetTileOwner(tile));
223 if (c != nullptr) c->infrastructure.water += len;
224 break;
225
226 default:
227 break;
228 }
229 }
230 break;
231 }
232
233 default:
234 break;
235 }
236 }
237 }
238
239
240
241 /* Save/load of companies */
242 static const SaveLoad _company_desc[] = {
243 SLE_VAR(CompanyProperties, name_2, SLE_UINT32),
244 SLE_VAR(CompanyProperties, name_1, SLE_STRINGID),
245 SLE_CONDSSTR(CompanyProperties, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
246
247 SLE_VAR(CompanyProperties, president_name_1, SLE_STRINGID),
248 SLE_VAR(CompanyProperties, president_name_2, SLE_UINT32),
249 SLE_CONDSSTR(CompanyProperties, president_name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
250
251 SLE_VAR(CompanyProperties, face, SLE_UINT32),
252
253 /* money was changed to a 64 bit field in savegame version 1. */
254 SLE_CONDVAR(CompanyProperties, money, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SLV_1),
255 SLE_CONDVAR(CompanyProperties, money, SLE_INT64, SLV_1, SL_MAX_VERSION),
256
257 SLE_CONDVAR(CompanyProperties, current_loan, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SLV_65),
258 SLE_CONDVAR(CompanyProperties, current_loan, SLE_INT64, SLV_65, SL_MAX_VERSION),
259
260 SLE_VAR(CompanyProperties, colour, SLE_UINT8),
261 SLE_VAR(CompanyProperties, money_fraction, SLE_UINT8),
262 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_58),
263 SLE_VAR(CompanyProperties, block_preview, SLE_UINT8),
264
265 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_94),
266 SLE_CONDNULL(4, SLV_94, SLV_170),
267 SLE_CONDVAR(CompanyProperties, location_of_HQ, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
268 SLE_CONDVAR(CompanyProperties, location_of_HQ, SLE_UINT32, SLV_6, SL_MAX_VERSION),
269 SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
270 SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_UINT32, SLV_6, SL_MAX_VERSION),
271 SLE_CONDVAR(CompanyProperties, inaugurated_year, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
272 SLE_CONDVAR(CompanyProperties, inaugurated_year, SLE_INT32, SLV_31, SL_MAX_VERSION),
273
274 SLE_ARR(CompanyProperties, share_owners, SLE_UINT8, 4),
275
276 SLE_VAR(CompanyProperties, num_valid_stat_ent, SLE_UINT8),
277
278 SLE_VAR(CompanyProperties, months_of_bankruptcy, SLE_UINT8),
279 SLE_CONDVAR(CompanyProperties, bankrupt_asked, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
280 SLE_CONDVAR(CompanyProperties, bankrupt_asked, SLE_UINT16, SLV_104, SL_MAX_VERSION),
281 SLE_VAR(CompanyProperties, bankrupt_timeout, SLE_INT16),
282 SLE_CONDVAR(CompanyProperties, bankrupt_value, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SLV_65),
283 SLE_CONDVAR(CompanyProperties, bankrupt_value, SLE_INT64, SLV_65, SL_MAX_VERSION),
284
285 /* yearly expenses was changed to 64-bit in savegame version 2. */
286 SLE_CONDARR(CompanyProperties, yearly_expenses, SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, SL_MIN_VERSION, SLV_2),
287 SLE_CONDARR(CompanyProperties, yearly_expenses, SLE_INT64, 3 * 13, SLV_2, SL_MAX_VERSION),
288
289 SLE_CONDVAR(CompanyProperties, is_ai, SLE_BOOL, SLV_2, SL_MAX_VERSION),
290 SLE_CONDNULL(1, SLV_107, SLV_112),
291 SLE_CONDNULL(1, SLV_4, SLV_100),
292
293 SLE_CONDVAR(CompanyProperties, terraform_limit, SLE_UINT32, SLV_156, SL_MAX_VERSION),
294 SLE_CONDVAR(CompanyProperties, clear_limit, SLE_UINT32, SLV_156, SL_MAX_VERSION),
295 SLE_CONDVAR(CompanyProperties, tree_limit, SLE_UINT32, SLV_175, SL_MAX_VERSION),
296
297 SLE_END()
298 };
299
300 static const SaveLoad _company_settings_desc[] = {
301 /* Engine renewal settings */
302 SLE_CONDNULL(512, SLV_16, SLV_19),
303 SLE_CONDREF(Company, engine_renew_list, REF_ENGINE_RENEWS, SLV_19, SL_MAX_VERSION),
304 SLE_CONDVAR(Company, settings.engine_renew, SLE_BOOL, SLV_16, SL_MAX_VERSION),
305 SLE_CONDVAR(Company, settings.engine_renew_months, SLE_INT16, SLV_16, SL_MAX_VERSION),
306 SLE_CONDVAR(Company, settings.engine_renew_money, SLE_UINT32, SLV_16, SL_MAX_VERSION),
307 SLE_CONDVAR(Company, settings.renew_keep_length, SLE_BOOL, SLV_2, SL_MAX_VERSION),
308
309 /* Default vehicle settings */
310 SLE_CONDVAR(Company, settings.vehicle.servint_ispercent, SLE_BOOL, SLV_120, SL_MAX_VERSION),
311 SLE_CONDVAR(Company, settings.vehicle.servint_trains, SLE_UINT16, SLV_120, SL_MAX_VERSION),
312 SLE_CONDVAR(Company, settings.vehicle.servint_roadveh, SLE_UINT16, SLV_120, SL_MAX_VERSION),
313 SLE_CONDVAR(Company, settings.vehicle.servint_aircraft, SLE_UINT16, SLV_120, SL_MAX_VERSION),
314 SLE_CONDVAR(Company, settings.vehicle.servint_ships, SLE_UINT16, SLV_120, SL_MAX_VERSION),
315
316 SLE_CONDNULL(63, SLV_2, SLV_144), // old reserved space
317
318 SLE_END()
319 };
320
321 static const SaveLoad _company_settings_skip_desc[] = {
322 /* Engine renewal settings */
323 SLE_CONDNULL(512, SLV_16, SLV_19),
324 SLE_CONDNULL(2, SLV_19, SLV_69), // engine_renew_list
325 SLE_CONDNULL(4, SLV_69, SL_MAX_VERSION), // engine_renew_list
326 SLE_CONDNULL(1, SLV_16, SL_MAX_VERSION), // settings.engine_renew
327 SLE_CONDNULL(2, SLV_16, SL_MAX_VERSION), // settings.engine_renew_months
328 SLE_CONDNULL(4, SLV_16, SL_MAX_VERSION), // settings.engine_renew_money
329 SLE_CONDNULL(1, SLV_2, SL_MAX_VERSION), // settings.renew_keep_length
330
331 /* Default vehicle settings */
332 SLE_CONDNULL(1, SLV_120, SL_MAX_VERSION), // settings.vehicle.servint_ispercent
333 SLE_CONDNULL(2, SLV_120, SL_MAX_VERSION), // settings.vehicle.servint_trains
334 SLE_CONDNULL(2, SLV_120, SL_MAX_VERSION), // settings.vehicle.servint_roadveh
335 SLE_CONDNULL(2, SLV_120, SL_MAX_VERSION), // settings.vehicle.servint_aircraft
336 SLE_CONDNULL(2, SLV_120, SL_MAX_VERSION), // settings.vehicle.servint_ships
337
338 SLE_CONDNULL(63, SLV_2, SLV_144), // old reserved space
339
340 SLE_END()
341 };
342
343 static const SaveLoad _company_economy_desc[] = {
344 /* these were changed to 64-bit in savegame format 2 */
345 SLE_CONDVAR(CompanyEconomyEntry, income, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_2),
346 SLE_CONDVAR(CompanyEconomyEntry, income, SLE_INT64, SLV_2, SL_MAX_VERSION),
347 SLE_CONDVAR(CompanyEconomyEntry, expenses, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_2),
348 SLE_CONDVAR(CompanyEconomyEntry, expenses, SLE_INT64, SLV_2, SL_MAX_VERSION),
349 SLE_CONDVAR(CompanyEconomyEntry, company_value, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_2),
350 SLE_CONDVAR(CompanyEconomyEntry, company_value, SLE_INT64, SLV_2, SL_MAX_VERSION),
351
352 SLE_CONDVAR(CompanyEconomyEntry, delivered_cargo[NUM_CARGO - 1], SLE_INT32, SL_MIN_VERSION, SLV_170),
353 SLE_CONDARR(CompanyEconomyEntry, delivered_cargo, SLE_UINT32, 32, SLV_170, SLV_EXTEND_CARGOTYPES),
354 SLE_CONDARR(CompanyEconomyEntry, delivered_cargo, SLE_UINT32, NUM_CARGO, SLV_EXTEND_CARGOTYPES, SL_MAX_VERSION),
355 SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32),
356
357 SLE_END()
358 };
359
360 /* We do need to read this single value, as the bigger it gets, the more data is stored */
361 struct CompanyOldAI {
362 uint8 num_build_rec;
363 };
364
365 static const SaveLoad _company_ai_desc[] = {
366 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_107),
367 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_13),
368 SLE_CONDNULL(4, SLV_13, SLV_107),
369 SLE_CONDNULL(8, SL_MIN_VERSION, SLV_107),
370 SLE_CONDVAR(CompanyOldAI, num_build_rec, SLE_UINT8, SL_MIN_VERSION, SLV_107),
371 SLE_CONDNULL(3, SL_MIN_VERSION, SLV_107),
372
373 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6),
374 SLE_CONDNULL(4, SLV_6, SLV_107),
375 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6),
376 SLE_CONDNULL(4, SLV_6, SLV_107),
377 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_107),
378
379 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6),
380 SLE_CONDNULL(4, SLV_6, SLV_107),
381 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6),
382 SLE_CONDNULL(4, SLV_6, SLV_107),
383 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_107),
384
385 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_69),
386 SLE_CONDNULL(4, SLV_69, SLV_107),
387
388 SLE_CONDNULL(18, SL_MIN_VERSION, SLV_107),
389 SLE_CONDNULL(20, SL_MIN_VERSION, SLV_107),
390 SLE_CONDNULL(32, SL_MIN_VERSION, SLV_107),
391
392 SLE_CONDNULL(64, SLV_2, SLV_107),
393 SLE_END()
394 };
395
396 static const SaveLoad _company_ai_build_rec_desc[] = {
397 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6),
398 SLE_CONDNULL(4, SLV_6, SLV_107),
399 SLE_CONDNULL(2, SL_MIN_VERSION, SLV_6),
400 SLE_CONDNULL(4, SLV_6, SLV_107),
401 SLE_CONDNULL(8, SL_MIN_VERSION, SLV_107),
402 SLE_END()
403 };
404
405 static const SaveLoad _company_livery_desc[] = {
406 SLE_CONDVAR(Livery, in_use, SLE_UINT8, SLV_34, SL_MAX_VERSION),
407 SLE_CONDVAR(Livery, colour1, SLE_UINT8, SLV_34, SL_MAX_VERSION),
408 SLE_CONDVAR(Livery, colour2, SLE_UINT8, SLV_34, SL_MAX_VERSION),
409 SLE_END()
410 };
411
412 static void SaveLoad_PLYR_common(Company *c, CompanyProperties *cprops)
413 {
414 int i;
415
416 SlObject(cprops, _company_desc);
417 if (c != nullptr) {
418 SlObject(c, _company_settings_desc);
419 } else {
420 char nothing;
421 SlObject(&nothing, _company_settings_skip_desc);
422 }
423
424 /* Keep backwards compatible for savegames, so load the old AI block */
425 if (IsSavegameVersionBefore(SLV_107) && cprops->is_ai) {
426 CompanyOldAI old_ai;
427 char nothing;
428
429 SlObject(&old_ai, _company_ai_desc);
430 for (i = 0; i != old_ai.num_build_rec; i++) {
431 SlObject(&nothing, _company_ai_build_rec_desc);
432 }
433 }
434
435 /* Write economy */
436 SlObject(&cprops->cur_economy, _company_economy_desc);
437
438 /* Write old economy entries. */
439 if (cprops->num_valid_stat_ent > lengthof(cprops->old_economy)) SlErrorCorrupt("Too many old economy entries");
440 for (i = 0; i < cprops->num_valid_stat_ent; i++) {
441 SlObject(&cprops->old_economy[i], _company_economy_desc);
442 }
443
444 /* Write each livery entry. */
445 int num_liveries = IsSavegameVersionBefore(SLV_63) ? LS_END - 4 : (IsSavegameVersionBefore(SLV_85) ? LS_END - 2: LS_END);
446 bool update_in_use = IsSavegameVersionBefore(SLV_GROUP_LIVERIES);
447 if (c != nullptr) {
448 for (i = 0; i < num_liveries; i++) {
449 SlObject(&c->livery[i], _company_livery_desc);
450 if (update_in_use && i != LS_DEFAULT) {
451 if (c->livery[i].in_use == 0) {
452 c->livery[i].colour1 = c->livery[LS_DEFAULT].colour1;
453 c->livery[i].colour2 = c->livery[LS_DEFAULT].colour2;
454 } else {
455 c->livery[i].in_use = 3;
456 }
457 }
458 }
459
460 if (num_liveries < LS_END) {
461 /* We want to insert some liveries somewhere in between. This means some have to be moved. */
462 memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0]));
463 c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL];
464 c->livery[LS_PASSENGER_WAGON_MAGLEV] = c->livery[LS_MAGLEV];
465 }
466
467 if (num_liveries == LS_END - 4) {
468 /* Copy bus/truck liveries over to trams */
469 c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS];
470 c->livery[LS_FREIGHT_TRAM] = c->livery[LS_TRUCK];
471 }
472 } else {
473 /* Skip liveries */
474 Livery dummy_livery;
475 for (i = 0; i < num_liveries; i++) {
476 SlObject(&dummy_livery, _company_livery_desc);
477 }
478 }
479 }
480
481 static void SaveLoad_PLYR(Company *c)
482 {
483 SaveLoad_PLYR_common(c, c);
484 }
485
486 static void Save_PLYR()
487 {
488 for (Company *c : Company::Iterate()) {
489 SlSetArrayIndex(c->index);
490 SlAutolength((AutolengthProc*)SaveLoad_PLYR, c);
491 }
492 }
493
494 static void Load_PLYR()
495 {
496 int index;
497 while ((index = SlIterateArray()) != -1) {
498 Company *c = new (index) Company();
499 SaveLoad_PLYR(c);
500 _company_colours[index] = (Colours)c->colour;
501 }
502 }
503
504 static void Check_PLYR()
505 {
506 int index;
507 while ((index = SlIterateArray()) != -1) {
508 CompanyProperties *cprops = new CompanyProperties();
509 SaveLoad_PLYR_common(nullptr, cprops);
510
511 /* We do not load old custom names */
512 if (IsSavegameVersionBefore(SLV_84)) {
513 if (GetStringTab(cprops->name_1) == TEXT_TAB_OLD_CUSTOM) {
514 cprops->name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
515 }
516
517 if (GetStringTab(cprops->president_name_1) == TEXT_TAB_OLD_CUSTOM) {
518 cprops->president_name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
519 }
520 }
521
522 if (cprops->name.empty() && !IsInsideMM(cprops->name_1, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_LAST + 1) &&
523 cprops->name_1 != STR_GAME_SAVELOAD_NOT_AVAILABLE && cprops->name_1 != STR_SV_UNNAMED &&
524 cprops->name_1 != SPECSTR_ANDCO_NAME && cprops->name_1 != SPECSTR_PRESIDENT_NAME &&
525 cprops->name_1 != SPECSTR_SILLY_NAME) {
526 cprops->name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
527 }
528
529 if (!_load_check_data.companies.Insert(index, cprops)) delete cprops;
530 }
531 }
532
533 static void Ptrs_PLYR()
534 {
535 for (Company *c : Company::Iterate()) {
536 SlObject(c, _company_settings_desc);
537 }
538 }
539
540
541 extern const ChunkHandler _company_chunk_handlers[] = {
542 { 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY | CH_LAST},
543 };
SLV_65
@ SLV_65
65 10210
Definition: saveload.h:121
CompanyProperties::is_ai
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
Definition: company_base.h:94
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
SLV_69
@ SLV_69
69 10319
Definition: saveload.h:125
SLV_144
@ SLV_144
144 20334
Definition: saveload.h:215
GetStringTab
static StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
Definition: strings_func.h:23
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:329
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
Pool::PoolItem<&_company_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:340
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
SLV_85
@ SLV_85
85 11874
Definition: saveload.h:145
CompanyManagerFace
uint32 CompanyManagerFace
Company manager face bits, info see in company_manager_face.h.
Definition: company_type.h:51
TRANSPORT_RAIL
@ TRANSPORT_RAIL
Transport by train.
Definition: transport_type.h:27
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:593
Station
Station data structure.
Definition: station_base.h:450
LEVELCROSSING_TRACKBIT_FACTOR
static const uint LEVELCROSSING_TRACKBIT_FACTOR
Multiplier for how many regular track bits a level crossing counts.
Definition: economy_type.h:224
SLV_112
@ SLV_112
112 15290
Definition: saveload.h:177
SLE_CONDARR
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of an array in some savegame versions.
Definition: saveload.h:572
INVALID_ROADTYPE
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition: road_type.h:27
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
GenderEthnicity
GenderEthnicity
The gender/race combinations that we have faces for.
Definition: company_manager_face.h:19
CompanyInfrastructure::water
uint32 water
Count of company owned track bits for canals.
Definition: company_base.h:34
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
ClampU
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:122
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:647
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
CompanyInfrastructure::station
uint32 station
Count of company owned station tiles.
Definition: company_base.h:35
TracksOverlap
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:653
MP_RAILWAY
@ MP_RAILWAY
A railway.
Definition: tile_type.h:42
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
CompanyProperties::name
std::string name
Name of the company if the user changed it.
Definition: company_base.h:58
GetTrackBits
static TrackBits GetTrackBits(TileIndex tile)
Gets the track bits of the given tile.
Definition: rail_map.h:136
HasSignals
static bool HasSignals(TileIndex t)
Checks if a rail tile has signals.
Definition: rail_map.h:72
SLV_104
@ SLV_104
104 14735
Definition: saveload.h:167
Company::infrastructure
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Definition: company_base.h:126
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLV_175
@ SLV_175
175 24136
Definition: saveload.h:253
SmallMap::Insert
bool Insert(const T &key, const U &data)
Adds new item to this map.
Definition: smallmap_type.hpp:127
SLV_GROUP_LIVERIES
@ SLV_GROUP_LIVERIES
205 PR#7108 Livery storage change and group liveries.
Definition: saveload.h:290
MP_ROAD
@ MP_ROAD
A tile with road (or tram tracks)
Definition: tile_type.h:43
SLV_100
@ SLV_100
100 13952
Definition: saveload.h:163
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:677
IsLevelCrossing
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:84
CompanyEconomyEntry
Statistics about the economy.
Definition: company_base.h:22
SLE_ARR
#define SLE_ARR(base, variable, type, length)
Storage of an array in every version of a savegame.
Definition: saveload.h:638
GetLockPart
static byte GetLockPart(TileIndex t)
Get the part of a lock.
Definition: water_map.h:320
CountBits
static uint CountBits(T value)
Counts the number of set bits in a variable.
Definition: bitmath_func.hpp:251
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition: base_station_base.h:270
GetPresentSignals
static uint GetPresentSignals(TileIndex tile)
Get whether the given signals are present (Along/AgainstTrackDir)
Definition: rail_map.h:393
GENDER_FEMALE
@ GENDER_FEMALE
This bit set means a female, otherwise male.
Definition: company_manager_face.h:20
IsInsideMM
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:204
TRANSPORT_ROAD
@ TRANSPORT_ROAD
Transport by road vehicle.
Definition: transport_type.h:28
GetRoadBits
static RoadBits GetRoadBits(TileIndex t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition: road_map.h:127
_company_colours
Colours _company_colours[MAX_COMPANIES]
NOSAVE: can be determined from company structs.
Definition: company_cmd.cpp:48
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
allow control codes in the strings
Definition: saveload.h:489
SLV_156
@ SLV_156
156 21728
Definition: saveload.h:230
MapSize
static uint MapSize()
Get the size of the map.
Definition: map_func.h:92
CompanyProperties::colour
byte colour
Company colour.
Definition: company_base.h:70
CompanyOldAI
Definition: company_sl.cpp:361
CompanyProperties::num_valid_stat_ent
byte num_valid_stat_ent
Number of valid statistical entries in old_economy.
Definition: company_base.h:99
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:80
IsNormalRoad
static bool IsNormalRoad(TileIndex t)
Return whether a tile is a normal road.
Definition: road_map.h:63
MP_OBJECT
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition: tile_type.h:51
IsBuoy
static bool IsBuoy(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:306
MP_WATER
@ MP_WATER
Water tile.
Definition: tile_type.h:47
GetStationType
static StationType GetStationType(TileIndex t)
Get the station type of this tile.
Definition: station_map.h:44
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
AfterLoadCompanyStats
void AfterLoadCompanyStats()
Rebuilding of company statistics after loading a savegame.
Definition: company_sl.cpp:94
IsShipDepot
static bool IsShipDepot(TileIndex t)
Is it a water tile with a ship depot on it?
Definition: water_map.h:216
TUNNELBRIDGE_TRACKBIT_FACTOR
static const uint TUNNELBRIDGE_TRACKBIT_FACTOR
Multiplier for how many regular track bits a tunnel/bridge counts.
Definition: economy_type.h:222
LOCK_PART_MIDDLE
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition: water_map.h:65
SLV_94
@ SLV_94
94 12816
Definition: saveload.h:155
Livery::in_use
byte in_use
Bit 0 set if this livery should override the default livery first colour, Bit 1 for the second colour...
Definition: livery.h:79
SLV_EXTEND_CARGOTYPES
@ SLV_EXTEND_CARGOTYPES
199 PR#6802 Extend cargotypes to 64
Definition: saveload.h:282
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:561
SLV_120
@ SLV_120
120 16439
Definition: saveload.h:187
settings
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:21
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
MP_TUNNELBRIDGE
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:50
REF_ENGINE_RENEWS
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition: saveload.h:396
CompanyInfrastructure::road
uint32 road[ROADTYPE_END]
Count of company owned track bits for each road type.
Definition: company_base.h:31
CompanyInfrastructure::signal
uint32 signal
Count of company owned signals.
Definition: company_base.h:32
IsRoadDepot
static bool IsRoadDepot(TileIndex t)
Return whether a tile is a road depot.
Definition: road_map.h:105
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
SlAutolength
void SlAutolength(AutolengthProc *proc, void *arg)
Do something of which I have no idea what it is :P.
Definition: saveload.cpp:1640
WATER_CLASS_CANAL
@ WATER_CLASS_CANAL
Canal.
Definition: water_map.h:49
RoadType
RoadType
The different roadtypes we support.
Definition: road_type.h:22
SLV_170
@ SLV_170
170 23826
Definition: saveload.h:247
SLV_107
@ SLV_107
107 15027
Definition: saveload.h:171
SLV_2
@ SLV_2
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition: saveload.h:34
GetTileOwner
static Owner GetTileOwner(TileIndex tile)
Returns the owner of a tile.
Definition: tile_map.h:178
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
LoadCheckData::companies
CompanyPropertiesMap companies
Company information.
Definition: fios.h:41
IsStationTileBlocked
bool IsStationTileBlocked(TileIndex tile)
Check whether a rail station tile is NOT traversable.
Definition: newgrf_station.cpp:869
SLV_16
@ SLV_16
16.0 2817 16.1 3155
Definition: saveload.h:60
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
GetWaterClass
static WaterClass GetWaterClass(TileIndex t)
Get the water class at a tile.
Definition: water_map.h:106
CompanyProperties::cur_economy
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Definition: company_base.h:97
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
IsPlainRail
static bool IsPlainRail(TileIndex t)
Returns whether this is plain rails, with or without signals.
Definition: rail_map.h:49
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
TRANSPORT_WATER
@ TRANSPORT_WATER
Transport over water.
Definition: transport_type.h:29
CompanyInfrastructure::rail
uint32 rail[RAILTYPE_END]
Count of company owned track bits for each rail type.
Definition: company_base.h:33
GetRailType
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:115
MP_STATION
@ MP_STATION
A tile of a station.
Definition: tile_type.h:46
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
IsLock
static bool IsLock(TileIndex t)
Is there a lock on a given water tile?
Definition: water_map.h:297
LOCK_DEPOT_TILE_FACTOR
static const uint LOCK_DEPOT_TILE_FACTOR
Multiplier for how many regular tiles a lock counts.
Definition: economy_type.h:230
CompanyProperties::president_name_1
StringID president_name_1
Name of the president if the user did not change it.
Definition: company_base.h:60
TrackBits
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:38
GetTunnelBridgeLength
static uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
Definition: tunnelbridge.h:25
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
GetRoadOwner
static Owner GetRoadOwner(TileIndex t, RoadTramType rtt)
Get the owner of a specific road type.
Definition: road_map.h:233
GetOtherTunnelBridgeEnd
static TileIndex GetOtherTunnelBridgeEnd(TileIndex t)
Determines type of the wormhole and returns its other end.
Definition: tunnelbridge_map.h:78
SLV_19
@ SLV_19
19 3396
Definition: saveload.h:65
SLV_58
@ SLV_58
58 9762
Definition: saveload.h:112
SetCompanyManagerFaceBits
static void SetCompanyManagerFaceBits(CompanyManagerFace &cmf, CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
Sets the company manager's face bits for the given company manager's face variable.
Definition: company_manager_face.h:111
GetTileType
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:96
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:318
GE_WM
@ GE_WM
A male of Caucasian origin (white)
Definition: company_manager_face.h:23
FACIL_AIRPORT
@ FACIL_AIRPORT
Station with an airport.
Definition: station_type.h:55
ConvertFromOldCompanyManagerFace
CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face)
Converts an old company manager's face format to the new company manager's face format.
Definition: company_sl.cpp:43
ScaleCompanyManagerFaceValue
static uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
Scales a company manager's face bits variable to the correct scope.
Definition: company_manager_face.h:164
SLV_4
@ SLV_4
4.0 1 4.1 122 0.3.3, 0.3.4 4.2 1222 0.3.5 4.3 1417 4.4 1426
Definition: saveload.h:37
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
GetTunnelBridgeTransportType
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Definition: tunnelbridge_map.h:39
Company
Definition: company_base.h:110
CompanyProperties::old_economy
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:98
ETHNICITY_BLACK
@ ETHNICITY_BLACK
This bit set means black, otherwise white.
Definition: company_manager_face.h:21
SLV_63
@ SLV_63
63 9956
Definition: saveload.h:118
Livery
Information about a particular livery.
Definition: livery.h:78
CompanyProperties
Statically loadable part of Company pool item.
Definition: company_base.h:55
SLV_34
@ SLV_34
34 6455
Definition: saveload.h:83
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
GE_WF
@ GE_WF
A female of Caucasian origin (white)
Definition: company_manager_face.h:24
SLV_1
@ SLV_1
1.0 0.1.x, 0.2.x
Definition: saveload.h:33
SLV_13
@ SLV_13
13.1 2080 0.4.0, 0.4.0.1
Definition: saveload.h:56
CompanyProperties::name_1
StringID name_1
Name of the company if the user did not change it.
Definition: company_base.h:57
Livery::colour2
byte colour2
Second colour, for vehicles with 2CC support.
Definition: livery.h:81
Livery::colour1
byte colour1
First colour, for all vehicles.
Definition: livery.h:80