diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-07-06 19:46:47 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-07-07 19:05:52 +0200 |
commit | 5036dd980d0d82fc8b69b14ab545d4717c0ae0d7 (patch) | |
tree | 879cca458996ec7f679f8a8c90fecea5b7d147a3 | |
parent | b1f87e0acdaf88d7759f8293598d42edd303be65 (diff) | |
download | time-of-crisis-5036dd980d0d82fc8b69b14ab545d4717c0ae0d7.tar.gz |
Check if any possible actions before selecting a general/governor/militia.
-rw-r--r-- | rules.js | 232 |
1 files changed, 157 insertions, 75 deletions
@@ -4,11 +4,10 @@ TODO [ ] killed leader stash for buy/trash phase -[ ] display of general+castra stacked with militia+castra -[ ] todo: battle twice with militia (remove mbattled?) TODO: can_select_general - check actual possible actions TODO: can_select_governor - check actual possible actions +TODO: can_select_militia - check actual possible actions */ @@ -838,6 +837,16 @@ function can_militia_initiate_battle(where) { return false } +function can_general_initiate_battle(where) { + if (is_enemy_province(where) && has_militia(where)) + return true + if (some_general((id, loc) => loc === where && is_enemy_general(id))) + return true + if (some_barbarian((id, loc) => loc === where)) + return true + return false +} + function has_available_governor() { for (let i = 0; i < 6; ++i) if (get_governor_location(game.current * 6 + i) === AVAILABLE) @@ -1689,6 +1698,86 @@ states.ludi_saeculares_done = { // === TAKE ACTIONS === +function can_select_general(id) { + let where = get_general_location(id) + + if (where === UNAVAILABLE) + return game.mip >= id % 6 + if (where === AVAILABLE) + return game.mip >= 1 + + // Disperse Mob + if (game.mip >= 1 && get_mobs(where) && is_own_province(where)) + return true + + // Train Legions + if (game.mip >= 1 && has_reduced_legions_in_army(id)) + return true + + // Add Legion to Army + if (is_own_province(where)) + if (game.mip > count_legions_in_army(id)) + return 1 + + if (!has_general_battled(id)) { + // Move Army + if (game.mip >= (where === BRITANNIA ? 2 : 1)) + return true + // Initiate Battle + if (game.mip >= 1 && can_general_initiate_battle(where)) + return true + // Free Action: Enter/Leave capital + if (is_province(where)) + if (can_general_leave_capital(id) || can_enter_capital(where)) + return true + } + return false +} + +function can_select_governor(id) { + let where = get_governor_location(id) + + if (where === UNAVAILABLE) + return game.sip >= id % 6 + if (where === AVAILABLE) + return game.sip >= 1 + + // Recall + if (game.sip >= 2) + return true + + // Place Militia + if (game.pip >= 2 && !has_militia(where) && is_capital_free_of_enemy(where)) + return true + + // Hold Game + if (game.pip >= 2 && get_mobs()) + return true + + // Increase Support Level + let support = get_support(where) + if (where !== ITALIA && support < 4) + if (game.pip > support) + return true + + // Build Improvement + if (can_build_improvement(where)) + if (game.pip >= get_improvement_cost()) + return true + + return false +} + +function can_select_militia(where) { + if (game.mip >= 1) { + if (get_mobs(where)) + return true + if (can_militia_initiate_battle(where)) + return true + } + return false +} + function goto_take_actions() { log_br() game.state = "take_actions" @@ -1707,6 +1796,16 @@ function goto_take_actions() { } } +function resume_take_actions() { + game.state = "take_actions" + if (game.selected_governor && !can_select_governor(game.selected_governor)) + game.selected_governor = -1 + if (game.selected_general && !can_select_general(game.selected_general)) + game.selected_general = -1 + if (game.selected_militia && !can_select_militia(game.selected_militia)) + game.selected_militia = -1 +} + states.take_actions = { get inactive() { return `Take Actions \u2013 ${game.mip} military, ${game.sip} senate, ${game.pip} populace` @@ -1718,7 +1817,8 @@ states.take_actions = { let where = UNAVAILABLE - if (game.selected_governor < 0 && game.selected_general < 0 && hand.length > 0 && game.mip + game.sip + game.pip === 0) + // TODO - play all or not? + if (game.selected_governor < 0 && game.selected_general < 0 && game.selected_militia < 0 && hand.length > 0) view.actions.play_all = 1 view.actions.end_actions = 1 @@ -1752,52 +1852,23 @@ states.take_actions = { // Select Governor for (let i = 0; i < 6; ++i) { let id = game.current * 6 + i - if (id !== game.selected_governor) { - switch (get_governor_location(id)) { - case UNAVAILABLE: - if (game.sip >= i) - gen_action_governor(id) - break - case AVAILABLE: - if (game.sip >= 1) - gen_action_governor(id) - break - default: - // TODO: can_select_governor (check possible actions) - if (game.sip >= 2 || game.pip >= 2) - gen_action_governor(id) - break - } - } - - // Select Militia (if can Disperse Mobs or Initiate Battle) - let loc = get_governor_location(id) - if (is_province(loc) && has_lone_militia(loc) && loc !== game.selected_militia) { - if (game.mip >= 1 && (get_mobs(loc) || can_militia_initiate_battle(loc))) - gen_action_militia(loc) + if (id !== game.selected_governor) + if (can_select_governor(id)) + gen_action_governor(id) + + let mid = get_governor_location(id) + if (is_province(mid) && mid !== game.selected_militia && has_lone_militia(mid)) { + if (can_select_militia(mid)) + gen_action_militia(mid) } } // Select General for (let i = 0; i < 6; ++i) { let id = game.current * 6 + i - if (id !== game.selected_general) { - switch (get_general_location(id)) { - case UNAVAILABLE: - if (game.mip >= i) - gen_action_general(id) - break - case AVAILABLE: - if (game.mip >= 1) - gen_action_general(id) - break - default: - // TODO: can_select_general (check possible actions) - if (game.mip >= 1 || can_general_leave_capital(id) || can_enter_capital(get_general_location(id))) - gen_action_general(id) - break - } - } + if (id !== game.selected_general) + if (can_select_general(id)) + gen_action_general(id) } // Recruit Governor @@ -2004,16 +2075,19 @@ states.take_actions = { log("Recruit General " + (game.selected_general % 6) + ".") spend_military(game.selected_general % 6) set_general_location(game.selected_general, AVAILABLE) + resume_take_actions() }, recall_governor() { push_undo() recall_governor() + resume_take_actions() }, support() { push_undo() increase_support_level() + resume_take_actions() }, place_militia() { @@ -2021,6 +2095,7 @@ states.take_actions = { let where = get_governor_location(game.selected_governor) spend_populace(2) add_militia(where) + resume_take_actions() }, hold_games() { @@ -2029,6 +2104,7 @@ states.take_actions = { log("Hold Games in %" + where + ".") spend_populace(2) set_mobs(where, get_mobs(where) - 1) + resume_take_actions() }, build_improvement() { @@ -2045,6 +2121,7 @@ states.take_actions = { let cost = count_legions_in_army(game.selected_general) + 1 spend_military(cost) set_legion_location(find_unused_legion(), ARMY + game.selected_general) + resume_take_actions() }, train_legions() { @@ -2053,17 +2130,20 @@ states.take_actions = { log("Train Legions in %" + where + ".") spend_military(1) set_legion_full_strength(find_reduced_legion_in_army(game.selected_general)) + resume_take_actions() }, enter() { push_undo() enter_capital() + resume_take_actions() }, leave() { push_undo() set_general_outside_capital(game.selected_general) remove_general_castra(game.selected_general) + resume_take_actions() }, disperse_mob() { @@ -2079,6 +2159,7 @@ states.take_actions = { log("Disperse " + n + " Mobs in %" + where + ".") set_mobs(where, get_mobs(where) - n) reduce_support(where) + resume_take_actions() }, region(where) { @@ -2103,6 +2184,7 @@ states.take_actions = { if (get_general_location(game.selected_general) !== where) move_army_to(game.selected_general, where) enter_capital() + resume_take_actions() }, } @@ -2129,17 +2211,17 @@ states.build_improvement = { amphitheater() { add_amphitheater(game.where) log("Build Amphitheater in %" + game.where + ".") - game.state = "take_actions" + resume_take_actions() }, basilica() { add_basilica(game.where) log("Build Basilica in %" + game.where + ".") - game.state = "take_actions" + resume_take_actions() }, limes() { add_limes(game.where) log("Build Limes in %" + game.where + ".") - game.state = "take_actions" + resume_take_actions() }, } @@ -2418,12 +2500,12 @@ function roll_to_place_governor(pg) { } } else { place_governor(game.where, game.selected_governor) - game.state = "take_actions" + resume_take_actions() } } else { log("Failed!") log_br() - game.state = "take_actions" + resume_take_actions() } } @@ -2512,7 +2594,7 @@ states.damnatio_memoriae = { }, pass() { push_undo() - game.state = "take_actions" + resume_take_actions() }, } @@ -2524,7 +2606,7 @@ function play_damnatio_memoriae() { function play_damnatio_memoriae_exp() { award_legacy(game.count >> 3, "Damnatio Memoriae", -get_support(ITALIA)) - game.state = "take_actions" + resume_take_actions() } states.damnatio_memoriae_mobs = { @@ -2541,7 +2623,7 @@ states.damnatio_memoriae_mobs = { logi("Mob in %" + where + ".") set_mobs(where, get_mobs(where) + 1) if (--game.count === 0) - game.state = "take_actions" + resume_take_actions() }, } @@ -2579,7 +2661,7 @@ function goto_becoming_emperor() { if (game.emperor === NEUTRAL_EMPEROR) game.state = "becoming_emperor" else - game.state = "take_actions" + resume_take_actions() } function has_mobs_in_own_provinces() { @@ -2609,14 +2691,14 @@ states.becoming_emperor = { if (where === ITALIA) { log("Senate Emperor in Italia.") log_br() - game.state = "take_actions" + resume_take_actions() } else { log("Populace Emperor in %" + where + ".") log_br() if (has_mobs_in_own_provinces()) game.state = "becoming_populace_emperor" else - game.state = "take_actions" + resume_take_actions() } }, general(id) { @@ -2625,7 +2707,7 @@ states.becoming_emperor = { let where = get_general_location(id) log("Military Emperor in %" + where + ".") log_br() - game.state = "take_actions" + resume_take_actions() }, } @@ -2643,7 +2725,7 @@ states.becoming_populace_emperor = { log("Remove mob from %" + where + ".") set_mobs(where, 0) if (!has_mobs_in_own_provinces()) - game.state = "take_actions" + resume_take_actions() }, } @@ -2714,7 +2796,7 @@ function move_army_to(who, to) { if (is_sea(to)) game.state = "move_army_at_sea" else - game.state = "take_actions" + resume_take_actions() } // FREE ACTION: MOVE INTO PROVINCIAL CAPITAL @@ -2804,7 +2886,7 @@ function goto_replace_pretender() { if (has_available_governor()) game.state = "replace_pretender_governor" else - game.state = "take_actions" + resume_take_actions() } states.replace_pretender_governor = { @@ -2824,11 +2906,11 @@ states.replace_pretender_governor = { push_undo() log("Replace Governor in %" + game.where + ".") set_governor_location(id, game.where) - game.state = "take_actions" + resume_take_actions() }, pass() { push_undo() - game.state = "take_actions" + resume_take_actions() }, } @@ -2867,13 +2949,13 @@ states.castra = { militia(where) { log("Castra on militia in %" + where + ".") add_militia_castra(where) - game.state = "take_actions" + resume_take_actions() }, general(id) { let where = get_general_location(id) log("Castra on army in %" + where + ".") add_general_castra(id) - game.state = "take_actions" + resume_take_actions() }, } @@ -2902,7 +2984,7 @@ states.quaestor = { region(where) { log("Quaestor in %" + where + ".") add_quaestor(where) - game.state = "take_actions" + resume_take_actions() } } @@ -2939,7 +3021,7 @@ states.tribute = { for (let id = first_barbarian[tribe]; id <= last_barbarian[tribe]; ++id) if (get_barbarian_location(id) === where && is_barbarian_active(id)) set_barbarian_inactive(id) - game.state = "take_actions" + resume_take_actions() }, } @@ -3043,7 +3125,7 @@ states.foederati_general = { log("Foederati " + BARBARIAN_NAME[tribe] + " in %" + from + ".") eliminate_barbarian(id) } - game.state = "take_actions" + resume_take_actions() }, } @@ -3061,7 +3143,7 @@ states.foederati_militia = { let from = get_barbarian_location(id) log("Foederati " + BARBARIAN_NAME[tribe] + " in %" + from + ".") eliminate_barbarian(id) - game.state = "take_actions" + resume_take_actions() }, } @@ -3090,7 +3172,7 @@ states.mob = { region(where) { log("Mob in %" + where + ".") set_mobs(where, get_mobs(where) + 1) - game.state = "take_actions" + resume_take_actions() }, } @@ -3150,7 +3232,7 @@ function goto_pretender_breakaway() { return } } - game.state = "take_actions" + resume_take_actions() } states.pretender_breakaway = { @@ -3328,14 +3410,14 @@ states.force_march = { enter() { push_undo() enter_capital() - game.state = "take_actions" + resume_take_actions() }, leave() { push_undo() set_general_outside_capital(game.selected_general) remove_general_castra(game.selected_general) - game.state = "take_actions" + resume_take_actions() }, } @@ -3376,7 +3458,7 @@ states.frumentarii = { if (draw.length === 0) flip_discard_to_available() if (--game.count === 0) - game.state = "take_actions" + resume_take_actions() }, } @@ -3433,11 +3515,11 @@ states.mobile_vulgus = { spend_populace(n) reduce_support(game.where) if (is_neutral_province(game.where)) - game.state = "take_actions" + resume_take_actions() }, done() { push_undo() - game.state = "take_actions" + resume_take_actions() }, } @@ -3553,7 +3635,7 @@ states.demagogue_mobs = { function end_demagogue() { log_br() game.demagogue = undefined - game.state = "take_actions" + resume_take_actions() } // === COMBAT === @@ -4437,7 +4519,7 @@ states.triumph = { function end_combat() { log_br() game.combat = null - game.state = "take_actions" + resume_take_actions() } // === SUPPORT CHECK === |