summaryrefslogtreecommitdiff
path: root/play.js
blob: 959a8de1943906ba5b7e91083eadc39ad2c28a89 (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
"use strict"

// TODO: tooltip on cylinders
//	fealty rating and starting assets + forces on calendar
//	current assets and forces on map

// TODO: held events, this_turn events

// inactive command cylinder color - other color outline
// moved/fought lord coloring - blue outline?

function find_lord(name) {
	return data.lords.findIndex((x) => x.name === name)
}

const LORD_ANDREAS = find_lord("Andreas")
const LORD_HERMANN = find_lord("Hermann")
const LORD_ALEKSANDR = find_lord("Aleksandr")
const LORD_ANDREY = find_lord("Andrey")

const MAP_DPI = 75

const VASSAL_UNAVAILABLE = 0
const VASSAL_READY = 1
const VASSAL_MUSTERED = 2

const NOWHERE = -1
const CALENDAR = 100

const LEGATE_INDISPOSED = -2
const LEGATE_ARRIVED = -1

const round = Math.round
const floor = Math.floor
const ceil = Math.ceil

const first_p1_card = 0
const last_p1_card = 20
const first_p2_card = 21
const last_p2_card = 41

// unit types
const KNIGHTS = 0
const SERGEANTS = 1
const LIGHT_HORSE = 2
const ASIATIC_HORSE = 3
const MEN_AT_ARMS = 4
const MILITIA = 5
const SERFS = 6

// asset types
const PROV = 0
const COIN = 1
const LOOT = 2
const CART = 3
const SLED = 4
const BOAT = 5
const SHIP = 6

const VECHE = 100

const on_click_asset = [
	(evt) => evt.button === 0 && send_action('prov', evt.target.my_id),
	(evt) => evt.button === 0 && send_action('coin', evt.target.my_id),
	(evt) => evt.button === 0 && send_action('loot', evt.target.my_id),
	(evt) => evt.button === 0 && send_action('cart', evt.target.my_id),
	(evt) => evt.button === 0 && send_action('sled', evt.target.my_id),
	(evt) => evt.button === 0 && send_action('boat', evt.target.my_id),
	(evt) => evt.button === 0 && send_action('ship', evt.target.my_id),
]

function on_click_veche_coin(evt) {
	if (evt.button === 0)
		send_action('veche_coin')
}

const SUMMER = 0
const EARLY_WINTER = 1
const LATE_WINTER = 2
const RASPUTITSA = 3

const SEASONS = [
	null,
	SUMMER, SUMMER, EARLY_WINTER, EARLY_WINTER, LATE_WINTER, LATE_WINTER, RASPUTITSA, RASPUTITSA,
	SUMMER, SUMMER, EARLY_WINTER, EARLY_WINTER, LATE_WINTER, LATE_WINTER, RASPUTITSA, RASPUTITSA,
	null
]

function current_season() {
	return SEASONS[view.turn >> 1]
}

function max_plan_length() {
	switch (current_season()) {
	case SUMMER: return 6
	case EARLY_WINTER: return 4
	case LATE_WINTER: return 4
	case RASPUTITSA: return 5
	}
}

function map_has(map, key) {
	let a = 0
	let b = (map.length >> 1) - 1
	while (a <= b) {
		let m = (a + b) >> 1
		let x = map[m<<1]
		if (key < x)
			b = m - 1
		else if (key > x)
			a = m + 1
		else
			return true
	}
	return false
}

function map_get(map, key, missing) {
	let a = 0
	let b = (map.length >> 1) - 1
	while (a <= b) {
		let m = (a + b) >> 1
		let x = map[m<<1]
		if (key < x)
			b = m - 1
		else if (key > x)
			a = m + 1
		else
			return map[(m<<1)+1]
	}
	return missing
}

function set_has(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 true
	}
	return false
}

function pack1_get(word, n) {
	return (word >>> n) & 1
}

function pack4_get(word, n) {
	n = n << 2
	return (word >>> n) & 15
}

function is_lord_action(lord) {
	return !!(view.actions && view.actions.lord && set_has(view.actions.lord, lord))
}

function is_asset_action(lord, action) {
	return !!(view.actions && view.actions[action] && set_has(view.actions[action], lord))
}

function is_plan_action(lord) {
	return !!(view.actions && view.actions.plan && set_has(view.actions.plan, lord))
}

function is_service_action(lord) {
	return !!(view.actions && view.actions.service && set_has(view.actions.service, lord))
}

function is_vassal_action(vassal) {
	return !!(view.actions && view.actions.vassal && set_has(view.actions.vassal, vassal))
}

function is_locale_action(locale) {
	return !!(view.actions && view.actions.locale && set_has(view.actions.locale, locale))
}

function is_laden_march_action(locale) {
	return !!(view.actions && view.actions.laden_march && set_has(view.actions.laden_march, locale))
}

function is_way_action(way) {
	return !!(view.actions && view.actions.way && set_has(view.actions.way, way))
}

function is_card_action(c) {
	return !!(view.actions && view.actions.card && set_has(view.actions.card, c))
}

function is_legate_action() {
	return !!(view.actions && view.actions.legate)
}

function is_legate_selected() {
	return !!view.nevsky.legate_selected
}

const force_type_count = 7
const force_type_name = [ "knights", "sergeants", "light_horse", "asiatic_horse", "men_at_arms", "militia", "serfs" ]
const force_type_tip = [ "knights", "sergeants", "light horse", "asiatic horse", "men-at-arms", "militia", "serfs" ]

const asset_type_count = 7
const asset_type_name = [ "prov", "coin", "loot", "cart", "sled", "boat", "ship" ]
const asset_type_x3 = [ 1, 1, 1, 0, 0, 0, 0 ]

const first_p1_locale = 0
const last_p1_locale = 23
const first_p2_locale = 24
const last_p2_locale = 52

let used_cache = {}
let unused_cache = {}

