diff options
Diffstat (limited to 'tools/process-layout.js')
-rw-r--r-- | tools/process-layout.js | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/tools/process-layout.js b/tools/process-layout.js new file mode 100644 index 0000000..6de0844 --- /dev/null +++ b/tools/process-layout.js @@ -0,0 +1,164 @@ +"use strict" + +const fs = require("fs") + +let layout = JSON.parse(fs.readFileSync("tools/layout.json", "utf-8")) + +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 find_enclosing_rect(list, x, y) { + for (let {x1, y1, x2, y2} of list) { + if (x >= x1 && x <= x2) + if (y >= y1 && y <= y2) + return true + } + return false +} + +const SEAS = [ "Sea1", "Sea2", "Sea3", "Sea4", "Sea5", "Sea6", "Sea7" ] +const COLONIES = [ "Canada", "NH", "NY", "MA", "CT", "RI", "PA", "NJ", "MD", "DE", "VA", "NC", "SC", "GA", ] + +let seas = SEAS.map(x => []) +let colonies = COLONIES.map(x => []) + +let spaces = [] +let space_index = {} + +function add_space(type, name, x, y) { + let id = spaces.length + spaces.push({ + name, + type, + adjacent: [], + path: [], + wilderness: [], + port: -1, + colony: -1, + x: Math.round(x), + y: Math.round(y), + }) +} + +function add_edge(type, a, b) { + let ax = space_index[a] + let bx = space_index[b] + set_add(spaces[ax][type], bx) + set_add(spaces[bx][type], ax) + set_add(spaces[ax].adjacent, bx) + set_add(spaces[bx].adjacent, ax) +} + +function add_oneway(type, a, b, list2, set2) { + let ax = space_index[a] + let bx = space_index[b] + if (ax !== undefined) { + let k = list2.indexOf(b) + spaces[ax][type] = k + set_add(set2[k], ax) + } + if (bx !== undefined) { + let k = list2.indexOf(a) + spaces[bx][type] = k + set_add(set2[k], bx) + } +} + +for (let item of layout.points.fortified_port) + add_space("fortified_port", item.name, item.x, item.y) +for (let item of layout.points.winter_quarters) + add_space("winter_quarters", item.name, item.x, item.y) +for (let item of layout.points.regular) + add_space("regular", item.name, item.x, item.y) +for (let item of layout.points.box) + add_space("box", item.name, item.x, item.y) + +const TYPE_SORT = [ "fortified_port", "winter_quarters", "regular", "box" ] + +spaces.sort((a,b) => { + let at = TYPE_SORT.indexOf(a.type) + let bt = TYPE_SORT.indexOf(b.type) + if (at < bt) return -1 + if (at > bt) return 1 + if (a.name < b.name) return -1 + if (a.name > b.name) return 1 + return b.y - a.y +}) + +for (let i = 0; i < spaces.length; ++i) + space_index[spaces[i].name] = i + +for (let item of layout.edges.sea) + add_oneway("port", item.name1, item.name2, SEAS, seas) +for (let item of layout.edges.colony) + add_oneway("colony", item.name1, item.name2, COLONIES, colonies) + +for (let item of layout.edges.path) + add_edge("path", item.name1, item.name2) +for (let item of layout.edges.wilderness) + add_edge("wilderness", item.name1, item.name2) + + +function map_point(group, name) { + let item = layout.points[group].find(item => item.name === name) + return [ item.x, item.y ].map(Math.round) +} + +function map_box(group, name) { + let item = layout.boxes[group].find(item => item.name === name) + return [ item.x, item.y, item.w, item.h ].map(Math.round) +} + +function map_point_list(group, names) { + return names.map(n => map_point(group, n)) +} + +function map_box_list(group, names) { + return names.map(n => map_box(group, n)) +} + +function map_box_dict(list) { + let out = {} + for (let item of list) + out[item.name] = [ item.x, item.y, item.w, item.h ].map(Math.round) + return out +} + +function map_point_dict(list) { + let out = {} + for (let item of list) + out[item.name] = [ item.x, item.y ].map(Math.round) + return out +} + +let boxes = { + sea: map_box_list("sea", SEAS), + colony: map_point_list("colony", COLONIES), + french_alliance_track: map_point_dict(layout.points["French Alliance Track"]), + turn: map_point_dict(layout.points["Game Turn"]), + card_box: map_point_dict(layout.points["Card Boxes"]), + box: map_box_dict(layout.boxes.box), +} + +let data = { layout: boxes, spaces, space_index, colonies, seas } + +fs.writeFileSync("tools/data.json", JSON.stringify(data)) |