DE
games/rob-revisited / src / Revisited / Land / Folk.php · commit f9a1307
<?phpnamespace App\Revisited\Land;use App\Revisited\Sim\Talents;/** * The humans of the canyon (docs/reborn.md "Npc factions"): the Haldi go about their day in the village and sleep * in their huts at night; the Rôdeurs patrol their camp, shoot or strike Rob on sight (less far at night, much * less when he crouches), flee when badly hurt and leave their gear when they die. */final class Folk{    public const HALDI = [        ['Maëva', 'elder', 'Maëva'],        ['Oko', 'trader', 'Oko'],        ['Sela', 'healer', 'Sela'],        ['Tilo', 'child', 'Tilo'],        ['Nia', 'child', 'Tilo'],        ['Bren', 'villager', 'Bren'],        ['Ysa', 'villager', 'Ysa'],        ['Koru', 'villager', 'Koru'],    ];    public const RODEURS = [        ['Vask le Balafré', 'melee', true, 'Vask le Balafré'],        ['Rôdeur à l\'arc', 'bow', false, 'le rôdeur à l\'arc'],        ['Rôdeur à l\'arc', 'bow', false, 'le rôdeur à l\'arc'],        ['Rôdeur au gourdin', 'melee', false, 'le rôdeur au gourdin'],        ['Mira', 'bow', false, 'Mira l\'archère'],        ['Grell la Fouine', 'melee', false, 'Grell la Fouine'],    ];    /** the named ones among them (index in RODEURS => People key) */    private const RODEUR_KEYS = [0 => 'vask', 4 => 'mira', 5 => 'grell'];    private const HALDI_KEYS = ['Maëva' => 'maeva', 'Oko' => 'oko', 'Sela' => 'sela', 'Koru' => 'koru'];    private const PARTS = ['head', 'torso', 'larm', 'rarm', 'lleg', 'rleg'];    /** The people of a zone, when Rob enters it. */    public static function populate(Game $g): void    {        $g->npcs = [];        People::populate($g);        Companions::populate($g);        if ($g->zone->id !== 'canyon') {            return;        }        [$vx, $vz] = ZoneFactory::villageCentre($g->seed);        $homes = [];        $spots = [];        foreach ($g->zone->things as $t) {            if (($t->kind === 'hut' || $t->kind === 'tent') && !empty($t->data['owner'])) {                $homes[(string) $t->data['owner']] = [$t->x, $t->z];            }            if (in_array($t->kind, ['bush_berry', 'water_spring', 'skin_rack', 'campfire'], true) && hypot($t->x - $vx, $t->z - $vz) < 50) {                $spots[] = [$t->x, $t->z];            }        }        foreach (self::HALDI as $k => [$name, $role, $home]) {            $h = $homes[$home] ?? [$vx + 10, $vz + 10];            $a = $k * 0.8;            $x = $vx + sin($a) * 8;            $z = $vz + cos($a) * 8;            $n = new Npc(100 + $k, $name, 'haldi', $x, $g->zone->heightAt($x, $z), $z, $a + 3.14, 'idle');            $n->data = ['role' => $role, 'home' => $h, 'spots' => $spots, 'centre' => [$vx, $vz]];            if (isset(self::HALDI_KEYS[$name])) {                $n->data['key'] = self::HALDI_KEYS[$name];                $n->data['personality'] = People::personality(self::HALDI_KEYS[$name]);            }            if (($n->data['key'] ?? '') === 'koru') {                $n->data['post'] = [$vx + 14, $vz + 16];       // at his forge            }            if ($role === 'elder') {                $n->data['post'] = [$vx + 4, $vz + 3];            }            if ($role === 'trader') {                $n->data['post'] = [$vx + sin(0.9) * 30, $vz + cos(0.9) * 30 - 2];            }            if ($role === 'healer') {                $n->data['post'] = [$h[0] + 2, $h[1] + 2];            }            $g->npcs[] = $n;        }        [$rx, $rz] = ZoneFactory::campCentre($g->seed);        foreach (self::RODEURS as $k => [$name, $arm, $leader, $the]) {            $id = 200 + $k;            if (!empty($g->flags['rodeur_dead'][$id]) || ($g->companion !== null && $g->companion === (self::RODEUR_KEYS[$k] ?? '-'))) {                continue;            }            $a = $k * 1.3;            $patrol = [];            for ($p = 0; $p < 4; $p++) {                $pa = $a + $p * 1.57;                $patrol[] = [$rx + sin($pa) * ($leader ? 8 : 22), $rz + cos($pa) * ($leader ? 8 : 22)];            }            $x = $patrol[0][0];            $z = $patrol[0][1];            $n = new Npc($id, $name, 'rodeur', $x, $g->zone->heightAt($x, $z), $z, $a, 'patrol', 0.0, true);            $n->health = $leader ? 140.0 : 80.0;            $n->data = ['the' => $the, 'arm' => $arm, 'leader' => $leader, 'patrol' => $patrol, 'p' => 0, 'camp' => [$rx, $rz], 'cool' => 0.0, 'lost' => 0.0];            if (isset(self::RODEUR_KEYS[$k])) {                $n->data['key'] = self::RODEUR_KEYS[$k];                $n->data['personality'] = People::personality(self::RODEUR_KEYS[$k]);            }            if ($k === 5) {                // Grell fishes alone by the stream, away from the camp: the one Rôdeur who talks before he shoots                [$vx2, $vz2] = ZoneFactory::villageCentre($g->seed);                $sx = ($vx2 + $rx) / 2;                $sz = ZoneFactory::canyonZ($sx / 512) * 512 + 6;                $n->data['patrol'] = [[$sx, $sz], [$sx + 12, $sz + 2], [$sx + 4, $sz - 3], [$sx - 8, $sz + 1]];                $n->x = $sx;                $n->z = $sz;                $n->y = $g->zone->heightAt($sx, $sz);                $n->health = 60.0;            }            if (self::truce($g)) {                $n->hostile = false;            }            $g->npcs[] = $n;        }    }    public static function update(Game $g, float $dt): void    {        foreach ($g->npcs as $n) {            $n->anim += $dt;            if (!empty($n->data['companion'])) {                continue;     // Companions moves it            }            if ($n->faction === 'rodeur' || $n->hostile) {                self::rodeur($g, $n, $dt);            } elseif ($n->state !== 'hurt') {                self::haldi($g, $n, $dt);            }        }    }    private static function haldi(Game $g, Npc $n, float $dt): void    {        $hour = $g->hour();        $talking = $g->talkingTo() === $n;        if ($talking) {            $n->state = 'talk';            $n->yaw = atan2($g->x - $n->x, $g->z - $n->z);            return;        }        $role = (string) $n->data['role'];        $night = $hour >= 21.5 || $hour < 6;        if ($night) {            [$hx, $hz] = $n->data['home'];            if (hypot($n->x - $hx, $n->z - $hz) > 2.5) {                $n->state = 'walk';                self::walk($g, $n, $hx, $hz, 1.4, $dt);            } else {                $n->state = 'sleep';            }            return;        }        if (isset($n->data['post']) && !($role === 'healer' && $hour > 16)) {            [$px, $pz] = $n->data['post'];            if (hypot($n->x - $px, $n->z - $pz) > 1.5) {                $n->state = 'walk';                self::walk($g, $n, $px, $pz, 1.3, $dt);            } else {                $n->state = $role === 'trader' ? 'work' : 'idle';                $d = hypot($g->x - $n->x, $g->z - $n->z);                if ($d < 8) {                    $n->yaw = atan2($g->x - $n->x, $g->z - $n->z);                }            }            return;        }        // villagers and children go from one place to the next        $n->data['t'] = (float) ($n->data['t'] ?? 0) - $dt;        if (!isset($n->data['goal']) || $n->data['t'] <= 0) {            $spots = $n->data['spots'];            [$cx, $cz] = $n->data['centre'];            if ($role === 'child' || $spots === []) {                $n->data['goal'] = [$cx + $g->rng()->range(-20, 20), $cz + $g->rng()->range(-20, 20)];            } else {                $s = $spots[$g->rng()->int(0, count($spots) - 1)];                $n->data['goal'] = [$s[0] + $g->rng()->range(-2, 2), $s[1] + $g->rng()->range(-2, 2)];            }            $n->data['t'] = $g->rng()->range(8, 20);        }        [$gx, $gz] = $n->data['goal'];        if (hypot($n->x - $gx, $n->z - $gz) > 1.2) {            $n->state = 'walk';            self::walk($g, $n, $gx, $gz, $role === 'child' ? 2.6 : 1.2, $dt);        } else {            $n->state = $role === 'child' ? 'play' : 'work';        }    }    /** Have the Rôdeurs agreed to leave Rob alone (and not been betrayed since)? */    public static function truce(Game $g): bool    {        return !empty($g->flags['rodeur_truce']) && People::trust($g, 'vask') > -50;    }    private static function rodeur(Game $g, Npc $n, float $dt): void    {        $d = hypot($g->x - $n->x, $g->z - $n->z);        $n->data['cool'] = max(0.0, (float) ($n->data['cool'] ?? 0) - $dt);        if (isset($n->data['wu'])) {            $n->data['wu'] = (float) $n->data['wu'] - $dt;     // a blow / an arrow being readied (the telegraph)        }        if ((float) ($n->data['stagger'] ?? 0) > 0) {            $n->data['stagger'] = (float) $n->data['stagger'] - $dt;   // countered: reeling            return;        }        if ($n->state === 'surrender' || ($g->talkingTo() === $n)) {            $n->yaw = atan2($g->x - $n->x, $g->z - $n->z);            return;        }        if ($n->faction === 'rodeur' && (self::truce($g) || ($g->flags['parley_until'] ?? 0) > $g->minutes)) {            // a truce, or a parley asked: they keep their distance, Vask comes to talk            $n->hostile = false;            if (!empty($n->data['leader']) && ($g->flags['parley_until'] ?? 0) > $g->minutes && $d > 3) {                $n->state = 'walk';                self::walk($g, $n, $g->x, $g->z, 2.0, $dt);                return;            }            $n->state = 'idle';            if ($d < 12) {                $n->yaw = atan2($g->x - $n->x, $g->z - $n->z);            }            return;        }        if ($n->faction === 'rodeur') {            $n->hostile = true;        }        if (!empty($n->data['leader']) && $g->isNight() && in_array($n->state, ['patrol', 'idle', 'sleep'], true)) {            // Vask sleeps by the fire at night, the key on his chest: a sneaking thief may take it            [$cx, $cz] = $n->data['camp'];            if (hypot($n->x - $cx - 3, $n->z - $cz - 2) > 1.2) {                self::walk($g, $n, $cx + 3, $cz + 2, 1.4, $dt);                $n->state = 'patrol';            } else {                $n->state = 'sleep';                $heard = ($g->running ? 9.0 : ($g->crouching ? 1.2 : 3.5));                if ($d < $heard && !$g->dead()) {                    $n->state = 'alert';                    $g->say('Vask ouvre un œil, puis bondit : « Un voleur ! »');                }            }            return;        }        if ($n->state === 'flee') {            [$cx, $cz] = $n->data['camp'];            self::walkAway($g, $n, $g->x, $g->z, 4.5, $dt);            if ($d > 45) {                $n->state = 'patrol';                $n->health = max($n->health, 40.0);            }            return;        }        $sight = ($g->isNight() ? 13.0 : 30.0) * ($g->crouching ? 0.45 : 1.0) * ($g->running ? 1.3 : 1.0);        if ($g->nearFire() && $g->isNight()) {            $sight *= 1.8;   // a fire in the night shows where Rob is        }        if ($n->state === 'patrol' || $n->state === 'idle') {            if ($d < $sight && !$g->dead()) {                $n->state = 'alert';                $n->data['lost'] = 0.0;                $g->say(self::the($n, true) . ' t\'a repéré ! « Un étranger ! Attrapez-le ! »');                foreach ($g->npcs as $o) {                    if ($o->faction === 'rodeur' && $o !== $n && $o->state === 'patrol' && hypot($o->x - $n->x, $o->z - $n->z) < 30) {                        $o->state = 'alert';                    }                }                return;            }            $p = (int) $n->data['p'];            [$px, $pz] = $n->data['patrol'][$p];            if (hypot($n->x - $px, $n->z - $pz) < 1.5) {                $n->data['p'] = ($p + 1) % count($n->data['patrol']);                $n->state = 'idle';                $n->data['wait'] = $g->rng()->range(2, 6);            }            if ($n->state === 'idle') {                $n->data['wait'] = (float) ($n->data['wait'] ?? 0) - $dt;                if ($n->data['wait'] <= 0) {                    $n->state = 'patrol';                }                return;            }            self::walk($g, $n, $px, $pz, 1.5, $dt);            return;        }        // alert / attack        if ($d > 60) {            $n->data['lost'] = (float) $n->data['lost'] + $dt;            if ($n->data['lost'] > 10) {                $n->state = 'patrol';                return;            }        } else {            $n->data['lost'] = 0.0;        }        $n->yaw = atan2($g->x - $n->x, $g->z - $n->z);        if ($n->data['arm'] === 'bow' && $d > 6 && $d < 30) {            $n->state = 'attack';            if ($n->data['cool'] <= 0 && !isset($n->data['wu'])) {                $n->data['wu'] = $n->data['wuT'] = Combat::BOW_AIM;     // the bow drawn: a moment to dodge                $g->combat->engage($g, $n);            } elseif ($n->data['cool'] <= 0 && (float) $n->data['wu'] <= 0) {                unset($n->data['wu'], $n->data['wuT']);                $n->data['cool'] = 2.6;                $n->anim = 0;                $p = ($g->combat->dodgeSafe > 0 ? 0.0 : 0.5) * ($g->crouching ? 0.7 : 1.0) * ($g->running ? 0.6 : 1.0) * ($g->isNight() ? 0.7 : 1.0) * (1 - $d / 45);                if ($g->rng()->chance($p)) {                    $part = self::PARTS[$g->rng()->int(0, 5)];                    $sev = $g->rng()->range(0.18, 0.4);                    if (!$g->blockBlow($n, $sev, 'Une flèche des Rôdeurs', true)) {                        $g->hitPlayer($part, 'cut', $sev, 'Une flèche des Rôdeurs');                    }                } else {                    $g->say('Une flèche siffle à tes oreilles.');                }            }            return;        }        if (isset($n->data['wu']) && ($n->data['arm'] === 'bow' || $d > 3.0)) {            unset($n->data['wu'], $n->data['wuT']);       // Rob got away from the blow (or the archer lost his aim)        }        if ($d > 1.8 && !isset($n->data['wu'])) {            $n->state = 'alert';            self::walk($g, $n, $g->x, $g->z, 4.2, $dt);            return;        }        $n->state = 'attack';        if ($n->data['cool'] <= 0 && !isset($n->data['wu'])) {            $n->data['wu'] = $n->data['wuT'] = !empty($n->data['leader']) ? Combat::HUMAN_WINDUP * 0.8 : Combat::HUMAN_WINDUP;            $n->anim = 0;            $g->combat->engage($g, $n);        } elseif ($n->data['cool'] <= 0 && (float) $n->data['wu'] <= 0) {            unset($n->data['wu'], $n->data['wuT']);            $n->data['cool'] = !empty($n->data['leader']) ? 1.2 : 1.5;            $n->anim = 0;            $g->lunged();            if ($g->combat->evades($g, $n, 2.8)) {                $g->say(!empty($n->data['leader']) ? 'Tu esquives le coup de Vask !' : 'Tu esquives le coup du Rôdeur !');                $g->combat->popup('Esquivé !', [140, 220, 255]);            } elseif ($g->rng()->chance(0.7)) {                $part = self::PARTS[$g->rng()->int(0, 5)];                $kind = $g->rng()->chance(0.5) ? 'cut' : 'bruise';                $sev = $g->rng()->range(0.22, !empty($n->data['leader']) ? 0.55 : 0.42);                if (!$g->blockBlow($n, $sev, self::the($n, true))) {                    $g->hitPlayer($part, $kind, $sev, self::the($n, true));                }            } else {                $g->say(self::the($n, true) . ' te manque de peu.');            }        }    }    /** "le rôdeur à l'arc", "Vask le Balafré" (for sentences). */    public static function the(Npc $n, bool $capital = false): string    {        $the = (string) ($n->data['the'] ?? $n->name);        return $capital ? ucfirst($the) : $the;    }    /** Rob hits a human. */    public static function hurt(Game $g, Npc $n, float $damage): void    {        $key = People::keyOf($n);        if ($n->faction !== 'rodeur' && !$n->hostile) {            // striking a friend: the whole group turns against Rob            if ($key !== null) {                People::change($g, $key, -60);            }            $g->flags[$n->faction . '_hostile'] = true;            foreach ($g->npcs as $o) {                if ($o->faction === $n->faction && empty($o->data['companion'])) {                    $c = $o->data['centre'] ?? [$o->x, $o->z];                    People::makeHostile($o, (float) $c[0], (float) $c[1]);                    $o->state = 'alert';                }            }            $g->say(self::the($n, true) . ' hurle : « Traître ! » Tout le monde prend les armes.');        }        if ($n->faction === 'rodeur' && !empty($g->flags['rodeur_truce'])) {            unset($g->flags['rodeur_truce']);            People::change($g, 'vask', -40);            $g->say('Tu as rompu la trêve des Rôdeurs.');        }        if ($key === 'grell' && $n->health - $damage < 45 && $n->state !== 'surrender') {            // the coward drops his club and begs            $n->health -= $damage;            $n->state = 'surrender';            $n->hostile = false;            $g->say('Grell lâche son gourdin et tombe à genoux : « Pitié ! Pitié, je sais des choses ! »');            return;        }        $n->health -= $damage;        if ($n->state === 'patrol' || $n->state === 'idle') {            $n->state = 'alert';        }        if ($n->health <= 0) {            self::kill($g, $n);            return;        }        if ($n->health < 25 && empty($n->data['leader'])) {            $n->state = 'flee';            $g->say(self::the($n, true) . ' s\'enfuit en hurlant.');        }    }    public static function kill(Game $g, Npc $n): void    {        $n->state = 'dead';        $k = array_search($n, $g->npcs, true);        if ($k !== false) {            array_splice($g->npcs, $k, 1);        }        $key = People::keyOf($n);        if ($n->faction === 'rodeur') {            $dead = $g->flags['rodeur_dead'] ?? [];            $dead[$n->id] = true;            $g->flags['rodeur_dead'] = $dead;        }        if ($key !== null) {            $g->flags['dead_' . $key] = true;            People::change($g, $key, -100, false);        }        $g->flags['people_killed'] = (int) ($g->flags['people_killed'] ?? 0) + 1;        if ($n->faction === 'rodeur' || $n->hostile) {            $g->gainXp(Talents::HUMAN_XP * (!empty($n->data['leader']) ? 2 : 1), true);        }        $loot = !empty($n->data['leader'])            ? ['trade_salt' => 2, 'dried_meat' => 1, 'metal_scrap' => 1, 'rope' => 1, 'credit' => 30]            : (($n->data['arm'] ?? '') === 'bow' ? ['arrow' => 4, 'dried_meat' => 1, 'credit' => 6] : ['dried_meat' => 1, 'cloth' => 1, 'credit' => 8]);        if ($key === 'vask' && empty($g->flags['key_taken'])) {            $loot['ignition_key'] = 1;            $g->flags['key_taken'] = 'fight';        }        if ($key === 'orsolo' && empty($g->flags['codes_given'])) {            $loot['launch_codes'] = 1;            $g->flags['codes_given'] = 'fight';        }        if ($key === 'grell') {            $loot += ['gun_parts' => 1, 'energy_cell' => 1];        }        if (($n->data['arm'] ?? '') === 'bow' && $g->rng()->chance(0.5)) {            $loot['bow'] = 1;        }        $g->addThing('bones', $n->x, $n->z, ['label' => 'Dépouille : ' . ($key !== null ? $n->name : lcfirst($n->name)), 'loot' => $loot], $n->yaw);        $g->say(self::the($n, true) . ' tombe et ne se relève pas.');    }    private static function walk(Game $g, Npc $n, float $tx, float $tz, float $speed, float $dt): void    {        if ($g->zone->solidHit($tx, $tz, Solids::PERSON) !== null) {            [$tx, $tz] = Solids::pushOut($g->zone, $tx, $tz, Solids::PERSON);    // a spot inside a hut: its door side        }        $dx = $tx - $n->x;        $dz = $tz - $n->z;        $l = hypot($dx, $dz);        if ($l < 0.05) {            return;        }        self::step($g, $n, $dx / $l, $dz / $l, $speed * $dt);    }    private static function walkAway(Game $g, Npc $n, float $fx, float $fz, float $speed, float $dt): void    {        $dx = $n->x - $fx;        $dz = $n->z - $fz;        $l = max(0.01, hypot($dx, $dz));        self::step($g, $n, $dx / $l, $dz / $l, $speed * $dt);    }    /** One step: the same rules as the animals and Rob (Solids: walls, rocks, trunks, huts), sliding then turning aside. */    private static function step(Game $g, Npc $n, float $dx, float $dz, float $s): void    {        $z = $g->zone;        $h0 = $z->heightAt($n->x, $n->z);        $ok = static fn (float $nx, float $nz): bool => $z->heightAt($nx, $nz) - $h0 <= $s * 1.1 + 0.01;        $side = (float) ($n->data['av'] ?? 0.0);        $turns = $side > 0 ? [0.6, 0.0, 1.3, -0.6, -1.3, 2.2, -2.2] : ($side < 0 ? [-0.6, 0.0, -1.3, 0.6, 1.3, -2.2, 2.2] : [0.0, 0.6, -0.6, 1.3, -1.3, 2.2, -2.2]);        $step = Solids::step($z, $n->x, $n->z, $dx, $dz, $s, Solids::PERSON, $turns, $ok);        if ($side !== 0.0) {            $n->data['av'] = $side > 0 ? max(0.0, $side - 0.03) : min(0.0, $side + 0.03);            if (abs((float) $n->data['av']) < 0.01) {                unset($n->data['av']);            }        }        if ($step === null) {            return;        }        [$nx, $nz, $mx, $mz, $turn] = $step;        if (abs($turn) > 0.05 && !isset($n->data['av'])) {            $n->data['av'] = $turn > 0 ? 0.8 : -0.8;        }        $n->x = $nx;        $n->z = $nz;        $n->y = $z->heightAt($nx, $nz);        $n->yaw = atan2($mx, $mz);    }}