OpenTTD Source 20261112-master-gbcbec5e63f
oldloader_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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../town.h"
12#include "../industry.h"
13#include "../company_func.h"
14#include "../aircraft.h"
15#include "../roadveh.h"
16#include "../ship.h"
17#include "../train.h"
18#include "../signs_base.h"
19#include "../station_base.h"
20#include "../subsidy_base.h"
21#include "../debug.h"
22#include "../depot_base.h"
25#include "../vehicle_func.h"
27#include "../engine_func.h"
28#include "../company_base.h"
29#include "../disaster_vehicle.h"
30#include "../string_func.h"
31#include "../timer/timer.h"
34#include "saveload_error.hpp"
35#include "saveload_internal.h"
36#include "oldloader.h"
37
38#include "table/strings.h"
39#include "../table/engines.h"
40#include "../table/townname.h"
41
42#include "../safeguards.h"
43
45static uint16_t _old_extra_chunk_nums;
46
47void FixOldMapArray()
48{
49 /* TTO/TTD/TTDP savegames could have buoys at tile 0
50 * (without assigned station struct) */
51 MakeSea(0);
52}
53
54static void FixTTDMapArray()
55{
56 for (auto tile : Map::Iterate()) {
57 switch (GetTileType(tile)) {
59 tile.m4() = 0; // We do not understand this TTDP station mapping (yet)
60 switch (tile.m5()) {
61 /* We have drive through stops at a totally different place */
62 case 0x53: case 0x54: tile.m5() += 170 - 0x53; break; // Bus drive through
63 case 0x57: case 0x58: tile.m5() += 168 - 0x57; break; // Truck drive through
64 case 0x55: case 0x56: tile.m5() += 170 - 0x55; break; // Bus tram stop
65 case 0x59: case 0x5A: tile.m5() += 168 - 0x59; break; // Truck tram stop
66 default: break;
67 }
68 break;
69
71 /* We save presignals different from TTDPatch, convert them */
72 if (GB(tile.m5(), 6, 2) == 1) { // RailTileType::Signals
73 /* This byte is always zero in TTD for this type of tile */
74 if (tile.m4()) { // Convert the presignals to our own format
75 tile.m4() = (tile.m4() >> 1) & 7;
76 }
77 }
78 /* TTDPatch stores PBS things in L6 and all elsewhere; so we'll just
79 * clear it for ourselves and let OTTD's rebuild PBS itself */
80 tile.m4() &= 0xF; // Only keep the lower four bits; upper four is PBS
81 break;
82
83 case TileType::Water:
84 /* if water class == 3, make river there */
85 if (GB(tile.m3(), 0, 2) == 3) {
88 tile.m2() = 0;
89 tile.m3() = 2; // WaterClass::River
90 tile.m4() = Random();
91 tile.m5() = 0;
92 }
93 break;
94
95 default:
96 break;
97 }
98 }
99
100 FixOldMapArray();
101}
102
103static void FixTTDDepots()
104{
105 for (const Depot *d : Depot::Iterate(252)) {
106 if (!IsDepotTile(d->xy) || GetDepotIndex(d->xy) != d->index) {
108 delete d;
109 }
110 }
111}
112
113static uint32_t RemapOldTownName(uint32_t townnameparts, uint8_t old_town_name_type)
114{
115 auto fix_num = [](uint32_t i, uint32_t n, uint8_t s) -> uint32_t {
116 return ((i << 16) / n + 1) << s;
117 };
118
119 switch (old_town_name_type) {
120 case 0: case 3: // English, American
121 /* Already OK */
122 return townnameparts;
123
124 case 1: // French
125 /* For some reason 86 needs to be subtracted from townnameparts
126 * 0000 0000 0000 0000 0000 0000 1111 1111 */
127 return fix_num(townnameparts - 86, lengthof(_name_french_real), 0);
128
129 case 2: // German
130 Debug(Facility::Misc, Severity::Critical, "German Townnames are buggy ({})", townnameparts);
131 return townnameparts;
132
133 case 4: // Latin-American
134 /* 0000 0000 0000 0000 0000 0000 1111 1111 */
135 return fix_num(townnameparts, lengthof(_name_spanish_real), 0);
136
137 case 5: // Silly
138 /* NUM_SILLY_1 - lower 16 bits
139 * NUM_SILLY_2 - upper 16 bits without leading 1 (first 8 bytes)
140 * 1000 0000 2222 2222 0000 0000 1111 1111 */
141 return fix_num(townnameparts, lengthof(_name_silly_1), 0) | fix_num(GB(townnameparts, 16, 8), lengthof(_name_silly_2), 16);
142 }
143 return 0;
144}
145
146static void FixOldTowns()
147{
148 /* Convert town-names if needed */
149 for (Town *town : Town::Iterate()) {
150 if (IsInsideMM(town->townnametype, 0x20C1, 0x20C3)) {
151 town->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
152 town->townnameparts = RemapOldTownName(town->townnameparts, _settings_game.game_creation.town_name);
153 }
154 }
155}
156
164{
165 for (Vehicle *v : Vehicle::Iterate()) {
166 if ((size_t)v->next == 0xFFFF) {
167 v->next = nullptr;
168 } else {
169 v->next = Vehicle::GetIfValid((size_t)v->next);
170 }
171
172 /* For some reason we need to correct for this */
173 switch (v->spritenum) {
174 case 0xfd: break;
175 case 0xff: v->spritenum = 0xfe; break;
176 default: v->spritenum >>= 1; break;
177 }
178
179 /* Vehicle-subtype is different in TTD(Patch) */
180 if (v->type == VehicleType::Effect) v->subtype = v->subtype >> 1;
181
182 v->name = CopyFromOldName(ls.vehicle_names[v->index.base()]);
183
184 /* We haven't used this bit for stations for ages */
185 if (v->type == VehicleType::Road) {
187 if (rv->state != RVSB_IN_DEPOT && rv->state != RVSB_WORMHOLE) {
188 ClrBit(rv->state, 2);
189 Tile tile(rv->tile);
190 if (IsTileType(tile, TileType::Station) && tile.m5() >= 168) {
191 /* Update the vehicle's road state to show we're in a drive through road stop. */
193 }
194 }
195 }
196
197 /* The subtype should be 0, but it sometimes isn't :( */
198 if (v->type == VehicleType::Road || v->type == VehicleType::Ship) v->subtype = 0;
199
200 /* Sometimes primary vehicles would have a nothing (invalid) order
201 * or vehicles that could not have an order would still have a
202 * (loading) order which causes assertions and the like later on.
203 */
205 (v->IsPrimaryVehicle() && v->current_order.IsType(OT_NOTHING))) {
206 v->current_order.MakeDummy();
207 }
208
209 /* Shared orders are fixed in AfterLoadVehicles now */
210 }
211}
212
213static bool FixTTOMapArray()
214{
215 for (auto tile : Map::Iterate()) {
216 TileType tt = GetTileType(tile);
217 if (to_underlying(tt) == 11) {
218 /* TTO has a different way of storing monorail.
219 * Instead of using bits in m3 it uses a different tile type. */
220 tile.m3() = 1; // rail type = monorail (in TTD)
222 tile.m2() = 1; // set monorail ground to RailGroundType::Grass
224 }
225
226 switch (tt) {
227 case TileType::Clear:
228 break;
229
231 switch (GB(tile.m5(), 6, 2)) {
232 case 0: // RailTileType::Normal
233 break;
234 case 1: // RailTileType::Signals
235 tile.m4() = (~tile.m5() & 1) << 2; // signal variant (present only in OTTD)
236 SB(tile.m2(), 6, 2, GB(tile.m5(), 3, 2)); // signal status
237 tile.m3() |= 0xC0; // both signals are present
238 tile.m5() = HasBit(tile.m5(), 5) ? 2 : 1; // track direction (only X or Y)
239 tile.m5() |= 0x40; // RailTileType::Signals
240 break;
241 case 3: // RailTileType::Depot
242 tile.m2() = 0;
243 break;
244 default:
245 return false;
246 }
247 break;
248
249 case TileType::Road: // road (depot) or level crossing
250 switch (GB(tile.m5(), 4, 4)) {
251 case 0: // RoadTileType::Normal
252 if (tile.m2() == 4) tile.m2() = 5; // 'small trees' -> Roadside::Trees
253 break;
254 case 1: // RoadTileType::Crossing (there aren't monorail crossings in TTO)
255 tile.m3() = tile.m1(); // set owner of road = owner of rail
256 break;
257 case 2: // RoadTileType::Depot
258 break;
259 default:
260 return false;
261 }
262 break;
263
264 case TileType::House:
265 tile.m3() = tile.m2() & 0xC0; // construction stage
266 tile.m2() &= 0x3F; // building type
267 if (tile.m2() >= 5) tile.m2()++; // skip "large office block on snow"
268 break;
269
270 case TileType::Trees:
271 tile.m3() = GB(tile.m5(), 3, 3); // type of trees
272 tile.m5() &= 0xC7; // number of trees and growth status
273 break;
274
276 tile.m3() = (tile.m5() >= 0x08 && tile.m5() <= 0x0F) ? 1 : 0; // monorail -> 1, others 0 (rail, road, airport, dock)
277 if (tile.m5() >= 8) tile.m5() -= 8; // shift for monorail
278 if (tile.m5() >= 0x42) tile.m5()++; // skip heliport
279 break;
280
281 case TileType::Water:
282 tile.m3() = tile.m2() = 0;
283 break;
284
285 case TileType::Void:
286 tile.m2() = tile.m3() = tile.m5() = 0;
287 break;
288
290 tile.m3() = 0;
291 switch (tile.m5()) {
292 case 0x24: // farm silo
293 tile.m5() = 0x25;
294 break;
295 case 0x25: case 0x27: // farm
296 case 0x28: case 0x29: case 0x2A: case 0x2B: // factory
297 tile.m5()--;
298 break;
299 default:
300 if (tile.m5() >= 0x2C) tile.m5() += 3; // iron ore mine, steel mill or bank
301 break;
302 }
303 break;
304
306 if (HasBit(tile.m5(), 7)) { // bridge
307 uint8_t m5 = tile.m5();
308 tile.m5() = m5 & 0xE1; // copy bits 7..5, 1
309 if (GB(m5, 1, 2) == 1) tile.m5() |= 0x02; // road bridge
310 if (GB(m5, 1, 2) == 3) tile.m2() |= 0xA0; // monorail bridge -> tubular, steel bridge
311 if (!HasBit(m5, 6)) { // bridge head
312 tile.m3() = (GB(m5, 1, 2) == 3) ? 1 : 0; // track subtype (1 for monorail, 0 for others)
313 } else { // middle bridge part
314 tile.m3() = HasBit(m5, 2) ? 0x10 : 0; // track subtype on bridge
315 if (GB(m5, 3, 2) == 3) tile.m3() |= 1; // track subtype under bridge
316 if (GB(m5, 3, 2) == 1) tile.m5() |= 0x08; // set for road/water under (0 for rail/clear)
317 }
318 } else { // tunnel entrance/exit
319 tile.m2() = 0;
320 tile.m3() = HasBit(tile.m5(), 3); // monorail
321 tile.m5() &= HasBit(tile.m5(), 3) ? 0x03 : 0x07 ; // direction, transport type (== 0 for rail)
322 }
323 break;
324
325 case TileType::Object:
326 tile.m2() = 0;
327 tile.m3() = 0;
328 break;
329
330 default:
331 return false;
332
333 }
334 }
335
336 FixOldMapArray();
337
338 return true;
339}
340
341static Engine *_old_engines;
342
343static bool FixTTOEngines()
344{
345 using OldEngineID = uint8_t;
347 static const OldEngineID ttd_to_tto[] = {
348 0, 255, 255, 255, 255, 255, 255, 255, 5, 7, 8, 9, 10, 11, 12, 13,
349 255, 255, 255, 255, 255, 255, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
350 25, 26, 27, 29, 28, 30, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
351 255, 255, 255, 255, 255, 255, 255, 31, 255, 32, 33, 34, 35, 36, 37, 38,
352 39, 40, 41, 42, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
353 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
354 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
355 255, 255, 255, 255, 44, 45, 46, 255, 255, 255, 255, 47, 48, 255, 49, 50,
356 255, 255, 255, 255, 51, 52, 255, 53, 54, 255, 55, 56, 255, 57, 59, 255,
357 58, 60, 255, 61, 62, 255, 63, 64, 255, 65, 66, 255, 255, 255, 255, 255,
358 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
359 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
360 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 67, 68, 69, 70,
361 71, 255, 255, 76, 77, 255, 255, 78, 79, 80, 81, 82, 83, 84, 85, 86,
362 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 255,
363 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 102, 255, 255
364 };
365
367 static const OldEngineID tto_to_ttd[] = {
368 0, 0, 8, 8, 8, 8, 8, 9, 10, 11, 12, 13, 14, 15, 15, 22,
369 23, 24, 25, 26, 27, 29, 28, 30, 31, 32, 33, 34, 35, 36, 37, 55,
370 57, 59, 58, 60, 61, 62, 63, 64, 65, 66, 67, 116, 116, 117, 118, 123,
371 124, 126, 127, 132, 133, 135, 136, 138, 139, 141, 142, 144, 145, 147, 148, 150,
372 151, 153, 154, 204, 205, 206, 207, 208, 211, 212, 211, 212, 211, 212, 215, 216,
373 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
374 233, 234, 235, 236, 237, 238, 253
375 };
376
377 for (Vehicle *v : Vehicle::Iterate()) {
378 if (v->engine_type >= lengthof(tto_to_ttd)) return false;
379 v->engine_type = static_cast<EngineID>(tto_to_ttd[v->engine_type.base()]);
380 }
381
382 /* Load the default engine set. Many of them will be overridden later */
383 {
384 EngineID j = EngineID::Begin();
385 for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); ++i, ++j) GetTempDataEngine(j, VehicleType::Train, i);
386 for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); ++i, ++j) GetTempDataEngine(j, VehicleType::Road, i);
387 for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); ++i, ++j) GetTempDataEngine(j, VehicleType::Ship, i);
388 for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) GetTempDataEngine(j, VehicleType::Aircraft, i);
389 }
390
392 TimerGameCalendar::YearMonthDay aging_ymd = TimerGameCalendar::ConvertDateToYMD(aging_date);
393
394 for (EngineID i = EngineID::Begin(); i < 256; ++i) {
395 OldEngineID oi = ttd_to_tto[i.base()];
397
398 if (oi == 255) {
399 /* Default engine is used */
401 StartupOneEngine(e, aging_ymd, 0);
402 CalcEngineReliability(e, false);
405
406 /* Make sure for example monorail and maglev are available when they should be */
409 e->company_avail.Set();
411 }
412 } else {
413 /* Using data from TTO savegame */
414 Engine *oe = &_old_engines[oi];
415
416 e->intro_date = oe->intro_date;
417 e->age = oe->age;
418 e->reliability = oe->reliability;
426 e->flags = oe->flags;
427
429
430 /* One or more engines were remapped to this one. Make this engine available
431 * if at least one of them was available. */
432 for (uint j = 0; j < lengthof(tto_to_ttd); j++) {
433 if (tto_to_ttd[j] == i && _old_engines[j].company_avail.Any()) {
434 e->company_avail.Set();
436 break;
437 }
438 }
439
441 }
442
443 e->preview_company = CompanyID::Invalid();
444 e->preview_asked.Set();
445 e->preview_wait = 0;
446 e->name = std::string{};
447 }
448
449 return true;
450}
451
452static void FixTTOCompanies()
453{
454 for (Company *c : Company::Iterate()) {
455 c->cur_economy.company_value = CalculateCompanyValue(c); // company value history is zeroed
456 }
457}
458
460{
462 static const Colours tto_colour_remap[] = {
467 };
468
469 if (static_cast<size_t>(tto) >= std::size(tto_colour_remap)) return Colours::Grey; // this shouldn't happen
470
471 return tto_colour_remap[to_underlying(tto)];
472}
473
474static inline uint RemapTownIndex(uint x)
475{
476 return _savegame_type == SavegameType::TTO ? (x - 0x264) / 78 : (x - 0x264) / 94;
477}
478
479static inline uint RemapOrderIndex(uint x)
480{
481 return _savegame_type == SavegameType::TTO ? (x - 0x1AC4) / 2 : (x - 0x1C18) / 2;
482}
483
484extern std::vector<TileIndex> _animated_tiles;
486extern std::unique_ptr<std::string[]> _old_name_array;
487
488static uint32_t _old_town_index;
489static uint16_t _old_string_id;
490static uint16_t _old_string_id_2;
491
492static void ClearOldMap3(TileIndex t)
493{
494 Tile tile(t);
495 tile.m3() = 0;
496 tile.m4() = 0;
497}
498
499static Town *RemapTown(TileIndex fallback)
500{
501 /* In some cases depots, industries and stations could refer to a missing town. */
502 Town *t = Town::GetIfValid(RemapTownIndex(_old_town_index));
503 if (t == nullptr) {
504 /* In case the town that was referred to does not exist, find the closest.
505 * However, this needs the kd-tree to be present. */
506 RebuildTownKdtree();
507 t = CalcClosestTownFromTile(fallback);
508 }
509 return t;
510}
511
512static void ReadTTDPatchFlags(LoadgameState &ls)
513{
514 if (_read_ttdpatch_flags) return;
515
517
518 /* Set default values */
519 ls.vehicle_multiplier = 1;
520 _ttdp_version = 0;
522 _bump_assert_value = 0;
523
525 ls.vehicle_names.resize(800);
526 return;
527 }
528
529 /* TTDPatch misuses old map3 (now m3/m4) for flags.. read them! */
530 ls.vehicle_multiplier = Tile(0).m3();
531 /* Somehow.... there was an error in some savegames, so 0 becomes 1
532 * and 1 becomes 2. The rest of the values are okay */
533 if (ls.vehicle_multiplier < 2) ls.vehicle_multiplier++;
534
535 ls.vehicle_names.resize(ls.vehicle_multiplier * 850);
536
537 /* TTDPatch increases the Vehicle-part in the middle of the game,
538 * so if the multiplier is anything else but 1, the assert fails..
539 * bump the assert value so it doesn't!
540 * (1 multiplier == 850 vehicles
541 * 1 vehicle == 128 bytes */
542 _bump_assert_value = (ls.vehicle_multiplier - 1) * 850 * 128;
543
544 /* The first 17 bytes are used by TTDP1, which translates to the first 9 m3s and first 8 m4s. */
545 for (TileIndex i{}; i <= 8; i++) { // check tile 0, too
546 Tile tile(i);
547 if (tile.m3() != 0 || (i != 8 && tile.m4() != 0)) _savegame_type = SavegameType::TTDP1;
548 }
549
550 /* Check if we have a modern TTDPatch savegame (has extra data all around) */
551 Tile ttdp2_header_first(Map::Size() - 3);
552 Tile ttdp2_header_second(Map::Size() - 2);
553 if (ttdp2_header_first.m3() == 'T' && ttdp2_header_first.m4() == 'T' &&
554 ttdp2_header_second.m3() == 'D' && ttdp2_header_second.m4() == 'p') {
556 }
557
558 Tile extra_chunk_tile = Tile(_savegame_type == SavegameType::TTDP2 ? Map::Size() - 1 : 1);
559 _old_extra_chunk_nums = extra_chunk_tile.m3() | extra_chunk_tile.m4() << 8;
560
561 /* Clean the misused places */
562 for (TileIndex i{}; i < 9; i++) ClearOldMap3(i);
563 for (TileIndex i = TileXY(0, Map::MaxY()); i < Map::Size(); i++) ClearOldMap3(i);
564
566
567 Debug(Facility::Oldloader, Severity::Notice, "Vehicle-multiplier is set to {} ({} vehicles)", ls.vehicle_multiplier, ls.vehicle_multiplier * 850);
568}
569
570static std::array<Town::SuppliedHistory, 2> _old_pass_supplied{};
571static std::array<Town::SuppliedHistory, 2> _old_mail_supplied{};
572
573static const OldChunks town_chunk[] = {
574 OCL_SVAR( OC_TILE, Town, xy ),
575 OCL_NULL( 2 ),
576 OCL_SVAR( OC_UINT16, Town, townnametype ),
577 OCL_SVAR( OC_UINT32, Town, townnameparts ),
578 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Town, grow_counter ),
579 OCL_NULL( 1 ),
580 OCL_NULL( 4 ),
581 OCL_NULL( 2 ),
582 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, Town, flags ),
583 OCL_NULL( 10 ),
584
585 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{0}] ),
586 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{1}] ),
587 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{2}] ),
588 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{3}] ),
589 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{4}] ),
590 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{5}] ),
591 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{6}] ),
592 OCL_SVAR( OC_INT16, Town, ratings[CompanyID{7}] ),
593
594 OCL_SVAR( OC_FILE_U32 | OC_VAR_U16, Town, have_ratings ),
595 OCL_SVAR( OC_FILE_U32 | OC_VAR_U16, Town, statues ),
596 OCL_NULL( 2 ),
597 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Town, time_until_rebuild ),
598 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Town, growth_rate ),
599
600 /* Slots 0 and 2 are passengers and mail respectively for old saves. */
601 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_pass_supplied[THIS_MONTH].production ),
602 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_mail_supplied[THIS_MONTH].production ),
603 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_pass_supplied[THIS_MONTH].transported ),
604 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_mail_supplied[THIS_MONTH].transported ),
605 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_pass_supplied[LAST_MONTH].production ),
606 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_mail_supplied[LAST_MONTH].production ),
607 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_pass_supplied[LAST_MONTH].transported ),
608 OCL_VAR( OC_FILE_U16 | OC_VAR_U32, 1, &_old_mail_supplied[LAST_MONTH].transported ),
609
610 OCL_NULL( 2 ),
611
612 OCL_SVAR( OC_TTD | OC_UINT16, Town, received[TownAcceptanceEffect::Food].new_act ),
613 OCL_SVAR( OC_TTD | OC_UINT16, Town, received[TownAcceptanceEffect::Water].new_act ),
614 OCL_SVAR( OC_TTD | OC_UINT16, Town, received[TownAcceptanceEffect::Food].old_act ),
615 OCL_SVAR( OC_TTD | OC_UINT16, Town, received[TownAcceptanceEffect::Water].old_act ),
616
617 OCL_SVAR( OC_UINT8, Town, road_build_months ),
618 OCL_SVAR( OC_UINT8, Town, fund_buildings_months ),
619
620 OCL_CNULL( OC_TTD, 8 ),
621
622 OCL_END()
623};
624
625static bool LoadOldTown(LoadgameState &ls, int num)
626{
627 Town *t = Town::CreateAtIndex(TownID(num));
628 if (!LoadChunk(ls, t, town_chunk)) return false;
629
630 if (t->xy != 0) {
632 /* 0x10B6 is auto-generated name, others are custom names */
633 t->townnametype = t->townnametype == 0x10B6 ? 0x20C1 : t->townnametype + 0x2A00;
634 }
635 /* Passengers and mail were always treated as slots 0 and 2 in older saves. */
636 auto &pass = t->supplied.emplace_back(CargoType{0});
637 pass.history[LAST_MONTH] = _old_pass_supplied[LAST_MONTH];
638 pass.history[THIS_MONTH] = _old_pass_supplied[THIS_MONTH];
639 auto &mail = t->supplied.emplace_back(CargoType{2});
640 mail.history[LAST_MONTH] = _old_mail_supplied[LAST_MONTH];
641 mail.history[THIS_MONTH] = _old_mail_supplied[THIS_MONTH];
642 } else {
643 delete t;
644 }
645
646 return true;
647}
648
649static uint16_t _old_order;
650static const OldChunks order_chunk[] = {
651 OCL_VAR ( OC_UINT16, 1, &_old_order ),
652 OCL_END()
653};
654
655static bool LoadOldOrder(LoadgameState &ls, int num)
656{
657 if (!LoadChunk(ls, nullptr, order_chunk)) return false;
658
660 o.order.AssignOrder(UnpackOldOrder(_old_order));
661
662 if (!o.order.IsType(OT_NOTHING) && num > 0) {
663 /* Relink the orders to each other (in the orders for one vehicle are behind each other,
664 * with an invalid order (OT_NOTHING) as indication that it is the last order */
665 OldOrderSaveLoadItem *prev = GetOldOrder(num + 1 - 1);
666 if (prev != nullptr) prev->next = num + 1; // next is 1-based.
667 }
668
669 return true;
670}
671
672static bool LoadOldAnimTileList(LoadgameState &ls, int)
673{
674 TileIndex anim_list[256];
675 const OldChunks anim_chunk[] = {
676 OCL_VAR ( OC_TILE, 256, anim_list ),
677 OCL_END ()
678 };
679
680 if (!LoadChunk(ls, nullptr, anim_chunk)) return false;
681
682 /* The first zero in the loaded array indicates the end of the list. */
683 for (int i = 0; i < 256; i++) {
684 if (anim_list[i] == 0) break;
685 _animated_tiles.push_back(anim_list[i]);
686 }
687
688 return true;
689}
690
691static const OldChunks depot_chunk[] = {
692 OCL_SVAR( OC_TILE, Depot, xy ),
693 OCL_VAR ( OC_UINT32, 1, &_old_town_index ),
694 OCL_END()
695};
696
697static bool LoadOldDepot(LoadgameState &ls, int num)
698{
700 if (!LoadChunk(ls, d, depot_chunk)) return false;
701
702 if (d->xy != 0) {
703 d->town = RemapTown(d->xy);
704 } else {
705 delete d;
706 }
707
708 return true;
709}
710
711static StationID _current_station_id;
712static uint16_t _waiting_acceptance;
713static uint8_t _cargo_source;
714static uint8_t _cargo_periods;
715
716static const OldChunks goods_chunk[] = {
717 OCL_VAR ( OC_UINT16, 1, &_waiting_acceptance ),
718 OCL_SVAR( OC_UINT8, GoodsEntry, time_since_pickup ),
719 OCL_SVAR( OC_UINT8, GoodsEntry, rating ),
720 OCL_VAR ( OC_UINT8, 1, &_cargo_source ),
721 OCL_VAR ( OC_UINT8, 1, &_cargo_periods ),
722 OCL_SVAR( OC_UINT8, GoodsEntry, last_speed ),
723 OCL_SVAR( OC_UINT8, GoodsEntry, last_age ),
724
725 OCL_END()
726};
727
728static bool LoadOldGood(LoadgameState &ls, int num)
729{
730 /* for TTO games, 12th (num == 11) goods entry is created in the Station constructor */
731 if (_savegame_type == SavegameType::TTO && num == 11) return true;
732
733 Station *st = Station::Get(_current_station_id);
734 GoodsEntry *ge = &st->goods[num];
735
736 if (!LoadChunk(ls, ge, goods_chunk)) return false;
737
738 ge->status.Set(GoodsEntry::State::Acceptance, HasBit(_waiting_acceptance, 15));
739 ge->status.Set(GoodsEntry::State::Rating, _cargo_source != 0xFF);
740 if (GB(_waiting_acceptance, 0, 12) != 0 && CargoPacket::CanAllocateItem()) {
741 ge->GetOrCreateData().cargo.Append(CargoPacket::Create(GB(_waiting_acceptance, 0, 12), _cargo_periods, (_cargo_source == 0xFF) ? StationID::Invalid() : StationID{_cargo_source}, INVALID_TILE, 0),
742 StationID::Invalid());
743 }
744
745 return true;
746}
747
748static const OldChunks station_chunk[] = {
749 OCL_SVAR( OC_TILE, Station, xy ),
750 OCL_VAR ( OC_UINT32, 1, &_old_town_index ),
751
752 OCL_NULL( 4 ),
753 OCL_SVAR( OC_TILE, Station, train_station.tile ),
754 OCL_SVAR( OC_TILE, Station, airport.tile ),
755 OCL_NULL( 2 ),
756 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Station, train_station.w ),
757
758 OCL_NULL( 1 ),
759 OCL_NULL( 2 ),
760
761 OCL_VAR ( OC_UINT16, 1, &_old_string_id ),
762
763 OCL_NULL( 4 ),
764
765 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, Station, had_vehicle_of_type ),
766
767 OCL_CHUNK( 12, LoadOldGood ),
768
769 OCL_SVAR( OC_UINT8, Station, time_since_load ),
770 OCL_SVAR( OC_UINT8, Station, time_since_unload ),
771 OCL_SVAR( OC_UINT8, Station, delete_ctr ),
772 OCL_SVAR( OC_UINT8, Station, owner ),
773 OCL_SVAR( OC_UINT8, Station, facilities ),
774 OCL_SVAR( OC_TTD | OC_UINT8, Station, airport.type ),
775 OCL_SVAR( OC_TTO | OC_FILE_U16 | OC_VAR_U64, Station, airport.blocks ),
776 OCL_NULL( 3 ),
777 OCL_CNULL( OC_TTD, 1 ),
778 OCL_SVAR( OC_TTD | OC_FILE_U16 | OC_VAR_U64, Station, airport.blocks ),
779 OCL_CNULL( OC_TTD, 2 ),
780 OCL_CNULL( OC_TTD, 4 ),
781
782 OCL_END()
783};
784
785static bool LoadOldStation(LoadgameState &ls, int num)
786{
787 Station *st = Station::CreateAtIndex(StationID(num));
788 _current_station_id = st->index;
789
790 if (!LoadChunk(ls, st, station_chunk)) return false;
791
792 if (st->xy != 0) {
793 st->town = RemapTown(st->xy);
794
796 if (IsInsideBS(_old_string_id, 0x180F, 32)) {
797 st->string_id = STR_SV_STNAME + (_old_string_id - 0x180F); // automatic name
798 } else {
799 st->string_id = static_cast<StringID>(_old_string_id + 0x2800); // custom name
800 }
801
802 if (st->airport.blocks.Test(AirportBlock{8})) {
803 st->airport.type = 1; // large airport
804 } else if (st->airport.blocks.Test(AirportBlock{6})) {
805 st->airport.type = 3; // oil rig
806 } else {
807 st->airport.type = 0; // small airport
808 }
809 } else {
810 st->string_id = RemapOldStringID(static_cast<StringID>(_old_string_id));
811 }
812 } else {
813 delete st;
814 }
815
816 return true;
817}
818
819/* Old save games always have 3 input and 2 output slots per industry. */
820static std::array<Industry::AcceptedCargo, INDUSTRY_ORIGINAL_NUM_INPUTS> _old_accepted{};
821static std::array<Industry::ProducedCargo, INDUSTRY_ORIGINAL_NUM_OUTPUTS> _old_produced{};
822
823static const OldChunks industry_chunk[] = {
824 OCL_SVAR( OC_TILE, Industry, location.tile ),
825 OCL_VAR ( OC_UINT32, 1, &_old_town_index ),
826 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Industry, location.w ),
827 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Industry, location.h ),
828 OCL_NULL( 2 ),
829
830 OCL_VAR( OC_TTD | OC_UINT16, 1, &_old_produced[0].waiting ),
831 OCL_VAR( OC_TTD | OC_UINT16, 1, &_old_produced[1].waiting ),
832 OCL_VAR( OC_TTO | OC_FILE_U8 | OC_VAR_U16, 1, &_old_produced[0].waiting ),
833 OCL_VAR( OC_TTO | OC_FILE_U8 | OC_VAR_U16, 1, &_old_produced[1].waiting ),
834
835 OCL_VAR( OC_UINT8, 1, &_old_produced[0].rate ),
836 OCL_VAR( OC_UINT8, 1, &_old_produced[1].rate ),
837
838 OCL_NULL( 3 ),
839
840 OCL_SVAR( OC_UINT8, Industry, prod_level ),
841
842 OCL_VAR( OC_UINT16, 1, &_old_produced[0].history[THIS_MONTH].production ),
843 OCL_VAR( OC_UINT16, 1, &_old_produced[1].history[THIS_MONTH].production ),
844 OCL_VAR( OC_UINT16, 1, &_old_produced[0].history[THIS_MONTH].transported ),
845 OCL_VAR( OC_UINT16, 1, &_old_produced[1].history[THIS_MONTH].transported ),
846
847 OCL_NULL( 2 ),
848
849 OCL_VAR( OC_UINT16, 1, &_old_produced[0].history[LAST_MONTH].production ),
850 OCL_VAR( OC_UINT16, 1, &_old_produced[1].history[LAST_MONTH].production ),
851 OCL_VAR( OC_UINT16, 1, &_old_produced[0].history[LAST_MONTH].transported ),
852 OCL_VAR( OC_UINT16, 1, &_old_produced[1].history[LAST_MONTH].transported ),
853
854 OCL_SVAR( OC_UINT8, Industry, type ),
855 OCL_SVAR( OC_TTO | OC_FILE_U8 | OC_VAR_U16, Industry, counter ),
856 OCL_SVAR( OC_UINT8, Industry, owner ),
857 OCL_SVAR( OC_UINT8, Industry, random_colour ),
858 OCL_SVAR( OC_TTD | OC_FILE_U8 | OC_VAR_I32, Industry, last_prod_year ),
859 OCL_SVAR( OC_TTD | OC_UINT16, Industry, counter ),
860 OCL_SVAR( OC_TTD | OC_UINT8, Industry, was_cargo_delivered ),
861
862 OCL_CNULL( OC_TTD, 9 ),
863
864 OCL_END()
865};
866
867static bool LoadOldIndustry(LoadgameState &ls, int num)
868{
869 Industry *i = Industry::CreateAtIndex(IndustryID(num));
870 if (!LoadChunk(ls, i, industry_chunk)) return false;
871
872 if (i->location.tile != 0) {
873 /* Copy data from old fixed arrays to industry. */
874 std::move(std::begin(_old_accepted), std::end(_old_accepted), std::back_inserter(i->accepted));
875 std::copy(std::begin(_old_produced), std::end(_old_produced), std::back_inserter(i->produced));
876
877 i->town = RemapTown(i->location.tile);
878
880 if (i->type > 0x06) i->type++; // Printing Works were added
881 if (i->type == 0x0A) i->type = 0x12; // Iron Ore Mine has different ID
882
883 TimerGameEconomy::YearMonthDay ymd = TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date);
884 i->last_prod_year = ymd.year;
885
887 }
888
889 Industry::industries[i->type].insert(i->index);
890 } else {
891 delete i;
892 }
893
894 return true;
895}
896
897static CompanyID _current_company_id;
898static int32_t _old_yearly;
899
900static const OldChunks _company_yearly_chunk[] = {
901 OCL_VAR( OC_INT32, 1, &_old_yearly ),
902 OCL_END()
903};
904
905static bool LoadOldCompanyYearly(LoadgameState &ls, int num)
906{
907 Company *c = Company::Get(_current_company_id);
908
911 _old_yearly = 0; // property maintenance
912 } else {
913 if (!LoadChunk(ls, nullptr, _company_yearly_chunk)) return false;
914 }
915
916 c->yearly_expenses[num][i] = _old_yearly;
917 }
918
919 return true;
920}
921
922static const OldChunks _company_economy_chunk[] = {
923 OCL_SVAR( OC_FILE_I32 | OC_VAR_I64, CompanyEconomyEntry, income ),
924 OCL_SVAR( OC_FILE_I32 | OC_VAR_I64, CompanyEconomyEntry, expenses ),
925 OCL_SVAR( OC_INT32, CompanyEconomyEntry, delivered_cargo[NUM_CARGO - 1] ),
926 OCL_SVAR( OC_INT32, CompanyEconomyEntry, performance_history ),
927 OCL_SVAR( OC_TTD | OC_FILE_I32 | OC_VAR_I64, CompanyEconomyEntry, company_value ),
928
929 OCL_END()
930};
931
932static bool LoadOldCompanyEconomy(LoadgameState &ls, int)
933{
934 Company *c = Company::Get(_current_company_id);
935
936 if (!LoadChunk(ls, &c->cur_economy, _company_economy_chunk)) return false;
937
938 /* Don't ask, but the number in TTD(Patch) are inverted to OpenTTD */
941
942 for (uint i = 0; i < 24; i++) {
943 if (!LoadChunk(ls, &c->old_economy[i], _company_economy_chunk)) return false;
944
945 c->old_economy[i].income = -c->old_economy[i].income;
946 c->old_economy[i].expenses = -c->old_economy[i].expenses;
947 }
948
949 return true;
950}
951
952static const OldChunks _company_chunk[] = {
953 OCL_VAR ( OC_UINT16, 1, &_old_string_id ),
954 OCL_SVAR( OC_UINT32, Company, name_2 ),
955 OCL_SVAR( OC_UINT32, Company, face.bits ),
956 OCL_VAR ( OC_UINT16, 1, &_old_string_id_2 ),
957 OCL_SVAR( OC_UINT32, Company, president_name_2 ),
958
959 OCL_SVAR( OC_FILE_I32 | OC_VAR_I64, Company, money ),
960 OCL_SVAR( OC_FILE_I32 | OC_VAR_I64, Company, current_loan ),
961
962 OCL_SVAR( OC_UINT8, Company, colour ),
963 OCL_SVAR( OC_UINT8, Company, money_fraction ),
964 OCL_SVAR( OC_UINT8, Company, months_of_bankruptcy ),
965 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Company, bankrupt_asked ),
966 OCL_SVAR( OC_FILE_U32 | OC_VAR_I64, Company, bankrupt_value ),
967 OCL_SVAR( OC_UINT16, Company, bankrupt_timeout ),
968
969 OCL_CNULL( OC_TTD, 4 ), // cargo_types
970 OCL_CNULL( OC_TTO, 2 ), // cargo_types
971
972 OCL_CHUNK( 3, LoadOldCompanyYearly ),
973 OCL_CHUNK( 1, LoadOldCompanyEconomy ),
974
975 OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Company, inaugurated_year),
976 OCL_SVAR( OC_TILE, Company, last_build_coordinate ),
977 OCL_SVAR( OC_UINT8, Company, num_valid_stat_ent ),
978
979 OCL_NULL( 230 ), // Old AI
980
981 OCL_SVAR( OC_UINT8, Company, block_preview ),
982 OCL_CNULL( OC_TTD, 1 ), // Old AI
983 OCL_CNULL( OC_TTD, 1 ), // avail_railtypes
984 OCL_SVAR( OC_TILE, Company, location_of_HQ ),
985
986 OCL_CNULL( OC_TTD, 4 ), // Shares
987
988 OCL_CNULL( OC_TTD, 8 ), // junk at end of chunk
989
990 OCL_END()
991};
992
993static bool LoadOldCompany(LoadgameState &ls, int num)
994{
995 Company *c = Company::CreateAtIndex(CompanyID(num));
996
997 _current_company_id = (CompanyID)num;
998
999 if (!LoadChunk(ls, c, _company_chunk)) return false;
1000
1001 if (_old_string_id == 0) {
1002 delete c;
1003 return true;
1004 }
1005
1007 /* adjust manager's face */
1008 if (HasBit(c->face.bits, 27) && GB(c->face.bits, 26, 1) == GB(c->face.bits, 19, 1)) {
1009 /* if face would be black in TTD, adjust tie colour and thereby face colour */
1010 ClrBit(c->face.bits, 27);
1011 }
1012
1013 /* Company name */
1014 if (_old_string_id == 0 || _old_string_id == 0x4C00) {
1015 _old_string_id = STR_SV_UNNAMED.base(); // "Unnamed"
1016 } else if (GB(_old_string_id, 8, 8) == 0x52) {
1017 _old_string_id += 0x2A00; // Custom name
1018 } else {
1019 _old_string_id = RemapOldStringID(static_cast<StringID>(_old_string_id += 0x240D)).base(); // Automatic name
1020 }
1021 c->name_1 = static_cast<StringID>(_old_string_id);
1022
1023 /* Manager name */
1024 switch (_old_string_id_2) {
1025 case 0x4CDA: _old_string_id_2 = SPECSTR_PRESIDENT_NAME.base(); break; // automatic name
1026 case 0x0006: _old_string_id_2 = STR_SV_EMPTY.base(); break; // empty name
1027 default: _old_string_id_2 = _old_string_id_2 + 0x2A00; break; // custom name
1028 }
1029 c->president_name_1 = static_cast<StringID>(_old_string_id_2);
1030
1031 c->colour = RemapTTOColour(c->colour);
1032
1033 if (num != 0) c->is_ai = true;
1034 } else {
1035 c->name_1 = RemapOldStringID(static_cast<StringID>(_old_string_id));
1036 c->president_name_1 = RemapOldStringID(static_cast<StringID>(_old_string_id_2));
1037
1038 if (num == 0) {
1039 /* If the first company has no name, make sure we call it UNNAMED */
1040 if (c->name_1 == STR_NULL) {
1041 c->name_1 = STR_SV_UNNAMED;
1042 }
1043 } else {
1044 /* Beside some multiplayer maps (1 on 1), which we don't official support,
1045 * all other companies are an AI.. mark them as such */
1046 c->is_ai = true;
1047 }
1048
1049 /* Sometimes it is better to not ask.. in old scenarios, the money
1050 * was always 893288 pounds. In the newer versions this is correct,
1051 * but correct for those oldies
1052 * Ps: this also means that if you had exact 893288 pounds, you will go back
1053 * to 100000.. this is a very VERY small chance ;) */
1054 if (c->money == 893288) c->money = c->current_loan = 100000;
1055 }
1056
1057 _company_colours[c->index] = c->colour;
1059
1060 return true;
1061}
1062
1063static uint32_t _old_order_ptr;
1064static uint16_t _old_next_ptr;
1065static typename VehicleID::BaseType _current_vehicle_id;
1066
1067static const OldChunks vehicle_train_chunk[] = {
1068 OCL_SVAR( OC_UINT8, Train, track ),
1069 OCL_SVAR( OC_UINT8, Train, force_proceed ),
1070 OCL_SVAR( OC_UINT16, Train, crash_anim_pos ),
1071 OCL_NULL( 1 ), // railtype
1072
1073 OCL_NULL( 5 ),
1074
1075 OCL_END()
1076};
1077
1078static const OldChunks vehicle_road_chunk[] = {
1079 OCL_SVAR( OC_UINT8, RoadVehicle, state ),
1080 OCL_SVAR( OC_UINT8, RoadVehicle, frame ),
1081 OCL_SVAR( OC_UINT16, RoadVehicle, blocked_ctr ),
1082 OCL_SVAR( OC_UINT8, RoadVehicle, overtaking ),
1083 OCL_SVAR( OC_UINT8, RoadVehicle, overtaking_ctr ),
1084 OCL_SVAR( OC_UINT16, RoadVehicle, crashed_ctr ),
1085 OCL_SVAR( OC_UINT8, RoadVehicle, reverse_ctr ),
1086
1087 OCL_NULL( 1 ),
1088
1089 OCL_END()
1090};
1091
1092static const OldChunks vehicle_ship_chunk[] = {
1093 OCL_SVAR( OC_UINT8, Ship, state ),
1094
1095 OCL_NULL( 9 ),
1096
1097 OCL_END()
1098};
1099
1100static const OldChunks vehicle_air_chunk[] = {
1101 OCL_SVAR( OC_UINT8, Aircraft, pos ),
1102 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Aircraft, targetairport ),
1103 OCL_SVAR( OC_UINT16, Aircraft, crashed_counter ),
1104 OCL_SVAR( OC_UINT8, Aircraft, state ),
1105
1106 OCL_NULL( 5 ),
1107
1108 OCL_END()
1109};
1110
1111static const OldChunks vehicle_effect_chunk[] = {
1112 OCL_SVAR( OC_UINT16, EffectVehicle, animation_state ),
1113 OCL_SVAR( OC_UINT8, EffectVehicle, animation_substate ),
1114
1115 OCL_NULL( 7 ), // Junk
1116
1117 OCL_END()
1118};
1119
1120static const OldChunks vehicle_disaster_chunk[] = {
1121 OCL_SVAR( OC_UINT16, DisasterVehicle, image_override ),
1122 OCL_SVAR( OC_UINT16, DisasterVehicle, big_ufo_destroyer_target ),
1123
1124 OCL_NULL( 6 ),
1125
1126 OCL_END()
1127};
1128
1129static const OldChunks vehicle_empty_chunk[] = {
1130 OCL_NULL( 10 ),
1131
1132 OCL_END()
1133};
1134
1135static bool LoadOldVehicleUnion(LoadgameState &ls, int)
1136{
1137 Vehicle *v = Vehicle::GetIfValid(_current_vehicle_id);
1138 uint temp = ls.total_read;
1139 bool res;
1140
1141 if (v == nullptr) {
1142 res = LoadChunk(ls, nullptr, vehicle_empty_chunk);
1143 } else {
1144 switch (v->type) {
1145 default: SlErrorCorrupt("Invalid vehicle type");
1146 case VehicleType::Train : res = LoadChunk(ls, v, vehicle_train_chunk); break;
1147 case VehicleType::Road : res = LoadChunk(ls, v, vehicle_road_chunk); break;
1148 case VehicleType::Ship : res = LoadChunk(ls, v, vehicle_ship_chunk); break;
1149 case VehicleType::Aircraft: res = LoadChunk(ls, v, vehicle_air_chunk); break;
1150 case VehicleType::Effect : res = LoadChunk(ls, v, vehicle_effect_chunk); break;
1151 case VehicleType::Disaster: res = LoadChunk(ls, v, vehicle_disaster_chunk); break;
1152 }
1153 }
1154
1155 /* This chunk size should always be 10 bytes */
1156 if (ls.total_read - temp != 10) {
1157 Debug(Facility::Oldloader, Severity::Critical, "Assert failed in VehicleUnion: invalid chunk size");
1158 return false;
1159 }
1160
1161 return res;
1162}
1163
1164static uint16_t _cargo_count;
1165
1166static const OldChunks vehicle_chunk[] = {
1167 OCL_SVAR( OC_UINT8, Vehicle, subtype ),
1168
1169 OCL_NULL( 2 ),
1170 OCL_NULL( 2 ),
1171
1172 OCL_VAR ( OC_UINT32, 1, &_old_order_ptr ),
1173 OCL_VAR ( OC_UINT16, 1, &_old_order ),
1174
1175 OCL_NULL ( 1 ),
1176 OCL_SVAR( OC_UINT8, Vehicle, cur_implicit_order_index ),
1177 OCL_SVAR( OC_TILE, Vehicle, dest_tile ),
1178 OCL_SVAR( OC_UINT16, Vehicle, load_unload_ticks ),
1179 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Vehicle, date_of_last_service ),
1180 OCL_SVAR( OC_UINT16, Vehicle, service_interval ),
1181 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Vehicle, last_station_visited ),
1182 OCL_SVAR( OC_TTD | OC_UINT8, Vehicle, tick_counter ),
1183 OCL_CNULL( OC_TTD, 2 ),
1184 OCL_CNULL( OC_TTO, 1 ),
1185
1186 OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Vehicle, x_pos ),
1187 OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Vehicle, y_pos ),
1188 OCL_SVAR( OC_FILE_U8 | OC_VAR_I32, Vehicle, z_pos ),
1189 OCL_SVAR( OC_UINT8, Vehicle, direction ),
1190 OCL_NULL( 2 ),
1191 OCL_NULL( 2 ),
1192 OCL_NULL( 1 ),
1193
1194 OCL_SVAR( OC_UINT8, Vehicle, owner ),
1195 OCL_SVAR( OC_TILE, Vehicle, tile ),
1196 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Vehicle, sprite_cache.sprite_seq.seq[0].sprite ),
1197
1198 OCL_NULL( 8 ),
1199
1200 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, Vehicle, vehstatus ),
1201 OCL_SVAR( OC_TTD | OC_UINT16, Vehicle, cur_speed ),
1202 OCL_SVAR( OC_TTO | OC_FILE_U8 | OC_VAR_U16, Vehicle, cur_speed ),
1203 OCL_SVAR( OC_UINT8, Vehicle, subspeed ),
1204 OCL_SVAR( OC_UINT8, Vehicle, acceleration ),
1205 OCL_SVAR( OC_UINT8, Vehicle, progress ),
1206
1207 OCL_SVAR( OC_UINT8, Vehicle, cargo_type ),
1208 OCL_SVAR( OC_TTD | OC_UINT16, Vehicle, cargo_cap ),
1209 OCL_SVAR( OC_TTO | OC_FILE_U8 | OC_VAR_U16, Vehicle, cargo_cap ),
1210 OCL_VAR ( OC_TTD | OC_UINT16, 1, &_cargo_count ),
1211 OCL_VAR ( OC_TTO | OC_FILE_U8 | OC_VAR_U16, 1, &_cargo_count ),
1212 OCL_VAR ( OC_UINT8, 1, &_cargo_source ),
1213 OCL_VAR ( OC_UINT8, 1, &_cargo_periods ),
1214
1215 OCL_SVAR( OC_TTO | OC_UINT8, Vehicle, tick_counter ),
1216
1217 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Vehicle, age ),
1218 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Vehicle, max_age ),
1219 OCL_SVAR( OC_FILE_U8 | OC_VAR_I32, Vehicle, build_year ),
1220 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Vehicle, unitnumber ),
1221
1222 OCL_SVAR( OC_TTD | OC_UINT16, Vehicle, engine_type ),
1223 OCL_SVAR( OC_TTO | OC_FILE_U8 | OC_VAR_U16, Vehicle, engine_type ),
1224
1225 OCL_SVAR( OC_UINT8, Vehicle, spritenum ),
1226 OCL_SVAR( OC_UINT8, Vehicle, day_counter ),
1227
1228 OCL_SVAR( OC_UINT8, Vehicle, breakdowns_since_last_service ),
1229 OCL_SVAR( OC_UINT8, Vehicle, breakdown_ctr ),
1230 OCL_SVAR( OC_UINT8, Vehicle, breakdown_delay ),
1231 OCL_SVAR( OC_UINT8, Vehicle, breakdown_chance ),
1232
1233 OCL_CNULL( OC_TTO, 1 ),
1234
1235 OCL_SVAR( OC_UINT16, Vehicle, reliability ),
1236 OCL_SVAR( OC_UINT16, Vehicle, reliability_spd_dec ),
1237
1238 OCL_SVAR( OC_FILE_I32 | OC_VAR_I64, Vehicle, profit_this_year ),
1239 OCL_SVAR( OC_FILE_I32 | OC_VAR_I64, Vehicle, profit_last_year ),
1240
1241 OCL_VAR ( OC_UINT16, 1, &_old_next_ptr ),
1242
1243 OCL_SVAR( OC_FILE_U32 | OC_VAR_I64, Vehicle, value ),
1244
1245 OCL_VAR ( OC_UINT16, 1, &_old_string_id ),
1246
1247 OCL_CHUNK( 1, LoadOldVehicleUnion ),
1248
1249 OCL_CNULL( OC_TTO, 24 ),
1250 OCL_CNULL( OC_TTD, 20 ),
1251
1252 OCL_END()
1253};
1254
1262{
1263 /* Read the TTDPatch flags, because we need some info from it */
1264 ReadTTDPatchFlags(ls);
1265
1266 for (uint i = 0; i < ls.vehicle_multiplier; i++) {
1267 _current_vehicle_id = num * ls.vehicle_multiplier + i;
1268
1269 Vehicle *v;
1270
1272 uint type = ReadByte(ls);
1273 switch (type) {
1274 default: return false;
1275 case 0x00 /* VehicleType::Invalid */: v = nullptr; break;
1276 case 0x25 /* MONORAIL */:
1277 case 0x20 /* VehicleType::Train */: v = Train::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1278 case 0x21 /* VehicleType::Road */: v = RoadVehicle::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1279 case 0x22 /* VehicleType::Ship */: v = Ship::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1280 case 0x23 /* VehicleType::Aircraft */: v = Aircraft::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1281 case 0x24 /* VehicleType::Effect */: v = EffectVehicle::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1282 case 0x26 /* VehicleType::Disaster */: v = DisasterVehicle::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1283 }
1284
1285 if (!LoadChunk(ls, v, vehicle_chunk)) return false;
1286 if (v == nullptr) continue;
1287 v->refit_cap = v->cargo_cap;
1288
1289 SpriteID sprite = v->sprite_cache.sprite_seq.seq[0].sprite;
1290 /* no need to override other sprites */
1291 if (IsInsideMM(sprite, 1460, 1465)) {
1292 sprite += 580; // aircraft smoke puff
1293 } else if (IsInsideMM(sprite, 2096, 2115)) {
1294 sprite += 977; // special effects part 1
1295 } else if (IsInsideMM(sprite, 2396, 2436)) {
1296 sprite += 1305; // special effects part 2
1297 } else if (IsInsideMM(sprite, 2516, 2539)) {
1298 sprite += 1385; // rotor or disaster-related vehicles
1299 }
1300 v->sprite_cache.sprite_seq.seq[0].sprite = sprite;
1301
1302 switch (v->type) {
1303 case VehicleType::Train: {
1304 static const uint8_t spriteset_rail[] = {
1305 0, 2, 4, 4, 8, 10, 12, 14, 16, 18, 20, 22, 40, 42, 44, 46,
1306 48, 52, 54, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 120, 122,
1307 124, 126, 128, 130, 132, 134, 136, 138, 140
1308 };
1309 if (v->spritenum / 2 >= lengthof(spriteset_rail)) return false;
1310 v->spritenum = spriteset_rail[v->spritenum / 2]; // adjust railway sprite set offset
1311 break;
1312 }
1313
1314 case VehicleType::Road:
1315 if (v->spritenum >= 22) v->spritenum += 12;
1316 break;
1317
1318 case VehicleType::Ship:
1319 v->spritenum += 2;
1320
1321 switch (v->spritenum) {
1322 case 2: // oil tanker && cargo type != oil
1323 if (v->cargo_type != 3) v->spritenum = 0; // make it a coal/goods ship
1324 break;
1325 case 4: // passenger ship && cargo type == mail
1326 if (v->cargo_type == 2) v->spritenum = 0; // make it a mail ship
1327 break;
1328 default:
1329 break;
1330 }
1331 break;
1332
1333 default:
1334 break;
1335 }
1336
1337 switch (_old_string_id) {
1338 case 0x0000: break; // empty (invalid vehicles)
1339 case 0x0006: _old_string_id = STR_SV_EMPTY.base(); break; // empty (special vehicles)
1340 case 0x8495: _old_string_id = STR_SV_TRAIN_NAME.base(); break; // "Train X"
1341 case 0x8842: _old_string_id = STR_SV_ROAD_VEHICLE_NAME.base(); break; // "Road Vehicle X"
1342 case 0x8C3B: _old_string_id = STR_SV_SHIP_NAME.base(); break; // "Ship X"
1343 case 0x9047: _old_string_id = STR_SV_AIRCRAFT_NAME.base(); break; // "Aircraft X"
1344 default: _old_string_id += 0x2A00; break; // custom name
1345 }
1346
1347 ls.vehicle_names[_current_vehicle_id] = static_cast<StringID>(_old_string_id);
1348 } else {
1349 /* Read the vehicle type and allocate the right vehicle */
1350 switch (ReadByte(ls)) {
1351 default: SlErrorCorrupt("Invalid vehicle type");
1352 case 0x00 /* VehicleType::Invalid */: v = nullptr; break;
1353 case 0x10 /* VehicleType::Train */: v = Train::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1354 case 0x11 /* VehicleType::Road */: v = RoadVehicle::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1355 case 0x12 /* VehicleType::Ship */: v = Ship::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1356 case 0x13 /* VehicleType::Aircraft */: v = Aircraft::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1357 case 0x14 /* VehicleType::Effect */: v = EffectVehicle::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1358 case 0x15 /* VehicleType::Disaster */: v = DisasterVehicle::CreateAtIndex(VehicleID(_current_vehicle_id)); break;
1359 }
1360
1361 if (!LoadChunk(ls, v, vehicle_chunk)) return false;
1362 if (v == nullptr) continue;
1363
1364 ls.vehicle_names[_current_vehicle_id] = RemapOldStringID(static_cast<StringID>(_old_string_id));
1365
1366 /* This should be consistent, else we have a big problem... */
1367 if (v->index != _current_vehicle_id) {
1368 Debug(Facility::Oldloader, Severity::Critical, "Loading failed - vehicle-array is invalid");
1369 return false;
1370 }
1371 }
1372
1373 if (_old_order_ptr != 0 && _old_order_ptr != 0xFFFFFFFF) {
1374 uint max = _savegame_type == SavegameType::TTO ? 3000 : 5000;
1375 uint old_id = RemapOrderIndex(_old_order_ptr);
1376 if (old_id < max) v->old_orders = old_id + 1;
1377 }
1379
1380 if (v->type == VehicleType::Disaster) {
1381 DisasterVehicle::From(v)->state = UnpackOldOrder(_old_order).GetDestination().value;
1382 }
1383
1384 v->next = (Vehicle *)(size_t)_old_next_ptr;
1385
1386 if (_cargo_count != 0 && CargoPacket::CanAllocateItem()) {
1387 StationID source = (_cargo_source == 0xFF) ? StationID::Invalid() : StationID{_cargo_source};
1388 TileIndex source_xy = (source != StationID::Invalid()) ? Station::Get(source)->xy : TileIndex{};
1389 v->cargo.Append(CargoPacket::Create(_cargo_count, _cargo_periods, source, source_xy, 0));
1390 }
1391 }
1392
1393 return true;
1394}
1395
1403{
1404 /*
1405 * Data is stored in fixed size "cells"; read these completely.
1406 * Validation and conversion to UTF-8 are happening at a later stage.
1407 */
1408 std::string &str = _old_name_array[index];
1409 str.resize(_savegame_type == SavegameType::TTO ? 24 : 32);
1410 for (auto &c : str) c = ReadByte(ls);
1411
1412 return true;
1413}
1414
1415static const OldChunks sign_chunk[] = {
1416 OCL_VAR ( OC_UINT16, 1, &_old_string_id ),
1417 OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Sign, x ),
1418 OCL_SVAR( OC_FILE_U16 | OC_VAR_I32, Sign, y ),
1419 OCL_SVAR( OC_FILE_U16 | OC_VAR_I8, Sign, z ),
1420
1421 OCL_NULL( 6 ),
1422
1423 OCL_END()
1424};
1425
1426static bool LoadOldSign(LoadgameState &ls, int num)
1427{
1428 Sign *si = Sign::CreateAtIndex(SignID(num));
1429 if (!LoadChunk(ls, si, sign_chunk)) return false;
1430
1431 if (_old_string_id != 0) {
1433 if (_old_string_id != 0x140A) si->name = CopyFromOldName(static_cast<StringID>(_old_string_id + 0x2A00));
1434 } else {
1435 si->name = CopyFromOldName(RemapOldStringID(static_cast<StringID>(_old_string_id)));
1436 }
1437 si->owner = OWNER_NONE;
1438 } else {
1439 delete si;
1440 }
1441
1442 return true;
1443}
1444
1445static const OldChunks engine_chunk[] = {
1446 OCL_SVAR( OC_UINT16, Engine, company_avail ),
1447 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Engine, intro_date ),
1448 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, Engine, age ),
1449 OCL_SVAR( OC_UINT16, Engine, reliability ),
1450 OCL_SVAR( OC_UINT16, Engine, reliability_spd_dec ),
1451 OCL_SVAR( OC_UINT16, Engine, reliability_start ),
1452 OCL_SVAR( OC_UINT16, Engine, reliability_max ),
1453 OCL_SVAR( OC_UINT16, Engine, reliability_final ),
1454 OCL_SVAR( OC_UINT16, Engine, duration_phase_1 ),
1455 OCL_SVAR( OC_UINT16, Engine, duration_phase_2 ),
1456 OCL_SVAR( OC_UINT16, Engine, duration_phase_3 ),
1457
1458 OCL_NULL( 1 ), // lifelength
1459 OCL_SVAR( OC_UINT8, Engine, flags ),
1460 OCL_NULL( 1 ), // preview_company_rank
1461 OCL_SVAR( OC_UINT8, Engine, preview_wait ),
1462
1463 OCL_CNULL( OC_TTD, 2 ),
1464
1465 OCL_END()
1466};
1467
1468static bool LoadOldEngine(LoadgameState &ls, int num)
1469{
1470 Engine *e = _savegame_type == SavegameType::TTO ? &_old_engines[num] : GetTempDataEngine(static_cast<EngineID>(num));
1471 return LoadChunk(ls, e, engine_chunk);
1472}
1473
1474static bool LoadOldEngineName(LoadgameState &ls, int num)
1475{
1476 Engine *e = GetTempDataEngine(static_cast<EngineID>(num));
1477 e->name = CopyFromOldName(RemapOldStringID(static_cast<StringID>(ReadUint16(ls))));
1478 return true;
1479}
1480
1481static const OldChunks subsidy_chunk[] = {
1482 OCL_SVAR( OC_UINT8, Subsidy, cargo_type ),
1483 OCL_SVAR( OC_UINT8, Subsidy, remaining ),
1484 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Subsidy, src ),
1485 OCL_SVAR( OC_FILE_U8 | OC_VAR_U16, Subsidy, dst ),
1486
1487 OCL_END()
1488};
1489
1490static bool LoadOldSubsidy(LoadgameState &ls, int num)
1491{
1493 bool ret = LoadChunk(ls, s, subsidy_chunk);
1494 if (!IsValidCargoType(s->cargo_type)) delete s;
1495 return ret;
1496}
1497
1498static const OldChunks game_difficulty_chunk[] = {
1499 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, max_no_competitors ),
1500 OCL_NULL( 2), // competitor_start_time
1501 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, number_towns ),
1502 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, industry_density ),
1503 OCL_SVAR( OC_FILE_U16 | OC_VAR_U32, DifficultySettings, max_loan ),
1504 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, initial_interest ),
1505 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, vehicle_costs ),
1506 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, competitor_speed ),
1507 OCL_NULL( 2), // competitor_intelligence
1508 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, vehicle_breakdowns ),
1509 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, subsidy_multiplier ),
1510 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, construction_cost ),
1511 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, terrain_type ),
1512 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, quantity_sea_lakes ),
1513 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, economy ),
1514 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, line_reverse_mode ),
1515 OCL_SVAR( OC_FILE_U16 | OC_VAR_U8, DifficultySettings, disasters ),
1516 OCL_END()
1517};
1518
1519static bool LoadOldGameDifficulty(LoadgameState &ls, int)
1520{
1521 bool ret = LoadChunk(ls, &_settings_game.difficulty, game_difficulty_chunk);
1522 _settings_game.difficulty.max_loan *= 1000;
1523 return ret;
1524}
1525
1526
1527static bool LoadOldMapPart1(LoadgameState &ls, int)
1528{
1530 Map::Allocate(OLD_MAP_SIZE, OLD_MAP_SIZE);
1531 }
1532
1533 for (auto t : Map::Iterate()) {
1534 t.m1() = ReadByte(ls);
1535 }
1536 for (auto t : Map::Iterate()) {
1537 t.m2() = ReadByte(ls);
1538 }
1539
1541 /* old map3 is split into to m3 and m4 */
1542 for (auto t : Map::Iterate()) {
1543 t.m3() = ReadByte(ls);
1544 t.m4() = ReadByte(ls);
1545 }
1546 auto range = Map::Iterate();
1547 for (auto it = range.begin(); it != range.end(); /* nothing. */) {
1548 uint8_t b = ReadByte(ls);
1549 for (int i = 0; i < 8; i += 2, ++it) (*it).m6() = GB(b, i, 2);
1550 }
1551 }
1552
1553 return true;
1554}
1555
1556static bool LoadOldMapPart2(LoadgameState &ls, int)
1557{
1558 for (auto t : Map::Iterate()) {
1559 t.type() = ReadByte(ls);
1560 }
1561 for (auto t : Map::Iterate()) {
1562 t.m5() = ReadByte(ls);
1563 }
1564
1565 return true;
1566}
1567
1568static bool LoadTTDPatchExtraChunks(LoadgameState &ls, int)
1569{
1570 ReadTTDPatchFlags(ls);
1571
1573
1574 for (int i = 0; i != _old_extra_chunk_nums; i++) {
1575 uint16_t id = ReadUint16(ls);
1576 uint32_t len = ReadUint32(ls);
1577
1578 switch (id) {
1579 /* List of GRFIDs, used in the savegame. 0x8004 is the new ID
1580 * They are saved in a 'GRFID:4 active:1' format, 5 bytes for each entry */
1581 case 0x2:
1582 case 0x8004: {
1583 /* Skip the first element: TTDP hack for the Action D special variables (FFFF0000 01) */
1584 ReadUint32(ls); ReadByte(ls); len -= 5;
1585
1587 while (len != 0) {
1588 GrfID grfid = UnflattenNewGRFLabel<GrfID>(ReadUint32(ls));
1589
1590 if (ReadByte(ls) == 1) {
1591 auto c = std::make_unique<GRFConfig>("TTDP game, no information");
1592 c->ident.grfid = grfid;
1593
1594 Debug(Facility::Oldloader, Severity::Notice, "TTDPatch game using GRF file with GRFID {}", FormatArrayAsHex(c->ident.grfid));
1595 AppendToGRFConfigList(_grfconfig, std::move(c));
1596 }
1597 len -= 5;
1598 }
1599
1600 /* Append static NewGRF configuration */
1602 break;
1603 }
1604
1605 /* TTDPatch version and configuration */
1606 case 0x3:
1607 _ttdp_version = ReadUint32(ls);
1608 Debug(Facility::Oldloader, Severity::Notice, "Game saved with TTDPatch version {}.{}.{} r{}",
1609 GB(_ttdp_version, 24, 8), GB(_ttdp_version, 20, 4), GB(_ttdp_version, 16, 4), GB(_ttdp_version, 0, 16));
1610 len -= 4;
1611 while (len-- != 0) ReadByte(ls); // skip the configuration
1612 break;
1613
1614 default:
1615 Debug(Facility::Oldloader, Severity::Info, "Skipping unknown extra chunk {}", id);
1616 while (len-- != 0) ReadByte(ls);
1617 break;
1618 }
1619 }
1620
1621 return true;
1622}
1623
1624extern TileIndex _cur_tileloop_tile;
1625extern uint16_t _disaster_delay;
1626extern uint8_t _trees_tick_ctr;
1627extern uint8_t _age_cargo_skip_counter; // From misc_sl.cpp
1628extern uint8_t _old_diff_level;
1629extern uint8_t _old_units;
1630static const OldChunks main_chunk[] = {
1631 OCL_ASSERT( OC_TTD, 0 ),
1632 OCL_ASSERT( OC_TTO, 0 ),
1633 OCL_VAR ( OC_FILE_U16 | OC_VAR_U32, 1, &TimerGameCalendar::date ),
1634 OCL_VAR ( OC_UINT16, 1, &TimerGameCalendar::date_fract ),
1635 OCL_NULL( 600 ),
1636 OCL_VAR ( OC_UINT32, 2, &_random.state ),
1637
1638 OCL_ASSERT( OC_TTD, 0x264 ),
1639 OCL_ASSERT( OC_TTO, 0x264 ),
1640
1641 OCL_CCHUNK( OC_TTD, 70, LoadOldTown ),
1642 OCL_CCHUNK( OC_TTO, 80, LoadOldTown ),
1643
1644 OCL_ASSERT( OC_TTD, 0x1C18 ),
1645 OCL_ASSERT( OC_TTO, 0x1AC4 ),
1646
1647 OCL_CCHUNK( OC_TTD, 5000, LoadOldOrder ),
1648 OCL_CCHUNK( OC_TTO, 3000, LoadOldOrder ),
1649
1650 OCL_ASSERT( OC_TTD, 0x4328 ),
1651 OCL_ASSERT( OC_TTO, 0x3234 ),
1652
1653 OCL_CHUNK( 1, LoadOldAnimTileList ),
1654 OCL_NULL( 4 ),
1655
1656 OCL_ASSERT( OC_TTO, 0x3438 ),
1657
1658 OCL_CCHUNK( OC_TTD, 255, LoadOldDepot ),
1659 OCL_CCHUNK( OC_TTO, 252, LoadOldDepot ),
1660
1661 OCL_ASSERT( OC_TTD, 0x4B26 ),
1662 OCL_ASSERT( OC_TTO, 0x3A20 ),
1663
1664 OCL_NULL( 4 ),
1665 OCL_NULL( 2 ),
1666 OCL_NULL( 2 ),
1667
1668 OCL_VAR ( OC_FILE_U16 | OC_VAR_U8, 1, &_age_cargo_skip_counter ),
1669 OCL_VAR ( OC_FILE_U16 | OC_VAR_U64, 1, &TimerGameTick::counter ),
1670 OCL_VAR ( OC_TILE, 1, &_cur_tileloop_tile ),
1671
1672 OCL_ASSERT( OC_TTO, 0x3A2E ),
1673
1674 OCL_CNULL( OC_TTO, 48 * 6 ),
1675 OCL_CNULL( OC_TTD, 49 * 6 ),
1676
1677 OCL_ASSERT( OC_TTO, 0x3B4E ),
1678
1679 OCL_CNULL( OC_TTO, 11 * 8 ),
1680 OCL_CNULL( OC_TTD, 12 * 8 ),
1681
1682 OCL_ASSERT( OC_TTD, 0x4CBA ),
1683 OCL_ASSERT( OC_TTO, 0x3BA6 ),
1684
1685 OCL_CHUNK( 1, LoadOldMapPart1 ),
1686
1687 OCL_ASSERT( OC_TTD, 0x48CBA ),
1688 OCL_ASSERT( OC_TTO, 0x23BA6 ),
1689
1690 OCL_CCHUNK( OC_TTD, 250, LoadOldStation ),
1691 OCL_CCHUNK( OC_TTO, 200, LoadOldStation ),
1692
1693 OCL_ASSERT( OC_TTO, 0x29E16 ),
1694
1695 OCL_CCHUNK( OC_TTD, 90, LoadOldIndustry ),
1696 OCL_CCHUNK( OC_TTO, 100, LoadOldIndustry ),
1697
1698 OCL_ASSERT( OC_TTO, 0x2ADB6 ),
1699
1700 OCL_CHUNK( 8, LoadOldCompany ),
1701
1702 OCL_ASSERT( OC_TTD, 0x547F2 ),
1703 OCL_ASSERT( OC_TTO, 0x2C746 ),
1704
1705 OCL_CCHUNK( OC_TTD, 850, LoadOldVehicle ),
1706 OCL_CCHUNK( OC_TTO, 800, LoadOldVehicle ),
1707
1708 OCL_ASSERT( OC_TTD, 0x6F0F2 ),
1709 OCL_ASSERT( OC_TTO, 0x45746 ),
1710
1711 OCL_CCHUNK( OC_TTD, 500, LoadOldCustomString ),
1712 OCL_CCHUNK( OC_TTO, 200, LoadOldCustomString ),
1713
1714 OCL_ASSERT( OC_TTO, 0x46A06 ),
1715
1716 OCL_NULL( 0x2000 ),
1717
1718 OCL_CHUNK( 40, LoadOldSign ),
1719
1720 OCL_ASSERT( OC_TTO, 0x48C36 ),
1721
1722 OCL_CCHUNK( OC_TTD, 256, LoadOldEngine ),
1723 OCL_CCHUNK( OC_TTO, 103, LoadOldEngine ),
1724
1725 OCL_ASSERT( OC_TTO, 0x496AC ),
1726
1727 OCL_NULL ( 2 ), // _vehicle_id_ctr_day
1728
1729 OCL_CHUNK( 8, LoadOldSubsidy ),
1730
1731 OCL_ASSERT( OC_TTO, 0x496CE ),
1732
1733 OCL_VAR ( OC_FILE_U16 | OC_VAR_U32, 1, &_new_competitor_timeout.period.value ),
1734
1735 OCL_CNULL( OC_TTO, 2 ),
1736
1737 OCL_VAR ( OC_FILE_I16 | OC_VAR_I32, 1, &_saved_scrollpos_x ),
1738 OCL_VAR ( OC_FILE_I16 | OC_VAR_I32, 1, &_saved_scrollpos_y ),
1739 OCL_VAR ( OC_FILE_U16 | OC_VAR_U8, 1, &_saved_scrollpos_zoom ),
1740
1741 OCL_NULL( 4 ),
1742 OCL_VAR ( OC_FILE_U32 | OC_VAR_I64, 1, &_economy.old_max_loan_unround ),
1743 OCL_VAR ( OC_INT16, 1, &_economy.fluct ),
1744
1745 OCL_VAR ( OC_UINT16, 1, &_disaster_delay ),
1746
1747 OCL_ASSERT( OC_TTO, 0x496E4 ),
1748
1749 OCL_CNULL( OC_TTD, 144 ),
1750
1751 OCL_CCHUNK( OC_TTD, 256, LoadOldEngineName ),
1752
1753 OCL_CNULL( OC_TTD, 144 ),
1754 OCL_NULL( 2 ),
1755 OCL_NULL( 1 ),
1756
1757 OCL_VAR ( OC_UINT8, 1, &_settings_game.locale.currency ),
1758 OCL_VAR ( OC_UINT8, 1, &_old_units ),
1759 OCL_VAR ( OC_FILE_U8 | OC_VAR_U32, 1, &_cur_company_tick_index ),
1760
1761 OCL_NULL( 2 ),
1762 OCL_NULL( 8 ),
1763
1764 OCL_VAR ( OC_UINT8, 1, &_economy.infl_amount ),
1765 OCL_VAR ( OC_UINT8, 1, &_economy.infl_amount_pr ),
1766 OCL_VAR ( OC_UINT8, 1, &_economy.interest_rate ),
1767 OCL_NULL( 1 ), // available airports
1768 OCL_VAR ( OC_UINT8, 1, &_settings_game.vehicle.road_side ),
1769 OCL_VAR ( OC_UINT8, 1, &_settings_game.game_creation.town_name ),
1770
1771 OCL_CHUNK( 1, LoadOldGameDifficulty ),
1772
1773 OCL_ASSERT( OC_TTD, 0x77130 ),
1774
1775 OCL_VAR ( OC_UINT8, 1, &_old_diff_level ),
1776
1777 OCL_VAR ( OC_TTD | OC_UINT8, 1, &_settings_game.game_creation.landscape ),
1778 OCL_VAR ( OC_TTD | OC_UINT8, 1, &_trees_tick_ctr ),
1779
1780 OCL_CNULL( OC_TTD, 1 ),
1781 OCL_VAR ( OC_TTD | OC_UINT8, 1, &_settings_game.game_creation.snow_line_height ),
1782
1783 OCL_CNULL( OC_TTD, 32 ),
1784 OCL_CNULL( OC_TTD, 36 ),
1785
1786 OCL_ASSERT( OC_TTD, 0x77179 ),
1787 OCL_ASSERT( OC_TTO, 0x4971D ),
1788
1789 OCL_CHUNK( 1, LoadOldMapPart2 ),
1790
1791 OCL_ASSERT( OC_TTD, 0x97179 ),
1792 OCL_ASSERT( OC_TTO, 0x6971D ),
1793
1794 /* Below any (if available) extra chunks from TTDPatch can follow */
1795 OCL_CHUNK(1, LoadTTDPatchExtraChunks),
1796
1797 OCL_END()
1798};
1799
1800bool LoadTTDMain(LoadgameState &ls)
1801{
1802 Debug(Facility::Oldloader, Severity::Notice, "Reading main chunk...");
1803
1804 _read_ttdpatch_flags = false;
1805
1806 /* Load the biggest chunk */
1807 if (!LoadChunk(ls, nullptr, main_chunk)) {
1808 Debug(Facility::Oldloader, Severity::Critical, "Loading failed");
1809 return false;
1810 }
1811
1812 Debug(Facility::Oldloader, Severity::Notice, "Done, converting game data...");
1813
1814 FixTTDMapArray();
1815 FixTTDDepots();
1816
1817 /* Fix some general stuff */
1818 if (to_underlying(_settings_game.game_creation.landscape) >= NUM_LANDSCAPE) _settings_game.game_creation.landscape = LandscapeType::Temperate;
1819
1820 /* Fix the game to be compatible with OpenTTD */
1821 FixOldTowns();
1822 FixOldVehicles(ls);
1823
1824 /* We have a new difficulty setting */
1825 _settings_game.difficulty.town_council_tolerance = Clamp(_old_diff_level, 0, 2);
1826
1827 Debug(Facility::Oldloader, Severity::Notice, "Finished converting game data");
1828 Debug(Facility::Oldloader, Severity::Error, "TTD(Patch) savegame successfully converted");
1829
1830 return true;
1831}
1832
1833bool LoadTTOMain(LoadgameState &ls)
1834{
1835 Debug(Facility::Oldloader, Severity::Notice, "Reading main chunk...");
1836
1837 _read_ttdpatch_flags = false;
1838
1839 std::array<uint8_t, 103 * sizeof(Engine)> engines; // we don't want to call Engine constructor here
1840 _old_engines = (Engine *)engines.data();
1841
1842 /* Load the biggest chunk */
1843 if (!LoadChunk(ls, nullptr, main_chunk)) {
1844 Debug(Facility::Oldloader, Severity::Critical, "Loading failed");
1845 return false;
1846 }
1847 Debug(Facility::Oldloader, Severity::Notice, "Done, converting game data...");
1848
1849 if (_settings_game.game_creation.town_name != 0) _settings_game.game_creation.town_name++;
1850
1851 _settings_game.game_creation.landscape = LandscapeType::Temperate;
1852 _trees_tick_ctr = 0xFF;
1853
1854 if (!FixTTOMapArray() || !FixTTOEngines()) {
1855 Debug(Facility::Oldloader, Severity::Critical, "Conversion failed");
1856 return false;
1857 }
1858
1859 FixOldTowns();
1860 FixOldVehicles(ls);
1861 FixTTOCompanies();
1862
1863 /* We have a new difficulty setting */
1864 _settings_game.difficulty.town_council_tolerance = Clamp(_old_diff_level, 0, 2);
1865
1866 /* SVXConverter about cargo payment rates correction:
1867 * "increase them to compensate for the faster time advance in TTD compared to TTO
1868 * which otherwise would cause much less income while the annual running costs of
1869 * the vehicles stay the same" */
1870 _economy.inflation_payment = std::min(_economy.inflation_payment * 124 / 74, MAX_INFLATION);
1871
1872 Debug(Facility::Oldloader, Severity::Notice, "Finished converting game data");
1873 Debug(Facility::Oldloader, Severity::Error, "TTO savegame successfully converted");
1874
1875 return true;
1876}
Base for aircraft.
AirportBlock
Movement Blocks on Airports blocks (eg_airport_flags).
Definition airport.h:91
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:112
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:77
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
@ Food
Cargo behaves food/fizzy-drinks-like.
Definition cargotype.h:28
@ Water
Cargo behaves water-like.
Definition cargotype.h:27
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Set()
Set all bits.
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:52
uint16_t reliability_start
Initial reliability of the engine.
Definition engine_base.h:53
TimerGameCalendar::Date intro_date
Date of introduction of the engine.
Definition engine_base.h:48
EngineFlags flags
Flags of the engine.
Definition engine_base.h:59
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition engine_base.h:42
uint16_t reliability_max
Maximal reliability of the engine.
Definition engine_base.h:54
uint16_t reliability_final
Final reliability of the engine.
Definition engine_base.h:55
CompanyID preview_company
Company which is currently being offered a preview CompanyID::Invalid() means no company.
Definition engine_base.h:61
uint16_t duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition engine_base.h:58
uint16_t duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition engine_base.h:57
uint8_t preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition engine_base.h:62
uint16_t reliability
Current reliability of the engine.
Definition engine_base.h:51
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition engine_base.h:44
int32_t age
Age of the engine in months.
Definition engine_base.h:49
std::string name
Custom name of engine.
Definition engine_base.h:46
uint16_t duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition engine_base.h:56
Iterate a range of enum values.
TileIndex tile
The base tile of the area.
void Append(CargoPacket *cp, StationID next)
Appends the given cargo packet to the range of packets with the same next station.
Wrapper class to abstract away the way the tiles are stored.
Definition map_func.h:25
uint8_t & m4()
General purpose.
Definition map_func.h:145
uint8_t & m3()
General purpose.
Definition map_func.h:134
A timeout timer will fire once after the interval.
Definition timer.h:116
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
static YearMonthDay ConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
static Date date
Current date in days (day counter).
static DateFract date_fract
Fractional part of the day.
static constexpr TimerGame< struct Economy >::Year ORIGINAL_BASE_YEAR
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
static Date date
Current date in days (day counter).
static YearMonthDay ConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
StrongType::Typedef< int32_t, struct YearTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Year
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Date
void Append(CargoPacket *cp, MoveToAction action=MoveToAction::Keep)
Appends the given cargo packet.
Definition of stuff that is very close to a company, like the company struct itself.
Money CalculateCompanyValue(const Company *c, bool including_loan=true)
Calculate the value of the company.
Definition economy.cpp:153
uint _cur_company_tick_index
used to generate a name for one company that doesn't have a name yet per tick
TimeoutTimer< TimerGameTick > _new_competitor_timeout({ TimerGameTick::Priority::CompetitorTimeout, 0 }, []() { if(_game_mode==GameMode::Menu||!AI::CanStartNew()) return;if(_networking &&Company::GetNumItems() >=_settings_client.network.max_companies) return;if(_settings_game.difficulty.competitors_interval==0) return;uint8_t n=0;for(const Company *c :Company::Iterate()) { if(c->is_ai) n++;} if(n >=_settings_game.difficulty.max_no_competitors) return;Command< Commands::CompanyControl >::Post(CompanyCtrlAction::NewAI, CompanyID::Invalid(), CompanyRemoveReason::None, ClientID::Invalid);})
Start a new competitor company if possible.
TypedIndexContainer< std::array< Colours, MAX_COMPANIES >, CompanyID > _company_colours
NOSAVE: can be determined from company structs.
Functions related to companies.
static constexpr Owner OWNER_NONE
The tile has no ownership.
static constexpr Owner OWNER_WATER
The tile/execution is done by "water".
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Oldloader
Oldloader message facility.
Definition debug_type.h:35
@ Misc
Misc message facility.
Definition debug_type.h:32
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
@ Notice
Notice.
Definition debug_type.h:18
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
@ Info
Info, information you might care about.
Definition debug_type.h:19
@ Error
Error, but we are recovering.
Definition debug_type.h:16
Base for all depots (except hangars).
DepotID GetDepotIndex(Tile t)
Get the index of which depot is attached to the tile.
Definition depot_map.h:56
bool IsDepotTile(Tile tile)
Is the given tile a tile with a depot on it?
Definition depot_map.h:45
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
uint16_t _disaster_delay
Delay counter for considering the next disaster.
All disaster vehicles.
ExpensesType
Types of expenses.
@ Property
Property costs.
@ End
End marker.
static const uint64_t MAX_INFLATION
Maximum inflation (including fractional part) without causing overflows in int64_t price computations...
Base class for all effect vehicles.
void StartupOneEngine(Engine *e, const TimerGameCalendar::YearMonthDay &aging_ymd, uint32_t seed)
Start/initialise one engine.
Definition engine.cpp:757
void CalcEngineReliability(Engine *e, bool new_month)
Update Engine::reliability and (if needed) update the engine GUIs.
Definition engine.cpp:685
Functions related to engines.
Engine * GetTempDataEngine(EngineID index, VehicleType type, uint16_t local_id)
Get temporary engine data for loading savegame engine information.
Definition engine_sl.cpp:53
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:25
@ Available
This vehicle is available to everyone.
This file contains all the data for vehicles.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
uint32_t _ttdp_version
version of TTDP savegame (if applicable)
Definition saveload.cpp:83
SavegameType _savegame_type
type of savegame we are loading
Definition saveload.cpp:80
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ White
White.
Definition gfx_type.h:300
@ PaleGreen
Pale green.
Definition gfx_type.h:286
@ Mauve
Mauve.
Definition gfx_type.h:295
@ LightBlue
Light blue.
Definition gfx_type.h:290
@ Yellow
Yellow.
Definition gfx_type.h:288
@ DarkBlue
Dark blue.
Definition gfx_type.h:285
@ Pink
Pink.
Definition gfx_type.h:287
@ Orange
Orange.
Definition gfx_type.h:297
@ Blue
Blue.
Definition gfx_type.h:293
@ Purple
Purple.
Definition gfx_type.h:296
@ Grey
Grey.
Definition gfx_type.h:299
@ Green
Green.
Definition gfx_type.h:291
@ Cream
Cream.
Definition gfx_type.h:294
@ Brown
Brown.
Definition gfx_type.h:298
@ Red
Red.
Definition gfx_type.h:289
@ DarkGreen
Dark green.
Definition gfx_type.h:292
@ Tile
Destination is a tile.
Definition goal_type.h:53
Base of all industries.
@ Temperate
Base landscape.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:376
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
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 T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
ZoomLevel _saved_scrollpos_zoom
Definition misc_sl.cpp:38
int _saved_scrollpos_x
Definition misc_sl.cpp:36
int _saved_scrollpos_y
Definition misc_sl.cpp:37
uint8_t _age_cargo_skip_counter
Skip aging of cargo? Used before savegame version 162.
Definition misc_sl.cpp:84
uint8_t _trees_tick_ctr
Determines when to consider building more trees.
Definition tree_cmd.cpp:47
constexpr T UnflattenNewGRFLabel(uint32_t value)
Unflatten a NewGRF related label from a 32 bits integer.
Definition newgrf.h:270
GRFConfigList _grfconfig
First item in list of current GRF set up.
void AppendStaticGRFConfigs(GRFConfigList &dst)
Appends the static GRFs to a list of GRFs.
void AppendToGRFConfigList(GRFConfigList &dst, std::unique_ptr< GRFConfig > &&el)
Appends an element to a list of GRFs.
void ClearGRFConfigList(GRFConfigList &config)
Clear a GRF Config list, freeing all nodes.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
bool LoadChunk(LoadgameState &ls, void *base, const OldChunks *chunks)
Loads a chunk from the old savegame.
uint8_t ReadByte(LoadgameState &ls)
Reads a byte from the buffer and decompress if needed.
Definition oldloader.cpp:86
Declarations of structures and functions used in loader of old savegames.
@ OC_TTO
-//- TTO (default is neither of these)
Definition oldloader.h:43
@ OC_TTD
chunk is valid ONLY for TTD savegames
Definition oldloader.h:42
#define OCL_SVAR(type, base, offset)
Load 'type' to offset 'offset' in a struct of type 'base', which must also be given via base in LoadC...
Definition oldloader.h:110
#define OCL_NULL(amount)
Read 'amount' of bytes and send them to /dev/null or something.
Definition oldloader.h:120
#define OCL_END()
Every struct must end with this.
Definition oldloader.h:114
#define OCL_VAR(type, amount, pointer)
Load 'type' to a global var.
Definition oldloader.h:112
#define OCL_ASSERT(type, size)
To check if we are really at the place we expect to be.
Definition oldloader.h:118
#define OCL_CHUNK(amount, proc)
Load another proc to load a part of the savegame, 'amount' times.
Definition oldloader.h:122
std::unique_ptr< std::string[]> _old_name_array
Location to load the old names to.
bool LoadOldCustomString(LoadgameState &ls, int index)
Read a single string from the savegame.
void FixOldVehicles(LoadgameState &ls)
Convert the old style vehicles into something that resembles the old new style savegames.
bool LoadOldVehicle(LoadgameState &ls, int num)
Load the vehicles of an old style savegame.
static void FixTTDDepots()
static bool _read_ttdpatch_flags
Have we (tried to) read TTDPatch extra flags?
static Colours RemapTTOColour(Colours tto)
static uint16_t _old_extra_chunk_nums
Number of extra TTDPatch chunks.
static bool FixTTOEngines()
OldOrderSaveLoadItem * GetOldOrder(size_t pool_index)
Get a pointer to an old order with the given reference index.
Definition order_sl.cpp:126
OldOrderSaveLoadItem & AllocateOldOrder(size_t pool_index)
Allocate an old order with the given pool index.
Definition order_sl.cpp:138
Order UnpackOldOrder(uint16_t packed)
Unpacks a order from savegames made with TTD(Patch).
Definition order_sl.cpp:96
Randomizer _random
Random used in the game state calculations.
Road vehicle states.
@ RVS_IN_DT_ROAD_STOP
The vehicle is in a drive-through road stop.
Definition roadveh.h:46
@ RVSB_IN_DEPOT
The vehicle is in a depot.
Definition roadveh.h:38
@ RVSB_WORMHOLE
The vehicle is in a tunnel and/or bridge.
Definition roadveh.h:39
A number of safeguards to prevent using unsafe methods.
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:372
@ TTDP1
TTDP savegame ( -//- ) (data at NW border).
Definition saveload.h:434
@ TTO
TTO savegame.
Definition saveload.h:437
@ TTDP2
TTDP savegame in new format (data at SE border).
Definition saveload.h:435
Functions/types related to errors from savegames.
Declaration of functions used in more save/load files.
StringID RemapOldStringID(StringID s)
Remap a string ID from the old format to the new format.
std::string CopyFromOldName(StringID id)
Copy and convert old custom names to UTF-8.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Base for ships.
Base class for signs.
PoolID< uint16_t, struct SignIDTag, 64000, 0xFFFF > SignID
The type of the IDs of signs.
Definition signs_type.h:16
Base classes/functions for stations.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:76
Functions related to low-level strings.
static constexpr uint16_t SPECSTR_TOWNNAME_START
Special strings for town names.
static constexpr StringID SPECSTR_PRESIDENT_NAME
Special string for the president's name.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:75
AirportBlocks blocks
stores which blocks on the airport are taken. was 16 bit earlier on, then 32
uint8_t type
Type of this airport,.
StringID string_id
Default name (town area) of station.
TileIndex xy
Base tile of the station.
Town * town
The town this station is associated with.
VehicleType type
Type of vehicle.
Statistics about the economy.
Money income
The amount of income.
Money expenses
The amount of expenses.
uint32_t bits
Company manager face bits, meaning is dependent on style.
bool is_ai
If true, the company is (also) controlled by the computer (a NoAI program).
StringID name_1
Name of the company if the user did not change it.
Money current_loan
Amount of money borrowed from the bank.
TimerGameEconomy::Year inaugurated_year
Economy year of starting the company.
CompanyEconomyEntry cur_economy
Economic data of the company of this quarter.
Colours colour
Company colour.
std::array< CompanyEconomyEntry, MAX_HISTORY_QUARTERS > old_economy
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
CompanyManagerFace face
Face description of the president.
std::array< Expenses, 3 > yearly_expenses
Expenses of the company for the last three years.
StringID president_name_1
Name of the president if the user did not change it.
Money money
Money owned by the company.
Settings related to the difficulty of the game.
Disasters, like submarines, skyrangers and their shadows, belong to this class.
uint16_t state
Action stage of the disaster vehicle.
A special vehicle is one of the following:
LandscapeTypes climates
Climates supported by the engine.
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Stores station stats for a single cargo.
States status
Status of this cargo, see State.
@ Acceptance
Set when the station accepts the cargo currently for final deliveries.
@ Rating
This indicates whether a cargo has a rating at the station.
GoodsEntryData & GetOrCreateData()
Get optional cargo packet/flow data.
Defines the internal data of a functional industry.
Definition industry.h:64
IndustryType type
type of industry.
Definition industry.h:117
Colours random_colour
randomized colour of the industry, for display purpose
Definition industry.h:119
TimerGameEconomy::Year last_prod_year
last economy year of production
Definition industry.h:120
ProducedCargoes produced
produced cargo slots
Definition industry.h:112
Town * town
Nearest town.
Definition industry.h:109
AcceptedCargoes accepted
accepted cargo slots
Definition industry.h:113
TileArea location
Location of the industry.
Definition industry.h:108
static std::array< FlatSet< IndustryID >, NUM_INDUSTRYTYPES > industries
List of industries of each type.
Definition industry.h:279
uint8_t vehicle_multiplier
TTDPatch vehicle multiplier.
Definition oldloader.h:30
static IterateWrapper Iterate()
Returns an iterable ensemble of all Tiles.
Definition map_func.h:366
static uint MaxY()
Gets the maximum Y coordinate within the map, including TileType::Void.
Definition map_func.h:298
static void Allocate(uint size_x, uint size_y)
(Re)allocates a map with the given dimension
Definition map.cpp:36
static uint Size()
Get the size of the map.
Definition map_func.h:280
VehicleSpriteSeq sprite_seq
Vehicle appearance.
Compatibility struct to allow saveload of pool-based orders.
Definition order_base.h:367
Order order
The order data.
Definition order_base.h:370
uint32_t next
The next order index (1-based).
Definition order_base.h:369
DestinationID GetDestination() const
Gets the destination of this order.
Definition order_base.h:99
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
void AssignOrder(const Order &other)
Assign data to an order (from another order) This function makes sure that the index is maintained co...
static Pool::IterateWrapper< Depot > Iterate(size_t from=0)
static T * CreateAtIndex(TownID index, Targs &&... args)
static Company * Get(auto index)
static Vehicle * GetIfValid(auto index)
Buses, trucks and trams belong to this class.
Definition roadveh.h:105
uint8_t state
Definition roadveh.h:107
All ships have this type.
Definition ship.h:39
Owner owner
Placed by this company. Anyone can delete them though. OWNER_NONE for gray signs from old games.
Definition signs_base.h:28
static Station * CreateAtIndex(StationID index, Targs &&... args)
static Station * Get(auto index)
static RoadVehicle * From(Vehicle *v)
Station data structure.
std::array< GoodsEntry, NUM_CARGO > goods
Goods at this station.
Airport airport
Tile area the airport covers.
Struct about subsidies, offered and awarded.
CargoType cargo_type
Cargo type involved in this subsidy, INVALID_CARGO for invalid subsidy.
Town data structure.
Definition town.h:64
TileIndex xy
town center tile
Definition town.h:65
SuppliedCargoes supplied
Cargo statistics about supplied cargo.
Definition town.h:131
uint16_t townnametype
The style of the name.
Definition town.h:72
'Train' is either a loco or a wagon.
Definition train.h:97
Vehicle data structure.
VehicleCargoList cargo
The cargo this vehicle is carrying.
uint16_t cargo_cap
total capacity
CargoType cargo_type
type of cargo this vehicle is carrying
Order current_order
The current order (+ status, like: loading).
Vehicle(VehicleID index, VehicleType type=VehicleType::Invalid)
Vehicle constructor.
Definition vehicle.cpp:381
uint16_t refit_cap
Capacity left over from before last refit.
uint32_t old_orders
Only used during conversion of old save games.
uint8_t spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
Vehicle * next
pointer to the next vehicle in the chain
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
TileIndex tile
Current tile index.
Subsidy base class.
PoolID< uint16_t, struct SubsidyIDTag, 256, 0xFFFF > SubsidyID
ID of a subsidy.
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
void SetTileType(Tile tile, TileType type)
Set the type of a tile.
Definition tile_map.h:131
void SetTileOwner(Tile tile, Owner owner)
Sets the owner of a tile.
Definition tile_map.h:198
static TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
TileType
The different types of tiles.
Definition tile_type.h:48
@ TunnelBridge
Tunnel entry/exit and bridge heads.
Definition tile_type.h:58
@ Water
Water tile.
Definition tile_type.h:55
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Object
Contains objects such as transmitters and owned land.
Definition tile_type.h:59
@ Industry
Part of an industry.
Definition tile_type.h:57
@ Railway
A tile with railway.
Definition tile_type.h:50
@ Void
Invisible tiles at the SW and SE border.
Definition tile_type.h:56
@ Trees
Tile with one or more trees.
Definition tile_type.h:53
@ House
A house by a town.
Definition tile_type.h:52
@ Road
A tile with road and/or tram tracks.
Definition tile_type.h:51
@ Clear
A tile without any structures, i.e. grass, rocks, farm fields etc.
Definition tile_type.h:49
Definition of Interval and OneShot timers.
Definition of the game-calendar-timer.
Definition of the tick-based game-timer.
Base of the town class.
Town * CalcClosestTownFromTile(TileIndex tile, uint threshold=UINT_MAX)
Return the town closest to the given tile within threshold.
Namepart tables for the town name generator.
Base for the train class.
Functions related to vehicles.
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
@ Ship
Ship vehicle type.
@ Effect
Effect vehicle type (smoke, explosions, sparks, bubbles).
@ Aircraft
Aircraft vehicle type.
@ Road
Road vehicle type.
@ Disaster
Disaster vehicle type.
@ Train
Train vehicle type.
void MakeSea(Tile t, bool rocks=false)
Make a sea tile.
Definition water_map.h:434