short name (numbered: "Coffre 2"), weight capacity (kg), stacks it holds, spoiling pace, footprint radius */ public const KINDS = [ 'chest_wood' => ['short' => 'Coffre', 'capacity' => 40.0, 'slots' => 24, 'pace' => 0.8, 'r' => 0.62], 'basket' => ['short' => 'Panier', 'capacity' => 12.0, 'slots' => 10, 'pace' => 1.0, 'r' => 0.42], 'chest_metal' => ['short' => 'Caisse', 'capacity' => 80.0, 'slots' => 36, 'pace' => 0.6, 'r' => 0.7], ]; /** the colour of the chests on the map and the radar */ public const COLOR = [235, 170, 70]; /** where a chest is put down: this far in front of Rob (metres) */ public const AHEAD = 1.5; /** a climber within this distance of a basket, Rob farther than THIEF_AWAY: a chance per game hour to lose a food */ public const THIEF_RANGE = 30.0; public const THIEF_AWAY = 12.0; public const THIEF_CHANCE = 0.25; public static function is(Thing $t): bool { return isset(self::KINDS[$t->kind]); } /** @return array{short: string, capacity: float, slots: int, pace: float, r: float} */ public static function spec(string $kind): array { return self::KINDS[$kind] ?? self::KINDS['chest_wood']; } /** "Coffre 2", "Panier 1": the name a chest was given when put down. */ public static function label(Thing $t): string { return (string) ($t->data['label'] ?? self::spec($t->kind)['short']); } // ------------------------------------------------------------------------------------------ putting down, picking up /** "Poser devant toi": the chest leaves the bag and stands on a free spot in front of Rob. Returns what happened. */ public static function place(Game $g, string $item): string { if (!isset(self::KINDS[$item]) || !$g->inv->has($item)) { return 'Tu n\'as pas cela.'; } if ($g->swimming || $g->raft !== null) { return 'Pas ici : il faut un sol ferme pour poser ' . mb_strtolower(Items::name($item)) . '.'; } $spot = self::spot($g, $item); if ($spot === null) { return 'Pas la place ici : il faut un sol libre devant toi (ni rocher, ni arbre, ni eau).'; } [$x, $z] = $spot; $n = (int) ($g->flags['chests'] ?? 0) + 1; $g->flags['chests'] = $n; $g->inv->remove($item); $label = self::spec($item)['short'] . ' ' . $n; // the front (local +z, the lid's hinge at the back) turned toward Rob $g->addThing($item, $x, $z, ['label' => $label, 'chest' => true, 'store' => [], 't' => $g->minutes], atan2($g->x - $x, $g->z - $z)); $g->passTime(3); return 'Tu poses ' . mb_strtolower(Items::name($item)) . ' devant toi : ' . $label . '. Ouvre-le pour y ranger tes affaires.'; } /** A free spot in front of Rob for that chest (not in a solid thing, a wall or the water, not on Rob), null: none. */ public static function spot(Game $g, string $item): ?array { $r = self::spec($item)['r']; $zone = $g->zone; foreach ([self::AHEAD, self::AHEAD + 0.6, self::AHEAD - 0.3] as $d) { foreach ([0.0, 0.35, -0.35, 0.7, -0.7] as $turn) { $x = $g->x + sin($g->yaw + $turn) * $d; $z = $g->z + cos($g->yaw + $turn) * $d; if ($x < 2 || $z < 2 || $x > $zone->size - 2 || $z > $zone->size - 2) { continue; } if (hypot($x - $g->x, $z - $g->z) < $r + Solids::ROB + 0.1 || $zone->waterDepth($x, $z) > 0.05) { continue; } if (Solids::free($zone, $x, $z, $r) && abs($zone->heightAt($x, $z) - $g->y) < 1.2) { return [$x, $z]; } } } return null; } /** Takes an empty chest back into the bag. Returns what happened. */ public static function pickUp(Game $g, Thing $t): string { if (self::store($t)->all() !== []) { return 'Vide-le d\'abord : on ne reprend qu\'un coffre vide.'; } if ($g->inv->add($t->kind) < 1) { return 'Ton sac est trop plein pour l\'emporter (' . Items::get($t->kind)['weight'] . ' kg).'; } $g->removeThing($t); $g->passTime(2); return 'Tu reprends ' . mb_strtolower(Items::name($t->kind)) . ' : il est dans ton sac.'; } /** The context actions of a chest. */ public static function options(Game $g, Thing $t, array &$o): void { $label = self::label($t); $o[] = ['id' => 'chest', 'label' => 'Ouvrir : ' . $label, 'run' => function () use ($g, $t): string { return self::open($g, $t); }]; if (self::store($t)->all() === []) { $o[] = ['id' => 'chest_take', 'label' => 'Reprendre : ' . $label . ' (vide)', 'run' => fn (): string => self::pickUp($g, $t)]; } } // ------------------------------------------------------------------------------------------ opening /** Opens the chest (the scene shows the transfer screen while Game::$chestOpen is set). Returns what Rob sees. */ public static function open(Game $g, Thing $t): string { $rotten = self::age($g, $t); $g->chestOpen = $t->id; $t->left = 0; // the lid open (Render\Props) $g->touch($t); $text = ''; $stolen = (array) ($t->data['stolen'] ?? []); if ($stolen !== []) { unset($t->data['stolen']); $g->touch($t); $text .= 'Des grimpeurs ont fouillé ' . mb_strtolower(self::label($t)) . ' : il manque ' . Dialogues::list($stolen) . '. '; } if ($rotten !== []) { $text .= 'Une odeur aigre : ' . Dialogues::list($rotten) . ' ' . (array_sum($rotten) > 1 ? 'se sont gâtés' : 's\'est gâté') . ' dans ' . mb_strtolower(self::label($t)) . '.'; } if ($text !== '') { $g->say(trim($text)); } return trim($text); } /** The chest open in the transfer screen (null: none, or it is no longer here). */ public static function opened(Game $g): ?Thing { if ($g->chestOpen === null) { return null; } $t = $g->zone->thing($g->chestOpen); return $t !== null && self::is($t) ? $t : null; } /** Closes the transfer screen: the lid comes down. */ public static function close(Game $g): void { $t = self::opened($g); if ($t !== null) { $t->left = 1; $g->touch($t); } $g->chestOpen = null; } // ------------------------------------------------------------------------------------------ contents /** The chest's contents as a bag of its own (its capacity: the chest's). */ public static function store(Thing $t): Inventory { $s = (array) ($t->data['store'] ?? []); return Inventory::fromArray(['items' => (array) ($s['items'] ?? []), 'age' => (array) ($s['age'] ?? []), 'cond' => (array) ($s['cond'] ?? []), 'capacity' => self::spec($t->kind)['capacity']]); } /** Writes the contents back into the thing (and the save's diff). */ private static function keep(Game $g, Thing $t, Inventory $s): void { $items = $s->all(); $t->data['store'] = $items === [] ? [] : [ 'items' => $items, 'age' => array_map(static fn (float $a): float => round($a, 2), array_intersect_key($s->age, $items)), 'cond' => array_intersect_key($s->cond, $items), ]; $g->touch($t); } /** Food in the chest ages for the time since it was last looked at. @return array what went bad */ public static function age(Game $g, Thing $t): array { $last = (float) ($t->data['t'] ?? $g->minutes); $hours = ($g->minutes - $last) / 60; if (!($hours > 0.01)) { return []; } $t->data['t'] = $g->minutes; $s = self::store($t); $zone = $g->zone; $climate = $zone->cave || $zone->id === 'plateau' ? 0.5 : ($zone->baseTemp > 27 ? 1.3 : 1.0); $rotten = $s->spoil($hours, self::spec($t->kind)['pace'] * $climate); self::keep($g, $t, $s); return $rotten; } /** How many of an item may leave the bag: what Rob holds in a hand or wears stays with him. */ public static function movable(Inventory $bag, string $id): int { $held = 0; foreach (['hand', 'handLeft', 'stowedLeft', ...Inventory::CLOTH_SLOTS] as $slot) { if ($bag->$slot === $id) { $held++; } } return max(0, $bag->count($id) - $held); } /** * Moves up to $n of an item from one bag to another, its food age and wear kept (mean of the stack). $slots: the most * different items $to may hold. Returns how many moved (the weight limit and the slots may stop it). */ public static function transfer(Inventory $from, Inventory $to, string $id, int $n, int $slots = PHP_INT_MAX): int { $n = min($n, $from->count($id)); if ($n <= 0) { return 0; } $had = $to->count($id); if ($had === 0 && count($to->all()) >= $slots) { return 0; } $ageFrom = $from->age[$id] ?? null; $ageTo = $to->age[$id] ?? null; $condFrom = $from->cond[$id] ?? null; $condTo = $to->cond[$id] ?? null; $fit = $to->add($id, $n); if ($fit <= 0) { return 0; } if ($ageFrom !== null || $ageTo !== null) { $to->age[$id] = (($ageTo ?? 0.0) * $had + ($ageFrom ?? 0.0) * $fit) / ($had + $fit); } if ($condFrom !== null || $condTo !== null) { $to->cond[$id] = (($condTo ?? 100.0) * $had + ($condFrom ?? 100.0) * $fit) / ($had + $fit); } $from->remove($id, $fit); if (!$from->has($id)) { unset($from->cond[$id]); } return $fit; } /** Bag → chest: up to $n of an item (never what is held or worn). Returns how many went in. */ public static function put(Game $g, Thing $t, string $id, int $n = 1): int { self::age($g, $t); $s = self::store($t); $done = self::transfer($g->inv, $s, $id, min($n, self::movable($g->inv, $id)), self::spec($t->kind)['slots']); if ($done > 0) { self::keep($g, $t, $s); } return $done; } /** Chest → bag: up to $n of an item (the bag's weight limit). Returns how many came out. */ public static function take(Game $g, Thing $t, string $id, int $n = 1): int { self::age($g, $t); $s = self::store($t); $done = self::transfer($s, $g->inv, $id, $n); if ($done > 0) { self::keep($g, $t, $s); } return $done; } /** "Tout déposer": everything of the bag but what is held or worn, as much as the chest takes. @return array */ public static function putAll(Game $g, Thing $t): array { $moved = []; foreach (array_keys($g->inv->all()) as $id) { $n = self::put($g, $t, (string) $id, PHP_INT_MAX); if ($n > 0) { $moved[(string) $id] = $n; } } return $moved; } /** "Tout prendre": everything the bag can carry, lightest first so that the most items fit. @return array */ public static function takeAll(Game $g, Thing $t): array { $ids = array_keys(self::store($t)->all()); usort($ids, static fn ($a, $b): int => Items::get((string) $a)['weight'] <=> Items::get((string) $b)['weight']); $moved = []; foreach ($ids as $id) { $n = self::take($g, $t, (string) $id, PHP_INT_MAX); if ($n > 0) { $moved[(string) $id] = $n; } } return $moved; } // ------------------------------------------------------------------------------------------ the zone /** @return list the chests of the zone Rob is in */ public static function inZone(Game $g): array { $out = []; foreach ($g->zone->things as $t) { if (isset(self::KINDS[$t->kind])) { $out[] = $t; } } return $out; } /** Climbers raid the open baskets left alone ($minutes of game time passed: called every couple of seconds). */ public static function thieves(Game $g, float $minutes): void { $climbers = array_filter($g->creatures, static fn (Creature $c): bool => $c->species === 'climber' && $c->state !== 'dead' && empty($c->data['companion'])); if ($climbers === []) { return; } foreach (self::inZone($g) as $t) { if ($t->kind !== 'basket' || hypot($t->x - $g->x, $t->z - $g->z) < self::THIEF_AWAY) { continue; } $near = false; foreach ($climbers as $c) { if (hypot($c->x - $t->x, $c->z - $t->z) < self::THIEF_RANGE) { $near = true; break; } } if (!$near || !$g->rng()->chance(self::THIEF_CHANCE * $minutes / 60)) { continue; } $s = self::store($t); $food = array_values(array_filter(array_keys($s->all()), static fn ($id): bool => Items::get((string) $id)['kind'] === 'food')); if ($food === []) { continue; } $id = (string) $food[$g->rng()->int(0, count($food) - 1)]; $s->remove($id, 1); $t->data['stolen'][$id] = (int) ($t->data['stolen'][$id] ?? 0) + 1; self::keep($g, $t, $s); } } }