/
PhraseBook.cpp
262 lines (220 loc) · 8.15 KB
/
PhraseBook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <ctime>
#include <boost/format.hpp>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "App.h"
#include "PhraseBook.h"
#include "Exceptions.h"
#include "Random.h"
#include "FontManager.h"
namespace typing
{
const std::string PhraseBook::PHRASE_FILE("phrases/phrases");
void PhraseBook::Init(Font phraseFont)
{
FILE * phraseFile = fopen(PHRASE_FILE.c_str(), "r");
if (phraseFile == NULL) {
throw FileNotFoundException(PHRASE_FILE);
}
char buffer[MAX_PHRASE_LENGTH];
while (fgets(buffer, MAX_PHRASE_LENGTH, phraseFile) != NULL) {
for (char *c = buffer;; c++) {
if (*c == '\r' || *c == '\n') {
*c = '\0';
AddPhrase(buffer);
} else if (!phraseFont.HasChar(*c)) {
// Don't add the phrase if the font doesn't have
// all the letters required.
break;
}
}
}
fclose(phraseFile);
}
const std::string& PhraseBook::GetPhrase(PhraseLength len)
{
// We're returning a reference to avoid copying strings,
// so we need this to return a reference to in case of error
static const std::string defPhrase("default");
if (UsingShortPhrases() && len > PL_SINGLE) {
len = static_cast<PhraseLength>(static_cast<int>(len) - 1);
}
// First pick a start character from the available ones.
const char startChar = PickAvailChar();
if (startChar == '\0') {
return defPhrase;
}
PhraseVectorPtr phraseVec = GetValidPhraseVector(startChar, len);
if (!phraseVec || phraseVec->empty()) {
return defPhrase;
}
// Mark the start char unavailable so we don't end up with two phrases
// in use which both have the same start char.
MakeCharUnavail(startChar);
// Select a random phrase from the vector of phrases.
unsigned int i = RAND.Range(0, static_cast<int>(phraseVec->size() - 1));
return *((*phraseVec)[i]);
}
const std::string PhraseBook::GetComboPhrase(unsigned int words,
PhraseLength length)
{
std::string phrase;
if (UsingShortPhrases() && length > PL_SINGLE) {
length = static_cast<PhraseLength>(static_cast<int>(length) - 1);
}
for (unsigned int i = 0; i < words; i++) {
const char c = (i == 0 ? PickAvailChar() : PickRandomChar());
PhraseVectorPtr phraseVec;
if (c != '\0' &&
(phraseVec = GetValidPhraseVector(c, length)) &&
!phraseVec->empty()) {
if (!phrase.empty()) {
phrase += " ";
}
phrase += *((*phraseVec)[
RAND.Range(0, static_cast<int>(phraseVec->size() - 1))]);
} else {
return "default";
}
}
MakeCharUnavail(phrase[0]);
return (phrase);
}
void PhraseBook::MakeCharAvail(char c)
{
// We should never try to make a char available that we don't have
// any phrases for, as a phrasebook user should only attempt to
// make chars available for phrases it has been given from the
// phrasebook.
assert(m_phrases.find(c) != m_phrases.end());
m_availChars.insert(c);
}
void PhraseBook::MakeAllCharsAvail()
{
for (PhraseMap::iterator iter = m_phrases.begin();
iter != m_phrases.end();
++iter) {
MakeCharAvail(iter->first);
}
}
void PhraseBook::MakeCharUnavail(char c)
{
m_availChars.erase(c);
}
char PhraseBook::PickAvailChar()
{
if (m_availChars.empty()) {
return '\0';
} else {
auto iter = m_availChars.begin();
std::advance(iter,
RAND.Range(
0, static_cast<int>(m_availChars.size() - 1)));
return *iter;
}
}
char PhraseBook::PickRandomChar()
{
if (m_allChars.empty()) {
return '\0';
} else {
auto iter = m_allChars.begin();
std::advance(iter,
RAND.Range(
0, static_cast<int>(m_allChars.size() - 1)));
return *iter;
}
}
void PhraseBook::AddPhrase(const std::string& phrase)
{
const unsigned int len = static_cast<unsigned int>(phrase.length());
if (len == 0)
{
return;
}
const char startChar = phrase[0];
// Create the arrays for this character if we don't already have them
if (m_phrases.find(startChar) == m_phrases.end())
{
m_phrases[startChar] = PhraseArrayPtr(new PhraseArray);
(*m_phrases[startChar])[PL_SINGLE] =
PhraseVectorPtr(new PhraseVector());
(*m_phrases[startChar])[PL_SHORT] =
PhraseVectorPtr(new PhraseVector());
(*m_phrases[startChar])[PL_MEDIUM] =
PhraseVectorPtr(new PhraseVector());
(*m_phrases[startChar])[PL_LONG] =
PhraseVectorPtr(new PhraseVector());
}
PhraseVectorPtr vec = GetPhraseVector(startChar, LengthToCategory(len));
if (vec)
{
PhrasePtr p(new std::string(phrase));
vec->push_back(p);
MakeCharAvail(startChar);
// Add the char to the list of all characters
m_allChars.insert(startChar);
}
}
PhraseBook::PhraseVectorPtr PhraseBook::GetPhraseVector(
char startChar,
PhraseLength cat)
{
PhraseVectorPtr vec;
// We should never get a phrase vector for a start char that
// we don't have an array for.
assert(m_phrases.find(startChar) != m_phrases.end());
assert(cat >= PL_SINGLE && cat < PL_COUNT);
vec = (*m_phrases[startChar])[cat];
return vec;
}
PhraseBook::PhraseVectorPtr PhraseBook::GetValidPhraseVector(
char startChar,
PhraseLength cat)
{
PhraseVectorPtr vec = GetPhraseVector(startChar, cat);
while (!vec || vec->empty()) {
APP.Log(App::LOG_DEBUG,
boost::str(boost::format(
"Couldn't find phrases for char %c category %u") %
startChar % cat));
if (cat == PL_SINGLE) {
// We've failed to find a populated vector.
break;
} else {
cat = static_cast<PhraseLength>(static_cast<int>(cat) - 1);
}
vec = (*m_phrases[startChar])[cat];
}
return vec;
}
PhraseBook::PhraseLength PhraseBook::LengthToCategory(unsigned int len)
{
if (len <= SINGLE_PHRASE_LENGTH) {
return PL_SINGLE;
} else if (len <= SHORT_PHRASE_LENGTH) {
return PL_SHORT;
} else if (len <= MEDIUM_PHRASE_LENGTH) {
return PL_MEDIUM;
} else {
return PL_LONG;
}
}
#ifdef _DEBUG
void PhraseBook::DrawChars(const std::string &font, float y, float height)
{
float x = 0.0f;
for (PhraseMap::iterator iter = m_phrases.begin(); iter != m_phrases.end(); ++iter)
{
PhraseVectorPtr vec = (*iter->second)[PL_SINGLE];
std::string str = *((*vec)[0]);
char c[2];
c[0] = str[0];
c[1] = '\0';
bool avail = (std::find(m_availChars.begin(), m_availChars.end(), c[0]) != m_availChars.end());
FONTS.Print(font, x, y, height, avail ? ColourRGBA::White() : ColourRGBA::Red(), Font::ALIGN_LEFT, c);
x += FONTS.GetLineWidth(font, 16.0f, c);
}
}
#endif
}