diff options
-rw-r--r-- | play.js | 95 | ||||
-rw-r--r-- | rules.js | 99 |
2 files changed, 120 insertions, 74 deletions
@@ -2,8 +2,6 @@ // === SYNC with rules.js === -const TRIBE_COUNT = [ 0, 5, 3, 4, 5 ] - const ITALIA = 0 const ASIA = 1 const GALLIA = 2 @@ -35,7 +33,10 @@ const UNAVAILABLE = 22 const ARMY = 23 +const TRIBE_COUNT = [ 0, 5, 3, 4, 5 ] +const BARBARIAN_COUNT = [ 0, 50, 30, 40, 50 ] const first_barbarian = [ 0, 10, 20, 30, 40 ] +const last_barbarian = [ 9, 19, 29, 39, 49 ] const first_governor = [ 0, 6, 12, 18 ] const first_general = [ 0, 6, 12, 18 ] @@ -105,6 +106,7 @@ function set_has(set, item) { } const PLAYER_CLASS = [ "red", "blue", "yellow", "green" ] +const BARBARIAN_CLASS = [ "alamanni", "franks", "goths", "nomads", "sassanids" ] const BOXES = { "Thracia Support": [1502,720,258,52], @@ -315,7 +317,7 @@ function register_action(target, action, id) { function on_click_action(evt, target) { if (evt.button === 0) { - if (target.my_stack) { + if (typeof target.my_stack === "number") { evt.stopPropagation() if (focus_stack(target.my_stack)) if (!send_action(target.my_action, target.my_id)) @@ -378,13 +380,9 @@ function on_init() { ui.rival_emperors[1] = create_piece(1, "rival_emperor", "rival_emperor", "priest_king") ui.rival_emperors[2] = create_piece(2, "rival_emperor", "rival_emperor", "zenobia") - for (let i = 0; i < 10; ++i) { - ui.barbarians[0][i] = create_piece(0 + i, "barbarian", "alamanni") - ui.barbarians[1][i] = create_piece(10 + i, "barbarian", "franks") - ui.barbarians[2][i] = create_piece(20 + i, "barbarian", "goths") - ui.barbarians[3][i] = create_piece(30 + i, "barbarian", "nomads") - ui.barbarians[4][i] = create_piece(40 + i, "barbarian", "sassanids") - } + for (let tribe = 0; tribe < 5; ++tribe) + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) + ui.barbarians[id] = create_piece(id, "barbarian", BARBARIAN_CLASS[tribe]) for (let p = 0; p < 4; ++p) { for (let g = 0; g < 6; ++g) { @@ -421,8 +419,8 @@ function on_init() { } let stack_count = new Array(12 + 5).fill(0) -let stack_focus = 0 -let stack_cache = {} +let stack_focus = -1 +let stack_cache = [] function focus_stack(id) { if (stack_focus !== id) { @@ -435,8 +433,8 @@ function focus_stack(id) { } function blur_stack() { - if (stack_focus !== 0) { - stack_focus = 0 + if (stack_focus >= 0) { + stack_focus = -1 on_update() } } @@ -447,7 +445,8 @@ function layout_stack(id, list, region, in_capital) { let dy = 8 let z = 1 - stack_cache[id] = list + if (id >= 0) + stack_cache[id] = list if (in_capital) { x += 5 @@ -462,7 +461,7 @@ function layout_stack(id, list, region, in_capital) { dy = 5 } - if (stack_focus === id) { + if (id >= 0 && stack_focus === id) { dx = 24 dy = 16 } @@ -507,7 +506,8 @@ function layout_governor_unavailable(e, color, ix) { function on_update() { let player_count = view.legacy.length - stack_cache = {} + for (let i = 0; i < 24; ++i) + stack_cache[i] = null ui.body.classList.toggle("p2", player_count === 2) ui.body.classList.toggle("p3", player_count === 3) @@ -615,44 +615,38 @@ function on_update() { // TODO: Cniva, Ardashir, and Shapur // TODO: Zenobia, Postumus, Priest King - let tribe_count = TRIBE_COUNT[player_count] - for (let tribe = 0; tribe < TRIBE_COUNT[player_count]; ++tribe) { - for (let i = 0; i < 10; ++i) { - show(ui.barbarians[tribe][i]) - if (is_barbarian_inactive(first_barbarian[tribe] + i)) - ui.barbarians[tribe][i].classList.toggle("inactive", true) - else - ui.barbarians[tribe][i].classList.toggle("inactive", false) - } + for (let id = 0; id < BARBARIAN_COUNT[player_count]; ++id) { + show(ui.barbarians[id]) + if (is_barbarian_inactive(id)) + ui.barbarians[id].classList.toggle("inactive", true) + else + ui.barbarians[id].classList.toggle("inactive", false) } - for (let tribe = TRIBE_COUNT[player_count]; tribe < 5; ++tribe) { - for (let i = 0; i < 10; ++i) { - hide(ui.barbarians[tribe][i]) - } - } + for (let id = BARBARIAN_COUNT[player_count]; id < ui.barbarians.length; ++id) + hide(ui.barbarians[id]) stack_count.fill(1) for (let region = 0; region < 12 + 5; ++region) { - for (let tribe = 0; tribe < tribe_count; ++tribe) { + for (let tribe = 0; tribe < TRIBE_COUNT[player_count]; ++tribe) { let active_barbarians = [] let inactive_barbarians = [] // TODO: Cniva, Ardashir, and Shapur - for (let i = 0; i < 10; ++i) { - let loc = get_barbarian_location(first_barbarian[tribe], i) - let inactive = is_barbarian_inactive(first_barbarian[tribe], i) + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) { + let loc = get_barbarian_location(id) + let inactive = is_barbarian_inactive(id) if (loc === region) { if (inactive) - inactive_barbarians.push(ui.barbarians[tribe][i]) + inactive_barbarians.push(ui.barbarians[id]) else - active_barbarians.push(ui.barbarians[tribe][i]) + active_barbarians.push(ui.barbarians[id]) } } if (active_barbarians.length > 0) - layout_stack(region * 10 + tribe * 2 + 0, active_barbarians, region, false) + layout_stack(-1, active_barbarians, region, false) if (inactive_barbarians.length > 0) - layout_stack(region * 10 + tribe * 2 + 1, inactive_barbarians, region, false) + layout_stack(-1, inactive_barbarians, region, false) } } @@ -675,10 +669,10 @@ function on_update() { let mcastra = has_militia_castra(region) if (mcastra) { show(ui.mcastra[region]) - layout_stack(0, [ ui.militia[region], ui.mcastra[region] ], region, true) + layout_stack(-1, [ ui.militia[region], ui.mcastra[region] ], region, true) } else { hide(ui.mcastra[region]) - layout_stack(0, [ ui.militia[region] ], region, true) + layout_stack(-1, [ ui.militia[region] ], region, true) } } } @@ -698,7 +692,7 @@ function on_update() { for (let pi = 0; pi < player_count; ++pi) { let avail_stack = [] for (let ai = 0; ai < 6; ++ai) { - let id = ARMY + first_general[pi] + ai + let army = ARMY + first_general[pi] + ai let region = get_general_location(first_general[pi] + ai) let inside = is_general_inside_capital(first_general[pi] + ai) let castra = has_general_castra(first_general[pi] + ai) @@ -710,17 +704,17 @@ function on_update() { if (has_militia(region) && inside) stack.push(ui.militia[region]) - for (let tribe = 0; tribe < tribe_count; ++tribe) { - for (let i = 0; i < 10; ++i) { - let loc = get_barbarian_location(first_barbarian[tribe] + i) - if (loc === id) - stack.push(ui.barbarians[tribe][i]) + for (let tribe = 0; tribe < TRIBE_COUNT[player_count]; ++tribe) { + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) { + let loc = get_barbarian_location(id) + if (loc === army) + stack.push(ui.barbarians[id]) } } for (let i = 0; i < 33; ++i) { let loc = get_legion_location(i) - if (loc === id) + if (loc === army) stack.push(ui.legions[i]) } @@ -734,9 +728,9 @@ function on_update() { } if (inside) - layout_stack(id, stack, region, true) + layout_stack(pi * 6 + ai, stack, region, true) else - layout_stack(id, stack, region, false) + layout_stack(pi * 6 + ai, stack, region, false) } else { avail_stack.push(e) } @@ -749,7 +743,6 @@ function on_update() { for (let pi = 0; pi < player_count; ++pi) { let avail_stack = [] for (let ai = 0; ai < 6; ++ai) { - let id = 100 + 100 * pi + ai let region = get_governor_location(first_governor[pi] + ai) let e = ui.governors[pi][ai] if (region < 12) { @@ -127,7 +127,9 @@ const GOTHS = 2 const NOMADS = 3 const SASSANIDS = 4 -const TRIBE_COUNT = [ 0, 5, 3, 4, 5 ] +const BARBARIAN_COUNT = [ 0, 50, 30, 40, 50 ] +const first_barbarian = [ 0, 10, 20, 30, 40 ] +const last_barbarian = [ 9, 19, 29, 39, 49 ] const BARBARIAN_NAME = [ "Alamanni", @@ -645,29 +647,26 @@ function can_place_governor(where) { function find_active_barbarian_at_home(tribe) { let home = BARBARIAN_HOMELAND[tribe] - for (let i = 0; i < 10; ++i) - if (get_barbarian_location(tribe * 10 + i) === home) - if (is_barbarian_active(tribe * 10 + i)) - return tribe * 10 + i + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) + if (get_barbarian_location(id) === home && is_barbarian_active(id)) + return id return -1 } function count_active_barbarians_at_home(tribe) { let home = BARBARIAN_HOMELAND[tribe] let n = 0 - for (let i = 0; i < 10; ++i) - if (get_barbarian_location(tribe * 10 + i) === home) - if (is_barbarian_active(tribe * 10 + i)) - n += 1 + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) + if (get_barbarian_location(id) === home && is_barbarian_active(id)) + n += 1 return n } function find_inactive_barbarian_at_home(tribe) { let home = BARBARIAN_HOMELAND[tribe] - for (let i = 0; i < 10; ++i) - if (get_barbarian_location(tribe * 10 + i) === home) - if (is_barbarian_inactive(tribe * 10 + i)) - return tribe * 10 + i + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) + if (get_barbarian_location(id) === home && is_barbarian_inactive(id)) + return id return -1 } @@ -679,8 +678,8 @@ function activate_one_barbarian(tribe) { function count_barbarians_in_province(tribe, where) { let n = 0 - for (let i = 0; i < 10; ++i) - if (get_barbarian_location(tribe * 10 + i) === where) + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) + if (get_barbarian_location(id) === where) n += 1 return n } @@ -901,9 +900,9 @@ function goto_barbarian_crisis(tribe) { function invade_with_active_barbarian(tribe, where) { // TODO: move leaders first - let b = find_active_barbarian_at_home(tribe) - if (b >= 0) - set_barbarian_location(tribe * 10 + b, where) + let id = find_active_barbarian_at_home(tribe) + if (id >= 0) + set_barbarian_location(id, where) } function goto_barbarian_invasion(tribe, black, white) { @@ -1532,13 +1531,68 @@ states.initiate_battle = { } }, general(id) { + push_undo() log("Initiate Battle vs Gen" + id) + game.misc.target = id + game.state = "battle_general" }, barbarian(id) { + push_undo() log("Initiate Battle vs B" + id) + game.misc.target = id + game.state = "battle_barbarians" + }, + rival_emperor(id) { + push_undo() + log("Initiate Battle vs B" + id) + game.misc.target = id + game.state = "battle_rival_emperor" }, militia(where) { + push_undo() log("Initiate Battle vs Militia" + id) + game.misc.target = -1 + game.state = "battle_militia" + }, +} + +states.battle_general = { + prompt() { + prompt("Battle vs General.") + view.actions.roll = 1 + }, + roll() { + clear_undo() + }, +} + +states.battle_barbarians = { + prompt() { + prompt("Battle vs Barbarians.") + view.actions.roll = 1 + }, + roll() { + clear_undo() + }, +} + +states.battle_rival_emperor = { + prompt() { + prompt("Battle vs Rival Emperor.") + view.actions.roll = 1 + }, + roll() { + clear_undo() + }, +} + +states.battle_militia = { + prompt() { + prompt("Battle vs Militia.") + view.actions.roll = 1 + }, + roll() { + clear_undo() }, } @@ -1730,9 +1784,9 @@ function setup_market_pile(cards) { } function setup_barbarians(tribe, home) { - for (let i = 0; i < 10; ++i) { - set_barbarian_location(tribe * 10 + i, home) - set_barbarian_inactive(tribe * 10 + i) + for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) { + set_barbarian_location(id, home) + set_barbarian_inactive(id) } } @@ -1741,7 +1795,6 @@ exports.setup = function (seed, scenario, options) { let player_count = real_player_count if (player_count === 1) player_count = 4 - let tribe_count = TRIBE_COUNT[player_count] game = { seed: seed, @@ -1779,7 +1832,7 @@ exports.setup = function (seed, scenario, options) { governors: new Array(6 * player_count).fill(UNAVAILABLE), generals: new Array(6 * player_count).fill(UNAVAILABLE), legions: new Array(LEGION_COUNT).fill(AVAILABLE), - barbarians: new Array(10 * tribe_count).fill(0), + barbarians: new Array(BARBARIAN_COUNT[player_count]).fill(AVAILABLE), rival_emperors: [ UNAVAILABLE, UNAVAILABLE, UNAVAILABLE ], barbarian_leaders: [ UNAVAILABLE, UNAVAILABLE, UNAVAILABLE ], |