function get_cached_element(className) {
	if (!(className in unused_cache)) {
		unused_cache[className] = []
		used_cache[className] = []
	}
	if (unused_cache[className].length > 0) {
		let elt = unused_cache[className].pop()
		used_cache[className].push(elt)
		return elt
	}
	let elt = document.createElement("div")
	elt.className = className
	used_cache[className].push(elt)
	return elt
}

function restart_cache() {
	for (let k in used_cache) {
		let u = used_cache[k]
		let uu = unused_cache[k]
		while (u.length > 0)
			uu.push(u.pop())
	}
}

function is_p1_locale(loc) {
	return loc >= first_p1_locale && loc <= last_p1_locale
}

function is_p2_locale(loc) {
	return loc >= first_p2_locale && loc <= last_p2_locale
}

function count_vp1() {
	let vp = 0
	for (let loc of view.locales.castles1)
		vp += 2
	for (let loc of view.locales.conquered)
		if (is_p2_locale(loc))
			vp += data.locales[loc].vp << 1
	for (let loc of view.locales.ravaged)
		if (is_p2_locale(loc))
			vp += 1
	return vp
}

function count_vp2() {
	let vp = view.nevsky.veche_vp * 2
	for (let loc of view.locales.castles2)
		vp += 2
	for (let loc of view.locales.conquered)
		if (is_p1_locale(loc))
			vp += data.locales[loc].vp << 1
	for (let loc of view.locales.ravaged)
		if (is_p1_locale(loc))
			vp += 1
	return vp
}

function get_lord_locale(lord) {
	return view.lords.locale[lord]
}

function is_lord_on_map(lord) {
	let loc = get_lord_locale(lord)
	return loc !== NOWHERE && loc < CALENDAR
}

function is_marshal(lord) {
	if (lord === LORD_HERMANN) return !is_lord_on_map(LORD_ANDREAS)
	if (lord === LORD_ANDREY) return !is_lord_on_map(LORD_ALEKSANDR)
}

function is_card_in_use(c) {
	if (set_has(view.events, c))
		return true
	if (set_has(view.capabilities, c))
		return true
	if (view.lords.capabilities.includes(c))
		return true
	if (c === 18 || c === 19 || c === 20)
		return true
	if (c === 39 || c === 40 || c === 41)
		return true
	return false
}

function has_global_capability(cap) {
	for (let c of view.capabilities)
		if (data.cards[c].capability === cap)
			return true
	return false
}

function is_vassal_ready(vassal) {
	return view.lords.vassals[vassal] === VASSAL_READY
}

function is_vassal_mustered(vassal) {
	return view.lords.vassals[vassal] === VASSAL_MUSTERED
}

function for_each_teutonic_card(fn) {
	for (let i = 0; i < 21; ++i)
		fn(i)
}

function for_each_russian_card(fn) {
	for (let i = 21; i < 42; ++i)
		fn(i)
}

function is_upper_lord(lord) {
	return map_has(view.lords.lieutenants, lord)
}

function is_lower_lord(lord) {
	for (let i = 1; i < view.lords.lieutenants.length; i += 2)
		if (view.lords.lieutenants[i] === lord)
			return true
	return false
}

function get_lower_lord(upper) {
	return map_get(view.lords.lieutenants, upper, -1)
}

function for_each_friendly_card(fn) {
	if (player === "Teutons")
		for_each_teutonic_card(fn)
	else
		for_each_russian_card(fn)
}

function for_each_enemy_card(fn) {
	if (player !== "Teutons")
		for_each_teutonic_card(fn)
	else
		for_each_russian_card(fn)
}

const original_boxes = {
	"way crossroads": [1500,4717,462,149],
	"way wirz": [1295,4526,175,350],
	"way peipus-east": [2232,4197,220,480],
	"way peipus-north": [2053,3830,361,228],
	"calendar summer box1": [40,168,590,916],
	"calendar summer box2": [650,168,590,916],
	"calendar winter box3": [1313,168,590,916],
	"calendar winter box4": [1922,168,590,916],
	"calendar winter box5": [2587,168,590,916],
	"calendar winter box6": [3196,168,590,916],
	"calendar rasputitsa box7": [3860,168,590,916],
	"calendar rasputitsa box8": [4470,168,590,916],
	"calendar summer box9": [40,1120,590,916],
	"calendar summer box10": [650,1120,590,916],
	"calendar winter box11": [1313,1120,590,916],
	"calendar winter box12": [1922,1120,590,916],
	"calendar winter box13": [2587,1120,590,916],
	"calendar winter box14": [3196,1120,590,916],
	"calendar rasputitsa box15": [3860,1120,590,916],
	"calendar rasputitsa box16": [4470,1120,590,916],
}

const calendar_xy = [
	[40,8],
	[40,168],
	[650,168],
	[1313,168],
	[1922,168],
	[2587,168],
	[3196,168],
	[3860,168],
	[4470,168],
	[40,1120],
	[650,1120],
	[1313,1120],
	[1922,1120],
	[2587,1120],
	[3196,1120],
	[3860,1120],
	[4470,1120],
	[4462,2068],
].map(([x,y])=>[x/4|0,y/4|0])

const locale_xy = []

let expand_calendar = -1

const ui = {
	locale: [],
	locale_extra: [],
	locale_markers: [],
	lord_cylinder: [],
	lord_service: [],
	lord_mat: [],
	lord_buttons: [],
	vassal_service: [],
	forces: [],
	routed: [],
	assets: [],
	ready_vassals: [],
	mustered_vassals: [],
	lord_capabilities: [],
	cards: [],
	boxes: {},
	ways: [],
	smerdi: document.getElementById("smerdi"),
	legate: document.getElementById("legate"),
	veche: document.getElementById("veche"),
	plan_dialog: document.getElementById("plan"),
	plan_list: document.getElementById("plan_list"),
	plan_actions: document.getElementById("plan_actions"),
	plan_list_cards: [],
	plan_action_cards: [],
	arts_of_war_dialog: document.getElementById("arts_of_war"),
	arts_of_war_list: document.getElementById("arts_of_war_list"),
	events: document.getElementById("events"),
	capabilities1: document.getElementById("capabilities1"),
	capabilities2: document.getElementById("capabilities2"),
	hand: document.getElementById("hand"),
	command: document.getElementById("command"),
	turn: document.getElementById("turn"),
	vp1: document.getElementById("vp1"),
	vp2: document.getElementById("vp2"),
}

