Browse Source

almost complete close range attack (only choosing direction is to be done)

mateuszb 17 years ago
parent
commit
4f0ce359f6
8 changed files with 107 additions and 22 deletions
  1. 73 6
      CBattleInterface.cpp
  2. 7 3
      CBattleInterface.h
  3. 12 1
      CCallback.cpp
  4. 2 0
      CCallback.h
  5. 4 4
      CGameInterface.h
  6. 3 2
      CGameState.cpp
  7. 4 4
      CPlayerInterface.cpp
  8. 2 2
      CPlayerInterface.h

+ 73 - 6
CBattleInterface.cpp

@@ -370,7 +370,7 @@ void CBattleInterface::stackRemoved(CStack stack)
 	creAnims.erase(stack.ID);
 }
 
-void CBattleInterface::stackKilled(int ID)
+void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby)
 {
 	creAnims[ID]->setType(5); //death
 	for(int i=0; i<creAnims[ID]->framesInGroup(5)-1; ++i)
@@ -379,6 +379,8 @@ void CBattleInterface::stackKilled(int ID)
 		CSDL_Ext::update();
 		SDL_framerateDelay(LOCPLINT->mainFPSmng);
 	}
+	
+	printConsoleAttacked(ID, dmg, killed, IDby);
 }
 
 void CBattleInterface::stackActivated(int number)
@@ -563,7 +565,7 @@ void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, boo
 	creAnims[number]->pos.y = coords.second;
 }
 
-void CBattleInterface::stackIsAttacked(int ID)
+void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby)
 {
 	creAnims[ID]->setType(3); //getting hit
 	for(int i=0; i<creAnims[ID]->framesInGroup(3); ++i)
@@ -573,6 +575,8 @@ void CBattleInterface::stackIsAttacked(int ID)
 		SDL_framerateDelay(LOCPLINT->mainFPSmng);
 	}
 	creAnims[ID]->setType(2);
+
+	printConsoleAttacked(ID, dmg, killed, IDby);
 }
 
 void CBattleInterface::stackAttacking(int ID, int dest)
@@ -831,6 +835,29 @@ void CBattleInterface::attackingShowHelper()
 	}
 }
 
+void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
+{
+	char tabh[200];
+	CStack attacker = LOCPLINT->cb->battleGetStackByID(IDby);
+	CStack defender = LOCPLINT->cb->battleGetStackByID(ID);
+	int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
+		(attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
+		dmg);
+	if(killed > 0)
+	{
+		if(killed > 1)
+		{
+			sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
+		}
+		else //killed == 1
+		{
+			sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
+		}
+	}
+
+	console->addText(std::string(tabh));
+}
+
 void CBattleHero::show(SDL_Surface *to)
 {
 	//animation of flag
@@ -966,9 +993,14 @@ void CBattleHex::hover(bool on)
 {
 	hovered = on;
 	Hoverable::hover(on);
+	if(!on && setAlterText)
+	{
+		myInterface->console->alterTxt = std::string();
+		setAlterText = false;
+	}
 }
 
-CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL)
+CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
 {
 }
 
@@ -985,6 +1017,26 @@ void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
 			strictHovered = true;
 		}
 	}
+
+	if(hovered && strictHovered) //print attacked creature to console
+	{
+		if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
+			LOCPLINT->cb->battleGetStackByPos(myNumber).owner != LOCPLINT->playerID &&
+			LOCPLINT->cb->battleGetStackByPos(myNumber).alive)
+		{
+			char tabh[160];
+			CStack attackedStack = LOCPLINT->cb->battleGetStackByPos(myNumber);
+			std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
+			sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
+			myInterface->console->alterTxt = std::string(tabh);
+			setAlterText = true;
+		}
+	}
+	else if(setAlterText)
+	{
+		myInterface->console->alterTxt = std::string();
+		setAlterText = false;
+	}
 }
 
 void CBattleHex::clickLeft(boost::logic::tribool down)
@@ -1020,7 +1072,7 @@ void CBattleHex::clickRight(boost::logic::tribool down)
 	}
 }
 
-CBattleConsole::CBattleConsole() : lastShown(-1)
+CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
 {
 }
 
