Переглянути джерело

a bit of escaping battlefield (to be finished)

mateuszb 17 роки тому
батько
коміт
54f94cf5ef
8 змінених файлів з 60 додано та 64 видалено
  1. 10 20
      CBattleInterface.cpp
  2. 1 1
      CBattleInterface.h
  3. 0 22
      CCallback.cpp
  4. 4 4
      CCallback.h
  5. 4 2
      CGameInterface.h
  6. 28 9
      CGameState.cpp
  7. 11 4
      CPlayerInterface.cpp
  8. 2 2
      CPlayerInterface.h

+ 10 - 20
CBattleInterface.cpp

@@ -17,7 +17,7 @@ extern SDL_Color zwykly;
 SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
 
 CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2) 
-: printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), curStackActed(false)
+: printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL)
 {
 	//initializing armies
 	this->army1 = army1;
@@ -117,6 +117,8 @@ CBattleInterface::~CBattleInterface()
 {
 	SDL_FreeSurface(background);
 	SDL_FreeSurface(menu);
+	SDL_FreeSurface(amountBasic);
+	SDL_FreeSurface(amountNormal);
 	delete bOptions;
 	delete bSurrender;
 	delete bFlee;
@@ -298,17 +300,9 @@ void CBattleInterface::bSurrenderf()
 
 void CBattleInterface::bFleef()
 {
-	for(int i=0; i<LOCPLINT->objsToBlit.size(); ++i)
-	{
-		if( dynamic_cast<CBattleInterface*>( LOCPLINT->objsToBlit[i] ) )
-		{
-			LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+i);
-		}
-	}
-	deactivate();
-
-	LOCPLINT->adventureInt->activate();
-	delete this;
+	BattleAction * ba = new BattleAction;
+	ba->actionType = 4;
+	givenCommand = ba;
 }
 
 void CBattleInterface::bAutofightf()
@@ -328,7 +322,7 @@ void CBattleInterface::bDefencef()
 	BattleAction * ba = new BattleAction;
 	ba->actionType = 3;
 	ba->stackNumber = activeStack;
-	LOCPLINT->cb->battleMakeAction(ba);
+	givenCommand = ba;
 }
 
 void CBattleInterface::bConsoleUpf()
@@ -354,7 +348,7 @@ void CBattleInterface::stackRemoved(CStack stack)
 
 void CBattleInterface::stackActivated(int number)
 {
-	curStackActed = false;
+	givenCommand = NULL;
 	activeStack = number;
 }
 
@@ -462,17 +456,13 @@ void CBattleInterface::stackAttacking(int ID, int dest)
 
 void CBattleInterface::hexLclicked(int whichOne)
 {
-	if(!LOCPLINT->cb->battleIsStackMine(LOCPLINT->cb->battleGetStack(whichOne))) //if player is trying to attack eney unit
+	if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
 	{
 		BattleAction * ba = new BattleAction(); //to be deleted by engine
 		ba->actionType = 6;
 		ba->destinationTile = whichOne;
 		ba->stackNumber = activeStack;
-		LOCPLINT->cb->battleMakeAction(ba);
-	}
-	if((whichOne%17)!=0 && (whichOne%17)!=16)
-	{
-		LOCPLINT->cb->battleMoveCreature(activeStack, whichOne);
+		givenCommand = ba;
 	}
 }
 

+ 1 - 1
CBattleInterface.h

@@ -72,7 +72,7 @@ public:
 	CBattleHex bfield[187]; //11 lines, 17 hexes on each
 	std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
 	static SDL_Surface * cellBorder, * cellShade;
-	bool curStackActed; //true if we have i.e. moved current unit
+	BattleAction * givenCommand; //true if we have i.e. moved current unit
 
 	//button handle funcs:
 	void bOptionsf();

+ 0 - 22
CCallback.cpp

@@ -676,19 +676,6 @@ int CCallback::battleGetPos(int stack)
 	return -1;
 }
 
-int CCallback::battleMakeAction(BattleAction* action)
-{
-	switch(action->actionType)
-	{
-	case 3: //defend
-		return battleMoveCreature(action->stackNumber, battleGetPos(action->stackNumber));
-	case 6: //move or attack
-		return battleMoveCreature(action->stackNumber, action->destinationTile);
-	}
-	delete action;
-	return -1; //error
-}
-
 std::map<int, CStack> CCallback::battleGetStacks()
 {
 	std::map<int, CStack> ret;
@@ -709,15 +696,6 @@ CCreature CCallback::battleGetCreature(int number)
 	throw new std::exception("Cannot find the creature");
 }
 
