summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rules.js114
1 files changed, 59 insertions, 55 deletions
diff --git a/rules.js b/rules.js
index 7a04d8b..344feb5 100644
--- a/rules.js
+++ b/rules.js
@@ -36,6 +36,10 @@ const DIE_MISS = [ 0, '\u2460', '\u2461', '\u2462', '\u2463', '\u2464', '\u2465'
const ATTACK_MARK = "*"
const RESERVE_MARK = ""
+// List for fast iteration
+const BLOCKLIST = Object.keys(BLOCKS)
+const AREALIST = Object.keys(AREAS)
+
let states = {}
let game = null
@@ -342,14 +346,14 @@ function is_available_home_for(where, who) {
function count_available_homes(who) {
let count = 0
- for (let where in AREAS)
+ for (let where of AREALIST)
if (is_available_home_for(where, who))
++count
return count
}
function available_home(who) {
- for (let where in AREAS)
+ for (let where of AREALIST)
if (is_available_home_for(where, who))
return where
}
@@ -399,7 +403,7 @@ function is_london_friendly_to(owner) {
function count_lancaster_nobles_and_heirs() {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (block_owner(b) === LANCASTER &&
(block_type(b) === 'nobles' || block_type(b) === 'church' || block_type(b) === 'heir'))
if (is_on_map_not_in_exile_or_man(b))
@@ -411,7 +415,7 @@ function count_lancaster_nobles_and_heirs() {
function count_york_nobles_and_heirs() {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (block_owner(b) === YORK &&
(block_type(b) === 'nobles' || block_type(b) === 'church' || block_type(b) === 'heir'))
if (is_on_map_not_in_exile_or_man(b))
@@ -524,11 +528,11 @@ function can_defect(source, target) {
function can_attempt_treason_event() {
if (game.treason === game.attacker[game.where]) {
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (is_defender(b) && can_defect(null, b))
return true
} else {
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (is_attacker(b) && can_defect(null, b))
return true
}
@@ -545,7 +549,7 @@ function treachery_tag(who) {
function can_attempt_treachery(who) {
let once = treachery_tag(who)
if (game.battle_list.includes(who) && !game.treachery[once]) {
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (game.active === game.attacker[game.where]) {
if (is_defender(b) && can_defect(who, b))
return true
@@ -605,7 +609,7 @@ function reset_border_limits() {
function count_friendly(where) {
let p = game.active
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (game.location[b] === where && block_owner(b) === p && !game.dead[b])
++count
return count
@@ -614,7 +618,7 @@ function count_friendly(where) {
function count_enemy(where) {
let p = ENEMY[game.active]
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (game.location[b] === where && block_owner(b) === p && !game.dead[b])
++count
return count
@@ -623,7 +627,7 @@ function count_enemy(where) {
function count_enemy_excluding_reserves(where) {
let p = ENEMY[game.active]
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (game.location[b] === where && block_owner(b) === p)
if (!game.reserves.includes(b))
++count
@@ -719,14 +723,14 @@ function can_recruit(who) {
if (game.active === game.piracy) return false
if (can_activate(who) && game.location[who] === POOL)
- for (let to in AREAS)
+ for (let to of AREALIST)
if (can_recruit_to(who, to))
return true
return false
}
function have_contested_areas() {
- for (let where in AREAS)
+ for (let where of AREALIST)
if (is_area_on_map(where) && is_contested_area(where))
return true
return false
@@ -738,7 +742,7 @@ function count_pinning(where) {
function count_pinned(where) {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (game.location[b] === where && block_owner(b) === game.active)
if (!game.reserves.includes(b))
++count
@@ -927,7 +931,7 @@ function can_block_muster(who, muster) {
}
function can_muster_to(muster) {
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (can_block_muster(b, muster))
return true
return false
@@ -1030,7 +1034,7 @@ function reduce_block(who) {
function count_attackers() {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (is_attacker(b))
++count
return count
@@ -1038,7 +1042,7 @@ function count_attackers() {
function count_defenders() {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (is_defender(b))
++count
return count
@@ -1046,7 +1050,7 @@ function count_defenders() {
function count_blocks_exclude_mercenaries(where) {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (!(game.reduced && game.reduced[b]) && game.location[b] === where && !is_mercenary(b))
++count
return count
@@ -1054,27 +1058,27 @@ function count_blocks_exclude_mercenaries(where) {
function count_blocks(where) {
let count = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (!(game.reduced && game.reduced[b]) && game.location[b] === where)
++count
return count
}
function add_blocks_exclude_mercenaries(list, where) {
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (!(game.reduced && game.reduced[b]) && game.location[b] === where && !is_mercenary(b))
list.push(b)
}
function add_blocks(list, where) {
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (!(game.reduced && game.reduced[b]) && game.location[b] === where)
list.push(b)
}
function check_supply_penalty() {
game.supply = []
- for (let where in AREAS) {
+ for (let where of AREALIST) {
if (is_friendly_area(where)) {
if (where === "Calais" || where === "France") {
if (count_blocks_exclude_mercenaries(where) > 4)
@@ -1096,7 +1100,7 @@ function check_supply_penalty() {
function check_exile_limits() {
game.exiles = []
- for (let where in AREAS) {
+ for (let where of AREALIST) {
if (is_friendly_area(where)) {
if (where === "Calais" || where === "France") {
if (count_blocks_exclude_mercenaries(where) > 4)
@@ -1142,7 +1146,7 @@ function deploy_york(name, where) {
}
function reset_blocks() {
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
game.location[b] = null
game.steps[b] = block_max_steps(b)
}
@@ -1558,7 +1562,7 @@ states.plague_event = {
return view.prompt = "Plague: Waiting for " + game.active + " to choose a city."
view.prompt = "Plague: Choose an enemy city area."
gen_action(view, 'pass')
- for (let where in AREAS)
+ for (let where of AREALIST)
if (is_enemy_area(where) && has_city(where))
gen_action(view, 'area', where)
},
@@ -1566,7 +1570,7 @@ states.plague_event = {
log("Plague ravaged " + has_city(where) + "!")
game.where = where
game.plague = []
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (game.location[b] === where)
game.plague.push(b)
game.active = ENEMY[game.active]
@@ -1609,7 +1613,7 @@ states.muster_event = {
view.prompt = "Muster: Choose one friendly or vacant muster area."
gen_action_undo(view)
gen_action(view, 'end_action_phase')
- for (let where in AREAS) {
+ for (let where of AREALIST) {
if (is_friendly_or_vacant_area(where))
if (can_muster_to(where))
gen_action(view, 'area', where)
@@ -1635,7 +1639,7 @@ states.muster_who = {
view.prompt = "Muster: Move blocks to the designated muster area."
gen_action_undo(view)
gen_action(view, 'end_action_phase')
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (can_block_muster(b, game.where))
gen_action(view, 'block', b)
},
@@ -1769,7 +1773,7 @@ states.action_phase = {
gen_action_undo(view)
gen_action(view, 'end_action_phase')
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
let from = game.location[b]
if (can_recruit(b)) {
if (game.moves > 0)
@@ -1831,7 +1835,7 @@ states.recruit_where = {
view.prompt = "Recruit " + block_name(game.who) + " where?"
gen_action_undo(view)
gen_action(view, 'block', game.who)
- for (let to in AREAS)
+ for (let to of AREALIST)
if (can_recruit_to(game.who, to))
gen_action(view, 'area', to)
},
@@ -2004,7 +2008,7 @@ states.battle_phase = {
if (is_inactive_player(current))
return view.prompt = "Waiting for " + game.active + " to choose a battle."
view.prompt = "Choose the next battle to fight!"
- for (let where in AREAS)
+ for (let where of AREALIST)
if (is_area_on_map(where) && is_contested_area(where))
gen_action(view, 'area', where)
},
@@ -2059,7 +2063,7 @@ states.treason_event = {
return view.prompt = "Treason: Waiting for " + game.active + " to choose a target."
view.prompt = "Treason: Choose a target or pass."
gen_action(view, 'pass')
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (game.active === game.attacker[game.where]) {
if (is_defender(b) && can_defect(null, b)) {
gen_action(view, 'battle_treachery', b)
@@ -2092,7 +2096,7 @@ states.treason_event = {
}
function bring_on_reserves(owner, moved) {
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === owner && game.location[b] === game.where) {
remove_from_array(game.reserves, b)
game.moved[b] = moved
@@ -2145,7 +2149,7 @@ function pump_battle_round() {
function filter_battle_blocks(ci, is_candidate) {
let output = null
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (is_candidate(b) && !game.moved[b] && !game.dead[b]) {
if (block_initiative(b) === ci) {
if (!output)
@@ -2324,7 +2328,7 @@ function can_block_fire(who) {
function find_minor_heir(owner) {
let candidate = null
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === owner && block_type(b) === 'heir' && game.location[b] === MINOR)
if (!candidate || BLOCKS[b].heir < BLOCKS[candidate].heir)
candidate = b
@@ -2334,7 +2338,7 @@ function find_minor_heir(owner) {
function find_senior_heir(owner) {
let candidate = null
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (block_owner(b) === owner && block_type(b) === 'heir' && !is_dead(b))
if (!candidate || BLOCKS[b].heir < BLOCKS[candidate].heir)
candidate = b
@@ -2343,7 +2347,7 @@ function find_senior_heir(owner) {
function find_next_king(owner) {
let candidate = null
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (block_owner(b) === owner && block_type(b) === 'heir' && game.location[b])
if (!candidate || BLOCKS[b].heir < BLOCKS[candidate].heir)
candidate = b
@@ -2352,7 +2356,7 @@ function find_next_king(owner) {
function find_senior_heir_in_area(owner, where) {
let candidate = null
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === owner && block_type(b) === 'heir' && game.location[b] === where) {
if (is_battle_reserve(b))
continue
@@ -2456,7 +2460,7 @@ states.battle_charge = {
return view.prompt = "Heir Charge: Waiting for " + game.active + " to choose a target."
view.prompt = "Heir Charge: Choose a target."
gen_action(view, 'undo')
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (game.active === game.attacker[game.where]) {
if (is_defender(b)) {
gen_action(view, 'battle_charge', b)
@@ -2488,7 +2492,7 @@ states.battle_treachery = {
return view.prompt = "Treachery: Waiting for " + game.active + " to choose a target."
view.prompt = "Treachery: Choose a target."
gen_action(view, 'undo')
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (game.active === game.attacker[game.where]) {
if (is_defender(b) && can_defect(game.who, b)) {
gen_action(view, 'battle_treachery', b)
@@ -2550,11 +2554,11 @@ function apply_hit(who) {
function list_victims(p) {
let is_candidate = (p === game.attacker[game.where]) ? is_attacker : is_defender
let max = 0
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (is_candidate(b) && game.steps[b] > max)
max = game.steps[b]
let list = []
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (is_candidate(b) && game.steps[b] === max)
list.push(b)
return list
@@ -2671,7 +2675,7 @@ states.regroup = {
view.prompt = "Regroup: Choose an army to move."
gen_action_undo(view)
gen_action(view, 'end_regroup')
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (game.location[b] === game.where) {
if (game.active === game.piracy) {
if (game.is_pirate[b])
@@ -2854,7 +2858,7 @@ states.enter_pretender_heir = {
if (is_inactive_player(current))
return view.prompt = "Waiting for " + game.active + " to enter pretender heirs."
view.prompt = "Death of an Heir: Enter " + block_name(game.who) + " in an exile area."
- for (let where in AREAS)
+ for (let where of AREALIST)
if (is_pretender_exile_area(where))
gen_action(view, 'area', where)
},
@@ -2932,7 +2936,7 @@ states.enter_royal_heir = {
return view.prompt = "Waiting for " + game.active + " to enter royal heirs."
view.prompt = "Death of an Heir: Enter " + block_name(game.who) + " in a Crown area."
let can_enter = false
- for (let where in AREAS) {
+ for (let where of AREALIST) {
if (is_crown_area(where) && is_friendly_or_vacant_area(where)) {
gen_action(view, 'area', where)
can_enter = true
@@ -3012,7 +3016,7 @@ function goto_political_turn() {
game.turn_log = []
// Levies disband
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (!is_land_area(game.location[b]))
continue
switch (block_type(b)) {
@@ -3093,7 +3097,7 @@ function goto_pretender_goes_home() {
game.state = 'pretender_goes_home'
game.turn_log = []
let choices = false
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (block_owner(b) === game.active && is_block_on_map(b))
if (go_home_if_possible(b))
choices = true
@@ -3111,7 +3115,7 @@ states.pretender_goes_home = {
return view.prompt = "Waiting for the Pretender to go to exile."
gen_action_undo(view)
let done = true
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === game.active && is_block_on_map(b) && !game.moved[b]) {
if (!is_in_exile(b)) {
if (is_heir(b)) {
@@ -3128,7 +3132,7 @@ states.pretender_goes_home = {
}
if (done) {
view.prompt = "Pretender Goes Home: You may move nobles to another home."
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === game.active && is_block_on_map(b) && !game.moved[b]) {
if (!is_in_exile(b)) {
if (is_at_home(b)) {
@@ -3166,7 +3170,7 @@ states.pretender_goes_home_to = {
else
view.prompt = "Pretender Goes Home: Move " + block_name(game.who) + " to home."
gen_action(view, 'block', game.who)
- for (let where in AREAS) {
+ for (let where of AREALIST) {
if (where !== game.location[game.who]) {
if (is_heir(game.who)) {
if (is_friendly_exile_area(where))
@@ -3233,7 +3237,7 @@ function goto_king_goes_home() {
game.state = 'king_goes_home'
game.turn_log = []
let choices = false
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (block_owner(b) === game.active && is_block_on_map(b))
if (go_home_if_possible(b))
choices = true
@@ -3251,7 +3255,7 @@ states.king_goes_home = {
return view.prompt = "Waiting for the King to go home."
gen_action_undo(view)
let done = true
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === game.active && is_block_on_map(b) && !game.moved[b]) {
if (!is_in_exile(b)) {
if (!is_at_home(b)) {
@@ -3265,7 +3269,7 @@ states.king_goes_home = {
}
if (done) {
view.prompt = "King Goes Home: You may move nobles and heirs to another home."
- for (let b in BLOCKS) {
+ for (let b of BLOCKLIST) {
if (block_owner(b) === game.active && is_block_on_map(b) && !game.moved[b]) {
if (!is_in_exile(b)) {
if (is_at_home(b)) {
@@ -3300,7 +3304,7 @@ states.king_goes_home_to = {
return view.prompt = "Waiting for the King to go home."
view.prompt = "King Goes Home: Move " + block_name(game.who) + " to home."
gen_action(view, 'block', game.who)
- for (let where in AREAS)
+ for (let where of AREALIST)
if (where !== game.location[game.who])
if (is_available_home_for(where, game.who))
gen_action(view, 'area', where)
@@ -3354,7 +3358,7 @@ states.exile_limits_king = {
function end_political_turn() {
// Campaign reset
game.dead = {}
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
game.steps[b] = block_max_steps(b)
++game.campaign
@@ -3392,7 +3396,7 @@ function make_battle_view() {
battle.title += " \u2014 round " + game.battle_round + " of 4"
function fill_cell(cell, owner, fn) {
- for (let b in BLOCKS)
+ for (let b of BLOCKLIST)
if (game.location[b] === game.where & block_owner(b) === owner && !game.dead[b] && fn(b))
cell.push(b)
}