let locale_layout = new Array(data.locales.length).fill(0)
let calendar_layout_service = []
let calendar_layout_cylinder = []

function clean_name(name) {
	return name.toLowerCase().replaceAll("&", "and").replaceAll(" ", "_")
}

const extra_size_100 = {
	town: [ 60, 42 ],
	castle: [ 60, 42 ],
	fort: [ 72, 42 ],
	traderoute: [ 72, 42 ],
	bishopric: [ 84, 60 ],
	city: [ 132, 72 ],
	novgorod: [ 156, 96 ],
}

const extra_size = {
	town: [ 45, 32 ],
	castle: [ 45, 32 ],
	fort: [ 54, 32 ],
	traderoute: [ 54, 32 ],
	bishopric: [ 63, 45 ],
	city: [ 100, 54 ],
	novgorod: [ 117, 72 ],
}

function toggle_pieces() {
	document.getElementById("pieces").classList.toggle("hide")
}

function on_click_locale(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('locale', id)
		send_action('laden_march', id)
	}
}

function on_focus_locale(evt) {
	let id = evt.target.my_id
	document.getElementById("status").textContent = `(${id}) ${data.locales[id].name} - ${data.locales[id].type}`
}

function on_click_way(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('way', id)
	}
}

function on_click_cylinder(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('lord', id)
	}
}

function on_click_card(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('card', id)
	}
}

function on_click_plan(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('plan', id)
	}
}

function on_focus_cylinder(evt) {
	let lord = evt.target.my_id
	let info = data.lords[lord]
	let loc = view.lords.locale[lord]
	if (loc >= CALENDAR) {
		document.getElementById("status").textContent = `${info.full_name} - ${info.fealty} Fealty`
	} else {
		let tip = `${info.full_name}`

		/*
		if (view.turn & 1)
			tip += ` - ${info.command} Command`
		else
			tip += ` - ${info.lordship} Lordship`
		*/

		let first = true
		let assets = view.lords.assets[lord]
		for (let i = 0; i < asset_type_count; ++i) {
			let x = pack4_get(assets, i)
			if (x > 0) {
				if (first)
					tip += " \u2013 "
				else
					tip += ", "
				tip += `${x} ${asset_type_name[i]}`
				first = false
			}
		}

		first = true
		let forces = view.lords.forces[lord]
		let routed = view.lords.routed[lord]
		for (let i = 0; i < force_type_count; ++i) {
			let x = pack4_get(forces, i) + pack4_get(routed, i)
			if (x > 0) {
				if (first)
					tip += " \u2013 "
				else
					tip += ", "
				tip += `${x} ${force_type_tip[i]}`
				first = false
			}
		}

		let c = view.lords.capabilities[(lord<<1)]
		if (c >= 0)
			tip += ` \u2013 ${data.cards[c].capability}`
		c = view.lords.capabilities[(lord<<1) + 1]
		if (c >= 0)
			tip += `, ${data.cards[c].capability}`

		document.getElementById("status").textContent = tip
	}
}

function on_click_lord_service_marker(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('service', id)
	}
}

function on_focus_lord_service_marker(evt) {
	let lord = evt.target.my_id
	let info = data.lords[lord]
	document.getElementById("status").textContent = `${info.full_name} - ${info.title}`
	if (expand_calendar !== view.lords.service[lord]) {
		expand_calendar = view.lords.service[lord]
		layout_calendar()
	}
}

function on_blur_lord_service_marker(evt) {
	let id = evt.target.my_id
	on_blur(evt)
	if (expand_calendar === view.lords.service[id]) {
		expand_calendar = -1
		layout_calendar()
	}
}

function on_click_vassal_service_marker(evt) {
	if (evt.button === 0) {
		let id = evt.target.my_id
		send_action('vassal', id)
	}
}

function on_focus_vassal_service_marker(evt) {
	let id = evt.target.my_id
	let vassal = data.vassals[id]
	let lord = data.lords[vassal.lord]
	document.getElementById("status").textContent = `(${id}) ${lord.name} / ${vassal.name}`
}

function on_click_legate(evt) {
	if (evt.button === 0)
		send_action('legate')
}

function on_blur(evt) {
	document.getElementById("status").textContent = ""
}

function update_current_card_display() {
	if (typeof view.what === "number" && view.what >= 0) {
		if (view.what <= first_p1_card)
			ui.command.className = `card teutonic aow_${view.what}`
		else
			ui.command.className = `card russian aow_${view.what}`
	} else if ((view.turn & 1) === 0) {
		if (player === "Russians")
			ui.command.className = `card russian aow_back`
		else
			ui.command.className = `card teutonic aow_back`
	} else if (view.command < 0) {
		if (player === "Russians")
			ui.command.className = `card russian cc_back`
		else
			ui.command.className = `card teutonic cc_back`
	} else {
		if (view.command < 6)
			ui.command.className = `card russian cc_lord_${view.command}`
		else
			ui.command.className = `card teutonic cc_lord_${view.command}`
	}
}

function on_focus_card_tip(c) {
	if (c <= first_p1_card)
		ui.command.className = `card teutonic aow_${c}`
	else
		ui.command.className = `card russian aow_${c}`
}

function on_blur_card_tip(c) {
	update_current_card_display()
}

function sub_card_capability(match, p1) {
	let x = p1 | 0
	return `<span class="card_tip" onmouseenter="on_focus_card_tip(${x})" onmouseleave="on_blur_card_tip(${x})">${data.cards[x].capability}</span>`
}

function sub_card_event(match, p1) {
	let x = p1 | 0
	return `<span class="card_tip" onmouseenter="on_focus_card_tip(${x})" onmouseleave="on_blur_card_tip(${x})">${data.cards[x].event}</span>`
}

