/src/client/craft_window.cc
Line | Count | Source |
1 | | #include "client/craft_window.h" |
2 | | |
3 | | #include <imgui.h> |
4 | | |
5 | | #include <algorithm> |
6 | | #include <cstdint> |
7 | | #include <optional> |
8 | | #include <string> |
9 | | #include <vector> |
10 | | |
11 | | #include "absl/strings/str_cat.h" |
12 | | #include "absl/strings/substitute.h" |
13 | | #include "client/icon.h" |
14 | | #include "client/names.h" |
15 | | #include "client/theme.h" |
16 | | #include "client/translations.h" |
17 | | |
18 | | namespace nanommo { |
19 | | |
20 | | namespace { |
21 | | |
22 | | // In points, at a scale of one: the bar's size, and how far its top is from |
23 | | // the bottom of the screen, clear of the hotbar's two rows. |
24 | | constexpr float kBarWidth = 300.0f; |
25 | | constexpr float kBarHeight = 18.0f; |
26 | | constexpr float kBarAbove = 200.0f; |
27 | | |
28 | | constexpr ImVec4 kTitle{1.0f, 0.82f, 0.3f, 1.0f}; |
29 | | // What there's enough of, in green, and what there isn't, faint. |
30 | | constexpr ImVec4 kEnough{0.49f, 0.84f, 0.44f, 1.0f}; |
31 | | constexpr ImVec4 kShort{0.56f, 0.48f, 0.44f, 1.0f}; |
32 | | |
33 | | // The item's icon and name, or a question mark for one the items file |
34 | | // doesn't have. |
35 | 0 | std::string Named(const ItemTable& items, uint32_t id) { |
36 | 0 | const auto item = items.by_id.find(id); |
37 | 0 | if (item == items.by_id.end()) { |
38 | 0 | return "?"; |
39 | 0 | } |
40 | 0 | return absl::StrCat(Icon(item->second.look().icon()), " ", |
41 | 0 | NameOf(item->second)); |
42 | 0 | } |
43 | | |
44 | | } // namespace |
45 | | |
46 | 7 | uint32_t HowMany(const protocol::Inventory& bag, uint32_t item) { |
47 | 7 | uint32_t count = 0; |
48 | 18 | for (const auto& [slot, stack] : bag.bag()) { |
49 | 18 | if (stack.item() == item) { |
50 | 8 | count += CountOf(stack); |
51 | 8 | } |
52 | 18 | } |
53 | 7 | return count; |
54 | 7 | } |
55 | | |
56 | | bool CanMake(const recipes::Recipe& recipe, const protocol::Inventory& bag, |
57 | 4 | int level) { |
58 | 4 | return level >= static_cast<int>(recipe.level()) && |
59 | 4 | std::ranges::all_of( |
60 | 5 | recipe.inputs(), [&bag](const recipes::ItemCount& input) { |
61 | 5 | return HowMany(bag, input.item()) >= input.count(); |
62 | 5 | }); |
63 | 4 | } |
64 | | |
65 | 3 | float MadeShare(const recipes::Recipe& recipe, uint32_t done) { |
66 | 3 | const float whole = 1000.0f * static_cast<float>(recipe.seconds()); |
67 | 3 | return whole > 0.0f ? std::min(static_cast<float>(done) / whole, 1.0f) : 1.0f0 ; |
68 | 3 | } |
69 | | |
70 | | std::optional<protocol::CraftAction> CraftWindow::Build( |
71 | | const std::vector<Figure>& figures, const protocol::Inventory& bag, |
72 | 0 | const protocol::Crafting& making) { |
73 | 0 | if (making.recipe() != 0) { |
74 | 0 | DrawProgress(making); |
75 | 0 | } |
76 | 0 | if (!visible_) { |
77 | 0 | return std::nullopt; |
78 | 0 | } |
79 | 0 | const auto you = std::ranges::find_if(figures, &Figure::you); |
80 | 0 | const int level = you != figures.end() ? you->level : 0; |
81 | 0 | const ImVec2 display = ImGui::GetIO().DisplaySize; |
82 | 0 | ImGui::SetNextWindowPos({display.x / 2, display.y * 0.45f}, |
83 | 0 | ImGuiCond_Appearing, {0.5f, 0.5f}); |
84 | 0 | ImGui::SetNextWindowSize({Ui(360.0f), 0.0f}); |
85 | 0 | BeginPanel("Crafting", &visible_, |
86 | 0 | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings); |
87 | 0 | std::optional<protocol::CraftAction> asked; |
88 | 0 | for (const auto& [id, recipe] : content_.recipes) { |
89 | | // They know each of their level or below. |
90 | 0 | if (static_cast<int>(recipe.level()) > level) { |
91 | 0 | continue; |
92 | 0 | } |
93 | 0 | ImGui::PushID(static_cast<int>(id)); |
94 | 0 | ImGui::PushFont(GetFonts().title, 17.0f); |
95 | 0 | ImGui::TextColored(kTitle, "%s", NameOf(recipe)); |
96 | 0 | ImGui::PopFont(); |
97 | 0 | for (const recipes::ItemCount& input : recipe.inputs()) { |
98 | 0 | const uint32_t have = HowMany(bag, input.item()); |
99 | 0 | ImGui::TextColored( |
100 | 0 | have >= input.count() ? kEnough : kShort, "%s %u / %u", |
101 | 0 | Named(content_.items, input.item()).c_str(), have, input.count()); |
102 | 0 | } |
103 | 0 | ImGui::TextUnformatted( |
104 | 0 | absl::Substitute(Tr("Makes $0 x$1, in $2 s"), |
105 | 0 | Named(content_.items, recipe.output().item()), |
106 | 0 | recipe.output().count(), recipe.seconds()) |
107 | 0 | .c_str()); |
108 | 0 | if (GameButton("Craft", {Ui(90.0f), Ui(28.0f)}, true, |
109 | 0 | making.recipe() == 0 && CanMake(recipe, bag, level))) { |
110 | 0 | asked.emplace(); |
111 | 0 | asked->set_start(id); |
112 | 0 | } |
113 | 0 | ImGui::PopID(); |
114 | 0 | ImGui::Spacing(); |
115 | 0 | } |
116 | | // Moving stops it too, and so does this. |
117 | 0 | if (making.recipe() != 0 && GameButton("Stop", {Ui(90.0f), Ui(28.0f)})) { |
118 | 0 | asked.emplace(); |
119 | 0 | asked->set_stop(true); |
120 | 0 | } |
121 | 0 | EndPanel(); |
122 | 0 | return asked; |
123 | 0 | } |
124 | | |
125 | 0 | void CraftWindow::DrawProgress(const protocol::Crafting& making) const { |
126 | 0 | const auto recipe = content_.recipes.find(making.recipe()); |
127 | 0 | if (recipe == content_.recipes.end()) { |
128 | 0 | return; |
129 | 0 | } |
130 | 0 | const auto item = content_.items.by_id.find(recipe->second.output().item()); |
131 | 0 | const char* name = |
132 | 0 | item != content_.items.by_id.end() ? NameOf(item->second) : ""; |
133 | 0 | ImDrawList* draw = ImGui::GetBackgroundDrawList(); |
134 | 0 | const ImVec2 display = ImGui::GetIO().DisplaySize; |
135 | 0 | const float width = Ui(kBarWidth); |
136 | 0 | const ImVec2 min{(display.x - width) / 2.0f, display.y - Ui(kBarAbove)}; |
137 | 0 | const ImVec2 max{min.x + width, min.y + Ui(kBarHeight)}; |
138 | | // In a panel, as everything else on the screen is. |
139 | 0 | const float around = Ui(10.0f); |
140 | 0 | DrawPanel(draw, {min.x - around, min.y - around}, |
141 | 0 | {max.x + around, max.y + around}); |
142 | 0 | DrawBar(draw, min, max, MadeShare(recipe->second, making.done()), |
143 | 0 | colors::kFlame, colors::kCrimson, name); |
144 | 0 | } |
145 | | |
146 | | } // namespace nanommo |