nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
animator.cc
Go to the documentation of this file.
1#include "client/animator.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <cmath>
7#include <map>
8#include <numbers>
9#include <type_traits>
10#include <utility>
11#include <vector>
12
13#include "client/names.h"
14
15namespace nanommo {
16
17namespace {
18
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);
22}
23
24// A monster uses no skills, so its skill is zero.
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
34 ? found->second
35 : Figure{.position = target,
36 .yaw = unit.yaw(),
37 .from = unit.motion(),
38 .from_skill = skill,
39 .to = unit.motion(),
40 .to_skill = skill};
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());
45
46 figure.from_time += seconds;
47 figure.to_time += seconds;
48 // One skill straight after another, or after itself, is a new clip too.
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;
57 figure.blend = 0.0f;
58 }
59 figure.casts = casts;
60 figure.blend = std::min(figure.blend + seconds / Animator::kBlendTime, 1.0f);
61
62 // Where the world puts them, which the interpolation has made smooth: an
63 // easing on top would only draw them further behind.
64 figure.position = target;
65 const float follow = 1.0f - std::exp(-Animator::kFollowRate * seconds);
66 if constexpr (std::is_same_v<Unit, protocol::Monster>) {
67 figure.lift = Lerp(figure.lift, unit.height(), follow);
68 }
69 figure.yaw = Turn(figure.yaw, unit.yaw(), Animator::kTurnRate * seconds);
70 return figure;
71}
72
73// The faction's colour, or none for no faction, or one this client's content
74// hasn't got.
75Color ColorOf(const std::map<uint32_t, factions::Faction>& factions,
76 uint32_t faction) {
77 const auto found = factions.find(faction);
78 return found != factions.end() ? GetColor(found->second.color()) : BLANK;
79}
80
81// NPCs stand still, so each is where the server says, and only its clip's time
82// goes on.
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{};
88 figure.id = npc.id();
89 figure.npc = true;
90 figure.kind = npc.kind();
91 // A kind from a newer server stays nameless, and undressed.
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()};
105 }
106 // A herald, of their faction.
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);
114 }
115 }
116 figure.position = {npc.x(), 0.0f, npc.z()};
117 figure.yaw = npc.yaw();
118 figure.to_time += seconds;
119 return figure;
120}
121
122} // namespace
123
124uint64_t Who(const Figure& figure) {
125 const uint64_t kind = figure.monster ? 1 : (figure.npc ? 2 : 0);
126 return kind << 32 | figure.id;
127}
128
129Features FeaturesOf(const figures::Figures& figures,
130 const protocol::Looks& looks) {
131 Features features;
132 if (!figures.skins().empty()) {
133 features.skin =
134 figures.skins(static_cast<int>(looks.skin() % figures.skins_size()));
135 }
136 if (!figures.hair_colors().empty()) {
137 features.hair_color = figures.hair_colors(
138 static_cast<int>(looks.hair_color() % figures.hair_colors_size()));
139 }
140 if (!figures.hairs().empty()) {
141 features.hair = static_cast<int>(looks.hair() % figures.hairs_size());
142 }
143 features.build = looks.build();
144 if (looks.has_shirt()) {
145 features.shirt = looks.shirt();
146 }
147 return features;
148}
149
150std::vector<Figure> Animator::Update(const protocol::World& world,
151 float seconds) {
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()) {
157 Figure figure =
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();
167 figure.target_player = player.target_player();
168 figure.features =
169 player.has_looks()
170 ? std::optional(FeaturesOf(content_.figures, player.looks()))
171 : std::nullopt;
172 players[player.id()] = figure;
173 figures.push_back(std::move(figure));
174 }
175 for (const protocol::Monster& monster : world.monsters()) {
176 Figure figure = Follow(monsters_, monster, 0, 0, seconds);
177 figure.monster = true;
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()) {
182 figure.name = NameOf(kind->second);
183 const monsters::Look& look = kind->second.look();
184 figure.color = look.has_tint() ? GetColor(look.tint()) : WHITE;
185 figure.height = look.height();
186 }
187 monsters[monster.id()] = figure;
188 figures.push_back(std::move(figure));
189 }
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));
194 }
195 // Players who left, and monsters and NPCs that went, drop out here.
196 players_ = std::move(players);
197 monsters_ = std::move(monsters);
198 npcs_ = std::move(npcs);
199 return figures;
200}
201
202} // namespace nanommo
static constexpr float kBlendTime
Definition animator.h:104
static constexpr float kFollowRate
Definition animator.h:108
std::vector< Figure > Update(const protocol::World &world, float seconds)
Definition animator.cc:150
static constexpr float kTurnRate
Definition animator.h:110
Definition chat.cc:9
void Turn(const Bend &bend, const Joints &joints, float metre, const ModelSkeleton &skeleton, Transform *pose)
Definition moves.cc:76
const factions::Faction * HeraldOf(const std::map< uint32_t, factions::Faction > &factions, uint32_t kind)
Definition factions.cc:46
uint64_t Who(const Figure &figure)
Definition animator.cc:124
Features FeaturesOf(const figures::Figures &figures, uint32_t id)
Definition figures.cc:74
const char * NameOf(const items::Item &item)
Definition names.cc:14
std::optional< uint32_t > shirt
Definition figures.h:40
uint32_t hair_color
Definition figures.h:31
std::optional< uint32_t > build
Definition figures.h:37
uint32_t skin
As 0xRRGGBBAA: white, when there are none to pick from.
Definition figures.h:30
uint32_t id
Definition animator.h:21
uint32_t target_player
Definition animator.h:62
uint32_t kind
A monster's or an NPC's kind, by its id in the content folder.
Definition animator.h:33
Color color
A monster's tint, or the colour of what an NPC wears that's dyed.
Definition animator.h:35
std::string name
Definition animator.h:22
Color faction_color
Definition animator.h:28
uint32_t target
Definition animator.h:61
std::string guild
A player's guild, by its name, or empty.
Definition animator.h:24
float height
A monster's height, in metres, from its kind's look.
Definition animator.h:54
uint32_t phase
A boss's phase, from one, or 0 for any other monster.
Definition animator.h:52
uint32_t weapon
What a player holds, by its id in the items file.
Definition animator.h:56
protocol::Motion to
Definition animator.h:77
std::vector< uint32_t > wearing
What else a player wears, by its ids in the items file.
Definition animator.h:58
std::optional< Features > features
Definition animator.h:38
uint32_t faction
Definition animator.h:27
The kinds of NPC, by id, and where each NPC stands.
Definition npcs.h:20