16#include "absl/strings/str_cat.h"
17#include "absl/strings/substitute.h"
23#include "extras/IconsFontAwesome6.h"
29constexpr float kPi = std::numbers::pi_v<float>;
32constexpr float kShallow = 1.5f;
35constexpr float kRelief = 1.6f;
39constexpr float kRadius = 100.0f;
40constexpr float kRing = 7.0f;
41constexpr float kMargin = 18.0f;
43constexpr ImU32 kOutline = IM_COL32(0, 0, 0, 210);
44constexpr ImU32 kPlayer = IM_COL32(150, 200, 255, 255);
45constexpr ImU32
kMonster = IM_COL32(235, 80, 64, 255);
46constexpr ImU32
kNpc = IM_COL32(255, 211, 77, 255);
48ImU32 Opaque(Color color) {
return IM_COL32(color.r, color.g, color.b, 255); }
51 const std::array<float, 3>& sky =
kLooks[land].atmosphere.sky;
52 return Opaque(ColorFromNormalized({sky[0], sky[1], sky[2], 1.0f}));
65 ImVec2 Way(
float x,
float z)
const {
66 const float c = std::cos(turn);
67 const float s = std::sin(turn);
68 return {x * c + z * s, -x * s + z * c};
71 ImVec2 operator()(
float x,
float z)
const {
72 const ImVec2 way = Way((x - centre.x) * scale, (z - centre.z) * scale);
73 return {origin.x + way.x, origin.y + way.y};
77void DrawDot(ImDrawList* draw, ImVec2 at,
float radius, ImU32 color) {
78 draw->AddCircleFilled(at, radius +
Ui(1.2f), kOutline);
79 draw->AddCircleFilled(at, radius, color);
82void DrawGlyph(ImDrawList* draw, ImVec2 at,
float size, ImU32 color,
85 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, glyph);
86 const ImVec2 corner{at.x - extent.x / 2.0f, at.y - extent.y / 2.0f};
87 for (
const ImVec2 offset : {ImVec2{-1, 0}, ImVec2{1, 0}, ImVec2{0, -1},
88 ImVec2{0, 1}, ImVec2{1, 1}}) {
89 draw->AddText(font, size, {corner.x + offset.x, corner.y + offset.y},
92 draw->AddText(font, size, corner, color, glyph);
98std::optional<ImVec2> OnDisc(ImVec2 at,
const View& view,
float radius,
100 if (radius <= 0.0f) {
103 const float dx = at.x - view.origin.x;
104 const float dy = at.y - view.origin.y;
105 const float distance = std::hypot(dx, dy);
106 if (distance <= radius) {
112 return ImVec2{view.origin.x + dx / distance * radius,
113 view.origin.y + dy / distance * radius};
120void DrawMarkers(ImDrawList* draw,
const View& view,
int land,
121 const std::vector<Figure>& figures, uint32_t target,
122 std::optional<Vector3> destination,
float radius = 0.0f) {
123 const float rim = radius > 0.0f ? radius -
Ui(6.0f) : 0.0f;
125 if (gate.land == land) {
126 if (
const std::optional<ImVec2> at =
127 OnDisc(view(gate.place.x, gate.place.z), view, rim,
false)) {
128 draw->AddCircle(*at,
Ui(6.0f), kOutline, 0,
Ui(4.0f));
129 draw->AddCircle(*at,
Ui(6.0f),
Sky(gate.to), 0,
Ui(2.0f));
133 const Figure* you =
nullptr;
134 for (
const Figure& figure : figures) {
135 const ImVec2 where = view(figure.position.x, figure.position.z);
138 }
else if (figure.npc) {
139 const bool quest = figure.mark != 0;
140 const std::optional<ImVec2> at = OnDisc(where, view, rim, quest);
145 const char mark[2] = {figure.mark, 0};
146 DrawGlyph(draw, *at,
Ui(17.0f),
kNpc, mark);
148 DrawDot(draw, *at,
Ui(2.8f),
kNpc);
150 }
else if (figure.monster) {
151 const bool picked = figure.id == target;
152 const std::optional<ImVec2> at = OnDisc(where, view, rim, picked);
156 if (figure.to != protocol::MOTION_DEAD) {
160 draw->AddCircle(*at,
Ui(5.5f), kOutline, 0,
Ui(3.0f));
163 }
else if (
const std::optional<ImVec2> at =
164 OnDisc(where, view, rim,
false)) {
165 DrawDot(draw, *at,
Ui(2.8f), kPlayer);
169 if (
const std::optional<ImVec2> at =
170 OnDisc(view(destination->x, destination->z), view, rim,
false)) {
171 const float arm =
Ui(4.0f);
173 const float width = color == kOutline ?
Ui(3.5f) :
Ui(1.5f);
174 draw->AddLine({at->x - arm, at->y - arm}, {at->x + arm, at->y + arm},
176 draw->AddLine({at->x - arm, at->y + arm}, {at->x + arm, at->y - arm},
181 if (you !=
nullptr) {
183 const ImVec2 at = view(you->position.x, you->position.z);
184 const ImVec2 ahead = view.Way(std::sin(you->yaw), std::cos(you->yaw));
185 const ImVec2 side{-ahead.y, ahead.x};
186 const auto point = [&](
float across,
float along) {
187 return ImVec2{at.x +
Ui(along * ahead.x + across * side.x),
188 at.y +
Ui(along * ahead.y + across * side.y)};
190 const ImVec2 tip = point(0.0f, 9.0f);
191 const ImVec2 left = point(-6.0f, -6.0f);
192 const ImVec2 notch = point(0.0f, -2.5f);
193 const ImVec2 right = point(6.0f, -6.0f);
196 draw->PathLineTo(tip);
197 draw->PathLineTo(right);
198 draw->PathLineTo(notch);
199 draw->PathLineTo(left);
200 draw->PathStroke(kOutline, ImDrawFlags_Closed,
Ui(1.5f));
207void DrawDisc(ImDrawList* draw, ImTextureID texture,
float half,
208 const View& view,
float radius,
float around) {
209 constexpr int kSegments = 72;
210 const float size = 2.0f * half;
211 const ImVec2 middle{(view.centre.x + half) / size,
212 (view.centre.z + half) / size};
213 draw->PushTexture(texture);
214 draw->PrimReserve(kSegments * 3, kSegments + 1);
215 const unsigned int first = draw->_VtxCurrentIdx;
216 draw->PrimWriteVtx(view.origin, middle, IM_COL32_WHITE);
217 for (
int i = 0; i < kSegments; ++i) {
218 const float angle = 2.0f * kPi *
static_cast<float>(i) / kSegments;
219 const float across = std::cos(angle);
220 const float down = std::sin(angle);
223 const float c = std::cos(view.turn);
224 const float s = std::sin(view.turn);
225 const float x = across * c - down * s;
226 const float z = across * s + down * c;
228 {view.origin.x + across * radius, view.origin.y + down * radius},
229 {middle.x + x * around / size, middle.y + z * around / size},
232 for (
int i = 0; i < kSegments; ++i) {
233 draw->PrimWriteIdx(
static_cast<ImDrawIdx
>(first));
234 draw->PrimWriteIdx(
static_cast<ImDrawIdx
>(first + 1 + i));
235 draw->PrimWriteIdx(
static_cast<ImDrawIdx
>(first + 1 + (i + 1) % kSegments));
241bool RingButton(
const char*
id, ImVec2 centre,
const char* icon) {
242 const float radius =
Ui(11.0f);
243 ImGui::SetCursorScreenPos({centre.x - radius, centre.y - radius});
244 const bool clicked = ImGui::InvisibleButton(
id, {radius * 2, radius * 2});
245 const bool hovered = ImGui::IsItemHovered();
246 ImDrawList* draw = ImGui::GetWindowDrawList();
247 draw->AddCircleFilled(centre, radius +
Ui(1.5f), kOutline);
248 draw->AddCircleFilled(centre, radius,
253 const float size =
Ui(12.0f);
254 const ImVec2 extent = font->CalcTextSizeA(size, FLT_MAX, 0.0f, icon);
255 draw->AddText(font, size,
256 {centre.x - extent.x / 2.0f, centre.y - extent.y / 2.0f},
264 if (texture_.id != 0) {
265 UnloadTexture(texture_);
272 const int size =
static_cast<int>(2.0f * half);
273 Image image = GenImageColor(size, size, BLACK);
274 const Grid<Color> pixels(
static_cast<Color*
>(image.data), size, size);
275 const float inside = half - 1.01f;
276 const auto height = [&terrain, inside](
float x,
float z) {
277 return terrain.
Height(std::clamp(x, -inside, inside),
278 std::clamp(z, -inside, inside));
280 for (
int down = 0; down < size; ++down) {
281 for (
int across = 0; across < size; ++across) {
282 const float x =
static_cast<float>(across) + 0.5f - half;
283 const float z =
static_cast<float>(down) + 0.5f - half;
284 const float here = height(x, z);
291 const float dx = (height(x + 1.0f, z) - height(x - 1.0f, z)) / 2.0f;
292 const float dz = (height(x, z + 1.0f) - height(x, z - 1.0f)) / 2.0f;
293 const float up = 1.0f / std::sqrt(1.0f + dx * dx + dz * dz);
298 std::clamp(1.0f + (dx + dz) * kRelief * up, 0.55f, 1.35f);
299 color = {
static_cast<unsigned char>(std::min(color.r * light, 255.0f)),
300 static_cast<unsigned char>(std::min(color.g * light, 255.0f)),
301 static_cast<unsigned char>(std::min(color.b * light, 255.0f)),
305 pixels[down, across] = color;
309 const Color tree = ColorTint({40, 80, 40, 255}, look.
trees);
310 for (
const Tree& planted : terrain.
trees()) {
311 const int across =
static_cast<int>(planted.x + half);
312 const int down =
static_cast<int>(planted.z + half);
313 for (
int j = down - 1; j <= down; ++j) {
314 for (
int i = across - 1; i <= across; ++i) {
315 if (i >= 0 && j >= 0 && i < size && j < size) {
327 Image image =
DrawLand(terrain, land);
328 if (texture_.id != 0) {
329 UnloadTexture(texture_);
331 texture_ = LoadTextureFromImage(image);
333 SetTextureFilter(texture_, TEXTURE_FILTER_BILINEAR);
335 SetTextureWrap(texture_, TEXTURE_WRAP_CLAMP);
339 zoom_ = std::clamp(
zoom, 0,
static_cast<int>(
kZooms.size()) - 1);
343 std::optional<Vector3> destination,
float heading,
345 const auto you = std::ranges::find_if(figures, &
Figure::you);
346 if (texture_.id == 0 || you == figures.end()) {
350 const ImVec2 screen = ImGui::GetIO().DisplaySize;
351 const float radius =
Ui(kRadius);
352 const float ring =
Ui(kRing);
353 const float outer = radius + ring;
354 const ImVec2 size{2.0f * outer +
Ui(8.0f), 2.0f * outer +
Ui(36.0f)};
355 ImGui::SetNextWindowPos({screen.x -
Ui(kMargin),
Ui(kMargin)},
356 ImGuiCond_Always, {1.0f, 0.0f});
357 ImGui::SetNextWindowSize(size);
358 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0.0f, 0.0f});
359 ImGui::Begin(
"Minimap",
nullptr,
360 ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
361 ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoNav |
362 ImGuiWindowFlags_NoFocusOnAppearing |
363 ImGuiWindowFlags_NoSavedSettings |
364 ImGuiWindowFlags_NoBringToFrontOnFocus);
365 ImGui::PopStyleVar();
366 ImDrawList* draw = ImGui::GetWindowDrawList();
367 const ImVec2 corner = ImGui::GetWindowPos();
368 const ImVec2 centre{corner.x + size.x / 2.0f, corner.y +
Ui(6.0f) + outer};
369 bottom_ = corner.y + size.y;
372 const float around =
kZooms[zoom_];
373 const View view{.centre = {you->position.x, you->position.z},
375 .scale = radius / around,
378 DrawDisc(draw,
static_cast<ImTextureID
>(texture_.id), half_, view, radius,
380 for (
int i = 0; i < 4; ++i) {
381 draw->AddCircle(centre, radius -
Ui(2.0f + 2.5f * i),
382 Faded(IM_COL32_BLACK, 0.22f - 0.05f * i), 0,
Ui(3.0f));
384 DrawMarkers(draw, view, land_, figures, target, destination, radius);
387 draw->AddCircle(centre, radius + ring / 2.0f,
colors::kBrass, 0, ring);
388 draw->PathArcTo(centre, radius + ring / 2.0f, kPi * 1.0f, kPi * 1.6f, 24);
393 for (
int quarter = 0; quarter < 4; ++quarter) {
394 const float bearing =
static_cast<float>(quarter) * kPi / 2.0f - view.turn;
395 const ImVec2 way{std::sin(bearing), -std::cos(bearing)};
397 const ImVec2 at{centre.x + way.x * (radius + ring / 2.0f),
398 centre.y + way.y * (radius + ring / 2.0f)};
401 const float letter =
Ui(13.0f);
402 const char* north =
Tr(
"compass",
"N");
403 const ImVec2 extent = font->CalcTextSizeA(letter, FLT_MAX, 0.0f, north);
404 draw->AddText(font, letter,
405 {at.x - extent.x / 2.0f, at.y - extent.y / 2.0f},
409 draw->AddLine({centre.x + way.x * radius, centre.y + way.y * radius},
410 {centre.x + way.x * outer, centre.y + way.y * outer},
415 const std::string place =
416 absl::StrCat(
static_cast<int>(std::lround(you->position.x)),
", ",
417 static_cast<int>(std::lround(you->position.z)));
419 const float place_size =
Ui(13.0f);
420 const ImVec2 place_extent =
421 bold->CalcTextSizeA(place_size, FLT_MAX, 0.0f, place.c_str());
423 draw, bold, place_size,
424 {centre.x - place_extent.x / 2.0f, centre.y + outer +
Ui(4.0f)},
428 const auto on_ring = [&](
float degrees) {
429 const float angle = degrees * kPi / 180.0f;
430 return ImVec2{centre.x + std::cos(angle) * (outer +
Ui(2.0f)),
431 centre.y + std::sin(angle) * (outer +
Ui(2.0f))};
433 if (RingButton(
"##in", on_ring(128.0f), ICON_FA_PLUS)) {
436 if (RingButton(
"##out", on_ring(150.0f), ICON_FA_MINUS)) {
439 const bool map = RingButton(
"##map", on_ring(45.0f), ICON_FA_MAP);
440 if (ImGui::IsItemHovered()) {
441 ImGui::SetTooltip(
"%s",
Tr(
"Map (M)"));
444 ImGui::SetCursorScreenPos({centre.x - radius, centre.y - radius});
445 ImGui::InvisibleButton(
"##disc", {radius * 2.0f, radius * 2.0f});
446 if (ImGui::IsItemHovered() && ImGui::GetIO().MouseWheel != 0.0f) {
447 set_zoom(zoom_ + (ImGui::GetIO().MouseWheel > 0.0f ? -1 : 1));
455 std::optional<Vector3> destination,
457 if (texture_.id == 0 || !*open) {
460 const ImVec2 screen = ImGui::GetIO().DisplaySize;
461 const float size = std::min(screen.x, screen.y) * 0.72f;
462 ImGui::SetNextWindowPos({screen.x / 2, screen.y / 2}, ImGuiCond_Always,
465 const std::string title = absl::StrCat(
468 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
469 ImGuiWindowFlags_NoSavedSettings);
470 ImGui::Image(
static_cast<ImTextureID
>(texture_.id), {size, size});
471 const ImVec2 min = ImGui::GetItemRectMin();
472 const ImVec2 max = ImGui::GetItemRectMax();
473 const View view{.centre = {0.0f, 0.0f},
474 .origin = {(min.x + max.x) / 2, (min.y + max.y) / 2},
475 .scale = size / (2.0f * half_)};
476 std::optional<Place> clicked;
477 if (ImGui::IsItemHovered()) {
478 ImGui::SetMouseCursor(ImGuiMouseCursor_Hand);
479 if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
480 const ImVec2 mouse = ImGui::GetMousePos();
481 clicked =
Place{.x = (mouse.x - view.origin.x) / view.scale,
482 .z = (mouse.y - view.origin.y) / view.scale};
485 ImDrawList* draw = ImGui::GetWindowDrawList();
487 draw->PushClipRect(min, max,
true);
488 DrawMarkers(draw, view, land_, figures, target, destination);
std::optional< Place > DrawMap(const std::vector< Figure > &figures, uint32_t target, std::optional< Vector3 > destination, bool *open) const
void Build(const Terrain &terrain, int land)
Draws the land into the texture, in place of the last one.
static constexpr std::array< float, 3 > kZooms
In metres from you to the minimap's rim, at each of its zooms.
bool DrawMinimap(const std::vector< Figure > &figures, uint32_t target, std::optional< Vector3 > destination, float heading, MinimapTurn turn)
float half_size() const
From the middle to each side of the grid.
const std::vector< Tree > & trees() const
static constexpr float kSeaLevel
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Vector3 Place(const Point &point, const Terrain &terrain)
constexpr ImU32 kGoldDark
constexpr ImU32 kGoldLight
constexpr ImU32 kBrass
The trim round every panel: gold gone dark with age.
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
constexpr std::array< Gate, 4 > kGates
constexpr std::array< Look, 3 > kLooks
In the order of the lands.
Image DrawLand(const Terrain &terrain, int land)
const char * Tr(const char *text)
std::string LandName(std::string_view land)
constexpr std::array< Land, 3 > kLands
Color GroundColor(const Look &look, float height, float up)
The surface's colour in the look.
ImU32 Faded(ImU32 color, float share)
The same colour, with its alpha times the share.
void DrawDiamond(ImDrawList *draw, ImVec2 centre, float radius, ImU32 fill, ImU32 outline)
A diamond, filled, with an outline.
void DrawShadowedText(ImDrawList *draw, ImFont *font, float size, ImVec2 at, ImU32 color, const char *text)
Text with a shadow under it.
A ring of stones in a land, which takes players to another.
Color deep
Out where it's deep. Its alpha is how much of the ground it hides.
Color shallow
Where it's shallow, near the shore.
A land's colours: its atmosphere, and the colours that go into its meshes.
Color trees
Multiplies the colours of the trees' faces.
In metres from the middle of a land.
What the players in a cell get: the players, monsters and NPCs near it.