OpenTTD Source: src/newgrf_class_func.h Source File

OpenTTD Source 12.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>
24 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
25
27 DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
28 {
29 this->global_id = 0;
30 this->name = STR_EMPTY;
31 this->count = 0;
32 this->ui_count = 0;
33
34 free(this->spec);
35 this->spec = nullptr;
36 }
37
39 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
40 {
41 for (Tid i = (Tid)0; i < Tmax; i++) {
42 classes[i].ResetClass();
43 }
44
45 InsertDefaults();
46 }
47
55 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
56 {
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;
65 }
66 }
67
68 grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
69 return (Tid)0;
70 }
71
76 DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
77 {
78 uint i = this->count++;
79 this->spec = ReallocT(this->spec, this->count);
80
81 this->spec[i] = spec;
82
83 if (this->IsUIAvailable(i)) this->ui_count++;
84 }
85
91 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
92 {
93 assert(spec->cls_id < Tmax);
94 Get(spec->cls_id)->Insert(spec);
95 }
96
102 template <typename Tspec, typename Tid, Tid Tmax>
103 NewGRFClass<Tspec, Tid, Tmax> *NewGRFClass<Tspec, Tid, Tmax>::Get(Tid cls_id)
104 {
105 assert(cls_id < Tmax);
106 return classes + cls_id;
107 }
108
113 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
114 {
115 uint i;
116 for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
117 return i;
118 }
119
124 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
125 {
126 uint cnt = 0;
127 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
128 if (classes[i].GetUISpecCount() > 0) cnt++;
129 }
130 return cnt;
131 }
132
138 DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index)
139 {
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;
143 }
144 NOT_REACHED();
145 }
146
152 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
153 {
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;
156 }
157
163 DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const
164 {
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;
169 }
170 return -1;
171 }
172
178 DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const
179 {
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++;
184 }
185 return ui_index;
186 }
187
195 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
196 {
197 uint j;
198
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;
206 }
207 }
208 }
209
210 return nullptr;
211 }
212
213 #undef DEFINE_NEWGRF_CLASS_METHOD
214
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);
DEFINE_NEWGRF_CLASS_METHOD
#define DEFINE_NEWGRF_CLASS_METHOD(type)
Helper for defining the class method's signatures.
Definition: newgrf_class_func.h:18
grfmsg
void CDECL grfmsg(int severity, const char *str,...)
Debug() function dedicated to newGRF debugging messages Function is essentially the same as Debug(grf...
Definition: newgrf.cpp:391
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:19
newgrf_class.h
ReallocT
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
Definition: alloc_func.hpp:111
NewGRFClass::Get
static NewGRFClass * Get(Tid cls_id)
Get a particular class.
Definition: newgrf_class_func.h:103
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460