OpenTTD Source 15.0-beta2
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
13template <typename Tspec, typename Tindex, Tindex Tmax>
21
29template <typename Tspec, typename Tindex, Tindex Tmax>
31{
32 auto found = std::ranges::find(NewGRFClass::classes, global_id, &NewGRFClass::global_id);
33
34 /* Id is already allocated, so reuse it. */
35 if (found != std::end(NewGRFClass::classes)) return found->Index();
36
37 /* More slots available, allocate a slot to the global id. */
38 if (NewGRFClass::classes.size() < Tmax) {
39 auto it = NewGRFClass::classes.emplace(std::end(NewGRFClass::classes), global_id, STR_EMPTY);
40 it->index = static_cast<Tindex>(std::distance(std::begin(NewGRFClass::classes), it));
41 return it->Index();
42 }
43
44 GrfMsg(2, "ClassAllocate: already allocated {} classes, using default", Tmax);
45 return static_cast<Tindex>(0);
46}
47
52template <typename Tspec, typename Tindex, Tindex Tmax>
54{
55 auto it = this->spec.insert(std::end(this->spec), spec);
56 uint16_t index = static_cast<uint16_t>(std::distance(std::begin(this->spec), it));
57 if (spec != nullptr) (*it)->index = index;
58
59 if (this->IsUIAvailable(index)) this->ui_count++;
60}
61
67template <typename Tspec, typename Tindex, Tindex Tmax>
69{
70 assert(static_cast<size_t>(spec->class_index) < NewGRFClass::classes.size());
71 Get(spec->class_index)->Insert(spec);
72}
73
79template <typename Tspec, typename Tindex, Tindex Tmax>
81{
82 assert(static_cast<size_t>(class_index) < NewGRFClass::classes.size());
83 return &NewGRFClass::classes[class_index];
84}
85
90template <typename Tspec, typename Tindex, Tindex Tmax>
92{
93 return static_cast<uint>(NewGRFClass::classes.size());
94}
95
100template <typename Tspec, typename Tindex, Tindex Tmax>
102{
103 return std::ranges::count_if(NewGRFClass::classes, [](const auto &cls) { return cls.GetUISpecCount() > 0; });
104}
105
111template <typename Tspec, typename Tindex, Tindex Tmax>
112const Tspec *NewGRFClass<Tspec, Tindex, Tmax>::GetSpec(uint index) const
113{
114 /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
115 return index < this->GetSpecCount() ? this->spec[index] : nullptr;
116}
117
125template <typename Tspec, typename Tindex, Tindex Tmax>
126const Tspec *NewGRFClass<Tspec, Tindex, Tmax>::GetByGrf(uint32_t grfid, uint16_t local_id)
127{
128 for (const auto &cls : NewGRFClass::classes) {
129 for (const auto &spec : cls.spec) {
130 if (spec == nullptr) continue;
131 if (spec->grf_prop.local_id != local_id) continue;
132 if (spec->grf_prop.grfid == grfid) return spec;
133 }
134 }
135
136 return nullptr;
137}
Struct containing information relating to NewGRF classes for stations and airports.
static void Assign(Tspec *spec)
Assign a spec to one of the classes.
void Insert(Tspec *spec)
Insert a spec into the class, and update its index.
uint32_t global_id
Global ID for class, e.g. 'DFLT', 'WAYP', etc.
static NewGRFClass * Get(Tindex class_index)
Get a particular class.
static uint GetUIClassCount()
Get the number of classes available to the user.
static Tindex Allocate(uint32_t global_id)
Allocate a class with a given global class ID.
static const Tspec * GetByGrf(uint32_t grfid, uint16_t local_id)
Retrieve a spec by GRF location.
static uint GetClassCount()
Get the number of allocated classes.
static void InsertDefaults()
Initialise the defaults.
const Tspec * GetSpec(uint index) const
Get a spec from the class at a given index.
static std::vector< NewGRFClass< Tspec, Tindex, Tmax > > classes
The actual classes.
static void Reset()
Reset the classes, i.e.
Header file for classes to be used by e.g.