Merge branch 'development' into master_joker
[openttd-joker.git] / src / saveload / tbtr_template_veh_sl.cpp
blobca1fb0a4aaec6254f5a33896c9cbabf3fcaebbca
1 #include "../stdafx.h"
3 #include "../tbtr_template_vehicle.h"
4 #include "../tbtr_template_vehicle_func.h"
5 #include "../train.h"
6 #include "../company_base.h"
7 #include "../core/backup_type.hpp"
8 #include "../core/random_func.hpp"
10 #include "saveload.h"
12 const SaveLoad* GTD() {
14 static const SaveLoad _template_veh_desc[] = {
15 SLE_REF(TemplateVehicle, next, REF_TEMPLATE_VEHICLE),
17 SLE_VAR(TemplateVehicle, reuse_depot_vehicles, SLE_UINT8),
18 SLE_VAR(TemplateVehicle, keep_remaining_vehicles, SLE_UINT8),
19 SLE_VAR(TemplateVehicle, refit_as_template, SLE_UINT8),
21 SLE_VAR(TemplateVehicle, owner, SLE_UINT32),
22 SLE_VAR(TemplateVehicle, owner_b, SLE_UINT8),
24 SLE_VAR(TemplateVehicle, engine_type, SLE_UINT16),
25 SLE_VAR(TemplateVehicle, cargo_type, SLE_UINT8),
26 SLE_VAR(TemplateVehicle, cargo_cap, SLE_UINT16),
27 SLE_VAR(TemplateVehicle, cargo_subtype, SLE_UINT8),
29 SLE_VAR(TemplateVehicle, subtype, SLE_UINT8),
30 SLE_VAR(TemplateVehicle, railtype, SLE_UINT8),
32 SLE_VAR(TemplateVehicle, index, SLE_UINT32),
34 SLE_VAR(TemplateVehicle, real_consist_length, SLE_UINT16),
36 SLE_VAR(TemplateVehicle, max_speed, SLE_UINT16),
37 SLE_VAR(TemplateVehicle, power, SLE_UINT32),
38 SLE_VAR(TemplateVehicle, weight, SLE_UINT32),
39 SLE_VAR(TemplateVehicle, max_te, SLE_UINT32),
41 SLE_VAR(TemplateVehicle, spritenum, SLE_UINT8),
42 SLE_CONDNULL(4, 0, SL_PATCH_PACK_1_15 - 1), // cur_image
43 SLE_CONDVAR(TemplateVehicle, sprite_seq.seq[0].sprite, SLE_UINT32, SL_PATCH_PACK_1_15, SL_MAX_VERSION),
44 SLE_CONDVAR(TemplateVehicle, sprite_seq.count, SLE_UINT32, SL_PATCH_PACK_1_15, SL_MAX_VERSION),
45 SLE_CONDARR(TemplateVehicle, sprite_seq.seq, SLE_UINT32, 8, SL_PATCH_PACK_1_15, SL_MAX_VERSION),
46 SLE_VAR(TemplateVehicle, image_width, SLE_UINT32),
48 SLE_END()
51 static const SaveLoad * const _ret[] = {
52 _template_veh_desc,
55 return _ret[0];
58 static void Save_TMPLS()
60 TemplateVehicle *tv;
62 FOR_ALL_TEMPLATES(tv) {
63 SlSetArrayIndex(tv->index);
64 SlObject(tv, GTD());
68 static void Load_TMPLS()
70 int index;
72 while ((index = SlIterateArray()) != -1) {
73 TemplateVehicle *tv = new (index) TemplateVehicle(); //TODO:check with veh sl code
74 SlObject(tv, GTD());
78 static void Ptrs_TMPLS()
80 TemplateVehicle *tv;
81 FOR_ALL_TEMPLATES(tv) {
82 SlObject(tv, GTD());
86 void AfterLoadTemplateVehicles()
88 TemplateVehicle *tv;
90 FOR_ALL_TEMPLATES(tv) {
91 /* Reinstate the previous pointer */
92 if (tv->next != nullptr) tv->next->previous = tv;
93 tv->first = nullptr;
95 FOR_ALL_TEMPLATES(tv) {
96 /* Fill the first pointers */
97 if (tv->previous == nullptr) {
98 for (TemplateVehicle *u = tv; u != nullptr; u = u->Next()) {
99 u->first = tv;
105 void AfterLoadTemplateVehiclesUpdateImage()
107 TemplateVehicle *tv;
109 SavedRandomSeeds saved_seeds;
110 SaveRandomSeeds(&saved_seeds);
112 if (IsSavegameVersionBefore(SL_PATCH_PACK_1_19)) {
113 FOR_ALL_TEMPLATES(tv) {
114 if (tv->Prev() == nullptr && !Company::IsValidID(tv->owner)) {
115 // clean up leftover template vehicles which no longer have a valid owner
116 delete tv;
121 FOR_ALL_TEMPLATES(tv) {
122 if (tv->Prev() == nullptr) {
123 Backup<CompanyByte> cur_company(_current_company, tv->owner, FILE_LINE);
124 StringID err;
125 Train* t = VirtualTrainFromTemplateVehicle(tv, err);
126 if (t != nullptr) {
127 int tv_len = 0;
128 for (TemplateVehicle *u = tv; u != nullptr; u = u->Next()) {
129 tv_len++;
131 int t_len = 0;
132 for (Train *u = t; u != nullptr; u = u->Next()) {
133 t_len++;
135 if (t_len == tv_len) {
136 Train *v = t;
137 for (TemplateVehicle *u = tv; u != nullptr; u = u->Next(), v = v->Next()) {
138 u->spritenum = v->spritenum;
139 v->GetImage(DIR_W, EIT_PURCHASE, &u->sprite_seq);
140 u->image_width = v->GetDisplayImageWidth(&u->image_offset);
143 delete t;
145 cur_company.Restore();
149 RestoreRandomSeeds(saved_seeds);
152 extern const ChunkHandler _template_vehicle_chunk_handlers[] = {
153 {'TMPL', Save_TMPLS, Load_TMPLS, Ptrs_TMPLS, nullptr, CH_ARRAY | CH_LAST},