newgrf_townname.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_townname.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
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 
00016 #include "stdafx.h"
00017 #include "newgrf_townname.h"
00018 #include "core/alloc_func.hpp"
00019 #include "string_func.h"
00020 
00021 static GRFTownName *_grf_townnames = NULL;
00022 
00023 GRFTownName *GetGRFTownName(uint32 grfid)
00024 {
00025   GRFTownName *t = _grf_townnames;
00026   for (; t != NULL; t = t->next) {
00027     if (t->grfid == grfid) return t;
00028   }
00029   return NULL;
00030 }
00031 
00032 GRFTownName *AddGRFTownName(uint32 grfid)
00033 {
00034   GRFTownName *t = GetGRFTownName(grfid);
00035   if (t == NULL) {
00036     t = CallocT<GRFTownName>(1);
00037     t->grfid = grfid;
00038     t->next = _grf_townnames;
00039     _grf_townnames = t;
00040   }
00041   return t;
00042 }
00043 
00044 void DelGRFTownName(uint32 grfid)
00045 {
00046   GRFTownName *t = _grf_townnames;
00047   GRFTownName *p = NULL;
00048   for (;t != NULL; p = t, t = t->next) if (t->grfid == grfid) break;
00049   if (t != NULL) {
00050     for (int i = 0; i < 128; i++) {
00051       for (int j = 0; j < t->nbparts[i]; j++) {
00052         for (int k = 0; k < t->partlist[i][j].partcount; k++) {
00053           if (!HasBit(t->partlist[i][j].parts[k].prob, 7)) free(t->partlist[i][j].parts[k].data.text);
00054         }
00055         free(t->partlist[i][j].parts);
00056       }
00057       free(t->partlist[i]);
00058     }
00059     if (p != NULL) {
00060       p->next = t->next;
00061     } else {
00062       _grf_townnames = t->next;
00063     }
00064     free(t);
00065   }
00066 }
00067 
00068 static char *RandomPart(char *buf, GRFTownName *t, uint32 seed, byte id, const char *last)
00069 {
00070   assert(t != NULL);
00071   for (int i = 0; i < t->nbparts[id]; i++) {
00072     byte count = t->partlist[id][i].bitcount;
00073     uint16 maxprob = t->partlist[id][i].maxprob;
00074     uint32 r = (GB(seed, t->partlist[id][i].bitstart, count) * maxprob) >> count;
00075     for (int j = 0; j < t->partlist[id][i].partcount; j++) {
00076       byte prob = t->partlist[id][i].parts[j].prob;
00077       maxprob -= GB(prob, 0, 7);
00078       if (maxprob > r) continue;
00079       if (HasBit(prob, 7)) {
00080         buf = RandomPart(buf, t, seed, t->partlist[id][i].parts[j].data.id, last);
00081       } else {
00082         buf = strecat(buf, t->partlist[id][i].parts[j].data.text, last);
00083       }
00084       break;
00085     }
00086   }
00087   return buf;
00088 }
00089 
00090 char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, const char *last)
00091 {
00092   strecpy(buf, "", last);
00093   for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
00094     if (t->grfid == grfid) {
00095       assert(gen < t->nb_gen);
00096       buf = RandomPart(buf, t, seed, t->id[gen], last);
00097       break;
00098     }
00099   }
00100   return buf;
00101 }
00102 
00103 StringID *GetGRFTownNameList()
00104 {
00105   int nb_names = 0, n = 0;
00106   for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) nb_names += t->nb_gen;
00107   StringID *list = MallocT<StringID>(nb_names + 1);
00108   for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
00109     for (int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j];
00110   }
00111   list[n] = INVALID_STRING_ID;
00112   return list;
00113 }
00114 
00115 void CleanUpGRFTownNames()
00116 {
00117   while (_grf_townnames != NULL) DelGRFTownName(_grf_townnames->grfid);
00118 }
00119 
00120 uint32 GetGRFTownNameId(int gen)
00121 {
00122   for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
00123     if (gen < t->nb_gen) return t->grfid;
00124     gen -= t->nb_gen;
00125   }
00126   /* Fallback to no NewGRF */
00127   return 0;
00128 }
00129 
00130 uint16 GetGRFTownNameType(int gen)
00131 {
00132   for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
00133     if (gen < t->nb_gen) return gen;
00134     gen -= t->nb_gen;
00135   }
00136   /* Fallback to english original */
00137   return SPECSTR_TOWNNAME_ENGLISH;
00138 }

Generated on Sat Jun 5 21:52:07 2010 for OpenTTD by  doxygen 1.6.1