nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
soundscape.cc
Go to the documentation of this file.
1#include "client/soundscape.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <cmath>
7#include <cstddef>
8#include <map>
9#include <utility>
10#include <vector>
11
12namespace nanommo {
13
14namespace {
15
16// Even far to one side, a little reaches the other ear.
17constexpr float kWidestPan = 0.8f;
18
19} // namespace
20
21Heard Hear(Vector3 source, Vector3 ears, Vector3 right) {
22 const Vector3 away = {source.x - ears.x, 0.0f, source.z - ears.z};
23 const float distance = Vector3Length(away);
24 if (distance >= kFar) {
25 return {.volume = 0.0f, .pan = 0.0f};
26 }
27 // Sound spreads out as it goes, so past near, it falls as one over the
28 // distance, and it fades out on the way to far.
29 const float volume = std::min(1.0f, kNear / distance) *
30 std::min(1.0f, (kFar - distance) / (kFar - kNear));
31 // What's right on top of us comes from both sides.
32 const float side =
33 distance > 0.0f ? Vector3DotProduct(away, right) / distance : 0.0f;
34 return {.volume = volume,
35 .pan = kWidestPan * side * std::min(distance, 1.0f)};
36}
37
38Variety::Variety(uint32_t seed, float pitch_spread, float volume_spread)
39 : noise_(seed),
40 pitch_spread_(pitch_spread),
41 volume_spread_(volume_spread) {}
42
44 int take = 0;
45 if (takes > 1) {
46 // One of the others: a step on from the last, by one to all but one.
47 const int skip =
48 1 + static_cast<int>(noise_.Uniform() * static_cast<float>(takes - 1));
49 take = last_ < 0 ? skip % takes : (last_ + skip) % takes;
50 }
51 last_ = take;
52 return {.take = take,
53 .pitch = 1.0f + noise_.Between(-pitch_spread_, pitch_spread_),
54 .volume = 1.0f - noise_.Between(0.0f, volume_spread_)};
55}
56
57std::array<float, 2> Footfalls(const ModelAnimation& clip, int left,
58 int right) {
59 std::array<float, 2> footfalls{0.0f, 0.5f};
60 // The last frame is the first again.
61 const int frames = clip.keyframeCount - 1;
62 const int bones = static_cast<int>(clip.boneCount);
63 if (frames < 2 || left < 0 || right < 0 || left >= bones || right >= bones) {
64 return footfalls;
65 }
66 const std::array<int, 2> feet = {left, right};
67 for (std::size_t foot = 0; foot < feet.size(); ++foot) {
68 const auto height = [&](int frame) {
69 return clip.keyframePoses[frame][feet[foot]].translation.y;
70 };
71 float low = height(0);
72 float high = height(0);
73 for (int frame = 1; frame < frames; ++frame) {
74 low = std::min(low, height(frame));
75 high = std::max(high, height(frame));
76 }
77 // Down is within a sixth of the way up from its lowest, and it comes
78 // down on the frame it gets there.
79 const float down = low + (high - low) / 6.0f;
80 for (int frame = 0; frame < frames; ++frame) {
81 const int before = (frame + frames - 1) % frames;
82 if (height(frame) <= down && height(before) > down) {
83 footfalls[foot] =
84 static_cast<float>(frame) / static_cast<float>(frames);
85 break;
86 }
87 }
88 }
89 return footfalls;
90}
91
92std::vector<uint64_t> Footsteps::Update(const std::map<uint64_t, Gait>& gaits) {
93 std::vector<uint64_t> stepped;
94 std::map<uint64_t, float> loops;
95 for (const auto& [who, gait] : gaits) {
96 loops[who] = gait.loops;
97 const auto last = loops_.find(who);
98 // A walk that's just started, or started over as a run, has no steps yet.
99 if (last == loops_.end() || gait.loops < last->second) {
100 continue;
101 }
102 for (const float footfall : gait.footfalls) {
103 // Whether the foot's moment came round again since the last update.
104 if (std::floor(gait.loops - footfall) >
105 std::floor(last->second - footfall)) {
106 stepped.push_back(who);
107 }
108 }
109 }
110 loops_ = std::move(loops);
111 return stepped;
112}
113
114std::vector<Figure> Casts::Update(const std::vector<Figure>& figures) {
115 std::vector<Figure> started;
116 std::map<uint32_t, std::pair<uint32_t, float>> skills;
117 for (const Figure& figure : figures) {
118 if (figure.monster || figure.npc || figure.to != protocol::MOTION_SKILL) {
119 continue;
120 }
121 skills[figure.id] = {figure.to_skill, figure.to_time};
122 const auto last = skills_.find(figure.id);
123 const bool fresh = last == skills_.end() ||
124 last->second.first != figure.to_skill ||
125 figure.to_time < last->second.second;
126 if (fresh) {
127 started.push_back(figure);
128 }
129 }
130 skills_ = std::move(skills);
131 return started;
132}
133
134Passage::Cue Passage::Update(bool covering, bool in_world) {
135 Cue cue = Cue::kNothing;
136 if (covering && !covering_ && in_world_) {
137 cue = Cue::kDeparture;
138 } else if (!covering && covering_ && in_world) {
139 cue = Cue::kArrival;
140 }
141 covering_ = covering;
142 in_world_ = in_world;
143 return cue;
144}
145
146} // namespace nanommo
std::vector< Figure > Update(const std::vector< Figure > &figures)
std::vector< uint64_t > Update(const std::map< uint64_t, Gait > &gaits)
Definition soundscape.cc:92
Cue Update(bool covering, bool in_world)
Variety(uint32_t seed, float pitch_spread, float volume_spread)
Definition soundscape.cc:38
Take Next(int takes)
One of that many takes.
Definition soundscape.cc:43
Definition chat.cc:9
constexpr float kNear
In metres: full volume this near, and nothing past far.
Definition soundscape.h:26
std::array< float, 2 > Footfalls(const ModelAnimation &clip, int left, int right)
Definition soundscape.cc:57
Heard Hear(Vector3 source, Vector3 ears, Vector3 right)
Definition soundscape.cc:21
constexpr float kFar
Definition soundscape.h:27