Room::find optimizations.

This commit is contained in:
Kirill Kirilenko 2021-07-01 20:10:31 +03:00
parent 76a242d822
commit 92111aadac
2 changed files with 37 additions and 34 deletions

View file

@ -61,19 +61,16 @@ public:
template <class T> template <class T>
std::vector<std::unique_ptr<RoomObject>> find(std::function<bool(const T&)> predicate = {}) const std::vector<std::unique_ptr<RoomObject>> find(std::function<bool(const T&)> predicate = {}) const
{ {
std::vector<std::unique_ptr<RoomObject>> objects = find(T::findConstant);
if (predicate) if (predicate)
objects.erase(std::remove_if(objects.begin(), objects.end(), {
[predicate](const std::unique_ptr<RoomObject>& object) auto p = [&](const JS::Value& value)
{ {
if (auto* ptr = dynamic_cast<T*>(object.get())) return predicate(T(value));
return !predicate(*ptr); };
else return find(T::findConstant, p);
return true; }
}), else
objects.end()); return find(T::findConstant);
return objects;
} }
int findExitTo(const std::string& room); int findExitTo(const std::string& room);

View file

@ -63,36 +63,42 @@ std::unique_ptr<RoomObject> createRoomObject(JS::Value object)
}; };
// FIXME: sort by rarity // FIXME: sort by rarity
if (is("StructureRoad")) if (is("Structure"))
return std::make_unique<StructureRoad>(std::move(object)); {
else if (is("StructureWall")) std::string type = object["structureType"].as<std::string>();
return std::make_unique<StructureWall>(std::move(object)); if (type == Screeps::STRUCTURE_ROAD)
else if (is("StructureRampart")) return std::make_unique<StructureRoad>(std::move(object));
return std::make_unique<StructureRampart>(std::move(object)); else if (type == Screeps::STRUCTURE_WALL)
else if (is("StructureExtension")) return std::make_unique<StructureWall>(std::move(object));
return std::make_unique<StructureExtension>(std::move(object)); else if (type == Screeps::STRUCTURE_RAMPART)
return std::make_unique<StructureRampart>(std::move(object));
else if (type == Screeps::STRUCTURE_EXTENSION)
return std::make_unique<StructureExtension>(std::move(object));
else if (type == Screeps::STRUCTURE_CONTAINER)
return std::make_unique<StructureContainer>(std::move(object));
else if (type == Screeps::STRUCTURE_TOWER)
return std::make_unique<StructureTower>(std::move(object));
else if (type == Screeps::STRUCTURE_LINK)
return std::make_unique<StructureLink>(std::move(object));
else if (type == Screeps::STRUCTURE_SPAWN)
return std::make_unique<StructureSpawn>(std::move(object));
else if (type == Screeps::STRUCTURE_CONTROLLER)
return std::make_unique<StructureController>(std::move(object));
else if (type == Screeps::STRUCTURE_STORAGE)
return std::make_unique<StructureStorage>(std::move(object));
else if (type == Screeps::STRUCTURE_EXTRACTOR)
return std::make_unique<StructureExtractor>(std::move(object));
else
return nullptr;
}
else if (is("Creep")) else if (is("Creep"))
return std::make_unique<Creep>(std::move(object)); return std::make_unique<Creep>(std::move(object));
else if (is("StructureContainer"))
return std::make_unique<StructureContainer>(std::move(object));
else if (is("Source")) else if (is("Source"))
return std::make_unique<Source>(std::move(object)); return std::make_unique<Source>(std::move(object));
else if (is("ConstructionSite")) else if (is("ConstructionSite"))
return std::make_unique<ConstructionSite>(std::move(object)); return std::make_unique<ConstructionSite>(std::move(object));
else if (is("StructureTower"))
return std::make_unique<StructureTower>(std::move(object));
else if (is("StructureLink"))
return std::make_unique<StructureLink>(std::move(object));
else if (is("StructureSpawn"))
return std::make_unique<StructureSpawn>(std::move(object));
else if (is("Ruin")) else if (is("Ruin"))
return std::make_unique<Ruin>(std::move(object)); return std::make_unique<Ruin>(std::move(object));
else if (is("StructureController"))
return std::make_unique<StructureController>(std::move(object));
else if (is("StructureStorage"))
return std::make_unique<StructureStorage>(std::move(object));
else if (is("StructureExtractor"))
return std::make_unique<StructureExtractor>(std::move(object));
else if (is("Deposit")) else if (is("Deposit"))
return std::make_unique<Deposit>(std::move(object)); return std::make_unique<Deposit>(std::move(object));
else if (is("Flag")) else if (is("Flag"))