1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

a bit of escaping battlefield (to be finished)

This commit is contained in:
mateuszb
2008-04-15 16:52:31 +00:00
parent 063d2279ee
commit 54f94cf5ef
8 changed files with 60 additions and 64 deletions

View File

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

View File

@ -72,7 +72,7 @@ public:
CBattleHex bfield[187]; //11 lines, 17 hexes on each CBattleHex bfield[187]; //11 lines, 17 hexes on each
std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield std::vector< CBattleObstacle * > obstacles; //vector of obstacles on the battlefield
static SDL_Surface * cellBorder, * cellShade; 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: //button handle funcs:
void bOptionsf(); void bOptionsf();

View File

@ -676,19 +676,6 @@ int CCallback::battleGetPos(int stack)
return -1; 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> CCallback::battleGetStacks()
{ {
std::map<int, CStack> ret; std::map<int, CStack> ret;
@ -709,15 +696,6 @@ CCreature CCallback::battleGetCreature(int number)
throw new std::exception("Cannot find the creature"); 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) std::vector<int> CCallback::battleGetAvailableHexes(int ID)
{ {
return CGI->state->battleGetRange(ID); return CGI->state->battleGetRange(ID);

View File

@ -49,10 +49,10 @@ public:
virtual int battleGetStack(int pos)=0; //returns ID of stack on the tile 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 battleGetStackByID(int ID)=0; //returns stack info by given ID
virtual int battleGetPos(int stack)=0; //returns position (tile ID) of stack 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 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 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 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 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 int battleGetStack(int pos); //returns ID of stack on the tile
CStack battleGetStackByID(int ID); //returns stack info by given ID CStack battleGetStackByID(int ID); //returns stack info by given ID
int battleGetPos(int stack); //returns position (tile ID) of stack 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 std::map<int, CStack> battleGetStacks(); //returns stacks on battlefield
CCreature battleGetCreature(int number); //returns type of creature by given number of stack 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 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 bool battleIsStackMine(int ID); //returns true if stack with id ID belongs to caller

View File

@ -16,6 +16,7 @@ class CGHeroInstance;
class CGTownInstance; class CGTownInstance;
class CGObjectInstance; class CGObjectInstance;
class CCreatureSet; class CCreatureSet;
class CArmedInstance;
class CObstacle 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 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 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 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 BattleAction activeStack(int stackID)=0; //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 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 battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)=0;
// //
@ -75,5 +76,6 @@ public:
virtual void heroKilled(const CGHeroInstance*){}; virtual void heroKilled(const CGHeroInstance*){};
virtual void heroCreated(const CGHeroInstance*){}; virtual void heroCreated(const CGHeroInstance*){};
virtual void battleStackMoved(int ID, int dest, bool startMoving, bool endMoving){}; 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 #endif //CGAMEINTERFACE_H

View File

@ -189,7 +189,7 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
{ {
curB->activeStack = i; curB->activeStack = i;
curB->stackActionPerformed = false; 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 owner = (stacks[i]->owner)?(hero2 ? hero2->tempOwner : 255):(hero1->tempOwner);
unsigned char serialOwner = -1; unsigned char serialOwner = -1;
@ -206,7 +206,26 @@ void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, C
} }
else if(CGI->playerint[serialOwner]->human) 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 else
{ {
@ -265,7 +284,7 @@ bool CGameState::battleMoveCreatureStack(int ID, int dest)
accessibility[k] = true; accessibility[k] = true;
for(int g=0; g<curB->stacks.size(); ++g) 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; accessibility[curB->stacks[g]->position] = false;
if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature 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(); int curHex = hexq.front();
hexq.pop(); hexq.pop();
curNext = curHex - ( (curHex/17)%2 ? 18 : 17 ); 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); hexq.push(curNext);
dists[curNext] = dists[curHex] + 1; dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex; predecessor[curNext] = curHex;
} }
curNext = curHex - ( (curHex/17)%2 ? 17 : 16 ); 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); hexq.push(curNext);
dists[curNext] = dists[curHex] + 1; dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex; predecessor[curNext] = curHex;
} }
curNext = curHex - 1; 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); hexq.push(curNext);
dists[curNext] = dists[curHex] + 1; dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex; predecessor[curNext] = curHex;
} }
curNext = curHex + 1; 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); hexq.push(curNext);
dists[curNext] = dists[curHex] + 1; dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex; predecessor[curNext] = curHex;
} }
curNext = curHex + ( (curHex/17)%2 ? 16 : 17 ); 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); hexq.push(curNext);
dists[curNext] = dists[curHex] + 1; dists[curNext] = dists[curHex] + 1;
predecessor[curNext] = curHex; predecessor[curNext] = curHex;
} }
curNext = curHex + ( (curHex/17)%2 ? 17 : 18 ); 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); hexq.push(curNext);
dists[curNext] = dists[curHex] + 1; dists[curNext] = dists[curHex] + 1;

View File

@ -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 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; unsigned char showCount = 0;
dynamic_cast<CBattleInterface*>(curint)->stackActivated(stackID); 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; ++showCount;
SDL_Event sEvent; 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_Delay(1); //give time for other apps
SDL_framerateDelay(mainFPSmng); 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) void CPlayerInterface::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)

View File

@ -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 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 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 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 BattleAction 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); 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 battleStackMoved(int ID, int dest, bool startMoving, bool endMoving);
void battleStackAttacking(int ID, int dest); void battleStackAttacking(int ID, int dest);