cntrl/src/game/skills.cpp
dandovlatov 79a2502a0a FBC: NIGHT SHIFT - blackout polish and responsive skill keys
- Keep the default tactical camera in the projector blackout; lights go out
  ~2 s after crossing the screen (Pending phase with normal play/grace)
- Remove the blackout task HUD (progress card, dots, instructions); replace
  with close-range world-anchored FBC inspection placards for the screen,
  phones and the glass case
- Fix manual J/K/U presses feeling dead: queue a press while the skill is on
  cooldown and flash the HUD stamp immediately; clear the queue on autoplay
  toggle and during the pocket. Add regression test
- Add --blackout-near-phone QA hook; update design docs and README
2026-11-25 00:20:49 +04:00

315 lines
12 KiB
C++

#include "game/skills.hpp"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include "core/events.hpp"
#include "core/spatial_hash.hpp"
#include "game/buffs.hpp"
#include "game/enemies.hpp"
#include "game/player.hpp"
namespace {
constexpr float kConcreteCooldown = 6.0f;
constexpr float kWarpCooldown = 9.0f;
constexpr float kWarpDuration = 0.75f;
constexpr float kWarpRadius = 4.5f;
constexpr float kWarpDamage = 75.0f;
constexpr float kWarpPull = 7.0f;
constexpr float kGatherTime = 0.38f;
constexpr float kFlightSpeed = 17.0f;
constexpr float kBlastRadius = 3.4f;
constexpr float kBlastDamage = 95.0f;
constexpr float kCastRange = 18.0f;
constexpr float kManualHold = 5.0f;
constexpr float kCrowdRadius = 2.6f;
constexpr float kFluorescentCooldown = 14.0f;
constexpr float kLightRadius = 6.0f;
constexpr float kLightDuration = 10.0f;
constexpr float kLightDamageMult = 1.25f;
// Picks the densest nearby crowd and returns its centroid; a lone enemy is a
// valid target. Returns false when there is nothing in casting range.
bool pick_crowd(const EnemySystem& enemies, Vec2 from, Vec2& center) {
SpatialHash hash(kCrowdRadius);
for (std::size_t i = 0; i < enemies.enemies.size(); ++i)
hash.insert(static_cast<int>(i), enemies.enemies[i].pos);
hash.build();
const std::size_t stride = enemies.enemies.size() > 200
? enemies.enemies.size() / 100 : 1;
int best_count = -1;
float best_dist = 0.0f;
Vec2 best_center{};
for (std::size_t i = 0; i < enemies.enemies.size(); i += stride) {
const Vec2 here = enemies.enemies[i].pos;
const float dist = distance(here, from);
if (dist > kCastRange) continue;
int count = 0;
Vec2 sum{};
hash.query(here, kCrowdRadius, [&](int id) {
const Vec2 other = enemies.enemies[static_cast<std::size_t>(id)].pos;
if (distance(here, other) <= kCrowdRadius) {
++count;
sum += other;
}
});
// Density wins; ties go to the closest crowd so the pile doesn't fly
// across the whole floor for an equally sized group.
if (count > best_count || (count == best_count && dist < best_dist)) {
best_count = count;
best_dist = dist;
best_center = sum * (1.0f / static_cast<float>(count));
}
}
if (best_count < 0) return false;
center = best_center;
return true;
}
void try_cast(SkillSystem& skills, Player& player, EnemySystem& enemies,
EventQueue& events) {
const int index = static_cast<int>(SkillType::ConcreteCall);
if (skills.cooldowns[index] > 0.0f || !player.alive()) return;
Vec2 center{};
if (!pick_crowd(enemies, player.pos, center)) return; // keep the cast ready
ConcretePile pile;
pile.pos = player.pos;
pile.target = center;
pile.phase = PilePhase::Gather;
pile.phase_duration = kGatherTime;
pile.damage = kBlastDamage * player.stats.damage_mult;
pile.blast_radius = kBlastRadius;
skills.piles.add(pile);
skills.cooldowns[index] = kConcreteCooldown;
// The event carries the landing zone and blast radius for a brief ground
// telegraph; the presenter knows the player position for the gather dust.
events.push(EventType::SkillCast, center, pile.blast_radius);
}
void try_cast_warp(SkillSystem& skills, Player& player, EnemySystem& enemies,
EventQueue& events) {
const int index = static_cast<int>(SkillType::Warp);
if (skills.cooldowns[index] > 0.0f || !player.alive()) return;
Vec2 center{};
if (!pick_crowd(enemies, player.pos, center)) return;
WarpField field;
field.pos = center;
field.duration = kWarpDuration;
field.radius = kWarpRadius;
field.damage = kWarpDamage * player.stats.damage_mult;
skills.warps.add(field);
skills.cooldowns[index] = kWarpCooldown;
events.push(EventType::SkillCast, center, field.radius,
static_cast<unsigned>(index));
}
// The projector-screen ritual reward: flood the room with fluorescent light.
void try_cast_fluorescent(SkillSystem& skills, Player& player, EnemySystem& enemies,
BuffSystem& buffs, EventQueue& events) {
const int index = static_cast<int>(SkillType::Fluorescent);
if (!skills.fluorescent_unlocked || skills.cooldowns[index] > 0.0f ||
!player.alive()) {
return;
}
BuffEffect effect;
effect.damage_taken_mult = kLightDamageMult;
for (std::size_t i = 0; i < enemies.enemies.size(); ++i) {
if (distance(player.pos, enemies.enemies[i].pos) > kLightRadius) continue;
buffs.apply(static_cast<int>(i), effect, kLightDuration, "FLUORESCENT LIGHT");
enemies.enemies[i].hit_flash = 0.15f;
}
skills.cooldowns[index] = kFluorescentCooldown;
events.push(EventType::SkillCast, player.pos, kLightRadius,
static_cast<unsigned>(index));
}
void detonate(const ConcretePile& pile, EnemySystem& enemies, EventQueue& events) {
for (Enemy& e : enemies.enemies) {
if (distance(pile.pos, e.pos) >= pile.blast_radius + e.radius) continue;
e.hp -= pile.damage * e.mods.damage_taken_mult;
e.hit_flash = 0.15f;
events.push(EventType::EnemyHit, e.pos, pile.damage);
}
events.push(EventType::AreaDetonation, pile.pos, pile.blast_radius,
kDetonateConcrete);
}
void warp_burst(const WarpField& field, EnemySystem& enemies, EventQueue& events) {
for (Enemy& e : enemies.enemies) {
if (distance(field.pos, e.pos) >= field.radius + e.radius) continue;
e.hp -= field.damage * e.mods.damage_taken_mult;
e.hit_flash = 0.15f;
events.push(EventType::EnemyHit, e.pos, field.damage);
}
events.push(EventType::WarpBurst, field.pos, field.radius);
}
} // namespace
const char* skill_name(SkillType type) {
switch (type) {
case SkillType::ConcreteCall: return "CONCRETE CALL";
case SkillType::Warp: return "WARP";
case SkillType::Fluorescent: return "FLUORESCENT";
default: return "UNKNOWN";
}
}
float skill_cooldown(SkillType type) {
switch (type) {
case SkillType::ConcreteCall: return kConcreteCooldown;
case SkillType::Warp: return kWarpCooldown;
case SkillType::Fluorescent: return kFluorescentCooldown;
default: return 1.0f;
}
}
void SkillSystem::reset() {
for (float& cd : cooldowns) cd = 0.0f;
for (bool& q : queued) q = false;
for (float& f : press_feedback) f = 0.0f;
manual_hold = 0.0f;
autoplay_enabled = true;
piles.clear();
warps.clear();
}
void SkillSystem::note_input(const InputState& in) {
if (in.skill_concrete) {
manual_hold = kManualHold;
queued[static_cast<int>(SkillType::ConcreteCall)] = true;
press_feedback[static_cast<int>(SkillType::ConcreteCall)] = 0.35f;
}
if (in.skill_warp) {
manual_hold = kManualHold;
queued[static_cast<int>(SkillType::Warp)] = true;
press_feedback[static_cast<int>(SkillType::Warp)] = 0.35f;
}
if (in.skill_fluorescent) {
manual_hold = kManualHold;
queued[static_cast<int>(SkillType::Fluorescent)] = true;
press_feedback[static_cast<int>(SkillType::Fluorescent)] = 0.35f;
}
}
void SkillSystem::reset_all() {
reset();
fluorescent_unlocked = false;
}
void SkillSystem::tick_presentation(float dt, const InputState& in) {
for (float& f : press_feedback) f = std::max(0.0f, f - dt);
if (in.skill_concrete) press_feedback[static_cast<int>(SkillType::ConcreteCall)] = 0.35f;
if (in.skill_warp) press_feedback[static_cast<int>(SkillType::Warp)] = 0.35f;
if (in.skill_fluorescent) press_feedback[static_cast<int>(SkillType::Fluorescent)] = 0.35f;
}
void SkillSystem::update(float dt, const InputState& in, Player& player,
EnemySystem& enemies, BuffSystem& buffs,
EventQueue& events) {
if (in.toggle_autoplay) {
autoplay_enabled = !autoplay_enabled;
for (bool& q : queued) q = false;
}
manual_hold = std::max(0.0f, manual_hold - dt);
for (float& cd : cooldowns) cd = std::max(0.0f, cd - dt);
for (float& f : press_feedback) f = std::max(0.0f, f - dt);
// A manual press takes control for a short window and is remembered while
// the skill cools down — autoplay otherwise keeps the cooldown spent and
// the key would look dead.
note_input(in);
if (manual_hold <= 0.0f) for (bool& q : queued) q = false;
const int ci = static_cast<int>(SkillType::ConcreteCall);
const int wi = static_cast<int>(SkillType::Warp);
const int fi = static_cast<int>(SkillType::Fluorescent);
// A successful cast resets the cooldown upward; an aborted attempt only
// ticks it down. Only the former clears the remembered press.
const float before_c = cooldowns[ci], before_w = cooldowns[wi],
before_f = cooldowns[fi];
if (in.skill_concrete || queued[ci]) {
try_cast(*this, player, enemies, events);
if (cooldowns[ci] > before_c) queued[ci] = false;
}
if (in.skill_warp || queued[wi]) {
try_cast_warp(*this, player, enemies, events);
if (cooldowns[wi] > before_w) queued[wi] = false;
}
if (in.skill_fluorescent || queued[fi]) {
try_cast_fluorescent(*this, player, enemies, buffs, events);
if (cooldowns[fi] > before_f) queued[fi] = false;
}
if (autoplay_active()) {
// Generic autoplay: every skill is cast from this same path when the
// player isn't driving them by hand.
try_cast(*this, player, enemies, events);
try_cast_warp(*this, player, enemies, events);
try_cast_fluorescent(*this, player, enemies, buffs, events);
}
for (std::size_t i = 0; i < piles.size();) {
ConcretePile& pile = piles[i];
pile.phase_time += dt;
if (pile.phase == PilePhase::Gather) {
pile.pos = player.pos; // the heap forms around the agent
pile.size = std::min(1.0f, pile.phase_time / pile.phase_duration);
if (pile.phase_time >= pile.phase_duration) {
pile.phase = PilePhase::Flight;
pile.phase_time = 0.0f;
pile.phase_duration = std::max(
0.05f, distance(pile.pos, pile.target) / kFlightSpeed);
}
++i;
continue;
}
const Vec2 delta = pile.target - pile.pos;
const float dist = length(delta);
const float step = kFlightSpeed * dt;
if (dist <= step + 0.25f) {
pile.pos = pile.target;
detonate(pile, enemies, events);
piles.remove_at(i);
continue;
}
pile.pos += normalized(delta) * step;
++i;
}
// WARP: drag the whole crowd inward, then burst it.
for (std::size_t i = 0; i < warps.size();) {
WarpField& field = warps[i];
field.timer += dt;
for (Enemy& e : enemies.enemies) {
const Vec2 delta = field.pos - e.pos;
const float dist = length(delta);
if (dist > field.radius || dist < 0.25f) continue;
const float pull = std::min(kWarpPull, dist * 3.0f) * dt;
e.pos += normalized(delta) * pull;
}
if (field.timer >= field.duration) {
warp_burst(field, enemies, events);
warps.remove_at(i);
continue;
}
++i;
}
}
float SkillSystem::readiness(SkillType type) const {
return std::clamp(1.0f - cooldown_left(type) / skill_cooldown(type),
0.0f, 1.0f);
}
float SkillSystem::cooldown_left(SkillType type) const {
return cooldowns[static_cast<int>(type)];
}