diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/newgrf_class.h | 3 | ||||
-rw-r--r-- | src/newgrf_class_func.h | 47 |
2 files changed, 50 insertions, 0 deletions
diff --git a/src/newgrf_class.h b/src/newgrf_class.h index 2dfeceba3..71b5608d8 100644 --- a/src/newgrf_class.h +++ b/src/newgrf_class.h @@ -46,6 +46,8 @@ public: uint GetSpecCount() const { return this->count; } /** Get the number of potentially user-available specs within the class. */ uint GetUISpecCount() const { return this->ui_count; } + int GetUIFromIndex(int index) const; + int GetIndexFromUI(int ui_index) const; const Tspec *GetSpec(uint index) const; @@ -57,6 +59,7 @@ public: static void Assign(Tspec *spec); static uint GetClassCount(); static uint GetUIClassCount(); + static Tid GetUIClass(uint index); static NewGRFClass *Get(Tid cls_id); static const Tspec *GetByGrf(uint32 grfid, byte local_id, int *index); diff --git a/src/newgrf_class_func.h b/src/newgrf_class_func.h index b5af35fdb..c2a30992a 100644 --- a/src/newgrf_class_func.h +++ b/src/newgrf_class_func.h @@ -133,6 +133,20 @@ DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount() } /** + * Get the nth-class with user available specs. + * @param index UI index of a class. + * @return The class ID of the class. + */ +DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index) +{ + for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) { + if (classes[i].GetUISpecCount() == 0) continue; + if (index-- == 0) return (Tid)i; + } + NOT_REACHED(); +} + +/** * Get a spec from the class at a given index. * @param index The index where to find the spec. * @return The spec at given location. @@ -144,6 +158,36 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const } /** + * Translate a UI spec index into a spec index. + * @param ui_index UI index of the spec. + * @return index of the spec, or -1 if out of range. + */ +DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const +{ + if (ui_index < 0) return -1; + for (uint i = 0; i < this->GetSpecCount(); i++) { + if (!this->IsUIAvailable(i)) continue; + if (ui_index-- == 0) return i; + } + return -1; +} + +/** + * Translate a spec index into a UI spec index. + * @param index index of the spec. + * @return UI index of the spec, or -1 if out of range. + */ +DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const +{ + if ((uint)index >= this->GetSpecCount()) return -1; + uint ui_index = 0; + for (int i = 0; i < index; i++) { + if (this->IsUIAvailable(i)) ui_index++; + } + return ui_index; +} + +/** * Retrieve a spec by GRF location. * @param grfid GRF ID of spec. * @param local_id Index within GRF file of spec. @@ -180,5 +224,8 @@ DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \ template uint name::GetClassCount(); \ template uint name::GetUIClassCount(); \ + template Tid name::GetUIClass(uint index); \ template const Tspec *name::GetSpec(uint index) const; \ + template int name::GetUIFromIndex(int index) const; \ + template int name::GetIndexFromUI(int ui_index) const; \ template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index); |