OpenTTD Source  14.0-beta1
newgrf_class_func.h
Go to the documentation of this file.
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  */
7 
10 #include "newgrf_class.h"
11 
12 #include "table/strings.h"
13 
18 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
19  template <typename Tspec, typename Tid, Tid Tmax> \
20  type NewGRFClass<Tspec, Tid, Tmax>
21 
23 template <typename Tspec, typename Tid, Tid Tmax>
25 
27 DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
28 {
29  this->global_id = 0;
30  this->name = STR_EMPTY;
31  this->ui_count = 0;
32 
33  this->spec.clear();
34 }
35 
37 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
38 {
39  for (Tid i = (Tid)0; i < Tmax; i++) {
40  classes[i].ResetClass();
41  }
42 
43  InsertDefaults();
44 }
45 
53 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32_t global_id)
54 {
55  for (Tid i = (Tid)0; i < Tmax; i++) {
56  if (classes[i].global_id == global_id) {
57  /* ClassID is already allocated, so reuse it. */
58  return i;
59  } else if (classes[i].global_id == 0) {
60  /* This class is empty, so allocate it to the global id. */
61  classes[i].global_id = global_id;
62  return i;
63  }
64  }
65 
66  GrfMsg(2, "ClassAllocate: already allocated {} classes, using default", Tmax);
67  return (Tid)0;
68 }
69 
74 DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
75 {
76  this->spec.push_back(spec);
77 
78  if (this->IsUIAvailable(static_cast<uint>(this->spec.size() - 1))) this->ui_count++;
79 }
80 
86 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
87 {
88  assert(spec->cls_id < Tmax);
89  Get(spec->cls_id)->Insert(spec);
90 }
91 
97 template <typename Tspec, typename Tid, Tid Tmax>
99 {
100  assert(cls_id < Tmax);
101  return classes + cls_id;
102 }
103 
108 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
109 {
110  uint i;
111  for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
112  return i;
113 }
114 
119 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
120 {
121  uint cnt = 0;
122  for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
123  if (classes[i].GetUISpecCount() > 0) cnt++;
124  }
125  return cnt;
126 }
127 
133 DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index)
134 {
135  for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
136  if (classes[i].GetUISpecCount() == 0) continue;
137  if (index-- == 0) return (Tid)i;
138  }
139  NOT_REACHED();
140 }
141 
147 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
148 {
149  /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
150  return index < this->GetSpecCount() ? this->spec[index] : nullptr;
151 }
152 
158 DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const
159 {
160  if (ui_index < 0) return -1;
161  for (uint i = 0; i < this->GetSpecCount(); i++) {
162  if (!this->IsUIAvailable(i)) continue;
163  if (ui_index-- == 0) return i;
164  }
165  return -1;
166 }
167 
173 DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const
174 {
175  if ((uint)index >= this->GetSpecCount()) return -1;
176  uint ui_index = 0;
177  for (int i = 0; i < index; i++) {
178  if (this->IsUIAvailable(i)) ui_index++;
179  }
180  return ui_index;
181 }
182 
190 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32_t grfid, uint16_t local_id, int *index)
191 {
192  uint j;
193 
194  for (Tid i = (Tid)0; i < Tmax; i++) {
195  uint count = static_cast<uint>(classes[i].spec.size());
196  for (j = 0; j < count; j++) {
197  const Tspec *spec = classes[i].spec[j];
198  if (spec == nullptr) continue;
199  if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
200  if (index != nullptr) *index = j;
201  return spec;
202  }
203  }
204  }
205 
206  return nullptr;
207 }
208 
209 #undef DEFINE_NEWGRF_CLASS_METHOD
210 
212 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
213  template void name::ResetClass(); \
214  template void name::Reset(); \
215  template Tid name::Allocate(uint32_t global_id); \
216  template void name::Insert(Tspec *spec); \
217  template void name::Assign(Tspec *spec); \
218  template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \
219  template uint name::GetClassCount(); \
220  template uint name::GetUIClassCount(); \
221  template Tid name::GetUIClass(uint index); \
222  template const Tspec *name::GetSpec(uint index) const; \
223  template int name::GetUIFromIndex(int index) const; \
224  template int name::GetIndexFromUI(int ui_index) const; \
225  template const Tspec *name::GetByGrf(uint32_t grfid, uint16_t local_id, int *index);
DEFINE_NEWGRF_CLASS_METHOD
#define DEFINE_NEWGRF_CLASS_METHOD(type)
Helper for defining the class method's signatures.
Definition: newgrf_class_func.h:18
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:20
newgrf_class.h
NewGRFClass::Get
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
Definition: newgrf_class_func.h:98