# 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
    00016 #define DEFINE_NEWGRF_CLASS_METHOD(type) \
    00017   template <typename Tspec, typename Tid, Tid Tmax> \
    00018   type NewGRFClass<Tspec, Tid, Tmax>
    00019
    00021 template <typename Tspec, typename Tid, Tid Tmax>
    00022 NewGRFClass<Tspec, Tid, Tmax> NewGRFClass<Tspec, Tid, Tmax>::classes[Tmax];
    00023
    00025 DEFINE_NEWGRF_CLASS_METHOD(void)::Reset()
    00026 {
    00027   for (Tid i = (Tid)0; i < Tmax; i++) {
    00028     classes[i].global_id = 0;
    00029     classes[i].name      = STR_EMPTY;
    00030     classes[i].count     = 0;
    00031
    00032     free(classes[i].spec);
    00033     classes[i].spec = NULL;
    00034   }
    00035
    00036   InsertDefaults();
    00037 }
    00038
    00046 DEFINE_NEWGRF_CLASS_METHOD(Tid)::Allocate(uint32 global_id)
    00047 {
    00048   for (Tid i = (Tid)0; i < Tmax; i++) {
    00049     if (classes[i].global_id == global_id) {
    00050       /* ClassID is already allocated, so reuse it. */
    00051       return i;
    00052     } else if (classes[i].global_id == 0) {
    00053       /* This class is empty, so allocate it to the global id. */
    00054       classes[i].global_id = global_id;
    00055       return i;
    00056     }
    00057   }
    00058
    00059   grfmsg(2, "ClassAllocate: already allocated %d classes, using default", Tmax);
    00060   return (Tid)0;
    00061 }
    00062
    00069 DEFINE_NEWGRF_CLASS_METHOD(void)::SetName(Tid cls_id, StringID name)
    00070 {
    00071   assert(cls_id < Tmax);
    00072   classes[cls_id].name = name;
    00073 }
    00074
    00080 DEFINE_NEWGRF_CLASS_METHOD(void)::Assign(Tspec *spec)
    00081 {
    00082   assert(spec->cls_id < Tmax);
    00083   NewGRFClass<Tspec, Tid, Tmax> *cls = &classes[spec->cls_id];
    00084
    00085   uint i = cls->count++;
    00086   cls->spec = ReallocT(cls->spec, cls->count);
    00087
    00088   cls->spec[i] = spec;
    00089 }
    00090
    00097 DEFINE_NEWGRF_CLASS_METHOD(StringID)::GetName(Tid cls_id)
    00098 {
    00099   assert(cls_id < Tmax);
    00100   return classes[cls_id].name;
    00101 }
    00102
    00107 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetCount()
    00108 {
    00109   uint i;
    00110   for (i = 0; i < Tmax && classes[i].global_id != 0; i++) {}
    00111   return i;
    00112 }
    00113
    00120 DEFINE_NEWGRF_CLASS_METHOD(uint)::GetCount(Tid cls_id)
    00121 {
    00122   assert(cls_id < Tmax);
    00123   return classes[cls_id].count;
    00124 }
    00125
    00133 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::Get(Tid cls_id, uint index)
    00134 {
    00135   assert(cls_id < Tmax);
    00136   if (index < classes[cls_id].count) return classes[cls_id].spec[index];
    00137
    00138   /* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
    00139   return NULL;
    00140 }
    00141
    00149 DEFINE_NEWGRF_CLASS_METHOD(const Tspec *)::GetByGrf(uint32 grfid, byte local_id, int *index)
    00150 {
    00151   uint j;
    00152
    00153   for (Tid i = (Tid)0; i < Tmax; i++) {
    00154     for (j = 0; j < classes[i].count; j++) {
    00155       const Tspec *spec = classes[i].spec[j];
    00156       if (spec == NULL) continue;
    00157       if (spec->grf_prop.grffile->grfid == grfid && spec->grf_prop.local_id == local_id) {
    00158         if (index != NULL) *index = j;
    00159         return spec;
    00160       }
    00161     }
    00162   }
    00163
    00164   return NULL;
    00165 }
    00166
    00167 #undef DEFINE_NEWGRF_CLASS_METHOD
    00168
    00170 #define INSTANTIATE_NEWGRF_CLASS_METHODS(name, Tspec, Tid, Tmax) \
    00171   template void name::Reset(); \
    00172   template Tid name::Allocate(uint32 global_id); \
    00173   template void name::SetName(Tid cls_id, StringID name); \
    00174   template void name::Assign(Tspec *spec); \
    00175   template StringID name::GetName(Tid cls_id); \
    00176   template uint name::GetCount(); \
    00177   template uint name::GetCount(Tid cls_id); \
    00178   template const Tspec *name::Get(Tid cls_id, uint index); \
    00179   template const Tspec *name::GetByGrf(uint32 grfid, byte localidx, int *index);