nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
nameplates.cc
Go to the documentation of this file.
1#include "client/nameplates.h"
2
3#include <imgui.h>
4#include <raymath.h>
5
6#include <algorithm>
7#include <cfloat>
8#include <cstdint>
9#include <map>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <vector>
14
15#include "client/names.h"
16#include "client/picking.h"
17#include "client/theme.h"
18
19namespace nanommo {
20
21namespace {
22
23constexpr ImU32 kShadow = IM_COL32(0, 0, 0, 200);
24constexpr ImU32 kLevel = IM_COL32(190, 190, 190, 255);
25constexpr ImU32 kYourName = IM_COL32(255, 210, 80, 255);
26constexpr ImU32 kMonsterName = IM_COL32(255, 140, 120, 255);
27constexpr ImU32 kNpcName = IM_COL32(150, 230, 140, 255);
28constexpr ImU32 kEnemyName = IM_COL32(255, 80, 70, 255);
29constexpr ImU32 kGuild = IM_COL32(205, 190, 150, 255);
30constexpr float kGuildScale = 0.85f;
31// A quest's mark, over an NPC's name, in gold, and twice its size.
32constexpr ImU32 kMark = IM_COL32(255, 205, 60, 255);
33constexpr float kMarkScale = 2.0f;
34// A faction's mark, a diamond half as wide as the name is high, and a fifth
35// of its height apart from it.
36constexpr float kFactionMark = 0.5f;
37constexpr float kFactionGap = 0.2f;
38constexpr ImU32 kPlayerHealth = IM_COL32(90, 200, 90, 255);
39constexpr ImU32 kMonsterHealth = IM_COL32(220, 70, 60, 255);
40
41constexpr ImVec2 kBar{64.0f, 6.0f};
42// A monster's skill on its way: a thinner bar, a little under its health, and
43// the skill's name, smaller than the monster's.
44constexpr float kCastBar = 4.0f;
45constexpr float kCastGap = 3.0f;
46constexpr float kCastScale = 0.85f;
47
48// Further than this, in metres, figures fade into the fog, and their
49// nameplates would float over the hills in front of them.
50constexpr float kFarthest = 50.0f;
51
52void DrawShadowedText(ImDrawList* draw, float size, ImVec2 at, ImU32 color,
53 const std::string& text) {
54 ImFont* font = ImGui::GetFont();
55 draw->AddText(font, size, {at.x + 1, at.y + 1}, kShadow, text.c_str());
56 draw->AddText(font, size, at, color, text.c_str());
57}
58
59// The space a faction's mark takes before a name of the height, if there's
60// one.
61float MarkWidth(const std::optional<Color>& faction, float height) {
62 return faction ? height * (kFactionMark + kFactionGap) : 0.0f;
63}
64
65// The mark at the left of a line of text, of the height, whose top is at.
66void DrawFactionMark(ImDrawList* draw, Color color, ImVec2 at, float height) {
67 const float radius = height * kFactionMark / 2.0f;
68 DrawDiamond(draw, {at.x + radius, at.y + height / 2.0f}, radius,
69 IM_COL32(color.r, color.g, color.b, 255), kShadow);
70}
71
72// Yours stands out from the other players', so that you can find yourself in
73// a crowd, and an enemy's is red, as they can hurt you.
74ImU32 NameColor(const Figure& figure, std::string_view enemy) {
75 if (figure.monster) {
76 return kMonsterName;
77 }
78 if (Enemy(figure, enemy)) {
79 return kEnemyName;
80 }
81 return figure.you ? kYourName : IM_COL32_WHITE;
82}
83
84} // namespace
85
86// Over a tall monster's head, such as a boss's.
87float Above(const Figure& figure) {
88 return figure.monster ? std::max(1.9f, figure.height + 0.3f) : 2.35f;
89}
90
91std::optional<Color> FactionMark(const Figure& figure) {
92 if (figure.monster || figure.faction == 0 || figure.faction_color.a == 0) {
93 return std::nullopt;
94 }
95 return figure.faction_color;
96}
97
98bool HasNameplate(const Figure& figure) {
99 return !figure.name.empty() &&
100 !(figure.monster && figure.to == protocol::MOTION_DEAD);
101}
102
103bool ShowsName(const Figure& figure, const Noticed& noticed) {
104 if (noticed.pointed && *noticed.pointed == Who(figure)) {
105 return true;
106 }
107 if (Picked(figure, noticed.target, noticed.target_player)) {
108 return true;
109 }
110 const NameplateSettings& always = noticed.settings;
111 if (figure.you) {
112 return always.yours;
113 }
114 if (figure.npc) {
115 return always.npcs;
116 }
117 return figure.monster ? always.monsters : always.players;
118}
119
120void DrawNameplates(const std::vector<Figure>& figures, const Camera3D& camera,
121 const std::map<uint32_t, Telegraph>& telegraphs,
122 std::string_view enemy, const Noticed& noticed) {
123 const NameplateSettings& with = noticed.settings;
124 ImDrawList* draw = ImGui::GetBackgroundDrawList();
125 ImFont* font = ImGui::GetFont();
126 const float font_size = ImGui::GetFontSize() * 1.4f;
127 const Vector3 forward =
128 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
129 for (const Figure& figure : figures) {
130 if (!HasNameplate(figure)) {
131 continue;
132 }
133 const bool named = ShowsName(figure, noticed);
134 // An NPC's quest mark shows without its name.
135 if (!named && !(figure.npc && figure.mark != 0)) {
136 continue;
137 }
138 const Vector3& position = figure.position;
139 const Vector3 away = Vector3Subtract(position, camera.position);
140 // Behind the camera, GetWorldToScreen would put it on the screen.
141 if (Vector3Length(away) > kFarthest ||
142 Vector3DotProduct(away, forward) <= 0.0f) {
143 continue;
144 }
145 const Vector2 at = GetWorldToScreen(
146 {position.x, position.y + Above(figure), position.z}, camera);
147 const std::optional<Color> faction = FactionMark(figure);
148 // NPCs don't fight, so they have a name and no level or health.
149 if (figure.npc) {
150 const ImVec2 size =
151 font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, figure.name.c_str());
152 if (named) {
153 const float marked = MarkWidth(faction, size.y);
154 const ImVec2 top{at.x - (marked + size.x) / 2, at.y - size.y};
155 if (faction) {
156 DrawFactionMark(draw, *faction, top, size.y);
157 }
158 DrawShadowedText(draw, font_size, {top.x + marked, top.y}, kNpcName,
159 figure.name);
160 }
161 if (figure.mark != 0) {
162 const std::string mark(1, figure.mark);
163 const float mark_size = font_size * kMarkScale;
164 const ImVec2 over =
165 font->CalcTextSizeA(mark_size, FLT_MAX, 0.0f, mark.c_str());
166 DrawShadowedText(draw, mark_size,
167 {at.x - over.x / 2, at.y - size.y - over.y}, kMark,
168 mark);
169 }
170 continue;
171 }
172
173 const ImVec2 bar_min{at.x - kBar.x / 2, at.y - kBar.y};
174 const ImVec2 bar_max{at.x + kBar.x / 2, at.y};
175 if (with.health) {
176 const float share = figure.max_health > 0
177 ? std::clamp(static_cast<float>(figure.health) /
178 figure.max_health,
179 0.0f, 1.0f)
180 : 0.0f;
181 draw->AddRectFilled(bar_min, bar_max, kShadow);
182 draw->AddRectFilled(bar_min, {bar_min.x + kBar.x * share, bar_max.y},
183 figure.monster ? kMonsterHealth : kPlayerHealth);
184 }
185 // A monster's skill on its way goes under its health, filling as its
186 // shape does.
187 if (const auto cast = telegraphs.find(figure.id);
188 figure.monster && cast != telegraphs.end()) {
189 const Telegraph& telegraph = cast->second;
190 const Color color = GetColor(telegraph.skill.color());
191 const ImVec2 cast_min{bar_min.x, bar_max.y + kCastGap};
192 const ImVec2 cast_max{bar_max.x, cast_min.y + kCastBar};
193 draw->AddRectFilled(cast_min, cast_max, kShadow);
194 draw->AddRectFilled(
195 cast_min, {cast_min.x + kBar.x * telegraph.Filled(), cast_max.y},
196 IM_COL32(color.r, color.g, color.b, 255));
197 const float cast_size = font_size * kCastScale;
198 const char* skill = NameOf(figure.kind, telegraph.skill);
199 const ImVec2 extent =
200 font->CalcTextSizeA(cast_size, FLT_MAX, 0.0f, skill);
201 DrawShadowedText(draw, cast_size, {at.x - extent.x / 2, cast_max.y + 1},
202 IM_COL32_WHITE, skill);
203 }
204
205 const std::string level =
206 with.levels ? std::to_string(figure.level) + " " : "";
207 const float level_width =
208 font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, level.c_str()).x;
209 const ImVec2 name_size =
210 font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, figure.name.c_str());
211 // Over the health, or where it'd be.
212 float top = with.health ? bar_min.y - 2.0f : at.y;
213 // A player's guild goes under their name, smaller, in the angle brackets
214 // games put a guild in.
215 if (with.guilds && !figure.guild.empty()) {
216 const std::string guild = "<" + figure.guild + ">";
217 const float guild_size = font_size * kGuildScale;
218 const ImVec2 extent =
219 font->CalcTextSizeA(guild_size, FLT_MAX, 0.0f, guild.c_str());
220 top -= extent.y;
221 DrawShadowedText(draw, guild_size, {at.x - extent.x / 2, top}, kGuild,
222 guild);
223 }
224 top -= name_size.y;
225 const float marked = MarkWidth(faction, name_size.y);
226 const float left = at.x - (marked + level_width + name_size.x) / 2;
227 if (faction) {
228 DrawFactionMark(draw, *faction, {left, top}, name_size.y);
229 }
230 DrawShadowedText(draw, font_size, {left + marked, top}, kLevel, level);
231 DrawShadowedText(draw, font_size, {left + marked + level_width, top},
232 NameColor(figure, enemy), figure.name);
233 }
234}
235
236} // namespace nanommo
constexpr ImU32 kShadow
Definition theme.h:29
Definition chat.cc:9
void DrawNameplates(const std::vector< Figure > &figures, const Camera3D &camera, const std::map< uint32_t, Telegraph > &telegraphs, std::string_view enemy, const Noticed &noticed)
float Above(const Figure &figure)
In metres above a figure's feet, where its nameplate goes.
Definition nameplates.cc:87
bool HasNameplate(const Figure &figure)
Definition nameplates.cc:98
uint64_t Who(const Figure &figure)
Definition animator.cc:124
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
Definition picking.cc:86
bool Enemy(const Figure &figure, std::string_view enemy)
Definition picking.cc:68
std::optional< Color > FactionMark(const Figure &figure)
Definition nameplates.cc:91
void DrawDiamond(ImDrawList *draw, ImVec2 centre, float radius, ImU32 fill, ImU32 outline)
A diamond, filled, with an outline.
Definition theme.cc:213
bool ShowsName(const Figure &figure, const Noticed &noticed)
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 char * NameOf(const items::Item &item)
Definition names.cc:14
std::string name
Definition animator.h:22
Color faction_color
Definition animator.h:28
float height
A monster's height, in metres, from its kind's look.
Definition animator.h:54
protocol::Motion to
Definition animator.h:77
uint32_t faction
Definition animator.h:27
bool monsters
Whose show always.
Definition settings.h:19
std::optional< uint64_t > pointed
Definition nameplates.h:35
NameplateSettings settings
Definition nameplates.h:34
uint32_t target
Definition nameplates.h:36
uint32_t target_player
Definition nameplates.h:37
monsters::Skill skill
Definition telegraphs.h:16
float Filled() const
How far its shape has filled, from 0 as it shows to 1 as it lands.
Definition telegraphs.cc:10