author | convert-repo |
Mon, 07 Apr 2008 16:21:55 +0000 | |
changeset 10076 | dfd70e42c4ae |
parent 8760 | ce0891c412ce |
permissions | -rw-r--r-- |
7452 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file newgrf_townname.cpp |
|
4 |
* Implementation of Action 0F "universal holder" structure and functions. |
|
5 |
* This file implements a linked-lists of townname generators, |
|
6 |
* holding everything that the newgrf action 0F will send over to OpenTTD. |
|
7 |
*/ |
|
8 |
||
9 |
#include "stdafx.h" |
|
10 |
#include "openttd.h" |
|
11 |
#include "newgrf_townname.h" |
|
8626
440dfcd14c4a
(svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
8424
diff
changeset
|
12 |
#include "core/alloc_func.hpp" |
8710
52015340050c
(svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents:
8626
diff
changeset
|
13 |
#include "string_func.h" |
7452 | 14 |
|
8760
ce0891c412ce
(svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents:
8710
diff
changeset
|
15 |
#include "table/strings.h" |
ce0891c412ce
(svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents:
8710
diff
changeset
|
16 |
|
7452 | 17 |
static GRFTownName *_grf_townnames = NULL; |
18 |
||
19 |
GRFTownName *GetGRFTownName(uint32 grfid) |
|
20 |
{ |
|
21 |
GRFTownName *t = _grf_townnames; |
|
22 |
for (; t != NULL; t = t->next) { |
|
23 |
if (t->grfid == grfid) return t; |
|
24 |
} |
|
25 |
return NULL; |
|
26 |
} |
|
27 |
||
28 |
GRFTownName *AddGRFTownName(uint32 grfid) |
|
29 |
{ |
|
30 |
GRFTownName *t = GetGRFTownName(grfid); |
|
31 |
if (t == NULL) { |
|
32 |
t = CallocT<GRFTownName>(1); |
|
33 |
t->grfid = grfid; |
|
34 |
t->next = _grf_townnames; |
|
35 |
_grf_townnames = t; |
|
36 |
} |
|
37 |
return t; |
|
38 |
} |
|
39 |
||
40 |
void DelGRFTownName(uint32 grfid) |
|
41 |
{ |
|
42 |
GRFTownName *t = _grf_townnames; |
|
43 |
GRFTownName *p = NULL; |
|
44 |
for (;t != NULL; p = t, t = t->next) if (t->grfid == grfid) break; |
|
45 |
if (t != NULL) { |
|
46 |
for (int i = 0; i < 128; i++) { |
|
47 |
for (int j = 0; j < t->nbparts[i]; j++) { |
|
48 |
for (int k = 0; k < t->partlist[i][j].partcount; k++) { |
|
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
7452
diff
changeset
|
49 |
if (!HasBit(t->partlist[i][j].parts[k].prob, 7)) free(t->partlist[i][j].parts[k].data.text); |
7452 | 50 |
} |
51 |
free(t->partlist[i][j].parts); |
|
52 |
} |
|
53 |
free(t->partlist[i]); |
|
54 |
} |
|
55 |
if (p != NULL) { |
|
56 |
p->next = t->next; |
|
57 |
} else { |
|
58 |
_grf_townnames = t->next; |
|
59 |
} |
|
60 |
free(t); |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
static char *RandomPart(char *buf, GRFTownName *t, uint32 seed, byte id, const char *last) |
|
65 |
{ |
|
66 |
assert(t != NULL); |
|
67 |
for (int i = 0; i < t->nbparts[id]; i++) { |
|
68 |
byte count = t->partlist[id][i].bitcount; |
|
69 |
uint16 maxprob = t->partlist[id][i].maxprob; |
|
70 |
uint32 r = (GB(seed, t->partlist[id][i].bitstart, count) * maxprob) >> count; |
|
71 |
for (int j = 0; j < t->partlist[id][i].partcount; j++) { |
|
72 |
byte prob = t->partlist[id][i].parts[j].prob; |
|
73 |
maxprob -= GB(prob, 0, 7); |
|
74 |
if (maxprob > r) continue; |
|
8424
4a488a90ccab
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents:
7452
diff
changeset
|
75 |
if (HasBit(prob, 7)) { |
7452 | 76 |
buf = RandomPart(buf, t, seed, t->partlist[id][i].parts[j].data.id, last); |
77 |
} else { |
|
78 |
buf = strecat(buf, t->partlist[id][i].parts[j].data.text, last); |
|
79 |
} |
|
80 |
break; |
|
81 |
} |
|
82 |
} |
|
83 |
return buf; |
|
84 |
} |
|
85 |
||
86 |
char *GRFTownNameGenerate(char *buf, uint32 grfid, uint16 gen, uint32 seed, const char *last) |
|
87 |
{ |
|
88 |
strecpy(buf, "", last); |
|
89 |
for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) { |
|
90 |
if (t->grfid == grfid) { |
|
91 |
assert(gen < t->nb_gen); |
|
92 |
buf = RandomPart(buf, t, seed, t->id[gen], last); |
|
93 |
break; |
|
94 |
} |
|
95 |
} |
|
96 |
return buf; |
|
97 |
} |
|
98 |
||
99 |
StringID *GetGRFTownNameList() |
|
100 |
{ |
|
101 |
int nb_names = 0, n = 0; |
|
102 |
for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) nb_names += t->nb_gen; |
|
103 |
StringID *list = MallocT<StringID>(nb_names + 1); |
|
104 |
for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) { |
|
105 |
for (int j = 0; j < t->nb_gen; j++) list[n++] = t->name[j]; |
|
106 |
} |
|
107 |
list[n] = INVALID_STRING_ID; |
|
108 |
return list; |
|
109 |
} |
|
110 |
||
111 |
void CleanUpGRFTownNames() |
|
112 |
{ |
|
113 |
while (_grf_townnames != NULL) DelGRFTownName(_grf_townnames->grfid); |
|
114 |
} |
|
115 |
||
116 |
uint32 GetGRFTownNameId(int gen) |
|
117 |
{ |
|
118 |
for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) { |
|
119 |
if (gen < t->nb_gen) return t->grfid; |
|
120 |
gen -= t->nb_gen; |
|
121 |
} |
|
122 |
/* Fallback to no NewGRF */ |
|
123 |
return 0; |
|
124 |
} |
|
125 |
||
126 |
uint16 GetGRFTownNameType(int gen) |
|
127 |
{ |
|
128 |
for (GRFTownName *t = _grf_townnames; t != NULL; t = t->next) { |
|
129 |
if (gen < t->nb_gen) return gen; |
|
130 |
gen -= t->nb_gen; |
|
131 |
} |
|
132 |
/* Fallback to english original */ |
|
133 |
return SPECSTR_TOWNNAME_ENGLISH; |
|
134 |
} |