2 #include "company_func.h"
4 #include "command_func.h"
5 #include "engine_func.h"
6 #include "vehicle_func.h"
7 #include "autoreplace_func.h"
8 #include "autoreplace_gui.h"
10 #include "articulated_vehicles.h"
11 #include "core/random_func.hpp"
12 #include "core/pool_type.hpp"
13 #include "engine_type.h"
14 #include "group_type.h"
15 #include "core/pool_func.hpp"
17 #include "table/strings.h"
21 #include "vehicle_type.h"
22 #include "vehicle_base.h"
23 #include "vehicle_func.h"
25 #include "table/train_cmd.h"
28 #include "tbtr_template_vehicle.h"
30 // since doing stuff with sprites
31 #include "newgrf_spritegroup.h"
32 #include "newgrf_engine.h"
33 #include "newgrf_cargo.h"
35 TemplatePool
_template_pool("TemplatePool");
36 INSTANTIATE_POOL_METHODS(Template
)
38 TemplateReplacementPool
_template_replacement_pool("TemplateReplacementPool");
39 INSTANTIATE_POOL_METHODS(TemplateReplacement
)
42 TemplateVehicle::TemplateVehicle(VehicleType ty
, EngineID eid
, byte subtypeflag
, Owner current_owner
)
45 this->engine_type
= eid
;
47 this->reuse_depot_vehicles
= true;
48 this->keep_remaining_vehicles
= true;
53 this->owner_b
= _current_company
;
55 this->sprite_seq
.Set(SPR_IMG_QUERY
);
56 this->image_offset
.x
= 0;
57 this->image_offset
.y
= 0;
59 this->owner
= current_owner
;
61 this->real_consist_length
= 0;
64 TemplateVehicle::~TemplateVehicle() {
65 TemplateVehicle
*v
= this->Next();
66 this->SetNext(nullptr);
72 void TemplateVehicle::SetNext(TemplateVehicle
*v
) { this->next
= v
; }
73 void TemplateVehicle::SetPrev(TemplateVehicle
*v
) { this->previous
= v
; }
74 void TemplateVehicle::SetFirst(TemplateVehicle
*v
) { this->first
= v
; }
76 TemplateVehicle
* TemplateVehicle::GetNextUnit() const
78 TemplateVehicle
*tv
= this->Next();
79 while ( tv
&& HasBit(tv
->subtype
, GVSF_ARTICULATED_PART
) ) tv
= tv
->Next();
80 if ( tv
&& HasBit(tv
->subtype
, GVSF_MULTIHEADED
) && !HasBit(tv
->subtype
, GVSF_ENGINE
) ) tv
= tv
->Next();
84 TemplateVehicle
* TemplateVehicle::GetPrevUnit()
86 TemplateVehicle
*tv
= this->Prev();
87 while ( tv
&& HasBit(tv
->subtype
, GVSF_ARTICULATED_PART
|GVSF_ENGINE
) ) tv
= tv
->Prev();
88 if ( tv
&& HasBit(tv
->subtype
, GVSF_MULTIHEADED
|GVSF_ENGINE
) ) tv
= tv
->Prev();
93 * @return: length of vehicle, including current part
95 int TemplateVehicle::Length() const
98 const TemplateVehicle
*tmp
=this;
99 while ( tmp
->Next() ) { tmp
=tmp
->Next(); l
++; }
103 TemplateReplacement
* GetTemplateReplacementByGroupID(GroupID gid
)
105 TemplateReplacement
*tr
;
106 FOR_ALL_TEMPLATE_REPLACEMENTS(tr
) {
107 if ( tr
->Group() == gid
)
113 bool IssueTemplateReplacement(GroupID gid
, TemplateID tid
) {
115 TemplateReplacement
*tr
= GetTemplateReplacementByGroupID(gid
);
118 /* Then set the new TemplateVehicle and return */
119 tr
->SetTemplate(tid
);
123 else if ( TemplateReplacement::CanAllocateItem() ) {
124 tr
= new TemplateReplacement(gid
, tid
);
131 short TemplateVehicle::NumGroupsUsingTemplate() const
134 const TemplateReplacement
*tr
;
135 FOR_ALL_TEMPLATE_REPLACEMENTS(tr
) {
136 if ( tr
->sel_template
== this->index
)
142 short DeleteTemplateReplacementsByGroupID(GroupID g_id
)
144 short del_amount
= 0;
145 const TemplateReplacement
*tr
;
146 FOR_ALL_TEMPLATE_REPLACEMENTS(tr
) {
147 if ( tr
->group
== g_id
) {