<?phpnamespace App\Revisited\Land;/** * The raft (docs/reborn.md "Wave 2"): built on a shore from logs and rope (a thing `raft` laid on the water), boarded, * steered with a pole (faster, firmer) or by hand, carried by the jungle river's current (south, strongest mid-stream), * worn by rocks and banks, repaired with rope and wood, fished from. The jungle's river cannot be swum (its current), so * the islands and the east bank need it (or Tamu's passage). While Rob is aboard the raft lives in Game::$raft (drawn * from View::$raft); when he steps ashore it is a thing again where he left it. */final class Raft{ public const NEEDS = ['log' => 4, 'rope' => 3]; public const HP = 100.0; /** Deep water where a raft floats (m). */ public const FLOAT = 0.45; /** The strong current of the jungle's river: [vx, vz] m/s at a point (zero elsewhere). */ public static function current(Game $g, float $x, float $z): array { if ($g->zone->id !== 'jungle' || $g->zone->waterDepth($x, $z) < 0.3) { return [0.0, 0.0]; } $n = $g->riverNoise(); $v = max(0.0, min(1.0, $z / 512)); $rx = ZoneFactory::riverX($v, $n) * 512; $half = ZoneFactory::riverHalf($v, $n); $k = max(0.0, 1 - abs($x - $rx) / ($half + 2)); // along the river's line, south $dv = 0.01; $dx = (ZoneFactory::riverX(min(1.0, $v + $dv), $n) - ZoneFactory::riverX($v, $n)) * 512; $dz = $dv * 512; $l = hypot($dx, $dz); $s = 1.4 * (0.35 + 0.65 * $k); return [$dx / $l * $s, $dz / $l * $s]; } /** Is this the river no one swims (Rob may not go deeper into it without a raft)? */ public static function forbidden(Game $g, float $x, float $z): bool { return $g->zone->id === 'jungle' && $g->zone->waterDepth($x, $z) > 1.0; } /** The water near Rob where a raft can be laid, null: none. */ public static function launchSpot(Game $g): ?array { $z = $g->zone; for ($d = 1.5; $d <= 6.0; $d += 1.5) { for ($a = 0; $a < 6.283; $a += 0.5236) { $x = $g->x + sin($g->yaw + $a) * $d; $zz = $g->z + cos($g->yaw + $a) * $d; if ($z->waterDepth($x, $zz) > self::FLOAT + 0.2 && !$z->solidAt($x, $zz)) { return [$x, $zz]; } } } return null; } public static function build(Game $g): string { foreach (self::NEEDS as $id => $n) { if (!$g->inv->has($id, $n)) { return 'Un radeau demande ' . self::NEEDS['log'] . ' rondins et ' . self::NEEDS['rope'] . ' cordes (tu as ' . $g->inv->count('log') . ' rondins, ' . $g->inv->count('rope') . ' cordes). Les rondins se taillent dans les bûches, à la hache.'; } } $spot = self::launchSpot($g); if ($spot === null) { return 'Il faut de l\'eau profonde à deux pas pour mettre un radeau à flot.'; } foreach (self::NEEDS as $id => $n) { $g->inv->remove($id, $n); } $g->passTime(90); $g->addThing('raft', $spot[0], $spot[1], ['label' => 'Radeau', 'hp' => self::HP], $g->yaw); $g->flags['q_raft'] = true; $g->flags['rafts'] = (int) ($g->flags['rafts'] ?? 0) + 1; return 'Une heure et demie de nœuds et d\'échardes : ton radeau flotte. Il tiendra, si tu évites les rochers.'; } public static function board(Game $g, Thing $t): string { $g->raft = ['x' => $t->x, 'z' => $t->z, 'yaw' => $t->yaw, 'hp' => (float) ($t->data['hp'] ?? self::HP), 'vx' => 0.0, 'vz' => 0.0, 'bump' => 0.0]; $g->removeThing($t); $g->x = $t->x; $g->z = $t->z; $g->y = $g->zone->water + 0.25; return $g->inv->has('raft_pole') ? 'Tu montes sur le radeau, ta perche à la main.' : 'Tu montes sur le radeau. Sans perche, il faudra pagayer des mains.'; } /** Steps ashore if land is within reach: the raft stays where it is. */ public static function land(Game $g): string { $r = $g->raft; if ($r === null) { return ''; } $z = $g->zone; $best = null; for ($a = 0; $a < 6.283; $a += 0.3927) { foreach ([1.5, 2.5, 3.5] as $d) { $x = $g->x + sin($g->yaw + $a) * $d; $zz = $g->z + cos($g->yaw + $a) * $d; if ($z->waterDepth($x, $zz) < 0.35 && !$z->solidAt($x, $zz)) { $best = [$x, $zz]; break 2; } } } if ($best === null) { return 'Aucune rive assez proche pour débarquer.'; } $g->raft = null; $g->addThing('raft', $r['x'], $r['z'], ['label' => 'Radeau', 'hp' => $r['hp']], $r['yaw']); $g->x = $best[0]; $g->z = $best[1]; $g->y = $z->heightAt($g->x, $g->z); return 'Tu sautes sur la rive et tires le radeau au sec.'; } public static function canLand(Game $g): bool { $z = $g->zone; for ($a = 0; $a < 6.283; $a += 0.3927) { $x = $g->x + sin($g->yaw + $a) * 3.0; $zz = $g->z + cos($g->yaw + $a) * 3.0; if ($z->waterDepth($x, $zz) < 0.35 && !$z->solidAt($x, $zz)) { return true; } } return false; } /** Moves the raft (and Rob on it) for a frame. */ public static function move(Game $g, float $dt, Intent $in): void { $r = $g->raft; if ($r === null) { return; } $pole = $g->inv->has('raft_pole'); $g->yaw -= $in->turn * ($pole ? 1.3 : 0.8) * $dt; $g->pitch = max(-1.3, min(1.3, $g->pitch + $in->look * 1.6 * $dt)); $push = $in->forward * ($pole ? 2.6 : 1.2); [$cx, $cz] = self::current($g, $r['x'], $r['z']); // the raft's speed eases toward the current plus the push $tx = $cx * ($pole ? 0.75 : 1.0) + sin($g->yaw) * $push; $tz = $cz * ($pole ? 0.75 : 1.0) + cos($g->yaw) * $push; $k = min(1.0, $dt * 1.2); $r['vx'] += ($tx - $r['vx']) * $k; $r['vz'] += ($tz - $r['vz']) * $k; $r['bump'] = max(0.0, $r['bump'] - $dt); $nx = $r['x'] + $r['vx'] * $dt; $nz = $r['z'] + $r['vz'] * $dt; $z = $g->zone; $hit = false; if ($z->waterDepth($nx, $nz) < self::FLOAT || $z->solidAt($nx, $nz)) { $hit = true; } else { foreach ($z->thingsNear($nx, $nz, 1.6) as $t) { if (in_array($t->kind, ['boulder', 'rock', 'basalt', 'mangrove'], true)) { $hit = true; break; } } } if ($hit) { $speed = hypot($r['vx'], $r['vz']); if ($speed > 1.0 && $r['bump'] <= 0) { $r['hp'] -= 6 + 8 * $speed; $r['bump'] = 1.0; $g->say($r['hp'] > 30 ? 'Le radeau cogne et craque.' : 'Le radeau se disloque sous tes pieds !'); } $r['vx'] *= -0.2; $r['vz'] *= -0.2; } else { $r['x'] = $nx; $r['z'] = $nz; } $r['yaw'] = $g->yaw; $g->raft = $r; $g->x = $r['x']; $g->z = $r['z']; $g->y = $z->water + 0.25; $g->moving = abs($in->forward) > 0.05; if ($r['hp'] <= 0) { $g->raft = null; $g->give('log', 1); $g->flags['raft_wrecked'] = (int) ($g->flags['raft_wrecked'] ?? 0) + 1; $g->inv->wet = 1.0; $g->say('Le radeau est en miettes. Tu nages vers la rive en t\'accrochant à un rondin.'); $g->toShore(); } } public static function repair(Game $g): string { if ($g->raft === null) { return ''; } if (!$g->inv->has('rope') || !($g->inv->has('wood') || $g->inv->has('log'))) { return 'Il faut une corde et du bois (bûche ou rondin) pour réparer le radeau.'; } $g->inv->remove('rope'); $g->inv->remove($g->inv->has('wood') ? 'wood' : 'log'); $r = $g->raft; $r['hp'] = min(self::HP, $r['hp'] + 45); $g->raft = $r; $g->passTime(20); return 'Tu resserres les liens et cales une pièce de bois : le radeau tient mieux. (' . (int) $r['hp'] . ' %)'; }}