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
use super::*;
use crate::{
    assets::AssetHandle,
    site2::{gen::PrimitiveTransform, util::Dir},
    util::{attempt, sampler::Sampler, RandomField},
    Land,
};
use common::{
    generation::{ChunkSupplement, EntityInfo},
    terrain::{Structure as PrefabStructure, StructuresGroup},
};
use kiddo::{distance::squared_euclidean, KdTree};
use lazy_static::lazy_static;
use rand::prelude::*;
use std::collections::HashMap;
use vek::*;

pub struct GnarlingFortification {
    name: String,
    seed: u32,
    origin: Vec2<i32>,
    radius: i32,
    wall_radius: i32,
    wall_segments: Vec<(Vec2<i32>, Vec2<i32>)>,
    wall_towers: Vec<Vec3<i32>>,
    // Structure indicates the kind of structure it is, vec2 is relative position of a hut compared
    // to origin, ori tells which way structure should face
    structure_locations: Vec<(GnarlingStructure, Vec3<i32>, Dir)>,
    tunnels: Tunnels,
}

enum GnarlingStructure {
    Hut,
    VeloriteHut,
    Totem,
    ChieftainHut,
    WatchTower,
    Banner,
}

impl GnarlingStructure {
    fn required_separation(&self, other: &Self) -> i32 {
        let radius = |structure: &Self| match structure {
            Self::Hut => 7,
            Self::VeloriteHut => 15,
            Self::Banner => 6,
            Self::Totem => 6,
            // Generated in different pass that doesn't use distance check
            Self::WatchTower => 0,
            Self::ChieftainHut => 0,
        };

        let additional_padding = match (self, other) {
            (Self::Banner, Self::Banner) => 50,
            (Self::Totem, Self::Totem) => 50,
            (Self::VeloriteHut, Self::VeloriteHut) => 50,
            _ => 0,
        };

        radius(self) + radius(other) + additional_padding
    }
}

impl GnarlingFortification {
    pub fn generate(wpos: Vec2<i32>, land: &Land, rng: &mut impl Rng) -> Self {
        let rpos_height = |rpos| land.get_alt_approx(rpos + wpos) as i32;

        let name = NameGen::location(rng).generate_gnarling();
        let seed = rng.gen();
        let origin = wpos;

        let wall_radius = {
            let unit_size = rng.gen_range(10..20);
            let num_units = rng.gen_range(5..10);
            let variation = rng.gen_range(0..50);
            unit_size * num_units + variation
        };

        let radius = wall_radius + 50;

        // Tunnels
        let alt = land.get_alt_approx(wpos) as i32;
        let start = wpos.with_z(alt);
        let boss_room_shift = rng.gen_range(60..110);
        let end_xy = match rng.gen_range(0..4) {
            0 => Vec2::new(start.x + boss_room_shift, start.y + boss_room_shift),
            1 => Vec2::new(start.x - boss_room_shift, start.y + boss_room_shift),
            2 => Vec2::new(start.x - boss_room_shift, start.y - boss_room_shift),
            3 => Vec2::new(start.x + boss_room_shift, start.y - boss_room_shift),
            // Unreachable
            _ => unreachable!(),
        };

        let is_underground = |pos: Vec3<i32>| land.get_alt_approx(pos.xy()) as i32 - 9 > pos.z;
        let is_valid_edge = |p1: Vec3<i32>, p2: Vec3<i32>| {
            let diff = p1 - p2;
            // Check that the new point is underground and the slope is mildish
            is_underground(p2) && (diff.z.pow(2) / diff.xy().magnitude_squared().max(1)) < 3
        };
        let mut end = end_xy.with_z(start.z - 20);
        for i in 1..31 {
            let new_z = start.z - (i * 20);
            if is_underground(end_xy.with_z(new_z + 35)) {
                end = end_xy.with_z(new_z);
                break;
            }
        }

        let tunnel_length_range = (12.0, 27.0);
        let tunnels =
            Tunnels::new(start, end, is_valid_edge, tunnel_length_range, rng).unwrap_or_default();

        let num_points = (wall_radius / 15).max(5);
        let outer_wall_corners = (0..num_points)
            .map(|a| {
                let angle = a as f32 / num_points as f32 * core::f32::consts::TAU;
                Vec2::new(angle.cos(), angle.sin()).map(|a| (a * wall_radius as f32) as i32)
            })
            .map(|point| {
                point.map(|a| {
                    let variation = wall_radius / 5;
                    a + rng.gen_range(-variation..=variation)
                })
            })
            .collect::<Vec<_>>();

        let gate_index = rng.gen_range(0..outer_wall_corners.len());

        let chieftain_indices = {
            // Will not be adjacent to gate and needs two sections, so subtract 4 (3 to get
            // rid of gate and adjacent, 1 to account for taking 2 sections)
            let chosen = rng.gen_range(0..(outer_wall_corners.len() - 4));
            let index = if gate_index < 2 {
                chosen + gate_index + 2
            } else if chosen < (gate_index - 2) {
                chosen
            } else {
                chosen + 4
            };
            [index, (index + 1) % outer_wall_corners.len()]
        };

        // TODO: Figure out how to resolve the allow
        #[allow(clippy::needless_collect)]
        let outer_wall_segments = outer_wall_corners
            .iter()
            .enumerate()
            .filter_map(|(i, point)| {
                if i == gate_index {
                    None
                } else {
                    let next_point = if let Some(point) = outer_wall_corners.get(i + 1) {
                        *point
                    } else {
                        outer_wall_corners[0]
                    };
                    Some((*point, next_point))
                }
            })
            .collect::<Vec<_>>();

        // Structures will not spawn in wall corner triangles corresponding to these
        // indices
        let forbidden_indices = [gate_index, chieftain_indices[0], chieftain_indices[1]];
        // Structures will be weighted to spawn further from these indices when
        // selecting a point in the triangle
        let restricted_indices = [
            (chieftain_indices[0] + outer_wall_corners.len() - 1) % outer_wall_corners.len(),
            (chieftain_indices[1] + 1) % outer_wall_corners.len(),
        ];

        let desired_structures = wall_radius.pow(2) / 100;
        let mut structure_locations = Vec::<(GnarlingStructure, Vec3<i32>, Dir)>::new();
        for _ in 0..desired_structures {
            if let Some((hut_loc, kind)) = attempt(50, || {
                // Choose structure kind
                let structure_kind = match rng.gen_range(0..10) {
                    0 => GnarlingStructure::Totem,
                    1..=3 => GnarlingStructure::VeloriteHut,
                    4..=5 => GnarlingStructure::Banner,
                    _ => GnarlingStructure::Hut,
                };

                // Choose triangle
                let corner_1_index = rng.gen_range(0..outer_wall_corners.len());

                if forbidden_indices.contains(&corner_1_index) {
                    return None;
                }

                let center = Vec2::zero();
                let corner_1 = outer_wall_corners[corner_1_index];
                let (corner_2, corner_2_index) =
                    if let Some(corner) = outer_wall_corners.get(corner_1_index + 1) {
                        (*corner, corner_1_index + 1)
                    } else {
                        (outer_wall_corners[0], 0)
                    };

                let center_weight: f32 = rng.gen_range(0.15..0.7);

                // Forbidden and restricted indices are near walls, so don't spawn structures
                // too close to avoid overlap with wall
                let corner_1_weight_range = if restricted_indices.contains(&corner_1_index) {
                    let limit = 0.75;
                    if chieftain_indices.contains(&corner_2_index) {
                        ((1.0 - center_weight) * (1.0 - limit))..(1.0 - center_weight)
                    } else {
                        0.0..((1.0 - center_weight) * limit)
                    }
                } else {
                    0.0..(1.0 - center_weight)
                };

                let corner_1_weight = rng.gen_range(corner_1_weight_range);
                let corner_2_weight = 1.0 - center_weight - corner_1_weight;

                let structure_center: Vec2<i32> = (center * center_weight
                    + corner_1.as_() * corner_1_weight
                    + corner_2.as_() * corner_2_weight)
                    .as_();

                // Check that structure not in the water or too close to another structure
                if land
                    .get_chunk_wpos(structure_center + origin)
                    .map_or(false, |c| c.is_underwater())
                    || structure_locations.iter().any(|(kind, loc, _door_dir)| {
                        structure_center.distance_squared(loc.xy())
                            < structure_kind.required_separation(kind).pow(2)
                    })
                {
                    None
                } else {
                    Some((
                        structure_center.with_z(rpos_height(structure_center)),
                        structure_kind,
                    ))
                }
            }) {
                let dir_to_center = Dir::from_vec2(hut_loc.xy()).opposite();
                let door_rng: u32 = rng.gen_range(0..9);
                let door_dir = match door_rng {
                    0..=3 => dir_to_center,
                    4..=5 => dir_to_center.rotated_cw(),
                    6..=7 => dir_to_center.rotated_ccw(),
                    // Should only be 8
                    _ => dir_to_center.opposite(),
                };
                structure_locations.push((kind, hut_loc, door_dir));
            }
        }

        let wall_connections = [
            outer_wall_corners[chieftain_indices[0]],
            outer_wall_corners[(chieftain_indices[1] + 1) % outer_wall_corners.len()],
        ];
        let inner_tower_locs = wall_connections
            .iter()
            .map(|corner_pos| *corner_pos / 3)
            .collect::<Vec<_>>();

        let chieftain_hut_loc = ((inner_tower_locs[0] + inner_tower_locs[1])
            + 2 * outer_wall_corners[chieftain_indices[1]])
            / 4;
        let chieftain_hut_ori = Dir::from_vec2(chieftain_hut_loc).opposite();
        structure_locations.push((
            GnarlingStructure::ChieftainHut,
            chieftain_hut_loc.with_z(rpos_height(chieftain_hut_loc)),
            chieftain_hut_ori,
        ));

        let watchtower_locs = {
            let (corner_1, corner_2) = (
                outer_wall_corners[gate_index],
                outer_wall_corners[(gate_index + 1) % outer_wall_corners.len()],
            );
            [
                corner_1 / 5 + corner_2 * 4 / 5,
                corner_1 * 4 / 5 + corner_2 / 5,
            ]
        };
        watchtower_locs.iter().for_each(|loc| {
            structure_locations.push((
                GnarlingStructure::WatchTower,
                loc.with_z(rpos_height(*loc)),
                Dir::Y,
            ));
        });

        let wall_towers = outer_wall_corners
            .into_iter()
            .chain(inner_tower_locs.iter().copied())
            .map(|pos_2d| pos_2d.with_z(rpos_height(pos_2d)))
            .collect::<Vec<_>>();
        let wall_segments = outer_wall_segments
            .into_iter()
            .chain(wall_connections.iter().copied().zip(inner_tower_locs))
            .collect::<Vec<_>>();

        Self {
            name,
            seed,
            origin,
            radius,
            wall_radius,
            wall_towers,
            wall_segments,
            structure_locations,
            tunnels,
        }
    }

