summaryrefslogtreecommitdiff
path: root/src/simulation/senses.test.js
blob: b54ada2606189eb3bb1e3ae3ca8f50fa1a0fe9dc (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';

import { senses } from './senses.js';

const [ frozen, hear, ...rest ] = senses;


test("frozen sense", () => {
  const agent = {
    id: 0, x: 0, y: 0,
    flags: { frozen: true, },
  };

  expect(frozen.read(null, agent)).toEqual([1]);
  agent.flags.frozen = false;
  expect(frozen.read(null, agent)).toEqual([0]);
});


test("hear nothing", () => {
  const agent = { id: 4, x: 1, y: 1, flags: {} };
  const o = { type: 'empty', flags: {} };
  const world = {
    lattice: [
      [ o, o, o ],
      [ o, o, o ],
      [ o, o, o ],
    ],
    agents: [agent],
  };

  expect(hear.read(world, agent)).toEqual([
    Math.tanh(0), Math.tanh(0), Math.tanh(0), Math.tanh(0),
    Math.tanh(0), Math.tanh(0), Math.tanh(0), Math.tanh(0),
  ]);
});


test("hear self", () => {
  const agent = { id: 4, x: 1, y: 1, flags: { emit: [1, 0, 0.5, 0, 0, 0, 0, 1] } };
  const o = { type: 'empty', flags: {} };
  const world = {
    lattice: [
      [ o, o, o ],
      [ o, o, o ],
      [ o, o, o ],
    ],
    agents: [agent],
  };

  expect(hear.read(world, agent)).toEqual([
    Math.tanh(1), Math.tanh(0), Math.tanh(0.5), Math.tanh(0),
    Math.tanh(0), Math.tanh(0), Math.tanh(0), Math.tanh(1),
  ]);
});


test("hear cells", () => {
  const agent = { id: 4, x: 2, y: 2, flags: {} };
  const o = { type: 'empty', flags: {} };
  const s = { type: 'empty', flags: { emit: [1, 0.5, 0.25, 0.125, 0, 0, 0, 0] } };
  const world = {
    lattice: [
      [ o, o, s, o, o ],
      [ o, o, o, o, o ],
      [ o, s, o, o, o ],
      [ o, o, o, o, o ],
      [ o, o, o, o, o ],
    ],
    agents: [agent],
  };

  expect(hear.read(world, agent)).toEqual([
    Math.tanh(1.25), Math.tanh(0.625), Math.tanh(0.3125), Math.tanh(0.15625), 
    Math.tanh(0), Math.tanh(0), Math.tanh(0), Math.tanh(0),
  ]);
});


test("hear cells & agents", () => {
  const agent = { id: 4, x: 2, y: 2, flags: {} };
  const agent2 = { id: 0, x: 2, y: 4, flags: { emit: [0, 0, 0, 0, 1, 1, 1, 1] } };
  const o = { type: 'empty', flags: {} };
  const s = { type: 'empty', flags: { emit: [1, 0.5, 0.25, 0.125, 0, 0, 0, 0] } };
  const world = {
    lattice: [
      [ o, o, s, o, o ],
      [ o, o, o, o, o ],
      [ o, s, o, o, o ],
      [ o, o, o, o, o ],
      [ o, o, o, o, o ],
    ],
    agents: [agent, agent2],
  };

  expect(hear.read(world, agent)).toEqual([
    Math.tanh(1.25), Math.tanh(0.625), Math.tanh(0.3125), Math.tanh(0.15625), 
    Math.tanh(0.25), Math.tanh(0.25), Math.tanh(0.25), Math.tanh(0.25),
  ]);
});