summaryrefslogtreecommitdiff
path: root/src/world/world.js
blob: e1ba0cf55c8965c49e0748eb3a6485fa59d5a085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { lattice_update, lattice_valid, lattice_apply } from './lattice.js';
import { agent_decide, agent_apply } from './agent.js';
import { proposal_merge } from './proposal.js';


// world structure:
// {
//   lattice
//   lattice_rules: object
//   agents: agent[]
//   senses: sense[]
//   actions: action[]
//   validity: (function(proposal) => bool)[]
// }


export function world_update(world, postprocess=[]) {
  const lattice_props = lattice_update(world.lattice, world.lattice_rules);
  const intermediate_lattice = lattice_apply(world.lattice, lattice_props);

  const decisions = world.agents
    .map(a => agent_decide(world, agent, world.senses, world.actions))
    .reduce(
      ([agents, props], [agent, prop]) => [[...agents, agent], [...props, prop]],
      [[], []]
    );
  const intermediate_agents = decisions[0];
  const agent_props = decisions[1]
    .flat()
    .reduce((acc, prop) => proposal_merge(acc, prop), [])
    .filter(prop => lattice_valid(intermediate_lattice, prop))

  const lattice = lattice_apply(intermediate_lattice, world.validity.reduce(
    (acc, rule) => acc.filter(rule),
    agent_props
  ));
  const agents = intermediate_agents.map(a => agent_apply(a, agent_props));

  const new_world = {...world, lattice, agents};
  return postprocess.reduce(
    (acc, f) => f(acc),
    new_world
  );
}