<?phpnamespace App\Revisited\Sim;/** * What Rob carries: counts per item, a weight limit, a hand slot and six clothing slots: head (cap, hat, helmet), under * (an inner layer on the torso), worn (the outer garment: the flight suit, a jacket, a coat), legs (trousers, leggings, * worn over the suit too), hands (gloves), feet (boots). A garment keeps the parts of the body in its `cover` warm. */final class Inventory{ public const DEFAULT_CAPACITY = 25.0; /** clothing slots, from head to feet */ public const CLOTH_SLOTS = ['head', 'under', 'worn', 'legs', 'hands', 'feet']; public const SLOT_LABEL = ['head' => 'Tête', 'under' => 'Torse (dessous)', 'worn' => 'Corps (dessus)', 'legs' => 'Jambes', 'hands' => 'Mains', 'feet' => 'Pieds']; /** the regions of the body clothes cover */ public const REGIONS = ['head', 'torso', 'arms', 'hands', 'legs', 'feet']; /** both hands on it: equipping one puts the left item away (and back when it is put away) */ public const TWO_HANDED = ['bow', 'crossbow', 'pulse_rifle', 'plasma_gun', 'raft_pole']; /** go to the left hand by themselves (lights, shields) */ public const LEFT_FIRST = ['torch', 'shield_hide', 'shield_scrap']; /** held on the left arm only (never in the right hand), never with a torch: the torch or the shield */ public const SHIELDS = ['shield_hide', 'shield_scrap']; public ?string $hand = null; // the right hand: weapon / tool (a two-handed one takes both hands); it stays counted in the bag public ?string $handLeft = null; // the left hand: a torch, or a one-handed tool / weapon /** the left item a two-handed one put away: back in the left hand when the two-handed one is put away */ public ?string $stowedLeft = null; public ?string $head = null; // cap, hat, helmet public ?string $under = null; // inner layer on the torso public ?string $worn = null; // the outer garment public ?string $legs = null; // trousers, leggings public ?string $hands = null; // gloves public ?string $feet = null; // boots /** @var array<string, float> condition of the clothes, 0 (in shreds) .. 100 (new); a missing id is new */ public array $cond = []; /** 0 dry .. 1 soaked: the clothes worn (rain, swimming); they dry by the fire */ public float $wet = 0.0; /** @var array<string, float> foods: the mean age of the stack in hours (Catalog2::spoilHours() turns it bad) */ public array $age = []; /** @var array<string,int> */ private array $items = []; private float $capacity; /** Rob's force (Body::carryFactor()): the bag's capacity times this (not saved: given by Game at each tick) */ public float $carry = 1.0; public function __construct(float $capacity = self::DEFAULT_CAPACITY) { $this->capacity = $capacity; } /** @return int how many were added (the weight limit may stop it) */ public function add(string $id, int $n = 1): int { if ($n <= 0 || !Items::exists($id)) { return 0; } $w = Items::get($id)['weight']; $fit = $n; if ($w > 0) { $free = $this->capacity() - $this->weight(); $fit = max(0, min($n, (int) floor(($free + 1e-9) / $w))); } if ($fit > 0) { $had = $this->items[$id] ?? 0; if (isset($this->age[$id]) && $had > 0) { $this->age[$id] = $this->age[$id] * $had / ($had + $fit); // fresh ones lower the stack's mean age } $this->items[$id] = $had + $fit; } return $fit; } public function remove(string $id, int $n = 1): bool { if ($n <= 0 || $this->count($id) < $n) { return false; } $this->items[$id] -= $n; if ($this->items[$id] <= 0) { unset($this->items[$id]); unset($this->age[$id]); foreach (['hand', 'handLeft', ...self::CLOTH_SLOTS] as $slot) { if ($this->$slot === $id) { $this->$slot = null; } } if ($this->stowedLeft === $id) { $this->stowedLeft = null; } } elseif ($this->hand === $id && $this->handLeft === $id && $this->items[$id] < 2) { $this->handLeft = null; // one left: it cannot be in both hands } return true; } public function count(string $id): int { return $this->items[$id] ?? 0; } public function has(string $id, int $n = 1): bool { return $this->count($id) >= $n; } /** @return array<string,int> */ public function all(): array { return $this->items; } public function weight(): float { $w = 0.0; foreach ($this->items as $id => $n) { $w += Items::get($id)['weight'] * $n; } return $w; } /** What Rob can carry (kg): the bag (and a companion) times his force. */ public function capacity(): float { return $this->capacity * max(0.5, $this->carry); } public function setCapacity(float $kg): void { $this->capacity = $kg; } /** 0..1+: how loaded Rob is (feeds Body::$loadRatio). */ public function load(): float { $cap = $this->capacity(); return $cap > 0 ? $this->weight() / $cap : 0.0; } /** * A tool or weapon goes to a hand (a torch to the left one, the rest to the right one), clothing is worn. * Null = done, else the reason. */ public function equip(string $id): ?string { if (!$this->has($id)) { return 'Tu ne portes pas cela.'; } $d = Items::get($id); if ($d['kind'] === 'clothing') { $slot = self::slotOf($id); $this->$slot = $id; return null; } if (!self::holdable($id)) { return 'On ne peut pas tenir cela en main.'; } return in_array($id, self::LEFT_FIRST, true) ? $this->equipLeft($id) : $this->equipRight($id); } public static function holdable(string $id): bool { $d = Items::get($id); return in_array($d['kind'], ['weapon', 'tool'], true) || isset($d['damage']); } public static function twoHanded(?string $id): bool { return $id !== null && in_array($id, self::TWO_HANDED, true); } public static function isShield(?string $id): bool { return $id !== null && in_array($id, self::SHIELDS, true); } /** The shield on the left arm (and still in the bag), null: none. */ public function shield(): ?string { return self::isShield($this->handLeft) && $this->has((string) $this->handLeft) ? $this->handLeft : null; } /** The right hand takes it; a two-handed item puts the left one away (remembered), a one-handed one gives it back. */ public function equipRight(string $id): ?string { if (!$this->has($id) || !self::holdable($id)) { return 'On ne peut pas tenir cela en main.'; } if (self::isShield($id)) { return $this->equipLeft($id); // a shield is worn on the left arm } if ($this->handLeft === $id && $this->count($id) < 2) { $this->handLeft = null; // it moves from the left hand } if ($id === 'torch' && self::isShield($this->handLeft)) { $this->handLeft = null; // the torch or the shield: the shield goes on the back } if (self::twoHanded($id)) { if ($this->handLeft !== null) { $this->stowedLeft = $this->handLeft; $this->handLeft = null; } } else { $this->restoreLeft($id); } $this->hand = $id; return null; } /** The left hand takes it (never a two-handed item); a two-handed item in the right hand is put away. */ public function equipLeft(string $id): ?string { if (!$this->has($id) || !self::holdable($id)) { return 'On ne peut pas tenir cela en main.'; } if (self::twoHanded($id)) { return 'Il faut les deux mains pour cela.'; } if ($this->hand === $id && $this->count($id) < 2) { $this->hand = null; } if (self::twoHanded($this->hand)) { $this->hand = null; // the bow goes back on the shoulder } if (self::isShield($id) && $this->hand === 'torch') { $this->hand = null; // the torch or the shield: the torch is put out and away } $this->handLeft = $id; $this->stowedLeft = null; return null; } /** Right and left exchanged (a two-handed item stays where it is). Null = done, else the reason. */ public function swapHands(): ?string { if ($this->hand === null && $this->handLeft === null) { return 'Tes mains sont vides.'; } if (self::twoHanded($this->hand)) { return 'Il te faut les deux mains pour ' . mb_strtolower(Items::name((string) $this->hand)) . '.'; } if (self::isShield($this->handLeft)) { return 'Le bouclier se porte au bras gauche.'; } [$this->hand, $this->handLeft] = [$this->handLeft, $this->hand]; return null; } /** The slot holding an item: 'hand' (right), 'handLeft', null: not in hand. */ public function heldSlot(string $id): ?string { return $this->hand === $id ? 'hand' : ($this->handLeft === $id ? 'handLeft' : null); } /** In either hand (and still in the bag). */ public function holds(string $id): bool { return $this->heldSlot($id) !== null && $this->has($id); } /** The left item a two-handed one put away comes back (if still carried and not now in the right hand). */ private function restoreLeft(?string $right): void { $back = $this->stowedLeft; $this->stowedLeft = null; if ($back !== null && $this->handLeft === null && $this->has($back) && ($back !== $right || $this->count($back) > 1) && !($right === 'torch' && self::isShield($back))) { $this->handLeft = $back; } } public function unequip(string $slot): void { if ($slot === 'hand') { $this->hand = null; $this->restoreLeft(null); return; } if ($slot === 'handLeft') { $this->handLeft = null; $this->stowedLeft = null; return; } if (in_array($slot, self::CLOTH_SLOTS, true)) { $this->$slot = null; } } /** The clothing slot of a garment (head under worn legs hands feet). */ public static function slotOf(string $id): string { $slot = (string) (Items::get($id)['slot'] ?? 'worn'); return in_array($slot, self::CLOTH_SLOTS, true) ? $slot : 'worn'; } /** @return list<string> the regions a garment covers */ public static function coverOf(string $id): array { $d = Items::get($id); if (isset($d['cover'])) { return (array) $d['cover']; } return match (self::slotOf($id)) { 'head' => ['head'], 'hands' => ['hands'], 'feet' => ['feet'], 'legs' => ['legs'], 'under' => ['torso'], default => ['torso', 'arms'], }; } /** @return array<string, string> slot => the garment worn there (only the slots in use) */ public function wornClothes(): array { $out = []; foreach (self::CLOTH_SLOTS as $slot) { $id = $this->$slot; if ($id !== null && $this->has($id)) { $out[$slot] = $id; } } return $out; } /** Is this item worn in one of the clothing slots? */ public function isWorn(string $id): ?string { foreach (self::CLOTH_SLOTS as $slot) { if ($this->$slot === $id) { return $slot; } } return null; } /** Degrees of warmth of one garment now (worn out and wet: less). */ public function garmentWarmth(string $id): float { return (float) (Items::get($id)['warmth'] ?? 0) * (0.35 + 0.65 * $this->condition($id) / 100) * (1 - 0.55 * $this->wet); } /** Degrees of warmth on one region of the body (head torso arms hands legs feet): what covers it, shared out. */ public function regionWarmth(string $region): float { $t = 0.0; foreach ($this->wornClothes() as $id) { $cover = self::coverOf($id); if (in_array($region, $cover, true)) { $t += $this->garmentWarmth($id) * (count($cover) > 1 ? 1.6 / count($cover) : 1.0); } } return $t; } /** * Degrees of warmth of what is worn (for Environment::$clothing): a worn-out garment warms less, a wet one much * less. */ public function warmth(): float { $t = 0.0; foreach ($this->wornClothes() as $id) { $t += $this->garmentWarmth($id); } return $t; } /** 0..100: the condition of a garment (100 when never worn). */ public function condition(string $id): float { return $this->cond[$id] ?? 100.0; } /** Wears the garments in these slots (default: all the clothes worn) down by $amount points. */ public function wear(float $amount, array $slots = self::CLOTH_SLOTS): void { foreach ($slots as $slot) { $id = $this->$slot ?? null; if ($id !== null && $this->has($id)) { $this->cond[$id] = max(0.0, $this->condition($id) - $amount); } } } /** Mends the most worn garment worn by $points; returns its id, null: nothing to mend. */ public function mend(float $points): ?string { $best = null; foreach ($this->wornClothes() as $id) { if ($this->condition($id) < 99 && ($best === null || $this->condition($id) < $this->condition($best))) { $best = $id; } } if ($best !== null) { $this->cond[$best] = min(100.0, $this->condition($best) + $points); } return $best; } /** 0..0.5: the share of a wound's severity the clothes take on that part (head torso larm rarm lleg rleg). */ public function protection(string $part): float { $region = match ($part) { 'larm', 'rarm' => 'arms', 'lleg', 'rleg' => 'legs', 'head' => 'head', default => 'torso', }; $p = 0.0; foreach ($this->wornClothes() as $slot => $id) { $cover = self::coverOf($id); $k = in_array($region, $cover, true) ? 1.0 : 0.0; if ($k === 0.0 && (($slot === 'hands' && $region === 'arms') || ($slot === 'feet' && $region === 'legs'))) { $k = 0.5; // gloves and boots take a part of what hits a limb } if ($k > 0) { $p += $k * (float) (Items::get($id)['protect'] ?? 0) * (0.3 + 0.7 * $this->condition($id) / 100); } } return min(0.5, $p); } /** The garment's state in words, agreed with its name ("usée", "en lambeaux"); '' when it is in good shape. */ public function conditionWord(string $id): string { $c = $this->condition($id); if (Items::get($id)['kind'] !== 'clothing' || $c >= 70) { return ''; } if ($c < 20) { return 'en lambeaux'; } // whole words (each one a key of the English dictionary) return ['m' => 'usé', 'f' => 'usée', 'mp' => 'usés', 'fp' => 'usées'][(string) (Items::get($id)['g'] ?? 'm')] ?? 'usé'; } /** The item's name with the state of a garment: "Combinaison de vol déchirée (usée)". */ public function label(string $id): string { $w = $this->conditionWord($id); return Items::name($id) . ($w === '' ? '' : ' (' . $w . ')'); } /** The castaway's clothes after the crash: the torn flight suit and the pilot boots, worn, already damaged. */ public function dressForTheCrash(): void { foreach (['flight_suit_torn' => 46.0, 'pilot_boots_worn' => 58.0] as $id => $c) { if (!$this->has($id)) { $this->items[$id] = 1; } $this->cond[$id] = $c; } $this->worn = 'flight_suit_torn'; $this->feet = 'pilot_boots_worn'; } /** Is a tool of that capability ('cut', 'fire', 'pot'...) in the bag? */ public function hasTool(string $capability): bool { foreach ($this->items as $id => $n) { if ((Items::get($id)['tool'] ?? null) === $capability) { return true; } } return false; } /** Use an item on the body: eat, drink, heal. The item is spent only if it did something. French text. */ public function use(Body $body, string $id, string $part = 'torso'): string { if (!$this->has($id)) { return 'Tu n\'as pas cela.'; } $d = Items::get($id); $heal = $d['heal'] ?? []; $local = isset($heal['bleed']) || isset($heal['fracture']) || isset($heal['infection']); $msg = $local ? $body->treat($id, $part) : $body->consume($id); if ($body->lastActionOk) { $this->remove($id); if (isset($d['returns'])) { $this->add($d['returns']); } } return $msg; } /** * Foods age: $hours of time at $pace (cold 0.5, heat 1.3). A stack past its time turns into rotten food (or what its * `rots` says). @return array<string, int> what went bad */ public function spoil(float $hours, float $pace = 1.0): array { $out = []; foreach ($this->items as $id => $n) { $life = Catalog2::spoilHours($id); if ($life === null) { continue; } $this->age[$id] = ($this->age[$id] ?? 0.0) + $hours * $pace; if ($this->age[$id] >= $life) { $to = (string) (Items::get($id)['rots'] ?? 'food_rotten'); unset($this->items[$id], $this->age[$id]); foreach (['hand', 'handLeft', 'stowedLeft'] as $h) { if ($this->$h === $id) { $this->$h = null; } } $this->items[$to] = ($this->items[$to] ?? 0) + $n; $out[$id] = $n; } } return $out; } /** 1 fresh .. 0 about to rot; null: it keeps */ public function freshness(string $id): ?float { $life = Catalog2::spoilHours($id); return $life === null ? null : max(0.0, 1 - ($this->age[$id] ?? 0.0) / $life); } public function toArray(): array { return ['age' => array_map(fn (float $a): float => round($a, 2), $this->age), 'items' => $this->items, 'hand' => $this->hand, 'handLeft' => $this->handLeft, 'stowedLeft' => $this->stowedLeft, 'head' => $this->head, 'under' => $this->under, 'worn' => $this->worn, 'legs' => $this->legs, 'hands' => $this->hands, 'feet' => $this->feet, 'capacity' => $this->capacity, 'cond' => $this->cond, 'wet' => round($this->wet, 3)]; } public static function fromArray(array $a): self { $inv = new self((float) ($a['capacity'] ?? self::DEFAULT_CAPACITY)); foreach (($a['items'] ?? []) as $id => $n) { if (Items::exists((string) $id) && (int) $n > 0) { $inv->items[(string) $id] = (int) $n; } } foreach ((array) ($a['age'] ?? []) as $id => $h) { if (isset($inv->items[(string) $id])) { $inv->age[(string) $id] = max(0.0, (float) $h); } } foreach (['hand', ...self::CLOTH_SLOTS] as $slot) { $v = $a[$slot] ?? null; $ok = $v !== null && $inv->has((string) $v); if ($ok && $slot !== 'hand') { // a save of before the six slots: a garment in "worn" that belongs elsewhere goes to its slot $real = self::slotOf((string) $v); if ($real !== $slot && $inv->$real === null) { $inv->$real = (string) $v; continue; } } $inv->$slot = $ok ? (string) $v : null; } if (array_key_exists('handLeft', $a)) { foreach (['handLeft', 'stowedLeft'] as $h) { $v = $a[$h] ?? null; $inv->$h = $v !== null && $inv->has((string) $v) ? (string) $v : null; } if ($inv->hand !== null && $inv->hand === $inv->handLeft && $inv->count($inv->hand) < 2) { $inv->handLeft = null; } } elseif ($inv->hand !== null && in_array($inv->hand, self::LEFT_FIRST, true)) { // a save of before the two hands: the torch held goes to the left hand $inv->handLeft = $inv->hand; $inv->hand = null; } if (!array_key_exists('cond', $a)) { // a save of before the clothes: Rob gets the clothes he crashed in if ($inv->worn === null && $inv->feet === null) { $inv->dressForTheCrash(); } } else { foreach ((array) $a['cond'] as $id => $c) { if (Items::exists((string) $id)) { $inv->cond[(string) $id] = max(0.0, min(100.0, (float) $c)); } } $inv->wet = max(0.0, min(1.0, (float) ($a['wet'] ?? 0))); } return $inv; }}