function on_focus_locale_tip(loc) {
	ui.locale[loc].classList.add("tip")
	if (ui.locale_extra[loc])
		ui.locale_extra[loc].classList.add("tip")
}

function on_blur_locale_tip(loc) {
	ui.locale[loc].classList.remove("tip")
	if (ui.locale_extra[loc])
		ui.locale_extra[loc].classList.remove("tip")
}

function on_click_locale_tip(loc) {
	ui.locale[loc].scrollIntoView({ block:"center", inline:"center", behavior:"smooth" })
}

function on_focus_lord_tip(lord) {
}

function on_blur_lord_tip(lord) {
}

function on_click_lord_tip(lord) {
	ui.lord_mat[lord].scrollIntoView({ block:"center", inline:"center", behavior:"smooth" })
}

function sub_locale_name(match, p1) {
	let x = p1 | 0
	let n = data.locales[x].name
	return `<span class="locale_tip" onmouseenter="on_focus_locale_tip(${x})" onmouseleave="on_blur_locale_tip(${x})" onclick="on_click_locale_tip(${x})">${n}</span>`
}

function sub_lord_name(match, p1) {
	let x = p1 | 0
	let n = data.lords[x].name
	return `<span class="lord_tip" onmouseenter="on_focus_lord_tip(${x})" onmouseleave="on_blur_lord_tip(${x})" onclick="on_click_lord_tip(${x})">${n}</span>`
}

function on_log(text) {
	let p = document.createElement("div")

	if (text.match(/^>>/)) {
                text = text.substring(2)
                p.className = "ii"
        }

	if (text.match(/^>/)) {
                text = text.substring(1)
                p.className = "i"
        }

	text = text.replace(/&/g, "&amp;")
	text = text.replace(/</g, "&lt;")
	text = text.replace(/>/g, "&gt;")

	text = text.replace(/C(\d+)/g, sub_card_capability)
	text = text.replace(/E(\d+)/g, sub_card_event)
	text = text.replace(/L(\d+)/g, sub_lord_name)
	text = text.replace(/%(\d+)/g, sub_locale_name)

	if (text.match(/^\.h1/)) {
		text = text.substring(4)
		p.className = "h1"
	}
	if (text.match(/^\.h2/)) {
		text = text.substring(4)
		if (text.startsWith("Teuton"))
			p.className = "h2 teutonic"
		else if (text.startsWith("Russian"))
			p.className = "h2 russian"
		else
			p.className = "h2"
	}
	if (text.match(/^\.h3/)) {
		text = text.substring(4)
		p.className = "h3"
	}
	if (text.match(/^\.h4/)) {
		text = text.substring(4)
		p.className = "h4"
	}

	p.innerHTML = text
	return p
}

function layout_locale_item(loc, e, is_upper, yofs = 0) {
	let [x, y] = locale_xy[loc]
	let z = 0
	if (is_upper) {
		y -= 18
		z = 1
	}
	x += locale_layout[loc] * 44
	y += yofs
	e.classList.toggle("lieutenant", is_upper)
	e.style.top = (y - 23) + "px"
	e.style.left = (x - 23) + "px"
	e.style.zIndex = z
	if (!is_upper)
		locale_layout[loc] ++
}

function layout_calendar() {
	for (let loc = 0; loc < 18; ++loc) {
		let [cx, cy] = calendar_xy[loc]
		let list = calendar_layout_service[loc]
		for (let i = 0; i < list.length; ++i) {
			let e = list[i]
			let x = cx, y = cy, z = 60 - i
			let d = 46 - 24
			if (loc === expand_calendar)
				d = 46
			if (loc === 0) {
				x += -6 + 46 * i
				z = 1
			} else if (loc === 17) {
				x += 60 - 46 * i
				z = 60 - i
			} else {
				x += (146 - 94 - 2)
				y += (227 - 46 - 2) - i * d
			}
			e.style.top = y + "px"
			e.style.left = x + "px"
			e.style.zIndex = z
		}

		list = calendar_layout_cylinder[loc]
		for (let i = 0; i < list.length; ++i) {
			let e = list[i]
			let x = cx, y = cy, z = 61
			if (loc === 0) {
				let k = calendar_layout_service[0].length
				if (k > 0)
					x += k * 46 + 46 + i * 46
				else
					x += 0 + i * 46
			} else if (loc === 17) {
				let k = calendar_layout_service[17].length
				if (k > 0)
					x += 60 - k * 46 - i * 46
				else
					x += 60 + i * 46
			} else if (loc === 1) {
				x += 46 + (i%2) * 46 + (i/2|0) * 12
				y += 66 + (i/2|0) * 36
			} else {
				x += 6 + (i%3) * 46 + (i/3|0) * 24
				y += 66 + (i/3|0) * 36
			}
			e.style.top = y + "px"
			e.style.left = x + "px"
			e.style.zIndex = z
		}
	}
}

function add_force(parent, type) {
	// TODO: reuse pool of elements?
	build_div(parent, "unit " + force_type_name[type], type)
}

function add_asset(parent, type, n, lord) {
	// TODO: reuse pool of elements?
	if (lord === VECHE) {
		if (view.actions && view.actions.veche_coin)
			build_div(parent, "action asset " + asset_type_name[type] + " x"+n, VECHE, on_click_veche_coin)
		else
			build_div(parent, "asset " + asset_type_name[type] + " x"+n)
	} else {
		if (is_asset_action(lord, asset_type_name[type]))
			build_div(parent, "action asset " + asset_type_name[type] + " x"+n, lord, on_click_asset[type])
		else
			build_div(parent, "asset " + asset_type_name[type] + " x"+n)
	}
}

function add_veche_vp(parent) {
	// TODO: reuse pool of elements?
	build_div(parent, "marker square conquered russian")
}

function update_forces(parent, forces) {
	parent.replaceChildren()
	for (let i = 0; i < force_type_count; ++i) {
		let n = pack4_get(forces, i)
		for (let k = 0; k < n; ++k) {
			add_force(parent, i)
		}
	}
}

