Update readme and changelog for v1.27.0
[openttd-joker.git] / src / tbtr_template_vehicle.cpp
blob260569db6f904caceeeb7620ecbee936a6e94ed3
1 #include "stdafx.h"
2 #include "company_func.h"
3 #include "train.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"
9 #include "group.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"
19 #include "newgrf.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)
44 this->type = ty;
45 this->engine_type = eid;
47 this->reuse_depot_vehicles = true;
48 this->keep_remaining_vehicles = true;
50 this->first = this;
51 this->next = 0x0;
52 this->previous = 0x0;
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);
68 delete v;
71 /** getting */
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();
81 return tv;
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();
89 return tv;
92 /** Length()
93 * @return: length of vehicle, including current part
95 int TemplateVehicle::Length() const
97 int l=1;
98 const TemplateVehicle *tmp=this;
99 while ( tmp->Next() ) { tmp=tmp->Next(); l++; }
100 return l;
103 TemplateReplacement* GetTemplateReplacementByGroupID(GroupID gid)
105 TemplateReplacement *tr;
106 FOR_ALL_TEMPLATE_REPLACEMENTS(tr) {
107 if ( tr->Group() == gid )
108 return tr;
110 return 0;
113 bool IssueTemplateReplacement(GroupID gid, TemplateID tid) {
115 TemplateReplacement *tr = GetTemplateReplacementByGroupID(gid);
117 if ( tr ) {
118 /* Then set the new TemplateVehicle and return */
119 tr->SetTemplate(tid);
120 return true;
123 else if ( TemplateReplacement::CanAllocateItem() ) {
124 tr = new TemplateReplacement(gid, tid);
125 return true;
128 else return false;
131 short TemplateVehicle::NumGroupsUsingTemplate() const
133 short amount = 0;
134 const TemplateReplacement *tr;
135 FOR_ALL_TEMPLATE_REPLACEMENTS(tr) {
136 if ( tr->sel_template == this->index )
137 amount++;
139 return amount;
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 ) {
148 delete tr;
149 del_amount++;
152 return del_amount;