summaryrefslogtreecommitdiff
path: root/src/world/lattice.test.js
blob: c2fdb6bcb172879753de12e19f620008c0d27b70 (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
101
102
103
104
105
106
107
108
'use strict';

import { lattice_update, lattice_valid, lattice_apply } from './lattice.js';


test("growth update rule", () => {
  const lattice = [[ 
    { type: 'empty', flags: {} }, 
    { type: 'empty', flags: {} }, 
    { type: 'plant', flags: {} },
  ]];
  const update_rules = {
    plant: () => {},
    empty: (lattice, x, y) => {
      if (lattice[y][x+1].type === 'plant') {
        return { world_updates: [{ x, y, from: 'empty', to: 'plant' }] };
      }
    },
  };

  expect(lattice_update(lattice, update_rules)).toEqual([
    { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' }] },
  ]);

  lattice[0][1] = { type: 'plant' };

  expect(lattice_update(lattice, update_rules)).toEqual([
    { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' }] },
  ]);

  lattice[0][0] = { type: 'plant' };

  expect(lattice_update(lattice, update_rules)).toEqual([]);
});


//test("agents cannot move into non-empty tiles", () => {
//  const lattice = [[ {type: 'empty', flags: {}}, {type: 'filled', flags: {}} ]];
//  const bad_prop = [{ agent_updates: [{ agent_id: 14, x: 1, y: 0 }] }];
//  expect(lattice_valid(lattice, bad_prop)).toBe(false);
//  const good_prop = [{ agent_updates: [{ agent_id: 14, x: 0, y: 0 }] }];
//  expect(lattice_valid(lattice, bad_prop)).toBe(true);
//});


test("growth update rule applied", () => {
  const lattice = [[ 
    { type: 'empty', flags: {} }, 
    { type: 'empty', flags: {} }, 
    { type: 'plant', flags: {} },
  ]];
  expect(lattice_apply(lattice, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([[
    { type: 'empty', flags: {} }, 
    { type: 'plant', flags: {} }, 
    { type: 'plant', flags: {} },
  ]]);

  expect(lattice_apply(lattice, [
    { world_updates: [{ x: 2, y: 0, from: 'plant', to: 'empty' } ]},
    { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' } ]},
    { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' } ]},
  ])).toEqual([[
    { type: 'plant', flags: {} }, 
    { type: 'plant', flags: {} }, 
    { type: 'empty', flags: {} },
  ]]);
});


test("check proposals agains lattice for validity", () => {
  const lattice = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]];
  expect(lattice_valid(lattice, { world_updates: [{ x: -1, y: 0, from: 'blah', to: 'blah' }] })).toBe(false);
  expect(lattice_valid(lattice, { world_updates: [{ x: 0, y: 0, from: 'blah', to: 'blah' }] })).toBe(false);
  expect(lattice_valid(lattice, { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'blah' }] })).toBe(true);
  expect(lattice_valid(lattice, { world_updates: [{ x: 2, y: 0, from: 'empty', to: 'blah' }] })).toBe(false);
  expect(lattice_valid(lattice, { world_updates: [{ x: 2, y: 1, from: 'empty', to: 'blah' }] })).toBe(false);
});


test("proposals update cell flags appropriately", () => {
  const lattice = [
    [ 
      { type: 'empty', flags: { step: 1} }, 
      { type: 'empty', flags: {} }, 
      { type: 'plant', flags: { foo: 'bar' } },
    ]
  ];

  // flags are reset each time step
  expect(lattice_apply(lattice, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([[
    { type: 'empty', flags: {} }, 
    { type: 'plant', flags: {} }, 
    { type: 'plant', flags: {} },
  ]]);

  // flags are combined when updating
  expect(lattice_apply(lattice, [
    { world_updates: [{ x: 1, y: 0, flags: { foo: 'bar' } } ]},
    { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant', flags: { baz: 'baz' } } ]},
    { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant', flags: { foo: 'foo' } } ]},
    { world_updates: [{ x: 0, y: 0, flags: { beep: 'boop' } } ]},
  ])).toEqual([[
    { type: 'plant', flags: { foo: 'foo', beep: 'boop' } }, 
    { type: 'plant', flags: { foo: 'bar', baz: 'baz' } }, 
    { type: 'plant', flags: {} },
  ]]);

});