import { alias, isExplosive } from "@blitzkit/core"; import { literals } from "@blitzkit/i18n"; import { DoubleArrowRightIcon, EyeOpenIcon } from "@radix-ui/react-icons"; import { times } from "lodash-es"; import { useCallback, useEffect, useMemo, useRef, useState, type CSSProperties, type PointerEvent, } from "react"; import { clamp, degToRad, radToDeg } from "three/src/math/MathUtils.js"; import type { ToyProps } from "../../config/toys"; import { DEFAULT_PITCH_TRANSITION, applyPitchYawLimits, } from "../../core/blitz/applyPitchYawLimits"; import { modelTransformEvent } from "../../core/blitzkit/modelTransform"; import { useDuelModels } from "../../hooks/useDuelModels"; import { useDuelModules } from "../../hooks/useDuelModules"; import { useEquipment } from "../../hooks/useEquipment"; import { useStrings } from "../../hooks/useStrings"; import { fakeLogSquishyHigh, fakeLogSquishyHighInverse, fakeLogSquishyLow, } from "../../core/blitzkit/fakeLog"; import { Tankopedia } from "../../stores/tankopedia"; import type { ComputedCharacteristics } from "../../types/characteristics"; import { Button } from "../Button"; import { Checkbox } from "../Checkbox"; import { Code } from "../Code"; import { LazyImage } from "../LazyImage"; import { SegmentedControl } from "../SegmentedControl"; import { Slider } from "../Slider"; import { ShellBorder } from "../Tankopedia/CharacteristicsSection/components/Characteristics/components/ReloadVisualizer/components/ShellBorder"; import { TankopediaFlexibilityCanvas } from "../TankopediaFlexibilityCanvas"; import styles from "./index.module.css"; export function RicochetToy({ characteristics }: ToyProps) { const strings = useStrings(); const protagonist = useDuelModules("protagonist"); const container = useRef(null); const normalizationValue = characteristics.get("normalization"); const ricochetValue = characteristics.get("ricochet"); const normalization = degToRad( typeof normalizationValue === "number" ? normalizationValue : 0, ); const ricochet = degToRad( typeof ricochetValue === "number" ? ricochetValue : 90, ); const explodes = isExplosive(protagonist.shell.type); const [rawAngle, setAngle] = useState(degToRad(-25)); const angle = explodes ? degToRad(-70) : rawAngle; const doesRicochet = Math.abs(angle) >= ricochet; const effectiveAngle = doesRicochet ? -angle + Math.PI : Math.sign(angle) * Math.max(0, Math.abs(angle) - normalization); const armorPercentage = Math.min(999, 100 / Math.cos(effectiveAngle)); function updateAngle(event: PointerEvent) { event.preventDefault(); if (!container.current || explodes) return; const rect = container.current.getBoundingClientRect(); const x = event.clientX - rect.left - rect.width / 3; const y = event.clientY - rect.top - rect.height / 2; setAngle(clamp(Math.atan2(-y, x), -Math.PI / 2, Math.PI / 2)); } return (
{explodes && (
{strings.tanks.toys.ricochet.no_ricochet}
)}
); } enum CornerStatPosition { TopLeft = "top-left", TopRight = "top-right", BottomLeft = "bottom-left", BottomRight = "bottom-right", } interface CornerStatProps { label: string; value: string; position: CornerStatPosition; } function CornerStat({ label, value, position }: CornerStatProps) { return (
{label} {value}
); } const terrains = ["hard", "medium", "soft"] as const; type Terrain = (typeof terrains)[number]; interface TraverseToyProps { characteristics: ComputedCharacteristics; } export function TraverseToy({ characteristics }: TraverseToyProps) { const hull = useRef(null); const turret = useRef(null); const [terrain, setTerrain] = useState("hard"); const [rotateHull, setRotateHull] = useState(true); const [rotateTurret, setRotateTurret] = useState(true); const turretAngle = useRef(0); const hullAngle = useRef(0); const strings = useStrings(); const hullSpeed = characteristics.get( `hull_traverse_speed_${terrain}`, ) as number; const turretTraverseSpeed = characteristics.get( "turret_traverse_speed", ) as number; const turretSpeed = rotateTurret ? turretTraverseSpeed : 0; const hullSpeedEffective = rotateHull ? hullSpeed : 0; const isTurretless = characteristics.has("azimuth_left") || characteristics.has("azimuth_right"); useEffect(() => { let cancel = false; const turretSpeedRad = (turretSpeed * Math.PI) / 180; const hullSpeedRad = (hullSpeedEffective * Math.PI) / 180; let lastT = Date.now() / 1000; function frame() { const t = Date.now() / 1000; const dt = t - lastT; lastT = t; if (turret.current) { turretAngle.current = (turretAngle.current + turretSpeedRad * dt) % (2 * Math.PI); turret.current.style.transform = `translate(-50%, -50%) rotate(${-turretAngle.current}rad)`; } if (hull.current) { hullAngle.current = (hullAngle.current + hullSpeedRad * dt) % (2 * Math.PI); hull.current.style.transform = `translate(-50%, -50%) rotate(${-hullAngle.current}rad)`; } if (!cancel) requestAnimationFrame(frame); } frame(); return () => { cancel = true; }; }, [hullSpeedEffective, turretSpeed]); const terrainOptions = terrains.map((terrain) => ({ value: terrain, label: strings.tanks.characteristics.items[`hull_traverse_speed_${terrain}`], })); return (
{!isTurretless && ( )}
{!isTurretless && ( <> {strings.tanks.toys.traverse.hull} {strings.tanks.toys.traverse.turret} )}
{ const nextTerrain = terrains.find((terrain) => terrain === value); if (nextTerrain) setTerrain(nextTerrain); }} variant="underline" />
{strings.tanks.toys.traverse.effective} {literals(strings.units.deg_s, { value: (turretSpeed + hullSpeedEffective).toFixed(1), })}
); } const RELOAD_PRECISION = 1; interface ReloadData { reloads: number[]; shells: number; intraClip: number; burstShells: number; burstInterShell?: number; } export function ReloadToy({ characteristics }: ToyProps) { const protagonist = useDuelModules("protagonist"); const strings = useStrings(); const reloadData = useMemo(() => { const reloadCoefficient = characteristics.get( "_reload_coefficient", ) as number; const intraClip = protagonist.gun.gun_type!.$case === "regular" ? 0 : (characteristics.get("intra_clip") as number); let reloads: number[]; let shells: number; switch (protagonist.gun.gun_type!.$case) { case "regular": reloads = [characteristics.get("reload") as number]; shells = 1; break; case "auto_loader": reloads = [characteristics.get("reload") as number]; shells = protagonist.gun.gun_type!.value.shell_count; break; case "auto_reloader": reloads = protagonist.gun.gun_type!.value.shell_reloads.map( (reload) => reload * reloadCoefficient, ); shells = protagonist.gun.gun_type!.value.shell_count; break; } return { reloads, shells, intraClip, burstShells: protagonist.gun.burst?.count ?? 1, burstInterShell: protagonist.gun.burst?.interval, }; }, [characteristics, protagonist.gun]); const glow = useRef(null); const ring = useRef(null); const core = useRef(null); const remainingTime = useRef(null); const totalTime = useRef(null); const flash = useRef(null); const shellBars = useRef>([]); const state = useRef(0); const shoot = useCallback(() => { for (const element of [flash.current, ring.current, glow.current]) { if (!element) continue; element.dataset.shot = "true"; void element.offsetWidth; delete element.dataset.shot; } }, []); useEffect(() => { let cancel = false; let lastTime = performance.now() / 1000; let lastState = state.current; state.current = 0; function frame() { const reloadThreshold = reloadData.reloads.length; const now = performance.now() / 1000; const deltaTime = now - lastTime; let reload = 0; let shells: number[]; let interval = 0; if (state.current < reloadThreshold) { const currentReload = reloadData.reloads[Math.floor(state.current)]; if (currentReload === undefined) { throw new Error("Missing reload duration for current reload stage"); } interval = currentReload; state.current = Math.min( reloadThreshold, state.current + deltaTime / interval, ); reload = state.current % 1; shells = Array(reloadData.shells).fill(reload); if (reloadThreshold > 1) { shells = shells.map((_, index) => Math.max(0, state.current - reloadData.shells + index + 1), ); } if (state.current >= reloadThreshold) shoot(); } else if (state.current < reloadData.shells + reloadThreshold - 1) { if ( reloadData.burstShells === 1 || Math.floor(state.current) % reloadData.burstShells === 0 ) { interval = reloadData.intraClip; } else { if (reloadData.burstInterShell === undefined) { throw new Error("Missing burst interval for firing sequence"); } interval = reloadData.burstInterShell; } state.current = Math.min( reloadData.shells + reloadThreshold, state.current + deltaTime / interval, ); reload = state.current % 1; shells = reloadThreshold > 1 ? times(reloadData.shells, (index) => Math.max( 0, Math.floor(index + 1 + reloadThreshold - state.current), ), ) : times(reloadData.shells, (index) => Number(index > state.current - 1), ); if (lastState % 1 > state.current % 1) shoot(); } else { state.current = 0; shells = Array(reloadData.shells).fill(0); } if (interval > 0 && totalTime.current) { totalTime.current.textContent = interval.toFixed(RELOAD_PRECISION); } if (interval > 0 && remainingTime.current) { remainingTime.current.textContent = ((1 - reload) * interval).toFixed( RELOAD_PRECISION, ); } for (const [index, bar] of shellBars.current.entries()) { if (bar) bar.style.width = `${(shells[index] ?? 0) * 100}%`; } if (ring.current && glow.current && core.current) { const angle = reload * 2 * Math.PI; ring.current.style.background = `conic-gradient(var(--amber-9) 0 ${angle}rad, var(--orange-7) ${angle}rad)`; core.current.style.background = `conic-gradient(var(--amber-6) 0 ${angle}rad, var(--orange-3) ${angle}rad)`; glow.current.style.background = `conic-gradient(var(--amber-9) 0 ${angle}rad, transparent ${angle}rad)`; } if (!cancel) requestAnimationFrame(frame); lastTime = now; lastState = state.current; } frame(); return () => { cancel = true; }; }, [reloadData, shoot]); return (
{(0).toFixed(RELOAD_PRECISION)} {(0).toFixed(RELOAD_PRECISION)} {reloadData.shells > 1 && (