-bool CCallback::battleMoveCreature(int ID, int dest)
-{
-	//checking parameters
-	if(dest<0 || dest > 187)
-		return false;
-	
-	return CGI->state->battleMoveCreatureStack(ID, dest); //everything finished successfully
-}
-
 std::vector<int> CCallback::battleGetAvailableHexes(int ID)
 {
 	return CGI->state->battleGetRange(ID);

+ 4 - 4
CCallback.h

@@ -49,10 +49,10 @@ public:
 	virtual int battleGetStack(int pos)=0; //returns ID of stack on the tile
 	virtual CStack battleGetStackByID(int ID)=0; //returns stack info by given ID
 	virtual int battleGetPos(int stack)=0; //returns position (tile ID) of stack
-	virtual int battleMakeAction(BattleAction* action)=0;//perform action with an active stack (or custom action)
+	//virtual int battleMakeAction(BattleAction* action)=0;//perform action with an active stack (or custom action)
 	virtual std::map<int, CStack> battleGetStacks()=0; //returns stacks on battlefield
 	virtual CCreature battleGetCreature(int number)=0; //returns type of creature by given number of stack
-	virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible
+	//virtual bool battleMoveCreature(int ID, int dest)=0; //moves creature with id ID to dest if possible
 	virtual std::vector<int> battleGetAvailableHexes(int ID)=0; //reutrns numbers of hexes reachable by creature with id ID
 	virtual bool battleIsStackMine(int ID)=0; //returns true if stack with id ID belongs to caller
 };
@@ -115,10 +115,10 @@ public:
 	int battleGetStack(int pos); //returns ID of stack on the tile
 	CStack battleGetStackByID(int ID); //returns stack info by given ID
 	int battleGetPos(int stack); //returns position (tile ID) of stack
-	int battleMakeAction(BattleAction* action);//perform action with an active stack (or custom action)
+	//int battleMakeAction(BattleAction* action);//perform action with an active stack (or custom action)
 	std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
 	CCreature battleGetCreature(int number); //returns type of creature by given number of stack
-	bool battleMoveCreature(int ID, int dest); //moves creature with id ID to dest if possible
+	//bool battleMoveCreature(int ID, int dest); //moves creature with id ID to dest if possible
 	std::vector<int> battleGetAvailableHexes(int ID); //reutrns numbers of hexes reachable by creature with id ID
 	bool battleIsStackMine(int ID); //returns true if stack with id ID belongs to caller
 	

+ 4 - 2
CGameInterface.h

@@ -16,6 +16,7 @@ class CGHeroInstance;
 class CGTownInstance;
 class CGObjectInstance;
 class CCreatureSet;
+class CArmedInstance;
 
 class CObstacle
 {
@@ -56,8 +57,8 @@ public:
 	virtual void battleNewRound(int round){}; //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
 	virtual void actionStarted(BattleAction action){};//occurs BEFORE every action taken by any stack or by the hero
 	virtual void actionFinished(BattleAction action){};//occurs AFTER every action taken by any stack or by the hero
-	virtual void activeStack(int stackID){}; //called when it's turn of that stack
-	virtual void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner){};
+	virtual BattleAction activeStack(int stackID)=0; //called when it's turn of that stack
+	virtual void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner){};
 	virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)=0;
 	//
 
@@ -75,5 +76,6 @@ public:
 	virtual void heroKilled(const CGHeroInstance*){};
 	virtual void heroCreated(const CGHeroInstance*){};
 	virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving){};
+	virtual BattleAction activeStack(int stackID) {BattleAction ba; ba.actionType = 3; ba.stackNumber = stackID; return ba;};
 };
 #endif //CGAMEINTERFACE_H

+ 28 - 9
CGameState.cpp

