3 #include "../tbtr_template_vehicle.h"
4 #include "../tbtr_template_vehicle_func.h"
6 #include "../company_base.h"
7 #include "../core/backup_type.hpp"
8 #include "../core/random_func.hpp"
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
),
51 static const SaveLoad
* const _ret
[] = {
58 static void Save_TMPLS()
62 FOR_ALL_TEMPLATES(tv
) {
63 SlSetArrayIndex(tv
->index
);
68 static void Load_TMPLS()
72 while ((index
= SlIterateArray()) != -1) {
73 TemplateVehicle
*tv
= new (index
) TemplateVehicle(); //TODO:check with veh sl code
78 static void Ptrs_TMPLS()
81 FOR_ALL_TEMPLATES(tv
) {
86 void AfterLoadTemplateVehicles()
90 FOR_ALL_TEMPLATES(tv
) {
91 /* Reinstate the previous pointer */
92 if (tv
->next
!= nullptr) tv
->next
->previous
= tv
;
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()) {
105 void AfterLoadTemplateVehiclesUpdateImage()
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
121 FOR_ALL_TEMPLATES(tv
) {
122 if (tv
->Prev() == nullptr) {
123 Backup
<CompanyByte
> cur_company(_current_company
, tv
->owner
, FILE_LINE
);
125 Train
* t
= VirtualTrainFromTemplateVehicle(tv
, err
);
128 for (TemplateVehicle
*u
= tv
; u
!= nullptr; u
= u
->Next()) {
132 for (Train
*u
= t
; u
!= nullptr; u
= u
->Next()) {
135 if (t_len
== tv_len
) {
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
);
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
},