nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
craft_window.cc
Go to the documentation of this file.
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
18namespace nanommo {
19
20namespace {
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.
24constexpr float kBarWidth = 300.0f;
25constexpr float kBarHeight = 18.0f;
26constexpr float kBarAbove = 200.0f;
27
28constexpr ImVec4 kTitle{1.0f, 0.82f, 0.3f, 1.0f};
29// What there's enough of, in green, and what there isn't, faint.
30constexpr ImVec4 kEnough{0.49f, 0.84f, 0.44f, 1.0f};
31constexpr 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.
35std::string Named(const ItemTable& items, uint32_t id) {
36 const auto item = items.by_id.find(id);
37 if (item == items.by_id.end()) {
38 return "?";
39 }
40 return absl::StrCat(Icon(item->second.look().icon()), " ",
41 NameOf(item->second));
42}
43
44} // namespace
45
46uint32_t HowMany(const protocol::Inventory& bag, uint32_t item) {
47 uint32_t count = 0;
48 for (const auto& [slot, stack] : bag.bag()) {
49 if (stack.item() == item) {
50 count += CountOf(stack);
51 }
52 }
53 return count;
54}
55
56bool CanMake(const recipes::Recipe& recipe, const protocol::Inventory& bag,
57 int level) {
58 return level >= static_cast<int>(recipe.level()) &&
59 std::ranges::all_of(
60 recipe.inputs(), [&bag](const recipes::ItemCount& input) {
61 return HowMany(bag, input.item()) >= input.count();
62 });
63}
64
65float MadeShare(const recipes::Recipe& recipe, uint32_t done) {
66 const float whole = 1000.0f * static_cast<float>(recipe.seconds());
67 return whole > 0.0f ? std::min(static_cast<float>(done) / whole, 1.0f) : 1.0f;
68}
69
70std::optional<protocol::CraftAction> CraftWindow::Build(
71 const std::vector<Figure>& figures, const protocol::Inventory& bag,
72 const protocol::Crafting& making) {
73 if (making.recipe() != 0) {
74 DrawProgress(making);
75 }
76 if (!visible_) {
77 return std::nullopt;
78 }
79 const auto you = std::ranges::find_if(figures, &Figure::you);
80 const int level = you != figures.end() ? you->level : 0;
81 const ImVec2 display = ImGui::GetIO().DisplaySize;
82 ImGui::SetNextWindowPos({display.x / 2, display.y * 0.45f},
83 ImGuiCond_Appearing, {0.5f, 0.5f});
84 ImGui::SetNextWindowSize({Ui(360.0f), 0.0f});
85 BeginPanel("Crafting", &visible_,
86 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings);
87 std::optional<protocol::CraftAction> asked;
88 for (const auto& [id, recipe] : content_.recipes) {
89 // They know each of their level or below.
90 if (static_cast<int>(recipe.level()) > level) {
91 continue;
92 }
93 ImGui::PushID(static_cast<int>(id));
94 ImGui::PushFont(GetFonts().title, 17.0f);
95 ImGui::TextColored(kTitle, "%s", NameOf(recipe));
96 ImGui::PopFont();
97 for (const recipes::ItemCount& input : recipe.inputs()) {
98 const uint32_t have = HowMany(bag, input.item());
99 ImGui::TextColored(
100 have >= input.count() ? kEnough : kShort, "%s %u / %u",
101 Named(content_.items, input.item()).c_str(), have, input.count());
102 }
103 ImGui::TextUnformatted(
104 absl::Substitute(Tr("Makes $0 x$1, in $2 s"),
105 Named(content_.items, recipe.output().item()),
106 recipe.output().count(), recipe.seconds())
107 .c_str());
108 if (GameButton("Craft", {Ui(90.0f), Ui(28.0f)}, true,
109 making.recipe() == 0 && CanMake(recipe, bag, level))) {
110 asked.emplace();
111 asked->set_start(id);
112 }
113 ImGui::PopID();
114 ImGui::Spacing();
115 }
116 // Moving stops it too, and so does this.
117 if (making.recipe() != 0 && GameButton("Stop", {Ui(90.0f), Ui(28.0f)})) {
118 asked.emplace();
119 asked->set_stop(true);
120 }
121 EndPanel();
122 return asked;
123}
124
125void CraftWindow::DrawProgress(const protocol::Crafting& making) const {
126 const auto recipe = content_.recipes.find(making.recipe());
127 if (recipe == content_.recipes.end()) {
128 return;
129 }
130 const auto item = content_.items.by_id.find(recipe->second.output().item());
131 const char* name =
132 item != content_.items.by_id.end() ? NameOf(item->second) : "";
133 ImDrawList* draw = ImGui::GetBackgroundDrawList();
134 const ImVec2 display = ImGui::GetIO().DisplaySize;
135 const float width = Ui(kBarWidth);
136 const ImVec2 min{(display.x - width) / 2.0f, display.y - Ui(kBarAbove)};
137 const ImVec2 max{min.x + width, min.y + Ui(kBarHeight)};
138 // In a panel, as everything else on the screen is.
139 const float around = Ui(10.0f);
140 DrawPanel(draw, {min.x - around, min.y - around},
141 {max.x + around, max.y + around});
142 DrawBar(draw, min, max, MadeShare(recipe->second, making.done()),
144}
145
146} // namespace nanommo
std::optional< protocol::CraftAction > Build(const std::vector< Figure > &figures, const protocol::Inventory &bag, const protocol::Crafting &making)
constexpr ImU32 kCrimson
Definition theme.h:17
constexpr ImU32 kFlame
Definition theme.h:20
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
uint32_t CountOf(const items::Stack &stack)
How many are in the stack, which is one when it doesn't say.
Definition items.cc:102
void DrawPanel(ImDrawList *draw, ImVec2 min, ImVec2 max, float alpha)
Definition theme.cc:223
void DrawBar(ImDrawList *draw, ImVec2 min, ImVec2 max, float share, ImU32 color, ImU32 dark, const char *text)
Definition theme.cc:452
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
bool CanMake(const recipes::Recipe &recipe, const protocol::Inventory &bag, int level)
float MadeShare(const recipes::Recipe &recipe, uint32_t done)
uint32_t HowMany(const protocol::Inventory &bag, uint32_t item)
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
const Fonts & GetFonts()
Definition theme.cc:66
const char * NameOf(const items::Item &item)
Definition names.cc:14
ItemTable items
Definition content.h:27
std::map< uint32_t, recipes::Recipe > recipes
By id.
Definition content.h:33
The items in the items file, by id.
Definition items.h:18
std::map< uint32_t, items::Item > by_id
Definition items.h:19