PT
games/rob-revisited / src / Revisited / Sim / Talents.php · commit f9a1307
<?phpnamespace App\Revisited\Sim;/** * Experience, levels and talents (docs/reborn.md "Experience and talents"). Rob earns experience by killing beasts (by * how dangerous they are) and hostile people, and a little for other feats (a recipe made for the first time, a new * region reached, a chapter of the story, a perfect counter). Each level gives one talent point to spend in three * branches: Combat, Survie, Artisanat. Pure data and maths: Land\Game applies the effects (see the getters). * * Levels: from level L to L + 1 takes round(XP_BASE * L^XP_POW) experience (40, 113, 208, 320, 447...), MAX_LEVEL at most. */final class Talents{    public const XP_BASE = 40.0;    public const XP_POW = 1.5;    public const MAX_LEVEL = 20;    /** experience for a kill, by species (danger); a boss counts BOSS_XP */    public const KILL_XP = [        'grazer' => 8, 'bird' => 3, 'fish' => 2, 'climber' => 10, 'swarm' => 6, 'lava_crab' => 10, 'crawler' => 16,        'burrower' => 30, 'stalker' => 40, 'river_croc' => 55, 'glacier_brute' => 70, 'stalker_cub' => 0,    ];    public const BOSS_XP = 200;    /** a hostile human beaten (the leader: twice) */    public const HUMAN_XP = 35;    /** smaller feats */    public const FEAT_XP = ['recipe' => 6, 'craft' => 1, 'zone' => 25, 'chapter' => 40, 'counter' => 4];    public const BRANCHES = ['combat' => 'Combat', 'survival' => 'Survie', 'craft' => 'Artisanat'];    /**     * id => [branch, name, max ranks, what one rank does (shown), the effect per rank in words for the totals]     * Effects are applied by the getters below (keep the texts and the numbers together).     */    public const TREE = [        'power' => ['combat', 'Bras puissant', 3, 'Coups de mêlée +8 % de dégâts par rang.'],        'timing' => ['combat', 'Sens du rythme', 3, 'Zone critique du curseur +30 % et fenêtre de contre +0,12 s par rang.'],        'stance' => ['combat', 'Appuis solides', 3, 'Blessures reçues −8 % et vacillement −25 % par rang.'],        'footwork' => ['combat', 'Jeu de jambes', 2, 'Esquive : −25 % d\'effort et +0,08 s d\'invulnérabilité par rang.'],        'frugal' => ['survival', 'Estomac frugal', 3, 'La faim vient 8 % moins vite par rang.'],        'camel' => ['survival', 'Gorge sèche', 3, 'La soif vient 8 % moins vite par rang.'],        'thick' => ['survival', 'Peau dure', 3, 'Tu ressens le froid 1,5 °C de moins par rang.'],        'forager' => ['survival', 'Cueilleur', 3, 'Cueillir et récolter : 20 % de chances par rang d\'en trouver un de plus.'],        'care' => ['craft', 'Soin des outils', 3, 'Armes et outils s\'usent 12 % moins vite par rang.'],        'butcher' => ['craft', 'Dépeceur', 3, 'Dépeçage : +20 % de viande, peau, fourrure et graisse par rang.'],        'quick' => ['craft', 'Mains habiles', 3, 'Fabriquer prend 15 % de temps en moins par rang.'],        'mender' => ['craft', 'Réparateur', 2, 'Réparer une arme rend +20 points d\'état de plus par rang.'],    ];    public float $xp = 0.0;    /** @var array<string, int> talent id => ranks bought */    public array $ranks = [];    // ------------------------------------------------------------------------------------------ levels    /** Experience from level $level to the next one. */    public static function need(int $level): int    {        return (int) round(self::XP_BASE * $level ** self::XP_POW);    }    /** Total experience to reach $level (level 1: 0). */    public static function total(int $level): int    {        $t = 0;        for ($l = 1; $l < $level; $l++) {            $t += self::need($l);        }        return $t;    }    public function level(): int    {        $l = 1;        $left = $this->xp;        while ($l < self::MAX_LEVEL && $left >= self::need($l)) {            $left -= self::need($l);            $l++;        }        return $l;    }    /** @return array{0: float, 1: int} experience into the current level, experience the level needs (0 at the top) */    public function progress(): array    {        $l = $this->level();        if ($l >= self::MAX_LEVEL) {            return [0.0, 0];        }        return [$this->xp - self::total($l), self::need($l)];    }    /** Points earned (one per level above the first) minus the points spent. */    public function points(): int    {        return $this->level() - 1 - array_sum($this->ranks);    }    /** Adds experience; returns the number of levels gained. */    public function gain(float $xp): int    {        if (!($xp > 0) || !is_finite($xp)) {            return 0;        }        $before = $this->level();        $this->xp += $xp;        return $this->level() - $before;    }    public static function killXp(string $species, bool $boss = false): int    {        return $boss ? self::BOSS_XP : (int) (self::KILL_XP[$species] ?? 5);    }    // ------------------------------------------------------------------------------------------ talents    public function rank(string $id): int    {        return (int) ($this->ranks[$id] ?? 0);    }    public static function max(string $id): int    {        return (int) (self::TREE[$id][2] ?? 0);    }    /** Why a point cannot go into this talent (French), null: it can. */    public function cannot(string $id): ?string    {        if (!isset(self::TREE[$id])) {            return 'Talent inconnu.';        }        if ($this->rank($id) >= self::max($id)) {            return 'Ce talent est déjà au plus haut.';        }        if ($this->points() <= 0) {            return 'Pas de point de talent : gagne un niveau (l\'expérience vient des bêtes vaincues).';        }        return null;    }    /** Spends a point; returns what happened (French). */    public function learn(string $id): string    {        $why = $this->cannot($id);        if ($why !== null) {            return $why;        }        $this->ranks[$id] = $this->rank($id) + 1;        return 'Talent appris : ' . self::TREE[$id][1] . ' (rang ' . $this->ranks[$id] . ' / ' . self::max($id) . ').';    }    /** @return list<string> the talents of a branch, in order */    public static function of(string $branch): array    {        $out = [];        foreach (self::TREE as $id => $t) {            if ($t[0] === $branch) {                $out[] = $id;            }        }        return $out;    }    // ------------------------------------------------------------------------------------------ the effects    /** melee damage times this */    public function damageMul(): float    {        return 1.0 + 0.08 * $this->rank('power');    }    /** the precision cursor's critical zone (fraction of the bar) */    public function sweetWidth(): float    {        return 0.16 * (1.0 + 0.3 * $this->rank('timing'));    }    /** seconds added to the counter window */    public function counterBonus(): float    {        return 0.12 * $this->rank('timing');    }    /** wounds taken times this */    public function woundMul(): float    {        return 1.0 - 0.08 * $this->rank('stance');    }    /** the stagger after a hit times this */    public function staggerMul(): float    {        return 1.0 - 0.25 * $this->rank('stance');    }    /** the dodge's effort times this */    public function dodgeCostMul(): float    {        return 1.0 - 0.25 * $this->rank('footwork');    }    /** seconds of the dodge's invulnerability added */    public function dodgeBonus(): float    {        return 0.08 * $this->rank('footwork');    }    public function hungerMul(): float    {        return 1.0 - 0.08 * $this->rank('frugal');    }    public function thirstMul(): float    {        return 1.0 - 0.08 * $this->rank('camel');    }    /** degrees of cold not felt */    public function coldResist(): float    {        return 1.5 * $this->rank('thick');    }    /** the chance of an extra item when foraging */    public function forageChance(): float    {        return 0.2 * $this->rank('forager');    }    /** weapon and tool wear times this */    public function wearMul(): float    {        return 1.0 - 0.12 * $this->rank('care');    }    /** $n of a butchering yield, with the talent: +20 % a rank, rounded */    public function butcher(int $n): int    {        return $n + (int) floor($n * 0.2 * $this->rank('butcher') + 0.5);    }    /** crafting time times this */    public function craftTimeMul(): float    {        return 1.0 - 0.15 * $this->rank('quick');    }    /** condition points added to a weapon's repair */    public function repairBonus(): float    {        return 20.0 * $this->rank('mender');    }    // ------------------------------------------------------------------------------------------ save    /** @return array{xp: float, ranks: array<string, int>} */    public function toArray(): array    {        return ['xp' => round($this->xp, 2), 'ranks' => $this->ranks];    }    /** Older saves have no talents: level 1, nothing learnt. */    public static function fromArray(mixed $a): self    {        $t = new self();        if (!is_array($a)) {            return $t;        }        $t->xp = max(0.0, (float) ($a['xp'] ?? 0));        foreach ((array) ($a['ranks'] ?? []) as $id => $n) {            if (isset(self::TREE[(string) $id])) {                $t->ranks[(string) $id] = max(0, min(self::max((string) $id), (int) $n));            }        }        // never more ranks than the level allows (a hand-edited save)        while (array_sum($t->ranks) > $t->level() - 1 && $t->ranks !== []) {            $last = array_key_last($t->ranks);            $t->ranks[$last]--;            if ($t->ranks[$last] <= 0) {                unset($t->ranks[$last]);            }        }        return $t;    }}