# newgrf_townname.cpp

Go to the documentation of this file.

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