Răsfoiți Sursa

completed calls of functions during stack's move

mateuszb 17 ani în urmă
părinte
comite
137f62798b
4 a modificat fișierele cu 36 adăugiri și 7 ștergeri
  1. 13 2
      CBattleInterface.cpp
  2. 4 0
      CBattleInterface.h
  3. 1 1
      CCallback.cpp
  4. 18 4
      CGameState.cpp

+ 13 - 2
CBattleInterface.cpp

@@ -81,6 +81,7 @@ CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, C
 		int y = 86 + 42 * (h/17);
 		bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
 		bfield[h].accesible = true;
+		bfield[h].myInterface = this;
 	}
 	//locking occupied positions on batlefield
 	for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
@@ -287,9 +288,15 @@ void CBattleInterface::stackMoved(int number, int destHex)
 {
 }
 
+void CBattleInterface::hexLclicked(int whichOne)
+{
+	if((whichOne%17)!=0 && (whichOne%17)!=16)
+		LOCPLINT->cb->battleMoveCreature(activeStack, whichOne);
+}
+
 void CBattleInterface::showRange(SDL_Surface * to, int initialPlace, int radius)
 {
-	int * dists = new int[187]; //calculated distances
+	int dists[187]; //calculated distances
 	std::queue<int> hexq; //bfs queue
 	hexq.push(initialPlace);
 	for(int g=0; g<187; ++g)
@@ -446,7 +453,7 @@ void CBattleHex::hover(bool on)
 	Hoverable::hover(on);
 }
 
-CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false)
+CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL)
 {
 }
 
@@ -467,4 +474,8 @@ void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
 
 void CBattleHex::clickLeft(boost::logic::tribool down)
 {
+	if(!down && hovered && strictHovered) //we've been really clicked!
+	{
+		myInterface->hexLclicked(myNumber);
+	}
 }

+ 4 - 0
CBattleInterface.h

@@ -22,6 +22,8 @@ public:
 	~CBattleHero(); //d-tor
 };
 
+class CBattleInterface;
+
 class CBattleHex : public Hoverable, public MotionInterested, public ClickableL
 {
 public:
@@ -29,6 +31,7 @@ public:
 	bool accesible;
 	//CStack * ourStack;
 	bool hovered, strictHovered;
+	CBattleInterface * myInterface; //interface that owns me
 	static std::pair<int, int> getXYUnitAnim(int hexNum, bool attacker); //returns (x, y) of left top corner of animation
 	//for user interactions
 	void hover (bool on);
@@ -89,4 +92,5 @@ public:
 	void stackRemoved(CStack stack); //stack disappeared from batlefiled
 	void stackActivated(int number); //active stack has been changed
 	void stackMoved(int number, int destHex); //stack with id number moved to destHex
+	void hexLclicked(int whichOne); //hex only call-in
 };

+ 1 - 1
CCallback.cpp

@@ -562,7 +562,7 @@ CCreature CCallback::battleGetCreature(int number)
 bool CCallback::battleMoveCreature(int ID, int dest)
 {
 	//checking parameters
-	if(dest<0 || dest > 187 || ID<0 || ID>=CGI->state->curB->stacks.size())
+	if(dest<0 || dest > 187)
 		return false;
 	
 	return CGI->state->battleMoveCreatureStack(ID, dest); //everything finished successfully

+ 18 - 4
CGameState.cpp

@@ -210,13 +210,25 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
 
 bool CGameState::battleMoveCreatureStack(int ID, int dest)
 {
+	//selecting moved stack
+	CStack * curStack = NULL;
+	for(int y=0; y<curB->stacks.size(); ++y)
+	{
+		if(curB->stacks[y]->ID == ID)
+		{
+			curStack = curB->stacks[y];
+			break;
+		}
+	}
+	if(!curStack)
+		return false;
 	//initing necessary tables
 	bool accessibility[187]; //accesibility of hexes
 	for(int k=0; k<187; k++)
 		accessibility[k] = true;
-	for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
+	for(int g=0; g<curB->stacks.size(); ++g)
 	{
-		accessibility[CGI->state->curB->stacks[g]->position] = false;
+		accessibility[curB->stacks[g]->position] = false;
 	}
 	int predecessor[187]; //for getting the Path
 	for(int b=0; b<187; ++b)
@@ -224,7 +236,7 @@ bool CGameState::battleMoveCreatureStack(int ID, int dest)
 	//bfsing
 	int dists[187]; //calculated distances
 	std::queue<int> hexq; //bfs queue
-	hexq.push(CGI->state->curB->stacks[ID]->position);
+	hexq.push(curStack->position);
 	for(int g=0; g<187; ++g)
 		dists[g] = 100000000;
 	dists[hexq.front()] = 0;
@@ -277,9 +289,11 @@ bool CGameState::battleMoveCreatureStack(int ID, int dest)
 		}
 	}
 	//following the Path
+	if(dists[dest] > curStack->creature->speed)
+		return false;
 	std::vector<int> path;
 	int curElem = dest;
-	while(curElem!=CGI->state->curB->stacks[ID]->position)
+	while(curElem!=curStack->position)
 	{
 		path.push_back(curElem);
 		curElem = predecessor[curElem];