    pub fn name(&self) -> &str { &self.name }

    pub fn radius(&self) -> i32 { self.radius }

    pub fn spawn_rules(&self, wpos: Vec2<i32>) -> SpawnRules {
        SpawnRules {
            trees: wpos.distance_squared(self.origin) > self.wall_radius.pow(2),
            waypoints: false,
            ..SpawnRules::default()
        }
    }

    // TODO: Find a better way of spawning entities in site2
    pub fn apply_supplement(
        &self,
        // NOTE: Used only for dynamic elements like chests and entities!
        dynamic_rng: &mut impl Rng,
        wpos2d: Vec2<i32>,
        supplement: &mut ChunkSupplement,
    ) {
        let rpos = wpos2d - self.origin;
        let area = Aabr {
            min: rpos,
            max: rpos + TerrainChunkSize::RECT_SIZE.map(|e| e as i32),
        };

        for terminal in &self.tunnels.terminals {
            if area.contains_point(terminal.xy() - self.origin) {
                let chance = dynamic_rng.gen_range(0..10);
                let entities = match chance {
                    0..=4 => vec![mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng)],
                    5 => vec![
                        mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                        mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                    ],
                    6 => vec![deadwood(*terminal - 5 * Vec3::unit_z(), dynamic_rng)],
                    7 => vec![
                        mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                        deadwood(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                    ],
                    8 => vec![
                        mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                        mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                        mandragora(*terminal - 5 * Vec3::unit_z(), dynamic_rng),
                    ],
                    _ => Vec::new(),
                };
                for entity in entities {
                    supplement.add_entity(entity)
                }
            }
        }
        if area.contains_point(self.tunnels.end.xy() - self.origin) {
            let boss_room_offset = (self.tunnels.end.xy() - self.tunnels.start.xy())
                .map(|e| if e < 0 { -20 } else { 20 });
            supplement.add_entity(harvester_boss(
                self.tunnels.end + boss_room_offset - 2 * Vec3::unit_z(),
                dynamic_rng,
            ));
        }

        for (loc, pos, _ori) in &self.structure_locations {
            let wpos = *pos + self.origin;
            if area.contains_point(pos.xy()) {
                match loc {
                    GnarlingStructure::Hut => {
                        let num = dynamic_rng.gen_range(1..=3);
                        for _ in 0..num {
                            supplement.add_entity(random_gnarling(wpos, dynamic_rng));
                        }
                    },
                    GnarlingStructure::VeloriteHut => {
                        let num = dynamic_rng.gen_range(1..=4);
                        for _ in 0..num {
                            supplement.add_entity(random_gnarling(
                                wpos.xy().with_z(wpos.z + 12),
                                dynamic_rng,
                            ));
                        }
                    },
                    GnarlingStructure::Banner => {},
                    GnarlingStructure::ChieftainHut => {
                        let pos = wpos.xy().with_z(wpos.z + 8);
                        supplement.add_entity(gnarling_chieftain(pos, dynamic_rng));
                        for _ in 0..2 {
                            supplement.add_entity(wood_golem(pos, dynamic_rng));
                        }
                        for _ in 0..6 {
                            supplement.add_entity(random_gnarling(pos, dynamic_rng));
                        }
                    },
                    GnarlingStructure::WatchTower => {
                        supplement.add_entity(wood_golem(wpos, dynamic_rng));
                        let spawn_pos = wpos.xy().with_z(wpos.z + 27);
                        let num = dynamic_rng.gen_range(2..=4);
                        for _ in 0..num {
                            supplement.add_entity(gnarling_stalker(
                                spawn_pos + Vec2::broadcast(4),
                                dynamic_rng,
                            ));
                        }
                    },
                    GnarlingStructure::Totem => {},
                }
            }
        }

        for pos in &self.wall_towers {
            let wpos = *pos + self.origin;
            if area.contains_point(pos.xy()) {
                for _ in 0..4 {
                    supplement
                        .add_entity(gnarling_stalker(wpos.xy().with_z(wpos.z + 21), dynamic_rng))
                }
            }
        }
    }
}

impl Structure for GnarlingFortification {
    #[cfg(feature = "use-dyn-lib")]
    const UPDATE_FN: &'static [u8] = b"render_gnarlingfortification\0";

    #[cfg_attr(feature = "be-dyn-lib", export_name = "render_gnarlingfortification")]
    fn render_inner(&self, _site: &Site, land: &Land, painter: &Painter) {
        // Create outer wall
        for (point, next_point) in self.wall_segments.iter() {
            // This adds additional points for the wall on the line between two points,
            // allowing the wall to better handle slopes
            const SECTIONS_PER_WALL_SEGMENT: usize = 8;

            (0..(SECTIONS_PER_WALL_SEGMENT as i32))
                .map(move |a| {
                    let get_point =
                        |a| point + (next_point - point) * a / (SECTIONS_PER_WALL_SEGMENT as i32);
                    (get_point(a), get_point(a + 1))
                })
                .for_each(|(point, next_point)| {
                    // 2d world positions of each point in wall segment
                    let start_wpos = point + self.origin;
                    let end_wpos = next_point + self.origin;

                    let darkwood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 12);
                    let lightwood = Fill::Brick(BlockKind::Wood, Rgb::new(71, 33, 11), 12);
                    let moss = Fill::Brick(BlockKind::Wood, Rgb::new(22, 36, 20), 24);

                    let start = (start_wpos + 2)
                        .as_()
                        .with_z(land.get_alt_approx(start_wpos) + 0.0);
                    let end = (end_wpos + 2)
                        .as_()
                        .with_z(land.get_alt_approx(end_wpos) + 0.0);
                    let randstart = start % 10.0 - 5.;
                    let randend = end % 10.0 - 5.0;
                    let mid = (start + end) / 2.0;
                    let startshift = Vec3::new(
                        randstart.x * 5.0,
                        randstart.y * 5.0,
                        randstart.z * 1.0 + 5.0,
                    );
                    let endshift =
                        Vec3::new(randend.x * 5.0, randend.y * 5.0, randend.z * 1.0 + 5.0);

                    let mossroot =
                        painter.cubic_bezier(start, mid + startshift, mid + endshift, end, 3.0);

                    let start = start_wpos
                        .as_()
                        .with_z(land.get_alt_approx(start_wpos) - 2.0);
                    let end = end_wpos.as_().with_z(land.get_alt_approx(end_wpos) - 2.0);
                    let randstart = start % 10.0 - 5.;
                    let randend = end % 10.0 - 5.0;
                    let mid = (start + end) / 2.0;
                    let startshift =
                        Vec3::new(randstart.x * 2.0, randstart.y * 2.0, randstart.z * 0.5);
                    let endshift = Vec3::new(randend.x * 2.0, randend.y * 2.0, randend.z * 0.5);

                    let root1 =
                        painter.cubic_bezier(start, mid + startshift, mid + endshift, end, 5.0);

                    let mosstop1 = root1.translate(Vec3::new(0, 0, 1));

                    root1.fill(darkwood.clone());

                    let start = (start_wpos + 3)
                        .as_()
                        .with_z(land.get_alt_approx(start_wpos) + 0.0);
                    let end = (end_wpos + 3)
                        .as_()
                        .with_z(land.get_alt_approx(end_wpos) + 0.0);
                    let randstart = start % 10.0 - 5.;
                    let randend = end % 10.0 - 5.0;
                    let mid = (start + end) / 2.0;
                    let startshift = Vec3::new(
                        randstart.x * 3.0,
                        randstart.y * 3.0,
                        randstart.z * 2.0 + 5.0,
                    );
                    let endshift =
                        Vec3::new(randend.x * 3.0, randend.y * 3.0, randend.z * 2.0 + 5.0);
                    let root2 =
                        painter.cubic_bezier(start, mid + startshift, mid + endshift, end, 2.0);

                    let mosstop2 = root2.translate(Vec3::new(0, 0, 1));
                    let start = start_wpos.as_().with_z(land.get_alt_approx(start_wpos));
                    let end = end_wpos.as_().with_z(land.get_alt_approx(end_wpos));

                    let wall_base_height = 3.0;
                    let wall_mid_thickness = 1.0;
                    let wall_mid_height = 7.0 + wall_base_height;

                    painter
                        .segment_prism(start, end, wall_mid_thickness, wall_mid_height)
                        .fill(darkwood);

                    let start = start_wpos
                        .as_()
                        .with_z(land.get_alt_approx(start_wpos) + wall_mid_height);
                    let end = end_wpos
                        .as_()
                        .with_z(land.get_alt_approx(end_wpos) + wall_mid_height);

                    let wall_top_thickness = 2.0;
                    let wall_top_height = 1.0;

                    let topwall =
                        painter.segment_prism(start, end, wall_top_thickness, wall_top_height);
                    let mosstopwall = topwall.translate(Vec3::new(0, 0, 1));

                    topwall.fill(lightwood.clone());

                    root2.fill(lightwood);

                    mosstopwall
                        .intersect(mossroot.translate(Vec3::new(0, 0, 8)))
                        .fill(moss.clone());

                    mosstop1.intersect(mossroot).fill(moss.clone());
                    mosstop2.intersect(mossroot).fill(moss);
                })
        }

