talents->level()); self::assertSame(0, $g->talents->points()); Wildlife::kill($g, $this->spawn($g, 'grazer', 5.0)); self::assertSame(8.0, $g->talents->xp, 'a grazer is little'); Wildlife::kill($g, $this->spawn($g, 'stalker', 5.0)); self::assertSame(48.0, $g->talents->xp, 'a stalker is a lot'); self::assertGreaterThan(Talents::killXp('crawler'), Talents::killXp('glacier_brute')); self::assertGreaterThan(Talents::killXp('grazer'), Talents::killXp('crawler')); self::assertSame(2, $g->talents->level(), '40 XP make level 2'); self::assertSame(1, $g->talents->points()); self::assertStringContainsString('Niveau 2', implode("\n", $g->takeNotices()), 'a toast says it'); self::assertContains('levelup', array_column($g->takeEvents(), 0)); // the curve: each level asks more self::assertSame([40, 113, 208, 320], [Talents::need(1), Talents::need(2), Talents::need(3), Talents::need(4)]); $g->gainXp(Talents::total(5) - $g->talents->xp); self::assertSame(5, $g->talents->level()); self::assertSame(4, $g->talents->points()); // smaller feats: a recipe made the first time, a new region $before = $g->talents->xp; $g->give('stick', 4); $g->give('fiber', 4); $g->give('flint', 2); $g->craft('knife_flint'); self::assertSame($before + Talents::FEAT_XP['recipe'], $g->talents->xp); } public function testTalentsArePointsSpentInThreeBranchesWithRanksAndTheyAreSaved(): void { $t = new Talents(); self::assertStringContainsString('Pas de point', $t->learn('power')); $t->gain(Talents::total(4)); self::assertSame(3, $t->points()); $t->learn('power'); $t->learn('power'); $t->learn('frugal'); self::assertSame(0, $t->points()); self::assertSame(2, $t->rank('power')); self::assertEqualsWithDelta(1.16, $t->damageMul(), 1e-9); foreach (array_keys(Talents::BRANCHES) as $b) { self::assertGreaterThanOrEqual(4, count(Talents::of($b)), "four talents at least in $b"); } $g = Game::newGame(self::SEED); $g->talents = $t; $g2 = Game::fromArray($g->toArray()); self::assertSame($t->xp, $g2->talents->xp); self::assertSame(['power' => 2, 'frugal' => 1], $g2->talents->ranks); // an older save (no talents): level 1, nothing learnt $old = $g->toArray(); unset($old['talents']); $g3 = Game::fromArray($old); self::assertSame(1, $g3->talents->level()); self::assertSame([], $g3->talents->ranks); } public function testTalentsChangeHungerColdWearButcheringAndTheCursor(): void { $g = Game::newGame(self::SEED); $g->talents->gain(Talents::total(12)); foreach (['frugal', 'frugal', 'frugal', 'thick', 'thick', 'butcher', 'butcher', 'butcher', 'timing', 'care', 'care'] as $id) { $g->talents->learn($id); } $e = $g->environment(); self::assertEqualsWithDelta(0.76, $e->hungerMul, 1e-9); self::assertEqualsWithDelta(3.0, $e->coldResist, 1e-9); // hunger: the same day with and without the talent $plain = new Body(); $frugal = new Body(); $env = new Environment(); $plain->tick(600, $env); $env->hungerMul = 0.76; $frugal->tick(600, $env); self::assertGreaterThan($plain->vitals()['hunger'] + 1.5, $frugal->vitals()['hunger']); // the cold felt less: a body in the cold stays warmer $cold = new Environment(); $cold->airTemp = -5.0; $a = new Body(); $a->tick(120, $cold); $cold->coldResist = 4.5; $b = new Body(); $b->tick(120, $cold); self::assertGreaterThan($a->vitals()['temperature'], $b->vitals()['temperature']); // butchering: +60 % with three ranks (3 meat -> 5) $g->give('knife_flint'); $g->useItem('knife_flint'); $carcass = $g->addThing('carcass', $g->x + sin($g->yaw) * 1.0, $g->z + cos($g->yaw) * 1.0, ['species' => 'grazer', 'label' => 'Carcasse', 'meat' => 3, 'hide' => 1]); $g->creatures = []; $g->act('skin'); self::assertSame('bones', $carcass->kind); self::assertSame(5, $g->inv->count('meat_raw')); // the cursor's critical zone is wider; the weapons wear less self::assertGreaterThan(0.16, $g->talents->sweetWidth()); self::assertEqualsWithDelta(0.76, $g->talents->wearMul(), 1e-9); } // ------------------------------------------------------------------------------------------ the combat mode public function testTheCombatModeEngagesWhenABeastComesAndLetsGoWhenItDies(): void { $g = $this->cave(); $c = $this->beast($g, 'crawler', 6.0); self::assertFalse($g->combat->active); $in = new Intent(); for ($i = 0; $i < 60 && !$g->combat->active; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertTrue($g->combat->active, 'the crawler hunts Rob: a fight'); self::assertSame('c' . $c->id, $g->combat->target); $info = $g->combat->info($g); self::assertNotNull($info); self::assertSame(1.0, $info['hp']); // it winds up its bite: the HUD sees the intention coming for ($i = 0; $i < 240 && !isset($c->data['wu']); $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertSame('strike', $g->combat->info($g)['intent'] ?? null, 'a bite being wound up is telegraphed'); // killed: the mode lets go Wildlife::kill($g, $c); $g->creatures = []; for ($i = 0; $i < 120; $i++) { $g->creatures = []; $g->update(1 / 60, $in); } self::assertFalse($g->combat->active, 'nothing left to fight'); self::assertGreaterThan(0.0, $g->talents->xp, 'the kill gave experience'); } public function testThePrecisionCursorGivesACriticalInTheCentreAndAWeakBlowAtTheEdge(): void { self::assertSame('crit', Combat::grade(0.5, 0.16)[0]); self::assertSame(1.5, Combat::grade(0.55, 0.16)[1]); self::assertSame('good', Combat::grade(0.3, 0.16)[0]); self::assertSame('weak', Combat::grade(0.05, 0.16)[0]); self::assertSame(0.7, Combat::grade(0.97, 0.16)[1]); self::assertSame('crit', Combat::grade(0.38, 0.3)[0], 'the timing talent widens the critical zone'); $g = $this->cave(); $g->give('club'); $g->useItem('club'); $c = $this->beast($g, 'crawler', 1.6); $c->data['pin'] = [$c->x, $c->z]; $c->health = 1000; $c->data['hp0'] = 1000; $c->cooldown = 999; // it never bites back here (no counter to mix in) $g->combat->engage($g, $c); $crit = $this->aimedBlow($g, $c, 0.5); self::assertSame('crit', $g->combat->lastGrade); $weak = $this->aimedBlow($g, $c, null); self::assertSame('weak', $g->combat->lastGrade, 'never pressed again: a weak blow at the end of the bar'); self::assertGreaterThan($weak * 1.3, $crit, 'the centre hits much harder than the edge'); $texts = array_column($g->combat->popups, 0); self::assertNotEmpty(array_filter($texts, static fn ($t) => str_starts_with($t, '−')), 'the damage pops up'); } public function testADodgeMakesAWoundUpBiteMiss(): void { $g = $this->cave(); $c = $this->beast($g, 'crawler', 1.2); $c->data['pin'] = [$c->x, $c->z]; $in = new Intent(); for ($i = 0; $i < 120 && !isset($c->data['wu']); $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertTrue(isset($c->data['wu']), 'the bite is wound up'); while ((float) $c->data['wu'] > 0.25) { $this->keep($g, $c); $g->update(1 / 60, $in); } $fatigue = $g->body->vitals()['fatigue']; $x0 = $g->x; $z0 = $g->z; $in->dodge = 0.0; $g->update(1 / 60, $in); $in->dodge = null; for ($i = 0; $i < 40; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertSame(0, (int) ($c->data['hits'] ?? 0), 'the bite missed'); self::assertGreaterThan(2.0, hypot($g->x - $x0, $g->z - $z0), 'Rob stepped back'); self::assertLessThan($fatigue, $g->body->vitals()['fatigue'], 'a dodge costs a little rest'); self::assertContains('dodge', array_column($g->takeEvents(), 0)); self::assertTrue($g->combat->dodgeCool > 0 || !$g->dodge(), 'not twice at once'); } public function testASwingNearCrawlersHeldOffByTheTorchProvokesThemAndTheyStillFearABrandish(): void { $g = $this->cave(); $g->give('torch'); $g->useItem('torch'); $g->give('knife_flint'); $g->useItem('knife_flint'); self::assertTrue($g->torchLit()); $a = $this->beast($g, 'crawler', 7.0); $b = Wildlife::make($g, 'crawler', $g->x + sin($g->yaw) * 10, $g->z + cos($g->yaw) * 10); $b->y = $g->zone->heightAt($b->x, $b->z); $both = [$a, $b]; $in = new Intent(); for ($i = 0; $i < 60 * 6; $i++) { $g->creatures = $both; $g->update(1 / 60, $in); } self::assertSame('keep', $a->state, 'held off by the flame'); self::assertSame(0, (int) ($a->data['hits'] ?? 0)); // Rob faces the nearest and swings his knife within 4 m of it $near = Wildlife::dist($g, $a) < Wildlife::dist($g, $b) ? $a : $b; $g->yaw = atan2($near->x - $g->x, $near->z - $g->z); self::assertLessThan(5.5, Wildlife::dist($g, $near)); $g->x += sin($g->yaw) * max(0.0, Wildlife::dist($g, $near) - 3.5); $g->z += cos($g->yaw) * max(0.0, Wildlife::dist($g, $near) - 3.5); $in->attack = true; $g->update(1 / 60, $in); $in->attack = false; self::assertGreaterThan(0.0, (float) ($near->data['angry'] ?? 0), 'the blade threatens it'); $other = $near === $a ? $b : $a; self::assertLessThan(12.0, hypot($other->x - $near->x, $other->z - $near->z), 'the other one is near it'); self::assertGreaterThan(0.0, (float) ($other->data['angry'] ?? 0), 'it calls the other one'); self::assertTrue($g->combat->active, 'a fight'); $bit = false; for ($i = 0; $i < 60 * 8 && !$bit; $i++) { $g->creatures = $both; $g->update(1 / 60, $in); $bit = (int) ($a->data['hits'] ?? 0) + (int) ($b->data['hits'] ?? 0) > 0 || isset($a->data['wu']) || isset($b->data['wu']); } self::assertTrue($bit, 'they come for real, in spite of the torch'); // a brandish still scares them off $g->brandish(); self::assertGreaterThan(0.0, (float) ($near->data['scared'] ?? 0)); $g->creatures = $both; $g->update(1 / 60, $in); self::assertSame('flee', $near->state); } public function testRunningAwayMakesANightHunterGiveUp(): void { $g = Game::fromArray(['seed' => self::SEED, 'zone' => 'valley', 'v2' => 1]); $g->minutes = 16 * 60; // 23:00 self::assertTrue($g->isNight()); $g->npcs = []; $c = $this->spawn($g, 'stalker', 10.0); $c->state = 'hunt'; $g->creatures = [$c]; $in = new Intent(); $fx = sin($g->yaw); $fz = cos($g->yaw); $engaged = false; for ($i = 0; $i < 60 * 40; $i++) { // Rob runs straight away from it (moved by hand: no tree in the way) $g->x -= $fx * Game::RUN / 60; $g->z -= $fz * Game::RUN / 60; $g->x = max(5.0, min($g->zone->size - 5, $g->x)); $g->z = max(5.0, min($g->zone->size - 5, $g->z)); $g->y = $g->zone->heightAt($g->x, $g->z); $g->running = true; $g->creatures = [$c]; $g->update(1 / 60, $in); $engaged = $engaged || $g->combat->active; if ($g->dead() || !in_array($c, $g->creatures, true)) { break; } } self::assertTrue($engaged, 'the hunter engaged the combat mode'); self::assertNotContains($c->state, ['hunt', 'attack'], 'it stopped chasing'); self::assertGreaterThan(12.0, Wildlife::dist($g, $c), 'left behind'); self::assertFalse($g->combat->active, 'the fight is over'); $log = implode("\n", $g->messages()); self::assertTrue(str_contains($log, 'abandonne la poursuite') || $c->state === 'flee', 'it gave up (or fled hurt)'); } // ------------------------------------------------------------------------------------------ helpers /** One aimed blow: released at $at (0..1 of the bar; null: never pressed again). Returns the damage done. */ private function aimedBlow(Game $g, Creature $c, ?float $at): float { $g->attackCoolReset(); $in = new Intent(); $in->attack = true; $this->keep($g, $c); $g->update(1 / 60, $in); $in->attack = false; self::assertTrue($g->aiming(), 'combat mode: the blow is aimed first'); $hp = $c->health; while ($g->aiming() && ($at === null || $g->aimPos() < $at - 0.01)) { $this->keep($g, $c); $g->update(1 / 60, $in); } if ($at !== null) { $in->attack = true; $this->keep($g, $c); $g->update(1 / 60, $in); $in->attack = false; } for ($i = 0; $i < 90 && $g->swing !== null; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } $g->combat->engage($g, $c); return $hp - $c->health; } private function spawn(Game $g, string $species, float $dist): Creature { $c = Wildlife::make($g, $species, $g->x + sin($g->yaw) * $dist, $g->z + cos($g->yaw) * $dist); $c->y = $g->zone->heightAt($c->x, $c->z); $g->creatures[] = $c; return $c; } /** Rob in the first cave, facing a stretch of open floor (as RevisitedHandsTest). */ private function cave(): Game { $g = Game::fromArray(['seed' => self::SEED, 'zone' => 'caves1', 'v2' => 1]); $e = $g->zone->exits[0]; $best = null; for ($r = 4.0; $r < 60 && $best === null; $r += 3.0) { for ($k = 0; $k < 24 && $best === null; $k++) { $x = $e['x'] + cos($k * 0.26) * $r; $z = $e['z'] + sin($k * 0.26) * $r; for ($a = 0; $a < 16; $a++) { $yaw = $a * M_PI / 8; $ok = true; for ($d = -3.0; $d <= 12.0; $d += 0.5) { foreach ([-1.5, 0.0, 1.5] as $side) { $px = $x + sin($yaw) * $d + cos($yaw) * $side; $pz = $z + cos($yaw) * $d - sin($yaw) * $side; if ($g->zone->solidAt($px, $pz) || $g->zone->solidHit($px, $pz, 0.4)) { $ok = false; break 2; } } } if ($ok) { $best = [$x, $z, $yaw]; break; } } } } self::assertNotNull($best, 'open floor in the cave'); [$g->x, $g->z, $g->yaw] = $best; $g->y = $g->zone->heightAt($g->x, $g->z); $g->creatures = []; $g->npcs = []; return $g; } private function beast(Game $g, string $species, float $dist): Creature { $c = Wildlife::make($g, $species, $g->x + sin($g->yaw) * $dist, $g->z + cos($g->yaw) * $dist); $c->y = $g->zone->heightAt($c->x, $c->z); $g->creatures = [$c]; return $c; } private function keep(Game $g, Creature $c): void { $g->creatures = [$c]; if (isset($c->data['pin'])) { [$c->x, $c->z] = $c->data['pin']; } } }