forked from miki151/keeperrl
/
options.cpp
294 lines (263 loc) · 9.09 KB
/
options.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright (C) 2013-2014 Michal Brzozowski (rusolis@poczta.fm)
This file is part of KeeperRL.
KeeperRL 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; either version 2
of the License, or (at your option) any later version.
KeeperRL 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.gnu.org/licenses/ . */
#include "stdafx.h"
#include "options.h"
#include "main_loop.h"
#include "view.h"
const EnumMap<OptionId, Options::Value> defaults {
{OptionId::HINTS, 1},
{OptionId::ASCII, 0},
{OptionId::MUSIC, 1},
{OptionId::KEEP_SAVEFILES, 0},
{OptionId::SHOW_MAP, 0},
{OptionId::FULLSCREEN, 0},
{OptionId::FULLSCREEN_RESOLUTION, 0},
{OptionId::ONLINE, 1},
{OptionId::AUTOSAVE, 1},
{OptionId::FAST_IMMIGRATION, 0},
{OptionId::STARTING_RESOURCE, 0},
{OptionId::START_WITH_NIGHT, 0},
{OptionId::KEEPER_NAME, string("")},
{OptionId::ADVENTURER_NAME, string("")},
};
const map<OptionId, string> names {
{OptionId::HINTS, "In-game hints"},
{OptionId::ASCII, "Unicode graphics"},
{OptionId::MUSIC, "Music"},
{OptionId::KEEP_SAVEFILES, "Keep save files"},
{OptionId::SHOW_MAP, "Show map"},
{OptionId::FULLSCREEN, "Fullscreen"},
{OptionId::FULLSCREEN_RESOLUTION, "Fullscreen resolution"},
{OptionId::ONLINE, "Online exchange of dungeons and highscores"},
{OptionId::AUTOSAVE, "Autosave"},
{OptionId::FAST_IMMIGRATION, "Fast immigration"},
{OptionId::STARTING_RESOURCE, "Resource bonus"},
{OptionId::START_WITH_NIGHT, "Start with night"},
{OptionId::KEEPER_NAME, "Keeper's name"},
{OptionId::ADVENTURER_NAME, "Adventurer's name"},
};
const map<OptionId, string> hints {
{OptionId::HINTS, "Display some extra helpful information during the game."},
{OptionId::ASCII, "Switch to old school roguelike graphics."},
{OptionId::MUSIC, ""},
{OptionId::KEEP_SAVEFILES, "Don't remove the save file when a game is loaded."},
{OptionId::SHOW_MAP, ""},
{OptionId::FULLSCREEN, "Switch between fullscreen and windowed mode."},
{OptionId::FULLSCREEN_RESOLUTION, "Choose resolution for fullscreen mode."},
{OptionId::ONLINE, "Upload your highscores and retired dungeons to keeperrl.com."},
{OptionId::AUTOSAVE, "Autosave the game every " + toString(MainLoop::getAutosaveFreq()) + " turns. "
"The save file will be used to recover in case of a crash."},
{OptionId::FAST_IMMIGRATION, ""},
{OptionId::STARTING_RESOURCE, ""},
{OptionId::START_WITH_NIGHT, ""},
{OptionId::KEEPER_NAME, ""},
{OptionId::ADVENTURER_NAME, ""},
};
const map<OptionSet, vector<OptionId>> optionSets {
{OptionSet::GENERAL, {
OptionId::HINTS,
OptionId::ASCII,
OptionId::MUSIC,
OptionId::FULLSCREEN,
OptionId::FULLSCREEN_RESOLUTION,
OptionId::ONLINE,
OptionId::AUTOSAVE,
#ifndef RELEASE
OptionId::KEEP_SAVEFILES,
OptionId::SHOW_MAP,
#endif
}},
{OptionSet::KEEPER, {
#ifndef RELEASE
OptionId::START_WITH_NIGHT,
OptionId::SHOW_MAP,
OptionId::STARTING_RESOURCE,
OptionId::FAST_IMMIGRATION,
#endif
OptionId::KEEPER_NAME,
}},
{OptionSet::ADVENTURER, {
OptionId::ADVENTURER_NAME,
}},
};
map<OptionId, Options::Trigger> triggers;
void Options::addTrigger(OptionId id, Trigger trigger) {
triggers[id] = trigger;
}
static EnumMap<OptionId, optional<Options::Value>> parseOverrides(const string& s) {
EnumMap<OptionId, optional<Options::Value>> ret;
for (string op : split(s, {','})) {
auto parts = split(op, {'='});
CHECK(parts.size() == 2);
OptionId id = EnumInfo<OptionId>::fromString(parts[0]);
if (parts[1] == "n")
ret[id] = false;
else if (parts[1] == "y")
ret[id] = true;
else
FAIL << "Bad override " << parts;
}
return ret;
}
Options::Options(const string& path, const string& _overrides)
: filename(path), overrides(parseOverrides(_overrides)) {
}
Options::Value Options::getValue(OptionId id) {
if (overrides[id])
return *overrides[id];
return readValues()[id];
}
bool Options::getBoolValue(OptionId id) {
return boost::get<int>(getValue(id));
}
string Options::getStringValue(OptionId id) {
return getValueString(id, getValue(id));
}
int Options::getChoiceValue(OptionId id) {
return boost::get<int>(getValue(id));
}
void Options::setValue(OptionId id, Value value) {
auto values = readValues();
values[id] = value;
if (triggers.count(id))
triggers.at(id)(boost::get<int>(value));
writeValues(values);
}
void Options::setDefaultString(OptionId id, const string& s) {
defaultStrings[id] = s;
}
static string getOnOff(const Options::Value& value) {
return boost::get<int>(value) ? "on" : "off";
}
static string getYesNo(const Options::Value& value) {
return boost::get<int>(value) ? "yes" : "no";
}
string Options::getValueString(OptionId id, Options::Value value) {
switch (id) {
case OptionId::HINTS:
case OptionId::ASCII:
case OptionId::FULLSCREEN:
case OptionId::AUTOSAVE:
case OptionId::MUSIC: return getOnOff(value);
case OptionId::KEEP_SAVEFILES:
case OptionId::SHOW_MAP:
case OptionId::FAST_IMMIGRATION:
case OptionId::STARTING_RESOURCE:
case OptionId::ONLINE:
case OptionId::START_WITH_NIGHT: return getYesNo(value);
case OptionId::ADVENTURER_NAME:
case OptionId::KEEPER_NAME: {
string val = boost::get<string>(value);
if (val.empty())
return defaultStrings[id];
else
return val;
}
case OptionId::FULLSCREEN_RESOLUTION: return choices[id][boost::get<int>(value)];
}
}
optional<Options::Value> Options::readValue(OptionId id, const string& input) {
switch (id) {
case OptionId::ADVENTURER_NAME:
case OptionId::KEEPER_NAME: return Options::Value(input);
default:
if (auto ret = fromStringSafe<int>(input))
return Options::Value(*ret);
else
return none;
}
}
static MenuType getMenuType(OptionSet set) {
switch (set) {
default: return MenuType::NORMAL;
}
}
void Options::changeValue(OptionId id, const Options::Value& value, View* view) {
switch (id) {
case OptionId::KEEPER_NAME:
case OptionId::ADVENTURER_NAME:
if (auto val = view->getText("Enter " + names.at(id), boost::get<string>(value), 23,
"Leave blank to use a random name."))
setValue(id, *val);
break;
case OptionId::FULLSCREEN_RESOLUTION:
if (auto index = view->chooseFromList("Choose resolution.", ListElem::convert(choices[id])))
setValue(id, *index);
break;
default:
setValue(id, !boost::get<int>(value));
}
}
void Options::setChoices(OptionId id, const vector<string>& v) {
choices[id] = v;
}
bool Options::handleOrExit(View* view, OptionSet set, int lastIndex) {
if (!optionSets.count(set))
return true;
vector<ListElem> options;
options.emplace_back("Change settings:", ListElem::TITLE);
for (OptionId option : optionSets.at(set))
options.push_back(ListElem(names.at(option),
getValueString(option, getValue(option))).setTip(hints.at(option)));
options.emplace_back("Done");
if (lastIndex == -1)
lastIndex = optionSets.at(set).size();
auto index = view->chooseFromList("", options, lastIndex, getMenuType(set));
if (!index)
return false;
else if (index && (*index) == optionSets.at(set).size())
return true;
OptionId option = optionSets.at(set)[*index];
changeValue(option, getValue(option), view);
return handleOrExit(view, set, *index);
}
void Options::handle(View* view, OptionSet set, int lastIndex) {
vector<ListElem> options;
options.emplace_back("Change settings:", ListElem::TITLE);
for (OptionId option : optionSets.at(set))
options.push_back(ListElem(names.at(option),
getValueString(option, getValue(option))).setTip(hints.at(option)));
options.emplace_back("Done");
auto index = view->chooseFromList("", options, lastIndex, getMenuType(set));
if (!index || (*index) == optionSets.at(set).size())
return;
OptionId option = optionSets.at(set)[*index];
changeValue(option, getValue(option), view);
handle(view, set, *index);
}
EnumMap<OptionId, Options::Value> Options::readValues() {
EnumMap<OptionId, Value> ret = defaults;
ifstream in(filename);
while (1) {
char buf[100];
in.getline(buf, 100);
if (!in)
break;
vector<string> p = split(string(buf), {','});
if (p.empty())
continue;
if (p.size() == 1)
p.push_back("");
OptionId optionId;
if (auto id = EnumInfo<OptionId>::fromStringSafe(p[0]))
optionId = *id;
else
continue;
if (auto val = readValue(optionId, p[1]))
ret[optionId] = *val;
}
return ret;
}
void Options::writeValues(const EnumMap<OptionId, Value>& values) {
ofstream out(filename);
for (OptionId id : ENUM_ALL(OptionId))
out << EnumInfo<OptionId>::getString(id) << "," << values[id] << std::endl;
}