mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Add pre-computed vectors of owned objects for faster access
This commit is contained in:
		| @@ -112,24 +112,24 @@ std::vector<T> PlayerState::getObjectsOfType() const | ||||
| 	return result; | ||||
| } | ||||
|  | ||||
| std::vector<const CGHeroInstance *> PlayerState::getHeroes() const | ||||
| const std::vector<const CGHeroInstance *> & PlayerState::getHeroes() const | ||||
| { | ||||
| 	return getObjectsOfType<const CGHeroInstance *>(); | ||||
| 	return constOwnedHeroes; | ||||
| } | ||||
|  | ||||
| std::vector<const CGTownInstance *> PlayerState::getTowns() const | ||||
| const std::vector<const CGTownInstance *> & PlayerState::getTowns() const | ||||
| { | ||||
| 	return getObjectsOfType<const CGTownInstance *>(); | ||||
| 	return constOwnedTowns; | ||||
| } | ||||
|  | ||||
| std::vector<CGHeroInstance *> PlayerState::getHeroes() | ||||
| const std::vector<CGHeroInstance *> & PlayerState::getHeroes() | ||||
| { | ||||
| 	return getObjectsOfType<CGHeroInstance *>(); | ||||
| 	return ownedHeroes; | ||||
| } | ||||
|  | ||||
| std::vector<CGTownInstance *> PlayerState::getTowns() | ||||
| const std::vector<CGTownInstance *> & PlayerState::getTowns() | ||||
| { | ||||
| 	return getObjectsOfType<CGTownInstance *>(); | ||||
| 	return ownedTowns; | ||||
| } | ||||
|  | ||||
| std::vector<const CGObjectInstance *> PlayerState::getOwnedObjects() const | ||||
| @@ -141,11 +141,51 @@ void PlayerState::addOwnedObject(CGObjectInstance * object) | ||||
| { | ||||
| 	assert(object->asOwnable() != nullptr); | ||||
| 	ownedObjects.push_back(object); | ||||
|  | ||||
| 	auto * town = dynamic_cast<CGTownInstance*>(object); | ||||
| 	auto * hero = dynamic_cast<CGHeroInstance*>(object); | ||||
|  | ||||
| 	if (town) | ||||
| 	{ | ||||
| 		ownedTowns.push_back(town); | ||||
| 		constOwnedTowns.push_back(town); | ||||
| 	} | ||||
|  | ||||
| 	if (hero) | ||||
| 	{ | ||||
| 		ownedHeroes.push_back(hero); | ||||
| 		constOwnedHeroes.push_back(hero); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void PlayerState::postDeserialize() | ||||
| { | ||||
| 	for (const auto& object : ownedObjects) | ||||
| 	{ | ||||
| 		auto* town = dynamic_cast<CGTownInstance*>(object); | ||||
| 		auto* hero = dynamic_cast<CGHeroInstance*>(object); | ||||
|  | ||||
| 		if (town) | ||||
| 		{ | ||||
| 			ownedTowns.push_back(town); | ||||
| 			constOwnedTowns.push_back(town); | ||||
| 		} | ||||
|  | ||||
| 		if (hero) | ||||
| 		{ | ||||
| 			ownedHeroes.push_back(hero); | ||||
| 			constOwnedHeroes.push_back(hero); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void PlayerState::removeOwnedObject(CGObjectInstance * object) | ||||
| { | ||||
| 	vstd::erase(ownedObjects, object); | ||||
| 	vstd::erase(ownedTowns, object); | ||||
| 	vstd::erase(constOwnedTowns, object); | ||||
| 	vstd::erase(ownedHeroes, object); | ||||
| 	vstd::erase(constOwnedHeroes, object); | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -49,6 +49,11 @@ class DLL_LINKAGE PlayerState : public CBonusSystemNode, public Player | ||||
|  | ||||
| 	std::vector<CGObjectInstance*> ownedObjects; | ||||
|  | ||||
| 	std::vector<const CGTownInstance*> constOwnedTowns; //not serialized | ||||
| 	std::vector<const CGHeroInstance*> constOwnedHeroes; //not serialized | ||||
| 	std::vector<CGTownInstance*> ownedTowns; //not serialized | ||||
| 	std::vector<CGHeroInstance*> ownedHeroes; //not serialized | ||||
|  | ||||
| 	template<typename T> | ||||
| 	std::vector<T> getObjectsOfType() const; | ||||
|  | ||||
| @@ -92,15 +97,16 @@ public: | ||||
| 	std::string getNameTextID() const override; | ||||
| 	void registerIcons(const IconRegistar & cb) const override; | ||||
|  | ||||
| 	std::vector<const CGHeroInstance* > getHeroes() const; | ||||
| 	std::vector<const CGTownInstance* > getTowns() const; | ||||
| 	std::vector<CGHeroInstance* > getHeroes(); | ||||
| 	std::vector<CGTownInstance* > getTowns(); | ||||
| 	const std::vector<const CGHeroInstance* > & getHeroes() const; | ||||
| 	const std::vector<const CGTownInstance* > & getTowns() const; | ||||
| 	const std::vector<CGHeroInstance* > & getHeroes(); | ||||
| 	const std::vector<CGTownInstance* > & getTowns(); | ||||
|  | ||||
| 	std::vector<const CGObjectInstance* > getOwnedObjects() const; | ||||
|  | ||||
| 	void addOwnedObject(CGObjectInstance * object); | ||||
| 	void removeOwnedObject(CGObjectInstance * object); | ||||
| 	void postDeserialize(); | ||||
|  | ||||
| 	bool checkVanquished() const | ||||
| 	{ | ||||
| @@ -145,6 +151,9 @@ public: | ||||
| 		h & enteredWinningCheatCode; | ||||
| 		h & static_cast<CBonusSystemNode&>(*this); | ||||
| 		h & destroyedObjects; | ||||
|  | ||||
| 		if (!h.saving) | ||||
| 			postDeserialize(); | ||||
| 	} | ||||
| }; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user