function update_assets(id, parent, assets) {
	parent.replaceChildren()
	for (let i = 0; i < asset_type_count; ++i) {
		let n = pack4_get(assets, i)
		while (n >= 4) {
			add_asset(parent, i, 4, id)
			n -= 4
		}
		if (asset_type_x3[i]) {
			while (n >= 3) {
				add_asset(parent, i, 3, id)
				n -= 3
			}
		}
		while (n >= 2) {
			add_asset(parent, i, 2, id)
			n -= 2
		}
		while (n >= 1) {
			add_asset(parent, i, 1, id)
			n -= 1
		}
	}
}

function update_vassals(ready_parent, mustered_parent, lord_ix) {
	for (let v of data.lords[lord_ix].vassals) {
		let e = ui.vassal_service[v]
		if (is_vassal_ready(v)) {
			e.classList.remove("hide")
			ready_parent.appendChild(e)
		}
		else if (is_vassal_mustered(v)) {
			e.classList.remove("hide")
			mustered_parent.appendChild(e)
		}
		else {
			e.classList.add("hide")
		}
		e.classList.toggle("action", is_vassal_action(v))
	}
}

function update_lord_mat(ix) {
	update_assets(ix, ui.assets[ix], view.lords.assets[ix])
	update_vassals(ui.ready_vassals[ix], ui.mustered_vassals[ix], ix)
	update_forces(ui.forces[ix], view.lords.forces[ix])
	update_forces(ui.routed[ix], view.lords.routed[ix])
}

function is_lord_mat_selected(ix) {
	return ix === view.who
}

function is_cylinder_selected(ix) {
	if (view.who === undefined)
		return ix === view.command
	return ix === view.who || !!(view.group && set_has(view.group, ix))
}

function update_lord(ix) {
	let locale = view.lords.locale[ix]
	let service = view.lords.service[ix]
	if (locale < 0) {
		ui.lord_cylinder[ix].classList.add("hide")
		ui.lord_service[ix].classList.add("hide")
		ui.lord_mat[ix].classList.add("hide")
		ui.lord_mat[ix].classList.remove("action")
		return
	}
	if (locale < 100) {
		calendar_layout_service[service].push(ui.lord_service[ix])

		if (!is_lower_lord(ix)) {
			if (is_upper_lord(ix)) {
				let lo = get_lower_lord(ix)
				if (view.lords.locale[lo] === locale) {
					layout_locale_item(locale, ui.lord_cylinder[ix], 1)
					layout_locale_item(locale, ui.lord_cylinder[lo], 0)
				} else {
					layout_locale_item(locale, ui.lord_cylinder[ix], 0)
				}
			} else {
				layout_locale_item(locale, ui.lord_cylinder[ix], 0)
			}
		}

		ui.lord_cylinder[ix].classList.remove("hide")
		ui.lord_service[ix].classList.remove("hide")
		ui.lord_mat[ix].classList.remove("hide")
		update_lord_mat(ix)
	} else {
		calendar_layout_cylinder[locale - 100].push(ui.lord_cylinder[ix])
		ui.lord_cylinder[ix].classList.remove("hide")
		ui.lord_service[ix].classList.add("hide")
		ui.lord_mat[ix].classList.add("hide")
	}
	ui.lord_buttons[ix].classList.toggle("action", is_lord_action(ix))
	ui.lord_cylinder[ix].classList.toggle("action", is_lord_action(ix))
	ui.lord_service[ix].classList.toggle("action", is_service_action(ix))

	if (ix === LORD_HERMANN)
		ui.lord_cylinder[ix].classList.toggle("marshal", !is_lord_on_map(LORD_ANDREAS))
	if (ix === LORD_ANDREY)
		ui.lord_cylinder[ix].classList.toggle("marshal", !is_lord_on_map(LORD_ALEKSANDR))

	if (view.who === undefined)
		ui.lord_cylinder[ix].classList.toggle("selected", ix === view.command)
	else
		ui.lord_cylinder[ix].classList.toggle("selected", is_cylinder_selected(ix))
	ui.lord_mat[ix].classList.toggle("selected", is_lord_mat_selected(ix))
}

function update_legate() {
	if (view.nevsky.legate === LEGATE_INDISPOSED) {
		ui.legate.classList.add("hide")
	} else {
		ui.legate.classList.remove("hide")
		ui.legate.classList.toggle("action", is_legate_action())
		ui.legate.classList.toggle("selected", is_legate_selected())
		if (view.nevsky.legate === LEGATE_ARRIVED) {
			ui.legate.style.top = "1580px"
			ui.legate.style.left = "170px"
		} else {
			layout_locale_item(view.nevsky.legate, ui.legate, 0, -16)
		}
	}
}

function update_smerdi() {
	ui.smerdi.replaceChildren()
	for (let i = 0; i < view.nevsky.smerdi; ++i)
		build_div(ui.smerdi, "unit serfs")
}

function update_veche() {
	ui.veche.replaceChildren()

	let n = view.nevsky.veche_coin
	while (n >= 3) {
		add_asset(ui.veche, COIN, 3, VECHE)
		n -= 3
	}
	while (n >= 2) {
		add_asset(ui.veche, COIN, 2, VECHE)
		n -= 2
	}
	while (n >= 1) {
		add_asset(ui.veche, COIN, 1, VECHE)
		n -= 1
	}

	for (let i = 0; i < view.nevsky.veche_vp; ++i)
		add_veche_vp(ui.veche)
}

