1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00

Fix terrain performance

This commit is contained in:
nordsoft 2022-09-07 04:20:02 +04:00
parent a4686c81ee
commit 289ae8a1f8
2 changed files with 21 additions and 13 deletions

View File

@ -154,6 +154,11 @@ Terrain::Manager::Manager()
} }
terrainInfo[terr.first] = info; terrainInfo[terr.first] = info;
if(!terrainId.count(terr.first))
{
terrainId[terr.first] = terrainVault.size();
terrainVault.push_back(terr.first);
}
} }
} }
} }
@ -164,12 +169,18 @@ Terrain::Manager & Terrain::Manager::get()
return manager; return manager;
} }
std::vector<Terrain> Terrain::Manager::terrains() const std::vector<Terrain> & Terrain::Manager::terrains()
{ {
std::set<Terrain> _terrains; //have to use std::set to have ordered container. Othervise de-sync is possible return Terrain::Manager::get().terrainVault;
for(const auto & info : Terrain::Manager::get().terrainInfo) }
_terrains.insert(info.first);
return std::vector<Terrain>(_terrains.begin(), _terrains.end()); int Terrain::Manager::id(const Terrain & terrain)
{
if(terrain.name == "ANY") return -3;
if(terrain.name == "WRONG") return -2;
if(terrain.name == "BORDER") return -1;
return Terrain::Manager::get().terrainId.at(terrain);
} }
const Terrain::Info & Terrain::Manager::getInfo(const Terrain & terrain) const Terrain::Info & Terrain::Manager::getInfo(const Terrain & terrain)
@ -219,13 +230,7 @@ bool operator<(const Terrain & l, const Terrain & r)
int Terrain::id() const int Terrain::id() const
{ {
if(name == "ANY") return -3; return Terrain::Manager::id(*this);
if(name == "WRONG") return -2;
if(name == "BORDER") return -1;
auto _terrains = Terrain::Manager::terrains();
auto iter = std::find(_terrains.begin(), _terrains.end(), *this);
return iter - _terrains.begin();
} }
bool Terrain::isLand() const bool Terrain::isLand() const

View File

@ -48,14 +48,17 @@ public:
class DLL_LINKAGE Manager class DLL_LINKAGE Manager
{ {
public: public:
static std::vector<Terrain> terrains(); static const std::vector<Terrain> & terrains();
static const Info & getInfo(const Terrain &); static const Info & getInfo(const Terrain &);
static int id(const Terrain &);
private: private:
static Manager & get(); static Manager & get();
Manager(); Manager();
std::unordered_map<std::string, Info> terrainInfo; std::unordered_map<std::string, Info> terrainInfo;
std::vector<Terrain> terrainVault;
std::map<Terrain, int> terrainId;
}; };
/*enum EETerrainType /*enum EETerrainType