Skip to content

Commit

Permalink
Merge pull request mrkite#181 from EtlamGit/speedup-biome-lockup
Browse files Browse the repository at this point in the history
Speedup biome lockup
  • Loading branch information
mrkite authored Aug 31, 2019
2 parents a51f0f4 + f13e9f7 commit 830f0ff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
29 changes: 20 additions & 9 deletions biomeidentifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,26 @@ BiomeIdentifier& BiomeIdentifier::Instance() {

const BiomeInfo &BiomeIdentifier::getBiome(int biome) const {
auto itr = biomes.find(biome);
if (itr == biomes.end())
{
if (itr == biomes.end()) {
return unknownBiome;
} else {
return *(*itr);
}
const QList<BiomeInfo*> &list = *itr;
// search backwards for priority sorting to work
for (int i = list.length() - 1; i >= 0; i--)
if (list[i]->enabled)
return *list[i];
return unknownBiome;
}

void BiomeIdentifier::enableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = true;
updateBiomeDefinition();
}
void BiomeIdentifier::disableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = false;
updateBiomeDefinition();
}

int BiomeIdentifier::addDefinitions(JSONArray *defs, int pack) {
Expand Down Expand Up @@ -295,9 +292,23 @@ int BiomeIdentifier::addDefinitions(JSONArray *defs, int pack) {
255*biome->alpha );
}

biomes[id].append(biome);
packs[pack].append(biome);
}

updateBiomeDefinition();
return pack;
}

void BiomeIdentifier::updateBiomeDefinition()
{
// start from scratch
biomes.clear();

for (int pack = 0; pack < packs.length(); pack++)
for (int i = 0; i < packs[pack].length(); i++) {
BiomeInfo *bi = packs[pack][i];
if (bi->enabled) {
biomes[bi->id] = bi;
}
}
}
5 changes: 3 additions & 2 deletions biomeidentifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class BiomeIdentifier {
int addDefinitions(JSONArray *, int pack = -1);
void enableDefinitions(int id);
void disableDefinitions(int id);
void updateBiomeDefinition();
const BiomeInfo &getBiome(int id) const;

private:
Expand All @@ -59,8 +60,8 @@ class BiomeIdentifier {
BiomeIdentifier(const BiomeIdentifier &);
BiomeIdentifier &operator=(const BiomeIdentifier &);

QHash<int, QList<BiomeInfo*>> biomes;
QList<QList<BiomeInfo*> > packs;
QHash<int, BiomeInfo*> biomes; // consolidated Biome mapping
QList<QList<BiomeInfo*> > packs; // raw data of all available packs
};

#endif // BIOMEIDENTIFIER_H_

0 comments on commit 830f0ff

Please sign in to comment.