OpenTTD: src/newgrf_townname.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_townname.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_townname.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
17 #include "stdafx.h"
18 #include "newgrf_townname.h"
19 #include "core/alloc_func.hpp"
20 #include "string_func.h"
21
22 #include "safeguards.h"
23
24 static GRFTownName *_grf_townnames = NULL;
25
26 GRFTownName *GetGRFTownName(uint32 grfid)
27 {
28 GRFTownName *t = _grf_townnames;
29 for (; t != NULL; t = t->next) {
30 if (t->grfid == grfid) return t;
31 }
32 return NULL;
33 }
34
35 GRFTownName *AddGRFTownName(uint32 grfid)
36 {
37 GRFTownName *t = GetGRFTownName(grfid);
38 if (t == NULL) {
39 t = CallocT<GRFTownName>(1);
40 t->grfid = grfid;
41 t->next = _grf_townnames;
42 _grf_townnames = t;
43 }
44 return t;
45 }
46
47 void DelGRFTownName(uint32 grfid)
48 {
49 GRFTownName *t = _grf_townnames;
50 GRFTownName *p = NULL;
51 for (;t != NULL; p = t, t = t->next) if (t->grfid == grfid) break;
52 if (t != NULL) {
53 for (int i = 0; i < 128; i++) {
54 for (int j = 0; j < t->nbparts[i]; j++) {
55 for (int k = 0; k < t->partlist[i][j].partcount; k++) {
56 if (!HasBit(t->partlist[i][j].parts[k].prob, 7)) free(t->partlist[i][j].parts[k].data.text);
57 }
58 free(t->partlist[i][j].parts);
59 }
60 free(t->partlist[i]);
61 }
62 if (p != NULL) {
63 p->next = t->next;
64 } else {
65 _grf_townnames = t->next;
66 }
67 free(t);
68 }
69 }
70
71 static char *RandomPart(char *buf, GRFTownName *t, uint32 seed, byte id, const char *last)
72 {
73 assert(t != NULL);
74 for (int i = 0; i < t->nbparts[id]; i++) {
75 byte count = t->partlist[id][i].bitcount;
76 uint16 maxprob = t->partlist[id][i].maxprob;
77 uint32 r = (GB(seed, t->partlist[id][i].bitstart, count) * maxprob) >> count;
78 for (int j = 0; j < t->partlist[id][i].partcount; j++) {
79 byte prob = t->partlist[id][i].parts[j].prob;
80 maxprob -= GB(prob, 0, 7);
81 if (maxprob > r) continue;
82 if (HasBit(prob, 7)) {
83 buf = RandomPart(buf, t, seed, t->partlist[id][i].parts[j].data.id, last);
84 } else {
85 buf = strecat(buf, t->partlist[id][i].parts[j].data.text, last);
86 }
87 break;
88 }
89 }
90 return buf;
91 }
92
93 char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, const char *last)
94 {
95 strecpy(buf, "", last);
96 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
97 if (t->grfid == grfid) {
98 assert(gen < t->nb_gen);
99 buf = RandomPart(buf, t, seed, t->id[gen], last);
100 break;
101 }
102 }
103 return buf;
104 }
105
106 StringID *GetGRFTownNameList()
107 {
108 int nb_names = 0, n = 0;
109 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) nb_names += t->nb_gen;
110 StringID *list = MallocT<StringID>(nb_names + 1);
111 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
112 for (int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j];
113 }
114 list[n] = INVALID_STRING_ID;
115 return list;
116 }
117
118 void CleanUpGRFTownNames()
119 {
120 while (_grf_townnames != NULL) DelGRFTownName(_grf_townnames->grfid);
121 }
122
123 uint32 GetGRFTownNameId(int gen)
124 {
125 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
126 if (gen < t->nb_gen) return t->grfid;
127 gen -= t->nb_gen;
128 }
129 /* Fallback to no NewGRF */
130 return 0;
131 }
132
133 uint16 GetGRFTownNameType(int gen)
134 {
135 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
136 if (gen < t->nb_gen) return gen;
137 gen -= t->nb_gen;
138 }
139 /* Fallback to english original */
140 return SPECSTR_TOWNNAME_ENGLISH;
141 }