From 62e2b0858c6b8f6e975161053d1653c59e08905d Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 17 May 2024 13:33:00 +0200 Subject: pc+cu in one array --- data.js | 6 +-- play.css | 6 --- play.js | 59 ++++++++++++++++++++--------- rules.js | 129 +++++++++++++++++++++++++++------------------------------------ 4 files changed, 100 insertions(+), 100 deletions(-) diff --git a/data.js b/data.js index 011d0fd..035323b 100644 --- a/data.js +++ b/data.js @@ -185,11 +185,11 @@ space(VA, "Point Pleasant", 132, 1218, "regular-space") space(VA, "Richmond", 608, 1323, "winter-quarters") space(VA, "Yorktown", 767, 1317, "regular-space") -not_space("box", "Continental Congress Dispersed", 545, 350) -not_space("box", "Captured Generals", 1463, 80) not_space("box", "British Leader Reinforcements", 1425, 1745) -not_space("box", "American Leader Reinforcements", 400, 310) not_space("box", "French Reinforcements", 150, 340) +not_space("box", "American Leader Reinforcements", 400, 310) +not_space("box", "Captured Generals", 1463, 80) +not_space("box", "Continental Congress Dispersed", 545, 350) box("SEA", "Sea1", 1277, 65, "blockade") box("SEA", "Sea2", 1469, 514, "blockade") diff --git a/play.css b/play.css index 64c66b0..4f883b9 100644 --- a/play.css +++ b/play.css @@ -110,11 +110,6 @@ aside { visibility: hidden; } -.marker { - transition-property: top, left; - transition-duration: 700ms; -} - .marker { position: absolute; background-size: cover; @@ -273,7 +268,6 @@ aside { height: 262px; border-radius: 10px; box-shadow: var(--piece-shadow); - transition: 100ms; display: none; } diff --git a/play.js b/play.js index dc5863e..04ce045 100644 --- a/play.js +++ b/play.js @@ -4,6 +4,13 @@ const PC_NONE = 0 const PC_BRITISH = 1 const PC_AMERICAN = 2 +const CU_BRITISH_SHIFT = 2 +const CU_AMERICAN_SHIFT = 8 +const CU_FRENCH_SHIFT = 14 +const CU_BRITISH_MASK = 63 << CU_BRITISH_SHIFT +const CU_AMERICAN_MASK = 63 << CU_AMERICAN_SHIFT +const CU_FRENCH_MASK = 7 << CU_FRENCH_SHIFT + const GENERALS = data.generals const CARDS = data.cards const SPACES = data.spaces @@ -235,7 +242,19 @@ function build_map() { } function get_space_pc(space) { - return view.pc[space] & 3 + return view.spc[space] & 3 +} + +function count_british_cu(s) { + return (view.spc[s] & CU_BRITISH_MASK) >>> CU_BRITISH_SHIFT +} + +function count_american_cu(s) { + return (view.spc[s] & CU_AMERICAN_MASK) >>> CU_AMERICAN_SHIFT +} + +function count_french_cu(s) { + return (view.spc[s] & CU_FRENCH_MASK) >>> CU_FRENCH_SHIFT } function update_units() { @@ -307,16 +326,16 @@ function update_units() { ui.control[c].className = "marker control" } - let offset = {} + let offset = [] for (let g in GENERALS) { let e = ui.generals[g] - let loc = view.generals[g] + let loc = view.loc[g] let space = SPACES[loc] || BOXES[loc] if (space) { - let o = offset[space.name] | 0 + let o = offset[loc] | 0 update_marker_xy(e, space.x + o * generalX, space.y + o * generalY - 32) e.classList.remove("offmap") - offset[space.name] = ++o + offset[loc] = ++o } else { e.classList.add("offmap") } @@ -327,17 +346,23 @@ function update_units() { } // TODO: reuse CU elements - offset = {} + offset = [] + function add_cu_marker(s, n, cn) { + if (n > 0) { + let space = SPACES[s] + let o = offset[s] | 0 + let x = space.x + o * cuX + let y = space.y + o * cuY + let e = build_marker("cu", null, x, y, 122 / 2, 122 / 2, cn) + e.textContent = n + offset[s] = ++o + } + } clear_group("cu") - for (let i = 0; i < view.cu.length; ++i) { - let cu = view.cu[i] - let space = SPACES[cu.location] || BOXES[cu.location] - let o = offset[space.name] | 0 - let x = space.x + o * cuX - let y = space.y + o * cuY - let e = build_marker("cu", "cu" + i, x, y, 122 / 2, 122 / 2, [ "cu", cu.owner.toLowerCase() ]) - e.textContent = cu.count - offset[space.name] = ++o + for (let s = 0; s < view.spc.length; ++s) { + add_cu_marker(s, count_british_cu(s), [ "cu", "british" ]) + add_cu_marker(s, count_american_cu(s), [ "cu", "american" ]) + add_cu_marker(s, count_french_cu(s), [ "cu", "french" ]) } } @@ -427,9 +452,6 @@ function on_update() { update_units() - if (player != view.active) - return - if (view.actions) for (let action of Object.keys(view.actions)) { let args = view.actions[action] @@ -457,6 +479,7 @@ function on_update() { case "place_british_pc": case "remove_pc": case "flip_pc": + console.log("ACTION", args) for (let space of args) ui.spaces[space].classList.add("enabled") break diff --git a/rules.js b/rules.js index 758de2e..d32f789 100644 --- a/rules.js +++ b/rules.js @@ -37,17 +37,6 @@ const PC_NONE = 0 const PC_BRITISH = 1 const PC_AMERICAN = 2 -// max 5 french CU in game total -// no stacking limit on other units, but max 6 new american CU per turn * 9 turns (=54) + starting forces (2+2+5) -// british cu = 3+8+1+8+1+5+1+1+1 (=29) reinforcements + starting forces (1+2+5) -// 6 bits for british, 6 bits for american, 3 bits for french = 15 bits total -// x2 for moved = 30 bits -// moved_cu=5+5+3 bits -// cu=5+5+3 bits -// pc=2 bits - -// OR: pack space=8, owner=2, count=6, moved=6 - const CU_BRITISH_SHIFT = 2 const CU_AMERICAN_SHIFT = 8 const CU_FRENCH_SHIFT = 14 @@ -133,8 +122,8 @@ function setup_game(seed) { played_british_reinforcements: 0, played_american_reinforcements: [], - pc: new Array(space_count).fill(PC_NONE), - generals: new Array(general_count).fill(NOWHERE), + loc: new Array(general_count).fill(NOWHERE), + spc: new Array(space_count + 2).fill(PC_NONE), moved: [], mcu: [], @@ -157,50 +146,46 @@ function setup_game(seed) { actions: [], } - function spawn_unit(owner, location, pc, cu, general=NOBODY) { - if (pc) - set_space_pc(location, pc) - if (general !== NOBODY) - set_general_location(general, location) - if (cu > 0) - spawn_cu(owner, location, cu) - } + set_space_pc(QUEBEC, PC_BRITISH) + set_space_pc(MONTREAL, PC_BRITISH) + set_space_pc(FORT_DETROIT, PC_BRITISH) + set_space_pc(BOSTON, PC_BRITISH) + set_space_pc(NORFOLK, PC_BRITISH) + set_space_pc(GILBERT_TOWN, PC_BRITISH) + set_space_pc(WILMINGTON_NC, PC_BRITISH) + set_space_pc(NINETY_SIX, PC_BRITISH) - function british(place, pc, cu, ld) { - spawn_unit(BRITISH, place, pc, cu, ld) - } - function american(place, pc, cu, ld) { - spawn_unit(AMERICAN, place, pc, cu, ld) - } - function french(place, pc, cu, ld) { - spawn_unit(FRENCH, place, pc, cu, ld) - } + set_general_location(CARLETON, QUEBEC) + set_general_location(HOWE, BOSTON) + + place_british_cu(QUEBEC, 2) + place_british_cu(BOSTON, 5) + place_british_cu(FORT_DETROIT, 1) + + set_space_pc(LEXINGTON_CONCORD, PC_AMERICAN) + set_space_pc(CHARLESTON, PC_AMERICAN) + set_space_pc(PHILADELPHIA, PC_AMERICAN) + + set_general_location(WASHINGTON, LEXINGTON_CONCORD) + set_general_location(GREENE, NEWPORT) - british(QUEBEC, PC_BRITISH, 2, CARLETON) - british(MONTREAL, PC_BRITISH) - british(FORT_DETROIT, PC_BRITISH, 1) - british(BOSTON, PC_BRITISH, 5, HOWE) - british(NORFOLK, PC_BRITISH) - british(GILBERT_TOWN, PC_BRITISH) - british(WILMINGTON_NC, PC_BRITISH) - british(NINETY_SIX, PC_BRITISH) + place_american_cu(LEXINGTON_CONCORD, 5) + place_american_cu(NEWPORT, 2) + place_american_cu(CHARLESTON, 2) - american(LEXINGTON_CONCORD, PC_AMERICAN, 5, WASHINGTON) - american(NEWPORT, PC_NONE, 2, GREENE) - american(CHARLESTON, PC_AMERICAN, 2) - american(PHILADELPHIA, PC_AMERICAN) + set_general_location(BURGOYNE, BRITISH_REINFORCEMENTS) + set_general_location(CLINTON, BRITISH_REINFORCEMENTS) + set_general_location(CORNWALLIS, BRITISH_REINFORCEMENTS) - british(BRITISH_REINFORCEMENTS, PC_NONE, 0, BURGOYNE) - british(BRITISH_REINFORCEMENTS, PC_NONE, 0, CLINTON) - british(BRITISH_REINFORCEMENTS, PC_NONE, 0, CORNWALLIS) + set_general_location(ARNOLD, AMERICAN_REINFORCEMENTS) + set_general_location(LINCOLN, AMERICAN_REINFORCEMENTS) + set_general_location(GATES, AMERICAN_REINFORCEMENTS) + set_general_location(LEE, AMERICAN_REINFORCEMENTS) + set_general_location(LAFAYETTE, AMERICAN_REINFORCEMENTS) - american(AMERICAN_REINFORCEMENTS, PC_NONE, 0, ARNOLD) - american(AMERICAN_REINFORCEMENTS, PC_NONE, 0, LINCOLN) - american(AMERICAN_REINFORCEMENTS, PC_NONE, 0, GATES) - american(AMERICAN_REINFORCEMENTS, PC_NONE, 0, LEE) - american(AMERICAN_REINFORCEMENTS, PC_NONE, 0, LAFAYETTE) + set_general_location(ROCHAMBEAU, FRENCH_REINFORCEMENTS) - french(FRENCH_REINFORCEMENTS, PC_NONE, 5, ROCHAMBEAU) + place_french_cu(FRENCH_REINFORCEMENTS, 5) goto_committees_of_correspondence() } @@ -378,11 +363,12 @@ function is_american_winter_offensive() { /* PC */ function set_space_pc(space, pc) { - game.pc[space] = pc + game.spc[space] &= ~3 + game.spc[space] |= pc } function get_space_pc(space) { - return game.pc[space] + return game.spc[space] & 3 } function has_no_pc(space) { @@ -481,30 +467,30 @@ function reset_moved_cu() { } function count_british_cu(s) { - return (game.pc[s] & CU_BRITISH_MASK) >>> CU_BRITISH_SHIFT + return (game.spc[s] & CU_BRITISH_MASK) >>> CU_BRITISH_SHIFT } function count_american_cu(s) { - return (game.pc[s] & CU_AMERICAN_MASK) >>> CU_AMERICAN_SHIFT + return (game.spc[s] & CU_AMERICAN_MASK) >>> CU_AMERICAN_SHIFT } function count_french_cu(s) { - return (game.pc[s] & CU_FRENCH_MASK) >>> CU_FRENCH_SHIFT + return (game.spc[s] & CU_FRENCH_MASK) >>> CU_FRENCH_SHIFT } function set_british_cu(s, n) { - game.pc[s] &= ~CU_BRITISH_MASK - game.pc[s] |= n << CU_BRITISH_SHIFT + game.spc[s] &= ~CU_BRITISH_MASK + game.spc[s] |= n << CU_BRITISH_SHIFT } function set_american_cu(s, n) { - game.pc[s] &= ~CU_AMERICAN_MASK - game.pc[s] |= n << CU_AMERICAN_SHIFT + game.spc[s] &= ~CU_AMERICAN_MASK + game.spc[s] |= n << CU_AMERICAN_SHIFT } function set_french_cu(s, n) { - game.pc[s] &= ~CU_FRENCH_MASK - game.pc[s] |= n << CU_FRENCH_SHIFT + game.spc[s] &= ~CU_FRENCH_MASK + game.spc[s] |= n << CU_FRENCH_SHIFT } function has_british_cu(space) { @@ -576,10 +562,6 @@ function count_enemy_cu(where) { return count_british_cu(where) } -function spawn_cu(owner, where, count) { - game.cu.push({ owner: owner, location: where, count: count, moved: 0 }) -} - function remove_british_cu(where, count) { set_british_cu(where, count_british_cu(where) - count) } @@ -622,11 +604,11 @@ function move_french_cu(from, to, count) { /* GENERALS */ function location_of_general(g) { - return game.generals[g] + return game.loc[g] } function set_general_location(g, s) { - game.generals[g] = s + game.loc[g] = s } function is_general_at_location(g, s) { @@ -3579,18 +3561,19 @@ exports.view = function (state, current) { game = state view = { - active: state.active, year: state.year, war_ends: state.war_ends, played_british_reinforcements: state.played_british_reinforcements, played_american_reinforcements: state.played_american_reinforcements, - congress: state.congress, - european_war: state.european_war, french_alliance: state.french_alliance, + european_war: state.european_war, + congress: state.congress, french_navy: state.french_navy, regulars: state.regulars, - generals: state.generals, - pc: state.pc, + + loc: state.loc, + spc: state.spc, + control: state.control, a_cards: state.a_hand.length, b_cards: state.b_hand.length, -- cgit v1.2.3