function update_locale(loc) {
	ui.locale[loc].classList.toggle("action", is_locale_action(loc) || is_laden_march_action(loc))
	ui.locale[loc].classList.toggle("laden", is_laden_march_action(loc))
	if (ui.locale_extra[loc]) {
		ui.locale_extra[loc].classList.toggle("action", is_locale_action(loc) || is_laden_march_action(loc))
		ui.locale_extra[loc].classList.toggle("laden", is_laden_march_action(loc))
	}

	ui.locale_markers[loc].replaceChildren()

	if (set_has(view.locales.ravaged, loc)) {
		let cn
		if (is_p1_locale(loc))
			cn = "marker small ravaged russian"
		else
			cn = "marker small ravaged teutonic"
		ui.locale_markers[loc].appendChild(get_cached_element(cn))
	}

	if (set_has(view.locales.conquered, loc)) {
		let cn
		if (is_p1_locale(loc))
			cn = "marker square conquered russian"
		else
			cn = "marker square conquered teutonic"
		for (let i = 0; i < data.locales[loc].vp; ++i)
			ui.locale_markers[loc].appendChild(get_cached_element(cn))
	}

	if (set_has(view.locales.castles1, loc)) {
		let cn = "marker rectangle castle teutonic"
		ui.locale_markers[loc].appendChild(get_cached_element(cn))
	}

	if (set_has(view.locales.castles2, loc)) {
		let cn = "marker rectangle castle russian"
		ui.locale_markers[loc].appendChild(get_cached_element(cn))
	}

	if (set_has(view.locales.walls, loc)) {
		let cn = "marker square walls"
		ui.locale_markers[loc].appendChild(get_cached_element(cn))
	}

	let sieges = map_get(view.locales.sieges, loc)
	if (sieges > 0) {
		let cn
		if (is_p1_locale(loc))
			cn = "marker square siege russian"
		else
			cn = "marker square siege teutonic"
		for (let i = 0; i < sieges; ++i)
			ui.locale_markers[loc].appendChild(get_cached_element(cn))
	}
}

function update_plan() {
	if (view.plan) {
		ui.plan_dialog.classList.remove("hide")
		for (let i = 0; i < 6; ++i) {
			if (i < view.plan.length) {
				let lord = view.plan[i]
				if (lord < 0) {
					if (player === "Teutons")
						ui.plan_list_cards[i].className = "card teutonic cc_pass"
					else
						ui.plan_list_cards[i].className = "card russian cc_pass"
				} else {
					if (lord < 6)
						ui.plan_list_cards[i].className = "card teutonic cc_lord_" + lord
					else
						ui.plan_list_cards[i].className = "card russian cc_lord_" + lord
				}
			} else if (i < max_plan_length()) {
				if (player === "Teutons")
					ui.plan_list_cards[i].className = "card teutonic cc_back"
				else
					ui.plan_list_cards[i].className = "card russian cc_back"
			} else {
				ui.plan_list_cards[i].className = "hide"
			}
		}
		for (let lord = 0; lord < 12; ++lord) {
			if (is_plan_action(lord)) {
				ui.plan_action_cards[lord].classList.add("action")
				ui.plan_action_cards[lord].classList.remove("disabled")
			} else {
				ui.plan_action_cards[lord].classList.remove("action")
				ui.plan_action_cards[lord].classList.add("disabled")
			}
		}
		if (is_plan_action(-1)) {
			ui.plan_action_pass_p1.classList.add("action")
			ui.plan_action_pass_p1.classList.remove("disabled")
			ui.plan_action_pass_p2.classList.add("action")
			ui.plan_action_pass_p2.classList.remove("disabled")
		} else {
			ui.plan_action_pass_p1.classList.remove("action")
			ui.plan_action_pass_p1.classList.add("disabled")
			ui.plan_action_pass_p2.classList.remove("action")
			ui.plan_action_pass_p2.classList.add("disabled")
		}
	} else {
		ui.plan_dialog.classList.add("hide")
	}
}

function update_cards() {
	if (Array.isArray(view.show_arts_of_war)) {
		ui.arts_of_war_dialog.classList.remove("hide")
		ui.arts_of_war_list.replaceChildren()
		for (let c of view.show_arts_of_war) {
			let elt = ui.cards[c]
			ui.arts_of_war_list.appendChild(ui.cards[c])
			elt.classList.toggle("selected", c === view.what)
			elt.classList.toggle("action", is_card_action(c))
			elt.classList.remove("disabled")
		}
	} else if (view.show_arts_of_war === 1) {
		ui.arts_of_war_dialog.classList.remove("hide")
		ui.arts_of_war_list.replaceChildren()
		for_each_friendly_card(c => {
			if (!is_card_in_use(c)) {
				let elt = ui.cards[c]
				ui.arts_of_war_list.appendChild(elt)
				elt.classList.toggle("selected", c === view.what)
				elt.classList.toggle("action", is_card_action(c))
				elt.classList.toggle("disabled", !is_card_action(c))
			}
		})
	} else {
		ui.arts_of_war_dialog.classList.add("hide")
		for (let c = 0; c < 42; ++c) {
			let elt = ui.cards[c]
			elt.classList.toggle("selected", false)
			elt.classList.toggle("action", is_card_action(c))
			elt.classList.toggle("disabled", false)
		}
	}

	ui.events.replaceChildren()
	for (let c of view.events)
		ui.events.appendChild(ui.cards[c])

	ui.capabilities1.replaceChildren()
	for_each_teutonic_card(c => {
		if (view.capabilities.includes(c))
			ui.capabilities1.appendChild(ui.cards[c])
	})

	ui.capabilities2.replaceChildren()
	for_each_russian_card(c => {
		if (view.capabilities.includes(c))
			ui.capabilities2.appendChild(ui.cards[c])
	})

	ui.hand.replaceChildren()
	if (view.hand) {
		for (let c of view.hand)
			ui.hand.appendChild(ui.cards[c])
	}

	for (let ix = 0; ix < data.lords.length; ++ix) {
		let side = ix < 6 ? "teutonic" : "russian"
		ui.lord_capabilities[ix].replaceChildren()
		let c = view.lords.capabilities[(ix << 1) + 0]
		if (c >= 0)
			ui.lord_capabilities[ix].appendChild(ui.cards[c])
		c = view.lords.capabilities[(ix << 1) + 1]
		if (c >= 0)
			ui.lord_capabilities[ix].appendChild(ui.cards[c])
	}
}