@@ -1031,7 +1083,11 @@ CBattleConsole::~CBattleConsole()
 
 void CBattleConsole::show(SDL_Surface * to)
 {
-	if(texts.size())
+	if(alterTxt.size())
+	{
+		CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
+	}
+	else if(texts.size())
 	{
 		if(texts.size()==1)
 		{
@@ -1049,7 +1105,18 @@ bool CBattleConsole::addText(std::string text)
 {
 	if(text.size()>70)
 		return false; //text too long!
-	texts.push_back(text);
+	int firstInToken = 0;
+	for(int i=0; i<text.size(); ++i) //tokenize
+	{
+		if(text[i] == 10)
+		{
+			texts.push_back( text.substr(firstInToken, i-firstInToken) );
+			firstInToken = i+1;
+			lastShown++;
+		}
+	}
+
+	texts.push_back( text.substr(firstInToken, text.size()) );
 	lastShown++;
 	return true;
 }

+ 7 - 3
CBattleInterface.h

@@ -26,6 +26,8 @@ class CBattleInterface;
 
 class CBattleHex : public Hoverable, public MotionInterested, public ClickableL, public ClickableR
 {
+private:
+	bool setAlterText; //if true, this hex has set alternative text in console and will clean it
 public:
 	unsigned int myNumber;
 	bool accesible;
@@ -53,8 +55,9 @@ class CBattleConsole : public IShowable, public CIntObject
 {
 private:
 	std::vector< std::string > texts; //a place where texts are stored
-	int lastShown; //las shown line of text
+	int lastShown; //last shown line of text
 public:
+	std::string alterTxt; //if it's not empty, this text is displayed
 	CBattleConsole(); //c-tor
 	~CBattleConsole(); //d-tor
 	void show(SDL_Surface * to = 0);
@@ -90,6 +93,7 @@ private:
 		bool reversing;
 	} * attackingInfo;
 	void attackingShowHelper();
+	void printConsoleAttacked(int ID, int dmg, int killed, int IDby);
 
 public:
 	CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2); //c-tor
@@ -122,10 +126,10 @@ public:
 	//call-ins
 	void newStack(CStack stack); //new stack appeared on battlefield
 	void stackRemoved(CStack stack); //stack disappeared from batlefiled
-	void stackKilled(int ID); //stack has been killed (but corpses remain)
+	void stackKilled(int ID, int dmg, int killed, int IDby); //stack has been killed (but corpses remain)
 	void stackActivated(int number); //active stack has been changed
 	void stackMoved(int number, int destHex, bool startMoving, bool endMoving); //stack with id number moved to destHex
-	void stackIsAttacked(int ID); //called when stack id attacked
+	void stackIsAttacked(int ID, int dmg, int killed, int IDby); //called when stack id attacked by stack with id IDby
 	void stackAttacking(int ID, int dest); //called when stack with id ID is attacking something on hex dest
 	void newRound(int number); //caled when round is ended; number is the number of round
 	void hexLclicked(int whichOne); //hex only call-in

+ 12 - 1
CCallback.cpp

@@ -765,7 +765,13 @@ int CCallback::battleGetStack(int pos)
 {
 	for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
 	{
-		if(CGI->state->curB->stacks[g]->position == pos)
+		if(CGI->state->curB->stacks[g]->position == pos ||
+				( CGI->state->curB->stacks[g]->creature->isDoubleWide() && 
+					( (CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position-1 == pos) || 
+						(!CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position+1 == pos)
+					) 
+				)
+			)
 			return CGI->state->curB->stacks[g]->ID;
 	}
 	return -1;
@@ -781,6 +787,11 @@ CStack CCallback::battleGetStackByID(int ID)
 	return CStack();
 }
 
+CStack CCallback::battleGetStackByPos(int pos)
+{
+	return battleGetStackByID(battleGetStack(pos));
+}
+
 int CCallback::battleGetPos(int stack)
 {
 	for(int g=0; g<CGI->state->curB->stacks.size(); ++g)

+ 2 - 0
CCallback.h

@@ -60,6 +60,7 @@ public:
 	virtual int battleGetObstaclesAtTile(int tile)=0; //returns bitfield 
 	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 CStack battleGetStackByPos(int pos)=0; //returns stack info by given pos
 	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 std::map<int, CStack> battleGetStacks()=0; //returns stacks on battlefield
@@ -131,6 +132,7 @@ public:
 	int battleGetObstaclesAtTile(int tile); //returns bitfield 
 	int battleGetStack(int pos); //returns ID of stack on the tile
 	CStack battleGetStackByID(int ID); //returns stack info by given ID
+	CStack battleGetStackByPos(int pos); //returns stack info by given pos
 	int battleGetPos(int stack); //returns position (tile ID) of stack
 	//int battleMakeAction(BattleAction* action);//perform action with an active stack (or custom action)
 	std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield

+ 4 - 4
CGameInterface.h

@@ -72,8 +72,8 @@ public:
 	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;
 	virtual void battleStackAttacking(int ID, int dest)=0;
-	virtual void battleStackIsAttacked(int ID)=0;
-	virtual void battleStackKilled(int ID)=0;
+	virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby)=0;
+	virtual void battleStackKilled(int ID, int dmg, int killed, int IDby)=0;
 	//
 
 };
@@ -91,8 +91,8 @@ public:
 	virtual void heroCreated(const CGHeroInstance*){};
 	virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving){};
 	virtual void battleStackAttacking(int ID, int dest){};
-	virtual void battleStackIsAttacked(int ID){};
-	virtual void battleStackKilled(int ID){};
+	virtual void battleStackIsAttacked(int ID, int dmg, int killed, int IDby){};
+	virtual void battleStackKilled(int ID, int dmg, int killed, int IDby){};
 	virtual BattleAction activeStack(int stackID) {BattleAction ba; ba.actionType = 3; ba.stackNumber = stackID; return ba;};
 };
 #endif //CGAMEINTERFACE_H

