From 50b26a3f1788085682692fccbf0b0d501383030b Mon Sep 17 00:00:00 2001 From: EltamGit Date: Wed, 8 Jul 2015 15:56:05 +0200 Subject: [PATCH] Speedup for Mob Spawn detection 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 --- blockidentifier.cpp | 36 +++++++++++++++++++++++++++--------- blockidentifier.h | 19 ++++++++++++++++++- mapview.cpp | 8 ++++---- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/blockidentifier.cpp b/blockidentifier.cpp index a92547a3..251de926 100755 --- a/blockidentifier.cpp +++ b/blockidentifier.cpp @@ -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; } @@ -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; } @@ -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() { @@ -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")) @@ -320,6 +337,7 @@ void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent, int pack for (int j=0;j(variants->at(j)),block,pack); } + blocks[id].append(block); packs[pack].append(block); } diff --git a/blockidentifier.h b/blockidentifier.h index bebdb0d5..418cbd26 100755 --- a/blockidentifier.h +++ b/blockidentifier.h @@ -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; @@ -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 diff --git a/mapview.cpp b/mapview.cpp index 441175ef..a54980e6 100755 --- a/mapview.cpp +++ b/mapview.cpp @@ -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) @@ -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 ) @@ -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 ) @@ -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;