OpenTTD Source 20250205-master-gfd85ab1e2c
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
12#include "saveload.h"
14
15#include "../company_func.h"
16#include "../company_manager_face.h"
17#include "../fios.h"
18#include "../tunnelbridge_map.h"
19#include "../tunnelbridge.h"
20#include "../station_base.h"
21#include "../strings_func.h"
22
23#include "table/strings.h"
24
25#include "../safeguards.h"
26
46{
47 CompanyManagerFace cmf = 0;
49
50 if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
51 if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
52
53 SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, ge, ge);
54 SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
55 SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
56 SetCompanyManagerFaceBits(cmf, CMFV_CHIN, ge, ScaleCompanyManagerFaceValue(CMFV_CHIN, ge, GB(face, 4, 2)));
57 SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS, ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face, 6, 4)));
58 SetCompanyManagerFaceBits(cmf, CMFV_HAIR, ge, ScaleCompanyManagerFaceValue(CMFV_HAIR, ge, GB(face, 16, 4)));
59 SetCompanyManagerFaceBits(cmf, CMFV_JACKET, ge, ScaleCompanyManagerFaceValue(CMFV_JACKET, ge, GB(face, 20, 2)));
60 SetCompanyManagerFaceBits(cmf, CMFV_COLLAR, ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR, ge, GB(face, 22, 2)));
61 SetCompanyManagerFaceBits(cmf, CMFV_GLASSES, ge, GB(face, 28, 1));
62
63 uint lips = GB(face, 10, 4);
64 if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
65 SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true);
66 SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE, ge, std::max(lips, 1U) - 1);
67 } else {
68 if (!HasBit(ge, GENDER_FEMALE)) {
69 lips = lips * 15 / 16;
70 lips -= 3;
71 if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
72 } else {
73 lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips);
74 }
75 SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips);
76
77 uint nose = GB(face, 13, 3);
78 if (ge == GE_WF) {
79 nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
80 } else {
81 nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose);
82 }
83 SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose);
84 }
85
86 uint tie_earring = GB(face, 24, 4);
87 if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
88 if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true);
89 SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2));
90 }
91
92 return cmf;
93}
94
97{
98 /* Reset infrastructure statistics to zero. */
99 for (Company *c : Company::Iterate()) c->infrastructure = {};
100
101 /* Collect airport count. */
102 for (const Station *st : Station::Iterate()) {
103 if ((st->facilities & FACIL_AIRPORT) && Company::IsValidID(st->owner)) {
104 Company::Get(st->owner)->infrastructure.airport++;
105 }
106 }
107
108 Company *c;
109 for (const auto tile : Map::Iterate()) {
110 switch (GetTileType(tile)) {
111 case MP_RAILWAY:
113 if (c != nullptr) {
114 uint pieces = 1;
115 if (IsPlainRail(tile)) {
116 TrackBits bits = GetTrackBits(tile);
117 pieces = CountBits(bits);
118 if (TracksOverlap(bits)) pieces *= pieces;
119 }
120 c->infrastructure.rail[GetRailType(tile)] += pieces;
121
123 }
124 break;
125
126 case MP_ROAD: {
127 if (IsLevelCrossing(tile)) {
129 if (c != nullptr) c->infrastructure.rail[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR;
130 }
131
132 /* Iterate all present road types as each can have a different owner. */
133 for (RoadTramType rtt : _roadtramtypes) {
134 RoadType rt = GetRoadType(tile, rtt);
135 if (rt == INVALID_ROADTYPE) continue;
136 c = Company::GetIfValid(IsRoadDepot(tile) ? GetTileOwner(tile) : GetRoadOwner(tile, rtt));
137 /* A level crossings and depots have two road bits. */
138 if (c != nullptr) c->infrastructure.road[rt] += IsNormalRoad(tile) ? CountBits(GetRoadBits(tile, rtt)) : 2;
139 }
140 break;
141 }
142
143 case MP_STATION:
145 if (c != nullptr && GetStationType(tile) != StationType::Airport && !IsBuoy(tile)) c->infrastructure.station++;
146
147 switch (GetStationType(tile)) {
148 case StationType::Rail:
149 case StationType::RailWaypoint:
150 if (c != nullptr && !IsStationTileBlocked(tile)) c->infrastructure.rail[GetRailType(tile)]++;
151 break;
152
153 case StationType::Bus:
154 case StationType::Truck:
155 case StationType::RoadWaypoint: {
156 /* Iterate all present road types as each can have a different owner. */
157 for (RoadTramType rtt : _roadtramtypes) {
158 RoadType rt = GetRoadType(tile, rtt);
159 if (rt == INVALID_ROADTYPE) continue;
160 c = Company::GetIfValid(GetRoadOwner(tile, rtt));
161 if (c != nullptr) c->infrastructure.road[rt] += 2; // A road stop has two road bits.
162 }
163 break;
164 }
165
166 case StationType::Dock:
167 case StationType::Buoy:
168 if (GetWaterClass(tile) == WATER_CLASS_CANAL) {
169 if (c != nullptr) c->infrastructure.water++;
170 }
171 break;
172
173 default:
174 break;
175 }
176 break;
177
178 case MP_WATER:
179 if (IsShipDepot(tile) || IsLock(tile)) {
181 if (c != nullptr) {
183 if (IsLock(tile) && GetLockPart(tile) == LOCK_PART_MIDDLE) {
184 /* The middle tile specifies the owner of the lock. */
185 c->infrastructure.water += 3 * LOCK_DEPOT_TILE_FACTOR; // the middle tile specifies the owner of the
186 break; // do not count the middle tile as canal
187 }
188 }
189 }
190 [[fallthrough]];
191
192 case MP_OBJECT:
193 if (GetWaterClass(tile) == WATER_CLASS_CANAL) {
195 if (c != nullptr) c->infrastructure.water++;
196 }
197 break;
198
199 case MP_TUNNELBRIDGE: {
200 /* Only count the tunnel/bridge if we're on the northern end tile. */
201 TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
202 if (tile < other_end) {
203 /* Count each tunnel/bridge TUNNELBRIDGE_TRACKBIT_FACTOR times to simulate
204 * the higher structural maintenance needs, and don't forget the end tiles. */
205 uint len = (GetTunnelBridgeLength(tile, other_end) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
206
207 switch (GetTunnelBridgeTransportType(tile)) {
208 case TRANSPORT_RAIL:
210 if (c != nullptr) c->infrastructure.rail[GetRailType(tile)] += len;
211 break;
212
213 case TRANSPORT_ROAD: {
214 /* Iterate all present road types as each can have a different owner. */
215 for (RoadTramType rtt : _roadtramtypes) {
216 RoadType rt = GetRoadType(tile, rtt);
217 if (rt == INVALID_ROADTYPE) continue;
218 c = Company::GetIfValid(GetRoadOwner(tile, rtt));
219 if (c != nullptr) c->infrastructure.road[rt] += len * 2; // A full diagonal road has two road bits.
220 }
221 break;
222 }
223
224 case TRANSPORT_WATER:
226 if (c != nullptr) c->infrastructure.water += len;
227 break;
228
229 default:
230 break;
231 }
232 }
233 break;
234 }
235
236 default:
237 break;
238 }
239 }
240}
241
242/* We do need to read this single value, as the bigger it gets, the more data is stored */
244 uint8_t num_build_rec;
245};
246
247class SlCompanyOldAIBuildRec : public DefaultSaveLoadHandler<SlCompanyOldAIBuildRec, CompanyOldAI> {
248public:
249 inline static const SaveLoad description[] = {{}}; // Needed to keep DefaultSaveLoadHandler happy.
250 inline const static SaveLoadCompatTable compat_description = _company_old_ai_buildrec_compat;
251
252 SaveLoadTable GetDescription() const override { return {}; }
253
254 void Load(CompanyOldAI *old_ai) const override
255 {
256 for (int i = 0; i != old_ai->num_build_rec; i++) {
257 SlObject(nullptr, this->GetLoadDescription());
258 }
259 }
260
261 void LoadCheck(CompanyOldAI *old_ai) const override { this->Load(old_ai); }
262};
263
264class SlCompanyOldAI : public DefaultSaveLoadHandler<SlCompanyOldAI, CompanyProperties> {
265public:
266 inline static const SaveLoad description[] = {
267 SLE_CONDVAR(CompanyOldAI, num_build_rec, SLE_UINT8, SL_MIN_VERSION, SLV_107),
269 };
270 inline const static SaveLoadCompatTable compat_description = _company_old_ai_compat;
271
272 void Load(CompanyProperties *c) const override
273 {
274 if (!c->is_ai) return;
275
276 CompanyOldAI old_ai;
277 SlObject(&old_ai, this->GetLoadDescription());
278 }
279
280 void LoadCheck(CompanyProperties *c) const override { this->Load(c); }
281};
282
283class SlCompanySettings : public DefaultSaveLoadHandler<SlCompanySettings, CompanyProperties> {
284public:
285 inline static const SaveLoad description[] = {
286 /* Engine renewal settings */
289 SLE_CONDVAR(CompanyProperties, settings.engine_renew_months, SLE_INT16, SLV_16, SL_MAX_VERSION),
290 SLE_CONDVAR(CompanyProperties, settings.engine_renew_money, SLE_UINT32, SLV_16, SL_MAX_VERSION),
291 SLE_CONDVAR(CompanyProperties, settings.renew_keep_length, SLE_BOOL, SLV_2, SL_MAX_VERSION),
292
293 /* Default vehicle settings */
294 SLE_CONDVAR(CompanyProperties, settings.vehicle.servint_ispercent, SLE_BOOL, SLV_120, SL_MAX_VERSION),
295 SLE_CONDVAR(CompanyProperties, settings.vehicle.servint_trains, SLE_UINT16, SLV_120, SL_MAX_VERSION),
296 SLE_CONDVAR(CompanyProperties, settings.vehicle.servint_roadveh, SLE_UINT16, SLV_120, SL_MAX_VERSION),
297 SLE_CONDVAR(CompanyProperties, settings.vehicle.servint_aircraft, SLE_UINT16, SLV_120, SL_MAX_VERSION),
298 SLE_CONDVAR(CompanyProperties, settings.vehicle.servint_ships, SLE_UINT16, SLV_120, SL_MAX_VERSION),
299 };
300 inline const static SaveLoadCompatTable compat_description = _company_settings_compat;
301
302 void Save(CompanyProperties *c) const override
303 {
304 SlObject(c, this->GetDescription());
305 }
306
307 void Load(CompanyProperties *c) const override
308 {
309 SlObject(c, this->GetLoadDescription());
310 }
311
312 void FixPointers(CompanyProperties *c) const override
313 {
314 SlObject(c, this->GetDescription());
315 }
316
317 void LoadCheck(CompanyProperties *c) const override { this->Load(c); }
318};
319
320class SlCompanyEconomy : public DefaultSaveLoadHandler<SlCompanyEconomy, CompanyProperties> {
321public:
322 inline static const SaveLoad description[] = {
323 SLE_CONDVAR(CompanyEconomyEntry, income, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_2),
325 SLE_CONDVAR(CompanyEconomyEntry, expenses, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_2),
326 SLE_CONDVAR(CompanyEconomyEntry, expenses, SLE_INT64, SLV_2, SL_MAX_VERSION),
327 SLE_CONDVAR(CompanyEconomyEntry, company_value, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_2),
328 SLE_CONDVAR(CompanyEconomyEntry, company_value, SLE_INT64, SLV_2, SL_MAX_VERSION),
329
330 SLE_CONDVAR(CompanyEconomyEntry, delivered_cargo[NUM_CARGO - 1], SLE_INT32, SL_MIN_VERSION, SLV_170),
331 SLE_CONDARR(CompanyEconomyEntry, delivered_cargo, SLE_UINT32, 32, SLV_170, SLV_EXTEND_CARGOTYPES),
333 SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32),
334 };
335 inline const static SaveLoadCompatTable compat_description = _company_economy_compat;
336
337 void Save(CompanyProperties *c) const override
338 {
339 SlObject(&c->cur_economy, this->GetDescription());
340 }
341
342 void Load(CompanyProperties *c) const override
343 {
344 SlObject(&c->cur_economy, this->GetLoadDescription());
345 }
346
347 void FixPointers(CompanyProperties *c) const override
348 {
349 SlObject(&c->cur_economy, this->GetDescription());
350 }
351
352 void LoadCheck(CompanyProperties *c) const override { this->Load(c); }
353};
354
356public:
357 void Save(CompanyProperties *c) const override
358 {
360 for (int i = 0; i < c->num_valid_stat_ent; i++) {
361 SlObject(&c->old_economy[i], this->GetDescription());
362 }
363 }
364
365 void Load(CompanyProperties *c) const override
366 {
368 c->num_valid_stat_ent = (uint8_t)SlGetStructListLength(UINT8_MAX);
369 }
370 if (c->num_valid_stat_ent > lengthof(c->old_economy)) SlErrorCorrupt("Too many old economy entries");
371
372 for (int i = 0; i < c->num_valid_stat_ent; i++) {
373 SlObject(&c->old_economy[i], this->GetLoadDescription());
374 }
375 }
376
377 void LoadCheck(CompanyProperties *c) const override { this->Load(c); }
378};
379
380class SlCompanyLiveries : public DefaultSaveLoadHandler<SlCompanyLiveries, CompanyProperties> {
381public:
382 inline static const SaveLoad description[] = {
383 SLE_CONDVAR(Livery, in_use, SLE_UINT8, SLV_34, SL_MAX_VERSION),
384 SLE_CONDVAR(Livery, colour1, SLE_UINT8, SLV_34, SL_MAX_VERSION),
385 SLE_CONDVAR(Livery, colour2, SLE_UINT8, SLV_34, SL_MAX_VERSION),
386 };
387 inline const static SaveLoadCompatTable compat_description = _company_liveries_compat;
388
393 size_t GetNumLiveries() const
394 {
395 if (IsSavegameVersionBefore(SLV_63)) return LS_END - 4;
396 if (IsSavegameVersionBefore(SLV_85)) return LS_END - 2;
398 /* Read from the savegame how long the list is. */
399 return SlGetStructListLength(LS_END);
400 }
401
402 void Save(CompanyProperties *c) const override
403 {
404 SlSetStructListLength(LS_END);
405 for (int i = 0; i < LS_END; i++) {
406 SlObject(&c->livery[i], this->GetDescription());
407 }
408 }
409
410 void Load(CompanyProperties *c) const override
411 {
412 size_t num_liveries = this->GetNumLiveries();
413 bool update_in_use = IsSavegameVersionBefore(SLV_GROUP_LIVERIES);
414
415 for (size_t i = 0; i < num_liveries; i++) {
416 SlObject(&c->livery[i], this->GetLoadDescription());
417 if (update_in_use && i != LS_DEFAULT) {
418 if (c->livery[i].in_use == 0) {
419 c->livery[i].colour1 = c->livery[LS_DEFAULT].colour1;
420 c->livery[i].colour2 = c->livery[LS_DEFAULT].colour2;
421 } else {
422 c->livery[i].in_use = 3;
423 }
424 }
425 }
426
428 /* We want to insert some liveries somewhere in between. This means some have to be moved. */
429 memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0]));
430 c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL];
431 c->livery[LS_PASSENGER_WAGON_MAGLEV] = c->livery[LS_MAGLEV];
432 }
433
435 /* Copy bus/truck liveries over to trams */
436 c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS];
437 c->livery[LS_FREIGHT_TRAM] = c->livery[LS_TRUCK];
438 }
439 }
440
441 void LoadCheck(CompanyProperties *c) const override { this->Load(c); }
442};
443
444class SlAllowListData : public VectorSaveLoadHandler<SlAllowListData, CompanyProperties, std::string> {
445public:
446 struct KeyWrapper {
447 std::string key;
448 };
449
450 inline static const SaveLoad description[] = {
451 SLE_SSTR(KeyWrapper, key, SLE_STR),
452 };
453 inline const static SaveLoadCompatTable compat_description = {};
454
455 std::vector<std::string> &GetVector(CompanyProperties *cprops) const override { return cprops->allow_list; }
456
457 void LoadCheck(CompanyProperties *cprops) const override { this->Load(cprops); }
458};
459
460/* Save/load of companies */
461static const SaveLoad _company_desc[] = {
462 SLE_VAR(CompanyProperties, name_2, SLE_UINT32),
463 SLE_VAR(CompanyProperties, name_1, SLE_STRINGID),
465
466 SLE_VAR(CompanyProperties, president_name_1, SLE_STRINGID),
467 SLE_VAR(CompanyProperties, president_name_2, SLE_UINT32),
469
472
473 SLE_VAR(CompanyProperties, face, SLE_UINT32),
474
475 /* money was changed to a 64 bit field in savegame version 1. */
476 SLE_CONDVAR(CompanyProperties, money, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SLV_1),
478
479 SLE_CONDVAR(CompanyProperties, current_loan, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SLV_65),
480 SLE_CONDVAR(CompanyProperties, current_loan, SLE_INT64, SLV_65, SL_MAX_VERSION),
482
483 SLE_VAR(CompanyProperties, colour, SLE_UINT8),
484 SLE_VAR(CompanyProperties, money_fraction, SLE_UINT8),
485 SLE_VAR(CompanyProperties, block_preview, SLE_UINT8),
486
487 SLE_CONDVAR(CompanyProperties, location_of_HQ, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
488 SLE_CONDVAR(CompanyProperties, location_of_HQ, SLE_UINT32, SLV_6, SL_MAX_VERSION),
489 SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
490 SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_UINT32, SLV_6, SL_MAX_VERSION),
491 SLE_CONDVAR(CompanyProperties, inaugurated_year, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
492 SLE_CONDVAR(CompanyProperties, inaugurated_year, SLE_INT32, SLV_31, SL_MAX_VERSION),
493 SLE_CONDVAR(CompanyProperties, inaugurated_year_calendar, SLE_INT32, SLV_COMPANY_INAUGURATED_PERIOD_V2, SL_MAX_VERSION),
494
496
497 SLE_VAR(CompanyProperties, months_of_bankruptcy, SLE_UINT8),
498 SLE_CONDVAR(CompanyProperties, bankrupt_asked, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_104),
499 SLE_CONDVAR(CompanyProperties, bankrupt_asked, SLE_UINT16, SLV_104, SL_MAX_VERSION),
500 SLE_VAR(CompanyProperties, bankrupt_timeout, SLE_INT16),
501 SLE_CONDVAR(CompanyProperties, bankrupt_value, SLE_VAR_I64 | SLE_FILE_I32, SL_MIN_VERSION, SLV_65),
502 SLE_CONDVAR(CompanyProperties, bankrupt_value, SLE_INT64, SLV_65, SL_MAX_VERSION),
503
504 /* yearly expenses was changed to 64-bit in savegame version 2. */
505 SLE_CONDARR(CompanyProperties, yearly_expenses, SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, SL_MIN_VERSION, SLV_2),
506 SLE_CONDARR(CompanyProperties, yearly_expenses, SLE_INT64, 3 * 13, SLV_2, SL_MAX_VERSION),
507
509
510 SLE_CONDVAR(CompanyProperties, terraform_limit, SLE_UINT32, SLV_156, SL_MAX_VERSION),
511 SLE_CONDVAR(CompanyProperties, clear_limit, SLE_UINT32, SLV_156, SL_MAX_VERSION),
512 SLE_CONDVAR(CompanyProperties, tree_limit, SLE_UINT32, SLV_175, SL_MAX_VERSION),
513 SLEG_STRUCT("settings", SlCompanySettings),
515 SLEG_STRUCT("cur_economy", SlCompanyEconomy),
516 SLEG_STRUCTLIST("old_economy", SlCompanyOldEconomy),
518};
519
521 PLYRChunkHandler() : ChunkHandler('PLYR', CH_TABLE) {}
522
523 void Save() const override
524 {
525 SlTableHeader(_company_desc);
526
527 for (Company *c : Company::Iterate()) {
528 SlSetArrayIndex(c->index);
529 SlObject(c, _company_desc);
530 }
531 }
532
533 void Load() const override
534 {
535 const std::vector<SaveLoad> slt = SlCompatTableHeader(_company_desc, _company_sl_compat);
536
537 int index;
538 while ((index = SlIterateArray()) != -1) {
539 Company *c = new (index) Company();
540 SlObject(c, slt);
541 _company_colours[index] = c->colour;
542 }
543 }
544
545
546 void LoadCheck(size_t) const override
547 {
548 const std::vector<SaveLoad> slt = SlCompatTableHeader(_company_desc, _company_sl_compat);
549
550 int index;
551 while ((index = SlIterateArray()) != -1) {
552 std::unique_ptr<CompanyProperties> cprops = std::make_unique<CompanyProperties>();
553 SlObject(cprops.get(), slt);
554
555 /* We do not load old custom names */
557 if (GetStringTab(cprops->name_1) == TEXT_TAB_OLD_CUSTOM) {
558 cprops->name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
559 }
560
561 if (GetStringTab(cprops->president_name_1) == TEXT_TAB_OLD_CUSTOM) {
562 cprops->president_name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
563 }
564 }
565
566 if (cprops->name.empty() && !IsInsideMM(cprops->name_1, SPECSTR_COMPANY_NAME_START, SPECSTR_COMPANY_NAME_END) &&
567 cprops->name_1 != STR_GAME_SAVELOAD_NOT_AVAILABLE && cprops->name_1 != STR_SV_UNNAMED &&
568 cprops->name_1 != SPECSTR_ANDCO_NAME && cprops->name_1 != SPECSTR_PRESIDENT_NAME &&
569 cprops->name_1 != SPECSTR_SILLY_NAME) {
570 cprops->name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
571 }
572
573 if (_load_check_data.companies.count(index) == 0) {
574 _load_check_data.companies[index] = std::move(cprops);
575 }
576 }
577 }
578
579 void FixPointers() const override
580 {
581 for (Company *c : Company::Iterate()) {
582 SlObject(c, _company_desc);
583 }
584 }
585};
586
587static const PLYRChunkHandler PLYR;
588static const ChunkHandlerRef company_chunk_handlers[] = {
589 PLYR,
590};
591
592extern const ChunkHandlerTable _company_chunk_handlers(company_chunk_handlers);
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:74
Default handler for saving/loading an object to/from disk.
Definition saveload.h:582
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
std::vector< std::string > & GetVector(CompanyProperties *cprops) const override
Get instance of vector to load/save.
size_t GetNumLiveries() const
Get the number of liveries used by this savegame version.
SaveLoadTable GetDescription() const override
Get the description of the fields in the savegame.
Default handler for saving/loading a vector to/from disk.
Definition saveload.h:1366
Colours _company_colours[MAX_COMPANIES]
NOSAVE: can be determined from company structs.
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.
GenderEthnicity
The gender/race combinations that we have faces for.
@ GE_WF
A female of Caucasian origin (white)
@ GE_WM
A male of Caucasian origin (white)
@ ETHNICITY_BLACK
This bit set means black, otherwise white.
@ GENDER_FEMALE
This bit set means a female, otherwise male.
uint ScaleCompanyManagerFaceValue(CompanyManagerFaceVariable cmfv, GenderEthnicity ge, uint val)
Scales a company manager's face bits variable to the correct scope.
void AfterLoadCompanyStats()
Rebuilding of company statistics after loading a savegame.
CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32_t face)
Converts an old company manager's face format to the new company manager's face format.
Loading of company chunks before table headers were added.
const SaveLoadCompat _company_liveries_compat[]
Original field order for SlCompanyLiveries.
const SaveLoadCompat _company_old_ai_compat[]
Original field order for SlCompanyOldAI.
const SaveLoadCompat _company_old_ai_buildrec_compat[]
Original field order for SlCompanyOldAIBuildRec.
const SaveLoadCompat _company_economy_compat[]
Original field order for SlCompanyEconomy.
const SaveLoadCompat _company_sl_compat[]
Original field order for company_desc.
const SaveLoadCompat _company_settings_compat[]
Original field order for SlCompanySettings.
uint32_t CompanyManagerFace
Company manager face bits, info see in company_manager_face.h.
static const uint LOCK_DEPOT_TILE_FACTOR
Multiplier for how many regular tiles a lock counts.
static const uint LEVELCROSSING_TRACKBIT_FACTOR
Multiplier for how many regular track bits a level crossing counts.
static const uint TUNNELBRIDGE_TRACKBIT_FACTOR
Multiplier for how many regular track bits a tunnel/bridge counts.
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition fios_gui.cpp:41
fluid_settings_t * settings
FluidSynth settings handle.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
TrackBits GetTrackBits(Tile tile)
Gets the track bits of the given tile.
Definition rail_map.h:136
uint GetPresentSignals(Tile tile)
Get whether the given signals are present (Along/AgainstTrackDir)
Definition rail_map.h:393
static debug_inline bool IsPlainRail(Tile t)
Returns whether this is plain rails, with or without signals.
Definition rail_map.h:49
bool HasSignals(Tile t)
Checks if a rail tile has signals.
Definition rail_map.h:72
static debug_inline bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:106
RoadBits GetRoadBits(Tile t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition road_map.h:128
Owner GetRoadOwner(Tile t, RoadTramType rtt)
Get the owner of a specific road type.
Definition road_map.h:234
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:85
static debug_inline bool IsNormalRoad(Tile t)
Return whether a tile is a normal road.
Definition road_map.h:64
RoadType
The different roadtypes we support.
Definition road_type.h:25
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:30
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
size_t SlGetStructListLength(size_t limit)
Get the length of this list; if it exceeds the limit, error out.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:662
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:354
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
void SlSetStructListLength(size_t length)
Set the length of this list.
Functions/types related to saving and loading games.
#define SLEG_STRUCTLIST(name, handler)
Storage of a list of structs in every savegame version.
Definition saveload.h:1242
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:689
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:510
@ REF_ENGINE_RENEWS
Load/save a reference to an engine renewal (autoreplace).
Definition saveload.h:608
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:513
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of a fixed-size array of SL_VAR elements in some savegame versions.
Definition saveload.h:901
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:519
#define SLEG_STRUCT(name, handler)
Storage of a structs in every savegame version.
Definition saveload.h:1219
#define SLEG_CONDSTRUCT(name, handler, from, to)
Storage of a structs in some savegame versions.
Definition saveload.h:1150
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:934
#define SLE_CONDVECTOR(base, variable, type, from, to)
Storage of a vector of SL_VAR elements in some savegame versions.
Definition saveload.h:975
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:869
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1264
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1047
@ SLV_COMPANY_INAUGURATED_PERIOD_V2
349 PR#13448 Fix savegame storage for company inaugurated year in wallclock mode.
Definition saveload.h:398
@ SLV_175
175 24136
Definition saveload.h:253
@ SLV_16
16.0 2817 16.1 3155
Definition saveload.h:60
@ SLV_84
84 11822
Definition saveload.h:143
@ SLV_107
107 15027
Definition saveload.h:171
@ SLV_85
85 11874
Definition saveload.h:145
@ SLV_6
6.0 1721 6.1 1768
Definition saveload.h:46
@ SLV_34
34 6455
Definition saveload.h:83
@ SLV_EXTEND_CARGOTYPES
199 PR#6802 Extend cargotypes to 64
Definition saveload.h:282
@ SLV_SAVELOAD_LIST_LENGTH
293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST.
Definition saveload.h:331
@ SLV_65
65 10210
Definition saveload.h:121
@ SLV_120
120 16439
Definition saveload.h:187
@ SLV_170
170 23826
Definition saveload.h:247
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:400
@ SLV_1
1.0 0.1.x, 0.2.x
Definition saveload.h:33
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
@ SLV_63
63 9956
Definition saveload.h:118
@ SLV_104
104 14735
Definition saveload.h:167
@ SLV_2
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition saveload.h:34
@ SLV_GROUP_LIVERIES
205 PR#7108 Livery storage change and group liveries.
Definition saveload.h:290
@ SLV_COMPANY_ALLOW_LIST_V2
341 PR#12908 Fixed savegame format for saving of list of client keys that are allowed to join this co...
Definition saveload.h:389
@ SLV_COMPANY_ALLOW_LIST
335 PR#12337 Saving of list of client keys that are allowed to join this company.
Definition saveload.h:382
@ SLV_19
19 3396
Definition saveload.h:65
@ SLV_156
156 21728
Definition saveload.h:230
@ SLV_31
31 5999
Definition saveload.h:80
@ SLV_MAX_LOAN_FOR_COMPANY
330 PR#11224 Separate max loan for each company.
Definition saveload.h:376
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition saveload.h:890
std::span< const struct SaveLoad > SaveLoadTable
A table of SaveLoad entries.
Definition saveload.h:516
#define SLEG_CONDSTRUCTLIST(name, handler, from, to)
Storage of a list of structs in some savegame versions.
Definition saveload.h:1179
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1003
StationType GetStationType(Tile t)
Get the station type of this tile.
Definition station_map.h:44
bool IsBuoy(Tile t)
Is tile t a buoy tile?
bool IsStationTileBlocked(Tile t)
Is tile t a blocked tile?
@ FACIL_AIRPORT
Station with an airport.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:277
StringTab GetStringTab(StringID str)
Extract the StringTab from a StringID.
static constexpr StringID SPECSTR_COMPANY_NAME_START
Special strings for company names on the form "TownName transport".
static constexpr StringID SPECSTR_SILLY_NAME
Special string for silly company names.
static constexpr StringID SPECSTR_ANDCO_NAME
Special string for Surname & Co company names.
static constexpr StringID SPECSTR_PRESIDENT_NAME
Special string for the president's name.
Handlers and description of chunk.
Definition saveload.h:464
Statistics about the economy.
std::array< uint32_t, ROADTYPE_END > road
Count of company owned track bits for each road type.
uint32_t station
Count of company owned station tiles.
uint32_t signal
Count of company owned signals.
std::array< uint32_t, RAILTYPE_END > rail
Count of company owned track bits for each rail type.
uint32_t water
Count of company owned track bits for canals.
Statically loadable part of Company pool item.
NetworkAuthorizedKeys allow_list
Public keys of clients that are allowed to join this company.
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Colours colour
Company colour.
uint8_t num_valid_stat_ent
Number of valid statistical entries in old_economy.
CompanyInfrastructure infrastructure
NOSAVE: Counts of company owned infrastructure.
Information about a particular livery.
Definition livery.h:78
Colours colour2
Second colour, for vehicles with 2CC support.
Definition livery.h:81
Colours colour1
First colour, for all vehicles.
Definition livery.h:80
uint8_t 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
CompanyPropertiesMap companies
Company information.
Definition fios.h:43
Size related data of the map.
Definition map_func.h:206
void Save() const override
Save the chunk.
void Load() const override
Load the chunk.
void FixPointers() const override
Fix the pointers.
void LoadCheck(size_t) const override
Load the chunk for game preview.
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static Titem * Get(size_t index)
Returns Titem with given index.
SaveLoad type struct.
Definition saveload.h:718
Station data structure.
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
@ MP_TUNNELBRIDGE
Tunnel entry/exit and bridge heads.
Definition tile_type.h:57
@ MP_WATER
Water tile.
Definition tile_type.h:54
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition tile_type.h:58
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:645
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
@ TRANSPORT_RAIL
Transport by train.
@ TRANSPORT_ROAD
Transport by road vehicle.
@ TRANSPORT_WATER
Transport over water.
uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
TransportType GetTunnelBridgeTransportType(Tile t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
TileIndex GetOtherTunnelBridgeEnd(Tile t)
Determines type of the wormhole and returns its other end.
bool IsShipDepot(Tile t)
Is it a water tile with a ship depot on it?
Definition water_map.h:222
@ WATER_CLASS_CANAL
Canal.
Definition water_map.h:41
WaterClass GetWaterClass(Tile t)
Get the water class at a tile.
Definition water_map.h:112
uint8_t GetLockPart(Tile t)
Get the part of a lock.
Definition water_map.h:326
@ LOCK_PART_MIDDLE
Middle part of a lock.
Definition water_map.h:66
bool IsLock(Tile t)
Is there a lock on a given water tile?
Definition water_map.h:303