forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_generator.cpp
88 lines (77 loc) · 2.65 KB
/
name_generator.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
#include "stdafx.h"
#include "name_generator.h"
#include "util.h"
NameGenerator NameGenerator::scrolls;
NameGenerator NameGenerator::firstNames;
NameGenerator NameGenerator::aztecNames;
NameGenerator NameGenerator::creatureNames;
NameGenerator NameGenerator::weaponNames;
NameGenerator NameGenerator::worldNames;
NameGenerator NameGenerator::townNames;
NameGenerator NameGenerator::dwarfNames;
NameGenerator NameGenerator::deityNames;
NameGenerator NameGenerator::demonNames;
NameGenerator NameGenerator::dogNames;
string getSyllable() {
string vowels = "aeyuio";
string consonants = "qwrtplkjhgfdszxcvbnm";
string ret;
if (Random.roll(3))
ret += consonants[Random.getRandom(consonants.size())];
ret += vowels[Random.getRandom(vowels.size())];
if (Random.roll(3))
ret += consonants[Random.getRandom(consonants.size())];
return ret;
}
string getWord() {
int syllables = chooseRandom({1, 2, 3, 4}, {1, 4, 3, 1});
string ret;
for (int i : Range(syllables))
ret += getSyllable();
return ret;
}
vector<string> readLines(const string& path) {
vector<string> input;
ifstream in(path);
CHECK(in.is_open()) << "Unable to open " << path;
char buf[100];
while (in.getline(buf, 100))
input.push_back(buf);
return input;
}
void NameGenerator::init(const string& firstNamesPath, const string& aztecNamesPath,
const string& creatureNamesPath, const string& weaponNamesPath, const string& worldsPath,
const string& townsPath, const string& dwarfsPath, const string& deitiesPath, const string& demonsPath,
const string& dogsPath) {
vector<string> input;
for (int i : Range(1000)) {
string ret;
int parts = chooseRandom({1, 2}, {3, 1});
for (int k : Range(parts))
ret += getWord() + " ";
trim(ret);
input.push_back(ret);
}
scrolls = NameGenerator(input);
firstNames = NameGenerator(readLines(firstNamesPath));
aztecNames = NameGenerator(readLines(aztecNamesPath));
creatureNames = NameGenerator(readLines(creatureNamesPath));
weaponNames = NameGenerator(readLines(weaponNamesPath));
worldNames = NameGenerator(readLines(worldsPath), true);
townNames = NameGenerator(readLines(townsPath));
deityNames = NameGenerator(readLines(deitiesPath));
dwarfNames = NameGenerator(readLines(dwarfsPath));
demonNames = NameGenerator(readLines(demonsPath));
dogNames = NameGenerator(readLines(dogsPath));
}
string NameGenerator::getNext() {
CHECK(!names.empty()) << "Out of names!";
string ret = names.front();
if (!oneName)
names.pop();
return ret;
}
NameGenerator::NameGenerator(vector<string> list, bool oneN) : oneName(oneN) {
for (string name : randomPermutation(list))
names.push(name);
}