        // Create towers
        self.wall_towers.iter().for_each(|point| {
            let wpos = point.xy() + self.origin;

            // Tower base
            let tower_depth = 3;
            let tower_base_pos = wpos.with_z(land.get_alt_approx(wpos) as i32 - tower_depth);
            let tower_radius = 5.0;
            let tower_height = 30.0;

            let randx = wpos.x.abs() % 10;
            let randy = wpos.y.abs() % 10;
            let randz = (land.get_alt_approx(wpos) as i32).abs() % 10;
            //layers of rings, starting at exterior
            let darkwood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 12);
            let lightwood = Fill::Brick(BlockKind::Wood, Rgb::new(71, 33, 11), 12);
            let moss = Fill::Brick(BlockKind::Wood, Rgb::new(22, 36, 20), 24);
            let red = Fill::Brick(BlockKind::Wood, Rgb::new(102, 31, 2), 12);

            let twist = painter.cubic_bezier(
                tower_base_pos + Vec3::new(4, 4, 8),
                tower_base_pos + Vec3::new(-9, 9, 14),
                tower_base_pos + Vec3::new(-11, -11, 16),
                tower_base_pos + Vec3::new(4, -4, 21),
                1.5,
            );
            let mosstwist = twist.translate(Vec3::new(0, 0, 1));
            let mossarea = twist.translate(Vec3::new(1, 0, 1));

            mossarea
                .intersect(mosstwist)
                .without(twist)
                .fill(moss.clone());
            twist.fill(darkwood.clone());

            let outside = painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32),
                    tower_radius,
                    tower_height,
                )
                .without(painter.cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32),
                    tower_radius - 1.0,
                    tower_height,
                ));
            outside.fill(lightwood.clone());
            painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32),
                    tower_radius - 1.0,
                    tower_height,
                )
                .fill(darkwood.clone());
            painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32),
                    tower_radius - 2.0,
                    tower_height,
                )
                .fill(lightwood.clone());
            painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32),
                    tower_radius - 3.0,
                    tower_height,
                )
                .fill(darkwood);
            painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32),
                    tower_radius - 4.0,
                    tower_height,
                )
                .fill(lightwood);
            //top layer, green above the tower
            painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32),
                    tower_radius,
                    2.0,
                )
                .fill(moss);
            //standing area one block deeper
            painter
                .cylinder_with_radius(
                    wpos.with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 9),
                    tower_radius - 1.0,
                    1.0,
                )
                .clear();
            //remove top sphere from trunk
            painter
                .sphere_with_radius(
                    Vec2::new(wpos.x - (randy - 5) / 2, wpos.y - (randz - 5) / 2).with_z(
                        land.get_alt_approx(wpos) as i32 + tower_height as i32 + 6 - randx / 3,
                    ),
                    5.5,
                )
                .clear();
            //remove bark from exterior layer
            painter
                .sphere_with_radius(
                    Vec2::new(wpos.x - (randy - 5) * 2, wpos.y - (randz - 5) * 2)
                        .with_z(land.get_alt_approx(wpos) as i32 + randx * 2),
                    7.5,
                )
                .intersect(outside)
                .clear();

            painter
                .sphere_with_radius(
                    Vec2::new(wpos.x - (randx - 5) * 2, wpos.y - (randy - 5) * 2)
                        .with_z(land.get_alt_approx(wpos) as i32 + randz * 2),
                    5.5,
                )
                .intersect(outside)
                .clear();

            //cut out standing room
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - 3, wpos.y - 10)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 8),
                    max: Vec2::new(wpos.x + 3, wpos.y + 10)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 3),
                })
                .clear();
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - 10, wpos.y - 3)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 8),
                    max: Vec2::new(wpos.x + 10, wpos.y + 3)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 3),
                })
                .clear();
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - 2, wpos.y - 10)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 8),
                    max: Vec2::new(wpos.x + 2, wpos.y + 10)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 2),
                })
                .clear();
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - 10, wpos.y - 2)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 8),
                    max: Vec2::new(wpos.x + 10, wpos.y + 2)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 2),
                })
                .clear();
            //flags
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - 2, wpos.y - tower_radius as i32 - 1)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 16),
                    max: Vec2::new(wpos.x + 2, wpos.y + tower_radius as i32 + 1)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 10),
                })
                .intersect(outside)
                .fill(red.clone());
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - tower_radius as i32 - 1, wpos.y - 2)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 16),
                    max: Vec2::new(wpos.x + tower_radius as i32 + 1, wpos.y + 2)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 10),
                })
                .intersect(outside)
                .fill(red.clone());
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - 1, wpos.y - tower_radius as i32 - 1)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 17),
                    max: Vec2::new(wpos.x + 1, wpos.y + tower_radius as i32 + 1)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 16),
                })
                .intersect(outside)
                .fill(red.clone());
            painter
                .aabb(Aabb {
                    min: Vec2::new(wpos.x - tower_radius as i32 - 1, wpos.y - 1)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 17),
                    max: Vec2::new(wpos.x + tower_radius as i32 + 1, wpos.y + 1)
                        .with_z(land.get_alt_approx(wpos) as i32 + tower_height as i32 - 16),
                })
                .intersect(outside)
                .fill(red);
        });

        self.structure_locations
            .iter()
            .for_each(|(kind, loc, door_dir)| {
                let wpos = self.origin + loc.xy();
                let alt = land.get_alt_approx(wpos) as i32;

                fn generate_hut(
                    painter: &Painter,
                    wpos: Vec2<i32>,
                    alt: i32,
                    door_dir: Dir,
                    hut_radius: f32,
                    hut_wall_height: f32,
                    door_height: i32,
                    roof_height: f32,
                    roof_overhang: f32,
                ) {
                    let randx = wpos.x.abs() % 10;
                    let randy = wpos.y.abs() % 10;
                    let randz = alt.abs() % 10;
                    let hut_wall_height = hut_wall_height + randy as f32 * 1.5;

                    let darkwood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 12);
                    let lightwood = Fill::Brick(BlockKind::Wood, Rgb::new(71, 33, 11), 12);
                    let moss = Fill::Brick(BlockKind::Leaves, Rgb::new(22, 36, 20), 24);

                    // Floor
                    let base = wpos.with_z(alt - 4);
                    painter
                        .cylinder_with_radius(base, hut_radius + 1.0, 6.0)
                        .fill(darkwood.clone());

                    // Wall
                    let floor_pos = wpos.with_z(alt + 1);
                    //alternating colors for ring pattern on ceiling
                    painter
                        .cylinder_with_radius(floor_pos, hut_radius, hut_wall_height + 3.0)
                        .fill(lightwood.clone());
                    painter
                        .cylinder_with_radius(floor_pos, hut_radius - 1.0, hut_wall_height + 3.0)
                        .fill(darkwood.clone());
                    painter
                        .cylinder_with_radius(floor_pos, hut_radius - 2.0, hut_wall_height + 3.0)
                        .fill(lightwood);
                    painter
                        .cylinder_with_radius(floor_pos, hut_radius - 3.0, hut_wall_height + 3.0)
                        .fill(darkwood);
                    painter
                        .cylinder_with_radius(floor_pos, hut_radius - 1.0, hut_wall_height)
                        .clear();

                    // Door
                    let aabb_min = |dir| {
                        match dir {
                            Dir::X | Dir::Y => wpos - Vec2::one(),
                            Dir::NegX | Dir::NegY => wpos + randx / 5 + 1,
                        }
                        .with_z(alt + 1)
                    };
                    let aabb_max = |dir| {
                        (match dir {
                            Dir::X | Dir::Y => wpos + randx / 5 + 1,
                            Dir::NegX | Dir::NegY => wpos - Vec2::one(),
                        } + dir.to_vec2() * hut_radius as i32)
                            .with_z(alt + 1 + door_height)
                    };

                    painter
                        .aabb(Aabb {
                            min: aabb_min(door_dir),
                            max: aabb_max(door_dir),
                        })
                        .clear();

                    // Roof
                    let roof_radius = hut_radius + roof_overhang;
                    painter
                        .cone_with_radius(
                            wpos.with_z(alt + 3 + hut_wall_height as i32),
                            roof_radius - 1.0,
                            roof_height - 1.0,
                        )
                        .fill(moss.clone());

                    //small bits hanging from huts
                    let tendril1 = painter.line(
                        Vec2::new(wpos.x - 3, wpos.y - 5)
                            .with_z(alt + (hut_wall_height * 0.75) as i32),
                        Vec2::new(wpos.x - 3, wpos.y - 5).with_z(alt + 3 + hut_wall_height as i32),
                        1.0,
                    );

                    let tendril2 = painter.line(
                        Vec2::new(wpos.x + 4, wpos.y + 2)
                            .with_z(alt + 1 + (hut_wall_height * 0.75) as i32),
                        Vec2::new(wpos.x + 4, wpos.y + 2).with_z(alt + 3 + hut_wall_height as i32),
                        1.0,
                    );

                    let tendril3 = tendril2.translate(Vec3::new(-7, 2, 0));
                    let tendril4 = tendril1.translate(Vec3::new(7, 4, 0));
                    let tendrils = tendril1.union(tendril2).union(tendril3).union(tendril4);

                    tendrils.fill(moss);

                    //sphere to delete some hut
                    painter
                        .sphere_with_radius(
                            Vec2::new(wpos.x - (randy - 5) / 2, wpos.y - (randz - 5) / 2)
                                .with_z(alt + 12 + hut_wall_height as i32 - randx / 3),
                            8.5,
                        )
                        .clear();
                }

                fn generate_chieftainhut(
                    painter: &Painter,
                    wpos: Vec2<i32>,
                    alt: i32,
                    roof_height: f32,
                ) {
                    let darkwood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 12);
                    let lightwood = Fill::Brick(BlockKind::Wood, Rgb::new(71, 33, 11), 12);
                    let moss = Fill::Brick(BlockKind::Wood, Rgb::new(22, 36, 20), 24);
                    let red = Fill::Brick(BlockKind::Wood, Rgb::new(102, 31, 2), 12);

                    // Floor
                    let raise = 5;

                    let platform = painter.aabb(Aabb {
                        min: (wpos - 20).with_z(alt + raise),
                        max: (wpos + 20).with_z(alt + raise + 1),
                    });

                    painter.fill(platform, darkwood.clone());

                    let supports = painter
                        .line(
                            (wpos - 19).with_z(alt - 3),
                            (wpos - 19).with_z(alt + raise),
                            2.0,
                        )
                        .repeat(Vec3::new(37, 0, 0), 2)
                        .repeat(Vec3::new(0, 37, 0), 2);

                    let supports_inner = painter
                        .aabb(Aabb {
                            min: (wpos - 19).with_z(alt - 10) + Vec3::unit_y() * 17,
                            max: (wpos - 15).with_z(alt + raise) + Vec3::unit_y() * 17,
                        })
                        .repeat(Vec3::new(17, 17, 0), 2)
                        .repeat(Vec3::new(17, -17, 0), 2);
                    // let support_inner_2 = support_inner_1.translate(Vec3::new(34, 0, 0));
                    // let support_inner_3 = support_inner_1.translate(Vec3::new(17, 17, 0));
                    // let support_inner_4 = support_inner_1.translate(Vec3::new(17, -17, 0));
                    let supports = supports.union(supports_inner);

                    painter.fill(supports, darkwood.clone());
                    let height_1 = 10.0;
                    let height_2 = 12.0;
                    let rad_1 = 18.0;
                    let rad_2 = 15.0;

                    let floor_pos = wpos.with_z(alt + 1 + raise);
                    painter
                        .cylinder_with_radius(floor_pos, rad_1, height_1)
                        .fill(lightwood.clone());
                    painter
                        .cylinder_with_radius(floor_pos, rad_1 - 1.0, height_1)
                        .clear();

                    let floor2_pos = wpos.with_z(alt + 1 + raise + height_1 as i32);
                    painter
                        .cylinder_with_radius(floor2_pos, rad_2, height_2)
                        .fill(lightwood);

                    // Roof
                    let roof_radius = rad_1 + 5.0;
                    let roof1 = painter.cone_with_radius(
                        wpos.with_z(alt + 1 + height_1 as i32 + raise),
                        roof_radius,
                        roof_height,
                    );
                    roof1.fill(moss.clone());
                    let roof_radius = rad_2 + 1.0;
                    painter
                        .cone_with_radius(
                            wpos.with_z(alt + 1 + height_1 as i32 + height_2 as i32 + raise),
                            roof_radius,
                            roof_height,
                        )
                        .fill(moss);
                    let centerspot = painter.line(
                        (wpos + 1).with_z(alt + raise + height_1 as i32 + 2),
                        (wpos + 1).with_z(alt + raise + height_1 as i32 + 2),
                        1.0,
                    );
                    let roof_support_1 = painter.line(
                        (wpos + rad_1 as i32 - 7).with_z(alt + raise + height_1 as i32 - 2),
                        (wpos + rad_1 as i32 - 2).with_z(alt + raise + height_1 as i32),
                        1.5,
                    );
                    let roof_strut = painter.line(
                        (wpos + rad_1 as i32 - 7).with_z(alt + raise + height_1 as i32 + 2),
                        (wpos + rad_1 as i32 - 2).with_z(alt + raise + height_1 as i32 + 2),
                        1.0,
                    );
                    let wall2support = painter.line(
                        (wpos + rad_2 as i32 - 5).with_z(alt + raise + height_1 as i32 + 7),
                        (wpos + rad_2 as i32 - 5).with_z(alt + raise + height_1 as i32 + 12),
                        1.5,
                    );
                    let wall2roof = painter.line(
                        (wpos + rad_2 as i32 - 7).with_z(alt + raise + height_2 as i32 + 12),
                        (wpos + rad_2 as i32 - 4).with_z(alt + raise + height_2 as i32 + 10),
                        2.0,
                    );

                    let roof_support_1 = centerspot
                        .union(roof_support_1)
                        .union(roof_strut)
                        .union(wall2support)
                        .union(wall2roof);

                    let roof_support_2 =
                        roof_support_1.rotate_about_min(Mat3::new(1, 0, 0, 0, -1, 0, 0, 0, 1));
                    let roof_support_3 =
                        roof_support_1.rotate_about_min(Mat3::new(-1, 0, 0, 0, 1, 0, 0, 0, 1));
                    let roof_support_4 =
                        roof_support_1.rotate_about_min(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1));
                    let roof_support = roof_support_1
                        .union(roof_support_2)
                        .union(roof_support_3)
                        .union(roof_support_4);

                    painter.fill(roof_support, red.clone());

                    let spike_high = painter.cubic_bezier(
                        (wpos + rad_2 as i32 - 5).with_z(alt + raise + height_1 as i32 + 2),
                        (wpos + rad_2 as i32 - 2).with_z(alt + raise + height_1 as i32 + 2),
                        (wpos + rad_2 as i32 - 1).with_z(alt + raise + height_1 as i32 + 5),
                        (wpos + rad_2 as i32).with_z(alt + raise + height_1 as i32 + 8),
                        1.3,
                    );
                    let spike_low =
                        spike_high.rotate_about_min(Mat3::new(1, 0, 0, 0, 1, 0, 0, 0, -1));
                    let spike_1 = centerspot.union(spike_low).union(spike_high);

                    let spike_2 = spike_1.rotate_about_min(Mat3::new(1, 0, 0, 0, -1, 0, 0, 0, 1));
                    let spike_3 = spike_1.rotate_about_min(Mat3::new(-1, 0, 0, 0, 1, 0, 0, 0, 1));
                    let spike_4 = spike_1.rotate_about_min(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1));
                    let spikes = spike_1.union(spike_2).union(spike_3).union(spike_4);

                    painter.fill(
                        spikes,
                        Fill::Brick(BlockKind::Wood, Rgb::new(112, 110, 99), 24),
                    );
                    //Open doorways (top floor)
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 2, wpos.y - 15)
                                .with_z(alt + raise + height_1 as i32 + 3),
                            max: Vec2::new(wpos.x + 2, wpos.y + 15)
                                .with_z(alt + raise + height_1 as i32 + height_2 as i32),
                        })
                        .clear();
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 3, wpos.y - 15)
                                .with_z(alt + raise + height_1 as i32 + 4),
                            max: Vec2::new(wpos.x + 3, wpos.y + 15)
                                .with_z(alt + raise + height_1 as i32 + 10),
                        })
                        .clear();
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 15, wpos.y - 2)
                                .with_z(alt + raise + height_1 as i32 + 3),
                            max: Vec2::new(wpos.x + 15, wpos.y + 2)
                                .with_z(alt + raise + height_1 as i32 + height_2 as i32),
                        })
                        .clear();
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 15, wpos.y - 3)
                                .with_z(alt + raise + height_1 as i32 + 4),
                            max: Vec2::new(wpos.x + 15, wpos.y + 3)
                                .with_z(alt + raise + height_1 as i32 + 10),
                        })
                        .clear();

                    //
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 18, wpos.y - 2)
                                .with_z(alt + raise + height_1 as i32 - 9),
                            max: Vec2::new(wpos.x + 18, wpos.y + 2)
                                .with_z(alt + raise + height_1 as i32 - 1),
                        })
                        .clear();
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 2, wpos.y - 18)
                                .with_z(alt + raise + height_1 as i32 - 9),
                            max: Vec2::new(wpos.x + 2, wpos.y + 18)
                                .with_z(alt + raise + height_1 as i32 - 1),
                        })
                        .clear();
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 18, wpos.y - 3)
                                .with_z(alt + raise + height_1 as i32 - 9),
                            max: Vec2::new(wpos.x + 18, wpos.y + 3)
                                .with_z(alt + raise + height_1 as i32 - 3),
                        })
                        .clear();
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 3, wpos.y - 18)
                                .with_z(alt + raise + height_1 as i32 - 9),
                            max: Vec2::new(wpos.x + 3, wpos.y + 18)
                                .with_z(alt + raise + height_1 as i32 - 3),
                        })
                        .clear();
                    //Roofing details

                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 23, wpos.y - 2)
                                .with_z(alt + raise + height_1 as i32 - 3),
                            max: Vec2::new(wpos.x + 23, wpos.y + 2)
                                .with_z(alt + raise + height_1 as i32 + 7),
                        })
                        .intersect(roof1)
                        .translate(Vec3::new(0, 0, -1))
                        .fill(darkwood.clone());
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 23, wpos.y - 2)
                                .with_z(alt + raise + height_1 as i32 - 3),
                            max: Vec2::new(wpos.x + 23, wpos.y + 2)
                                .with_z(alt + raise + height_1 as i32 + 7),
                        })
                        .intersect(roof1)
                        .fill(red.clone());
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 2, wpos.y - 23)
                                .with_z(alt + raise + height_1 as i32 - 3),
                            max: Vec2::new(wpos.x + 2, wpos.y + 23)
                                .with_z(alt + raise + height_1 as i32 + 7),
                        })
                        .intersect(roof1)
                        .translate(Vec3::new(0, 0, -1))
                        .fill(darkwood);
                    painter
                        .aabb(Aabb {
                            min: Vec2::new(wpos.x - 2, wpos.y - 23)
                                .with_z(alt + raise + height_1 as i32 - 3),
                            max: Vec2::new(wpos.x + 2, wpos.y + 23)
                                .with_z(alt + raise + height_1 as i32 + 7),
                        })
                        .intersect(roof1)
                        .fill(red);
                    painter
                        .cylinder_with_radius(floor2_pos, rad_2 - 1.0, height_2)
                        .clear();
                }

                match kind {
                    GnarlingStructure::Hut => {
                        let hut_radius = 5.0;
                        let hut_wall_height = 4.0;
                        let door_height = 4;
                        let roof_height = 3.0;
                        let roof_overhang = 1.0;

                        generate_hut(
                            painter,
                            wpos,
                            alt,
                            *door_dir,
                            hut_radius,
                            hut_wall_height,
                            door_height,
                            roof_height,
                            roof_overhang,
                        );
                    },
                    GnarlingStructure::VeloriteHut => {
                        let rand = Vec3::new(
                            wpos.x.abs() % 10,
                            wpos.y.abs() % 10,
                            (land.get_alt_approx(wpos) as i32).abs() % 10,
                        );

                        let length = 14;
                        let width = 6;
                        let height = alt + 12;
                        let darkwood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 12);
                        let lightwood = Fill::Brick(BlockKind::Wood, Rgb::new(71, 33, 11), 12);
                        let moss = Fill::Brick(BlockKind::Wood, Rgb::new(22, 36, 20), 24);
                        let red = Fill::Brick(BlockKind::Wood, Rgb::new(102, 31, 2), 12);

                        let low1 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - width, wpos.y - length).with_z(height + 1),
                            max: Vec2::new(wpos.x + width, wpos.y + length).with_z(height + 2),
                        });

                        let low2 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - length, wpos.y - width).with_z(height + 1),
                            max: Vec2::new(wpos.x + length, wpos.y + width).with_z(height + 2),
                        });
                        let top1 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - width + 1, wpos.y - length + 1)
                                .with_z(height + 2),
                            max: Vec2::new(wpos.x + width - 1, wpos.y + length - 1)
                                .with_z(height + 2 + 1),
                        });

                        let top2 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - length + 1, wpos.y - width + 1)
                                .with_z(height + 2),
                            max: Vec2::new(wpos.x + length - 1, wpos.y + width - 1)
                                .with_z(height + 2 + 1),
                        });
                        let low = low1.union(low2);
                        let top = top1.union(top2);

                        let roof = low1.union(low2).union(top1).union(top2);
                        let roofmoss = roof.translate(Vec3::new(0, 0, 1)).without(top).without(low);
                        top.fill(darkwood.clone());
                        low.fill(lightwood);
                        roofmoss.fill(moss);
                        painter
                            .sphere_with_radius(
                                Vec2::new(wpos.x + rand.y - 5, wpos.y + rand.z - 5)
                                    .with_z(height + 4),
                                7.0,
                            )
                            .intersect(roofmoss)
                            .clear();
                        painter
                            .sphere_with_radius(
                                Vec2::new(wpos.x + rand.x - 5, wpos.y + rand.y - 5)
                                    .with_z(height + 4),
                                4.0,
                            )
                            .intersect(roofmoss)
                            .clear();
                        painter
                            .sphere_with_radius(
                                Vec2::new(wpos.x + 2 * (rand.z - 5), wpos.y + 2 * (rand.x - 5))
                                    .with_z(height + 4),
                                4.0,
                            )
                            .intersect(roofmoss)
                            .clear();

                        //inside leg
                        let leg1 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - width, wpos.y - width).with_z(height - 12),
                            max: Vec2::new(wpos.x - width + 2, wpos.y - width + 2)
                                .with_z(height + 2),
                        });
                        let legsupport1 = painter.line(
                            Vec2::new(wpos.x - width, wpos.y - width).with_z(height - 6),
                            Vec2::new(wpos.x - width + 3, wpos.y - width + 3).with_z(height),
                            1.0,
                        );

                        let leg2 = leg1.translate(Vec3::new(0, width * 2 - 2, 0));
                        let leg3 = leg1.translate(Vec3::new(width * 2 - 2, 0, 0));
                        let leg4 = leg1.translate(Vec3::new(width * 2 - 2, width * 2 - 2, 0));
                        let legsupport2 = legsupport1
                            .rotate_about_min(Mat3::new(0, 1, 0, -1, 0, 0, 0, 0, 1))
                            .translate(Vec3::new(1, width * 2 + 1, 0));
                        let legsupport3 = legsupport1
                            .rotate_about_min(Mat3::new(0, -1, 0, 1, 0, 0, 0, 0, 1))
                            .translate(Vec3::new(width * 2 + 1, 1, 0));
                        let legsupport4 = legsupport1
                            .rotate_about_min(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1))
                            .translate(Vec3::new(width * 2 + 2, width * 2 + 2, 0));

                        let legsupports = legsupport1
                            .union(legsupport2)
                            .union(legsupport3)
                            .union(legsupport4);

                        let legs = leg1.union(leg2).union(leg3).union(leg4);
                        legs.fill(darkwood);
                        legsupports.without(legs).fill(red);

                        let spike1 = painter.line(
                            Vec2::new(wpos.x - 12, wpos.y + 2).with_z(height + 3),
                            Vec2::new(wpos.x - 7, wpos.y + 2).with_z(height + 5),
                            1.0,
                        );
                        let spike2 = painter.line(
                            Vec2::new(wpos.x - 12, wpos.y - 3).with_z(height + 3),
                            Vec2::new(wpos.x - 7, wpos.y - 3).with_z(height + 5),
                            1.0,
                        );
                        let spikes = spike1.union(spike2);
                        let spikesalt = spikes
                            .rotate_about_min(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1))
                            .translate(Vec3::new(26, 8, 0));
                        let spikeshalf = spikes.union(spikesalt);
                        let spikesotherhalf = spikeshalf
                            .rotate_about_min(Mat3::new(0, -1, 0, 1, 0, 0, 0, 0, 1))
                            .translate(Vec3::new(16, -9, 0));
                        let spikesall = spikeshalf.union(spikesotherhalf);

                        spikesall.fill(Fill::Brick(BlockKind::Wood, Rgb::new(112, 110, 99), 24));
                        let crystal1 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - 2, wpos.y - 3).with_z(alt),
                            max: Vec2::new(wpos.x + 2, wpos.y + 1).with_z(alt + 7),
                        });
                        let crystal2 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - 3, wpos.y - 3).with_z(alt),
                            max: Vec2::new(wpos.x + 3, wpos.y + 1).with_z(alt + 6),
                        });
                        let crystal3 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - 2, wpos.y - 2).with_z(alt),
                            max: Vec2::new(wpos.x + 4, wpos.y + 3).with_z(alt + 4),
                        });
                        let crystal4 = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x - 1, wpos.y - 4).with_z(alt),
                            max: Vec2::new(wpos.x + 2, wpos.y + 2).with_z(alt + 8),
                        });
                        let crystalp1 = crystal1.union(crystal3);
                        let crystalp2 = crystal2.union(crystal4);

                        crystalp1.fill(Fill::Brick(
                            BlockKind::GlowingRock,
                            Rgb::new(50, 225, 210),
                            24,
                        ));
                        crystalp2.fill(Fill::Brick(
                            BlockKind::GlowingRock,
                            Rgb::new(36, 187, 151),
                            24,
                        ));
                    },
                    GnarlingStructure::Totem => {
                        let totem_pos = wpos.with_z(alt);

                        lazy_static! {
                            pub static ref TOTEM: AssetHandle<StructuresGroup> =
                                PrefabStructure::load_group("site_structures.gnarling.totem");
                        }

                        let totem = TOTEM.read();
                        let totem = totem[self.seed as usize % totem.len()].clone();

                        painter
                            .prim(Primitive::Prefab(Box::new(totem.clone())))
                            .translate(totem_pos)
                            .fill(Fill::Prefab(Box::new(totem), totem_pos, self.seed));
                    },
                    GnarlingStructure::ChieftainHut => {
                        let roof_height = 3.0;

                        generate_chieftainhut(painter, wpos, alt, roof_height);
                    },

                    GnarlingStructure::Banner => {
                        let rand = Vec3::new(
                            wpos.x.abs() % 10,
                            wpos.y.abs() % 10,
                            (land.get_alt_approx(wpos) as i32).abs() % 10,
                        );

                        let darkwood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 12);
                        let moss = Fill::Brick(BlockKind::Leaves, Rgb::new(22, 36, 20), 24);
                        let red = Fill::Brick(BlockKind::Wood, Rgb::new(102, 31, 2), 12);
                        let flag = painter.aabb(Aabb {
                            min: Vec2::new(wpos.x + 1, wpos.y - 1).with_z(alt + 8),
                            max: Vec2::new(wpos.x + 8, wpos.y).with_z(alt + 38),
                        });
                        flag.fill(red);
                        //add green streaks
                        let streak1 = painter
                            .line(
                                Vec2::new(wpos.x - 5, wpos.y - 1).with_z(alt + 20),
                                Vec2::new(wpos.x + 8, wpos.y - 1).with_z(alt + 33),
                                4.0,
                            )
                            .intersect(flag);

                        let streak2 = painter
                            .line(
                                Vec2::new(wpos.x - 5, wpos.y - 1).with_z(alt + 12),
                                Vec2::new(wpos.x + 8, wpos.y - 1).with_z(alt + 25),
                                1.5,
                            )
                            .intersect(flag);
                        let streak3 = painter
                            .line(
                                Vec2::new(wpos.x - 5, wpos.y - 1).with_z(alt + 8),
                                Vec2::new(wpos.x + 8, wpos.y - 1).with_z(alt + 21),
                                1.0,
                            )
                            .intersect(flag);
                        let streaks = streak1.union(streak2).union(streak3);
                        streaks.fill(moss);
                        //erase from top and bottom of rectangle flag for shape
                        painter
                            .line(
                                Vec2::new(wpos.x - 5, wpos.y - 1).with_z(alt + 31),
                                Vec2::new(wpos.x + 8, wpos.y - 1).with_z(alt + 44),
                                5.0,
                            )
                            .intersect(flag)
                            .clear();
                        painter
                            .sphere_with_radius(Vec2::new(wpos.x + 8, wpos.y).with_z(alt + 8), 6.0)
                            .intersect(flag)
                            .clear();
                        //tatters
                        painter
                            .line(
                                Vec2::new(wpos.x + 3 + rand.x / 5, wpos.y - 1)
                                    .with_z(alt + 15 + rand.y),
                                Vec2::new(wpos.x + 3 + rand.y / 5, wpos.y - 1).with_z(alt + 5),
                                0.9 * rand.z as f32 / 4.0,
                            )
                            .clear();
                        painter
                            .line(
                                Vec2::new(wpos.x + 4 + rand.z / 2, wpos.y - 1)
                                    .with_z(alt + 20 + rand.x),
                                Vec2::new(wpos.x + 4 + rand.z / 2, wpos.y - 1)
                                    .with_z(alt + 17 + rand.y),
                                0.9 * rand.z as f32 / 6.0,
                            )
                            .clear();

                        //flagpole
                        let column = painter.aabb(Aabb {
                            min: (wpos - 1).with_z(alt - 3),
                            max: (wpos).with_z(alt + 30),
                        });

                        let arm = painter.line(
                            Vec2::new(wpos.x - 5, wpos.y - 1).with_z(alt + 26),
                            Vec2::new(wpos.x + 8, wpos.y - 1).with_z(alt + 39),
                            0.9,
                        );
                        let flagpole = column.union(arm);
                        flagpole.fill(darkwood);
                    },
                    GnarlingStructure::WatchTower => {
                        let platform_1_height = 14;
                        let platform_2_height = 20;
                        let platform_3_height = 26;
                        let roof_height = 30;

                        let platform_1 = painter.aabb(Aabb {
                            min: (wpos + 1).with_z(alt + platform_1_height),
                            max: (wpos + 10).with_z(alt + platform_1_height + 1),
                        });

                        let platform_2 = painter.aabb(Aabb {
                            min: (wpos + 1).with_z(alt + platform_2_height),
                            max: (wpos + 10).with_z(alt + platform_2_height + 1),
                        });

                        let platform_3 = painter.aabb(Aabb {
                            min: (wpos + 2).with_z(alt + platform_3_height),
                            max: (wpos + 9).with_z(alt + platform_3_height + 1),
                        });

                        let support_1 = painter.line(
                            wpos.with_z(alt),
                            (wpos + 2).with_z(alt + platform_1_height),
                            1.0,
                        );
                        let support_2 = support_1
                            .rotate_about_min(Mat3::new(1, 0, 0, 0, -1, 0, 0, 0, 1))
                            .translate(Vec3::new(0, 13, 0));
                        let support_3 = support_1
                            .rotate_about_min(Mat3::new(-1, 0, 0, 0, 1, 0, 0, 0, 1))
                            .translate(Vec3::new(13, 0, 0));
                        let support_4 = support_1
                            .rotate_about_min(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1))
                            .translate(Vec3::new(13, 13, 0));

                        let supports = support_1.union(support_2).union(support_3).union(support_4);

                        let platform_1_supports = painter
                            .plane(
                                Aabr {
                                    min: (wpos + 2),
                                    max: (wpos + 9),
                                },
                                (wpos + 3).with_z(alt + platform_1_height + 1),
                                Vec2::new(0.0, 1.0),
                            )
                            .union(painter.plane(
                                Aabr {
                                    min: (wpos + 2),
                                    max: (wpos + 9),
                                },
                                (wpos + 3).with_z(alt + platform_2_height),
                                Vec2::new(0.0, -1.0),
                            ))
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 3, wpos.y + 2)
                                        .with_z(alt + platform_1_height),
                                    max: Vec2::new(wpos.x + 8, wpos.y + 9)
                                        .with_z(alt + platform_2_height),
                                }),
                            )
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 2, wpos.y + 2)
                                        .with_z(alt + platform_2_height),
                                    max: Vec2::new(wpos.x + 9, wpos.y + 9)
                                        .with_z(alt + platform_2_height + 2),
                                }),
                            )
                            .union(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 2, wpos.y + 2)
                                        .with_z(alt + platform_1_height),
                                    max: Vec2::new(wpos.x + 9, wpos.y + 9)
                                        .with_z(alt + platform_2_height),
                                }),
                            )
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 3, wpos.y + 2)
                                        .with_z(alt + platform_1_height),
                                    max: Vec2::new(wpos.x + 8, wpos.y + 9)
                                        .with_z(alt + platform_2_height),
                                }),
                            )
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 2, wpos.y + 3)
                                        .with_z(alt + platform_1_height),
                                    max: Vec2::new(wpos.x + 9, wpos.y + 8)
                                        .with_z(alt + platform_2_height),
                                }),
                            );

                        let platform_2_supports = painter
                            .plane(
                                Aabr {
                                    min: (wpos + 3),
                                    max: (wpos + 8),
                                },
                                (wpos + 3).with_z(alt + platform_2_height + 1),
                                Vec2::new(1.0, 0.0),
                            )
                            .union(painter.plane(
                                Aabr {
                                    min: (wpos + 3),
                                    max: (wpos + 8),
                                },
                                (wpos + 3).with_z(alt + platform_3_height),
                                Vec2::new(-1.0, 0.0),
                            ))
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 3, wpos.y + 4)
                                        .with_z(alt + platform_2_height),
                                    max: Vec2::new(wpos.x + 8, wpos.y + 7)
                                        .with_z(alt + platform_3_height),
                                }),
                            )
                            .union(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 3, wpos.y + 3)
                                        .with_z(alt + platform_2_height),
                                    max: Vec2::new(wpos.x + 8, wpos.y + 8)
                                        .with_z(alt + platform_3_height),
                                }),
                            )
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 4, wpos.y + 3)
                                        .with_z(alt + platform_2_height),
                                    max: Vec2::new(wpos.x + 7, wpos.y + 8)
                                        .with_z(alt + platform_3_height),
                                }),
                            )
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 3, wpos.y + 4)
                                        .with_z(alt + platform_2_height),
                                    max: Vec2::new(wpos.x + 8, wpos.y + 7)
                                        .with_z(alt + platform_3_height),
                                }),
                            );

                        let roof = painter
                            .gable(
                                Aabb {
                                    min: (wpos + 2).with_z(alt + roof_height),
                                    max: (wpos + 9).with_z(alt + roof_height + 4),
                                },
                                0,
                                Dir::Y,
                            )
                            .without(
                                painter.gable(
                                    Aabb {
                                        min: Vec2::new(wpos.x + 3, wpos.y + 2)
                                            .with_z(alt + roof_height),
                                        max: Vec2::new(wpos.x + 8, wpos.y + 9)
                                            .with_z(alt + roof_height + 3),
                                    },
                                    0,
                                    Dir::Y,
                                ),
                            );

                        let roof_pillars = painter
                            .aabb(Aabb {
                                min: Vec2::new(wpos.x + 3, wpos.y + 3)
                                    .with_z(alt + platform_3_height),
                                max: Vec2::new(wpos.x + 8, wpos.y + 8)
                                    .with_z(alt + roof_height + 1),
                            })
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 4, wpos.y + 3)
                                        .with_z(alt + platform_3_height),
                                    max: Vec2::new(wpos.x + 7, wpos.y + 8)
                                        .with_z(alt + roof_height),
                                }),
                            )
                            .without(
                                painter.aabb(Aabb {
                                    min: Vec2::new(wpos.x + 3, wpos.y + 4)
                                        .with_z(alt + platform_3_height),
                                    max: Vec2::new(wpos.x + 8, wpos.y + 7)
                                        .with_z(alt + roof_height),
                                }),
                            );
                        //skirt
                        let skirt1 = painter
                            .aabb(Aabb {
                                min: Vec2::new(wpos.x + 1, wpos.y)
                                    .with_z(alt + platform_1_height - 3),
                                max: Vec2::new(wpos.x + 4, wpos.y + 1)
                                    .with_z(alt + platform_1_height + 1),
                            })
                            .without(painter.line(
                                Vec2::new(wpos.x + 1, wpos.y).with_z(alt + platform_1_height - 1),
                                Vec2::new(wpos.x + 1, wpos.y).with_z(alt + platform_1_height - 3),
                                1.0,
                            ))
                            .without(painter.line(
                                Vec2::new(wpos.x + 3, wpos.y).with_z(alt + platform_1_height - 2),
                                Vec2::new(wpos.x + 3, wpos.y).with_z(alt + platform_1_height - 3),
                                1.0,
                            ));
                        let skirt2 = skirt1
                            .translate(Vec3::new(6, 0, 0))
                            .rotate_about_min(Mat3::new(-1, 0, 0, 0, 1, 0, 0, 0, 1));
                        let skirt3 = skirt2.translate(Vec3::new(3, 0, 0));

                        let skirtside1 = skirt1.union(skirt2).union(skirt3);
                        let skirtside2 = skirtside1
                            .rotate_about_min(Mat3::new(0, -1, 0, 1, 0, 0, 0, 0, 1))
                            .translate(Vec3::new(0, 1, 0));

                        let skirtcorner1 = skirtside1.union(skirtside2);
                        let skirtcorner2 = skirtcorner1
                            .rotate_about_min(Mat3::new(-1, 0, 0, 0, -1, 0, 0, 0, 1))
                            .translate(Vec3::new(11, 11, 0));

                        let skirt1 = skirtcorner1.union(skirtcorner2);
                        let skirt2 = skirt1
                            .rotate_about_min(Mat3::new(1, 0, 0, 0, -1, 0, 0, 0, 1))
                            .translate(Vec3::new(0, 11, 6));

                        let skirt = skirt1.union(skirt2).union(roof);
                        painter.fill(
                            skirt,
                            Fill::Brick(BlockKind::Leaves, Rgb::new(22, 36, 20), 24),
                        );

                        let towerplatform = platform_1.union(platform_2).union(platform_3);

                        painter.fill(
                            towerplatform,
                            Fill::Brick(BlockKind::Wood, Rgb::new(71, 33, 11), 24),
                        );
                        let towervertical = supports
                            .union(platform_1_supports)
                            .union(platform_2_supports)
                            .union(roof_pillars);

                        painter.fill(
                            towervertical,
                            Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 24),
                        );
                    },
                }
            });

        // Create tunnels beneath the fortification
        let wood = Fill::Brick(BlockKind::Wood, Rgb::new(55, 25, 8), 24);
        let dirt = Fill::Brick(BlockKind::Earth, Rgb::new(55, 25, 8), 24);
        let alt = land.get_alt_approx(self.origin) as i32;
        let stump = painter
            .cylinder(Aabb {
                min: (self.tunnels.start.xy() - 10).with_z(alt - 15),
                max: (self.tunnels.start.xy() + 11).with_z(alt + 10),
            })
            .union(painter.cylinder(Aabb {
                min: (self.tunnels.start.xy() - 11).with_z(alt),
                max: (self.tunnels.start.xy() + 12).with_z(alt + 2),
            }))
            .union(painter.line(
                self.tunnels.start.xy().with_z(alt + 10),
                (self.tunnels.start.xy() + 15).with_z(alt - 8),
                5.0,
            ))
            .union(painter.line(
                self.tunnels.start.xy().with_z(alt + 10),
                Vec2::new(self.tunnels.start.x - 15, self.tunnels.start.y + 15).with_z(alt - 8),
                5.0,
            ))
            .union(painter.line(
                self.tunnels.start.xy().with_z(alt + 10),
                Vec2::new(self.tunnels.start.x + 15, self.tunnels.start.y - 15).with_z(alt - 8),
                5.0,
            ))
            .union(painter.line(
                self.tunnels.start.xy().with_z(alt + 10),
                (self.tunnels.start.xy() - 15).with_z(alt - 8),
                5.0,
            ))
            .without(
                painter.sphere_with_radius((self.tunnels.start.xy() + 10).with_z(alt + 26), 18.0),
            )
            .without(
                painter.sphere_with_radius((self.tunnels.start.xy() - 10).with_z(alt + 26), 18.0),
            );
        let entrance_hollow = painter.line(
            self.tunnels.start,
            self.tunnels.start.xy().with_z(alt + 10),
            9.0,
        );

        let boss_room_offset =
            (self.tunnels.end.xy() - self.tunnels.start.xy()).map(|e| if e < 0 { -20 } else { 20 });

        let boss_room = painter.ellipsoid(Aabb {
            min: (self.tunnels.end.xy() + boss_room_offset - 30).with_z(self.tunnels.end.z - 10),
            max: (self.tunnels.end.xy() + boss_room_offset + 30).with_z(self.tunnels.end.z + 10),
        });

        let boss_room_clear = painter.ellipsoid(Aabb {
            min: (self.tunnels.end.xy() + boss_room_offset - 29).with_z(self.tunnels.end.z - 9),
            max: (self.tunnels.end.xy() + boss_room_offset + 29).with_z(self.tunnels.end.z + 9),
        });

        let random_field = RandomField::new(self.seed);

        let mut tunnels = Vec::new();
        let mut path_tunnels = Vec::new();
        let mut tunnels_clear = Vec::new();
        let mut ferns = Vec::new();
        let mut velorite_ores = Vec::new();
        let mut fire_bowls = Vec::new();
        for branch in self.tunnels.branches.iter() {
            let tunnel_radius_i32 = 4 + branch.0.x % 4;
            let in_path =
                self.tunnels.path.contains(&branch.0) && self.tunnels.path.contains(&branch.1);
            let tunnel_radius = tunnel_radius_i32 as f32;
            let start = branch.0;
            let end = branch.1;
            let ctrl0_offset = start.x % 6 * if start.y % 2 == 0 { -1 } else { 1 };
            let ctrl1_offset = end.x % 6 * if end.y % 2 == 0 { -1 } else { 1 };
            let ctrl0 = (((start + end) / 2) + start) / 2 + ctrl0_offset;
            let ctrl1 = (((start + end) / 2) + end) / 2 + ctrl1_offset;
            let tunnel = painter.cubic_bezier(start, ctrl0, ctrl1, end, tunnel_radius);
            let tunnel_clear = painter.cubic_bezier(start, ctrl0, ctrl1, end, tunnel_radius - 1.0);
            let mut fern_scatter = painter.empty();
            let mut velorite_scatter = painter.empty();
            let mut fire_bowl_scatter = painter.empty();

            let min_z = branch.0.z.min(branch.1.z);
            let max_z = branch.0.z.max(branch.1.z);
            for i in branch.0.x - tunnel_radius_i32..branch.1.x + tunnel_radius_i32 {
                for j in branch.0.y - tunnel_radius_i32..branch.1.y + tunnel_radius_i32 {
                    if random_field.get(Vec3::new(i, j, min_z)) % 6 == 0 {
                        fern_scatter = fern_scatter.union(painter.aabb(Aabb {
                            min: Vec3::new(i, j, min_z),
                            max: Vec3::new(i + 1, j + 1, max_z),
                        }));
                    }
                    if random_field.get(Vec3::new(i, j, min_z)) % 30 == 0 {
                        velorite_scatter = velorite_scatter.union(painter.aabb(Aabb {
                            min: Vec3::new(i, j, min_z),
                            max: Vec3::new(i + 1, j + 1, max_z),
                        }));
                    }
                    if random_field.get(Vec3::new(i, j, min_z)) % 30 == 0 {
                        fire_bowl_scatter = fire_bowl_scatter.union(painter.aabb(Aabb {
                            min: Vec3::new(i, j, min_z),
                            max: Vec3::new(i + 1, j + 1, max_z),
                        }));
                    }
                }
            }
            let fern = tunnel_clear.intersect(fern_scatter);
            let velorite = tunnel_clear.intersect(velorite_scatter);
            let fire_bowl = tunnel_clear.intersect(fire_bowl_scatter);
            if in_path {
                path_tunnels.push(tunnel);
            } else {
                tunnels.push(tunnel);
            }
            tunnels_clear.push(tunnel_clear);
            ferns.push(fern);
            velorite_ores.push(velorite);
            fire_bowls.push(fire_bowl);
        }

        let mut rooms = Vec::new();
        let mut rooms_clear = Vec::new();
        let mut chests_ori_0 = Vec::new();
        let mut chests_ori_2 = Vec::new();
        let mut chests_ori_4 = Vec::new();
        for terminal in self.tunnels.terminals.iter() {
            let room = painter.sphere(Aabb {
                min: terminal - 8,
                max: terminal + 8 + 1,
            });
            let room_clear = painter.sphere(Aabb {
                min: terminal - 7,
                max: terminal + 7 + 1,
            });
            rooms.push(room);
            rooms_clear.push(room_clear);

            // FIRE!!!!!
            let fire_bowl = painter.aabb(Aabb {
                min: terminal.with_z(terminal.z - 7),
                max: terminal.with_z(terminal.z - 7) + 1,
            });
            fire_bowls.push(fire_bowl);

            // Chest
            let chest_seed = random_field.get(*terminal) % 5;
            if chest_seed < 4 {
                let chest_pos = Vec3::new(terminal.x, terminal.y - 4, terminal.z - 6);
                let chest = painter.aabb(Aabb {
                    min: chest_pos,
                    max: chest_pos + 1,
                });
                chests_ori_4.push(chest);
                if chest_seed < 2 {
                    let chest_pos = Vec3::new(terminal.x, terminal.y + 4, terminal.z - 6);
                    let chest = painter.aabb(Aabb {
                        min: chest_pos,
                        max: chest_pos + 1,
                    });
                    chests_ori_0.push(chest);
                    if chest_seed < 1 {
                        let chest_pos = Vec3::new(terminal.x - 4, terminal.y, terminal.z - 6);
                        let chest = painter.aabb(Aabb {
                            min: chest_pos,
                            max: chest_pos + 1,
                        });
                        chests_ori_2.push(chest);
                    }
                }
            }
        }
        tunnels
            .into_iter()
            .chain(rooms)
            .chain(core::iter::once(boss_room))
            .chain(core::iter::once(stump))
            .for_each(|prim| prim.fill(wood.clone()));
        path_tunnels.into_iter().for_each(|t| t.fill(dirt.clone()));

        // Clear out insides after filling the walls in
        let mut sprite_clear = Vec::new();
        tunnels_clear
            .into_iter()
            .chain(rooms_clear)
            .chain(core::iter::once(boss_room_clear))
            .for_each(|prim| {
                sprite_clear.push(prim.translate(Vec3::new(0, 0, 1)).intersect(prim));

                prim.clear();
            });

        // Place sprites
        ferns
            .into_iter()
            .for_each(|prim| prim.fill(Fill::Block(Block::air(SpriteKind::JungleFern))));
        velorite_ores
            .into_iter()
            .for_each(|prim| prim.fill(Fill::Block(Block::air(SpriteKind::Velorite))));
        fire_bowls
            .into_iter()
            .for_each(|prim| prim.fill(Fill::Block(Block::air(SpriteKind::FireBowlGround))));

        chests_ori_0.into_iter().for_each(|prim| {
            prim.fill(Fill::Block(
                Block::air(SpriteKind::DungeonChest0).with_ori(0).unwrap(),
            ))
        });
        chests_ori_2.into_iter().for_each(|prim| {
            prim.fill(Fill::Block(
                Block::air(SpriteKind::DungeonChest0).with_ori(2).unwrap(),
            ))
        });
        chests_ori_4.into_iter().for_each(|prim| {
            prim.fill(Fill::Block(
                Block::air(SpriteKind::DungeonChest0).with_ori(4).unwrap(),
            ))
        });

        entrance_hollow.clear();
        sprite_clear.into_iter().for_each(|prim| prim.clear());
    }
}

