浏览代码

another piece of battles (with code reorganization)

mateuszb 17 年之前
父节点
当前提交
7b88f4a70b
共有 8 个文件被更改,包括 55 次插入19 次删除
  1. 24 9
      CBattleInterface.cpp
  2. 7 5
      CBattleInterface.h
  3. 12 0
      CCallback.cpp
  4. 3 0
      CCallback.h
  5. 4 1
      CGameState.cpp
  6. 2 1
      CGameState.h
  7. 2 2
      CPlayerInterface.cpp
  8. 1 1
      CPlayerInterface.h

+ 24 - 9
CBattleInterface.cpp

@@ -10,20 +10,22 @@
 #include "CGameState.h"
 
 extern SDL_Surface * screen;
+SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
 
-CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CCallback * callback, CGHeroInstance *hero1, CGHeroInstance *hero2, const std::vector< CStack* > & stcks) 
-: printCellBorders(true), cb(callback), stacks(stcks), attackingHeroInstance(hero1), defendingHeroInstance(hero2)
+CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2) 
+: printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2)
 {
 	//initializing armies
 	this->army1 = army1;
 	this->army2 = army2;
-	for(int b=0; b<stacks.size(); ++b)
+	std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
+	for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
 	{
-		creAnims.push_back(new CCreatureAnimation(stacks[b]->creature->animDefName));
-		creAnims[b]->setType(2);
+		creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
+		creAnims[b->second.ID]->setType(2);
 	}
 	//preparing menu background and terrain
-	std::vector< std::string > & backref = CGI->mh->battleBacks[ cb->battleGetBattlefieldType() ];
+	std::vector< std::string > & backref = CGI->mh->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
 	background = CGI->bitmaph->loadBitmap(backref[ rand() % backref.size()] );
 	menu = CGI->bitmaph->loadBitmap("CBAR.BMP");
 	CSDL_Ext::blueToPlayersAdv(menu, hero1->tempOwner);
@@ -188,10 +190,11 @@ void CBattleInterface::show(SDL_Surface * to)
 		defendingHero->show(to);
 
 	//showing units //a lot of work...
-	for(int j=0; j<creAnims.size(); ++j)
+	std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
+	for(std::map<int, CCreatureAnimation*>::iterator j=creAnims.begin(); j!=creAnims.end(); ++j)
 	{
-		std::pair <int, int> coords = CBattleHex::getXYUnitAnim(stacks[j]->position, stacks[j]->owner == attackingHeroInstance->tempOwner);
-		creAnims[j]->nextFrame(to, coords.first, coords.second, stacks[j]->owner == attackingHeroInstance->tempOwner);
+		std::pair <int, int> coords = CBattleHex::getXYUnitAnim(stacks[j->first].position, stacks[j->first].owner == attackingHeroInstance->tempOwner);
+		j->second->nextFrame(to, coords.first, coords.second, stacks[j->first].owner == attackingHeroInstance->tempOwner);
 	}
 	//units shown
 
@@ -245,6 +248,18 @@ void CBattleInterface::bConsoleDownf()
 {
 }
 
+void CBattleInterface::newStack(CStack stack)
+{
+	creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
+	creAnims[stack.ID]->setType(2);
+}
+
+void CBattleInterface::stackRemoved(CStack stack)
+{
+	delete creAnims[stack.ID];
+	creAnims.erase(stack.ID);
+}
+
 void CBattleHero::show(SDL_Surface *to)
 {
 	int tick=-1;

+ 7 - 5
CBattleInterface.h

@@ -49,21 +49,19 @@ private:
 	AdventureMapButton<CBattleInterface> * bOptions, * bSurrender, * bFlee, * bAutofight, * bSpell,
 		* bWait, * bDefence, * bConsoleUp, * bConsoleDown;
 	CBattleHero * attackingHero, * defendingHero;
-	SDL_Surface * cellBorder, * cellShade;
 	CCreatureSet * army1, * army2; //fighting armies
 	CGHeroInstance * attackingHeroInstance, * defendingHeroInstance;
-	std::vector< CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order like in BattleInfo's stacks)
+	std::map< int, CCreatureAnimation * > creAnims; //animations of creatures from fighting armies (order like in BattleInfo's stacks)
 
-	CCallback * cb; //our callback for getting info about different things
-	const std::vector< CStack* > & stacks; //fighting stacks
 public:
-	CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CCallback * callback, CGHeroInstance *hero1, CGHeroInstance *hero2, const std::vector< CStack* > & stcks); //c-tor
+	CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2); //c-tor
 	~CBattleInterface(); //d-tor
 
 	//std::vector<TimeInterested*> timeinterested; //animation handling
 	bool printCellBorders; //if true, cell borders will be printed
 	CBattleHex bfield[187]; //11 lines, 17 hexes on each
 	std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
