<?phpnamespace Tests;use App\Revisited\Land\Creature;use App\Revisited\Land\Game;use App\Revisited\Land\Intent;use App\Revisited\Land\Npc;use App\Revisited\Land\Wildlife;use App\Revisited\Render\HeldPose;use App\Revisited\Render\Models;use App\Revisited\Sim\Crafting;use App\Revisited\Sim\Inventory;use App\Revisited\Sim\Items;use App\Revisited\Sim\Weapons;use App\Revisited\Ui\Icons;use PHPUnit\Framework\TestCase;/** The shield on the left arm: equip rules, the guard, blows blocked or parried, its wear, its crafting. */final class RevisitedShieldTest extends TestCase{ private const SEED = 4242; public function testTwoShieldsAreCraftedFromHideSticksRopeAndFromScrapWireHide(): void { $recipes = []; foreach (Crafting::recipes() as $r) { $recipes[$r['id']] = $r; } self::assertSame(['hide' => 2, 'stick' => 3, 'rope' => 1], $recipes['shield_hide']['needs']); self::assertSame(['metal_scrap' => 4, 'wire' => 2, 'hide' => 1], $recipes['shield_scrap']['needs']); self::assertSame('Bouclier en peau', Items::name('shield_hide')); self::assertSame('Bouclier de ferraille', Items::name('shield_scrap')); self::assertTrue(Inventory::holdable('shield_hide')); self::assertGreaterThan(Weapons::shield('shield_hide')['block'], Weapons::shield('shield_scrap')['block'], 'the scrap one stops more'); self::assertNull(Weapons::shield('club')); $g = Game::newGame(self::SEED); foreach (['hide' => 2, 'stick' => 3, 'rope' => 1, 'knife_flint' => 1] as $id => $n) { $g->give($id, $n); } $g->craft('shield_hide'); self::assertTrue($g->inv->has('shield_hide')); foreach (['shield_hide', 'shield_scrap'] as $id) { self::assertTrue(Icons::covers($id), "$id has its own icon"); self::assertStringStartsWith('shield', Models::itemKind($id)); self::assertGreaterThan(100, Models::held($id)[0]->count(), "$id has a first-person model"); } } public function testTheShieldGoesOnTheLeftArmAndTwoHandedItemsOrATorchPutItAway(): void { $inv = new Inventory(); foreach (['shield_scrap', 'club', 'bow', 'torch'] as $id) { $inv->add($id); } self::assertNull($inv->equip('shield_scrap')); self::assertNull($inv->equip('club')); self::assertSame(['club', 'shield_scrap'], [$inv->hand, $inv->handLeft], 'club right, shield left'); self::assertSame('shield_scrap', $inv->shield()); self::assertNull($inv->equipRight('shield_scrap'), 'asked for the right hand, it still goes on the left arm'); self::assertSame(['club', 'shield_scrap'], [$inv->hand, $inv->handLeft]); self::assertNotNull($inv->swapHands(), 'a shield is not swapped to the right hand'); // a two-handed bow puts the shield on the back, and gives it back when put away $inv->equip('bow'); self::assertSame(['bow', null, 'shield_scrap'], [$inv->hand, $inv->handLeft, $inv->stowedLeft]); $inv->unequip('hand'); self::assertSame('shield_scrap', $inv->handLeft); // the torch or the shield: never both $inv->equip('torch'); self::assertSame('torch', $inv->handLeft); self::assertNull($inv->shield()); $inv->equip('shield_scrap'); $inv->equipRight('torch'); self::assertSame('torch', $inv->hand); self::assertNull($inv->handLeft, 'a torch taken in the right hand puts the shield away'); $inv->equip('shield_scrap'); self::assertNull($inv->hand, 'and a shield taken puts the torch away'); self::assertSame('shield_scrap', $inv->handLeft); } public function testHoldingTheLeftHandRaisesTheShieldSlowsTheWalkAndATorchNeverGuards(): void { $g = $this->armed('shield_hide'); $in = new Intent(); $in->left = true; $g->update(0.05, $in); self::assertNotNull($g->guardAt, 'held: the shield goes up'); $g->update(0.1, $in); $g->update(0.1, $in); self::assertEqualsWithDelta(1.0, $g->guardPhase(), 1e-9, 'fully up after ' . Game::GUARD_UP . ' s'); self::assertNull($g->swing, 'holding a shield does not swing it'); // guarded steps are short $x0 = $g->x; $z0 = $g->z; $in->forward = 1.0; $g->update(0.1, $in); $guarded = hypot($g->x - $x0, $g->z - $z0); $in->left = false; $g->update(0.01, $in); self::assertNull($g->guardAt, 'let go: lowered'); $x0 = $g->x; $z0 = $g->z; $g->update(0.1, $in); self::assertLessThan(0.75 * hypot($g->x - $x0, $g->z - $z0), $guarded); // a torch on the left arm is brandished, never held up as a guard $t = $this->armed('torch'); $in = new Intent(); $in->left = true; $t->update(0.05, $in); self::assertNull($t->guardAt); self::assertGreaterThan(0.0, $t->brandishPhase(), 'the torch is swept instead'); // too tired: the arm cannot hold it $w = $this->armed('shield_hide'); $w->body->exert(500); $in = new Intent(); $in->left = true; $w->update(0.05, $in); self::assertNull($w->guardAt); self::assertStringContainsString('épuisé', implode("\n", $w->messages())); } public function testABlockedFrontalBlowLosesSixtyOrEightyPercentButNotOneFromBehind(): void { foreach (['shield_hide' => 0.6, 'shield_scrap' => 0.8] as $id => $cut) { $g = $this->armed($id); $front = $this->beast($g, 'stalker', 1.5, 0.0); $back = $this->beast($g, 'stalker', 1.5, M_PI); self::assertSame(['none', 0.5], $g->shieldCheck($front, 0.5), 'lowered: nothing stopped'); $this->raise($g, 1.0); [$res, $left] = $g->shieldCheck($front, 0.5); self::assertSame('block', $res); self::assertEqualsWithDelta(0.5 * (1 - $cut), $left, 1e-9, "$id stops " . ($cut * 100) . ' %'); self::assertSame(['none', 0.5], $g->shieldCheck($back, 0.5), 'a blow from behind goes round it'); self::assertSame('block', $g->shieldCheck($this->beast($g, 'stalker', 2.0, 1.0), 0.5)[0], 'a little to the side still meets it'); // an arrow from the front is stopped too self::assertSame('block', $g->shieldCheck($front, 0.3, true)[0]); } } public function testABlockedBlowBruisesTheShieldArmTiresAndWearsTheShieldUntilItBreaks(): void { $g = $this->armed('shield_hide'); $foe = $this->beast($g, 'glacier_brute', 1.8, 0.0); $this->raise($g, 1.0); $rest = $g->body->vitals()['fatigue']; self::assertTrue($g->blockBlow($foe, 0.6, 'La brute')); $inj = $g->body->injuries(); self::assertCount(1, $inj); self::assertSame(['larm', 'bruise'], [$inj[0]['part'], $inj[0]['kind']], 'what gets through bruises the shield arm'); self::assertLessThanOrEqual(0.24 + 1e-9, $inj[0]['severity'], '0.6 less the hide shield\'s 60 % (less the sleeve)'); self::assertGreaterThan(0.15, $inj[0]['severity']); self::assertLessThan($rest, $g->body->vitals()['fatigue'], 'holding a blow tires the arm'); self::assertLessThan(100.0, $g->inv->condition('shield_hide')); // a light blow is wholly absorbed $g2 = $this->armed('shield_scrap'); $this->raise($g2, 1.0); self::assertTrue($g2->blockBlow($this->beast($g2, 'crawler', 1.2, 0.0), 0.2, 'Le rampant')); self::assertSame([], $g2->body->injuries()); self::assertStringContainsString('bouclier encaisse', implode("\n", $g2->messages())); // blow after blow the hide gives way $n = 0; while ($g->inv->has('shield_hide') && $n < 50) { $g->body->exert(-100); $g->blockBlow($foe, 0.5, 'La brute'); $n++; } self::assertFalse($g->inv->has('shield_hide'), 'the hide shield broke'); self::assertGreaterThan(5, $n, 'after a fair number of blows'); self::assertNull($g->guardAt, 'and the guard dropped with it'); self::assertStringContainsString('vole en éclats', implode("\n", $g->messages())); // the scrap one lasts much longer $g3 = $this->armed('shield_scrap'); $this->raise($g3, 1.0); $m = 0; while ($g3->inv->has('shield_scrap') && $m < 200) { $g3->body->exert(-100); $g3->blockBlow($this->beast($g3, 'stalker', 1.5, 0.0), 0.5, 'Le rôdeur'); $m++; } self::assertGreaterThan($n * 1.8, $m); // repaired with its materials $g4 = $this->armed('shield_hide'); $g4->inv->cond['shield_hide'] = 30.0; $g4->give('hide'); $g4->give('rope'); self::assertStringContainsString('répares', $g4->repairWeapon('shield_hide')); self::assertGreaterThan(80.0, $g4->inv->condition('shield_hide')); } public function testRaisingTheShieldRightAsTheBlowLandsParriesItAndTheFoeReels(): void { $g = $this->armed('shield_hide'); $foe = $this->beast($g, 'stalker', 1.6, 0.0); $foe->data['wu'] = 0.0; $this->raise($g, 0.1); // up a tenth of a second before the bite self::assertSame('parry', $g->shieldCheck($foe, 0.5)[0]); $xp = $g->talents->xp; self::assertTrue($g->blockBlow($foe, 0.5, 'Le rôdeur de nuit')); self::assertSame([], $g->body->injuries(), 'a parry: no harm'); self::assertEqualsWithDelta(Game::STAGGER_PARRY, (float) $foe->data['stagger'], 1e-9, 'the foe reels'); self::assertArrayNotHasKey('wu', $foe->data, 'its blow is broken off'); self::assertGreaterThan(0.0, $foe->cooldown); self::assertGreaterThan($xp, $g->talents->xp); self::assertSame('c' . $foe->id, $g->combat->target, 'the combat mode is on it'); self::assertContains('parry', array_column($g->takeEvents(), 0)); self::assertTrue($g->counterNow(), 'a blow right after the parry is a counter'); // held up long before: a plain block $late = $this->armed('shield_hide'); $foe2 = $this->beast($late, 'stalker', 1.6, 0.0); $this->raise($late, Game::PARRY + 0.4); self::assertSame('block', $late->shieldCheck($foe2, 0.5)[0]); // an arrow is never "parried", only blocked $g5 = $this->armed('shield_scrap'); $archer = new Npc(9001, 'R', 'rodeur', $g5->x + sin($g5->yaw) * 10, $g5->y, $g5->z + cos($g5->yaw) * 10, 0.0, 'attack', 0.0, true); $this->raise($g5, 0.05); self::assertSame('block', $g5->shieldCheck($archer, 0.3, true)[0]); // a person's blow is parried the same way $man = new Npc(9002, 'R', 'rodeur', $g5->x + sin($g5->yaw) * 1.5, $g5->y, $g5->z + cos($g5->yaw) * 1.5, 0.0, 'attack', 0.0, true); self::assertTrue($g5->blockBlow($man, 0.4, 'Le Rôdeur')); self::assertGreaterThan(1.0, (float) $man->data['cool']); } public function testTheFirstPersonShieldIsLowAtTheSideAndRaisedAcrossTheView(): void { $low = HeldPose::shield(0.0); $high = HeldPose::shield(1.0); self::assertLessThan($high[1], $low[1], 'raised: higher'); self::assertGreaterThan($low[0], $high[0], 'raised: toward the middle'); self::assertGreaterThan(abs($high[4]), abs($low[4]), 'lowered: turned edge-on'); self::assertLessThan(0.0, HeldPose::LEFT[0] + $high[0], 'still on the left side'); } // ------------------------------------------------------------------------------------------ helpers private function armed(string $left): Game { $g = Game::newGame(self::SEED); $g->creatures = []; $g->npcs = []; $g->give($left); $g->give('club'); $g->inv->equip($left); $g->inv->equip('club'); return $g; } /** The guard raised $ago seconds ago. */ private function raise(Game $g, float $ago): void { $g->clock += 5.0; $g->guardAt = $g->clock - $ago; } private function beast(Game $g, string $species, float $dist, float $angle): Creature { $a = $g->yaw + $angle; $c = Wildlife::make($g, $species, $g->x + sin($a) * $dist, $g->z + cos($a) * $dist); $g->creatures[] = $c; return $c; }}