JA
games/rob-revisited / src / Revisited / Land / People.php · commit f9a1307
<?phpnamespace App\Revisited\Land;/** * The named people of Ysor (docs/reborn.md "Wave 2"): who they are, their group, their personality, and what they think * of Rob. Each keeps a hidden trust (-100 hostile .. 100 devoted), a mood (the last reaction), a memory of what Rob said * or did (ids), and the days he came. What one hears, the others of the group soon know (gossip); the Haldi and the * Rôdeurs hate each other: helping one cools the other. * * Tones of an answer: kind, honest, lie, threat, gift, joke, flatter, trade, neutral. A personality reacts to each * (REACT); a liar is caught by the wise and the paranoid. */final class People{    /** key => [name, group, personality, zone, role, short description] */    public const WHO = [        'maeva' => ['Maëva', 'haldi', 'wise', 'canyon', 'elder', 'la doyenne des Haldi, petite-fille d\'une pilote de la colonie'],        'oko' => ['Oko', 'haldi', 'greedy', 'canyon', 'trader', 'le marchand des Haldi, qui échange tout'],        'sela' => ['Sela', 'haldi', 'empathic', 'canyon', 'healer', 'la guérisseuse des Haldi'],        'koru' => ['Koru', 'haldi', 'proud', 'canyon', 'smith', 'le forgeron des Haldi, qui rêve de chasser les Rôdeurs'],        'aika' => ['Aïka', 'ouvi', 'proud', 'jungle', 'chief', 'la cheffe des Ouvi, gardienne du totem des écailles'],        'tamu' => ['Tamu', 'ouvi', 'empathic', 'jungle', 'fisher', 'un pêcheur ouvi, passeur du fleuve'],        'nadje' => ['Nadjé', 'ouvi', 'greedy', 'jungle', 'trader', 'la marchande et brasseuse des Ouvi'],        'vask' => ['Vask le Balafré', 'rodeur', 'proud', 'canyon', 'chief', 'le chef des Rôdeurs, qui porte la clé d\'allumage au cou'],        'grell' => ['Grell la Fouine', 'rodeur', 'coward', 'canyon', 'fence', 'un Rôdeur peureux qui revend ce que les autres pillent'],        'mira' => ['Mira', 'rodeur', 'paranoid', 'canyon', 'archer', 'l\'archère des Rôdeurs, qui ne dort jamais'],        'orsolo' => ['Orsolo', 'hermit', 'mad', 'marsh', 'hermit', 'le dernier officier de vol de la colonie, devenu fou dans le marais'],        'kessa' => ['Kessa', 'hermit', 'paranoid', 'marsh', 'widow', 'une veuve qui vit seule dans les ruines sur pilotis'],        'jonas' => ['Jonas Merle', 'colonist', 'wise', 'caves3', 'engineer', 'l\'ingénieur de la nacelle MR-4 du Meridian, perdu sur Ysor depuis trois ans'],    ];    public const PERSONALITY = [        'wise' => 'sage', 'greedy' => 'cupide', 'empathic' => 'bienveillant', 'proud' => 'fier', 'coward' => 'lâche',        'paranoid' => 'paranoïaque', 'mad' => 'fou',    ];    /** personality => tone => trust change */    public const REACT = [        'wise' => ['kind' => 4, 'honest' => 8, 'lie' => -15, 'threat' => -12, 'gift' => 5, 'joke' => 2, 'flatter' => -4, 'trade' => 1],        'greedy' => ['kind' => 1, 'honest' => 2, 'lie' => -2, 'threat' => -6, 'gift' => 12, 'joke' => 2, 'flatter' => 3, 'trade' => 5],        'empathic' => ['kind' => 10, 'honest' => 6, 'lie' => -6, 'threat' => -15, 'gift' => 8, 'joke' => 4, 'flatter' => 1, 'trade' => 1],        'proud' => ['kind' => 3, 'honest' => 6, 'lie' => -8, 'threat' => -20, 'gift' => 5, 'joke' => -4, 'flatter' => 9, 'trade' => 2],        'coward' => ['kind' => 6, 'honest' => 2, 'lie' => 0, 'threat' => 6, 'gift' => 8, 'joke' => 3, 'flatter' => 4, 'trade' => 3],        'paranoid' => ['kind' => 2, 'honest' => 8, 'lie' => -20, 'threat' => -18, 'gift' => 3, 'joke' => -6, 'flatter' => -8, 'trade' => 2],        'mad' => ['kind' => 3, 'honest' => -3, 'lie' => 6, 'threat' => -10, 'gift' => 4, 'joke' => 10, 'flatter' => 5, 'trade' => 0],    ];    /** the face a person shows for a tone (for the dialogue line), by personality: one text, or [said of a woman, said of a man] (whole sentences: the English translates each) */    private const MOOD = [        'wise' => ['lie' => ['(Un long regard. Elle n\'est pas dupe.)', '(Un long regard. Il n\'est pas dupe.)'], 'threat' => '(Le silence se fait, glacé.)', 'honest' => ['(Elle hoche lentement la tête.)', '(Il hoche lentement la tête.)']],        'greedy' => ['gift' => '(Ses yeux brillent.)', 'threat' => ['(Elle recule en serrant sa bourse.)', '(Il recule en serrant sa bourse.)'], 'trade' => ['(Elle se frotte les mains.)', '(Il se frotte les mains.)']],        'empathic' => ['kind' => '(Un sourire chaleureux.)', 'threat' => ['(Elle a l\'air blessée.)', '(Il a l\'air blessé.)'], 'gift' => ['(Elle est touchée.)', '(Il est touché.)']],        'proud' => ['flatter' => '(Le menton se redresse.)', 'threat' => '(La main se pose sur une arme.)', 'joke' => '(Un pli de mépris.)'],        'coward' => ['threat' => ['(Elle tremble comme une feuille.)', '(Il tremble comme une feuille.)'], 'kind' => ['(Elle se détend un peu.)', '(Il se détend un peu.)']],        'paranoid' => ['lie' => ['(Elle plisse les yeux : « Menteur. »)', '(Il plisse les yeux : « Menteur. »)'], 'joke' => '(« Tu te moques de moi ? »)', 'honest' => ['(Elle baisse un peu sa garde.)', '(Il baisse un peu sa garde.)']],        'mad' => ['joke' => ['(Elle éclate d\'un rire qui ne s\'arrête plus.)', '(Il éclate d\'un rire qui ne s\'arrête plus.)'], 'lie' => '(« Oui... oui, je le savais ! »)', 'honest' => ['(Elle te regarde comme un fantôme.)', '(Il te regarde comme un fantôme.)']],    ];    /** the groups that hate each other: a gain with one is a small loss with the other */    private const FOES = ['haldi' => 'rodeur', 'rodeur' => 'haldi'];    public static function name(string $key): string    {        return self::WHO[$key][0] ?? $key;    }    public static function group(string $key): string    {        return self::WHO[$key][1] ?? '';    }    public static function personality(string $key): string    {        return self::WHO[$key][2] ?? 'wise';    }    /** @return array{trust: float, mood: string, mem: list<string>, days: list<int>} */    public static function get(Game $g, string $key): array    {        $p = $g->people[$key] ?? [];        return ['trust' => (float) ($p['trust'] ?? self::start($key)), 'mood' => (string) ($p['mood'] ?? ''), 'mem' => array_values((array) ($p['mem'] ?? [])),            'days' => array_values(array_map('intval', (array) ($p['days'] ?? [])))];    }    public static function trust(Game $g, string $key): float    {        return self::get($g, $key)['trust'];    }    /** How a group feels at first. */    private static function start(string $key): float    {        return match (self::group($key)) {            'haldi' => 10.0, 'ouvi' => -5.0, 'rodeur' => -40.0, 'hermit' => -15.0, default => 20.0,        };    }    /** Changes a person's trust (and, softened, their group's, the enemies' the other way). */    public static function change(Game $g, string $key, float $delta, bool $gossip = true): void    {        $p = self::get($g, $key);        $p['trust'] = max(-100.0, min(100.0, $p['trust'] + $delta));        $g->people[$key] = $p;        if (!$gossip || abs($delta) < 0.5) {            return;        }        $group = self::group($key);        foreach (self::WHO as $other => $w) {            if ($other === $key) {                continue;            }            $k = $w[1] === $group ? 0.3 : (($w[1] === (self::FOES[$group] ?? '-')) && $delta > 0 ? -0.12 : 0.0);            if ($k !== 0.0) {                $q = self::get($g, $other);                $q['trust'] = max(-100.0, min(100.0, $q['trust'] + $delta * $k));                $g->people[$other] = $q;            }        }    }    /** A person reacts to the tone of an answer: trust and mood; returns the mood line ('' if none). */    public static function react(Game $g, string $key, string $tone): string    {        $pers = self::personality($key);        $delta = (float) (self::REACT[$pers][$tone] ?? 0);        if ($pers === 'mad' && $tone !== 'neutral') {            // a mad man's mood swings: half the time the other way            $delta = $g->rng()->chance(0.3) ? -$delta : $delta;        }        if ($delta !== 0.0) {            self::change($g, $key, $delta);        }        $mood = self::MOOD[$pers][$tone] ?? '';        $p = self::get($g, $key);        $p['mood'] = $tone;        $g->people[$key] = $p;        if ($tone === 'lie' || $tone === 'threat') {            self::remember($g, $key, $tone);        }        return is_array($mood) ? $mood[self::isWoman($key) ? 0 : 1] : $mood;    }    public static function isWoman(string $key): bool    {        return in_array($key, ['maeva', 'sela', 'aika', 'nadje', 'mira', 'kessa'], true);    }    public static function remember(Game $g, string $key, string $what): void    {        $p = self::get($g, $key);        if (!in_array($what, $p['mem'], true)) {            $p['mem'][] = $what;        }        $g->people[$key] = $p;    }    public static function recalls(Game $g, string $key, string $what): bool    {        return in_array($what, self::get($g, $key)['mem'], true);    }    /** Counts a visit (once a day). @return int how many different days Rob came */    public static function visit(Game $g, string $key): int    {        $p = self::get($g, $key);        $d = $g->day();        if (!in_array($d, $p['days'], true)) {            $p['days'][] = $d;        }        $g->people[$key] = $p;        return count($p['days']);    }    /** A word for the player: how the person seems toward him. */    public static function attitude(Game $g, string $key): string    {        $t = self::trust($g, $key);        return match (true) {            $t <= -60 => 'hostile', $t <= -20 => 'méfiant', $t < 15 => 'réservé', $t < 45 => 'amical', default => 'confiant',        };    }    /** The mean trust of a group (merchants' prices, gate checks). */    public static function reputation(Game $g, string $group): float    {        $sum = 0.0;        $n = 0;        foreach (self::WHO as $key => $w) {            if ($w[1] === $group) {                $sum += self::trust($g, $key);                $n++;            }        }        return $n > 0 ? $sum / $n : 0.0;    }    /** The key of a named NPC (Npc::$data['key']), null for others. */    public static function keyOf(Npc $n): ?string    {        $k = $n->data['key'] ?? null;        return is_string($k) && isset(self::WHO[$k]) ? $k : null;    }    /**     * The named people of the zones besides the Haldi and Rôdeurs of Folk: the Ouvi of the jungle, the hermits of the     * marsh, Jonas in the caves (or at the shuttle, once rescued). Called by Folk::populate().     */    public static function populate(Game $g): void    {        $id = $g->zone->id;        if ($id === 'jungle') {            [$ox, $oz] = ZoneFactory::ouviCentre($g->seed);            $hostile = !empty($g->flags['ouvi_hostile']);            $posts = ['aika' => [$ox + 5, $oz - 1], 'tamu' => [$ox + 30, $oz + 4], 'nadje' => [$ox - 8, $oz + 6]];            foreach ($posts as $key => [$x, $z]) {                if (!empty($g->flags['dead_' . $key]) || $g->companion === $key) {                    continue;                }                $n = self::npc($g, $key, 'ouvi', $x, $z, 300 + count($g->npcs));                $n->data['post'] = [$x, $z];                $n->data['home'] = [$x, $z];                $n->data['centre'] = [$ox, $oz];                $n->data['spots'] = [];                $n->data['role'] = self::WHO[$key][4];                if ($hostile) {                    self::makeHostile($n, $ox, $oz);                }                $g->npcs[] = $n;            }            foreach ([['Oumi', 'villager'], ['Saro', 'villager'], ['Pépé Lio', 'villager']] as $k => [$name, $role]) {                $a = $k * 2.1;                $x = $ox + sin($a) * 10;                $z = $oz + cos($a) * 10;                $n = new Npc(320 + $k, $name, 'ouvi', $x, $g->zone->heightAt($x, $z), $z, $a);                $n->data = ['role' => $role, 'home' => [$x, $z], 'spots' => [[$ox, $oz], [$ox + 10, $oz + 8]], 'centre' => [$ox, $oz], 'ouviLine' => $k];                if ($hostile) {                    self::makeHostile($n, $ox, $oz);                }                $g->npcs[] = $n;            }        }        if ($id === 'marsh') {            [$hx, $hz] = ZoneFactory::HERMIT;            [$rx, $rz] = ZoneFactory::RUINS;            foreach (['orsolo' => [$hx + 2, $hz + 3], 'kessa' => [$rx + 3, $rz - 12]] as $key => [$x, $z]) {                if (!empty($g->flags['dead_' . $key])) {                    continue;                }                $n = self::npc($g, $key, 'hermit', $x, $z, 400 + count($g->npcs));                $n->data += ['post' => [$x, $z], 'home' => [$x, $z], 'centre' => [$x, $z], 'spots' => [], 'role' => self::WHO[$key][4]];                $g->npcs[] = $n;            }        }        $jonasHere = !empty($g->flags['jonas_free']) ? $id === 'coast' : $id === 'caves3';        if ($jonasHere && empty($g->flags['dead_jonas']) && $g->companion !== 'jonas') {            [$x, $z] = $id === 'coast' ? [ZoneFactory::SHUTTLE[0] - 10, ZoneFactory::SHUTTLE[1] + 6] : [138.0, 181.0];            $n = self::npc($g, 'jonas', 'colonist', $x, $z, 450);            $n->data += ['post' => [$x, $z], 'home' => [$x, $z], 'centre' => [$x, $z], 'spots' => [], 'role' => 'engineer'];            $n->state = empty($g->flags['jonas_free']) ? 'hurt' : 'work';            $g->npcs[] = $n;        }    }    public static function npc(Game $g, string $key, string $faction, float $x, float $z, int $id): Npc    {        $n = new Npc($id, self::name($key), $faction, $x, $g->zone->heightAt($x, $z), $z, 0.0, 'idle');        $n->data = ['key' => $key, 'personality' => self::personality($key), 'the' => self::name($key)];        return $n;    }    /** People of a friendly group who turned against Rob fight like the Rôdeurs (Folk::rodeur()). */    public static function makeHostile(Npc $n, float $cx, float $cz): void    {        $n->hostile = true;        $n->state = 'patrol';        $n->health = 90.0;        $patrol = [];        for ($p = 0; $p < 4; $p++) {            $patrol[] = [$cx + sin($p * 1.57 + $n->id) * 16, $cz + cos($p * 1.57 + $n->id) * 16];        }        $n->data += ['arm' => $n->id % 2 === 0 ? 'bow' : 'melee', 'leader' => false, 'patrol' => $patrol, 'p' => 0, 'camp' => [$cx, $cz], 'cool' => 0.0, 'lost' => 0.0];        $n->data['the'] = $n->data['the'] ?? $n->name;    }}