index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
-rw-r--r-- | src/radial.js | 266 |
1 files changed, 33 insertions, 233 deletions
diff --git a/src/radial.js b/src/radial.js index 4ef77d2..5771099 100644 --- a/src/radial.js +++ b/src/radial.js @@ -29,7 +29,6 @@ function sameSigns(a, b) { } function getNeighbors(coords) { - // const [q, r, s] = coords.split(',').map(n => +n); const { q, r, s, t } = fromKey(coords); return [ @@ -42,43 +41,6 @@ function getNeighbors(coords) { ] } -function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = {}, { q = 0, r = 0, s = 0 } = {}) { - const list = new Map(); - - for (let row = 0; row < rows; row++) { - const alternating = row % 2; - const dr = row; - - for (let col = 0; col < columns; col++) { - let dq = -(alternating + row) / 2 + alternating - col; - let ds = -(row + alternating) / 2 + col; - - if (odd) { - dq = dq - alternating; - ds = ds + alternating; - } - - list.set(toKey({ q: q + dq, r: r + dr, s: s + ds }), null); - } - - if (!equal) { - if ((odd && !(row % 2)) || (!odd && row % 2)) { - let dq = -(alternating + row) / 2 + alternating - columns; - let ds = -(row + alternating) / 2 + columns; - - if (odd) { - dq = dq - alternating; - ds = ds + alternating; - } - - list.set(toKey({ q: q + dq, r: r + dr, s: s + ds }), null); - } - } - } - - return list; -} - function addHexText(q, r, s, v) { const qText = document.createElementNS(xmlns, 'text'); qText.textContent = q; @@ -122,7 +84,6 @@ function radialToScreenCoords({ q, r, s }) { function drawHexes(el, list, renderText = false) { for ([key, v] of list) { - // const [q, r, s] = key.split(',').map(n => +n); const { q, r, s, t } = fromKey(key); const { x, y } = radialToScreenCoords({ q, r, s }); @@ -148,7 +109,7 @@ function translateRadialCoords({ q, r, s }, direction, distance = 1) { }; } -function generateRadialCoords(l, { q, r, s, t = 0 } = {}, { left, top, right, bottom }, offset) { +function generateRadialCoords(l, { q, r, s, t = 0 } = {}, { left, top, right, bottom }, offset = false) { const origin = toKey({ q, r, s, t }); const list = new Map(l); @@ -159,7 +120,6 @@ function generateRadialCoords(l, { q, r, s, t = 0 } = {}, { left, top, right, bo const v = queue.shift(); getNeighbors(v).forEach(w => { - // const [wq, wr, ws] = w.split(',').map(n => +n); const { q: wq, r: wr, s: ws } = fromKey(w); const rDist = Math.abs(wr - r); const alternating = rDist % 2; @@ -187,7 +147,6 @@ function translateCoords(map, translator) { const translated = new Map(); for ([key, val] of map) { - // const [q, r, s] = key.split(',').map(n => +n); const { q, r, s, t } = fromKey(key); translated.set(toKey(translator({ q, r, s, t })), val); } @@ -260,26 +219,11 @@ buildingHexes.bld1 = generateRadialCoords( 'both' ); -buildingHexes.bld2 = generateRadialCoords( - new Map(), - { q: 0, r: 0, s: 0 }, - { left: 3, top: 5, right: 3, bottom: 5 }, - 'both' -); - -buildingHexes.bld2 = generateRadialCoords( - buildingHexes.bld2, - translateRadialCoords({ q: 0, r: 0, s: 0 }, { q: 1, r: 0, s: -1 }, 3), - { left: 1, top: 2, right: 1, bottom: 4 }, - 'right' -); - -buildingHexes.bld2 = generateRadialCoords( - buildingHexes.bld2, - translateRadialCoords({ q: 0, r: 0, s: 0 }, { q: 1, r: 0, s: -1 }, -3), - { left: 1, top: 2, right: 1, bottom: 4 }, - 'left' -); +buildingHexes.bld2 = [ + [{ q: 0, r: 0, s: 0 }, { left: 3, top: 5, right: 3, bottom: 5 }, 'both'], + [{ q: 3, r: 0, s: -3 }, { left: 1, top: 2, right: 1, bottom: 4 }, 'right'], + [{ q: -3, r: 0, s: 3 }, { left: 1, top: 2, right: 1, bottom: 4 }, 'left'], +].reduce((acc, args) => new Map([...generateRadialCoords(acc, ...args)]), new Map()); buildingHexes.bld3 = generateRadialCoords( new Map(), @@ -306,92 +250,24 @@ buildingHexes.bld6 = generateRadialCoords( { left: 1, top: 5, right: 2, bottom: 4 } ); -buildingHexes.bld7 = generateRadialCoords( - new Map(), - { q: 0, r: 0, s: 0 }, - { left: 7, top: 4, right: 7, bottom: 3 }, - 'left' -); - -buildingHexes.bld7 = generateRadialCoords( - buildingHexes.bld7, - { q: 1, r: -4, s: 3 }, - { left: 5, top: 1, right: 6, bottom: 1 } -); - -buildingHexes.bld7 = generateRadialCoords( - buildingHexes.bld7, - { q: 5, r: 3, s: -8 }, - { left: 1, top: 1, right: 2, bottom: 1 } -); - -buildingHexes.bld7 = generateRadialCoords( - buildingHexes.bld7, - { q: -4, r: 3, s: 1 }, - { left: 3, top: 1, right: 4, bottom: 1 } -); - -buildingHexes.bld8 = generateRadialCoords( - new Map(), - { q: 0, r: 0, s: 0 }, - { left: 6, top: 7, right: 6, bottom: 4 }, - // { left: 1, top: 1, right: 1, bottom: 1 }, - 'both' -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: 5, r: 2, s: -7 }, - { left: 1, top: 2, right: 1, bottom: 2 } -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: 1, r: 5, s: -6 }, - { left: 4, top: 2, right: 4, bottom: 2 }, - 'both' -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: -2, r: 4, s: -2 }, - { left: 1, top: 1, right: 1, bottom: 1 } -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: 7, r: -7, s: 0 }, - { left: 2, top: 1, right: 2, bottom: 1 } -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: -2, r: -7, s: 9 }, - { left: 2, top: 1, right: 2, bottom: 1 } -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: -3, r: -6, s: 9 }, - { left: 1, top: 1, right: 1, bottom: 2 }, - 'left' -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: -7, r: 3, s: 4 }, - { left: 1, top: 3, right: 2, bottom: 3 }, - 'right' -); - -buildingHexes.bld8 = generateRadialCoords( - buildingHexes.bld8, - { q: -7, r: 4, s: 3 }, - { left: 1, top: 1, right: 1, bottom: 1 } -); - -const grid = generateRadialCoordsRect({ rows: 25, columns: 33 }, { q: 22, r: -12, s: -10 }); -// const grid = generateRadialCoordsRect({ rows: 52, columns: 33 }, translateRadialCoords({ q: 22, r: -12, s: -10 }, { q: 1, r: -2, s: 1 }, 7)); +buildingHexes.bld7 = [ + [{ q: 0, r: 0, s: 0 }, { left: 7, top: 4, right: 7, bottom: 3 }, 'left'], + [{ q: 1, r: -4, s: 3 }, { left: 5, top: 1, right: 6, bottom: 1 }], + [{ q: 5, r: 3, s: -8 }, { left: 1, top: 1, right: 2, bottom: 1 }], + [{ q: -4, r: 3, s: 1 }, { left: 3, top: 1, right: 4, bottom: 1 }], +].reduce((acc, args) => new Map([...generateRadialCoords(acc, ...args)]), new Map()); + +buildingHexes.bld8 = [ + [{ q: 0, r: 0, s: 0 }, { left: 6, top: 7, right: 6, bottom: 4 }, 'both'], + [{ q: 5, r: 2, s: -7 }, { left: 1, top: 2, right: 1, bottom: 2 }], + [{ q: 1, r: 5, s: -6 }, { left: 4, top: 2, right: 4, bottom: 2 }, 'both'], + [{ q: -2, r: 4, s: -2 }, { left: 1, top: 1, right: 1, bottom: 1 }], + [{ q: 7, r: -7, s: 0 }, { left: 2, top: 1, right: 2, bottom: 1 }], + [{ q: -2, r: -7, s: 9 }, { left: 2, top: 1, right: 2, bottom: 1 }], + [{ q: -3, r: -6, s: 9 }, { left: 1, top: 1, right: 1, bottom: 2 }, 'left'], + [{ q: -7, r: 3, s: 4 }, { left: 1, top: 3, right: 2, bottom: 3 }, 'right'], + [{ q: -7, r: 4, s: 3 }, { left: 1, top: 1, right: 1, bottom: 1 }] +].reduce((acc, args) => new Map([...generateRadialCoords(acc, ...args)]), new Map()); const mapsheet1BuildingCoords = [ translateCoords(buildingHexes.bld2, ({ q, r, s }) => ({ q: q + 7, r: r + 7, s: s - 14 })), @@ -506,80 +382,14 @@ function drawMapsheet(gameboard, { id, grid, buildings }) { return new Map([...grid, ...buildingHexes]); } -// const mapsheetHexCoords = generateRadialCoords( -// new Map(), -// { q: 0, r: 0, s: 0 }, -// { left: 17, top: 13, right: 17, bottom: 14 }, -// 'left' -// ); - -function drawScenario1() { - const northMapsheet = function(map) { - return translateCoords(map, ({ q, r, s }) => ({ q: q + 7, r: r - 14, s: s + 7 })); - } - - const southMapsheet = function(map) { - return translateCoords(map, ({ q, r, s }) => ({ q: q - 6, r: r + 12, s: s - 6 })); - } - - const westMapsheet = function(map) { - return translateCoords(map, ({ q, r, s }) => ({ q: q + 16, r, s: s - 16 })); - } - - const eastMapsheet = function(map) { - return translateCoords(map, ({ q, r, s }) => ({ q: q - 17, r, s: s + 17 })); - } - - mapsheet2.grid = westMapsheet(mapsheet2.grid); - mapsheet2.buildings = mapsheet2.buildings.map(westMapsheet); - mapsheet2 = drawMapsheet(gameboard, mapsheet2); - - mapsheet3.grid = eastMapsheet(mapsheet3.grid); - mapsheet3.buildings = mapsheet3.buildings.map(eastMapsheet); - mapsheet3 = drawMapsheet(gameboard, mapsheet3); - - return new Map([...mapsheet2, ...mapsheet3]); -} - -function drawScenario2() { - const northMapsheet = function(map) { - return translateCoords(map, ({ q, r, s }) => ({ q: q + 13, r: r - 26, s: s + 13 })); - } - - const southMapsheet = function(map) { - return translateCoords(map, ({ q, r, s }) => ({ q: q - 13, r: r + 26, s: s - 13 })); - } - - mapsheet1 = drawMapsheet(gameboard, mapsheet1); - - mapsheet2.grid = northMapsheet(mapsheet2.grid); - mapsheet2.buildings = mapsheet2.buildings.map(northMapsheet); - mapsheet2 = drawMapsheet(gameboard, mapsheet2); - - mapsheet3.grid = southMapsheet(mapsheet3.grid); - mapsheet3.buildings = mapsheet3.buildings.map(southMapsheet); - mapsheet3 = drawMapsheet(gameboard, mapsheet3); - - return new Map([...mapsheet1, ...mapsheet2, ...mapsheet3]); -} - -// const scenario1grid = drawScenario1(); -// const scenario2grid = drawScenario2(); - const horzMapVect = function(map) { return vectorAdd(map, { q: 1, r: 0, s: -1 }, 33); } -// [-16, 17], [-33, 0, 33], [-49, -16, 17, 50] const vertMapVect = function(map) { return vectorAdd(map, { q: 1, r: -2, s: 1 }, 13); } -// [[7], [[13], [[14], -// [-6]] [0], [7], -// [-13]] [-6] -// [-12]] - function vectorAdd(map, { q: dq, r: dr, s: ds }, scalar) { return translateCoords(map, ({ q, r, s }) => ({ q: q - dq * scalar, r: r + dr * scalar, s: s - ds * scalar })); } @@ -602,8 +412,8 @@ function findScalar(arr) { let neg; if (arr.length % 2) { - pos = 13; - neg = 13; + pos = -13; + neg = -13; } else { pos = -7; neg = -6; @@ -625,22 +435,18 @@ function findScalar(arr) { }); } -// const mults = findMult([['a', 'b', 'c'], ['c', 'd', 'e'], ['e', 'f', 'g']]); -// const mults = findMult([['a', 'b'], ['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]); - -// console.log(findScalar(mults)); - // const sheets = [[mapsheet2], [mapsheet3]]; -// const sheets = [[mapsheet2], [mapsheet1], [mapsheet3]]; +const sheets = [[mapsheet2], [mapsheet1], [mapsheet3]]; // const sheets = [[mapsheet4]]; -const sheets = [ - [mapsheet2, mapsheet1], - [mapsheet3, mapsheet4] -]; +// const sheets = [ +// [mapsheet2, mapsheet1], +// [mapsheet3, mapsheet4] +// ]; -// drawHexes(gameboard, buildingHexes.bld8, true); +// mapsheet1 = drawMapsheet(gameboard, mapsheet1); +// drawHexes(gameboard, buildingHexes.bld8, true); // drawHexes(addGroup(gameboard, 'part-1'), bld8, true); findScalar(findMult(sheets)).forEach(([vscalar, row]) => { @@ -659,12 +465,6 @@ findScalar(findMult(sheets)).forEach(([vscalar, row]) => { }) }); -// mapsheet1 = drawMapsheet(gameboard, mapsheet1); - -// mapsheet3.grid = horzMapVect(vertMapVect(mapsheet3.grid)); -// mapsheet3.buildings = mapsheet3.buildings.map(buildings => horzMapVect(vertMapVect(buildings))); -// mapsheet3 = drawMapsheet(gameboard, mapsheet3); - const circle = document.createElementNS(xmlns, 'circle'); circle.setAttributeNS(null, 'r', 5); circle.setAttributeNS(null, 'fill', 'green'); |