nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
quest_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cfloat>
7#include <cstdint>
8#include <map>
9#include <optional>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include "absl/strings/str_cat.h"
15#include "absl/strings/substitute.h"
16#include "client/icon.h"
17#include "client/names.h"
18#include "client/theme.h"
19#include "client/translations.h"
20#include "extras/IconsFontAwesome6.h"
21
22namespace nanommo {
23
24namespace {
25
26constexpr ImVec4 kTitle{1.0f, 0.82f, 0.3f, 1.0f};
27
28// "3 Skeletons", by the monster's kind.
29std::string Monsters(const Content& content, const quests::Kill& kill) {
30 const auto kind = content.monsters.kinds.find(kill.monster());
31 if (kind == content.monsters.kinds.end()) {
32 return absl::Substitute(TrN("$0 monster", "$0 monsters", kill.count()),
33 kill.count());
34 }
35 return NameOf(kind->second, kill.count());
36}
37
38// The experience a quest gives, a plural, as the servers' is, in a quest's
39// prizes.
40std::string Experience(const quests::Quest& quest) {
41 return absl::Substitute(
42 TrN("$0 experience", "$0 experience", quest.experience()),
43 quest.experience());
44}
45
46// What a quest gives, an item's icon and name and how many: "Health Potion
47// x2".
48std::string Prize(const items::Item& item, uint32_t count) {
49 // Translators: An item's name, and how many of it.
50 return absl::StrCat(Icon(item.look().icon()), " ",
51 absl::Substitute(Tr("$0 x$1"), NameOf(item), count));
52}
53
54} // namespace
55
56void QuestWindow::Open(uint32_t npc, std::string name,
57 std::vector<uint32_t> quests) {
58 npc_ = npc;
59 name_ = std::move(name);
60 quests_ = std::move(quests);
61}
62
63std::optional<protocol::Input> QuestWindow::Build(
64 const protocol::Quests& yours) {
65 if (!npc_) {
66 return std::nullopt;
67 }
68 const ImVec2 display = ImGui::GetIO().DisplaySize;
69 ImGui::SetNextWindowPos({display.x / 2, display.y * 0.6f},
70 ImGuiCond_Appearing, {0.5f, 0.5f});
71 ImGui::SetNextWindowSize({Ui(400.0f), 0.0f});
72 bool open = true;
73 BeginPanel(name_.c_str(), &open,
74 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
75 std::optional<protocol::Input> ask;
76 for (const uint32_t id : quests_) {
77 const quests::Quest& quest = content_.quests.at(id);
78 ImGui::PushID(static_cast<int>(id));
79 ImGui::PushFont(GetFonts().title, 17.0f);
80 ImGui::TextColored(kTitle, "%s", NameOf(quest));
81 ImGui::PopFont();
82 ImGui::TextWrapped("%s", TextOf(quest));
83 ImGui::Spacing();
84 ImGui::Text(
85 "%s %s", ICON_FA_SKULL,
86 absl::Substitute(Tr("Kill $0"), Monsters(content_, quest.kill()))
87 .c_str());
88 ImGui::Text("%s %s", ICON_FA_STAR, Experience(quest).c_str());
89 for (const quests::Prize& prize : quest.prizes()) {
90 ImGui::TextUnformatted(
91 Prize(content_.items.by_id.at(prize.item()), prize.count()).c_str());
92 }
93 ImGui::Spacing();
94 const auto on = yours.on().find(id);
95 if (std::ranges::find(yours.done(), id) != yours.done().end()) {
96 ImGui::TextDisabled("%s", Tr("Done"));
97 } else if (on == yours.on().end()) {
98 if (GameButton("Take the quest", {0.0f, 0.0f}, true)) {
99 ask.emplace();
100 ask->mutable_take()->set_npc(*npc_);
101 ask->mutable_take()->set_quest(id);
102 }
103 } else if (on->second < quest.kill().count()) {
104 ImGui::TextDisabled("%s",
105 absl::Substitute(Tr("$0 of $1 killed"), on->second,
106 quest.kill().count())
107 .c_str());
108 } else if (GameButton("Turn it in", {0.0f, 0.0f}, true)) {
109 ask.emplace();
110 ask->mutable_turn_in()->set_npc(*npc_);
111 ask->mutable_turn_in()->set_quest(id);
112 }
113 ImGui::PopID();
114 ImGui::Spacing();
115 }
116 EndPanel();
117 if (!open) {
118 npc_.reset();
119 }
120 return ask;
121}
122
123std::optional<protocol::Input> QuestLog::Build(const protocol::Quests& yours) {
124 if (!visible_) {
125 return std::nullopt;
126 }
127 // In the order of their ids, as a protobuf map has none, and those they've
128 // done, the highest id first.
129 const std::map<uint32_t, uint32_t> on(yours.on().begin(), yours.on().end());
130 const std::vector<uint32_t> done(yours.done().rbegin(), yours.done().rend());
131 const auto known = [&](uint32_t id) { return content_.quests.contains(id); };
132 // The one picked, while it's still on or done, or else the first on.
133 if (!on.contains(picked_) && std::ranges::find(done, picked_) == done.end()) {
134 picked_ = on.empty() ? 0 : on.begin()->first;
135 giving_up_ = false;
136 }
137
138 const ImVec2 display = ImGui::GetIO().DisplaySize;
139 ImGui::SetNextWindowPos({display.x / 2.0f, display.y * 0.45f},
140 ImGuiCond_FirstUseEver, {0.5f, 0.5f});
141 ImGui::SetNextWindowSize(Ui(ImVec2{680.0f, 420.0f}));
142 std::optional<protocol::Input> ask;
143 if (!BeginPanel("Quests", &visible_,
144 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings |
145 ImGuiWindowFlags_NoCollapse)) {
146 EndPanel();
147 return ask;
148 }
149
150 // The quests, down the left.
151 ImGui::BeginChild("List", {Ui(230.0f), 0.0f});
152 ImGui::SeparatorText(Tr("On"));
153 for (const auto& [id, kills] : on) {
154 if (!known(id)) {
155 continue;
156 }
157 const quests::Quest& quest = content_.quests.at(id);
158 const bool finished = kills >= quest.kill().count();
159 const std::string label = absl::StrCat(finished ? ICON_FA_CHECK " " : "",
160 NameOf(quest), "###quest", id);
161 if (ImGui::Selectable(label.c_str(), picked_ == id)) {
162 picked_ = id;
163 giving_up_ = false;
164 }
165 }
166 if (on.empty()) {
167 ImGui::TextDisabled("%s", Tr("None yet."));
168 }
169 if (!done.empty()) {
170 ImGui::Spacing();
171 ImGui::SeparatorText(Tr("Done"));
172 for (const uint32_t id : done) {
173 if (!known(id)) {
174 continue;
175 }
176 ImGui::PushStyleColor(ImGuiCol_Text,
177 ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled));
178 const std::string label =
179 absl::StrCat(NameOf(content_.quests.at(id)), "###done", id);
180 if (ImGui::Selectable(label.c_str(), picked_ == id)) {
181 picked_ = id;
182 giving_up_ = false;
183 }
184 ImGui::PopStyleColor();
185 }
186 }
187 ImGui::EndChild();
188 ImGui::SameLine(0.0f, Ui(16.0f));
189
190 // The one picked, on the right.
191 ImGui::BeginChild("Quest");
192 if (!known(picked_)) {
193 ImGui::TextDisabled("%s",
194 Tr("Someone with a ! over them has a quest for you."));
195 ImGui::EndChild();
196 EndPanel();
197 return ask;
198 }
199 const quests::Quest& quest = content_.quests.at(picked_);
200 const auto giver = content_.npcs.kinds.find(quest.giver());
201 const char* from = giver != content_.npcs.kinds.end() ? NameOf(giver->second)
202 : Tr("someone");
203 ImGui::PushFont(GetFonts().title, 19.0f);
204 ImGui::TextColored(kTitle, "%s", NameOf(quest));
205 ImGui::PopFont();
206 ImGui::TextDisabled("%s", absl::Substitute(Tr("From $0"), from).c_str());
207 ImGui::Spacing();
208 ImGui::TextWrapped("%s", TextOf(quest));
209 ImGui::Spacing();
210 ImGui::Text("%s %s", ICON_FA_SKULL,
211 absl::Substitute(Tr("Kill $0"), Monsters(content_, quest.kill()))
212 .c_str());
213 const auto kills = on.find(picked_);
214 if (kills != on.end()) {
215 // How far they've got, as a bar.
216 const uint32_t wanted = std::max(quest.kill().count(), 1u);
217 const uint32_t got = std::min(kills->second, wanted);
218 const ImVec2 at = ImGui::GetCursorScreenPos();
219 const float width = ImGui::GetContentRegionAvail().x;
220 const std::string count = absl::StrCat(got, " / ", wanted);
221 DrawBar(ImGui::GetWindowDrawList(), at, {at.x + width, at.y + Ui(18.0f)},
222 static_cast<float>(got) / static_cast<float>(wanted),
223 colors::kGoldDark, IM_COL32(90, 58, 10, 255), count.c_str());
224 ImGui::Dummy({width, Ui(22.0f)});
225 }
226 ImGui::Text("%s %s", ICON_FA_STAR, Experience(quest).c_str());
227 for (const quests::Prize& prize : quest.prizes()) {
228 const auto item = content_.items.by_id.find(prize.item());
229 if (item == content_.items.by_id.end()) {
230 continue;
231 }
232 ImGui::TextUnformatted(Prize(item->second, prize.count()).c_str());
233 }
234 ImGui::Spacing();
235 if (kills == on.end()) {
236 ImGui::TextDisabled("%s", Tr("Turned in."));
237 } else {
238 if (kills->second >= quest.kill().count()) {
239 ImGui::TextColored(
240 ImGui::ColorConvertU32ToFloat4(colors::kGood), ICON_FA_CHECK " %s",
241 absl::Substitute(Tr("Done: take it back to $0."), from).c_str());
242 }
243 ImGui::Spacing();
244 if (!giving_up_) {
245 giving_up_ = GameButton("Give up", {Ui(160.0f), 0.0f});
246 } else {
247 ImGui::TextColored(
248 {1.0f, 0.7f, 0.4f, 1.0f}, "%s",
249 absl::Substitute(
250 Tr("Give up $0? What you've killed for it goes too."),
251 NameOf(quest))
252 .c_str());
253 if (GameButton("Give up", {Ui(160.0f), 0.0f}, true)) {
254 ask.emplace();
255 ask->set_give_up(picked_);
256 giving_up_ = false;
257 }
258 ImGui::SameLine();
259 if (GameButton("Keep it", {Ui(160.0f), 0.0f})) {
260 giving_up_ = false;
261 }
262 }
263 }
264 ImGui::EndChild();
265 EndPanel();
266 return ask;
267}
268
269void MarkQuestGivers(std::vector<Figure>& figures, const Content& content,
270 const protocol::Quests& yours) {
271 for (Figure& figure : figures) {
272 figure.mark = 0;
273 for (const uint32_t id : figure.quests) {
274 const auto on = yours.on().find(id);
275 if (on != yours.on().end()) {
276 const auto quest = content.quests.find(id);
277 if (quest != content.quests.end() &&
278 on->second >= quest->second.kill().count()) {
279 figure.mark = '?';
280 }
281 } else if (figure.mark == 0 &&
282 std::ranges::find(yours.done(), id) == yours.done().end()) {
283 figure.mark = '!';
284 }
285 }
286 }
287}
288
289void DrawQuestTracker(const Content& content, const protocol::Quests& yours,
290 float top) {
291 if (yours.on().empty()) {
292 return;
293 }
294 // Words with shadows, without a window, lined up on the right, so that
295 // they read on any ground, and hide as little of it as they can.
296 ImDrawList* draw = ImGui::GetBackgroundDrawList();
297 const Fonts& fonts = GetFonts();
298 const float right = ImGui::GetIO().DisplaySize.x - Ui(24.0f);
299 float y = top + Ui(10.0f);
300 const auto line = [&](ImFont* font, float size, ImU32 color,
301 const std::string& text) {
302 const ImVec2 extent =
303 font->CalcTextSizeA(size, FLT_MAX, 0.0f, text.c_str());
304 DrawShadowedText(draw, font, size, {right - extent.x, y}, color,
305 text.c_str());
306 y += extent.y + Ui(2.0f);
307 };
308 line(fonts.title, Ui(16.0f), colors::kGold, Tr("Quests"));
309 y += Ui(4.0f);
310 // In the order of their ids, as a protobuf map has none.
311 const std::map<uint32_t, uint32_t> on(yours.on().begin(), yours.on().end());
312 for (const auto& [id, kills] : on) {
313 const auto quest = content.quests.find(id);
314 if (quest == content.quests.end()) {
315 continue;
316 }
317 line(fonts.bold, Ui(17.0f), colors::kGoldLight, NameOf(quest->second));
318 const std::string wanted = Monsters(content, quest->second.kill());
319 // Done, it says so, until it's turned in.
320 if (kills >= quest->second.kill().count()) {
321 // Translators: How many they've killed, of so many of a kind of
322 // monster, such as 3 Skeletons.
323 line(fonts.text, Ui(16.0f), colors::kGood,
324 absl::StrCat(ICON_FA_CHECK " ",
325 absl::Substitute(Tr("$0 / $1, done"), kills, wanted)));
326 } else {
327 line(fonts.text, Ui(16.0f), colors::kPaper,
328 absl::StrCat(kills, " / ", wanted));
329 }
330 y += Ui(6.0f);
331 }
332}
333
334} // namespace nanommo
std::optional< protocol::Input > Build(const protocol::Quests &yours)
std::optional< protocol::Input > Build(const protocol::Quests &yours)
void Open(uint32_t npc, std::string name, std::vector< uint32_t > quests)
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kGood
Definition theme.h:32
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kPaper
Definition theme.h:26
constexpr ImU32 kGold
Definition theme.h:21
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
void DrawBar(ImDrawList *draw, ImVec2 min, ImVec2 max, float share, ImU32 color, ImU32 dark, const char *text)
Definition theme.cc:452
void MarkQuestGivers(std::vector< Figure > &figures, const Content &content, const protocol::Quests &yours)
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
std::string Icon(uint32_t code)
Definition icon.h:12
const char * TrN(const char *one, const char *many, uint64_t n)
The form for the count: in English, one for 1 and many for the rest.
const char * TextOf(const quests::Quest &quest)
What a quest's giver says, giving it.
Definition names.cc:55
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
Definition theme.cc:272
const Fonts & GetFonts()
Definition theme.cc:66
void DrawQuestTracker(const Content &content, const protocol::Quests &yours, float top)
const char * NameOf(const items::Item &item)
Definition names.cc:14
std::map< uint32_t, quests::Quest > quests
By id.
Definition content.h:31
ImFont * text
Definition theme.h:42
ImFont * bold
Definition theme.h:43
ImFont * title
Definition theme.h:44