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 
8 /** @file newgrf_class_func.h Implementation of the NewGRF class' functions. */
9 
10 #include "newgrf_class.h"
11 
12 #include "table/strings.h"
13 
14 /**
15  * Helper for defining the class method's signatures.
16  * @param type The type of the class.
17  */
18 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
19 	template <typename Tspec, typename Tid, Tid Tmax> \
20 	type NewGRFClass<Tspec, Tid, Tmax>
21 
22 /** Instantiate the array. */
23 template <typename Tspec, typename Tid, Tid Tmax>
24 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
25 
26 /** Reset the class, i.e. clear everything. */
DEFINE_NEWGRF_CLASS_METHOD(void)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 
38 /** Reset the classes, i.e. clear everything. */
DEFINE_NEWGRF_CLASS_METHOD(void)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 
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.
54  */
DEFINE_NEWGRF_CLASS_METHOD(Tid)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 
72 /**
73  * Insert a spec into the class.
74  * @param spec The spec to insert.
75  */
DEFINE_NEWGRF_CLASS_METHOD(void)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 
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.
90  */
DEFINE_NEWGRF_CLASS_METHOD(void)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 
97 /**
98  * Get a particular class.
99  * @param cls_id The id for the class.
100  * @pre cls_id < Tmax
101  */
102 template <typename Tspec, typename Tid, Tid Tmax>
Get(Tid cls_id)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 
109 /**
110  * Get the number of allocated classes.
111  * @return The number of classes.
112  */
DEFINE_NEWGRF_CLASS_METHOD(uint)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 
120 /**
121  * Get the number of classes available to the user.
122  * @return The number of classes.
123  */
DEFINE_NEWGRF_CLASS_METHOD(uint)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 
133 /**
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.
137  */
DEFINE_NEWGRF_CLASS_METHOD(Tid)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 
147 /**
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.
151  */
DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)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 
158 /**
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.
162  */
DEFINE_NEWGRF_CLASS_METHOD(int)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 
173 /**
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.
177  */
DEFINE_NEWGRF_CLASS_METHOD(int)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 
188 /**
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.
194  */
DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)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 
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);
230