OpenTTD: src/newgrf_class_func.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_class_func.h
Go to the documentation of this file.
1 /* $Id: newgrf_class_func.h 24170 2012-04-22 16:28:32Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "newgrf_class.h"
13
14 #include "table/strings.h"
15
20 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
21 template <typename Tspec, typename Tid, Tid Tmax> \
22 type NewGRFClass<Tspec, Tid, Tmax>
23
25 template <typename Tspec, typename Tid, Tid Tmax>
26 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
27
29 DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
30 {
31 this->global_id = 0;
32 this->name = STR_EMPTY;
33 this->count = 0;
34 this->ui_count = 0;
35
36 free(this->spec);
37 this->spec = NULL;
38 }
39
41 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
42 {
43 for (Tid i = (Tid)0; i < Tmax; i++) {
44 classes[i].ResetClass();
45 }
46
47 InsertDefaults();
48 }
49
57 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
58 {
59 for (Tid i = (Tid)0; i < Tmax; i++) {
60 if (classes[i].global_id == global_id) {
61 /* ClassID is already allocated, so reuse it. */
62 return i;
63 } else if (classes[i].global_id == 0) {
64 /* This class is empty, so allocate it to the global id. */
65 classes[i].global_id = global_id;
66 return i;
67 }
68 }
69
70 grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
71 return (Tid)0;
72 }
73
78 DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
79 {
80 uint i = this->count++;
81 this->spec = ReallocT(this->spec, this->count);
82
83 this->spec[i] = spec;
84
85 if (this->IsUIAvailable(i)) this->ui_count++;
86 }
87
93 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
94 {
95 assert(spec->cls_id < Tmax);
96 Get(spec->cls_id)->Insert(spec);
97 }
98
104 template <typename Tspec, typename Tid, Tid Tmax>
105 NewGRFClass<Tspec, Tid, Tmax> *NewGRFClass<Tspec, Tid, Tmax>::Get(Tid cls_id)
106 {
107 assert(cls_id < Tmax);
108 return classes + cls_id;
109 }
110
115 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
116 {
117 uint i;
118 for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
119 return i;
120 }
121
126 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
127 {
128 uint cnt = 0;
129 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
130 if (classes[i].GetUISpecCount() > 0) cnt++;
131 }
132 return cnt;
133 }
134
140 DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index)
141 {
142 for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
143 if (classes[i].GetUISpecCount() == 0) continue;
144 if (index-- == 0) return (Tid)i;
145 }
146 NOT_REACHED();
147 }
148
154 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
155 {
156 /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
157 return index < this->GetSpecCount() ? this->spec[index] : NULL;
158 }
159
165 DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const
166 {
167 if (ui_index < 0) return -1;
168 for (uint i = 0; i < this->GetSpecCount(); i++) {
169 if (!this->IsUIAvailable(i)) continue;
170 if (ui_index-- == 0) return i;
171 }
172 return -1;
173 }
174
180 DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const
181 {
182 if ((uint)index >= this->GetSpecCount()) return -1;
183 uint ui_index = 0;
184 for (int i = 0; i < index; i++) {
185 if (this->IsUIAvailable(i)) ui_index++;
186 }
187 return ui_index;
188 }
189
197 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
198 {
199 uint j;
200
201 for (Tid i = (Tid)0; i < Tmax; i++) {
202 for (j = 0; j < classes[i].count; j++) {
203 const Tspec *spec = classes[i].spec[j];
204 if (spec == NULL) continue;
205 if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
206 if (index != NULL) *index = j;
207 return spec;
208 }
209 }
210 }
211
212 return NULL;
213 }
214
215 #undef DEFINE_NEWGRF_CLASS_METHOD
216
218 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
219 template void name::ResetClass(); \
220 template void name::Reset(); \
221 template Tid name::Allocate(uint32 global_id); \
222 template void name::Insert(Tspec *spec); \
223 template void name::Assign(Tspec *spec); \
224 template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \
225 template uint name::GetClassCount(); \
226 template uint name::GetUIClassCount(); \
227 template Tid name::GetUIClass(uint index); \
228 template const Tspec *name::GetSpec(uint index) const; \
229 template int name::GetUIFromIndex(int index) const; \
230 template int name::GetIndexFromUI(int ui_index) const; \
231 template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);