summaryrefslogtreecommitdiff
path: root/tools/process-layout.js
blob: 2d19c0426dbc0b364079e2e51314c4e7ea1e517f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"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) => {
	if (a.type === "box" && b.type === "box")
		return 0
	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))