# newgrf_class_func.h

Go to the documentation of this file.

    00001 /* $Id$ */
    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)::Reset()
    00030 {
    00031   for (Tid i = (Tid)0; i < Tmax; i++) {
    00032     classes[i].global_id = 0;
    00033     classes[i].name      = STR_EMPTY;
    00034     classes[i].count     = 0;
    00035
    00036     free(classes[i].spec);
    00037     classes[i].spec = NULL;
    00038   }
    00039
    00040   InsertDefaults();
    00041 }
    00042
    00050 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
    00051 {
    00052   for (Tid i = (Tid)0; i < Tmax; i++) {
    00053     if (classes[i].global_id == global_id) {
    00054       /* ClassID is already allocated, so reuse it. */
    00055       return i;
    00056     } else if (classes[i].global_id == 0) {
    00057       /* This class is empty, so allocate it to the global id. */
    00058       classes[i].global_id = global_id;
    00059       return i;
    00060     }
    00061   }
    00062
    00063   grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
    00064   return (Tid)0;
    00065 }
    00066
    00073 DEFINE_NEWGRF_CLASS_METHOD(void)::SetName(Tid cls_id, StringID name)
    00074 {
    00075   assert(cls_id < Tmax);
    00076   classes[cls_id].name = name;
    00077 }
    00078
    00084 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
    00085 {
    00086   assert(spec->cls_id < Tmax);
    00087   NewGRFClass<Tspec, Tid, Tmax> *cls = &classes[spec->cls_id];
    00088
    00089   uint i = cls->count++;
    00090   cls->spec = ReallocT(cls->spec, cls->count);
    00091
    00092   cls->spec[i] = spec;
    00093 }
    00094
    00101 DEFINE_NEWGRF_CLASS_METHOD(StringID)::GetName(Tid cls_id)
    00102 {
    00103   assert(cls_id < Tmax);
    00104   return classes[cls_id].name;
    00105 }
    00106
    00111 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetCount()
    00112 {
    00113   uint i;
    00114   for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
    00115   return i;
    00116 }
    00117
    00124 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetCount(Tid cls_id)
    00125 {
    00126   assert(cls_id < Tmax);
    00127   return classes[cls_id].count;
    00128 }
    00129
    00137 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::Get(Tid cls_id, uint index)
    00138 {
    00139   assert(cls_id < Tmax);
    00140   if (index < classes[cls_id].count) return classes[cls_id].spec[index];
    00141
    00142   /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
    00143   return NULL;
    00144 }
    00145
    00153 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
    00154 {
    00155   uint j;
    00156
    00157   for (Tid i = (Tid)0; i < Tmax; i++) {
    00158     for (j = 0; j < classes[i].count; j++) {
    00159       const Tspec *spec = classes[i].spec[j];
    00160       if (spec == NULL) continue;
    00161       if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
    00162         if (index != NULL) *index = j;
    00163         return spec;
    00164       }
    00165     }
    00166   }
    00167
    00168   return NULL;
    00169 }
    00170
    00171 #undef DEFINE_NEWGRF_CLASS_METHOD
    00172
    00174 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
    00175   template void name::Reset(); \
    00176   template Tid name::Allocate(uint32 global_id); \
    00177   template void name::SetName(Tid cls_id, StringID name); \
    00178   template void name::Assign(Tspec *spec); \
    00179   template StringID name::GetName(Tid cls_id); \
    00180   template uint name::GetCount(); \
    00181   template uint name::GetCount(Tid cls_id); \
    00182   template const Tspec *name::Get(Tid cls_id, uint index); \
    00183   template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);