浏览代码

Better handling of after-battle callbacks. Looks messy, but works.

DjWarmonger 14 年之前
父节点
当前提交
096e77333f
共有 2 个文件被更改,包括 44 次插入20 次删除
  1. 43 20
      server/CGameHandler.cpp
  2. 1 0
      server/CGameHandler.h

+ 43 - 20
server/CGameHandler.cpp

@@ -264,20 +264,41 @@ void CGameHandler::levelUpHero(int ID)
 		hlu.skills.push_back(hero->type->heroClass->chooseSecSkill(basicAndAdv)); //upgrade existing
 	}
 
-	if(hlu.skills.size() > 1) //apply and ask for secondary skill
+	if (hero->exp >= VLC->heroh->reqExp(hero->level+2)) // more level-ups
 	{
-		boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(callWith<ui16>,hlu.skills,boost::function<void(ui16)>(boost::bind(&CGameHandler::levelUpHero,this,ID,_1)),_1));
-		applyAndAsk(&hlu,hero->tempOwner,callback); //call levelUpHero when client responds
-	}
-	else if(hlu.skills.size() == 1) //apply, give only possible skill  and send info
-	{
-		sendAndApply(&hlu);
-		levelUpHero(ID, hlu.skills.back());
+		if(hlu.skills.size() > 1) //apply and ask for secondary skill
+		{
+			boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(callWith<ui16>,hlu.skills,boost::function<void(ui16)>(boost::bind(&CGameHandler::levelUpHero,this,ID,_1)),_1));
+			applyAndAsk(&hlu,hero->tempOwner,callback); //call levelUpHero when client responds
+		}
+		else if(hlu.skills.size() == 1) //apply, give only possible skill  and send info
+		{
+			sendAndApply(&hlu);
+			levelUpHero(ID, hlu.skills.back());
+		}
+		else //apply and send info
+		{
+			sendAndApply(&hlu);
+			levelUpHero(ID);
+		}
 	}
-	else //apply and send info
+	else //call function after battle was finished and all exp given
 	{
-		sendAndApply(&hlu);
-		levelUpHero(ID);
+		boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(callWith<ui16>,hlu.skills,boost::function<void(ui16)>(boost::bind(&CGameHandler::afterBattleCallback,this,_1)),_1));
+		if(hlu.skills.size() > 1) //apply and ask for secondary skill
+		{
+			applyAndAsk(&hlu, hero->tempOwner, callback); //call levelUpHero when client responds
+		}
+		else if(hlu.skills.size() == 1) //apply, give only possible skill  and send info
+		{
+			applyAndAsk(&hlu, hero->tempOwner, callback); //call levelUpHero when client responds
+			levelUpHero(ID, hlu.skills.back());
+		}
+		else //apply and send info
+		{
+			applyAndAsk(&hlu, hero->tempOwner, callback); //call levelUpHero when client responds
+			levelUpHero(ID);
+		}
 	}
 }
 
@@ -437,14 +458,6 @@ void CGameHandler::endBattle(int3 tile, const CGHeroInstance *hero1, const CGHer
 
 	sendAndApply(&resultsApplied);
 
-	if(battleEndCallback && *battleEndCallback) //TODO: object interaction after level dialog is handled
-	{
-		(*battleEndCallback)(battleResult.data);
-		delete battleEndCallback;
-		battleEndCallback = 0;
-	}
-
-
 	if(duel)
 	{
 		CSaveFile resultFile("result.vdrst");
@@ -500,10 +513,20 @@ void CGameHandler::endBattle(int3 tile, const CGHeroInstance *hero1, const CGHer
 
 		sendAndApply(&sah);
 	}
+	if(!(battleEndCallback && *battleEndCallback))
+		delete battleResult.data;
+}
 
+void CGameHandler::afterBattleCallback(int ID) //object interaction after leveling up is done
+{
+	if(battleEndCallback && *battleEndCallback)
+	{
+		(*battleEndCallback)(battleResult.data);
+		delete battleEndCallback;
+		battleEndCallback = 0;
+	}
 	delete battleResult.data;
 }
-
 void CGameHandler::prepareAttack(BattleAttack &bat, const CStack *att, const CStack *def, int distance, int targetHex)
 {
 	bat.bsa.clear();

+ 1 - 0
server/CGameHandler.h

@@ -184,6 +184,7 @@ public:
 	void vistiCastleObjects (const CGTownInstance *t, const CGHeroInstance *h);
 	void levelUpHero(int ID, int skill);//handle client respond and send one more request if needed 
 	void levelUpHero(int ID);//initial call - check if hero have remaining levelups & handle them
+	void afterBattleCallback(int ID); // called after level-ups are finished, ID is just temporarily for compatibility
 	//////////////////////////////////////////////////////////////////////////
 
 	void commitPackage(CPackForClient *pack) OVERRIDE;