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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
NSEW_pairs = sorted_pairs -- todo: make an iterator that explicitly visits them in a predefined order
-- EnumVolumes([class, ] { box, | obj, | point, | x, y | x, y, z, }) - returns an array of the volumes interesecting the box/point
-- EnumVolumes(..., "smallest") - returns the smallest volume (by surface) interesecting the box/point
-- EnumVolumes(..., filter, ...) - returns an array with all volumes interesecting the box/point for which filter(volume, ...) returned true
local gofPermanent = const.gofPermanent

if FirstLoad then
	GedRoomEditor = false
	GedRoomEditorObjList = false
	g_RoomCornerTaskList = {}
	SelectedVolume = false
	VolumeCollisonEnabled = false
	HideFloorsAboveThisOne = false
	--moves wall slabs on load to their expected pos
	--it seems that lvl designers commonly misplace wall slabs which causes weird glitches, this should fix that
	RepositionWallSlabsOnLoad = Platform.developer or false
end

function VolumeStructuresList()
	local list = {""}
	EnumVolumes(function (volume, list, find)
		if not find(list, volume.structure) then
			list[#list + 1] = volume.structure
		end
	end, list, table.find)
	table.sort(list)
	return list
end

-- the map is considered in bottom-right quadrant, which means that (0, 0) is north, west
local noneWallMat = const.SlabNoMaterial
local defaultWallMat = "default"

DefineClass.Volume = {
	__parents = { "RoomRoof", "StripObjectProperties", "AlignedObj", "ComponentAttach", "EditorVisibleObject" },
	flags = { gofPermanent = true, cofComponentVolume = true, efVisible = true },
	
	properties = {
		{ category = "Not Room Specific", id = "volumeCollisionEnabled",  name = "Toggle Global Volume Collision", default = true, editor = "bool", dont_save = true },
		{ category = "Not Room Specific", id = "buttons3", name = "Buttons", editor = "buttons", default = false, dont_save = true, read_only = true,
			buttons = {
				{name = "Recreate All Walls", func = "RecreateAllWallsOnMap"},
				{name = "Recreate All Roofs", func = "RecreateAllRoofsOnMap"},
				{name = "Recreate All Floors", func = "RecreateAllFloorsOnMap"},
			},
		},
		{ category = "General", id = "buttons2", name = "Buttons", editor = "buttons", default = false, dont_save = true, read_only = true,
			buttons = {
				{name = "Recreate Walls", func = "RecreateWalls"},
				{name = "Recreate Floor", func = "RecreateFloor"},
				{name = "Recreate Roof", func = "RecreateRoofBtn"},
				{name = "Re Randomize", func = "ReRandomize"},
				{name = "Copy Above", func = "CopyAbove"},
				{name = "Copy Below", func = "CopyBelow"},
			},
		},
		{ category = "General", id = "buttons2row2", name = "Buttons", editor = "buttons", default = false, dont_save = true, read_only = true,
			buttons = {
				{name = "Lock Subvariants", func = "LockAllSlabsToCurrentSubvariants"},
				{name = "Unlock Subvariants", func = "UnlockAllSlabs"},
				{name = "Make Slabs Vulnerable", func = "MakeOwnedSlabsVulnerable"},
				{name = "Make Slabs Invulnerable", func = "MakeOwnedSlabsInvulnerable"},
			},
		},
		
		{ category = "General", id = "box", name = "Box", editor = "box", default = false, no_edit = true },
		{ category = "General", id = "locked_slabs_count", name = "Locked Slabs Count", editor = "text", default = "", read_only = true, dont_save = true},
		{ category = "General", id = "wireframe_visible", name = "Wireframe Visible", editor = "bool", default = false,},
		{ category = "General", id = "wall_text_markers_visible", name = "Wall Text ID Visible", editor = "bool", default = false, dont_save = true},
		{ category = "General", id = "dont_use_interior_lighting", name = "No Interior Lighting", editor = "bool", default = false, },
		{ category = "General", id = "seed", name = "Random Seed", editor = "number", default = false},
		{ category = "General", id = "floor", name = "Floor", editor = "number", default = 1, min = -9, max = 99 },
		--bottom left corner of room in world coords
		{ category = "General", id = "position", name = "Position", editor = "point", default = false, no_edit = true, },
		{ category = "General", id = "size", name = "Size", editor = "point", default = point30, no_edit = true, },
		
		{ category = "General", id = "override_terrain_z", editor = "number", default = false, no_edit = true },
		{ category = "General", id = "structure", name = "Structure", editor = "combo", default = "", items = VolumeStructuresList },
		
		{ category = "Roof", id = "room_is_roof", name = "Room is Roof", editor = "bool", default = false, help = "Mark room as roof, roofs are hidden entirely (all walls, floors, etc.) when their floor is touched. Rooms that have zero height are considered roofs by default."},
	},
	
	wireframeColor = RGB(100, 100, 100),
	lines = false,
	
	adjacent_rooms = false, --{ ? }
	
	text_markers = false,
	
	last_wall_recreate_seed = false,
	building = false, --{ [floor] = {room, room, room} }
	
	being_placed = false,
	enable_collision = true, --with other rooms
	
	EditorView = Untranslated("<opt(u(structure),'',' - ')><u(name)>"),
	
	light_vol_obj = false,
	entity = "InvisibleObject",
	editor_force_excluded = true, -- exclude from Map editor palette (causes crash)
}

local voxelSizeX = const.SlabSizeX or 0
local voxelSizeY = const.SlabSizeY or 0
local voxelSizeZ = const.SlabSizeZ or 0
local halfVoxelSizeX = voxelSizeX / 2
local halfVoxelSizeY = voxelSizeY / 2
local halfVoxelSizeZ = voxelSizeZ / 2
local InvalidZ = const.InvalidZ
maxRoomVoxelSizeX = const.MaxRoomVoxelSizeX or 40
maxRoomVoxelSizeY = const.MaxRoomVoxelSizeY or 40
maxRoomVoxelSizeZ = const.MaxRoomVoxelSizeZ or 40
roomQueryRadius = Max((maxRoomVoxelSizeX + 1) * voxelSizeX, (maxRoomVoxelSizeY + 1) * voxelSizeY, (maxRoomVoxelSizeZ + 1) * voxelSizeZ)
defaultVolumeVoxelHeight = 4

local halfVoxelPtZeroZ = point(halfVoxelSizeX, halfVoxelSizeY, 0)
local halfVoxelPt = point(halfVoxelSizeX, halfVoxelSizeY, halfVoxelSizeZ)
function SnapVolumePos(pos)
	return SnapToVoxel(pos) - halfVoxelPtZeroZ
end

function snapZRound(z)
	z = (z + halfVoxelSizeZ) / voxelSizeZ
	return z * voxelSizeZ
end

function snapZCeil(z)
	z = DivCeil(z, voxelSizeZ)
	return z * voxelSizeZ
end

function snapZ(z)
	z = z / voxelSizeZ
	return z * voxelSizeZ
end

function Volume:Getlocked_slabs_count()
	return "N/A"
end

function Volume:GetvolumeCollisionEnabled()
	return VolumeCollisonEnabled
end

function Volume:Setroom_is_roof(val)
	if self.room_is_roof == val then return end
	self.room_is_roof = val
	ComputeSlabVisibilityInBox(self.box)
end

function Volume:IsRoofOnly()
	return self.room_is_roof or self.size:z() == 0
end

function Volume:SetvolumeCollisionEnabled(v)
	VolumeCollisonEnabled = v
end

function Volume:Init()
	--todo: if platform.somethingorother,
	self.text_markers = {
		North = PlaceObject("Text"),
		South = PlaceObject("Text"),
		West = PlaceObject("Text"),
		East = PlaceObject("Text"),
	}
	
	for k, v in pairs(self.text_markers) do
		v.hide_in_editor = false
		v:SetText(k)
		v:SetColor(RGB(255, 0, 0))
		v:SetGameFlags(const.gofDetailClass1)
		v:ClearGameFlags(const.gofDetailClass0)
	end
	
	self:SetPosMarkersVisible(self.wall_text_markers_visible)
	self:CopyBoxToCCD()
	self:InitEntity()
end

function Volume:InitEntity()
	if IsEditorActive() then
		self:ChangeEntity("RoomHelper")
	end
end

function Volume:EditorEnter()
	--dont mess with visibility flags
	self:ChangeEntity("RoomHelper")
end

function Volume:EditorExit()
	--dont mess with visibility flags
	self:ChangeEntity("InvisibleObject")
end

function Volume:Setdont_use_interior_lighting(val)
	self.dont_use_interior_lighting = val
	self:UpdateInteriorLighting()
end

function Volume:CopyBoxToCCD()
	local box = self.box
	if not box then return end
	SetVolumeBox(self, box)
	self:UpdateInteriorLighting()
end

function Volume:UpdateInteriorLighting()
	local box = self.box
	if not box then
		return
	end
	
	if self.dont_use_interior_lighting then
		DoneObject(self.light_vol_obj)
		self.light_vol_obj = nil
		return
	end
	
	local lo = self.light_vol_obj
	if not IsValid(lo) then
		lo = PlaceObject("ComponentLight")
		lo:SetLightType(const.eLightTypeClusterVolume)
		self.light_vol_obj = lo
	end
	
	if not self.dont_use_interior_lighting and self.floor_mat == noneWallMat and self.floor == 1 then
		lo:SetClusterVolumeBox(box:minx(), box:miny(), box:minz() - 100, box:maxx(), box:maxy(), box:maxz()) --this is here so vfx can properly catch ground below rooms
	else
		lo:SetClusterVolumeBox(box:minx(), box:miny(), box:minz(), box:maxx(), box:maxy(), box:maxz())
	end
	lo:SetVolumeId(self.handle)
	lo:SetPos(self:GetPos())
end

function Volume:CalcZ()
	local posZ = self.position:z()
	
	if self.being_placed then
		local z = self.override_terrain_z or terrain.GetHeight(self.position)
		posZ = snapZ(z + voxelSizeZ / 2)
		self.position = self.position:SetZ(posZ)
	end
	
	if posZ == nil then
		--save compat
		local z = self.override_terrain_z or terrain.GetHeight(self.position)
		z = snapZ(z + voxelSizeZ / 2)
		posZ = (rawget(self, "z_offset") or 0) * voxelSizeZ + z + (self.floor - 1) * self.size:z() * voxelSizeZ
		self.position = self.position:SetZ(posZ)
	end

	return posZ
end

function Volume:LockToCurrentTerrainZ()
	self.override_terrain_z = terrain.GetHeight(self.position)
	return self.override_terrain_z
end

function Volume:CalcSnappedZ()
	local z = self:CalcZ()
	z = z / voxelSizeZ
	z = z * voxelSizeZ
	return z
end

function FloorFromZ(z, roomHeight, ground_level)
	return (z - ground_level) / (roomHeight * voxelSizeZ) + 1
end

function ZFromFloor(f, roomHeight, ground_level) --essentialy calcz but makes assumptions about floor size
	return ground_level + (f - 1) * (roomHeight * voxelSizeZ)
end

function Volume:Move(pos)
	--bottom left corner of the voxel at pos will be the new positon
	self.position = SnapVolumePos(pos)
	self:AlignObj()
end

function Volume:ChangeFloor(newFloor)
	if self.floor == newFloor then
		return
	end
	
	self.floor = newFloor
	self:AlignObj()
end

function Volume:SetSize(newSize)
	if self.size == newSize then
		return
	end
	
	self.size = newSize
	self:AlignObj()
end

function Volume:AlignObj(pos, angle)
	if pos then
		local v = pos - self:GetPos()
		self.position = SnapVolumePos(self.position + v)
	end
	self:InternalAlignObj()
end

function Volume:InternalAlignObj(test)
	local w, h, d = self.size:x() * voxelSizeX, self.size:y() * voxelSizeY, self.size:z() * voxelSizeZ
	local cx, cy = w / 2, h / 2
	local z = self:CalcZ()
	local pos = point(self.position:x() + cx, self.position:y() + cy, z)
	local p = self.position
	local newBox = box(p:x(), p:y(), z, p:x() + w, p:y() + h, z + d)
	if not test and self:GetPos() == pos and self.box == newBox then return p end --nothing to align
	self:SetPos(pos)
	self:SetAngle(0)
	self.box = newBox
	if not test then
		self:FinishAlign()
	end
	return p
end

function GetOppositeSide(side)
	if side == "North" then return "South"
	elseif side == "South" then return "North"
	elseif side == "West" then return "East"
	elseif side == "East" then return "West" 
	end
end

local function GetOppositeCorner(c)
	if c == "NW" then return "SE"
	elseif c == "NE" then return "SW"
	elseif c == "SW" then return "NE"
	elseif c == "SE" then return "NW"
	end
end

local function SetAdjacentRoom(adjacent_rooms, room, data)
	if not adjacent_rooms then
		return
	end
	if data then
		if not adjacent_rooms[room] then
			adjacent_rooms[#adjacent_rooms + 1] = room
		end
		adjacent_rooms[room] = data
		return data
	end
	data = adjacent_rooms[room]
	if data then
		adjacent_rooms[room] = nil
		table.remove_value(adjacent_rooms, room)
		return data
	end
end

function Volume:ClearAdjacencyData()
	local adjacent_rooms = self.adjacent_rooms
	self.adjacent_rooms = nil
	
	for _, room in ipairs(adjacent_rooms or empty_table) do
		local hisData = SetAdjacentRoom(room.adjacent_rooms, self, false)
		if hisData then
			local hisAW = hisData[2]
			for i = 1, #(hisAW or empty_table) do
				room:OnAdjacencyChanged(hisAW[i])
			end
		end
	end
end


local AdjacencyEvents = {}
function Volume:RebuildAdjacencyData()
	-- must be called inside an undo op, otherwise delayed updating may cause changes not captured by undo
	assert(XEditorUndo:AssertOpCapture())
	
	local adjacent_rooms = self.adjacent_rooms
	local new_adjacent_rooms = {}
	local mb = self.box
	local events = {}
	--DbgAddBox(mb)
	local is_permanent = self:GetGameFlags(gofPermanent) ~= 0
	local gameFlags = is_permanent and gofPermanent or nil
	MapForEach(self, roomQueryRadius, self.class, nil, nil, gameFlags, function(o, mb, is_permanent)
		if o == self or not is_permanent and o:GetGameFlags(gofPermanent) ~= 0 then
			return
		end
		
		--TODO: use +roof box for sides, -roof box for ceiling/floor
		local hb = o.box
		--DbgAddBox(hb, RGB(0, 255, 0))
		local ib = IntersectRects(hb, mb)
		--DbgAddBox(ib, RGB(255, 0, 0))
		if not ib:IsValid() then
			return
		end
		local myData = adjacent_rooms and adjacent_rooms[o]
		local oldIb = myData and myData[1]
		
		local myNewData = {}
		local hisData = o.adjacent_rooms and o.adjacent_rooms[self]
		local hisNewData = {}
		
		--restore previously affected walls
		local myaw = myData and myData[2]
		local hisaw = hisData and hisData[2]
		for i = 1, #(myaw or empty_table) do
			table.insert(events, {self, myaw[i]})
		end
		for i = 1, #(hisaw or empty_table) do
			table.insert(events, {o, hisaw[i]})
		end
		
		hisNewData[1] = ib
		myNewData[1] = ib
		hisNewData[2] = {}
		myNewData[2] = {}
		
		if ib:sizez() > 0 then
			if ib:minx() == ib:maxx() and ib:miny() == ib:maxy() then
				--corner adj, rebuild corner
				local p = ib:min()
				if p:x() == mb:minx() then --west
					if p:y() == mb:miny() then --north
						table.insert(events, {self, "NW"})
						table.insert(hisNewData[2], "SE")
						table.insert(myNewData[2], "NW")
					else
						table.insert(events, {self, "SW"})
						table.insert(hisNewData[2], "NE")
						table.insert(myNewData[2], "SW")
					end
				else --east
					if p:y() == mb:miny() then
						table.insert(events, {self, "NE"})
						table.insert(hisNewData[2], "SW")
						table.insert(myNewData[2], "NE")
					else
						table.insert(events, {self, "SE"})
						table.insert(hisNewData[2], "NW")
						table.insert(myNewData[2], "SE")
					end
				end
			elseif ib:minx() == ib:maxx()
					and ib:miny() ~= ib:maxy() then
				--east/west adjacency
				if mb:maxx() == ib:maxx() then
					--my east, his west
					table.insert(events, {self, "East"})
					table.insert(events, {o, "West"})
					table.insert(hisNewData[2], "West")
					table.insert(myNewData[2], "East")
				else
					--my west, his east
					table.insert(events, {self, "West"})
					table.insert(events, {o, "East"})
					table.insert(hisNewData[2], "East")
					table.insert(myNewData[2], "West")
				end
			elseif ib:minx() ~= ib:maxx()
					and ib:miny() == ib:maxy() then
				--nort/south adjacency
				if mb:maxy() == ib:maxy() then
					--my north, his south
					table.insert(events, {self, "South"})
					table.insert(events, {o, "North"})
					table.insert(hisNewData[2], "North")
					table.insert(myNewData[2], "South")
				else
					--my south, his north
					table.insert(events, {self, "North"})
					table.insert(events, {o, "South"})
					table.insert(hisNewData[2], "South")
					table.insert(myNewData[2], "North")
				end
			else
				--rooms intersect
				if (ib:maxx() == mb:maxx() or ib:minx() == mb:maxx())
						and mb:maxx() == hb:maxx() then
					--east
					table.insert(events, {self, "East"})
					table.insert(myNewData[2], "East")
					table.insert(hisNewData[2], "East")
				end
				
				if (ib:minx() == mb:minx() or ib:maxx() == mb:minx())
					and mb:minx() == hb:minx() then
					--west
					table.insert(events, {self, "West"})
					table.insert(myNewData[2], "West")
					table.insert(hisNewData[2], "West")
				end
				
				if (ib:maxy() == mb:maxy() or ib:miny() == mb:maxy())
					and mb:maxy() == hb:maxy() then
					--south
					table.insert(events, {self, "South"})
					table.insert(myNewData[2], "South")
					table.insert(hisNewData[2], "South")
				end
				
				if ib:maxy() == mb:miny() or ib:miny() == mb:miny() 
					and mb:miny() == hb:miny() then
					--north
					table.insert(events, {self, "North"})
					table.insert(myNewData[2], "North")
					table.insert(hisNewData[2], "North")
				end
			end
		end
		
		
		if ib:sizex() > 0 and ib:sizey() > 0 then
			if (mb:minz() >= ib:minz() and  mb:minz() <= ib:maxz()) or
				(hb:maxz() >= ib:minz() and hb:maxz() <= ib:maxz()) then
				--floor
				table.insert(events, {self, "Floor"})
				table.insert(myNewData[2], "Floor")
				table.insert(hisNewData[2], "Roof")
			end
			
			if (mb:maxz() <= ib:maxz() and mb:maxz() >= ib:minz()) or
				(hb:minz() <= ib:maxz() and hb:minz() >= ib:minz()) then
				--roof
				table.insert(events, {self, "Roof"})
				table.insert(myNewData[2], "Roof")
				table.insert(hisNewData[2], "Floor")
			end
		end
		
		SetAdjacentRoom(o.adjacent_rooms, self, #hisNewData[2] > 0 and hisNewData)
		SetAdjacentRoom(new_adjacent_rooms, o, #myNewData[2] > 0 and myNewData)
	end, mb, is_permanent)
	
	for _, room in ipairs(adjacent_rooms or empty_table) do
		if not new_adjacent_rooms[room] then
			--adjacency removed
			local data = adjacent_rooms[room]
			local myaw = data[2]
			local hisData = SetAdjacentRoom(room.adjacent_rooms, self, false)
			local hisaw = hisData and hisData[2]
			for i = 1, #(myaw or empty_table) do
				table.insert(events, {self, myaw[i]})
			end
			for i = 1, #(hisaw or empty_table) do
				table.insert(events, {room, hisaw[i]})
			end
		end
	end
	
	self.adjacent_rooms = new_adjacent_rooms
	
	if IsChangingMap() or XEditorUndo.undoredo_in_progress then
		return
	end
	
	if #(events or empty_table) > 0 then
		table.insert(AdjacencyEvents, events)
		Wakeup(PeriodicRepeatThreads["AdjacencyEvents"])
	end
end

function ProcessVolumeAdjacencyEvents()
	local passed = {}
	for i = 1, #AdjacencyEvents do
		local events = AdjacencyEvents[i]
		for i = 1, #(events or empty_table) do
			local ev = events[i]
			local o = ev[1]
			local s = ev[2]
			if IsValid(o) and (not passed[o] or (passed[o] and not passed[o][s])) then
				passed[o] = passed[o] or {}
				passed[o][s] = true
				--print(o.name, s)
				o:OnAdjacencyChanged(s)
			end
		end
	end
	table.clear(AdjacencyEvents)
end

-- make sure all changes to rooms are completed before we finish capturing undo data
OnMsg.EditorObjectOperationEnding = ProcessVolumeAdjacencyEvents

MapGameTimeRepeat("AdjacencyEvents", -1, function(sleep)
	PauseInfiniteLoopDetection("AdjacencyEvents")
	ProcessVolumeAdjacencyEvents()
	ResumeInfiniteLoopDetection("AdjacencyEvents")
	WaitWakeup()
end)

local dirToWallMatMember = {
	North = "north_wall_mat",
	South = "south_wall_mat",
	West = "west_wall_mat",
	East = "east_wall_mat",
	Floor = "floor_mat",
}

local sideToFuncName = {
	NW = "RecreateNWCornerBeam",
	NE = "RecreateNECornerBeam",
	SW = "RecreateSWCornerBeam",
	SE = "RecreateSECornerBeam",
}

function Volume:CheckWallSizes()
	if not Platform.developer then return end
	local t = self.spawned_walls
	assert(#t.West == #t.East)
	assert(#t.North == #t.South)
end

function Volume:OnAdjacencyChanged(side)
	if #side == 2 then
		self[sideToFuncName[side]](self)
	elseif side == "Floor" then
		self:CreateFloor(self.floor_mat)
	elseif side == "Roof" then
		if not self.being_placed then
			self:UpdateRoofSlabVisibility()
		end
	else
		self:CreateWalls(side, self[dirToWallMatMember[side]])
		self:CheckWallSizes()
	end
	
	self:DelayedRecalcRoof()
end

if FirstLoad then
	SelectedRooms = false
	RoomSelectionMode = false
end

function SetRoomSelectionMode(bVal)
	RoomSelectionMode = bVal
	print(string.format("RoomSelectionMode is %s", RoomSelectionMode and "ON" or "OFF"))
end

function ToggleRoomSelectionMode()
	SetRoomSelectionMode(not RoomSelectionMode)
end

if FirstLoad then
	roomsToDeselect = false
end

local function selectRoomHelper(r, t)
	t = t or SelectedRooms
	t = t or {}
	r:SetPosMarkersVisible(true)
	table.insert(t, r)
	if roomsToDeselect then
		table.remove_entry(roomsToDeselect, r)
	end
end

local function deselectRoomHelper(r)
	if IsValid(r) then
		r:SetPosMarkersVisible(false)
		r:ClearSelectedWall()
	end
end

local function deselectRooms()
	for i = 1, #(roomsToDeselect or "") do
		deselectRoomHelper(roomsToDeselect[i])
	end
	roomsToDeselect = false
end

function OnMsg.EditorSelectionChanged(objects)
	--room selection
	if RoomSelectionMode then
		--if 1 slab is selected?
		local o = #objects == 1 and objects[1]
		if o and IsKindOf(o, "Slab") and IsValid(o.room) then
			editor.ClearSel()
			editor.AddToSel({o.room})
			return --don't do further analysis this pass
		end
	end
	--selected rooms
	local newSelectedRooms = {}
	
	for i = 1, #objects do
		local o = objects[i]
		if IsKindOf(o, "Slab") then
			local r = o.room
			if IsValid(r) then
				selectRoomHelper(r, newSelectedRooms)
			end
		elseif IsKindOf(o, "Room") then
			selectRoomHelper(o, newSelectedRooms)
		end
	end
	
	for i = 1, #(SelectedRooms or "") do
		local r = SelectedRooms[i]
		if not table.find(newSelectedRooms, r) then
			--deselect
			roomsToDeselect = roomsToDeselect or {}
			table.insert(roomsToDeselect, r)
			DelayedCall(0, deselectRooms)
		end
	end
	
	SelectedRooms = #newSelectedRooms > 0 and newSelectedRooms or false
end

function Volume:TogglePosMarkersVisible()
	local el = self.text_markers.North
	self:SetPosMarkersVisible(el:GetEnumFlags(const.efVisible) == 0)
end

function Volume:SetPosMarkersVisible(val)
	for k, v in pairs(self.text_markers) do
		if not val then
			v:ClearEnumFlags(const.efVisible)
		else
			v:SetEnumFlags(const.efVisible)
		end
	end
end

function Volume:PositionWallTextMarkers()
	local t = self.text_markers
	local gz = self:CalcZ() + self.size:z() * voxelSizeZ / 2
	local p = self.position + point(self.size:x() * voxelSizeX / 2, 0)
	p = p:SetZ(gz)
	t.North:SetPos(p)
	p = self.position + point(self.size:x() * voxelSizeX / 2, self.size:y() * voxelSizeY)
	p = p:SetZ(gz)
	t.South:SetPos(p)
	p = self.position + point(0, self.size:y() * voxelSizeY / 2)
	p = p:SetZ(gz)
	t.West:SetPos(p)
	p = self.position + point(self.size:x() * voxelSizeX, self.size:y() * voxelSizeY / 2)
	p = p:SetZ(gz)
	t.East:SetPos(p)
end

function Volume:FinishAlign()
	if not self.seed then
		self.seed = EncodeVoxelPos(self)
	end
	
	self:CopyBoxToCCD()
	self:RebuildAdjacencyData()
	if self.wireframe_visible then
		self:GenerateGeometry()
	else
		self:DoneLines()
	end
	self:PositionWallTextMarkers()
	self.box_at_last_roof_edit = self.box
	
	if not IsChangingMap() then
		self:RefreshFloorCombatStatus()
	end
	
	Msg("RoomAligned", self)
end

function Volume:RefreshFloorCombatStatus()
end

function Volume:Setfloor(v)
	self.floor = v
	self:RefreshFloorCombatStatus()
end

function Volume:VolumeDestructor()
	self:DoneLines()
	DoneObject(self.light_vol_obj)
	DoneObjects(self.light_vol_objs)
	self.light_vol_obj = nil
	self.light_vol_objs = nil
	for k, v in pairs(self.text_markers) do
		DoneObject(v)
	end
	self["VolumeDestructor"] = empty_func
end

function Volume:Done()
	self:VolumeDestructor()
end

function Volume.ToggleVolumeCollision(_, self)
	VolumeCollisonEnabled = not VolumeCollisonEnabled
end

function Volume:CheckCollision(cls, box)
	if not VolumeCollisonEnabled then return false end
	if not self.enable_collision then return false end
	cls = cls or self.class
	local ret = false
	box = box or self.box
	MapForEach(self:GetPos(), roomQueryRadius, cls, function(o)
		if o ~= self and o.enable_collision then
			if box:Intersect(o.box) ~= 0 then
				ret = true
				return "break"
			end
		end
	end, box)
	
	return ret
end

local dontCopyTheeseProps = {
	name = true,
	floor = true,
	adjacent_rooms = true,
	box = true,
	position = true,
	roof_objs = true,
	spawned_doors = true,
	spawned_windows = true,
	spawned_decals = true,
	spawned_walls = true,
	spawned_corners = true,
	spawned_floors = true,
	text_markers = true,
}

function Volume:RecreateAllWallsOnMap()
	MapForEach("map", "Volume", Volume.RecreateWalls)
end

function Volume:RecreateAllRoofsOnMap()
	local all_volumes = MapGet("map", "Volume")
	table.sortby_field(all_volumes, "floor")
	for i,volume in ipairs(all_volumes) do
		volume:RecreateRoof()
	end
end

function Volume:RecreateAllFloorsOnMap()
	MapForEach("map", "Volume", Volume.RecreateFloor)
end

function Volume:RecreateWalls()
	SuspendPassEdits("Volume:RecreateWalls")
	self:DeleteAllWallObjs()
	self:DeleteAllCornerObjs()
	self:CreateAllWalls()
	self:CreateAllCorners()
	self:OnSetouter_colors(self.outer_colors)
	self:OnSetinner_colors(self.inner_colors)
	ResumePassEdits("Volume:RecreateWalls")
end

function Volume:RecreateFloor()
	SuspendPassEdits("Volume:RecreateFloor")
	self:DeleteAllFloors()
	self:CreateFloor()
	self:OnSetfloor_colors(self.floor_colors)
	ResumePassEdits("Volume:RecreateFloor")
end

function Volume:RecreateRoofBtn()
	self:RecreateRoof()
end

function Volume:ReRandomize()
	self.last_wall_recreate_seed = self.seed
	self.seed = BraidRandom(self.seed)
	self:CreateAllWalls()
	self.last_wall_recreate_seed = self.seed
	ObjModified(self)
end

function Volume:CopyAbove()
	XEditorUndo:BeginOp()
	local nv = self:Copy(1)
	SetSelectedVolume(nv)
	XEditorUndo:EndOp{ nv }
end

function Volume:CopyBelow()
	XEditorUndo:BeginOp()
	local nv = self:Copy(-1)
	SetSelectedVolume(nv)
	XEditorUndo:EndOp{ nv }
end

function Volume:CollisionCheckNextFloor(floorOffset)
	if not VolumeCollisonEnabled then return false end
	if not self.enable_collision then return false end
	local b = self.box
	local offset = point(0, 0, voxelSizeZ * self.size:z() * floorOffset)
	b = Offset(b, offset)
	local collision = false	
	MapForEach(self:GetPos(), roomQueryRadius, self.class, function(o)
		if o ~= self and o.enable_collision then
			if b:Intersect(o.box) ~= 0 then
				collision = true
				return "break"
			end
		end
	end)
	return collision
end

function Volume:Copy(floorOffset, inputObj, skipCollisionTest)
	local offset = point(0, 0, voxelSizeZ * self.size:z() * floorOffset)
	local collision = false
	if not skipCollisionTest then
		collision = self:CollisionCheckNextFloor(floorOffset)
	end
	
	if skipCollisionTest or not collision then
		inputObj = inputObj or {}
		inputObj.floor = inputObj.floor or self.floor + floorOffset
		inputObj.position = inputObj.position or self.position + offset
		inputObj.size = inputObj.size or self.size
		inputObj.name = inputObj.name or self.name .. " Copy"
		local doNotCopyTheseEither = table.copy(inputObj)
		
		local cpy = PlaceObject(self.class, inputObj)
		local prps = self:GetProperties()
		for i = 1, #prps do
			local prop = prps[i]
			if not dontCopyTheeseProps[prop.id] and not doNotCopyTheseEither[prop.id] then
				cpy:SetProperty(prop.id, self:GetProperty(prop.id))
			end
		end
		cpy:OnCopied(self, offset)
		DelayedCall(500, BuildBuildingsData)
		return cpy
	end
end

function Volume:OnCopied(from)
	self:AlignObj()
end

function Volume:ToggleGeometryVisible()
	if self.lines == false then
		self:GenerateGeometry()
		return
	end
	if self.lines and self.lines[1] then
		local visible = self.lines[1]:GetEnumFlags(const.efVisible) == 0
		for i = 1, #(self.lines or empty_table) do
			if visible then
				self.lines[i]:SetEnumFlags(const.efVisible)
			else
				self.lines[i]:ClearEnumFlags(const.efVisible)
			end
		end
	end
end

function Volume:DoneLines()
	DoneObjects(self.lines)
	self.lines = false
end

function Volume:GetWallBox(side, roomBox)
	local ret = false
	local b = roomBox or self.box
	if side == "North" then
		ret = box(b:minx(), b:miny(), b:minz(), b:maxx(), b:miny() + 1, b:maxz())
	elseif side == "South" then
		ret = box(b:minx(), b:maxy() - 1, b:minz(), b:maxx(), b:maxy(), b:maxz())
	elseif side == "East" then
		ret = box(b:maxx() - 1, b:miny(), b:minz(), b:maxx(), b:maxy(), b:maxz())
	elseif side == "West" then
		ret = box(b:minx(), b:miny(), b:minz(), b:minx() + 1, b:maxy(), b:maxz())
	end
	
	return ret
end

local function SetLineMesh(line, line_pstr)
	if not line_pstr or line_pstr:size() == 0 then return end
	line:SetMesh(line_pstr)
	return line
end

local offsetFromVoxelEdge = 20
function Volume:GenerateGeometry()
	self:DoneLines()
	
	local lines = {}
	local xPoints = {}
	local xPointsRoof = {}
	local yPoints = {}
	local yPointsRoof = {}
	
	local zOrigin = self:CalcSnappedZ()
	local p = self.position
	local x, y = p:xyz()
	local sx = abs(self.size:x())
	local sy = abs(self.size:y())
	local sz = abs(self.size:z())
	
	for inX = 0, sx - 1 do
		for inY = 0, sy - 1 do
			xPoints[inY] = xPoints[inY] or pstr("")
			yPoints[inX] = yPoints[inX] or pstr("")
			
			xPointsRoof[inY] = xPointsRoof[inY] or pstr("")
			yPointsRoof[inX] = yPointsRoof[inX] or pstr("")
			
			local xx, yy, zz, ox, oy, oz
			
			xx = x + inX * voxelSizeX + halfVoxelSizeX
			if inX == 0 then
				ox = xx - halfVoxelSizeX + offsetFromVoxelEdge
			elseif inX == sx - 1 then
				ox = xx + halfVoxelSizeX - offsetFromVoxelEdge
			else
				ox = xx
			end
			
			yy = y + inY * voxelSizeY + halfVoxelSizeY
			if inY == 0 then
				oy = yy - halfVoxelSizeY + offsetFromVoxelEdge
			elseif inY == sy - 1 then
				oy = yy + halfVoxelSizeY - offsetFromVoxelEdge
			else
				oy = yy
			end
			
			zz = zOrigin + offsetFromVoxelEdge
			oz = zz + sz * voxelSizeZ - offsetFromVoxelEdge*2
			
			if inX == 0 then
				--wall
				xPoints[inY]:AppendVertex(ox, yy, oz, self.wireframeColor)
			end
			xPoints[inY]:AppendVertex(ox, yy, zz, self.wireframeColor)
			if sx == 1 then
				xPointsRoof[inY]:AppendVertex(ox, yy, oz, self.wireframeColor)
				ox = xx + halfVoxelSizeX - offsetFromVoxelEdge
				xPoints[inY]:AppendVertex(ox, yy, zz, self.wireframeColor)
			end
			if inX == sx - 1 then
				--wall
				xPoints[inY]:AppendVertex(ox, yy, oz, self.wireframeColor)
			end
			
			if inY == 0 then
				--wall
				yPoints[inX]:AppendVertex(xx, oy, oz, self.wireframeColor)
			end
			yPoints[inX]:AppendVertex(xx, oy, zz, self.wireframeColor)
			if sy == 1 then
				yPointsRoof[inX]:AppendVertex(xx, oy, oz, self.wireframeColor)
				oy = yy + halfVoxelSizeY - offsetFromVoxelEdge
				yPoints[inX]:AppendVertex(xx, oy, zz, self.wireframeColor)
			end
			if inY == sy - 1 then
				--wall
				yPoints[inX]:AppendVertex(xx, oy, oz, self.wireframeColor)
			end
			
			xPointsRoof[inY]:AppendVertex(ox, yy, oz, self.wireframeColor)
			yPointsRoof[inX]:AppendVertex(xx, oy, oz, self.wireframeColor)
		end
	end
	
	local visible = self.wireframe_visible
	local function SetVisibilityHelper(line)
		if not visible then
			line:ClearEnumFlags(const.efVisible)
		end
	end
	
	for inX = 0, sx - 1 do
		local line = PlaceObject("Polyline")
		SetVisibilityHelper(line)
		line:SetPos(p)
		SetLineMesh(line, yPoints[inX])
		table.insert(lines, line)
		self:Attach(line)
		line = PlaceObject("Polyline")
		SetVisibilityHelper(line)
		line:SetPos(p)
		SetLineMesh(line, yPointsRoof[inX])
		table.insert(lines, line)
		self:Attach(line)
	end
	
	for inY = 0, sy - 1 do
		local line = PlaceObject("Polyline")
		SetVisibilityHelper(line)
		line:SetPos(p)
		SetLineMesh(line, xPoints[inY])
		table.insert(lines, line)
		self:Attach(line)
		line = PlaceObject("Polyline")
		SetVisibilityHelper(line)
		line:SetPos(p)
		SetLineMesh(line, xPointsRoof[inY])
		table.insert(lines, line)
		self:Attach(line)
	end
	
	self.lines = lines
end

function Volume:GetBiggestEncompassingRoom(func, ...)
	--this presumes no wall crossing
	local biggestRoom = self
	if self.box then
		local sizex, sizey = self.box:sizexyz()
		local biggestRoomSize = sizex + sizey
		EnumVolumes(self.box, function(o, ...)
			local szx, szy = o.box:sizexyz()
			local size = szx + szy
			if size > biggestRoomSize then
				if not func or func(o, ...) then
					biggestRoom = o
					biggestRoomSize = size
				end
			end
		end, ...)
	end
	return biggestRoom
end

local function MakeSlabInvulnerable(o, val)
	o.forceInvulnerableBecauseOfGameRules = val
	o.invulnerable = val
	SetupObjInvulnerabilityColorMarkingOnValueChanged(o)
end

function Volume:MakeOwnedSlabsInvulnerable()
	self:ForEachSpawnedObj(function(o)
		MakeSlabInvulnerable(o, true)
		if IsKindOf(o, "SlabWallObject") then
			local os = o.owned_slabs
			if os then
				for _, oo in ipairs(os) do
					MakeSlabInvulnerable(oo, true)
				end
			end
		end
	end)
end

function Volume:MakeOwnedSlabsVulnerable()
	local floorsInvul = self.floor == 1
	self:ForEachSpawnedObj(function(o)
		if not floorsInvul or not IsKindOf(o, "FloorSlab") then
			MakeSlabInvulnerable(o, false)
			if IsKindOf(o, "SlabWallObject") then
				local os = o.owned_slabs
				if os then
					for _, oo in ipairs(os) do
						MakeSlabInvulnerable(oo, false)
					end
				end
			end
		end
	end)
end

function Volume:Destroy()
	--so shift + d in f3 doesn't kill these
end

function ShowVolumes(bShow, volume_class, max_floor, fn)
	MapClearEnumFlags(const.efVisible, "map", "Volume")
	if not bShow or not volume_class then return end
	MapSetEnumFlags(const.efVisible, "map", volume_class, function(volume, max_floor, fn)
		if volume.floor <= max_floor then
			fn(volume)
			return true
		end
	end, max_floor or max_int, fn or empty_func)
end

function SelectVolume(pt) -- in screen coordinates, terminal.GetMousePos()
	-- enumerate all visible volumes on the map and select the one under the mouse point pt
	local start = ScreenToGame(pt)
	local pos = cameraRTS.GetPos()
	local dir = start - pos
	dir = dir * 1000
	local dir2 = start + dir
	local camFloor = cameraTac.GetFloor() + 1

	--DbgAddCircle(start, 100)
	--DbgAddVector(start, pos - start)  
	--DbgAddVector(start, dir*1000, RGB(0, 255, 0))

	return MapFindMin("map", "Volume", nil, nil, nil, nil, nil, nil, function(volume, dir2, camFloor)
		if HideFloorsAboveThisOne then
			if volume.floor > HideFloorsAboveThisOne then
				return false
			end
		end
		--return distance to intersection between the camera ray and volume box
		local p1, p2 = ClipSegmentWithBox3D(start, dir2, volume.box)
		if p1 then
			--DbgAddCircle(p1, 100)
			--DbgAddCircle(p2, 100, RGB(0, 0, 255))
			return p1:Dist2(start)
		end
		
		return false
	end, start, dir2, camFloor) or false, start, dir2
end

local lastSelectedVolume = false
local function SetSelectedVolumeAndFireEvents(vol)
	if vol ~= SelectedVolume then
		local oldVolume = SelectedVolume
		SelectedVolume = vol
		if oldVolume then
			lastSelectedVolume = oldVolume
			if IsValid(oldVolume) then
				oldVolume.wall_text_markers_visible = false
				oldVolume:SetPosMarkersVisible(false)
			end
			Msg("VolumeDeselected", oldVolume)
		end
		if SelectedVolume then
			if SelectedVolume ~= lastSelectedVolume then --only deselect wall if another vol is selected
				SelectedVolume.selected_wall = false
				ObjModified(SelectedVolume)
			end
			
			SelectedVolume.wall_text_markers_visible = true
			SelectedVolume:SetPosMarkersVisible(true)
			editor.ClearSel()
		end
		Msg("VolumeSelected", SelectedVolume)
	end
end

function SetSelectedVolume(vol)
	SetSelectedVolumeAndFireEvents(vol)
	if GedRoomEditor then
		GedRoomEditorObjList = GedRoomEditor:ResolveObj("root")
		CreateRealTimeThread(function()
			GedRoomEditor:SetSelection("root", table.find(GedRoomEditorObjList, SelectedVolume))
		end)
	end
end

local doorId = "Door"
local windowId = "Window"
local doorTemplate = "%s_%s"
local Doors_WidthNames = { "Single", "Double" }
local Windows_WidthNames = { "Single", "Double", "Triple" }

function DoorsDropdown()
	return function()
		local ret = { {name = "", id = ""} }
		
		for j = 1, #Doors_WidthNames do
			local name = string.format(doorTemplate, doorId, Doors_WidthNames[j])
			local data = {mat = false, width = j, height = 3}
			table.insert(ret, {name = name, id = data})
		end
		
		return ret
	end
end

function WindowsDropdown()
	return function()
		local ret = { {name = "", id = ""} }
		
		for j = 1, #Windows_WidthNames do
			local name = string.format(doorTemplate, windowId, Windows_WidthNames[j])
			local data = {mat = false, width = j, height = 2}
			table.insert(ret, {name = name, id = data})
		end
		
		return ret
	end
end

function GetDecalPresetData()
	return Presets.RoomDecalData.Default
end

function DecalsDropdown()
	return function()
		local ret = { {name = "", id = ""} }
		local presetData = GetDecalPresetData()
		for _, entry in ipairs(presetData) do
			local data = { entity = entry.id, }
			table.insert(ret, {name = entry.id, id = data})
		end
		return ret
	end
end

local function GetAllWindowEntitiesForMaterial(obj)
	local material = type(obj) == "string" and obj or obj.linked_obj and obj.linked_obj.material or obj.material
	local ret = { false }
	for w = 0, 3 do
		for h = 1, 3 do
			for v = 1, 10 do
				local e = SlabWallObjectName(material, h, w, v, false)
				if IsValidEntity(e) then
					ret[#ret + 1] = { name = e, value = {entity = e, height = h, width = w, subvariant = v, material = material} }
				end
			end
		end
	end
	
	return ret
end

local function GetAllDoorEntitiesForMaterial(obj)
	local material = type(obj) == "string" and obj or obj.linked_obj and obj.linked_obj.material or obj.material
	local ret = { false }
	for w = 1, 3 do
		for h = 3, 4 do
			for v = 1, 10 do
				local e = SlabWallObjectName(material, h, w, v, true)
				if IsValidEntity(e) then
					ret[#ret + 1] = { name = e, value = {entity = e, height = h, width = w, subvariant = v, material = material} }
				end
				
				if v == 1 then
					e = SlabWallObjectName(material, h, w, nil, true)
					if IsValidEntity(e) then
						ret[#ret + 1] = { name = e, value = {entity = e, height = h, width = w, subvariant = v, material = material} }
					end
				end
			end
		end
	end
	
	return ret
end

local function SelectedWallNoEdit(self)
	return self.selected_wall == false
end

slabDirToAngle = {
	North = 270 * 60,
	South = 90 * 60,
	West = 180 * 60,
	East = 0,
}

slabAngleToDir = {
	[270 * 60] = "North",
	[90 * 60] = "South",
	[180 * 60] = "West",
	[0] = "East",
}

slabCornerAngleToDir = {
	[270 * 60] = "East",
	[90 * 60] = "West",
	[180 * 60] = "North",
	[0] = "South",
}

function _RoomVisibilityCategoryNoEdit()
	return RoomVisibilityCategoryNoEdit()
end

function RoomVisibilityCategoryNoEdit()
	return true
end

local VisibilityStateItems = {
	"Closed",
	"Hidden",
	"Open"
}

function SlabMaterialComboItemsWithNone()
	return PresetGroupCombo("SlabPreset", "SlabMaterials", nil, noneWallMat)
end

function SlabMaterialComboItemsOnly()
	return function()
		local f1 = SlabMaterialComboItemsWithNone()
		local ret = f1()
		table.remove(ret, 1)
		return ret
	end
end

function SlabMaterialComboItemsWithDefault()
	return function() 
		local f1 = SlabMaterialComboItemsWithNone()
		local ret = f1()
		table.insert(ret, 2, defaultWallMat)
		return ret
	end
end

DefineClass.Room = {
	__parents = { "Volume", "EditorSubVariantObject" },
	flags = { gofWarped = true },
	
	properties = {
		{ category = "General", name = "Doors And Windows Are Blocked", id = "doors_windows_blocked", editor = "bool", default = false, },
		{ category = "General", id = "name", name = "Name", editor = "text", default = false, help = "Default 'Room <handle>', renameable." },
		
		{ category = "General", id = "size_z", name = "Height (z)", editor = "number", default = defaultVolumeVoxelHeight, min = 0, max = maxRoomVoxelSizeZ, dont_save = true},
		{ category = "General", id = "size_x", name = "Width (x)", editor = "number", default = 1, min = 1, max = maxRoomVoxelSizeX, dont_save = true},
		{ category = "General", id = "size_y", name = "Depth (y)", editor = "number", default = 1, min = 1, max = maxRoomVoxelSizeY, dont_save = true},
		{ category = "General", id = "move_x", name = "Move EW (x)", editor = "number", default = 0, dont_save = true},
		{ category = "General", id = "move_y", name = "Move NS (y)", editor = "number", default = 0, dont_save = true},
		{ category = "General", id = "move_z", name = "Move UD (z)", editor = "number", default = 0, dont_save = true},
		--materials
		{ category = "Materials", id = "wall_mat", name = "Wall Material", editor = "preset_id", preset_class = "SlabPreset", preset_group = "SlabMaterials", extra_item = noneWallMat, default = "Planks",
			buttons = {
				{name = "Reset", func = "ResetWallMaterials"}, 
			},
		},
		{ category = "Materials", id = "outer_colors", name = "Outer Color Modifier", editor = "nested_obj", base_class = "ColorizationPropSet", inclusive = true, default = false, },
		{ category = "Materials", id = "inner_wall_mat", name = "Inner Wall Material", editor = "preset_id", preset_class = "SlabIndoorMaterials", extra_item = noneWallMat, default = "Planks", },
		
		{ category = "Materials", id = "inner_colors", name = "Inner Color Modifier", editor = "nested_obj", base_class = "ColorizationPropSet", inclusive = true, default = false, },
		{ category = "Materials", id = "north_wall_mat", name = "North Wall Material", editor = "dropdownlist", items = SlabMaterialComboItemsWithDefault, default = defaultWallMat,
			buttons = { 
				{name = "Select", func = "ViewNorthWallFromOutside"},
			}
		},		
		{ category = "Materials", id = "south_wall_mat", name = "South Wall Material", editor = "dropdownlist", items = SlabMaterialComboItemsWithDefault, default = defaultWallMat, 
			buttons = { 
				{name =  "Select", func = "ViewSouthWallFromOutside"},
			} 
		},
		{ category = "Materials", id = "east_wall_mat", name = "East Wall Material", editor = "dropdownlist", items = SlabMaterialComboItemsWithDefault, default = defaultWallMat, 
			buttons = { 
				{name =  "Select", func = "ViewEastWallFromOutside"},
			}
		},
		{ category = "Materials", id = "west_wall_mat", name = "West Wall Material", editor = "dropdownlist", items = SlabMaterialComboItemsWithDefault, default = defaultWallMat,
			buttons = { 
				{name =  "Select", func = "ViewWestWallFromOutside"},
			}
		},
		{ category = "Materials", id = "floor_mat", name = "Floor Material", editor = "preset_id", preset_class = "SlabPreset", preset_group = "FloorSlabMaterials", extra_item = noneWallMat, default = "Planks", },
		{ category = "Materials", id = "floor_colors", name = "Floor Color Modifier", editor = "nested_obj", base_class = "ColorizationPropSet", inclusive = true, default = false, },
		{ category = "Materials", id = "Warped", name = "Warped", editor = "bool", default = true },
		
		{ category = "Materials", id = "selected_wall_buttons", name = "selected wall buttons", editor = "buttons", default = false, dont_save = true, read_only = true,
			no_edit = SelectedWallNoEdit,
			buttons = {
				{name = "Clear Wall Selection", func = "ClearSelectedWall"},
				{name = "Delete Doors", func = "UIDeleteDoors"},
				{name = "Delete Windows", func = "UIDeleteWindows"},
			},
		},
		
		{ category = "Materials", id = "place_decal", name = "Place Decal", editor = "choice", items = DecalsDropdown, default = "", no_edit = SelectedWallNoEdit,},
		
		{ category = "General", id = "spawned_doors", editor = "objects", no_edit = true,},
		{ category = "General", id = "spawned_windows", editor = "objects", no_edit = true,},
		{ category = "General", id = "spawned_decals", editor = "objects", no_edit = true,}, --todo: kill
		
		{ category = "General", id = "spawned_floors", editor = "objects", no_edit = true},
		{ category = "General", id = "spawned_walls", editor = "objects", no_edit = true,},
		{ category = "General", id = "spawned_corners", editor = "objects", no_edit = true,},
		
		{ category = "Not Room Specific", id = "hide_floors_editor", editor = "number", default = 100, name = "Hide Floors Above", dont_save = true},
		
		--bacon specific?
		{ category = "Visibility", name = "Visibility State", id = "visibility_state", editor = "choice", items = VisibilityStateItems, dont_save = true, no_edit = _RoomVisibilityCategoryNoEdit },
		{ category = "Visibility", name = "Focused", id = "is_focused", editor = "bool", default = false, dont_save = true, no_edit = _RoomVisibilityCategoryNoEdit },
		
		{ category = "Ignore None Material", name = "Wall", id = "none_wall_mat_does_not_affect_nbrs", editor = "bool", default = false, help = "By default, setting a wall material to none will hide overlapping walls, tick this for it to stop happening. Affects all walls of a room." },
		{ category = "Ignore None Material", name = "Roof Wall", id = "none_roof_wall_mat_does_not_affect_nbrs", editor = "bool", default = false, help = "Same as walls (see above), but for walls that are part of the roof - roof walls." },
		{ category = "Ignore None Material", name = "Floor", id = "none_floor_mat_does_not_affect_nbrs", editor = "bool", default = false, help = "By default, setting a floor material to none will hide overlapping floors, tick this for it to stop happening. Affects all floors of a room." },
	},
	
	auto_add_in_editor = true, -- for Room editor
	
	spawned_walls = false, -- {["North"] = {}, etc.}
	spawned_corners = false,
	spawned_floors = false,
	spawned_doors = false,
	spawned_windows = false,
	spawned_decals = false,
	
	selected_wall = false, -- false, "North", "South", etc.
	
	next_visibility_state = false,
	visibility_state = false, -- when defined purely as a prop, something strips it.
	open_state_collapsed_walls = false,
	outside_border = false,
	nametag = false,
}

local function moveHelper(self, key, old_v, x, y, z, ignore_collision)
	if IsChangingMap() then return end
	self:InternalAlignObj(true) -- this moves box
	if not ignore_collision and self:CheckCollision() then
		self[key] = old_v
		self:InternalAlignObj(true)
		print("Could not move room due to collision with other room!")
		return false
	else
		self:MoveAllSpawnedObjs(x, y, z)
		Volume.FinishAlign(self)
		Msg("RoomMoved", self, x, y, z) -- x, y, z - move delta in voxels
		return true
	end
end

function sign(v)
	return v ~= 0 and abs(v) / v or 0
end

function moveHelperHelper(r, delta)
	local old = r.position
	delta = delta + halfVoxelPt
	r.position = SnapVolumePos(old + delta)
	return moveHelper(r, "position", old, delta:x() / voxelSizeX, delta:y() / voxelSizeY, delta:z() / voxelSizeZ)
end

function Room:EditorExit()
	if IsValid(self.nametag) then
		self.nametag:ClearEnumFlags(const.efVisible)
	end
end

function Room:EditorEnter()
	if IsValid(self.nametag) and self:GetEnumFlags(const.efVisible) ~= 0 then
		self.nametag:SetEnumFlags(const.efVisible)
	end
end

function Room:Setname(n)
	self.name = n
	if not IsValid(self.nametag) then
		self.nametag = PlaceObject("TextEditor")
		self:Attach(self.nametag)
		self.nametag:SetAttachOffset((axis_z * 3 * voxelSizeZ) / 4096)
		
		if not IsEditorActive() or self:GetEnumFlags(const.efVisible) == 0 then
			self.nametag:ClearEnumFlags(const.efVisible)
		end
	end
	self.nametag:SetText(self.name)
end

local movedRooms = false
function Room_RecalcRoofsOfMovedRooms()
	if not movedRooms then return end
	for i = 1, #movedRooms do
		local room = movedRooms[i]
		if IsValid(room) then
			room:RecalcRoof()
			room:UpdateRoofVfxControllers()
		end
	end
	movedRooms = false
end

function Room:DelayedRecalcRoof()
	movedRooms = table.create_add_unique(movedRooms, self)
	if LocalStorage.FilteredCategories["Roofs"] and not XEditorUndo.undoredo_in_progress then
		DelayedCall(200, Room_RecalcRoofsOfMovedRooms)
	end
end

-- make sure all changes to roofs are completed before we finish capturing undo data
OnMsg.EditorObjectOperationEnding = Room_RecalcRoofsOfMovedRooms

function Room:AlignObj(pos, angle)
	if pos then
		assert(IsEditorActive())
		local offset = pos - self:GetPos()
		local box = self.box
		local didMove = false
		if abs(offset:x()) / voxelSizeX > 0 or abs(offset:y()) / voxelSizeY > 0 or abs(offset:z()) / voxelSizeZ > 0 then
			didMove = moveHelperHelper(self, offset)
		end
		if didMove then
			ObjModified(self)
			assert(self:GetGameFlags(const.gofPermanent) ~= 0)
			box = AddRects(box, self.box)
			ComputeSlabVisibilityInBox(box)
			DelayedCall(500, BuildBuildingsData)
			self:DelayedRecalcRoof()
		end
	else
		self:InternalAlignObj()
	end
end

function Room:GetEditorLabel()
	return self.name or self.class
end

function InsertMaterialProperties(name, count)
	assert(count >= 1 and count <= 4)
	for i = 1, count do
		table.insert(Room.properties, {
			id = name .. "color" .. count,
			editor = "color",
			alpha = false,
		})
		table.insert(Room.properties, {
			id = name .. "metallic" .. count,
			editor = "number",
		})
	end
end


local room_NSWE_lists = {
	"spawned_walls",
	"spawned_corners",
	"spawned_doors",
	"spawned_windows",
	"spawned_decals",
}

local room_NSWE_lists_no_DoorsWindows = {
	"spawned_walls",
	"spawned_corners",
}

local room_regular_lists = {
	"spawned_floors",
	"roof_objs",
}

local room_regular_list_sides = {
	"Floor",
	false -- see RoomRoof:GetPivots
}

function ForEachInTable(t, f, ...)
	for i = 1, #(t or "") do
		local o = t[i]
		if IsValid(o) then
			f(o, ...)
		end
	end
end

function Room:UnlockAllSlabs()
	self:UnlockFloor()
	self:UnlockAllWalls()
	self:UnlockRoof()
end

function Room:UnlockFloor()
	ForEachInTable(self.spawned_floors, Slab.UnlockSubvariant)
end

function Room:UnlockAllWalls()
	for side, t in pairs(self.spawned_walls or empty_table) do
		ForEachInTable(t, Slab.UnlockSubvariant)
	end
	for side, t in pairs(self.spawned_corners or empty_table) do
		ForEachInTable(t, Slab.UnlockSubvariant)
	end
	ForEachInTable(self.roof_objs, function(o)
		if not IsKindOf(o, "RoofSlab") then
			o:UnlockSubvariant()
		end
	end)
end

function Room:UnlockRoof()
	ForEachInTable(self.roof_objs, function(o)
		if IsKindOf(o, "RoofSlab") then
			o:UnlockSubvariant()
		end
	end)
end

sideToCornerSides = {
	East = { "East", "South" },
	South = { "West", "South" },
	West = { "West", "North" },
	North = { "East", "North" },
}

function Room:UnlockWallSide(side) --both walls and corners + roof walls n corners in one
	ForEachInTable(self.spawned_walls and self.spawned_walls[side], Slab.UnlockSubvariant)
	local css = sideToCornerSides[side]
	for _, cs in ipairs(css) do
		ForEachInTable(self.spawned_corners and self.spawned_corners[cs], Slab.UnlockSubvariant)
	end
	
	ForEachInTable(self.roof_objs, function(o, side)
		if o.side == side and not IsKindOf(o, "RoofSlab") then
			o:UnlockSubvariant()
		end
	end ,side)
end

function Room:ForEachSpawnedObjNoDoorsWindows(func, ...)
	return self:_ForEachSpawnedObj(room_NSWE_lists_no_DoorsWindows, room_regular_lists, func, ...)
end

function Room:ForEachSpawnedObj(func, ...)
	return self:_ForEachSpawnedObj(room_NSWE_lists, room_regular_lists, func, ...)
end

function Room:_ForEachSpawnedObj(NSWE_lists, regular_lists, func, ...)
	for i = 1, #NSWE_lists do
		for side, objs in NSEW_pairs(self[NSWE_lists[i]] or empty_table) do
			for j = 1, #objs do
				if IsValid(objs[j]) then func(objs[j], ...) end
			end
		end
	end
	
	for i = 1, #regular_lists do
		local lst = self[regular_lists[i]] or ""
		for j = 1, #lst do
			if IsValid(lst[j]) then func(lst[j], ...) end
		end
	end
end

function Room:GetEditorRelatedObjects()
	local ret = {}
	for i = 1, #room_NSWE_lists do
		for side, objs in NSEW_pairs(self[room_NSWE_lists[i]] or empty_table) do
			for _, obj in ipairs(objs) do
				if obj then
					ret[#ret + 1] = obj
					if obj:HasMember("owned_objs") and obj.owned_objs then
						table.iappend(ret, obj.owned_objs)
					end
					if obj:HasMember("owned_slabs") and obj.owned_slabs then
						table.iappend(ret, obj.owned_slabs)
					end
				end
			end
		end
	end
	for i = 1, #room_regular_lists do
		table.iappend(ret, self[room_regular_lists[i]] or empty_table)
	end
	Msg("GatherRoomRelatedObjects", self, ret)
	return ret
end

function Room:SetWarped(warped, force)
	CObject.SetWarped(self, warped)
	if force or not IsChangingMap() then
		self:ForEachSpawnedObj(function(obj)
			obj:SetWarped(warped)
		end)
	end
end

local function copyWallObjs(t, offset, room)
	local ret = {}
	for side, objs in NSEW_pairs(t or empty_table) do
		ret[side] = {}
		for i = 1, #objs do
			local o = objs[i]
			local no = PlaceObject(o.class)
			no.floor = room.floor
			no.width = o.width
			no.height = o.height
			no.material = o.material
			no:SetPos(o:GetPos() + offset)
			no:SetAngle(o:GetAngle())
			table.insert(ret[side], no)
			no:UpdateEntity()
		end
	end
	
	return ret
end

local function copyDecals(t, offset, room)
	local ret = {}
	for side, objs in NSEW_pairs(t or empty_table) do
		ret[side] = {}
		for i = 1, #objs do
			local o = objs[i]
			local no = PlaceObject(o.class)
			no.floor = room.floor
			no:SetPos(o:GetPos() + offset)
			no:SetAngle(o:GetAngle())
			no.restriction_box = Offset(o.restriction_box, offset)
			table.insert(ret[side], no)
		end
	end
	
	return ret
end

function Room:OnSetdoors_windows_blocked()
	self:ForEachSpawnedWallObj(function(o, val)
		o:SetlockpickState(val and "blocked" or "closed")
	end, self.doors_windows_blocked)
end

function Room:OnSetnone_roof_wall_mat_does_not_affect_nbrs()
	self:ComputeRoomVisibility()
end

function Room:OnSetnone_wall_mat_does_not_affect_nbrs()
	self:ComputeRoomVisibility()
end

function Room:OnCopied(from, offset)
	Volume.OnCopied(self, from, offset)
	self:CreateAllSlabs()
	
	self.spawned_doors = copyWallObjs(from.spawned_doors, offset, self)
	self.spawned_windows = copyWallObjs(from.spawned_windows, offset, self)
	self.spawned_decals = copyDecals(from.spawned_decals, offset, self)
end

function Room:OnAfterEditorNew(parent, ged, is_paste)
	--undo deletion from ged
	self.adjacent_rooms = nil
	self:AlignObj()
	self:CreateAllSlabs()
end

function Room:OnEditorSetProperty(prop_id, old_value, ged)
	if not IsValid(self) then return end --undo on deleted obj
	local f = rawget(Room, string.format("OnSet%s", prop_id))
	if f then
		f(self, self[prop_id], old_value)
		DelayedCall(500, BuildBuildingsData)
	end
end

function Room:OnSethide_floors_editor()
	assert(IsEditorActive())
	HideFloorsAboveThisOne = rawget(self, "hide_floors_editor")
	HideFloorsAbove(HideFloorsAboveThisOne)
end

function Room:Gethide_floors_editor()
	return HideFloorsAboveThisOne
end

function Room:OnSetwireframe_visible()
	self:ToggleGeometryVisible()
end

function Room:OnSetwall_text_markers_visible()
	self:TogglePosMarkersVisible()
end

function Room:OnSetinner_wall_mat(val, oldVal)
	if val == "" then
		val = noneWallMat
		self.inner_wall_mat = val
	end
	if (val == noneWallMat or oldVal == noneWallMat) and val ~= oldVal then
		self:UnlockAllWalls()
	end
	self:SetInnerMaterialToSlabs("North")
	self:SetInnerMaterialToSlabs("South")
	self:SetInnerMaterialToSlabs("West")
	self:SetInnerMaterialToSlabs("East")
	self:SetInnerMaterialToRoofObjs()
end

function Room:OnSetinner_colors(val, oldVal)
	self:SetInnerMaterialToSlabs("North")
	self:SetInnerMaterialToSlabs("South")
	self:SetInnerMaterialToSlabs("West")
	self:SetInnerMaterialToSlabs("East")
	self:SetInnerMaterialToRoofObjs()
end

function Room:OnSetouter_colors(val, oldVal)
	local function iterateNSEWTableAndSetColor(t)
		if not t then return end
		for side, list in NSEW_pairs(t) do
			for i = 1, #list do
				local o = list[i]
				if IsValid(o) then --wall piece might be deleted by lvl designer
					o:Setcolors(val)
				end
			end
		end
	end
	iterateNSEWTableAndSetColor(self.spawned_walls)
	
	for side, list in NSEW_pairs(self.spawned_corners) do
		for i = 1, #list do
			local o = list[i]
			if IsValid(o) then
				o:SetColorFromRoom()
			end
		end
	end
	
	for side, list in NSEW_pairs(self.spawned_windows or empty_table) do
		for i = 1, #list do
			list[i]:UpdateManagedSlabs()
			list[i]:RefreshColors()
		end
	end
	
	for side, list in NSEW_pairs(self.spawned_doors or empty_table) do
		for i = 1, #list do
			list[i]:RefreshColors()
		end
	end
	
	if self.roof_objs then
		for i=1,#self.roof_objs do
			local o = self.roof_objs[i]
			if IsValid(o) and not IsKindOf(o, "RoofSlab") then
				o:Setcolors(val)
			end
		end
	end
end

function Room:OnSetfloor_colors(val, oldVal)
	for i = 1, #(self.spawned_floors or "") do
		local o = self.spawned_floors[i]
		if IsValid(o) then
			o:Setcolors(val)
		end
	end
end

function Room:OnSetfloor_mat(val)
	self:UnlockFloor()
	self:CreateFloor()
end

function Room:ResetWallMaterials()
	local wm = defaultWallMat
	local change = self.north_wall_mat ~= wm
	self.north_wall_mat = wm
	change = change or self.south_wall_mat ~= wm
	self.south_wall_mat = wm
	change = change or self.west_wall_mat ~= wm
	self.west_wall_mat = wm
	change = change or self.east_wall_mat ~= wm
	self.east_wall_mat = wm
	--todo: wall added/removed msgs
	if change then
		ObjModified(self)
		self:UnlockAllWalls()
		self:CreateAllWalls()
		self:RecreateRoof()
	end
end

local function FireWallChangedEventsHelper(self, side, val, oldVal)
	local wasWall = oldVal ~= noneWallMat and (oldVal ~= defaultWallMat or self.wall_mat ~= noneWallMat)
	local isWall = val ~= noneWallMat and (val ~= defaultWallMat or self.wall_mat ~= noneWallMat)
	if not wasWall and isWall then
		Msg("RoomAddedWall", self, side)
	elseif wasWall and not isWall then
		Msg("RoomRemovedWall", self, side)
	end
end

function Room:OnSetnorth_wall_mat(val, oldVal)
	self:UnlockWallSide("North")
	self:CreateWalls("North", val)
	self:RecreateNECornerBeam()
	self:RecreateNWCornerBeam()
	self:RecreateRoof()
	FireWallChangedEventsHelper(self, "North", val, oldVal)
	self:CheckWallSizes()
end

function Room:OnSetsouth_wall_mat(val, oldVal)
	self:UnlockWallSide("South")
	self:CreateWalls("South", val)
	self:RecreateSECornerBeam()
	self:RecreateSWCornerBeam()
	self:RecreateRoof()
	FireWallChangedEventsHelper(self, "South", val, oldVal)
	self:CheckWallSizes()
end

function Room:OnSetwest_wall_mat(val, oldVal)
	self:UnlockWallSide("West")
	self:CreateWalls("West", val)
	self:RecreateSWCornerBeam()
	self:RecreateNWCornerBeam()
	self:RecreateRoof()
	FireWallChangedEventsHelper(self, "West", val, oldVal)
	self:CheckWallSizes()
end

function Room:OnSeteast_wall_mat(val, oldVal)
	self:UnlockWallSide("East")
	self:CreateWalls("East", val)
	self:RecreateSECornerBeam()
	self:RecreateNECornerBeam()
	self:RecreateRoof()
	FireWallChangedEventsHelper(self, "East", val, oldVal)
	self:CheckWallSizes()
end

function Room:SetWallMaterial(val)
	local ov = self.wall_mat
	self.wall_mat = val
	if IsChangingMap() then return end
	self:OnSetwall_mat(val, ov)
end

function Room:OnSetwall_mat(val, oldVal)
	if val == "" then
		val = noneWallMat
		self.wall_mat = val
	end
	self:UnlockAllWalls()
	self:CreateAllWalls()
	self:RecreateRoof()
	
	local wasWall = oldVal ~= noneWallMat
	local isWall = val ~= noneWallMat
	local ev = false
	if wasWall and not isWall then
		ev = "RoomRemovedWall"
	elseif not wasWall and isWall then
		ev = "RoomAddedWall"
	end
	
	if ev then
		if self.south_wall_mat == defaultWallMat then
			Msg(ev, self, "South")
		end
		if self.north_wall_mat == defaultWallMat then
			Msg(ev, self, "North")
		end
		if self.west_wall_mat == defaultWallMat then
			Msg(ev, self, "West")
		end
		if self.east_wall_mat == defaultWallMat then
			Msg(ev, self, "East")
		end
	end
end

function SizeSetterHelper(self, old_v)
	if IsChangingMap() then return end
	local oldBox = self.box
	self:InternalAlignObj(true)
	if self:CheckCollision() then
		self.size = old_v
		self:InternalAlignObj(true)
		return false
	else
		self:Resize(old_v, self.size, oldBox)
		Volume.FinishAlign(self)
		Msg("RoomResized", self, old_v) --old_v == old self.size
		return true
	end
end

function Room:OnSetsize_x(val)
	local old_v = self.size
	self.size = point(val, self.size:y(), self.size:z())
	SizeSetterHelper(self, old_v)
end

function Room:OnSetsize_y(val)
	local old_v = self.size
	self.size = point(self.size:x(), val, self.size:z())
	SizeSetterHelper(self, old_v)
end

function Room:OnSetsize_z(val)
	local old_v = self.size
	self.size = point(self.size:x(), self.size:y(), val)
	SizeSetterHelper(self, old_v)
	if val == 0 then
		self:DeleteAllWallObjs()
		self:DeleteAllFloors()
		self:DeleteAllCornerObjs()
	end
end

function Room:Getsize_x()
	return self.size:x()
end

function Room:Getsize_y()
	return self.size:y()
end

function Room:Getsize_z()
	return self.size:z()
end

function Room:Getmove_x()
	local x = WorldToVoxel(self.position)
	return x
end

function Room:Getmove_y()
	local _, y = WorldToVoxel(self.position)
	return y
end

function Room:Getmove_z()
	local x, y, z = WorldToVoxel(self.position)
	return z
end

function Room:OnSetz_offset(val, old_v)
	moveHelper(self, "z_offset", old_v, 0, 0, val - old_v)
end

function Room:OnSetmove_x(val)
	local old_v = self.position
	local x, y, z = WorldToVoxel(self.position)
	self.position = SnapVolumePos(VoxelToWorld(val, y, z, true))
	moveHelper(self, "position", old_v, val - x, 0, 0)
end

function Room:OnSetmove_y(val)
	local old_v = self.position
	local x, y, z = WorldToVoxel(self.position)
	self.position = SnapVolumePos(VoxelToWorld(x, val, z, true))
	moveHelper(self, "position", old_v, 0, val - y, 0)
end

function Room:OnSetmove_z(val)
	local old_v = self.position
	local x, y, z = WorldToVoxel(self.position)
	self.position = SnapVolumePos(VoxelToWorld(x, y, val, true))
	moveHelper(self, "position", old_v, 0, 0, val - z)
end

--left to right sort on selected wall
local dirToComparitor = {
	South = function(o1, o2)
		local x1, _, _ = o1:GetPosXYZ()
		local x2, _, _ = o2:GetPosXYZ()
		return x1 < x2
	end,
	
	North = function(o1, o2)
		local x1, _, _ = o1:GetPosXYZ()
		local x2, _, _ = o2:GetPosXYZ()
		return x2 < x1
	end,
	
	West = function(o1, o2)
		local _, y1, _ = o1:GetPosXYZ()
		local _, y2, _ = o2:GetPosXYZ()
		return y1 < y2
	end,
	
	East = function(o1, o2)
		local _, y1, _ = o1:GetPosXYZ()
		local _, y2, _ = o2:GetPosXYZ()
		return y2 < y1
	end,
}

function Room:SortWallObjs(objs, dir)
	table.sort(objs, dirToComparitor[dir])
end

--for doors/windows
function Room:CalculateRestrictionBox(dir, wallPos, wallSize, height, width)
	local xofs, nxofs = 0, 0
	local yofs, nyofs = 0, 0
	width = Max(width, 1)
	
	if dir == "North" or dir == "South" then
		xofs = (wallSize / 2 - width * voxelSizeX / 2)
		nxofs = xofs
		if width % 2 == 0 then
			local m = (dir == "South" and -1 or 1)
			xofs = xofs + m * voxelSizeX / 2
			nxofs = nxofs - m * voxelSizeX / 2
		end
	else
		yofs = (wallSize / 2 - width * voxelSizeY / 2)
		nyofs = yofs
		if width % 2 == 0 then
			local m = (dir == "West" and -1 or 1)
			yofs = yofs + m * voxelSizeX / 2
			nyofs = nyofs - m * voxelSizeX / 2
		end
	end
	
	local maxZ = wallPos:z() + (self.size:z() * voxelSizeZ - height * voxelSizeZ)
	return box(wallPos:x() - nxofs, wallPos:y() - nyofs, wallPos:z(), wallPos:x() + xofs, wallPos:y() + yofs, maxZ)
end

function Room:FindSlabObjPos(dir, width, height)
	local sizeX, sizeY = self.size:x(), self.size:y()
	if dir == "North" or dir == "South" then
		if width > sizeX then
			print("Obj is too big")
			return false
		end
	else
		if width > sizeY then
			print("Obj is too big")
			return false
		end
	end
	
	local z = self:CalcZ() + (3 - height) * voxelSizeZ
	local angle = 0
	local sx, sy = self.position:x(), self.position:y()
	local offsx = 0
	local offsy = 0
	local max = 0
	
	if dir == "North" then
		angle = 270 * 60
		offsx = voxelSizeX
		sx = sx + halfVoxelSizeX
		max = sizeX
	elseif dir == "East" then
		angle = 0
		offsy = voxelSizeY
		sx = sx + sizeX * voxelSizeX
		sy = sy + halfVoxelSizeY
		max = sizeY
	elseif dir == "South" then
		angle = 90 * 60
		offsx = voxelSizeX
		sy = sy + sizeY * voxelSizeY
		sx = sx + halfVoxelSizeX
		max = sizeX
	elseif dir == "West" then
		angle = 180 * 60
		offsy = voxelSizeY
		sy = sy + halfVoxelSizeY
		max = sizeY
	end
	
	local iStart = width == 3 and 1 or 0
	
	for i = iStart, max - 1 do
		local x = sx + offsx * i
		local y = sy + offsy * i
		
		local newPos = point(x, y, z)
		local canPlace = not IntersectWallObjs(nil, newPos, width, height, angle)
		
		if canPlace then
			return newPos
		end
	end
	
	return false
end

function Room:NewSlabWallObj(obj, class)
	class = class or SlabWallObject
	return class:new(obj)
end

function Room:ForEachSpawnedWindow(func, ...)
	for _, t in sorted_pairs(self.spawned_windows or empty_table) do
		for i = #t, 1, -1 do
			func(t[i], ...)
		end
	end
end

function Room:ForEachSpawnedDoor(func, ...)
	for _, t in sorted_pairs(self.spawned_doors or empty_table) do
		for i = #t, 1, -1 do --functor may del
			func(t[i], ...)
		end
	end
end

function Room:ForEachSpawnedWallObj(func, ...) --doors and windows
	self:ForEachSpawnedDoor(func, ...)
	self:ForEachSpawnedWindow(func, ...)
end

function Room:PlaceWallObj(val, side, class)
	local dir = side or self.selected_wall
	assert(dir)
	if not dir then return end
	--check for collision, pick pos
	local freePos = self:FindSlabObjPos(dir, val.width, val.height)
	
	if not freePos then
		print("No free pos found!")
		return
	end
	local wallPos, wallSize, center = self:GetWallPos(dir)
	local obj = self:NewSlabWallObj({
			entity = false, room = self, material = val.mat or "Planks",
			building_class = val.building_class or nil, building_template = val.building_template or nil,
			side = dir
		},
		class.class)
	local a = slabDirToAngle[dir]
	local zPosOffset = (3 - val.height) * voxelSizeZ
	local vx, vy, vz, va = WallWorldToVoxel(freePos:x(), freePos:y(), wallPos:z() + zPosOffset, a)
	local pos = point(WallVoxelToWorld(vx, vy, vz, va))
	obj.room = self
	obj.floor = self.floor
	obj.subvariant = 1
	obj:SetPos(pos)
	obj:SetAngle(a)
	obj:SetProperty("width", val.width)
	obj:SetProperty("height", val.height)
	obj:AlignObj()
	obj:UpdateEntity()
	
	local container, nestedList
	if val.is_door or obj:IsDoor() then --door
		self.spawned_doors = self.spawned_doors or {}
		self.spawned_doors[dir] = self.spawned_doors[dir] or {}
		container = self.spawned_doors
	else --window
		self.spawned_windows = self.spawned_windows or {}
		self.spawned_windows[dir] = self.spawned_windows[dir] or {}
		container = self.spawned_windows
	end
	
	if container then
		table.insert(container[dir], obj)
	end
	
	if Platform.editor and IsEditorActive() then
		editor.ClearSel()
		editor.AddToSel({obj})
	end
	
	return obj
end


function Room:CalculateDecalRestrictionBox(dir, wallPos, wallSize)
	local xofs, nxofs = 0, 0
	local yofs, nyofs = 0, 0
	if dir == "North" or dir == "South" then
		xofs = wallSize / 2
		nxofs = xofs
		
		wallPos = wallPos:SetY(wallPos:y() + 100 * (dir == "North" and -1 or 1))
	else
		yofs = wallSize / 2
		nyofs = yofs
		
		wallPos = wallPos:SetX(wallPos:x() + 100 * (dir == "West" and -1 or 1))
	end
	local maxZ = wallPos:z() + (self.size:z() * voxelSizeZ) + 1
	return box(wallPos:x() - nxofs, wallPos:y() - nyofs, wallPos:z(), wallPos:x() + xofs, wallPos:y() + yofs, maxZ)
end

DefineClass.RoomDecal = {
	__parents = { "AlignedObj", "Decal", "Shapeshifter", "Restrictor", "HideOnFloorChange" },
	properties = {
		{ category = "General", id = "entity", editor = "text", default = false, no_edit = true },
	},
	flags = { cfAlignObj = true, cfDecal = true, efCollision = false, gofPermanent = true, },
}

function RoomDecal:AlignObj(pos, angle, axis)
	pos = pos or self:GetPos()
	local x, y, z = self:RestrictXYZ(pos:xyz())
	
	self:SetPos(x, y, z)
	self:SetAxisAngle(axis or self:GetAxis(), angle or self:GetAngle())
end

function RoomDecal:ChangeEntity(val)
	Shapeshifter.ChangeEntity(self, val)
	self.entity = val
end

function RoomDecal:GameInit()
	if IsChangingMap() and self.entity then
		Shapeshifter.ChangeEntity(self, self.entity)
	end
end

function RoomDecal:Done()
	local safe = rawget(self, "safe_deletion")
	if not safe then
		--decals dont have ref to the room they belong to, so the check is all weird
		local box = self.restriction_box
		if box then
			local passed = {}
			MapForEach(box:grow(100, 100, 0), "WallSlab", function(s)
				local side = s.side
				local room = s.room
				if room then
					local id = xxhash(room.handle, side)
					if not passed[id] then
						passed[id] = true
						local t = room.spawned_decals[side]
						local t_idx = table.find(t, self)
						if t_idx then
							table.remove(t, t_idx)
							ObjModified(room)
							return "break"
						end
					end
				end
			end)
		else
			local b = self:GetObjectBBox()
			local success = false
			b = b:grow(guim, guim, guim)
			EnumVolumes(b, function(r)
				local t = r.spawned_decals
				for side, tt in pairs(t or empty_table) do
					local t_idx = table.find(tt, self)
					if t_idx then
						table.remove(tt, t_idx)
						ObjModified(r)
						success = true
						return "break"
					end
				end
			end)
			
			if not success then
				assert(false, "RoomDecal not safely deleted, ref in room remains!")
			end
		end
	end
end

function Room:Setplace_decal(val)
	local dir = self.selected_wall
	if not dir then return end
	
	local wallPos, wallSize, center = self:GetWallPos(dir)
	local a = slabDirToAngle[dir]
	
	local obj = RoomDecal:new()
	obj.floor = self.floor
	obj:ChangeEntity(val.entity)
	obj:SetAngle(a)
	local xOffs = 0
	local yOffs = 0
	if dir == "East" then
		obj:SetAxis(axis_y)
		obj:SetAngle(90 * 60)
		xOffs = 100
	elseif dir == "West" then
		obj:SetAxis(axis_y)
		obj:SetAngle(-90 * 60)
		xOffs = -100
	elseif dir == "North" then
		obj:SetAxis(axis_x)
		obj:SetAngle(90 * 60)
		yOffs = -100
	elseif dir == "South" then
		obj:SetAxis(axis_x)
		obj:SetAngle(-90 * 60)
		yOffs = 100
	end
	obj:SetPos(wallPos + point(xOffs, yOffs, voxelSizeZ * self.size:z() / 2))
	obj.restriction_box = self:CalculateDecalRestrictionBox(dir, wallPos, wallSize)
	--DbgAddBox(obj.restriction_box, RGB(255, 0, 0))
	
	self.spawned_decals = self.spawned_decals or {}
	self.spawned_decals[dir] = self.spawned_decals[dir] or {}
	table.insert(self.spawned_decals[dir], obj)
	
	editor.ClearSel()
	editor.AddToSel({obj})
	
	self.place_decal = "temp"
	ObjModified(self)
	self.place_decal = ""
	ObjModified(self)
end

function Room:DeleteWallObjHelper(d)
	d:RestoreAffectedSlabs()
	DoneObject(d)
	ObjModified(self)
end

function Room:DeleteWallObjs(container, dir)
	if not dir then
		self:DeleteWallObjs("North", container)
		self:DeleteWallObjs("South", container)
		self:DeleteWallObjs("East", container)
		self:DeleteWallObjs("West", container)
		self:DeleteAllFloors()
		self:DeleteAllCornerObjs()
	else
		local t = container and container[dir]
		for i = #(t or empty_table), 1, -1 do
			if IsValid(t[i]) then --can be killed from editor
				self:DeleteWallObjHelper(t[i])
			end
			t[i] = nil
		end
		ObjModified(self)
	end
end

function Room:RebuildAllSlabs()
	self:DeleteAllSlabs()
	self:CreateAllSlabs()
	self:RecreateRoof("force")
end

function Room:DoneObjectsInNWESTable(t)
	for k, v in NSEW_pairs(t or empty_table) do
		--windows and doors are so clever that they remove themselves from these lists when deleted, which causes DoneObjects to sometimes fail
		while #v > 0 do
			local idx = #v
			DoneObject(v[idx])
			v[idx] = nil
		end
	end
end

function Room:DeleteAllSlabs()
	SuspendPassEdits("Room:DeleteAllSpawnedObjs")
	self:DeleteAllWallObjs()
	self:DeleteAllCornerObjs()
	self:DeleteAllFloors()
	self:DeleteRoofObjs()
	ResumePassEdits("Room:DeleteAllSpawnedObjs")
end

function Room:DeleteAllSpawnedObjs()
	SuspendPassEdits("Room:DeleteAllSpawnedObjs")
	self:DeleteAllWallObjs()
	self:DeleteAllCornerObjs()
	self:DeleteAllFloors()
	self:DeleteRoofObjs()
	self:DoneObjectsInNWESTable(self.spawned_doors)
	self:DoneObjectsInNWESTable(self.spawned_windows)
	self:DoneObjectsInNWESTable(self.spawned_decals)
	ResumePassEdits("Room:DeleteAllSpawnedObjs")
end

function Room:DeleteAllFloors()
	SuspendPassEdits("Room:DeleteAllFloors")
	DoneObjects(self.spawned_floors, "clear")
	ResumePassEdits("Room:DeleteAllFloors")
	Msg("RoomDestroyedFloor", self)
end

function Room:DeleteAllCornerObjs()
	for k, v in NSEW_pairs(self.spawned_corners or empty_table) do
		DoneObjects(v, "clear")
	end
end

function Room:DeleteAllWallObjs()
	SuspendPassEdits("Room:DeleteAllWallObjs")
	for k, v in NSEW_pairs(self.spawned_walls or empty_table) do
		DoneObjects(v, "clear")
	end
	ResumePassEdits("Room:DeleteAllWallObjs")
end

function Room:HasWall(mat)
	return mat ~= noneWallMat and (mat ~= defaultWallMat or self.wall_mat ~= noneWallMat)
end

function Room:HasWallOnSide(side)
	return self:GetWallMatHelperSide(side) ~= noneWallMat
end

function Room:HasAllWalls()
	for _, side in ipairs(CardinalDirectionNames) do
		if not self:HasWallOnSide(side) then
			return false
		end
	end
	
	return true
end

function Room:RecreateNWCornerBeam()
	local mat = self.north_wall_mat
	if mat == noneWallMat then
		mat = self.west_wall_mat
	end
	self:CreateCornerBeam("North", mat) --nw
end

function Room:RecreateSWCornerBeam()
	local mat = self.west_wall_mat
	if mat == noneWallMat then
		mat = self.south_wall_mat
	end
	self:CreateCornerBeam("West", mat) --sw
end

function Room:RecreateNECornerBeam()
	local mat = self.east_wall_mat
	if mat == noneWallMat then
		mat = self.north_wall_mat
	end
	self:CreateCornerBeam("East", mat) --ne
end

function Room:RecreateSECornerBeam()
	local mat = self.south_wall_mat
	if mat == noneWallMat then
		mat = self.east_wall_mat
	end
	self:CreateCornerBeam("South", mat) --se
end

function Room:CreateAllWalls()
	SuspendPassEdits("Room:CreateAllWalls")
	self:CreateWalls("North", self.north_wall_mat)
	self:CreateWalls("South", self.south_wall_mat)
	self:CreateWalls("West", self.west_wall_mat)
	self:CreateWalls("East", self.east_wall_mat)
	self:CheckWallSizes()
	ResumePassEdits("Room:CreateAllWalls")
end

function Room:CreateAllSlabs()
	SuspendPassEdits("Room:CreateAllSlabs")
	self:CreateAllWalls()
	self:CreateFloor()
	self:CreateAllCorners()
	if not self.being_placed then
		self:RecreateRoof()
	end
	self:SetWarped(self:GetWarped(), true)
	ResumePassEdits("Room:CreateAllSlabs")
end

function Room:RefreshFloorCombatStatus()
	local floorsAreCO = g_Classes.CombatObject and IsKindOf(FloorSlab, "CombatObject")
	if not floorsAreCO then return end
	
	local flr = self.floor
	local val = not self:IsRoofOnly() and flr == 1
	for i = 1, #(self.spawned_floors or "") do
		local f = self.spawned_floors[i]
		if IsValid(f) then
			f.impenetrable = val
			f.invulnerable = val
			f.forceInvulnerableBecauseOfGameRules = val
		end
	end
end

function Room:CreateFloor(mat, startI, startJ)
	mat = mat or self.floor_mat
	self.spawned_floors = self.spawned_floors or {}
	local objs = self.spawned_floors
	
	local gz = self:CalcZ()
	local sx, sy = self.position:x(), self.position:y()
	local sizeX, sizeY, sizeZ = self.size:xyz()
	
	if sizeZ <= 0 then
		self:DeleteAllFloors()
		print("<color 0 255 38>Removed floor because it is a zero height room. </color>")
		return
	end
	
	sx = sx + halfVoxelSizeX
	sy = sy + halfVoxelSizeY
	startI = startI or 0
	startJ = startJ or 0
	
	if self:GetGameFlags(const.gofPermanent) ~= 0 then
		local floorBBox = box(sx, sy, gz, sx + voxelSizeX * (sizeX - 1), sy + voxelSizeY * (sizeY - 1), gz + 1)
		ComputeSlabVisibilityInBox(floorBBox)
	end
	
	SuspendPassEdits("Room:CreateFloor")
	local insertElements = startJ ~= 0 and #objs < sizeX * sizeY
	local floorsAreCO = g_Classes.CombatObject and IsKindOf(FloorSlab, "CombatObject")
	local floorsAreInvulnerable = floorsAreCO and not self:IsRoofOnly() and self.floor == 1
	for xOffset = startI, sizeX - 1 do
		for yOffset = xOffset == startI and startJ or 0, sizeY - 1 do
			local x = sx + xOffset * voxelSizeX
			local y = sy + yOffset * voxelSizeY
			local idx = xOffset * sizeY + yOffset + 1
			
			if insertElements then
				if #objs < idx then
					objs[idx] = false
				else
					table.insert(objs, idx, false)
				end
				
				insertElements = insertElements and #objs < sizeX * sizeY
			end
			
			local floor = objs[idx]
			if not IsValid(floor) then
				floor = FloorSlab:new{floor = self.floor, material = mat, side = "Floor", room = self}
				floor:SetPos(x, y, gz)
				floor:AlignObj()
				floor:UpdateEntity()
				floor:Setcolors(self.floor_colors)
				objs[idx] = floor
			else
				floor:SetPos(x, y, gz)
				if floor.material ~= mat then
					floor.material = mat
					floor:UpdateEntity()
				else
					floor:UpdateSimMaterialId()
				end
				
			end
			
			floor.floor = self.floor
			if floorsAreCO then --zulu specific
				floor.impenetrable = floorsAreInvulnerable
				floor.invulnerable = floorsAreInvulnerable
				floor.forceInvulnerableBecauseOfGameRules = floorsAreInvulnerable
			end
		end
	end
	ResumePassEdits("Room:CreateFloor")
	Msg("RoomCreatedFloor", self, mat)
end

function Room:CreateCornerBeam(dir, mat) --corner is next clockwise corner from dir
	self.spawned_corners = self.spawned_corners or {North = {}, South = {}, West = {}, East = {}}	
	local objs = self.spawned_corners[dir]
	
	if mat == defaultWallMat then
		mat = self.wall_mat
	end
	
	local gz = self:CalcSnappedZ()
	local sx, sy = self.position:x(), self.position:y()
	local sizeX, sizeY = self.size:x(), self.size:y()
	if dir == "South" or dir == "West" then
		sy = sy + sizeY * voxelSizeY
	end
	
	if dir == "South" or dir == "East" then
		sx = sx + sizeX * voxelSizeX
	end
	
	local count = self.size:z() + 1
	if count < #objs then
		for i = #objs, count + 1, -1 do
			DoneObject(objs[i])
			objs[i] = nil
		end
	end
	
	local isPermanent = self:GetGameFlags(const.gofPermanent) ~= 0
	local sz = self.size:z()

	if sz > 0 then
		for j = 0, sz do
			local z = gz + voxelSizeZ * Min(j, self.size:z() - 1)
			local pt = point(sx, sy, z)

			local obj = objs[j + 1]
			if not IsValid(obj) then
				obj = PlaceObject("RoomCorner", {room = self, side = dir, floor = self.floor, material = mat})
				objs[j + 1] = obj
			end
			obj.isPlug = j == self.size:z()
			obj:SetPos(pt)
			obj.material = mat
			obj.invulnerable = false
			obj.forceInvulnerableBecauseOfGameRules = false
			if not isPermanent then
				obj:UpdateEntity() --corners rely on ComputeSlabVisibilityInBox to update their ents
			end
		end
	end
	
	if isPermanent then
		local box = box(sx, sy, gz, sx, sy, gz + voxelSizeZ * (self.size:z() - 1))
		ComputeSlabVisibilityInBox(box)
	end
end

function Room:GetWallMatHelperSide(side)
	local m = dirToWallMatMember[side]
	return m and self:GetWallMatHelper(self[m]) or nil
end

function Room:GetWallMatHelper(mat)
	return mat == defaultWallMat and self.wall_mat or mat
end

function Room:RecalcAllRestrictionBoxes(dir, containers)
	local wallPos, wallSize, center = self:GetWallPos(dir)
	for j = 1, #(containers or empty_table) do
		local container = containers[j]
		local t = container and container[dir]
		
		--save fixup, idk how, sometimes decal lists have false entries
		for i = #(t or ""), 1, -1 do
			if type(t[i]) == "boolean" then
				table.remove(t, i)
				print("once", "Found badly saved decals/windows/doors!")
			end
		end
		
		if t and IsKindOf(t[1], "RoomDecal") then
			for i = 1, #(t or empty_table) do
				local o = t[i]
				if o then
					o.restriction_box = self:CalculateDecalRestrictionBox(dir, wallPos, wallSize)
					o:AlignObj()
				end
			end
		end
	end
end

function Room:Resize(oldSize, newSize, oldBox)
	if oldSize == newSize then
		return
	end
	
	SuspendPassEdits("Room:Resize")
	local delta = newSize - oldSize
	
	local offsetY = delta:y() * voxelSizeY
	local offsetX = delta:x() * voxelSizeX
	local offsetZ = delta:z() * voxelSizeZ
	local sx, sy = self.position:x(), self.position:y()
	sx = sx + halfVoxelSizeX
	sy = sy + halfVoxelSizeY
	local sizeX, sizeY = newSize:x(), newSize:y()
	
	local function moveObjs(objs)
		if not objs then return end
		for i = 1, #objs do
			local o = objs[i]
			if IsValid(o) then
				local x, y, z = o:GetPosXYZ()
				o:SetPos(x + offsetX, y + offsetY, z + offsetZ)
			end
		end
	end
	
	local function moveObjX(o)
		local x, y, z = o:GetPosXYZ()
		o:SetPos(x + offsetX, y, z)
		if IsKindOf(o, "SlabWallObject") then
			o:UpdateManagedObj()
		end
	end
	
	local function moveObjsX(objs)
		if not objs then return end
		for i = 1, #objs do
			local o = objs[i]
			if IsValid(o) then
				moveObjX(o)
			end
		end
	end
	
	local function moveObjY(o)
		local x, y, z = o:GetPosXYZ()
		o:SetPos(x, y + offsetY, z)
		if IsKindOf(o, "SlabWallObject") then
			o:UpdateManagedObj()
		end
	end
	
	local function moveObjsY(objs)
		if not objs then return end
		for i = 1, #objs do
			local o = objs[i]
			if IsValid(o) then
				moveObjY(o)
			end
		end
	end
	
	local function moveObjsZ(objs)
		if not objs then return end
		for i = 1, #objs do
			local o = objs[i]
			if IsValid(o) then
				local x, y, z = o:GetPosXYZ()
				o:SetPos(x, y, z + offsetZ)
			end
		end
	end
	
	if delta:y() ~= 0 then
		--south wall moves
		moveObjsY(self.spawned_walls and self.spawned_walls.South)
		moveObjsY(self.spawned_doors and self.spawned_doors.South)
		moveObjsY(self.spawned_windows and self.spawned_windows.South)
		if self.spawned_corners then
			moveObjsY(self.spawned_corners.South)
			moveObjsY(self.spawned_corners.West)
		end
		local containers = {self.spawned_doors, self.spawned_windows, self.spawned_decals}
		self:RecalcAllRestrictionBoxes("East", containers)
		self:RecalcAllRestrictionBoxes("West", containers)
		self:RecalcAllRestrictionBoxes("South", containers)
	end
	if delta:x() ~= 0 then
		--east wall moves
		moveObjsX(self.spawned_walls and self.spawned_walls.East)
		moveObjsX(self.spawned_doors and self.spawned_doors.East)
		moveObjsX(self.spawned_windows and self.spawned_windows.East)
		if self.spawned_corners then
			moveObjsX(self.spawned_corners.South)
			moveObjsX(self.spawned_corners.East)
		end
		local containers = {self.spawned_doors, self.spawned_windows, self.spawned_decals}
		self:RecalcAllRestrictionBoxes("North", containers)
		self:RecalcAllRestrictionBoxes("East", containers)
		self:RecalcAllRestrictionBoxes("South", containers)
	end
	
	if delta:z() ~= 0 then
		if delta:z() > 0 then
			local move = delta:x() ~= 0 or delta:y() ~= 0 --needs to reorder slabs so let it go through the entire wall
			self:CreateWalls("South", self.south_wall_mat, nil, not move and oldSize:z(), nil, nil, move)
			self:CreateWalls("North", self.north_wall_mat, nil, not move and oldSize:z(), nil, nil, move)
			self:CreateWalls("East", self.east_wall_mat, nil, not move and oldSize:z(), nil, nil, move)
			self:CreateWalls("West", self.west_wall_mat, nil, not move and oldSize:z(), nil, nil, move)
		else
			self:DestroyWalls("East", nil, oldSize, nil, newSize:z())
			self:DestroyWalls("West", nil, oldSize, nil, newSize:z())
			self:DestroyWalls("North", nil, oldSize, nil, newSize:z())
			self:DestroyWalls("South", nil, oldSize, nil, newSize:z())
		end
	end
	
	if delta:y() ~= 0 then
		if delta:y() < 0 then
			local count = abs(delta:y())
			self:DestroyWalls("East", count, oldSize:SetZ(newSize:z()))
			self:DestroyWalls("West", count, oldSize:SetZ(newSize:z()))
			
			
			local floors = self.spawned_floors
			for i = oldSize:x() - 1, 0, -1 do
				for j = oldSize:y() - 1, newSize:y(), -1 do
					local idx = (i * oldSize:y()) + j + 1
					local o = floors[idx]
					if o then
						DoneObject(o)
						table.remove(floors, idx)
					end
				end
			end			
		else
			if self.spawned_walls then
				local ew = self.spawned_walls.East
				local ww = self.spawned_walls.West
				if ew then
					self:CreateWalls("East", self.east_wall_mat, newSize:y() - delta:y())
				end
				if ww then
					self:CreateWalls("West", self.west_wall_mat, newSize:y() - delta:y())
				end
			end
			
			self:CreateFloor(self.floor_mat, 0, oldSize:y())
		end
	end
	
	if delta:x() ~= 0 then
		if delta:x() < 0 then
			local count = abs(delta:x())
			self:DestroyWalls("South", count, oldSize:SetZ(newSize:z()))
			self:DestroyWalls("North", count, oldSize:SetZ(newSize:z()))
						
			local floors = self.spawned_floors
			local nc = newSize:x() * newSize:y()
			local lc = #floors - nc
			for i = 1, lc do
				local idx = #floors
				local f = floors[idx]
				DoneObject(f)
				floors[idx] = nil
			end
		else
			if self.spawned_walls then
				local sw = self.spawned_walls.South
				local nw = self.spawned_walls.North
				if sw then
					self:CreateWalls("South", self.south_wall_mat, newSize:x() - delta:x())
				end
				if nw then
					self:CreateWalls("North", self.north_wall_mat, newSize:x() - delta:x())
				end
			end
			
			self:CreateFloor(self.floor_mat, oldSize:x())
		end
	end
	
	if oldSize:z() > 0 and newSize:z() <= 0 then
		self:DeleteAllFloors()
		self:DestroyCorners()
	elseif oldSize:z() <= 0 and newSize:z() > 0 then
		self:CreateFloor(self.floor_mat)
	end
	
	self:RecreateNECornerBeam()
	self:RecreateSECornerBeam()
	self:RecreateNWCornerBeam()
	self:RecreateSWCornerBeam()
	
	if not self.being_placed then
		self:RecreateRoof()
	end
	
	ResumePassEdits("Room:Resize")
	self:CheckWallSizes()
end

function Room:DestroyCorners()
	for side, t in NSEW_pairs(self.spawned_corners or empty_table) do
		DoneObjects(t)
		self.spawned_corners[side] = {}
	end
end

function Room:MoveAllSpawnedObjs(dvx, dvy, dvz)
	local offsetX = dvx * voxelSizeX
	local offsetY = dvy * voxelSizeY
	local offsetZ = dvz * voxelSizeZ

	if offsetX == 0 and offsetY == 0 and offsetZ == 0 then
		return
	end
	
	SuspendPassEdits("Room:MoveAllSpawnedObjs")
	local function move(o)
		if not IsValid(o) then return end
		local x, y, z = o:GetPosXYZ()
		o:SetPos(x + offsetX, y + offsetY, z + offsetZ)
		--align should not be required, omitted for performance
	end
	
	local function move_window(o)
		if not IsValid(o) then return end
		local x, y, z = o:GetPosXYZ()
		o:SetPos(x + offsetX, y + offsetY, z + offsetZ)
		o:AlignObj() --specifically so small windows realign managed slabs, t.t
	end
	
	local function iterateNSWETable(t, m)
		m = m or move
		for _, st in NSEW_pairs(t or empty_table) do
			for i = 1, #st do
				local o = st[i]
				m(o)
			end
		end
	end
	
	for i = 1, #(self.spawned_floors or empty_table) do
		move(self.spawned_floors[i])
	end
	
	iterateNSWETable(self.spawned_walls)
	iterateNSWETable(self.spawned_corners)
	for i = 1, #(self.roof_objs or empty_table) do
		move(self.roof_objs[i])
	end
	-- move doors & windows after roof_objs, otherwise doors & windows won't find their "main_wall"
	iterateNSWETable(self.spawned_doors)
	iterateNSWETable(self.spawned_windows, move_window)
	iterateNSWETable(self.spawned_decals)
	
	local containers = {self.spawned_doors, self.spawned_windows, self.spawned_decals}
	self:RecalcAllRestrictionBoxes("East", containers)
	self:RecalcAllRestrictionBoxes("West", containers)
	self:RecalcAllRestrictionBoxes("South", containers)
	self:RecalcAllRestrictionBoxes("North", containers)

	ResumePassEdits("Room:MoveAllSpawnedObjs")
end

function Room:DestroyWalls(dir, count, size, startJ, endJ)
	local objs = self.spawned_walls and self.spawned_walls[dir]
	local wnd = self.spawned_windows and self.spawned_windows[dir]
	local doors = self.spawned_doors and self.spawned_doors[dir]
	local len = 0
	local offsX = 0
	local flatOffsX = 0
	local offsY = 0
	local flatOffsY = 0
	local sx, sy = self.position:x(), self.position:y()
	local mat
	sx = sx + halfVoxelSizeX
	sy = sy + halfVoxelSizeY
	size = size or self.size
	
	if dir == "North" then
		len = size:x()
		mat = self:GetWallMatHelper(self.north_wall_mat)
		offsX = voxelSizeX
		flatOffsY = -voxelSizeY / 2
	elseif dir == "East" then
		len = size:y()
		mat = self:GetWallMatHelper(self.east_wall_mat)
		flatOffsX = voxelSizeX / 2
		offsY = voxelSizeY
		sx = sx + (size:x() - 1) * voxelSizeX
	elseif dir == "South" then
		len = size:x()
		mat = self:GetWallMatHelper(self.south_wall_mat)
		offsX = voxelSizeX
		flatOffsY = voxelSizeY / 2
		sy = sy + (size:y() - 1) * voxelSizeY
	elseif dir == "West" then
		len = size:y()
		mat = self:GetWallMatHelper(self.west_wall_mat)
		flatOffsX = -voxelSizeX / 2
		offsY = voxelSizeY
	end
	startJ = startJ or size:z()
	endJ = endJ or 0
	count = count or len
	local gz = self:CalcZ()
	
	SuspendPassEdits("Room:DestroyWalls")
	
	self:SortWallObjs(doors or empty_table, dir)
	self:SortWallObjs(wnd or empty_table, dir)
	
	for i = len - 1, len - count, -1 do
		for j = startJ - 1, endJ, -1 do
			if endJ == 0 then
				--clear wind/door
				local px = sx + i * offsX + flatOffsX
				local py = sy + i * offsY + flatOffsY
				local pz = gz + j * voxelSizeZ
				local p = point(px, py, pz)
			end
			
			if objs and #objs > 0 then
				local idx = i * size:z() + j + 1
				local o = objs[idx]
				DoneObject(o)
				if #objs >= idx then
					table.remove(objs, idx)
				end
			end
		end
	end
	
	local containers = {self.spawned_decals}
	self:RecalcAllRestrictionBoxes(dir, containers)
	self:TouchWallsAndWindows(dir)
	ResumePassEdits("Room:DestroyWalls")
	Msg("RoomDestroyedWall", self, dir)
end

function Room:GetWallSlabPos(dir, idx)
	local x, y, z = self.position:xyz()
	local sizeX, sizeY, sizeZ = self.size:xyz()
	
	assert(voxelSizeX == voxelSizeY)
	local offs = ((idx - 1) / sizeZ) * voxelSizeX + halfVoxelSizeX
	z = z + ((idx - 1) % sizeZ) * voxelSizeZ
	
	if dir == "North" then
		x = x + offs
	elseif dir == "South" then
		x = x + offs
		y = y + sizeY * voxelSizeY
	elseif dir == "West" then
		y = y + offs
	else --dir == "East"
		y = y + offs
		x = x + sizeX * voxelSizeX
	end
	
	return x, y, z
end

function Room:TestAllWallPositions()
	self:TestWallPositions("North")
	self:TestWallPositions("South")
	self:TestWallPositions("West")
	self:TestWallPositions("East")
end

function Room:TestWallPositions(dir)
	self.spawned_walls = self.spawned_walls or {North = {}, South = {}, East = {}, West = {}}
	dir = dir or "North"
	local objs = self.spawned_walls[dir]
	
	local gz = self:CalcZ()
	local angle = 0
	local sx, sy = self.position:x(), self.position:y()
	--local size = oldSize or self.size
	local size = self.size
	local sizeX, sizeY, sizeZ = size:x(), size:y(), size:z()
	local offsx = 0
	local offsy = 0
	
	local endI = (dir == "North" or dir == "South") and sizeX or sizeY
	local endJ = sizeZ
	local startI = 0
	local startJ = 0
	
	if dir == "North" then
		angle = 270 * 60
		offsx = voxelSizeX
		sx = sx + halfVoxelSizeX
	elseif dir == "East" then
		angle = 0
		offsy = voxelSizeY
		sx = sx + sizeX * voxelSizeX
		sy = sy + halfVoxelSizeY
	elseif dir == "South" then
		angle = 90 * 60
		offsx = voxelSizeX
		sy = sy + sizeY * voxelSizeY
		sx = sx + halfVoxelSizeX
	elseif dir == "West" then
		angle = 180 * 60
		offsy = voxelSizeY
		sy = sy + halfVoxelSizeY
	end
	
	local insertElements = startJ ~= 0 and #objs < ((dir == "North" or dir == "South") and sizeX or sizeY) * sizeZ
	
	for i = startI, endI - 1 do
		for j = startJ, endJ - 1 do
			local px = sx + i * offsx
			local py = sy + i * offsy
			local z = gz + j * voxelSizeZ
			local idx = i * sizeZ + j + 1
			
			local s = objs[idx]
			if not s or s:GetPos() ~= point(px, py, z) then
				print(dir, idx)
			end
		end
	end
end

function Room:CreateWalls(dir, mat, startI, startJ, endI, endJ, move)
	self.spawned_walls = self.spawned_walls or {North = {}, South = {}, East = {}, West = {}}
	mat = mat or "Planks"
	dir = dir or "North"
	local objs = self.spawned_walls[dir]
	
	if mat == defaultWallMat then
		mat = self.wall_mat
	end
	
	local oppositeDir = nil
	local gz = self:CalcZ()
	local angle = 0
	local sx, sy = self.position:x(), self.position:y()
	local size = self.size
	local sizeX, sizeY, sizeZ = size:x(), size:y(), size:z()
	local offsx = 0
	local offsy = 0
	
	endI = endI or (dir == "North" or dir == "South") and sizeX or sizeY
	endJ = endJ or sizeZ
	startI = startI or 0
	startJ = startJ or 0
	
	if dir == "North" then
		angle = 270 * 60
		offsx = voxelSizeX
		sx = sx + halfVoxelSizeX
		oppositeDir = "South"
	elseif dir == "East" then
		angle = 0
		offsy = voxelSizeY
		sx = sx + sizeX * voxelSizeX
		sy = sy + halfVoxelSizeY
		oppositeDir = "West"
	elseif dir == "South" then
		angle = 90 * 60
		offsx = voxelSizeX
		sy = sy + sizeY * voxelSizeY
		sx = sx + halfVoxelSizeX
		oppositeDir = "North"
	elseif dir == "West" then
		angle = 180 * 60
		offsy = voxelSizeY
		sy = sy + halfVoxelSizeY
		oppositeDir = "East"
	end
	
	if self:GetGameFlags(const.gofPermanent) ~= 0 then
		local wallBBox = self:GetWallBox(dir)
		ComputeSlabVisibilityInBox(wallBBox)
	end
	
	SuspendPassEdits("Room:CreateWalls")
	local insertElements = startJ ~= 0 and #objs < ((dir == "North" or dir == "South") and sizeX or sizeY) * sizeZ
	local forceUpdate = self.last_wall_recreate_seed ~= self.seed
	local isLoadingMap = IsChangingMap()
	local affectedRooms = {}
	
	for i = startI, endI - 1 do
		for j = startJ, endJ - 1 do
			local px = sx + i * offsx
			local py = sy + i * offsy
			local z = gz + j * voxelSizeZ
			local idx = i * sizeZ + j + 1
			local m = mat
			
			if insertElements then
				if idx > #objs then
					objs[idx] = false --Happens when resizing in (x or y) ~= 0 and z ~= 0 in the same time. Fill it in, second pass will fill in the missing ones without breaking integrity, probably..
				else
					table.insert(objs, idx, false)
				end
			end
			
			local wall = objs[idx]
			if not IsValid(wall) then
				wall = WallSlab:new{floor = self.floor, material = m, room = self, side = dir, 
										variant = self.inner_wall_mat ~= noneWallMat and "OutdoorIndoor" or "Outdoor", indoor_material_1 = self.inner_wall_mat}
				wall:SetAngle(angle)
				wall:SetPos(px, py, z)
				wall:AlignObj()
				wall:UpdateEntity()
				wall:UpdateVariantEntities()
				
				wall:Setcolors(self.outer_colors)
				wall:Setinterior_attach_colors(self.inner_colors)
				wall.invulnerable = false
				wall.forceInvulnerableBecauseOfGameRules = false
				objs[idx] = wall
			else
				if move then
					--sometimes if we change both z and x or y sizing at the same time we end up with the same number of slabs per wall but they need to be re-arranged.
					wall:SetAngle(angle)
					local op = wall:GetPos()
					wall:SetPos(px, py, z)
					if op ~= wall:GetPos() and wall.wall_obj then
						local o = wall.wall_obj
						o:RestoreAffectedSlabs()
					end
					wall:AlignObj()
				end
				wall:UpdateSimMaterialId()
			end
			
			if not isLoadingMap then
				if forceUpdate or wall.material ~= m or wall.indoor_material_1 ~= self.inner_wall_mat then
					wall.material = m
					wall.indoor_material_1 = self.inner_wall_mat
					wall:UpdateEntity()
					wall:UpdateVariantEntities()
				end
			end
		end
	end
	
	affectedRooms[self] = nil
	for room, isMySide in pairs(affectedRooms) do
		if IsValid(room) then
			local d = isMySide and dir or oppositeDir
			room:TouchWallsAndWindows(d)
			room:TouchCorners(d)
		end
	end
	
	self:TouchWallsAndWindows(dir)
	self:TouchCorners(dir)
	ResumePassEdits("Room:CreateWalls")
	Msg("RoomCreatedWall", self, dir, mat)
end

local postComputeBatch = false
function Room:SetInnerMaterialToRoofObjs()
	local objs = self.roof_objs
	if not objs or #objs <= 0 then return end
	
	local passedSWO = {}
	local col = self.inner_colors
	for i = 1, #objs do
		local o = objs[i]
		if IsValid(o) and IsKindOf(o, "WallSlab") then
			if o.indoor_material_1 ~= self.inner_wall_mat then
				o.indoor_material_1 = self.inner_wall_mat
				o:UpdateVariantEntities()
			end
			o:Setinterior_attach_colors(col)
			
			local swo = o.wall_obj
			if swo and not passedSWO[swo] then
				passedSWO[swo] = true
			end
		end
	end
	
	if next(passedSWO) then
		--in some cases slabs need to recalibrate in computeslabvisibility, we need to pass after that
		postComputeBatch = postComputeBatch or {}
		postComputeBatch[#postComputeBatch + 1] = passedSWO
	end
	
	ComputeSlabVisibilityInBox(self.roof_box)
end

function Room:SetInnerMaterialToSlabs(dir)
	local objs = self.spawned_walls and self.spawned_walls[dir]
	local gz = self:CalcZ()
	local sizeX, sizeY = self.size:x(), self.size:y()
	local endI = (dir == "North" or dir == "South") and sizeX or sizeY
	local passedSWO = {}
	local wallBBox = box()
	local col = self.inner_colors
	if objs then
		for i = 0, endI - 1 do
			for j = 0, self.size:z() - 1 do
				local idx = i * self.size:z() + j + 1
				local o = objs[idx]
				if IsValid(o) then
					wallBBox = Extend(wallBBox, o:GetPos())
					if o.indoor_material_1 ~= self.inner_wall_mat then
						o.indoor_material_1 = self.inner_wall_mat
						o:UpdateVariantEntities()
					end
					o:Setinterior_attach_colors(col)
					
					local swo = o.wall_obj
					if swo and not passedSWO[swo] then
						passedSWO[swo] = true
					end
				end
			end
		end
	end
	
	objs = self.spawned_corners[dir]
	for i = 1, #(objs or "") do
		if IsValid(objs[i]) then --can be gone
			objs[i]:SetColorFromRoom()
		end
	end
	
	if next(passedSWO) then
		--in some cases slabs need to recalibrate in computeslabvisibility, we need to pass after that
		postComputeBatch = postComputeBatch or {}
		postComputeBatch[#postComputeBatch + 1] = passedSWO
	end
	
	if self:GetGameFlags(const.gofPermanent) ~= 0 then
		ComputeSlabVisibilityInBox(wallBBox)
	end
	self:CreateAllCorners()
end

function OnMsg.SlabVisibilityComputeDone()
	if not postComputeBatch then return end
	
	local allPassed = {}
	for i, batch in ipairs(postComputeBatch) do
		for swo, _ in pairs(batch) do
			if not allPassed[swo] then
				allPassed[swo] = true
				swo:UpdateManagedSlabs()
				swo:UpdateManagedObj()
				swo:RefreshColors()
			end
		end
	end
	postComputeBatch = false
end

function TouchWallsAndWindowsHelper(objs)
	if not objs then return end
	table.validate(objs)
	for i = #(objs or empty_table), 1, -1 do
		local o = objs[i]
		o:AlignObj()
		if o.room == false then
			DoneObject(o)
		else
			o:UpdateSimMaterialId()
		end
	end
end

function Room:TouchWallsAndWindows(side)
	TouchWallsAndWindowsHelper(self.spawned_doors and self.spawned_doors[side])
	TouchWallsAndWindowsHelper(self.spawned_windows and self.spawned_windows[side])
end

function Room:TouchCorners(side)
	if side == "North" then
		self:RecreateNECornerBeam()
		self:RecreateNWCornerBeam()
	elseif side == "South" then
		self:RecreateSECornerBeam()
		self:RecreateSWCornerBeam()
	elseif side == "East" then
		self:RecreateSECornerBeam()
		self:RecreateNECornerBeam()
	elseif side == "West" then
		self:RecreateSWCornerBeam()
		self:RecreateNWCornerBeam()
	end
end

function GedOpViewRoom(socket, obj)
	if IsValid(obj) then
		Room.CenterCameraOnMe(nil, obj)
	else
		print("No room selected.")
	end
end

function GedOpNewVolume(socket, obj)
	print("Use f3 -> map -> new room or ctrl+shift+n instead. This method is no longer supported.")
end

function Room:SelectWall(side)
	self:ClearBoldedMarker()
	self.selected_wall = side
	local m = self.text_markers[side]
	m:SetTextStyle("EditorTextBold")
	m:SetColor(RGB(0, 255, 0))
	ObjModified(self)
end

function Room:ViewNorthWallFromOutside()
	self:SelectWall("North")
	self:ViewWall("North")
	ObjModified(self)
end

function Room:ViewSouthWallFromOutside()
	self:SelectWall("South")
	self:ViewWall("South")
	ObjModified(self)
end

function Room:ViewWestWallFromOutside()
	self:SelectWall("West")
	self:ViewWall("West")
	ObjModified(self)
end

function Room:ViewEastWallFromOutside()
	self:SelectWall("East")
	self:ViewWall("East")
	ObjModified(self)
end

function Room:ClearBoldedMarker()
	if self.selected_wall then
		local m = self.text_markers[self.selected_wall]
		m:SetTextStyle("EditorText")
		m:SetColor(RGB(255, 0, 0))
	end
end

function Room:ClearSelectedWall()
	self:ClearBoldedMarker()
	self.selected_wall = false
	ObjModified(self)
end

function GetSelectedRoom()
	--find a selected room..
	return SelectedRooms and SelectedRooms[1] or SelectedVolume
end

function SelectedRoomClearSelectedWall()
	local r = GetSelectedRoom()
	if IsValid(r) then
		r:ClearSelectedWall()
		print("Cleared selected wall")
	else
		print("No selected room found!")
	end
end

function SelectedRoomSelectWall(side)
	local r = GetSelectedRoom()
	if IsValid(r) then
		r:SelectWall(side)
		print(string.format("Selected wall %s of room %s", side, r.name))
	else
		print("No selected room found!")
	end
end

function SelectedRoomResetWallMaterials()
	local r = GetSelectedRoom()
	if IsValid(r) then
		if r.selected_wall then
			local side = r.selected_wall
			local matMember = string.format("%s_wall_mat", string.lower(side))
			local curMat = r[matMember]
			if curMat ~= defaultWallMat then
				r[matMember] = defaultWallMat
				local matPostSetter = string.format("OnSet%s", matMember)
				r[matPostSetter](r, defaultWallMat, curMat)
			end
		else
			r:ResetWallMaterials()
			print(string.format("Reset wall materials."))
		end
	else
		print("No selected room found!")
	end
end

function Room:CycleWallMaterial(delta, side)
	local mats
	local matMember = "wall_mat"
	if side then
		mats = SlabMaterialComboItemsWithDefault()()
		matMember = string.format("%s_wall_mat", string.lower(side))
	else
		mats = SlabMaterialComboItemsWithNone()()
	end
	local matPostSetter = string.format("OnSet%s", matMember)
	local curMat = self[matMember]
	local idx = table.find(mats, curMat) or 1
	local newIdx = idx + delta
	if newIdx > #mats then
		newIdx = 1
	elseif newIdx <= 0 then
		newIdx = #mats
	end
	local newMat = mats[newIdx]
	self[matMember] = newMat
	self[matPostSetter](self, newMat, curMat)
	print(string.format("Changed wall material of room %s side %s new material %s", self.name, side or "all", newMat))
end

function Room:CycleEntity(delta)
	local sw = self.selected_wall
	if not sw then
		self:CycleWallMaterial(delta)
		return
	end
	self:CycleWallMaterial(delta, sw)
end

function Room:UIDeleteDoors()
	self:DeleteWallObjs(self.spawned_doors, self.selected_wall)
end

function Room:UIDeleteWindows()
	self:DeleteWallObjs(self.spawned_windows, self.selected_wall)
end

local decalIdPrefix = "decal_lst_"
function Room:UIDeleteDecal(gedRoot, prop_id)
	local sh = string.gsub(prop_id, decalIdPrefix, "")
	local h = tonumber(sh)
	
	local t = self.spawned_decals[self.selected_wall]
	local idx = table.find(t, "handle", h)
	if idx then
		local d = t[idx]
		table.remove(t, idx)
		rawset(d, "safe_deletion", true)
		DoneObject(d)
		ObjModified(self)
	end
end

function Room:UISelectDecal(gedRoot, prop_id)
	local sh = string.gsub(prop_id, decalIdPrefix, "")
	local h = tonumber(sh)
	
	local t = self.spawned_decals[self.selected_wall]
	local idx = table.find(t, "handle", h)
	if idx then
		local d = t[idx]
		if d then
			editor.ClearSel()
			editor.AddToSel({d})
		end
	end
end

function Room:GetWallPos(dir, zOffset)
	local wallSize, wallPos
	local wsx = self.size:x() * voxelSizeX
	local wsy = self.size:y() * voxelSizeY
	local pos = self:GetPos()
	if zOffset then
		pos = pos:SetZ(pos:z() + zOffset)
	end
	
	if dir == "North" then
		wallPos = point(pos:x(), pos:y() - wsy / 2, pos:z())
		wallSize = wsx
	elseif dir == "South" then
		wallPos = point(pos:x(), pos:y() + wsy / 2, pos:z())
		wallSize = wsx
	elseif dir == "West" then
		wallPos = point(pos:x() - wsx / 2, pos:y(), pos:z())
		wallSize = wsy
	elseif dir == "East" then
		wallPos = point(pos:x() + wsx / 2, pos:y(), pos:z())
		wallSize = wsy
	end
	
	return wallPos, wallSize, pos
end

function Room:ViewWall(dir, inside)
	dir = dir or "North"
	local wallPos, wallSize, pos = self:GetWallPos(dir, self.size:z() * voxelSizeZ / 2)
	
	--fit wall to screen
	local fovX = camera.GetFovX()
	local a = (180 * 60 - fovX) / 2
	local wallWidth = wallSize
	local s = MulDivRound(wallWidth, sin(a), sin(fovX))
	local x = wallWidth / 2
	local dist = sqrt(s * s - x * x)
	
	local fovY = camera.GetFovY()
	a = (180 * 60 - fovY) / 2
	local wallHeight = self.size:z() * voxelSizeZ
	s = MulDivRound(wallHeight, sin(a), sin(fovY))
	x = wallHeight / 2
	dist = Max(sqrt(s * s - x * x), dist)
	
	local offset
	if inside then
		offset = pos - wallPos
	else
		offset = wallPos - pos
	end
	
	dist = dist + 3 * guim --some x margin so wall edge is not stuck to the screen edge
	offset = SetLen(offset, dist)
	offset = offset:SetZ(offset:z() + self.size:z() * voxelSizeZ * 3) --move eye up a little bit
	
	local cPos, cLookAt, cType = GetCamera()
	local cam = _G[string.format("camera%s", cType)]
	cam.SetCamera(wallPos + offset, wallPos, 1000, "Cubic out")
	
	if rawget(terminal, "BringToTop") then
		return terminal.BringToTop()
	end
end

function Room.CenterCameraOnMe(_, self)
	local cPos, cLookAt, cType = GetCamera()
	local cOffs = cPos - cLookAt
	local mPos = self:GetPos()
	local cam = _G[string.format("camera%s", cType)]
	if cType == "Max" then
		local len = 20*guim * ((Max(self.size:x(), self.size:y()) / 10) + 1)
		cOffs = SetLen(cOffs, len)
	end
	cam.SetCamera(mPos + cOffs, mPos, 1000, "Cubic out")
	
	if rawget(terminal, "BringToTop") then
		return terminal.BringToTop()
	end
end

local defaultDecalProp = { category = "Materials", id = decalIdPrefix, name = "Decal ", editor = "text", default = "", read_only = true,
										buttons = {
											{name = "Delete", func = "UIDeleteDecal"},
											{name = "Select", func = "UISelectDecal"},
										},}

local function AddDecalPropsFromContainerHelper(self, props, container, idx, defaultProp)
	for i = 1, #container do
		local np = table.copy(defaultProp)
		local obj = container[i]
		np.id = string.format("%s%s", np.id, obj.handle)
		np.name = string.format("%s%s", np.name, obj:GetEntity())
		
		table.insert(props, idx + i, np)
	end
end

function Room:GetProperties()
	local decals = self.spawned_decals and self.spawned_decals[self.selected_wall]
	
	if #(decals or empty_table) > 0 then
		local p = table.copy(self.properties)
		
		if decals then
			local idx = table.find(p, "id", "place_decal")
			AddDecalPropsFromContainerHelper(self, p, decals, idx, defaultDecalProp)
		end
		
		return p
	else
		return self.properties
	end
end

function Room:GenerateName()
	return string.format("Room %d%s", self.handle, self:IsRoofOnly() and " - Roof only" or "")
end

function Room:Init()
	self.name = self.name or self:GenerateName()
	
	if self.auto_add_in_editor then
		self:AddInEditor()
	end
end

function ComputeVisibilityOfNearbyShelters()
	--stub
end

function Room:ClearRoomAdjacencyData()
	self:ClearAdjacencyData()
end

function Room:RoomDestructor()
	Msg("RoomDone", self)
	local wasPermanent = self:GetGameFlags(const.gofPermanent) ~= 0
	self:ClearGameFlags(const.gofPermanent) --this is to dodge this assert -> assert(false, "Passability rebuild provoked from destructor! Obj class: " .. (obj.class or "N/A"))
	self:DeleteAllSpawnedObjs()
	self:ClearRoomAdjacencyData()
	
	if GedRoomEditor then
		table.remove_entry(GedRoomEditorObjList, self)
		ObjModified(GedRoomEditorObjList)
		if SelectedVolume == self then
			SetSelectedVolumeAndFireEvents(false)
			--todo: this does not work to clear the props pane
			GedRoomEditor:UnbindObjs("SelectedObject")
			ObjModified(GedRoomEditorObjList)
		end
	end
	
	if wasPermanent then
		ComputeSlabVisibilityInBox(self.box) --since we are no longer gofPermanent, call directly
		ComputeVisibilityOfNearbyShelters(self.box)
	end
	self["RoomDestructor"] = empty_func
end

function Room:ComputeRoomVisibility()
	if self:GetGameFlags(const.gofPermanent) ~= 0 then
		ComputeSlabVisibilityInBox(self.box)
	end
end

function Room:OnEditorDelete()
	self:VolumeDestructor()
	self:RoomDestructor()
end

function Room:Done()
	self:RoomDestructor()
	self.spawned_walls = nil
	self.spawned_corners = nil
	self.spawned_floors = nil
	self.spawned_doors = nil
	self.spawned_windows = nil
	self.spawned_decals = nil
end

function Room:AddInEditor()
	if GedRoomEditor then
		table.insert_unique(GedRoomEditorObjList, self)
		ObjModified(GedRoomEditorObjList)
	end
end

function WallObjToNestedListEntry(d, cls)
	cls = cls or "DoorNestedListEntry"
	local entry = PlaceObject(cls)
	entry.linked_obj = d
	entry.width = d.width
	entry.material = d.material
	entry.subvariant = d.subvariant or 1
	return entry
end

function Room:TestCorners()
	for k, v in NSEW_pairs(self.spawned_corners or empty_table) do
		for i = 1, #v do
			if not IsValid(v[i]) then
				print(k, v[i])
			end
		end
	end
end

function Room:AssignPropValuesToMySlabs()
	-- assign prop values that are convenient to have per slab but are not saved per slab
	local reposition = RepositionWallSlabsOnLoad
	for i = 1, #room_NSWE_lists do
		for side, objs in NSEW_pairs(self[room_NSWE_lists[i]] or empty_table) do
			local isDecals = "spawned_decals" == room_NSWE_lists[i]
			local isCorners = "spawned_corners" == room_NSWE_lists[i]
			local isWalls = "spawned_walls" == room_NSWE_lists[i]
			local isFloors = "spawned_floors" == room_NSWE_lists[i]
			local isWindows = "spawned_windows" == room_NSWE_lists[i]
			local isDoors = "spawned_doors" == room_NSWE_lists[i]
			for j = 1, #objs do
				local obj = objs[j]
				if obj then
					obj.room = self
					obj.side = side
					obj.floor = self.floor
					
					if not isDecals then -- could be a decal from spawned_decals
						obj.invulnerable = obj.forceInvulnerableBecauseOfGameRules
					end
					
					if isCorners and j == #objs then
						obj.isPlug = true -- last one is always a plug
					end
					if isWalls or isCorners then
						obj:DelayedUpdateEntity() -- call this after room is set for the correct seed
					end
					if isWalls then
						obj:DelayedUpdateVariantEntities() -- default colors are not available 'till .room gets assigned, this will reapply them to attaches
					end
					
					if reposition and isWalls then
						obj:SetPos(self:GetWallSlabPos(side, j))
					end
				end
			end
		end
	end
	
	local floorsAreCO = g_Classes.CombatObject and IsKindOf(FloorSlab, "CombatObject")
	for i = 1, #room_regular_lists do
		local isFloors = room_regular_lists[i] == "spawned_floors"
		local t = self[room_regular_lists[i]] or empty_table
		local side = room_regular_list_sides[i]
		for j = #t, 1, -1 do
			local o = t[j]
			if o then
				o.room = self
				o.side = side
				o.floor = self.floor
				o.invulnerable = o.forceInvulnerableBecauseOfGameRules
				
				if isFloors then
					o:DelayedUpdateEntity() -- these now have random ents as well, so rerandomize after seed is setup
				end
			end
		end
	end
end

function Room:CreateAllCorners()
	self:RecreateNECornerBeam()
	self:RecreateNWCornerBeam()
	self:RecreateSWCornerBeam()
	self:RecreateSECornerBeam()
end

-- used manually when resaving maps from old schema to new shcema
function RecreateAllCornersAndColors()
	MapForEach("map", "RoomCorner", DoneObject)
	MapForEach("map", "Room", function(room)
		room:RecreateWalls()
		room:RecreateFloor()
		room:RecreateRoof()
		room:OnSetouter_colors(room.outer_colors)
		room:OnSetinner_colors(room.inner_colors)
	end)
end

function RefreshAllRoomColors()
	MapForEach("map", "Room", function(room)
		room:OnSetouter_colors(room.outer_colors)
		room:OnSetinner_colors(room.inner_colors)
	end)
end

function Room:SaveFixups()
	local hasCeiling = type(self.roof_objs) == "table" and IsKindOf(self.roof_objs[#self.roof_objs], "CeilingSlab") or false
	if not self.build_ceiling and hasCeiling then
		-- tweaked this default value, so now there are blds with disabled ceiling who have ceilings
		-- because we avoid touching roofs during load they remain
		while IsKindOf(self.roof_objs[#self.roof_objs], "CeilingSlab") do
			local o = self.roof_objs[#self.roof_objs]
			self.roof_objs[#self.roof_objs] = nil
			DoneObject(o)
		end
	end
end

function Room:Getlocked_slabs_count()
	local total, locked = 0, 0
	
	local function iterateAndCount(t)
		for i = 1, #(t or "") do
			local slab = t[i]
			if IsValid(slab) and slab.isVisible then
				total = total + 1
				locked = locked + (slab.subvariant ~= -1 and 1 or 0)
			end
		end
	end
	
	local function iterateAndCountNSEW(objs)
		for side, t in NSEW_pairs(objs or empty_table) do
			iterateAndCount(t)
		end
	end
	
	iterateAndCountNSEW(self.spawned_walls)
	local ws = string.format("%d/%d walls", locked, total)
	locked, total = 0, 0
	iterateAndCountNSEW(self.spawned_corners)
	local cs = string.format("%d/%d corners", locked, total)
	locked, total = 0, 0
	iterateAndCount(self.spawned_floors)
	local fs = string.format("%d/%d floors", locked, total)
	locked, total = 0, 0
	iterateAndCount(self.roof_objs)
	local rs = string.format("%d/%d roof objs", locked, total)
	
	return string.format("%s; %s; %s; %s;", ws, cs, fs, rs)
end

function Room:LockAllSlabsToCurrentSubvariants()
	-- goes through all slabs and switches -1 subvariant val to their current subvariant.
	-- this will lock those variants in case of random generator changes
	
	local function iterateAndSet(t)
		for i = 1, #(t or "") do
			local slab = t[i]
			if IsValid(slab) and slab.isVisible then
				slab:LockSubvariantToCurrentEntSubvariant()
			end
		end
	end
	
	local function iterateAndSetNSEW(objs)
		for side, t in NSEW_pairs(objs or empty_table) do
			iterateAndSet(t)
		end
	end
	
	iterateAndSetNSEW(self.spawned_walls)
	iterateAndSetNSEW(self.spawned_corners)
	iterateAndSet(self.spawned_floors)
	iterateAndSet(self.roof_objs)
	ObjModified(self)
end

local function extractCpyId(str)
	local r = string.gmatch(str, "copy%d+")()
	return r and tonumber(string.gmatch(r, "%d+")()) or 0
end

function Room:GenerateNameWithCpyTag()
	local n = self.name
	local pid = extractCpyId(n)
	local topId = pid
	EnumVolumes(function(v, n, find, sub)
		local hn = v.name
		local mn = n
		if #hn < #mn then
			mn = string.sub(mn, 1, #hn)
		elseif #hn > #mn then
			hn = string.sub(hn, 1, #mn)
		end
		
		if hn == mn then
			local hpid = extractCpyId(v.name)
			if hpid > topId then
				topId = hpid
			end
		end
	end, string.gsub(n, " copy%d+", ""), string.find, string.sub)
	
	local tag = string.format("copy%d", tonumber(topId) + 1)
	if pid == 0 then
		return string.format("%s %s", self.name, tag)
	else
		return string.gsub(self.name, "copy%d+", tag)
	end
end

function Room:PostLoad(reason)
	if reason == "paste" then
		self:Setname(self:GenerateNameWithCpyTag())
	end
	
	SuspendPassEdits("Room:PostLoad")
	
	self:SaveFixups()
	self:InternalAlignObj()
	self:SetWarped(self:GetWarped(), true)
	self:AssignPropValuesToMySlabs() -- sets slab properties that are not saved - .room, .side, etc.
	
	self:RecalcRoof()
	self:ComputeRoomVisibility()
	
	ResumePassEdits("Room:PostLoad")
end

function Room:OnWallObjDeletedOutsideOfGedRoomEditor(obj)
	local dir = slabAngleToDir[obj:GetAngle()]
	local t = self[obj:IsDoor() and string.format("placed_doors_nl_%s", string.lower(dir)) or string.format("placed_windows_nl_%s", string.lower(dir))]
	local container = obj:IsDoor() and self.spawned_doors or self.spawned_windows
	if container then
		for i = 1, #(t or empty_table) do
			if t[i].linked_obj == obj then
				DoneObject(t[i])
				table.remove(t, i)
				table.remove_entry(container[dir], obj)
				return
			end
		end
	elseif Platform.developer then
		local cs = obj:IsDoor() and "spawned_doors" or "spawned_windows"
		local dirFound
		local r = MapGetFirst("map", "Room", function(o, cs, obj, et)
			for side, t in sorted_pairs(o[cs] or et) do
				if table.find(t or et, obj) then
					dirFound = side
					return true
				end
			end
		end, cs, obj, empty_table)
		
		if r then
			print(string.format("Wall obj was found in room %s, side %s container", r.name, dirFound))
		else
			print("Wall obj was not found in any room container")
		end
		
		assert(false, string.format("Room %s had no contaier initialized for wall obj with entity %s, deduced side %s, member side %s", self.name, obj.entity, dir, obj.side))
	end
end

local dirs = { "North", "East", "South", "West" }
function rotate_direction(direction, angle)
	local idx = table.find(dirs, direction)
	if not idx then return direction end
	idx = idx + angle / (90 * 60)
	if idx > 4 then idx = idx - 4 end
	return dirs[idx]
end

function Room:EditorRotate(center, axis, angle, last_angle)
	angle = angle - last_angle
	if axis:z() < 0 then angle = -angle end
	angle = (angle + 360 * 60 + 45 * 60 ) / (90 * 60) * (90 * 60)
	while angle >= 360 * 60 do angle = angle - 360 * 60 end
	if axis:z() == 0 or angle == 0 then return end

	-- rotate room properties
	local a = center + Rotate(self.box:min() - center, angle)
	local b = center + Rotate(self.box:max() - center, angle)
	self.box = boxdiag(a, b)
	self.position = self.box:min()
	local x, y, z = self.box:size():xyz()
	self.size = point(x / voxelSizeX, y / voxelSizeY, z / voxelSizeZ)
	
	-- rotate roof properties
	if self:GetRoofType() == "Gable" then
		if angle == 90 * 60 or angle == 270 * 60 then
			self.roof_direction = self.roof_direction == GableRoofDirections[1] and GableRoofDirections[2] or GableRoofDirections[1]
		end
	elseif self:GetRoofType() == "Shed" then
		self.roof_direction = rotate_direction(self.roof_direction, angle)
	end
	
	-- rotate slabs
	self:ForEachSpawnedObj(function(obj, center, angle)
		local new_angle = 0
		if not IsKindOf(obj, "FloorAlignedObj") then
			new_angle = obj:GetAngle() + angle
		end
		obj:SetPosAngle(center + Rotate(obj:GetPos() - center, angle), new_angle)
		obj.side = rotate_direction(obj.side, angle)
		if obj:IsKindOf("SlabWallObject") then obj:UpdateManagedObj() end
	end, center, angle)
	
	-- assign slabs to the proper lists after rotation
	local d = table.copy(dirs)
	while angle >= 90 * 60 do
		d[1], d[2], d[3], d[4] = d[2], d[3], d[4], d[1]
		angle = angle - 90 * 60
	end
	for i = 1, #room_NSWE_lists do
		local lists = self[room_NSWE_lists[i]]
		if lists then
			lists[d[1]], lists[d[2]], lists[d[3]], lists[d[4]] = lists.North, lists.East, lists.South, lists.West
		end
	end
	self:InternalAlignObj()
	self:RecreateRoof()
end

function OnMsg.GedClosing(ged_id)
	if GedRoomEditor and GedRoomEditor.ged_id == ged_id then
		GedRoomEditor = false
		GedRoomEditorObjList = false
	end
end

function OnMsg.GedOnEditorSelect(obj, selected, editor)
	if editor == GedRoomEditor then
		SetSelectedVolumeAndFireEvents(selected and obj or false)
	end
end

function OpenGedRoomEditor()
	CreateRealTimeThread(function()
		if not IsValid(GedRoomEditor) then
			GedRoomEditorObjList = MapGet("map", "Room") or {}
			table.sortby_field(GedRoomEditorObjList, "name")
			table.sortby_field(GedRoomEditorObjList, "structure")
			GedRoomEditor = OpenGedApp("GedRoomEditor", GedRoomEditorObjList) or false
		end
	end)
end

function OnMsg.ChangeMap()
	if GedRoomEditor then
		GedRoomEditor:Send("rfnClose")
		GedRoomEditor = false
	end
end
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
DefineClass.SlabPreset = {
	__parents = { "Preset", },
	properties = {
		{ id = "Group", no_edit = false, },
	},
	HasSortKey = false,
	PresetClass = "SlabPreset",
	NoInstances = true,
	EditorMenubarName = "Slab Presets",
	EditorMenubar = "Editors.Art",
}

DefineClass.SlabMaterialSubvariant = {
	__parents = {"PropertyObject"},
	properties = {
		{ id = "suffix", name = "Suffix", editor = "text", default = "01" },
		{ id = "chance", name = "Chance", editor = "number", default = 100 },
	},
}

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
function TouchAllRoomCorners()
	MapForEach("map", "Room", function(o)
		o:TouchCorners("North")
		o:TouchCorners("South")
		o:TouchCorners("West")
		o:TouchCorners("East")
	end)
end

DefineClass.HideOnFloorChange = {
	__parents = { "Object" },
	properties = {
		{ id = "floor", name = "Floor", editor = "number", min = -10, max = 100, default = 1, dont_save = function (obj) return obj.room end },
	},
	room = false,
	invisible_reasons = false,
}

function HideOnFloorChange:Getfloor()
	local room = self.room
	return room and room.floor or self.floor
end

HideSlab = false -- used only if defined

function HideFloorsAbove(floor, fnHide)
	SuspendPassEdits("HideFloorsAbove")
	HideFloorsAboveC(floor, fnHide or HideSlab or nil)
	Msg("FloorsHiddenAbove", floor, fnHide)
	ResumePassEdits("HideFloorsAbove")
end

function CountRoomSlabs()
	local t = 0
	MapForEach("map", "Room", function(o)
		t = t + (o.size:x() + o.size:y()) * 2 * o.size:z()
	end)
	
	return t
end

function CountMirroredSlabs()
	local t, tm = 0, 0
	
	MapForEach("map", "WallSlab", function(o)
		if o:CanMirror() and o:GetEnumFlags(const.efVisible) ~= 0 then
			if o:GetGameFlags(const.gofMirrored) ~= 0 then
				tm = tm + 1
			else
				t = t + 1
			end
		end
	end)
	
	return t, tm
end

function BuildBuildingsData()
end

function DbgWindowDoorOwnership()
	MapForEach("map", "SlabWallObject", function(o)
		if o.room then
			DbgAddVector(o:GetPos(), o.room:GetPos() - o:GetPos())
		else
			DbgAddVector(o:GetPos())
		end
	end)
end