summaryrefslogtreecommitdiff
path: root/tools/parse-layout.js
blob: 90065603195e258950a79cc471c95c003e718d1f (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
const fs = require("fs")

let points = {}
let rects = {}
let edges = {}
let mode, name, x, y, w, h, cx, cy, rx, ry, x2, y2
let labels = []

function add_point(x, y) {
	if (name in points)
		points[name].push([x,y])
	else
		points[name] = [ [x,y] ]
}

function add_rect(x, y, w, h) {
	if (name in rects)
		rects[name].push([x,y,x+w,y+h])
	else
		rects[name] = [ [x,y,x+w,y+h] ]
}

function add_edge(x1, y1, x2, y2) {
	if (name in edges)
		edges[name].push({x1,y1,x2,y2})
	else
		edges[name] = [ {x1,y1,x2,y2} ]
}

function flush() {
	if (mode === 'path') {
		add_edge(x, y, x2, y2)
	}
	if (mode === 'rect') {
		if (name.startsWith("$"))
			add_rect(x, y, w, h)
		else
			add_point( x + w/2, y + h/2 )
	}
	if (mode === 'circle') {
		add_point( cx, cy )
	}
	x = y = x2 = y2 = w = h = cx = cy = rx = ry = 0
}

function parse_path_data(path) {
	let cx = 0
	let cy = 0
	let abs = 0
	for (let i = 0; i < path.length;) {
		switch (path[i]) {
		case 'M':
			x2 = x = cx = Number(path[i+1])
			y2 = y = cy = Number(path[i+2])
			i += 3
			abs = true
			break
		case 'm':
			x2 = x = cx = cx + Number(path[i+1])
			y2 = y = cy = cy + Number(path[i+2])
			i += 3
			abs = false
			break
		case 'C':
			x2 = cx = Number(path[i+5])
			y2 = cy = Number(path[i+6])
			i += 7
			abs = true
			break
		case 'L':
			i += 1
			abs = true
			break
		case 'H':
			x2 = cx = Number(path[i+1])
			i += 2
			abs = true
			break
		case 'V':
			y2 = cy = Number(path[i+1])
			i += 2
			abs = true
			break
		case 'c':
			x2 = cx = cx + Number(path[i+5])
			y2 = cy = cy + Number(path[i+6])
			i += 7
			break
		case 'l':
			i += 1
			abs = false
			break
		case 'h':
			x2 = cx = cx + Number(path[i+1])
			i += 2
			abs = false
			break
		case 'v':
			y2 = cy = cy + Number(path[i+1])
			i += 2
			abs = false
			break
		default:
			if (abs) {
				x2 = cx = Number(path[i+0])
				y2 = cy = Number(path[i+1])
			} else {
				x2 = cx = cx + Number(path[i+0])
				y2 = cy = cy + Number(path[i+1])
			}
			i += 2
			break
		}
	}
}

for (let line of fs.readFileSync("tools/layout.svg", "utf-8").split("\n")) {
	line = line.trim()
	if (line.startsWith("<rect")) {
		flush()
		mode = "rect"
		x = y = w = h = 0
	}
	else if (line.startsWith("<ellipse") || line.startsWith("<circle")) {
		flush()
		mode = "circle"
		cx = cy = rx = ry = 0
	}
	else if (line.startsWith("<path")) {
		flush()
		mode = "path"
	}
	else if (line.startsWith("<g")) {
		flush()
		mode = "g"
	}
	else if (line.startsWith('x="'))
		x = (Number(line.split('"')[1]))
	else if (line.startsWith('y="'))
		y = (Number(line.split('"')[1]))
	else if (line.startsWith('width="'))
		w = (Number(line.split('"')[1]))
	else if (line.startsWith('height="'))
		h = (Number(line.split('"')[1]))
	else if (line.startsWith('cx="'))
		cx = (Number(line.split('"')[1]))
	else if (line.startsWith('cy="'))
		cy = (Number(line.split('"')[1]))
	else if (line.startsWith('r="'))
		rx = ry = (Number(line.split('"')[1]))
	else if (line.startsWith('rx="'))
		rx = (Number(line.split('"')[1]))
	else if (line.startsWith('ry="'))
		ry = (Number(line.split('"')[1]))
	else if (line.startsWith('inkscape:label="') && mode === "g")
		name = line.split('"')[1]
	else if (line.startsWith('d="'))
		parse_path_data(line.split('"')[1].split(/[ ,]/))
	if (line.includes("</tspan>")) {
		let name = line.replace(/^[^>]*>/, "").replace(/<\/tspan.*/, "")
		labels.push({x, y, name})
	}
}

flush()

function find_closest_node(list, x, y) {
	let nd = Infinity, nn = null

	for (let n of list) {
		let d = Math.hypot(n.x - x, n.y - y)
		if (d < nd) {
			nd = d
			nn = n
		}
	}

	if (!nn) {
		console.log("NOT FOUND", x, y)
		return [ null, 0 ]
	}

	return [ nn, nd ]
}

function find_closest_city(x, y) {
	let nd = Infinity, nn = -1

	for (let i = 0; i < cities.length; ++i) {
		let n = cities[i]
		let d = Math.hypot(n.x - x, n.y - y)
		if (d < nd) {
			nd = d
			nn = i
		}
	}

	return nn
}

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 FLANDERS = "Flanders"
const BOHEMIA = "Bohemia"

const CLUBS = "clubs"
const HEARTS = "hearts"
const SPADES = "spades"
const DIAMONDS = "diamonds"

const FRANCE = "France"
const BAVARIA = "Bavaria"
const PRUSSIA = "Prussia"
const SAXONY = "Saxony"
const AUSTRIA = "Austria"
const AUSTRIAN_NETHERLANDS = "Austria"
const EMPIRE = "Empire"
const NETHERLANDS = "Netherlands"
const SILESIA = "Silesia"
const POLAND = "Poland"

const PRAGMATIC_ARMY = "Pragmatic Army"

// FIND and label all points!
let all_labels = labels.slice()
let cities = []
for (let key in points) {
	for (let [x, y] of points[key]) {
		let [ node, dist ] = find_closest_node(labels, x, y)
		if (dist > 15) {
			console.log(key,x,y, dist, node.name)
		}
		if (node) {
			labels = labels.filter(x => x !== node)
			let suit = "UNKNOWN"

			if (find_enclosing_rect(rects.$CLUBS, x, y))
				suit = CLUBS
			else if (find_enclosing_rect(rects.$HEARTS, x, y))
				suit = HEARTS
			else if (find_enclosing_rect(rects.$DIAMONDS, x, y))
				suit = DIAMONDS
			else if (find_enclosing_rect(rects.$SPADES, x, y))
				suit = SPADES
			else
				console.log("NOT ASSIGNED SUIT", x, y)

			let map = "UNKNOWN"
			let country = "UNKNOWN"
			if (find_enclosing_rect(rects.$Empire_W, x, y)) {
				map = FLANDERS
				country = EMPIRE
			}
			else if (find_enclosing_rect(rects.$Empire_E, x, y)) {
				map = BOHEMIA
				country = EMPIRE
			}
			else if (find_enclosing_rect(rects.$Austria_W, x, y)) {
				map = FLANDERS
				country = AUSTRIAN_NETHERLANDS
			}
			else if (find_enclosing_rect(rects.$Austria_E, x, y)) {
				map = BOHEMIA
				country = AUSTRIA
			}
			else if (find_enclosing_rect(rects.$Bavaria, x, y)) {
				map = BOHEMIA
				country = BAVARIA
			}
			else if (find_enclosing_rect(rects.$Saxony, x, y)) {
				map = BOHEMIA
				country = SAXONY
			}
			else if (find_enclosing_rect(rects.$Silesia, x, y)) {
				map = BOHEMIA
				country = SILESIA
			}
			else if (find_enclosing_rect(rects.$Poland, x, y)) {
				map = BOHEMIA
				country = POLAND
			}
			else if (find_enclosing_rect(rects.$Prussia, x, y)) {
				map = BOHEMIA
				country = PRUSSIA
			}
			else if (find_enclosing_rect(rects.$Netherlands, x, y)) {
				map = FLANDERS
				country = NETHERLANDS
			}
			else if (find_enclosing_rect(rects.$France, x, y)) {
				map = FLANDERS
				country = FRANCE
			}

			if (country === "UNKNOWN")
				console.log("no country:", node)

			cities.push({
				name: node.name,
				map,
				country,
				suit,
				type: key,
				x: Math.round(x),
				y: Math.round(y),
				main_roads: [],
				roads: [],
			})
		} else {
			let [ dupname, dupdist ] = find_closest_node(all_labels, x, y)
			console.log("ALREADY USED", dupname, dupdist, x, y)
		}
	}
}

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

	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.name < b.name) return -1
	if (a.name > b.name) return 1
	return b.y - a.y
})

