From 562e1822729e85834e54cc76d0bfbc8a584c572c Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 1 Jun 2024 15:04:23 +0200 Subject: setup --- tools/layout.svg | 29 ++++++----- tools/parse-layout.js | 137 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 106 insertions(+), 60 deletions(-) (limited to 'tools') diff --git a/tools/layout.svg b/tools/layout.svg index 841fb90..aa73244 100644 --- a/tools/layout.svg +++ b/tools/layout.svg @@ -40,10 +40,10 @@ inkscape:window-height="480" id="namedview6" showgrid="false" - inkscape:zoom="8.3661045" - inkscape:cx="236.64652" - inkscape:cy="1572.9559" - inkscape:current-layer="g3013" + inkscape:zoom="6.1864511" + inkscape:cx="1511.6937" + inkscape:cy="597.4918" + inkscape:current-layer="g5174" inkscape:document-rotation="0" showguides="false"> + inkscape:label="box" + style="display:none"> + sodipodi:insensitive="true" + style="display:none"> + sodipodi:insensitive="true" + style="display:none"> + style="display:none;stroke:#b3b3b3"> + style="display:none;stroke:#cccccc"> Meißsen + y="570.19531" + id="tspan2983">Meißen index; --i) + array[i] = array[i - 1] + array[index] = item +} + +function set_add(set, item) { + let a = 0 + let b = set.length - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = set[m] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else + return + } + array_insert(set, a, item) +} function add_point(x, y) { if (name in points) @@ -32,6 +57,8 @@ function flush() { add_edge(x, y, x2, y2) } if (mode === 'rect') { + if (subname && subname.startsWith("#")) + sectors[subname.slice(1)] = [x, y, x+w, y+h] if (name.startsWith("$")) add_rect(x, y, w, h) else @@ -41,6 +68,7 @@ function flush() { add_point( cx, cy ) } x = y = x2 = y2 = w = h = cx = cy = rx = ry = 0 + subname = null } function parse_path_data(path) { @@ -85,6 +113,7 @@ function parse_path_data(path) { x2 = cx = cx + Number(path[i+5]) y2 = cy = cy + Number(path[i+6]) i += 7 + abs = false break case 'l': i += 1 @@ -154,6 +183,8 @@ for (let line of fs.readFileSync("tools/layout.svg", "utf-8").split("\n")) { ry = (Number(line.split('"')[1])) else if (line.startsWith('inkscape:label="') && mode === "g") name = line.split('"')[1] + else if (line.startsWith('inkscape:label="') && mode !== "g") + subname = line.split('"')[1] else if (line.startsWith('d="')) parse_path_data(line.split('"')[1].split(/[ ,]/)) if (line.includes("")) { @@ -300,7 +331,7 @@ for (let key in points) { } if (country === "UNKNOWN") - console.log("no country:", node) + console.warn("no country:", node) cities.push({ name: node.name, @@ -310,6 +341,7 @@ for (let key in points) { type: key, x: Math.round(x), y: Math.round(y), + adjacent: [], main_roads: [], roads: [], }) @@ -320,48 +352,65 @@ for (let key in points) { } } + cities.sort((a,b) => { - if (a.country < b.country) return -1 - if (a.country > b.country) return 1 + if (a.map < b.map) return -1 + if (a.map > b.map) return 1 + + //let a_obj = a.type.includes("fortress") + //let b_obj = b.type.includes("fortress") + //if (a_obj && !b_obj) return -1 + //if (!a_obj && b_obj) return 1 - if (a.map < b.map) return 1 - if (a.map > b.map) return -1 + // if (a.type === "depot" && b.type !== "depot") return -1 + // if (a.type !== "depot" && b.type === "depot") return 1 if (a.suit < b.suit) return -1 if (a.suit > b.suit) return 1 - //if (a.type === "major_fortress" && b.type !== "major_fortress") return -1 - //if (a.type !== "major_fortress" && b.type === "major_fortress") return 1 - //if (a.type === "minor_fortress" && b.type !== "minor_fortress") return -1 - //if (a.type !== "minor_fortress" && b.type === "minor_fortress") return 1 + // if (a.country < b.country) return -1 + // if (a.country > b.country) return 1 + + if (a.type === "major_fortress" && b.type !== "major_fortress") return -1 + if (a.type !== "major_fortress" && b.type === "major_fortress") return 1 + if (a.type === "minor_fortress" && b.type !== "minor_fortress") return -1 + if (a.type !== "minor_fortress" && b.type === "minor_fortress") return 1 + + if (a.type === "box" && b.type !== "box") return 1 + if (a.type !== "box" && b.type === "box") return -1 if (a.name < b.name) return -1 if (a.name > b.name) return 1 + return b.y - a.y }) -for (let e of edges.major_road) { +for (let e of edges.main_road) { let a = find_closest_city(e.x1, e.y1) let b = find_closest_city(e.x2, e.y2) - cities[a].main_roads.push(b) - cities[b].main_roads.push(a) + set_add(cities[a].main_roads, b) + set_add(cities[b].main_roads, a) + set_add(cities[a].adjacent, b) + set_add(cities[b].adjacent, a) } for (let e of edges.road) { let a = find_closest_city(e.x1, e.y1) let b = find_closest_city(e.x2, e.y2) - cities[a].roads.push(b) - cities[b].roads.push(a) + set_add(cities[a].roads, b) + set_add(cities[b].roads, a) + set_add(cities[a].adjacent, b) + set_add(cities[b].adjacent, a) } let arrays = { name: [], - // map: [], - country: [], + // country: [], // suit: [], // type: [], x: [], y: [], + adjacent: [], main_roads: [], roads: [], } @@ -379,21 +428,21 @@ let sets = { hearts: [], spades: [], }, - map: { - Flanders: [], - Bohemia: [], - UNKNOWN: [], - }, country: { Austria: [], Bavaria: [], + Empire: [], France: [], Netherlands: [], Poland: [], Prussia: [], Saxony: [], Silesia: [], - Empire: [], + UNKNOWN: [], + }, + map: { + Bohemia: [], + Flanders: [], UNKNOWN: [], }, } @@ -426,35 +475,29 @@ function remap_to_range(obj) { obj[key] = map_to_range(obj[key]) } -remap_to_range(sets.map) remap_to_range(sets.suit) -remap_to_range(sets.country) - -function object_group_by(items, callback) { - let groups = {} - if (typeof callback === "function") { - for (let item of items) { - let key = callback(item) - if (key in groups) - groups[key].push(item) - else - groups[key] = [ item ] - } - } else { - for (let item of items) { - let key = item[callback] - if (key in groups) - groups[key].push(item) - else - groups[key] = [ item ] - } - } - return groups -} +remap_to_range(sets.map) +//remap_to_range(sets.country) delete sets.type.city sets.cities = arrays +function list_cities_in_rect(rect) { + let [ x1, y1, x2, y2 ] = rect + let list = [] + for (let i = 0; i < sets.cities.name.length; ++i) { + let x = sets.cities.x[i] + let y = sets.cities.y[i] + if (x >= x1 && x <= x2 && y >= y1 && y <= y2) + list.push(i) + } + return list +} + +sets.sectors = {} +for (let key in sectors) + sets.sectors[key] = list_cities_in_rect(sectors[key]) + console.log("const data = " + JSON.stringify(sets)) console.log("if (typeof module === 'object') module.exports = data") -- cgit v1.2.3