diff options
author | Tor Andersson <tor@ccxvii.net> | 2024-10-27 19:25:45 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2024-10-27 19:25:45 +0100 |
commit | 0f7c20a726225883da01cc0c6030a6641b4fbba2 (patch) | |
tree | 5675ccbe63112317cbd657ac7c9874f1eebe6968 /rules.js | |
parent | 2db92cb232c85fdc5d039d9e91cab73b549db585 (diff) | |
download | maria-0f7c20a726225883da01cc0c6030a6641b4fbba2.tar.gz |
automate clear_undo when control changes
Diffstat (limited to 'rules.js')
-rw-r--r-- | rules.js | 95 |
1 files changed, 40 insertions, 55 deletions
@@ -922,13 +922,13 @@ function player_from_power(pow) { } } -function set_active_to_power(power) { - game.power = power - game.active = current_player() -} - -function current_player() { - return player_from_power(game.power) +function set_active_to_power(new_power) { + let old_active = player_from_power(game.power) + let new_active = player_from_power(new_power) + if (old_active !== new_active) + clear_undo() + game.power = new_power + game.active = new_active } function get_top_piece(s) { @@ -1188,7 +1188,6 @@ function set_active_to_current_sequence_of_play() { } function next_sequence_of_play() { - clear_undo() ++game.stage let row = current_sequence_of_play() set_active_to_power(row.power) @@ -1445,6 +1444,8 @@ function give_subsidy(other) { } function goto_tactical_cards() { + clear_undo() // reveal random cards + if (game.power === P_SAXONY) { if (game.flags & F_SAXONY_TC_ONCE) { next_sequence_of_play() @@ -2751,16 +2752,12 @@ function goto_winter_turn() { function goto_winter_stage() { set_active_to_current_winter_stage() - clear_undo() - log(title_from_winter_stage[game.stage]) goto_recruit() } function end_winter_stage() { - clear_undo() - if (++game.stage === 6) goto_end_turn() else @@ -3049,7 +3046,6 @@ function goto_combat() { } function next_combat() { - clear_undo() set_active_to_current_sequence_of_play() game.count = 0 delete game.attacker @@ -3084,7 +3080,7 @@ states.combat_target = { gen_action_supreme_commander(game.combat[i+1]) }, piece(p) { - clear_undo() + clear_undo() // reveal hidden information game.defender = game.pos[p] @@ -3278,7 +3274,6 @@ states.combat_attack = { play_combat_card(c, +1, resume_combat_attack, "combat_attack_reserve") }, pass() { - clear_undo() end_resolve_combat() }, } @@ -3293,7 +3288,6 @@ states.combat_defend = { play_combat_card(c, -1, resume_combat_defend, "combat_defend_reserve") }, pass() { - clear_undo() end_resolve_combat() }, } @@ -3331,7 +3325,6 @@ states.combat_attack_swap = { view.actions.next = 1 }, next() { - clear_undo() set_active_defender() game.state = "combat_defend" }, @@ -3344,7 +3337,6 @@ states.combat_defend_swap = { view.actions.next = 1 }, next() { - clear_undo() set_active_attacker() game.state = "combat_attack" }, @@ -3734,7 +3726,7 @@ function check_instant_victory() { best = pow if (margin[best] >= 0) { - goto_game_over(player_from_power(best), power_name[best] + " won!") + goto_game_over(player_from_power(best), power_name[best] + " instant victory!") return true } @@ -3758,6 +3750,8 @@ const INFLUENCE_ORDER = [ ] function goto_politics() { + clear_undo() // reveal random cards + game.political = [] game.stage = 0 @@ -3848,7 +3842,6 @@ states.place_tc_on_display_done = { view.actions.next = 1 }, next() { - clear_undo() end_place_tc_on_display() }, } @@ -4078,7 +4071,6 @@ states.political_card_done = { } function end_execute_political_card() { - clear_undo() array_remove_item(game.political, game.pc) delete game.pc delete game.pcx @@ -4127,19 +4119,18 @@ states.political_troop_power = { view.actions.pass = 1 }, power(pow) { - clear_undo() + clear_undo() // reveal random cards let info = event_troops[current_political_effect()] set_active_to_power(pow) if (info.tcs > 0) { draw_tc(game.draw = [], info.tcs, game.power) game.state = "political_troops_draw" } else { - game.state = "political_troops_place" + goto_political_troops_place() } game.count = info.troops }, pass() { - push_undo() next_execute_political_card() }, } @@ -4159,10 +4150,29 @@ states.political_troops_draw = { set_add(game.hand1[game.power], c) delete game.draw } - game.state = "political_troops_place" + goto_political_troops_place() }, } +function can_add_troops() { + for (let p of all_power_generals[game.power]) { + // can re-enter generals + if (is_piece_eliminated(p) && has_re_entry_space_for_general(p)) + return true + // can recruit troops? (even to off-map boxes) + if (!is_piece_eliminated(p) && game.troops[p] < 8) + return true + } + return false +} + +function goto_political_troops_place() { + if (can_add_troops()) + game.state = "political_troops_place" + else + next_execute_political_card() +} + states.political_troops_place = { inactive: "execute political card", prompt() { @@ -4173,14 +4183,12 @@ states.political_troops_place = { if (game.count > 0) { for (let p of all_power_generals[game.power]) { - if (is_piece_on_map(p) && game.troops[p] < 8) + if (!is_piece_eliminated(p) && game.troops[p] < 8) gen_action_piece(p) if (is_piece_eliminated(p) && has_re_entry_space_for_general(p)) gen_action_piece(p) } } - - view.actions.pass = 1 }, piece(p) { push_undo() @@ -4189,14 +4197,10 @@ states.political_troops_place = { game.state = "re_enter_general_from_political_card" } else { game.troops[p] += 1 - if (--game.count === 0) + if (--game.count === 0 || !can_add_troops()) next_execute_political_card() } }, - pass() { - push_undo() - next_execute_political_card() - }, } function goto_mannheim_to_french_control() { @@ -4242,7 +4246,6 @@ states.saxony_shift = { view.actions.pass = 1 }, shift(_) { - clear_undo() log("Saxony marker shifted " + game.count + " right.") let save_saxony = game.saxony game.saxony = Math.max(1, Math.min(5, game.saxony + game.count)) @@ -4257,8 +4260,6 @@ states.saxony_shift = { next_combat() }, pass() { - clear_undo() - log("Saxony marker not shifted.") game.count = 0 next_combat() @@ -4497,7 +4498,6 @@ states.saxony_return_foreign_who = { game.state = "saxony_return_foreign_where" }, next() { - clear_undo() goto_saxony_return_foreign_pieces() }, } @@ -4740,7 +4740,6 @@ states.silesia_return_austrian_who = { game.state = "silesia_return_austrian_where" }, next() { - clear_undo() goto_annex_silesia_return_prussian_pieces() }, } @@ -4776,25 +4775,15 @@ states.silesia_return_prussian_who = { inactive: "return Austrian pieces from Prussia and Poland", prompt() { prompt("Return pieces to the nearest city in their home country.") - let done = true - for (let p of all_power_pieces[P_PRUSSIA]) { - if (is_piece_on_map(p) && !set_has(all_prussian_and_silesian_cities, game.pos[p])) { + for (let p of all_power_pieces[P_PRUSSIA]) + if (is_piece_on_map(p) && !set_has(all_prussian_and_silesian_cities, game.pos[p])) gen_action_piece(p) - done = false - } - } - if (done) - view.actions.next = 1 }, piece(p) { push_undo() game.selected = p game.state = "silesia_return_prussian_where" }, - next() { - clear_undo() - game.state = "silesia_enter_prussian_train" - }, } states.silesia_return_prussian_where = { @@ -4809,7 +4798,7 @@ states.silesia_return_prussian_where = { log(">P" + game.selected + " to S" + s) enter_piece_at(game.selected, s) game.selected = -1 - game.state = "silesia_return_prussian_who" + goto_annex_silesia_return_prussian_pieces() }, } @@ -4850,7 +4839,6 @@ states.silesia_done = { view.actions.next = 1 }, next() { - clear_undo() next_sequence_of_play() } } @@ -5066,7 +5054,6 @@ states.propose_subsidy_length = { view.actions.value = [ 1, 2, 3, 4, 5, 6 ] }, value(n) { - clear_undo() game.proposal.n = n set_active_to_power(game.proposal.from) game.state = "propose_subsidy_approve" @@ -5166,7 +5153,6 @@ states.cancel_subsidy_to = { power(to) { let player = game.power let from = game.proposal.from - clear_undo() game.proposal.to = to if (is_controlled_power(player, from)) set_active_to_power(to) @@ -5469,7 +5455,6 @@ states.setup = { game.state = "setup_general" }, end_setup() { - clear_undo() end_setup() }, } |