ソースを参照

renamed & reused CBonusSystemNode::updateBonuses; tweaked updater signature

Henning Koehler 8 年 前
コミット
f1fd00f005
4 ファイル変更30 行追加18 行削除
  1. 21 6
      lib/HeroBonus.cpp
  2. 6 9
      lib/HeroBonus.h
  3. 2 2
      lib/NetPacksLib.cpp
  4. 1 1
      lib/battle/BattleInfo.cpp

+ 21 - 6
lib/HeroBonus.cpp

@@ -318,6 +318,15 @@ void BonusList::eliminateDuplicates()
 	bonuses.erase( unique( bonuses.begin(), bonuses.end() ), bonuses.end() );
 }
 
+void BonusList::updateBonuses(const CBonusSystemNode & context)
+{
+	for(std::shared_ptr<Bonus> b : *this)
+	{
+		if(b->updater)
+			b->updater->update(*b, context);
+	}
+}
+
 void BonusList::push_back(std::shared_ptr<Bonus> x)
 {
 	bonuses.push_back(x);
@@ -782,7 +791,7 @@ void CBonusSystemNode::popBonuses(const CSelector &s)
 		child->popBonuses(s);
 }
 
-void CBonusSystemNode::updateBonuses(const CSelector &s)
+void CBonusSystemNode::reduceBonusDurations(const CSelector &s)
 {
 	BonusList bl;
 	exportedBonuses.getBonuses(bl, s, Selector::all);
@@ -794,7 +803,13 @@ void CBonusSystemNode::updateBonuses(const CSelector &s)
 	}
 
 	for(CBonusSystemNode *child : children)
-		child->updateBonuses(s);
+		child->reduceBonusDurations(s);
+}
+
+void CBonusSystemNode::updateBonuses()
+{
+	bonuses.updateBonuses(*this);
+	exportedBonuses.updateBonuses(*this);
 }
 
 void CBonusSystemNode::addNewBonus(const std::shared_ptr<Bonus>& b)
@@ -1544,14 +1559,14 @@ void LimiterList::add( TLimiterPtr limiter )
 	limiters.push_back(limiter);
 }
 
-void ScalingUpdater::update(BonusUpdateContext & context)
+void ScalingUpdater::update(Bonus & b, const CBonusSystemNode & context)
 {
-	if(context.node.getNodeType() == CBonusSystemNode::HERO)
+	if(context.getNodeType() == CBonusSystemNode::HERO)
 	{
-		int level = static_cast<const CGHeroInstance &>(context.node).level;
+		int level = static_cast<const CGHeroInstance &>(context).level;
 		int steps = stepSize ? level / stepSize : level;
 		//rounding follows format for HMM3 creature specialty bonus
-		context.b->val = (valPer20 * steps + 19) / 20;
+		b.val = (valPer20 * steps + 19) / 20;
 	}
 }
 

+ 6 - 9
lib/HeroBonus.h

@@ -485,6 +485,7 @@ public:
 	int valOfBonuses(const CSelector &select) const;
 
 	void eliminateDuplicates();
+	void updateBonuses(const CBonusSystemNode & node);
 
 	// remove_if implementation for STL vector types
 	template <class Predicate>
@@ -707,7 +708,9 @@ public:
 	///removes bonuses by selector
 	void popBonuses(const CSelector &s);
 	///updates count of remaining turns and removes outdated bonuses by selector
-	void updateBonuses(const CSelector &s);
+	void reduceBonusDurations(const CSelector &s);
+	//run update decorators
+	void updateBonuses();
 	virtual std::string bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const {return "";}; //description or bonus name
 	virtual std::string nodeName() const;
 
@@ -1013,16 +1016,10 @@ void BonusList::insert(const int position, InputIterator first, InputIterator la
 
 // bonus decorators for updating bonuses based on events (e.g. hero gaining level)
 
-struct BonusUpdateContext
-{
-	std::shared_ptr<Bonus> b;
-	const CBonusSystemNode & node;
-};
-
 class DLL_LINKAGE IUpdater
 {
 public:
-	virtual void update(BonusUpdateContext & context) const = 0;
+	virtual void update(Bonus & b, const CBonusSystemNode & context) const = 0;
 
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
@@ -1041,5 +1038,5 @@ struct DLL_LINKAGE ScalingUpdater : public IUpdater
 		h & stepSize;
 	}
 
-	void update(BonusUpdateContext & context);
+	void update(Bonus & b, const CBonusSystemNode & context);
 };

+ 2 - 2
lib/NetPacksLib.cpp

@@ -1085,8 +1085,8 @@ DLL_LINKAGE void NewTurn::applyGs(CGameState *gs)
 
 	// Update bonuses before doing anything else so hero don't get more MP than needed
 	gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
-	gs->globalEffects.updateBonuses(Bonus::NDays);
-	gs->globalEffects.updateBonuses(Bonus::OneWeek);
+	gs->globalEffects.reduceBonusDurations(Bonus::NDays);
+	gs->globalEffects.reduceBonusDurations(Bonus::OneWeek);
 	//TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
 
 	for(NewTurn::Hero h : heroes) //give mana/movement point

+ 1 - 1
lib/battle/BattleInfo.cpp

@@ -765,7 +765,7 @@ void BattleInfo::nextRound(int32_t roundNr)
 	for(CStack * s : stacks)
 	{
 		// new turn effects
-		s->updateBonuses(Bonus::NTurns);
+		s->reduceBonusDurations(Bonus::NTurns);
 
 		s->afterNewRound();
 	}