# newgrf_townname.cpp

Go to the documentation of this file.

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