newgrf_class_func.h

Go to the documentation of this file.
00001 /* $Id: newgrf_class_func.h 24170 2012-04-22 16:28:32Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "newgrf_class.h"
00013 
00014 #include "table/strings.h"
00015 
00020 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
00021   template <typename Tspec, typename Tid, Tid Tmax> \
00022   type NewGRFClass<Tspec, Tid, Tmax>
00023 
00025 template <typename Tspec, typename Tid, Tid Tmax>
00026 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
00027 
00029 DEFINE_NEWGRF_CLASS_METHOD(void)::ResetClass()
00030 {
00031   this->global_id = 0;
00032   this->name      = STR_EMPTY;
00033   this->count     = 0;
00034   this->ui_count  = 0;
00035 
00036   free(this->spec);
00037   this->spec = NULL;
00038 }
00039 
00041 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
00042 {
00043   for (Tid i = (Tid)0; i < Tmax; i++) {
00044     classes[i].ResetClass();
00045   }
00046 
00047   InsertDefaults();
00048 }
00049 
00057 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
00058 {
00059   for (Tid i = (Tid)0; i < Tmax; i++) {
00060     if (classes[i].global_id == global_id) {
00061       /* ClassID is already allocated, so reuse it. */
00062       return i;
00063     } else if (classes[i].global_id == 0) {
00064       /* This class is empty, so allocate it to the global id. */
00065       classes[i].global_id = global_id;
00066       return i;
00067     }
00068   }
00069 
00070   grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
00071   return (Tid)0;
00072 }
00073 
00078 DEFINE_NEWGRF_CLASS_METHOD(void)::Insert(Tspec *spec)
00079 {
00080   uint i = this->count++;
00081   this->spec = ReallocT(this->spec, this->count);
00082 
00083   this->spec[i] = spec;
00084 
00085   if (this->IsUIAvailable(i)) this->ui_count++;
00086 }
00087 
00093 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
00094 {
00095   assert(spec->cls_id < Tmax);
00096   Get(spec->cls_id)->Insert(spec);
00097 }
00098 
00104 template <typename Tspec, typename Tid, Tid Tmax>
00105 NewGRFClass<Tspec, Tid, Tmax> *NewGRFClass<Tspec, Tid, Tmax>::Get(Tid cls_id)
00106 {
00107   assert(cls_id < Tmax);
00108   return classes + cls_id;
00109 }
00110 
00115 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetClassCount()
00116 {
00117   uint i;
00118   for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
00119   return i;
00120 }
00121 
00126 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetUIClassCount()
00127 {
00128   uint cnt = 0;
00129   for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
00130     if (classes[i].GetUISpecCount() > 0) cnt++;
00131   }
00132   return cnt;
00133 }
00134 
00140 DEFINE_NEWGRF_CLASS_METHOD(Tid)::GetUIClass(uint index)
00141 {
00142   for (uint i = 0; i < Tmax && classes[i].global_id != 0; i++) {
00143     if (classes[i].GetUISpecCount() == 0) continue;
00144     if (index-- == 0) return (Tid)i;
00145   }
00146   NOT_REACHED();
00147 }
00148 
00154 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetSpec(uint index) const
00155 {
00156   /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
00157   return index < this->GetSpecCount() ? this->spec[index] : NULL;
00158 }
00159 
00165 DEFINE_NEWGRF_CLASS_METHOD(int)::GetIndexFromUI(int ui_index) const
00166 {
00167   if (ui_index < 0) return -1;
00168   for (uint i = 0; i < this->GetSpecCount(); i++) {
00169     if (!this->IsUIAvailable(i)) continue;
00170     if (ui_index-- == 0) return i;
00171   }
00172   return -1;
00173 }
00174 
00180 DEFINE_NEWGRF_CLASS_METHOD(int)::GetUIFromIndex(int index) const
00181 {
00182   if ((uint)index >= this->GetSpecCount()) return -1;
00183   uint ui_index = 0;
00184   for (int i = 0; i < index; i++) {
00185     if (this->IsUIAvailable(i)) ui_index++;
00186   }
00187   return ui_index;
00188 }
00189 
00197 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
00198 {
00199   uint j;
00200 
00201   for (Tid i = (Tid)0; i < Tmax; i++) {
00202     for (j = 0; j < classes[i].count; j++) {
00203       const Tspec *spec = classes[i].spec[j];
00204       if (spec == NULL) continue;
00205       if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
00206         if (index != NULL) *index = j;
00207         return spec;
00208       }
00209     }
00210   }
00211 
00212   return NULL;
00213 }
00214 
00215 #undef DEFINE_NEWGRF_CLASS_METHOD
00216 
00218 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
00219   template void name::ResetClass(); \
00220   template void name::Reset(); \
00221   template Tid name::Allocate(uint32 global_id); \
00222   template void name::Insert(Tspec *spec); \
00223   template void name::Assign(Tspec *spec); \
00224   template NewGRFClass<Tspec, Tid, Tmax> *name::Get(Tid cls_id); \
00225   template uint name::GetClassCount(); \
00226   template uint name::GetUIClassCount(); \
00227   template Tid name::GetUIClass(uint index); \
00228   template const Tspec *name::GetSpec(uint index) const; \
00229   template int name::GetUIFromIndex(int index) const; \
00230   template int name::GetIndexFromUI(int ui_index) const; \
00231   template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);