19float Turn(
float from,
float to,
float step) {
20 const float gap = std::remainder(to - from, 2.0f * std::numbers::pi_v<float>);
21 return from + std::clamp(gap, -step, step);
25template <
typename Unit>
26Figure Follow(
const std::map<uint32_t, Figure>& figures,
const Unit& unit,
27 uint32_t skill, uint32_t casts,
float seconds) {
28 const Vector3 target{unit.x(), 0.0f, unit.z()};
29 const auto found = figures.find(unit.id());
30 const bool back = found != figures.end() &&
31 found->second.
to == protocol::MOTION_DEAD &&
32 unit.motion() != protocol::MOTION_DEAD;
33 Figure figure = found != figures.end() && !back
35 :
Figure{.position = target,
37 .from = unit.motion(),
41 figure.
id = unit.id();
42 figure.level =
static_cast<int>(unit.level());
43 figure.health =
static_cast<int>(unit.health());
44 figure.max_health =
static_cast<int>(unit.max_health());
46 figure.from_time += seconds;
47 figure.to_time += seconds;
49 if (unit.motion() != figure.to || skill != figure.to_skill ||
50 casts != figure.casts) {
51 figure.from = figure.to;
52 figure.from_skill = figure.to_skill;
53 figure.from_time = figure.to_time;
54 figure.to = unit.motion();
55 figure.to_skill = skill;
56 figure.to_time = 0.0f;
64 figure.position = target;
66 if constexpr (std::is_same_v<Unit, protocol::Monster>) {
67 figure.lift = Lerp(figure.lift, unit.height(), follow);
75Color ColorOf(
const std::map<uint32_t, factions::Faction>& factions,
77 const auto found = factions.find(faction);
78 return found != factions.end() ? GetColor(found->second.color()) : BLANK;
83Figure Stand(
const std::map<uint32_t, Figure>& figures,
84 const protocol::Npc& npc,
const Content& content,
float seconds) {
85 const NpcTable& table = content.npcs;
86 const auto found = figures.find(npc.id());
87 Figure figure = found != figures.end() ? found->second :
Figure{};
90 figure.kind = npc.kind();
92 const auto kind = table.kinds.find(npc.kind());
93 if (kind != table.kinds.end()) {
94 const npcs::Look& look = kind->second.look();
95 figure.name =
NameOf(kind->second);
96 figure.color = GetColor(look.color());
97 figure.features =
Features{.skin = look.skin(),
98 .hair_color = look.hair_color(),
99 .hair =
static_cast<int>(look.hair())};
100 figure.weapon = look.weapon();
101 figure.wearing = {look.wearing().begin(), look.wearing().end()};
102 figure.talks = kind->second.talks();
103 figure.teaches = {kind->second.teaches().begin(),
104 kind->second.teaches().end()};
107 const factions::Faction* heralded =
HeraldOf(content.factions, npc.kind());
108 figure.faction = heralded !=
nullptr ? heralded->id() : 0;
109 figure.faction_color = ColorOf(content.factions, figure.faction);
110 figure.quests.clear();
111 for (
const auto& [
id, quest] : content.quests) {
112 if (quest.giver() == npc.kind()) {
113 figure.quests.push_back(
id);
116 figure.position = {npc.x(), 0.0f, npc.z()};
117 figure.yaw = npc.yaw();
118 figure.to_time += seconds;
125 const uint64_t kind = figure.
monster ? 1 : (figure.
npc ? 2 : 0);
126 return kind << 32 | figure.
id;
130 const protocol::Looks& looks) {
132 if (!figures.skins().empty()) {
134 figures.skins(
static_cast<int>(looks.skin() % figures.skins_size()));
136 if (!figures.hair_colors().empty()) {
138 static_cast<int>(looks.hair_color() % figures.hair_colors_size()));
140 if (!figures.hairs().empty()) {
141 features.
hair =
static_cast<int>(looks.hair() % figures.hairs_size());
143 features.
build = looks.build();
144 if (looks.has_shirt()) {
145 features.
shirt = looks.shirt();
152 std::map<uint32_t, Figure> players;
153 std::map<uint32_t, Figure> monsters;
154 std::map<uint32_t, Figure> npcs;
155 std::vector<Figure> figures;
156 for (
const protocol::Player& player : world.players()) {
158 Follow(players_, player, player.skill(), player.casts(), seconds);
159 figure.
name = player.name();
160 figure.
guild = player.guild();
161 figure.
faction = player.faction();
162 figure.
faction_color = ColorOf(content_.factions, player.faction());
163 figure.
you = player.id() == world.you();
164 figure.
weapon = player.weapon();
165 figure.
wearing = {player.wearing().begin(), player.wearing().end()};
166 figure.
target = player.target();
170 ? std::optional(
FeaturesOf(content_.figures, player.looks()))
172 players[player.id()] = figure;
173 figures.push_back(std::move(figure));
175 for (
const protocol::Monster& monster : world.monsters()) {
176 Figure figure = Follow(monsters_, monster, 0, 0, seconds);
178 figure.
kind = monster.kind();
179 figure.
phase = monster.phase();
180 const auto kind = content_.monsters.kinds.find(monster.kind());
181 if (kind != content_.monsters.kinds.end()) {
183 const monsters::Look& look = kind->second.look();
184 figure.
color = look.has_tint() ? GetColor(look.tint()) : WHITE;
185 figure.
height = look.height();
187 monsters[monster.id()] = figure;
188 figures.push_back(std::move(figure));
190 for (
const protocol::Npc& npc : world.npcs()) {
191 Figure figure = Stand(npcs_, npc, content_, seconds);
192 npcs[npc.id()] = figure;
193 figures.push_back(std::move(figure));
196 players_ = std::move(players);
197 monsters_ = std::move(monsters);
198 npcs_ = std::move(npcs);
static constexpr float kBlendTime
static constexpr float kFollowRate
std::vector< Figure > Update(const protocol::World &world, float seconds)
static constexpr float kTurnRate
void Turn(const Bend &bend, const Joints &joints, float metre, const ModelSkeleton &skeleton, Transform *pose)
const factions::Faction * HeraldOf(const std::map< uint32_t, factions::Faction > &factions, uint32_t kind)
uint64_t Who(const Figure &figure)
Features FeaturesOf(const figures::Figures &figures, uint32_t id)
const char * NameOf(const items::Item &item)
std::optional< uint32_t > shirt
std::optional< uint32_t > build
uint32_t skin
As 0xRRGGBBAA: white, when there are none to pick from.
The kinds of NPC, by id, and where each NPC stands.