-
Notifications
You must be signed in to change notification settings - Fork 39
/
mcpe_viz.cc
6364 lines (5351 loc) · 231 KB
/
mcpe_viz.cc
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
/*
Minecraft Pocket Edition (MCPE) World File Visualization & Reporting Tool
(c) Plethora777, 2015.9.26
GPL'ed code - see LICENSE
Requires Mojang's modified LevelDB library (see README.md for details)
Requires libnbt++ (see README.md for details)
To build it, use cmake
todozooz
* update web ui for new mobs / objects / entities
* block or item based on ID is no longer valid?! (search on 256 in nbt code)
maybe?
* move from xml to json?
* ui for mobs from xml instead of hard-coded?
todohere
* as of beta 1.2.x everything has gone quite mad:
- cubic chunks now have a palette instead of direct block-id's
- " " no longer have block light / sky light?! (therefore, can't do check spawnable anymore)
* as of 0.17 - grass color appears to no longer be a thing that is stored in the data files? remove related code here and in js?
* as of 0.16 entity id's have extra data in the high bytes -- collect these and see if we can figure out what they are -- see mcpe.nbt.cc
* update inventory images for 0.16 and 0.17
* web - tool to extract schematics for a 3d cube of space -- produce a layer-by-layer depiction of the blocks in the cube
-- see: http://minecraft.gamepedia.com/Schematic_file_format (NBT file format for schematics)
-- https://irath96.github.io/webNBT/ = web nbt viewer/editor
* /u/JustinUser -- reset world file outside given area (e.g. X blocks from spawn; given rectangles; etc)
* see data dump here; http://pastebin.com/tuMyCDyc -- any data we need? (updated: http://pastebin.com/be3dwGFA)
also: https://www.reddit.com/r/MCPE/comments/4nip1u/updated_the_blocksitems_list_for_0150/
* tool to show differences between worlds (v1 vs v2 etc)
-- layer by layer?
-- geojson?
* add a tool to help convert from overwold to nether coordinates
* check minor update to libnbt++ 5/2/2016
* should error on fail copy js lib files
* out56.log had a zombie pigman with 'nan' for position - filter this
* xml missing item 358 (probably filled map); probably other map related things + maps in item frames etc
* possible xml errors:
-- another1-dropbox - top of pyramid - quartz with unknown blockdata (6 and 10)
-- "upper slab, quartz" shows as "upper slab, nether brick" -- R.E. all variants in creative
-- "flower pot" w/ cactus shows as flower pot with poppy -- R.E. all variants in creative
-- block of quartz variants appear to be wrong -- R.E. in creative
* win gui --
-- icon
-- use taskbar progress bar - see: http://www.codeproject.com/Articles/42345/Windows-Goodies-in-C-Taskbar-Progress-and-Status
* could use png files to determine avg color of each block
* add icons for enchanted items?
* expand icons idea - use lil images of mobs etc in UI?
* test web app on mobile -- what can be optimized? smaller tiles?
* option to geojson items in chests etc (e.g. melon seeds; beetroot seeds)
* NO -- rethink coordinates used in js -- just negate the y-coord? could then adjust mouse pos output accordingly
* bug: js in tile mode, zoom to extent, does not show full extent (right side is chopped for 'another1')
-- also see reddit bartszelag world for extreme example (very wide, not so high world)
-- ol bug?
* check spawnable code - it might be missing spots?
* gui: allow users to easily add cmd-line params
-- a btn for mcpe_viz --help output (to see usage)
-- a text field for "Add Params"
* minimize geojson output?
-- remove spaces
-- simplify the properties? e.g. GType=Foo where Foo is the type of this record
* auto-tile (and useTilesFlag) should be per-dimension
-- js should use per-dimension tile vars instead of global
* optimize tiler (slow libpng is probably the limiting factor)
* finalize new elevation stuff
-- simplify it? combine the shaded relief and the alpha into one web app ui control + opacity slider -- or into one layer
todobig
* investigate parsing mcpc worlds
-- create an abstraction lib/base class of libminecraftworld (search on 'todolib')
---- move leveldb stuff to something like libminecraftworld.leveldb
---- create/adapt a lib for libminecraftworld.anvil (and mcregion)
---- read AND write
---- one way that ppl can convert mcpe to mcpc:
------ https://www.reddit.com/r/MCPE/comments/1y996x/tutorial_how_to_convert_worlds_from_pe_to_pc/
-- note: PocketMine uses mcregion, anvil or leveldb
---- see CrystalKingdom.zip for an example pocketmine world w/ mcregion files (level.dat gzipped?)
* allow user to provide APK (or file from it) and then use assets from the APK in web app?
-- e.g. - chest contents w/ icons for items
* add options or separate tool to do world modification
-- remove chunks (to allow them to regenerate w/ new features from an updated mcpe)
* add "interesting" blocks to geojson (this is something like the "layers" idea)
-- end portal frame
-- diamonds
-- ores: gold, iron, coal, redstone, lapis
-- trees: (by type -- use wood up/down?)
-- cobwebs
-- flowers?
-- crops: beetroot esp + netherwart + melon stem + pumpkin stem; lily pad?
-- ice + packed ice (how to coalesce?)
* change git layout
-- remove winX.zip files; make "release" for each update w/ the winX.zip files
-- put code in subdirs?
* android/ios app
* write script to check for updates to js/ files?
* check --out -- error if it is a directory
* parse potions etc in inventories etc
* do variants for items + entities?
* do away with image coord space for web app? coords are crazy -- confirmed that negative-Z is north
* do chunk grid here instead of in web ui? (same func as slime chunks)
* use boost for filesystem stuff?
* try with clang; possible to mingw w/ clang?
* see reddit test1 world -- has this msg (could be result of bad mcedit or similar):
-- WARNING: Did not find block variant for block(Wooden Double Slab) with blockdata=8 (0x8)
* join chests for geojson? (pairchest) - would require that we wait to toGeoJSON until we parse all chunks
-- put ALL chests in a map<x,y,z>; go through list of chests and put PairChest in matching sets and mark all as unprocessed; go thru list and do all, if PairChest mark both as done
* see if there is interesting info re colors for overview map: http://minecraft.gamepedia.com/Map_item_format
* option to name output files w/ world name
* use "solid" info from block info to do something? could it fix the light map?
* update block xml w/ transparency info - bool or int? or just solid/nonsolid?
-- use this transparency (or something else? e.g. spawnable flag?) to determine which block is the top block for purposes of the light map
-- go and look at wiki to see the type of info that is stored per block
* convert all printf-style stuff to streams
* find better hsl/hsv to/from rgb funcs; or really, better way to sort colors so that colortest is more useful
user suggestions
* tobomori: ability to hide only a block variant (e.g. Tall Grass)
-- difficult because of fastBlockHideList etc
todo
** cmdline options:
save set of slices
save a particular slice
draw text on slice files (e.g. Y)
separate logfiles for overworld + nether + unknown
options to reduce log file detail
** maps/instructions to get from point A (e.g. spawn) to biome type X (in blocks but also in landmark form: over 2 seas left at the birch forest etc); same for items and entities
todo win32/win64 build
* immediate crash if using -O2? (and now -O1 and -O)
* leveldb close() issue -- link fails on missing stream stuff
* leveldb -O2 build issue -- link fails on missing stream stuff
* leveldb fread_nolock issue -- link fails; forcing msvcrXXX.a crashes on windows
* log file: change end line to CRLF?
** osx build? cross compile tools look kinda horrible; see https://www.macports.org/
*/
// define this to use memcpy instead of manual copy of individual pixel values
// memcpy appears to be approx 1.3% faster for another1 --html-all
#define PIXEL_COPY_MEMCPY
#include <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
#include <getopt.h>
#include <math.h>
#include <dirent.h>
#include <random>
#include "leveldb/db.h"
#include "leveldb/env.h"
#include "leveldb/cache.h"
#include "leveldb/decompress_allocator.h"
// hide innocuous warnings here
#pragma GCC diagnostic ignored "-Wshadow"
#include "leveldb/zlib_compressor.h"
#pragma GCC diagnostic pop
#include "leveldb/filter_policy.h"
#include "mcpe_viz.util.h"
#include "mcpe_viz.h"
#include "mcpe_viz.nbt.h"
#include "mcpe_viz.xml.h"
namespace mcpe_viz {
// todo - removed anonymous namespace here
// maximum build height -- as of MCPE 0.13 it is 127
const int32_t MAX_BLOCK_HEIGHT_127 = 127;
const int32_t MAX_BLOCK_HEIGHT = 255;
const int32_t MAX_CUBIC_Y = (MAX_BLOCK_HEIGHT + 1) / 16;
const int32_t NUM_BYTES_CHUNK_V3 = 10241;
std::string dirExec;
Logger logger;
Logger slogger;
// todobig -- would be nice for these to be in world class
double playerPositionImageX=0.0, playerPositionImageY=0.0;
int32_t playerPositionDimensionId=kDimIdOverworld;
// list of geojson items
std::vector<std::string> listGeoJSON;
// palettes
int32_t palRedBlackGreen[256];
// info lists (from XML)
BlockInfo blockInfoList[512];
ItemInfoList itemInfoList;
EntityInfoList entityInfoList;
BiomeInfoList biomeInfoList;
EnchantmentInfoList enchantmentInfoList;
IntIntMap mcpcToMcpeBlock;
IntIntMap mcpeToMcpcBlock;
IntIntMap mcpcToMcpeItem;
IntIntMap mcpeToMcpcItem;
StringIntMap imageFileMap;
int32_t globalIconImageId = 1;
PlayerIdToName playerIdToName;
leveldb::ReadOptions levelDbReadOptions;
enum OutputType : int32_t {
kDoOutputNone = -2,
kDoOutputAll = -1
};
enum HeightMode : int32_t {
kHeightModeTop = 0,
kHeightModeLevelDB = 1
};
// output image types
enum ImageModeType : int32_t {
kImageModeTerrain = 0,
kImageModeBiome = 1,
kImageModeGrass = 2,
kImageModeHeightCol = 3,
kImageModeHeightColGrayscale = 4,
kImageModeBlockLight = 5,
kImageModeSkyLight = 6,
kImageModeSlimeChunksMCPC = 7,
kImageModeHeightColAlpha = 8,
kImageModeShadedRelief = 9,
kImageModeSlimeChunksMCPE = 10
};
// suggestion from mcpe_sample_setup.cpp
class NullLogger : public leveldb::Logger {
public:
void Logv(const char*, va_list) override {
}
};
// all user options are stored here
class Control {
public:
std::string dirLeveldb;
std::string fnOutputBase;
std::string fnCfg;
std::string fnXml;
std::string fnLog;
std::string fnGeoJSON;
std::string fnHtml;
std::string fnJs;
// per-dimension filenames
std::string fnLayerTop[kDimIdCount];
std::string fnLayerBiome[kDimIdCount];
std::string fnLayerHeight[kDimIdCount];
std::string fnLayerHeightGrayscale[kDimIdCount];
std::string fnLayerHeightAlpha[kDimIdCount];
std::string fnLayerBlockLight[kDimIdCount];
std::string fnLayerSkyLight[kDimIdCount];
std::string fnLayerSlimeChunks[kDimIdCount];
std::string fnLayerGrass[kDimIdCount];
std::string fnLayerShadedRelief[kDimIdCount];
std::string fnLayerRaw[kDimIdCount][MAX_BLOCK_HEIGHT + 1];
bool doDetailParseFlag;
int32_t doMovie;
int32_t doSlices;
int32_t doGrid;
int32_t doHtml;
int32_t doTiles;
int32_t doImageBiome;
int32_t doImageGrass;
int32_t doImageHeightCol;
int32_t doImageHeightColGrayscale;
int32_t doImageHeightColAlpha;
int32_t doImageLightBlock;
int32_t doImageLightSky;
int32_t doImageSlimeChunks;
int32_t doImageShadedRelief;
bool autoTileFlag;
bool noForceGeoJSONFlag;
bool shortRunFlag;
bool colorTestFlag;
bool verboseFlag;
bool quietFlag;
int32_t movieX, movieY, movieW, movieH;
bool doFindImages;
std::string dirFindImagesIn;
std::string dirFindImagesOut;
int32_t heightMode;
int32_t tileWidth;
int32_t tileHeight;
bool fpLogNeedCloseFlag;
FILE *fpLog;
// this is the BloomFilterPolicy bits, set to 0 to disable filter
int32_t leveldbFilter = 10;
// this is the block_size used by leveldb
int32_t leveldbBlockSize = 4096;
Control() {
init();
}
~Control() {
if ( fpLogNeedCloseFlag ) {
if ( fpLog != nullptr ) {
fclose(fpLog);
}
}
}
void init() {
dirLeveldb = "";
fnXml = "";
fnOutputBase = "";
fnLog = "";
fnGeoJSON = "";
fnHtml = "";
fnJs = "";
doDetailParseFlag = false;
doMovie = kDoOutputNone;
doSlices = kDoOutputNone;
doGrid = kDoOutputNone;
doHtml = 0;
doTiles = 0;
doImageBiome = kDoOutputNone;
doImageGrass = kDoOutputNone;
doImageHeightCol = kDoOutputNone;
doImageHeightColGrayscale = kDoOutputNone;
doImageHeightColAlpha = kDoOutputNone;
doImageLightBlock = kDoOutputNone;
doImageLightSky = kDoOutputNone;
doImageSlimeChunks = kDoOutputNone;
doImageShadedRelief = kDoOutputNone;
noForceGeoJSONFlag = false;
autoTileFlag = false;
// todobig - reasonable default? strike a balance between speed/# of files
tileWidth = 1024;
tileHeight = 1024;
doFindImages = false;
dirFindImagesIn = "";
dirFindImagesOut = "";
shortRunFlag = false;
colorTestFlag = false;
verboseFlag = false;
quietFlag = false;
movieX = movieY = movieW = movieH = 0;
fpLogNeedCloseFlag = false;
fpLog = stdout;
leveldbFilter = 10;
leveldbBlockSize = 4096;
// todo - cmdline option for this?
heightMode = kHeightModeTop;
for (int32_t did=0; did < kDimIdCount; did++) {
fnLayerTop[did] = "";
fnLayerBiome[did] = "";
fnLayerHeight[did] = "";
fnLayerHeightGrayscale[did] = "";
fnLayerHeightAlpha[did] = "";
fnLayerBlockLight[did] = "";
fnLayerSkyLight[did] = "";
fnLayerSlimeChunks[did] = "";
fnLayerShadedRelief[did] = "";
fnLayerGrass[did] = "";
for ( int32_t i=0; i <= MAX_BLOCK_HEIGHT; i++ ) {
fnLayerRaw[did][i] = "";
}
}
}
void setupOutput() {
if ( fnLog.compare("-") == 0 ) {
fpLog = stdout;
fpLogNeedCloseFlag = false;
}
else {
if ( fnLog.size() == 0 ) {
fnLog = fnOutputBase + ".log";
}
fpLog = fopen(fnLog.c_str(), "w");
if ( fpLog ) {
fpLogNeedCloseFlag = true;
} else {
fprintf(stderr,"ERROR: Failed to create output log file (%s error=%s (%d)). Reverting to stdout...\n", fnLog.c_str(), strerror(errno), errno);
fpLog = stdout;
fpLogNeedCloseFlag = false;
}
}
// setup logger
logger.setStdout(fpLog);
logger.setStderr(stderr);
if ( doHtml ) {
fnGeoJSON = fnOutputBase + ".geojson";
listGeoJSON.clear();
fnHtml = fnOutputBase + ".html";
fnJs = fnOutputBase + ".js";
}
}
};
Control control;
void makePalettes() {
// create red-green ramp; red to black and then black to green
makeHslRamp(palRedBlackGreen, 0, 61, 0.0,0.0, 0.9,0.9, 0.8,0.1);
makeHslRamp(palRedBlackGreen, 63, MAX_BLOCK_HEIGHT, 0.4,0.4, 0.9,0.9, 0.1,0.8);
// force 62 (sea level) to gray
palRedBlackGreen[62]=0x303030;
// fill 128..255 with purple (we should never see this color)
for (int32_t i=(MAX_BLOCK_HEIGHT + 1); i < 256; i++) {
palRedBlackGreen[i] = kColorDefault;
}
// convert palette
for (int32_t i=0; i < 256; i++) {
palRedBlackGreen[i] = htobe32(palRedBlackGreen[i]);
}
}
// todolib - these funcs should be in a class?
// calculate an offset into mcpe chunk data for block data
inline int32_t _calcOffsetBlock_LevelDB_v2(int32_t x, int32_t z, int32_t y) {
return (((x*16) + z)*(MAX_BLOCK_HEIGHT_127+1)) + y;
}
// calculate an offset into mcpe chunk data for column data
inline int32_t _calcOffsetColumn_LevelDB_v2(int32_t x, int32_t z) {
// NOTE! this is the OPPOSITE of block data (oy)
return (z*16) + x;
}
inline uint8_t getBlockId_LevelDB_v2(const char* p, int32_t x, int32_t z, int32_t y) {
return (p[_calcOffsetBlock_LevelDB_v2(x,z,y)] & 0xff);
}
uint8_t getBlockData_LevelDB_v2(const char* p, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v2(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
int32_t v = p[32768 + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// a block opacity value? (e.g. glass is 0xf, water is semi (0xc) and an opaque block is 0x0)
uint8_t getBlockSkyLight_LevelDB_v2(const char* p, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v2(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
int32_t v = p[32768 + 16384 + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// block light is light value from torches et al -- super cool looking as an image, but it looks like block light is probably stored in air blocks which are above top block
uint8_t getBlockBlockLight_LevelDB_v2(const char* p, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v2(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
int32_t v = p[32768 + 16384 + 16384 + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// height of top *solid* block? (e.g. a glass block will NOT be the top block here)
uint8_t getColData_Height_LevelDB_v2(const char *buf, int32_t x, int32_t z) {
int32_t off = _calcOffsetColumn_LevelDB_v2(x,z);
int8_t v = buf[32768 + 16384 + 16384 + 16384 + off];
return v;
}
// this is 4-bytes: lsb is biome, the high 3-bytes are RGB grass color
uint32_t getColData_GrassAndBiome_LevelDB_v2(const char *buf, int32_t x, int32_t z) {
int32_t off = _calcOffsetColumn_LevelDB_v2(x,z) * 4;
int32_t v;
memcpy(&v,&buf[32768 + 16384 + 16384 + 16384 + 256 + off],4);
return v;
}
// calculate an offset into mcpe chunk data for block data
inline int32_t _calcOffsetBlock_LevelDB_v3(int32_t x, int32_t z, int32_t y) {
return (((x*16) + z) * 16) + y;
}
inline uint8_t getBlockId_LevelDB_v3(const char* p, int32_t x, int32_t z, int32_t y) {
return (p[_calcOffsetBlock_LevelDB_v3(x,z,y)+1] & 0xff);
}
uint8_t getBlockData_LevelDB_v3(const char* p, size_t plen, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v3(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
// todonow - temp test to find bug
size_t tmp_offset = (16*16*16) + 1 + off2;
if ( tmp_offset >= plen ) {
if ( control.verboseFlag ) {
slogger.msg(kLogError,"getBlockData_LevelDB_v3 get data out of bounds! (%d >= %d) (%d %d %d)\n"
, (int32_t)tmp_offset, (int32_t)plen, x,z,y);
}
return 0;
}
int32_t v = p[tmp_offset];
//int32_t v = p[(16*16*16) + 1 + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// todozooz - this is getting crazy
uint8_t getBlockData_LevelDB_v3__fake_v7(const int16_t* p, size_t plen, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v3(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
// todonow - temp test to find bug
size_t tmp_offset = (16*16*16) + 1 + off2;
if ( tmp_offset >= plen ) {
if ( control.verboseFlag ) {
slogger.msg(kLogError,"getBlockData_LevelDB_v3 get data out of bounds! (%d >= %d) (%d %d %d)\n"
, (int32_t)tmp_offset, (int32_t)plen, x,z,y);
}
return 0;
}
int32_t v = p[tmp_offset];
//int32_t v = p[(16*16*16) + 1 + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// a block opacity value? (e.g. glass is 0xf, water is semi (0xc) and an opaque block is 0x0)
uint8_t getBlockSkyLight_LevelDB_v3(const char* p, size_t plen, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v3(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
// todonow - temp test to find bug
size_t tmp_offset = (16*16*16) + 1 + (16*16*8) + off2;
if ( tmp_offset >= plen ) {
if ( control.verboseFlag ) {
slogger.msg(kLogError,"getBlockSkyLight_LevelDB_v3 get data out of bounds! (%d >= %d) (%d %d %d)\n"
, (int32_t)tmp_offset, (int32_t)plen, x,z,y);
}
return 0;
}
int32_t v = p[tmp_offset];
// int32_t v = p[(16*16*16) + 1 + (16*16*8) + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// block light is light value from torches et al -- super cool looking as an image, but it looks like block light is probably stored in air blocks which are above top block
uint8_t getBlockBlockLight_LevelDB_v3(const char* p, size_t plen, int32_t x, int32_t z, int32_t y) {
int32_t off = _calcOffsetBlock_LevelDB_v3(x,z,y);
int32_t off2 = off / 2;
int32_t mod2 = off % 2;
// todonow - temp test to find bug
size_t tmp_offset = (16*16*16) + 1 + (16*16*8) + (16*16*8) + off2;
if ( tmp_offset >= plen ) {
if ( control.verboseFlag ) {
slogger.msg(kLogError,"getBlockBlockLight_LevelDB_v3 get data out of bounds! (%d >= %d) (%d %d %d)\n"
, (int32_t)tmp_offset, (int32_t)plen, x,z,y);
}
return 0;
}
int32_t v = p[tmp_offset];
//int32_t v = p[(16*16*16) + 1 + (16*16*8) + (16*16*8) + off2];
if ( mod2 == 0 ) {
return v & 0x0f;
} else {
return (v & 0xf0) >> 4;
}
}
// calculate an offset into mcpe chunk data for column data
inline int32_t _calcOffsetColumn_LevelDB_v3(int32_t x, int32_t z) {
// NOTE! this is the OPPOSITE of block data (oy)
return (z*16) + x;
}
// height appears to be stored as a 2-byte int
// height of top *solid* block? (e.g. a glass block will NOT be the top block here)
uint8_t getColData_Height_LevelDB_v3(const char *buf, int32_t x, int32_t z) {
int32_t off = _calcOffsetColumn_LevelDB_v3(x,z) * 2;
int8_t v = buf[off];
return v;
}
// this is 3-bytes: lsb is biome?, the high 2-bytes are RGB grass color?
uint32_t getColData_GrassAndBiome_LevelDB_v3(const char *buf, int32_t buflen, int32_t x, int32_t z) {
// format appears to be:
// 16x16 of 2-byte ints for HEIGHT OF TOP BLOCK
// 16x16 of 4-byte ints for BIOME and GRASS COLOR
// todo -- there was a bug in early 0.17 that really messed this data up
// tood -- grass colors are pretty weird (some are 01 01 01)
// as of, v0.17.01 we'll just roll with it and adjust as necessary
//int32_t off = _calcOffsetColumn_LevelDB_v3(x,z) * 4;
int32_t off = _calcOffsetColumn_LevelDB_v3(x,z);
int32_t v = 0;
// HACK! to work around MCPE bug (biome data is not complete in this record as of v0.17.01
if ( (512+off+1) <= buflen ) {
// memcpy(&v,&buf[512 + off],1);
v = buf[512 + off];
} else {
// nothing - this is deals with the bug in early 0.17
}
return v;
}
// todobig - this (XXX_v3_fullchunk) needs to be cleaner/simpler
// calculate an offset into mcpe chunk data for block data
inline int32_t _calcOffsetBlock_LevelDB_v3_fullchunk(int32_t x, int32_t z, int32_t y) {
return (((x*16) + z) * MAX_BLOCK_HEIGHT) + y;
}
inline uint8_t getData_LevelDB_v3_fullchunk(const char* p, int32_t x, int32_t z, int32_t y) {
return p[_calcOffsetBlock_LevelDB_v3_fullchunk(x,z,y)];
}
inline uint8_t getBlockId_LevelDB_v7(const char* p, int blocksPerWord, int bitsPerBlock, int32_t x, int32_t z, int32_t y) {
//int bitstart = ( (((x*16) + z) * 16) + y ) * bitsPerBlock;
// int bitstart = ( (((y*16) + x) * 16) + z ) * bitsPerBlock;
int blockPos = (((x*16) + z) * 16) + y;
// we find which 4-byte word we want
int wordStart = blockPos / blocksPerWord;
// we find the bit offset within that 4-byte word
int bitOffset = (blockPos % blocksPerWord) * bitsPerBlock;
int bitStart = wordStart * 4 * 8 + bitOffset;
return getBitsFromBytes(p, bitStart, bitsPerBlock);
}
// todomajor -- see tomcc gist re multiple storages in ONE cubick chunk in version == 8
inline int32_t setupBlockVars_v7(const char* cdata, int32_t& blocksPerWord, int32_t& bitsPerBlock, bool& paddingFlag, int32_t& offsetBlockInfoList, int32_t& extraOffset) {
int32_t v = -1;
if ( cdata[0] == 0x01 ) {
v = cdata[1];
extraOffset = 0;
} else {
// this is version 8+, cdata[1] contains the number of storage groups in this cubic chunk (can be more than 1)
v = cdata[2];
extraOffset = 1;
}
switch (v) {
case 0x02:
blocksPerWord = 32;
bitsPerBlock = 1;
offsetBlockInfoList = 512;
break;
case 0x04:
blocksPerWord = 16;
bitsPerBlock = 2;
offsetBlockInfoList = 1024;
break;
case 0x06:
blocksPerWord = 10;
bitsPerBlock = 3;
paddingFlag = true;
offsetBlockInfoList = 1640;
break;
case 0x08:
blocksPerWord = 8;
bitsPerBlock = 4;
offsetBlockInfoList = 2048;
break;
case 0x0a:
blocksPerWord = 6;
bitsPerBlock = 5;
paddingFlag = true;
offsetBlockInfoList = 2732;
break;
case 0x0c:
blocksPerWord = 5;
bitsPerBlock = 6;
paddingFlag = true;
offsetBlockInfoList = 3280;
break;
case 0x10:
blocksPerWord = 4;
bitsPerBlock = 8;
offsetBlockInfoList = (4096 / blocksPerWord) * 4;
break;
case 0x20:
blocksPerWord = 2;
bitsPerBlock = 16;
offsetBlockInfoList = (4096 / blocksPerWord) * 4;
break;
default:
slogger.msg(kLogError, "Unknown chunk cdata[1] value = %d\n",(int)v);
logger.msg(kLogError, "Unknown chunk cdata[1] value = %d\n",(int)v);
return -1;
}
// logger.msg(kLogInfo, "setupBlockVars_v7 v=%d bpw=%d bpb=%d pf=%d ob=%d\n", v, blocksPerWord, bitsPerBlock, (int)paddingFlag, offsetBlockInfoList);
return 0;
}
int32_t convertChunkV7toV3(const char* cdata, size_t cdata_size, int16_t* emuchunk) {
// we have a v7 chunk and we want to unpack it into a v3-like chunk
// determine location of chunk palette
// some details here: https://gist.github.com/Tomcc/a96af509e275b1af483b25c543cfbf37
int32_t blocksPerWord = -1;
int32_t bitsPerBlock = -1;
bool paddingFlag = false;
int32_t offsetBlockInfoList = -1;
int32_t extraOffset = -1;
memset(emuchunk,0,NUM_BYTES_CHUNK_V3*sizeof(int16_t));
if ( setupBlockVars_v7(cdata, blocksPerWord, bitsPerBlock, paddingFlag, offsetBlockInfoList, extraOffset) != 0 ) {
return -1;
}
// read chunk palette and associate old-school block id's
MyNbtTagList tagList;
int xoff = offsetBlockInfoList + 6 + extraOffset;
parseNbtQuiet(&cdata[xoff], cdata_size-xoff, cdata[offsetBlockInfoList + 3], tagList);
std::vector<int32_t> chunkBlockPalette_BlockId(tagList.size());
std::vector<int32_t> chunkBlockPalette_BlockData(tagList.size());
for ( size_t i=0; i < tagList.size(); i++ ) {
// check tagList
if ( tagList[i].second->get_type() == nbt::tag_type::Compound ) {
nbt::tag_compound tc = tagList[i].second->as<nbt::tag_compound>();
bool processedFlag = false;
if ( tc.has_key("name", nbt::tag_type::String) ) {
std::string bname = tc["name"].as<nbt::tag_string>().get();
if ( tc.has_key("val", nbt::tag_type::Short) ) {
int bdata = tc["val"].as<nbt::tag_short>().get();
int32_t blockId, blockData;
if ( getBlockByUname(bname, blockId, blockData) == 0 ) {
chunkBlockPalette_BlockId[i] = blockId;
// todonow - correct?
chunkBlockPalette_BlockData[i] = bdata;
} else {
logger.msg(kLogWarning,"Did not find block uname '%s' in XML file\n", bname.c_str());
// todonow - reasonable?
chunkBlockPalette_BlockId[i] = 0;
chunkBlockPalette_BlockData[i] = 0;
}
processedFlag = true;
}
}
if ( ! processedFlag ) {
slogger.msg(kLogError,"(Safe) Did not find 'name' and/or 'val' tags in a chunk palette! (i=%d) (len=%d)\n"
, (int)i, (int)tagList.size() );
//todozooz - dump tc to screen log
}
} else {
logger.msg(kLogWarning,"Unexpected NBT format in _do_chunk_v7\n");
}
}
//todozooz -- new 16-bit block-id's (instead of 8-bit) are a BIG issue - this needs attention here
// iterate over chunk space
uint8_t paletteBlockId, blockData;
int32_t blockId;
for (int32_t cy=0; cy < 16; cy++) {
for ( int32_t cx=0; cx < 16; cx++) {
for ( int32_t cz=0; cz < 16; cz++ ) {
paletteBlockId = getBlockId_LevelDB_v7(&cdata[2 + extraOffset], blocksPerWord, bitsPerBlock, cx,cz,cy);
// look up blockId
//todonow error checking
if ( paletteBlockId < chunkBlockPalette_BlockId.size() ) {
blockId = chunkBlockPalette_BlockId[paletteBlockId];
blockData = chunkBlockPalette_BlockData[paletteBlockId];
} else {
blockId = 0;
blockData = 0;
logger.msg(kLogWarning,"Found chunk palette id out of range %d (size=%d)\n", paletteBlockId, (int)chunkBlockPalette_BlockId.size());
}
int32_t bdoff = _calcOffsetBlock_LevelDB_v3(cx,cz,cy);
emuchunk[bdoff+1] = blockId;
// put block data
int32_t bdoff2 = bdoff / 2;
int32_t bdmod2 = bdoff % 2;
// todonow - temp test to find bug
size_t tmp_offset = (16*16*16) + 1 + bdoff2;
if ( bdmod2 == 0 ) {
emuchunk[tmp_offset] |= (blockData & 0x0f);
} else {
emuchunk[tmp_offset] |= (blockData & 0x0f) << 4;
}
}
}
}
return 0;
}
// todolib - move to util?
int32_t myParseInt32(const char* p, int32_t startByte) {
int32_t ret;
memcpy(&ret, &p[startByte], 4);
return ret;
}
int8_t myParseInt8(const char* p, int32_t startByte) {
return (p[startByte] & 0xff);
}
bool has_key(const ItemInfoList &m, int32_t k) {
return m.find(k) != m.end();
}
bool has_key(const EntityInfoList &m, int32_t k) {
return m.find(k) != m.end();
}
bool has_key(const BiomeInfoList &m, int32_t k) {
return m.find(k) != m.end();
}
bool has_key(const EnchantmentInfoList &m, int32_t k) {
return m.find(k) != m.end();
}
bool has_key(const IntIntMap &m, int32_t k) {
return m.find(k) != m.end();
}
bool has_key(const StringIntMap &m, const std::string& k) {
return m.find(k) != m.end();
}
int32_t findEntityByUname(const EntityInfoList &m, std::string& un) {
// convert search key to lower case
std::string uname = un;
std::transform(uname.begin(), uname.end(), uname.begin(), ::tolower);
for (const auto& it : m) {
for ( const auto& u : it.second->unameList ) {
if ( u == uname ) {
return it.first;
}
}
}
return -1;
}
int32_t findIdByItemName(std::string& un) {
std::string uname = un;
std::transform(uname.begin(), uname.end(), uname.begin(), ::tolower);
for (const auto& it : itemInfoList) {
for ( const auto& u : it.second->unameList ) {
if ( u == uname ) {
return it.first;
}
}
}
return -1;
}
int32_t findIdByBlockName(std::string& un) {
std::string uname = un;
std::transform(uname.begin(), uname.end(), uname.begin(), ::tolower);
for (const auto& it : blockInfoList ) {
for ( const auto& u : it.unameList ) {
if ( u == uname ) {
return it.id;
}
}
}
return -1;
}