cntrl/src/render/debris.cpp
dandovlatov bb5937d921 FBC: NIGHT SHIFT - added skill debris surfaces, reliable WARP casting and opt-in tracing
- Skills shake nearby shelves, tables and garbage cans into paper + rock
  debris; props stay intact, destroyed bins stop shedding, and rock debris
  can originate from prop surfaces
- Manual K always casts WARP (field drops ahead of the agent when no crowd
  is in range); autoplay still waits for a real target
- Added opt-in --trace categories for silent skill decisions, debris bursts,
  input and consumed events, with zero cost when disabled
- Added debris_surface_wiring and debug_trace_toggle regression tests
2026-11-25 01:06:26 +04:00

289 lines
9.8 KiB
C++

#include "render/debris.hpp"
#include "render/profiler.hpp"
#include "raymath.h"
#include "rlgl.h"
#define DrawCube(...) do { PROF_DRAW_CALL(); DrawCube(__VA_ARGS__); } while(0)
#define DrawCylinder(...) do { PROF_DRAW_CALL(); DrawCylinder(__VA_ARGS__); } while(0)
#include <algorithm>
#include <cmath>
namespace {
float rand01() {
return static_cast<float>(GetRandomValue(0, 10000)) * 0.0001f;
}
float range(float lo, float hi) {
return lo + (hi - lo) * rand01();
}
} // namespace
void PaperDebris::init() {
shards_.reserve(kCapacity);
batch_.init();
}
void PaperDebris::shutdown() {
batch_.shutdown();
}
void PaperDebris::clear() {
shards_.clear();
}
void PaperDebris::burst(Vec2 center, float height, float radius,
const Vector3* origins, int origin_count, int count) {
for (int i = 0; i < count && static_cast<int>(shards_.size()) < kCapacity;
++i) {
PaperShard s;
if (origins != nullptr && origin_count > 0) {
const Vector3& src = origins[GetRandomValue(0, origin_count - 1)];
s.pos = {src.x + range(-0.20f, 0.20f),
src.y + range(-0.08f, 0.12f),
src.z + range(-0.20f, 0.20f)};
} else {
const float a = range(0.0f, 6.2831853f);
const float d = range(0.0f, radius * 0.65f);
s.pos = {center.x + std::cos(a) * d, height,
center.y + std::sin(a) * d};
}
float dx = s.pos.x - center.x;
float dz = s.pos.z - center.y;
float len = std::sqrt(dx * dx + dz * dz);
if (len < 0.05f) {
const float a = range(0.0f, 6.2831853f);
dx = std::cos(a);
dz = std::sin(a);
len = 1.0f;
}
const float spread = range(2.2f, 6.0f) * (1.0f + radius * 0.07f);
s.vel = {dx / len * spread, range(3.2f, 7.0f), dz / len * spread};
s.yaw = range(0.0f, 360.0f);
s.pitch = range(-30.0f, 30.0f);
s.roll = range(-30.0f, 30.0f);
s.spin_y = range(-420.0f, 420.0f);
s.spin_x = range(-300.0f, 300.0f);
s.spin_z = range(-300.0f, 300.0f);
s.age = 0.0f;
s.life = range(1.5f, 2.7f);
s.size = range(0.46f, 0.80f);
s.phase = range(0.0f, 6.2831853f);
const float pick = rand01();
s.variant = pick < 0.62f ? 0 : (pick < 0.90f ? 1 : 2);
shards_.push_back(s);
}
}
void PaperDebris::update(float dt, float corruption) {
const float flutter_amp = 1.0f + corruption * 0.8f;
for (std::size_t i = 0; i < shards_.size();) {
PaperShard& s = shards_[i];
s.age += dt;
const float flutter =
std::sin(s.phase + s.age * 9.0f) * 1.5f * flutter_amp;
s.vel.y -= 9.0f * dt;
s.vel.x *= 0.985f;
s.vel.z *= 0.985f;
s.pos.x += (s.vel.x + flutter * 0.14f) * dt;
s.pos.y += s.vel.y * dt;
s.pos.z += (s.vel.z + flutter * 0.10f) * dt;
s.yaw += s.spin_y * dt;
s.pitch += s.spin_x * dt;
s.roll += s.spin_z * dt;
if (s.pos.y <= 0.03f) {
s.pos.y = 0.03f;
s.vel.y *= -0.28f;
s.vel.x *= 0.62f;
s.vel.z *= 0.62f;
s.spin_x *= 0.70f;
s.spin_y *= 0.70f;
s.spin_z *= 0.70f;
if (std::abs(s.vel.y) < 0.35f) s.age += dt * 2.0f;
}
if (s.age > s.life) {
shards_[i] = shards_.back();
shards_.pop_back();
} else {
++i;
}
}
}
void PaperDebris::draw(Vector3 camera_pos) const {
const Color paper{244, 240, 228, 255};
const Color memo{250, 205, 42, 255};
const Color ink{38, 39, 44, 255};
const Color red{150, 24, 32, 255};
batch_.begin();
for (const PaperShard& s : shards_) {
const float dx = s.pos.x - camera_pos.x;
const float dz = s.pos.z - camera_pos.z;
if (dx * dx + dz * dz > 62.0f * 62.0f) continue;
const float fade = 1.0f - s.age / s.life;
Color tint = s.variant == 2 ? memo : paper;
tint.a = static_cast<unsigned char>(255.0f * std::min(1.0f, fade * 2.2f));
batch_.add(s.pos, {s.size, 0.012f, s.size * 0.74f},
s.yaw, s.pitch, s.roll, tint);
if (s.variant != 2) {
Color ink_tint = ink;
ink_tint.a = tint.a;
const Matrix rot = MatrixMultiply(
MatrixRotateZ(s.roll * DEG2RAD),
MatrixMultiply(MatrixRotateX(s.pitch * DEG2RAD),
MatrixRotateY(s.yaw * DEG2RAD)));
auto offset = [&](Vector3 local) {
const Vector3 o = Vector3Transform(local, rot);
return Vector3{s.pos.x + o.x, s.pos.y + o.y, s.pos.z + o.z};
};
batch_.add(offset({0.0f, 0.0f, s.size * 0.10f}),
{s.size * 0.58f, 0.002f, s.size * 0.055f},
s.yaw, s.pitch, s.roll, ink_tint);
if (s.variant == 1) {
batch_.add(offset({-s.size * 0.08f, 0.0f, -s.size * 0.06f}),
{s.size * 0.42f, 0.002f, s.size * 0.045f},
s.yaw, s.pitch, s.roll, ink_tint);
Color stamp = red;
stamp.a = tint.a;
batch_.add(offset({s.size * 0.22f, 0.0f, -s.size * 0.20f}),
{s.size * 0.12f, 0.002f, s.size * 0.10f},
s.yaw, s.pitch, s.roll, stamp);
}
}
}
batch_.draw();
}
void ConcreteDebris::init() {
chunks_.reserve(kCapacity);
batch_.init();
}
void ConcreteDebris::shutdown() {
batch_.shutdown();
}
void ConcreteDebris::clear() {
chunks_.clear();
}
void ConcreteDebris::burst(Vec2 center, float radius, int count) {
burst(center, radius, count, nullptr, 0);
}
void ConcreteDebris::burst(Vec2 center, float radius, int count,
const Vector3* origins, int origin_count) {
for (int i = 0; i < count && static_cast<int>(chunks_.size()) < kCapacity;
++i) {
ConcreteChunk c;
const float a = range(0.0f, 6.2831853f);
if (origins != nullptr && origin_count > 0) {
const Vector3& src = origins[GetRandomValue(0, origin_count - 1)];
c.pos = {src.x + range(-0.24f, 0.24f),
src.y + range(-0.08f, 0.10f),
src.z + range(-0.24f, 0.24f)};
} else {
const float d = range(0.2f, radius * 0.9f);
c.pos = {center.x + std::cos(a) * d, 0.09f,
center.y + std::sin(a) * d};
}
const float spread = range(1.8f, 4.8f) * (1.0f + radius * 0.12f);
c.vel = {std::cos(a) * spread, range(2.6f, 6.2f),
std::sin(a) * spread};
c.yaw = range(0.0f, 360.0f);
c.pitch = range(-35.0f, 35.0f);
c.roll = range(-35.0f, 35.0f);
c.spin_y = range(-360.0f, 360.0f);
c.spin_x = range(-280.0f, 280.0f);
c.age = 0.0f;
c.life = range(0.9f, 1.5f);
c.size = range(0.22f, 0.52f);
c.shade = static_cast<unsigned char>(range(72.0f, 138.0f));
chunks_.push_back(c);
}
}
void ConcreteDebris::update(float dt) {
for (std::size_t i = 0; i < chunks_.size();) {
ConcreteChunk& c = chunks_[i];
c.age += dt;
c.vel.y -= 11.0f * dt;
c.vel.x *= 0.982f;
c.vel.z *= 0.982f;
c.pos.x += c.vel.x * dt;
c.pos.y += c.vel.y * dt;
c.pos.z += c.vel.z * dt;
c.yaw += c.spin_y * dt;
c.pitch += c.spin_x * dt;
if (c.pos.y <= 0.09f) {
c.pos.y = 0.09f;
c.vel.y *= -0.22f;
c.vel.x *= 0.55f;
c.vel.z *= 0.55f;
if (std::abs(c.vel.y) < 0.4f) c.age += dt * 2.5f;
}
if (c.age > c.life) {
chunks_[i] = chunks_.back();
chunks_.pop_back();
} else {
++i;
}
}
}
void ConcreteDebris::draw(Vector3 camera_pos) const {
batch_.begin();
for (const ConcreteChunk& c : chunks_) {
const float dx = c.pos.x - camera_pos.x;
const float dz = c.pos.z - camera_pos.z;
if (dx * dx + dz * dz > 62.0f * 62.0f) continue;
const float fade = 1.0f - c.age / c.life;
Color tint{c.shade, c.shade, static_cast<unsigned char>(c.shade + 4),
static_cast<unsigned char>(255.0f * std::min(1.0f, fade * 2.5f))};
batch_.add(c.pos, {c.size, c.size * 0.55f, c.size * 0.8f},
c.yaw, c.pitch, c.roll, tint);
}
batch_.draw();
}
void ScorchDecals::add(Vec2 pos, float radius) {
items_[head_] = {pos, radius, static_cast<float>(GetRandomValue(0, 1000))};
head_ = (head_ + 1) % kCapacity;
if (count_ < kCapacity) ++count_;
}
void ScorchDecals::clear() {
head_ = 0;
count_ = 0;
}
void ScorchDecals::draw(Vector3 camera_pos) const {
const Color burn{72, 22, 26, 82};
const Color core{20, 10, 14, 120};
const Color paper{224, 218, 202, 220};
const Color redacted{126, 26, 34, 230};
for (int i = 0; i < count_; ++i) {
const ScorchDecal& d = items_[i];
const float dx = d.pos.x - camera_pos.x;
const float dz = d.pos.y - camera_pos.z;
if (dx * dx + dz * dz > 60.0f * 60.0f) continue;
DrawCylinder({d.pos.x, 0.072f, d.pos.y}, d.radius, d.radius * 1.18f,
0.012f, 11, burn);
DrawCylinder({d.pos.x, 0.076f, d.pos.y}, d.radius * 0.62f,
d.radius * 0.70f, 0.012f, 11, core);
const float a = d.seed * 0.0062832f;
for (int k = 0; k < 3; ++k) {
const float ang = a + k * 2.0944f;
DrawCube({d.pos.x + std::cos(ang) * d.radius * 0.72f, 0.082f,
d.pos.y + std::sin(ang) * d.radius * 0.72f},
0.32f, 0.010f, 0.42f, k == 1 ? redacted : paper);
}
}
}