diff options
author | sanine <sanine.not@pm.me> | 2023-11-08 00:47:06 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-08 00:47:06 -0600 |
commit | f78493e192daf4639b91352909e4029b9970fcdb (patch) | |
tree | 5b43952cc5da05ffcc942d079f0c3b60674060be /src/world/cell.js | |
parent | 3ae8f476f3865982ca69d0e39b05b5aa8ec43694 (diff) |
implement basic sensing & refactor world/cells -> lattice
Diffstat (limited to 'src/world/cell.js')
-rw-r--r-- | src/world/cell.js | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/src/world/cell.js b/src/world/cell.js deleted file mode 100644 index 8795623..0000000 --- a/src/world/cell.js +++ /dev/null @@ -1,52 +0,0 @@ -'use strict'; - -// get the proposals for cell updates -export function cells_update(cells, update_rules) { - return cells - .map((row, y) => row.map((cell, x) => [x, y, cell.type])) - .flat() - .reduce((acc, [x, y, type]) => [...acc, update_rules[type](cells, x, y)], []) - .filter(x => x !== undefined) -} - - -// check if, given the current cells configuration, a proposal is valid -export function cells_valid(cells, proposal) { - if (!proposal.world_updates) { return true; } - return proposal.world_updates.reduce( - (acc, update) => { - const valid = - (update.x >= 0 && update.x < cells[0].length) && - (update.y >= 0 && update.y < cells.length) && - (cells[update.y][update.x].type == update.from) - return valid && acc; - }, - true - ); -} - - -// apply a set of proposals, returning the new cells -export function cells_apply(cells, proposals) { - return 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]; - } - } - return acc_ - }, - [...acc] - ); - return change; - }, - [...cells].map(row => row.map(cell => ({ ...cell, flags: {}, }))) - ); -} |