index : btroops | |
Virtual board game-aid for BattleTroops, an infantry combat simulator wargame published by FASA in 1989. |
aboutsummaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/radial.js | 322 |
1 files changed, 217 insertions, 105 deletions
diff --git a/src/radial.js b/src/radial.js index 22c8904..8142f77 100644 --- a/src/radial.js +++ b/src/radial.js @@ -36,30 +36,30 @@ function getNeighbors(coords) { ] } -function generateRadialCoords(l, { q, r, s }, radius) { - const origin = toKey(q, r, s); - const list = new Map(l); - const neighbors = new Set(); +// function generateRadialCoords(l, { q, r, s }, radius) { +// const origin = toKey(q, r, s); +// const list = new Map(l); +// const neighbors = new Set(); - let next = new Set(); +// let next = new Set(); - list.set(origin); - next.add(origin); +// list.set(origin); +// next.add(origin); - for (let i = 0; i < radius; i++) { - next.forEach(coords => { - getNeighbors(coords).forEach(n => { - list.set(n, null); - neighbors.add(n); - }); - }); +// for (let i = 0; i < radius; i++) { +// next.forEach(coords => { +// getNeighbors(coords).forEach(n => { +// list.set(n, null); +// neighbors.add(n); +// }); +// }); - next = new Set(neighbors); - neighbors.clear(); - } +// next = new Set(neighbors); +// neighbors.clear(); +// } - return list; -} +// return list; +// } function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = {}, { q = 0, r = 0, s = 0 } = {}) { const list = new Map(); @@ -98,7 +98,32 @@ function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = return list; } -function drawHexes(el, list) { +function addHexText(q, r, s, v) { + const qText = document.createElementNS(xmlns, 'text'); + qText.textContent = q; + qText.setAttributeNS(null, 'x', -3); + qText.setAttributeNS(null, 'y', -3); + + const rText = document.createElementNS(xmlns, 'text'); + rText.textContent = r; + rText.setAttributeNS(null, 'x', 5); + rText.setAttributeNS(null, 'y', 1.5); + + const sText = document.createElementNS(xmlns, 'text'); + sText.textContent = s; + sText.setAttributeNS(null, 'x', -3); + sText.setAttributeNS(null, 'y', 5); + + const vText = document.createElementNS(xmlns, 'text'); + vText.textContent = v; + vText.style.fill = 'red'; + vText.setAttributeNS(null, 'y', 1); + vText.setAttributeNS(null, 'x', -2); + + return [qText, rText, sText, vText]; +} + +function drawHexes(el, list, renderText = false) { for ([key, v] of list) { const [q, r, s] = key.split(',').map(n => +n); @@ -114,35 +139,16 @@ function drawHexes(el, list) { x = (q > s ? -1 : 1) * x * horzSpacing; y = r * vertSpacing; - const g = document.createElementNS(xmlns, 'g'); - g.setAttributeNS(null, 'transform', `translate(${x}, ${y})`); + const cell = document.createElementNS(xmlns, 'g'); + cell.setAttributeNS(null, 'transform', `translate(${x}, ${y})`); const use = document.createElementNS(xmlns, 'use'); use.setAttributeNS(null, 'href', '#hex'); + cell.appendChild(use); + + if (renderText) addHexText(q, r, s, v).forEach(txt => cell.appendChild(txt)); - const qText = document.createElementNS(xmlns, 'text'); - qText.textContent = q; - qText.setAttributeNS(null, 'x', -3); - qText.setAttributeNS(null, 'y', -3); - - const rText = document.createElementNS(xmlns, 'text'); - rText.textContent = r; - rText.setAttributeNS(null, 'x', 5); - rText.setAttributeNS(null, 'y', 1.5); - - const sText = document.createElementNS(xmlns, 'text'); - sText.textContent = s; - sText.setAttributeNS(null, 'x', -3); - sText.setAttributeNS(null, 'y', 5); - - const vText = document.createElementNS(xmlns, 'text'); - vText.textContent = v; - vText.style.fill = 'red'; - vText.setAttributeNS(null, 'y', 1); - vText.setAttributeNS(null, 'x', -2); - - [use, qText, rText, sText, vText].forEach(el => g.appendChild(el)); - el.appendChild(g); + el.appendChild(cell); } } @@ -154,26 +160,10 @@ function translateRadialCoords({ q, r, s }, direction, distance) { }; } -// const grid = generateRadialCoordsRect({ rows: 26, 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)); - -const building1 = generateRadialCoordsRect({ rows: 5, columns: 3, equal: false }, { q: 7, r: -4, s: -3 }); -const building2 = generateRadialCoordsRect({ rows: 4, columns: 3, odd: true }, { q: 0, r: -4, s: 4 }); -const building3 = generateRadialCoords(new Map(), { q: -1, r: 2, s: -1 }, 1); - -const buildingData = [ - [{ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }], - [{ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }], - [{ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }], - [{ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }], - [{ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }], - [{ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }], - [{ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }], - [{ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }], -].map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 7)]); +// const building1 = generateRadialCoordsRect({ rows: 5, columns: 3, equal: false }, { q: 7, r: -4, s: -3 }); +// const building2 = generateRadialCoordsRect({ rows: 4, columns: 3, odd: true }, { q: 0, r: -4, s: 4 }); +// const building3 = generateRadialCoords(new Map(), { q: -1, r: 2, s: -1 }, 1); -const buildingCoords = buildingData.map(([size, start]) => generateRadialCoordsRect(size, start)); // const buildingCoords = [ // generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }), @@ -192,8 +182,6 @@ const buildingCoords = buildingData.map(([size, start]) => generateRadialCoordsR // // generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }), // ] -buildingCoords.forEach(building => building.forEach(coords => grid.delete(coords))); - // building2.forEach(coords => grid.delete(coords)); // building3.forEach(coords => grid.delete(coords)); @@ -237,13 +225,32 @@ const buildingBlah = svg.querySelector('#building2'); const bld2grid = document.createElementNS(xmlns, 'g'); -const bld2hexes = [ - generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }), - generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }), - generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }), -] +// const bld2hexes = new Map([ +// ...generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 0, r: 0, s: 0 }), +// ...generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }), +// ...generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }), +// ]); + +let bld2hexes = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 3, top: 5, right: 3, bottom: 5 }, 'both'); + +bld2hexes = generateRadialCoords( + bld2hexes, + translateRadialCoords({ q: 0, r: 0, s: 0 }, { q: 1, r: 0, s: -1 }, 3), + { left: 1, top: 2, right: 1, bottom: 4 }, + 'right' +); -// bld2hexes.forEach(list => drawHexes(bld2grid, list)); +bld2hexes = generateRadialCoords( + bld2hexes, + translateRadialCoords({ q: 0, r: 0, s: 0 }, { q: 1, r: 0, s: -1 }, -3), + { left: 1, top: 2, right: 1, bottom: 4 }, + 'left' +); + + +console.log(bld2hexes); + +drawHexes(bld2grid, bld2hexes); const bld2elvBasement = bld2grid.cloneNode(true); bld2elvBasement.classList.add('elevation-basement'); @@ -272,37 +279,27 @@ const bld1elv1 = bld1grid.cloneNode(true); bld1elv1.classList.add('elevation-1'); bld1.appendChild(bld1elv1); -bld1hexes.forEach(coords => grid.delete(coords)) -bld2hexes.forEach(list => list.forEach(coords => grid.delete(coords))); - -function generateRadialCoords2(l, { q, r, s }) { +function generateRadialCoords(l, { q, r, s }, { left, top, right, bottom }, offset) { const origin = toKey(q, r, s); const list = new Map(l); list.set(origin, 0); let queue = [origin]; - const top = 26; - const bottom = 27; - const left = 17; - const right = 17; - const offset = 'right'; - while (queue.length > 0) { const v = queue.shift(); getNeighbors(v).forEach(w => { const [wq, wr, ws] = w.split(',').map(n => +n); const rDist = Math.abs(wr - r); - const dr = (rDist + rDist % 2) / 2; + const alternating = rDist % 2; + const dr = (rDist + alternating) / 2; let dLeft = left; let dRight = right; - if (offset === 'left') - dRight -= rDist % 2; - else if (offset === 'right') - dLeft -= rDist % 2; + if (['left', 'both'].includes(offset)) dRight -= alternating; + if (['right', 'both'].includes(offset)) dLeft -= alternating; if ([ !list.has(w), @@ -322,28 +319,143 @@ function generateRadialCoords2(l, { q, r, s }) { const elevation2 = document.createElementNS(xmlns, 'g'); elevation2.classList.add('elevation-2'); -const list = generateRadialCoords2(new Map(), { q: 0, r: 0, s: 0 }); -// const list = generateRadialCoords2(new Map(), { q: -5, r: 5, s: 0 }); +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)); -// q [3, -3] -// s [-3, 3] +const mapsheet2BuildingData = [ + [{ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }], + [{ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }], -// const list = generateRadialCoords2(new Map(), { q: -2, r: 0, s: 2 }); -// q [1, -5] -// s [-1, 5] + [{ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }], + [{ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }], + [{ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }], + [{ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }], -// const list = generateRadialCoords2(new Map(), { q: -1, r: 2, s: -1 }); -// q [2, -4] -// s [-4, 2] + [{ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }], + [{ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }], + + [{ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }], + [{ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }], + [{ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }], + + [{ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: 3, s: -7 }] +]//.map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 7)]) +; + +// const bld2hexes = [ +// generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }), +// generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }), +// generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }), +// ] +// const bld1hexes = generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: 3, s: -7 }); + +const mapsheet3BuildingData = [ + [{ rows: 6, columns: 3, equal: false, odd: true }, { q: 20, r: -10, s: -10 }], + [{ rows: 4, columns: 8, equal: false }, { q: 11, r: 6, s: -17 }], + [{ rows: 8, columns: 2, equal: false, odd: true }, { q: 3, r: 3, s: -6 }], + + [{ rows: 9, columns: 4, odd: true, equal: false }, { q: 13, r: -10, s: -3 }], + [{ rows: 5, columns: 1, odd: true }, { q: 8, r: -9, s: 1 }], + [{ rows: 5, columns: 1 }, { q: 13, r: -9, s: -4 }], + + [{ rows: 9, columns: 4, odd: true, equal: false }, { q: 2, r: -9, s: 7 }], + + [{ rows: 8, columns: 4, odd: true }, { q: -6, r: -9, s: 15 }], + + [{ rows: 6, columns: 13, odd: true }, { q: -4, r: 5, s: -1 }], + [{ rows: 1, columns: 10 }, { q: -7, r: 4, s: 3 }], + [{ rows: 1, columns: 2 }, { q: -7, r: 11, s: -4 }], + [{ rows: 1, columns: 6 }, { q: -14, r: 11, s: 3 }], +]; + +const mapsheet1BuildingData = [ + [{ rows: 6, columns: 3, equal: false }, { q: -4, r: -8, s: 12 }], + [{ rows: 8, columns: 2, equal: false, odd: true }, { q: -10, r: 4, s: 6 }], + + [{ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }], + [{ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }], + [{ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }], + + [{ rows: 8, columns: 4 }, { q: 17, r: -10, s: -7 }], + + // tree/brush copse + [{ rows: 9, columns: 6, equal: false }, { q: 5, r: -4, s: -1 }], +]; + +const mapsheet1BuildingCoords = mapsheet1BuildingData.map(([size, start]) => generateRadialCoordsRect(size, start)); +const mapsheet2BuildingCoords = mapsheet2BuildingData.map(([size, start]) => generateRadialCoordsRect(size, start)); +const mapsheet3BuildingCoords = mapsheet3BuildingData.map(([size, start]) => generateRadialCoordsRect(size, start)); + +const list = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 13, right: 17, bottom: 14 }); + +const mapsheet1 = new Map(list); +const mapsheet2 = new Map(list); +const mapsheet3 = new Map(list); + +mapsheet1BuildingCoords.forEach(building => { + for ([coords, v] of building) mapsheet1.delete(coords); +}); + +mapsheet2BuildingCoords.forEach(building => { + for ([coords, v] of building) mapsheet2.delete(coords); +}); + +mapsheet3BuildingCoords.forEach(building => { + for ([coords, v] of building) mapsheet3.delete(coords); +}); + +function coordsScenario2() { + const grid = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 39, right: 17, bottom: 40 }); + + mapsheet2BuildingData + .map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 13)]) + .map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => { + for ([coords, v] of building) grid.delete(coords); + } + ); + + mapsheet3BuildingData + .map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, -13)]) + .map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => { + for ([coords, v] of building) grid.delete(coords); + } + ); + + mapsheet1BuildingData + .map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => { + for ([coords, v] of building) grid.delete(coords); + } + ); + + return grid; +} + +function coordsScenario1() { + const grid = generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, { left: 17, top: 27, right: 17, bottom: 26 }); + + mapsheet2BuildingData + .map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 7)]) + .map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => { + for ([coords, v] of building) grid.delete(coords); + } + ); + + mapsheet3BuildingData + .map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, -6)]) + .map(([size, start]) => generateRadialCoordsRect(size, start)).forEach(building => { + for ([coords, v] of building) grid.delete(coords); + } + ); + + return grid; +} -// const list = generateRadialCoords2(new Map(), { q: -5, r: 4, s: 1 }); -// q [-2, -8] -// s [-2, 4] +drawHexes(elevation1, coordsScenario1(), true); -// drawHexes(elevation2, generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, 10)); -drawHexes(elevation2, grid); -drawHexes(elevation1, list); +// drawHexes(document.querySelector('#mapsheet1'), mapsheet1, true); +// drawHexes(document.querySelector('#mapsheet2'), mapsheet2, true); +// drawHexes(document.querySelector('#mapsheet3'), mapsheet3, true); const defs = svg.querySelector('defs'); -defs.after(elevation2); -elevation2.after(elevation1); +// defs.after(elevation2); +defs.after(elevation1); |