fn gnarling_mugger<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.dungeon.gnarling.mugger",
        rng,
        None,
    )
}

fn gnarling_stalker<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.dungeon.gnarling.stalker",
        rng,
        None,
    )
}

fn gnarling_logger<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.dungeon.gnarling.logger",
        rng,
        None,
    )
}

fn random_gnarling<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    match rng.gen_range(0..4) {
        0 => gnarling_stalker(pos, rng),
        1 => gnarling_mugger(pos, rng),
        _ => gnarling_logger(pos, rng),
    }
}

fn gnarling_chieftain<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32))
        .with_asset_expect("common.entity.dungeon.gnarling.chieftain", rng, None)
        .with_no_flee()
}

fn deadwood<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.wild.aggressive.deadwood",
        rng,
        None,
    )
}

fn mandragora<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.dungeon.gnarling.mandragora",
        rng,
        None,
    )
}

fn wood_golem<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.dungeon.gnarling.woodgolem",
        rng,
        None,
    )
}

fn harvester_boss<R: Rng>(pos: Vec3<i32>, rng: &mut R) -> EntityInfo {
    EntityInfo::at(pos.map(|x| x as f32)).with_asset_expect(
        "common.entity.dungeon.gnarling.harvester",
        rng,
        None,
    )
}

#[derive(Default)]
struct Tunnels {
    start: Vec3<i32>,
    end: Vec3<i32>,
    branches: Vec<(Vec3<i32>, Vec3<i32>)>,
    path: Vec<Vec3<i32>>,
    terminals: Vec<Vec3<i32>>,
}

