summaryrefslogtreecommitdiff
path: root/tools/parse-layout.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/parse-layout.js')
-rw-r--r--tools/parse-layout.js137
1 files changed, 90 insertions, 47 deletions
diff --git a/tools/parse-layout.js b/tools/parse-layout.js
index 9006560..785bc8c 100644
--- a/tools/parse-layout.js
+++ b/tools/parse-layout.js
@@ -1,10 +1,35 @@
+"use strict"
+
const fs = require("fs")
let points = {}
let rects = {}
let edges = {}
-let mode, name, x, y, w, h, cx, cy, rx, ry, x2, y2
+let mode, name, subname, x, y, w, h, cx, cy, rx, ry, x2, y2
let labels = []
+let sectors = {}
+
+function array_insert(array, index, item) {
+ for (let i = array.length; i > 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("</tspan>")) {
@@ -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")