1.0, 'jungle' => 0.55, 'jungle_dense' => 0.45, 'sand' => 0.08, 'redrock' => 0.1, 'rock' => 0.05, 'basalt' => 0.05, 'mud' => 0.0, 'marsh' => 0.0, 'snow' => 0.0, 'ice' => 0.0, 'beach' => 0.0, 'cave' => 0.0, 'burnt' => 0.0, ]; private const TREES = ['tree_broad', 'tree_palm', 'tree_conifer', 'tree_dead', 'giant_tree', 'mangrove', 'palm_coast', 'bush_berry', 'bush_red', 'vine', 'reeds_tall']; /** 0 soaked .. 1 tinder: the zone's climate, the hours since the last rain, the rain falling now. */ public static function dryness(Game $g): float { if ($g->zone->cave) { return 0.0; } $base = ['canyon' => 0.75, 'valley' => 0.45, 'jungle' => 0.3, 'marsh' => 0.05, 'plateau' => 0.15, 'coast' => 0.55][$g->zone->id] ?? 0.4; $since = isset($g->flags['rain_at']) ? ($g->minutes - (float) $g->flags['rain_at']) / 60 : 48.0; $d = $base + min(0.3, max(0.0, $since) * 0.012) - (isset($g->flags['rain_at']) && $since < 6 ? 0.35 : 0.0); if ($g->weather !== null && in_array($g->weather['kind'], ['rain', 'storm', 'snow'], true)) { $d -= 0.7 * (float) $g->weather['amount']; } return max(0.0, min(1.0, $d)); } public static function burnable(Game $g, float $x, float $z): float { if (self::burntAt($g, $x, $z) || $g->zone->waterDepth($x, $z) > -0.05) { return 0.0; } return self::BURN[$g->zone->biomeAt($x, $z)] ?? 0.3; } public static function burntAt(Game $g, float $x, float $z): bool { foreach ($g->burnt[$g->zone->id] ?? [] as [$bx, $bz, $br]) { if (abs($x - $bx) < $br && hypot($x - $bx, $z - $bz) < $br) { return true; } } return false; } /** Sets fire to a point (a spark, a flare, a lightning bolt, a torch). @return bool it caught */ public static function ignite(Game $g, float $x, float $z, float $strength = 1.0): bool { if ($g->zone->cave || count($g->blazes) >= self::MAX_BLAZES) { return false; } $b = self::burnable($g, $x, $z); $near = self::treeNear($g, $x, $z, 3.0); $fuel = max($b, $near !== null ? 0.7 : 0.0); if ($fuel * (0.3 + self::dryness($g)) * $strength < 0.25) { return false; } foreach ($g->blazes as $f) { if (hypot($f['x'] - $x, $f['z'] - $z) < $f['r'] + 1.5) { return true; } } $g->blazes[] = ['x' => $x, 'z' => $z, 'r' => 1.2, 'fuel' => 6.0 + 10 * $fuel, 'age' => 0.0]; $g->flags['wildfires'] = (int) ($g->flags['wildfires'] ?? 0) + 1; return true; } /** One step of the fires and the storm (seconds of play; time runs at Game::TIME_RATE minutes a second). */ public static function update(Game $g, float $dt): void { self::wind($g, $dt); self::storm($g, $dt); if ($g->blazes === []) { return; } $dry = self::dryness($g); $rain = $g->weather !== null && in_array($g->weather['kind'], ['rain', 'storm'], true) ? (float) $g->weather['amount'] : 0.0; [$wx, $wz, $ws] = $g->wind; $out = []; foreach ($g->blazes as $f) { $f['age'] += $dt; $b = max(self::burnable($g, $f['x'], $f['z']), 0.4); $f['r'] = min(self::MAX_R, $f['r'] + $dt * 0.22 * $b * (0.3 + $dry) * (1 + $ws)); $f['fuel'] -= $dt * (0.25 + 2.5 * $rain); // sparks downwind if ($f['r'] > 4 && count($g->blazes) + count($out) < self::MAX_BLAZES && $g->rng()->chance($dt * 0.12 * $dry * (0.5 + $ws))) { $d = $f['r'] + $g->rng()->range(2.0, 6.0); $a = atan2($wx, $wz) + $g->rng()->range(-0.6, 0.6); $nx = $f['x'] + sin($a) * $d; $nz = $f['z'] + cos($a) * $d; $taken = false; foreach ([...$g->blazes, ...$out] as $o) { if (hypot($o['x'] - $nx, $o['z'] - $nz) < $o['r'] + 2) { $taken = true; break; } } if (!$taken && self::burnable($g, $nx, $nz) > 0.2) { $out[] = ['x' => $nx, 'z' => $nz, 'r' => 1.0, 'fuel' => 6.0 + 8 * self::burnable($g, $nx, $nz), 'age' => 0.0]; } } self::scorch($g, $f, $dt); if ($f['fuel'] <= 0) { self::addBurnt($g, $f['x'], $f['z'], $f['r'] + 1.0); continue; } $out[] = $f; } $g->blazes = $out; } /** The blaze burns what it covers: trees, animals flee, Rob burns. */ private static function scorch(Game $g, array $f, float $dt): void { foreach ($g->zone->thingsNear($f['x'], $f['z'], $f['r']) as $t) { if (in_array($t->kind, self::TREES, true)) { $wasTree = str_contains($t->kind, 'tree') || $t->kind === 'mangrove' || $t->kind === 'palm_coast'; $t->kind = $wasTree ? 'burnt_tree' : 'ash'; $t->data = ['label' => $wasTree ? 'Arbre calciné' : 'Cendres', 'sticks' => $wasTree ? 1 : 0]; $t->left = 1; $g->touch($t); } } foreach ($g->creatures as $c) { $d = hypot($c->x - $f['x'], $c->z - $f['z']); if ($d < $f['r'] + 25 && empty($c->data['companion']) && $c->state !== 'dead') { $c->state = 'flee'; $c->data['from'] = [$f['x'], $f['z']]; } } $d = hypot($g->x - $f['x'], $g->z - $f['z']); if ($d < $f['r'] && !$g->swimming && $g->zone->waterDepth($g->x, $g->z) < 0.3) { $g->fireHurt -= $dt; if ($g->fireHurt <= 0) { $g->fireHurt = 1.2; $g->hitPlayer($g->rng()->chance(0.5) ? 'lleg' : 'rleg', 'burn', $g->rng()->range(0.08, 0.18), 'Le feu'); $g->inv->wear(3.0, ['legs', 'feet', 'worn']); } } } public static function addBurnt(Game $g, float $x, float $z, float $r): void { $list = $g->burnt[$g->zone->id] ?? []; foreach ($list as $k => [$bx, $bz, $br]) { if (hypot($bx - $x, $bz - $z) < max($br, $r) * 0.5) { $list[$k] = [$bx, $bz, max($br, $r)]; $g->burnt[$g->zone->id] = $list; return; } } $list[] = [round($x, 1), round($z, 1), round($r, 1)]; $g->burnt[$g->zone->id] = array_slice($list, -60); } /** The wind turns slowly; storms blow hard. [x, z, strength 0..1] */ private static function wind(Game $g, float $dt): void { [, , $ws] = $g->wind; // the direction drifts with the hours (no draw from the game's dice: the weather of a seed stays the same) $a = 1.3 * sin($g->minutes / 190.0 + $g->seed) + 0.6 * sin($g->minutes / 47.0); $target = $g->weather !== null ? (['storm' => 0.9, 'rain' => 0.5, 'snow' => 0.5, 'fog' => 0.05][$g->weather['kind']] ?? 0.25) * max(0.3, (float) $g->weather['amount']) : 0.2; $ws += ($target - $ws) * min(1.0, $dt * 0.1); $g->wind = [sin($a), cos($a), $ws]; } /** Lightning in a storm. */ private static function storm(Game $g, float $dt): void { $g->lightningTimer -= $dt; $storm = !$g->zone->cave && $g->weather !== null && $g->weather['kind'] === 'storm' && (float) $g->weather['amount'] > 0.35; if (!$storm) { $g->lightningTimer = max($g->lightningTimer, 4.0); return; } if ($g->lightningTimer > 0) { return; } $g->lightningTimer = $g->rng()->range(6.0, 22.0) / (float) $g->weather['amount']; self::strike($g); } /** Is Rob exposed to lightning (high above the land around, outdoors, not sheltered)? 0 .. 1 */ public static function exposure(Game $g): float { if ($g->zone->cave || $g->sheltered() || $g->swimming) { return 0.0; } $z = $g->zone; $sum = 0.0; foreach ([[40, 0], [-40, 0], [0, 40], [0, -40], [28, 28], [-28, -28]] as [$dx, $dz]) { $sum += $z->heightAt($g->x + $dx, $g->z + $dz); } $above = $g->y - $sum / 6; $metal = in_array($g->inv->hand, ['machete', 'crossbow', 'pulse_rifle', 'plasma_gun', 'flare_gun', 'harpoon'], true) ? 0.15 : 0.0; return max(0.0, min(1.0, ($above - 6) / 14 + $metal)); } /** A bolt falls: on Rob if exposed (sometimes), else on a tall tree or a height near him. */ public static function strike(Game $g, ?array $at = null): array { $amount = $g->weather !== null ? (float) $g->weather['amount'] : 1.0; $exp = self::exposure($g); if ($at === null && $exp > 0 && $g->rng()->chance(0.35 * $exp * $amount)) { $at = [$g->x + 0.4, $g->z + 0.3]; } if ($at === null) { $best = null; $bestScore = -INF; for ($k = 0; $k < 10; $k++) { $a = $g->rng()->range(0, 6.2832); $d = $g->rng()->range(25.0, 160.0); $x = max(8.0, min($g->zone->size - 8, $g->x + sin($a) * $d)); $z = max(8.0, min($g->zone->size - 8, $g->z + cos($a) * $d)); $tree = self::treeNear($g, $x, $z, 12.0); $score = $g->zone->heightAt($x, $z) + ($tree !== null ? 8.0 : 0.0) + $g->rng()->range(0, 6); if ($score > $bestScore) { $bestScore = $score; $best = $tree !== null ? [$tree->x, $tree->z] : [$x, $z]; } } $at = $best ?? [$g->x + 60, $g->z]; } [$x, $z] = $at; $dist = hypot($x - $g->x, $z - $g->z); $g->lightning = ['x' => $x, 'z' => $z, 't' => $g->clock]; $g->events[] = ['flash', 0.0, 1.0]; $g->events[] = ['thunder', $dist / 343.0, max(0.25, 1.0 - $dist / 600)]; $g->flags['strikes'] = (int) ($g->flags['strikes'] ?? 0) + 1; if ($dist < 2.5) { $g->hitPlayer('torso', 'burn', 0.55, 'La foudre'); $g->say('LA FOUDRE ! Une douleur blanche te traverse de part en part. Tu te réveilles face contre terre.'); $g->passTime(3); $g->flags['struck'] = (int) ($g->flags['struck'] ?? 0) + 1; return $at; } $tree = self::treeNear($g, $x, $z, 3.0); if ($tree !== null) { $tree->kind = 'burnt_tree'; $tree->data = ['label' => 'Arbre foudroyé', 'sticks' => 2]; $g->touch($tree); } if (self::ignite($g, $x, $z, 1.6) && $dist < 200) { $g->say($dist < 60 ? 'La foudre frappe tout près : un arbre s\'embrase !' : 'Au loin, la foudre allume un incendie.'); } return $at; } public static function treeNear(Game $g, float $x, float $z, float $r): ?Thing { $best = null; foreach ($g->zone->thingsNear($x, $z, $r) as $t) { if (in_array($t->kind, ['tree_broad', 'tree_palm', 'tree_conifer', 'tree_dead', 'giant_tree', 'mangrove', 'palm_coast'], true)) { if ($best === null || hypot($t->x - $x, $t->z - $z) < hypot($best->x - $x, $best->z - $z)) { $best = $t; } } } return $best; } /** The blazes for View::$fires (with their radius). */ public static function viewFires(Game $g): array { $out = []; foreach ($g->blazes as $f) { $out[] = ['x' => $f['x'], 'z' => $f['z'], 'minutes' => 60.0, 'r' => $f['r']]; } return $out; } /** Is a blaze near (a fire to warm oneself or cook on, a danger)? */ public static function near(Game $g, float $r): ?array { foreach ($g->blazes as $f) { if (hypot($f['x'] - $g->x, $f['z'] - $g->z) < $f['r'] + $r) { return $f; } } return null; } }