<?phpnamespace Tests;use App\Revisited\Sim\Body;use App\Revisited\Sim\Crafting;use App\Revisited\Sim\Environment;use App\Revisited\Sim\Inventory;use App\Revisited\Sim\Items;use PHPUnit\Framework\TestCase;final class RevisitedSimTest extends TestCase{ private function run_(Body $b, float $minutes, ?Environment $env = null, float $step = 30): void { $env ??= new Environment(); for ($t = 0.0; $t < $minutes && !$b->dead(); $t += $step) { $b->tick($step, $env); } } /** Keeps a body fed and watered while time passes (to isolate other causes). */ private function maintain(Body $b, float $minutes, ?Environment $env = null): void { for ($t = 0.0; $t < $minutes && !$b->dead(); $t += 120) { $this->run_($b, 120, $env); if ($b->vitals()['thirst'] < 60) { $b->drink(true); } if ($b->vitals()['hunger'] < 50) { $b->consume('ration'); } if ($b->vitals()['fatigue'] < 40) { $sleep = new Environment(); $sleep->sleeping = true; $sleep->airTemp = $env?->airTemp ?? 20.0; $this->run_($b, 240, $sleep); } } } public function testDehydrationKillsInAboutTwoDays(): void { $b = new Body(); $env = new Environment(); $env->effort = 0.3; $this->run_($b, 6 * 1440, $env); self::assertTrue($b->dead()); self::assertSame('dehydration', $b->causeId()); self::assertStringContainsString('soif', (string) $b->causeOfDeath()); } public function testACutBleedsUntilBandaged(): void { $b = new Body(); $b->wound('larm', 'cut', 0.6); $before = $b->vitals()['blood']; $this->run_($b, 120); $bleeding = $b->vitals()['blood']; self::assertLessThan($before - 0.3, $bleeding); self::assertSame('bleeding', $b->conditions()[0]['id']); $msg = $b->treat('bandage', 'larm'); self::assertTrue($b->lastActionOk, $msg); $this->run_($b, 120); self::assertGreaterThanOrEqual($bleeding, $b->vitals()['blood']); self::assertSame(0.0, $b->vitals()['bleeding']); } public function testAnUntreatedBigCutLeftAloneKills(): void { $b = new Body(); $b->wound('torso', 'cut', 1.0); $this->maintain($b, 600); self::assertLessThan(3.4, $b->vitals()['blood']); self::assertContains('shock', array_column($b->conditions(), 'id')); $this->maintain($b, 4 * 1440); self::assertTrue($b->dead()); self::assertContains($b->causeId(), ['blood', 'infection']); } public function testADirtyWoundGetsInfectedAndCausesFever(): void { $b = new Body(); $b->wound('rleg', 'bite', 0.5); $this->maintain($b, 1440); $ids = array_column($b->conditions(), 'id'); self::assertContains('infection', $ids); self::assertContains('fever', $ids); self::assertGreaterThan(37.5, $b->vitals()['temperature']); // a poultice cleans it $b->treat('poultice', 'rleg'); self::assertTrue($b->lastActionOk); $this->maintain($b, 1440); self::assertNotContains('infection', array_column($b->conditions(), 'id')); } public function testSepsisKillsWhoNeverTreatsAnInfectedBite(): void { $b = new Body(); $b->wound('rleg', 'bite', 0.7); $b->treat('bandage', 'rleg'); $this->maintain($b, 8 * 1440); self::assertTrue($b->dead()); self::assertSame('infection', $b->causeId()); } public function testPoisonIsCuredByTheAntidote(): void { $sick = new Body(); $sick->consume('berries_red'); self::assertContains('poison', array_column($sick->conditions(), 'id')); $cured = Body::fromArray($sick->toArray()); $msg = $cured->consume('antidote'); self::assertTrue($cured->lastActionOk, $msg); self::assertNotContains('poison', array_column($cured->conditions(), 'id')); $this->run_($sick, 300); $this->run_($cured, 300); self::assertLessThan($cured->vitals()['health'] - 10, $sick->vitals()['health']); } public function testAColdNightWithoutFireCausesHypothermia(): void { $cold = new Environment(); $cold->airTemp = 3.0; $b = new Body(); $this->run_($b, 600, $cold); self::assertLessThan(35.0, $b->vitals()['temperature']); self::assertContains('hypothermia', array_column($b->conditions(), 'id')); $warm = new Environment(); $warm->airTemp = 3.0; $warm->nearFire = true; $warm->clothing = Items::get('coat_fur')['warmth']; $w = new Body(); $this->run_($w, 600, $warm); self::assertGreaterThan(36.5, $w->vitals()['temperature']); self::assertNotContains('hypothermia', array_column($w->conditions(), 'id')); } public function testHeatAndThirstGiveAHeatstroke(): void { $env = new Environment(); $env->airTemp = 46.0; $env->effort = 1.0; $b = new Body(); $this->run_($b, 900, $env); self::assertGreaterThan(39.5, $b->vitals()['temperature']); } public function testAFractureSlowsTheWalkUntilSplinted(): void { $b = new Body(); $normal = $b->speedFactor(); $b->wound('lleg', 'fracture', 0.7); $broken = $b->speedFactor(); self::assertLessThan(0.5, $broken); self::assertLessThan($normal, $broken); $msg = $b->treat('splint', 'lleg'); self::assertTrue($b->lastActionOk, $msg); self::assertGreaterThan($broken + 0.2, $b->speedFactor()); } public function testDirtyWaterGivesDysenteryAndBoiledWaterIsSafe(): void { $clean = new Body(); for ($i = 0; $i < 10; $i++) { $clean->tick(300, new Environment()); $clean->drink(true); } self::assertNotContains('dysentery', array_column($clean->conditions(), 'id')); $d = new Body(); $d->tick(600, new Environment()); for ($i = 0; $i < 12 && !in_array('dysentery', array_column($d->conditions(), 'id'), true); $i++) { $d->tick(300, new Environment()); $d->drink(false); } self::assertContains('dysentery', array_column($d->conditions(), 'id')); } public function testSleepingInAShelterByTheFireRestsFaster(): void { $open = new Environment(); $open->sleeping = true; $cosy = new Environment(); $cosy->sleeping = true; $cosy->sheltered = true; $cosy->nearFire = true; $a = new Body(); $b = new Body(); $tire = new Environment(); $tire->effort = 1.0; $this->run_($a, 600, $tire); $this->run_($b, 600, $tire); $this->run_($a, 240, $open); $this->run_($b, 240, $cosy); self::assertGreaterThan($a->vitals()['fatigue'], $b->vitals()['fatigue']); self::assertGreaterThan(60, $b->vitals()['fatigue']); } public function testASpearNeedsTheKnife(): void { $inv = new Inventory(); $inv->add('stick', 2); $inv->add('flint', 1); $why = Crafting::craft('spear', $inv, false); self::assertNotNull($why); self::assertStringContainsString('couteau', $why); self::assertSame(0, $inv->count('spear')); $inv->add('knife_flint'); self::assertNull(Crafting::craft('spear', $inv, false)); self::assertSame(1, $inv->count('spear')); self::assertSame(0, $inv->count('stick')); self::assertTrue($inv->has('knife_flint')); } public function testCookingNeedsAFire(): void { $inv = new Inventory(); $inv->add('meat_raw'); self::assertNotNull(Crafting::craft('meat_cooked', $inv, false)); self::assertSame(1, $inv->count('meat_raw')); self::assertNull(Crafting::craft('meat_cooked', $inv, true)); self::assertSame(1, $inv->count('meat_cooked')); $ids = array_column(Crafting::available($inv, true), 'id'); self::assertNotContains('meat_cooked', $ids); } public function testBoilingDirtyWaterNeedsThePotAndAFire(): void { $inv = new Inventory(); $inv->add('gourd_dirty'); self::assertNotNull(Crafting::craft('water_boiled', $inv, true)); $inv->add('pot'); self::assertNotNull(Crafting::craft('water_boiled', $inv, false)); self::assertNull(Crafting::craft('water_boiled', $inv, true)); self::assertSame(1, $inv->count('gourd_boiled'), 'boiled water is its own state'); self::assertStringContainsString('bouillie', Items::name('gourd_boiled')); } public function testEveryGarmentHasItsSlotAndEveryNewRecipeIsMadeOfKnownItems(): void { foreach (Items::all() as $id) { $d = Items::get($id); if ($d['kind'] === 'clothing') { self::assertContains(Inventory::slotOf($id), Inventory::CLOTH_SLOTS, $id); self::assertNotEmpty(Inventory::coverOf($id), $id); } } foreach (['cap_hide', 'hat_fur', 'tunic_fiber', 'trousers_hide', 'leggings_fur', 'gloves_hide', 'mittens_fur', 'water_filter', 'worms_cooked', 'water_boiled_stones'] as $rid) { $r = Crafting::recipe($rid); self::assertNotNull($r, $rid); foreach ($r['needs'] + $r['gives'] as $id => $n) { self::assertTrue(Items::exists($id), "$rid: $id"); } } // a hat warms the head only; the suit warms torso, arms and legs $inv = new Inventory(); $inv->add('hat_fur'); $inv->equip('hat_fur'); self::assertGreaterThan(0.0, $inv->regionWarmth('head')); self::assertSame(0.0, $inv->regionWarmth('torso')); $inv->dressForTheCrash(); self::assertGreaterThan(0.0, $inv->regionWarmth('legs')); self::assertSame(0.0, $inv->regionWarmth('hands')); } public function testWaterFromAWaterfallIsSaferThanTheLakeAndRawWormsHurtTheMorale(): void { $sick = 0; for ($i = 0; $i < 60; $i++) { $b = Body::fromArray(['thirst' => 40.0, 'seed' => 1000 + $i * 7919]); $b->drink(true, 0.04); $sick += $b->conditions() !== [] ? 1 : 0; } $lake = 0; for ($i = 0; $i < 60; $i++) { $b = Body::fromArray(['thirst' => 40.0, 'seed' => 1000 + $i * 7919]); $b->drink(false); $lake += array_filter($b->conditions(), static fn ($c) => $c['id'] === 'dysentery') !== [] ? 1 : 0; } self::assertLessThan(10, $sick); self::assertGreaterThan(20, $lake); $b = Body::fromArray(['hunger' => 50.0]); $m = $b->vitals()['morale']; $b->consume('earthworm'); self::assertLessThan($m, $b->vitals()['morale']); self::assertGreaterThan(50.0, $b->vitals()['hunger']); } public function testTheInventoryRefusesWhatIsTooHeavy(): void { $inv = new Inventory(); $fitted = $inv->add('stone', 100); self::assertLessThan(100, $fitted); self::assertGreaterThan(0, $fitted); self::assertLessThanOrEqual($inv->capacity(), $inv->weight()); self::assertSame(0, $inv->add('wood', 5)); self::assertFalse($inv->remove('stone', 999)); } public function testUsingItemsHealsAndSpendsThem(): void { $inv = new Inventory(); $inv->add('bandage', 2); $inv->add('gourd_clean'); $b = new Body(); $b->wound('rarm', 'cut', 0.5); $inv->use($b, 'bandage', 'rarm'); self::assertSame(1, $inv->count('bandage')); $b->tick(60, new Environment()); $b->drink(true); $inv->use($b, 'bandage', 'rarm'); // nothing left to bandage: not spent self::assertSame(1, $inv->count('bandage')); $b->tick(1000, new Environment()); $inv->use($b, 'gourd_clean'); self::assertSame(1, $inv->count('gourd_empty')); } public function testCataloguesAreConsistent(): void { self::assertGreaterThanOrEqual(50, count(Items::all())); foreach (Items::all() as $id) { $d = Items::get($id); foreach (['id', 'name', 'kind', 'weight', 'stack', 'desc'] as $k) { self::assertArrayHasKey($k, $d, "$id lacks $k"); } } self::assertGreaterThanOrEqual(30, count(Crafting::recipes())); foreach (Crafting::recipes() as $r) { foreach ([...array_keys($r['needs']), ...array_keys($r['gives'])] as $id) { self::assertTrue(Items::exists($id), $r['id'] . ' uses unknown ' . $id); } if ($r['tool'] !== null) { self::assertTrue(Items::exists($r['tool'])); } self::assertGreaterThan(0, $r['minutes']); } } public function testSaveAndLoadRoundTrip(): void { $b = new Body(); $b->wound('lleg', 'fracture', 0.6); $b->wound('head', 'cut', 0.5); $b->consume('berries_red'); $this->run_($b, 400); $b2 = Body::fromArray(json_decode(json_encode($b->toArray()), true)); self::assertEquals($b->vitals(), $b2->vitals()); self::assertEquals($b->injuries(), $b2->injuries()); self::assertEquals($b->conditions(), $b2->conditions()); $this->run_($b, 300); $this->run_($b2, 300); self::assertEqualsWithDelta($b->vitals()['health'], $b2->vitals()['health'], 1e-6); $inv = new Inventory(); $inv->add('knife_flint'); $inv->add('stick', 3); $inv->add('coat_fur'); $inv->equip('coat_fur'); $inv->equip('knife_flint'); $i2 = Inventory::fromArray(json_decode(json_encode($inv->toArray()), true)); self::assertSame($inv->all(), $i2->all()); self::assertSame('coat_fur', $i2->worn); self::assertSame('knife_flint', $i2->hand); self::assertSame(19.0, $i2->warmth()); }}