summaryrefslogtreecommitdiff
path: root/src/world/cell.js
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-06 01:55:26 -0600
committersanine <sanine.not@pm.me>2023-11-06 01:55:26 -0600
commit41d720dbd78d91ac85239c8352cd479ec898f9e0 (patch)
tree49e4d73ed89fb6ab69c635525a8e4b1ce88b0b81 /src/world/cell.js
parent541b103179cac2dc58962c52b5144b41f75316b0 (diff)
add cell.js
Diffstat (limited to 'src/world/cell.js')
-rw-r--r--src/world/cell.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/world/cell.js b/src/world/cell.js
new file mode 100644
index 0000000..5783d72
--- /dev/null
+++ b/src/world/cell.js
@@ -0,0 +1,41 @@
+'use strict';
+
+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)
+}
+
+
+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
+ );
+}
+
+
+export function cells_apply(cells, proposals) {
+ return proposals.reduce(
+ (acc, prop) => {
+ const change = (prop.world_updates || []).reduce(
+ (acc_, update) => {
+ acc_[update.y][update.x].type = update.to;
+ return acc_
+ },
+ [...acc]
+ );
+ return change;
+ },
+ [...cells]
+ );
+}