newgrf_townname.cpp

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

Generated on Wed Apr 13 00:47:51 2011 for OpenTTD by  doxygen 1.6.1