20constexpr const char* kLightVertexShader = R
"(#version 330
21in vec3 vertexPosition;
29 gl_Position = mvp * vec4(vertexPosition, 1.0);
30 depth = gl_Position.w;
35constexpr const char* kLightFragmentShader = R
"(#version 330
42const float kBright = 3.0;
45 float fog = clamp((depth - fogNear) / (fogFar - fogNear), 0.0, 1.0);
46 pixel = vec4(color.rgb * kBright, color.a * (1.0 - fog));
51constexpr float kTrail = 0.16f;
53constexpr Vector3 kUp{0.0f, 1.0f, 0.0f};
55constexpr float kMiddle = 0.9f;
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;
67 return static_cast<float>(h >> 8) / 16777216.0f;
70float EaseOut(
float t) {
return 1.0f - (1.0f - t) * (1.0f - t) * (1.0f - t); }
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);
80Color Hot(Color color,
float heat) {
81 return ColorLerp(color, WHITE, std::clamp(heat, 0.0f, 1.0f));
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);
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);
105void Streak(Vector3 a, Vector3 b,
float width, Color color,
float alpha,
108 Vector3Scale(Vector3Normalize(Vector3CrossProduct(
109 Vector3Subtract(b, a), Vector3Subtract(eye, a))),
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);
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;
130 at, Vector3Add(Vector3Scale(right, out * radius * std::cos(turn)),
131 Vector3Scale(up, out * radius * std::sin(turn))));
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,
145Vector3 Around(
const Terrain& terrain, Vector3 at,
float distance,
float turn,
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};
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)};
164 gem.Add(corner(i), corner(i + 1), top, WHITE);
165 gem.Add(corner(i + 1), corner(i), bottom, WHITE);
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();
182 for (
const auto& [
id, skill] : skills_) {
184 for (
const skills::Fx& fx : skill.look().fx()) {
188 static_cast<float>(fx.from_ms() + fx.for_ms()) / 1000.0f + kTrail);
196 UnloadShader(light_);
199void Fx::Update(
const std::vector<Figure>& figures,
float seconds) {
200 for (
auto& [
id, cast] : casts_) {
201 cast.clock += seconds;
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()) {
209 auto [found, fresh] = casts_.try_emplace(figure.id);
210 Cast& cast = found->second;
212 if (fresh || cast.figure.
to_skill != figure.to_skill ||
213 figure.to_time < cast.clock - seconds) {
214 cast = Cast{.seed = ++seeds_};
216 cast.figure = figure;
217 cast.clock = figure.to_time;
220 for (
const Figure& other : figures) {
221 if (
Picked(other, figure.target, figure.target_player)) {
222 cast.target = other.position;
226 for (
auto& [
id, cast] : casts_) {
227 const skills::Skill& skill = skills_.at(cast.figure.to_skill);
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));
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);
239 while (cast.path.size() > 2 &&
240 cast.path[1].first < cast.time -
kGhostGap * 10.0f) {
241 cast.path.pop_front();
244 std::erase_if(casts_, [&](
const auto& entry) {
245 const Cast& cast = entry.second;
246 return cast.time > ends_.at(cast.figure.
to_skill);
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);
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())});
264 const auto weapon = weapons_.find(figure.
weapon);
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());
271 {.at = hit / 1000.0f, .weight = weight(effect.push(), 0.0f)});
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;
284 const std::vector<Beat> blows = BlowsOf(skill, cast.figure);
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) {
292 const float most = (1.0f - kStill) * held;
294 lag += (1.0f - kStill) * since;
295 }
else if (since < held * (1.0f + kCatchUp)) {
296 lag += most * (1.0f - (since - held) / (held * kCatchUp));
302bool Fx::Lands(Cast& cast,
const skills::Skill& skill,
303 const std::vector<Figure>& figures)
const {
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);
311 (effect.weapon() && weapon != weapons_.end() ? weapon->second.reach()
314 if (effect.damage() == 0 && !effect.weapon()) {
318 for (
const Figure& other : figures) {
319 if (!other.monster || other.to == protocol::MOTION_DEAD ||
320 (!effect.all() && other.id != player.target)) {
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) {
328 if (effect.arc() > 0.0f) {
330 std::abs(std::remainder(std::atan2(dx, dz) - player.yaw, 2.0f * PI));
331 if (off > effect.arc() * DEG2RAD / 2.0f) {
335 cast.struck.insert(other.id);
342 for (
Figure& figure : figures) {
345 for (
const auto& [
id, cast] : casts_) {
346 if (cast.struck.contains(figure.
id)) {
347 lag = std::max(lag, cast.clock - cast.time);
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;
365 const skills::Fx& fx;
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);
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) {
393 Figure posed = figure;
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);
404 fade[i] = std::pow(1.0f - (time - posed.to_time) / kTrail, 2.0f);
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);
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]),
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);
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);
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);
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) {
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});
468 const Vector3 head = where(t);
469 if (head.y < terrain.Height(head.x, head.z)) {
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);
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) {
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),
497void DrawBolt(
const Moment& m, Vector3 eye) {
498 constexpr int kSteps = 16;
499 constexpr float kFlicker = 0.07f;
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;
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)});
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);
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) {
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);
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)});
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;
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));
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);
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);
567void DrawGlow(
const Moment& m,
const Camera3D& camera,
bool lifted) {
568 const float u = m.done();
570 m.fx.size() * (0.5f + 0.5f * EaseOut(std::min(3.0f * u, 1.0f)));
572 u < 0.15f ? u / 0.15f : std::pow((1.0f - u) / 0.85f, 2.0f);
574 lifted ? Vector3Add(m.at, Vector3Scale(kUp, kMiddle)) : m.at;
575 Blob(at, radius, m.color, alpha, camera);
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,
592 Streak(guard, tip, width, Hot(m.color, 0.8f), fade * flicker,
594 Blob(tip, 2.5f * width, m.color, 0.6f * fade, camera);
599void DrawSlash(
const Moment& m,
const Camera3D& camera) {
600 constexpr int kSteps = 16;
601 constexpr float kDraw = 0.07f;
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) {
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 +
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();
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)));
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,
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,
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);
662void DrawFlash(
const Moment& m,
const Camera3D& camera) {
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));
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);
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()) {
697 rlDrawRenderBatchActive();
698 rlDisableDepthMask();
699 BeginBlendMode(BLEND_ALPHA);
702 const Color color = GetColor(fx.color());
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) {
709 Figure ghost = cast.figure;
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];
719 t1 > t0 ? std::clamp((then - t0) / (t1 - t0), 0.0f, 1.0f)
725 1.0f -
static_cast<float>(k) /
static_cast<float>(fx.count() + 1);
728 Fade(color, 0.45f * fade *
static_cast<float>(color.a) / 255.0f));
733 rlDrawRenderBatchActive();
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);
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);
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)) {
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,
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) {
779 const float rise = (45.0f + 35.0f * m.Random(i, 1)) * DEG2RAD;
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;
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)),
796 axis, (6.0f + 10.0f * m.Random(i, 6)) * t, turn,
797 {size, 1.4f * size, size}, m.color);
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();
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},
828 if (casts_.empty()) {
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);
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;
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);
859 if (age < 0.0f || age > span) {
862 Vector3 at = cast.figure.position;
863 if (fx.at() == skills::Fx::TARGET) {
869 }
else if (fx.at() == skills::Fx::BLADE) {
870 Figure posed = cast.figure;
872 if (
const auto blade = characters.
Blade(posed)) {
876 if (fx.at() != skills::Fx::BLADE) {
877 at.y = terrain.
Height(at.x, at.z) + fx.up();
879 const Moment m{.fx = fx,
881 .yaw = cast.figure.yaw,
885 .index =
static_cast<uint32_t
>(index),
887 rlBegin(RL_TRIANGLES);
889 case skills::Fx::RING:
890 DrawRing(m, terrain);
892 case skills::Fx::SPARKS:
893 DrawSparks(m, terrain, camera.position);
895 case skills::Fx::MOTES:
896 DrawMotes(m, camera);
898 case skills::Fx::BOLT:
899 DrawBolt(m, camera.position);
901 case skills::Fx::WAVE:
904 case skills::Fx::GLOW:
905 DrawGlow(m, camera, fx.at() != skills::Fx::BLADE);
907 case skills::Fx::EDGE: {
908 Figure posed = cast.figure;
910 if (
const auto blade = characters.
Blade(posed)) {
911 DrawEdge(m, *blade, camera);
915 case skills::Fx::SLASH:
916 DrawSlash(m, camera);
918 case skills::Fx::FLASH:
920 if (cast.figure.you) {
921 DrawFlash(m, camera);
933 rlEnableBackfaceCulling();
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) {
946 const float near = std::clamp(
948 (Vector3Distance(camera, cast.figure.position) - 15.0f) / 25.0f,
950 const float size = fx.size() * std::pow(1.0f - age / span, 2.0f) * near;
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)});
961 constexpr float kIn = 0.25f;
962 constexpr float kOut = 0.35f;
964 for (
const auto& [
id, cast] : casts_) {
965 if (!cast.figure.you) {
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;
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) {
976 auto smooth = [](
float t) {
977 t = std::clamp(t, 0.0f, 1.0f);
978 return t * t * (3.0f - 2.0f * t);
980 zoom += fx.size() * smooth(u / kIn) * smooth((1.0f - u) / kOut);
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.
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.
Vector3 Shake(Vector3 camera) const
How far the camera's shaken, at this moment, by the skills near it.
void Hold(std::vector< Figure > &figures) const
Fx(const SkillTable &skills, Shader lit, const ItemTable &items={})
float Zoom() const
How far in our own camera goes, in metres, for a skill we're using.
void DrawSolid(const Terrain &terrain) const
The shards and the spikes.
static constexpr float kGhostGap
How far apart the copies a fast move leaves are, in seconds.
void Update(const std::vector< Figure > &figures, float seconds)
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Mesh Upload() const
Hands them to the GPU, as a mesh for LoadModelFromMesh.
Shader LoadGlsl(const char *vertex, const char *fragment)
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
The sky and the light, which the shaders read each frame.
The items in the items file, by id.
std::map< uint32_t, items::Item > by_id