function on_update() {
	restart_cache()

	locale_layout.fill(0)
	for (let i = 0; i < 18; ++i) {
		calendar_layout_cylinder[i] = []
		calendar_layout_service[i] = []
	}

	for (let ix = 0; ix < data.lords.length; ++ix) {
		if (view.lords.locale[ix] < 0) {
			ui.lord_cylinder[ix].classList.add("hide")
			ui.lord_service[ix].classList.add("hide")
			ui.lord_mat[ix].classList.add("hide")
		} else {
			ui.lord_cylinder[ix].classList.remove("hide")
			update_lord(ix)
		}
	}

	for (let loc = 0; loc < data.locales.length; ++loc)
		update_locale(loc)

	for (let way = 0; way < ui.ways.length; ++way) {
		if (is_way_action(way))
			ui.ways[way].classList.add("action")
		else
			ui.ways[way].classList.remove("action")
	}

	layout_calendar()

	update_legate()
	update_smerdi()
	update_veche()

	update_current_card_display()

	if (view.turn & 1)
		ui.turn.className = `marker circle turn campaign t${view.turn>>1}`
	else
		ui.turn.className = `marker circle turn levy t${view.turn>>1}`

	let vp1 = count_vp1()
	let vp2 = count_vp2()
	if ((vp1 >> 1) === (vp2 >> 1)) {
		if (vp1 & 1)
			ui.vp1.className = `marker circle victory teutonic stack v${vp1>>1} half`
		else
			ui.vp1.className = `marker circle victory teutonic stack v${vp1>>1}`
		if (vp2 & 1)
			ui.vp2.className = `marker circle victory russian stack v${vp2>>1} half`
		else
			ui.vp2.className = `marker circle victory russian stack v${vp2>>1}`
	} else {
		if (vp1 & 1)
			ui.vp1.className = `marker circle victory teutonic v${vp1>>1} half`
		else
			ui.vp1.className = `marker circle victory teutonic v${vp1>>1}`
		if (vp2 & 1)
			ui.vp2.className = `marker circle victory russian v${vp2>>1} half`
		else
			ui.vp2.className = `marker circle victory russian v${vp2>>1}`
	}

	update_plan()
	update_cards()

	action_button("use_legate", "Legate")

	action_button("stonemasons", "Stonemasons")
	action_button("stone_kremlin", "Stone Kremlin")
	action_button("smerdi", "Smerdi")

	action_button("withdraw", "Withdraw")
	action_button("end_withdraw", "End withdraw")

	action_button("surrender", "Surrender")
	action_button("siegeworks", "Siegeworks")

	action_button("march", "March")
	action_button("sail", "Sail")
	action_button("siege", "Siege")
	action_button("storm", "Storm")
	action_button("sally", "Sally")
	action_button("supply", "Supply")
	action_button("forage", "Forage")
	action_button("ravage", "Ravage")
	action_button("tax", "Tax")

	action_button("ship", "Ship")
	action_button("boat", "Boat")
	action_button("cart", "Cart")
	action_button("sled", "Sled")

	action_button("capability", "Capability")

	action_button("deploy", "Deploy")
	action_button("discard", "Discard")
	action_button("hold", "Hold")
	action_button("play", "Play")

	action_button("end_actions", "End actions")
	action_button("end_avoid_battle", "End avoid battle")
	action_button("end_disband", "End disband")
	action_button("end_feed", "End feed")
	action_button("end_levy", "End levy")
	action_button("end_muster", "End muster")
	action_button("end_pay", "End pay")
	action_button("end_plan", "End plan")
	action_button("end_setup", "End setup")
	action_button("end_spoils", "End spoils")
	action_button("end_supply", "End supply")

	action_button("pass", "Pass")
	action_button("done", "Done")
	action_button("undo", "Undo")
}

function build_div(parent, className, id, onclick) {
	let e = document.createElement("div")
	e.className = className
	if (onclick) {
		e.my_id = id
		e.addEventListener("mousedown", onclick)
	}
	parent.appendChild(e)
	return e
}

function build_lord_mat(lord, ix, side, name) {
	let parent = document.getElementById(side === 'teutonic' ? "court1" : "court2")
	let mat = build_div(parent, `mat ${side} ${name} hide`)
	let bg = build_div(mat, "background")
	ui.forces[ix] = build_div(bg, "forces")
	ui.routed[ix] = build_div(bg, "routed")
	ui.assets[ix] = build_div(bg, "assets")
	ui.ready_vassals[ix] = build_div(bg, "ready_vassals")
	ui.mustered_vassals[ix] = build_div(bg, "mustered_vassals")
	ui.lord_buttons[ix] = build_div(bg, "shield", ix, on_click_cylinder)
	ui.lord_capabilities[ix] = build_div(mat, "capabilities")
	ui.lord_mat[ix] = mat
}

function build_card(side, c) {
	let card = ui.cards[c] = document.createElement("div")
	card.className = `card ${side} aow_${c}`
	card.my_id = c
	card.addEventListener("mousedown", on_click_card)
}

function build_plan() {
	let elt
	for (let i = 0; i < 6; ++i) {
		elt = document.createElement("div")
		elt.className = "hide"
		ui.plan_list_cards.push(elt)
		ui.plan_list.appendChild(elt)
	}
	for (let lord = 0; lord < 12; ++lord) {
		let side = lord < 6 ? "teutonic" : "russian"
		elt = document.createElement("div")
		elt.className = `card ${side} cc_lord_${lord}`
		elt.my_id = lord
		elt.addEventListener("mousedown", on_click_plan)
		ui.plan_action_cards.push(elt)
		ui.plan_actions.appendChild(elt)
	}

	ui.plan_action_pass_p1 = elt = document.createElement("div")
	elt.className = `card teutonic cc_pass`
	elt.my_id = -1
	elt.addEventListener("mousedown", on_click_plan)
	ui.plan_actions.appendChild(elt)

	ui.plan_action_pass_p2 = elt = document.createElement("div")
	elt.className = `card russian cc_pass`
	elt.my_id = -1
	elt.addEventListener("mousedown", on_click_plan)
	ui.plan_actions.appendChild(elt)
}

