diff options
author | sanine <sanine.not@pm.me> | 2023-11-11 15:04:36 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-11 15:04:36 -0600 |
commit | f7b0ed22b2fd0ddafc4a84dde1ad8e3208144844 (patch) | |
tree | e290cda13f58eb9a2e3cf2e5e7b05748a0c8e982 /src/world | |
parent | c2466a35fb1edce6088b72df17444631f7034e07 (diff) |
gol changes make sound
Diffstat (limited to 'src/world')
-rw-r--r-- | src/world/lattice.js | 16 | ||||
-rw-r--r-- | src/world/lattice.test.js | 61 | ||||
-rw-r--r-- | src/world/world.js | 5 |
3 files changed, 45 insertions, 37 deletions
diff --git a/src/world/lattice.js b/src/world/lattice.js index 243a47d..066a5ca 100644 --- a/src/world/lattice.js +++ b/src/world/lattice.js @@ -28,18 +28,19 @@ export function lattice_valid(lattice, proposal) { // apply a set of proposals, returning the new lattice export function lattice_apply(lattice, proposals) { - return proposals.reduce( + const result = proposals.reduce( (acc, prop) => { const change = (prop.world_updates || []).reduce( (acc_, update) => { const cell = acc_[update.y][update.x]; if (update.to) { cell.type = update.to; } if (update.flags) { - cell.flags = cell.flags || {} - // this is very side-effect-y but i couldn't think of a nicer compatible way of doing it 😔 - for (let k of Object.keys(update.flags)) { - cell.flags[k] = update.flags[k]; - } + cell.flags = { ...(cell.flags || {}), ...update.flags }; + //cell.flags = cell.flags || {} + //// this is very side-effect-y but i couldn't think of a nicer compatible way of doing it 😔 + //for (let k of Object.keys(update.flags)) { + // cell.flags[k] = update.flags[k]; + //} } return acc_ }, @@ -47,6 +48,7 @@ export function lattice_apply(lattice, proposals) { ); return change; }, - [...lattice].map(row => row.map(cell => ({ ...cell, flags: {}, }))) + [...lattice] ); + return result; } diff --git a/src/world/lattice.test.js b/src/world/lattice.test.js index c2fdb6b..d1bdd13 100644 --- a/src/world/lattice.test.js +++ b/src/world/lattice.test.js @@ -77,32 +77,35 @@ test("check proposals agains lattice for validity", () => { }); -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: {} }, - ]]); - -}); +// this test is no longer relevant because resetting the cell flags is taken care of by world_update, +// not lattice_apply +// +//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: {} }, +// ]]); +// +//}); diff --git a/src/world/world.js b/src/world/world.js index 4bab4d3..63d974a 100644 --- a/src/world/world.js +++ b/src/world/world.js @@ -16,7 +16,10 @@ import { proposal_merge } from './proposal.js'; 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 intermediate_lattice = lattice_apply( + world.lattice.map(row => row.map(cell => ({ ...cell, flags: {} }))), + lattice_props + ); const decisions = world.agents .map(a => agent_decide(world, a, world.senses, world.actions)) |