OpenTTD Source: src/newgrf_class_func.h Source File

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