function build_way(name, sel) {
	let way = data.ways.findIndex(w => w.name === name)
	ui.ways[way] = document.querySelector(sel)
	ui.ways[way].my_id = way
	ui.ways[way].addEventListener("mousedown", on_click_way)
}

function build_map() {
	data.locales.forEach((locale, ix) => {
		let e = ui.locale[ix] = document.createElement("div")
		let region = clean_name(locale.region)
		e.className = "locale " + locale.type + " " + region
		let x = round(locale.box.x * MAP_DPI / 300)
		let y = round(locale.box.y * MAP_DPI / 300)
		let w = floor((locale.box.x+locale.box.w) * MAP_DPI / 300) - x
		let h = floor((locale.box.y+locale.box.h) * MAP_DPI / 300) - y
		if (locale.type === 'town') {
			locale_xy[ix] = [ round(x + w / 2), y - 24 ]
			x -= 11
			y -= 5
			w += 16
			h += 5
		} else if (locale.type === 'region') {
			locale_xy[ix] = [ round(x + w / 2), round(y + h / 2) - 32 ]
			x -= 3
			y -= 4
			// XXX
			x -= 8
			y -= 6
			w += 16
			h += 12

		} else {
			locale_xy[ix] = [ round(x + w / 2), y - 36 ]
			x -= 2
			y -= 2
			w -= 2
			h -= 2
		}
		e.style.left = x + "px"
		e.style.top = y + "px"
		e.style.width = w + "px"
		e.style.height = h + "px"
		e.my_id = ix
		e.addEventListener("mousedown", on_click_locale)
		e.addEventListener("mouseenter", on_focus_locale)
		e.addEventListener("mouseleave", on_blur)
		document.getElementById("locales").appendChild(e)

		if (locale.type !== 'region') {
			e = ui.locale_extra[ix] = document.createElement("div")
			e.className = "locale_extra " + locale.type + " " + region
			let cx = x + (w >> 1) + 4
			let ew = extra_size[locale.type][0]
			let eh = extra_size[locale.type][1]
			e.style.top = (y - eh) + "px"
			e.style.left = (cx - ew/2) + "px"
			e.style.width = ew + "px"
			e.style.height = eh + "px"
			e.my_id = ix
			e.addEventListener("mousedown", on_click_locale)
			e.addEventListener("mouseenter", on_focus_locale)
			e.addEventListener("mouseleave", on_blur)
			document.getElementById("locales").appendChild(e)
		}

		e = ui.locale_markers[ix] = document.createElement("div")
		e.className = "locale_markers " + locale.type + " " + region
		x = (locale.box.x + locale.box.w / 2) * MAP_DPI / 300 - 196/2
		y = (locale.box.y + locale.box.h * 0) * MAP_DPI / 300 - 8
		e.style.top = y + "px"
		e.style.left = x + "px"
		e.style.width = 196 + "px"
		document.getElementById("pieces").appendChild(e)
	})

	let x = 160
	let y = 2740
	data.lords.forEach((lord, ix) => {
		let e = ui.lord_cylinder[ix] = document.createElement("div")
		e.className = "cylinder lord " + clean_name(lord.side) + " " + clean_name(lord.name) + " hide"
		e.my_id = ix
		e.addEventListener("mousedown", on_click_cylinder)
		e.addEventListener("mouseenter", on_focus_cylinder)
		e.addEventListener("mouseleave", on_blur)
		document.getElementById("pieces").appendChild(e)

		e = ui.lord_service[ix] = document.createElement("div")
		e.className = "service_marker lord image" + lord.image + " " + clean_name(lord.side) + " " + clean_name(lord.name) + " hide"
		e.my_id = ix
		e.addEventListener("mousedown", on_click_lord_service_marker)
		e.addEventListener("mouseenter", on_focus_lord_service_marker)
		e.addEventListener("mouseleave", on_blur_lord_service_marker)
		document.getElementById("pieces").appendChild(e)

		build_lord_mat(lord, ix, clean_name(lord.side), clean_name(lord.name))

		x += 70
	})

	data.vassals.forEach((vassal, ix) => {
		let lord = data.lords[vassal.lord]
		let e = ui.vassal_service[ix] = document.createElement("div")
		e.className = "service_marker vassal image" + vassal.image + " " + clean_name(lord.side) + " " + clean_name(vassal.name) + " hide"
		e.my_id = ix
		e.addEventListener("mousedown", on_click_vassal_service_marker)
		e.addEventListener("mouseenter", on_focus_vassal_service_marker)
		e.addEventListener("mouseleave", on_blur)
		document.getElementById("pieces").appendChild(e)
	})

	document.getElementById("legate").addEventListener("mousedown", on_click_legate)

	for (let name in original_boxes) {
		let x = round(original_boxes[name][0] * MAP_DPI / 300)
		let y = round(original_boxes[name][1] * MAP_DPI / 300)
		let w = round(original_boxes[name][2] * MAP_DPI / 300) - 8
		let h = round(original_boxes[name][3] * MAP_DPI / 300) - 8
		let e = ui.boxes[name] = document.createElement("div")
		e.className = "box " + name
		e.style.left = x + "px"
		e.style.top = y + "px"
		e.style.width = w + "px"
		e.style.height = h + "px"
		document.getElementById("boxes").appendChild(e)
	}

	build_way("Crossroads", ".way.crossroads")
	build_way("Peipus E", ".way.peipus-east")
	build_way("Peipus W", ".way.peipus-north")
	build_way("Wirz", ".way.wirz")

	build_plan()

	for (let c = 0; c < 21; ++c)
		build_card("teutonic", c)
	for (let c = 21; c < 42; ++c)
		build_card("russian", c)
}

build_map()
// drag_element_with_mouse("#battle", "#battle_header")
drag_element_with_mouse("#arts_of_war", "#arts_of_war_header")
scroll_with_middle_mouse("main")