add('torch'); $inv->add('knife_flint'); self::assertNull($inv->equip('torch')); self::assertNull($inv->equip('knife_flint')); self::assertSame('torch', $inv->handLeft); self::assertSame('knife_flint', $inv->hand); self::assertTrue($inv->holds('torch')); self::assertSame('handLeft', $inv->heldSlot('torch')); } public function testATwoHandedWeaponStowsTheTorchAndGivesItBackWhenPutAway(): void { $inv = new Inventory(); foreach (['torch', 'bow', 'spear'] as $id) { $inv->add($id); } $inv->equip('torch'); $inv->equip('bow'); self::assertSame('bow', $inv->hand); self::assertNull($inv->handLeft, 'both hands on the bow'); self::assertSame('torch', $inv->stowedLeft); $inv->unequip('hand'); self::assertSame('torch', $inv->handLeft, 'the torch comes back to the left hand'); // a one-handed weapon after the bow gives the torch back too $inv->equip('bow'); $inv->equip('spear'); self::assertSame(['spear', 'torch'], [$inv->hand, $inv->handLeft]); // the torch taken while the bow is held: the bow goes on the shoulder $inv->equip('bow'); $inv->equip('torch'); self::assertNull($inv->hand); self::assertSame('torch', $inv->handLeft); self::assertNotNull($inv->equipLeft('bow'), 'a bow never fits one hand'); foreach (['raft_pole', 'crossbow', 'pulse_rifle', 'plasma_gun'] as $id) { self::assertTrue(Inventory::twoHanded($id), $id); } self::assertFalse(Inventory::twoHanded('spear'), 'a spear and a torch: the cave hunter'); } public function testSwappingHandsAndOneTorchCannotBeInBothHands(): void { $inv = new Inventory(); $inv->add('torch'); $inv->add('club'); $inv->equip('torch'); $inv->equip('club'); self::assertNull($inv->swapHands()); self::assertSame(['torch', 'club'], [$inv->hand, $inv->handLeft]); $inv->equipRight('club'); self::assertSame('club', $inv->hand); self::assertNull($inv->handLeft, 'the only club moved hands'); $inv->equipLeft('torch'); $inv->remove('torch'); self::assertNull($inv->handLeft, 'a burnt torch leaves the hand'); self::assertNotNull($inv->equip('bow'), 'no bow in the bag: equip refuses'); } public function testOldSavesWithTheTorchInHandPutItInTheLeftHand(): void { $inv = Inventory::fromArray(['items' => ['torch' => 2, 'knife_flint' => 1], 'hand' => 'torch', 'cond' => []]); self::assertSame('torch', $inv->handLeft); self::assertNull($inv->hand); $inv = Inventory::fromArray(['items' => ['knife_flint' => 1], 'hand' => 'knife_flint', 'cond' => []]); self::assertSame('knife_flint', $inv->hand, 'a weapon stays in the right hand'); // and the new saves keep both hands and the stowed torch $new = new Inventory(); $new->add('torch'); $new->add('crossbow'); $new->equip('torch'); $new->equip('crossbow'); $back = Inventory::fromArray($new->toArray()); self::assertSame(['crossbow', null, 'torch'], [$back->hand, $back->handLeft, $back->stowedLeft]); $g = $this->cave(); $g->give('torch'); $g->useItem('torch'); $g2 = Game::fromArray($g->toArray()); self::assertTrue($g2->torchLit(), 'a game saved with the torch in the left hand keeps it lit'); } public function testTheDamageLandsAtTheStrikeNotAtThePress(): void { $g = $this->cave(); $g->give('club'); $g->useItem('club'); $c = $this->beast($g, 'grazer', 1.6); $c->health = 500; $c->data['pin'] = [$c->x, $c->z]; // keep() holds it there (a grazer would run) $in = new Intent(); $in->attack = true; $g->update(1 / 60, $in); $in->attack = false; self::assertNotNull($g->swing, 'the blow began'); self::assertSame('swing', $g->swing['kind']); $windup = Weapons::windup('club'); $t = 1 / 60; while ($t + 1 / 60 < $windup - 0.02) { $this->keep($g, $c); $g->update(1 / 60, $in); $t += 1 / 60; } self::assertSame(500.0, $c->health, 'still winding up: nothing yet'); for ($i = 0; $i < 4; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertLessThan(500.0, $c->health, 'the strike landed'); self::assertTrue($g->swing['hit'] ?? false); // the recovery: a new press does nothing until the weapon's time is over $in->attack = true; $g->update(1 / 60, $in); $in->attack = false; self::assertGreaterThan(0.0, $g->swingPhase()); for ($i = 0; $i < 90; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertNull($g->swing, 'the blow is over'); self::assertLessThan(Weapons::windup('bow'), Weapons::windup('knife_flint'), 'a knife stabs before a bow is drawn'); } public function testTheBlowAnimationAnticipatesStrikesAndRecovers(): void { $rest = HeldPose::attack('swing', 0.0, 0.3); $wind = HeldPose::attack('swing', 0.3, 0.3); $strike = HeldPose::attack('swing', 0.42, 0.3); $end = HeldPose::attack('swing', 0.999, 0.3); self::assertSame(0.0, $rest[3]); self::assertLessThan(-0.5, $wind[3], 'raised back first'); self::assertGreaterThan(0.5, $strike[5], 'then brought down across the view'); self::assertEqualsWithDelta(0.0, $end[3], 0.01, 'and back at rest'); self::assertGreaterThan(0.4, HeldPose::attack('thrust', 0.35, 0.25)[2], 'a spear thrusts forward'); self::assertGreaterThan(0.5, HeldPose::flash('gun', 0.06, 0.05), 'a gun flashes as it fires'); self::assertSame(0.0, HeldPose::flash('bow', 0.5, 0.4)); self::assertGreaterThan(0.15, abs(HeldPose::brandish(0.5)[0]), 'the torch sweeps across'); } public function testALitTorchKeepsTheCrawlersAtADistance(): void { $g = $this->cave(); $g->give('torch'); $g->useItem('torch'); self::assertTrue($g->torchLit()); $c = $this->beast($g, 'crawler', 8.0); $closest = 99.0; $in = new Intent(); for ($i = 0; $i < 60 * 15; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); $closest = min($closest, Wildlife::dist($g, $c)); } self::assertSame(0, (int) ($c->data['hits'] ?? 0), 'it did not bite'); self::assertGreaterThan(3.0, $closest, 'it kept off the flame'); self::assertSame('keep', $c->state, 'circling and hissing'); // the torch put away: it comes for Rob $g->unequip('handLeft'); self::assertFalse($g->torchLit()); for ($i = 0; $i < 60 * 10 && (int) ($c->data['hits'] ?? 0) === 0; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertGreaterThan(0, (int) ($c->data['hits'] ?? 0), 'no flame: it bites'); } public function testBrandishingTheTorchMakesTheNearCrawlersFlee(): void { $g = $this->cave(); $g->give('torch'); $g->useItem('torch'); $c = $this->beast($g, 'crawler', 5.0); $in = new Intent(); $g->update(1 / 60, $in); $in->left = true; $g->update(1 / 60, $in); $in->left = false; self::assertGreaterThan(0.0, $g->brandishPhase(), 'the sweep is under way'); $before = Wildlife::dist($g, $c); for ($i = 0; $i < 90; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertSame('flee', $c->state); self::assertGreaterThan($before + 3.0, Wildlife::dist($g, $c), 'it ran from the flame'); self::assertContains('brandish', array_column($g->takeEvents(), 0)); } public function testAStarvingOrCorneredCrawlerStillLungesAtATorch(): void { $g = $this->cave(); $g->give('torch'); $g->useItem('torch'); $c = $this->beast($g, 'crawler', 5.0); $c->state = 'keep'; $c->data['starve'] = Wildlife::CRAWLER_STARVE + 1; $in = new Intent(); for ($i = 0; $i < 60 * 8 && (int) ($c->data['hits'] ?? 0) === 0; $i++) { $this->keep($g, $c); $g->update(1 / 60, $in); } self::assertGreaterThan(0, (int) ($c->data['hits'] ?? 0), 'hunger beat the fear'); self::assertLessThan(1.0, (float) ($c->data['starve'] ?? 0), 'one lunge, then the flame holds it off again'); } // ------------------------------------------------------------------------------------------ helpers /** Rob in the first cave, facing a stretch of open floor. */ 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; } /** Only this beast (the spawner adds others). */ private function keep(Game $g, Creature $c): void { $g->creatures = [$c]; if (isset($c->data['pin'])) { [$c->x, $c->z] = $c->data['pin']; } } }