瀏覽代碼

invalidate bonus caching on update

Henning Koehler 8 年之前
父節點
當前提交
f867b1a37e
共有 2 個文件被更改,包括 19 次插入9 次删除
  1. 16 6
      lib/HeroBonus.cpp
  2. 3 3
      lib/HeroBonus.h

+ 16 - 6
lib/HeroBonus.cpp

@@ -318,13 +318,15 @@ void BonusList::eliminateDuplicates()
 	bonuses.erase( unique( bonuses.begin(), bonuses.end() ), bonuses.end() );
 }
 
-void BonusList::updateBonuses(const CBonusSystemNode & context)
+bool BonusList::updateBonuses(const CBonusSystemNode & context)
 {
+	bool updated = false;
 	for(std::shared_ptr<Bonus> b : *this)
 	{
 		if(b->updater)
-			b->updater->update(*b, context);
+			updated = b->updater->update(*b, context) || updated;
 	}
+	return updated;
 }
 
 void BonusList::push_back(std::shared_ptr<Bonus> x)
@@ -808,8 +810,10 @@ void CBonusSystemNode::reduceBonusDurations(const CSelector &s)
 
 void CBonusSystemNode::updateBonuses()
 {
-	bonuses.updateBonuses(*this);
-	exportedBonuses.updateBonuses(*this);
+	bool updated = bonuses.updateBonuses(*this);
+	updated = exportedBonuses.updateBonuses(*this) || updated;
+	if(updated)
+		treeHasChanged();
 }
 
 void CBonusSystemNode::addNewBonus(const std::shared_ptr<Bonus>& b)
@@ -1559,15 +1563,21 @@ void LimiterList::add( TLimiterPtr limiter )
 	limiters.push_back(limiter);
 }
 
-void ScalingUpdater::update(Bonus & b, const CBonusSystemNode & context)
+bool ScalingUpdater::update(Bonus & b, const CBonusSystemNode & context)
 {
 	if(context.getNodeType() == CBonusSystemNode::HERO)
 	{
 		int level = static_cast<const CGHeroInstance &>(context).level;
 		int steps = stepSize ? level / stepSize : level;
 		//rounding follows format for HMM3 creature specialty bonus
-		b.val = (valPer20 * steps + 19) / 20;
+		int newVal = (valPer20 * steps + 19) / 20;
+		if(b.val != newVal)
+		{
+			b.val = newVal;
+			return true;
+		}
 	}
+	return false;
 }
 
 std::shared_ptr<Bonus> Bonus::addUpdater(TUpdaterPtr Updater)

+ 3 - 3
lib/HeroBonus.h

@@ -485,7 +485,7 @@ public:
 	int valOfBonuses(const CSelector &select) const;
 
 	void eliminateDuplicates();
-	void updateBonuses(const CBonusSystemNode & node);
+	bool updateBonuses(const CBonusSystemNode & node);
 
 	// remove_if implementation for STL vector types
 	template <class Predicate>
@@ -1019,7 +1019,7 @@ void BonusList::insert(const int position, InputIterator first, InputIterator la
 class DLL_LINKAGE IUpdater
 {
 public:
-	virtual void update(Bonus & b, const CBonusSystemNode & context) const = 0;
+	virtual bool update(Bonus & b, const CBonusSystemNode & context) const = 0;
 
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
@@ -1038,5 +1038,5 @@ struct DLL_LINKAGE ScalingUpdater : public IUpdater
 		h & stepSize;
 	}
 
-	void update(Bonus & b, const CBonusSystemNode & context);
+	bool update(Bonus & b, const CBonusSystemNode & context);
 };