nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
fx.cc
Go to the documentation of this file.
1#include "client/fx.h"
2
3#include <raymath.h>
4#include <rlgl.h>
5
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <utility>
10#include <vector>
11
12#include "client/glsl.h"
13#include "client/picking.h"
14#include "client/triangles.h"
15
16namespace nanommo {
17
18namespace {
19
20constexpr const char* kLightVertexShader = R"(#version 330
21in vec3 vertexPosition;
22in vec4 vertexColor;
23uniform mat4 mvp;
24out vec4 color;
25out float depth;
26
27void main() {
28 color = vertexColor;
29 gl_Position = mvp * vec4(vertexPosition, 1.0);
30 depth = gl_Position.w;
31}
32)";
33
34// Light is brighter than white, so that it blooms, and fades into the fog.
35constexpr const char* kLightFragmentShader = R"(#version 330
36in vec4 color;
37in float depth;
38uniform float fogNear;
39uniform float fogFar;
40out vec4 pixel;
41
42const float kBright = 3.0;
43
44void main() {
45 float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0);
46 pixel = vec4(color.rgb * kBright, color.a * (1.0 - fog));
47}
48)";
49
50// How much of the blade's path a trail shows, in seconds.
51constexpr float kTrail = 0.16f;
52constexpr float kGravity = 9.8f;
53constexpr Vector3 kUp{0.0f, 1.0f, 0.0f};
54// Where a ball of light is, above someone's feet: about their middle.
55constexpr float kMiddle = 0.9f;
56
57// The same number in [0, 1) for the same four, every time, so that a spark
58// flies the same way in every frame.
59float Random(uint32_t seed, uint32_t fx, uint32_t i, uint32_t what) {
60 uint32_t h = seed * 0x9E3779B1u ^ (fx + 1) * 0x85EBCA77u ^
61 (i + 1) * 0xC2B2AE3Du ^ (what + 1) * 0x27D4EB2Fu;
62 h ^= h >> 15;
63 h *= 0x2C1B3C6Du;
64 h ^= h >> 12;
65 h *= 0x297A2D39u;
66 h ^= h >> 15;
67 return static_cast<float>(h >> 8) / 16777216.0f;
68}
69
70float EaseOut(float t) { return 1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t); }
71
72// Past the end, and back.
73float Overshoot(float t) {
74 constexpr float kBack = 1.7f;
75 const float s = t - 1.0f;
76 return 1.0f + s * s * ((kBack + 1.0f) * s + kBack);
77}
78
79// Towards white as it's hotter, from 0 to 1.
80Color Hot(Color color, float heat) {
81 return ColorLerp(color, WHITE, std::clamp(heat, 0.0f, 1.0f));
82}
83
84void Vertex(Vector3 at, Color color, float alpha) {
85 rlColor4ub(color.r, color.g, color.b,
86 static_cast<unsigned char>(std::clamp(alpha, 0.0f, 1.0f) *
87 static_cast<float>(color.a)));
88 rlVertex3f(at.x, at.y, at.z);
89}
90
91// Two triangles from a to b on one side, and c to d on the other.
92void Quad(Vector3 a, Color a_color, float a_alpha, Vector3 b, Color b_color,
93 float b_alpha, Vector3 c, Color c_color, float c_alpha, Vector3 d,
94 Color d_color, float d_alpha) {
95 Vertex(a, a_color, a_alpha);
96 Vertex(b, b_color, b_alpha);
97 Vertex(d, d_color, d_alpha);
98 Vertex(a, a_color, a_alpha);
99 Vertex(d, d_color, d_alpha);
100 Vertex(c, c_color, c_alpha);
101}
102
103// A line from a to b, as wide as `width`, turned to face the camera, bright
104// down its middle.
105void Streak(Vector3 a, Vector3 b, float width, Color color, float alpha,
106 Vector3 eye) {
107 const Vector3 side =
108 Vector3Scale(Vector3Normalize(Vector3CrossProduct(
109 Vector3Subtract(b, a), Vector3Subtract(eye, a))),
110 width / 2.0f);
111 Quad(Vector3Subtract(a, side), color, 0.0f, Vector3Subtract(b, side), color,
112 0.0f, a, color, alpha, b, color, alpha);
113 Quad(a, color, alpha, b, color, alpha, Vector3Add(a, side), color, 0.0f,
114 Vector3Add(b, side), color, 0.0f);
115}
116
117// A round patch of light facing the camera: a bright middle, which blooms,
118// fading fast to nothing at its edge.
119void Blob(Vector3 at, float radius, Color color, float alpha,
120 const Camera3D& camera) {
121 constexpr int kSides = 20;
122 constexpr float kCore = 0.35f;
123 const Vector3 forward =
124 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
125 const Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, kUp));
126 const Vector3 up = Vector3CrossProduct(right, forward);
127 auto edge = [&](int j, float out) {
128 const float turn = 2.0f * PI * static_cast<float>(j) / kSides;
129 return Vector3Add(
130 at, Vector3Add(Vector3Scale(right, out * radius * std::cos(turn)),
131 Vector3Scale(up, out * radius * std::sin(turn))));
132 };
133 const Color hot = Hot(color, 0.7f);
134 for (int i = 0; i < kSides; ++i) {
135 Vertex(at, hot, 0.85f * alpha);
136 Vertex(edge(i, kCore), color, 0.55f * alpha);
137 Vertex(edge(i + 1, kCore), color, 0.55f * alpha);
138 Quad(edge(i, kCore), color, 0.55f * alpha, edge(i + 1, kCore), color,
139 0.55f * alpha, edge(i, 1.0f), color, 0.0f, edge(i + 1, 1.0f), color,
140 0.0f);
141 }
142}
143
144// On the ground, at a distance and a turn from somewhere.
145Vector3 Around(const Terrain& terrain, Vector3 at, float distance, float turn,
146 float above) {
147 const float x = at.x + distance * std::sin(turn);
148 const float z = at.z + distance * std::cos(turn);
149 return {x, terrain.Height(x, z) + above, z};
150}
151
152// A gem: five sides round its middle, coming to a point at the top and at
153// the bottom, a unit tall.
154Mesh Gem() {
155 Triangles gem;
156 constexpr int kSides = 5;
157 const Vector3 top{0.0f, 0.5f, 0.0f};
158 const Vector3 bottom{0.0f, -0.5f, 0.0f};
159 for (int i = 0; i < kSides; ++i) {
160 auto corner = [](int j) {
161 const float turn = 2.0f * PI * static_cast<float>(j) / kSides;
162 return Vector3{0.5f * std::sin(turn), 0.0f, 0.5f * std::cos(turn)};
163 };
164 gem.Add(corner(i), corner(i + 1), top, WHITE);
165 gem.Add(corner(i + 1), corner(i), bottom, WHITE);
166 }
167 return gem.Upload();
168}
169
170} // namespace
171
172Fx::Fx(const SkillTable& skills, Shader lit, const ItemTable& items)
173 : skills_(skills.by_id),
174 light_(LoadGlsl(kLightVertexShader, kLightFragmentShader)),
175 shard_(LoadModelFromMesh(Gem())) {
176 shard_.materials[0].shader = lit;
177 for (const auto& [id, item] : items.by_id) {
178 if (item.has_weapon()) {
179 weapons_[id] = item.weapon();
180 }
181 }
182 for (const auto& [id, skill] : skills_) {
183 float end = 0.0f;
184 for (const skills::Fx& fx : skill.look().fx()) {
185 // A trail fades for a moment after the blade's stopped.
186 end = std::max(
187 end,
188 static_cast<float>(fx.from_ms() + fx.for_ms()) / 1000.0f + kTrail);
189 }
190 ends_[id] = end;
191 }
192}
193
195 UnloadModel(shard_);
196 UnloadShader(light_);
197}
198
199void Fx::Update(const std::vector<Figure>& figures, float seconds) {
200 for (auto& [id, cast] : casts_) {
201 cast.clock += seconds;
202 }
203 for (const Figure& figure : figures) {
204 const auto skill = skills_.find(figure.to_skill);
205 if (figure.monster || figure.npc || figure.to != protocol::MOTION_SKILL ||
206 skill == skills_.end()) {
207 continue;
208 }
209 auto [found, fresh] = casts_.try_emplace(figure.id);
210 Cast& cast = found->second;
211 // Another skill, or the same one again, is a cast of its own.
212 if (fresh || cast.figure.to_skill != figure.to_skill ||
213 figure.to_time < cast.clock - seconds) {
214 cast = Cast{.seed = ++seeds_};
215 }
216 cast.figure = figure;
217 cast.clock = figure.to_time;
218 // A monster the skill kills is let go of, so the cast keeps where it
219 // fell, and the rest of its fx still land there.
220 for (const Figure& other : figures) {
221 if (Picked(other, figure.target, figure.target_player)) {
222 cast.target = other.position;
223 }
224 }
225 }
226 for (auto& [id, cast] : casts_) {
227 const skills::Skill& skill = skills_.at(cast.figure.to_skill);
228 // Each blow, as the cast comes to it, lands or doesn't, for good.
229 const std::vector<Beat> blows = BlowsOf(skill, cast.figure);
230 while (cast.landed.size() < blows.size() &&
231 cast.clock >= blows[cast.landed.size()].at) {
232 cast.landed.push_back(Lands(cast, skill, figures));
233 }
234 cast.time = cast.clock - Lag(cast);
235 cast.figure.to_time = cast.time;
236 if (cast.path.empty() || cast.path.back().first < cast.time) {
237 cast.path.emplace_back(cast.time, cast.figure.position);
238 }
239 while (cast.path.size() > 2 &&
240 cast.path[1].first < cast.time - kGhostGap * 10.0f) {
241 cast.path.pop_front();
242 }
243 }
244 std::erase_if(casts_, [&](const auto& entry) {
245 const Cast& cast = entry.second;
246 return cast.time > ends_.at(cast.figure.to_skill);
247 });
248}
249
250// Half as long again for each metre a blow pushes or throws.
251std::vector<Fx::Beat> Fx::BlowsOf(const skills::Skill& skill,
252 const Figure& figure) const {
253 static constexpr float kHeaviest = 2.5f;
254 auto weight = [](float push, float lift) {
255 return std::min(1.0f + 0.5f * (push + std::abs(lift)), kHeaviest);
256 };
257 const skills::Effect& effect = skill.effect();
258 std::vector<Beat> blows;
259 for (const skills::Blow& blow : effect.blows()) {
260 blows.push_back({.at = static_cast<float>(blow.at_ms()) / 1000.0f,
261 .weight = weight(blow.push(), blow.lift())});
262 }
263 if (blows.empty()) {
264 const auto weapon = weapons_.find(figure.weapon);
265 // A quick swing of the weapon lands as much sooner as it's quicker.
266 const float pace = effect.pace() > 0.0f ? effect.pace() : 1.0f;
267 const float hit = effect.weapon() && weapon != weapons_.end()
268 ? pace * static_cast<float>(weapon->second.hit_ms())
269 : static_cast<float>(effect.hit_ms());
270 blows.push_back(
271 {.at = hit / 1000.0f, .weight = weight(effect.push(), 0.0f)});
272 }
273 return blows;
274}
275
276// Each blow that's landed holds it back while it's still, and then less and
277// less as it catches up.
278float Fx::Lag(const Cast& cast) const {
279 const skills::Skill& skill = skills_.at(cast.figure.to_skill);
280 const float still = static_cast<float>(skill.look().stop_ms()) / 1000.0f;
281 if (still <= 0.0f) {
282 return 0.0f;
283 }
284 const std::vector<Beat> blows = BlowsOf(skill, cast.figure);
285 float lag = 0.0f;
286 for (std::size_t i = 0; i < cast.landed.size(); ++i) {
287 const float since = cast.clock - blows[i].at;
288 const float held = still * blows[i].weight;
289 if (!cast.landed[i] || since < 0.0f) {
290 continue;
291 }
292 const float most = (1.0f - kStill) * held;
293 if (since < held) {
294 lag += (1.0f - kStill) * since;
295 } else if (since < held * (1.0f + kCatchUp)) {
296 lag += most * (1.0f - (since - held) / (held * kCatchUp));
297 }
298 }
299 return lag;
300}
301
302bool Fx::Lands(Cast& cast, const skills::Skill& skill,
303 const std::vector<Figure>& figures) const {
304 // A little further than the server's reach, as we see everyone a moment
305 // late.
306 constexpr float kSlack = 0.5f;
307 const skills::Effect& effect = skill.effect();
308 const Figure& player = cast.figure;
309 const auto weapon = weapons_.find(player.weapon);
310 const float reach =
311 (effect.weapon() && weapon != weapons_.end() ? weapon->second.reach()
312 : effect.reach()) +
313 kSlack;
314 if (effect.damage() == 0 && !effect.weapon()) {
315 return false;
316 }
317 bool landed = false;
318 for (const Figure& other : figures) {
319 if (!other.monster || other.to == protocol::MOTION_DEAD ||
320 (!effect.all() && other.id != player.target)) {
321 continue;
322 }
323 const float dx = other.position.x - player.position.x;
324 const float dz = other.position.z - player.position.z;
325 if (std::hypot(dx, dz) > reach) {
326 continue;
327 }
328 if (effect.arc() > 0.0f) {
329 const float off =
330 std::abs(std::remainder(std::atan2(dx, dz) - player.yaw, 2.0f * PI));
331 if (off > effect.arc() * DEG2RAD / 2.0f) {
332 continue;
333 }
334 }
335 cast.struck.insert(other.id);
336 landed = true;
337 }
338 return landed;
339}
340
341void Fx::Hold(std::vector<Figure>& figures) const {
342 for (Figure& figure : figures) {
343 float lag = 0.0f;
344 if (figure.monster) {
345 for (const auto& [id, cast] : casts_) {
346 if (cast.struck.contains(figure.id)) {
347 lag = std::max(lag, cast.clock - cast.time);
348 }
349 }
350 } else if (const auto cast = casts_.find(figure.id);
351 cast != casts_.end() && figure.to == protocol::MOTION_SKILL) {
352 lag = cast->second.clock - cast->second.time;
353 }
354 // Never back past where its clips started, as one that's just changed
355 // is hardly into its own.
356 figure.from_time = std::max(figure.from_time - lag, 0.0f);
357 figure.to_time = std::max(figure.to_time - lag, 0.0f);
358 }
359}
360
361namespace {
362
363// One fx of a cast, as far as it's got.
364struct Moment {
365 const skills::Fx& fx;
366 /// Where it is.
367 Vector3 at;
368 /// Which way the player faces.
369 float yaw;
370 /// Seconds since it started, and how long it lasts.
371 float age;
372 float span;
373 uint32_t seed;
374 uint32_t index;
375 Color color;
376
377 float done() const { return age / span; }
378 float Random(uint32_t i, uint32_t what) const {
379 return nanommo::Random(seed, index, i, what);
380 }
381};
382
383// The blade, posed again at moments just gone, which makes a ribbon through
384// where it's been, brightest at its tip.
385void DrawTrail(const Figure& figure, float time, float from, float span,
386 Color color, const Characters& characters) {
387 constexpr int kSteps = 16;
388 const float newest = std::min(time, from + span);
389 const float oldest = std::max(from, time - kTrail);
390 if (newest <= oldest) {
391 return;
392 }
393 Figure posed = figure;
394 posed.blend = 1.0f;
395 std::array<std::pair<Vector3, Vector3>, kSteps + 1> blade{};
396 std::array<float, kSteps + 1> fade{};
397 for (int i = 0; i <= kSteps; ++i) {
398 posed.to_time = oldest + (newest - oldest) * static_cast<float>(i) / kSteps;
399 const auto found = characters.Blade(posed);
400 if (!found) {
401 return;
402 }
403 blade[i] = *found;
404 fade[i] = std::pow(1.0f - (time - posed.to_time) / kTrail, 2.0f);
405 }
406 rlBegin(RL_TRIANGLES);
407 for (int i = 0; i < kSteps; ++i) {
408 const auto& [hilt, tip] = blade[i];
409 const auto& [next_hilt, next_tip] = blade[i + 1];
410 const Vector3 middle = Vector3Lerp(hilt, tip, 0.6f);
411 const Vector3 next_middle = Vector3Lerp(next_hilt, next_tip, 0.6f);
412 // From nothing at the hilt, to half way along, to bright at the tip.
413 Quad(hilt, color, 0.0f, next_hilt, color, 0.0f, middle, color,
414 0.5f * fade[i], next_middle, color, 0.5f * fade[i + 1]);
415 Quad(middle, color, 0.5f * fade[i], next_middle, color, 0.5f * fade[i + 1],
416 tip, Hot(color, fade[i]), fade[i], next_tip, Hot(color, fade[i + 1]),
417 fade[i + 1]);
418 }
419 rlEnd();
420}
421
422void DrawRing(const Moment& m, const Terrain& terrain) {
423 constexpr int kSides = 48;
424 constexpr float kRim = 0.1f;
425 const float u = m.done();
426 const float radius = std::max(m.fx.size() * EaseOut(u), 0.2f);
427 const float width = std::min(0.3f + 0.6f * (1.0f - u), radius);
428 const float fade = std::pow(1.0f - u, 1.5f);
429 const float wall = 0.4f * (1.0f - u);
430 const Color hot = Hot(m.color, 0.6f * (1.0f - u));
431 for (int i = 0; i < kSides; ++i) {
432 const float turn = 2.0f * PI * static_cast<float>(i) / kSides;
433 const float next = 2.0f * PI * static_cast<float>(i + 1) / kSides;
434 auto at = [&](float distance, float angle) {
435 return Around(terrain, m.at, distance, angle, 0.05f);
436 };
437 // On the ground: fading in from inside, to a bright rim.
438 Quad(at(radius - width, turn), m.color, 0.0f, at(radius - width, next),
439 m.color, 0.0f, at(radius - kRim, turn), m.color, 0.35f * fade,
440 at(radius - kRim, next), m.color, 0.35f * fade);
441 Quad(at(radius - kRim, turn), hot, fade, at(radius - kRim, next), hot, fade,
442 at(radius, turn), hot, fade, at(radius, next), hot, fade);
443 // And a low wall on the rim, fading upwards.
444 Quad(at(radius, turn), hot, 0.7f * fade, at(radius, next), hot, 0.7f * fade,
445 Vector3Add(at(radius, turn), Vector3Scale(kUp, wall)), m.color, 0.0f,
446 Vector3Add(at(radius, next), Vector3Scale(kUp, wall)), m.color, 0.0f);
447 }
448}
449
450void DrawSparks(const Moment& m, const Terrain& terrain, Vector3 eye) {
451 for (uint32_t i = 0; i < m.fx.count(); ++i) {
452 const float life = m.span * (0.45f + 0.55f * m.Random(i, 0));
453 const float t = m.age - 0.1f * m.span * m.Random(i, 1);
454 if (t < 0.0f || t > life) {
455 continue;
456 }
457 const float rise = (15.0f + 60.0f * m.Random(i, 2)) * DEG2RAD;
458 const float turn = 2.0f * PI * m.Random(i, 3);
459 const float speed = m.fx.size() / life * (1.0f + 1.5f * m.Random(i, 4));
460 const Vector3 way{std::cos(rise) * std::sin(turn), std::sin(rise),
461 std::cos(rise) * std::cos(turn)};
462 auto where = [&](float at) {
463 return Vector3Add(m.at,
464 Vector3{way.x * speed * at,
465 way.y * speed * at - 0.5f * kGravity * at * at,
466 way.z * speed * at});
467 };
468 const Vector3 head = where(t);
469 if (head.y < terrain.Height(head.x, head.z)) {
470 continue;
471 }
472 const float heat = 1.0f - t / life;
473 Streak(where(std::max(t - 0.05f, 0.0f)), head, 0.03f + 0.04f * heat,
474 Hot(m.color, heat), heat, eye);
475 }
476}
477
478void DrawMotes(const Moment& m, const Camera3D& camera) {
479 for (uint32_t i = 0; i < m.fx.count(); ++i) {
480 const float life = 0.5f * m.span;
481 const float t = m.age - 0.5f * m.span * m.Random(i, 0);
482 if (t < 0.0f || t > life) {
483 continue;
484 }
485 const float spin =
486 (m.Random(i, 1) < 0.5f ? -1.0f : 1.0f) * (2.0f + 2.0f * m.Random(i, 2));
487 const float turn = 2.0f * PI * m.Random(i, 3) + spin * t;
488 const float out = (0.3f + 0.6f * m.Random(i, 4)) * (1.0f - 0.4f * t / life);
489 const Vector3 at = Vector3Add(
490 m.at, Vector3{out * std::sin(turn), 0.1f + m.fx.size() * t / life,
491 out * std::cos(turn)});
492 Blob(at, 0.06f + 0.06f * m.Random(i, 5), m.color, std::sin(PI * t / life),
493 camera);
494 }
495}
496
497void DrawBolt(const Moment& m, Vector3 eye) {
498 constexpr int kSteps = 16;
499 constexpr float kFlicker = 0.07f;
500 // A new path every flicker, as lightning strikes again and again.
501 const uint32_t strike = static_cast<uint32_t>(m.age / kFlicker);
502 const bool lit = std::fmod(m.age, kFlicker) < 0.65f * kFlicker;
503 const float bright = std::sqrt(1.0f - m.done()) * (lit ? 1.0f : 0.35f);
504 auto jag = [&](uint32_t i, uint32_t what) {
505 return m.Random(strike * 101 + i, what) - 0.5f;
506 };
507 const Vector3 sky =
508 Vector3Add(m.at, Vector3{3.0f * jag(0, 0), 16.0f, 3.0f * jag(0, 1)});
509 std::array<Vector3, kSteps + 1> path{};
510 for (int i = 0; i <= kSteps; ++i) {
511 const float along = static_cast<float>(i) / kSteps;
512 const float wander = 1.1f * std::sqrt(std::sin(PI * along));
513 path[i] = Vector3Add(
514 Vector3Lerp(sky, m.at, along),
515 Vector3{wander * jag(i + 1, 2), 0.0f, wander * jag(i + 1, 3)});
516 }
517 for (int i = 0; i < kSteps; ++i) {
518 Streak(path[i], path[i + 1], 0.45f, m.color, 0.7f * bright, eye);
519 Streak(path[i], path[i + 1], 0.12f, WHITE, bright, eye);
520 }
521 // Forks, off the path partway down.
522 for (uint32_t fork = 0; fork < 3; ++fork) {
523 Vector3 at = path[5 + 3 * fork];
524 for (uint32_t i = 0; i < 4; ++i) {
525 const Vector3 next =
526 Vector3Add(at, Vector3{1.6f * jag(20 + fork * 4 + i, 4), -1.1f,
527 1.6f * jag(20 + fork * 4 + i, 5)});
528 Streak(at, next, 0.22f, m.color, 0.5f * bright, eye);
529 Streak(at, next, 0.06f, WHITE, 0.8f * bright, eye);
530 at = next;
531 }
532 }
533}
534
535void DrawWave(const Moment& m) {
536 constexpr int kSteps = 24;
537 constexpr float kHalf = 60.0f * DEG2RAD;
538 const float u = m.done();
539 const float reach = 0.8f + m.fx.size() * u;
540 const float fade = std::min(u / 0.1f, 1.0f) * (1.0f - u * u * u);
541 const Vector3 middle = Vector3Add(m.at, Vector3Scale(kUp, kMiddle));
542 auto at = [&](float turn, float distance, float up) {
543 return Vector3Add(middle, Vector3{distance * std::sin(m.yaw + turn), up,
544 distance * std::cos(m.yaw + turn)});
545 };
546 const Color hot = Hot(m.color, 0.7f);
547 for (int i = 0; i < kSteps; ++i) {
548 const float a = -kHalf + 2.0f * kHalf * static_cast<float>(i) / kSteps;
549 const float b = -kHalf + 2.0f * kHalf * static_cast<float>(i + 1) / kSteps;
550 // Thickest in the middle, and thin at its horns.
551 const float thick_a = 0.55f * std::sqrt(std::cos(a / kHalf * PI / 2.0f));
552 const float thick_b = 0.55f * std::sqrt(std::cos(b / kHalf * PI / 2.0f));
553 // Flat, from nothing behind to bright at the front.
554 Quad(at(a, reach - thick_a, 0.0f), m.color, 0.0f,
555 at(b, reach - thick_b, 0.0f), m.color, 0.0f, at(a, reach, 0.0f), hot,
556 fade, at(b, reach, 0.0f), hot, fade);
557 // And standing, bright along the middle.
558 for (const float side : {-1.0f, 1.0f}) {
559 Quad(at(a, reach, 0.0f), hot, fade, at(b, reach, 0.0f), hot, fade,
560 at(a, reach - 0.3f * thick_a, side * 0.6f * thick_a), m.color, 0.0f,
561 at(b, reach - 0.3f * thick_b, side * 0.6f * thick_b), m.color, 0.0f);
562 }
563 }
564}
565
566// Grows fast and fades slowly.
567void DrawGlow(const Moment& m, const Camera3D& camera, bool lifted) {
568 const float u = m.done();
569 const float radius =
570 m.fx.size() * (0.5f + 0.5f * EaseOut(std::min(3.0f * u, 1.0f)));
571 const float alpha =
572 u < 0.15f ? u / 0.15f : std::pow((1.0f - u) / 0.85f, 2.0f);
573 const Vector3 at =
574 lifted ? Vector3Add(m.at, Vector3Scale(kUp, kMiddle)) : m.at;
575 Blob(at, radius, m.color, alpha, camera);
576}
577
578// The blade lit along its length: a bright core down the middle, flickering
579// a little, and a glow at the tip. It comes on fast and goes slowly.
580void DrawEdge(const Moment& m, const std::pair<Vector3, Vector3>& blade,
581 const Camera3D& camera) {
582 constexpr float kOn = 0.06f;
583 constexpr float kOff = 0.15f;
584 const float left = m.span - m.age;
585 const float fade = std::min({m.age / kOn, left / kOff, 1.0f});
586 const float flicker = 0.85f + 0.15f * std::sin(m.age * 47.0f);
587 const auto& [hilt, tip] = blade;
588 const Vector3 guard = Vector3Lerp(hilt, tip, 0.18f);
589 const float width = m.fx.size() > 0.0f ? m.fx.size() : 0.08f;
590 Streak(guard, tip, 3.0f * width, m.color, 0.35f * fade * flicker,
591 camera.position);
592 Streak(guard, tip, width, Hot(m.color, 0.8f), fade * flicker,
593 camera.position);
594 Blob(tip, 2.5f * width, m.color, 0.6f * fade, camera);
595}
596
597// Cuts across the air, flat to the camera, so that each reads as the stroke
598// that made it: drawn across from one end in a flash, then thinning away.
599void DrawSlash(const Moment& m, const Camera3D& camera) {
600 constexpr int kSteps = 16;
601 constexpr float kDraw = 0.07f;
602 // A cut after the first comes a little later, and slants its own way.
603 constexpr float kStagger = 0.06f;
604 constexpr float kSpread = 50.0f * DEG2RAD;
605 const Vector3 forward =
606 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
607 const Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, kUp));
608 const Vector3 up = Vector3CrossProduct(right, forward);
609 const int count = static_cast<int>(std::max(m.fx.count(), 1u));
610 for (int j = 0; j < count; ++j) {
611 const float age = m.age - kStagger * static_cast<float>(j);
612 const float life = m.span - kStagger * static_cast<float>(count - 1);
613 if (age < 0.0f || age > life) {
614 continue;
615 }
616 const float drawn = std::min(age / kDraw, 1.0f);
617 const float fade = std::pow(1.0f - age / life, 1.5f);
618 const float slant = m.fx.angle() * DEG2RAD +
619 (j == 0 ? 0.0f
620 : (j % 2 == 1 ? -1.0f : 1.0f) * kSpread *
621 (0.6f + 0.4f * m.Random(j, 0)));
622 const Vector3 along = Vector3Add(Vector3Scale(right, std::cos(slant)),
623 Vector3Scale(up, -std::sin(slant)));
624 const Vector3 bow = Vector3CrossProduct(forward, along);
625 const Vector3 middle = Vector3Add(
626 m.at, Vector3Add(Vector3Scale(right, 0.3f * (m.Random(j, 1) - 0.5f)),
627 Vector3Scale(up, 0.3f * (m.Random(j, 2) - 0.5f))));
628 const float length = m.fx.size();
629 // Widest in the middle and sharp at both ends, bowed a little, and
630 // thinner as it fades.
631 const float thick = 0.07f * length * (0.35f + 0.65f * fade);
632 auto point = [&](float u, float side) {
633 const float width = thick * std::sin(PI * u) * side;
634 const float curve = 0.08f * length * std::sin(PI * u);
635 return Vector3Add(middle,
636 Vector3Add(Vector3Scale(along, length * (u - 0.5f)),
637 Vector3Scale(bow, curve + width)));
638 };
639 // Light over the grass takes on its green, so the glow round it is
640 // faint, and the bloom does the rest, round a white-hot middle that's
641 // nearly solid.
642 const Color hot = Hot(m.color, 0.85f);
643 for (int i = 0; i < kSteps; ++i) {
644 const float a = drawn * static_cast<float>(i) / kSteps;
645 const float b = drawn * static_cast<float>(i + 1) / kSteps;
646 Quad(point(a, -2.2f), m.color, 0.0f, point(b, -2.2f), m.color, 0.0f,
647 point(a, 0.0f), m.color, 0.22f * fade, point(b, 0.0f), m.color,
648 0.22f * fade);
649 Quad(point(a, 0.0f), m.color, 0.22f * fade, point(b, 0.0f), m.color,
650 0.22f * fade, point(a, 2.2f), m.color, 0.0f, point(b, 2.2f), m.color,
651 0.0f);
652 Quad(point(a, -0.8f), hot, 0.3f * fade, point(b, -0.8f), hot, 0.3f * fade,
653 point(a, 0.0f), hot, fade, point(b, 0.0f), hot, fade);
654 Quad(point(a, 0.0f), hot, fade, point(b, 0.0f), hot, fade, point(a, 0.8f),
655 hot, 0.3f * fade, point(b, 0.8f), hot, 0.3f * fade);
656 }
657 }
658}
659
660// A sheet of the colour just in front of the camera, over the whole view:
661// at once, then fading.
662void DrawFlash(const Moment& m, const Camera3D& camera) {
663 // The light's three times as bright as its colour, so a sheet of it
664 // would hide the view: a sixth of the colour's alpha does.
665 constexpr float kSheet = 1.0f / 6.0f;
666 const float u = m.done();
667 const float alpha = kSheet * std::pow(1.0f - u, 2.0f);
668 const Vector3 forward =
669 Vector3Normalize(Vector3Subtract(camera.target, camera.position));
670 const Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, kUp));
671 const Vector3 up = Vector3CrossProduct(right, forward);
672 const Vector3 middle =
673 Vector3Add(camera.position, Vector3Scale(forward, 0.5f));
674 // Wider than any view of 45 degrees, at half a metre.
675 const Vector3 across = Vector3Scale(right, 2.0f);
676 const Vector3 high = Vector3Scale(up, 2.0f);
677 Quad(Vector3Subtract(Vector3Subtract(middle, across), high), m.color, alpha,
678 Vector3Subtract(Vector3Add(middle, across), high), m.color, alpha,
679 Vector3Add(Vector3Subtract(middle, across), high), m.color, alpha,
680 Vector3Add(Vector3Add(middle, across), high), m.color, alpha);
681}
682
683} // namespace
684
685void Fx::DrawGhosts(const Characters& characters) const {
686 bool any = false;
687 for (const auto& [id, cast] : casts_) {
688 const skills::Skill& skill = skills_.at(cast.figure.to_skill);
689 for (const skills::Fx& fx : skill.look().fx()) {
690 const float from = static_cast<float>(fx.from_ms()) / 1000.0f;
691 const float span = static_cast<float>(fx.for_ms()) / 1000.0f;
692 if (fx.kind() != skills::Fx::GHOSTS || cast.time < from ||
693 cast.time > from + span + kGhostGap * fx.count()) {
694 continue;
695 }
696 if (!any) {
697 rlDrawRenderBatchActive();
698 rlDisableDepthMask();
699 BeginBlendMode(BLEND_ALPHA);
700 any = true;
701 }
702 const Color color = GetColor(fx.color());
703 // The oldest first, as each goes over the one before.
704 for (int k = static_cast<int>(fx.count()); k >= 1; --k) {
705 const float then = cast.time - kGhostGap * static_cast<float>(k);
706 if (then < from || then > from + span) {
707 continue;
708 }
709 Figure ghost = cast.figure;
710 ghost.to_time = then;
711 ghost.blend = 1.0f;
712 // Where the player was then, between the two places either side.
713 for (std::size_t i = 1; i < cast.path.size(); ++i) {
714 const auto& [t0, p0] = cast.path[i - 1];
715 const auto& [t1, p1] = cast.path[i];
716 if (then <= t1) {
717 ghost.position = Vector3Lerp(
718 p0, p1,
719 t1 > t0 ? std::clamp((then - t0) / (t1 - t0), 0.0f, 1.0f)
720 : 1.0f);
721 break;
722 }
723 }
724 const float fade =
725 1.0f - static_cast<float>(k) / static_cast<float>(fx.count() + 1);
726 characters.DrawGhost(
727 ghost,
728 Fade(color, 0.45f * fade * static_cast<float>(color.a) / 255.0f));
729 }
730 }
731 }
732 if (any) {
733 rlDrawRenderBatchActive();
734 EndBlendMode();
735 rlEnableDepthMask();
736 }
737}
738
739void Fx::DrawSolid(const Terrain& terrain) const {
740 Model piece = shard_;
741 auto draw = [&](Vector3 at, Vector3 axis, float angle, float turn,
742 Vector3 size, Color color) {
743 piece.materials[0].maps[MATERIAL_MAP_DIFFUSE].color = color;
744 const Matrix transform = MatrixMultiply(
745 MatrixMultiply(MatrixMultiply(MatrixScale(size.x, size.y, size.z),
746 MatrixRotateY(turn)),
747 MatrixRotate(axis, angle)),
748 MatrixTranslate(at.x, at.y, at.z));
749 DrawMesh(piece.meshes[0], piece.materials[0], transform);
750 };
751 for (const auto& [id, cast] : casts_) {
752 const skills::Skill& skill = skills_.at(cast.figure.to_skill);
753 for (int index = 0; index < skill.look().fx_size(); ++index) {
754 const skills::Fx& fx = skill.look().fx(index);
755 // Thrown out by a blow, they fly on through its hold.
756 const float age = cast.clock - static_cast<float>(fx.from_ms()) / 1000.0f;
757 const float span = static_cast<float>(fx.for_ms()) / 1000.0f;
758 const bool there = fx.at() != skills::Fx::TARGET || cast.target;
759 if (age < 0.0f || age > span || !there ||
760 (fx.kind() != skills::Fx::SHARDS &&
761 fx.kind() != skills::Fx::SPIKES)) {
762 continue;
763 }
764 const Vector3 at = fx.at() == skills::Fx::TARGET
765 ? cast.target.value_or(cast.figure.position)
766 : cast.figure.position;
767 const Moment m{.fx = fx,
768 .at = {at.x, terrain.Height(at.x, at.z), at.z},
769 .yaw = cast.figure.yaw,
770 .age = age,
771 .span = span,
772 .seed = cast.seed,
773 .index = static_cast<uint32_t>(index),
774 .color = GetColor(fx.color())};
775 for (uint32_t i = 0; i < fx.count(); ++i) {
776 const float turn = 2.0f * PI * m.Random(i, 0);
777 if (fx.kind() == skills::Fx::SHARDS) {
778 // Thrown up and out, until they land, and shrink away at the end.
779 const float rise = (45.0f + 35.0f * m.Random(i, 1)) * DEG2RAD;
780 const float speed =
781 std::sqrt(fx.size() * kGravity) * (0.6f + 0.6f * m.Random(i, 2));
782 const float up = speed * std::sin(rise);
783 const float landed = 2.0f * up / kGravity;
784 const float t = std::min(age, landed);
785 const float out = speed * std::cos(rise) * t;
786 const Vector3 here =
787 Vector3Add(m.at, Vector3{out * std::sin(turn),
788 0.1f + up * t - 0.5f * kGravity * t * t,
789 out * std::cos(turn)});
790 const float size = (0.12f + 0.16f * m.Random(i, 3)) *
791 std::min((span - age) / (0.3f * span), 1.0f);
792 const Vector3 axis = Vector3Normalize(
793 {m.Random(i, 4) - 0.5f, m.Random(i, 5) - 0.5f, 0.3f});
794 draw({here.x, std::max(here.y, terrain.Height(here.x, here.z)),
795 here.z},
796 axis, (6.0f + 10.0f * m.Random(i, 6)) * t, turn,
797 {size, 1.4f * size, size}, m.color);
798 } else {
799 // Out of the ground, nearest first, leaning outwards, and back
800 // into it at the end.
801 const float distance =
802 fx.size() * std::sqrt(0.06f + 0.94f * m.Random(i, 1));
803 const float t = age - 0.35f * span * distance / fx.size();
804 if (t < 0.0f) {
805 continue;
806 }
807 const float grow = Overshoot(std::min(t / 0.12f, 1.0f)) *
808 std::min((span - age) / (0.25f * span), 1.0f);
809 const float height = (0.8f + 1.0f * m.Random(i, 2)) *
810 (1.0f - 0.4f * distance / fx.size());
811 const float lean = (15.0f + 20.0f * m.Random(i, 3)) * DEG2RAD;
812 const Vector3 axis{std::cos(turn), 0.0f, -std::sin(turn)};
813 const Vector3 up = Vector3RotateByAxisAngle(kUp, axis, lean);
814 const Vector3 base = Around(terrain, m.at, distance, turn, 0.0f);
815 const float width = 0.3f + 0.25f * m.Random(i, 4);
816 draw(Vector3Add(base, Vector3Scale(up, height * (grow - 0.7f))), axis,
817 lean, 2.0f * PI * m.Random(i, 5), {width, 1.4f * height, width},
818 m.color);
819 }
820 }
821 }
822 }
823}
824
825void Fx::DrawLight(const Camera3D& camera, const Terrain& terrain,
826 const Atmosphere& atmosphere,
827 const Characters& characters) const {
828 if (casts_.empty()) {
829 return;
830 }
831 SetShaderValue(light_, GetShaderLocation(light_, "fogNear"),
832 &atmosphere.fog_near, SHADER_UNIFORM_FLOAT);
833 SetShaderValue(light_, GetShaderLocation(light_, "fogFar"),
834 &atmosphere.fog_far, SHADER_UNIFORM_FLOAT);
835 // Light goes over what's behind it, in its own colour, so it's drawn last,
836 // without hiding anything, and from both sides.
837 rlDrawRenderBatchActive();
838 rlDisableBackfaceCulling();
839 rlDisableDepthMask();
840 BeginBlendMode(BLEND_ALPHA);
841 BeginShaderMode(light_);
842 for (const auto& [id, cast] : casts_) {
843 const skills::Skill& skill = skills_.at(cast.figure.to_skill);
844 for (int index = 0; index < skill.look().fx_size(); ++index) {
845 const skills::Fx& fx = skill.look().fx(index);
846 const float from = static_cast<float>(fx.from_ms()) / 1000.0f;
847 const float span = static_cast<float>(fx.for_ms()) / 1000.0f;
848 // What goes with the body is held back with it, while what a blow
849 // throws out, such as its sparks, flies through the hold.
850 const bool held = fx.kind() == skills::Fx::TRAIL ||
851 fx.kind() == skills::Fx::EDGE ||
852 fx.at() == skills::Fx::BLADE;
853 const float age = (held ? cast.time : cast.clock) - from;
854 const Color color = GetColor(fx.color());
855 if (fx.kind() == skills::Fx::TRAIL) {
856 DrawTrail(cast.figure, cast.time, from, span, color, characters);
857 continue;
858 }
859 if (age < 0.0f || age > span) {
860 continue;
861 }
862 Vector3 at = cast.figure.position;
863 if (fx.at() == skills::Fx::TARGET) {
864 // Without a monster picked, there's nothing for it to land on.
865 if (!cast.target) {
866 continue;
867 }
868 at = *cast.target;
869 } else if (fx.at() == skills::Fx::BLADE) {
870 Figure posed = cast.figure;
871 posed.blend = 1.0f;
872 if (const auto blade = characters.Blade(posed)) {
873 at = blade->second;
874 }
875 }
876 if (fx.at() != skills::Fx::BLADE) {
877 at.y = terrain.Height(at.x, at.z) + fx.up();
878 }
879 const Moment m{.fx = fx,
880 .at = at,
881 .yaw = cast.figure.yaw,
882 .age = age,
883 .span = span,
884 .seed = cast.seed,
885 .index = static_cast<uint32_t>(index),
886 .color = color};
887 rlBegin(RL_TRIANGLES);
888 switch (fx.kind()) {
889 case skills::Fx::RING:
890 DrawRing(m, terrain);
891 break;
892 case skills::Fx::SPARKS:
893 DrawSparks(m, terrain, camera.position);
894 break;
895 case skills::Fx::MOTES:
896 DrawMotes(m, camera);
897 break;
898 case skills::Fx::BOLT:
899 DrawBolt(m, camera.position);
900 break;
901 case skills::Fx::WAVE:
902 DrawWave(m);
903 break;
904 case skills::Fx::GLOW:
905 DrawGlow(m, camera, fx.at() != skills::Fx::BLADE);
906 break;
907 case skills::Fx::EDGE: {
908 Figure posed = cast.figure;
909 posed.blend = 1.0f;
910 if (const auto blade = characters.Blade(posed)) {
911 DrawEdge(m, *blade, camera);
912 }
913 break;
914 }
915 case skills::Fx::SLASH:
916 DrawSlash(m, camera);
917 break;
918 case skills::Fx::FLASH:
919 // Only our own: in a crowd, everyone's would never stop.
920 if (cast.figure.you) {
921 DrawFlash(m, camera);
922 }
923 break;
924 default:
925 break;
926 }
927 rlEnd();
928 }
929 }
930 EndShaderMode();
931 EndBlendMode();
932 rlEnableDepthMask();
933 rlEnableBackfaceCulling();
934}
935
936Vector3 Fx::Shake(Vector3 camera) const {
937 Vector3 shake{0.0f, 0.0f, 0.0f};
938 for (const auto& [id, cast] : casts_) {
939 for (const skills::Fx& fx : skills_.at(cast.figure.to_skill).look().fx()) {
940 const float age = cast.clock - static_cast<float>(fx.from_ms()) / 1000.0f;
941 const float span = static_cast<float>(fx.for_ms()) / 1000.0f;
942 if (fx.kind() != skills::Fx::SHAKE || age < 0.0f || age > span) {
943 continue;
944 }
945 // Full within 15 metres of the camera, and nothing past 40.
946 const float near = std::clamp(
947 1.0f -
948 (Vector3Distance(camera, cast.figure.position) - 15.0f) / 25.0f,
949 0.0f, 1.0f);
950 const float size = fx.size() * std::pow(1.0f - age / span, 2.0f) * near;
951 shake =
952 Vector3Add(shake, Vector3{size * std::sin(age * 71.0f),
953 0.6f * size * std::sin(age * 53.0f + 1.3f),
954 size * std::sin(age * 67.0f + 2.1f)});
955 }
956 }
957 return shake;
958}
959
960float Fx::Zoom() const {
961 constexpr float kIn = 0.25f;
962 constexpr float kOut = 0.35f;
963 float zoom = 0.0f;
964 for (const auto& [id, cast] : casts_) {
965 if (!cast.figure.you) {
966 continue;
967 }
968 for (const skills::Fx& fx : skills_.at(cast.figure.to_skill).look().fx()) {
969 const float span = static_cast<float>(fx.for_ms()) / 1000.0f;
970 const float u =
971 (cast.clock - static_cast<float>(fx.from_ms()) / 1000.0f) / span;
972 if (fx.kind() != skills::Fx::ZOOM || u < 0.0f || u > 1.0f) {
973 continue;
974 }
975 // In smoothly, held, and smoothly back out.
976 auto smooth = [](float t) {
977 t = std::clamp(t, 0.0f, 1.0f);
978 return t * t * (3.0f - 2.0f * t);
979 };
980 zoom += fx.size() * smooth(u / kIn) * smooth((1.0f - u) / kOut);
981 }
982 }
983 return zoom;
984}
985
986} // namespace nanommo
void DrawGhost(const Figure &figure, Color color) const
std::optional< std::pair< Vector3, Vector3 > > Blade(const Figure &figure) const
void DrawGhosts(const Characters &characters) const
The copies fast moves leave behind them, which go over what's solid.
Definition fx.cc:685
void DrawLight(const Camera3D &camera, const Terrain &terrain, const Atmosphere &atmosphere, const Characters &characters) const
The light, which goes over what's behind it, and blooms.
Definition fx.cc:825
Vector3 Shake(Vector3 camera) const
How far the camera's shaken, at this moment, by the skills near it.
Definition fx.cc:936
void Hold(std::vector< Figure > &figures) const
Definition fx.cc:341
Fx(const SkillTable &skills, Shader lit, const ItemTable &items={})
Definition fx.cc:172
float Zoom() const
How far in our own camera goes, in metres, for a skill we're using.
Definition fx.cc:960
void DrawSolid(const Terrain &terrain) const
The shards and the spikes.
Definition fx.cc:739
static constexpr float kGhostGap
How far apart the copies a fast move leaves are, in seconds.
Definition fx.h:34
void Update(const std::vector< Figure > &figures, float seconds)
Definition fx.cc:199
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
Mesh Upload() const
Hands them to the GPU, as a mesh for LoadModelFromMesh.
Definition triangles.cc:36
Definition chat.cc:9
Shader LoadGlsl(const char *vertex, const char *fragment)
Definition glsl.cc:32
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
Definition picking.cc:86
constexpr float kGravity
Definition world.h:140
The sky and the light, which the shaders read each frame.
Definition atmosphere.h:13
uint32_t id
Definition animator.h:21
float from_time
Definition animator.h:76
Vector3 position
Definition animator.h:63
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
uint32_t to_skill
Definition animator.h:78
The items in the items file, by id.
Definition items.h:18
std::map< uint32_t, items::Item > by_id
Definition items.h:19