impl Tunnels {
    /// Attempts to find a path from a `start` to the `end` using a rapidly
    /// exploring random tree (RRT). A point is sampled from an AABB extending
    /// slightly beyond the start and end in the x and y axes and below the
    /// start in the z axis to slightly below the end. Nodes are stored in a
    /// k-d tree for quicker nearest node calculations. A maximum of 7000
    /// points are sampled until the tree connects the start to the end. A
    /// final path is then reconstructed from the nodes. Returns a `Tunnels`
    /// struct of the RRT branches, the complete path, and the location of
    /// dead ends. Each branch is a tuple of the start and end locations of
    /// each segment. The path is a vector of all the nodes along the
    /// complete path from the `start` to the `end`.
    fn new<F>(
        start: Vec3<i32>,
        end: Vec3<i32>,
        is_valid_edge: F,
        radius_range: (f32, f32),
        rng: &mut impl Rng,
    ) -> Option<Self>
    where
        F: Fn(Vec3<i32>, Vec3<i32>) -> bool,
    {
        let mut nodes = Vec::new();
        let mut node_index: usize = 0;

        // HashMap<ChildNode, ParentNode>
        let mut parents = HashMap::new();

        let mut kdtree = KdTree::new();
        let startf = start.map(|a| (a + 1) as f32);
        let endf = end.map(|a| (a + 1) as f32);

        let min = Vec3::new(
            startf.x.min(endf.x),
            startf.y.min(endf.y),
            startf.z.min(endf.z),
        );
        let max = Vec3::new(
            startf.x.max(endf.x),
            startf.y.max(endf.y),
            startf.z.max(endf.z),
        );

        kdtree
            .add(&[startf.x, startf.y, startf.z], node_index)
            .ok()?;
        nodes.push(startf);
        node_index += 1;
        let mut connect = false;

        for _i in 0..7000 {
            let radius: f32 = rng.gen_range(radius_range.0..radius_range.1);
            let radius_sqrd = radius.powi(2);
            if connect {
                break;
            }
            let sampled_point = Vec3::new(
                rng.gen_range(min.x - 20.0..max.x + 20.0),
                rng.gen_range(min.y - 20.0..max.y + 20.0),
                rng.gen_range(min.z - 20.0..max.z - 7.0),
            );
            let nearest_index = *kdtree
                .nearest_one(
                    &[sampled_point.x, sampled_point.y, sampled_point.z],
                    &squared_euclidean,
                )
                .ok()?
                .1;
            let nearest = nodes[nearest_index];
            let dist_sqrd = sampled_point.distance_squared(nearest);
            let new_point = if dist_sqrd > radius_sqrd {
                nearest + (sampled_point - nearest).normalized().map(|a| a * radius)
            } else {
                sampled_point
            };
            if is_valid_edge(
                nearest.map(|e| e.floor() as i32),
                new_point.map(|e| e.floor() as i32),
            ) {
                kdtree
                    .add(&[new_point.x, new_point.y, new_point.z], node_index)
                    .ok()?;
                nodes.push(new_point);
                parents.insert(node_index, nearest_index);
                node_index += 1;
            }
            if new_point.distance_squared(endf) < radius.powi(2) {
                connect = true;
            }
        }

        let mut path = Vec::new();
        let nearest_index = *kdtree
            .nearest_one(&[endf.x, endf.y, endf.z], &squared_euclidean)
            .ok()?
            .1;
        kdtree.add(&[endf.x, endf.y, endf.z], node_index).ok()?;
        nodes.push(endf);
        parents.insert(node_index, nearest_index);
        path.push(endf);
        let mut current_node_index = node_index;
        while current_node_index > 0 {
            current_node_index = *parents.get(&current_node_index).unwrap();
            path.push(nodes[current_node_index]);
        }

        let mut terminals = Vec::new();
        let last = nodes.len() - 1;
        for (node_id, node_pos) in nodes.iter().enumerate() {
            if !parents.values().any(|e| e == &node_id) && node_id != 0 && node_id != last {
                terminals.push(node_pos.map(|e| e.floor() as i32));
            }
        }

        let branches = parents
            .iter()
            .map(|(a, b)| {
                (
                    nodes[*a].map(|e| e.floor() as i32),
                    nodes[*b].map(|e| e.floor() as i32),
                )
            })
            .collect::<Vec<(Vec3<i32>, Vec3<i32>)>>();
        let path = path
            .iter()
            .map(|a| a.map(|e| e.floor() as i32))
            .collect::<Vec<Vec3<i32>>>();

        Some(Self {
            start,
            end,
            branches,
            path,
            terminals,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_creating_entities() {
        let pos = Vec3::zero();
        let mut rng = thread_rng();

        gnarling_mugger(pos, &mut rng);
        gnarling_stalker(pos, &mut rng);
        gnarling_logger(pos, &mut rng);
        gnarling_chieftain(pos, &mut rng);
        deadwood(pos, &mut rng);
        mandragora(pos, &mut rng);
        wood_golem(pos, &mut rng);
        harvester_boss(pos, &mut rng);
    }
}