JA
games/rob-revisited / src / Revisited / Sim / Weapons.php · commit f9a1307
<?phpnamespace App\Revisited\Sim;/** * What every weapon does (docs/reborn.md "Wave 2"): melee or ranged, damage, reach, time between two blows, ammunition * (best first), wear per use (its condition lives in Inventory::$cond, 100 new .. 0 broken) and what repairs it. * Energy weapons spend the charge of a cell (`shots` per energy_cell). The hands are the weapon of last resort. */final class Weapons{    /** keys: type (melee|ranged), damage, range, cool, wear; optional ammo (list), acc, repair (item => n), shots, fire, scare */    public const STATS = [        'hands' => ['type' => 'melee', 'damage' => 4, 'range' => 2.2, 'cool' => 0.6, 'wear' => 0],        'stick' => ['type' => 'melee', 'damage' => 6, 'range' => 2.4, 'cool' => 0.6, 'wear' => 2.0],        'stone' => ['type' => 'melee', 'damage' => 5, 'range' => 2.0, 'cool' => 0.6, 'wear' => 0],        'torch' => ['type' => 'melee', 'damage' => 8, 'range' => 2.3, 'cool' => 0.7, 'wear' => 0, 'fire' => true],        'club' => ['type' => 'melee', 'damage' => 14, 'range' => 2.5, 'cool' => 0.85, 'wear' => 1.2, 'repair' => ['wood' => 1, 'vine' => 1]],        'knife_flint' => ['type' => 'melee', 'damage' => 12, 'range' => 2.0, 'cool' => 0.45, 'wear' => 1.0, 'repair' => ['flint' => 1]],        'axe_stone' => ['type' => 'melee', 'damage' => 20, 'range' => 2.3, 'cool' => 0.8, 'wear' => 1.0, 'repair' => ['stone' => 1, 'vine' => 1]],        'spear' => ['type' => 'melee', 'damage' => 26, 'range' => 3.2, 'cool' => 0.9, 'wear' => 1.0, 'repair' => ['flint' => 1, 'stick' => 1]],        'machete' => ['type' => 'melee', 'damage' => 22, 'range' => 2.4, 'cool' => 0.5, 'wear' => 0.5, 'repair' => ['metal_scrap' => 1]],        'harpoon' => ['type' => 'melee', 'damage' => 18, 'range' => 3.0, 'cool' => 0.8, 'wear' => 0.8, 'repair' => ['bone' => 1, 'rope' => 1]],        'raft_pole' => ['type' => 'melee', 'damage' => 7, 'range' => 3.4, 'cool' => 0.8, 'wear' => 1.0],        'sling' => ['type' => 'ranged', 'damage' => 10, 'range' => 25, 'cool' => 1.0, 'ammo' => ['stone'], 'acc' => 0.8, 'wear' => 0.5, 'repair' => ['fiber' => 2]],        'bow' => ['type' => 'ranged', 'damage' => 30, 'range' => 40, 'cool' => 0.9, 'ammo' => ['arrow_metal', 'arrow_bone', 'arrow'], 'acc' => 0.92, 'wear' => 0.5, 'repair' => ['fiber' => 2, 'resin' => 1]],        'crossbow' => ['type' => 'ranged', 'damage' => 45, 'range' => 55, 'cool' => 2.0, 'ammo' => ['bolt'], 'acc' => 0.96, 'wear' => 0.4, 'repair' => ['metal_scrap' => 1, 'rope' => 1]],        'flare_gun' => ['type' => 'ranged', 'damage' => 15, 'range' => 35, 'cool' => 1.5, 'ammo' => ['flare'], 'acc' => 0.9, 'wear' => 0.3, 'fire' => true, 'scare' => true, 'repair' => ['metal_scrap' => 1]],        'pulse_rifle' => ['type' => 'ranged', 'damage' => 70, 'range' => 70, 'cool' => 0.8, 'ammo' => ['energy_cell'], 'shots' => 6, 'acc' => 0.97, 'wear' => 0.3, 'scare' => true, 'repair' => ['gun_parts' => 1]],        'plasma_gun' => ['type' => 'ranged', 'damage' => 120, 'range' => 50, 'cool' => 1.2, 'ammo' => ['energy_cell'], 'shots' => 3, 'acc' => 0.98, 'wear' => 0.25, 'fire' => true, 'scare' => true, 'repair' => ['gun_parts' => 1, 'wire' => 1]],    ];    /**     * Shields (the left arm, Game::guard()): block = the share of a frontal blow they stop, wear = condition lost per     * blow taken (times 0.5 + its severity), tire = rest spent per blow held, repair = materials (Game::repairWeapon()).     */    public const SHIELDS = [        'shield_hide' => ['block' => 0.6, 'wear' => 7.0, 'tire' => 3.0, 'repair' => ['hide' => 1, 'rope' => 1]],        'shield_scrap' => ['block' => 0.8, 'wear' => 3.0, 'tire' => 4.5, 'repair' => ['metal_scrap' => 1, 'wire' => 1]],    ];    /** A shield's stats, null: not a shield. @return array{block: float, wear: float, tire: float, repair: array<string, int>}|null */    public static function shield(?string $id): ?array    {        return $id !== null ? (self::SHIELDS[$id] ?? null) : null;    }    /**     * How a blow is drawn in first person and when it lands: animation kind => seconds from the press to the strike     * (the anticipation: a knife stabs at once, an axe is raised first, a bow is drawn, a sling whirled). The damage     * lands at the strike, the recovery fills the rest of the weapon's `cool`.     */    public const WINDUP = ['punch' => 0.1, 'stab' => 0.1, 'swing' => 0.22, 'chop' => 0.16, 'thrust' => 0.18, 'bow' => 0.42,        'crossbow' => 0.06, 'gun' => 0.05, 'sling' => 0.45];    /** The animation of a blow with this item (Render's first-person poses). */    public static function anim(?string $id): string    {        return match ($id) {            null, 'hands' => 'punch',            'knife_flint' => 'stab',            'machete' => 'chop',            'spear', 'harpoon', 'raft_pole' => 'thrust',            'bow' => 'bow',            'crossbow' => 'crossbow',            'flare_gun', 'pulse_rifle', 'plasma_gun' => 'gun',            'sling' => 'sling',            default => 'swing',        };    }    /** Seconds from the press to the strike. */    public static function windup(?string $id): float    {        return self::WINDUP[self::anim($id)];    }    /** Seconds from the press to the next blow: the weapon's cool, never shorter than its strike and a recovery. */    public static function duration(?string $id): float    {        return max((float) self::of($id)['cool'], self::windup($id) + 0.25);    }    /** @return array<string, mixed> the stats of what is in hand (null: the fists) */    public static function of(?string $id): array    {        if ($id !== null && isset(self::STATS[$id])) {            return self::STATS[$id];        }        if ($id !== null && Items::exists($id)) {            // any other object swung: its damage, an ordinary reach            return ['type' => 'melee', 'damage' => (float) (Items::get($id)['damage'] ?? 4), 'range' => 2.2, 'cool' => 0.7, 'wear' => 0];        }        return self::STATS['hands'];    }    public static function ranged(?string $id): bool    {        return self::of($id)['type'] === 'ranged';    }    /** The ammunition of the bag this weapon fires next, null: none left. */    public static function ammo(Inventory $inv, ?string $id): ?string    {        foreach ((array) (self::of($id)['ammo'] ?? []) as $a) {            if ($inv->has((string) $a)) {                return (string) $a;            }        }        return null;    }    /** Everything a weapon can fire, in words (for the HUD). */    public static function ammoLabel(?string $id): string    {        $names = [];        foreach ((array) (self::of($id)['ammo'] ?? []) as $a) {            $names[] = mb_strtolower(Items::name((string) $a));        }        return implode(' ou ', $names);    }    /** The weapons of the catalogue, weakest first (for tests and the journal). @return list<string> */    public static function all(): array    {        $ids = array_keys(self::STATS);        $ids = array_values(array_filter($ids, fn (string $i) => $i !== 'hands'));        usort($ids, fn (string $a, string $b) => self::STATS[$a]['damage'] <=> self::STATS[$b]['damage']);        return $ids;    }}