-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathMaps.cpp
More file actions
1525 lines (1322 loc) · 47.1 KB
/
Maps.cpp
File metadata and controls
1525 lines (1322 loc) · 47.1 KB
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
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek ([email protected])
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include "ColorText.h"
#include "Core.h"
#include "DataDefs.h"
#include "Error.h"
#include "MemAccess.h"
#include "MiscUtils.h"
#include "ModuleFactory.h"
#include "VersionInfo.h"
#include "modules/Buildings.h"
#include "modules/MapCache.h"
#include "modules/Maps.h"
#include "df/biome_type.h"
#include "df/block_burrow.h"
#include "df/block_burrow_link.h"
#include "df/block_square_event_grassst.h"
#include "df/block_square_event_item_spatterst.h"
#include "df/block_square_event_material_spatterst.h"
#include "df/block_square_event_spoorst.h"
#include "df/building.h"
#include "df/building_type.h"
#include "df/builtin_mats.h"
#include "df/burrow.h"
#include "df/feature_init.h"
#include "df/feature_map_shellst.h"
#include "df/feature_mapst.h"
#include "df/flow_info.h"
#include "df/map_block.h"
#include "df/map_block_column.h"
#include "df/material.h"
#include "df/plant.h"
#include "df/plant_root_tile.h"
#include "df/plant_tree_info.h"
#include "df/plant_tree_tile.h"
#include "df/region_map_entry.h"
#include "df/world.h"
#include "df/world_data.h"
#include "df/world_geo_biome.h"
#include "df/world_geo_layer.h"
#include "df/world_region_details.h"
#include "df/world_underground_region.h"
#include "df/z_level_flags.h"
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <iostream>
using std::max;
using std::min;
using std::string;
using std::vector;
using namespace DFHack;
using namespace df::enums;
using df::global::world;
const char * DFHack::sa_feature(df::feature_type index)
{
switch(index)
{
case feature_type::outdoor_river:
return "River";
case feature_type::cave:
return "Cave";
case feature_type::pit:
return "Pit";
case feature_type::magma_pool:
return "Magma pool";
case feature_type::volcano:
return "Volcano";
case feature_type::deep_special_tube:
return "Adamantine deposit";
case feature_type::deep_surface_portal:
return "Underworld portal";
case feature_type::subterranean_from_layer:
return "Cavern";
case feature_type::magma_core_from_layer:
return "Magma sea";
case feature_type::underworld_from_layer:
return "Underworld";
default:
return "Unknown/Error";
}
};
/*
* Cuboid class fns
*/
cuboid::cuboid(int16_t x1, int16_t y1, int16_t z1, int16_t x2, int16_t y2, int16_t z2) {
x_min = min(x1, x2);
x_max = max(x1, x2);
y_min = min(y1, y2);
y_max = max(y1, y2);
z_min = min(z1, z2);
z_max = max(z1, z2);
}
cuboid::cuboid(int16_t x, int16_t y, int16_t z) {
x_min = x_max = x;
y_min = y_max = y;
z_min = z_max = z;
}
cuboid::cuboid(const df::map_block *block) {
if (block) {
auto &pos = block->map_pos;
x_min = pos.x;
x_max = pos.x + 15;
y_min = pos.y;
y_max = pos.y + 15;
z_min = z_max = pos.z;
}
}
bool cuboid::isValid() const {
return x_min >= 0 && y_min >= 0 && z_min >= 0 &&
x_max >= x_min && y_max >= y_min && z_max >= z_min;
}
cuboid cuboid::clamp(const cuboid &other)
{
if (isValid() && other.isValid()) {
x_min = max(x_min, other.x_min);
y_min = max(y_min, other.y_min);
z_min = max(z_min, other.z_min);
x_max = min(x_max, other.x_max);
y_max = min(y_max, other.y_max);
z_max = min(z_max, other.z_max);
}
else
clear();
return *this;
}
cuboid cuboid::clampMap(bool block)
{
if (!Maps::IsValid() || !isValid()) {
clear();
return *this;
}
int32_t x, y, z;
if (block)
Maps::getSize(x, y, z);
else
Maps::getTileSize(x, y, z);
return clamp(cuboid(0, 0, 0, x-1, y-1, z-1));
}
bool cuboid::addPos(int16_t x, int16_t y, int16_t z)
{
if (x < 0 || y < 0 || z < 0 || (isValid() && containsPos(x, y, z)))
return false;
x_min = (x_min < 0 || x < x_min) ? x : x_min;
x_max = (x_max < 0 || x > x_max) ? x : x_max;
y_min = (y_min < 0 || y < y_min) ? y : y_min;
y_max = (y_max < 0 || y > y_max) ? y : y_max;
z_min = (z_min < 0 || z < z_min) ? z : z_min;
z_max = (z_max < 0 || z > z_max) ? z : z_max;
return true;
}
bool cuboid::containsPos(int16_t x, int16_t y, int16_t z) const {
return x >= x_min && y >= y_min && z >= z_min &&
x <= x_max && y <= y_max && z <= z_max;
}
void cuboid::forCoord(std::function<bool(df::coord)> fn, bool row_major) const
{
if (isValid()) // Only iterate if valid cuboid.
Maps::forCoord(fn, x_min, y_min, z_max, x_max, y_max, z_min, row_major);
}
void cuboid::forBlock(std::function<bool(df::map_block *, cuboid)> fn, bool ensure_block) const
{
auto c = *this; // Create a copy to modify.
if (!c.clampMap().isValid()) // No intersection.
return;
// Process z, y, then x.
for (int16_t x = (c.x_min >> 4) << 4; x <= c.x_max; x += 16)
for (int16_t y = (c.y_min >> 4) << 4; y <= c.y_max; y += 16)
for (int16_t z = c.z_max; z >= c.z_min; z--)
{
auto *block = ensure_block ? Maps::ensureTileBlock(x, y, z) : Maps::getTileBlock(x, y, z);
if (!block) // Skip unallocated block.
continue;
else if (!fn(block, cuboid(block).clamp(c)))
return; // Break iterator.
}
}
/*
* The Maps module
*/
bool Maps::IsValid() {
return (world->map.block_index != NULL);
}
void Maps::forCoord(std::function<bool(df::coord)> fn, int16_t x1, int16_t y1, int16_t z1,
int16_t x2, int16_t y2, int16_t z2, bool row_major)
{
int16_t dx = x1 > x2 ? -1 : 1;
int16_t dy = y1 > y2 ? -1 : 1;
int16_t dz = z1 > z2 ? -1 : 1;
if (row_major) {
// Process z, y, then x.
for (int16_t z = z1; z != z2 + dz; z += dz)
for (int16_t y = y1; y != y2 + dy; y += dy)
for (int16_t x = x1; x != x2 + dx; x += dx)
if (!fn(df::coord(x, y, z)))
return; // Break iterator.
} else {
// Process z, x, then y.
for (int16_t z = z1; z != z2 + dz; z += dz)
for (int16_t x = x1; x != x2 + dx; x += dx)
for (int16_t y = y1; y != y2 + dy; y += dy)
if (!fn(df::coord(x, y, z)))
return; // Break iterator.
}
}
// getter for map size in blocks
inline void getSizeInline (int32_t &x, int32_t &y, int32_t &z)
{
if (!Maps::IsValid())
{
x = y = z = 0;
return;
}
x = world->map.x_count_block;
y = world->map.y_count_block;
z = world->map.z_count_block;
}
void Maps::getSize (int32_t &x, int32_t &y, int32_t &z)
{
getSizeInline(x, y, z);
}
void Maps::getSize (uint32_t &x, uint32_t &y, uint32_t &z) //todo: deprecate me
{
int32_t sx, sy, sz;
getSizeInline(sx, sy, sz);
x = uint32_t(sx);
y = uint32_t(sy);
z = uint32_t(sz);
}
// getter for map size in tiles
inline void getTileSizeInline (int32_t &x, int32_t &y, int32_t &z)
{
getSizeInline(x, y, z);
x *= 16;
y *= 16;
}
void Maps::getTileSize (int32_t &x, int32_t &y, int32_t &z)
{
getTileSizeInline(x, y, z);
}
void Maps::getTileSize (uint32_t &x, uint32_t &y, uint32_t &z) //todo: deprecate me
{
int32_t sx, sy, sz;
getTileSizeInline(sx, sy, sz);
x = uint32_t(sx);
y = uint32_t(sy);
z = uint32_t(sz);
}
// getter for map position
void Maps::getPosition (int32_t &x, int32_t &y, int32_t &z)
{
if (!IsValid())
{
x = y = z = 0;
return;
}
x = world->map.region_x;
y = world->map.region_y;
z = world->map.region_z;
}
/*
* Block reading
*/
df::map_block *Maps::getBlock (int32_t blockx, int32_t blocky, int32_t blockz)
{
if (!IsValid())
return NULL;
if ((blockx < 0) || (blocky < 0) || (blockz < 0))
return NULL;
if ((blockx >= world->map.x_count_block) || (blocky >= world->map.y_count_block) || (blockz >= world->map.z_count_block))
return NULL;
return world->map.block_index[blockx][blocky][blockz];
}
df::map_block_column *Maps::getBlockColumn(int32_t blockx, int32_t blocky)
{
if (!IsValid())
return NULL;
if ((blockx < 0) || (blocky < 0))
return NULL;
if ((blockx >= world->map.x_count_block) || (blocky >= world->map.y_count_block))
return NULL;
return world->map.column_index[blockx][blocky];
}
bool Maps::isValidTilePos(int32_t x, int32_t y, int32_t z)
{
if (!IsValid())
return false;
if ((x < 0) || (y < 0) || (z < 0))
return false;
if ((x >= world->map.x_count) || (y >= world->map.y_count) || (z >= world->map.z_count))
return false;
return true;
}
bool Maps::isTileVisible(int32_t x, int32_t y, int32_t z)
{
df::map_block *block = getTileBlock(x, y, z);
if (!block)
return false;
if (block->designation[x % 16][y % 16].bits.hidden)
return false;
return true;
}
df::map_block *Maps::getTileBlock (int32_t x, int32_t y, int32_t z)
{
if (!isValidTilePos(x,y,z))
return NULL;
return world->map.block_index[x >> 4][y >> 4][z];
}
df::map_block *Maps::ensureTileBlock (int32_t x, int32_t y, int32_t z)
{
if (!isValidTilePos(x,y,z))
return NULL;
auto column = world->map.block_index[x >> 4][y >> 4];
auto &slot = column[z];
if (slot)
return slot;
// Find another block below
int z2 = z;
while (z2 >= 0 && !column[z2]) z2--;
if (z2 < 0)
return NULL;
slot = new df::map_block();
slot->region_pos = column[z2]->region_pos;
slot->map_pos = column[z2]->map_pos;
slot->map_pos.z = z;
// Assume sky
df::tile_designation dsgn;
dsgn.bits.light = true;
dsgn.bits.outside = true;
for (int tx = 0; tx < 16; tx++)
for (int ty = 0; ty < 16; ty++) {
slot->designation[tx][ty] = dsgn;
slot->temperature_1[tx][ty] = column[z2]->temperature_1[tx][ty];
slot->temperature_2[tx][ty] = column[z2]->temperature_2[tx][ty];
}
df::global::world->map.map_blocks.push_back(slot);
return slot;
}
df::tiletype *Maps::getTileType(int32_t x, int32_t y, int32_t z)
{
df::map_block *block = getTileBlock(x,y,z);
return block ? &block->tiletype[x&15][y&15] : NULL;
}
df::tile_designation *Maps::getTileDesignation(int32_t x, int32_t y, int32_t z)
{
df::map_block *block = getTileBlock(x,y,z);
return block ? &block->designation[x&15][y&15] : NULL;
}
df::tile_occupancy *Maps::getTileOccupancy(int32_t x, int32_t y, int32_t z)
{
df::map_block *block = getTileBlock(x,y,z);
return block ? &block->occupancy[x&15][y&15] : NULL;
}
df::region_map_entry *Maps::getRegionBiome(df::coord2d rgn_pos)
{
auto data = world->world_data;
if (!data)
return NULL;
if (rgn_pos.x < 0 || rgn_pos.x >= data->world_width ||
rgn_pos.y < 0 || rgn_pos.y >= data->world_height)
return NULL;
return &data->region_map[rgn_pos.x][rgn_pos.y];
}
void Maps::enableBlockUpdates(df::map_block *blk, bool flow, bool temperature)
{
if (!blk || !(flow || temperature)) return;
if (temperature)
blk->flags.bits.update_temperature = true;
if (flow)
{
blk->flags.bits.update_liquid = true;
blk->flags.bits.update_liquid_twice = true;
}
auto z_flags = world->map_extras.z_level_flags;
int z_level = blk->map_pos.z;
if (z_flags && z_level >= 0 && z_level < world->map.z_count_block)
{
z_flags += z_level;
z_flags->bits.update = true;
z_flags->bits.update_twice = true;
}
}
df::flow_info *Maps::spawnFlow(df::coord pos, df::flow_type type, int mat_type, int mat_index, int density)
{
using df::global::flows;
auto block = getTileBlock(pos);
if (!flows || !block)
return NULL;
auto flow = new df::flow_info();
flow->type = type;
flow->mat_type = mat_type;
flow->mat_index = mat_index;
flow->density = min(100, density);
flow->pos = pos;
block->flows.push_back(flow);
flows->push_back(flow);
return flow;
}
df::feature_init *Maps::getGlobalInitFeature(int32_t index)
{
auto data = world->world_data;
if (!data || index < 0)
return NULL;
auto rgn = vector_get(data->underground_regions, index);
if (!rgn)
return NULL;
return rgn->feature_init;
}
bool Maps::GetGlobalFeature(t_feature &feature, int32_t index)
{
feature.type = (df::feature_type)-1;
auto f = Maps::getGlobalInitFeature(index);
if (!f)
return false;
feature.discovered = false;
feature.origin = f;
feature.type = f->getType();
f->getMaterial(&feature.main_material, &feature.sub_material);
return true;
}
df::feature_init *Maps::getLocalInitFeature(df::coord2d rgn_pos, int32_t index)
{
auto data = world->world_data;
if (!data || index < 0)
return NULL;
if (rgn_pos.x < 0 || rgn_pos.x >= data->world_width ||
rgn_pos.y < 0 || rgn_pos.y >= data->world_height)
return NULL;
// megaregions = 16x16 squares of regions = 256x256 squares of embark squares
df::coord2d bigregion = rgn_pos / 16;
// bigregion is 16x16 regions. for each bigregion in X dimension:
auto fptr = data->feature_map[bigregion.x][bigregion.y].features;
if (!fptr)
return NULL;
df::coord2d sub = rgn_pos & 15;
vector<df::feature_init *> &features = fptr->feature_init[sub.x][sub.y];
return vector_get(features, index);
}
bool GetLocalFeature(t_feature &feature, df::coord2d rgn_pos, int32_t index)
{
feature.type = (df::feature_type)-1;
auto f = Maps::getLocalInitFeature(rgn_pos, index);
if (!f)
return false;
feature.discovered = false;
feature.origin = f;
feature.type = f->getType();
f->getMaterial(&feature.main_material, &feature.sub_material);
return true;
}
inline bool ReadFeaturesInline(int32_t x, int32_t y, int32_t z, t_feature *local, t_feature *global)
{
df::map_block *block = Maps::getBlock(x, y, z);
if (!block)
return false;
return Maps::ReadFeatures(block, local, global);
}
bool Maps::ReadFeatures(int32_t x, int32_t y, int32_t z, t_feature *local, t_feature *global)
{
return ReadFeaturesInline(x, y, z, local, global);
}
bool Maps::ReadFeatures(uint32_t x, uint32_t y, uint32_t z, t_feature *local, t_feature *global) //todo: deprecate me
{
return ReadFeaturesInline(int32_t(x), int32_t(y), int32_t(z), local, global);
}
bool Maps::ReadFeatures(df::map_block * block, t_feature * local, t_feature * global)
{
bool result = true;
if (global)
{
if (block->global_feature != -1)
result &= GetGlobalFeature(*global, block->global_feature);
else
global->type = (df::feature_type)-1;
}
if (local)
{
if (block->local_feature != -1)
result &= GetLocalFeature(*local, block->region_pos, block->local_feature);
else
local->type = (df::feature_type)-1;
}
return result;
}
/*
* Block events
*/
bool Maps::SortBlockEvents(df::map_block *block,
vector <df::block_square_event_mineralst *> *veins,
vector <df::block_square_event_frozen_liquidst *> *ices,
vector <df::block_square_event_material_spatterst *> *materials,
vector <df::block_square_event_grassst *> *grasses,
vector <df::block_square_event_world_constructionst *> *constructions,
vector <df::block_square_event_spoorst *> *spoors,
vector <df::block_square_event_item_spatterst *> *items,
vector <df::block_square_event_designation_priorityst *> *priorities)
{
if (veins)
veins->clear();
if (ices)
ices->clear();
if (constructions)
constructions->clear();
if (materials)
materials->clear();
if (grasses)
grasses->clear();
if (spoors)
spoors->clear();
if (items)
items->clear();
if (!block)
return false;
// read all events
for (size_t i = 0; i < block->block_events.size(); i++)
{
df::block_square_event *evt = block->block_events[i];
switch (evt->getType())
{
case block_square_event_type::mineral:
if (veins)
veins->push_back((df::block_square_event_mineralst *)evt);
break;
case block_square_event_type::frozen_liquid:
if (ices)
ices->push_back((df::block_square_event_frozen_liquidst *)evt);
break;
case block_square_event_type::world_construction:
if (constructions)
constructions->push_back((df::block_square_event_world_constructionst *)evt);
break;
case block_square_event_type::material_spatter:
if (materials)
materials->push_back((df::block_square_event_material_spatterst *)evt);
break;
case block_square_event_type::grass:
if (grasses)
grasses->push_back((df::block_square_event_grassst *)evt);
break;
case block_square_event_type::spoor:
if (spoors)
spoors->push_back((df::block_square_event_spoorst *)evt);
break;
case block_square_event_type::item_spatter:
if (items)
items->push_back((df::block_square_event_item_spatterst *)evt);
break;
case block_square_event_type::designation_priority:
if (priorities)
priorities->push_back((df::block_square_event_designation_priorityst *)evt);
break;
default:
assert("Unhandled block event type" && false); // FIXME temporary - replace with NONE case after structure are updated
break;
}
}
return true;
}
// Based on worldst::add_material_spatter_tile_capped
int32_t Maps::addMaterialSpatter (df::coord pos, int16_t mat, int32_t matg, df::matter_state state, int32_t amount)
{
// Hardcoded maximum
int32_t cap = 255;
// Sanity checks
if (amount > cap)
amount = cap;
// DF doesn't handle negative numbers, so disallow them
if (amount < 0)
amount = 0;
// DF rejects materials of NONE:*
if (mat == -1)
return amount;
// Extra check: make sure the material correctly exists
MaterialInfo matinfo(mat, matg);
if (!matinfo.isValid())
return amount;
df::map_block *block = Maps::getTileBlock(pos);
if (!block)
return amount;
int16_t bx = pos.x & 0xF, by = pos.y & 0xF;
// Extra check: specify state == NONE to auto-pick based on tile temperature
// Note that this won't choose POWDER/PASTE/PRESSED
if (state == df::matter_state::None)
{
uint16_t tile = block->temperature_1[bx][by];
uint16_t melt = matinfo.material->heat.melting_point;
uint16_t boil = matinfo.material->heat.boiling_point;
if (boil != 60001 && tile >= boil)
state = df::matter_state::Gas;
else if (melt != 60001 && tile >= melt)
state = df::matter_state::Liquid;
else
state = df::matter_state::Solid;
}
if (amount > 0)
{
// scan all SPOOR events and clear the PRESENT flag if the type is HFID_COMBINEDCASTE_BP, ITEMT_ITEMST_ORIENT, or MESS
for (size_t i = 0; i < block->block_events.size(); i++)
{
df::block_square_event *evt = block->block_events[i];
if (evt->getType() != block_square_event_type::spoor)
continue;
auto spoor = (df::block_square_event_spoorst *)evt;
if (!spoor->info.flags[bx][by].bits.present)
continue;
if (spoor->info.type[bx][by] == df::spoor_type::HFID_COMBINEDCASTE_BP ||
spoor->info.type[bx][by] == df::spoor_type::ITEMT_ITEMST_ORIENT ||
spoor->info.type[bx][by] == df::spoor_type::MESS)
spoor->info.flags[bx][by].bits.present = false;
}
}
// Find existing matching material spatter
df::block_square_event_material_spatterst *spatter = nullptr;
// DF: get_material_spatter_event_even_if_empty(...)
for (int i = block->block_events.size() - 1; i >= 0; i--)
{
df::block_square_event *evt = block->block_events[i];
if (evt->getType() != block_square_event_type::material_spatter)
continue;
auto spt = (df::block_square_event_material_spatterst *)evt;
if (spt->mat_type == mat && spt->mat_index == matg &&
spt->mat_state == state)
{
spatter = spt;
break;
}
}
// If we didn't find one, make a new one
if (!spatter)
{
spatter = df::allocate<df::block_square_event_material_spatterst>();
spatter->mat_type = mat;
spatter->mat_index = matg;
spatter->mat_state = state;
memset(spatter->amount, 0, sizeof(spatter->amount));
spatter->min_temperature = spatter->max_temperature = 60001;
uint16_t melt = matinfo.material->heat.melting_point;
uint16_t boil = matinfo.material->heat.boiling_point;
switch (state)
{ using namespace df::enums::matter_state;
case Solid:
case Powder:
case Paste:
case Pressed:
if (melt != 60001)
boil = melt;
spatter->max_temperature = boil;
break;
case Liquid:
if (melt != 60001 && melt != 0)
spatter->min_temperature = melt - 1;
spatter->max_temperature = boil;
break;
// Can't really have gas spatters, but DF has this check
// presumably, DF could convert this into a flow
case Gas:
if (boil != 60001 && boil != 0)
spatter->min_temperature = boil - 1;
else if (melt != 60001 && melt != 0)
spatter->min_temperature = melt - 1;
break;
case None:
// impossible
break;
}
// DF doesn't check heatdam/colddam/ignite points here
block->block_events.push_back(spatter);
}
int32_t newamount = spatter->amount[bx][by] + amount;
if (newamount > cap)
{
amount = newamount - cap;
newamount = cap;
}
else
amount = 0;
spatter->amount[bx][by] = (uint8_t)newamount;
block->flags.bits.may_have_material_spatter = 1;
return amount;
}
// Based on worldst::add_item_spatter_tile_capped
int32_t Maps::addItemSpatter (df::coord pos, df::item_type i_type, int16_t i_subtype, int16_t i_subcat1, int32_t i_subcat2, int32_t print_variant, int32_t amount)
{
// DF passes this as a parameter, but it's always the same
int32_t cap = 10000;
// Sanity checks
if (amount > cap)
amount = cap;
// DF doesn't handle negative numbers, so disallow them
if (amount < 0)
amount = 0;
df::map_block *block = Maps::getTileBlock(pos);
if (!block)
return amount;
int16_t bx = pos.x & 0xF, by = pos.y & 0xF;
if (amount > 0)
{
// scan all SPOOR events and clear the PRESENT flag if the type is HFID_COMBINEDCASTE_BP, ITEMT_ITEMST_ORIENT, or MESS
for (size_t i = 0; i < block->block_events.size(); i++)
{
df::block_square_event *evt = block->block_events[i];
if (evt->getType() != block_square_event_type::spoor)
continue;
auto spoor = (df::block_square_event_spoorst *)evt;
if (!spoor->info.flags[bx][by].bits.present)
continue;
if (spoor->info.type[bx][by] == df::spoor_type::HFID_COMBINEDCASTE_BP ||
spoor->info.type[bx][by] == df::spoor_type::ITEMT_ITEMST_ORIENT ||
spoor->info.type[bx][by] == df::spoor_type::MESS)
spoor->info.flags[bx][by].bits.present = false;
}
}
// Allow auto-selecting growth print for plant growths
if (i_type == df::item_type::PLANT_GROWTH && print_variant == -1)
print_variant = Items::pickGrowthPrint(i_subtype, i_subcat1, i_subcat2);
// Find existing matching item spatter
df::block_square_event_item_spatterst *spatter = nullptr;
// DF: get_item_spatter_event_even_if_empty(...)
for (int i = block->block_events.size() - 1; i >= 0; i--)
{
df::block_square_event *evt = block->block_events[i];
if (evt->getType() != block_square_event_type::item_spatter)
continue;
auto spt = (df::block_square_event_item_spatterst *)evt;
if (spt->item_type == i_type && spt->item_subtype == i_subtype &&
spt->mattype == i_subcat1 && spt->matindex == i_subcat2 &&
spt->print_variant == print_variant)
{
spatter = spt;
break;
}
}
// If we didn't find one, make a new one
if (!spatter)
{
spatter = df::allocate<df::block_square_event_item_spatterst>();
spatter->item_type = i_type;
spatter->item_subtype = i_subtype;
spatter->mattype = i_subcat1;
spatter->matindex = i_subcat2;
spatter->print_variant = print_variant;
memset(spatter->amount, 0, sizeof(spatter->amount));
memset(spatter->flag, 0, sizeof(spatter->flag));
spatter->min_temperature = spatter->max_temperature = 60001;
if (Items::usesStandardMaterial(i_type))
{
MaterialInfo info(i_subcat1, i_subcat2);
if (info.isValid())
{
uint16_t melt = info.material->heat.melting_point;
uint16_t boil = info.material->heat.melting_point;
if (melt != 60001)
spatter->max_temperature = melt;
else
spatter->max_temperature = boil;
// DF doesn't look at the heatdam/colddam/ignite temperatures
}
}
block->block_events.push_back(spatter);
}
int32_t newamount = spatter->amount[bx][by] + amount;
if (newamount > cap)
{
amount = newamount - cap;
newamount = cap;
}
else
amount = 0;
spatter->amount[bx][by] = newamount;
spatter->flag[bx][by].bits.season_full_timer = 7;
block->flags.bits.may_have_item_spatter = 1;
return amount;
}
inline bool RemoveBlockEventInline(int32_t x, int32_t y, int32_t z, df::block_square_event * which)
{
df::map_block *block = Maps::getBlock(x, y, z);
if (!block)
return false;
int idx = linear_index(block->block_events, which);
if (idx >= 0)
{
delete which;
vector_erase_at(block->block_events, idx);
return true;
}
else
return false;
}
inline bool Maps::RemoveBlockEvent(int32_t x, int32_t y, int32_t z, df::block_square_event * which)
{
return RemoveBlockEventInline(x, y, z, which);
}
bool Maps::RemoveBlockEvent(uint32_t x, uint32_t y, uint32_t z, df::block_square_event * which) //todo: deprecate me
{
return RemoveBlockEventInline(int32_t(x), int32_t(y), int32_t(z), which);
}
static df::coord2d biome_offsets[9] = {
df::coord2d(-1,-1), df::coord2d(0,-1), df::coord2d(1,-1),
df::coord2d(-1,0), df::coord2d(0,0), df::coord2d(1,0),
df::coord2d(-1,1), df::coord2d(0,1), df::coord2d(1,1)
};
inline df::coord2d getBiomeRgnPos(df::coord2d base, int idx)
{
auto r = base + biome_offsets[idx];
int world_width = world->world_data->world_width;
int world_height = world->world_data->world_height;
return df::coord2d(clip_range(r.x,0,world_width-1),clip_range(r.y,0,world_height-1));
}
df::coord2d Maps::getBlockTileBiomeRgn(df::map_block *block, df::coord2d pos)
{
if (!block || !world->world_data)
return df::coord2d();
auto des = index_tile(block->designation,pos);
unsigned idx = des.bits.biome;
if (idx < 9)
{
idx = block->region_offset[idx];
if (idx < 9)
return getBiomeRgnPos(block->region_pos, idx);
}
return df::coord2d();
}
/*
* Layer geology
*/
bool Maps::ReadGeology(vector<vector<int16_t> > *layer_mats, vector<df::coord2d> *geoidx)
{
if (!world->world_data)
return false;
layer_mats->resize(eBiomeCount);
geoidx->resize(eBiomeCount);
for (int i = 0; i < eBiomeCount; i++)
{
(*layer_mats)[i].clear();
(*geoidx)[i] = df::coord2d(-30000,-30000);
}
// regionX is in embark squares
// regionX/16 is in 16x16 embark square regions
df::coord2d map_region(world->map.region_x / 16, world->map.region_y / 16);
// iterate over 8 surrounding regions + local region
for (int i = eNorthWest; i < eBiomeCount; i++)
{
df::coord2d rgn_pos = getBiomeRgnPos(map_region, i);
(*geoidx)[i] = rgn_pos;
auto biome = getRegionBiome(rgn_pos);
if (!biome)
continue;
// get index into geoblock vector
int16_t geoindex = biome->geo_index;
/// geology blocks have a vector of layer descriptors