diff --git a/CBattleInterface.cpp b/CBattleInterface.cpp index c47b93343..5e4efbb2d 100644 --- a/CBattleInterface.cpp +++ b/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::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 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); + } } diff --git a/CBattleInterface.h b/CBattleInterface.h index 5c404ae0b..a68c33361 100644 --- a/CBattleInterface.h +++ b/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 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 }; diff --git a/CCallback.cpp b/CCallback.cpp index 964792421..4515fae84 100644 --- a/CCallback.cpp +++ b/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 diff --git a/CGameState.cpp b/CGameState.cpp index cc68d6524..813f28820 100644 --- a/CGameState.cpp +++ b/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; ystacks.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; gstate->curB->stacks.size(); ++g) + for(int g=0; gstacks.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 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 path; int curElem = dest; - while(curElem!=CGI->state->curB->stacks[ID]->position) + while(curElem!=curStack->position) { path.push_back(curElem); curElem = predecessor[curElem];