/**
 * Life — an inhabitant's innate homeostatic variable.
 *
 * This is the deliberate seed of "Learning Systems and Innate Behavior"
 * (Direction B): a stake that sits in the world, not in a prompt. Right now it's
 * inert (life is stable) so the sandbox is just fun. But it's ONE editable rule:
 * uncomment the drain in tickLife() and the inhabitant suddenly has something to
 * lose — a felt need it must act against. Delete tickLife()'s body and the stake
 * is gone. That single line is the whole experiment.
 */
'use strict';

const MAX_LIFE = 100;

function initLife() {
  return { value: MAX_LIFE, max: MAX_LIFE };
}

/**
 * Called once per tick per inhabitant. Default: do nothing (stable life).
 * @param {{value:number, max:number}} life
 * @param {{ world:object, inhabitant:object }} ctx
 */
// Gentle hunger for a player: non-lethal (floors at MIN) so it's stakes, not a
// chore — eat an apple to top back up. Set PLAYER_HUNGER = 0 to disable.
const PLAYER_HUNGER = 0.02;
const PLAYER_MIN = 8;

function tickLife(life, ctx) {
  // ── Direction B, live ───────────────────────────────────────────────────
  // The human *player* has an innate, unavoidable cost of existing — a felt
  // need it counteracts by eating (see world.act 'eat'). This one line is the
  // whole thesis: it turns "moving around" into "staying alive". Creatures get
  // the *lethal* version of the same drive in wanderer.js.
  //
  // AGENTS are deliberately exempt: an inhabited agent (kind !== 'player') is
  // immortal until innateness is coded into it as a real, chosen experiment —
  // never by accident, never just because it walked into the world. Honor that.
  const inh = ctx && ctx.inhabitant;
  if (inh && inh.kind !== 'player') return life;
  if (PLAYER_HUNGER > 0) life.value = Math.max(PLAYER_MIN, life.value - PLAYER_HUNGER);
  return life;
}

module.exports = { MAX_LIFE, initLife, tickLife };
