annotate src/newgrf_townname.cpp @ 8214:6385dffc0b37 draft

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