Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / newgrf_class_func.h
blobf97ba58b9537106e186aac8020ddaf40148c4f9e
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file newgrf_class_func.h Implementation of the NewGRF class' functions. */
10 #include "newgrf_class.h"
12 #include "table/strings.h"
14 /**
15 * Helper for defining the class method's signatures.
16 * @param type The type of the class.
18 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
19 template <typename Tspec, typename Tid, Tid Tmax> \
20 type NewGRFClass<Tspec, Tid, Tmax>
22 /** Instantiate the array. */
23 template <typename Tspec, typename Tid, Tid Tmax>
24 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
26 /** Reset the class, i.e. clear everything. */
27 DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
29 this->global_id = 0;
30 this->name = STR_EMPTY;
31 this->count = 0;
32 this->ui_count = 0;
34 free(this->spec);
35 this->spec = nullptr;
38 /** Reset the classes, i.e. clear everything. */
39 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
41 for (Tid i = (Tid)0; i < Tmax; i++) {
42 classes[i].ResetClass();
45 InsertDefaults();
48 /**
49 * Allocate a class with a given global class ID.
50 * @param cls_id The global class id, such as 'DFLT'.
51 * @return The (non global!) class ID for the class.
52 * @note Upon allocating the same global class ID for a
53 * second time, this first allocation will be given.
55 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
57 for (Tid i = (Tid)0; i < Tmax; i++) {
58 if (classes[i].global_id == global_id) {
59 /* ClassID is already allocated, so reuse it. */
60 return i;
61 } else if (classes[i].global_id == 0) {
62 /* This class is empty, so allocate it to the global id. */
63 classes[i].global_id = global_id;
64 return i;
68 grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
69 return (Tid)0;
72 /**
73 * Insert a spec into the class.
74 * @param spec The spec to insert.
76 DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
78 uint i = this->count++;
79 this->spec = ReallocT(this->spec, this->count);
81 this->spec[i] = spec;
83 if (this->IsUIAvailable(i)) this->ui_count++;
86 /**
87 * Assign a spec to one of the classes.
88 * @param spec The spec to assign.
89 * @note The spec must have a valid class id set.
91 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
93 assert(spec->cls_id < Tmax);
94 Get(spec->cls_id)->Insert(spec);
97 /**
98 * Get a particular class.
99 * @param cls_id The id for the class.
100 * @pre cls_id < Tmax
102 template <typename Tspec, typename Tid, Tid Tmax>
103 NewGRFClass<Tspec, Tid, Tmax> *NewGRFClass<Tspec, Tid, Tmax>::Get(Tid cls_id)
105 assert(cls_id < Tmax);
106 return classes + cls_id;
110 * Get the number of allocated classes.
111 * @return The number of classes.
113 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
115 uint i;
116 for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
117 return i;
121 * Get the number of classes available to the user.
122 * @return The number of classes.
124 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
126 uint cnt = 0;
127 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
128 if (classes[i].GetUISpecCount() > 0) cnt++;
130 return cnt;
134 * Get the nth-class with user available specs.
135 * @param index UI index of a class.
136 * @return The class ID of the class.
138 DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index)
140 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
141 if (classes[i].GetUISpecCount() == 0) continue;
142 if (index-- == 0) return (Tid)i;
144 NOT_REACHED();
148 * Get a spec from the class at a given index.
149 * @param index The index where to find the spec.
150 * @return The spec at given location.
152 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
154 /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
155 return index < this->GetSpecCount() ? this->spec[index] : nullptr;
159 * Translate a UI spec index into a spec index.
160 * @param ui_index UI index of the spec.
161 * @return index of the spec, or -1 if out of range.
163 DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const
165 if (ui_index < 0) return -1;
166 for (uint i = 0; i < this->GetSpecCount(); i++) {
167 if (!this->IsUIAvailable(i)) continue;
168 if (ui_index-- == 0) return i;
170 return -1;
174 * Translate a spec index into a UI spec index.
175 * @param index index of the spec.
176 * @return UI index of the spec, or -1 if out of range.
178 DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const
180 if ((uint)index >= this->GetSpecCount()) return -1;
181 uint ui_index = 0;
182 for (int i = 0; i < index; i++) {
183 if (this->IsUIAvailable(i)) ui_index++;
185 return ui_index;
189 * Retrieve a spec by GRF location.
190 * @param grfid GRF ID of spec.
191 * @param local_id Index within GRF file of spec.
192 * @param index Pointer to return the index of the spec in its class. If nullptr then not used.
193 * @return The spec.
195 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
197 uint j;
199 for (Tid i = (Tid)0; i < Tmax; i++) {
200 for (j = 0; j < classes[i].count; j++) {
201 const Tspec *spec = classes[i].spec[j];
202 if (spec == nullptr) continue;
203 if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
204 if (index != nullptr) *index = j;
205 return spec;
210 return nullptr;
213 #undef DEFINE_NEWGRF_CLASS_METHOD
215 /** Force instantiation of the methods so we don't get linker errors. */
216 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
217 template void name::ResetClass(); \
218 template void name::Reset(); \
219 template Tid name::Allocate(uint32 global_id); \
220 template void name::Insert(Tspec *spec); \
221 template void name::Assign(Tspec *spec); \
222 template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \
223 template uint name::GetClassCount(); \
224 template uint name::GetUIClassCount(); \
225 template Tid name::GetUIClass(uint index); \
226 template const Tspec *name::GetSpec(uint index) const; \
227 template int name::GetUIFromIndex(int index) const; \
228 template int name::GetIndexFromUI(int ui_index) const; \
229 template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);