nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
compass.cc
Go to the documentation of this file.
1#include "client/compass.h"
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <array>
7#include <cfloat>
8#include <cmath>
9#include <cstdint>
10#include <numbers>
11#include <optional>
12#include <string>
13#include <vector>
14
15#include "absl/strings/substitute.h"
16#include "client/atmosphere.h"
17#include "client/theme.h"
18#include "client/translations.h"
19#include "extras/IconsFontAwesome6.h"
20#include "terrain/lands.h"
21
22namespace nanommo {
23
24namespace {
25
26constexpr float kPi = std::numbers::pi_v<float>;
27
28// Half the world, a quarter turn to each side of the way the camera looks.
29constexpr float kSpan = kPi;
30
31// In points, at a scale of one.
32constexpr float kWidth = 620.0f;
33constexpr float kHeight = 36.0f;
34constexpr float kTop = 14.0f;
35
36// Nearer the middle than this share of each half, a mark says how far it is.
37constexpr float kNear = 0.12f;
38
39// The compass's points, every eighth of a turn from north, translated as
40// they're shown, in the compass's context, as a letter alone could be many
41// things.
42constexpr std::array<const char*, 8> kPoints = {
43 N_("compass", "N"), N_("compass", "NE"), N_("compass", "E"),
44 N_("compass", "SE"), N_("compass", "S"), N_("compass", "SW"),
45 N_("compass", "W"), N_("compass", "NW")};
46
47// Things fade out towards the compass's ends, from three quarters of the way.
48float EdgeFade(float at) {
49 return std::clamp((1.0f - std::abs(at)) / 0.25f, 0.0f, 1.0f);
50}
51
52// A line along the compass's edge, which fades out at its ends.
53void DrawEdge(ImDrawList* draw, float left, float right, float y) {
54 const float middle = (left + right) / 2.0f;
55 const float thickness = std::max(1.0f, Ui(1.0f));
56 const ImU32 brass = colors::kBrass;
57 draw->AddRectFilledMultiColor({left, y}, {middle, y + thickness},
58 Faded(brass, 0.0f), brass, brass,
59 Faded(brass, 0.0f));
60 draw->AddRectFilledMultiColor({middle, y}, {right, y + thickness}, brass,
61 Faded(brass, 0.0f), Faded(brass, 0.0f), brass);
62}
63
64void DrawCentred(ImDrawList* draw, ImFont* font, float size, ImVec2 centre,
65 ImU32 color, const char* text) {
66 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, text);
67 DrawShadowedText(draw, font, size,
68 {centre.x - extent.x / 2.0f, centre.y - extent.y / 2.0f},
69 color, text);
70}
71
72ImU32 SkyOf(int land) {
73 const std::array<float, 3>& sky = kLooks[land].atmosphere.sky;
74 const auto byte = [](float value) {
75 return static_cast<int>(std::clamp(value, 0.0f, 1.0f) * 255.0f);
76 };
77 return IM_COL32(byte(sky[0]), byte(sky[1]), byte(sky[2]), 255);
78}
79
80void DrawMark(ImDrawList* draw, const CompassMark& mark, ImVec2 at,
81 float alpha) {
82 const Fonts& fonts = GetFonts();
83 switch (mark.kind) {
85 DrawCentred(draw, fonts.text, Ui(16.0f), at, Faded(mark.color, alpha),
86 ICON_FA_SKULL);
87 break;
90 DrawCentred(draw, fonts.title, Ui(22.0f), at, Faded(mark.color, alpha),
91 mark.kind == CompassMark::Kind::kQuest ? "!" : "?");
92 break;
94 draw->AddCircle(at, Ui(6.5f), Faded(IM_COL32_BLACK, 0.8f * alpha), 0,
95 Ui(4.5f));
96 draw->AddCircle(at, Ui(6.5f), Faded(mark.color, alpha), 0, Ui(2.5f));
97 break;
99 DrawCentred(draw, fonts.text, Ui(15.0f), at, Faded(mark.color, alpha),
100 ICON_FA_LOCATION_DOT);
101 break;
102 }
103}
104
105} // namespace
106
107float Bearing(Vector3 from, Vector3 to) {
108 return std::atan2(to.x - from.x, -(to.z - from.z));
109}
110
111float Heading(const Camera3D& camera) {
112 return Bearing(camera.position, camera.target);
113}
114
115std::optional<float> CompassAt(float heading, float bearing, float span) {
116 // Round from the heading the shorter way, to between minus and plus half a
117 // turn.
118 const float round = std::remainder(bearing - heading, 2.0f * kPi);
119 if (std::abs(round) > span / 2.0f) {
120 return std::nullopt;
121 }
122 return round / (span / 2.0f);
123}
124
125std::vector<CompassMark> CompassMarks(const std::vector<Figure>& figures,
126 int land, uint32_t target,
127 std::optional<Vector3> destination) {
128 const auto you = std::ranges::find_if(figures, &Figure::you);
129 if (you == figures.end()) {
130 return {};
131 }
132 const Vector3 here = you->position;
133 std::vector<CompassMark> marks;
134 const auto add = [&](CompassMark::Kind kind, Vector3 to, ImU32 color) {
135 marks.push_back({.kind = kind,
136 .bearing = Bearing(here, to),
137 .distance = std::hypot(to.x - here.x, to.z - here.z),
138 .color = color});
139 };
140 for (const Gate& gate : kGates) {
141 if (gate.land == land) {
142 add(CompassMark::Kind::kGate, {gate.place.x, 0.0f, gate.place.z},
143 SkyOf(gate.to));
144 }
145 }
146 for (const Figure& figure : figures) {
147 if (figure.npc && figure.mark == '!') {
148 add(CompassMark::Kind::kQuest, figure.position, colors::kGold);
149 } else if (figure.npc && figure.mark == '?') {
150 add(CompassMark::Kind::kTurnIn, figure.position, colors::kGold);
151 }
152 }
153 if (destination) {
155 }
156 // Last, so that it's drawn over the rest.
157 for (const Figure& figure : figures) {
158 if (figure.monster && figure.id == target && target != 0 &&
159 figure.to != protocol::MOTION_DEAD) {
160 add(CompassMark::Kind::kTarget, figure.position, colors::kEmber);
161 }
162 }
163 return marks;
164}
165
166void DrawCompass(float heading, const std::vector<CompassMark>& marks) {
167 // Under the windows, so that one dragged over it covers it.
168 ImDrawList* draw = ImGui::GetBackgroundDrawList();
169 const Fonts& fonts = GetFonts();
170 const ImVec2 screen = ImGui::GetIO().DisplaySize;
171 const float width = std::min(Ui(kWidth), screen.x * 0.6f);
172 const float height = Ui(kHeight);
173 const ImVec2 min{(screen.x - width) / 2.0f, Ui(kTop)};
174 const ImVec2 max{min.x + width, min.y + height};
175 const float middle = (min.x + max.x) / 2.0f;
176 const float half = width / 2.0f;
177
178 // A dark band, which fades out at its ends.
179 const float fade = width * 0.2f;
180 const ImU32 band = Faded(colors::kNight, 0.7f);
181 const ImU32 clear = Faded(colors::kNight, 0.0f);
182 draw->AddRectFilledMultiColor(min, {min.x + fade, max.y}, clear, band, band,
183 clear);
184 draw->AddRectFilled({min.x + fade, min.y}, {max.x - fade, max.y}, band);
185 draw->AddRectFilledMultiColor({max.x - fade, min.y}, max, band, clear, clear,
186 band);
187 DrawEdge(draw, min.x, max.x, min.y);
188 DrawEdge(draw, min.x, max.x, max.y - std::max(1.0f, Ui(1.0f)));
189
190 // The ticks, every five degrees, longer every fifteen, and the points, every
191 // forty-five.
192 for (int degrees = 0; degrees < 360; degrees += 5) {
193 const std::optional<float> at =
194 CompassAt(heading, static_cast<float>(degrees) * kPi / 180.0f, kSpan);
195 if (!at) {
196 continue;
197 }
198 const float x = middle + *at * half;
199 const float alpha = EdgeFade(*at);
200 if (degrees % 45 == 0) {
201 const bool cardinal = degrees % 90 == 0;
202 const ImU32 color =
203 degrees == 0 ? colors::kEmber
204 : (cardinal ? colors::kGoldLight : colors::kMuted);
205 DrawCentred(draw, fonts.title, cardinal ? Ui(22.0f) : Ui(14.0f),
206 {x, (min.y + max.y) / 2.0f}, Faded(color, alpha),
207 Tr("compass", kPoints[degrees / 45]));
208 } else {
209 const float tall = degrees % 15 == 0 ? Ui(9.0f) : Ui(5.0f);
210 const ImU32 color =
211 degrees % 15 == 0 ? colors::kGoldDark : colors::kFaint;
212 draw->AddLine({x, max.y - Ui(3.0f) - tall}, {x, max.y - Ui(3.0f)},
213 Faded(color, alpha), std::max(1.0f, Ui(1.2f)));
214 }
215 }
216
217 // The marks, and how far the one nearest the middle is, if it's near.
218 const CompassMark* nearest = nullptr;
219 float nearest_at = kNear;
220 for (const CompassMark& mark : marks) {
221 if (const std::optional<float> at = CompassAt(heading, mark.bearing, kSpan);
222 at && std::abs(*at) < nearest_at) {
223 nearest = &mark;
224 nearest_at = std::abs(*at);
225 }
226 }
227 for (const CompassMark& mark : marks) {
228 const std::optional<float> at = CompassAt(heading, mark.bearing, kSpan);
229 const bool kept = mark.kind == CompassMark::Kind::kTarget ||
230 mark.kind == CompassMark::Kind::kQuest ||
231 mark.kind == CompassMark::Kind::kTurnIn;
232 if (!at && !kept) {
233 continue;
234 }
235 // Behind you, it waits at the end it's nearer.
236 const float side =
237 at ? *at
238 : (std::remainder(mark.bearing - heading, 2.0f * kPi) < 0.0f ? -1.0f
239 : 1.0f);
240 const float x = middle + side * (half - (at ? 0.0f : Ui(10.0f)));
241 const float alpha = at ? std::max(EdgeFade(*at), 0.0f) : 0.8f;
242 if (alpha <= 0.0f) {
243 continue;
244 }
245 DrawMark(draw, mark, {x, (min.y + max.y) / 2.0f}, alpha);
246 if (!at) {
247 // An arrow out past the end, the way to turn.
248 const float tip = side < 0.0f ? min.x - Ui(2.0f) : max.x + Ui(2.0f);
249 const float back = tip - side * Ui(7.0f);
250 const float y = (min.y + max.y) / 2.0f;
251 draw->AddTriangleFilled({tip, y}, {back, y - Ui(5.0f)},
252 {back, y + Ui(5.0f)}, Faded(mark.color, 0.8f));
253 } else if (&mark == nearest) {
254 const std::string distance = absl::Substitute(
255 Tr("$0 m"), static_cast<int>(std::lround(mark.distance)));
256 DrawCentred(draw, fonts.bold, Ui(13.0f), {x, max.y + Ui(9.0f)},
257 Faded(colors::kPaper, 0.9f), distance.c_str());
258 }
259 }
260
261 // A thin line down the middle, and a notch at the top, where the camera
262 // looks.
263 draw->AddLine({middle, min.y + Ui(2.0f)}, {middle, max.y - Ui(2.0f)},
264 Faded(colors::kGold, 0.35f), std::max(1.0f, Ui(1.0f)));
265 const float notch = Ui(7.0f);
266 draw->AddTriangleFilled({middle - notch, min.y - Ui(1.0f)},
267 {middle + notch, min.y - Ui(1.0f)},
268 {middle, min.y + notch}, colors::kGold);
269}
270
271} // namespace nanommo
constexpr ImU32 kFaint
Definition theme.h:28
constexpr ImU32 kEmber
Definition theme.h:19
constexpr ImU32 kGoldDark
Definition theme.h:23
constexpr ImU32 kGoldLight
Definition theme.h:22
constexpr ImU32 kNight
Definition theme.h:14
constexpr ImU32 kMuted
Definition theme.h:27
constexpr ImU32 kPaper
Definition theme.h:26
constexpr ImU32 kBrass
The trim round every panel: gold gone dark with age.
Definition theme.h:25
constexpr ImU32 kGold
Definition theme.h:21
Definition chat.cc:9
constexpr const char * N_(const char *text)
constexpr std::array< Gate, 4 > kGates
Definition lands.h:45
constexpr float kNear
In metres: full volume this near, and nothing past far.
Definition soundscape.h:26
constexpr std::array< Look, 3 > kLooks
In the order of the lands.
Definition atmosphere.h:164
float Heading(const Camera3D &camera)
Definition compass.cc:111
float Ui(float points)
Definition theme.h:67
void DrawCompass(float heading, const std::vector< CompassMark > &marks)
Definition compass.cc:166
const char * Tr(const char *text)
std::vector< CompassMark > CompassMarks(const std::vector< Figure > &figures, int land, uint32_t target, std::optional< Vector3 > destination)
Definition compass.cc:125
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
Definition theme.cc:59
float Bearing(Vector3 from, Vector3 to)
Which way a place is from another, the same way.
Definition compass.cc:107
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
std::optional< float > CompassAt(float heading, float bearing, float span)
Definition compass.cc:115
Something the compass shows the way to.
Definition compass.h:29
ImFont * bold
Definition theme.h:43
ImFont * title
Definition theme.h:44
A ring of stones in a land, which takes players to another.
Definition lands.h:38