mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Merge pull request #4520 from IvanSavenko/market_fix
Remove marketModes as member
This commit is contained in:
		| @@ -199,7 +199,7 @@ void CAltarArtifacts::onSlotClickPressed(const std::shared_ptr<CTradeableItem> & | |||||||
|  |  | ||||||
| 	if(const auto pickedArtInst = heroArts->getPickedArtifact()) | 	if(const auto pickedArtInst = heroArts->getPickedArtifact()) | ||||||
| 	{ | 	{ | ||||||
| 		if(pickedArtInst->canBePutAt(altarArtifactsStorage.get())) | 		if(pickedArtInst->canBePutAt(altarArtifactsStorage)) | ||||||
| 		{ | 		{ | ||||||
| 			if(pickedArtInst->artType->isTradable()) | 			if(pickedArtInst->artType->isTradable()) | ||||||
| 			{ | 			{ | ||||||
|   | |||||||
| @@ -26,7 +26,7 @@ public: | |||||||
| 	void putBackArtifacts(); | 	void putBackArtifacts(); | ||||||
|  |  | ||||||
| private: | private: | ||||||
| 	std::shared_ptr<CArtifactSet> altarArtifactsStorage; | 	const CArtifactSet * altarArtifactsStorage; | ||||||
| 	std::shared_ptr<CButton> sacrificeBackpackButton; | 	std::shared_ptr<CButton> sacrificeBackpackButton; | ||||||
| 	std::shared_ptr<CArtifactsOfHeroAltar> heroArts; | 	std::shared_ptr<CArtifactsOfHeroAltar> heroArts; | ||||||
| 	std::map<std::shared_ptr<CTradeableItem>, const CArtifactInstance*> tradeSlotsMap; | 	std::map<std::shared_ptr<CTradeableItem>, const CArtifactInstance*> tradeSlotsMap; | ||||||
|   | |||||||
| @@ -284,7 +284,7 @@ CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc) | |||||||
| 	else if(auto market = getMarket(loc.artHolder)) | 	else if(auto market = getMarket(loc.artHolder)) | ||||||
| 	{ | 	{ | ||||||
| 		if(auto artSet = market->getArtifactsStorage()) | 		if(auto artSet = market->getArtifactsStorage()) | ||||||
| 			return artSet.get(); | 			return artSet; | ||||||
| 	} | 	} | ||||||
| 	else if(auto army = getArmyInstance(loc.artHolder)) | 	else if(auto army = getArmyInstance(loc.artHolder)) | ||||||
| 	{ | 	{ | ||||||
|   | |||||||
| @@ -243,7 +243,6 @@ CGMarket * MarketInstanceConstructor::createObject(IGameCallback * cb) const | |||||||
|  |  | ||||||
| void MarketInstanceConstructor::initializeObject(CGMarket * market) const | void MarketInstanceConstructor::initializeObject(CGMarket * market) const | ||||||
| { | { | ||||||
| 	market->addMarketMode(marketModes); |  | ||||||
| 	market->marketEfficiency = marketEfficiency; | 	market->marketEfficiency = marketEfficiency; | ||||||
| 	 | 	 | ||||||
| 	if(auto university = dynamic_cast<CGUniversity*>(market)) | 	if(auto university = dynamic_cast<CGUniversity*>(market)) | ||||||
| @@ -257,6 +256,11 @@ void MarketInstanceConstructor::initializeObject(CGMarket * market) const | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | const std::set<EMarketMode> & MarketInstanceConstructor::availableModes() const | ||||||
|  | { | ||||||
|  | 	return marketModes; | ||||||
|  | } | ||||||
|  |  | ||||||
| void MarketInstanceConstructor::randomizeObject(CGMarket * object, vstd::RNG & rng) const | void MarketInstanceConstructor::randomizeObject(CGMarket * object, vstd::RNG & rng) const | ||||||
| { | { | ||||||
| 	JsonRandom randomizer(object->cb); | 	JsonRandom randomizer(object->cb); | ||||||
|   | |||||||
| @@ -14,6 +14,7 @@ | |||||||
|  |  | ||||||
| #include "../mapObjects/MiscObjects.h" | #include "../mapObjects/MiscObjects.h" | ||||||
| #include "../mapObjects/CGCreature.h" | #include "../mapObjects/CGCreature.h" | ||||||
|  | #include "../mapObjects/CGHeroInstance.h" | ||||||
| #include "../mapObjects/ObstacleSetHandler.h" | #include "../mapObjects/ObstacleSetHandler.h" | ||||||
|  |  | ||||||
| VCMI_LIB_NAMESPACE_BEGIN | VCMI_LIB_NAMESPACE_BEGIN | ||||||
| @@ -127,6 +128,8 @@ public: | |||||||
| 	void initializeObject(CGMarket * object) const override; | 	void initializeObject(CGMarket * object) const override; | ||||||
| 	void randomizeObject(CGMarket * object, vstd::RNG & rng) const override; | 	void randomizeObject(CGMarket * object, vstd::RNG & rng) const override; | ||||||
|  |  | ||||||
|  | 	const std::set<EMarketMode> & availableModes() const; | ||||||
|  |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| VCMI_LIB_NAMESPACE_END | VCMI_LIB_NAMESPACE_END | ||||||
|   | |||||||
| @@ -19,6 +19,7 @@ | |||||||
| #include "../CSkillHandler.h" | #include "../CSkillHandler.h" | ||||||
| #include "../mapObjectConstructors/AObjectTypeHandler.h" | #include "../mapObjectConstructors/AObjectTypeHandler.h" | ||||||
| #include "../mapObjectConstructors/CObjectClassesHandler.h" | #include "../mapObjectConstructors/CObjectClassesHandler.h" | ||||||
|  | #include "../mapObjectConstructors/CommonConstructors.h" | ||||||
| #include "../networkPacks/PacksForClient.h" | #include "../networkPacks/PacksForClient.h" | ||||||
|  |  | ||||||
| VCMI_LIB_NAMESPACE_BEGIN | VCMI_LIB_NAMESPACE_BEGIN | ||||||
| @@ -48,6 +49,14 @@ int CGMarket::availableUnits(EMarketMode mode, int marketItemSerial) const | |||||||
| 	return -1; | 	return -1; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | std::set<EMarketMode> CGMarket::availableModes() const | ||||||
|  | { | ||||||
|  | 	const auto & baseHandler = getObjectHandler(); | ||||||
|  | 	const auto & ourHandler = std::dynamic_pointer_cast<MarketInstanceConstructor>(baseHandler); | ||||||
|  |  | ||||||
|  | 	return ourHandler->availableModes(); | ||||||
|  | } | ||||||
|  |  | ||||||
| CGMarket::CGMarket(IGameCallback *cb): | CGMarket::CGMarket(IGameCallback *cb): | ||||||
| 	CGObjectInstance(cb) | 	CGObjectInstance(cb) | ||||||
| {} | {} | ||||||
|   | |||||||
| @@ -29,11 +29,17 @@ public: | |||||||
| 	ObjectInstanceID getObjInstanceID() const override; | 	ObjectInstanceID getObjInstanceID() const override; | ||||||
| 	int getMarketEfficiency() const override; | 	int getMarketEfficiency() const override; | ||||||
| 	int availableUnits(EMarketMode mode, int marketItemSerial) const override; //-1 if unlimited | 	int availableUnits(EMarketMode mode, int marketItemSerial) const override; //-1 if unlimited | ||||||
|  | 	std::set<EMarketMode> availableModes() const override; | ||||||
|  |  | ||||||
| 	template <typename Handler> void serialize(Handler &h) | 	template <typename Handler> void serialize(Handler &h) | ||||||
| 	{ | 	{ | ||||||
| 		h & static_cast<CGObjectInstance&>(*this); | 		h & static_cast<CGObjectInstance&>(*this); | ||||||
| 		h & static_cast<IMarket&>(*this); | 		if (h.version < Handler::Version::NEW_MARKETS) | ||||||
|  | 		{ | ||||||
|  | 			std::set<EMarketMode> marketModes; | ||||||
|  | 			h & marketModes; | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		h & marketEfficiency; | 		h & marketEfficiency; | ||||||
| 		if (h.version < Handler::Version::NEW_MARKETS) | 		if (h.version < Handler::Version::NEW_MARKETS) | ||||||
| 		{ | 		{ | ||||||
| @@ -47,7 +53,6 @@ public: | |||||||
| 	template <typename Handler> void serializeArtifactsAltar(Handler &h) | 	template <typename Handler> void serializeArtifactsAltar(Handler &h) | ||||||
| 	{ | 	{ | ||||||
| 		serialize(h); | 		serialize(h); | ||||||
| 		IMarket::serializeArtifactsAltar(h); |  | ||||||
| 	} | 	} | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -938,20 +938,19 @@ void CGTownInstance::addBuilding(const BuildingID & buildingID) | |||||||
| 	if(buildingID == BuildingID::NONE) | 	if(buildingID == BuildingID::NONE) | ||||||
| 		return; | 		return; | ||||||
|  |  | ||||||
| 	const auto townType = (*VLC->townh)[getFaction()]->town; | 	builtBuildings.insert(buildingID); | ||||||
| 	if(const auto & building = townType->buildings.find(buildingID); building != townType->buildings.end()) |  | ||||||
| 	{ |  | ||||||
| 		builtBuildings.insert(buildingID); |  | ||||||
| 		addMarketMode(building->second->marketModes); |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void CGTownInstance::postDeserializeMarketFix() | std::set<EMarketMode> CGTownInstance::availableModes() const | ||||||
| { | { | ||||||
| 	// re-add all buildings to recreate existing market modes | 	std::set<EMarketMode> result; | ||||||
| 	auto buildingsBak = builtBuildings; | 	for (const auto & buildingID : builtBuildings) | ||||||
| 	for (auto building : buildingsBak) | 	{ | ||||||
| 		addBuilding(building); | 		const auto * buildingPtr = town->buildings.at(buildingID).get(); | ||||||
|  | 		result.insert(buildingPtr->marketModes.begin(), buildingPtr->marketModes.end()); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return result; | ||||||
| } | } | ||||||
|  |  | ||||||
| void CGTownInstance::removeBuilding(const BuildingID & buildingID) | void CGTownInstance::removeBuilding(const BuildingID & buildingID) | ||||||
| @@ -959,17 +958,12 @@ void CGTownInstance::removeBuilding(const BuildingID & buildingID) | |||||||
| 	if(!vstd::contains(builtBuildings, buildingID)) | 	if(!vstd::contains(builtBuildings, buildingID)) | ||||||
| 		return; | 		return; | ||||||
|  |  | ||||||
| 	if(const auto & building = town->buildings.find(buildingID); building != town->buildings.end()) | 	builtBuildings.erase(buildingID); | ||||||
| 	{ |  | ||||||
| 		builtBuildings.erase(buildingID); |  | ||||||
| 		removeMarketMode(building->second->marketModes); |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void CGTownInstance::removeAllBuildings() | void CGTownInstance::removeAllBuildings() | ||||||
| { | { | ||||||
| 	builtBuildings.clear(); | 	builtBuildings.clear(); | ||||||
| 	removeAllMarketModes(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| std::set<BuildingID> CGTownInstance::getBuildings() const | std::set<BuildingID> CGTownInstance::getBuildings() const | ||||||
|   | |||||||
| @@ -77,9 +77,6 @@ public: | |||||||
| 	template <typename Handler> void serialize(Handler &h) | 	template <typename Handler> void serialize(Handler &h) | ||||||
| 	{ | 	{ | ||||||
| 		h & static_cast<CGDwelling&>(*this); | 		h & static_cast<CGDwelling&>(*this); | ||||||
| 		if (h.version >= Handler::Version::NEW_MARKETS) |  | ||||||
| 			h & static_cast<IMarket&>(*this); |  | ||||||
|  |  | ||||||
| 		h & nameTextId; | 		h & nameTextId; | ||||||
| 		h & built; | 		h & built; | ||||||
| 		h & destroyed; | 		h & destroyed; | ||||||
| @@ -118,9 +115,6 @@ public: | |||||||
| 			town = faction ? faction->town : nullptr; | 			town = faction ? faction->town : nullptr; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if (!h.saving && h.version < Handler::Version::NEW_MARKETS) |  | ||||||
| 			postDeserializeMarketFix(); |  | ||||||
|  |  | ||||||
| 		h & townAndVis; | 		h & townAndVis; | ||||||
| 		BONUS_TREE_DESERIALIZATION_FIX | 		BONUS_TREE_DESERIALIZATION_FIX | ||||||
|  |  | ||||||
| @@ -140,7 +134,6 @@ public: | |||||||
| 	void updateMoraleBonusFromArmy() override; | 	void updateMoraleBonusFromArmy() override; | ||||||
| 	void deserializationFix(); | 	void deserializationFix(); | ||||||
| 	void postDeserialize(); | 	void postDeserialize(); | ||||||
| 	void postDeserializeMarketFix(); |  | ||||||
| 	void recreateBuildingsBonuses(); | 	void recreateBuildingsBonuses(); | ||||||
| 	void setVisitingHero(CGHeroInstance *h); | 	void setVisitingHero(CGHeroInstance *h); | ||||||
| 	void setGarrisonedHero(CGHeroInstance *h); | 	void setGarrisonedHero(CGHeroInstance *h); | ||||||
| @@ -160,6 +153,7 @@ public: | |||||||
| 	EGeneratorState shipyardStatus() const override; | 	EGeneratorState shipyardStatus() const override; | ||||||
| 	const IObjectInterface * getObject() const override; | 	const IObjectInterface * getObject() const override; | ||||||
| 	int getMarketEfficiency() const override; //=market count | 	int getMarketEfficiency() const override; //=market count | ||||||
|  | 	std::set<EMarketMode> availableModes() const override; | ||||||
| 	std::vector<TradeItemBuy> availableItemsIds(EMarketMode mode) const override; | 	std::vector<TradeItemBuy> availableItemsIds(EMarketMode mode) const override; | ||||||
| 	ObjectInstanceID getObjInstanceID() const override; | 	ObjectInstanceID getObjInstanceID() const override; | ||||||
| 	void updateAppearance(); | 	void updateAppearance(); | ||||||
|   | |||||||
| @@ -22,7 +22,7 @@ VCMI_LIB_NAMESPACE_BEGIN | |||||||
|  |  | ||||||
| bool IMarket::allowsTrade(const EMarketMode mode) const | bool IMarket::allowsTrade(const EMarketMode mode) const | ||||||
| { | { | ||||||
| 	return vstd::contains(marketModes, mode); | 	return vstd::contains(availableModes(), mode); | ||||||
| } | } | ||||||
|  |  | ||||||
| bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const | bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const | ||||||
| @@ -140,42 +140,19 @@ int IMarket::availableUnits(const EMarketMode mode, const int marketItemSerial) | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void IMarket::addMarketMode(const EMarketMode mode) | IMarket::IMarket() | ||||||
|  | 	:altarArtifactsStorage(std::make_unique<CArtifactSetAltar>()) | ||||||
| { | { | ||||||
| 	marketModes.insert(mode); |  | ||||||
|  |  | ||||||
| 	if(mode == EMarketMode::ARTIFACT_EXP) |  | ||||||
| 		altarArtifactsStorage = std::make_shared<CArtifactSetAltar>(); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void IMarket::addMarketMode(const std::set<EMarketMode> & modes) | IMarket::~IMarket() = default; | ||||||
| { |  | ||||||
| 	for(const auto & mode : modes) |  | ||||||
| 		addMarketMode(mode); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void IMarket::removeMarketMode(const EMarketMode mode) | CArtifactSet * IMarket::getArtifactsStorage() const | ||||||
| { | { | ||||||
| 	marketModes.erase(mode); | 	if (availableModes().count(EMarketMode::ARTIFACT_EXP)) | ||||||
|  | 		return altarArtifactsStorage.get(); | ||||||
| 	if(mode == EMarketMode::ARTIFACT_EXP) | 	else | ||||||
| 		altarArtifactsStorage.reset(); | 		return nullptr; | ||||||
| } |  | ||||||
|  |  | ||||||
| void IMarket::removeMarketMode(const std::set<EMarketMode> & modes) |  | ||||||
| { |  | ||||||
| 	for(const auto & mode : modes) |  | ||||||
| 		removeMarketMode(mode); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void IMarket::removeAllMarketModes() |  | ||||||
| { |  | ||||||
| 	marketModes.clear(); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| std::shared_ptr<CArtifactSet> IMarket::getArtifactsStorage() const |  | ||||||
| { |  | ||||||
| 	return altarArtifactsStorage; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| std::vector<TradeItemBuy> IMarket::availableItemsIds(const EMarketMode mode) const | std::vector<TradeItemBuy> IMarket::availableItemsIds(const EMarketMode mode) const | ||||||
| @@ -192,9 +169,4 @@ std::vector<TradeItemBuy> IMarket::availableItemsIds(const EMarketMode mode) con | |||||||
| 	return ret; | 	return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| std::set<EMarketMode> IMarket::availableModes() const |  | ||||||
| { |  | ||||||
| 	return marketModes; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| VCMI_LIB_NAMESPACE_END | VCMI_LIB_NAMESPACE_END | ||||||
|   | |||||||
| @@ -15,9 +15,12 @@ | |||||||
|  |  | ||||||
| VCMI_LIB_NAMESPACE_BEGIN | VCMI_LIB_NAMESPACE_BEGIN | ||||||
|  |  | ||||||
| class DLL_LINKAGE IMarket : public virtual Serializeable | class DLL_LINKAGE IMarket : public virtual Serializeable, boost::noncopyable | ||||||
| { | { | ||||||
| public: | public: | ||||||
|  | 	IMarket(); | ||||||
|  | 	~IMarket(); | ||||||
|  |  | ||||||
| 	class CArtifactSetAltar : public CArtifactSet | 	class CArtifactSetAltar : public CArtifactSet | ||||||
| 	{ | 	{ | ||||||
| 	public: | 	public: | ||||||
| @@ -29,36 +32,12 @@ public: | |||||||
| 	virtual bool allowsTrade(const EMarketMode mode) const; | 	virtual bool allowsTrade(const EMarketMode mode) const; | ||||||
| 	virtual int availableUnits(const EMarketMode mode, const int marketItemSerial) const; //-1 if unlimited | 	virtual int availableUnits(const EMarketMode mode, const int marketItemSerial) const; //-1 if unlimited | ||||||
| 	virtual std::vector<TradeItemBuy> availableItemsIds(const EMarketMode mode) const; | 	virtual std::vector<TradeItemBuy> availableItemsIds(const EMarketMode mode) const; | ||||||
| 	void addMarketMode(const EMarketMode mode); | 	virtual std::set<EMarketMode> availableModes() const = 0; | ||||||
| 	void addMarketMode(const std::set<EMarketMode> & modes); | 	CArtifactSet * getArtifactsStorage() const; | ||||||
| 	void removeMarketMode(const EMarketMode mode); |  | ||||||
| 	void removeMarketMode(const std::set<EMarketMode> & modes); |  | ||||||
| 	void removeAllMarketModes(); |  | ||||||
| 	std::set<EMarketMode> availableModes() const; |  | ||||||
| 	std::shared_ptr<CArtifactSet> getArtifactsStorage() const; |  | ||||||
| 	bool getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const; //val1 - how many units of id1 player has to give to receive val2 units | 	bool getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const; //val1 - how many units of id1 player has to give to receive val2 units | ||||||
|  |  | ||||||
| 	template <typename Handler> void serialize(Handler & h) |  | ||||||
| 	{ |  | ||||||
| 		h & marketModes; |  | ||||||
|  |  | ||||||
| 		if(vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP)) |  | ||||||
| 		{ |  | ||||||
| 			if (!h.saving) |  | ||||||
| 				altarArtifactsStorage = std::make_shared<CArtifactSetAltar>(); |  | ||||||
|  |  | ||||||
| 			h & *altarArtifactsStorage; |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	template <typename Handler> void serializeArtifactsAltar(Handler & h) |  | ||||||
| 	{ |  | ||||||
| 		h & *altarArtifactsStorage; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| private: | private: | ||||||
| 	std::shared_ptr<CArtifactSetAltar> altarArtifactsStorage; | 	std::unique_ptr<CArtifactSetAltar> altarArtifactsStorage; | ||||||
| 	std::set<EMarketMode> marketModes; |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| VCMI_LIB_NAMESPACE_END | VCMI_LIB_NAMESPACE_END | ||||||
|   | |||||||
| @@ -2198,7 +2198,6 @@ CGObjectInstance * CMapLoaderH3M::readTown(const int3 & position, std::shared_pt | |||||||
| 	bool hasCustomBuildings = reader->readBool(); | 	bool hasCustomBuildings = reader->readBool(); | ||||||
| 	if(hasCustomBuildings) | 	if(hasCustomBuildings) | ||||||
| 	{ | 	{ | ||||||
| 		object->subID = faction.value(); |  | ||||||
| 		std::set<BuildingID> builtBuildings; | 		std::set<BuildingID> builtBuildings; | ||||||
| 		reader->readBitmaskBuildings(builtBuildings, faction); | 		reader->readBitmaskBuildings(builtBuildings, faction); | ||||||
| 		for(const auto & building : builtBuildings) | 		for(const auto & building : builtBuildings) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user