4 #include "company_func.h"
6 #include "vehicle_type.h"
7 #include "vehicle_base.h"
8 #include "vehicle_func.h"
10 #include "articulated_vehicles.h"
11 #include "newgrf_callbacks.h"
12 #include "newgrf_engine.h"
13 #include "newgrf_spritegroup.h"
15 #include "engine_base.h"
16 #include "engine_type.h"
17 #include "engine_func.h"
19 #include "sortlist_type.h"
21 #define FOR_ALL_TEMPLATES_FROM(var, start) FOR_ALL_ITEMS_FROM(TemplateVehicle, template_index, var, start)
22 #define FOR_ALL_TEMPLATES(var) FOR_ALL_TEMPLATES_FROM(var, 0)
24 #define FOR_ALL_TEMPLATE_REPLACEMENTS_FROM(var, start) FOR_ALL_ITEMS_FROM(TemplateReplacement, template_replacement_index, var, start)
25 #define FOR_ALL_TEMPLATE_REPLACEMENTS(var) FOR_ALL_TEMPLATE_REPLACEMENTS_FROM(var, 0)
27 struct TemplateVehicle
;
28 struct TemplateReplacement
;
30 typedef uint16 TemplateID
;
33 static const uint16 CONSIST_HEAD
= 0x0;
34 static const uint16 CONSIST_TAIL
= 0xffff;
36 /** A pool allowing to store up to ~64k templates */
37 typedef Pool
<TemplateVehicle
, TemplateID
, 512, 0x10000> TemplatePool
;
38 extern TemplatePool _template_pool
;
40 /// listing/sorting templates
41 typedef GUIList
<const TemplateVehicle
*> GUITemplateList
;
43 struct TemplateVehicle
: TemplatePool::PoolItem
<&_template_pool
>, BaseVehicle
{
45 TemplateVehicle
*next
; ///< pointer to the next vehicle in the chain
46 TemplateVehicle
*previous
; ///< NOSAVE: pointer to the previous vehicle in the chain
47 TemplateVehicle
*first
; ///< NOSAVE: pointer to the first vehicle in the chain
50 friend const SaveLoad
* GTD();
51 friend void AfterLoadTemplateVehicles();
53 // Template usage configuration
54 bool reuse_depot_vehicles
;
55 bool keep_remaining_vehicles
;
56 bool refit_as_template
;
58 // Things derived from a virtual train
59 TemplateVehicle
*other_multiheaded_part
; ///< Multiheaded Engine support
60 Money value
; ///< Value of the vehicle
64 EngineID engine_type
; ///< The type of engine used for this vehicle.
65 CargoID cargo_type
; ///< type of cargo this vehicle is carrying
66 uint16 cargo_cap
; ///< total capacity
70 RailTypeByte railtype
;
74 uint16 real_consist_length
;
82 VehicleSpriteSeq sprite_seq
; ///< Vehicle appearance.
86 TemplateVehicle(VehicleType type
=VEH_INVALID
, EngineID e
=INVALID_ENGINE
, byte B
=0, Owner
=_local_company
);
87 TemplateVehicle(EngineID
, RailVehicleInfo
*);
88 TemplateVehicle(EngineID eid
) {
93 this->reuse_depot_vehicles
= true;
94 this->keep_remaining_vehicles
= true;
95 this->refit_as_template
= true;
96 this->sprite_seq
.count
= 1;
100 inline TemplateVehicle
* Next() const { return this->next
; }
101 inline TemplateVehicle
* Prev() const { return this->previous
; }
102 inline TemplateVehicle
* First() const { return this->first
; }
104 void SetNext(TemplateVehicle
*);
105 void SetPrev(TemplateVehicle
*);
106 void SetFirst(TemplateVehicle
*);
108 TemplateVehicle
* GetNextUnit() const;
109 TemplateVehicle
* GetPrevUnit();
111 bool IsSetReuseDepotVehicles() const { return this->reuse_depot_vehicles
; }
112 bool IsSetKeepRemainingVehicles() const { return this->keep_remaining_vehicles
; }
113 bool IsSetRefitAsTemplate() const { return this->refit_as_template
; }
114 void ToggleReuseDepotVehicles() { this->reuse_depot_vehicles
= !this->reuse_depot_vehicles
; }
115 void ToggleKeepRemainingVehicles() { this->keep_remaining_vehicles
= !this->keep_remaining_vehicles
; }
116 void ToggleRefitAsTemplate() { this->refit_as_template
= !this->refit_as_template
; }
118 bool IsPrimaryVehicle() const { return this->IsFrontEngine(); }
119 inline bool IsFrontEngine() const { return HasBit(this->subtype
, GVSF_FRONT
); }
120 inline bool HasArticulatedPart() const { return this->Next() != nullptr && this->Next()->IsArticulatedPart(); }
122 inline bool IsArticulatedPart() const { return HasBit(this->subtype
, GVSF_ARTICULATED_PART
); }
123 inline bool IsMultiheaded() const { return HasBit(this->subtype
, GVSF_MULTIHEADED
); }
125 inline bool IsFreeWagonChain() const { return HasBit(this->subtype
, GVSF_FREE_WAGON
); }
127 // since CmdBuildTemplateVehicle(...)
128 inline void SetFrontEngine() { SetBit(this->subtype
, GVSF_FRONT
); }
129 inline void SetEngine() { SetBit(this->subtype
, GVSF_ENGINE
); }
130 inline void SetArticulatedPart() { SetBit(this->subtype
, GVSF_ARTICULATED_PART
); }
131 inline void SetMultiheaded() { SetBit(this->subtype
, GVSF_MULTIHEADED
); }
133 inline void SetWagon() { SetBit(this->subtype
, GVSF_WAGON
); }
134 inline void SetFreeWagon() { SetBit(this->subtype
, GVSF_FREE_WAGON
); }
136 inline uint16
GetRealLength() const { return this->real_consist_length
; }
137 inline void SetRealLength(uint16 len
) { this->real_consist_length
= len
; }
141 short NumGroupsUsingTemplate() const;
145 // TemplateReplacement stuff
147 typedef Pool
<TemplateReplacement
, uint16
, 16, 1024> TemplateReplacementPool
;
148 extern TemplateReplacementPool _template_replacement_pool
;
150 struct TemplateReplacement
: TemplateReplacementPool::PoolItem
<&_template_replacement_pool
> {
152 TemplateID sel_template
;
154 TemplateReplacement(GroupID gid
, TemplateID tid
) { this->group
=gid
; this->sel_template
=tid
; }
155 TemplateReplacement() {}
156 ~TemplateReplacement() {}
158 inline GroupID
Group() { return this->group
; }
159 inline GroupID
Template() { return this->sel_template
; }
161 inline void SetGroup(GroupID gid
) { this->group
= gid
; }
162 inline void SetTemplate(TemplateID tid
) { this->sel_template
= tid
; }
164 inline TemplateID
GetTemplateVehicleID() { return sel_template
; }
165 inline const TemplateVehicle
* GetTemplateVehicle() {
166 const TemplateVehicle
*tv
;
167 FOR_ALL_TEMPLATES(tv
) {
168 if ( tv
->index
== this->sel_template
)
175 TemplateReplacement
* GetTemplateReplacementByGroupID(GroupID
);
176 bool IssueTemplateReplacement(GroupID
, TemplateID
);
178 short DeleteTemplateReplacementsByGroupID(GroupID
);
180 #endif /* TEMPLATE_VEH_H */