Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / newgrf_townname.cpp
blob2d03d3babdf2cd0e17f61a064cd54138d7f48611
1 /* $Id$ */
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 */
10 /**
11 * @file newgrf_townname.cpp
12 * Implementation of Action 0F "universal holder" structure and functions.
13 * This file implements a linked-lists of townname generators,
14 * holding everything that the newgrf action 0F will send over to OpenTTD.
17 #include "stdafx.h"
18 #include "newgrf_townname.h"
19 #include "townnamegen.h"
20 #include "townname_type.h"
21 #include "core/alloc_func.hpp"
22 #include "string.h"
24 static GRFTownName *_grf_townnames = NULL;
26 GRFTownName *GetGRFTownName(uint32 grfid)
28 GRFTownName *t = _grf_townnames;
29 for (; t != NULL; t = t->next) {
30 if (t->grfid == grfid) return t;
32 return NULL;
35 GRFTownName *AddGRFTownName(uint32 grfid)
37 GRFTownName *t = GetGRFTownName(grfid);
38 if (t == NULL) {
39 t = xcalloct<GRFTownName>();
40 t->grfid = grfid;
41 t->next = _grf_townnames;
42 _grf_townnames = t;
44 return t;
47 void DelGRFTownName(uint32 grfid)
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);
58 free(t->partlist[i][j].parts);
60 free(t->partlist[i]);
62 if (p != NULL) {
63 p->next = t->next;
64 } else {
65 _grf_townnames = t->next;
67 free(t);
71 static void RandomPart (stringb *buf, GRFTownName *t, uint32 seed, byte id)
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 RandomPart (buf, t, seed, t->partlist[id][i].parts[j].data.id);
84 } else {
85 buf->append (t->partlist[id][i].parts[j].data.text);
87 break;
92 void GRFTownNameGenerate (stringb *buf, uint32 grfid, uint16 gen, uint32 seed)
94 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
95 if (t->grfid == grfid) {
96 assert(gen < t->nb_gen);
97 RandomPart (buf, t, seed, t->id[gen]);
98 break;
103 StringID *GetGRFTownNameList()
105 int nb_names = 0, n = 0;
106 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) nb_names += t->nb_gen;
107 StringID *list = xmalloct<StringID>(nb_names + 1);
108 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
109 for (int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j];
111 list[n] = INVALID_STRING_ID;
112 return list;
115 void CleanUpGRFTownNames()
117 while (_grf_townnames != NULL) DelGRFTownName(_grf_townnames->grfid);
121 * Initializes this struct from language ID
122 * @param town_name town name 'language' ID
124 TownNameParams::TownNameParams (byte town_name)
126 assert_compile (SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1 == N_ORIG_TOWN_NAME_GEN);
128 if (town_name >= N_ORIG_TOWN_NAME_GEN) {
129 int gen = town_name - N_ORIG_TOWN_NAME_GEN;
130 for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) {
131 if (gen < t->nb_gen) {
132 this->grfid = t->grfid;
133 this->type = gen;
134 return;
136 gen -= t->nb_gen;
138 /* Fallback to english original */
139 town_name = SPECSTR_TOWNNAME_ENGLISH - SPECSTR_TOWNNAME_START;
142 this->grfid = 0;
143 this->type = SPECSTR_TOWNNAME_START + town_name;