From 26721bd0947120cc4715235c9593de2147aaa23c Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 11 Nov 2024 15:51:55 +0100 Subject: Add and use map_filter helper (and fix glitch). --- rules.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'rules.js') diff --git a/rules.js b/rules.js index 30bee2f..c0a9af3 100644 --- a/rules.js +++ b/rules.js @@ -3768,13 +3768,7 @@ states.combat_target = { game.defender = game.pos[p] - // TODO: map_filter - for (let i = 0; i < game.combat.length; i += 2) { - if (game.combat[i] === game.attacker && game.combat[i+1] === game.defender) { - array_remove_pair(game.combat, i) - break - } - } + game.combat = map_filter(game.combat, (atk, def) => atk !== game.attacker || def !== game.defender) goto_resolve_combat() }, @@ -4356,10 +4350,7 @@ function goto_retroactive_conquest() { } }) - // TODO: map_filter - for (let i = 0; i < game.retro.length; i += 2) - if (game.retro[i+1] === game.power) - array_remove_pair(game.retro, i) + game.retro = map_filter(game.retro, (s,pow) => pow !== game.power) log_conquest(conq) @@ -7238,6 +7229,14 @@ function map_for_each(map, f) { f(map[i], map[i+1]) } +function map_filter(map, f) { + let out = [] + for (let i = 0; i < map.length; i += 2) + if (f(map[i], map[i+1])) + out.push(map[i], map[i+1]) + return out +} + function map_group_by(items, callback) { let groups = [] if (typeof callback === "function") { -- cgit v1.2.3