summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-10-06 19:49:30 +0200
committerTor Andersson <tor@ccxvii.net>2023-12-10 18:16:55 +0100
commit509116f2d6226473f206bc58d1b40f4cddfb4061 (patch)
tree8e2343448172298e3d7ea0244bd5c2762aab546d
parent8b95b0ecd590f40fd6bb14d1f6c23731cacb25a7 (diff)
downloadplantagenet-509116f2d6226473f206bc58d1b40f4cddfb4061.tar.gz
Use common routed_vassals set (instead of one set per lord).
-rw-r--r--play.js25
-rw-r--r--rules.js31
2 files changed, 31 insertions, 25 deletions
diff --git a/play.js b/play.js
index 4beb59b..c82b651 100644
--- a/play.js
+++ b/play.js
@@ -392,15 +392,10 @@ function get_vassal_locale(vassal) {
return pack8_get(view.pieces.vassals[vassal], 1)
}
-function get_vassals_with_lord(lord) {
- let results = []
- for (let x = first_vassal; x < last_vassal; x++) {
- if (pack8_get(view.pieces.vassals[x], 0) === lord) {
- results.push(x)
- }
- }
-
- return results
+function for_each_vassal_with_lord(lord, f) {
+ for (let x = first_vassal; x < last_vassal; x++)
+ if (pack8_get(view.pieces.vassals[x], 0) === lord)
+ f(x)
}
function is_york_locale(loc) {
@@ -1104,9 +1099,15 @@ function update_forces(parent, forces, lord_ix, routed) {
parent.replaceChildren()
for (let i = 0; i < force_type_count; ++i) {
if (i === VASSAL) {
- get_vassals_with_lord(lord_ix)
- .filter(v => (view.battle === 0 && routed === false) || (view.battle !== 0 && view.battle.routed_vassals[lord_ix].includes(v) === routed))
- .forEach(v => add_vassal(parent, v, lord_ix, routed))
+ for_each_vassal_with_lord(lord_ix, v => {
+ if (view.battle) {
+ if (routed === false)
+ add_vassal(parent, v, lord_ix, routed)
+ } else {
+ if (set_has(view.battle.routed_vassals, v) === routed)
+ add_vassal(parent, v, lord_ix, routed)
+ }
+ })
} else {
let n = pack4_get(forces, i)
for (let k = 0; k < n; ++k) {
diff --git a/rules.js b/rules.js
index e6a5e01..a7e40f6 100644
--- a/rules.js
+++ b/rules.js
@@ -744,23 +744,25 @@ function lord_has_unrouted_units(lord) {
return true
let result = false
for_each_vassal_with_lord(lord, v => {
- if (!set_has(game.battle.routed_vassals[lord], v))
+ if (!set_has(game.battle.routed_vassals, v))
result = true
})
return result
}
function lord_has_routed_units(lord) {
- return (
- game.pieces.routed[lord] !== 0 || game.battle.routed_vassals[lord].length > 0
- )
+ if (game.pieces.routed[lord] !== 0)
+ return true
+ let result = false
+ for_each_vassal_with_lord(lord, v => {
+ if (set_has(game.battle.routed_vassals, v))
+ result = true
+ })
+ return result
}
function rout_vassal(lord, vassal) {
- if (game.battle.routed_vassals[lord] === 0)
- game.battle.routed_vassals[lord] = []
-
- set_add(game.battle.routed_vassals[lord], vassal)
+ set_add(game.battle.routed_vassals, vassal)
}
function set_lord_locale(lord, locale) {
@@ -4556,7 +4558,7 @@ function init_battle(here) {
-1, -1, -1,
],
valour: Array(lord_count).fill(0),
- routed_vassals: Array(lord_count).fill([]),
+ routed_vassals: [],
engagements: [],
reserves: [],
retreated: 0,
@@ -5484,7 +5486,7 @@ function prompt_hit_forces() {
gen_action_militia(lord)
for_each_vassal_with_lord(lord, v => {
- if (!set_has(game.battle.routed_vassals[lord], v))
+ if (!set_has(game.battle.routed_vassals, v))
gen_action_vassal(v)
})
})
@@ -6020,9 +6022,12 @@ function goto_battle_aftermath() {
// Routed Vassals get disbanded
for (let lord = first_lord; lord <= last_lord; lord++) {
if (is_lord_on_map(lord)) {
- for (let vassal of game.battle.routed_vassals[lord]) {
- disband_vassal(vassal)
- }
+ for_each_vassal_with_lord(lord, v => {
+ if (set_has(game.battle.routed_vassals, v)) {
+ set_remove(game.battle.routed_vassals, v)
+ disband_vassal(vassal)
+ }
+ })
}
}