+	static SDL_Surface * cellBorder, * cellShade;
 
 	//button handle funcs:
 	void bOptionsf();
@@ -80,4 +78,8 @@ public:
 	void activate();
 	void deactivate();
 	void show(SDL_Surface * to);
+
+	//call-ins
+	void newStack(CStack stack);
+	void stackRemoved(CStack stack);
 };

+ 12 - 0
CCallback.cpp

@@ -12,7 +12,9 @@
 #include "hch/CGeneralTextHandler.h"
 #include "CAdvmapInterface.h"
 #include "CPlayerInterface.h"
+
 LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
+
 int CCallback::lowestSpeed(CGHeroInstance * chi)
 {
 	int min = 150;
@@ -518,6 +520,16 @@ int CCallback::battleGetBattlefieldType()
 	return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].terType;
 }
 
+std::map<int, CStack> CCallback::battleGetStacks()
+{
+	std::map<int, CStack> ret;
+	for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
+	{
+		ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
+	}
+	return ret;
+}
+
 int3 CScriptCallback::getPos(CGObjectInstance * ob)
 {
 	return ob->pos;

+ 3 - 0
CCallback.h

@@ -3,6 +3,8 @@
 
 #include "mapHandler.h"
 #include "tchar.h"
+#include "CGameState.h"
+
 class CGameState;
 class CPath;
 class CGObjectInstance;
@@ -96,6 +98,7 @@ public:
 	int battleGetStack(int pos); //returns ID of stack on the tile
 	int battleGetPos(int stack); //returns position (tile ID) of stack
 	int battleMakeAction(Action* action);//perform action with an active stack (or custom action)
+	std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
 	
 
 //friends

+ 4 - 1
CGameState.cpp

@@ -29,7 +29,10 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
 	curB->side2=(hero2)?(hero2->tempOwner):(-1);
 	curB->round = -2;
 	for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
+	{
 		stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size()));
+		stacks[stacks.size()-1]->ID = stacks.size()-1;
+	}
 	//initialization of positions
 	switch(army1->slots.size()) //for attacker
 	{
@@ -149,7 +152,7 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
 				return; //no witnesses
 			if(CGI->playerint[j->second.serial]->human)
 			{
-				((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side, curB->stacks);
+				((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
 			}
 			else
 			{

+ 2 - 1
CGameState.h

@@ -40,13 +40,14 @@ struct BattleInfo
 class CStack
 {
 public:
-	int ID; //type of creature
+	int ID; //unique ID of stack
 	CCreature * creature;
 	int amount;
 	int owner;
 	int position; //position on battlefield
 	bool alive; //true if it is alive
 	CStack(CCreature * C, int A, int O, int I):creature(C),amount(A),owner(O), alive(true), position(-1), ID(I){};
+	CStack() : creature(NULL),amount(-1),owner(255), alive(true), position(-1), ID(-1){};
 };
 
 class CGameState

+ 2 - 2
CPlayerInterface.cpp

@@ -1850,10 +1850,10 @@ void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
 	}
 }
 
-void CPlayerInterface::battleStart(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, tribool side, std::vector< CStack* > & stacks) //called by engine when battle starts; side=0 - left, side=1 - right
+void CPlayerInterface::battleStart(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, tribool side) //called by engine when battle starts; side=0 - left, side=1 - right
  {
 	curint->deactivate();
-	curint = new CBattleInterface(army1,army2,cb,hero1,hero2,stacks);
+	curint = new CBattleInterface(army1,army2,hero1,hero2);
 	curint->activate();
 	LOCPLINT->objsToBlit.push_back(dynamic_cast<IShowable*>(curint));
 }

+ 1 - 1
CPlayerInterface.h

@@ -304,7 +304,7 @@ public:
 	void garrisonChanged(const CGObjectInstance * obj);
 
 	//battles
-	void battleStart(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, tribool side, std::vector< CStack* > & stacks); //called by engine when battle starts; side=0 - left, side=1 - right
+	void battleStart(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, tribool side); //called by engine when battle starts; side=0 - left, side=1 - right
 	void battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles); //called when battlefield is prepared, prior the battle beginning
 	void battleNewRound(int round); //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
 	void actionStarted(Action action);//occurs BEFORE every action taken by any stack or by the hero