mwg API
    Preparing search index...

    Function personalScoreView

    • The view one subject has: its own score, and the scores of the allies and enemies it can see.

      sees is the visibility set, injected rather than assumed: a unit's own sight for an actor, the union over a side's units for a whole-side mind. The subject sees its own score whether or not it can see where it stands, so own is always read and seen always counts it.

      Type Parameters

      • T

      Parameters

      Returns ScoreView

      import { personalScoreView, scoreWith } from '@datamoc/mw_games/ai';

      const hero = { id: 'hero', side: 'blue', x: 2, y: 2 };
      const world = [
      hero,
      { id: 'ally', side: 'blue', x: 3, y: 2 },
      { id: 'rat', side: 'red', x: 4, y: 2 },
      { id: 'dragon', side: 'red', x: 9, y: 9 },
      ];
      const worth = (id: string) => ({ hero: 10, ally: 4, rat: -2, dragon: -40 })[id] ?? 0;

      // what a scout with a two-cell reach can see
      const sees = (x: number, y: number) => Math.abs(x - hero.x) + Math.abs(y - hero.y) <= 2;

      const view = personalScoreView(hero, world, worth, sees);
      console.log(view); // { own: 10, allies: 4, enemies: -2, seen: 3 } - the dragon is out of reach
      console.log(scoreWith(view, { own: 1, allies: 1, enemies: -1 })); // 16 - the rat is a nuisance