forked from miki151/keeperrl
/
options.cpp
382 lines (345 loc) · 11.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* 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::SOUND, 1},
{OptionId::KEEP_SAVEFILES, 0},
{OptionId::SHOW_MAP, 0},
{OptionId::FULLSCREEN, 0},
{OptionId::FULLSCREEN_RESOLUTION, 0},
{OptionId::ZOOM_UI, 0},
{OptionId::ONLINE, 1},
{OptionId::AUTOSAVE, 1},
{OptionId::WASD_SCROLLING, 0},
{OptionId::FAST_IMMIGRATION, 0},
{OptionId::STARTING_RESOURCE, 0},
{OptionId::START_WITH_NIGHT, 0},
{OptionId::KEEPER_NAME, string("")},
{OptionId::KEEPER_SEED, string("")},
{OptionId::ADVENTURER_NAME, string("")},
{OptionId::MAIN_VILLAINS, 4},
{OptionId::RETIRED_VILLAINS, 1},
{OptionId::LESSER_VILLAINS, 3},
{OptionId::ALLIES, 2},
{OptionId::INFLUENCE_SIZE, 3},
};
const map<OptionId, string> names {
{OptionId::HINTS, "In-game hints"},
{OptionId::ASCII, "Unicode graphics"},
{OptionId::MUSIC, "Music"},
{OptionId::SOUND, "Sound effects"},
{OptionId::KEEP_SAVEFILES, "Keep save files"},
{OptionId::SHOW_MAP, "Show map"},
{OptionId::FULLSCREEN, "Fullscreen"},
{OptionId::FULLSCREEN_RESOLUTION, "Fullscreen resolution"},
{OptionId::ZOOM_UI, "Zoom in UI"},
{OptionId::ONLINE, "Online exchange of dungeons and highscores"},
{OptionId::AUTOSAVE, "Autosave"},
{OptionId::WASD_SCROLLING, "WASD scrolling"},
{OptionId::FAST_IMMIGRATION, "Fast immigration"},
{OptionId::STARTING_RESOURCE, "Resource bonus"},
{OptionId::START_WITH_NIGHT, "Start with night"},
{OptionId::KEEPER_NAME, "Keeper's name"},
{OptionId::KEEPER_SEED, "Level generation seed"},
{OptionId::ADVENTURER_NAME, "Adventurer's name"},
{OptionId::MAIN_VILLAINS, "Main villains"},
{OptionId::RETIRED_VILLAINS, "Retired villains"},
{OptionId::LESSER_VILLAINS, "Lesser villains"},
{OptionId::ALLIES, "Allies"},
{OptionId::INFLUENCE_SIZE, "Min. tribes in influence zone"},
};
const map<OptionId, string> hints {
{OptionId::HINTS, "Display some extra helpful information during the game."},
{OptionId::ASCII, "Switch to old school roguelike graphics."},
{OptionId::KEEP_SAVEFILES, "Don't remove the save file when a game is loaded."},
{OptionId::FULLSCREEN, "Switch between fullscreen and windowed mode."},
{OptionId::FULLSCREEN_RESOLUTION, "Choose resolution for fullscreen mode."},
{OptionId::ZOOM_UI, "All UI and graphics are zoomed in 2x. "
"Use you have a large resolution screen and things appear too small."},
{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::WASD_SCROLLING, "Scroll the map using W-A-S-D keys. In this mode building shortcuts are accessed "
"using alt + letter."},
};
const map<OptionSet, vector<OptionId>> optionSets {
{OptionSet::GENERAL, {
OptionId::HINTS,
OptionId::ASCII,
OptionId::MUSIC,
OptionId::SOUND,
OptionId::FULLSCREEN,
OptionId::FULLSCREEN_RESOLUTION,
OptionId::ZOOM_UI,
OptionId::ONLINE,
OptionId::AUTOSAVE,
OptionId::WASD_SCROLLING,
#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,
OptionId::KEEPER_SEED,
}},
{OptionSet::ADVENTURER, {
OptionId::ADVENTURER_NAME,
}},
{OptionSet::CAMPAIGN, {
OptionId::KEEPER_NAME,
OptionId::MAIN_VILLAINS,
//OptionId::RETIRED_VILLAINS,
OptionId::LESSER_VILLAINS,
OptionId::ALLIES,
//OptionId::INFLUENCE_SIZE
}},
};
map<OptionId, Options::Trigger> triggers;
void Options::addTrigger(OptionId id, Trigger trigger) {
triggers[id] = trigger;
}
const string& Options::getName(OptionId id) {
return names.at(id);
}
Options::Type Options::getType(OptionId id) {
switch (id) {
case OptionId::ADVENTURER_NAME:
case OptionId::KEEPER_SEED:
case OptionId::KEEPER_NAME: return Options::STRING;
default: return Options::INT;
}
}
vector<OptionId> Options::getOptions(OptionSet set) {
return optionSets.at(set);
}
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)) {
readValues();
}
Options::Value Options::getValue(OptionId id) {
if (overrides[id])
return *overrides[id];
readValues();
return (*values)[id];
}
bool Options::getBoolValue(OptionId id) {
return boost::get<int>(getValue(id));
}
string Options::getStringValue(OptionId id) {
return getValueString(id);
}
int Options::getChoiceValue(OptionId id) {
return boost::get<int>(getValue(id));
}
int Options::getIntValue(OptionId id) {
int v = boost::get<int>(getValue(id));
if (limits[id]) {
if (v > limits[id]->second)
return limits[id]->second;
if (v < limits[id]->first)
return limits[id]->first;
}
return v;
}
void Options::setLimits(OptionId id, int minV, int maxV) {
limits[id] = make_pair(minV, maxV);
}
optional<pair<int, int>> Options::getLimits(OptionId id) {
return limits[id];
}
void Options::setValue(OptionId id, Value value) {
readValues();
(*values)[id] = value;
if (triggers.count(id))
triggers.at(id)(boost::get<int>(value));
writeValues();
}
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) {
Value value = getValue(id);
switch (id) {
case OptionId::HINTS:
case OptionId::ASCII:
case OptionId::FULLSCREEN:
case OptionId::AUTOSAVE:
case OptionId::WASD_SCROLLING:
case OptionId::SOUND:
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::ZOOM_UI:
case OptionId::START_WITH_NIGHT: return getYesNo(value);
case OptionId::ADVENTURER_NAME:
case OptionId::KEEPER_SEED:
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)];
case OptionId::MAIN_VILLAINS:
case OptionId::LESSER_VILLAINS:
case OptionId::RETIRED_VILLAINS:
case OptionId::INFLUENCE_SIZE:
case OptionId::ALLIES: return toString(getIntValue(id));
}
}
optional<Options::Value> Options::readValue(OptionId id, const string& input) {
switch (id) {
case OptionId::ADVENTURER_NAME:
case OptionId::KEEPER_SEED:
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::KEEPER_SEED:
if (auto val = view->getText("Enter " + names.at(id), boost::get<string>(value), 23,
"Leave blank to use a random seed."))
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)));
if (hints.count(option))
options.back().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)));
if (hints.count(option))
options.back().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);
}
void Options::readValues() {
if (!values) {
values = 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]))
(*values)[optionId] = *val;
}
}
}
void Options::writeValues() {
ofstream out(filename);
for (OptionId id : ENUM_ALL(OptionId))
out << EnumInfo<OptionId>::getString(id) << "," << (*values)[id] << std::endl;
}