forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3dveins.cpp
1658 lines (1331 loc) · 43.9 KB
/
3dveins.cpp
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
#include <iostream>
#include <iomanip>
#include <map>
#include <algorithm>
#include <vector>
#include <math.h>
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/MapCache.h"
#include "modules/Random.h"
#include "modules/World.h"
#include "MiscUtils.h"
#include "DataDefs.h"
#include "df/world.h"
#include "df/world_data.h"
#include "df/world_region_details.h"
#include "df/world_region_feature.h"
#include "df/world_geo_biome.h"
#include "df/world_geo_layer.h"
#include "df/world_underground_region.h"
#include "df/feature_init.h"
#include "df/region_map_entry.h"
#include "df/inclusion_type.h"
#include "df/viewscreen_choose_start_sitest.h"
#include "df/plant.h"
#ifdef LINUX_BUILD
#include <tr1/memory>
using std::tr1::shared_ptr;
#else
#include <memory>
using std::shared_ptr;
#endif
using namespace df::enums;
using namespace DFHack;
using namespace MapExtras;
using namespace DFHack::Random;
DFHACK_PLUGIN("3dveins");
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(gametype);
command_result cmd_3dveins(color_ostream &out, std::vector <std::string> & parameters);
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand(
"3dveins", "Rewrites the veins to make them extend in 3D space.",
cmd_3dveins, false,
" Run this after embark to change all veins on the map to a shape\n"
" that consistently spans Z levels. The operation preserves the\n"
" mineral counts reported by prospect.\n"
));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
/*
* Vein density fields
*/
typedef std::pair<int,df::inclusion_type> t_veinkey;
struct NoiseFunction
{
typedef shared_ptr<NoiseFunction> Ptr;
typedef std::pair<float,float> t_range;
virtual ~NoiseFunction() {};
/*
* Veins are placed by clipping the computed value
* against a floating threshold, with values above
* the threshold causing placement of a vein tile.
*/
virtual float eval(float x, float y, float z) = 0;
virtual t_range range() = 0;
virtual void displace(float &x, float &y, float &z) = 0;
};
inline float apow(float a, float b) { return powf(fabsf(a), b); }
struct Distribution : NoiseFunction
{
float bx, by, bz;
Distribution(MersenneRNG &rng, float scale)
{
bx = rng.drandom() * scale;
by = rng.drandom() * scale;
bz = rng.drandom() * scale;
}
void displace(float &x, float &y, float &z) {
x += bx; y += by; z += bz;
}
};
struct DistributionVein : Distribution
{
PerlinNoise3D<float> density1, density2;
PerlinNoise3D<float> strand1a, strand1b;
DistributionVein(MersenneRNG &rng) : Distribution(rng, 96) {
density1.init(rng);
density2.init(rng);
strand1a.init(rng);
strand1b.init(rng);
}
float eval(float x, float y, float z) {
return 0.1f * density1(x/96, y/96, z/48)
+ 0.2f * density2(x/48, y/48, z/24)
- apow( strand1a(x/24,y/24,z/12)
+0.6f*strand1b(x/16,y/16,z/8), 0.6f);
}
t_range range() { return t_range(-0.3f-1.33f,0.3f); }
};
struct DistributionCluster : Distribution
{
PerlinNoise3D<float> density1, density2, shape;
DistributionCluster(MersenneRNG &rng) : Distribution(rng, 96) {
density1.init(rng);
density2.init(rng);
shape.init(rng);
}
float eval(float x, float y, float z) {
return 0.2f * density1(x/96, y/96, z/32)
+ 0.6f * density2(x/48, y/48, z/16)
+ shape(x/24, y/24, z/8);
}
t_range range() { return t_range(-1.8f,1.8f); }
};
struct DistributionClusterSmall : Distribution
{
PerlinNoise3D<float> density1, density2, shape;
DistributionClusterSmall(MersenneRNG &rng) : Distribution(rng, 96) {
density1.init(rng);
density2.init(rng);
shape.init(rng);
}
float eval(float x, float y, float z) {
const float scale = 1.0f/4.3f;
return 0.06f * density1(x/96, y/96, z/48)
+ 0.12f * density2(x/24, y/24, z/12)
+ apow(shape(x*scale, y*scale, z*scale), 0.1f);
}
t_range range() { return t_range(-0.18f,1.18f); }
};
struct DistributionClusterOne : Distribution
{
PerlinNoise3D<float> density1, density2, shape;
DistributionClusterOne(MersenneRNG &rng) : Distribution(rng, 96) {
density1.init(rng);
density2.init(rng);
shape.init(rng);
}
float eval(float x, float y, float z) {
return 0.05f * density1(x/96, y/96, z/48)
+ 0.1f * density2(x/48, y/48, z/24)
+ shape(x-bx, y-by, z-bz);
}
t_range range() { return t_range(-1.15f,1.15f); }
};
static NoiseFunction *makeVeinDistribution(t_veinkey vein, MersenneRNG &rng)
{
using namespace df::enums::inclusion_type;
switch (vein.second)
{
case VEIN:
return new DistributionVein(rng);
case CLUSTER_SMALL:
return new DistributionClusterSmall(rng);
case CLUSTER_ONE:
return new DistributionClusterOne(rng);
case CLUSTER:
default:
return new DistributionCluster(rng);
}
}
/*
* Data structures.
*/
template<class T>
class BlockGrid
{
df::coord dim;
std::vector<T> buf;
public:
BlockGrid(df::coord size) : dim(size) {
buf.resize(dim.x * dim.y * dim.z);
}
BlockGrid(df::coord2d size, int zdepth = 1)
: dim(df::coord(size.x, size.y, zdepth))
{
buf.resize(dim.x * dim.y * dim.z);
}
const df::coord &size() { return dim; }
void resize_depth(int16_t zdepth) {
dim.z = zdepth;
buf.resize(dim.x * dim.y * dim.z);
}
void shift_depth(int16_t to_add) {
dim.z += to_add;
buf.insert(buf.begin(), dim.x*dim.y*to_add, T());
}
T &operator() (int x, int y, int z = 0) {
return buf[x + dim.x*(y + dim.y*z)];
}
T &operator() (df::coord pos) {
return buf[pos.x + dim.x*(pos.y + dim.y*pos.z)];
}
T &operator() (df::coord2d pos, int z = 0) {
return buf[pos.x + dim.x*(pos.y + dim.y*z)];
}
};
enum SpecialMatCodes {
// Tile not mapped to an actual tile
SMC_NO_MAPPING = -1,
// Tile not assigned to a vein yet
SMC_LAYER = -2
};
struct GeoLayer;
struct GeoColumn;
struct GeoBiome;
/* Representation of a block in geolayer coordinate space.
* That is, it represents a block that would have happened
* if geological layers weren't shifted in Z direction to
* conform to terrain height. Computing veins in this system
* of coordinates should make them nicely follow the layers.
*/
struct GeoBlock
{
GeoLayer *layer;
GeoColumn *column;
df::coord pos;
uint16_t arena_mask, arena_unmined;
int16_t arena_material;
df::tile_bitmask unmined;
int16_t material[16][16];
uint8_t veintype[16][16];
float weight[16][16];
GeoBlock(GeoLayer *parent, df::coord pos) : layer(parent), pos(pos) {
memset(material, -1, sizeof(material));
}
bool prepare_arena(int16_t env_material, NoiseFunction::Ptr fn);
int measure_placement(float threshold);
void place_tiles(float threshold, int16_t new_material, df::inclusion_type itype);
};
/*
* A set of layers that have the same vein type, distribution
* and approximate location, and thus should be placed using
* one binsearch pass for more uniform appearance.
*/
struct VeinExtent
{
typedef shared_ptr<VeinExtent> Ptr;
typedef std::vector<Ptr> PVec;
t_veinkey vein;
int probability, num_tiles;
Ptr parent;
int parent_depth;
bool placed;
int placed_tiles;
NoiseFunction::Ptr distribution;
int num_unmined, num_layer, min_z, max_z;
std::vector<GeoLayer*> layers;
VeinExtent(t_veinkey vein) : vein(vein) {
probability = num_tiles = placed_tiles = 0;
num_unmined = num_layer = 0;
min_z = max_z = 0;
parent_depth = 0;
placed = false;
}
float density() { return float(num_tiles) / num_unmined; }
void set_parent(Ptr pp) {
if (parent)
parent->add_tiles(-num_tiles);
parent = pp;
if (parent)
parent->add_tiles(num_tiles);
parent_depth = (pp ? pp->parent_depth+1 : 0);
}
int parent_mat() {
return parent ? parent->vein.first : SMC_LAYER;
}
void add_tiles(int tiles) {
num_tiles += tiles;
if (parent)
parent->add_tiles(tiles);
}
bool is_similar(Ptr ext2) {
return probability == ext2->probability &&
parent_mat() == ext2->parent_mat();
}
void link(GeoLayer *layer);
void merge_into(VeinExtent::Ptr ext2);
void place_tiles();
};
struct GeoColumn
{
// Original Z level values for each layer
int16_t min_level[16][16][BiomeInfo::MAX_LAYERS];
int16_t max_level[16][16][BiomeInfo::MAX_LAYERS];
int8_t top_layer[16][16];
int8_t bottom_layer[16][16];
int8_t top_solid_z[16][16];
GeoColumn()
{
memset(min_level, -1, sizeof(min_level));
memset(max_level, -1, sizeof(max_level));
memset(top_layer, -1, sizeof(top_layer));
memset(bottom_layer, -1, sizeof(bottom_layer));
memset(top_solid_z, -1, sizeof(top_solid_z));
}
};
struct GeoLayer
{
GeoBiome *biome;
int index;
df::world_geo_layer *info;
int thickness, z_bias;
int aquifer_depth;
int16_t material;
bool is_soil;
bool is_soil_layer;
// World-global origin coordinates in blocks
df::coord world_pos;
int min_z() { return world_pos.z - z_bias; }
int max_z() { return world_pos.z + thickness - 1; }
df::coord2d size;
BlockGrid<GeoBlock*> blocks;
std::vector<GeoBlock*> block_list;
int tiles, unmined_tiles, mineral_tiles;
std::map<t_veinkey,int> mineral_count;
std::map<t_veinkey, VeinExtent::Ptr> veins;
GeoLayer(GeoBiome *parent, int index, df::world_geo_layer *info);
~GeoLayer() {
for (size_t i = 0; i < block_list.size(); i++)
delete block_list[i];
}
GeoBlock *blockAt(df::coord pos)
{
assert(pos.z >= 0);
if (pos.z >= blocks.size().z)
blocks.resize_depth(pos.z+1);
GeoBlock *&blk = blocks(pos);
if (!blk) {
blk = new GeoBlock(this, pos);
block_list.push_back(blk);
}
return blk;
}
GeoBlock *getBlockAt(df::coord pos)
{
if (pos.z < 0 || pos.z >= blocks.size().z)
return NULL;
return blocks(pos);
}
void setZBias(int bias)
{
if (bias <= z_bias) return;
blocks.shift_depth(bias - z_bias);
z_bias = bias;
}
void print_mineral_stats(color_ostream &out)
{
for (auto it = mineral_count.begin(); it != mineral_count.end(); ++it)
out << " " << MaterialInfo(0,it->first.first).getToken()
<< " " << ENUM_KEY_STR(inclusion_type,it->first.second)
<< ": \t\t" << it->second << " (" << (float(it->second)/unmined_tiles) << ")" << std::endl;
out.print(" Total tiles: %d (%d unmined)\n", tiles, unmined_tiles);
}
bool form_veins(color_ostream &out);
};
struct GeoBiome
{
const BiomeInfo &info;
df::coord2d world_pos;
df::coord2d size;
BlockGrid<GeoColumn> columns;
std::vector<GeoLayer*> layers;
GeoBiome(const BiomeInfo &biome, df::coord2d base, df::coord2d mapsize)
: info(biome), world_pos(base), size(mapsize), columns(mapsize)
{
}
~GeoBiome()
{
for (size_t i = 0; i < layers.size(); i++)
delete layers[i];
}
bool init_layers();
void print_mineral_stats(color_ostream &out)
{
out.print("Geological biome %d:\n", info.geo_index);
for (size_t i = 0; i < layers.size(); i++)
if (layers[i])
{
out << " Layer " << i << std::endl;
layers[i]->print_mineral_stats(out);
}
}
};
GeoLayer::GeoLayer(GeoBiome *parent, int index, df::world_geo_layer *info)
: biome(parent), index(index), info(info),
world_pos(parent->world_pos.x, parent->world_pos.y, -info->top_height),
size(parent->size), blocks(parent->size)
{
thickness = info->top_height - info->bottom_height + 1;
z_bias = 0;
aquifer_depth = 0;
tiles = unmined_tiles = mineral_tiles = 0;
material = info->mat_index;
is_soil = isSoilInorganic(material);
is_soil_layer = (info->type == geo_layer_type::SOIL || info->type == geo_layer_type::SOIL_SAND);
}
const unsigned NUM_INCLUSIONS = 1+(int)ENUM_LAST_ITEM(inclusion_type);
struct VeinGenerator
{
color_ostream &out;
MapCache map;
df::coord2d size;
df::coord2d base;
std::map<int, GeoBiome*> biomes;
std::vector<GeoBiome*> biome_by_idx;
struct VMats {
bool can_support_aquifer;
int8_t default_type;
int8_t valid_type[NUM_INCLUSIONS];
uint32_t seeds[NUM_INCLUSIONS];
NoiseFunction::Ptr funcs[NUM_INCLUSIONS];
VMats()
{
default_type = -2;
memset(valid_type, -1, sizeof(valid_type));
}
};
std::vector<VMats> materials;
std::map<t_veinkey, VeinExtent::PVec> veins;
VeinGenerator(color_ostream &out) : out(out) {}
~VeinGenerator() {
for (auto it = biomes.begin(); it != biomes.end(); ++it)
delete it->second;
}
GeoLayer *mapLayer(Block *pb, df::coord2d tile);
bool init_biomes();
bool scan_tiles();
bool scan_layer_depth(Block *b, df::coord2d column, int z);
bool adjust_layer_depth(df::coord2d column);
bool scan_block_tiles(Block *b, df::coord2d column, int z);
void write_tiles();
void write_block_tiles(Block *b, df::coord2d column, int z);
bool form_veins();
bool place_orphan(t_veinkey vein, int size, GeoLayer *from);
void init_seeds();
NoiseFunction::Ptr get_noise(t_veinkey vein);
bool place_veins(bool verbose);
void print_mineral_stats()
{
for (auto it = biomes.begin(); it != biomes.end(); ++it)
it->second->print_mineral_stats(out);
}
};
/*
* General initialization
*/
bool VeinGenerator::init_biomes()
{
auto &mats = df::inorganic_raw::get_vector();
materials.resize(world->raws.inorganics.size());
for (size_t i = 0; i < mats.size(); i++)
{
materials[i].can_support_aquifer = mats[i]->flags.is_set(inorganic_flags::AQUIFER);
// Be silent about slade veins, which can happen below hell
if (mats[i]->flags.is_set(inorganic_flags::DEEP_SURFACE))
materials[i].valid_type[0] = -2;
}
biome_by_idx.resize(map.getBiomeCount());
size = df::coord2d(map.maxBlockX()+1, map.maxBlockY()+1);
base = df::coord2d(world->map.region_x*3, world->map.region_y*3);
for (size_t i = 0; i < biome_by_idx.size(); i++)
{
const BiomeInfo &info = map.getBiomeByIndex(i);
if (info.geo_index < 0 || !info.geobiome)
{
out.printerr("Biome %zd is not defined.\n", i);
return false;
}
GeoBiome *&biome = biomes[info.geo_index];
if (!biome)
{
biome = new GeoBiome(info, base, size);
if (!biome->init_layers())
return false;
// Mark valid vein types
auto &layers = info.geobiome->layers;
for (size_t i = 0; i < layers.size(); i++)
{
auto layer = layers[i];
for (size_t j = 0; j < layer->vein_mat.size(); j++)
{
if (unsigned(layer->vein_mat[j]) >= materials.size() ||
unsigned(layer->vein_type[j]) >= NUM_INCLUSIONS)
continue;
auto &minfo = materials[layer->vein_mat[j]];
int type = layer->vein_type[j];
minfo.valid_type[type] = type;
if (minfo.default_type < 0 || type == inclusion_type::CLUSTER)
minfo.default_type = type;
}
}
}
biome_by_idx[i] = biome;
}
return true;
}
bool GeoBiome::init_layers()
{
auto &info_layers = info.geobiome->layers;
layers.resize(info_layers.size());
for (size_t i = 0; i < layers.size(); i++)
{
layers[i] = new GeoLayer(this, i, info_layers[i]);
}
return true;
}
/*
* Initial statistics collection scan. It has to:
*
* 1) Find out how the layers are shifted in Z direction for each tile column.
* 2) Record which tiles are available, which are unmined, and how much minerals are there.
*/
GeoLayer *VeinGenerator::mapLayer(Block *pb, df::coord2d tile)
{
int idx = pb->biomeIndexAt(tile);
GeoBiome *biome = biome_by_idx.at(idx);
int lidx = pb->layerIndexAt(tile);
if (unsigned(lidx) >= biome->layers.size())
return NULL;
return biome->layers[lidx];
}
static bool isTransientMaterial(df::tiletype tile)
{
using namespace df::enums::tiletype_material;
switch (tileMaterial(tile))
{
case AIR:
case LAVA_STONE:
case PLANT:
case ROOT:
case TREE:
case MUSHROOM:
return true;
default:
return false;
}
}
static bool isSkyBlock(Block *b)
{
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
df::coord2d tile(x,y);
auto dsgn = b->DesignationAt(tile);
auto ttype = b->baseTiletypeAt(tile);
if (dsgn.bits.subterranean || !dsgn.bits.light || !isTransientMaterial(ttype))
return false;
}
}
return true;
}
static int findTopBlock(MapCache &map, int x, int y)
{
for (int z = map.maxZ(); z >= 0; z--)
{
Block *b = map.BlockAt(df::coord(x,y,z));
if (b && b->is_valid() && !isSkyBlock(b))
return z;
}
return -1;
}
bool VeinGenerator::scan_tiles()
{
for (int x = 0; x < size.x; x++)
{
for (int y = 0; y < size.y; y++)
{
df::coord2d column(x,y);
int top = findTopBlock(map, x, y);
// First find where layers start and end
for (int z = top; z >= 0; z--)
{
Block *b = map.BlockAt(df::coord(x,y,z));
if (!b || !b->is_valid())
continue;
if (!scan_layer_depth(b, column, z))
return false;
}
if (!adjust_layer_depth(column))
return false;
// Collect tile data
for (int z = top; z >= 0; z--)
{
Block *b = map.BlockAt(df::coord(x,y,z));
if (!b || !b->is_valid())
continue;
if (!scan_block_tiles(b, column, z))
return false;
map.discardBlock(b);
}
// Discard this column of parsed blocks
map.trash();
}
}
return true;
}
bool VeinGenerator::scan_layer_depth(Block *b, df::coord2d column, int z)
{
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
df::coord2d tile(x,y);
GeoLayer *layer = mapLayer(b, tile);
if (!layer)
continue;
int idx = layer->index;
auto &col_info = layer->biome->columns(column);
auto &max_level = col_info.max_level[x][y];
auto &min_level = col_info.min_level[x][y];
auto &top = col_info.top_layer[x][y];
auto &top_solid = col_info.top_solid_z[x][y];
auto &bottom = col_info.bottom_layer[x][y];
auto ttype = b->baseTiletypeAt(tile);
bool obsidian = isTransientMaterial(ttype);
if (top_solid < 0 && !obsidian && isWallTerrain(ttype))
top_solid = z;
if (max_level[idx] < 0)
{
// Do not start the layer stack in open air.
// Those tiles can be very weird.
if (bottom < 0 && (isOpenTerrain(ttype) || obsidian))
continue;
max_level[idx] = min_level[idx] = z;
if (top < 0 || idx < top)
top = idx;
bottom = std::max<int8_t>(idx, bottom);
}
else
{
if (z != min_level[idx]-1 && min_level[idx] <= top_solid)
{
out.printerr(
"Discontinuous layer %d at (%d,%d,%d).\n",
layer->index, x+column.x*16, y+column.y*16, z
);
return false;
}
min_level[idx] = z;
}
}
}
return true;
}
bool VeinGenerator::adjust_layer_depth(df::coord2d column)
{
for (auto it = biomes.begin(); it != biomes.end(); ++it)
{
GeoBiome *biome = it->second;
auto &col_info = biome->columns(column);
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
auto &max_level = col_info.max_level[x][y];
auto &min_level = col_info.min_level[x][y];
auto top_solid = col_info.top_solid_z[x][y];
int min_defined = col_info.top_layer[x][y];
int max_defined = col_info.bottom_layer[x][y];
if (max_defined < 0)
continue;
int last_top = min_defined;
// Verify assumptions
for (int i = min_defined; i < max_defined; i++)
{
if (max_level[i] >= top_solid)
last_top = i;
if (max_level[i+1] < 0 && min_level[i] > top_solid)
max_level[i+1] = min_level[i+1] = min_level[i];
if (max_level[i+1] > top_solid)
continue;
if (max_level[i+1] != min_level[i]-1)
{
out.printerr(
"Gap or overlap with next layer %d at (%d,%d,%d-%d).\n",
i+1, x+column.x*16, y+column.y*16, max_level[i+1], min_level[i]
);
return false;
}
}
for (int i = min_defined; i < max_defined; i++)
{
auto layer = biome->layers[i];
int size = max_level[i]-min_level[i]+1;
if (size == layer->thickness)
continue;
// Adjust the top layers so that the bottom of the layer maps to the correct place
if (max_level[i] >= top_solid)
{
max_level[i] += layer->thickness - size;
if (size > layer->thickness)
layer->setZBias(size - layer->thickness);
continue;
}
// If below a thick soil layer, allow thickness to pass from prev to current.
// This accounts for a probable bug in worldgen soil placement code.
if (i > min_defined && i-1 <= last_top)
{
auto prev = biome->layers[i-1];
if (size > layer->thickness &&
prev->is_soil_layer && prev->thickness > 1 &&
size <= layer->thickness+prev->thickness-1)
{
max_level[i] += layer->thickness - size;
layer->setZBias(size - layer->thickness);
continue;
}
}
out.printerr(
"Layer height change in layer %d at (%d,%d,%d): %d instead of %d.\n",
i, x+column.x*16, y+column.y*16, max_level[i],
size, biome->layers[i]->thickness
);
return false;
}
}
}
}
return true;
}
bool VeinGenerator::scan_block_tiles(Block *b, df::coord2d column, int z)
{
bool aquifer = b->getRaw()->flags.bits.has_aquifer;
for (int x = 0; x < 16; x++)
{
for (int y = 0; y < 16; y++)
{
df::coord2d tile(x,y);
GeoLayer *layer = mapLayer(b, tile);
if (!layer)
continue;
auto tt = b->baseTiletypeAt(tile);
bool wall = isWallTerrain(tt);
bool matches = false;
switch (tileMaterial(tt))
{
case tiletype_material::MINERAL:
{
t_veinkey key(b->veinMaterialAt(tile),b->veinTypeAt(tile));
// Check if the vein material and type is reasonable
if (unsigned(key.first) >= materials.size() ||
unsigned(key.second) >= NUM_INCLUSIONS)
{
out.printerr("Invalid vein code: %d %d - aborting.\n",key.first,key.second);
return false;
}
int8_t &status = materials[key.first].valid_type[key.second];
if (status == -1)
{
// Report first occurence of unreasonable vein spec
out.printerr(
"Unexpected vein %s %s - ",
MaterialInfo(0,key.first).getToken().c_str(),
ENUM_KEY_STR(inclusion_type, key.second).c_str()
);
status = materials[key.first].default_type;
if (status < 0)
out.printerr("will be left in place.\n");
else
out.printerr(
"correcting to %s.\n",
ENUM_KEY_STR(inclusion_type, df::inclusion_type(status)).c_str()
);
}
if (status >= 0)
{
key.second = df::inclusion_type(status);
matches = true;
// Count minerals
if (wall)
{
layer->mineral_tiles++;
layer->mineral_count[key]++;
}
}
break;
}
case tiletype_material::SOIL:
matches = layer->is_soil;
break;
case tiletype_material::STONE:
matches = !layer->is_soil;
break;
default:;
}
// This tile should be overwritten
if (matches)
{
auto &col_info = layer->biome->columns(column);
int max_level = col_info.max_level[x][y][layer->index] + layer->z_bias;
GeoBlock *block = layer->blockAt(df::coord(column, max_level-z));
block->material[x][y] = SMC_LAYER;
layer->tiles++;
if (wall)
{