for (let e of edges.major_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)
}

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)
}

let arrays = {
	name: [],
	// map: [],
	country: [],
	// suit: [],
	// type: [],
	x: [],
	y: [],
	main_roads: [],
	roads: [],
}

let sets = {
	type: {
		major_fortress: [],
		minor_fortress: [],
		city: [],
		box: [],
	},
	suit: {
		clubs: [],
		diamonds: [],
		hearts: [],
		spades: [],
	},
	map: {
		Flanders: [],
		Bohemia: [],
		UNKNOWN: [],
	},
	country: {
		Austria: [],
		Bavaria: [],
		France: [],
		Netherlands: [],
		Poland: [],
		Prussia: [],
		Saxony: [],
		Silesia: [],
		Empire: [],
		UNKNOWN: [],
	},
}

for (let i = 0; i < cities.length; ++i) {
	let city = cities[i]
	for (let key in city) {
		if (arrays[key])
			arrays[key].push(city[key])
		if (sets[key])
			sets[key][city[key]].push(i)
	}
}

function map_to_range(x) {
	let pairs = []
	let a = 0, b = 1
	for (; b < x.length; ++b) {
		if (x[b-1] + 1 === x[b])
			continue
		pairs.push([x[a],x[b-1]])
		a = b
	}
	pairs.push([x[a],x[b-1]])
	return pairs
}

function remap_to_range(obj) {
	for (let key in 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
}

delete sets.type.city

sets.cities = arrays

console.log("const data = " + JSON.stringify(sets))
console.log("if (typeof module === 'object') module.exports = data")