+ 3 - 2
CGameState.cpp

@@ -474,16 +474,17 @@ bool CGameState::battleMoveCreatureStack(int ID, int dest)
 				curB->stacks[numberOfStackAtEnd]->firstHPleft -= damageFirst;
 			}
 
+			int cresInstackBefore = curB->stacks[numberOfStackAtEnd]->amount; 
 			curB->stacks[numberOfStackAtEnd]->amount -= cresKilled;
 			if(curB->stacks[numberOfStackAtEnd]->amount<=0) //stack killed
 			{
 				curB->stacks[numberOfStackAtEnd]->amount = 0;
-				LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID);
+				LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore) , ID);
 				curB->stacks[numberOfStackAtEnd]->alive = false;
 			}
 			else
 			{
-				LOCPLINT->battleStackIsAttacked(curB->stacks[numberOfStackAtEnd]->ID);
+				LOCPLINT->battleStackIsAttacked(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore), ID);
 			}
 
 			//damage applied

+ 4 - 4
CPlayerInterface.cpp

@@ -2078,14 +2078,14 @@ void CPlayerInterface::battleStackAttacking(int ID, int dest)
 	dynamic_cast<CBattleInterface*>(curint)->stackAttacking(ID, dest);
 }
 
-void CPlayerInterface::battleStackIsAttacked(int ID)
+void CPlayerInterface::battleStackIsAttacked(int ID, int dmg, int killed, int IDby)
 {
-	dynamic_cast<CBattleInterface*>(curint)->stackIsAttacked(ID);
+	dynamic_cast<CBattleInterface*>(curint)->stackIsAttacked(ID, dmg, killed, IDby);
 }
 
-void CPlayerInterface::battleStackKilled(int ID)
+void CPlayerInterface::battleStackKilled(int ID, int dmg, int killed, int IDby)
 {
-	dynamic_cast<CBattleInterface*>(curint)->stackKilled(ID);
+	dynamic_cast<CBattleInterface*>(curint)->stackKilled(ID, dmg, killed, IDby);
 }
 
 void CPlayerInterface::showComp(SComponent comp)

+ 2 - 2
CPlayerInterface.h

@@ -341,8 +341,8 @@ public:
 	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);
-	void battleStackIsAttacked(int ID);
-	void battleStackKilled(int ID);
+	void battleStackIsAttacked(int ID, int dmg, int killed, int IDby);
+	void battleStackKilled(int ID, int dmg, int killed, int IDby);
 
 
 	//-------------//