15 Companion server.js (7/13)

// Which directions lead somewhere? Try each one on a throwaway copy of the
// game and see whether the location changes. Also finds the player object
// (the one that moved along), so it isn't offered as "examine yourself".
function survey(game, state) {
  if (state.exits) return state.exits;
  const m = machineFor(game, state);
  const loc = location(m);
  const result = { exits: [], objects: [] };
  if (!loc || state.waiting !== 'line' || state.quit) return (state.exits = result);
  const children = (mm, o) => {
    const list = [];
    for (let c = mm.child(o), n = 0; c && n < 100; c = mm.sibling(c), n++) list.push(c);
    return list;
  };
  const here = children(m, loc);
  let moved = null; // objects that came along on every move: the player (and floating scenery)
  for (const dir of DIRECTIONS) {
    try {
      const probe = machineFor(game, state);
      probe.undoState = null;
      probe.saveSlot = null;
      const r = probe.sendLine(dir);
      const now = location(probe);
      if (!r.quit && now && now !== loc) {
        result.exits.push(dir);
        const along = children(probe, now).filter((c) => here.includes(c));
        moved = moved ? moved.filter((c) => along.includes(c)) : along;
      }
    } catch { /* a probe that crashes is just not an exit */ }
  }
  const player = moved && moved.length === 1 ? moved[0] : 0;
  const skip = /^(\(.*\)|yourself|you|me|myself|self|player|darkness)$/i;
  for (const o of here) {
    if (o === player) continue;
‹ PreviousReader file (.txt)Next ›