@@ -189,7 +189,7 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
 		{
 			curB->activeStack = i;
 			curB->stackActionPerformed = false;
-			if(stacks[i]->alive) //niech interfejs ruszy oddzialem
+			if(stacks[i]->alive) //indicate posiibility of making action for this unit
 			{
 				unsigned char owner = (stacks[i]->owner)?(hero2 ? hero2->tempOwner : 255):(hero1->tempOwner);
 				unsigned char serialOwner = -1;
@@ -206,7 +206,26 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
 				}
 				else if(CGI->playerint[serialOwner]->human)
 				{
-					((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
+					BattleAction ba = ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
+					switch(ba.actionType)
+					{
+					case 3: //defend
+						{
+							break;
+						}
+					case 4: //retreat/flee
+						{
+							for(int v=0; v<CGI->playerint.size(); ++v) //tell about the end of this battle to interfaces
+								CGI->playerint[v]->battleEnd(army1, army2, hero1, hero2, std::vector<int>(), 0, false);
+							return; //return from this function, I hope it'll work
+							break;
+						}
+					case 6: //walk or attack
+						{
+							battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
+							break;
+						}
+					}
 				}
 				else
 				{
@@ -265,7 +284,7 @@ bool CGameState::battleMoveCreatureStack(int ID, int dest)
 		accessibility[k] = true;
 	for(int g=0; g<curB->stacks.size(); ++g)
 	{
-		if(curB->stacks[g]->owner == owner) //we don't want to lock enemy's positions
+		//if(curB->stacks[g]->owner == owner) //we don't want to lock enemy's positions
 		{
 			accessibility[curB->stacks[g]->position] = false;
 			if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
@@ -293,42 +312,42 @@ bool CGameState::battleMoveCreatureStack(int ID, int dest)
 		int curHex = hexq.front();
 		hexq.pop();
 		curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
-		if((curNext > 0) && accessibility[curNext]  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
+		if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
 		{
 			hexq.push(curNext);
 			dists[curNext] = dists[curHex] + 1;
 			predecessor[curNext] = curHex;
 		}
 		curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
-		if((curNext > 0) && accessibility[curNext]  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
+		if((curNext > 0) && (accessibility[curNext] || curNext==dest)  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
 		{
 			hexq.push(curNext);
 			dists[curNext] = dists[curHex] + 1;
 			predecessor[curNext] = curHex;
 		}
 		curNext = curHex - 1;
-		if((curNext > 0) && accessibility[curNext]  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
+		if((curNext > 0) && (accessibility[curNext] || curNext==dest)  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
 		{
 			hexq.push(curNext);
 			dists[curNext] = dists[curHex] + 1;
 			predecessor[curNext] = curHex;
 		}
 		curNext = curHex + 1;
-		if((curNext < 187) && accessibility[curNext]  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
+		if((curNext < 187) && (accessibility[curNext] || curNext==dest)  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
 		{
 			hexq.push(curNext);
 			dists[curNext] = dists[curHex] + 1;
 			predecessor[curNext] = curHex;
 		}
 		curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
-		if((curNext < 187) && accessibility[curNext]  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
+		if((curNext < 187) && (accessibility[curNext] || curNext==dest)  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
 		{
 			hexq.push(curNext);
 			dists[curNext] = dists[curHex] + 1;
 			predecessor[curNext] = curHex;
 		}
 		curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
-		if((curNext < 187) && accessibility[curNext]  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
+		if((curNext < 187) && (accessibility[curNext] || curNext==dest)  && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
 		{
 			hexq.push(curNext);
 			dists[curNext] = dists[curHex] + 1;

+ 11 - 4
CPlayerInterface.cpp

@@ -1945,14 +1945,14 @@ void CPlayerInterface::actionStarted(BattleAction action)//occurs BEFORE every a
 
 void CPlayerInterface::actionFinished(BattleAction action)//occurs AFTER every action taken by any stack or by the hero
 {
-	dynamic_cast<CBattleInterface*>(curint)->curStackActed = true;
+	//dynamic_cast<CBattleInterface*>(curint)->givenCommand = -1;
 }
 
-void CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
+BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
 {
 	unsigned char showCount = 0;
 	dynamic_cast<CBattleInterface*>(curint)->stackActivated(stackID);
-	while(!dynamic_cast<CBattleInterface*>(curint)->curStackActed) //while current unit can perform an action
+	while(!dynamic_cast<CBattleInterface*>(curint)->givenCommand) //while current unit can perform an action
 	{
 		++showCount;
 		SDL_Event sEvent;
@@ -1980,10 +1980,17 @@ void CPlayerInterface::activeStack(int stackID) //called when it's turn of that
 		SDL_Delay(1); //give time for other apps
 		SDL_framerateDelay(mainFPSmng);
 	}
+	BattleAction ret = *(dynamic_cast<CBattleInterface*>(curint)->givenCommand);
+	delete dynamic_cast<CBattleInterface*>(curint)->givenCommand;
+	dynamic_cast<CBattleInterface*>(curint)->givenCommand = NULL;
+	return ret;
 }
 
-void CPlayerInterface::battleEnd(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner)
+void CPlayerInterface::battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner)
 {
+	dynamic_cast<CBattleInterface*>(curint)->deactivate();
+	delete dynamic_cast<CBattleInterface*>(curint);
+	curint = NULL;
 }
 
 void CPlayerInterface::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)

+ 2 - 2
CPlayerInterface.h

@@ -332,8 +332,8 @@ public:
 	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(BattleAction action);//occurs BEFORE every action taken by any stack or by the hero
 	void actionFinished(BattleAction action);//occurs AFTER every action taken by any stack or by the hero
-	void activeStack(int stackID); //called when it's turn of that stack
-	void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
+	BattleAction activeStack(int stackID); //called when it's turn of that stack
+	void battleEnd(CCreatureSet * army1, CCreatureSet * army2, CArmedInstance *hero1, CArmedInstance *hero2, std::vector<int> capturedArtifacts, int expForWinner, bool winner);
 	void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
 	void battleStackAttacking(int ID, int dest);