Skip to content

Commit

Permalink
Speedup for Mob Spawn detection
Browse files Browse the repository at this point in the history
this will increase render speed by about 3x (when Mob Spawn detection is
switched on)
+ the overhead for mob spawn detection seems to be removed completely!
+ previously the string search was the performance killer
  • Loading branch information
EtlamGit committed Jul 8, 2015
1 parent 557dbb1 commit 50b26a3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
36 changes: 27 additions & 9 deletions blockidentifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ bool BlockInfo::isLiquid()
bool BlockInfo::doesBlockHaveSolidTopSurface(int data)
{
if (this->isOpaque() && this->renderAsNormalBlock()) return true;
if (this->name.contains("Stairs") && ((data&4)==4)) return true;
if (this->name.contains("Slab") && !this->name.contains("Double") && !this->name.contains("Full") &&
((data&8)==8)) return true;
if (this->name.contains("Hopper")) return true;
if (this->name.contains("Snow") && ((data&7)==7)) return true;
if (this->stairs && ((data&4)==4)) return true;
if (this->halfslab && ((data&8)==8)) return true;
if (this->hopper) return true;
if (this->snow && ((data&7)==7)) return true;
return false;
}

Expand All @@ -78,6 +77,24 @@ bool BlockInfo::canProvidePower()
return this->providepower;
}

void BlockInfo::setName( const QString & newname )
{
name = newname;
bedrock = this->name.contains("Bedrock");
hopper = this->name.contains("Hopper");
stairs = this->name.contains("Stairs");
halfslab = this->name.contains("Slab") && !this->name.contains("Double") && !this->name.contains("Full");
snow = this->name.contains("Snow");
}

const QString & BlockInfo::getName() { return name; }


bool BlockInfo::isBedrock() { return bedrock; }
bool BlockInfo::isHopper() { return hopper; }
bool BlockInfo::isStairs() { return stairs; }
bool BlockInfo::isHalfSlab() { return halfslab; }
bool BlockInfo::isSnow() { return snow; }



Expand All @@ -89,7 +106,7 @@ BlockIdentifier::BlockIdentifier()
for (int i=0;i<16;i++)
unknownBlock.colors[i]=0xff00ff;
unknownBlock.alpha=1.0;
unknownBlock.name="Unknown";
unknownBlock.setName("Unknown");
}
BlockIdentifier::~BlockIdentifier()
{
Expand Down Expand Up @@ -207,11 +224,11 @@ void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent, int pack
block->id=id;

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

if (b->has("transparent"))
Expand Down Expand Up @@ -320,6 +337,7 @@ void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent, int pack
for (int j=0;j<vlen;j++)
parseDefinition(dynamic_cast<JSONObject *>(variants->at(j)),block,pack);
}

blocks[id].append(block);
packs[pack].append(block);
}
19 changes: 18 additions & 1 deletion blockidentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ class BlockInfo
bool isBlockNormalCube();
bool renderAsNormalBlock();
bool canProvidePower();

// special blocks used during mob spawning detection
bool isBedrock();
bool isHopper();
bool isStairs();
bool isHalfSlab();
bool isSnow();

void setName( const QString & newname );
const QString & getName();

int id;
QString name;
double alpha;
quint8 mask;
bool enabled;
Expand All @@ -66,6 +75,14 @@ class BlockInfo
bool providepower;
bool spawninside;
quint32 colors[16];
private:
QString name;
// cache special blocks used during mob spawning detection
bool bedrock;
bool hopper;
bool stairs;
bool halfslab;
bool snow;
};

class BlockIdentifier
Expand Down
8 changes: 4 additions & 4 deletions mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ void MapView::renderChunk(Chunk *chunk)
{
// get block info from 1 and 2 above and 1 below
quint16 blid1(0), blid2(0), blidB(0); // default to air
int data1(0), data2(0), dataB(0); // default variant
int data1(0), data2(0), dataB(0); // default variant
ChunkSection *section2=NULL;
ChunkSection *sectionB=NULL;
if (y<254)
Expand Down Expand Up @@ -541,7 +541,7 @@ void MapView::renderChunk(Chunk *chunk)

// spawn check #1: on top of solid block
if ( block0.doesBlockHaveSolidTopSurface(data) &&
!block0.name.contains("Bedrock") &&
!block0.isBedrock() &&
(light1<8) &&
!block1.isBlockNormalCube() && block1.spawninside && !block1.isLiquid() &&
!block2.isBlockNormalCube() && block2.spawninside )
Expand All @@ -552,7 +552,7 @@ void MapView::renderChunk(Chunk *chunk)
}
// spawn check #2: current block is transparent, but mob can spawn through (e.g. snow)
if ( blockB.doesBlockHaveSolidTopSurface(dataB) &&
!blockB.name.contains("Bedrock") &&
!blockB.isBedrock() &&
(light0<8) &&
!block0.isBlockNormalCube() && block0.spawninside && !block0.isLiquid() &&
!block1.isBlockNormalCube() && block1.spawninside )
Expand Down Expand Up @@ -626,7 +626,7 @@ void MapView::getToolTip(int x, int z)
BlockInfo &block=blocks->getBlock(section->blocks[offset+yoffset],data&0xf);
if (block.alpha==0.0) continue;
//found block
name=block.name;
name=block.getName();
id=section->blocks[offset+yoffset];
bd=data&0xf;
break;
Expand Down

0 comments on commit 50b26a3

Please sign in to comment.