Skip to content

Commit

Permalink
Initial 1.13 update
Browse files Browse the repository at this point in the history
- Addition of New NBT Tag "Tag_Long_Array"
- Updates to chunk.cpp to parse new chunk format
- Begin flattening of vanilla_ids.json
- Update mapview.cpp to remove "data" tag
- Update blockidentifier to search based upon block name
  • Loading branch information
nnooney committed Jul 18, 2018
1 parent ececca1 commit e495e51
Show file tree
Hide file tree
Showing 8 changed files with 1,852 additions and 1,852 deletions.
70 changes: 37 additions & 33 deletions blockidentifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,39 @@ BlockIdentifier::~BlockIdentifier() {
}

// this routine is ridiculously slow
BlockInfo &BlockIdentifier::getBlock(int id, int data) {
BlockInfo &BlockIdentifier::getBlock(QString name, int data) {
// first apply the mask
if (blocks.contains(id))
data &= blocks[id].first()->mask;

quint32 bid = id | (data << 12);
// first check the cache
if (cache[bid] != NULL)
return *cache[bid];

// now find the variant
if (blocks.contains(bid)) {
QList<BlockInfo*> &list = blocks[bid];
// run backwards for priority sorting
for (int i = list.length() - 1; i >= 0; i--) {
if (list[i]->enabled) {
cache[bid] = list[i];
return *list[i];
}
}
}
// no enabled variant found
if (blocks.contains(id)) {
QList<BlockInfo*> &list = blocks[id];
for (int i = list.length() - 1; i >= 0; i--) {
if (list[i]->enabled) {
cache[bid] = list[i];
return *list[i];
}
}
if (blocks.contains(name)) {
return *(blocks[name].first());
}
// data &= blocks[name].first()->mask;
//
// quint32 bid = id | (data << 12);
// // first check the cache
// if (cache[bid] != NULL)
// return *cache[bid];
//
// // now find the variant
// if (blocks.contains(bid)) {
// QList<BlockInfo*> &list = blocks[bid];
// // run backwards for priority sorting
// for (int i = list.length() - 1; i >= 0; i--) {
// if (list[i]->enabled) {
// cache[bid] = list[i];
// return *list[i];
// }
// }
// }
// // no enabled variant found
// if (blocks.contains(id)) {
// QList<BlockInfo*> &list = blocks[id];
// for (int i = list.length() - 1; i >= 0; i--) {
// if (list[i]->enabled) {
// cache[bid] = list[i];
// return *list[i];
// }
// }
// }
// no blocks at all found.. dammit
return unknownBlock;
}
Expand Down Expand Up @@ -165,6 +167,7 @@ void BlockIdentifier::clearCache() {
void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent,
int pack) {
int id;
QString name;
if (parent == NULL) {
id = b->at("id")->asNumber();
} else {
Expand All @@ -176,11 +179,12 @@ void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent,
block->id = id;

if (b->has("name"))
block->setName(b->at("name")->asString());
name = b->at("name")->asString();
else if (parent != NULL)
block->setName(parent->getName());
name = parent->getName();
else
block->setName("Unknown");
name = "Unknown";
block->setName(name);
block->enabled = true;

if (b->has("transparent")) {
Expand Down Expand Up @@ -275,6 +279,6 @@ void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent,
parseDefinition(dynamic_cast<JSONObject *>(variants->at(j)), block, pack);
}

blocks[id].append(block);
blocks[name].append(block);
packs[pack].append(block);
}
4 changes: 2 additions & 2 deletions blockidentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class BlockIdentifier {
int addDefinitions(JSONArray *, int pack = -1);
void enableDefinitions(int id);
void disableDefinitions(int id);
BlockInfo &getBlock(int id, int data);
BlockInfo &getBlock(QString name, int data);
private:
void clearCache();
void parseDefinition(JSONObject *block, BlockInfo *parent, int pack);
QMap<quint32, QList<BlockInfo *>> blocks;
QMap<QString, QList<BlockInfo *>> blocks;
QList<QList<BlockInfo*> > packs;
BlockInfo *cache[65536];
};
Expand Down
73 changes: 49 additions & 24 deletions chunk.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/** Copyright (c) 2013, Sean Kasun */

#include "./chunk.h"
#include <algorithm>

quint16 getBits(const unsigned char *data, int pos, int n) {
quint16 result = 0;
int arrIndex = pos/8;
int bitIndex = pos%8;
quint32 loc =
data[arrIndex] << 24 |
data[arrIndex+1] << 16 |
data[arrIndex+2] << 8 |
data[arrIndex+3];

return ((loc >> (32-bitIndex-n)) & ((1 << n) -1));
}



Chunk::Chunk() {
loaded = false;
Expand All @@ -19,24 +35,32 @@ void Chunk::load(const NBT &nbt) {
chunkZ = level->at("zPos")->toInt();

auto biomes = level->at("Biomes");
memcpy(this->biomes, biomes->toByteArray(), biomes->length());
memcpy(this->biomes, biomes->toIntArray(), 4*biomes->length());
auto sections = level->at("Sections");
int numSections = sections->length();
for (int i = 0; i < numSections; i++) {
auto section = sections->at(i);
auto cs = new ChunkSection();
auto raw = section->at("Blocks")->toByteArray();
for (int i = 0; i < 4096; i++)
cs->blocks[i] = raw[i];
if (section->has("Add")) {
raw = section->at("Add")->toByteArray();
for (int i = 0; i < 2048; i++) {
cs->blocks[i * 2] |= (raw[i] & 0xf) << 8;
cs->blocks[i * 2 + 1] |= (raw[i] & 0xf0) << 4;
}
auto rawPalette = section->at("Palette");
cs->paletteLength = rawPalette->length();
cs->palette = new BlockData[cs->paletteLength];
for (int j = 0; j < rawPalette->length(); j++) {
cs->palette[j].name = rawPalette->at(j)->at("Name")->toString();
if (rawPalette->at(j)->has("Properties"))
cs->palette[j].properties = rawPalette->at(j)->at("Properties")->getData().toMap();
}
auto raw = section->at("BlockStates")->toLongArray();
int blockStatesLength = section->at("BlockStates")->length();
unsigned char *byteData = new unsigned char[8*blockStatesLength];
memcpy(byteData, raw, 8*blockStatesLength);
std::reverse(byteData, byteData+(8*blockStatesLength));
int bitSize = (blockStatesLength)*64/4096;
for (int i = 0; i < 4096; i++) {
cs->blocks[4095-i] = getBits(byteData, i*bitSize, bitSize);
}
memcpy(cs->data, section->at("Data")->toByteArray(), 2048);
memcpy(cs->light, section->at("BlockLight")->toByteArray(), 2048);
free(byteData);
memcpy(cs->skyLight, section->at("SkyLight")->toByteArray(), 2048);
memcpy(cs->blockLight, section->at("BlockLight")->toByteArray(), 2048);
int idx = section->at("Y")->toInt();
this->sections[idx] = cs;
}
Expand Down Expand Up @@ -67,53 +91,54 @@ Chunk::~Chunk() {
if (loaded) {
for (int i = 0; i < 16; i++)
if (sections[i]) {
delete[] sections[i]->palette;
delete sections[i];
sections[i] = NULL;
}
}
}


quint16 ChunkSection::getBlock(int x, int y, int z) {
QString ChunkSection::getBlock(int x, int y, int z) {
int xoffset = x;
int yoffset = (y & 0x0f) << 8;
int zoffset = z << 4;
return blocks[xoffset + yoffset + zoffset];
return palette[blocks[xoffset + yoffset + zoffset]].name;
}

quint16 ChunkSection::getBlock(int offset, int y) {
QString ChunkSection::getBlock(int offset, int y) {
int yoffset = (y & 0x0f) << 8;
return blocks[offset + yoffset];
return palette[blocks[offset + yoffset]].name;
}

quint8 ChunkSection::getData(int x, int y, int z) {
quint8 ChunkSection::getSkyLight(int x, int y, int z) {
int xoffset = x;
int yoffset = (y & 0x0f) << 8;
int zoffset = z << 4;
int value = data[(xoffset + yoffset + zoffset) / 2];
int value = skyLight[(xoffset + yoffset + zoffset) / 2];
if (x & 1) value >>= 4;
return value & 0x0f;
}

quint8 ChunkSection::getData(int offset, int y) {
quint8 ChunkSection::getSkyLight(int offset, int y) {
int yoffset = (y & 0x0f) << 8;
int value = data[(offset + yoffset) / 2];
int value = skyLight[(offset + yoffset) / 2];
if (offset & 1) value >>= 4;
return value & 0x0f;
}

quint8 ChunkSection::getLight(int x, int y, int z) {
quint8 ChunkSection::getBlockLight(int x, int y, int z) {
int xoffset = x;
int yoffset = (y & 0x0f) << 8;
int zoffset = z << 4;
int value = light[(xoffset + yoffset + zoffset) / 2];
int value = blockLight[(xoffset + yoffset + zoffset) / 2];
if (x & 1) value >>= 4;
return value & 0x0f;
}

quint8 ChunkSection::getLight(int offset, int y) {
quint8 ChunkSection::getBlockLight(int offset, int y) {
int yoffset = (y & 0x0f) << 8;
int value = light[(offset + yoffset) / 2];
int value = blockLight[(offset + yoffset) / 2];
if (offset & 1) value >>= 4;
return value & 0x0f;
}
26 changes: 17 additions & 9 deletions chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@
#include "./entity.h"
class BlockIdentifier;

class BlockData {
public:
QString name;
QMap<QString, QVariant> properties;
};

class ChunkSection {
public:
quint16 getBlock(int x, int y, int z);
quint16 getBlock(int offset, int y);
quint8 getData(int x, int y, int z);
quint8 getData(int offset, int y);
quint8 getLight(int x, int y, int z);
quint8 getLight(int offset, int y);
QString getBlock(int x, int y, int z);
QString getBlock(int offset, int y);
quint8 getSkyLight(int x, int y, int z);
quint8 getSkyLight(int offset, int y);
quint8 getBlockLight(int x, int y, int z);
quint8 getBlockLight(int offset, int y);

BlockData *palette;
int paletteLength;
quint16 blocks[4096];
quint8 data[2048];
quint8 light[2048];
quint8 skyLight[2048];
quint8 blockLight[2048];
};

class Chunk {
Expand All @@ -31,7 +39,7 @@ class Chunk {
protected:
typedef QMap<QString, QSharedPointer<OverlayItem>> EntityMap;

quint8 biomes[256];
quint32 biomes[256];
int highest;
ChunkSection *sections[16];
int renderedAt;
Expand Down
Loading

0 comments on commit e495e51

Please sign in to comment.